* dwarf2out.c (compare_loc_descriptor, scompare_loc_descriptor,
[official-gcc.git] / gcc / ada / exp_ch4.adb
blobfa1ad4f4459e813ae683baf44fe58b64c58a97ce
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-2010, 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_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Fixd; use Exp_Fixd;
40 with Exp_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Exp_VFpt; use Exp_VFpt;
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_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Sinfo; use Sinfo;
67 with Snames; use Snames;
68 with Stand; use Stand;
69 with SCIL_LL; use SCIL_LL;
70 with Targparm; use Targparm;
71 with Tbuild; use Tbuild;
72 with Ttypes; use Ttypes;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
75 with Validsw; use Validsw;
77 package body Exp_Ch4 is
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Binary_Op_Validity_Checks (N : Node_Id);
84 pragma Inline (Binary_Op_Validity_Checks);
85 -- Performs validity checks for a binary operator
87 procedure Build_Boolean_Array_Proc_Call
88 (N : Node_Id;
89 Op1 : Node_Id;
90 Op2 : Node_Id);
91 -- If a boolean array assignment can be done in place, build call to
92 -- corresponding library procedure.
94 procedure Displace_Allocator_Pointer (N : Node_Id);
95 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
96 -- Expand_Allocator_Expression. Allocating class-wide interface objects
97 -- this routine displaces the pointer to the allocated object to reference
98 -- the component referencing the corresponding secondary dispatch table.
100 procedure Expand_Allocator_Expression (N : Node_Id);
101 -- Subsidiary to Expand_N_Allocator, for the case when the expression
102 -- is a qualified expression or an aggregate.
104 procedure Expand_Array_Comparison (N : Node_Id);
105 -- This routine handles expansion of the comparison operators (N_Op_Lt,
106 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
107 -- code for these operators is similar, differing only in the details of
108 -- the actual comparison call that is made. Special processing (call a
109 -- run-time routine)
111 function Expand_Array_Equality
112 (Nod : Node_Id;
113 Lhs : Node_Id;
114 Rhs : Node_Id;
115 Bodies : List_Id;
116 Typ : Entity_Id) return Node_Id;
117 -- Expand an array equality into a call to a function implementing this
118 -- equality, and a call to it. Loc is the location for the generated nodes.
119 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
120 -- on which to attach bodies of local functions that are created in the
121 -- process. It is the responsibility of the caller to insert those bodies
122 -- at the right place. Nod provides the Sloc value for the generated code.
123 -- Normally the types used for the generated equality routine are taken
124 -- from Lhs and Rhs. However, in some situations of generated code, the
125 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
126 -- the type to be used for the formal parameters.
128 procedure Expand_Boolean_Operator (N : Node_Id);
129 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
130 -- case of array type arguments.
132 procedure Expand_Short_Circuit_Operator (N : Node_Id);
133 -- Common expansion processing for short-circuit boolean operators
135 function Expand_Composite_Equality
136 (Nod : Node_Id;
137 Typ : Entity_Id;
138 Lhs : Node_Id;
139 Rhs : Node_Id;
140 Bodies : List_Id) return Node_Id;
141 -- Local recursive function used to expand equality for nested composite
142 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
143 -- to attach bodies of local functions that are created in the process.
144 -- This is the responsibility of the caller to insert those bodies at the
145 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
146 -- are the left and right sides for the comparison, and Typ is the type of
147 -- the arrays to compare.
149 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
150 -- Routine to expand concatenation of a sequence of two or more operands
151 -- (in the list Operands) and replace node Cnode with the result of the
152 -- concatenation. The operands can be of any appropriate type, and can
153 -- include both arrays and singleton elements.
155 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
156 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
157 -- fixed. We do not have such a type at runtime, so the purpose of this
158 -- routine is to find the real type by looking up the tree. We also
159 -- determine if the operation must be rounded.
161 function Get_Allocator_Final_List
162 (N : Node_Id;
163 T : Entity_Id;
164 PtrT : Entity_Id) return Entity_Id;
165 -- If the designated type is controlled, build final_list expression for
166 -- created object. If context is an access parameter, create a local access
167 -- type to have a usable finalization list.
169 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
170 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
171 -- discriminants if it has a constrained nominal type, unless the object
172 -- is a component of an enclosing Unchecked_Union object that is subject
173 -- to a per-object constraint and the enclosing object lacks inferable
174 -- discriminants.
176 -- An expression of an Unchecked_Union type has inferable discriminants
177 -- if it is either a name of an object with inferable discriminants or a
178 -- qualified expression whose subtype mark denotes a constrained subtype.
180 procedure Insert_Dereference_Action (N : Node_Id);
181 -- N is an expression whose type is an access. When the type of the
182 -- associated storage pool is derived from Checked_Pool, generate a
183 -- call to the 'Dereference' primitive operation.
185 function Make_Array_Comparison_Op
186 (Typ : Entity_Id;
187 Nod : Node_Id) return Node_Id;
188 -- Comparisons between arrays are expanded in line. This function produces
189 -- the body of the implementation of (a > b), where a and b are one-
190 -- dimensional arrays of some discrete type. The original node is then
191 -- expanded into the appropriate call to this function. Nod provides the
192 -- Sloc value for the generated code.
194 function Make_Boolean_Array_Op
195 (Typ : Entity_Id;
196 N : Node_Id) return Node_Id;
197 -- Boolean operations on boolean arrays are expanded in line. This function
198 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
199 -- b). It is used only the normal case and not the packed case. The type
200 -- involved, Typ, is the Boolean array type, and the logical operations in
201 -- the body are simple boolean operations. Note that Typ is always a
202 -- constrained type (the caller has ensured this by using
203 -- Convert_To_Actual_Subtype if necessary).
205 procedure Rewrite_Comparison (N : Node_Id);
206 -- If N is the node for a comparison whose outcome can be determined at
207 -- compile time, then the node N can be rewritten with True or False. If
208 -- the outcome cannot be determined at compile time, the call has no
209 -- effect. If N is a type conversion, then this processing is applied to
210 -- its expression. If N is neither comparison nor a type conversion, the
211 -- call has no effect.
213 procedure Tagged_Membership
214 (N : Node_Id;
215 SCIL_Node : out Node_Id;
216 Result : out Node_Id);
217 -- Construct the expression corresponding to the tagged membership test.
218 -- Deals with a second operand being (or not) a class-wide type.
220 function Safe_In_Place_Array_Op
221 (Lhs : Node_Id;
222 Op1 : Node_Id;
223 Op2 : Node_Id) return Boolean;
224 -- In the context of an assignment, where the right-hand side is a boolean
225 -- operation on arrays, check whether operation can be performed in place.
227 procedure Unary_Op_Validity_Checks (N : Node_Id);
228 pragma Inline (Unary_Op_Validity_Checks);
229 -- Performs validity checks for a unary operator
231 -------------------------------
232 -- Binary_Op_Validity_Checks --
233 -------------------------------
235 procedure Binary_Op_Validity_Checks (N : Node_Id) is
236 begin
237 if Validity_Checks_On and Validity_Check_Operands then
238 Ensure_Valid (Left_Opnd (N));
239 Ensure_Valid (Right_Opnd (N));
240 end if;
241 end Binary_Op_Validity_Checks;
243 ------------------------------------
244 -- Build_Boolean_Array_Proc_Call --
245 ------------------------------------
247 procedure Build_Boolean_Array_Proc_Call
248 (N : Node_Id;
249 Op1 : Node_Id;
250 Op2 : Node_Id)
252 Loc : constant Source_Ptr := Sloc (N);
253 Kind : constant Node_Kind := Nkind (Expression (N));
254 Target : constant Node_Id :=
255 Make_Attribute_Reference (Loc,
256 Prefix => Name (N),
257 Attribute_Name => Name_Address);
259 Arg1 : Node_Id := Op1;
260 Arg2 : Node_Id := Op2;
261 Call_Node : Node_Id;
262 Proc_Name : Entity_Id;
264 begin
265 if Kind = N_Op_Not then
266 if Nkind (Op1) in N_Binary_Op then
268 -- Use negated version of the binary operators
270 if Nkind (Op1) = N_Op_And then
271 Proc_Name := RTE (RE_Vector_Nand);
273 elsif Nkind (Op1) = N_Op_Or then
274 Proc_Name := RTE (RE_Vector_Nor);
276 else pragma Assert (Nkind (Op1) = N_Op_Xor);
277 Proc_Name := RTE (RE_Vector_Xor);
278 end if;
280 Call_Node :=
281 Make_Procedure_Call_Statement (Loc,
282 Name => New_Occurrence_Of (Proc_Name, Loc),
284 Parameter_Associations => New_List (
285 Target,
286 Make_Attribute_Reference (Loc,
287 Prefix => Left_Opnd (Op1),
288 Attribute_Name => Name_Address),
290 Make_Attribute_Reference (Loc,
291 Prefix => Right_Opnd (Op1),
292 Attribute_Name => Name_Address),
294 Make_Attribute_Reference (Loc,
295 Prefix => Left_Opnd (Op1),
296 Attribute_Name => Name_Length)));
298 else
299 Proc_Name := RTE (RE_Vector_Not);
301 Call_Node :=
302 Make_Procedure_Call_Statement (Loc,
303 Name => New_Occurrence_Of (Proc_Name, Loc),
304 Parameter_Associations => New_List (
305 Target,
307 Make_Attribute_Reference (Loc,
308 Prefix => Op1,
309 Attribute_Name => Name_Address),
311 Make_Attribute_Reference (Loc,
312 Prefix => Op1,
313 Attribute_Name => Name_Length)));
314 end if;
316 else
317 -- We use the following equivalences:
319 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
320 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
321 -- (not X) xor (not Y) = X xor Y
322 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
324 if Nkind (Op1) = N_Op_Not then
325 Arg1 := Right_Opnd (Op1);
326 Arg2 := Right_Opnd (Op2);
327 if Kind = N_Op_And then
328 Proc_Name := RTE (RE_Vector_Nor);
329 elsif Kind = N_Op_Or then
330 Proc_Name := RTE (RE_Vector_Nand);
331 else
332 Proc_Name := RTE (RE_Vector_Xor);
333 end if;
335 else
336 if Kind = N_Op_And then
337 Proc_Name := RTE (RE_Vector_And);
338 elsif Kind = N_Op_Or then
339 Proc_Name := RTE (RE_Vector_Or);
340 elsif Nkind (Op2) = N_Op_Not then
341 Proc_Name := RTE (RE_Vector_Nxor);
342 Arg2 := Right_Opnd (Op2);
343 else
344 Proc_Name := RTE (RE_Vector_Xor);
345 end if;
346 end if;
348 Call_Node :=
349 Make_Procedure_Call_Statement (Loc,
350 Name => New_Occurrence_Of (Proc_Name, Loc),
351 Parameter_Associations => New_List (
352 Target,
353 Make_Attribute_Reference (Loc,
354 Prefix => Arg1,
355 Attribute_Name => Name_Address),
356 Make_Attribute_Reference (Loc,
357 Prefix => Arg2,
358 Attribute_Name => Name_Address),
359 Make_Attribute_Reference (Loc,
360 Prefix => Arg1,
361 Attribute_Name => Name_Length)));
362 end if;
364 Rewrite (N, Call_Node);
365 Analyze (N);
367 exception
368 when RE_Not_Available =>
369 return;
370 end Build_Boolean_Array_Proc_Call;
372 --------------------------------
373 -- Displace_Allocator_Pointer --
374 --------------------------------
376 procedure Displace_Allocator_Pointer (N : Node_Id) is
377 Loc : constant Source_Ptr := Sloc (N);
378 Orig_Node : constant Node_Id := Original_Node (N);
379 Dtyp : Entity_Id;
380 Etyp : Entity_Id;
381 PtrT : Entity_Id;
383 begin
384 -- Do nothing in case of VM targets: the virtual machine will handle
385 -- interfaces directly.
387 if not Tagged_Type_Expansion then
388 return;
389 end if;
391 pragma Assert (Nkind (N) = N_Identifier
392 and then Nkind (Orig_Node) = N_Allocator);
394 PtrT := Etype (Orig_Node);
395 Dtyp := Available_View (Designated_Type (PtrT));
396 Etyp := Etype (Expression (Orig_Node));
398 if Is_Class_Wide_Type (Dtyp)
399 and then Is_Interface (Dtyp)
400 then
401 -- If the type of the allocator expression is not an interface type
402 -- we can generate code to reference the record component containing
403 -- the pointer to the secondary dispatch table.
405 if not Is_Interface (Etyp) then
406 declare
407 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
409 begin
410 -- 1) Get access to the allocated object
412 Rewrite (N,
413 Make_Explicit_Dereference (Loc,
414 Relocate_Node (N)));
415 Set_Etype (N, Etyp);
416 Set_Analyzed (N);
418 -- 2) Add the conversion to displace the pointer to reference
419 -- the secondary dispatch table.
421 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
422 Analyze_And_Resolve (N, Dtyp);
424 -- 3) The 'access to the secondary dispatch table will be used
425 -- as the value returned by the allocator.
427 Rewrite (N,
428 Make_Attribute_Reference (Loc,
429 Prefix => Relocate_Node (N),
430 Attribute_Name => Name_Access));
431 Set_Etype (N, Saved_Typ);
432 Set_Analyzed (N);
433 end;
435 -- If the type of the allocator expression is an interface type we
436 -- generate a run-time call to displace "this" to reference the
437 -- component containing the pointer to the secondary dispatch table
438 -- or else raise Constraint_Error if the actual object does not
439 -- implement the target interface. This case corresponds with the
440 -- following example:
442 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
443 -- begin
444 -- return new Iface_2'Class'(Obj);
445 -- end Op;
447 else
448 Rewrite (N,
449 Unchecked_Convert_To (PtrT,
450 Make_Function_Call (Loc,
451 Name => New_Reference_To (RTE (RE_Displace), Loc),
452 Parameter_Associations => New_List (
453 Unchecked_Convert_To (RTE (RE_Address),
454 Relocate_Node (N)),
456 New_Occurrence_Of
457 (Elists.Node
458 (First_Elmt
459 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
460 Loc)))));
461 Analyze_And_Resolve (N, PtrT);
462 end if;
463 end if;
464 end Displace_Allocator_Pointer;
466 ---------------------------------
467 -- Expand_Allocator_Expression --
468 ---------------------------------
470 procedure Expand_Allocator_Expression (N : Node_Id) is
471 Loc : constant Source_Ptr := Sloc (N);
472 Exp : constant Node_Id := Expression (Expression (N));
473 PtrT : constant Entity_Id := Etype (N);
474 DesigT : constant Entity_Id := Designated_Type (PtrT);
476 procedure Apply_Accessibility_Check
477 (Ref : Node_Id;
478 Built_In_Place : Boolean := False);
479 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
480 -- type, generate an accessibility check to verify that the level of the
481 -- type of the created object is not deeper than the level of the access
482 -- type. If the type of the qualified expression is class- wide, then
483 -- always generate the check (except in the case where it is known to be
484 -- unnecessary, see comment below). Otherwise, only generate the check
485 -- if the level of the qualified expression type is statically deeper
486 -- than the access type.
488 -- Although the static accessibility will generally have been performed
489 -- as a legality check, it won't have been done in cases where the
490 -- allocator appears in generic body, so a run-time check is needed in
491 -- general. One special case is when the access type is declared in the
492 -- same scope as the class-wide allocator, in which case the check can
493 -- never fail, so it need not be generated.
495 -- As an open issue, there seem to be cases where the static level
496 -- associated with the class-wide object's underlying type is not
497 -- sufficient to perform the proper accessibility check, such as for
498 -- allocators in nested subprograms or accept statements initialized by
499 -- class-wide formals when the actual originates outside at a deeper
500 -- static level. The nested subprogram case might require passing
501 -- accessibility levels along with class-wide parameters, and the task
502 -- case seems to be an actual gap in the language rules that needs to
503 -- be fixed by the ARG. ???
505 -------------------------------
506 -- Apply_Accessibility_Check --
507 -------------------------------
509 procedure Apply_Accessibility_Check
510 (Ref : Node_Id;
511 Built_In_Place : Boolean := False)
513 Ref_Node : Node_Id;
515 begin
516 -- Note: we skip the accessibility check for the VM case, since
517 -- there does not seem to be any practical way of implementing it.
519 if Ada_Version >= Ada_2005
520 and then Tagged_Type_Expansion
521 and then Is_Class_Wide_Type (DesigT)
522 and then not Scope_Suppress (Accessibility_Check)
523 and then
524 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
525 or else
526 (Is_Class_Wide_Type (Etype (Exp))
527 and then Scope (PtrT) /= Current_Scope))
528 then
529 -- If the allocator was built in place Ref is already a reference
530 -- to the access object initialized to the result of the allocator
531 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
532 -- it is the entity associated with the object containing the
533 -- address of the allocated object.
535 if Built_In_Place then
536 Ref_Node := New_Copy (Ref);
537 else
538 Ref_Node := New_Reference_To (Ref, Loc);
539 end if;
541 Insert_Action (N,
542 Make_Raise_Program_Error (Loc,
543 Condition =>
544 Make_Op_Gt (Loc,
545 Left_Opnd =>
546 Build_Get_Access_Level (Loc,
547 Make_Attribute_Reference (Loc,
548 Prefix => Ref_Node,
549 Attribute_Name => Name_Tag)),
550 Right_Opnd =>
551 Make_Integer_Literal (Loc,
552 Type_Access_Level (PtrT))),
553 Reason => PE_Accessibility_Check_Failed));
554 end if;
555 end Apply_Accessibility_Check;
557 -- Local variables
559 Indic : constant Node_Id := Subtype_Mark (Expression (N));
560 T : constant Entity_Id := Entity (Indic);
561 Flist : Node_Id;
562 Node : Node_Id;
563 Temp : Entity_Id;
565 TagT : Entity_Id := Empty;
566 -- Type used as source for tag assignment
568 TagR : Node_Id := Empty;
569 -- Target reference for tag assignment
571 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
573 Tag_Assign : Node_Id;
574 Tmp_Node : Node_Id;
576 -- Start of processing for Expand_Allocator_Expression
578 begin
579 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
581 if Is_CPP_Constructor_Call (Exp) then
583 -- Generate:
584 -- Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn
586 -- Allocate the object with no expression
588 Node := Relocate_Node (N);
589 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
591 -- Avoid its expansion to avoid generating a call to the default
592 -- C++ constructor
594 Set_Analyzed (Node);
596 Temp := Make_Temporary (Loc, 'P', N);
598 Insert_Action (N,
599 Make_Object_Declaration (Loc,
600 Defining_Identifier => Temp,
601 Constant_Present => True,
602 Object_Definition => New_Reference_To (PtrT, Loc),
603 Expression => Node));
605 Apply_Accessibility_Check (Temp);
607 -- Locate the enclosing list and insert the C++ constructor call
609 declare
610 P : Node_Id;
612 begin
613 P := Parent (Node);
614 while not Is_List_Member (P) loop
615 P := Parent (P);
616 end loop;
618 Insert_List_After_And_Analyze (P,
619 Build_Initialization_Call (Loc,
620 Id_Ref =>
621 Make_Explicit_Dereference (Loc,
622 Prefix => New_Reference_To (Temp, Loc)),
623 Typ => Etype (Exp),
624 Constructor_Ref => Exp));
625 end;
627 Rewrite (N, New_Reference_To (Temp, Loc));
628 Analyze_And_Resolve (N, PtrT);
629 return;
630 end if;
632 -- Ada 2005 (AI-318-02): If the initialization expression is a call
633 -- to a build-in-place function, then access to the allocated object
634 -- must be passed to the function. Currently we limit such functions
635 -- to those with constrained limited result subtypes, but eventually
636 -- we plan to expand the allowed forms of functions that are treated
637 -- as build-in-place.
639 if Ada_Version >= Ada_2005
640 and then Is_Build_In_Place_Function_Call (Exp)
641 then
642 Make_Build_In_Place_Call_In_Allocator (N, Exp);
643 Apply_Accessibility_Check (N, Built_In_Place => True);
644 return;
645 end if;
647 -- Actions inserted before:
648 -- Temp : constant ptr_T := new T'(Expression);
649 -- <no CW> Temp._tag := T'tag;
650 -- <CTRL> Adjust (Finalizable (Temp.all));
651 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
653 -- We analyze by hand the new internal allocator to avoid
654 -- any recursion and inappropriate call to Initialize
656 -- We don't want to remove side effects when the expression must be
657 -- built in place. In the case of a build-in-place function call,
658 -- that could lead to a duplication of the call, which was already
659 -- substituted for the allocator.
661 if not Aggr_In_Place then
662 Remove_Side_Effects (Exp);
663 end if;
665 Temp := Make_Temporary (Loc, 'P', N);
667 -- For a class wide allocation generate the following code:
669 -- type Equiv_Record is record ... end record;
670 -- implicit subtype CW is <Class_Wide_Subytpe>;
671 -- temp : PtrT := new CW'(CW!(expr));
673 if Is_Class_Wide_Type (T) then
674 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
676 -- Ada 2005 (AI-251): If the expression is a class-wide interface
677 -- object we generate code to move up "this" to reference the
678 -- base of the object before allocating the new object.
680 -- Note that Exp'Address is recursively expanded into a call
681 -- to Base_Address (Exp.Tag)
683 if Is_Class_Wide_Type (Etype (Exp))
684 and then Is_Interface (Etype (Exp))
685 and then Tagged_Type_Expansion
686 then
687 Set_Expression
688 (Expression (N),
689 Unchecked_Convert_To (Entity (Indic),
690 Make_Explicit_Dereference (Loc,
691 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
692 Make_Attribute_Reference (Loc,
693 Prefix => Exp,
694 Attribute_Name => Name_Address)))));
696 else
697 Set_Expression
698 (Expression (N),
699 Unchecked_Convert_To (Entity (Indic), Exp));
700 end if;
702 Analyze_And_Resolve (Expression (N), Entity (Indic));
703 end if;
705 -- Keep separate the management of allocators returning interfaces
707 if not Is_Interface (Directly_Designated_Type (PtrT)) then
708 if Aggr_In_Place then
709 Tmp_Node :=
710 Make_Object_Declaration (Loc,
711 Defining_Identifier => Temp,
712 Object_Definition => New_Reference_To (PtrT, Loc),
713 Expression =>
714 Make_Allocator (Loc,
715 New_Reference_To (Etype (Exp), Loc)));
717 -- Copy the Comes_From_Source flag for the allocator we just
718 -- built, since logically this allocator is a replacement of
719 -- the original allocator node. This is for proper handling of
720 -- restriction No_Implicit_Heap_Allocations.
722 Set_Comes_From_Source
723 (Expression (Tmp_Node), Comes_From_Source (N));
725 Set_No_Initialization (Expression (Tmp_Node));
726 Insert_Action (N, Tmp_Node);
728 if Needs_Finalization (T)
729 and then Ekind (PtrT) = E_Anonymous_Access_Type
730 then
731 -- Create local finalization list for access parameter
733 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
734 end if;
736 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
738 else
739 Node := Relocate_Node (N);
740 Set_Analyzed (Node);
741 Insert_Action (N,
742 Make_Object_Declaration (Loc,
743 Defining_Identifier => Temp,
744 Constant_Present => True,
745 Object_Definition => New_Reference_To (PtrT, Loc),
746 Expression => Node));
747 end if;
749 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
750 -- interface type. In this case we use the type of the qualified
751 -- expression to allocate the object.
753 else
754 declare
755 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
756 New_Decl : Node_Id;
758 begin
759 New_Decl :=
760 Make_Full_Type_Declaration (Loc,
761 Defining_Identifier => Def_Id,
762 Type_Definition =>
763 Make_Access_To_Object_Definition (Loc,
764 All_Present => True,
765 Null_Exclusion_Present => False,
766 Constant_Present => False,
767 Subtype_Indication =>
768 New_Reference_To (Etype (Exp), Loc)));
770 Insert_Action (N, New_Decl);
772 -- Inherit the final chain to ensure that the expansion of the
773 -- aggregate is correct in case of controlled types
775 if Needs_Finalization (Directly_Designated_Type (PtrT)) then
776 Set_Associated_Final_Chain (Def_Id,
777 Associated_Final_Chain (PtrT));
778 end if;
780 -- Declare the object using the previous type declaration
782 if Aggr_In_Place then
783 Tmp_Node :=
784 Make_Object_Declaration (Loc,
785 Defining_Identifier => Temp,
786 Object_Definition => New_Reference_To (Def_Id, Loc),
787 Expression =>
788 Make_Allocator (Loc,
789 New_Reference_To (Etype (Exp), Loc)));
791 -- Copy the Comes_From_Source flag for the allocator we just
792 -- built, since logically this allocator is a replacement of
793 -- the original allocator node. This is for proper handling
794 -- of restriction No_Implicit_Heap_Allocations.
796 Set_Comes_From_Source
797 (Expression (Tmp_Node), Comes_From_Source (N));
799 Set_No_Initialization (Expression (Tmp_Node));
800 Insert_Action (N, Tmp_Node);
802 if Needs_Finalization (T)
803 and then Ekind (PtrT) = E_Anonymous_Access_Type
804 then
805 -- Create local finalization list for access parameter
807 Flist :=
808 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
809 end if;
811 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
812 else
813 Node := Relocate_Node (N);
814 Set_Analyzed (Node);
815 Insert_Action (N,
816 Make_Object_Declaration (Loc,
817 Defining_Identifier => Temp,
818 Constant_Present => True,
819 Object_Definition => New_Reference_To (Def_Id, Loc),
820 Expression => Node));
821 end if;
823 -- Generate an additional object containing the address of the
824 -- returned object. The type of this second object declaration
825 -- is the correct type required for the common processing that
826 -- is still performed by this subprogram. The displacement of
827 -- this pointer to reference the component associated with the
828 -- interface type will be done at the end of common processing.
830 New_Decl :=
831 Make_Object_Declaration (Loc,
832 Defining_Identifier => Make_Temporary (Loc, 'P'),
833 Object_Definition => New_Reference_To (PtrT, Loc),
834 Expression => Unchecked_Convert_To (PtrT,
835 New_Reference_To (Temp, Loc)));
837 Insert_Action (N, New_Decl);
839 Tmp_Node := New_Decl;
840 Temp := Defining_Identifier (New_Decl);
841 end;
842 end if;
844 Apply_Accessibility_Check (Temp);
846 -- Generate the tag assignment
848 -- Suppress the tag assignment when VM_Target because VM tags are
849 -- represented implicitly in objects.
851 if not Tagged_Type_Expansion then
852 null;
854 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
855 -- interface objects because in this case the tag does not change.
857 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
858 pragma Assert (Is_Class_Wide_Type
859 (Directly_Designated_Type (Etype (N))));
860 null;
862 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
863 TagT := T;
864 TagR := New_Reference_To (Temp, Loc);
866 elsif Is_Private_Type (T)
867 and then Is_Tagged_Type (Underlying_Type (T))
868 then
869 TagT := Underlying_Type (T);
870 TagR :=
871 Unchecked_Convert_To (Underlying_Type (T),
872 Make_Explicit_Dereference (Loc,
873 Prefix => New_Reference_To (Temp, Loc)));
874 end if;
876 if Present (TagT) then
877 Tag_Assign :=
878 Make_Assignment_Statement (Loc,
879 Name =>
880 Make_Selected_Component (Loc,
881 Prefix => TagR,
882 Selector_Name =>
883 New_Reference_To (First_Tag_Component (TagT), Loc)),
885 Expression =>
886 Unchecked_Convert_To (RTE (RE_Tag),
887 New_Reference_To
888 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
889 Loc)));
891 -- The previous assignment has to be done in any case
893 Set_Assignment_OK (Name (Tag_Assign));
894 Insert_Action (N, Tag_Assign);
895 end if;
897 if Needs_Finalization (DesigT)
898 and then Needs_Finalization (T)
899 then
900 declare
901 Attach : Node_Id;
902 Apool : constant Entity_Id :=
903 Associated_Storage_Pool (PtrT);
905 begin
906 -- If it is an allocation on the secondary stack (i.e. a value
907 -- returned from a function), the object is attached on the
908 -- caller side as soon as the call is completed (see
909 -- Expand_Ctrl_Function_Call)
911 if Is_RTE (Apool, RE_SS_Pool) then
912 declare
913 F : constant Entity_Id := Make_Temporary (Loc, 'F');
914 begin
915 Insert_Action (N,
916 Make_Object_Declaration (Loc,
917 Defining_Identifier => F,
918 Object_Definition =>
919 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
920 Flist := New_Reference_To (F, Loc);
921 Attach := Make_Integer_Literal (Loc, 1);
922 end;
924 -- Normal case, not a secondary stack allocation
926 else
927 if Needs_Finalization (T)
928 and then Ekind (PtrT) = E_Anonymous_Access_Type
929 then
930 -- Create local finalization list for access parameter
932 Flist :=
933 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
934 else
935 Flist := Find_Final_List (PtrT);
936 end if;
938 Attach := Make_Integer_Literal (Loc, 2);
939 end if;
941 -- Generate an Adjust call if the object will be moved. In Ada
942 -- 2005, the object may be inherently limited, in which case
943 -- there is no Adjust procedure, and the object is built in
944 -- place. In Ada 95, the object can be limited but not
945 -- inherently limited if this allocator came from a return
946 -- statement (we're allocating the result on the secondary
947 -- stack). In that case, the object will be moved, so we _do_
948 -- want to Adjust.
950 if not Aggr_In_Place
951 and then not Is_Immutably_Limited_Type (T)
952 then
953 Insert_Actions (N,
954 Make_Adjust_Call (
955 Ref =>
957 -- An unchecked conversion is needed in the classwide
958 -- case because the designated type can be an ancestor of
959 -- the subtype mark of the allocator.
961 Unchecked_Convert_To (T,
962 Make_Explicit_Dereference (Loc,
963 Prefix => New_Reference_To (Temp, Loc))),
965 Typ => T,
966 Flist_Ref => Flist,
967 With_Attach => Attach,
968 Allocator => True));
969 end if;
970 end;
971 end if;
973 Rewrite (N, New_Reference_To (Temp, Loc));
974 Analyze_And_Resolve (N, PtrT);
976 -- Ada 2005 (AI-251): Displace the pointer to reference the record
977 -- component containing the secondary dispatch table of the interface
978 -- type.
980 if Is_Interface (Directly_Designated_Type (PtrT)) then
981 Displace_Allocator_Pointer (N);
982 end if;
984 elsif Aggr_In_Place then
985 Temp := Make_Temporary (Loc, 'P', N);
986 Tmp_Node :=
987 Make_Object_Declaration (Loc,
988 Defining_Identifier => Temp,
989 Object_Definition => New_Reference_To (PtrT, Loc),
990 Expression => Make_Allocator (Loc,
991 New_Reference_To (Etype (Exp), Loc)));
993 -- Copy the Comes_From_Source flag for the allocator we just built,
994 -- since logically this allocator is a replacement of the original
995 -- allocator node. This is for proper handling of restriction
996 -- No_Implicit_Heap_Allocations.
998 Set_Comes_From_Source
999 (Expression (Tmp_Node), Comes_From_Source (N));
1001 Set_No_Initialization (Expression (Tmp_Node));
1002 Insert_Action (N, Tmp_Node);
1003 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
1004 Rewrite (N, New_Reference_To (Temp, Loc));
1005 Analyze_And_Resolve (N, PtrT);
1007 elsif Is_Access_Type (T)
1008 and then Can_Never_Be_Null (T)
1009 then
1010 Install_Null_Excluding_Check (Exp);
1012 elsif Is_Access_Type (DesigT)
1013 and then Nkind (Exp) = N_Allocator
1014 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1015 then
1016 -- Apply constraint to designated subtype indication
1018 Apply_Constraint_Check (Expression (Exp),
1019 Designated_Type (DesigT),
1020 No_Sliding => True);
1022 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1024 -- Propagate constraint_error to enclosing allocator
1026 Rewrite (Exp, New_Copy (Expression (Exp)));
1027 end if;
1028 else
1029 -- If we have:
1030 -- type A is access T1;
1031 -- X : A := new T2'(...);
1032 -- T1 and T2 can be different subtypes, and we might need to check
1033 -- both constraints. First check against the type of the qualified
1034 -- expression.
1036 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1038 if Do_Range_Check (Exp) then
1039 Set_Do_Range_Check (Exp, False);
1040 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1041 end if;
1043 -- A check is also needed in cases where the designated subtype is
1044 -- constrained and differs from the subtype given in the qualified
1045 -- expression. Note that the check on the qualified expression does
1046 -- not allow sliding, but this check does (a relaxation from Ada 83).
1048 if Is_Constrained (DesigT)
1049 and then not Subtypes_Statically_Match (T, DesigT)
1050 then
1051 Apply_Constraint_Check
1052 (Exp, DesigT, No_Sliding => False);
1054 if Do_Range_Check (Exp) then
1055 Set_Do_Range_Check (Exp, False);
1056 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1057 end if;
1058 end if;
1060 -- For an access to unconstrained packed array, GIGI needs to see an
1061 -- expression with a constrained subtype in order to compute the
1062 -- proper size for the allocator.
1064 if Is_Array_Type (T)
1065 and then not Is_Constrained (T)
1066 and then Is_Packed (T)
1067 then
1068 declare
1069 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1070 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1071 begin
1072 Insert_Action (Exp,
1073 Make_Subtype_Declaration (Loc,
1074 Defining_Identifier => ConstrT,
1075 Subtype_Indication =>
1076 Make_Subtype_From_Expr (Exp, T)));
1077 Freeze_Itype (ConstrT, Exp);
1078 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1079 end;
1080 end if;
1082 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1083 -- to a build-in-place function, then access to the allocated object
1084 -- must be passed to the function. Currently we limit such functions
1085 -- to those with constrained limited result subtypes, but eventually
1086 -- we plan to expand the allowed forms of functions that are treated
1087 -- as build-in-place.
1089 if Ada_Version >= Ada_2005
1090 and then Is_Build_In_Place_Function_Call (Exp)
1091 then
1092 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1093 end if;
1094 end if;
1096 exception
1097 when RE_Not_Available =>
1098 return;
1099 end Expand_Allocator_Expression;
1101 -----------------------------
1102 -- Expand_Array_Comparison --
1103 -----------------------------
1105 -- Expansion is only required in the case of array types. For the unpacked
1106 -- case, an appropriate runtime routine is called. For packed cases, and
1107 -- also in some other cases where a runtime routine cannot be called, the
1108 -- form of the expansion is:
1110 -- [body for greater_nn; boolean_expression]
1112 -- The body is built by Make_Array_Comparison_Op, and the form of the
1113 -- Boolean expression depends on the operator involved.
1115 procedure Expand_Array_Comparison (N : Node_Id) is
1116 Loc : constant Source_Ptr := Sloc (N);
1117 Op1 : Node_Id := Left_Opnd (N);
1118 Op2 : Node_Id := Right_Opnd (N);
1119 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1120 Ctyp : constant Entity_Id := Component_Type (Typ1);
1122 Expr : Node_Id;
1123 Func_Body : Node_Id;
1124 Func_Name : Entity_Id;
1126 Comp : RE_Id;
1128 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1129 -- True for byte addressable target
1131 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1132 -- Returns True if the length of the given operand is known to be less
1133 -- than 4. Returns False if this length is known to be four or greater
1134 -- or is not known at compile time.
1136 ------------------------
1137 -- Length_Less_Than_4 --
1138 ------------------------
1140 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1141 Otyp : constant Entity_Id := Etype (Opnd);
1143 begin
1144 if Ekind (Otyp) = E_String_Literal_Subtype then
1145 return String_Literal_Length (Otyp) < 4;
1147 else
1148 declare
1149 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1150 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1151 Hi : constant Node_Id := Type_High_Bound (Ityp);
1152 Lov : Uint;
1153 Hiv : Uint;
1155 begin
1156 if Compile_Time_Known_Value (Lo) then
1157 Lov := Expr_Value (Lo);
1158 else
1159 return False;
1160 end if;
1162 if Compile_Time_Known_Value (Hi) then
1163 Hiv := Expr_Value (Hi);
1164 else
1165 return False;
1166 end if;
1168 return Hiv < Lov + 3;
1169 end;
1170 end if;
1171 end Length_Less_Than_4;
1173 -- Start of processing for Expand_Array_Comparison
1175 begin
1176 -- Deal first with unpacked case, where we can call a runtime routine
1177 -- except that we avoid this for targets for which are not addressable
1178 -- by bytes, and for the JVM/CIL, since they do not support direct
1179 -- addressing of array components.
1181 if not Is_Bit_Packed_Array (Typ1)
1182 and then Byte_Addressable
1183 and then VM_Target = No_VM
1184 then
1185 -- The call we generate is:
1187 -- Compare_Array_xn[_Unaligned]
1188 -- (left'address, right'address, left'length, right'length) <op> 0
1190 -- x = U for unsigned, S for signed
1191 -- n = 8,16,32,64 for component size
1192 -- Add _Unaligned if length < 4 and component size is 8.
1193 -- <op> is the standard comparison operator
1195 if Component_Size (Typ1) = 8 then
1196 if Length_Less_Than_4 (Op1)
1197 or else
1198 Length_Less_Than_4 (Op2)
1199 then
1200 if Is_Unsigned_Type (Ctyp) then
1201 Comp := RE_Compare_Array_U8_Unaligned;
1202 else
1203 Comp := RE_Compare_Array_S8_Unaligned;
1204 end if;
1206 else
1207 if Is_Unsigned_Type (Ctyp) then
1208 Comp := RE_Compare_Array_U8;
1209 else
1210 Comp := RE_Compare_Array_S8;
1211 end if;
1212 end if;
1214 elsif Component_Size (Typ1) = 16 then
1215 if Is_Unsigned_Type (Ctyp) then
1216 Comp := RE_Compare_Array_U16;
1217 else
1218 Comp := RE_Compare_Array_S16;
1219 end if;
1221 elsif Component_Size (Typ1) = 32 then
1222 if Is_Unsigned_Type (Ctyp) then
1223 Comp := RE_Compare_Array_U32;
1224 else
1225 Comp := RE_Compare_Array_S32;
1226 end if;
1228 else pragma Assert (Component_Size (Typ1) = 64);
1229 if Is_Unsigned_Type (Ctyp) then
1230 Comp := RE_Compare_Array_U64;
1231 else
1232 Comp := RE_Compare_Array_S64;
1233 end if;
1234 end if;
1236 Remove_Side_Effects (Op1, Name_Req => True);
1237 Remove_Side_Effects (Op2, Name_Req => True);
1239 Rewrite (Op1,
1240 Make_Function_Call (Sloc (Op1),
1241 Name => New_Occurrence_Of (RTE (Comp), Loc),
1243 Parameter_Associations => New_List (
1244 Make_Attribute_Reference (Loc,
1245 Prefix => Relocate_Node (Op1),
1246 Attribute_Name => Name_Address),
1248 Make_Attribute_Reference (Loc,
1249 Prefix => Relocate_Node (Op2),
1250 Attribute_Name => Name_Address),
1252 Make_Attribute_Reference (Loc,
1253 Prefix => Relocate_Node (Op1),
1254 Attribute_Name => Name_Length),
1256 Make_Attribute_Reference (Loc,
1257 Prefix => Relocate_Node (Op2),
1258 Attribute_Name => Name_Length))));
1260 Rewrite (Op2,
1261 Make_Integer_Literal (Sloc (Op2),
1262 Intval => Uint_0));
1264 Analyze_And_Resolve (Op1, Standard_Integer);
1265 Analyze_And_Resolve (Op2, Standard_Integer);
1266 return;
1267 end if;
1269 -- Cases where we cannot make runtime call
1271 -- For (a <= b) we convert to not (a > b)
1273 if Chars (N) = Name_Op_Le then
1274 Rewrite (N,
1275 Make_Op_Not (Loc,
1276 Right_Opnd =>
1277 Make_Op_Gt (Loc,
1278 Left_Opnd => Op1,
1279 Right_Opnd => Op2)));
1280 Analyze_And_Resolve (N, Standard_Boolean);
1281 return;
1283 -- For < the Boolean expression is
1284 -- greater__nn (op2, op1)
1286 elsif Chars (N) = Name_Op_Lt then
1287 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1289 -- Switch operands
1291 Op1 := Right_Opnd (N);
1292 Op2 := Left_Opnd (N);
1294 -- For (a >= b) we convert to not (a < b)
1296 elsif Chars (N) = Name_Op_Ge then
1297 Rewrite (N,
1298 Make_Op_Not (Loc,
1299 Right_Opnd =>
1300 Make_Op_Lt (Loc,
1301 Left_Opnd => Op1,
1302 Right_Opnd => Op2)));
1303 Analyze_And_Resolve (N, Standard_Boolean);
1304 return;
1306 -- For > the Boolean expression is
1307 -- greater__nn (op1, op2)
1309 else
1310 pragma Assert (Chars (N) = Name_Op_Gt);
1311 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1312 end if;
1314 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1315 Expr :=
1316 Make_Function_Call (Loc,
1317 Name => New_Reference_To (Func_Name, Loc),
1318 Parameter_Associations => New_List (Op1, Op2));
1320 Insert_Action (N, Func_Body);
1321 Rewrite (N, Expr);
1322 Analyze_And_Resolve (N, Standard_Boolean);
1324 exception
1325 when RE_Not_Available =>
1326 return;
1327 end Expand_Array_Comparison;
1329 ---------------------------
1330 -- Expand_Array_Equality --
1331 ---------------------------
1333 -- Expand an equality function for multi-dimensional arrays. Here is an
1334 -- example of such a function for Nb_Dimension = 2
1336 -- function Enn (A : atyp; B : btyp) return boolean is
1337 -- begin
1338 -- if (A'length (1) = 0 or else A'length (2) = 0)
1339 -- and then
1340 -- (B'length (1) = 0 or else B'length (2) = 0)
1341 -- then
1342 -- return True; -- RM 4.5.2(22)
1343 -- end if;
1345 -- if A'length (1) /= B'length (1)
1346 -- or else
1347 -- A'length (2) /= B'length (2)
1348 -- then
1349 -- return False; -- RM 4.5.2(23)
1350 -- end if;
1352 -- declare
1353 -- A1 : Index_T1 := A'first (1);
1354 -- B1 : Index_T1 := B'first (1);
1355 -- begin
1356 -- loop
1357 -- declare
1358 -- A2 : Index_T2 := A'first (2);
1359 -- B2 : Index_T2 := B'first (2);
1360 -- begin
1361 -- loop
1362 -- if A (A1, A2) /= B (B1, B2) then
1363 -- return False;
1364 -- end if;
1366 -- exit when A2 = A'last (2);
1367 -- A2 := Index_T2'succ (A2);
1368 -- B2 := Index_T2'succ (B2);
1369 -- end loop;
1370 -- end;
1372 -- exit when A1 = A'last (1);
1373 -- A1 := Index_T1'succ (A1);
1374 -- B1 := Index_T1'succ (B1);
1375 -- end loop;
1376 -- end;
1378 -- return true;
1379 -- end Enn;
1381 -- Note on the formal types used (atyp and btyp). If either of the arrays
1382 -- is of a private type, we use the underlying type, and do an unchecked
1383 -- conversion of the actual. If either of the arrays has a bound depending
1384 -- on a discriminant, then we use the base type since otherwise we have an
1385 -- escaped discriminant in the function.
1387 -- If both arrays are constrained and have the same bounds, we can generate
1388 -- a loop with an explicit iteration scheme using a 'Range attribute over
1389 -- the first array.
1391 function Expand_Array_Equality
1392 (Nod : Node_Id;
1393 Lhs : Node_Id;
1394 Rhs : Node_Id;
1395 Bodies : List_Id;
1396 Typ : Entity_Id) return Node_Id
1398 Loc : constant Source_Ptr := Sloc (Nod);
1399 Decls : constant List_Id := New_List;
1400 Index_List1 : constant List_Id := New_List;
1401 Index_List2 : constant List_Id := New_List;
1403 Actuals : List_Id;
1404 Formals : List_Id;
1405 Func_Name : Entity_Id;
1406 Func_Body : Node_Id;
1408 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1409 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1411 Ltyp : Entity_Id;
1412 Rtyp : Entity_Id;
1413 -- The parameter types to be used for the formals
1415 function Arr_Attr
1416 (Arr : Entity_Id;
1417 Nam : Name_Id;
1418 Num : Int) return Node_Id;
1419 -- This builds the attribute reference Arr'Nam (Expr)
1421 function Component_Equality (Typ : Entity_Id) return Node_Id;
1422 -- Create one statement to compare corresponding components, designated
1423 -- by a full set of indexes.
1425 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1426 -- Given one of the arguments, computes the appropriate type to be used
1427 -- for that argument in the corresponding function formal
1429 function Handle_One_Dimension
1430 (N : Int;
1431 Index : Node_Id) return Node_Id;
1432 -- This procedure returns the following code
1434 -- declare
1435 -- Bn : Index_T := B'First (N);
1436 -- begin
1437 -- loop
1438 -- xxx
1439 -- exit when An = A'Last (N);
1440 -- An := Index_T'Succ (An)
1441 -- Bn := Index_T'Succ (Bn)
1442 -- end loop;
1443 -- end;
1445 -- If both indexes are constrained and identical, the procedure
1446 -- returns a simpler loop:
1448 -- for An in A'Range (N) loop
1449 -- xxx
1450 -- end loop
1452 -- N is the dimension for which we are generating a loop. Index is the
1453 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1454 -- xxx statement is either the loop or declare for the next dimension
1455 -- or if this is the last dimension the comparison of corresponding
1456 -- components of the arrays.
1458 -- The actual way the code works is to return the comparison of
1459 -- corresponding components for the N+1 call. That's neater!
1461 function Test_Empty_Arrays return Node_Id;
1462 -- This function constructs the test for both arrays being empty
1463 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1464 -- and then
1465 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1467 function Test_Lengths_Correspond return Node_Id;
1468 -- This function constructs the test for arrays having different lengths
1469 -- in at least one index position, in which case the resulting code is:
1471 -- A'length (1) /= B'length (1)
1472 -- or else
1473 -- A'length (2) /= B'length (2)
1474 -- or else
1475 -- ...
1477 --------------
1478 -- Arr_Attr --
1479 --------------
1481 function Arr_Attr
1482 (Arr : Entity_Id;
1483 Nam : Name_Id;
1484 Num : Int) return Node_Id
1486 begin
1487 return
1488 Make_Attribute_Reference (Loc,
1489 Attribute_Name => Nam,
1490 Prefix => New_Reference_To (Arr, Loc),
1491 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1492 end Arr_Attr;
1494 ------------------------
1495 -- Component_Equality --
1496 ------------------------
1498 function Component_Equality (Typ : Entity_Id) return Node_Id is
1499 Test : Node_Id;
1500 L, R : Node_Id;
1502 begin
1503 -- if a(i1...) /= b(j1...) then return false; end if;
1505 L :=
1506 Make_Indexed_Component (Loc,
1507 Prefix => Make_Identifier (Loc, Chars (A)),
1508 Expressions => Index_List1);
1510 R :=
1511 Make_Indexed_Component (Loc,
1512 Prefix => Make_Identifier (Loc, Chars (B)),
1513 Expressions => Index_List2);
1515 Test := Expand_Composite_Equality
1516 (Nod, Component_Type (Typ), L, R, Decls);
1518 -- If some (sub)component is an unchecked_union, the whole operation
1519 -- will raise program error.
1521 if Nkind (Test) = N_Raise_Program_Error then
1523 -- This node is going to be inserted at a location where a
1524 -- statement is expected: clear its Etype so analysis will set
1525 -- it to the expected Standard_Void_Type.
1527 Set_Etype (Test, Empty);
1528 return Test;
1530 else
1531 return
1532 Make_Implicit_If_Statement (Nod,
1533 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1534 Then_Statements => New_List (
1535 Make_Simple_Return_Statement (Loc,
1536 Expression => New_Occurrence_Of (Standard_False, Loc))));
1537 end if;
1538 end Component_Equality;
1540 ------------------
1541 -- Get_Arg_Type --
1542 ------------------
1544 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1545 T : Entity_Id;
1546 X : Node_Id;
1548 begin
1549 T := Etype (N);
1551 if No (T) then
1552 return Typ;
1554 else
1555 T := Underlying_Type (T);
1557 X := First_Index (T);
1558 while Present (X) loop
1559 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1560 or else
1561 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1562 then
1563 T := Base_Type (T);
1564 exit;
1565 end if;
1567 Next_Index (X);
1568 end loop;
1570 return T;
1571 end if;
1572 end Get_Arg_Type;
1574 --------------------------
1575 -- Handle_One_Dimension --
1576 ---------------------------
1578 function Handle_One_Dimension
1579 (N : Int;
1580 Index : Node_Id) return Node_Id
1582 Need_Separate_Indexes : constant Boolean :=
1583 Ltyp /= Rtyp
1584 or else not Is_Constrained (Ltyp);
1585 -- If the index types are identical, and we are working with
1586 -- constrained types, then we can use the same index for both
1587 -- of the arrays.
1589 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1591 Bn : Entity_Id;
1592 Index_T : Entity_Id;
1593 Stm_List : List_Id;
1594 Loop_Stm : Node_Id;
1596 begin
1597 if N > Number_Dimensions (Ltyp) then
1598 return Component_Equality (Ltyp);
1599 end if;
1601 -- Case where we generate a loop
1603 Index_T := Base_Type (Etype (Index));
1605 if Need_Separate_Indexes then
1606 Bn := Make_Temporary (Loc, 'B');
1607 else
1608 Bn := An;
1609 end if;
1611 Append (New_Reference_To (An, Loc), Index_List1);
1612 Append (New_Reference_To (Bn, Loc), Index_List2);
1614 Stm_List := New_List (
1615 Handle_One_Dimension (N + 1, Next_Index (Index)));
1617 if Need_Separate_Indexes then
1619 -- Generate guard for loop, followed by increments of indexes
1621 Append_To (Stm_List,
1622 Make_Exit_Statement (Loc,
1623 Condition =>
1624 Make_Op_Eq (Loc,
1625 Left_Opnd => New_Reference_To (An, Loc),
1626 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1628 Append_To (Stm_List,
1629 Make_Assignment_Statement (Loc,
1630 Name => New_Reference_To (An, Loc),
1631 Expression =>
1632 Make_Attribute_Reference (Loc,
1633 Prefix => New_Reference_To (Index_T, Loc),
1634 Attribute_Name => Name_Succ,
1635 Expressions => New_List (New_Reference_To (An, Loc)))));
1637 Append_To (Stm_List,
1638 Make_Assignment_Statement (Loc,
1639 Name => New_Reference_To (Bn, Loc),
1640 Expression =>
1641 Make_Attribute_Reference (Loc,
1642 Prefix => New_Reference_To (Index_T, Loc),
1643 Attribute_Name => Name_Succ,
1644 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1645 end if;
1647 -- If separate indexes, we need a declare block for An and Bn, and a
1648 -- loop without an iteration scheme.
1650 if Need_Separate_Indexes then
1651 Loop_Stm :=
1652 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1654 return
1655 Make_Block_Statement (Loc,
1656 Declarations => New_List (
1657 Make_Object_Declaration (Loc,
1658 Defining_Identifier => An,
1659 Object_Definition => New_Reference_To (Index_T, Loc),
1660 Expression => Arr_Attr (A, Name_First, N)),
1662 Make_Object_Declaration (Loc,
1663 Defining_Identifier => Bn,
1664 Object_Definition => New_Reference_To (Index_T, Loc),
1665 Expression => Arr_Attr (B, Name_First, N))),
1667 Handled_Statement_Sequence =>
1668 Make_Handled_Sequence_Of_Statements (Loc,
1669 Statements => New_List (Loop_Stm)));
1671 -- If no separate indexes, return loop statement with explicit
1672 -- iteration scheme on its own
1674 else
1675 Loop_Stm :=
1676 Make_Implicit_Loop_Statement (Nod,
1677 Statements => Stm_List,
1678 Iteration_Scheme =>
1679 Make_Iteration_Scheme (Loc,
1680 Loop_Parameter_Specification =>
1681 Make_Loop_Parameter_Specification (Loc,
1682 Defining_Identifier => An,
1683 Discrete_Subtype_Definition =>
1684 Arr_Attr (A, Name_Range, N))));
1685 return Loop_Stm;
1686 end if;
1687 end Handle_One_Dimension;
1689 -----------------------
1690 -- Test_Empty_Arrays --
1691 -----------------------
1693 function Test_Empty_Arrays return Node_Id is
1694 Alist : Node_Id;
1695 Blist : Node_Id;
1697 Atest : Node_Id;
1698 Btest : Node_Id;
1700 begin
1701 Alist := Empty;
1702 Blist := Empty;
1703 for J in 1 .. Number_Dimensions (Ltyp) loop
1704 Atest :=
1705 Make_Op_Eq (Loc,
1706 Left_Opnd => Arr_Attr (A, Name_Length, J),
1707 Right_Opnd => Make_Integer_Literal (Loc, 0));
1709 Btest :=
1710 Make_Op_Eq (Loc,
1711 Left_Opnd => Arr_Attr (B, Name_Length, J),
1712 Right_Opnd => Make_Integer_Literal (Loc, 0));
1714 if No (Alist) then
1715 Alist := Atest;
1716 Blist := Btest;
1718 else
1719 Alist :=
1720 Make_Or_Else (Loc,
1721 Left_Opnd => Relocate_Node (Alist),
1722 Right_Opnd => Atest);
1724 Blist :=
1725 Make_Or_Else (Loc,
1726 Left_Opnd => Relocate_Node (Blist),
1727 Right_Opnd => Btest);
1728 end if;
1729 end loop;
1731 return
1732 Make_And_Then (Loc,
1733 Left_Opnd => Alist,
1734 Right_Opnd => Blist);
1735 end Test_Empty_Arrays;
1737 -----------------------------
1738 -- Test_Lengths_Correspond --
1739 -----------------------------
1741 function Test_Lengths_Correspond return Node_Id is
1742 Result : Node_Id;
1743 Rtest : Node_Id;
1745 begin
1746 Result := Empty;
1747 for J in 1 .. Number_Dimensions (Ltyp) loop
1748 Rtest :=
1749 Make_Op_Ne (Loc,
1750 Left_Opnd => Arr_Attr (A, Name_Length, J),
1751 Right_Opnd => Arr_Attr (B, Name_Length, J));
1753 if No (Result) then
1754 Result := Rtest;
1755 else
1756 Result :=
1757 Make_Or_Else (Loc,
1758 Left_Opnd => Relocate_Node (Result),
1759 Right_Opnd => Rtest);
1760 end if;
1761 end loop;
1763 return Result;
1764 end Test_Lengths_Correspond;
1766 -- Start of processing for Expand_Array_Equality
1768 begin
1769 Ltyp := Get_Arg_Type (Lhs);
1770 Rtyp := Get_Arg_Type (Rhs);
1772 -- For now, if the argument types are not the same, go to the base type,
1773 -- since the code assumes that the formals have the same type. This is
1774 -- fixable in future ???
1776 if Ltyp /= Rtyp then
1777 Ltyp := Base_Type (Ltyp);
1778 Rtyp := Base_Type (Rtyp);
1779 pragma Assert (Ltyp = Rtyp);
1780 end if;
1782 -- Build list of formals for function
1784 Formals := New_List (
1785 Make_Parameter_Specification (Loc,
1786 Defining_Identifier => A,
1787 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1789 Make_Parameter_Specification (Loc,
1790 Defining_Identifier => B,
1791 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1793 Func_Name := Make_Temporary (Loc, 'E');
1795 -- Build statement sequence for function
1797 Func_Body :=
1798 Make_Subprogram_Body (Loc,
1799 Specification =>
1800 Make_Function_Specification (Loc,
1801 Defining_Unit_Name => Func_Name,
1802 Parameter_Specifications => Formals,
1803 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1805 Declarations => Decls,
1807 Handled_Statement_Sequence =>
1808 Make_Handled_Sequence_Of_Statements (Loc,
1809 Statements => New_List (
1811 Make_Implicit_If_Statement (Nod,
1812 Condition => Test_Empty_Arrays,
1813 Then_Statements => New_List (
1814 Make_Simple_Return_Statement (Loc,
1815 Expression =>
1816 New_Occurrence_Of (Standard_True, Loc)))),
1818 Make_Implicit_If_Statement (Nod,
1819 Condition => Test_Lengths_Correspond,
1820 Then_Statements => New_List (
1821 Make_Simple_Return_Statement (Loc,
1822 Expression =>
1823 New_Occurrence_Of (Standard_False, Loc)))),
1825 Handle_One_Dimension (1, First_Index (Ltyp)),
1827 Make_Simple_Return_Statement (Loc,
1828 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1830 Set_Has_Completion (Func_Name, True);
1831 Set_Is_Inlined (Func_Name);
1833 -- If the array type is distinct from the type of the arguments, it
1834 -- is the full view of a private type. Apply an unchecked conversion
1835 -- to insure that analysis of the call succeeds.
1837 declare
1838 L, R : Node_Id;
1840 begin
1841 L := Lhs;
1842 R := Rhs;
1844 if No (Etype (Lhs))
1845 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1846 then
1847 L := OK_Convert_To (Ltyp, Lhs);
1848 end if;
1850 if No (Etype (Rhs))
1851 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1852 then
1853 R := OK_Convert_To (Rtyp, Rhs);
1854 end if;
1856 Actuals := New_List (L, R);
1857 end;
1859 Append_To (Bodies, Func_Body);
1861 return
1862 Make_Function_Call (Loc,
1863 Name => New_Reference_To (Func_Name, Loc),
1864 Parameter_Associations => Actuals);
1865 end Expand_Array_Equality;
1867 -----------------------------
1868 -- Expand_Boolean_Operator --
1869 -----------------------------
1871 -- Note that we first get the actual subtypes of the operands, since we
1872 -- always want to deal with types that have bounds.
1874 procedure Expand_Boolean_Operator (N : Node_Id) is
1875 Typ : constant Entity_Id := Etype (N);
1877 begin
1878 -- Special case of bit packed array where both operands are known to be
1879 -- properly aligned. In this case we use an efficient run time routine
1880 -- to carry out the operation (see System.Bit_Ops).
1882 if Is_Bit_Packed_Array (Typ)
1883 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1884 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1885 then
1886 Expand_Packed_Boolean_Operator (N);
1887 return;
1888 end if;
1890 -- For the normal non-packed case, the general expansion is to build
1891 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1892 -- and then inserting it into the tree. The original operator node is
1893 -- then rewritten as a call to this function. We also use this in the
1894 -- packed case if either operand is a possibly unaligned object.
1896 declare
1897 Loc : constant Source_Ptr := Sloc (N);
1898 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1899 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1900 Func_Body : Node_Id;
1901 Func_Name : Entity_Id;
1903 begin
1904 Convert_To_Actual_Subtype (L);
1905 Convert_To_Actual_Subtype (R);
1906 Ensure_Defined (Etype (L), N);
1907 Ensure_Defined (Etype (R), N);
1908 Apply_Length_Check (R, Etype (L));
1910 if Nkind (N) = N_Op_Xor then
1911 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1912 end if;
1914 if Nkind (Parent (N)) = N_Assignment_Statement
1915 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1916 then
1917 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1919 elsif Nkind (Parent (N)) = N_Op_Not
1920 and then Nkind (N) = N_Op_And
1921 and then
1922 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1923 then
1924 return;
1925 else
1927 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1928 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1929 Insert_Action (N, Func_Body);
1931 -- Now rewrite the expression with a call
1933 Rewrite (N,
1934 Make_Function_Call (Loc,
1935 Name => New_Reference_To (Func_Name, Loc),
1936 Parameter_Associations =>
1937 New_List (
1939 Make_Type_Conversion
1940 (Loc, New_Reference_To (Etype (L), Loc), R))));
1942 Analyze_And_Resolve (N, Typ);
1943 end if;
1944 end;
1945 end Expand_Boolean_Operator;
1947 -------------------------------
1948 -- Expand_Composite_Equality --
1949 -------------------------------
1951 -- This function is only called for comparing internal fields of composite
1952 -- types when these fields are themselves composites. This is a special
1953 -- case because it is not possible to respect normal Ada visibility rules.
1955 function Expand_Composite_Equality
1956 (Nod : Node_Id;
1957 Typ : Entity_Id;
1958 Lhs : Node_Id;
1959 Rhs : Node_Id;
1960 Bodies : List_Id) return Node_Id
1962 Loc : constant Source_Ptr := Sloc (Nod);
1963 Full_Type : Entity_Id;
1964 Prim : Elmt_Id;
1965 Eq_Op : Entity_Id;
1967 begin
1968 if Is_Private_Type (Typ) then
1969 Full_Type := Underlying_Type (Typ);
1970 else
1971 Full_Type := Typ;
1972 end if;
1974 -- Defense against malformed private types with no completion the error
1975 -- will be diagnosed later by check_completion
1977 if No (Full_Type) then
1978 return New_Reference_To (Standard_False, Loc);
1979 end if;
1981 Full_Type := Base_Type (Full_Type);
1983 if Is_Array_Type (Full_Type) then
1985 -- If the operand is an elementary type other than a floating-point
1986 -- type, then we can simply use the built-in block bitwise equality,
1987 -- since the predefined equality operators always apply and bitwise
1988 -- equality is fine for all these cases.
1990 if Is_Elementary_Type (Component_Type (Full_Type))
1991 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1992 then
1993 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1995 -- For composite component types, and floating-point types, use the
1996 -- expansion. This deals with tagged component types (where we use
1997 -- the applicable equality routine) and floating-point, (where we
1998 -- need to worry about negative zeroes), and also the case of any
1999 -- composite type recursively containing such fields.
2001 else
2002 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2003 end if;
2005 elsif Is_Tagged_Type (Full_Type) then
2007 -- Call the primitive operation "=" of this type
2009 if Is_Class_Wide_Type (Full_Type) then
2010 Full_Type := Root_Type (Full_Type);
2011 end if;
2013 -- If this is derived from an untagged private type completed with a
2014 -- tagged type, it does not have a full view, so we use the primitive
2015 -- operations of the private type. This check should no longer be
2016 -- necessary when these types receive their full views ???
2018 if Is_Private_Type (Typ)
2019 and then not Is_Tagged_Type (Typ)
2020 and then not Is_Controlled (Typ)
2021 and then Is_Derived_Type (Typ)
2022 and then No (Full_View (Typ))
2023 then
2024 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2025 else
2026 Prim := First_Elmt (Primitive_Operations (Full_Type));
2027 end if;
2029 loop
2030 Eq_Op := Node (Prim);
2031 exit when Chars (Eq_Op) = Name_Op_Eq
2032 and then Etype (First_Formal (Eq_Op)) =
2033 Etype (Next_Formal (First_Formal (Eq_Op)))
2034 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2035 Next_Elmt (Prim);
2036 pragma Assert (Present (Prim));
2037 end loop;
2039 Eq_Op := Node (Prim);
2041 return
2042 Make_Function_Call (Loc,
2043 Name => New_Reference_To (Eq_Op, Loc),
2044 Parameter_Associations =>
2045 New_List
2046 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2047 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2049 elsif Is_Record_Type (Full_Type) then
2050 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2052 if Present (Eq_Op) then
2053 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2055 -- Inherited equality from parent type. Convert the actuals to
2056 -- match signature of operation.
2058 declare
2059 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2061 begin
2062 return
2063 Make_Function_Call (Loc,
2064 Name => New_Reference_To (Eq_Op, Loc),
2065 Parameter_Associations =>
2066 New_List (OK_Convert_To (T, Lhs),
2067 OK_Convert_To (T, Rhs)));
2068 end;
2070 else
2071 -- Comparison between Unchecked_Union components
2073 if Is_Unchecked_Union (Full_Type) then
2074 declare
2075 Lhs_Type : Node_Id := Full_Type;
2076 Rhs_Type : Node_Id := Full_Type;
2077 Lhs_Discr_Val : Node_Id;
2078 Rhs_Discr_Val : Node_Id;
2080 begin
2081 -- Lhs subtype
2083 if Nkind (Lhs) = N_Selected_Component then
2084 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2085 end if;
2087 -- Rhs subtype
2089 if Nkind (Rhs) = N_Selected_Component then
2090 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2091 end if;
2093 -- Lhs of the composite equality
2095 if Is_Constrained (Lhs_Type) then
2097 -- Since the enclosing record type can never be an
2098 -- Unchecked_Union (this code is executed for records
2099 -- that do not have variants), we may reference its
2100 -- discriminant(s).
2102 if Nkind (Lhs) = N_Selected_Component
2103 and then Has_Per_Object_Constraint (
2104 Entity (Selector_Name (Lhs)))
2105 then
2106 Lhs_Discr_Val :=
2107 Make_Selected_Component (Loc,
2108 Prefix => Prefix (Lhs),
2109 Selector_Name =>
2110 New_Copy (
2111 Get_Discriminant_Value (
2112 First_Discriminant (Lhs_Type),
2113 Lhs_Type,
2114 Stored_Constraint (Lhs_Type))));
2116 else
2117 Lhs_Discr_Val := New_Copy (
2118 Get_Discriminant_Value (
2119 First_Discriminant (Lhs_Type),
2120 Lhs_Type,
2121 Stored_Constraint (Lhs_Type)));
2123 end if;
2124 else
2125 -- It is not possible to infer the discriminant since
2126 -- the subtype is not constrained.
2128 return
2129 Make_Raise_Program_Error (Loc,
2130 Reason => PE_Unchecked_Union_Restriction);
2131 end if;
2133 -- Rhs of the composite equality
2135 if Is_Constrained (Rhs_Type) then
2136 if Nkind (Rhs) = N_Selected_Component
2137 and then Has_Per_Object_Constraint (
2138 Entity (Selector_Name (Rhs)))
2139 then
2140 Rhs_Discr_Val :=
2141 Make_Selected_Component (Loc,
2142 Prefix => Prefix (Rhs),
2143 Selector_Name =>
2144 New_Copy (
2145 Get_Discriminant_Value (
2146 First_Discriminant (Rhs_Type),
2147 Rhs_Type,
2148 Stored_Constraint (Rhs_Type))));
2150 else
2151 Rhs_Discr_Val := New_Copy (
2152 Get_Discriminant_Value (
2153 First_Discriminant (Rhs_Type),
2154 Rhs_Type,
2155 Stored_Constraint (Rhs_Type)));
2157 end if;
2158 else
2159 return
2160 Make_Raise_Program_Error (Loc,
2161 Reason => PE_Unchecked_Union_Restriction);
2162 end if;
2164 -- Call the TSS equality function with the inferred
2165 -- discriminant values.
2167 return
2168 Make_Function_Call (Loc,
2169 Name => New_Reference_To (Eq_Op, Loc),
2170 Parameter_Associations => New_List (
2171 Lhs,
2172 Rhs,
2173 Lhs_Discr_Val,
2174 Rhs_Discr_Val));
2175 end;
2177 else
2178 return
2179 Make_Function_Call (Loc,
2180 Name => New_Reference_To (Eq_Op, Loc),
2181 Parameter_Associations => New_List (Lhs, Rhs));
2182 end if;
2183 end if;
2185 elsif Ada_Version >= Ada_2012 then
2187 -- if no TSS has been created for the type, check whether there is
2188 -- a primitive equality declared for it. If it is abstract replace
2189 -- the call with an explicit raise (AI05-0123).
2191 declare
2192 Prim : Elmt_Id;
2194 begin
2195 Prim := First_Elmt (Collect_Primitive_Operations (Full_Type));
2196 while Present (Prim) loop
2198 -- Locate primitive equality with the right signature
2200 if Chars (Node (Prim)) = Name_Op_Eq
2201 and then Etype (First_Formal (Node (Prim))) =
2202 Etype (Next_Formal (First_Formal (Node (Prim))))
2203 and then Etype (Node (Prim)) = Standard_Boolean
2204 then
2205 if Is_Abstract_Subprogram (Node (Prim)) then
2206 return
2207 Make_Raise_Program_Error (Loc,
2208 Reason => PE_Explicit_Raise);
2209 else
2210 return
2211 Make_Function_Call (Loc,
2212 Name => New_Reference_To (Node (Prim), Loc),
2213 Parameter_Associations => New_List (Lhs, Rhs));
2214 end if;
2215 end if;
2217 Next_Elmt (Prim);
2218 end loop;
2219 end;
2221 -- Use predefined equality iff no user-defined primitive exists
2223 return Make_Op_Eq (Loc, Lhs, Rhs);
2225 else
2226 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2227 end if;
2229 else
2230 -- If not array or record type, it is predefined equality.
2232 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2233 end if;
2234 end Expand_Composite_Equality;
2236 ------------------------
2237 -- Expand_Concatenate --
2238 ------------------------
2240 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2241 Loc : constant Source_Ptr := Sloc (Cnode);
2243 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2244 -- Result type of concatenation
2246 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2247 -- Component type. Elements of this component type can appear as one
2248 -- of the operands of concatenation as well as arrays.
2250 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2251 -- Index subtype
2253 Ityp : constant Entity_Id := Base_Type (Istyp);
2254 -- Index type. This is the base type of the index subtype, and is used
2255 -- for all computed bounds (which may be out of range of Istyp in the
2256 -- case of null ranges).
2258 Artyp : Entity_Id;
2259 -- This is the type we use to do arithmetic to compute the bounds and
2260 -- lengths of operands. The choice of this type is a little subtle and
2261 -- is discussed in a separate section at the start of the body code.
2263 Concatenation_Error : exception;
2264 -- Raised if concatenation is sure to raise a CE
2266 Result_May_Be_Null : Boolean := True;
2267 -- Reset to False if at least one operand is encountered which is known
2268 -- at compile time to be non-null. Used for handling the special case
2269 -- of setting the high bound to the last operand high bound for a null
2270 -- result, thus ensuring a proper high bound in the super-flat case.
2272 N : constant Nat := List_Length (Opnds);
2273 -- Number of concatenation operands including possibly null operands
2275 NN : Nat := 0;
2276 -- Number of operands excluding any known to be null, except that the
2277 -- last operand is always retained, in case it provides the bounds for
2278 -- a null result.
2280 Opnd : Node_Id;
2281 -- Current operand being processed in the loop through operands. After
2282 -- this loop is complete, always contains the last operand (which is not
2283 -- the same as Operands (NN), since null operands are skipped).
2285 -- Arrays describing the operands, only the first NN entries of each
2286 -- array are set (NN < N when we exclude known null operands).
2288 Is_Fixed_Length : array (1 .. N) of Boolean;
2289 -- True if length of corresponding operand known at compile time
2291 Operands : array (1 .. N) of Node_Id;
2292 -- Set to the corresponding entry in the Opnds list (but note that null
2293 -- operands are excluded, so not all entries in the list are stored).
2295 Fixed_Length : array (1 .. N) of Uint;
2296 -- Set to length of operand. Entries in this array are set only if the
2297 -- corresponding entry in Is_Fixed_Length is True.
2299 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2300 -- Set to lower bound of operand. Either an integer literal in the case
2301 -- where the bound is known at compile time, else actual lower bound.
2302 -- The operand low bound is of type Ityp.
2304 Var_Length : array (1 .. N) of Entity_Id;
2305 -- Set to an entity of type Natural that contains the length of an
2306 -- operand whose length is not known at compile time. Entries in this
2307 -- array are set only if the corresponding entry in Is_Fixed_Length
2308 -- is False. The entity is of type Artyp.
2310 Aggr_Length : array (0 .. N) of Node_Id;
2311 -- The J'th entry in an expression node that represents the total length
2312 -- of operands 1 through J. It is either an integer literal node, or a
2313 -- reference to a constant entity with the right value, so it is fine
2314 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2315 -- entry always is set to zero. The length is of type Artyp.
2317 Low_Bound : Node_Id;
2318 -- A tree node representing the low bound of the result (of type Ityp).
2319 -- This is either an integer literal node, or an identifier reference to
2320 -- a constant entity initialized to the appropriate value.
2322 Last_Opnd_High_Bound : Node_Id;
2323 -- A tree node representing the high bound of the last operand. This
2324 -- need only be set if the result could be null. It is used for the
2325 -- special case of setting the right high bound for a null result.
2326 -- This is of type Ityp.
2328 High_Bound : Node_Id;
2329 -- A tree node representing the high bound of the result (of type Ityp)
2331 Result : Node_Id;
2332 -- Result of the concatenation (of type Ityp)
2334 Actions : constant List_Id := New_List;
2335 -- Collect actions to be inserted if Save_Space is False
2337 Save_Space : Boolean;
2338 pragma Warnings (Off, Save_Space);
2339 -- Set to True if we are saving generated code space by calling routines
2340 -- in packages System.Concat_n.
2342 Known_Non_Null_Operand_Seen : Boolean;
2343 -- Set True during generation of the assignments of operands into
2344 -- result once an operand known to be non-null has been seen.
2346 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2347 -- This function makes an N_Integer_Literal node that is returned in
2348 -- analyzed form with the type set to Artyp. Importantly this literal
2349 -- is not flagged as static, so that if we do computations with it that
2350 -- result in statically detected out of range conditions, we will not
2351 -- generate error messages but instead warning messages.
2353 function To_Artyp (X : Node_Id) return Node_Id;
2354 -- Given a node of type Ityp, returns the corresponding value of type
2355 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2356 -- For enum types, the Pos of the value is returned.
2358 function To_Ityp (X : Node_Id) return Node_Id;
2359 -- The inverse function (uses Val in the case of enumeration types)
2361 ------------------------
2362 -- Make_Artyp_Literal --
2363 ------------------------
2365 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2366 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2367 begin
2368 Set_Etype (Result, Artyp);
2369 Set_Analyzed (Result, True);
2370 Set_Is_Static_Expression (Result, False);
2371 return Result;
2372 end Make_Artyp_Literal;
2374 --------------
2375 -- To_Artyp --
2376 --------------
2378 function To_Artyp (X : Node_Id) return Node_Id is
2379 begin
2380 if Ityp = Base_Type (Artyp) then
2381 return X;
2383 elsif Is_Enumeration_Type (Ityp) then
2384 return
2385 Make_Attribute_Reference (Loc,
2386 Prefix => New_Occurrence_Of (Ityp, Loc),
2387 Attribute_Name => Name_Pos,
2388 Expressions => New_List (X));
2390 else
2391 return Convert_To (Artyp, X);
2392 end if;
2393 end To_Artyp;
2395 -------------
2396 -- To_Ityp --
2397 -------------
2399 function To_Ityp (X : Node_Id) return Node_Id is
2400 begin
2401 if Is_Enumeration_Type (Ityp) then
2402 return
2403 Make_Attribute_Reference (Loc,
2404 Prefix => New_Occurrence_Of (Ityp, Loc),
2405 Attribute_Name => Name_Val,
2406 Expressions => New_List (X));
2408 -- Case where we will do a type conversion
2410 else
2411 if Ityp = Base_Type (Artyp) then
2412 return X;
2413 else
2414 return Convert_To (Ityp, X);
2415 end if;
2416 end if;
2417 end To_Ityp;
2419 -- Local Declarations
2421 Opnd_Typ : Entity_Id;
2422 Ent : Entity_Id;
2423 Len : Uint;
2424 J : Nat;
2425 Clen : Node_Id;
2426 Set : Boolean;
2428 begin
2429 -- Choose an appropriate computational type
2431 -- We will be doing calculations of lengths and bounds in this routine
2432 -- and computing one from the other in some cases, e.g. getting the high
2433 -- bound by adding the length-1 to the low bound.
2435 -- We can't just use the index type, or even its base type for this
2436 -- purpose for two reasons. First it might be an enumeration type which
2437 -- is not suitable for computations of any kind, and second it may
2438 -- simply not have enough range. For example if the index type is
2439 -- -128..+127 then lengths can be up to 256, which is out of range of
2440 -- the type.
2442 -- For enumeration types, we can simply use Standard_Integer, this is
2443 -- sufficient since the actual number of enumeration literals cannot
2444 -- possibly exceed the range of integer (remember we will be doing the
2445 -- arithmetic with POS values, not representation values).
2447 if Is_Enumeration_Type (Ityp) then
2448 Artyp := Standard_Integer;
2450 -- If index type is Positive, we use the standard unsigned type, to give
2451 -- more room on the top of the range, obviating the need for an overflow
2452 -- check when creating the upper bound. This is needed to avoid junk
2453 -- overflow checks in the common case of String types.
2455 -- ??? Disabled for now
2457 -- elsif Istyp = Standard_Positive then
2458 -- Artyp := Standard_Unsigned;
2460 -- For modular types, we use a 32-bit modular type for types whose size
2461 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2462 -- identity type, and for larger unsigned types we use 64-bits.
2464 elsif Is_Modular_Integer_Type (Ityp) then
2465 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2466 Artyp := Standard_Unsigned;
2467 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2468 Artyp := Ityp;
2469 else
2470 Artyp := RTE (RE_Long_Long_Unsigned);
2471 end if;
2473 -- Similar treatment for signed types
2475 else
2476 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2477 Artyp := Standard_Integer;
2478 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2479 Artyp := Ityp;
2480 else
2481 Artyp := Standard_Long_Long_Integer;
2482 end if;
2483 end if;
2485 -- Supply dummy entry at start of length array
2487 Aggr_Length (0) := Make_Artyp_Literal (0);
2489 -- Go through operands setting up the above arrays
2491 J := 1;
2492 while J <= N loop
2493 Opnd := Remove_Head (Opnds);
2494 Opnd_Typ := Etype (Opnd);
2496 -- The parent got messed up when we put the operands in a list,
2497 -- so now put back the proper parent for the saved operand, that
2498 -- is to say the concatenation node, to make sure that each operand
2499 -- is seen as a subexpression, e.g. if actions must be inserted.
2501 Set_Parent (Opnd, Cnode);
2503 -- Set will be True when we have setup one entry in the array
2505 Set := False;
2507 -- Singleton element (or character literal) case
2509 if Base_Type (Opnd_Typ) = Ctyp then
2510 NN := NN + 1;
2511 Operands (NN) := Opnd;
2512 Is_Fixed_Length (NN) := True;
2513 Fixed_Length (NN) := Uint_1;
2514 Result_May_Be_Null := False;
2516 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2517 -- since we know that the result cannot be null).
2519 Opnd_Low_Bound (NN) :=
2520 Make_Attribute_Reference (Loc,
2521 Prefix => New_Reference_To (Istyp, Loc),
2522 Attribute_Name => Name_First);
2524 Set := True;
2526 -- String literal case (can only occur for strings of course)
2528 elsif Nkind (Opnd) = N_String_Literal then
2529 Len := String_Literal_Length (Opnd_Typ);
2531 if Len /= 0 then
2532 Result_May_Be_Null := False;
2533 end if;
2535 -- Capture last operand high bound if result could be null
2537 if J = N and then Result_May_Be_Null then
2538 Last_Opnd_High_Bound :=
2539 Make_Op_Add (Loc,
2540 Left_Opnd =>
2541 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2542 Right_Opnd => Make_Integer_Literal (Loc, 1));
2543 end if;
2545 -- Skip null string literal
2547 if J < N and then Len = 0 then
2548 goto Continue;
2549 end if;
2551 NN := NN + 1;
2552 Operands (NN) := Opnd;
2553 Is_Fixed_Length (NN) := True;
2555 -- Set length and bounds
2557 Fixed_Length (NN) := Len;
2559 Opnd_Low_Bound (NN) :=
2560 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2562 Set := True;
2564 -- All other cases
2566 else
2567 -- Check constrained case with known bounds
2569 if Is_Constrained (Opnd_Typ) then
2570 declare
2571 Index : constant Node_Id := First_Index (Opnd_Typ);
2572 Indx_Typ : constant Entity_Id := Etype (Index);
2573 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2574 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2576 begin
2577 -- Fixed length constrained array type with known at compile
2578 -- time bounds is last case of fixed length operand.
2580 if Compile_Time_Known_Value (Lo)
2581 and then
2582 Compile_Time_Known_Value (Hi)
2583 then
2584 declare
2585 Loval : constant Uint := Expr_Value (Lo);
2586 Hival : constant Uint := Expr_Value (Hi);
2587 Len : constant Uint :=
2588 UI_Max (Hival - Loval + 1, Uint_0);
2590 begin
2591 if Len > 0 then
2592 Result_May_Be_Null := False;
2593 end if;
2595 -- Capture last operand bound if result could be null
2597 if J = N and then Result_May_Be_Null then
2598 Last_Opnd_High_Bound :=
2599 Convert_To (Ityp,
2600 Make_Integer_Literal (Loc,
2601 Intval => Expr_Value (Hi)));
2602 end if;
2604 -- Exclude null length case unless last operand
2606 if J < N and then Len = 0 then
2607 goto Continue;
2608 end if;
2610 NN := NN + 1;
2611 Operands (NN) := Opnd;
2612 Is_Fixed_Length (NN) := True;
2613 Fixed_Length (NN) := Len;
2615 Opnd_Low_Bound (NN) := To_Ityp (
2616 Make_Integer_Literal (Loc,
2617 Intval => Expr_Value (Lo)));
2619 Set := True;
2620 end;
2621 end if;
2622 end;
2623 end if;
2625 -- All cases where the length is not known at compile time, or the
2626 -- special case of an operand which is known to be null but has a
2627 -- lower bound other than 1 or is other than a string type.
2629 if not Set then
2630 NN := NN + 1;
2632 -- Capture operand bounds
2634 Opnd_Low_Bound (NN) :=
2635 Make_Attribute_Reference (Loc,
2636 Prefix =>
2637 Duplicate_Subexpr (Opnd, Name_Req => True),
2638 Attribute_Name => Name_First);
2640 if J = N and Result_May_Be_Null then
2641 Last_Opnd_High_Bound :=
2642 Convert_To (Ityp,
2643 Make_Attribute_Reference (Loc,
2644 Prefix =>
2645 Duplicate_Subexpr (Opnd, Name_Req => True),
2646 Attribute_Name => Name_Last));
2647 end if;
2649 -- Capture length of operand in entity
2651 Operands (NN) := Opnd;
2652 Is_Fixed_Length (NN) := False;
2654 Var_Length (NN) := Make_Temporary (Loc, 'L');
2656 Append_To (Actions,
2657 Make_Object_Declaration (Loc,
2658 Defining_Identifier => Var_Length (NN),
2659 Constant_Present => True,
2661 Object_Definition =>
2662 New_Occurrence_Of (Artyp, Loc),
2664 Expression =>
2665 Make_Attribute_Reference (Loc,
2666 Prefix =>
2667 Duplicate_Subexpr (Opnd, Name_Req => True),
2668 Attribute_Name => Name_Length)));
2669 end if;
2670 end if;
2672 -- Set next entry in aggregate length array
2674 -- For first entry, make either integer literal for fixed length
2675 -- or a reference to the saved length for variable length.
2677 if NN = 1 then
2678 if Is_Fixed_Length (1) then
2679 Aggr_Length (1) :=
2680 Make_Integer_Literal (Loc,
2681 Intval => Fixed_Length (1));
2682 else
2683 Aggr_Length (1) :=
2684 New_Reference_To (Var_Length (1), Loc);
2685 end if;
2687 -- If entry is fixed length and only fixed lengths so far, make
2688 -- appropriate new integer literal adding new length.
2690 elsif Is_Fixed_Length (NN)
2691 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2692 then
2693 Aggr_Length (NN) :=
2694 Make_Integer_Literal (Loc,
2695 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2697 -- All other cases, construct an addition node for the length and
2698 -- create an entity initialized to this length.
2700 else
2701 Ent := Make_Temporary (Loc, 'L');
2703 if Is_Fixed_Length (NN) then
2704 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2705 else
2706 Clen := New_Reference_To (Var_Length (NN), Loc);
2707 end if;
2709 Append_To (Actions,
2710 Make_Object_Declaration (Loc,
2711 Defining_Identifier => Ent,
2712 Constant_Present => True,
2714 Object_Definition =>
2715 New_Occurrence_Of (Artyp, Loc),
2717 Expression =>
2718 Make_Op_Add (Loc,
2719 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2720 Right_Opnd => Clen)));
2722 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2723 end if;
2725 <<Continue>>
2726 J := J + 1;
2727 end loop;
2729 -- If we have only skipped null operands, return the last operand
2731 if NN = 0 then
2732 Result := Opnd;
2733 goto Done;
2734 end if;
2736 -- If we have only one non-null operand, return it and we are done.
2737 -- There is one case in which this cannot be done, and that is when
2738 -- the sole operand is of the element type, in which case it must be
2739 -- converted to an array, and the easiest way of doing that is to go
2740 -- through the normal general circuit.
2742 if NN = 1
2743 and then Base_Type (Etype (Operands (1))) /= Ctyp
2744 then
2745 Result := Operands (1);
2746 goto Done;
2747 end if;
2749 -- Cases where we have a real concatenation
2751 -- Next step is to find the low bound for the result array that we
2752 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2754 -- If the ultimate ancestor of the index subtype is a constrained array
2755 -- definition, then the lower bound is that of the index subtype as
2756 -- specified by (RM 4.5.3(6)).
2758 -- The right test here is to go to the root type, and then the ultimate
2759 -- ancestor is the first subtype of this root type.
2761 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2762 Low_Bound :=
2763 Make_Attribute_Reference (Loc,
2764 Prefix =>
2765 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2766 Attribute_Name => Name_First);
2768 -- If the first operand in the list has known length we know that
2769 -- the lower bound of the result is the lower bound of this operand.
2771 elsif Is_Fixed_Length (1) then
2772 Low_Bound := Opnd_Low_Bound (1);
2774 -- OK, we don't know the lower bound, we have to build a horrible
2775 -- expression actions node of the form
2777 -- if Cond1'Length /= 0 then
2778 -- Opnd1 low bound
2779 -- else
2780 -- if Opnd2'Length /= 0 then
2781 -- Opnd2 low bound
2782 -- else
2783 -- ...
2785 -- The nesting ends either when we hit an operand whose length is known
2786 -- at compile time, or on reaching the last operand, whose low bound we
2787 -- take unconditionally whether or not it is null. It's easiest to do
2788 -- this with a recursive procedure:
2790 else
2791 declare
2792 function Get_Known_Bound (J : Nat) return Node_Id;
2793 -- Returns the lower bound determined by operands J .. NN
2795 ---------------------
2796 -- Get_Known_Bound --
2797 ---------------------
2799 function Get_Known_Bound (J : Nat) return Node_Id is
2800 begin
2801 if Is_Fixed_Length (J) or else J = NN then
2802 return New_Copy (Opnd_Low_Bound (J));
2804 else
2805 return
2806 Make_Conditional_Expression (Loc,
2807 Expressions => New_List (
2809 Make_Op_Ne (Loc,
2810 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2811 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2813 New_Copy (Opnd_Low_Bound (J)),
2814 Get_Known_Bound (J + 1)));
2815 end if;
2816 end Get_Known_Bound;
2818 begin
2819 Ent := Make_Temporary (Loc, 'L');
2821 Append_To (Actions,
2822 Make_Object_Declaration (Loc,
2823 Defining_Identifier => Ent,
2824 Constant_Present => True,
2825 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2826 Expression => Get_Known_Bound (1)));
2828 Low_Bound := New_Reference_To (Ent, Loc);
2829 end;
2830 end if;
2832 -- Now we can safely compute the upper bound, normally
2833 -- Low_Bound + Length - 1.
2835 High_Bound :=
2836 To_Ityp (
2837 Make_Op_Add (Loc,
2838 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2839 Right_Opnd =>
2840 Make_Op_Subtract (Loc,
2841 Left_Opnd => New_Copy (Aggr_Length (NN)),
2842 Right_Opnd => Make_Artyp_Literal (1))));
2844 -- Note that calculation of the high bound may cause overflow in some
2845 -- very weird cases, so in the general case we need an overflow check on
2846 -- the high bound. We can avoid this for the common case of string types
2847 -- and other types whose index is Positive, since we chose a wider range
2848 -- for the arithmetic type.
2850 if Istyp /= Standard_Positive then
2851 Activate_Overflow_Check (High_Bound);
2852 end if;
2854 -- Handle the exceptional case where the result is null, in which case
2855 -- case the bounds come from the last operand (so that we get the proper
2856 -- bounds if the last operand is super-flat).
2858 if Result_May_Be_Null then
2859 High_Bound :=
2860 Make_Conditional_Expression (Loc,
2861 Expressions => New_List (
2862 Make_Op_Eq (Loc,
2863 Left_Opnd => New_Copy (Aggr_Length (NN)),
2864 Right_Opnd => Make_Artyp_Literal (0)),
2865 Last_Opnd_High_Bound,
2866 High_Bound));
2867 end if;
2869 -- Here is where we insert the saved up actions
2871 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
2873 -- Now we construct an array object with appropriate bounds. We mark
2874 -- the target as internal to prevent useless initialization when
2875 -- Initialize_Scalars is enabled.
2877 Ent := Make_Temporary (Loc, 'S');
2878 Set_Is_Internal (Ent);
2880 -- If the bound is statically known to be out of range, we do not want
2881 -- to abort, we want a warning and a runtime constraint error. Note that
2882 -- we have arranged that the result will not be treated as a static
2883 -- constant, so we won't get an illegality during this insertion.
2885 Insert_Action (Cnode,
2886 Make_Object_Declaration (Loc,
2887 Defining_Identifier => Ent,
2888 Object_Definition =>
2889 Make_Subtype_Indication (Loc,
2890 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2891 Constraint =>
2892 Make_Index_Or_Discriminant_Constraint (Loc,
2893 Constraints => New_List (
2894 Make_Range (Loc,
2895 Low_Bound => Low_Bound,
2896 High_Bound => High_Bound))))),
2897 Suppress => All_Checks);
2899 -- If the result of the concatenation appears as the initializing
2900 -- expression of an object declaration, we can just rename the
2901 -- result, rather than copying it.
2903 Set_OK_To_Rename (Ent);
2905 -- Catch the static out of range case now
2907 if Raises_Constraint_Error (High_Bound) then
2908 raise Concatenation_Error;
2909 end if;
2911 -- Now we will generate the assignments to do the actual concatenation
2913 -- There is one case in which we will not do this, namely when all the
2914 -- following conditions are met:
2916 -- The result type is Standard.String
2918 -- There are nine or fewer retained (non-null) operands
2920 -- The optimization level is -O0
2922 -- The corresponding System.Concat_n.Str_Concat_n routine is
2923 -- available in the run time.
2925 -- The debug flag gnatd.c is not set
2927 -- If all these conditions are met then we generate a call to the
2928 -- relevant concatenation routine. The purpose of this is to avoid
2929 -- undesirable code bloat at -O0.
2931 if Atyp = Standard_String
2932 and then NN in 2 .. 9
2933 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
2934 and then not Debug_Flag_Dot_C
2935 then
2936 declare
2937 RR : constant array (Nat range 2 .. 9) of RE_Id :=
2938 (RE_Str_Concat_2,
2939 RE_Str_Concat_3,
2940 RE_Str_Concat_4,
2941 RE_Str_Concat_5,
2942 RE_Str_Concat_6,
2943 RE_Str_Concat_7,
2944 RE_Str_Concat_8,
2945 RE_Str_Concat_9);
2947 begin
2948 if RTE_Available (RR (NN)) then
2949 declare
2950 Opnds : constant List_Id :=
2951 New_List (New_Occurrence_Of (Ent, Loc));
2953 begin
2954 for J in 1 .. NN loop
2955 if Is_List_Member (Operands (J)) then
2956 Remove (Operands (J));
2957 end if;
2959 if Base_Type (Etype (Operands (J))) = Ctyp then
2960 Append_To (Opnds,
2961 Make_Aggregate (Loc,
2962 Component_Associations => New_List (
2963 Make_Component_Association (Loc,
2964 Choices => New_List (
2965 Make_Integer_Literal (Loc, 1)),
2966 Expression => Operands (J)))));
2968 else
2969 Append_To (Opnds, Operands (J));
2970 end if;
2971 end loop;
2973 Insert_Action (Cnode,
2974 Make_Procedure_Call_Statement (Loc,
2975 Name => New_Reference_To (RTE (RR (NN)), Loc),
2976 Parameter_Associations => Opnds));
2978 Result := New_Reference_To (Ent, Loc);
2979 goto Done;
2980 end;
2981 end if;
2982 end;
2983 end if;
2985 -- Not special case so generate the assignments
2987 Known_Non_Null_Operand_Seen := False;
2989 for J in 1 .. NN loop
2990 declare
2991 Lo : constant Node_Id :=
2992 Make_Op_Add (Loc,
2993 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2994 Right_Opnd => Aggr_Length (J - 1));
2996 Hi : constant Node_Id :=
2997 Make_Op_Add (Loc,
2998 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2999 Right_Opnd =>
3000 Make_Op_Subtract (Loc,
3001 Left_Opnd => Aggr_Length (J),
3002 Right_Opnd => Make_Artyp_Literal (1)));
3004 begin
3005 -- Singleton case, simple assignment
3007 if Base_Type (Etype (Operands (J))) = Ctyp then
3008 Known_Non_Null_Operand_Seen := True;
3009 Insert_Action (Cnode,
3010 Make_Assignment_Statement (Loc,
3011 Name =>
3012 Make_Indexed_Component (Loc,
3013 Prefix => New_Occurrence_Of (Ent, Loc),
3014 Expressions => New_List (To_Ityp (Lo))),
3015 Expression => Operands (J)),
3016 Suppress => All_Checks);
3018 -- Array case, slice assignment, skipped when argument is fixed
3019 -- length and known to be null.
3021 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3022 declare
3023 Assign : Node_Id :=
3024 Make_Assignment_Statement (Loc,
3025 Name =>
3026 Make_Slice (Loc,
3027 Prefix =>
3028 New_Occurrence_Of (Ent, Loc),
3029 Discrete_Range =>
3030 Make_Range (Loc,
3031 Low_Bound => To_Ityp (Lo),
3032 High_Bound => To_Ityp (Hi))),
3033 Expression => Operands (J));
3034 begin
3035 if Is_Fixed_Length (J) then
3036 Known_Non_Null_Operand_Seen := True;
3038 elsif not Known_Non_Null_Operand_Seen then
3040 -- Here if operand length is not statically known and no
3041 -- operand known to be non-null has been processed yet.
3042 -- If operand length is 0, we do not need to perform the
3043 -- assignment, and we must avoid the evaluation of the
3044 -- high bound of the slice, since it may underflow if the
3045 -- low bound is Ityp'First.
3047 Assign :=
3048 Make_Implicit_If_Statement (Cnode,
3049 Condition =>
3050 Make_Op_Ne (Loc,
3051 Left_Opnd =>
3052 New_Occurrence_Of (Var_Length (J), Loc),
3053 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3054 Then_Statements =>
3055 New_List (Assign));
3056 end if;
3058 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3059 end;
3060 end if;
3061 end;
3062 end loop;
3064 -- Finally we build the result, which is a reference to the array object
3066 Result := New_Reference_To (Ent, Loc);
3068 <<Done>>
3069 Rewrite (Cnode, Result);
3070 Analyze_And_Resolve (Cnode, Atyp);
3072 exception
3073 when Concatenation_Error =>
3075 -- Kill warning generated for the declaration of the static out of
3076 -- range high bound, and instead generate a Constraint_Error with
3077 -- an appropriate specific message.
3079 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3080 Apply_Compile_Time_Constraint_Error
3081 (N => Cnode,
3082 Msg => "concatenation result upper bound out of range?",
3083 Reason => CE_Range_Check_Failed);
3084 -- Set_Etype (Cnode, Atyp);
3085 end Expand_Concatenate;
3087 ------------------------
3088 -- Expand_N_Allocator --
3089 ------------------------
3091 procedure Expand_N_Allocator (N : Node_Id) is
3092 PtrT : constant Entity_Id := Etype (N);
3093 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3094 Etyp : constant Entity_Id := Etype (Expression (N));
3095 Loc : constant Source_Ptr := Sloc (N);
3096 Desig : Entity_Id;
3097 Temp : Entity_Id;
3098 Nod : Node_Id;
3100 procedure Complete_Coextension_Finalization;
3101 -- Generate finalization calls for all nested coextensions of N. This
3102 -- routine may allocate list controllers if necessary.
3104 procedure Rewrite_Coextension (N : Node_Id);
3105 -- Static coextensions have the same lifetime as the entity they
3106 -- constrain. Such occurrences can be rewritten as aliased objects
3107 -- and their unrestricted access used instead of the coextension.
3109 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3110 -- Given a constrained array type E, returns a node representing the
3111 -- code to compute the size in storage elements for the given type.
3112 -- This is done without using the attribute (which malfunctions for
3113 -- large sizes ???)
3115 ---------------------------------------
3116 -- Complete_Coextension_Finalization --
3117 ---------------------------------------
3119 procedure Complete_Coextension_Finalization is
3120 Coext : Node_Id;
3121 Coext_Elmt : Elmt_Id;
3122 Flist : Node_Id;
3123 Ref : Node_Id;
3125 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
3126 -- Determine whether node N is part of a return statement
3128 function Needs_Initialization_Call (N : Node_Id) return Boolean;
3129 -- Determine whether node N is a subtype indicator allocator which
3130 -- acts a coextension. Such coextensions need initialization.
3132 -------------------------------
3133 -- Inside_A_Return_Statement --
3134 -------------------------------
3136 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
3137 P : Node_Id;
3139 begin
3140 P := Parent (N);
3141 while Present (P) loop
3142 if Nkind_In
3143 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
3144 then
3145 return True;
3147 -- Stop the traversal when we reach a subprogram body
3149 elsif Nkind (P) = N_Subprogram_Body then
3150 return False;
3151 end if;
3153 P := Parent (P);
3154 end loop;
3156 return False;
3157 end Inside_A_Return_Statement;
3159 -------------------------------
3160 -- Needs_Initialization_Call --
3161 -------------------------------
3163 function Needs_Initialization_Call (N : Node_Id) return Boolean is
3164 Obj_Decl : Node_Id;
3166 begin
3167 if Nkind (N) = N_Explicit_Dereference
3168 and then Nkind (Prefix (N)) = N_Identifier
3169 and then Nkind (Parent (Entity (Prefix (N)))) =
3170 N_Object_Declaration
3171 then
3172 Obj_Decl := Parent (Entity (Prefix (N)));
3174 return
3175 Present (Expression (Obj_Decl))
3176 and then Nkind (Expression (Obj_Decl)) = N_Allocator
3177 and then Nkind (Expression (Expression (Obj_Decl))) /=
3178 N_Qualified_Expression;
3179 end if;
3181 return False;
3182 end Needs_Initialization_Call;
3184 -- Start of processing for Complete_Coextension_Finalization
3186 begin
3187 -- When a coextension root is inside a return statement, we need to
3188 -- use the finalization chain of the function's scope. This does not
3189 -- apply for controlled named access types because in those cases we
3190 -- can use the finalization chain of the type itself.
3192 if Inside_A_Return_Statement (N)
3193 and then
3194 (Ekind (PtrT) = E_Anonymous_Access_Type
3195 or else
3196 (Ekind (PtrT) = E_Access_Type
3197 and then No (Associated_Final_Chain (PtrT))))
3198 then
3199 declare
3200 Decl : Node_Id;
3201 Outer_S : Entity_Id;
3202 S : Entity_Id;
3204 begin
3205 S := Current_Scope;
3206 while Present (S) and then S /= Standard_Standard loop
3207 if Ekind (S) = E_Function then
3208 Outer_S := Scope (S);
3210 -- Retrieve the declaration of the body
3212 Decl :=
3213 Parent
3214 (Parent
3215 (Corresponding_Body (Parent (Parent (S)))));
3216 exit;
3217 end if;
3219 S := Scope (S);
3220 end loop;
3222 -- Push the scope of the function body since we are inserting
3223 -- the list before the body, but we are currently in the body
3224 -- itself. Override the finalization list of PtrT since the
3225 -- finalization context is now different.
3227 Push_Scope (Outer_S);
3228 Build_Final_List (Decl, PtrT);
3229 Pop_Scope;
3230 end;
3232 -- The root allocator may not be controlled, but it still needs a
3233 -- finalization list for all nested coextensions.
3235 elsif No (Associated_Final_Chain (PtrT)) then
3236 Build_Final_List (N, PtrT);
3237 end if;
3239 Flist :=
3240 Make_Selected_Component (Loc,
3241 Prefix =>
3242 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
3243 Selector_Name => Make_Identifier (Loc, Name_F));
3245 Coext_Elmt := First_Elmt (Coextensions (N));
3246 while Present (Coext_Elmt) loop
3247 Coext := Node (Coext_Elmt);
3249 -- Generate:
3250 -- typ! (coext.all)
3252 if Nkind (Coext) = N_Identifier then
3253 Ref :=
3254 Make_Unchecked_Type_Conversion (Loc,
3255 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
3256 Expression =>
3257 Make_Explicit_Dereference (Loc,
3258 Prefix => New_Copy_Tree (Coext)));
3259 else
3260 Ref := New_Copy_Tree (Coext);
3261 end if;
3263 -- No initialization call if not allowed
3265 Check_Restriction (No_Default_Initialization, N);
3267 if not Restriction_Active (No_Default_Initialization) then
3269 -- Generate:
3270 -- initialize (Ref)
3271 -- attach_to_final_list (Ref, Flist, 2)
3273 if Needs_Initialization_Call (Coext) then
3274 Insert_Actions (N,
3275 Make_Init_Call (
3276 Ref => Ref,
3277 Typ => Etype (Coext),
3278 Flist_Ref => Flist,
3279 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3281 -- Generate:
3282 -- attach_to_final_list (Ref, Flist, 2)
3284 else
3285 Insert_Action (N,
3286 Make_Attach_Call (
3287 Obj_Ref => Ref,
3288 Flist_Ref => New_Copy_Tree (Flist),
3289 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3290 end if;
3291 end if;
3293 Next_Elmt (Coext_Elmt);
3294 end loop;
3295 end Complete_Coextension_Finalization;
3297 -------------------------
3298 -- Rewrite_Coextension --
3299 -------------------------
3301 procedure Rewrite_Coextension (N : Node_Id) is
3302 Temp : constant Node_Id := Make_Temporary (Loc, 'C');
3304 -- Generate:
3305 -- Cnn : aliased Etyp;
3307 Decl : constant Node_Id :=
3308 Make_Object_Declaration (Loc,
3309 Defining_Identifier => Temp,
3310 Aliased_Present => True,
3311 Object_Definition =>
3312 New_Occurrence_Of (Etyp, Loc));
3313 Nod : Node_Id;
3315 begin
3316 if Nkind (Expression (N)) = N_Qualified_Expression then
3317 Set_Expression (Decl, Expression (Expression (N)));
3318 end if;
3320 -- Find the proper insertion node for the declaration
3322 Nod := Parent (N);
3323 while Present (Nod) loop
3324 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3325 or else Nkind (Nod) = N_Procedure_Call_Statement
3326 or else Nkind (Nod) in N_Declaration;
3327 Nod := Parent (Nod);
3328 end loop;
3330 Insert_Before (Nod, Decl);
3331 Analyze (Decl);
3333 Rewrite (N,
3334 Make_Attribute_Reference (Loc,
3335 Prefix => New_Occurrence_Of (Temp, Loc),
3336 Attribute_Name => Name_Unrestricted_Access));
3338 Analyze_And_Resolve (N, PtrT);
3339 end Rewrite_Coextension;
3341 ------------------------------
3342 -- Size_In_Storage_Elements --
3343 ------------------------------
3345 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3346 begin
3347 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3348 -- However, the reason for the existence of this function is
3349 -- to construct a test for sizes too large, which means near the
3350 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3351 -- is that we get overflows when sizes are greater than 2**31.
3353 -- So what we end up doing for array types is to use the expression:
3355 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3357 -- which avoids this problem. All this is a bit bogus, but it does
3358 -- mean we catch common cases of trying to allocate arrays that
3359 -- are too large, and which in the absence of a check results in
3360 -- undetected chaos ???
3362 declare
3363 Len : Node_Id;
3364 Res : Node_Id;
3366 begin
3367 for J in 1 .. Number_Dimensions (E) loop
3368 Len :=
3369 Make_Attribute_Reference (Loc,
3370 Prefix => New_Occurrence_Of (E, Loc),
3371 Attribute_Name => Name_Length,
3372 Expressions => New_List (
3373 Make_Integer_Literal (Loc, J)));
3375 if J = 1 then
3376 Res := Len;
3378 else
3379 Res :=
3380 Make_Op_Multiply (Loc,
3381 Left_Opnd => Res,
3382 Right_Opnd => Len);
3383 end if;
3384 end loop;
3386 return
3387 Make_Op_Multiply (Loc,
3388 Left_Opnd => Len,
3389 Right_Opnd =>
3390 Make_Attribute_Reference (Loc,
3391 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3392 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3393 end;
3394 end Size_In_Storage_Elements;
3396 -- Start of processing for Expand_N_Allocator
3398 begin
3399 -- RM E.2.3(22). We enforce that the expected type of an allocator
3400 -- shall not be a remote access-to-class-wide-limited-private type
3402 -- Why is this being done at expansion time, seems clearly wrong ???
3404 Validate_Remote_Access_To_Class_Wide_Type (N);
3406 -- Set the Storage Pool
3408 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3410 if Present (Storage_Pool (N)) then
3411 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3412 if VM_Target = No_VM then
3413 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3414 end if;
3416 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3417 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3419 else
3420 Set_Procedure_To_Call (N,
3421 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3422 end if;
3423 end if;
3425 -- Under certain circumstances we can replace an allocator by an access
3426 -- to statically allocated storage. The conditions, as noted in AARM
3427 -- 3.10 (10c) are as follows:
3429 -- Size and initial value is known at compile time
3430 -- Access type is access-to-constant
3432 -- The allocator is not part of a constraint on a record component,
3433 -- because in that case the inserted actions are delayed until the
3434 -- record declaration is fully analyzed, which is too late for the
3435 -- analysis of the rewritten allocator.
3437 if Is_Access_Constant (PtrT)
3438 and then Nkind (Expression (N)) = N_Qualified_Expression
3439 and then Compile_Time_Known_Value (Expression (Expression (N)))
3440 and then Size_Known_At_Compile_Time (Etype (Expression
3441 (Expression (N))))
3442 and then not Is_Record_Type (Current_Scope)
3443 then
3444 -- Here we can do the optimization. For the allocator
3446 -- new x'(y)
3448 -- We insert an object declaration
3450 -- Tnn : aliased x := y;
3452 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3453 -- marked as requiring static allocation.
3455 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
3456 Desig := Subtype_Mark (Expression (N));
3458 -- If context is constrained, use constrained subtype directly,
3459 -- so that the constant is not labelled as having a nominally
3460 -- unconstrained subtype.
3462 if Entity (Desig) = Base_Type (Dtyp) then
3463 Desig := New_Occurrence_Of (Dtyp, Loc);
3464 end if;
3466 Insert_Action (N,
3467 Make_Object_Declaration (Loc,
3468 Defining_Identifier => Temp,
3469 Aliased_Present => True,
3470 Constant_Present => Is_Access_Constant (PtrT),
3471 Object_Definition => Desig,
3472 Expression => Expression (Expression (N))));
3474 Rewrite (N,
3475 Make_Attribute_Reference (Loc,
3476 Prefix => New_Occurrence_Of (Temp, Loc),
3477 Attribute_Name => Name_Unrestricted_Access));
3479 Analyze_And_Resolve (N, PtrT);
3481 -- We set the variable as statically allocated, since we don't want
3482 -- it going on the stack of the current procedure!
3484 Set_Is_Statically_Allocated (Temp);
3485 return;
3486 end if;
3488 -- Same if the allocator is an access discriminant for a local object:
3489 -- instead of an allocator we create a local value and constrain the
3490 -- enclosing object with the corresponding access attribute.
3492 if Is_Static_Coextension (N) then
3493 Rewrite_Coextension (N);
3494 return;
3495 end if;
3497 -- The current allocator creates an object which may contain nested
3498 -- coextensions. Use the current allocator's finalization list to
3499 -- generate finalization call for all nested coextensions.
3501 if Is_Coextension_Root (N) then
3502 Complete_Coextension_Finalization;
3503 end if;
3505 -- Check for size too large, we do this because the back end misses
3506 -- proper checks here and can generate rubbish allocation calls when
3507 -- we are near the limit. We only do this for the 32-bit address case
3508 -- since that is from a practical point of view where we see a problem.
3510 if System_Address_Size = 32
3511 and then not Storage_Checks_Suppressed (PtrT)
3512 and then not Storage_Checks_Suppressed (Dtyp)
3513 and then not Storage_Checks_Suppressed (Etyp)
3514 then
3515 -- The check we want to generate should look like
3517 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3518 -- raise Storage_Error;
3519 -- end if;
3521 -- where 3.5 gigabytes is a constant large enough to accommodate any
3522 -- reasonable request for. But we can't do it this way because at
3523 -- least at the moment we don't compute this attribute right, and
3524 -- can silently give wrong results when the result gets large. Since
3525 -- this is all about large results, that's bad, so instead we only
3526 -- apply the check for constrained arrays, and manually compute the
3527 -- value of the attribute ???
3529 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3530 Insert_Action (N,
3531 Make_Raise_Storage_Error (Loc,
3532 Condition =>
3533 Make_Op_Gt (Loc,
3534 Left_Opnd => Size_In_Storage_Elements (Etyp),
3535 Right_Opnd =>
3536 Make_Integer_Literal (Loc,
3537 Intval => Uint_7 * (Uint_2 ** 29))),
3538 Reason => SE_Object_Too_Large));
3539 end if;
3540 end if;
3542 -- Handle case of qualified expression (other than optimization above)
3543 -- First apply constraint checks, because the bounds or discriminants
3544 -- in the aggregate might not match the subtype mark in the allocator.
3546 if Nkind (Expression (N)) = N_Qualified_Expression then
3547 Apply_Constraint_Check
3548 (Expression (Expression (N)), Etype (Expression (N)));
3550 Expand_Allocator_Expression (N);
3551 return;
3552 end if;
3554 -- If the allocator is for a type which requires initialization, and
3555 -- there is no initial value (i.e. operand is a subtype indication
3556 -- rather than a qualified expression), then we must generate a call to
3557 -- the initialization routine using an expressions action node:
3559 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3561 -- Here ptr_T is the pointer type for the allocator, and T is the
3562 -- subtype of the allocator. A special case arises if the designated
3563 -- type of the access type is a task or contains tasks. In this case
3564 -- the call to Init (Temp.all ...) is replaced by code that ensures
3565 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3566 -- for details). In addition, if the type T is a task T, then the
3567 -- first argument to Init must be converted to the task record type.
3569 declare
3570 T : constant Entity_Id := Entity (Expression (N));
3571 Init : Entity_Id;
3572 Arg1 : Node_Id;
3573 Args : List_Id;
3574 Decls : List_Id;
3575 Decl : Node_Id;
3576 Discr : Elmt_Id;
3577 Flist : Node_Id;
3578 Temp_Decl : Node_Id;
3579 Temp_Type : Entity_Id;
3580 Attach_Level : Uint;
3582 begin
3583 if No_Initialization (N) then
3584 null;
3586 -- Case of no initialization procedure present
3588 elsif not Has_Non_Null_Base_Init_Proc (T) then
3590 -- Case of simple initialization required
3592 if Needs_Simple_Initialization (T) then
3593 Check_Restriction (No_Default_Initialization, N);
3594 Rewrite (Expression (N),
3595 Make_Qualified_Expression (Loc,
3596 Subtype_Mark => New_Occurrence_Of (T, Loc),
3597 Expression => Get_Simple_Init_Val (T, N)));
3599 Analyze_And_Resolve (Expression (Expression (N)), T);
3600 Analyze_And_Resolve (Expression (N), T);
3601 Set_Paren_Count (Expression (Expression (N)), 1);
3602 Expand_N_Allocator (N);
3604 -- No initialization required
3606 else
3607 null;
3608 end if;
3610 -- Case of initialization procedure present, must be called
3612 else
3613 Check_Restriction (No_Default_Initialization, N);
3615 if not Restriction_Active (No_Default_Initialization) then
3616 Init := Base_Init_Proc (T);
3617 Nod := N;
3618 Temp := Make_Temporary (Loc, 'P');
3620 -- Construct argument list for the initialization routine call
3622 Arg1 :=
3623 Make_Explicit_Dereference (Loc,
3624 Prefix => New_Reference_To (Temp, Loc));
3625 Set_Assignment_OK (Arg1);
3626 Temp_Type := PtrT;
3628 -- The initialization procedure expects a specific type. if the
3629 -- context is access to class wide, indicate that the object
3630 -- being allocated has the right specific type.
3632 if Is_Class_Wide_Type (Dtyp) then
3633 Arg1 := Unchecked_Convert_To (T, Arg1);
3634 end if;
3636 -- If designated type is a concurrent type or if it is private
3637 -- type whose definition is a concurrent type, the first
3638 -- argument in the Init routine has to be unchecked conversion
3639 -- to the corresponding record type. If the designated type is
3640 -- a derived type, we also convert the argument to its root
3641 -- type.
3643 if Is_Concurrent_Type (T) then
3644 Arg1 :=
3645 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3647 elsif Is_Private_Type (T)
3648 and then Present (Full_View (T))
3649 and then Is_Concurrent_Type (Full_View (T))
3650 then
3651 Arg1 :=
3652 Unchecked_Convert_To
3653 (Corresponding_Record_Type (Full_View (T)), Arg1);
3655 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3656 declare
3657 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3658 begin
3659 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3660 Set_Etype (Arg1, Ftyp);
3661 end;
3662 end if;
3664 Args := New_List (Arg1);
3666 -- For the task case, pass the Master_Id of the access type as
3667 -- the value of the _Master parameter, and _Chain as the value
3668 -- of the _Chain parameter (_Chain will be defined as part of
3669 -- the generated code for the allocator).
3671 -- In Ada 2005, the context may be a function that returns an
3672 -- anonymous access type. In that case the Master_Id has been
3673 -- created when expanding the function declaration.
3675 if Has_Task (T) then
3676 if No (Master_Id (Base_Type (PtrT))) then
3678 -- The designated type was an incomplete type, and the
3679 -- access type did not get expanded. Salvage it now.
3681 if not Restriction_Active (No_Task_Hierarchy) then
3682 pragma Assert (Present (Parent (Base_Type (PtrT))));
3683 Expand_N_Full_Type_Declaration
3684 (Parent (Base_Type (PtrT)));
3685 end if;
3686 end if;
3688 -- If the context of the allocator is a declaration or an
3689 -- assignment, we can generate a meaningful image for it,
3690 -- even though subsequent assignments might remove the
3691 -- connection between task and entity. We build this image
3692 -- when the left-hand side is a simple variable, a simple
3693 -- indexed assignment or a simple selected component.
3695 if Nkind (Parent (N)) = N_Assignment_Statement then
3696 declare
3697 Nam : constant Node_Id := Name (Parent (N));
3699 begin
3700 if Is_Entity_Name (Nam) then
3701 Decls :=
3702 Build_Task_Image_Decls
3703 (Loc,
3704 New_Occurrence_Of
3705 (Entity (Nam), Sloc (Nam)), T);
3707 elsif Nkind_In
3708 (Nam, N_Indexed_Component, N_Selected_Component)
3709 and then Is_Entity_Name (Prefix (Nam))
3710 then
3711 Decls :=
3712 Build_Task_Image_Decls
3713 (Loc, Nam, Etype (Prefix (Nam)));
3714 else
3715 Decls := Build_Task_Image_Decls (Loc, T, T);
3716 end if;
3717 end;
3719 elsif Nkind (Parent (N)) = N_Object_Declaration then
3720 Decls :=
3721 Build_Task_Image_Decls
3722 (Loc, Defining_Identifier (Parent (N)), T);
3724 else
3725 Decls := Build_Task_Image_Decls (Loc, T, T);
3726 end if;
3728 if Restriction_Active (No_Task_Hierarchy) then
3729 Append_To (Args,
3730 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
3731 else
3732 Append_To (Args,
3733 New_Reference_To
3734 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3735 end if;
3737 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3739 Decl := Last (Decls);
3740 Append_To (Args,
3741 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3743 -- Has_Task is false, Decls not used
3745 else
3746 Decls := No_List;
3747 end if;
3749 -- Add discriminants if discriminated type
3751 declare
3752 Dis : Boolean := False;
3753 Typ : Entity_Id;
3755 begin
3756 if Has_Discriminants (T) then
3757 Dis := True;
3758 Typ := T;
3760 elsif Is_Private_Type (T)
3761 and then Present (Full_View (T))
3762 and then Has_Discriminants (Full_View (T))
3763 then
3764 Dis := True;
3765 Typ := Full_View (T);
3766 end if;
3768 if Dis then
3770 -- If the allocated object will be constrained by the
3771 -- default values for discriminants, then build a subtype
3772 -- with those defaults, and change the allocated subtype
3773 -- to that. Note that this happens in fewer cases in Ada
3774 -- 2005 (AI-363).
3776 if not Is_Constrained (Typ)
3777 and then Present (Discriminant_Default_Value
3778 (First_Discriminant (Typ)))
3779 and then (Ada_Version < Ada_2005
3780 or else
3781 not Has_Constrained_Partial_View (Typ))
3782 then
3783 Typ := Build_Default_Subtype (Typ, N);
3784 Set_Expression (N, New_Reference_To (Typ, Loc));
3785 end if;
3787 Discr := First_Elmt (Discriminant_Constraint (Typ));
3788 while Present (Discr) loop
3789 Nod := Node (Discr);
3790 Append (New_Copy_Tree (Node (Discr)), Args);
3792 -- AI-416: when the discriminant constraint is an
3793 -- anonymous access type make sure an accessibility
3794 -- check is inserted if necessary (3.10.2(22.q/2))
3796 if Ada_Version >= Ada_2005
3797 and then
3798 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3799 then
3800 Apply_Accessibility_Check
3801 (Nod, Typ, Insert_Node => Nod);
3802 end if;
3804 Next_Elmt (Discr);
3805 end loop;
3806 end if;
3807 end;
3809 -- We set the allocator as analyzed so that when we analyze the
3810 -- expression actions node, we do not get an unwanted recursive
3811 -- expansion of the allocator expression.
3813 Set_Analyzed (N, True);
3814 Nod := Relocate_Node (N);
3816 -- Here is the transformation:
3817 -- input: new T
3818 -- output: Temp : constant ptr_T := new T;
3819 -- Init (Temp.all, ...);
3820 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3821 -- <CTRL> Initialize (Finalizable (Temp.all));
3823 -- Here ptr_T is the pointer type for the allocator, and is the
3824 -- subtype of the allocator.
3826 Temp_Decl :=
3827 Make_Object_Declaration (Loc,
3828 Defining_Identifier => Temp,
3829 Constant_Present => True,
3830 Object_Definition => New_Reference_To (Temp_Type, Loc),
3831 Expression => Nod);
3833 Set_Assignment_OK (Temp_Decl);
3834 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3836 -- If the designated type is a task type or contains tasks,
3837 -- create block to activate created tasks, and insert
3838 -- declaration for Task_Image variable ahead of call.
3840 if Has_Task (T) then
3841 declare
3842 L : constant List_Id := New_List;
3843 Blk : Node_Id;
3844 begin
3845 Build_Task_Allocate_Block (L, Nod, Args);
3846 Blk := Last (L);
3847 Insert_List_Before (First (Declarations (Blk)), Decls);
3848 Insert_Actions (N, L);
3849 end;
3851 else
3852 Insert_Action (N,
3853 Make_Procedure_Call_Statement (Loc,
3854 Name => New_Reference_To (Init, Loc),
3855 Parameter_Associations => Args));
3856 end if;
3858 if Needs_Finalization (T) then
3860 -- Postpone the generation of a finalization call for the
3861 -- current allocator if it acts as a coextension.
3863 if Is_Dynamic_Coextension (N) then
3864 if No (Coextensions (N)) then
3865 Set_Coextensions (N, New_Elmt_List);
3866 end if;
3868 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3870 else
3871 Flist :=
3872 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3874 -- Anonymous access types created for access parameters
3875 -- are attached to an explicitly constructed controller,
3876 -- which ensures that they can be finalized properly,
3877 -- even if their deallocation might not happen. The list
3878 -- associated with the controller is doubly-linked. For
3879 -- other anonymous access types, the object may end up
3880 -- on the global final list which is singly-linked.
3881 -- Work needed for access discriminants in Ada 2005 ???
3883 if Ekind (PtrT) = E_Anonymous_Access_Type then
3884 Attach_Level := Uint_1;
3885 else
3886 Attach_Level := Uint_2;
3887 end if;
3889 Insert_Actions (N,
3890 Make_Init_Call (
3891 Ref => New_Copy_Tree (Arg1),
3892 Typ => T,
3893 Flist_Ref => Flist,
3894 With_Attach => Make_Integer_Literal (Loc,
3895 Intval => Attach_Level)));
3896 end if;
3897 end if;
3899 Rewrite (N, New_Reference_To (Temp, Loc));
3900 Analyze_And_Resolve (N, PtrT);
3901 end if;
3902 end if;
3903 end;
3905 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3906 -- object that has been rewritten as a reference, we displace "this"
3907 -- to reference properly its secondary dispatch table.
3909 if Nkind (N) = N_Identifier
3910 and then Is_Interface (Dtyp)
3911 then
3912 Displace_Allocator_Pointer (N);
3913 end if;
3915 exception
3916 when RE_Not_Available =>
3917 return;
3918 end Expand_N_Allocator;
3920 -----------------------
3921 -- Expand_N_And_Then --
3922 -----------------------
3924 procedure Expand_N_And_Then (N : Node_Id)
3925 renames Expand_Short_Circuit_Operator;
3927 ------------------------------
3928 -- Expand_N_Case_Expression --
3929 ------------------------------
3931 procedure Expand_N_Case_Expression (N : Node_Id) is
3932 Loc : constant Source_Ptr := Sloc (N);
3933 Typ : constant Entity_Id := Etype (N);
3934 Cstmt : Node_Id;
3935 Tnn : Entity_Id;
3936 Pnn : Entity_Id;
3937 Actions : List_Id;
3938 Ttyp : Entity_Id;
3939 Alt : Node_Id;
3940 Fexp : Node_Id;
3942 begin
3943 -- We expand
3945 -- case X is when A => AX, when B => BX ...
3947 -- to
3949 -- do
3950 -- Tnn : typ;
3951 -- case X is
3952 -- when A =>
3953 -- Tnn := AX;
3954 -- when B =>
3955 -- Tnn := BX;
3956 -- ...
3957 -- end case;
3958 -- in Tnn end;
3960 -- However, this expansion is wrong for limited types, and also
3961 -- wrong for unconstrained types (since the bounds may not be the
3962 -- same in all branches). Furthermore it involves an extra copy
3963 -- for large objects. So we take care of this by using the following
3964 -- modified expansion for non-scalar types:
3966 -- do
3967 -- type Pnn is access all typ;
3968 -- Tnn : Pnn;
3969 -- case X is
3970 -- when A =>
3971 -- T := AX'Unrestricted_Access;
3972 -- when B =>
3973 -- T := BX'Unrestricted_Access;
3974 -- ...
3975 -- end case;
3976 -- in Tnn.all end;
3978 Cstmt :=
3979 Make_Case_Statement (Loc,
3980 Expression => Expression (N),
3981 Alternatives => New_List);
3983 Actions := New_List;
3985 -- Scalar case
3987 if Is_Scalar_Type (Typ) then
3988 Ttyp := Typ;
3990 else
3991 Pnn := Make_Temporary (Loc, 'P');
3992 Append_To (Actions,
3993 Make_Full_Type_Declaration (Loc,
3994 Defining_Identifier => Pnn,
3995 Type_Definition =>
3996 Make_Access_To_Object_Definition (Loc,
3997 All_Present => True,
3998 Subtype_Indication =>
3999 New_Reference_To (Typ, Loc))));
4000 Ttyp := Pnn;
4001 end if;
4003 Tnn := Make_Temporary (Loc, 'T');
4004 Append_To (Actions,
4005 Make_Object_Declaration (Loc,
4006 Defining_Identifier => Tnn,
4007 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
4009 -- Now process the alternatives
4011 Alt := First (Alternatives (N));
4012 while Present (Alt) loop
4013 declare
4014 Aexp : Node_Id := Expression (Alt);
4015 Aloc : constant Source_Ptr := Sloc (Aexp);
4017 begin
4018 if not Is_Scalar_Type (Typ) then
4019 Aexp :=
4020 Make_Attribute_Reference (Aloc,
4021 Prefix => Relocate_Node (Aexp),
4022 Attribute_Name => Name_Unrestricted_Access);
4023 end if;
4025 Append_To
4026 (Alternatives (Cstmt),
4027 Make_Case_Statement_Alternative (Sloc (Alt),
4028 Discrete_Choices => Discrete_Choices (Alt),
4029 Statements => New_List (
4030 Make_Assignment_Statement (Aloc,
4031 Name => New_Occurrence_Of (Tnn, Loc),
4032 Expression => Aexp))));
4033 end;
4035 Next (Alt);
4036 end loop;
4038 Append_To (Actions, Cstmt);
4040 -- Construct and return final expression with actions
4042 if Is_Scalar_Type (Typ) then
4043 Fexp := New_Occurrence_Of (Tnn, Loc);
4044 else
4045 Fexp :=
4046 Make_Explicit_Dereference (Loc,
4047 Prefix => New_Occurrence_Of (Tnn, Loc));
4048 end if;
4050 Rewrite (N,
4051 Make_Expression_With_Actions (Loc,
4052 Expression => Fexp,
4053 Actions => Actions));
4055 Analyze_And_Resolve (N, Typ);
4056 end Expand_N_Case_Expression;
4058 -------------------------------------
4059 -- Expand_N_Conditional_Expression --
4060 -------------------------------------
4062 -- Deal with limited types and expression actions
4064 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4065 Loc : constant Source_Ptr := Sloc (N);
4066 Cond : constant Node_Id := First (Expressions (N));
4067 Thenx : constant Node_Id := Next (Cond);
4068 Elsex : constant Node_Id := Next (Thenx);
4069 Typ : constant Entity_Id := Etype (N);
4071 Cnn : Entity_Id;
4072 Decl : Node_Id;
4073 New_If : Node_Id;
4074 New_N : Node_Id;
4075 P_Decl : Node_Id;
4076 Expr : Node_Id;
4077 Actions : List_Id;
4079 begin
4080 -- Fold at compile time if condition known. We have already folded
4081 -- static conditional expressions, but it is possible to fold any
4082 -- case in which the condition is known at compile time, even though
4083 -- the result is non-static.
4085 -- Note that we don't do the fold of such cases in Sem_Elab because
4086 -- it can cause infinite loops with the expander adding a conditional
4087 -- expression, and Sem_Elab circuitry removing it repeatedly.
4089 if Compile_Time_Known_Value (Cond) then
4090 if Is_True (Expr_Value (Cond)) then
4091 Expr := Thenx;
4092 Actions := Then_Actions (N);
4093 else
4094 Expr := Elsex;
4095 Actions := Else_Actions (N);
4096 end if;
4098 Remove (Expr);
4100 if Present (Actions) then
4102 -- If we are not allowed to use Expression_With_Actions, just
4103 -- skip the optimization, it is not critical for correctness.
4105 if not Use_Expression_With_Actions then
4106 goto Skip_Optimization;
4107 end if;
4109 Rewrite (N,
4110 Make_Expression_With_Actions (Loc,
4111 Expression => Relocate_Node (Expr),
4112 Actions => Actions));
4113 Analyze_And_Resolve (N, Typ);
4115 else
4116 Rewrite (N, Relocate_Node (Expr));
4117 end if;
4119 -- Note that the result is never static (legitimate cases of static
4120 -- conditional expressions were folded in Sem_Eval).
4122 Set_Is_Static_Expression (N, False);
4123 return;
4124 end if;
4126 <<Skip_Optimization>>
4128 -- If the type is limited or unconstrained, we expand as follows to
4129 -- avoid any possibility of improper copies.
4131 -- Note: it may be possible to avoid this special processing if the
4132 -- back end uses its own mechanisms for handling by-reference types ???
4134 -- type Ptr is access all Typ;
4135 -- Cnn : Ptr;
4136 -- if cond then
4137 -- <<then actions>>
4138 -- Cnn := then-expr'Unrestricted_Access;
4139 -- else
4140 -- <<else actions>>
4141 -- Cnn := else-expr'Unrestricted_Access;
4142 -- end if;
4144 -- and replace the conditional expression by a reference to Cnn.all.
4146 -- This special case can be skipped if the back end handles limited
4147 -- types properly and ensures that no incorrect copies are made.
4149 if Is_By_Reference_Type (Typ)
4150 and then not Back_End_Handles_Limited_Types
4151 then
4152 Cnn := Make_Temporary (Loc, 'C', N);
4154 P_Decl :=
4155 Make_Full_Type_Declaration (Loc,
4156 Defining_Identifier => Make_Temporary (Loc, 'A'),
4157 Type_Definition =>
4158 Make_Access_To_Object_Definition (Loc,
4159 All_Present => True,
4160 Subtype_Indication =>
4161 New_Reference_To (Typ, Loc)));
4163 Insert_Action (N, P_Decl);
4165 Decl :=
4166 Make_Object_Declaration (Loc,
4167 Defining_Identifier => Cnn,
4168 Object_Definition =>
4169 New_Occurrence_Of (Defining_Identifier (P_Decl), Loc));
4171 New_If :=
4172 Make_Implicit_If_Statement (N,
4173 Condition => Relocate_Node (Cond),
4175 Then_Statements => New_List (
4176 Make_Assignment_Statement (Sloc (Thenx),
4177 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4178 Expression =>
4179 Make_Attribute_Reference (Loc,
4180 Attribute_Name => Name_Unrestricted_Access,
4181 Prefix => Relocate_Node (Thenx)))),
4183 Else_Statements => New_List (
4184 Make_Assignment_Statement (Sloc (Elsex),
4185 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4186 Expression =>
4187 Make_Attribute_Reference (Loc,
4188 Attribute_Name => Name_Unrestricted_Access,
4189 Prefix => Relocate_Node (Elsex)))));
4191 New_N :=
4192 Make_Explicit_Dereference (Loc,
4193 Prefix => New_Occurrence_Of (Cnn, Loc));
4195 -- For other types, we only need to expand if there are other actions
4196 -- associated with either branch.
4198 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4200 -- We have two approaches to handling this. If we are allowed to use
4201 -- N_Expression_With_Actions, then we can just wrap the actions into
4202 -- the appropriate expression.
4204 if Use_Expression_With_Actions then
4205 if Present (Then_Actions (N)) then
4206 Rewrite (Thenx,
4207 Make_Expression_With_Actions (Sloc (Thenx),
4208 Actions => Then_Actions (N),
4209 Expression => Relocate_Node (Thenx)));
4210 Set_Then_Actions (N, No_List);
4211 Analyze_And_Resolve (Thenx, Typ);
4212 end if;
4214 if Present (Else_Actions (N)) then
4215 Rewrite (Elsex,
4216 Make_Expression_With_Actions (Sloc (Elsex),
4217 Actions => Else_Actions (N),
4218 Expression => Relocate_Node (Elsex)));
4219 Set_Else_Actions (N, No_List);
4220 Analyze_And_Resolve (Elsex, Typ);
4221 end if;
4223 return;
4225 -- if we can't use N_Expression_With_Actions nodes, then we insert
4226 -- the following sequence of actions (using Insert_Actions):
4228 -- Cnn : typ;
4229 -- if cond then
4230 -- <<then actions>>
4231 -- Cnn := then-expr;
4232 -- else
4233 -- <<else actions>>
4234 -- Cnn := else-expr
4235 -- end if;
4237 -- and replace the conditional expression by a reference to Cnn
4239 else
4240 Cnn := Make_Temporary (Loc, 'C', N);
4242 Decl :=
4243 Make_Object_Declaration (Loc,
4244 Defining_Identifier => Cnn,
4245 Object_Definition => New_Occurrence_Of (Typ, Loc));
4247 New_If :=
4248 Make_Implicit_If_Statement (N,
4249 Condition => Relocate_Node (Cond),
4251 Then_Statements => New_List (
4252 Make_Assignment_Statement (Sloc (Thenx),
4253 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4254 Expression => Relocate_Node (Thenx))),
4256 Else_Statements => New_List (
4257 Make_Assignment_Statement (Sloc (Elsex),
4258 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4259 Expression => Relocate_Node (Elsex))));
4261 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4262 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4264 New_N := New_Occurrence_Of (Cnn, Loc);
4265 end if;
4267 -- If no actions then no expansion needed, gigi will handle it using
4268 -- the same approach as a C conditional expression.
4270 else
4271 return;
4272 end if;
4274 -- Fall through here for either the limited expansion, or the case of
4275 -- inserting actions for non-limited types. In both these cases, we must
4276 -- move the SLOC of the parent If statement to the newly created one and
4277 -- change it to the SLOC of the expression which, after expansion, will
4278 -- correspond to what is being evaluated.
4280 if Present (Parent (N))
4281 and then Nkind (Parent (N)) = N_If_Statement
4282 then
4283 Set_Sloc (New_If, Sloc (Parent (N)));
4284 Set_Sloc (Parent (N), Loc);
4285 end if;
4287 -- Make sure Then_Actions and Else_Actions are appropriately moved
4288 -- to the new if statement.
4290 if Present (Then_Actions (N)) then
4291 Insert_List_Before
4292 (First (Then_Statements (New_If)), Then_Actions (N));
4293 end if;
4295 if Present (Else_Actions (N)) then
4296 Insert_List_Before
4297 (First (Else_Statements (New_If)), Else_Actions (N));
4298 end if;
4300 Insert_Action (N, Decl);
4301 Insert_Action (N, New_If);
4302 Rewrite (N, New_N);
4303 Analyze_And_Resolve (N, Typ);
4304 end Expand_N_Conditional_Expression;
4306 -----------------------------------
4307 -- Expand_N_Explicit_Dereference --
4308 -----------------------------------
4310 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4311 begin
4312 -- Insert explicit dereference call for the checked storage pool case
4314 Insert_Dereference_Action (Prefix (N));
4315 end Expand_N_Explicit_Dereference;
4317 -----------------
4318 -- Expand_N_In --
4319 -----------------
4321 procedure Expand_N_In (N : Node_Id) is
4322 Loc : constant Source_Ptr := Sloc (N);
4323 Restyp : constant Entity_Id := Etype (N);
4324 Lop : constant Node_Id := Left_Opnd (N);
4325 Rop : constant Node_Id := Right_Opnd (N);
4326 Static : constant Boolean := Is_OK_Static_Expression (N);
4328 Ltyp : Entity_Id;
4329 Rtyp : Entity_Id;
4331 procedure Expand_Set_Membership;
4332 -- For each choice we create a simple equality or membership test.
4333 -- The whole membership is rewritten connecting these with OR ELSE.
4335 ---------------------------
4336 -- Expand_Set_Membership --
4337 ---------------------------
4339 procedure Expand_Set_Membership is
4340 Alt : Node_Id;
4341 Res : Node_Id;
4343 function Make_Cond (Alt : Node_Id) return Node_Id;
4344 -- If the alternative is a subtype mark, create a simple membership
4345 -- test. Otherwise create an equality test for it.
4347 ---------------
4348 -- Make_Cond --
4349 ---------------
4351 function Make_Cond (Alt : Node_Id) return Node_Id is
4352 Cond : Node_Id;
4353 L : constant Node_Id := New_Copy (Lop);
4354 R : constant Node_Id := Relocate_Node (Alt);
4356 begin
4357 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
4358 or else Nkind (Alt) = N_Range
4359 then
4360 Cond :=
4361 Make_In (Sloc (Alt),
4362 Left_Opnd => L,
4363 Right_Opnd => R);
4364 else
4365 Cond :=
4366 Make_Op_Eq (Sloc (Alt),
4367 Left_Opnd => L,
4368 Right_Opnd => R);
4369 end if;
4371 return Cond;
4372 end Make_Cond;
4374 -- Start of processing for Expand_Set_Membership
4376 begin
4377 Alt := Last (Alternatives (N));
4378 Res := Make_Cond (Alt);
4380 Prev (Alt);
4381 while Present (Alt) loop
4382 Res :=
4383 Make_Or_Else (Sloc (Alt),
4384 Left_Opnd => Make_Cond (Alt),
4385 Right_Opnd => Res);
4386 Prev (Alt);
4387 end loop;
4389 Rewrite (N, Res);
4390 Analyze_And_Resolve (N, Standard_Boolean);
4391 end Expand_Set_Membership;
4393 procedure Substitute_Valid_Check;
4394 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4395 -- test for the left operand being in range of its subtype.
4397 ----------------------------
4398 -- Substitute_Valid_Check --
4399 ----------------------------
4401 procedure Substitute_Valid_Check is
4402 begin
4403 Rewrite (N,
4404 Make_Attribute_Reference (Loc,
4405 Prefix => Relocate_Node (Lop),
4406 Attribute_Name => Name_Valid));
4408 Analyze_And_Resolve (N, Restyp);
4410 Error_Msg_N ("?explicit membership test may be optimized away", N);
4411 Error_Msg_N -- CODEFIX
4412 ("\?use ''Valid attribute instead", N);
4413 return;
4414 end Substitute_Valid_Check;
4416 -- Start of processing for Expand_N_In
4418 begin
4419 -- If set membership case, expand with separate procedure
4421 if Present (Alternatives (N)) then
4422 Remove_Side_Effects (Lop);
4423 Expand_Set_Membership;
4424 return;
4425 end if;
4427 -- Not set membership, proceed with expansion
4429 Ltyp := Etype (Left_Opnd (N));
4430 Rtyp := Etype (Right_Opnd (N));
4432 -- Check case of explicit test for an expression in range of its
4433 -- subtype. This is suspicious usage and we replace it with a 'Valid
4434 -- test and give a warning. For floating point types however, this is a
4435 -- standard way to check for finite numbers, and using 'Valid would
4436 -- typically be a pessimization. Also skip this test for predicated
4437 -- types, since it is perfectly reasonable to check if a value meets
4438 -- its predicate.
4440 if Is_Scalar_Type (Ltyp)
4441 and then not Is_Floating_Point_Type (Ltyp)
4442 and then Nkind (Rop) in N_Has_Entity
4443 and then Ltyp = Entity (Rop)
4444 and then Comes_From_Source (N)
4445 and then VM_Target = No_VM
4446 and then not (Is_Discrete_Type (Ltyp)
4447 and then Present (Predicate_Function (Ltyp)))
4448 then
4449 Substitute_Valid_Check;
4450 return;
4451 end if;
4453 -- Do validity check on operands
4455 if Validity_Checks_On and Validity_Check_Operands then
4456 Ensure_Valid (Left_Opnd (N));
4457 Validity_Check_Range (Right_Opnd (N));
4458 end if;
4460 -- Case of explicit range
4462 if Nkind (Rop) = N_Range then
4463 declare
4464 Lo : constant Node_Id := Low_Bound (Rop);
4465 Hi : constant Node_Id := High_Bound (Rop);
4467 Lo_Orig : constant Node_Id := Original_Node (Lo);
4468 Hi_Orig : constant Node_Id := Original_Node (Hi);
4470 Lcheck : Compare_Result;
4471 Ucheck : Compare_Result;
4473 Warn1 : constant Boolean :=
4474 Constant_Condition_Warnings
4475 and then Comes_From_Source (N)
4476 and then not In_Instance;
4477 -- This must be true for any of the optimization warnings, we
4478 -- clearly want to give them only for source with the flag on. We
4479 -- also skip these warnings in an instance since it may be the
4480 -- case that different instantiations have different ranges.
4482 Warn2 : constant Boolean :=
4483 Warn1
4484 and then Nkind (Original_Node (Rop)) = N_Range
4485 and then Is_Integer_Type (Etype (Lo));
4486 -- For the case where only one bound warning is elided, we also
4487 -- insist on an explicit range and an integer type. The reason is
4488 -- that the use of enumeration ranges including an end point is
4489 -- common, as is the use of a subtype name, one of whose bounds is
4490 -- the same as the type of the expression.
4492 begin
4493 -- If test is explicit x'First .. x'Last, replace by valid check
4495 -- Could use some individual comments for this complex test ???
4497 if Is_Scalar_Type (Ltyp)
4498 and then Nkind (Lo_Orig) = N_Attribute_Reference
4499 and then Attribute_Name (Lo_Orig) = Name_First
4500 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4501 and then Entity (Prefix (Lo_Orig)) = Ltyp
4502 and then Nkind (Hi_Orig) = N_Attribute_Reference
4503 and then Attribute_Name (Hi_Orig) = Name_Last
4504 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4505 and then Entity (Prefix (Hi_Orig)) = Ltyp
4506 and then Comes_From_Source (N)
4507 and then VM_Target = No_VM
4508 then
4509 Substitute_Valid_Check;
4510 goto Leave;
4511 end if;
4513 -- If bounds of type are known at compile time, and the end points
4514 -- are known at compile time and identical, this is another case
4515 -- for substituting a valid test. We only do this for discrete
4516 -- types, since it won't arise in practice for float types.
4518 if Comes_From_Source (N)
4519 and then Is_Discrete_Type (Ltyp)
4520 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4521 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4522 and then Compile_Time_Known_Value (Lo)
4523 and then Compile_Time_Known_Value (Hi)
4524 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4525 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4527 -- Kill warnings in instances, since they may be cases where we
4528 -- have a test in the generic that makes sense with some types
4529 -- and not with other types.
4531 and then not In_Instance
4532 then
4533 Substitute_Valid_Check;
4534 goto Leave;
4535 end if;
4537 -- If we have an explicit range, do a bit of optimization based on
4538 -- range analysis (we may be able to kill one or both checks).
4540 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4541 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4543 -- If either check is known to fail, replace result by False since
4544 -- the other check does not matter. Preserve the static flag for
4545 -- legality checks, because we are constant-folding beyond RM 4.9.
4547 if Lcheck = LT or else Ucheck = GT then
4548 if Warn1 then
4549 Error_Msg_N ("?range test optimized away", N);
4550 Error_Msg_N ("\?value is known to be out of range", N);
4551 end if;
4553 Rewrite (N, New_Reference_To (Standard_False, Loc));
4554 Analyze_And_Resolve (N, Restyp);
4555 Set_Is_Static_Expression (N, Static);
4556 goto Leave;
4558 -- If both checks are known to succeed, replace result by True,
4559 -- since we know we are in range.
4561 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4562 if Warn1 then
4563 Error_Msg_N ("?range test optimized away", N);
4564 Error_Msg_N ("\?value is known to be in range", N);
4565 end if;
4567 Rewrite (N, New_Reference_To (Standard_True, Loc));
4568 Analyze_And_Resolve (N, Restyp);
4569 Set_Is_Static_Expression (N, Static);
4570 goto Leave;
4572 -- If lower bound check succeeds and upper bound check is not
4573 -- known to succeed or fail, then replace the range check with
4574 -- a comparison against the upper bound.
4576 elsif Lcheck in Compare_GE then
4577 if Warn2 and then not In_Instance then
4578 Error_Msg_N ("?lower bound test optimized away", Lo);
4579 Error_Msg_N ("\?value is known to be in range", Lo);
4580 end if;
4582 Rewrite (N,
4583 Make_Op_Le (Loc,
4584 Left_Opnd => Lop,
4585 Right_Opnd => High_Bound (Rop)));
4586 Analyze_And_Resolve (N, Restyp);
4587 goto Leave;
4589 -- If upper bound check succeeds and lower bound check is not
4590 -- known to succeed or fail, then replace the range check with
4591 -- a comparison against the lower bound.
4593 elsif Ucheck in Compare_LE then
4594 if Warn2 and then not In_Instance then
4595 Error_Msg_N ("?upper bound test optimized away", Hi);
4596 Error_Msg_N ("\?value is known to be in range", Hi);
4597 end if;
4599 Rewrite (N,
4600 Make_Op_Ge (Loc,
4601 Left_Opnd => Lop,
4602 Right_Opnd => Low_Bound (Rop)));
4603 Analyze_And_Resolve (N, Restyp);
4604 goto Leave;
4605 end if;
4607 -- We couldn't optimize away the range check, but there is one
4608 -- more issue. If we are checking constant conditionals, then we
4609 -- see if we can determine the outcome assuming everything is
4610 -- valid, and if so give an appropriate warning.
4612 if Warn1 and then not Assume_No_Invalid_Values then
4613 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4614 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4616 -- Result is out of range for valid value
4618 if Lcheck = LT or else Ucheck = GT then
4619 Error_Msg_N
4620 ("?value can only be in range if it is invalid", N);
4622 -- Result is in range for valid value
4624 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4625 Error_Msg_N
4626 ("?value can only be out of range if it is invalid", N);
4628 -- Lower bound check succeeds if value is valid
4630 elsif Warn2 and then Lcheck in Compare_GE then
4631 Error_Msg_N
4632 ("?lower bound check only fails if it is invalid", Lo);
4634 -- Upper bound check succeeds if value is valid
4636 elsif Warn2 and then Ucheck in Compare_LE then
4637 Error_Msg_N
4638 ("?upper bound check only fails for invalid values", Hi);
4639 end if;
4640 end if;
4641 end;
4643 -- For all other cases of an explicit range, nothing to be done
4645 goto Leave;
4647 -- Here right operand is a subtype mark
4649 else
4650 declare
4651 Typ : Entity_Id := Etype (Rop);
4652 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4653 Cond : Node_Id := Empty;
4654 New_N : Node_Id;
4655 Obj : Node_Id := Lop;
4656 SCIL_Node : Node_Id;
4658 begin
4659 Remove_Side_Effects (Obj);
4661 -- For tagged type, do tagged membership operation
4663 if Is_Tagged_Type (Typ) then
4665 -- No expansion will be performed when VM_Target, as the VM
4666 -- back-ends will handle the membership tests directly (tags
4667 -- are not explicitly represented in Java objects, so the
4668 -- normal tagged membership expansion is not what we want).
4670 if Tagged_Type_Expansion then
4671 Tagged_Membership (N, SCIL_Node, New_N);
4672 Rewrite (N, New_N);
4673 Analyze_And_Resolve (N, Restyp);
4675 -- Update decoration of relocated node referenced by the
4676 -- SCIL node.
4678 if Generate_SCIL and then Present (SCIL_Node) then
4679 Set_SCIL_Node (N, SCIL_Node);
4680 end if;
4681 end if;
4683 goto Leave;
4685 -- If type is scalar type, rewrite as x in t'First .. t'Last.
4686 -- This reason we do this is that the bounds may have the wrong
4687 -- type if they come from the original type definition. Also this
4688 -- way we get all the processing above for an explicit range.
4690 -- Don't do this for predicated types, since in this case we
4691 -- want to check the predicate!
4693 elsif Is_Scalar_Type (Typ) then
4694 if No (Predicate_Function (Typ)) then
4695 Rewrite (Rop,
4696 Make_Range (Loc,
4697 Low_Bound =>
4698 Make_Attribute_Reference (Loc,
4699 Attribute_Name => Name_First,
4700 Prefix => New_Reference_To (Typ, Loc)),
4702 High_Bound =>
4703 Make_Attribute_Reference (Loc,
4704 Attribute_Name => Name_Last,
4705 Prefix => New_Reference_To (Typ, Loc))));
4706 Analyze_And_Resolve (N, Restyp);
4707 end if;
4709 goto Leave;
4711 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4712 -- a membership test if the subtype mark denotes a constrained
4713 -- Unchecked_Union subtype and the expression lacks inferable
4714 -- discriminants.
4716 elsif Is_Unchecked_Union (Base_Type (Typ))
4717 and then Is_Constrained (Typ)
4718 and then not Has_Inferable_Discriminants (Lop)
4719 then
4720 Insert_Action (N,
4721 Make_Raise_Program_Error (Loc,
4722 Reason => PE_Unchecked_Union_Restriction));
4724 -- Prevent Gigi from generating incorrect code by rewriting the
4725 -- test as False.
4727 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4728 goto Leave;
4729 end if;
4731 -- Here we have a non-scalar type
4733 if Is_Acc then
4734 Typ := Designated_Type (Typ);
4735 end if;
4737 if not Is_Constrained (Typ) then
4738 Rewrite (N, New_Reference_To (Standard_True, Loc));
4739 Analyze_And_Resolve (N, Restyp);
4741 -- For the constrained array case, we have to check the subscripts
4742 -- for an exact match if the lengths are non-zero (the lengths
4743 -- must match in any case).
4745 elsif Is_Array_Type (Typ) then
4746 Check_Subscripts : declare
4747 function Build_Attribute_Reference
4748 (E : Node_Id;
4749 Nam : Name_Id;
4750 Dim : Nat) return Node_Id;
4751 -- Build attribute reference E'Nam (Dim)
4753 -------------------------------
4754 -- Build_Attribute_Reference --
4755 -------------------------------
4757 function Build_Attribute_Reference
4758 (E : Node_Id;
4759 Nam : Name_Id;
4760 Dim : Nat) return Node_Id
4762 begin
4763 return
4764 Make_Attribute_Reference (Loc,
4765 Prefix => E,
4766 Attribute_Name => Nam,
4767 Expressions => New_List (
4768 Make_Integer_Literal (Loc, Dim)));
4769 end Build_Attribute_Reference;
4771 -- Start of processing for Check_Subscripts
4773 begin
4774 for J in 1 .. Number_Dimensions (Typ) loop
4775 Evolve_And_Then (Cond,
4776 Make_Op_Eq (Loc,
4777 Left_Opnd =>
4778 Build_Attribute_Reference
4779 (Duplicate_Subexpr_No_Checks (Obj),
4780 Name_First, J),
4781 Right_Opnd =>
4782 Build_Attribute_Reference
4783 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4785 Evolve_And_Then (Cond,
4786 Make_Op_Eq (Loc,
4787 Left_Opnd =>
4788 Build_Attribute_Reference
4789 (Duplicate_Subexpr_No_Checks (Obj),
4790 Name_Last, J),
4791 Right_Opnd =>
4792 Build_Attribute_Reference
4793 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4794 end loop;
4796 if Is_Acc then
4797 Cond :=
4798 Make_Or_Else (Loc,
4799 Left_Opnd =>
4800 Make_Op_Eq (Loc,
4801 Left_Opnd => Obj,
4802 Right_Opnd => Make_Null (Loc)),
4803 Right_Opnd => Cond);
4804 end if;
4806 Rewrite (N, Cond);
4807 Analyze_And_Resolve (N, Restyp);
4808 end Check_Subscripts;
4810 -- These are the cases where constraint checks may be required,
4811 -- e.g. records with possible discriminants
4813 else
4814 -- Expand the test into a series of discriminant comparisons.
4815 -- The expression that is built is the negation of the one that
4816 -- is used for checking discriminant constraints.
4818 Obj := Relocate_Node (Left_Opnd (N));
4820 if Has_Discriminants (Typ) then
4821 Cond := Make_Op_Not (Loc,
4822 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4824 if Is_Acc then
4825 Cond := Make_Or_Else (Loc,
4826 Left_Opnd =>
4827 Make_Op_Eq (Loc,
4828 Left_Opnd => Obj,
4829 Right_Opnd => Make_Null (Loc)),
4830 Right_Opnd => Cond);
4831 end if;
4833 else
4834 Cond := New_Occurrence_Of (Standard_True, Loc);
4835 end if;
4837 Rewrite (N, Cond);
4838 Analyze_And_Resolve (N, Restyp);
4839 end if;
4840 end;
4841 end if;
4843 -- At this point, we have done the processing required for the basic
4844 -- membership test, but not yet dealt with the predicate.
4846 <<Leave>>
4848 -- If a predicate is present, then we do the predicate test, but we
4849 -- most certainly want to omit this if we are within the predicate
4850 -- function itself, since otherwise we have an infinite recursion!
4852 declare
4853 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
4855 begin
4856 if Present (PFunc)
4857 and then Current_Scope /= PFunc
4858 then
4859 Rewrite (N,
4860 Make_And_Then (Loc,
4861 Left_Opnd => Relocate_Node (N),
4862 Right_Opnd => Make_Predicate_Call (Rtyp, Lop)));
4864 -- Analyze new expression, mark left operand as analyzed to
4865 -- avoid infinite recursion adding predicate calls.
4867 Set_Analyzed (Left_Opnd (N));
4868 Analyze_And_Resolve (N, Standard_Boolean);
4870 -- All done, skip attempt at compile time determination of result
4872 return;
4873 end if;
4874 end;
4875 end Expand_N_In;
4877 --------------------------------
4878 -- Expand_N_Indexed_Component --
4879 --------------------------------
4881 procedure Expand_N_Indexed_Component (N : Node_Id) is
4882 Loc : constant Source_Ptr := Sloc (N);
4883 Typ : constant Entity_Id := Etype (N);
4884 P : constant Node_Id := Prefix (N);
4885 T : constant Entity_Id := Etype (P);
4887 begin
4888 -- A special optimization, if we have an indexed component that is
4889 -- selecting from a slice, then we can eliminate the slice, since, for
4890 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4891 -- the range check required by the slice. The range check for the slice
4892 -- itself has already been generated. The range check for the
4893 -- subscripting operation is ensured by converting the subject to
4894 -- the subtype of the slice.
4896 -- This optimization not only generates better code, avoiding slice
4897 -- messing especially in the packed case, but more importantly bypasses
4898 -- some problems in handling this peculiar case, for example, the issue
4899 -- of dealing specially with object renamings.
4901 if Nkind (P) = N_Slice then
4902 Rewrite (N,
4903 Make_Indexed_Component (Loc,
4904 Prefix => Prefix (P),
4905 Expressions => New_List (
4906 Convert_To
4907 (Etype (First_Index (Etype (P))),
4908 First (Expressions (N))))));
4909 Analyze_And_Resolve (N, Typ);
4910 return;
4911 end if;
4913 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4914 -- function, then additional actuals must be passed.
4916 if Ada_Version >= Ada_2005
4917 and then Is_Build_In_Place_Function_Call (P)
4918 then
4919 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4920 end if;
4922 -- If the prefix is an access type, then we unconditionally rewrite if
4923 -- as an explicit dereference. This simplifies processing for several
4924 -- cases, including packed array cases and certain cases in which checks
4925 -- must be generated. We used to try to do this only when it was
4926 -- necessary, but it cleans up the code to do it all the time.
4928 if Is_Access_Type (T) then
4929 Insert_Explicit_Dereference (P);
4930 Analyze_And_Resolve (P, Designated_Type (T));
4931 end if;
4933 -- Generate index and validity checks
4935 Generate_Index_Checks (N);
4937 if Validity_Checks_On and then Validity_Check_Subscripts then
4938 Apply_Subscript_Validity_Checks (N);
4939 end if;
4941 -- All done for the non-packed case
4943 if not Is_Packed (Etype (Prefix (N))) then
4944 return;
4945 end if;
4947 -- For packed arrays that are not bit-packed (i.e. the case of an array
4948 -- with one or more index types with a non-contiguous enumeration type),
4949 -- we can always use the normal packed element get circuit.
4951 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4952 Expand_Packed_Element_Reference (N);
4953 return;
4954 end if;
4956 -- For a reference to a component of a bit packed array, we have to
4957 -- convert it to a reference to the corresponding Packed_Array_Type.
4958 -- We only want to do this for simple references, and not for:
4960 -- Left side of assignment, or prefix of left side of assignment, or
4961 -- prefix of the prefix, to handle packed arrays of packed arrays,
4962 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4964 -- Renaming objects in renaming associations
4965 -- This case is handled when a use of the renamed variable occurs
4967 -- Actual parameters for a procedure call
4968 -- This case is handled in Exp_Ch6.Expand_Actuals
4970 -- The second expression in a 'Read attribute reference
4972 -- The prefix of an address or bit or size attribute reference
4974 -- The following circuit detects these exceptions
4976 declare
4977 Child : Node_Id := N;
4978 Parnt : Node_Id := Parent (N);
4980 begin
4981 loop
4982 if Nkind (Parnt) = N_Unchecked_Expression then
4983 null;
4985 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4986 N_Procedure_Call_Statement)
4987 or else (Nkind (Parnt) = N_Parameter_Association
4988 and then
4989 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4990 then
4991 return;
4993 elsif Nkind (Parnt) = N_Attribute_Reference
4994 and then (Attribute_Name (Parnt) = Name_Address
4995 or else
4996 Attribute_Name (Parnt) = Name_Bit
4997 or else
4998 Attribute_Name (Parnt) = Name_Size)
4999 and then Prefix (Parnt) = Child
5000 then
5001 return;
5003 elsif Nkind (Parnt) = N_Assignment_Statement
5004 and then Name (Parnt) = Child
5005 then
5006 return;
5008 -- If the expression is an index of an indexed component, it must
5009 -- be expanded regardless of context.
5011 elsif Nkind (Parnt) = N_Indexed_Component
5012 and then Child /= Prefix (Parnt)
5013 then
5014 Expand_Packed_Element_Reference (N);
5015 return;
5017 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
5018 and then Name (Parent (Parnt)) = Parnt
5019 then
5020 return;
5022 elsif Nkind (Parnt) = N_Attribute_Reference
5023 and then Attribute_Name (Parnt) = Name_Read
5024 and then Next (First (Expressions (Parnt))) = Child
5025 then
5026 return;
5028 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
5029 and then Prefix (Parnt) = Child
5030 then
5031 null;
5033 else
5034 Expand_Packed_Element_Reference (N);
5035 return;
5036 end if;
5038 -- Keep looking up tree for unchecked expression, or if we are the
5039 -- prefix of a possible assignment left side.
5041 Child := Parnt;
5042 Parnt := Parent (Child);
5043 end loop;
5044 end;
5045 end Expand_N_Indexed_Component;
5047 ---------------------
5048 -- Expand_N_Not_In --
5049 ---------------------
5051 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
5052 -- can be done. This avoids needing to duplicate this expansion code.
5054 procedure Expand_N_Not_In (N : Node_Id) is
5055 Loc : constant Source_Ptr := Sloc (N);
5056 Typ : constant Entity_Id := Etype (N);
5057 Cfs : constant Boolean := Comes_From_Source (N);
5059 begin
5060 Rewrite (N,
5061 Make_Op_Not (Loc,
5062 Right_Opnd =>
5063 Make_In (Loc,
5064 Left_Opnd => Left_Opnd (N),
5065 Right_Opnd => Right_Opnd (N))));
5067 -- If this is a set membership, preserve list of alternatives
5069 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
5071 -- We want this to appear as coming from source if original does (see
5072 -- transformations in Expand_N_In).
5074 Set_Comes_From_Source (N, Cfs);
5075 Set_Comes_From_Source (Right_Opnd (N), Cfs);
5077 -- Now analyze transformed node
5079 Analyze_And_Resolve (N, Typ);
5080 end Expand_N_Not_In;
5082 -------------------
5083 -- Expand_N_Null --
5084 -------------------
5086 -- The only replacement required is for the case of a null of a type that
5087 -- is an access to protected subprogram, or a subtype thereof. We represent
5088 -- such access values as a record, and so we must replace the occurrence of
5089 -- null by the equivalent record (with a null address and a null pointer in
5090 -- it), so that the backend creates the proper value.
5092 procedure Expand_N_Null (N : Node_Id) is
5093 Loc : constant Source_Ptr := Sloc (N);
5094 Typ : constant Entity_Id := Base_Type (Etype (N));
5095 Agg : Node_Id;
5097 begin
5098 if Is_Access_Protected_Subprogram_Type (Typ) then
5099 Agg :=
5100 Make_Aggregate (Loc,
5101 Expressions => New_List (
5102 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
5103 Make_Null (Loc)));
5105 Rewrite (N, Agg);
5106 Analyze_And_Resolve (N, Equivalent_Type (Typ));
5108 -- For subsequent semantic analysis, the node must retain its type.
5109 -- Gigi in any case replaces this type by the corresponding record
5110 -- type before processing the node.
5112 Set_Etype (N, Typ);
5113 end if;
5115 exception
5116 when RE_Not_Available =>
5117 return;
5118 end Expand_N_Null;
5120 ---------------------
5121 -- Expand_N_Op_Abs --
5122 ---------------------
5124 procedure Expand_N_Op_Abs (N : Node_Id) is
5125 Loc : constant Source_Ptr := Sloc (N);
5126 Expr : constant Node_Id := Right_Opnd (N);
5128 begin
5129 Unary_Op_Validity_Checks (N);
5131 -- Deal with software overflow checking
5133 if not Backend_Overflow_Checks_On_Target
5134 and then Is_Signed_Integer_Type (Etype (N))
5135 and then Do_Overflow_Check (N)
5136 then
5137 -- The only case to worry about is when the argument is equal to the
5138 -- largest negative number, so what we do is to insert the check:
5140 -- [constraint_error when Expr = typ'Base'First]
5142 -- with the usual Duplicate_Subexpr use coding for expr
5144 Insert_Action (N,
5145 Make_Raise_Constraint_Error (Loc,
5146 Condition =>
5147 Make_Op_Eq (Loc,
5148 Left_Opnd => Duplicate_Subexpr (Expr),
5149 Right_Opnd =>
5150 Make_Attribute_Reference (Loc,
5151 Prefix =>
5152 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
5153 Attribute_Name => Name_First)),
5154 Reason => CE_Overflow_Check_Failed));
5155 end if;
5157 -- Vax floating-point types case
5159 if Vax_Float (Etype (N)) then
5160 Expand_Vax_Arith (N);
5161 end if;
5162 end Expand_N_Op_Abs;
5164 ---------------------
5165 -- Expand_N_Op_Add --
5166 ---------------------
5168 procedure Expand_N_Op_Add (N : Node_Id) is
5169 Typ : constant Entity_Id := Etype (N);
5171 begin
5172 Binary_Op_Validity_Checks (N);
5174 -- N + 0 = 0 + N = N for integer types
5176 if Is_Integer_Type (Typ) then
5177 if Compile_Time_Known_Value (Right_Opnd (N))
5178 and then Expr_Value (Right_Opnd (N)) = Uint_0
5179 then
5180 Rewrite (N, Left_Opnd (N));
5181 return;
5183 elsif Compile_Time_Known_Value (Left_Opnd (N))
5184 and then Expr_Value (Left_Opnd (N)) = Uint_0
5185 then
5186 Rewrite (N, Right_Opnd (N));
5187 return;
5188 end if;
5189 end if;
5191 -- Arithmetic overflow checks for signed integer/fixed point types
5193 if Is_Signed_Integer_Type (Typ)
5194 or else Is_Fixed_Point_Type (Typ)
5195 then
5196 Apply_Arithmetic_Overflow_Check (N);
5197 return;
5199 -- Vax floating-point types case
5201 elsif Vax_Float (Typ) then
5202 Expand_Vax_Arith (N);
5203 end if;
5204 end Expand_N_Op_Add;
5206 ---------------------
5207 -- Expand_N_Op_And --
5208 ---------------------
5210 procedure Expand_N_Op_And (N : Node_Id) is
5211 Typ : constant Entity_Id := Etype (N);
5213 begin
5214 Binary_Op_Validity_Checks (N);
5216 if Is_Array_Type (Etype (N)) then
5217 Expand_Boolean_Operator (N);
5219 elsif Is_Boolean_Type (Etype (N)) then
5221 -- Replace AND by AND THEN if Short_Circuit_And_Or active and the
5222 -- type is standard Boolean (do not mess with AND that uses a non-
5223 -- standard Boolean type, because something strange is going on).
5225 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
5226 Rewrite (N,
5227 Make_And_Then (Sloc (N),
5228 Left_Opnd => Relocate_Node (Left_Opnd (N)),
5229 Right_Opnd => Relocate_Node (Right_Opnd (N))));
5230 Analyze_And_Resolve (N, Typ);
5232 -- Otherwise, adjust conditions
5234 else
5235 Adjust_Condition (Left_Opnd (N));
5236 Adjust_Condition (Right_Opnd (N));
5237 Set_Etype (N, Standard_Boolean);
5238 Adjust_Result_Type (N, Typ);
5239 end if;
5241 elsif Is_Intrinsic_Subprogram (Entity (N)) then
5242 Expand_Intrinsic_Call (N, Entity (N));
5244 end if;
5245 end Expand_N_Op_And;
5247 ------------------------
5248 -- Expand_N_Op_Concat --
5249 ------------------------
5251 procedure Expand_N_Op_Concat (N : Node_Id) is
5252 Opnds : List_Id;
5253 -- List of operands to be concatenated
5255 Cnode : Node_Id;
5256 -- Node which is to be replaced by the result of concatenating the nodes
5257 -- in the list Opnds.
5259 begin
5260 -- Ensure validity of both operands
5262 Binary_Op_Validity_Checks (N);
5264 -- If we are the left operand of a concatenation higher up the tree,
5265 -- then do nothing for now, since we want to deal with a series of
5266 -- concatenations as a unit.
5268 if Nkind (Parent (N)) = N_Op_Concat
5269 and then N = Left_Opnd (Parent (N))
5270 then
5271 return;
5272 end if;
5274 -- We get here with a concatenation whose left operand may be a
5275 -- concatenation itself with a consistent type. We need to process
5276 -- these concatenation operands from left to right, which means
5277 -- from the deepest node in the tree to the highest node.
5279 Cnode := N;
5280 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
5281 Cnode := Left_Opnd (Cnode);
5282 end loop;
5284 -- Now Cnode is the deepest concatenation, and its parents are the
5285 -- concatenation nodes above, so now we process bottom up, doing the
5286 -- operations. We gather a string that is as long as possible up to five
5287 -- operands.
5289 -- The outer loop runs more than once if more than one concatenation
5290 -- type is involved.
5292 Outer : loop
5293 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5294 Set_Parent (Opnds, N);
5296 -- The inner loop gathers concatenation operands
5298 Inner : while Cnode /= N
5299 and then Base_Type (Etype (Cnode)) =
5300 Base_Type (Etype (Parent (Cnode)))
5301 loop
5302 Cnode := Parent (Cnode);
5303 Append (Right_Opnd (Cnode), Opnds);
5304 end loop Inner;
5306 Expand_Concatenate (Cnode, Opnds);
5308 exit Outer when Cnode = N;
5309 Cnode := Parent (Cnode);
5310 end loop Outer;
5311 end Expand_N_Op_Concat;
5313 ------------------------
5314 -- Expand_N_Op_Divide --
5315 ------------------------
5317 procedure Expand_N_Op_Divide (N : Node_Id) is
5318 Loc : constant Source_Ptr := Sloc (N);
5319 Lopnd : constant Node_Id := Left_Opnd (N);
5320 Ropnd : constant Node_Id := Right_Opnd (N);
5321 Ltyp : constant Entity_Id := Etype (Lopnd);
5322 Rtyp : constant Entity_Id := Etype (Ropnd);
5323 Typ : Entity_Id := Etype (N);
5324 Rknow : constant Boolean := Is_Integer_Type (Typ)
5325 and then
5326 Compile_Time_Known_Value (Ropnd);
5327 Rval : Uint;
5329 begin
5330 Binary_Op_Validity_Checks (N);
5332 if Rknow then
5333 Rval := Expr_Value (Ropnd);
5334 end if;
5336 -- N / 1 = N for integer types
5338 if Rknow and then Rval = Uint_1 then
5339 Rewrite (N, Lopnd);
5340 return;
5341 end if;
5343 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5344 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5345 -- operand is an unsigned integer, as required for this to work.
5347 if Nkind (Ropnd) = N_Op_Expon
5348 and then Is_Power_Of_2_For_Shift (Ropnd)
5350 -- We cannot do this transformation in configurable run time mode if we
5351 -- have 64-bit integers and long shifts are not available.
5353 and then
5354 (Esize (Ltyp) <= 32
5355 or else Support_Long_Shifts_On_Target)
5356 then
5357 Rewrite (N,
5358 Make_Op_Shift_Right (Loc,
5359 Left_Opnd => Lopnd,
5360 Right_Opnd =>
5361 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5362 Analyze_And_Resolve (N, Typ);
5363 return;
5364 end if;
5366 -- Do required fixup of universal fixed operation
5368 if Typ = Universal_Fixed then
5369 Fixup_Universal_Fixed_Operation (N);
5370 Typ := Etype (N);
5371 end if;
5373 -- Divisions with fixed-point results
5375 if Is_Fixed_Point_Type (Typ) then
5377 -- No special processing if Treat_Fixed_As_Integer is set, since
5378 -- from a semantic point of view such operations are simply integer
5379 -- operations and will be treated that way.
5381 if not Treat_Fixed_As_Integer (N) then
5382 if Is_Integer_Type (Rtyp) then
5383 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5384 else
5385 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5386 end if;
5387 end if;
5389 -- Other cases of division of fixed-point operands. Again we exclude the
5390 -- case where Treat_Fixed_As_Integer is set.
5392 elsif (Is_Fixed_Point_Type (Ltyp) or else
5393 Is_Fixed_Point_Type (Rtyp))
5394 and then not Treat_Fixed_As_Integer (N)
5395 then
5396 if Is_Integer_Type (Typ) then
5397 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5398 else
5399 pragma Assert (Is_Floating_Point_Type (Typ));
5400 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5401 end if;
5403 -- Mixed-mode operations can appear in a non-static universal context,
5404 -- in which case the integer argument must be converted explicitly.
5406 elsif Typ = Universal_Real
5407 and then Is_Integer_Type (Rtyp)
5408 then
5409 Rewrite (Ropnd,
5410 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5412 Analyze_And_Resolve (Ropnd, Universal_Real);
5414 elsif Typ = Universal_Real
5415 and then Is_Integer_Type (Ltyp)
5416 then
5417 Rewrite (Lopnd,
5418 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5420 Analyze_And_Resolve (Lopnd, Universal_Real);
5422 -- Non-fixed point cases, do integer zero divide and overflow checks
5424 elsif Is_Integer_Type (Typ) then
5425 Apply_Divide_Check (N);
5427 -- Check for 64-bit division available, or long shifts if the divisor
5428 -- is a small power of 2 (since such divides will be converted into
5429 -- long shifts).
5431 if Esize (Ltyp) > 32
5432 and then not Support_64_Bit_Divides_On_Target
5433 and then
5434 (not Rknow
5435 or else not Support_Long_Shifts_On_Target
5436 or else (Rval /= Uint_2 and then
5437 Rval /= Uint_4 and then
5438 Rval /= Uint_8 and then
5439 Rval /= Uint_16 and then
5440 Rval /= Uint_32 and then
5441 Rval /= Uint_64))
5442 then
5443 Error_Msg_CRT ("64-bit division", N);
5444 end if;
5446 -- Deal with Vax_Float
5448 elsif Vax_Float (Typ) then
5449 Expand_Vax_Arith (N);
5450 return;
5451 end if;
5452 end Expand_N_Op_Divide;
5454 --------------------
5455 -- Expand_N_Op_Eq --
5456 --------------------
5458 procedure Expand_N_Op_Eq (N : Node_Id) is
5459 Loc : constant Source_Ptr := Sloc (N);
5460 Typ : constant Entity_Id := Etype (N);
5461 Lhs : constant Node_Id := Left_Opnd (N);
5462 Rhs : constant Node_Id := Right_Opnd (N);
5463 Bodies : constant List_Id := New_List;
5464 A_Typ : constant Entity_Id := Etype (Lhs);
5466 Typl : Entity_Id := A_Typ;
5467 Op_Name : Entity_Id;
5468 Prim : Elmt_Id;
5470 procedure Build_Equality_Call (Eq : Entity_Id);
5471 -- If a constructed equality exists for the type or for its parent,
5472 -- build and analyze call, adding conversions if the operation is
5473 -- inherited.
5475 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5476 -- Determines whether a type has a subcomponent of an unconstrained
5477 -- Unchecked_Union subtype. Typ is a record type.
5479 -------------------------
5480 -- Build_Equality_Call --
5481 -------------------------
5483 procedure Build_Equality_Call (Eq : Entity_Id) is
5484 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5485 L_Exp : Node_Id := Relocate_Node (Lhs);
5486 R_Exp : Node_Id := Relocate_Node (Rhs);
5488 begin
5489 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5490 and then not Is_Class_Wide_Type (A_Typ)
5491 then
5492 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5493 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5494 end if;
5496 -- If we have an Unchecked_Union, we need to add the inferred
5497 -- discriminant values as actuals in the function call. At this
5498 -- point, the expansion has determined that both operands have
5499 -- inferable discriminants.
5501 if Is_Unchecked_Union (Op_Type) then
5502 declare
5503 Lhs_Type : constant Node_Id := Etype (L_Exp);
5504 Rhs_Type : constant Node_Id := Etype (R_Exp);
5505 Lhs_Discr_Val : Node_Id;
5506 Rhs_Discr_Val : Node_Id;
5508 begin
5509 -- Per-object constrained selected components require special
5510 -- attention. If the enclosing scope of the component is an
5511 -- Unchecked_Union, we cannot reference its discriminants
5512 -- directly. This is why we use the two extra parameters of
5513 -- the equality function of the enclosing Unchecked_Union.
5515 -- type UU_Type (Discr : Integer := 0) is
5516 -- . . .
5517 -- end record;
5518 -- pragma Unchecked_Union (UU_Type);
5520 -- 1. Unchecked_Union enclosing record:
5522 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5523 -- . . .
5524 -- Comp : UU_Type (Discr);
5525 -- . . .
5526 -- end Enclosing_UU_Type;
5527 -- pragma Unchecked_Union (Enclosing_UU_Type);
5529 -- Obj1 : Enclosing_UU_Type;
5530 -- Obj2 : Enclosing_UU_Type (1);
5532 -- [. . .] Obj1 = Obj2 [. . .]
5534 -- Generated code:
5536 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5538 -- A and B are the formal parameters of the equality function
5539 -- of Enclosing_UU_Type. The function always has two extra
5540 -- formals to capture the inferred discriminant values.
5542 -- 2. Non-Unchecked_Union enclosing record:
5544 -- type
5545 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5546 -- is record
5547 -- . . .
5548 -- Comp : UU_Type (Discr);
5549 -- . . .
5550 -- end Enclosing_Non_UU_Type;
5552 -- Obj1 : Enclosing_Non_UU_Type;
5553 -- Obj2 : Enclosing_Non_UU_Type (1);
5555 -- ... Obj1 = Obj2 ...
5557 -- Generated code:
5559 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5560 -- obj1.discr, obj2.discr)) then
5562 -- In this case we can directly reference the discriminants of
5563 -- the enclosing record.
5565 -- Lhs of equality
5567 if Nkind (Lhs) = N_Selected_Component
5568 and then Has_Per_Object_Constraint
5569 (Entity (Selector_Name (Lhs)))
5570 then
5571 -- Enclosing record is an Unchecked_Union, use formal A
5573 if Is_Unchecked_Union
5574 (Scope (Entity (Selector_Name (Lhs))))
5575 then
5576 Lhs_Discr_Val := Make_Identifier (Loc, Name_A);
5578 -- Enclosing record is of a non-Unchecked_Union type, it is
5579 -- possible to reference the discriminant.
5581 else
5582 Lhs_Discr_Val :=
5583 Make_Selected_Component (Loc,
5584 Prefix => Prefix (Lhs),
5585 Selector_Name =>
5586 New_Copy
5587 (Get_Discriminant_Value
5588 (First_Discriminant (Lhs_Type),
5589 Lhs_Type,
5590 Stored_Constraint (Lhs_Type))));
5591 end if;
5593 -- Comment needed here ???
5595 else
5596 -- Infer the discriminant value
5598 Lhs_Discr_Val :=
5599 New_Copy
5600 (Get_Discriminant_Value
5601 (First_Discriminant (Lhs_Type),
5602 Lhs_Type,
5603 Stored_Constraint (Lhs_Type)));
5604 end if;
5606 -- Rhs of equality
5608 if Nkind (Rhs) = N_Selected_Component
5609 and then Has_Per_Object_Constraint
5610 (Entity (Selector_Name (Rhs)))
5611 then
5612 if Is_Unchecked_Union
5613 (Scope (Entity (Selector_Name (Rhs))))
5614 then
5615 Rhs_Discr_Val := Make_Identifier (Loc, Name_B);
5617 else
5618 Rhs_Discr_Val :=
5619 Make_Selected_Component (Loc,
5620 Prefix => Prefix (Rhs),
5621 Selector_Name =>
5622 New_Copy (Get_Discriminant_Value (
5623 First_Discriminant (Rhs_Type),
5624 Rhs_Type,
5625 Stored_Constraint (Rhs_Type))));
5627 end if;
5628 else
5629 Rhs_Discr_Val :=
5630 New_Copy (Get_Discriminant_Value (
5631 First_Discriminant (Rhs_Type),
5632 Rhs_Type,
5633 Stored_Constraint (Rhs_Type)));
5635 end if;
5637 Rewrite (N,
5638 Make_Function_Call (Loc,
5639 Name => New_Reference_To (Eq, Loc),
5640 Parameter_Associations => New_List (
5641 L_Exp,
5642 R_Exp,
5643 Lhs_Discr_Val,
5644 Rhs_Discr_Val)));
5645 end;
5647 -- Normal case, not an unchecked union
5649 else
5650 Rewrite (N,
5651 Make_Function_Call (Loc,
5652 Name => New_Reference_To (Eq, Loc),
5653 Parameter_Associations => New_List (L_Exp, R_Exp)));
5654 end if;
5656 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5657 end Build_Equality_Call;
5659 ------------------------------------
5660 -- Has_Unconstrained_UU_Component --
5661 ------------------------------------
5663 function Has_Unconstrained_UU_Component
5664 (Typ : Node_Id) return Boolean
5666 Tdef : constant Node_Id :=
5667 Type_Definition (Declaration_Node (Base_Type (Typ)));
5668 Clist : Node_Id;
5669 Vpart : Node_Id;
5671 function Component_Is_Unconstrained_UU
5672 (Comp : Node_Id) return Boolean;
5673 -- Determines whether the subtype of the component is an
5674 -- unconstrained Unchecked_Union.
5676 function Variant_Is_Unconstrained_UU
5677 (Variant : Node_Id) return Boolean;
5678 -- Determines whether a component of the variant has an unconstrained
5679 -- Unchecked_Union subtype.
5681 -----------------------------------
5682 -- Component_Is_Unconstrained_UU --
5683 -----------------------------------
5685 function Component_Is_Unconstrained_UU
5686 (Comp : Node_Id) return Boolean
5688 begin
5689 if Nkind (Comp) /= N_Component_Declaration then
5690 return False;
5691 end if;
5693 declare
5694 Sindic : constant Node_Id :=
5695 Subtype_Indication (Component_Definition (Comp));
5697 begin
5698 -- Unconstrained nominal type. In the case of a constraint
5699 -- present, the node kind would have been N_Subtype_Indication.
5701 if Nkind (Sindic) = N_Identifier then
5702 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5703 end if;
5705 return False;
5706 end;
5707 end Component_Is_Unconstrained_UU;
5709 ---------------------------------
5710 -- Variant_Is_Unconstrained_UU --
5711 ---------------------------------
5713 function Variant_Is_Unconstrained_UU
5714 (Variant : Node_Id) return Boolean
5716 Clist : constant Node_Id := Component_List (Variant);
5718 begin
5719 if Is_Empty_List (Component_Items (Clist)) then
5720 return False;
5721 end if;
5723 -- We only need to test one component
5725 declare
5726 Comp : Node_Id := First (Component_Items (Clist));
5728 begin
5729 while Present (Comp) loop
5730 if Component_Is_Unconstrained_UU (Comp) then
5731 return True;
5732 end if;
5734 Next (Comp);
5735 end loop;
5736 end;
5738 -- None of the components withing the variant were of
5739 -- unconstrained Unchecked_Union type.
5741 return False;
5742 end Variant_Is_Unconstrained_UU;
5744 -- Start of processing for Has_Unconstrained_UU_Component
5746 begin
5747 if Null_Present (Tdef) then
5748 return False;
5749 end if;
5751 Clist := Component_List (Tdef);
5752 Vpart := Variant_Part (Clist);
5754 -- Inspect available components
5756 if Present (Component_Items (Clist)) then
5757 declare
5758 Comp : Node_Id := First (Component_Items (Clist));
5760 begin
5761 while Present (Comp) loop
5763 -- One component is sufficient
5765 if Component_Is_Unconstrained_UU (Comp) then
5766 return True;
5767 end if;
5769 Next (Comp);
5770 end loop;
5771 end;
5772 end if;
5774 -- Inspect available components withing variants
5776 if Present (Vpart) then
5777 declare
5778 Variant : Node_Id := First (Variants (Vpart));
5780 begin
5781 while Present (Variant) loop
5783 -- One component within a variant is sufficient
5785 if Variant_Is_Unconstrained_UU (Variant) then
5786 return True;
5787 end if;
5789 Next (Variant);
5790 end loop;
5791 end;
5792 end if;
5794 -- Neither the available components, nor the components inside the
5795 -- variant parts were of an unconstrained Unchecked_Union subtype.
5797 return False;
5798 end Has_Unconstrained_UU_Component;
5800 -- Start of processing for Expand_N_Op_Eq
5802 begin
5803 Binary_Op_Validity_Checks (N);
5805 if Ekind (Typl) = E_Private_Type then
5806 Typl := Underlying_Type (Typl);
5807 elsif Ekind (Typl) = E_Private_Subtype then
5808 Typl := Underlying_Type (Base_Type (Typl));
5809 else
5810 null;
5811 end if;
5813 -- It may happen in error situations that the underlying type is not
5814 -- set. The error will be detected later, here we just defend the
5815 -- expander code.
5817 if No (Typl) then
5818 return;
5819 end if;
5821 Typl := Base_Type (Typl);
5823 -- Boolean types (requiring handling of non-standard case)
5825 if Is_Boolean_Type (Typl) then
5826 Adjust_Condition (Left_Opnd (N));
5827 Adjust_Condition (Right_Opnd (N));
5828 Set_Etype (N, Standard_Boolean);
5829 Adjust_Result_Type (N, Typ);
5831 -- Array types
5833 elsif Is_Array_Type (Typl) then
5835 -- If we are doing full validity checking, and it is possible for the
5836 -- array elements to be invalid then expand out array comparisons to
5837 -- make sure that we check the array elements.
5839 if Validity_Check_Operands
5840 and then not Is_Known_Valid (Component_Type (Typl))
5841 then
5842 declare
5843 Save_Force_Validity_Checks : constant Boolean :=
5844 Force_Validity_Checks;
5845 begin
5846 Force_Validity_Checks := True;
5847 Rewrite (N,
5848 Expand_Array_Equality
5850 Relocate_Node (Lhs),
5851 Relocate_Node (Rhs),
5852 Bodies,
5853 Typl));
5854 Insert_Actions (N, Bodies);
5855 Analyze_And_Resolve (N, Standard_Boolean);
5856 Force_Validity_Checks := Save_Force_Validity_Checks;
5857 end;
5859 -- Packed case where both operands are known aligned
5861 elsif Is_Bit_Packed_Array (Typl)
5862 and then not Is_Possibly_Unaligned_Object (Lhs)
5863 and then not Is_Possibly_Unaligned_Object (Rhs)
5864 then
5865 Expand_Packed_Eq (N);
5867 -- Where the component type is elementary we can use a block bit
5868 -- comparison (if supported on the target) exception in the case
5869 -- of floating-point (negative zero issues require element by
5870 -- element comparison), and atomic types (where we must be sure
5871 -- to load elements independently) and possibly unaligned arrays.
5873 elsif Is_Elementary_Type (Component_Type (Typl))
5874 and then not Is_Floating_Point_Type (Component_Type (Typl))
5875 and then not Is_Atomic (Component_Type (Typl))
5876 and then not Is_Possibly_Unaligned_Object (Lhs)
5877 and then not Is_Possibly_Unaligned_Object (Rhs)
5878 and then Support_Composite_Compare_On_Target
5879 then
5880 null;
5882 -- For composite and floating-point cases, expand equality loop to
5883 -- make sure of using proper comparisons for tagged types, and
5884 -- correctly handling the floating-point case.
5886 else
5887 Rewrite (N,
5888 Expand_Array_Equality
5890 Relocate_Node (Lhs),
5891 Relocate_Node (Rhs),
5892 Bodies,
5893 Typl));
5894 Insert_Actions (N, Bodies, Suppress => All_Checks);
5895 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5896 end if;
5898 -- Record Types
5900 elsif Is_Record_Type (Typl) then
5902 -- For tagged types, use the primitive "="
5904 if Is_Tagged_Type (Typl) then
5906 -- No need to do anything else compiling under restriction
5907 -- No_Dispatching_Calls. During the semantic analysis we
5908 -- already notified such violation.
5910 if Restriction_Active (No_Dispatching_Calls) then
5911 return;
5912 end if;
5914 -- If this is derived from an untagged private type completed with
5915 -- a tagged type, it does not have a full view, so we use the
5916 -- primitive operations of the private type. This check should no
5917 -- longer be necessary when these types get their full views???
5919 if Is_Private_Type (A_Typ)
5920 and then not Is_Tagged_Type (A_Typ)
5921 and then Is_Derived_Type (A_Typ)
5922 and then No (Full_View (A_Typ))
5923 then
5924 -- Search for equality operation, checking that the operands
5925 -- have the same type. Note that we must find a matching entry,
5926 -- or something is very wrong!
5928 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5930 while Present (Prim) loop
5931 exit when Chars (Node (Prim)) = Name_Op_Eq
5932 and then Etype (First_Formal (Node (Prim))) =
5933 Etype (Next_Formal (First_Formal (Node (Prim))))
5934 and then
5935 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5937 Next_Elmt (Prim);
5938 end loop;
5940 pragma Assert (Present (Prim));
5941 Op_Name := Node (Prim);
5943 -- Find the type's predefined equality or an overriding
5944 -- user- defined equality. The reason for not simply calling
5945 -- Find_Prim_Op here is that there may be a user-defined
5946 -- overloaded equality op that precedes the equality that we want,
5947 -- so we have to explicitly search (e.g., there could be an
5948 -- equality with two different parameter types).
5950 else
5951 if Is_Class_Wide_Type (Typl) then
5952 Typl := Root_Type (Typl);
5953 end if;
5955 Prim := First_Elmt (Primitive_Operations (Typl));
5956 while Present (Prim) loop
5957 exit when Chars (Node (Prim)) = Name_Op_Eq
5958 and then Etype (First_Formal (Node (Prim))) =
5959 Etype (Next_Formal (First_Formal (Node (Prim))))
5960 and then
5961 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5963 Next_Elmt (Prim);
5964 end loop;
5966 pragma Assert (Present (Prim));
5967 Op_Name := Node (Prim);
5968 end if;
5970 Build_Equality_Call (Op_Name);
5972 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5973 -- predefined equality operator for a type which has a subcomponent
5974 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5976 elsif Has_Unconstrained_UU_Component (Typl) then
5977 Insert_Action (N,
5978 Make_Raise_Program_Error (Loc,
5979 Reason => PE_Unchecked_Union_Restriction));
5981 -- Prevent Gigi from generating incorrect code by rewriting the
5982 -- equality as a standard False.
5984 Rewrite (N,
5985 New_Occurrence_Of (Standard_False, Loc));
5987 elsif Is_Unchecked_Union (Typl) then
5989 -- If we can infer the discriminants of the operands, we make a
5990 -- call to the TSS equality function.
5992 if Has_Inferable_Discriminants (Lhs)
5993 and then
5994 Has_Inferable_Discriminants (Rhs)
5995 then
5996 Build_Equality_Call
5997 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5999 else
6000 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6001 -- the predefined equality operator for an Unchecked_Union type
6002 -- if either of the operands lack inferable discriminants.
6004 Insert_Action (N,
6005 Make_Raise_Program_Error (Loc,
6006 Reason => PE_Unchecked_Union_Restriction));
6008 -- Prevent Gigi from generating incorrect code by rewriting
6009 -- the equality as a standard False.
6011 Rewrite (N,
6012 New_Occurrence_Of (Standard_False, Loc));
6014 end if;
6016 -- If a type support function is present (for complex cases), use it
6018 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
6019 Build_Equality_Call
6020 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6022 -- Otherwise expand the component by component equality. Note that
6023 -- we never use block-bit comparisons for records, because of the
6024 -- problems with gaps. The backend will often be able to recombine
6025 -- the separate comparisons that we generate here.
6027 else
6028 Remove_Side_Effects (Lhs);
6029 Remove_Side_Effects (Rhs);
6030 Rewrite (N,
6031 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
6033 Insert_Actions (N, Bodies, Suppress => All_Checks);
6034 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6035 end if;
6036 end if;
6038 -- Test if result is known at compile time
6040 Rewrite_Comparison (N);
6042 -- If we still have comparison for Vax_Float, process it
6044 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
6045 Expand_Vax_Comparison (N);
6046 return;
6047 end if;
6048 end Expand_N_Op_Eq;
6050 -----------------------
6051 -- Expand_N_Op_Expon --
6052 -----------------------
6054 procedure Expand_N_Op_Expon (N : Node_Id) is
6055 Loc : constant Source_Ptr := Sloc (N);
6056 Typ : constant Entity_Id := Etype (N);
6057 Rtyp : constant Entity_Id := Root_Type (Typ);
6058 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
6059 Bastyp : constant Node_Id := Etype (Base);
6060 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
6061 Exptyp : constant Entity_Id := Etype (Exp);
6062 Ovflo : constant Boolean := Do_Overflow_Check (N);
6063 Expv : Uint;
6064 Xnode : Node_Id;
6065 Temp : Node_Id;
6066 Rent : RE_Id;
6067 Ent : Entity_Id;
6068 Etyp : Entity_Id;
6070 begin
6071 Binary_Op_Validity_Checks (N);
6073 -- If either operand is of a private type, then we have the use of an
6074 -- intrinsic operator, and we get rid of the privateness, by using root
6075 -- types of underlying types for the actual operation. Otherwise the
6076 -- private types will cause trouble if we expand multiplications or
6077 -- shifts etc. We also do this transformation if the result type is
6078 -- different from the base type.
6080 if Is_Private_Type (Etype (Base))
6081 or else
6082 Is_Private_Type (Typ)
6083 or else
6084 Is_Private_Type (Exptyp)
6085 or else
6086 Rtyp /= Root_Type (Bastyp)
6087 then
6088 declare
6089 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
6090 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
6092 begin
6093 Rewrite (N,
6094 Unchecked_Convert_To (Typ,
6095 Make_Op_Expon (Loc,
6096 Left_Opnd => Unchecked_Convert_To (Bt, Base),
6097 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
6098 Analyze_And_Resolve (N, Typ);
6099 return;
6100 end;
6101 end if;
6103 -- Test for case of known right argument
6105 if Compile_Time_Known_Value (Exp) then
6106 Expv := Expr_Value (Exp);
6108 -- We only fold small non-negative exponents. You might think we
6109 -- could fold small negative exponents for the real case, but we
6110 -- can't because we are required to raise Constraint_Error for
6111 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
6112 -- See ACVC test C4A012B.
6114 if Expv >= 0 and then Expv <= 4 then
6116 -- X ** 0 = 1 (or 1.0)
6118 if Expv = 0 then
6120 -- Call Remove_Side_Effects to ensure that any side effects
6121 -- in the ignored left operand (in particular function calls
6122 -- to user defined functions) are properly executed.
6124 Remove_Side_Effects (Base);
6126 if Ekind (Typ) in Integer_Kind then
6127 Xnode := Make_Integer_Literal (Loc, Intval => 1);
6128 else
6129 Xnode := Make_Real_Literal (Loc, Ureal_1);
6130 end if;
6132 -- X ** 1 = X
6134 elsif Expv = 1 then
6135 Xnode := Base;
6137 -- X ** 2 = X * X
6139 elsif Expv = 2 then
6140 Xnode :=
6141 Make_Op_Multiply (Loc,
6142 Left_Opnd => Duplicate_Subexpr (Base),
6143 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6145 -- X ** 3 = X * X * X
6147 elsif Expv = 3 then
6148 Xnode :=
6149 Make_Op_Multiply (Loc,
6150 Left_Opnd =>
6151 Make_Op_Multiply (Loc,
6152 Left_Opnd => Duplicate_Subexpr (Base),
6153 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
6154 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6156 -- X ** 4 ->
6157 -- En : constant base'type := base * base;
6158 -- ...
6159 -- En * En
6161 else -- Expv = 4
6162 Temp := Make_Temporary (Loc, 'E', Base);
6164 Insert_Actions (N, New_List (
6165 Make_Object_Declaration (Loc,
6166 Defining_Identifier => Temp,
6167 Constant_Present => True,
6168 Object_Definition => New_Reference_To (Typ, Loc),
6169 Expression =>
6170 Make_Op_Multiply (Loc,
6171 Left_Opnd => Duplicate_Subexpr (Base),
6172 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
6174 Xnode :=
6175 Make_Op_Multiply (Loc,
6176 Left_Opnd => New_Reference_To (Temp, Loc),
6177 Right_Opnd => New_Reference_To (Temp, Loc));
6178 end if;
6180 Rewrite (N, Xnode);
6181 Analyze_And_Resolve (N, Typ);
6182 return;
6183 end if;
6184 end if;
6186 -- Case of (2 ** expression) appearing as an argument of an integer
6187 -- multiplication, or as the right argument of a division of a non-
6188 -- negative integer. In such cases we leave the node untouched, setting
6189 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
6190 -- of the higher level node converts it into a shift.
6192 -- Another case is 2 ** N in any other context. We simply convert
6193 -- this to 1 * 2 ** N, and then the above transformation applies.
6195 -- Note: this transformation is not applicable for a modular type with
6196 -- a non-binary modulus in the multiplication case, since we get a wrong
6197 -- result if the shift causes an overflow before the modular reduction.
6199 if Nkind (Base) = N_Integer_Literal
6200 and then Intval (Base) = 2
6201 and then Is_Integer_Type (Root_Type (Exptyp))
6202 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
6203 and then Is_Unsigned_Type (Exptyp)
6204 and then not Ovflo
6205 then
6206 -- First the multiply and divide cases
6208 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
6209 declare
6210 P : constant Node_Id := Parent (N);
6211 L : constant Node_Id := Left_Opnd (P);
6212 R : constant Node_Id := Right_Opnd (P);
6214 begin
6215 if (Nkind (P) = N_Op_Multiply
6216 and then not Non_Binary_Modulus (Typ)
6217 and then
6218 ((Is_Integer_Type (Etype (L)) and then R = N)
6219 or else
6220 (Is_Integer_Type (Etype (R)) and then L = N))
6221 and then not Do_Overflow_Check (P))
6222 or else
6223 (Nkind (P) = N_Op_Divide
6224 and then Is_Integer_Type (Etype (L))
6225 and then Is_Unsigned_Type (Etype (L))
6226 and then R = N
6227 and then not Do_Overflow_Check (P))
6228 then
6229 Set_Is_Power_Of_2_For_Shift (N);
6230 return;
6231 end if;
6232 end;
6234 -- Now the other cases
6236 elsif not Non_Binary_Modulus (Typ) then
6237 Rewrite (N,
6238 Make_Op_Multiply (Loc,
6239 Left_Opnd => Make_Integer_Literal (Loc, 1),
6240 Right_Opnd => Relocate_Node (N)));
6241 Analyze_And_Resolve (N, Typ);
6242 return;
6243 end if;
6244 end if;
6246 -- Fall through if exponentiation must be done using a runtime routine
6248 -- First deal with modular case
6250 if Is_Modular_Integer_Type (Rtyp) then
6252 -- Non-binary case, we call the special exponentiation routine for
6253 -- the non-binary case, converting the argument to Long_Long_Integer
6254 -- and passing the modulus value. Then the result is converted back
6255 -- to the base type.
6257 if Non_Binary_Modulus (Rtyp) then
6258 Rewrite (N,
6259 Convert_To (Typ,
6260 Make_Function_Call (Loc,
6261 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
6262 Parameter_Associations => New_List (
6263 Convert_To (Standard_Integer, Base),
6264 Make_Integer_Literal (Loc, Modulus (Rtyp)),
6265 Exp))));
6267 -- Binary case, in this case, we call one of two routines, either the
6268 -- unsigned integer case, or the unsigned long long integer case,
6269 -- with a final "and" operation to do the required mod.
6271 else
6272 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
6273 Ent := RTE (RE_Exp_Unsigned);
6274 else
6275 Ent := RTE (RE_Exp_Long_Long_Unsigned);
6276 end if;
6278 Rewrite (N,
6279 Convert_To (Typ,
6280 Make_Op_And (Loc,
6281 Left_Opnd =>
6282 Make_Function_Call (Loc,
6283 Name => New_Reference_To (Ent, Loc),
6284 Parameter_Associations => New_List (
6285 Convert_To (Etype (First_Formal (Ent)), Base),
6286 Exp)),
6287 Right_Opnd =>
6288 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
6290 end if;
6292 -- Common exit point for modular type case
6294 Analyze_And_Resolve (N, Typ);
6295 return;
6297 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6298 -- It is not worth having routines for Short_[Short_]Integer, since for
6299 -- most machines it would not help, and it would generate more code that
6300 -- might need certification when a certified run time is required.
6302 -- In the integer cases, we have two routines, one for when overflow
6303 -- checks are required, and one when they are not required, since there
6304 -- is a real gain in omitting checks on many machines.
6306 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6307 or else (Rtyp = Base_Type (Standard_Long_Integer)
6308 and then
6309 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6310 or else (Rtyp = Universal_Integer)
6311 then
6312 Etyp := Standard_Long_Long_Integer;
6314 if Ovflo then
6315 Rent := RE_Exp_Long_Long_Integer;
6316 else
6317 Rent := RE_Exn_Long_Long_Integer;
6318 end if;
6320 elsif Is_Signed_Integer_Type (Rtyp) then
6321 Etyp := Standard_Integer;
6323 if Ovflo then
6324 Rent := RE_Exp_Integer;
6325 else
6326 Rent := RE_Exn_Integer;
6327 end if;
6329 -- Floating-point cases, always done using Long_Long_Float. We do not
6330 -- need separate routines for the overflow case here, since in the case
6331 -- of floating-point, we generate infinities anyway as a rule (either
6332 -- that or we automatically trap overflow), and if there is an infinity
6333 -- generated and a range check is required, the check will fail anyway.
6335 else
6336 pragma Assert (Is_Floating_Point_Type (Rtyp));
6337 Etyp := Standard_Long_Long_Float;
6338 Rent := RE_Exn_Long_Long_Float;
6339 end if;
6341 -- Common processing for integer cases and floating-point cases.
6342 -- If we are in the right type, we can call runtime routine directly
6344 if Typ = Etyp
6345 and then Rtyp /= Universal_Integer
6346 and then Rtyp /= Universal_Real
6347 then
6348 Rewrite (N,
6349 Make_Function_Call (Loc,
6350 Name => New_Reference_To (RTE (Rent), Loc),
6351 Parameter_Associations => New_List (Base, Exp)));
6353 -- Otherwise we have to introduce conversions (conversions are also
6354 -- required in the universal cases, since the runtime routine is
6355 -- typed using one of the standard types).
6357 else
6358 Rewrite (N,
6359 Convert_To (Typ,
6360 Make_Function_Call (Loc,
6361 Name => New_Reference_To (RTE (Rent), Loc),
6362 Parameter_Associations => New_List (
6363 Convert_To (Etyp, Base),
6364 Exp))));
6365 end if;
6367 Analyze_And_Resolve (N, Typ);
6368 return;
6370 exception
6371 when RE_Not_Available =>
6372 return;
6373 end Expand_N_Op_Expon;
6375 --------------------
6376 -- Expand_N_Op_Ge --
6377 --------------------
6379 procedure Expand_N_Op_Ge (N : Node_Id) is
6380 Typ : constant Entity_Id := Etype (N);
6381 Op1 : constant Node_Id := Left_Opnd (N);
6382 Op2 : constant Node_Id := Right_Opnd (N);
6383 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6385 begin
6386 Binary_Op_Validity_Checks (N);
6388 if Is_Array_Type (Typ1) then
6389 Expand_Array_Comparison (N);
6390 return;
6391 end if;
6393 if Is_Boolean_Type (Typ1) then
6394 Adjust_Condition (Op1);
6395 Adjust_Condition (Op2);
6396 Set_Etype (N, Standard_Boolean);
6397 Adjust_Result_Type (N, Typ);
6398 end if;
6400 Rewrite_Comparison (N);
6402 -- If we still have comparison, and Vax_Float type, process it
6404 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6405 Expand_Vax_Comparison (N);
6406 return;
6407 end if;
6408 end Expand_N_Op_Ge;
6410 --------------------
6411 -- Expand_N_Op_Gt --
6412 --------------------
6414 procedure Expand_N_Op_Gt (N : Node_Id) is
6415 Typ : constant Entity_Id := Etype (N);
6416 Op1 : constant Node_Id := Left_Opnd (N);
6417 Op2 : constant Node_Id := Right_Opnd (N);
6418 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6420 begin
6421 Binary_Op_Validity_Checks (N);
6423 if Is_Array_Type (Typ1) then
6424 Expand_Array_Comparison (N);
6425 return;
6426 end if;
6428 if Is_Boolean_Type (Typ1) then
6429 Adjust_Condition (Op1);
6430 Adjust_Condition (Op2);
6431 Set_Etype (N, Standard_Boolean);
6432 Adjust_Result_Type (N, Typ);
6433 end if;
6435 Rewrite_Comparison (N);
6437 -- If we still have comparison, and Vax_Float type, process it
6439 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6440 Expand_Vax_Comparison (N);
6441 return;
6442 end if;
6443 end Expand_N_Op_Gt;
6445 --------------------
6446 -- Expand_N_Op_Le --
6447 --------------------
6449 procedure Expand_N_Op_Le (N : Node_Id) is
6450 Typ : constant Entity_Id := Etype (N);
6451 Op1 : constant Node_Id := Left_Opnd (N);
6452 Op2 : constant Node_Id := Right_Opnd (N);
6453 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6455 begin
6456 Binary_Op_Validity_Checks (N);
6458 if Is_Array_Type (Typ1) then
6459 Expand_Array_Comparison (N);
6460 return;
6461 end if;
6463 if Is_Boolean_Type (Typ1) then
6464 Adjust_Condition (Op1);
6465 Adjust_Condition (Op2);
6466 Set_Etype (N, Standard_Boolean);
6467 Adjust_Result_Type (N, Typ);
6468 end if;
6470 Rewrite_Comparison (N);
6472 -- If we still have comparison, and Vax_Float type, process it
6474 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6475 Expand_Vax_Comparison (N);
6476 return;
6477 end if;
6478 end Expand_N_Op_Le;
6480 --------------------
6481 -- Expand_N_Op_Lt --
6482 --------------------
6484 procedure Expand_N_Op_Lt (N : Node_Id) is
6485 Typ : constant Entity_Id := Etype (N);
6486 Op1 : constant Node_Id := Left_Opnd (N);
6487 Op2 : constant Node_Id := Right_Opnd (N);
6488 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6490 begin
6491 Binary_Op_Validity_Checks (N);
6493 if Is_Array_Type (Typ1) then
6494 Expand_Array_Comparison (N);
6495 return;
6496 end if;
6498 if Is_Boolean_Type (Typ1) then
6499 Adjust_Condition (Op1);
6500 Adjust_Condition (Op2);
6501 Set_Etype (N, Standard_Boolean);
6502 Adjust_Result_Type (N, Typ);
6503 end if;
6505 Rewrite_Comparison (N);
6507 -- If we still have comparison, and Vax_Float type, process it
6509 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6510 Expand_Vax_Comparison (N);
6511 return;
6512 end if;
6513 end Expand_N_Op_Lt;
6515 -----------------------
6516 -- Expand_N_Op_Minus --
6517 -----------------------
6519 procedure Expand_N_Op_Minus (N : Node_Id) is
6520 Loc : constant Source_Ptr := Sloc (N);
6521 Typ : constant Entity_Id := Etype (N);
6523 begin
6524 Unary_Op_Validity_Checks (N);
6526 if not Backend_Overflow_Checks_On_Target
6527 and then Is_Signed_Integer_Type (Etype (N))
6528 and then Do_Overflow_Check (N)
6529 then
6530 -- Software overflow checking expands -expr into (0 - expr)
6532 Rewrite (N,
6533 Make_Op_Subtract (Loc,
6534 Left_Opnd => Make_Integer_Literal (Loc, 0),
6535 Right_Opnd => Right_Opnd (N)));
6537 Analyze_And_Resolve (N, Typ);
6539 -- Vax floating-point types case
6541 elsif Vax_Float (Etype (N)) then
6542 Expand_Vax_Arith (N);
6543 end if;
6544 end Expand_N_Op_Minus;
6546 ---------------------
6547 -- Expand_N_Op_Mod --
6548 ---------------------
6550 procedure Expand_N_Op_Mod (N : Node_Id) is
6551 Loc : constant Source_Ptr := Sloc (N);
6552 Typ : constant Entity_Id := Etype (N);
6553 Left : constant Node_Id := Left_Opnd (N);
6554 Right : constant Node_Id := Right_Opnd (N);
6555 DOC : constant Boolean := Do_Overflow_Check (N);
6556 DDC : constant Boolean := Do_Division_Check (N);
6558 LLB : Uint;
6559 Llo : Uint;
6560 Lhi : Uint;
6561 LOK : Boolean;
6562 Rlo : Uint;
6563 Rhi : Uint;
6564 ROK : Boolean;
6566 pragma Warnings (Off, Lhi);
6568 begin
6569 Binary_Op_Validity_Checks (N);
6571 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
6572 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
6574 -- Convert mod to rem if operands are known non-negative. We do this
6575 -- since it is quite likely that this will improve the quality of code,
6576 -- (the operation now corresponds to the hardware remainder), and it
6577 -- does not seem likely that it could be harmful.
6579 if LOK and then Llo >= 0
6580 and then
6581 ROK and then Rlo >= 0
6582 then
6583 Rewrite (N,
6584 Make_Op_Rem (Sloc (N),
6585 Left_Opnd => Left_Opnd (N),
6586 Right_Opnd => Right_Opnd (N)));
6588 -- Instead of reanalyzing the node we do the analysis manually. This
6589 -- avoids anomalies when the replacement is done in an instance and
6590 -- is epsilon more efficient.
6592 Set_Entity (N, Standard_Entity (S_Op_Rem));
6593 Set_Etype (N, Typ);
6594 Set_Do_Overflow_Check (N, DOC);
6595 Set_Do_Division_Check (N, DDC);
6596 Expand_N_Op_Rem (N);
6597 Set_Analyzed (N);
6599 -- Otherwise, normal mod processing
6601 else
6602 if Is_Integer_Type (Etype (N)) then
6603 Apply_Divide_Check (N);
6604 end if;
6606 -- Apply optimization x mod 1 = 0. We don't really need that with
6607 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6608 -- certainly harmless.
6610 if Is_Integer_Type (Etype (N))
6611 and then Compile_Time_Known_Value (Right)
6612 and then Expr_Value (Right) = Uint_1
6613 then
6614 -- Call Remove_Side_Effects to ensure that any side effects in
6615 -- the ignored left operand (in particular function calls to
6616 -- user defined functions) are properly executed.
6618 Remove_Side_Effects (Left);
6620 Rewrite (N, Make_Integer_Literal (Loc, 0));
6621 Analyze_And_Resolve (N, Typ);
6622 return;
6623 end if;
6625 -- Deal with annoying case of largest negative number remainder
6626 -- minus one. Gigi does not handle this case correctly, because
6627 -- it generates a divide instruction which may trap in this case.
6629 -- In fact the check is quite easy, if the right operand is -1, then
6630 -- the mod value is always 0, and we can just ignore the left operand
6631 -- completely in this case.
6633 -- The operand type may be private (e.g. in the expansion of an
6634 -- intrinsic operation) so we must use the underlying type to get the
6635 -- bounds, and convert the literals explicitly.
6637 LLB :=
6638 Expr_Value
6639 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6641 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6642 and then
6643 ((not LOK) or else (Llo = LLB))
6644 then
6645 Rewrite (N,
6646 Make_Conditional_Expression (Loc,
6647 Expressions => New_List (
6648 Make_Op_Eq (Loc,
6649 Left_Opnd => Duplicate_Subexpr (Right),
6650 Right_Opnd =>
6651 Unchecked_Convert_To (Typ,
6652 Make_Integer_Literal (Loc, -1))),
6653 Unchecked_Convert_To (Typ,
6654 Make_Integer_Literal (Loc, Uint_0)),
6655 Relocate_Node (N))));
6657 Set_Analyzed (Next (Next (First (Expressions (N)))));
6658 Analyze_And_Resolve (N, Typ);
6659 end if;
6660 end if;
6661 end Expand_N_Op_Mod;
6663 --------------------------
6664 -- Expand_N_Op_Multiply --
6665 --------------------------
6667 procedure Expand_N_Op_Multiply (N : Node_Id) is
6668 Loc : constant Source_Ptr := Sloc (N);
6669 Lop : constant Node_Id := Left_Opnd (N);
6670 Rop : constant Node_Id := Right_Opnd (N);
6672 Lp2 : constant Boolean :=
6673 Nkind (Lop) = N_Op_Expon
6674 and then Is_Power_Of_2_For_Shift (Lop);
6676 Rp2 : constant Boolean :=
6677 Nkind (Rop) = N_Op_Expon
6678 and then Is_Power_Of_2_For_Shift (Rop);
6680 Ltyp : constant Entity_Id := Etype (Lop);
6681 Rtyp : constant Entity_Id := Etype (Rop);
6682 Typ : Entity_Id := Etype (N);
6684 begin
6685 Binary_Op_Validity_Checks (N);
6687 -- Special optimizations for integer types
6689 if Is_Integer_Type (Typ) then
6691 -- N * 0 = 0 for integer types
6693 if Compile_Time_Known_Value (Rop)
6694 and then Expr_Value (Rop) = Uint_0
6695 then
6696 -- Call Remove_Side_Effects to ensure that any side effects in
6697 -- the ignored left operand (in particular function calls to
6698 -- user defined functions) are properly executed.
6700 Remove_Side_Effects (Lop);
6702 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6703 Analyze_And_Resolve (N, Typ);
6704 return;
6705 end if;
6707 -- Similar handling for 0 * N = 0
6709 if Compile_Time_Known_Value (Lop)
6710 and then Expr_Value (Lop) = Uint_0
6711 then
6712 Remove_Side_Effects (Rop);
6713 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6714 Analyze_And_Resolve (N, Typ);
6715 return;
6716 end if;
6718 -- N * 1 = 1 * N = N for integer types
6720 -- This optimisation is not done if we are going to
6721 -- rewrite the product 1 * 2 ** N to a shift.
6723 if Compile_Time_Known_Value (Rop)
6724 and then Expr_Value (Rop) = Uint_1
6725 and then not Lp2
6726 then
6727 Rewrite (N, Lop);
6728 return;
6730 elsif Compile_Time_Known_Value (Lop)
6731 and then Expr_Value (Lop) = Uint_1
6732 and then not Rp2
6733 then
6734 Rewrite (N, Rop);
6735 return;
6736 end if;
6737 end if;
6739 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6740 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6741 -- operand is an integer, as required for this to work.
6743 if Rp2 then
6744 if Lp2 then
6746 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6748 Rewrite (N,
6749 Make_Op_Expon (Loc,
6750 Left_Opnd => Make_Integer_Literal (Loc, 2),
6751 Right_Opnd =>
6752 Make_Op_Add (Loc,
6753 Left_Opnd => Right_Opnd (Lop),
6754 Right_Opnd => Right_Opnd (Rop))));
6755 Analyze_And_Resolve (N, Typ);
6756 return;
6758 else
6759 Rewrite (N,
6760 Make_Op_Shift_Left (Loc,
6761 Left_Opnd => Lop,
6762 Right_Opnd =>
6763 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6764 Analyze_And_Resolve (N, Typ);
6765 return;
6766 end if;
6768 -- Same processing for the operands the other way round
6770 elsif Lp2 then
6771 Rewrite (N,
6772 Make_Op_Shift_Left (Loc,
6773 Left_Opnd => Rop,
6774 Right_Opnd =>
6775 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6776 Analyze_And_Resolve (N, Typ);
6777 return;
6778 end if;
6780 -- Do required fixup of universal fixed operation
6782 if Typ = Universal_Fixed then
6783 Fixup_Universal_Fixed_Operation (N);
6784 Typ := Etype (N);
6785 end if;
6787 -- Multiplications with fixed-point results
6789 if Is_Fixed_Point_Type (Typ) then
6791 -- No special processing if Treat_Fixed_As_Integer is set, since from
6792 -- a semantic point of view such operations are simply integer
6793 -- operations and will be treated that way.
6795 if not Treat_Fixed_As_Integer (N) then
6797 -- Case of fixed * integer => fixed
6799 if Is_Integer_Type (Rtyp) then
6800 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6802 -- Case of integer * fixed => fixed
6804 elsif Is_Integer_Type (Ltyp) then
6805 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6807 -- Case of fixed * fixed => fixed
6809 else
6810 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6811 end if;
6812 end if;
6814 -- Other cases of multiplication of fixed-point operands. Again we
6815 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6817 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6818 and then not Treat_Fixed_As_Integer (N)
6819 then
6820 if Is_Integer_Type (Typ) then
6821 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6822 else
6823 pragma Assert (Is_Floating_Point_Type (Typ));
6824 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6825 end if;
6827 -- Mixed-mode operations can appear in a non-static universal context,
6828 -- in which case the integer argument must be converted explicitly.
6830 elsif Typ = Universal_Real
6831 and then Is_Integer_Type (Rtyp)
6832 then
6833 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6835 Analyze_And_Resolve (Rop, Universal_Real);
6837 elsif Typ = Universal_Real
6838 and then Is_Integer_Type (Ltyp)
6839 then
6840 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6842 Analyze_And_Resolve (Lop, Universal_Real);
6844 -- Non-fixed point cases, check software overflow checking required
6846 elsif Is_Signed_Integer_Type (Etype (N)) then
6847 Apply_Arithmetic_Overflow_Check (N);
6849 -- Deal with VAX float case
6851 elsif Vax_Float (Typ) then
6852 Expand_Vax_Arith (N);
6853 return;
6854 end if;
6855 end Expand_N_Op_Multiply;
6857 --------------------
6858 -- Expand_N_Op_Ne --
6859 --------------------
6861 procedure Expand_N_Op_Ne (N : Node_Id) is
6862 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6864 begin
6865 -- Case of elementary type with standard operator
6867 if Is_Elementary_Type (Typ)
6868 and then Sloc (Entity (N)) = Standard_Location
6869 then
6870 Binary_Op_Validity_Checks (N);
6872 -- Boolean types (requiring handling of non-standard case)
6874 if Is_Boolean_Type (Typ) then
6875 Adjust_Condition (Left_Opnd (N));
6876 Adjust_Condition (Right_Opnd (N));
6877 Set_Etype (N, Standard_Boolean);
6878 Adjust_Result_Type (N, Typ);
6879 end if;
6881 Rewrite_Comparison (N);
6883 -- If we still have comparison for Vax_Float, process it
6885 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6886 Expand_Vax_Comparison (N);
6887 return;
6888 end if;
6890 -- For all cases other than elementary types, we rewrite node as the
6891 -- negation of an equality operation, and reanalyze. The equality to be
6892 -- used is defined in the same scope and has the same signature. This
6893 -- signature must be set explicitly since in an instance it may not have
6894 -- the same visibility as in the generic unit. This avoids duplicating
6895 -- or factoring the complex code for record/array equality tests etc.
6897 else
6898 declare
6899 Loc : constant Source_Ptr := Sloc (N);
6900 Neg : Node_Id;
6901 Ne : constant Entity_Id := Entity (N);
6903 begin
6904 Binary_Op_Validity_Checks (N);
6906 Neg :=
6907 Make_Op_Not (Loc,
6908 Right_Opnd =>
6909 Make_Op_Eq (Loc,
6910 Left_Opnd => Left_Opnd (N),
6911 Right_Opnd => Right_Opnd (N)));
6912 Set_Paren_Count (Right_Opnd (Neg), 1);
6914 if Scope (Ne) /= Standard_Standard then
6915 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6916 end if;
6918 -- For navigation purposes, the inequality is treated as an
6919 -- implicit reference to the corresponding equality. Preserve the
6920 -- Comes_From_ source flag so that the proper Xref entry is
6921 -- generated.
6923 Preserve_Comes_From_Source (Neg, N);
6924 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6925 Rewrite (N, Neg);
6926 Analyze_And_Resolve (N, Standard_Boolean);
6927 end;
6928 end if;
6929 end Expand_N_Op_Ne;
6931 ---------------------
6932 -- Expand_N_Op_Not --
6933 ---------------------
6935 -- If the argument is other than a Boolean array type, there is no special
6936 -- expansion required, except for VMS operations on signed integers.
6938 -- For the packed case, we call the special routine in Exp_Pakd, except
6939 -- that if the component size is greater than one, we use the standard
6940 -- routine generating a gruesome loop (it is so peculiar to have packed
6941 -- arrays with non-standard Boolean representations anyway, so it does not
6942 -- matter that we do not handle this case efficiently).
6944 -- For the unpacked case (and for the special packed case where we have non
6945 -- standard Booleans, as discussed above), we generate and insert into the
6946 -- tree the following function definition:
6948 -- function Nnnn (A : arr) is
6949 -- B : arr;
6950 -- begin
6951 -- for J in a'range loop
6952 -- B (J) := not A (J);
6953 -- end loop;
6954 -- return B;
6955 -- end Nnnn;
6957 -- Here arr is the actual subtype of the parameter (and hence always
6958 -- constrained). Then we replace the not with a call to this function.
6960 procedure Expand_N_Op_Not (N : Node_Id) is
6961 Loc : constant Source_Ptr := Sloc (N);
6962 Typ : constant Entity_Id := Etype (N);
6963 Opnd : Node_Id;
6964 Arr : Entity_Id;
6965 A : Entity_Id;
6966 B : Entity_Id;
6967 J : Entity_Id;
6968 A_J : Node_Id;
6969 B_J : Node_Id;
6971 Func_Name : Entity_Id;
6972 Loop_Statement : Node_Id;
6974 begin
6975 Unary_Op_Validity_Checks (N);
6977 -- For boolean operand, deal with non-standard booleans
6979 if Is_Boolean_Type (Typ) then
6980 Adjust_Condition (Right_Opnd (N));
6981 Set_Etype (N, Standard_Boolean);
6982 Adjust_Result_Type (N, Typ);
6983 return;
6984 end if;
6986 -- For the VMS "not" on signed integer types, use conversion to and from
6987 -- a predefined modular type.
6989 if Is_VMS_Operator (Entity (N)) then
6990 declare
6991 Rtyp : Entity_Id;
6992 Utyp : Entity_Id;
6994 begin
6995 -- If this is a derived type, retrieve original VMS type so that
6996 -- the proper sized type is used for intermediate values.
6998 if Is_Derived_Type (Typ) then
6999 Rtyp := First_Subtype (Etype (Typ));
7000 else
7001 Rtyp := Typ;
7002 end if;
7004 -- The proper unsigned type must have a size compatible with the
7005 -- operand, to prevent misalignment.
7007 if RM_Size (Rtyp) <= 8 then
7008 Utyp := RTE (RE_Unsigned_8);
7010 elsif RM_Size (Rtyp) <= 16 then
7011 Utyp := RTE (RE_Unsigned_16);
7013 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
7014 Utyp := RTE (RE_Unsigned_32);
7016 else
7017 Utyp := RTE (RE_Long_Long_Unsigned);
7018 end if;
7020 Rewrite (N,
7021 Unchecked_Convert_To (Typ,
7022 Make_Op_Not (Loc,
7023 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
7024 Analyze_And_Resolve (N, Typ);
7025 return;
7026 end;
7027 end if;
7029 -- Only array types need any other processing
7031 if not Is_Array_Type (Typ) then
7032 return;
7033 end if;
7035 -- Case of array operand. If bit packed with a component size of 1,
7036 -- handle it in Exp_Pakd if the operand is known to be aligned.
7038 if Is_Bit_Packed_Array (Typ)
7039 and then Component_Size (Typ) = 1
7040 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
7041 then
7042 Expand_Packed_Not (N);
7043 return;
7044 end if;
7046 -- Case of array operand which is not bit-packed. If the context is
7047 -- a safe assignment, call in-place operation, If context is a larger
7048 -- boolean expression in the context of a safe assignment, expansion is
7049 -- done by enclosing operation.
7051 Opnd := Relocate_Node (Right_Opnd (N));
7052 Convert_To_Actual_Subtype (Opnd);
7053 Arr := Etype (Opnd);
7054 Ensure_Defined (Arr, N);
7055 Silly_Boolean_Array_Not_Test (N, Arr);
7057 if Nkind (Parent (N)) = N_Assignment_Statement then
7058 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
7059 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7060 return;
7062 -- Special case the negation of a binary operation
7064 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
7065 and then Safe_In_Place_Array_Op
7066 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
7067 then
7068 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7069 return;
7070 end if;
7072 elsif Nkind (Parent (N)) in N_Binary_Op
7073 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
7074 then
7075 declare
7076 Op1 : constant Node_Id := Left_Opnd (Parent (N));
7077 Op2 : constant Node_Id := Right_Opnd (Parent (N));
7078 Lhs : constant Node_Id := Name (Parent (Parent (N)));
7080 begin
7081 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
7083 -- (not A) op (not B) can be reduced to a single call
7085 if N = Op1 and then Nkind (Op2) = N_Op_Not then
7086 return;
7088 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
7089 return;
7091 -- A xor (not B) can also be special-cased
7093 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
7094 return;
7095 end if;
7096 end if;
7097 end;
7098 end if;
7100 A := Make_Defining_Identifier (Loc, Name_uA);
7101 B := Make_Defining_Identifier (Loc, Name_uB);
7102 J := Make_Defining_Identifier (Loc, Name_uJ);
7104 A_J :=
7105 Make_Indexed_Component (Loc,
7106 Prefix => New_Reference_To (A, Loc),
7107 Expressions => New_List (New_Reference_To (J, Loc)));
7109 B_J :=
7110 Make_Indexed_Component (Loc,
7111 Prefix => New_Reference_To (B, Loc),
7112 Expressions => New_List (New_Reference_To (J, Loc)));
7114 Loop_Statement :=
7115 Make_Implicit_Loop_Statement (N,
7116 Identifier => Empty,
7118 Iteration_Scheme =>
7119 Make_Iteration_Scheme (Loc,
7120 Loop_Parameter_Specification =>
7121 Make_Loop_Parameter_Specification (Loc,
7122 Defining_Identifier => J,
7123 Discrete_Subtype_Definition =>
7124 Make_Attribute_Reference (Loc,
7125 Prefix => Make_Identifier (Loc, Chars (A)),
7126 Attribute_Name => Name_Range))),
7128 Statements => New_List (
7129 Make_Assignment_Statement (Loc,
7130 Name => B_J,
7131 Expression => Make_Op_Not (Loc, A_J))));
7133 Func_Name := Make_Temporary (Loc, 'N');
7134 Set_Is_Inlined (Func_Name);
7136 Insert_Action (N,
7137 Make_Subprogram_Body (Loc,
7138 Specification =>
7139 Make_Function_Specification (Loc,
7140 Defining_Unit_Name => Func_Name,
7141 Parameter_Specifications => New_List (
7142 Make_Parameter_Specification (Loc,
7143 Defining_Identifier => A,
7144 Parameter_Type => New_Reference_To (Typ, Loc))),
7145 Result_Definition => New_Reference_To (Typ, Loc)),
7147 Declarations => New_List (
7148 Make_Object_Declaration (Loc,
7149 Defining_Identifier => B,
7150 Object_Definition => New_Reference_To (Arr, Loc))),
7152 Handled_Statement_Sequence =>
7153 Make_Handled_Sequence_Of_Statements (Loc,
7154 Statements => New_List (
7155 Loop_Statement,
7156 Make_Simple_Return_Statement (Loc,
7157 Expression => Make_Identifier (Loc, Chars (B)))))));
7159 Rewrite (N,
7160 Make_Function_Call (Loc,
7161 Name => New_Reference_To (Func_Name, Loc),
7162 Parameter_Associations => New_List (Opnd)));
7164 Analyze_And_Resolve (N, Typ);
7165 end Expand_N_Op_Not;
7167 --------------------
7168 -- Expand_N_Op_Or --
7169 --------------------
7171 procedure Expand_N_Op_Or (N : Node_Id) is
7172 Typ : constant Entity_Id := Etype (N);
7174 begin
7175 Binary_Op_Validity_Checks (N);
7177 if Is_Array_Type (Etype (N)) then
7178 Expand_Boolean_Operator (N);
7180 elsif Is_Boolean_Type (Etype (N)) then
7182 -- Replace OR by OR ELSE if Short_Circuit_And_Or active and the type
7183 -- is standard Boolean (do not mess with AND that uses a non-standard
7184 -- Boolean type, because something strange is going on).
7186 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
7187 Rewrite (N,
7188 Make_Or_Else (Sloc (N),
7189 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7190 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7191 Analyze_And_Resolve (N, Typ);
7193 -- Otherwise, adjust conditions
7195 else
7196 Adjust_Condition (Left_Opnd (N));
7197 Adjust_Condition (Right_Opnd (N));
7198 Set_Etype (N, Standard_Boolean);
7199 Adjust_Result_Type (N, Typ);
7200 end if;
7202 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7203 Expand_Intrinsic_Call (N, Entity (N));
7205 end if;
7206 end Expand_N_Op_Or;
7208 ----------------------
7209 -- Expand_N_Op_Plus --
7210 ----------------------
7212 procedure Expand_N_Op_Plus (N : Node_Id) is
7213 begin
7214 Unary_Op_Validity_Checks (N);
7215 end Expand_N_Op_Plus;
7217 ---------------------
7218 -- Expand_N_Op_Rem --
7219 ---------------------
7221 procedure Expand_N_Op_Rem (N : Node_Id) is
7222 Loc : constant Source_Ptr := Sloc (N);
7223 Typ : constant Entity_Id := Etype (N);
7225 Left : constant Node_Id := Left_Opnd (N);
7226 Right : constant Node_Id := Right_Opnd (N);
7228 Lo : Uint;
7229 Hi : Uint;
7230 OK : Boolean;
7232 Lneg : Boolean;
7233 Rneg : Boolean;
7234 -- Set if corresponding operand can be negative
7236 pragma Unreferenced (Hi);
7238 begin
7239 Binary_Op_Validity_Checks (N);
7241 if Is_Integer_Type (Etype (N)) then
7242 Apply_Divide_Check (N);
7243 end if;
7245 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
7246 -- but it is useful with other back ends (e.g. AAMP), and is certainly
7247 -- harmless.
7249 if Is_Integer_Type (Etype (N))
7250 and then Compile_Time_Known_Value (Right)
7251 and then Expr_Value (Right) = Uint_1
7252 then
7253 -- Call Remove_Side_Effects to ensure that any side effects in the
7254 -- ignored left operand (in particular function calls to user defined
7255 -- functions) are properly executed.
7257 Remove_Side_Effects (Left);
7259 Rewrite (N, Make_Integer_Literal (Loc, 0));
7260 Analyze_And_Resolve (N, Typ);
7261 return;
7262 end if;
7264 -- Deal with annoying case of largest negative number remainder minus
7265 -- one. Gigi does not handle this case correctly, because it generates
7266 -- a divide instruction which may trap in this case.
7268 -- In fact the check is quite easy, if the right operand is -1, then
7269 -- the remainder is always 0, and we can just ignore the left operand
7270 -- completely in this case.
7272 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
7273 Lneg := (not OK) or else Lo < 0;
7275 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
7276 Rneg := (not OK) or else Lo < 0;
7278 -- We won't mess with trying to find out if the left operand can really
7279 -- be the largest negative number (that's a pain in the case of private
7280 -- types and this is really marginal). We will just assume that we need
7281 -- the test if the left operand can be negative at all.
7283 if Lneg and Rneg then
7284 Rewrite (N,
7285 Make_Conditional_Expression (Loc,
7286 Expressions => New_List (
7287 Make_Op_Eq (Loc,
7288 Left_Opnd => Duplicate_Subexpr (Right),
7289 Right_Opnd =>
7290 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
7292 Unchecked_Convert_To (Typ,
7293 Make_Integer_Literal (Loc, Uint_0)),
7295 Relocate_Node (N))));
7297 Set_Analyzed (Next (Next (First (Expressions (N)))));
7298 Analyze_And_Resolve (N, Typ);
7299 end if;
7300 end Expand_N_Op_Rem;
7302 -----------------------------
7303 -- Expand_N_Op_Rotate_Left --
7304 -----------------------------
7306 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
7307 begin
7308 Binary_Op_Validity_Checks (N);
7309 end Expand_N_Op_Rotate_Left;
7311 ------------------------------
7312 -- Expand_N_Op_Rotate_Right --
7313 ------------------------------
7315 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
7316 begin
7317 Binary_Op_Validity_Checks (N);
7318 end Expand_N_Op_Rotate_Right;
7320 ----------------------------
7321 -- Expand_N_Op_Shift_Left --
7322 ----------------------------
7324 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
7325 begin
7326 Binary_Op_Validity_Checks (N);
7327 end Expand_N_Op_Shift_Left;
7329 -----------------------------
7330 -- Expand_N_Op_Shift_Right --
7331 -----------------------------
7333 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
7334 begin
7335 Binary_Op_Validity_Checks (N);
7336 end Expand_N_Op_Shift_Right;
7338 ----------------------------------------
7339 -- Expand_N_Op_Shift_Right_Arithmetic --
7340 ----------------------------------------
7342 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
7343 begin
7344 Binary_Op_Validity_Checks (N);
7345 end Expand_N_Op_Shift_Right_Arithmetic;
7347 --------------------------
7348 -- Expand_N_Op_Subtract --
7349 --------------------------
7351 procedure Expand_N_Op_Subtract (N : Node_Id) is
7352 Typ : constant Entity_Id := Etype (N);
7354 begin
7355 Binary_Op_Validity_Checks (N);
7357 -- N - 0 = N for integer types
7359 if Is_Integer_Type (Typ)
7360 and then Compile_Time_Known_Value (Right_Opnd (N))
7361 and then Expr_Value (Right_Opnd (N)) = 0
7362 then
7363 Rewrite (N, Left_Opnd (N));
7364 return;
7365 end if;
7367 -- Arithmetic overflow checks for signed integer/fixed point types
7369 if Is_Signed_Integer_Type (Typ)
7370 or else
7371 Is_Fixed_Point_Type (Typ)
7372 then
7373 Apply_Arithmetic_Overflow_Check (N);
7375 -- VAX floating-point types case
7377 elsif Vax_Float (Typ) then
7378 Expand_Vax_Arith (N);
7379 end if;
7380 end Expand_N_Op_Subtract;
7382 ---------------------
7383 -- Expand_N_Op_Xor --
7384 ---------------------
7386 procedure Expand_N_Op_Xor (N : Node_Id) is
7387 Typ : constant Entity_Id := Etype (N);
7389 begin
7390 Binary_Op_Validity_Checks (N);
7392 if Is_Array_Type (Etype (N)) then
7393 Expand_Boolean_Operator (N);
7395 elsif Is_Boolean_Type (Etype (N)) then
7396 Adjust_Condition (Left_Opnd (N));
7397 Adjust_Condition (Right_Opnd (N));
7398 Set_Etype (N, Standard_Boolean);
7399 Adjust_Result_Type (N, Typ);
7401 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7402 Expand_Intrinsic_Call (N, Entity (N));
7404 end if;
7405 end Expand_N_Op_Xor;
7407 ----------------------
7408 -- Expand_N_Or_Else --
7409 ----------------------
7411 procedure Expand_N_Or_Else (N : Node_Id)
7412 renames Expand_Short_Circuit_Operator;
7414 -----------------------------------
7415 -- Expand_N_Qualified_Expression --
7416 -----------------------------------
7418 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7419 Operand : constant Node_Id := Expression (N);
7420 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7422 begin
7423 -- Do validity check if validity checking operands
7425 if Validity_Checks_On
7426 and then Validity_Check_Operands
7427 then
7428 Ensure_Valid (Operand);
7429 end if;
7431 -- Apply possible constraint check
7433 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7435 if Do_Range_Check (Operand) then
7436 Set_Do_Range_Check (Operand, False);
7437 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7438 end if;
7439 end Expand_N_Qualified_Expression;
7441 ------------------------------------
7442 -- Expand_N_Quantified_Expression --
7443 ------------------------------------
7445 -- We expand:
7447 -- for all X in range => Cond
7449 -- into:
7451 -- T := True;
7452 -- for X in range loop
7453 -- if not Cond then
7454 -- T := False;
7455 -- exit;
7456 -- end if;
7457 -- end loop;
7459 -- Conversely, an existentially quantified expression:
7461 -- for some X in range => Cond
7463 -- becomes:
7465 -- T := False;
7466 -- for X in range loop
7467 -- if Cond then
7468 -- T := True;
7469 -- exit;
7470 -- end if;
7471 -- end loop;
7473 -- In both cases, the iteration may be over a container in which case it is
7474 -- given by an iterator specification, not a loop parameter specification.
7476 procedure Expand_N_Quantified_Expression (N : Node_Id) is
7477 Loc : constant Source_Ptr := Sloc (N);
7478 Is_Universal : constant Boolean := All_Present (N);
7479 Actions : constant List_Id := New_List;
7480 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7481 Cond : Node_Id;
7482 Decl : Node_Id;
7483 I_Scheme : Node_Id;
7484 Test : Node_Id;
7486 begin
7487 Decl :=
7488 Make_Object_Declaration (Loc,
7489 Defining_Identifier => Tnn,
7490 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7491 Expression =>
7492 New_Occurrence_Of (Boolean_Literals (Is_Universal), Loc));
7493 Append_To (Actions, Decl);
7495 Cond := Relocate_Node (Condition (N));
7497 if Is_Universal then
7498 Cond := Make_Op_Not (Loc, Cond);
7499 end if;
7501 Test :=
7502 Make_Implicit_If_Statement (N,
7503 Condition => Cond,
7504 Then_Statements => New_List (
7505 Make_Assignment_Statement (Loc,
7506 Name => New_Occurrence_Of (Tnn, Loc),
7507 Expression =>
7508 New_Occurrence_Of (Boolean_Literals (not Is_Universal), Loc)),
7509 Make_Exit_Statement (Loc)));
7511 if Present (Loop_Parameter_Specification (N)) then
7512 I_Scheme :=
7513 Make_Iteration_Scheme (Loc,
7514 Loop_Parameter_Specification =>
7515 Loop_Parameter_Specification (N));
7516 else
7517 I_Scheme :=
7518 Make_Iteration_Scheme (Loc,
7519 Iterator_Specification => Iterator_Specification (N));
7520 end if;
7522 Append_To (Actions,
7523 Make_Loop_Statement (Loc,
7524 Iteration_Scheme => I_Scheme,
7525 Statements => New_List (Test),
7526 End_Label => Empty));
7528 -- The components of the scheme have already been analyzed, and the loop
7529 -- parameter declaration has been processed.
7531 Set_Analyzed (Iteration_Scheme (Last (Actions)));
7533 Rewrite (N,
7534 Make_Expression_With_Actions (Loc,
7535 Expression => New_Occurrence_Of (Tnn, Loc),
7536 Actions => Actions));
7538 Analyze_And_Resolve (N, Standard_Boolean);
7539 end Expand_N_Quantified_Expression;
7541 ---------------------------------
7542 -- Expand_N_Selected_Component --
7543 ---------------------------------
7545 -- If the selector is a discriminant of a concurrent object, rewrite the
7546 -- prefix to denote the corresponding record type.
7548 procedure Expand_N_Selected_Component (N : Node_Id) is
7549 Loc : constant Source_Ptr := Sloc (N);
7550 Par : constant Node_Id := Parent (N);
7551 P : constant Node_Id := Prefix (N);
7552 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7553 Disc : Entity_Id;
7554 New_N : Node_Id;
7555 Dcon : Elmt_Id;
7556 Dval : Node_Id;
7558 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7559 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7560 -- unless the context of an assignment can provide size information.
7561 -- Don't we have a general routine that does this???
7563 -----------------------
7564 -- In_Left_Hand_Side --
7565 -----------------------
7567 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7568 begin
7569 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7570 and then Comp = Name (Parent (Comp)))
7571 or else (Present (Parent (Comp))
7572 and then Nkind (Parent (Comp)) in N_Subexpr
7573 and then In_Left_Hand_Side (Parent (Comp)));
7574 end In_Left_Hand_Side;
7576 -- Start of processing for Expand_N_Selected_Component
7578 begin
7579 -- Insert explicit dereference if required
7581 if Is_Access_Type (Ptyp) then
7582 Insert_Explicit_Dereference (P);
7583 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7585 if Ekind (Etype (P)) = E_Private_Subtype
7586 and then Is_For_Access_Subtype (Etype (P))
7587 then
7588 Set_Etype (P, Base_Type (Etype (P)));
7589 end if;
7591 Ptyp := Etype (P);
7592 end if;
7594 -- Deal with discriminant check required
7596 if Do_Discriminant_Check (N) then
7598 -- Present the discriminant checking function to the backend, so that
7599 -- it can inline the call to the function.
7601 Add_Inlined_Body
7602 (Discriminant_Checking_Func
7603 (Original_Record_Component (Entity (Selector_Name (N)))));
7605 -- Now reset the flag and generate the call
7607 Set_Do_Discriminant_Check (N, False);
7608 Generate_Discriminant_Check (N);
7609 end if;
7611 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7612 -- function, then additional actuals must be passed.
7614 if Ada_Version >= Ada_2005
7615 and then Is_Build_In_Place_Function_Call (P)
7616 then
7617 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7618 end if;
7620 -- Gigi cannot handle unchecked conversions that are the prefix of a
7621 -- selected component with discriminants. This must be checked during
7622 -- expansion, because during analysis the type of the selector is not
7623 -- known at the point the prefix is analyzed. If the conversion is the
7624 -- target of an assignment, then we cannot force the evaluation.
7626 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7627 and then Has_Discriminants (Etype (N))
7628 and then not In_Left_Hand_Side (N)
7629 then
7630 Force_Evaluation (Prefix (N));
7631 end if;
7633 -- Remaining processing applies only if selector is a discriminant
7635 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7637 -- If the selector is a discriminant of a constrained record type,
7638 -- we may be able to rewrite the expression with the actual value
7639 -- of the discriminant, a useful optimization in some cases.
7641 if Is_Record_Type (Ptyp)
7642 and then Has_Discriminants (Ptyp)
7643 and then Is_Constrained (Ptyp)
7644 then
7645 -- Do this optimization for discrete types only, and not for
7646 -- access types (access discriminants get us into trouble!)
7648 if not Is_Discrete_Type (Etype (N)) then
7649 null;
7651 -- Don't do this on the left hand of an assignment statement.
7652 -- Normally one would think that references like this would not
7653 -- occur, but they do in generated code, and mean that we really
7654 -- do want to assign the discriminant!
7656 elsif Nkind (Par) = N_Assignment_Statement
7657 and then Name (Par) = N
7658 then
7659 null;
7661 -- Don't do this optimization for the prefix of an attribute or
7662 -- the name of an object renaming declaration since these are
7663 -- contexts where we do not want the value anyway.
7665 elsif (Nkind (Par) = N_Attribute_Reference
7666 and then Prefix (Par) = N)
7667 or else Is_Renamed_Object (N)
7668 then
7669 null;
7671 -- Don't do this optimization if we are within the code for a
7672 -- discriminant check, since the whole point of such a check may
7673 -- be to verify the condition on which the code below depends!
7675 elsif Is_In_Discriminant_Check (N) then
7676 null;
7678 -- Green light to see if we can do the optimization. There is
7679 -- still one condition that inhibits the optimization below but
7680 -- now is the time to check the particular discriminant.
7682 else
7683 -- Loop through discriminants to find the matching discriminant
7684 -- constraint to see if we can copy it.
7686 Disc := First_Discriminant (Ptyp);
7687 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7688 Discr_Loop : while Present (Dcon) loop
7689 Dval := Node (Dcon);
7691 -- Check if this is the matching discriminant
7693 if Disc = Entity (Selector_Name (N)) then
7695 -- Here we have the matching discriminant. Check for
7696 -- the case of a discriminant of a component that is
7697 -- constrained by an outer discriminant, which cannot
7698 -- be optimized away.
7700 if Denotes_Discriminant
7701 (Dval, Check_Concurrent => True)
7702 then
7703 exit Discr_Loop;
7705 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
7706 and then
7707 Denotes_Discriminant
7708 (Selector_Name (Original_Node (Dval)), True)
7709 then
7710 exit Discr_Loop;
7712 -- Do not retrieve value if constraint is not static. It
7713 -- is generally not useful, and the constraint may be a
7714 -- rewritten outer discriminant in which case it is in
7715 -- fact incorrect.
7717 elsif Is_Entity_Name (Dval)
7718 and then Nkind (Parent (Entity (Dval)))
7719 = N_Object_Declaration
7720 and then Present (Expression (Parent (Entity (Dval))))
7721 and then
7722 not Is_Static_Expression
7723 (Expression (Parent (Entity (Dval))))
7724 then
7725 exit Discr_Loop;
7727 -- In the context of a case statement, the expression may
7728 -- have the base type of the discriminant, and we need to
7729 -- preserve the constraint to avoid spurious errors on
7730 -- missing cases.
7732 elsif Nkind (Parent (N)) = N_Case_Statement
7733 and then Etype (Dval) /= Etype (Disc)
7734 then
7735 Rewrite (N,
7736 Make_Qualified_Expression (Loc,
7737 Subtype_Mark =>
7738 New_Occurrence_Of (Etype (Disc), Loc),
7739 Expression =>
7740 New_Copy_Tree (Dval)));
7741 Analyze_And_Resolve (N, Etype (Disc));
7743 -- In case that comes out as a static expression,
7744 -- reset it (a selected component is never static).
7746 Set_Is_Static_Expression (N, False);
7747 return;
7749 -- Otherwise we can just copy the constraint, but the
7750 -- result is certainly not static! In some cases the
7751 -- discriminant constraint has been analyzed in the
7752 -- context of the original subtype indication, but for
7753 -- itypes the constraint might not have been analyzed
7754 -- yet, and this must be done now.
7756 else
7757 Rewrite (N, New_Copy_Tree (Dval));
7758 Analyze_And_Resolve (N);
7759 Set_Is_Static_Expression (N, False);
7760 return;
7761 end if;
7762 end if;
7764 Next_Elmt (Dcon);
7765 Next_Discriminant (Disc);
7766 end loop Discr_Loop;
7768 -- Note: the above loop should always find a matching
7769 -- discriminant, but if it does not, we just missed an
7770 -- optimization due to some glitch (perhaps a previous error),
7771 -- so ignore.
7773 end if;
7774 end if;
7776 -- The only remaining processing is in the case of a discriminant of
7777 -- a concurrent object, where we rewrite the prefix to denote the
7778 -- corresponding record type. If the type is derived and has renamed
7779 -- discriminants, use corresponding discriminant, which is the one
7780 -- that appears in the corresponding record.
7782 if not Is_Concurrent_Type (Ptyp) then
7783 return;
7784 end if;
7786 Disc := Entity (Selector_Name (N));
7788 if Is_Derived_Type (Ptyp)
7789 and then Present (Corresponding_Discriminant (Disc))
7790 then
7791 Disc := Corresponding_Discriminant (Disc);
7792 end if;
7794 New_N :=
7795 Make_Selected_Component (Loc,
7796 Prefix =>
7797 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7798 New_Copy_Tree (P)),
7799 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7801 Rewrite (N, New_N);
7802 Analyze (N);
7803 end if;
7804 end Expand_N_Selected_Component;
7806 --------------------
7807 -- Expand_N_Slice --
7808 --------------------
7810 procedure Expand_N_Slice (N : Node_Id) is
7811 Loc : constant Source_Ptr := Sloc (N);
7812 Typ : constant Entity_Id := Etype (N);
7813 Pfx : constant Node_Id := Prefix (N);
7814 Ptp : Entity_Id := Etype (Pfx);
7816 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7817 -- Check whether the argument is an actual for a procedure call, in
7818 -- which case the expansion of a bit-packed slice is deferred until the
7819 -- call itself is expanded. The reason this is required is that we might
7820 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7821 -- that copy out would be missed if we created a temporary here in
7822 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7823 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7824 -- is harmless to defer expansion in the IN case, since the call
7825 -- processing will still generate the appropriate copy in operation,
7826 -- which will take care of the slice.
7828 procedure Make_Temporary_For_Slice;
7829 -- Create a named variable for the value of the slice, in cases where
7830 -- the back-end cannot handle it properly, e.g. when packed types or
7831 -- unaligned slices are involved.
7833 -------------------------
7834 -- Is_Procedure_Actual --
7835 -------------------------
7837 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7838 Par : Node_Id := Parent (N);
7840 begin
7841 loop
7842 -- If our parent is a procedure call we can return
7844 if Nkind (Par) = N_Procedure_Call_Statement then
7845 return True;
7847 -- If our parent is a type conversion, keep climbing the tree,
7848 -- since a type conversion can be a procedure actual. Also keep
7849 -- climbing if parameter association or a qualified expression,
7850 -- since these are additional cases that do can appear on
7851 -- procedure actuals.
7853 elsif Nkind_In (Par, N_Type_Conversion,
7854 N_Parameter_Association,
7855 N_Qualified_Expression)
7856 then
7857 Par := Parent (Par);
7859 -- Any other case is not what we are looking for
7861 else
7862 return False;
7863 end if;
7864 end loop;
7865 end Is_Procedure_Actual;
7867 ------------------------------
7868 -- Make_Temporary_For_Slice --
7869 ------------------------------
7871 procedure Make_Temporary_For_Slice is
7872 Decl : Node_Id;
7873 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7875 begin
7876 Decl :=
7877 Make_Object_Declaration (Loc,
7878 Defining_Identifier => Ent,
7879 Object_Definition => New_Occurrence_Of (Typ, Loc));
7881 Set_No_Initialization (Decl);
7883 Insert_Actions (N, New_List (
7884 Decl,
7885 Make_Assignment_Statement (Loc,
7886 Name => New_Occurrence_Of (Ent, Loc),
7887 Expression => Relocate_Node (N))));
7889 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7890 Analyze_And_Resolve (N, Typ);
7891 end Make_Temporary_For_Slice;
7893 -- Start of processing for Expand_N_Slice
7895 begin
7896 -- Special handling for access types
7898 if Is_Access_Type (Ptp) then
7900 Ptp := Designated_Type (Ptp);
7902 Rewrite (Pfx,
7903 Make_Explicit_Dereference (Sloc (N),
7904 Prefix => Relocate_Node (Pfx)));
7906 Analyze_And_Resolve (Pfx, Ptp);
7907 end if;
7909 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7910 -- function, then additional actuals must be passed.
7912 if Ada_Version >= Ada_2005
7913 and then Is_Build_In_Place_Function_Call (Pfx)
7914 then
7915 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7916 end if;
7918 -- The remaining case to be handled is packed slices. We can leave
7919 -- packed slices as they are in the following situations:
7921 -- 1. Right or left side of an assignment (we can handle this
7922 -- situation correctly in the assignment statement expansion).
7924 -- 2. Prefix of indexed component (the slide is optimized away in this
7925 -- case, see the start of Expand_N_Slice.)
7927 -- 3. Object renaming declaration, since we want the name of the
7928 -- slice, not the value.
7930 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7931 -- be required, and this is handled in the expansion of call
7932 -- itself.
7934 -- 5. Prefix of an address attribute (this is an error which is caught
7935 -- elsewhere, and the expansion would interfere with generating the
7936 -- error message).
7938 if not Is_Packed (Typ) then
7940 -- Apply transformation for actuals of a function call, where
7941 -- Expand_Actuals is not used.
7943 if Nkind (Parent (N)) = N_Function_Call
7944 and then Is_Possibly_Unaligned_Slice (N)
7945 then
7946 Make_Temporary_For_Slice;
7947 end if;
7949 elsif Nkind (Parent (N)) = N_Assignment_Statement
7950 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7951 and then Parent (N) = Name (Parent (Parent (N))))
7952 then
7953 return;
7955 elsif Nkind (Parent (N)) = N_Indexed_Component
7956 or else Is_Renamed_Object (N)
7957 or else Is_Procedure_Actual (N)
7958 then
7959 return;
7961 elsif Nkind (Parent (N)) = N_Attribute_Reference
7962 and then Attribute_Name (Parent (N)) = Name_Address
7963 then
7964 return;
7966 else
7967 Make_Temporary_For_Slice;
7968 end if;
7969 end Expand_N_Slice;
7971 ------------------------------
7972 -- Expand_N_Type_Conversion --
7973 ------------------------------
7975 procedure Expand_N_Type_Conversion (N : Node_Id) is
7976 Loc : constant Source_Ptr := Sloc (N);
7977 Operand : constant Node_Id := Expression (N);
7978 Target_Type : constant Entity_Id := Etype (N);
7979 Operand_Type : Entity_Id := Etype (Operand);
7981 procedure Handle_Changed_Representation;
7982 -- This is called in the case of record and array type conversions to
7983 -- see if there is a change of representation to be handled. Change of
7984 -- representation is actually handled at the assignment statement level,
7985 -- and what this procedure does is rewrite node N conversion as an
7986 -- assignment to temporary. If there is no change of representation,
7987 -- then the conversion node is unchanged.
7989 procedure Raise_Accessibility_Error;
7990 -- Called when we know that an accessibility check will fail. Rewrites
7991 -- node N to an appropriate raise statement and outputs warning msgs.
7992 -- The Etype of the raise node is set to Target_Type.
7994 procedure Real_Range_Check;
7995 -- Handles generation of range check for real target value
7997 -----------------------------------
7998 -- Handle_Changed_Representation --
7999 -----------------------------------
8001 procedure Handle_Changed_Representation is
8002 Temp : Entity_Id;
8003 Decl : Node_Id;
8004 Odef : Node_Id;
8005 Disc : Node_Id;
8006 N_Ix : Node_Id;
8007 Cons : List_Id;
8009 begin
8010 -- Nothing else to do if no change of representation
8012 if Same_Representation (Operand_Type, Target_Type) then
8013 return;
8015 -- The real change of representation work is done by the assignment
8016 -- statement processing. So if this type conversion is appearing as
8017 -- the expression of an assignment statement, nothing needs to be
8018 -- done to the conversion.
8020 elsif Nkind (Parent (N)) = N_Assignment_Statement then
8021 return;
8023 -- Otherwise we need to generate a temporary variable, and do the
8024 -- change of representation assignment into that temporary variable.
8025 -- The conversion is then replaced by a reference to this variable.
8027 else
8028 Cons := No_List;
8030 -- If type is unconstrained we have to add a constraint, copied
8031 -- from the actual value of the left hand side.
8033 if not Is_Constrained (Target_Type) then
8034 if Has_Discriminants (Operand_Type) then
8035 Disc := First_Discriminant (Operand_Type);
8037 if Disc /= First_Stored_Discriminant (Operand_Type) then
8038 Disc := First_Stored_Discriminant (Operand_Type);
8039 end if;
8041 Cons := New_List;
8042 while Present (Disc) loop
8043 Append_To (Cons,
8044 Make_Selected_Component (Loc,
8045 Prefix =>
8046 Duplicate_Subexpr_Move_Checks (Operand),
8047 Selector_Name =>
8048 Make_Identifier (Loc, Chars (Disc))));
8049 Next_Discriminant (Disc);
8050 end loop;
8052 elsif Is_Array_Type (Operand_Type) then
8053 N_Ix := First_Index (Target_Type);
8054 Cons := New_List;
8056 for J in 1 .. Number_Dimensions (Operand_Type) loop
8058 -- We convert the bounds explicitly. We use an unchecked
8059 -- conversion because bounds checks are done elsewhere.
8061 Append_To (Cons,
8062 Make_Range (Loc,
8063 Low_Bound =>
8064 Unchecked_Convert_To (Etype (N_Ix),
8065 Make_Attribute_Reference (Loc,
8066 Prefix =>
8067 Duplicate_Subexpr_No_Checks
8068 (Operand, Name_Req => True),
8069 Attribute_Name => Name_First,
8070 Expressions => New_List (
8071 Make_Integer_Literal (Loc, J)))),
8073 High_Bound =>
8074 Unchecked_Convert_To (Etype (N_Ix),
8075 Make_Attribute_Reference (Loc,
8076 Prefix =>
8077 Duplicate_Subexpr_No_Checks
8078 (Operand, Name_Req => True),
8079 Attribute_Name => Name_Last,
8080 Expressions => New_List (
8081 Make_Integer_Literal (Loc, J))))));
8083 Next_Index (N_Ix);
8084 end loop;
8085 end if;
8086 end if;
8088 Odef := New_Occurrence_Of (Target_Type, Loc);
8090 if Present (Cons) then
8091 Odef :=
8092 Make_Subtype_Indication (Loc,
8093 Subtype_Mark => Odef,
8094 Constraint =>
8095 Make_Index_Or_Discriminant_Constraint (Loc,
8096 Constraints => Cons));
8097 end if;
8099 Temp := Make_Temporary (Loc, 'C');
8100 Decl :=
8101 Make_Object_Declaration (Loc,
8102 Defining_Identifier => Temp,
8103 Object_Definition => Odef);
8105 Set_No_Initialization (Decl, True);
8107 -- Insert required actions. It is essential to suppress checks
8108 -- since we have suppressed default initialization, which means
8109 -- that the variable we create may have no discriminants.
8111 Insert_Actions (N,
8112 New_List (
8113 Decl,
8114 Make_Assignment_Statement (Loc,
8115 Name => New_Occurrence_Of (Temp, Loc),
8116 Expression => Relocate_Node (N))),
8117 Suppress => All_Checks);
8119 Rewrite (N, New_Occurrence_Of (Temp, Loc));
8120 return;
8121 end if;
8122 end Handle_Changed_Representation;
8124 -------------------------------
8125 -- Raise_Accessibility_Error --
8126 -------------------------------
8128 procedure Raise_Accessibility_Error is
8129 begin
8130 Rewrite (N,
8131 Make_Raise_Program_Error (Sloc (N),
8132 Reason => PE_Accessibility_Check_Failed));
8133 Set_Etype (N, Target_Type);
8135 Error_Msg_N ("?accessibility check failure", N);
8136 Error_Msg_NE
8137 ("\?& will be raised at run time", N, Standard_Program_Error);
8138 end Raise_Accessibility_Error;
8140 ----------------------
8141 -- Real_Range_Check --
8142 ----------------------
8144 -- Case of conversions to floating-point or fixed-point. If range checks
8145 -- are enabled and the target type has a range constraint, we convert:
8147 -- typ (x)
8149 -- to
8151 -- Tnn : typ'Base := typ'Base (x);
8152 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
8153 -- Tnn
8155 -- This is necessary when there is a conversion of integer to float or
8156 -- to fixed-point to ensure that the correct checks are made. It is not
8157 -- necessary for float to float where it is enough to simply set the
8158 -- Do_Range_Check flag.
8160 procedure Real_Range_Check is
8161 Btyp : constant Entity_Id := Base_Type (Target_Type);
8162 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
8163 Hi : constant Node_Id := Type_High_Bound (Target_Type);
8164 Xtyp : constant Entity_Id := Etype (Operand);
8165 Conv : Node_Id;
8166 Tnn : Entity_Id;
8168 begin
8169 -- Nothing to do if conversion was rewritten
8171 if Nkind (N) /= N_Type_Conversion then
8172 return;
8173 end if;
8175 -- Nothing to do if range checks suppressed, or target has the same
8176 -- range as the base type (or is the base type).
8178 if Range_Checks_Suppressed (Target_Type)
8179 or else (Lo = Type_Low_Bound (Btyp)
8180 and then
8181 Hi = Type_High_Bound (Btyp))
8182 then
8183 return;
8184 end if;
8186 -- Nothing to do if expression is an entity on which checks have been
8187 -- suppressed.
8189 if Is_Entity_Name (Operand)
8190 and then Range_Checks_Suppressed (Entity (Operand))
8191 then
8192 return;
8193 end if;
8195 -- Nothing to do if bounds are all static and we can tell that the
8196 -- expression is within the bounds of the target. Note that if the
8197 -- operand is of an unconstrained floating-point type, then we do
8198 -- not trust it to be in range (might be infinite)
8200 declare
8201 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
8202 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
8204 begin
8205 if (not Is_Floating_Point_Type (Xtyp)
8206 or else Is_Constrained (Xtyp))
8207 and then Compile_Time_Known_Value (S_Lo)
8208 and then Compile_Time_Known_Value (S_Hi)
8209 and then Compile_Time_Known_Value (Hi)
8210 and then Compile_Time_Known_Value (Lo)
8211 then
8212 declare
8213 D_Lov : constant Ureal := Expr_Value_R (Lo);
8214 D_Hiv : constant Ureal := Expr_Value_R (Hi);
8215 S_Lov : Ureal;
8216 S_Hiv : Ureal;
8218 begin
8219 if Is_Real_Type (Xtyp) then
8220 S_Lov := Expr_Value_R (S_Lo);
8221 S_Hiv := Expr_Value_R (S_Hi);
8222 else
8223 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
8224 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
8225 end if;
8227 if D_Hiv > D_Lov
8228 and then S_Lov >= D_Lov
8229 and then S_Hiv <= D_Hiv
8230 then
8231 Set_Do_Range_Check (Operand, False);
8232 return;
8233 end if;
8234 end;
8235 end if;
8236 end;
8238 -- For float to float conversions, we are done
8240 if Is_Floating_Point_Type (Xtyp)
8241 and then
8242 Is_Floating_Point_Type (Btyp)
8243 then
8244 return;
8245 end if;
8247 -- Otherwise rewrite the conversion as described above
8249 Conv := Relocate_Node (N);
8250 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
8251 Set_Etype (Conv, Btyp);
8253 -- Enable overflow except for case of integer to float conversions,
8254 -- where it is never required, since we can never have overflow in
8255 -- this case.
8257 if not Is_Integer_Type (Etype (Operand)) then
8258 Enable_Overflow_Check (Conv);
8259 end if;
8261 Tnn := Make_Temporary (Loc, 'T', Conv);
8263 Insert_Actions (N, New_List (
8264 Make_Object_Declaration (Loc,
8265 Defining_Identifier => Tnn,
8266 Object_Definition => New_Occurrence_Of (Btyp, Loc),
8267 Constant_Present => True,
8268 Expression => Conv),
8270 Make_Raise_Constraint_Error (Loc,
8271 Condition =>
8272 Make_Or_Else (Loc,
8273 Left_Opnd =>
8274 Make_Op_Lt (Loc,
8275 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8276 Right_Opnd =>
8277 Make_Attribute_Reference (Loc,
8278 Attribute_Name => Name_First,
8279 Prefix =>
8280 New_Occurrence_Of (Target_Type, Loc))),
8282 Right_Opnd =>
8283 Make_Op_Gt (Loc,
8284 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8285 Right_Opnd =>
8286 Make_Attribute_Reference (Loc,
8287 Attribute_Name => Name_Last,
8288 Prefix =>
8289 New_Occurrence_Of (Target_Type, Loc)))),
8290 Reason => CE_Range_Check_Failed)));
8292 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
8293 Analyze_And_Resolve (N, Btyp);
8294 end Real_Range_Check;
8296 -- Start of processing for Expand_N_Type_Conversion
8298 begin
8299 -- Nothing at all to do if conversion is to the identical type so remove
8300 -- the conversion completely, it is useless, except that it may carry
8301 -- an Assignment_OK attribute, which must be propagated to the operand.
8303 if Operand_Type = Target_Type then
8304 if Assignment_OK (N) then
8305 Set_Assignment_OK (Operand);
8306 end if;
8308 Rewrite (N, Relocate_Node (Operand));
8309 goto Done;
8310 end if;
8312 -- Nothing to do if this is the second argument of read. This is a
8313 -- "backwards" conversion that will be handled by the specialized code
8314 -- in attribute processing.
8316 if Nkind (Parent (N)) = N_Attribute_Reference
8317 and then Attribute_Name (Parent (N)) = Name_Read
8318 and then Next (First (Expressions (Parent (N)))) = N
8319 then
8320 goto Done;
8321 end if;
8323 -- Check for case of converting to a type that has an invariant
8324 -- associated with it. This required an invariant check. We convert
8326 -- typ (expr)
8328 -- into
8330 -- do invariant_check (typ (expr)) in typ (expr);
8332 -- using Duplicate_Subexpr to avoid multiple side effects
8334 -- Note: the Comes_From_Source check, and then the resetting of this
8335 -- flag prevents what would otherwise be an infinite recursion.
8337 if Has_Invariants (Target_Type)
8338 and then Present (Invariant_Procedure (Target_Type))
8339 and then Comes_From_Source (N)
8340 then
8341 Set_Comes_From_Source (N, False);
8342 Rewrite (N,
8343 Make_Expression_With_Actions (Loc,
8344 Actions => New_List (
8345 Make_Invariant_Call (Duplicate_Subexpr (N))),
8346 Expression => Duplicate_Subexpr_No_Checks (N)));
8347 Analyze_And_Resolve (N, Target_Type);
8348 goto Done;
8349 end if;
8351 -- Here if we may need to expand conversion
8353 -- If the operand of the type conversion is an arithmetic operation on
8354 -- signed integers, and the based type of the signed integer type in
8355 -- question is smaller than Standard.Integer, we promote both of the
8356 -- operands to type Integer.
8358 -- For example, if we have
8360 -- target-type (opnd1 + opnd2)
8362 -- and opnd1 and opnd2 are of type short integer, then we rewrite
8363 -- this as:
8365 -- target-type (integer(opnd1) + integer(opnd2))
8367 -- We do this because we are always allowed to compute in a larger type
8368 -- if we do the right thing with the result, and in this case we are
8369 -- going to do a conversion which will do an appropriate check to make
8370 -- sure that things are in range of the target type in any case. This
8371 -- avoids some unnecessary intermediate overflows.
8373 -- We might consider a similar transformation in the case where the
8374 -- target is a real type or a 64-bit integer type, and the operand
8375 -- is an arithmetic operation using a 32-bit integer type. However,
8376 -- we do not bother with this case, because it could cause significant
8377 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
8378 -- much cheaper, but we don't want different behavior on 32-bit and
8379 -- 64-bit machines. Note that the exclusion of the 64-bit case also
8380 -- handles the configurable run-time cases where 64-bit arithmetic
8381 -- may simply be unavailable.
8383 -- Note: this circuit is partially redundant with respect to the circuit
8384 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
8385 -- the processing here. Also we still need the Checks circuit, since we
8386 -- have to be sure not to generate junk overflow checks in the first
8387 -- place, since it would be trick to remove them here!
8389 if Integer_Promotion_Possible (N) then
8391 -- All conditions met, go ahead with transformation
8393 declare
8394 Opnd : Node_Id;
8395 L, R : Node_Id;
8397 begin
8398 R :=
8399 Make_Type_Conversion (Loc,
8400 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8401 Expression => Relocate_Node (Right_Opnd (Operand)));
8403 Opnd := New_Op_Node (Nkind (Operand), Loc);
8404 Set_Right_Opnd (Opnd, R);
8406 if Nkind (Operand) in N_Binary_Op then
8407 L :=
8408 Make_Type_Conversion (Loc,
8409 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8410 Expression => Relocate_Node (Left_Opnd (Operand)));
8412 Set_Left_Opnd (Opnd, L);
8413 end if;
8415 Rewrite (N,
8416 Make_Type_Conversion (Loc,
8417 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
8418 Expression => Opnd));
8420 Analyze_And_Resolve (N, Target_Type);
8421 goto Done;
8422 end;
8423 end if;
8425 -- Do validity check if validity checking operands
8427 if Validity_Checks_On
8428 and then Validity_Check_Operands
8429 then
8430 Ensure_Valid (Operand);
8431 end if;
8433 -- Special case of converting from non-standard boolean type
8435 if Is_Boolean_Type (Operand_Type)
8436 and then (Nonzero_Is_True (Operand_Type))
8437 then
8438 Adjust_Condition (Operand);
8439 Set_Etype (Operand, Standard_Boolean);
8440 Operand_Type := Standard_Boolean;
8441 end if;
8443 -- Case of converting to an access type
8445 if Is_Access_Type (Target_Type) then
8447 -- Apply an accessibility check when the conversion operand is an
8448 -- access parameter (or a renaming thereof), unless conversion was
8449 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
8450 -- Note that other checks may still need to be applied below (such
8451 -- as tagged type checks).
8453 if Is_Entity_Name (Operand)
8454 and then
8455 (Is_Formal (Entity (Operand))
8456 or else
8457 (Present (Renamed_Object (Entity (Operand)))
8458 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
8459 and then Is_Formal
8460 (Entity (Renamed_Object (Entity (Operand))))))
8461 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
8462 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
8463 or else Attribute_Name (Original_Node (N)) = Name_Access)
8464 then
8465 Apply_Accessibility_Check
8466 (Operand, Target_Type, Insert_Node => Operand);
8468 -- If the level of the operand type is statically deeper than the
8469 -- level of the target type, then force Program_Error. Note that this
8470 -- can only occur for cases where the attribute is within the body of
8471 -- an instantiation (otherwise the conversion will already have been
8472 -- rejected as illegal). Note: warnings are issued by the analyzer
8473 -- for the instance cases.
8475 elsif In_Instance_Body
8476 and then Type_Access_Level (Operand_Type) >
8477 Type_Access_Level (Target_Type)
8478 then
8479 Raise_Accessibility_Error;
8481 -- When the operand is a selected access discriminant the check needs
8482 -- to be made against the level of the object denoted by the prefix
8483 -- of the selected name. Force Program_Error for this case as well
8484 -- (this accessibility violation can only happen if within the body
8485 -- of an instantiation).
8487 elsif In_Instance_Body
8488 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8489 and then Nkind (Operand) = N_Selected_Component
8490 and then Object_Access_Level (Operand) >
8491 Type_Access_Level (Target_Type)
8492 then
8493 Raise_Accessibility_Error;
8494 goto Done;
8495 end if;
8496 end if;
8498 -- Case of conversions of tagged types and access to tagged types
8500 -- When needed, that is to say when the expression is class-wide, Add
8501 -- runtime a tag check for (strict) downward conversion by using the
8502 -- membership test, generating:
8504 -- [constraint_error when Operand not in Target_Type'Class]
8506 -- or in the access type case
8508 -- [constraint_error
8509 -- when Operand /= null
8510 -- and then Operand.all not in
8511 -- Designated_Type (Target_Type)'Class]
8513 if (Is_Access_Type (Target_Type)
8514 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8515 or else Is_Tagged_Type (Target_Type)
8516 then
8517 -- Do not do any expansion in the access type case if the parent is a
8518 -- renaming, since this is an error situation which will be caught by
8519 -- Sem_Ch8, and the expansion can interfere with this error check.
8521 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
8522 goto Done;
8523 end if;
8525 -- Otherwise, proceed with processing tagged conversion
8527 Tagged_Conversion : declare
8528 Actual_Op_Typ : Entity_Id;
8529 Actual_Targ_Typ : Entity_Id;
8530 Make_Conversion : Boolean := False;
8531 Root_Op_Typ : Entity_Id;
8533 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
8534 -- Create a membership check to test whether Operand is a member
8535 -- of Targ_Typ. If the original Target_Type is an access, include
8536 -- a test for null value. The check is inserted at N.
8538 --------------------
8539 -- Make_Tag_Check --
8540 --------------------
8542 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
8543 Cond : Node_Id;
8545 begin
8546 -- Generate:
8547 -- [Constraint_Error
8548 -- when Operand /= null
8549 -- and then Operand.all not in Targ_Typ]
8551 if Is_Access_Type (Target_Type) then
8552 Cond :=
8553 Make_And_Then (Loc,
8554 Left_Opnd =>
8555 Make_Op_Ne (Loc,
8556 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8557 Right_Opnd => Make_Null (Loc)),
8559 Right_Opnd =>
8560 Make_Not_In (Loc,
8561 Left_Opnd =>
8562 Make_Explicit_Dereference (Loc,
8563 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
8564 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
8566 -- Generate:
8567 -- [Constraint_Error when Operand not in Targ_Typ]
8569 else
8570 Cond :=
8571 Make_Not_In (Loc,
8572 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8573 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
8574 end if;
8576 Insert_Action (N,
8577 Make_Raise_Constraint_Error (Loc,
8578 Condition => Cond,
8579 Reason => CE_Tag_Check_Failed));
8580 end Make_Tag_Check;
8582 -- Start of processing for Tagged_Conversion
8584 begin
8585 if Is_Access_Type (Target_Type) then
8587 -- Handle entities from the limited view
8589 Actual_Op_Typ :=
8590 Available_View (Designated_Type (Operand_Type));
8591 Actual_Targ_Typ :=
8592 Available_View (Designated_Type (Target_Type));
8593 else
8594 Actual_Op_Typ := Operand_Type;
8595 Actual_Targ_Typ := Target_Type;
8596 end if;
8598 Root_Op_Typ := Root_Type (Actual_Op_Typ);
8600 -- Ada 2005 (AI-251): Handle interface type conversion
8602 if Is_Interface (Actual_Op_Typ) then
8603 Expand_Interface_Conversion (N, Is_Static => False);
8604 goto Done;
8605 end if;
8607 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
8609 -- Create a runtime tag check for a downward class-wide type
8610 -- conversion.
8612 if Is_Class_Wide_Type (Actual_Op_Typ)
8613 and then Actual_Op_Typ /= Actual_Targ_Typ
8614 and then Root_Op_Typ /= Actual_Targ_Typ
8615 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
8616 then
8617 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
8618 Make_Conversion := True;
8619 end if;
8621 -- AI05-0073: If the result subtype of the function is defined
8622 -- by an access_definition designating a specific tagged type
8623 -- T, a check is made that the result value is null or the tag
8624 -- of the object designated by the result value identifies T.
8625 -- Constraint_Error is raised if this check fails.
8627 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
8628 declare
8629 Func : Entity_Id;
8630 Func_Typ : Entity_Id;
8632 begin
8633 -- Climb scope stack looking for the enclosing function
8635 Func := Current_Scope;
8636 while Present (Func)
8637 and then Ekind (Func) /= E_Function
8638 loop
8639 Func := Scope (Func);
8640 end loop;
8642 -- The function's return subtype must be defined using
8643 -- an access definition.
8645 if Nkind (Result_Definition (Parent (Func))) =
8646 N_Access_Definition
8647 then
8648 Func_Typ := Directly_Designated_Type (Etype (Func));
8650 -- The return subtype denotes a specific tagged type,
8651 -- in other words, a non class-wide type.
8653 if Is_Tagged_Type (Func_Typ)
8654 and then not Is_Class_Wide_Type (Func_Typ)
8655 then
8656 Make_Tag_Check (Actual_Targ_Typ);
8657 Make_Conversion := True;
8658 end if;
8659 end if;
8660 end;
8661 end if;
8663 -- We have generated a tag check for either a class-wide type
8664 -- conversion or for AI05-0073.
8666 if Make_Conversion then
8667 declare
8668 Conv : Node_Id;
8669 begin
8670 Conv :=
8671 Make_Unchecked_Type_Conversion (Loc,
8672 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8673 Expression => Relocate_Node (Expression (N)));
8674 Rewrite (N, Conv);
8675 Analyze_And_Resolve (N, Target_Type);
8676 end;
8677 end if;
8678 end if;
8679 end Tagged_Conversion;
8681 -- Case of other access type conversions
8683 elsif Is_Access_Type (Target_Type) then
8684 Apply_Constraint_Check (Operand, Target_Type);
8686 -- Case of conversions from a fixed-point type
8688 -- These conversions require special expansion and processing, found in
8689 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8690 -- since from a semantic point of view, these are simple integer
8691 -- conversions, which do not need further processing.
8693 elsif Is_Fixed_Point_Type (Operand_Type)
8694 and then not Conversion_OK (N)
8695 then
8696 -- We should never see universal fixed at this case, since the
8697 -- expansion of the constituent divide or multiply should have
8698 -- eliminated the explicit mention of universal fixed.
8700 pragma Assert (Operand_Type /= Universal_Fixed);
8702 -- Check for special case of the conversion to universal real that
8703 -- occurs as a result of the use of a round attribute. In this case,
8704 -- the real type for the conversion is taken from the target type of
8705 -- the Round attribute and the result must be marked as rounded.
8707 if Target_Type = Universal_Real
8708 and then Nkind (Parent (N)) = N_Attribute_Reference
8709 and then Attribute_Name (Parent (N)) = Name_Round
8710 then
8711 Set_Rounded_Result (N);
8712 Set_Etype (N, Etype (Parent (N)));
8713 end if;
8715 -- Otherwise do correct fixed-conversion, but skip these if the
8716 -- Conversion_OK flag is set, because from a semantic point of view
8717 -- these are simple integer conversions needing no further processing
8718 -- (the backend will simply treat them as integers).
8720 if not Conversion_OK (N) then
8721 if Is_Fixed_Point_Type (Etype (N)) then
8722 Expand_Convert_Fixed_To_Fixed (N);
8723 Real_Range_Check;
8725 elsif Is_Integer_Type (Etype (N)) then
8726 Expand_Convert_Fixed_To_Integer (N);
8728 else
8729 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8730 Expand_Convert_Fixed_To_Float (N);
8731 Real_Range_Check;
8732 end if;
8733 end if;
8735 -- Case of conversions to a fixed-point type
8737 -- These conversions require special expansion and processing, found in
8738 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8739 -- since from a semantic point of view, these are simple integer
8740 -- conversions, which do not need further processing.
8742 elsif Is_Fixed_Point_Type (Target_Type)
8743 and then not Conversion_OK (N)
8744 then
8745 if Is_Integer_Type (Operand_Type) then
8746 Expand_Convert_Integer_To_Fixed (N);
8747 Real_Range_Check;
8748 else
8749 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8750 Expand_Convert_Float_To_Fixed (N);
8751 Real_Range_Check;
8752 end if;
8754 -- Case of float-to-integer conversions
8756 -- We also handle float-to-fixed conversions with Conversion_OK set
8757 -- since semantically the fixed-point target is treated as though it
8758 -- were an integer in such cases.
8760 elsif Is_Floating_Point_Type (Operand_Type)
8761 and then
8762 (Is_Integer_Type (Target_Type)
8763 or else
8764 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8765 then
8766 -- One more check here, gcc is still not able to do conversions of
8767 -- this type with proper overflow checking, and so gigi is doing an
8768 -- approximation of what is required by doing floating-point compares
8769 -- with the end-point. But that can lose precision in some cases, and
8770 -- give a wrong result. Converting the operand to Universal_Real is
8771 -- helpful, but still does not catch all cases with 64-bit integers
8772 -- on targets with only 64-bit floats.
8774 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8775 -- Can this code be removed ???
8777 if Do_Range_Check (Operand) then
8778 Rewrite (Operand,
8779 Make_Type_Conversion (Loc,
8780 Subtype_Mark =>
8781 New_Occurrence_Of (Universal_Real, Loc),
8782 Expression =>
8783 Relocate_Node (Operand)));
8785 Set_Etype (Operand, Universal_Real);
8786 Enable_Range_Check (Operand);
8787 Set_Do_Range_Check (Expression (Operand), False);
8788 end if;
8790 -- Case of array conversions
8792 -- Expansion of array conversions, add required length/range checks but
8793 -- only do this if there is no change of representation. For handling of
8794 -- this case, see Handle_Changed_Representation.
8796 elsif Is_Array_Type (Target_Type) then
8797 if Is_Constrained (Target_Type) then
8798 Apply_Length_Check (Operand, Target_Type);
8799 else
8800 Apply_Range_Check (Operand, Target_Type);
8801 end if;
8803 Handle_Changed_Representation;
8805 -- Case of conversions of discriminated types
8807 -- Add required discriminant checks if target is constrained. Again this
8808 -- change is skipped if we have a change of representation.
8810 elsif Has_Discriminants (Target_Type)
8811 and then Is_Constrained (Target_Type)
8812 then
8813 Apply_Discriminant_Check (Operand, Target_Type);
8814 Handle_Changed_Representation;
8816 -- Case of all other record conversions. The only processing required
8817 -- is to check for a change of representation requiring the special
8818 -- assignment processing.
8820 elsif Is_Record_Type (Target_Type) then
8822 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8823 -- a derived Unchecked_Union type to an unconstrained type that is
8824 -- not Unchecked_Union if the operand lacks inferable discriminants.
8826 if Is_Derived_Type (Operand_Type)
8827 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8828 and then not Is_Constrained (Target_Type)
8829 and then not Is_Unchecked_Union (Base_Type (Target_Type))
8830 and then not Has_Inferable_Discriminants (Operand)
8831 then
8832 -- To prevent Gigi from generating illegal code, we generate a
8833 -- Program_Error node, but we give it the target type of the
8834 -- conversion.
8836 declare
8837 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
8838 Reason => PE_Unchecked_Union_Restriction);
8840 begin
8841 Set_Etype (PE, Target_Type);
8842 Rewrite (N, PE);
8844 end;
8845 else
8846 Handle_Changed_Representation;
8847 end if;
8849 -- Case of conversions of enumeration types
8851 elsif Is_Enumeration_Type (Target_Type) then
8853 -- Special processing is required if there is a change of
8854 -- representation (from enumeration representation clauses).
8856 if not Same_Representation (Target_Type, Operand_Type) then
8858 -- Convert: x(y) to x'val (ytyp'val (y))
8860 Rewrite (N,
8861 Make_Attribute_Reference (Loc,
8862 Prefix => New_Occurrence_Of (Target_Type, Loc),
8863 Attribute_Name => Name_Val,
8864 Expressions => New_List (
8865 Make_Attribute_Reference (Loc,
8866 Prefix => New_Occurrence_Of (Operand_Type, Loc),
8867 Attribute_Name => Name_Pos,
8868 Expressions => New_List (Operand)))));
8870 Analyze_And_Resolve (N, Target_Type);
8871 end if;
8873 -- Case of conversions to floating-point
8875 elsif Is_Floating_Point_Type (Target_Type) then
8876 Real_Range_Check;
8877 end if;
8879 -- At this stage, either the conversion node has been transformed into
8880 -- some other equivalent expression, or left as a conversion that can be
8881 -- handled by Gigi, in the following cases:
8883 -- Conversions with no change of representation or type
8885 -- Numeric conversions involving integer, floating- and fixed-point
8886 -- values. Fixed-point values are allowed only if Conversion_OK is
8887 -- set, i.e. if the fixed-point values are to be treated as integers.
8889 -- No other conversions should be passed to Gigi
8891 -- Check: are these rules stated in sinfo??? if so, why restate here???
8893 -- The only remaining step is to generate a range check if we still have
8894 -- a type conversion at this stage and Do_Range_Check is set. For now we
8895 -- do this only for conversions of discrete types.
8897 if Nkind (N) = N_Type_Conversion
8898 and then Is_Discrete_Type (Etype (N))
8899 then
8900 declare
8901 Expr : constant Node_Id := Expression (N);
8902 Ftyp : Entity_Id;
8903 Ityp : Entity_Id;
8905 begin
8906 if Do_Range_Check (Expr)
8907 and then Is_Discrete_Type (Etype (Expr))
8908 then
8909 Set_Do_Range_Check (Expr, False);
8911 -- Before we do a range check, we have to deal with treating a
8912 -- fixed-point operand as an integer. The way we do this is
8913 -- simply to do an unchecked conversion to an appropriate
8914 -- integer type large enough to hold the result.
8916 -- This code is not active yet, because we are only dealing
8917 -- with discrete types so far ???
8919 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8920 and then Treat_Fixed_As_Integer (Expr)
8921 then
8922 Ftyp := Base_Type (Etype (Expr));
8924 if Esize (Ftyp) >= Esize (Standard_Integer) then
8925 Ityp := Standard_Long_Long_Integer;
8926 else
8927 Ityp := Standard_Integer;
8928 end if;
8930 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8931 end if;
8933 -- Reset overflow flag, since the range check will include
8934 -- dealing with possible overflow, and generate the check. If
8935 -- Address is either a source type or target type, suppress
8936 -- range check to avoid typing anomalies when it is a visible
8937 -- integer type.
8939 Set_Do_Overflow_Check (N, False);
8940 if not Is_Descendent_Of_Address (Etype (Expr))
8941 and then not Is_Descendent_Of_Address (Target_Type)
8942 then
8943 Generate_Range_Check
8944 (Expr, Target_Type, CE_Range_Check_Failed);
8945 end if;
8946 end if;
8947 end;
8948 end if;
8950 -- Final step, if the result is a type conversion involving Vax_Float
8951 -- types, then it is subject for further special processing.
8953 if Nkind (N) = N_Type_Conversion
8954 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8955 then
8956 Expand_Vax_Conversion (N);
8957 goto Done;
8958 end if;
8960 -- Here at end of processing
8962 <<Done>>
8963 -- Apply predicate check if required. Note that we can't just call
8964 -- Apply_Predicate_Check here, because the type looks right after
8965 -- the conversion and it would omit the check. The Comes_From_Source
8966 -- guard is necessary to prevent infinite recursions when we generate
8967 -- internal conversions for the purpose of checking predicates.
8969 if Present (Predicate_Function (Target_Type))
8970 and then Target_Type /= Operand_Type
8971 and then Comes_From_Source (N)
8972 then
8973 Insert_Action (N,
8974 Make_Predicate_Check (Target_Type, Duplicate_Subexpr (N)));
8975 end if;
8976 end Expand_N_Type_Conversion;
8978 -----------------------------------
8979 -- Expand_N_Unchecked_Expression --
8980 -----------------------------------
8982 -- Remove the unchecked expression node from the tree. Its job was simply
8983 -- to make sure that its constituent expression was handled with checks
8984 -- off, and now that that is done, we can remove it from the tree, and
8985 -- indeed must, since Gigi does not expect to see these nodes.
8987 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8988 Exp : constant Node_Id := Expression (N);
8989 begin
8990 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
8991 Rewrite (N, Exp);
8992 end Expand_N_Unchecked_Expression;
8994 ----------------------------------------
8995 -- Expand_N_Unchecked_Type_Conversion --
8996 ----------------------------------------
8998 -- If this cannot be handled by Gigi and we haven't already made a
8999 -- temporary for it, do it now.
9001 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
9002 Target_Type : constant Entity_Id := Etype (N);
9003 Operand : constant Node_Id := Expression (N);
9004 Operand_Type : constant Entity_Id := Etype (Operand);
9006 begin
9007 -- Nothing at all to do if conversion is to the identical type so remove
9008 -- the conversion completely, it is useless, except that it may carry
9009 -- an Assignment_OK indication which must be propagated to the operand.
9011 if Operand_Type = Target_Type then
9013 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
9015 if Assignment_OK (N) then
9016 Set_Assignment_OK (Operand);
9017 end if;
9019 Rewrite (N, Relocate_Node (Operand));
9020 return;
9021 end if;
9023 -- If we have a conversion of a compile time known value to a target
9024 -- type and the value is in range of the target type, then we can simply
9025 -- replace the construct by an integer literal of the correct type. We
9026 -- only apply this to integer types being converted. Possibly it may
9027 -- apply in other cases, but it is too much trouble to worry about.
9029 -- Note that we do not do this transformation if the Kill_Range_Check
9030 -- flag is set, since then the value may be outside the expected range.
9031 -- This happens in the Normalize_Scalars case.
9033 -- We also skip this if either the target or operand type is biased
9034 -- because in this case, the unchecked conversion is supposed to
9035 -- preserve the bit pattern, not the integer value.
9037 if Is_Integer_Type (Target_Type)
9038 and then not Has_Biased_Representation (Target_Type)
9039 and then Is_Integer_Type (Operand_Type)
9040 and then not Has_Biased_Representation (Operand_Type)
9041 and then Compile_Time_Known_Value (Operand)
9042 and then not Kill_Range_Check (N)
9043 then
9044 declare
9045 Val : constant Uint := Expr_Value (Operand);
9047 begin
9048 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
9049 and then
9050 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
9051 and then
9052 Val >= Expr_Value (Type_Low_Bound (Target_Type))
9053 and then
9054 Val <= Expr_Value (Type_High_Bound (Target_Type))
9055 then
9056 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
9058 -- If Address is the target type, just set the type to avoid a
9059 -- spurious type error on the literal when Address is a visible
9060 -- integer type.
9062 if Is_Descendent_Of_Address (Target_Type) then
9063 Set_Etype (N, Target_Type);
9064 else
9065 Analyze_And_Resolve (N, Target_Type);
9066 end if;
9068 return;
9069 end if;
9070 end;
9071 end if;
9073 -- Nothing to do if conversion is safe
9075 if Safe_Unchecked_Type_Conversion (N) then
9076 return;
9077 end if;
9079 -- Otherwise force evaluation unless Assignment_OK flag is set (this
9080 -- flag indicates ??? -- more comments needed here)
9082 if Assignment_OK (N) then
9083 null;
9084 else
9085 Force_Evaluation (N);
9086 end if;
9087 end Expand_N_Unchecked_Type_Conversion;
9089 ----------------------------
9090 -- Expand_Record_Equality --
9091 ----------------------------
9093 -- For non-variant records, Equality is expanded when needed into:
9095 -- and then Lhs.Discr1 = Rhs.Discr1
9096 -- and then ...
9097 -- and then Lhs.Discrn = Rhs.Discrn
9098 -- and then Lhs.Cmp1 = Rhs.Cmp1
9099 -- and then ...
9100 -- and then Lhs.Cmpn = Rhs.Cmpn
9102 -- The expression is folded by the back-end for adjacent fields. This
9103 -- function is called for tagged record in only one occasion: for imple-
9104 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
9105 -- otherwise the primitive "=" is used directly.
9107 function Expand_Record_Equality
9108 (Nod : Node_Id;
9109 Typ : Entity_Id;
9110 Lhs : Node_Id;
9111 Rhs : Node_Id;
9112 Bodies : List_Id) return Node_Id
9114 Loc : constant Source_Ptr := Sloc (Nod);
9116 Result : Node_Id;
9117 C : Entity_Id;
9119 First_Time : Boolean := True;
9121 function Suitable_Element (C : Entity_Id) return Entity_Id;
9122 -- Return the first field to compare beginning with C, skipping the
9123 -- inherited components.
9125 ----------------------
9126 -- Suitable_Element --
9127 ----------------------
9129 function Suitable_Element (C : Entity_Id) return Entity_Id is
9130 begin
9131 if No (C) then
9132 return Empty;
9134 elsif Ekind (C) /= E_Discriminant
9135 and then Ekind (C) /= E_Component
9136 then
9137 return Suitable_Element (Next_Entity (C));
9139 elsif Is_Tagged_Type (Typ)
9140 and then C /= Original_Record_Component (C)
9141 then
9142 return Suitable_Element (Next_Entity (C));
9144 elsif Chars (C) = Name_uController
9145 or else Chars (C) = Name_uTag
9146 then
9147 return Suitable_Element (Next_Entity (C));
9149 elsif Is_Interface (Etype (C)) then
9150 return Suitable_Element (Next_Entity (C));
9152 else
9153 return C;
9154 end if;
9155 end Suitable_Element;
9157 -- Start of processing for Expand_Record_Equality
9159 begin
9160 -- Generates the following code: (assuming that Typ has one Discr and
9161 -- component C2 is also a record)
9163 -- True
9164 -- and then Lhs.Discr1 = Rhs.Discr1
9165 -- and then Lhs.C1 = Rhs.C1
9166 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
9167 -- and then ...
9168 -- and then Lhs.Cmpn = Rhs.Cmpn
9170 Result := New_Reference_To (Standard_True, Loc);
9171 C := Suitable_Element (First_Entity (Typ));
9172 while Present (C) loop
9173 declare
9174 New_Lhs : Node_Id;
9175 New_Rhs : Node_Id;
9176 Check : Node_Id;
9178 begin
9179 if First_Time then
9180 First_Time := False;
9181 New_Lhs := Lhs;
9182 New_Rhs := Rhs;
9183 else
9184 New_Lhs := New_Copy_Tree (Lhs);
9185 New_Rhs := New_Copy_Tree (Rhs);
9186 end if;
9188 Check :=
9189 Expand_Composite_Equality (Nod, Etype (C),
9190 Lhs =>
9191 Make_Selected_Component (Loc,
9192 Prefix => New_Lhs,
9193 Selector_Name => New_Reference_To (C, Loc)),
9194 Rhs =>
9195 Make_Selected_Component (Loc,
9196 Prefix => New_Rhs,
9197 Selector_Name => New_Reference_To (C, Loc)),
9198 Bodies => Bodies);
9200 -- If some (sub)component is an unchecked_union, the whole
9201 -- operation will raise program error.
9203 if Nkind (Check) = N_Raise_Program_Error then
9204 Result := Check;
9205 Set_Etype (Result, Standard_Boolean);
9206 exit;
9207 else
9208 Result :=
9209 Make_And_Then (Loc,
9210 Left_Opnd => Result,
9211 Right_Opnd => Check);
9212 end if;
9213 end;
9215 C := Suitable_Element (Next_Entity (C));
9216 end loop;
9218 return Result;
9219 end Expand_Record_Equality;
9221 -----------------------------------
9222 -- Expand_Short_Circuit_Operator --
9223 -----------------------------------
9225 -- Deal with special expansion if actions are present for the right operand
9226 -- and deal with optimizing case of arguments being True or False. We also
9227 -- deal with the special case of non-standard boolean values.
9229 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
9230 Loc : constant Source_Ptr := Sloc (N);
9231 Typ : constant Entity_Id := Etype (N);
9232 Left : constant Node_Id := Left_Opnd (N);
9233 Right : constant Node_Id := Right_Opnd (N);
9234 LocR : constant Source_Ptr := Sloc (Right);
9235 Actlist : List_Id;
9237 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
9238 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
9239 -- If Left = Shortcut_Value then Right need not be evaluated
9241 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
9242 -- For Opnd a boolean expression, return a Boolean expression equivalent
9243 -- to Opnd /= Shortcut_Value.
9245 --------------------
9246 -- Make_Test_Expr --
9247 --------------------
9249 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
9250 begin
9251 if Shortcut_Value then
9252 return Make_Op_Not (Sloc (Opnd), Opnd);
9253 else
9254 return Opnd;
9255 end if;
9256 end Make_Test_Expr;
9258 Op_Var : Entity_Id;
9259 -- Entity for a temporary variable holding the value of the operator,
9260 -- used for expansion in the case where actions are present.
9262 -- Start of processing for Expand_Short_Circuit_Operator
9264 begin
9265 -- Deal with non-standard booleans
9267 if Is_Boolean_Type (Typ) then
9268 Adjust_Condition (Left);
9269 Adjust_Condition (Right);
9270 Set_Etype (N, Standard_Boolean);
9271 end if;
9273 -- Check for cases where left argument is known to be True or False
9275 if Compile_Time_Known_Value (Left) then
9277 -- Mark SCO for left condition as compile time known
9279 if Generate_SCO and then Comes_From_Source (Left) then
9280 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
9281 end if;
9283 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
9284 -- Any actions associated with Right will be executed unconditionally
9285 -- and can thus be inserted into the tree unconditionally.
9287 if Expr_Value_E (Left) /= Shortcut_Ent then
9288 if Present (Actions (N)) then
9289 Insert_Actions (N, Actions (N));
9290 end if;
9292 Rewrite (N, Right);
9294 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
9295 -- In this case we can forget the actions associated with Right,
9296 -- since they will never be executed.
9298 else
9299 Kill_Dead_Code (Right);
9300 Kill_Dead_Code (Actions (N));
9301 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9302 end if;
9304 Adjust_Result_Type (N, Typ);
9305 return;
9306 end if;
9308 -- If Actions are present for the right operand, we have to do some
9309 -- special processing. We can't just let these actions filter back into
9310 -- code preceding the short circuit (which is what would have happened
9311 -- if we had not trapped them in the short-circuit form), since they
9312 -- must only be executed if the right operand of the short circuit is
9313 -- executed and not otherwise.
9315 -- the temporary variable C.
9317 if Present (Actions (N)) then
9318 Actlist := Actions (N);
9320 -- The old approach is to expand:
9322 -- left AND THEN right
9324 -- into
9326 -- C : Boolean := False;
9327 -- IF left THEN
9328 -- Actions;
9329 -- IF right THEN
9330 -- C := True;
9331 -- END IF;
9332 -- END IF;
9334 -- and finally rewrite the operator into a reference to C. Similarly
9335 -- for left OR ELSE right, with negated values. Note that this
9336 -- rewrite causes some difficulties for coverage analysis because
9337 -- of the introduction of the new variable C, which obscures the
9338 -- structure of the test.
9340 -- We use this "old approach" if use of N_Expression_With_Actions
9341 -- is False (see description in Opt of when this is or is not set).
9343 if not Use_Expression_With_Actions then
9344 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
9346 Insert_Action (N,
9347 Make_Object_Declaration (Loc,
9348 Defining_Identifier =>
9349 Op_Var,
9350 Object_Definition =>
9351 New_Occurrence_Of (Standard_Boolean, Loc),
9352 Expression =>
9353 New_Occurrence_Of (Shortcut_Ent, Loc)));
9355 Append_To (Actlist,
9356 Make_Implicit_If_Statement (Right,
9357 Condition => Make_Test_Expr (Right),
9358 Then_Statements => New_List (
9359 Make_Assignment_Statement (LocR,
9360 Name => New_Occurrence_Of (Op_Var, LocR),
9361 Expression =>
9362 New_Occurrence_Of
9363 (Boolean_Literals (not Shortcut_Value), LocR)))));
9365 Insert_Action (N,
9366 Make_Implicit_If_Statement (Left,
9367 Condition => Make_Test_Expr (Left),
9368 Then_Statements => Actlist));
9370 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
9371 Analyze_And_Resolve (N, Standard_Boolean);
9373 -- The new approach, activated for now by the use of debug flag
9374 -- -gnatd.X is to use the new Expression_With_Actions node for the
9375 -- right operand of the short-circuit form. This should solve the
9376 -- traceability problems for coverage analysis.
9378 else
9379 Rewrite (Right,
9380 Make_Expression_With_Actions (LocR,
9381 Expression => Relocate_Node (Right),
9382 Actions => Actlist));
9383 Set_Actions (N, No_List);
9384 Analyze_And_Resolve (Right, Standard_Boolean);
9385 end if;
9387 Adjust_Result_Type (N, Typ);
9388 return;
9389 end if;
9391 -- No actions present, check for cases of right argument True/False
9393 if Compile_Time_Known_Value (Right) then
9395 -- Mark SCO for left condition as compile time known
9397 if Generate_SCO and then Comes_From_Source (Right) then
9398 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
9399 end if;
9401 -- Change (Left and then True), (Left or else False) to Left.
9402 -- Note that we know there are no actions associated with the right
9403 -- operand, since we just checked for this case above.
9405 if Expr_Value_E (Right) /= Shortcut_Ent then
9406 Rewrite (N, Left);
9408 -- Change (Left and then False), (Left or else True) to Right,
9409 -- making sure to preserve any side effects associated with the Left
9410 -- operand.
9412 else
9413 Remove_Side_Effects (Left);
9414 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9415 end if;
9416 end if;
9418 Adjust_Result_Type (N, Typ);
9419 end Expand_Short_Circuit_Operator;
9421 -------------------------------------
9422 -- Fixup_Universal_Fixed_Operation --
9423 -------------------------------------
9425 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
9426 Conv : constant Node_Id := Parent (N);
9428 begin
9429 -- We must have a type conversion immediately above us
9431 pragma Assert (Nkind (Conv) = N_Type_Conversion);
9433 -- Normally the type conversion gives our target type. The exception
9434 -- occurs in the case of the Round attribute, where the conversion
9435 -- will be to universal real, and our real type comes from the Round
9436 -- attribute (as well as an indication that we must round the result)
9438 if Nkind (Parent (Conv)) = N_Attribute_Reference
9439 and then Attribute_Name (Parent (Conv)) = Name_Round
9440 then
9441 Set_Etype (N, Etype (Parent (Conv)));
9442 Set_Rounded_Result (N);
9444 -- Normal case where type comes from conversion above us
9446 else
9447 Set_Etype (N, Etype (Conv));
9448 end if;
9449 end Fixup_Universal_Fixed_Operation;
9451 ------------------------------
9452 -- Get_Allocator_Final_List --
9453 ------------------------------
9455 function Get_Allocator_Final_List
9456 (N : Node_Id;
9457 T : Entity_Id;
9458 PtrT : Entity_Id) return Entity_Id
9460 Loc : constant Source_Ptr := Sloc (N);
9462 Owner : Entity_Id := PtrT;
9463 -- The entity whose finalization list must be used to attach the
9464 -- allocated object.
9466 begin
9467 if Ekind (PtrT) = E_Anonymous_Access_Type then
9469 -- If the context is an access parameter, we need to create a
9470 -- non-anonymous access type in order to have a usable final list,
9471 -- because there is otherwise no pool to which the allocated object
9472 -- can belong. We create both the type and the finalization chain
9473 -- here, because freezing an internal type does not create such a
9474 -- chain. The Final_Chain that is thus created is shared by the
9475 -- access parameter. The access type is tested against the result
9476 -- type of the function to exclude allocators whose type is an
9477 -- anonymous access result type. We freeze the type at once to
9478 -- ensure that it is properly decorated for the back-end, even
9479 -- if the context and current scope is a loop.
9481 if Nkind (Associated_Node_For_Itype (PtrT))
9482 in N_Subprogram_Specification
9483 and then
9484 PtrT /=
9485 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
9486 then
9487 Owner := Make_Temporary (Loc, 'J');
9488 Insert_Action (N,
9489 Make_Full_Type_Declaration (Loc,
9490 Defining_Identifier => Owner,
9491 Type_Definition =>
9492 Make_Access_To_Object_Definition (Loc,
9493 Subtype_Indication =>
9494 New_Occurrence_Of (T, Loc))));
9496 Freeze_Before (N, Owner);
9497 Build_Final_List (N, Owner);
9498 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
9500 -- Ada 2005 (AI-318-02): If the context is a return object
9501 -- declaration, then the anonymous return subtype is defined to have
9502 -- the same accessibility level as that of the function's result
9503 -- subtype, which means that we want the scope where the function is
9504 -- declared.
9506 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
9507 and then Ekind (Scope (PtrT)) = E_Return_Statement
9508 then
9509 Owner := Scope (Return_Applies_To (Scope (PtrT)));
9511 -- Case of an access discriminant, or (Ada 2005) of an anonymous
9512 -- access component or anonymous access function result: find the
9513 -- final list associated with the scope of the type. (In the
9514 -- anonymous access component kind, a list controller will have
9515 -- been allocated when freezing the record type, and PtrT has an
9516 -- Associated_Final_Chain attribute designating it.)
9518 elsif No (Associated_Final_Chain (PtrT)) then
9519 Owner := Scope (PtrT);
9520 end if;
9521 end if;
9523 return Find_Final_List (Owner);
9524 end Get_Allocator_Final_List;
9526 ---------------------------------
9527 -- Has_Inferable_Discriminants --
9528 ---------------------------------
9530 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
9532 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
9533 -- Determines whether the left-most prefix of a selected component is a
9534 -- formal parameter in a subprogram. Assumes N is a selected component.
9536 --------------------------------
9537 -- Prefix_Is_Formal_Parameter --
9538 --------------------------------
9540 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
9541 Sel_Comp : Node_Id := N;
9543 begin
9544 -- Move to the left-most prefix by climbing up the tree
9546 while Present (Parent (Sel_Comp))
9547 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
9548 loop
9549 Sel_Comp := Parent (Sel_Comp);
9550 end loop;
9552 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
9553 end Prefix_Is_Formal_Parameter;
9555 -- Start of processing for Has_Inferable_Discriminants
9557 begin
9558 -- For identifiers and indexed components, it is sufficient to have a
9559 -- constrained Unchecked_Union nominal subtype.
9561 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
9562 return Is_Unchecked_Union (Base_Type (Etype (N)))
9563 and then
9564 Is_Constrained (Etype (N));
9566 -- For selected components, the subtype of the selector must be a
9567 -- constrained Unchecked_Union. If the component is subject to a
9568 -- per-object constraint, then the enclosing object must have inferable
9569 -- discriminants.
9571 elsif Nkind (N) = N_Selected_Component then
9572 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
9574 -- A small hack. If we have a per-object constrained selected
9575 -- component of a formal parameter, return True since we do not
9576 -- know the actual parameter association yet.
9578 if Prefix_Is_Formal_Parameter (N) then
9579 return True;
9580 end if;
9582 -- Otherwise, check the enclosing object and the selector
9584 return Has_Inferable_Discriminants (Prefix (N))
9585 and then
9586 Has_Inferable_Discriminants (Selector_Name (N));
9587 end if;
9589 -- The call to Has_Inferable_Discriminants will determine whether
9590 -- the selector has a constrained Unchecked_Union nominal type.
9592 return Has_Inferable_Discriminants (Selector_Name (N));
9594 -- A qualified expression has inferable discriminants if its subtype
9595 -- mark is a constrained Unchecked_Union subtype.
9597 elsif Nkind (N) = N_Qualified_Expression then
9598 return Is_Unchecked_Union (Subtype_Mark (N))
9599 and then
9600 Is_Constrained (Subtype_Mark (N));
9602 end if;
9604 return False;
9605 end Has_Inferable_Discriminants;
9607 -------------------------------
9608 -- Insert_Dereference_Action --
9609 -------------------------------
9611 procedure Insert_Dereference_Action (N : Node_Id) is
9612 Loc : constant Source_Ptr := Sloc (N);
9613 Typ : constant Entity_Id := Etype (N);
9614 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
9615 Pnod : constant Node_Id := Parent (N);
9617 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
9618 -- Return true if type of P is derived from Checked_Pool;
9620 -----------------------------
9621 -- Is_Checked_Storage_Pool --
9622 -----------------------------
9624 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
9625 T : Entity_Id;
9627 begin
9628 if No (P) then
9629 return False;
9630 end if;
9632 T := Etype (P);
9633 while T /= Etype (T) loop
9634 if Is_RTE (T, RE_Checked_Pool) then
9635 return True;
9636 else
9637 T := Etype (T);
9638 end if;
9639 end loop;
9641 return False;
9642 end Is_Checked_Storage_Pool;
9644 -- Start of processing for Insert_Dereference_Action
9646 begin
9647 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
9649 if not (Is_Checked_Storage_Pool (Pool)
9650 and then Comes_From_Source (Original_Node (Pnod)))
9651 then
9652 return;
9653 end if;
9655 Insert_Action (N,
9656 Make_Procedure_Call_Statement (Loc,
9657 Name => New_Reference_To (
9658 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
9660 Parameter_Associations => New_List (
9662 -- Pool
9664 New_Reference_To (Pool, Loc),
9666 -- Storage_Address. We use the attribute Pool_Address, which uses
9667 -- the pointer itself to find the address of the object, and which
9668 -- handles unconstrained arrays properly by computing the address
9669 -- of the template. i.e. the correct address of the corresponding
9670 -- allocation.
9672 Make_Attribute_Reference (Loc,
9673 Prefix => Duplicate_Subexpr_Move_Checks (N),
9674 Attribute_Name => Name_Pool_Address),
9676 -- Size_In_Storage_Elements
9678 Make_Op_Divide (Loc,
9679 Left_Opnd =>
9680 Make_Attribute_Reference (Loc,
9681 Prefix =>
9682 Make_Explicit_Dereference (Loc,
9683 Duplicate_Subexpr_Move_Checks (N)),
9684 Attribute_Name => Name_Size),
9685 Right_Opnd =>
9686 Make_Integer_Literal (Loc, System_Storage_Unit)),
9688 -- Alignment
9690 Make_Attribute_Reference (Loc,
9691 Prefix =>
9692 Make_Explicit_Dereference (Loc,
9693 Duplicate_Subexpr_Move_Checks (N)),
9694 Attribute_Name => Name_Alignment))));
9696 exception
9697 when RE_Not_Available =>
9698 return;
9699 end Insert_Dereference_Action;
9701 --------------------------------
9702 -- Integer_Promotion_Possible --
9703 --------------------------------
9705 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
9706 Operand : constant Node_Id := Expression (N);
9707 Operand_Type : constant Entity_Id := Etype (Operand);
9708 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
9710 begin
9711 pragma Assert (Nkind (N) = N_Type_Conversion);
9713 return
9715 -- We only do the transformation for source constructs. We assume
9716 -- that the expander knows what it is doing when it generates code.
9718 Comes_From_Source (N)
9720 -- If the operand type is Short_Integer or Short_Short_Integer,
9721 -- then we will promote to Integer, which is available on all
9722 -- targets, and is sufficient to ensure no intermediate overflow.
9723 -- Furthermore it is likely to be as efficient or more efficient
9724 -- than using the smaller type for the computation so we do this
9725 -- unconditionally.
9727 and then
9728 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
9729 or else
9730 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
9732 -- Test for interesting operation, which includes addition,
9733 -- division, exponentiation, multiplication, subtraction, absolute
9734 -- value and unary negation. Unary "+" is omitted since it is a
9735 -- no-op and thus can't overflow.
9737 and then Nkind_In (Operand, N_Op_Abs,
9738 N_Op_Add,
9739 N_Op_Divide,
9740 N_Op_Expon,
9741 N_Op_Minus,
9742 N_Op_Multiply,
9743 N_Op_Subtract);
9744 end Integer_Promotion_Possible;
9746 ------------------------------
9747 -- Make_Array_Comparison_Op --
9748 ------------------------------
9750 -- This is a hand-coded expansion of the following generic function:
9752 -- generic
9753 -- type elem is (<>);
9754 -- type index is (<>);
9755 -- type a is array (index range <>) of elem;
9757 -- function Gnnn (X : a; Y: a) return boolean is
9758 -- J : index := Y'first;
9760 -- begin
9761 -- if X'length = 0 then
9762 -- return false;
9764 -- elsif Y'length = 0 then
9765 -- return true;
9767 -- else
9768 -- for I in X'range loop
9769 -- if X (I) = Y (J) then
9770 -- if J = Y'last then
9771 -- exit;
9772 -- else
9773 -- J := index'succ (J);
9774 -- end if;
9776 -- else
9777 -- return X (I) > Y (J);
9778 -- end if;
9779 -- end loop;
9781 -- return X'length > Y'length;
9782 -- end if;
9783 -- end Gnnn;
9785 -- Note that since we are essentially doing this expansion by hand, we
9786 -- do not need to generate an actual or formal generic part, just the
9787 -- instantiated function itself.
9789 function Make_Array_Comparison_Op
9790 (Typ : Entity_Id;
9791 Nod : Node_Id) return Node_Id
9793 Loc : constant Source_Ptr := Sloc (Nod);
9795 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
9796 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
9797 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
9798 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9800 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
9802 Loop_Statement : Node_Id;
9803 Loop_Body : Node_Id;
9804 If_Stat : Node_Id;
9805 Inner_If : Node_Id;
9806 Final_Expr : Node_Id;
9807 Func_Body : Node_Id;
9808 Func_Name : Entity_Id;
9809 Formals : List_Id;
9810 Length1 : Node_Id;
9811 Length2 : Node_Id;
9813 begin
9814 -- if J = Y'last then
9815 -- exit;
9816 -- else
9817 -- J := index'succ (J);
9818 -- end if;
9820 Inner_If :=
9821 Make_Implicit_If_Statement (Nod,
9822 Condition =>
9823 Make_Op_Eq (Loc,
9824 Left_Opnd => New_Reference_To (J, Loc),
9825 Right_Opnd =>
9826 Make_Attribute_Reference (Loc,
9827 Prefix => New_Reference_To (Y, Loc),
9828 Attribute_Name => Name_Last)),
9830 Then_Statements => New_List (
9831 Make_Exit_Statement (Loc)),
9833 Else_Statements =>
9834 New_List (
9835 Make_Assignment_Statement (Loc,
9836 Name => New_Reference_To (J, Loc),
9837 Expression =>
9838 Make_Attribute_Reference (Loc,
9839 Prefix => New_Reference_To (Index, Loc),
9840 Attribute_Name => Name_Succ,
9841 Expressions => New_List (New_Reference_To (J, Loc))))));
9843 -- if X (I) = Y (J) then
9844 -- if ... end if;
9845 -- else
9846 -- return X (I) > Y (J);
9847 -- end if;
9849 Loop_Body :=
9850 Make_Implicit_If_Statement (Nod,
9851 Condition =>
9852 Make_Op_Eq (Loc,
9853 Left_Opnd =>
9854 Make_Indexed_Component (Loc,
9855 Prefix => New_Reference_To (X, Loc),
9856 Expressions => New_List (New_Reference_To (I, Loc))),
9858 Right_Opnd =>
9859 Make_Indexed_Component (Loc,
9860 Prefix => New_Reference_To (Y, Loc),
9861 Expressions => New_List (New_Reference_To (J, Loc)))),
9863 Then_Statements => New_List (Inner_If),
9865 Else_Statements => New_List (
9866 Make_Simple_Return_Statement (Loc,
9867 Expression =>
9868 Make_Op_Gt (Loc,
9869 Left_Opnd =>
9870 Make_Indexed_Component (Loc,
9871 Prefix => New_Reference_To (X, Loc),
9872 Expressions => New_List (New_Reference_To (I, Loc))),
9874 Right_Opnd =>
9875 Make_Indexed_Component (Loc,
9876 Prefix => New_Reference_To (Y, Loc),
9877 Expressions => New_List (
9878 New_Reference_To (J, Loc)))))));
9880 -- for I in X'range loop
9881 -- if ... end if;
9882 -- end loop;
9884 Loop_Statement :=
9885 Make_Implicit_Loop_Statement (Nod,
9886 Identifier => Empty,
9888 Iteration_Scheme =>
9889 Make_Iteration_Scheme (Loc,
9890 Loop_Parameter_Specification =>
9891 Make_Loop_Parameter_Specification (Loc,
9892 Defining_Identifier => I,
9893 Discrete_Subtype_Definition =>
9894 Make_Attribute_Reference (Loc,
9895 Prefix => New_Reference_To (X, Loc),
9896 Attribute_Name => Name_Range))),
9898 Statements => New_List (Loop_Body));
9900 -- if X'length = 0 then
9901 -- return false;
9902 -- elsif Y'length = 0 then
9903 -- return true;
9904 -- else
9905 -- for ... loop ... end loop;
9906 -- return X'length > Y'length;
9907 -- end if;
9909 Length1 :=
9910 Make_Attribute_Reference (Loc,
9911 Prefix => New_Reference_To (X, Loc),
9912 Attribute_Name => Name_Length);
9914 Length2 :=
9915 Make_Attribute_Reference (Loc,
9916 Prefix => New_Reference_To (Y, Loc),
9917 Attribute_Name => Name_Length);
9919 Final_Expr :=
9920 Make_Op_Gt (Loc,
9921 Left_Opnd => Length1,
9922 Right_Opnd => Length2);
9924 If_Stat :=
9925 Make_Implicit_If_Statement (Nod,
9926 Condition =>
9927 Make_Op_Eq (Loc,
9928 Left_Opnd =>
9929 Make_Attribute_Reference (Loc,
9930 Prefix => New_Reference_To (X, Loc),
9931 Attribute_Name => Name_Length),
9932 Right_Opnd =>
9933 Make_Integer_Literal (Loc, 0)),
9935 Then_Statements =>
9936 New_List (
9937 Make_Simple_Return_Statement (Loc,
9938 Expression => New_Reference_To (Standard_False, Loc))),
9940 Elsif_Parts => New_List (
9941 Make_Elsif_Part (Loc,
9942 Condition =>
9943 Make_Op_Eq (Loc,
9944 Left_Opnd =>
9945 Make_Attribute_Reference (Loc,
9946 Prefix => New_Reference_To (Y, Loc),
9947 Attribute_Name => Name_Length),
9948 Right_Opnd =>
9949 Make_Integer_Literal (Loc, 0)),
9951 Then_Statements =>
9952 New_List (
9953 Make_Simple_Return_Statement (Loc,
9954 Expression => New_Reference_To (Standard_True, Loc))))),
9956 Else_Statements => New_List (
9957 Loop_Statement,
9958 Make_Simple_Return_Statement (Loc,
9959 Expression => Final_Expr)));
9961 -- (X : a; Y: a)
9963 Formals := New_List (
9964 Make_Parameter_Specification (Loc,
9965 Defining_Identifier => X,
9966 Parameter_Type => New_Reference_To (Typ, Loc)),
9968 Make_Parameter_Specification (Loc,
9969 Defining_Identifier => Y,
9970 Parameter_Type => New_Reference_To (Typ, Loc)));
9972 -- function Gnnn (...) return boolean is
9973 -- J : index := Y'first;
9974 -- begin
9975 -- if ... end if;
9976 -- end Gnnn;
9978 Func_Name := Make_Temporary (Loc, 'G');
9980 Func_Body :=
9981 Make_Subprogram_Body (Loc,
9982 Specification =>
9983 Make_Function_Specification (Loc,
9984 Defining_Unit_Name => Func_Name,
9985 Parameter_Specifications => Formals,
9986 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
9988 Declarations => New_List (
9989 Make_Object_Declaration (Loc,
9990 Defining_Identifier => J,
9991 Object_Definition => New_Reference_To (Index, Loc),
9992 Expression =>
9993 Make_Attribute_Reference (Loc,
9994 Prefix => New_Reference_To (Y, Loc),
9995 Attribute_Name => Name_First))),
9997 Handled_Statement_Sequence =>
9998 Make_Handled_Sequence_Of_Statements (Loc,
9999 Statements => New_List (If_Stat)));
10001 return Func_Body;
10002 end Make_Array_Comparison_Op;
10004 ---------------------------
10005 -- Make_Boolean_Array_Op --
10006 ---------------------------
10008 -- For logical operations on boolean arrays, expand in line the following,
10009 -- replacing 'and' with 'or' or 'xor' where needed:
10011 -- function Annn (A : typ; B: typ) return typ is
10012 -- C : typ;
10013 -- begin
10014 -- for J in A'range loop
10015 -- C (J) := A (J) op B (J);
10016 -- end loop;
10017 -- return C;
10018 -- end Annn;
10020 -- Here typ is the boolean array type
10022 function Make_Boolean_Array_Op
10023 (Typ : Entity_Id;
10024 N : Node_Id) return Node_Id
10026 Loc : constant Source_Ptr := Sloc (N);
10028 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
10029 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
10030 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
10031 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
10033 A_J : Node_Id;
10034 B_J : Node_Id;
10035 C_J : Node_Id;
10036 Op : Node_Id;
10038 Formals : List_Id;
10039 Func_Name : Entity_Id;
10040 Func_Body : Node_Id;
10041 Loop_Statement : Node_Id;
10043 begin
10044 A_J :=
10045 Make_Indexed_Component (Loc,
10046 Prefix => New_Reference_To (A, Loc),
10047 Expressions => New_List (New_Reference_To (J, Loc)));
10049 B_J :=
10050 Make_Indexed_Component (Loc,
10051 Prefix => New_Reference_To (B, Loc),
10052 Expressions => New_List (New_Reference_To (J, Loc)));
10054 C_J :=
10055 Make_Indexed_Component (Loc,
10056 Prefix => New_Reference_To (C, Loc),
10057 Expressions => New_List (New_Reference_To (J, Loc)));
10059 if Nkind (N) = N_Op_And then
10060 Op :=
10061 Make_Op_And (Loc,
10062 Left_Opnd => A_J,
10063 Right_Opnd => B_J);
10065 elsif Nkind (N) = N_Op_Or then
10066 Op :=
10067 Make_Op_Or (Loc,
10068 Left_Opnd => A_J,
10069 Right_Opnd => B_J);
10071 else
10072 Op :=
10073 Make_Op_Xor (Loc,
10074 Left_Opnd => A_J,
10075 Right_Opnd => B_J);
10076 end if;
10078 Loop_Statement :=
10079 Make_Implicit_Loop_Statement (N,
10080 Identifier => Empty,
10082 Iteration_Scheme =>
10083 Make_Iteration_Scheme (Loc,
10084 Loop_Parameter_Specification =>
10085 Make_Loop_Parameter_Specification (Loc,
10086 Defining_Identifier => J,
10087 Discrete_Subtype_Definition =>
10088 Make_Attribute_Reference (Loc,
10089 Prefix => New_Reference_To (A, Loc),
10090 Attribute_Name => Name_Range))),
10092 Statements => New_List (
10093 Make_Assignment_Statement (Loc,
10094 Name => C_J,
10095 Expression => Op)));
10097 Formals := New_List (
10098 Make_Parameter_Specification (Loc,
10099 Defining_Identifier => A,
10100 Parameter_Type => New_Reference_To (Typ, Loc)),
10102 Make_Parameter_Specification (Loc,
10103 Defining_Identifier => B,
10104 Parameter_Type => New_Reference_To (Typ, Loc)));
10106 Func_Name := Make_Temporary (Loc, 'A');
10107 Set_Is_Inlined (Func_Name);
10109 Func_Body :=
10110 Make_Subprogram_Body (Loc,
10111 Specification =>
10112 Make_Function_Specification (Loc,
10113 Defining_Unit_Name => Func_Name,
10114 Parameter_Specifications => Formals,
10115 Result_Definition => New_Reference_To (Typ, Loc)),
10117 Declarations => New_List (
10118 Make_Object_Declaration (Loc,
10119 Defining_Identifier => C,
10120 Object_Definition => New_Reference_To (Typ, Loc))),
10122 Handled_Statement_Sequence =>
10123 Make_Handled_Sequence_Of_Statements (Loc,
10124 Statements => New_List (
10125 Loop_Statement,
10126 Make_Simple_Return_Statement (Loc,
10127 Expression => New_Reference_To (C, Loc)))));
10129 return Func_Body;
10130 end Make_Boolean_Array_Op;
10132 ------------------------
10133 -- Rewrite_Comparison --
10134 ------------------------
10136 procedure Rewrite_Comparison (N : Node_Id) is
10137 Warning_Generated : Boolean := False;
10138 -- Set to True if first pass with Assume_Valid generates a warning in
10139 -- which case we skip the second pass to avoid warning overloaded.
10141 Result : Node_Id;
10142 -- Set to Standard_True or Standard_False
10144 begin
10145 if Nkind (N) = N_Type_Conversion then
10146 Rewrite_Comparison (Expression (N));
10147 return;
10149 elsif Nkind (N) not in N_Op_Compare then
10150 return;
10151 end if;
10153 -- Now start looking at the comparison in detail. We potentially go
10154 -- through this loop twice. The first time, Assume_Valid is set False
10155 -- in the call to Compile_Time_Compare. If this call results in a
10156 -- clear result of always True or Always False, that's decisive and
10157 -- we are done. Otherwise we repeat the processing with Assume_Valid
10158 -- set to True to generate additional warnings. We can skip that step
10159 -- if Constant_Condition_Warnings is False.
10161 for AV in False .. True loop
10162 declare
10163 Typ : constant Entity_Id := Etype (N);
10164 Op1 : constant Node_Id := Left_Opnd (N);
10165 Op2 : constant Node_Id := Right_Opnd (N);
10167 Res : constant Compare_Result :=
10168 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
10169 -- Res indicates if compare outcome can be compile time determined
10171 True_Result : Boolean;
10172 False_Result : Boolean;
10174 begin
10175 case N_Op_Compare (Nkind (N)) is
10176 when N_Op_Eq =>
10177 True_Result := Res = EQ;
10178 False_Result := Res = LT or else Res = GT or else Res = NE;
10180 when N_Op_Ge =>
10181 True_Result := Res in Compare_GE;
10182 False_Result := Res = LT;
10184 if Res = LE
10185 and then Constant_Condition_Warnings
10186 and then Comes_From_Source (Original_Node (N))
10187 and then Nkind (Original_Node (N)) = N_Op_Ge
10188 and then not In_Instance
10189 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10190 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10191 then
10192 Error_Msg_N
10193 ("can never be greater than, could replace by ""'=""?", N);
10194 Warning_Generated := True;
10195 end if;
10197 when N_Op_Gt =>
10198 True_Result := Res = GT;
10199 False_Result := Res in Compare_LE;
10201 when N_Op_Lt =>
10202 True_Result := Res = LT;
10203 False_Result := Res in Compare_GE;
10205 when N_Op_Le =>
10206 True_Result := Res in Compare_LE;
10207 False_Result := Res = GT;
10209 if Res = GE
10210 and then Constant_Condition_Warnings
10211 and then Comes_From_Source (Original_Node (N))
10212 and then Nkind (Original_Node (N)) = N_Op_Le
10213 and then not In_Instance
10214 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10215 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10216 then
10217 Error_Msg_N
10218 ("can never be less than, could replace by ""'=""?", N);
10219 Warning_Generated := True;
10220 end if;
10222 when N_Op_Ne =>
10223 True_Result := Res = NE or else Res = GT or else Res = LT;
10224 False_Result := Res = EQ;
10225 end case;
10227 -- If this is the first iteration, then we actually convert the
10228 -- comparison into True or False, if the result is certain.
10230 if AV = False then
10231 if True_Result or False_Result then
10232 if True_Result then
10233 Result := Standard_True;
10234 else
10235 Result := Standard_False;
10236 end if;
10238 Rewrite (N,
10239 Convert_To (Typ,
10240 New_Occurrence_Of (Result, Sloc (N))));
10241 Analyze_And_Resolve (N, Typ);
10242 Warn_On_Known_Condition (N);
10243 return;
10244 end if;
10246 -- If this is the second iteration (AV = True), and the original
10247 -- node comes from source and we are not in an instance, then give
10248 -- a warning if we know result would be True or False. Note: we
10249 -- know Constant_Condition_Warnings is set if we get here.
10251 elsif Comes_From_Source (Original_Node (N))
10252 and then not In_Instance
10253 then
10254 if True_Result then
10255 Error_Msg_N
10256 ("condition can only be False if invalid values present?",
10258 elsif False_Result then
10259 Error_Msg_N
10260 ("condition can only be True if invalid values present?",
10262 end if;
10263 end if;
10264 end;
10266 -- Skip second iteration if not warning on constant conditions or
10267 -- if the first iteration already generated a warning of some kind or
10268 -- if we are in any case assuming all values are valid (so that the
10269 -- first iteration took care of the valid case).
10271 exit when not Constant_Condition_Warnings;
10272 exit when Warning_Generated;
10273 exit when Assume_No_Invalid_Values;
10274 end loop;
10275 end Rewrite_Comparison;
10277 ----------------------------
10278 -- Safe_In_Place_Array_Op --
10279 ----------------------------
10281 function Safe_In_Place_Array_Op
10282 (Lhs : Node_Id;
10283 Op1 : Node_Id;
10284 Op2 : Node_Id) return Boolean
10286 Target : Entity_Id;
10288 function Is_Safe_Operand (Op : Node_Id) return Boolean;
10289 -- Operand is safe if it cannot overlap part of the target of the
10290 -- operation. If the operand and the target are identical, the operand
10291 -- is safe. The operand can be empty in the case of negation.
10293 function Is_Unaliased (N : Node_Id) return Boolean;
10294 -- Check that N is a stand-alone entity
10296 ------------------
10297 -- Is_Unaliased --
10298 ------------------
10300 function Is_Unaliased (N : Node_Id) return Boolean is
10301 begin
10302 return
10303 Is_Entity_Name (N)
10304 and then No (Address_Clause (Entity (N)))
10305 and then No (Renamed_Object (Entity (N)));
10306 end Is_Unaliased;
10308 ---------------------
10309 -- Is_Safe_Operand --
10310 ---------------------
10312 function Is_Safe_Operand (Op : Node_Id) return Boolean is
10313 begin
10314 if No (Op) then
10315 return True;
10317 elsif Is_Entity_Name (Op) then
10318 return Is_Unaliased (Op);
10320 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
10321 return Is_Unaliased (Prefix (Op));
10323 elsif Nkind (Op) = N_Slice then
10324 return
10325 Is_Unaliased (Prefix (Op))
10326 and then Entity (Prefix (Op)) /= Target;
10328 elsif Nkind (Op) = N_Op_Not then
10329 return Is_Safe_Operand (Right_Opnd (Op));
10331 else
10332 return False;
10333 end if;
10334 end Is_Safe_Operand;
10336 -- Start of processing for Is_Safe_In_Place_Array_Op
10338 begin
10339 -- Skip this processing if the component size is different from system
10340 -- storage unit (since at least for NOT this would cause problems).
10342 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
10343 return False;
10345 -- Cannot do in place stuff on VM_Target since cannot pass addresses
10347 elsif VM_Target /= No_VM then
10348 return False;
10350 -- Cannot do in place stuff if non-standard Boolean representation
10352 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
10353 return False;
10355 elsif not Is_Unaliased (Lhs) then
10356 return False;
10358 else
10359 Target := Entity (Lhs);
10360 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
10361 end if;
10362 end Safe_In_Place_Array_Op;
10364 -----------------------
10365 -- Tagged_Membership --
10366 -----------------------
10368 -- There are two different cases to consider depending on whether the right
10369 -- operand is a class-wide type or not. If not we just compare the actual
10370 -- tag of the left expr to the target type tag:
10372 -- Left_Expr.Tag = Right_Type'Tag;
10374 -- If it is a class-wide type we use the RT function CW_Membership which is
10375 -- usually implemented by looking in the ancestor tables contained in the
10376 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
10378 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
10379 -- function IW_Membership which is usually implemented by looking in the
10380 -- table of abstract interface types plus the ancestor table contained in
10381 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
10383 procedure Tagged_Membership
10384 (N : Node_Id;
10385 SCIL_Node : out Node_Id;
10386 Result : out Node_Id)
10388 Left : constant Node_Id := Left_Opnd (N);
10389 Right : constant Node_Id := Right_Opnd (N);
10390 Loc : constant Source_Ptr := Sloc (N);
10392 Left_Type : Entity_Id;
10393 New_Node : Node_Id;
10394 Right_Type : Entity_Id;
10395 Obj_Tag : Node_Id;
10397 begin
10398 SCIL_Node := Empty;
10400 -- Handle entities from the limited view
10402 Left_Type := Available_View (Etype (Left));
10403 Right_Type := Available_View (Etype (Right));
10405 if Is_Class_Wide_Type (Left_Type) then
10406 Left_Type := Root_Type (Left_Type);
10407 end if;
10409 Obj_Tag :=
10410 Make_Selected_Component (Loc,
10411 Prefix => Relocate_Node (Left),
10412 Selector_Name =>
10413 New_Reference_To (First_Tag_Component (Left_Type), Loc));
10415 if Is_Class_Wide_Type (Right_Type) then
10417 -- No need to issue a run-time check if we statically know that the
10418 -- result of this membership test is always true. For example,
10419 -- considering the following declarations:
10421 -- type Iface is interface;
10422 -- type T is tagged null record;
10423 -- type DT is new T and Iface with null record;
10425 -- Obj1 : T;
10426 -- Obj2 : DT;
10428 -- These membership tests are always true:
10430 -- Obj1 in T'Class
10431 -- Obj2 in T'Class;
10432 -- Obj2 in Iface'Class;
10434 -- We do not need to handle cases where the membership is illegal.
10435 -- For example:
10437 -- Obj1 in DT'Class; -- Compile time error
10438 -- Obj1 in Iface'Class; -- Compile time error
10440 if not Is_Class_Wide_Type (Left_Type)
10441 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
10442 or else (Is_Interface (Etype (Right_Type))
10443 and then Interface_Present_In_Ancestor
10444 (Typ => Left_Type,
10445 Iface => Etype (Right_Type))))
10446 then
10447 Result := New_Reference_To (Standard_True, Loc);
10448 return;
10449 end if;
10451 -- Ada 2005 (AI-251): Class-wide applied to interfaces
10453 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
10455 -- Support to: "Iface_CW_Typ in Typ'Class"
10457 or else Is_Interface (Left_Type)
10458 then
10459 -- Issue error if IW_Membership operation not available in a
10460 -- configurable run time setting.
10462 if not RTE_Available (RE_IW_Membership) then
10463 Error_Msg_CRT
10464 ("dynamic membership test on interface types", N);
10465 Result := Empty;
10466 return;
10467 end if;
10469 Result :=
10470 Make_Function_Call (Loc,
10471 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
10472 Parameter_Associations => New_List (
10473 Make_Attribute_Reference (Loc,
10474 Prefix => Obj_Tag,
10475 Attribute_Name => Name_Address),
10476 New_Reference_To (
10477 Node (First_Elmt
10478 (Access_Disp_Table (Root_Type (Right_Type)))),
10479 Loc)));
10481 -- Ada 95: Normal case
10483 else
10484 Build_CW_Membership (Loc,
10485 Obj_Tag_Node => Obj_Tag,
10486 Typ_Tag_Node =>
10487 New_Reference_To (
10488 Node (First_Elmt
10489 (Access_Disp_Table (Root_Type (Right_Type)))),
10490 Loc),
10491 Related_Nod => N,
10492 New_Node => New_Node);
10494 -- Generate the SCIL node for this class-wide membership test.
10495 -- Done here because the previous call to Build_CW_Membership
10496 -- relocates Obj_Tag.
10498 if Generate_SCIL then
10499 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
10500 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
10501 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
10502 end if;
10504 Result := New_Node;
10505 end if;
10507 -- Right_Type is not a class-wide type
10509 else
10510 -- No need to check the tag of the object if Right_Typ is abstract
10512 if Is_Abstract_Type (Right_Type) then
10513 Result := New_Reference_To (Standard_False, Loc);
10515 else
10516 Result :=
10517 Make_Op_Eq (Loc,
10518 Left_Opnd => Obj_Tag,
10519 Right_Opnd =>
10520 New_Reference_To
10521 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
10522 end if;
10523 end if;
10524 end Tagged_Membership;
10526 ------------------------------
10527 -- Unary_Op_Validity_Checks --
10528 ------------------------------
10530 procedure Unary_Op_Validity_Checks (N : Node_Id) is
10531 begin
10532 if Validity_Checks_On and Validity_Check_Operands then
10533 Ensure_Valid (Right_Opnd (N));
10534 end if;
10535 end Unary_Op_Validity_Checks;
10537 end Exp_Ch4;