]>
Commit | Line | Data |
---|---|---|
70482933 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ C H 4 -- | |
59262ebb | 6 | -- -- |
70482933 RK |
7 | -- B o d y -- |
8 | -- -- | |
1d005acc | 9 | -- Copyright (C) 1992-2019, Free Software Foundation, Inc. -- |
70482933 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
b5c84c3c | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
70482933 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
b5c84c3c RD |
18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
70482933 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
70482933 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
27 | with Checks; use Checks; | |
bded454f | 28 | with Debug; use Debug; |
70482933 RK |
29 | with Einfo; use Einfo; |
30 | with Elists; use Elists; | |
31 | with Errout; use Errout; | |
32 | with Exp_Aggr; use Exp_Aggr; | |
0669bebe | 33 | with Exp_Atag; use Exp_Atag; |
6cce2156 | 34 | with Exp_Ch2; use Exp_Ch2; |
70482933 | 35 | with Exp_Ch3; use Exp_Ch3; |
20b5d666 | 36 | with Exp_Ch6; use Exp_Ch6; |
70482933 RK |
37 | with Exp_Ch7; use Exp_Ch7; |
38 | with Exp_Ch9; use Exp_Ch9; | |
20b5d666 | 39 | with Exp_Disp; use Exp_Disp; |
70482933 | 40 | with Exp_Fixd; use Exp_Fixd; |
437f8c1e | 41 | with Exp_Intr; use Exp_Intr; |
70482933 RK |
42 | with Exp_Pakd; use Exp_Pakd; |
43 | with Exp_Tss; use Exp_Tss; | |
44 | with Exp_Util; use Exp_Util; | |
f02b8bb8 | 45 | with Freeze; use Freeze; |
70482933 | 46 | with Inline; use Inline; |
26bff3d9 | 47 | with Namet; use Namet; |
70482933 RK |
48 | with Nlists; use Nlists; |
49 | with Nmake; use Nmake; | |
50 | with Opt; use Opt; | |
25adc5fb | 51 | with Par_SCO; use Par_SCO; |
0669bebe GB |
52 | with Restrict; use Restrict; |
53 | with Rident; use Rident; | |
70482933 RK |
54 | with Rtsfind; use Rtsfind; |
55 | with Sem; use Sem; | |
a4100e55 | 56 | with Sem_Aux; use Sem_Aux; |
70482933 | 57 | with Sem_Cat; use Sem_Cat; |
5d09245e | 58 | with Sem_Ch3; use Sem_Ch3; |
70482933 RK |
59 | with Sem_Ch13; use Sem_Ch13; |
60 | with Sem_Eval; use Sem_Eval; | |
61 | with Sem_Res; use Sem_Res; | |
62 | with Sem_Type; use Sem_Type; | |
63 | with Sem_Util; use Sem_Util; | |
07fc65c4 | 64 | with Sem_Warn; use Sem_Warn; |
70482933 | 65 | with Sinfo; use Sinfo; |
70482933 RK |
66 | with Snames; use Snames; |
67 | with Stand; use Stand; | |
7665e4bd | 68 | with SCIL_LL; use SCIL_LL; |
07fc65c4 | 69 | with Targparm; use Targparm; |
70482933 RK |
70 | with Tbuild; use Tbuild; |
71 | with Ttypes; use Ttypes; | |
72 | with Uintp; use Uintp; | |
73 | with Urealp; use Urealp; | |
74 | with Validsw; use Validsw; | |
b3889fff | 75 | with Warnsw; use Warnsw; |
70482933 RK |
76 | |
77 | package body Exp_Ch4 is | |
78 | ||
15ce9ca2 AC |
79 | ----------------------- |
80 | -- Local Subprograms -- | |
81 | ----------------------- | |
70482933 RK |
82 | |
83 | procedure Binary_Op_Validity_Checks (N : Node_Id); | |
84 | pragma Inline (Binary_Op_Validity_Checks); | |
85 | -- Performs validity checks for a binary operator | |
86 | ||
fbf5a39b AC |
87 | procedure Build_Boolean_Array_Proc_Call |
88 | (N : Node_Id; | |
89 | Op1 : Node_Id; | |
90 | Op2 : Node_Id); | |
303b4d58 | 91 | -- If a boolean array assignment can be done in place, build call to |
fbf5a39b AC |
92 | -- corresponding library procedure. |
93 | ||
26bff3d9 JM |
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. | |
99 | ||
fbf5a39b AC |
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. | |
103 | ||
70482933 RK |
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 | |
fbf5a39b AC |
108 | -- the actual comparison call that is made. Special processing (call a |
109 | -- run-time routine) | |
70482933 RK |
110 | |
111 | function Expand_Array_Equality | |
112 | (Nod : Node_Id; | |
70482933 RK |
113 | Lhs : Node_Id; |
114 | Rhs : Node_Id; | |
0da2c8ac AC |
115 | Bodies : List_Id; |
116 | Typ : Entity_Id) return Node_Id; | |
70482933 | 117 | -- Expand an array equality into a call to a function implementing this |
685094bf RD |
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. | |
70482933 RK |
127 | |
128 | procedure Expand_Boolean_Operator (N : Node_Id); | |
685094bf RD |
129 | -- Common expansion processing for Boolean operators (And, Or, Xor) for the |
130 | -- case of array type arguments. | |
70482933 | 131 | |
c7a494c9 AC |
132 | procedure Expand_Nonbinary_Modular_Op (N : Node_Id); |
133 | -- When generating C code, convert nonbinary modular arithmetic operations | |
134 | -- into code that relies on the front-end expansion of operator Mod. No | |
135 | -- expansion is performed if N is not a nonbinary modular operand. | |
05dbb83f | 136 | |
5875f8d6 AC |
137 | procedure Expand_Short_Circuit_Operator (N : Node_Id); |
138 | -- Common expansion processing for short-circuit boolean operators | |
139 | ||
456cbfa5 | 140 | procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id); |
5707e389 AC |
141 | -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is |
142 | -- where we allow comparison of "out of range" values. | |
456cbfa5 | 143 | |
70482933 RK |
144 | function Expand_Composite_Equality |
145 | (Nod : Node_Id; | |
146 | Typ : Entity_Id; | |
147 | Lhs : Node_Id; | |
148 | Rhs : Node_Id; | |
2e071734 | 149 | Bodies : List_Id) return Node_Id; |
685094bf RD |
150 | -- Local recursive function used to expand equality for nested composite |
151 | -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which | |
d26d790d AC |
152 | -- to attach bodies of local functions that are created in the process. It |
153 | -- is the responsibility of the caller to insert those bodies at the right | |
154 | -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are | |
155 | -- the left and right sides for the comparison, and Typ is the type of the | |
156 | -- objects to compare. | |
70482933 | 157 | |
fdac1f80 AC |
158 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id); |
159 | -- Routine to expand concatenation of a sequence of two or more operands | |
160 | -- (in the list Operands) and replace node Cnode with the result of the | |
161 | -- concatenation. The operands can be of any appropriate type, and can | |
162 | -- include both arrays and singleton elements. | |
70482933 | 163 | |
f6194278 | 164 | procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id); |
5707e389 AC |
165 | -- N is an N_In membership test mode, with the overflow check mode set to |
166 | -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed | |
167 | -- integer type. This is a case where top level processing is required to | |
168 | -- handle overflow checks in subtrees. | |
f6194278 | 169 | |
70482933 | 170 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); |
685094bf RD |
171 | -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal |
172 | -- fixed. We do not have such a type at runtime, so the purpose of this | |
173 | -- routine is to find the real type by looking up the tree. We also | |
174 | -- determine if the operation must be rounded. | |
70482933 | 175 | |
5d09245e AC |
176 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean; |
177 | -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable | |
178 | -- discriminants if it has a constrained nominal type, unless the object | |
179 | -- is a component of an enclosing Unchecked_Union object that is subject | |
180 | -- to a per-object constraint and the enclosing object lacks inferable | |
181 | -- discriminants. | |
182 | -- | |
183 | -- An expression of an Unchecked_Union type has inferable discriminants | |
184 | -- if it is either a name of an object with inferable discriminants or a | |
185 | -- qualified expression whose subtype mark denotes a constrained subtype. | |
186 | ||
70482933 | 187 | procedure Insert_Dereference_Action (N : Node_Id); |
e6f69614 AC |
188 | -- N is an expression whose type is an access. When the type of the |
189 | -- associated storage pool is derived from Checked_Pool, generate a | |
190 | -- call to the 'Dereference' primitive operation. | |
70482933 RK |
191 | |
192 | function Make_Array_Comparison_Op | |
2e071734 AC |
193 | (Typ : Entity_Id; |
194 | Nod : Node_Id) return Node_Id; | |
685094bf RD |
195 | -- Comparisons between arrays are expanded in line. This function produces |
196 | -- the body of the implementation of (a > b), where a and b are one- | |
197 | -- dimensional arrays of some discrete type. The original node is then | |
198 | -- expanded into the appropriate call to this function. Nod provides the | |
199 | -- Sloc value for the generated code. | |
70482933 RK |
200 | |
201 | function Make_Boolean_Array_Op | |
2e071734 AC |
202 | (Typ : Entity_Id; |
203 | N : Node_Id) return Node_Id; | |
685094bf RD |
204 | -- Boolean operations on boolean arrays are expanded in line. This function |
205 | -- produce the body for the node N, which is (a and b), (a or b), or (a xor | |
206 | -- b). It is used only the normal case and not the packed case. The type | |
207 | -- involved, Typ, is the Boolean array type, and the logical operations in | |
208 | -- the body are simple boolean operations. Note that Typ is always a | |
209 | -- constrained type (the caller has ensured this by using | |
210 | -- Convert_To_Actual_Subtype if necessary). | |
70482933 | 211 | |
b6b5cca8 | 212 | function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean; |
a7f1b24f RD |
213 | -- For signed arithmetic operations when the current overflow mode is |
214 | -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks | |
215 | -- as the first thing we do. We then return. We count on the recursive | |
216 | -- apparatus for overflow checks to call us back with an equivalent | |
217 | -- operation that is in CHECKED mode, avoiding a recursive entry into this | |
218 | -- routine, and that is when we will proceed with the expansion of the | |
219 | -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do | |
220 | -- these optimizations without first making this check, since there may be | |
221 | -- operands further down the tree that are relying on the recursive calls | |
222 | -- triggered by the top level nodes to properly process overflow checking | |
223 | -- and remaining expansion on these nodes. Note that this call back may be | |
224 | -- skipped if the operation is done in Bignum mode but that's fine, since | |
225 | -- the Bignum call takes care of everything. | |
b6b5cca8 | 226 | |
0580d807 AC |
227 | procedure Optimize_Length_Comparison (N : Node_Id); |
228 | -- Given an expression, if it is of the form X'Length op N (or the other | |
229 | -- way round), where N is known at compile time to be 0 or 1, and X is a | |
230 | -- simple entity, and op is a comparison operator, optimizes it into a | |
231 | -- comparison of First and Last. | |
232 | ||
0da343bc AC |
233 | procedure Process_If_Case_Statements (N : Node_Id; Stmts : List_Id); |
234 | -- Inspect and process statement list Stmt of if or case expression N for | |
937e9676 AC |
235 | -- transient objects. If such objects are found, the routine generates code |
236 | -- to clean them up when the context of the expression is evaluated. | |
237 | ||
238 | procedure Process_Transient_In_Expression | |
239 | (Obj_Decl : Node_Id; | |
240 | Expr : Node_Id; | |
241 | Stmts : List_Id); | |
0da343bc AC |
242 | -- Subsidiary routine to the expansion of expression_with_actions, if and |
243 | -- case expressions. Generate all necessary code to finalize a transient | |
937e9676 AC |
244 | -- object when the enclosing context is elaborated or evaluated. Obj_Decl |
245 | -- denotes the declaration of the transient object, which is usually the | |
246 | -- result of a controlled function call. Expr denotes the expression with | |
247 | -- actions, if expression, or case expression node. Stmts denotes the | |
248 | -- statement list which contains Decl, either at the top level or within a | |
249 | -- nested construct. | |
b2c28399 | 250 | |
70482933 | 251 | procedure Rewrite_Comparison (N : Node_Id); |
20b5d666 | 252 | -- If N is the node for a comparison whose outcome can be determined at |
d26dc4b5 AC |
253 | -- compile time, then the node N can be rewritten with True or False. If |
254 | -- the outcome cannot be determined at compile time, the call has no | |
255 | -- effect. If N is a type conversion, then this processing is applied to | |
256 | -- its expression. If N is neither comparison nor a type conversion, the | |
257 | -- call has no effect. | |
70482933 | 258 | |
82878151 AC |
259 | procedure Tagged_Membership |
260 | (N : Node_Id; | |
261 | SCIL_Node : out Node_Id; | |
262 | Result : out Node_Id); | |
70482933 RK |
263 | -- Construct the expression corresponding to the tagged membership test. |
264 | -- Deals with a second operand being (or not) a class-wide type. | |
265 | ||
fbf5a39b | 266 | function Safe_In_Place_Array_Op |
2e071734 AC |
267 | (Lhs : Node_Id; |
268 | Op1 : Node_Id; | |
269 | Op2 : Node_Id) return Boolean; | |
685094bf RD |
270 | -- In the context of an assignment, where the right-hand side is a boolean |
271 | -- operation on arrays, check whether operation can be performed in place. | |
fbf5a39b | 272 | |
70482933 RK |
273 | procedure Unary_Op_Validity_Checks (N : Node_Id); |
274 | pragma Inline (Unary_Op_Validity_Checks); | |
275 | -- Performs validity checks for a unary operator | |
276 | ||
277 | ------------------------------- | |
278 | -- Binary_Op_Validity_Checks -- | |
279 | ------------------------------- | |
280 | ||
281 | procedure Binary_Op_Validity_Checks (N : Node_Id) is | |
282 | begin | |
283 | if Validity_Checks_On and Validity_Check_Operands then | |
284 | Ensure_Valid (Left_Opnd (N)); | |
285 | Ensure_Valid (Right_Opnd (N)); | |
286 | end if; | |
287 | end Binary_Op_Validity_Checks; | |
288 | ||
fbf5a39b AC |
289 | ------------------------------------ |
290 | -- Build_Boolean_Array_Proc_Call -- | |
291 | ------------------------------------ | |
292 | ||
293 | procedure Build_Boolean_Array_Proc_Call | |
294 | (N : Node_Id; | |
295 | Op1 : Node_Id; | |
296 | Op2 : Node_Id) | |
297 | is | |
298 | Loc : constant Source_Ptr := Sloc (N); | |
299 | Kind : constant Node_Kind := Nkind (Expression (N)); | |
300 | Target : constant Node_Id := | |
301 | Make_Attribute_Reference (Loc, | |
302 | Prefix => Name (N), | |
303 | Attribute_Name => Name_Address); | |
304 | ||
bed8af19 | 305 | Arg1 : Node_Id := Op1; |
fbf5a39b AC |
306 | Arg2 : Node_Id := Op2; |
307 | Call_Node : Node_Id; | |
308 | Proc_Name : Entity_Id; | |
309 | ||
310 | begin | |
311 | if Kind = N_Op_Not then | |
312 | if Nkind (Op1) in N_Binary_Op then | |
313 | ||
5e1c00fa | 314 | -- Use negated version of the binary operators |
fbf5a39b AC |
315 | |
316 | if Nkind (Op1) = N_Op_And then | |
317 | Proc_Name := RTE (RE_Vector_Nand); | |
318 | ||
319 | elsif Nkind (Op1) = N_Op_Or then | |
320 | Proc_Name := RTE (RE_Vector_Nor); | |
321 | ||
322 | else pragma Assert (Nkind (Op1) = N_Op_Xor); | |
323 | Proc_Name := RTE (RE_Vector_Xor); | |
324 | end if; | |
325 | ||
326 | Call_Node := | |
327 | Make_Procedure_Call_Statement (Loc, | |
328 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
329 | ||
330 | Parameter_Associations => New_List ( | |
331 | Target, | |
332 | Make_Attribute_Reference (Loc, | |
333 | Prefix => Left_Opnd (Op1), | |
334 | Attribute_Name => Name_Address), | |
335 | ||
336 | Make_Attribute_Reference (Loc, | |
337 | Prefix => Right_Opnd (Op1), | |
338 | Attribute_Name => Name_Address), | |
339 | ||
340 | Make_Attribute_Reference (Loc, | |
341 | Prefix => Left_Opnd (Op1), | |
342 | Attribute_Name => Name_Length))); | |
343 | ||
344 | else | |
345 | Proc_Name := RTE (RE_Vector_Not); | |
346 | ||
347 | Call_Node := | |
348 | Make_Procedure_Call_Statement (Loc, | |
349 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
350 | Parameter_Associations => New_List ( | |
351 | Target, | |
352 | ||
353 | Make_Attribute_Reference (Loc, | |
354 | Prefix => Op1, | |
355 | Attribute_Name => Name_Address), | |
356 | ||
357 | Make_Attribute_Reference (Loc, | |
358 | Prefix => Op1, | |
359 | Attribute_Name => Name_Length))); | |
360 | end if; | |
361 | ||
362 | else | |
363 | -- We use the following equivalences: | |
364 | ||
365 | -- (not X) or (not Y) = not (X and Y) = Nand (X, Y) | |
366 | -- (not X) and (not Y) = not (X or Y) = Nor (X, Y) | |
367 | -- (not X) xor (not Y) = X xor Y | |
368 | -- X xor (not Y) = not (X xor Y) = Nxor (X, Y) | |
369 | ||
370 | if Nkind (Op1) = N_Op_Not then | |
bed8af19 AC |
371 | Arg1 := Right_Opnd (Op1); |
372 | Arg2 := Right_Opnd (Op2); | |
533369aa | 373 | |
fbf5a39b AC |
374 | if Kind = N_Op_And then |
375 | Proc_Name := RTE (RE_Vector_Nor); | |
fbf5a39b AC |
376 | elsif Kind = N_Op_Or then |
377 | Proc_Name := RTE (RE_Vector_Nand); | |
fbf5a39b AC |
378 | else |
379 | Proc_Name := RTE (RE_Vector_Xor); | |
380 | end if; | |
381 | ||
382 | else | |
383 | if Kind = N_Op_And then | |
384 | Proc_Name := RTE (RE_Vector_And); | |
fbf5a39b AC |
385 | elsif Kind = N_Op_Or then |
386 | Proc_Name := RTE (RE_Vector_Or); | |
fbf5a39b AC |
387 | elsif Nkind (Op2) = N_Op_Not then |
388 | Proc_Name := RTE (RE_Vector_Nxor); | |
389 | Arg2 := Right_Opnd (Op2); | |
fbf5a39b AC |
390 | else |
391 | Proc_Name := RTE (RE_Vector_Xor); | |
392 | end if; | |
393 | end if; | |
394 | ||
395 | Call_Node := | |
396 | Make_Procedure_Call_Statement (Loc, | |
397 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
398 | Parameter_Associations => New_List ( | |
399 | Target, | |
955871d3 AC |
400 | Make_Attribute_Reference (Loc, |
401 | Prefix => Arg1, | |
402 | Attribute_Name => Name_Address), | |
403 | Make_Attribute_Reference (Loc, | |
404 | Prefix => Arg2, | |
405 | Attribute_Name => Name_Address), | |
406 | Make_Attribute_Reference (Loc, | |
a8ef12e5 | 407 | Prefix => Arg1, |
955871d3 | 408 | Attribute_Name => Name_Length))); |
fbf5a39b AC |
409 | end if; |
410 | ||
411 | Rewrite (N, Call_Node); | |
412 | Analyze (N); | |
413 | ||
414 | exception | |
415 | when RE_Not_Available => | |
416 | return; | |
417 | end Build_Boolean_Array_Proc_Call; | |
418 | ||
eedc5882 HK |
419 | ----------------------- |
420 | -- Build_Eq_Call -- | |
421 | ----------------------- | |
422 | ||
423 | function Build_Eq_Call | |
424 | (Typ : Entity_Id; | |
425 | Loc : Source_Ptr; | |
426 | Lhs : Node_Id; | |
427 | Rhs : Node_Id) return Node_Id | |
428 | is | |
429 | Prim : Node_Id; | |
430 | Prim_E : Elmt_Id; | |
431 | ||
432 | begin | |
433 | Prim_E := First_Elmt (Collect_Primitive_Operations (Typ)); | |
434 | while Present (Prim_E) loop | |
435 | Prim := Node (Prim_E); | |
436 | ||
437 | -- Locate primitive equality with the right signature | |
438 | ||
439 | if Chars (Prim) = Name_Op_Eq | |
440 | and then Etype (First_Formal (Prim)) = | |
441 | Etype (Next_Formal (First_Formal (Prim))) | |
442 | and then Etype (Prim) = Standard_Boolean | |
443 | then | |
444 | if Is_Abstract_Subprogram (Prim) then | |
445 | return | |
446 | Make_Raise_Program_Error (Loc, | |
447 | Reason => PE_Explicit_Raise); | |
448 | ||
449 | else | |
450 | return | |
451 | Make_Function_Call (Loc, | |
452 | Name => New_Occurrence_Of (Prim, Loc), | |
453 | Parameter_Associations => New_List (Lhs, Rhs)); | |
454 | end if; | |
455 | end if; | |
456 | ||
457 | Next_Elmt (Prim_E); | |
458 | end loop; | |
459 | ||
460 | -- If not found, predefined operation will be used | |
461 | ||
462 | return Empty; | |
463 | end Build_Eq_Call; | |
464 | ||
26bff3d9 JM |
465 | -------------------------------- |
466 | -- Displace_Allocator_Pointer -- | |
467 | -------------------------------- | |
468 | ||
469 | procedure Displace_Allocator_Pointer (N : Node_Id) is | |
470 | Loc : constant Source_Ptr := Sloc (N); | |
471 | Orig_Node : constant Node_Id := Original_Node (N); | |
472 | Dtyp : Entity_Id; | |
473 | Etyp : Entity_Id; | |
474 | PtrT : Entity_Id; | |
475 | ||
476 | begin | |
303b4d58 AC |
477 | -- Do nothing in case of VM targets: the virtual machine will handle |
478 | -- interfaces directly. | |
479 | ||
1f110335 | 480 | if not Tagged_Type_Expansion then |
303b4d58 AC |
481 | return; |
482 | end if; | |
483 | ||
26bff3d9 JM |
484 | pragma Assert (Nkind (N) = N_Identifier |
485 | and then Nkind (Orig_Node) = N_Allocator); | |
486 | ||
487 | PtrT := Etype (Orig_Node); | |
d6a24cdb | 488 | Dtyp := Available_View (Designated_Type (PtrT)); |
26bff3d9 JM |
489 | Etyp := Etype (Expression (Orig_Node)); |
490 | ||
533369aa AC |
491 | if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then |
492 | ||
26bff3d9 JM |
493 | -- If the type of the allocator expression is not an interface type |
494 | -- we can generate code to reference the record component containing | |
495 | -- the pointer to the secondary dispatch table. | |
496 | ||
497 | if not Is_Interface (Etyp) then | |
498 | declare | |
499 | Saved_Typ : constant Entity_Id := Etype (Orig_Node); | |
500 | ||
501 | begin | |
502 | -- 1) Get access to the allocated object | |
503 | ||
504 | Rewrite (N, | |
5972791c | 505 | Make_Explicit_Dereference (Loc, Relocate_Node (N))); |
26bff3d9 JM |
506 | Set_Etype (N, Etyp); |
507 | Set_Analyzed (N); | |
508 | ||
509 | -- 2) Add the conversion to displace the pointer to reference | |
510 | -- the secondary dispatch table. | |
511 | ||
512 | Rewrite (N, Convert_To (Dtyp, Relocate_Node (N))); | |
513 | Analyze_And_Resolve (N, Dtyp); | |
514 | ||
515 | -- 3) The 'access to the secondary dispatch table will be used | |
516 | -- as the value returned by the allocator. | |
517 | ||
518 | Rewrite (N, | |
519 | Make_Attribute_Reference (Loc, | |
520 | Prefix => Relocate_Node (N), | |
521 | Attribute_Name => Name_Access)); | |
522 | Set_Etype (N, Saved_Typ); | |
523 | Set_Analyzed (N); | |
524 | end; | |
525 | ||
526 | -- If the type of the allocator expression is an interface type we | |
527 | -- generate a run-time call to displace "this" to reference the | |
528 | -- component containing the pointer to the secondary dispatch table | |
529 | -- or else raise Constraint_Error if the actual object does not | |
533369aa | 530 | -- implement the target interface. This case corresponds to the |
26bff3d9 JM |
531 | -- following example: |
532 | ||
8fc789c8 | 533 | -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is |
26bff3d9 JM |
534 | -- begin |
535 | -- return new Iface_2'Class'(Obj); | |
536 | -- end Op; | |
537 | ||
538 | else | |
539 | Rewrite (N, | |
540 | Unchecked_Convert_To (PtrT, | |
541 | Make_Function_Call (Loc, | |
e4494292 | 542 | Name => New_Occurrence_Of (RTE (RE_Displace), Loc), |
26bff3d9 JM |
543 | Parameter_Associations => New_List ( |
544 | Unchecked_Convert_To (RTE (RE_Address), | |
545 | Relocate_Node (N)), | |
546 | ||
547 | New_Occurrence_Of | |
548 | (Elists.Node | |
549 | (First_Elmt | |
550 | (Access_Disp_Table (Etype (Base_Type (Dtyp))))), | |
551 | Loc))))); | |
552 | Analyze_And_Resolve (N, PtrT); | |
553 | end if; | |
554 | end if; | |
555 | end Displace_Allocator_Pointer; | |
556 | ||
fbf5a39b AC |
557 | --------------------------------- |
558 | -- Expand_Allocator_Expression -- | |
559 | --------------------------------- | |
560 | ||
561 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
f02b8bb8 RD |
562 | Loc : constant Source_Ptr := Sloc (N); |
563 | Exp : constant Node_Id := Expression (Expression (N)); | |
f02b8bb8 RD |
564 | PtrT : constant Entity_Id := Etype (N); |
565 | DesigT : constant Entity_Id := Designated_Type (PtrT); | |
26bff3d9 JM |
566 | |
567 | procedure Apply_Accessibility_Check | |
568 | (Ref : Node_Id; | |
569 | Built_In_Place : Boolean := False); | |
570 | -- Ada 2005 (AI-344): For an allocator with a class-wide designated | |
685094bf RD |
571 | -- type, generate an accessibility check to verify that the level of the |
572 | -- type of the created object is not deeper than the level of the access | |
50878404 | 573 | -- type. If the type of the qualified expression is class-wide, then |
685094bf RD |
574 | -- always generate the check (except in the case where it is known to be |
575 | -- unnecessary, see comment below). Otherwise, only generate the check | |
576 | -- if the level of the qualified expression type is statically deeper | |
577 | -- than the access type. | |
578 | -- | |
579 | -- Although the static accessibility will generally have been performed | |
580 | -- as a legality check, it won't have been done in cases where the | |
581 | -- allocator appears in generic body, so a run-time check is needed in | |
582 | -- general. One special case is when the access type is declared in the | |
583 | -- same scope as the class-wide allocator, in which case the check can | |
584 | -- never fail, so it need not be generated. | |
585 | -- | |
586 | -- As an open issue, there seem to be cases where the static level | |
587 | -- associated with the class-wide object's underlying type is not | |
588 | -- sufficient to perform the proper accessibility check, such as for | |
589 | -- allocators in nested subprograms or accept statements initialized by | |
590 | -- class-wide formals when the actual originates outside at a deeper | |
591 | -- static level. The nested subprogram case might require passing | |
592 | -- accessibility levels along with class-wide parameters, and the task | |
593 | -- case seems to be an actual gap in the language rules that needs to | |
594 | -- be fixed by the ARG. ??? | |
26bff3d9 JM |
595 | |
596 | ------------------------------- | |
597 | -- Apply_Accessibility_Check -- | |
598 | ------------------------------- | |
599 | ||
600 | procedure Apply_Accessibility_Check | |
601 | (Ref : Node_Id; | |
602 | Built_In_Place : Boolean := False) | |
603 | is | |
a98838ff HK |
604 | Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT); |
605 | Cond : Node_Id; | |
606 | Fin_Call : Node_Id; | |
607 | Free_Stmt : Node_Id; | |
608 | Obj_Ref : Node_Id; | |
609 | Stmts : List_Id; | |
26bff3d9 JM |
610 | |
611 | begin | |
0791fbe9 | 612 | if Ada_Version >= Ada_2005 |
26bff3d9 | 613 | and then Is_Class_Wide_Type (DesigT) |
535a8637 | 614 | and then Tagged_Type_Expansion |
3217f71e | 615 | and then not Scope_Suppress.Suppress (Accessibility_Check) |
26bff3d9 JM |
616 | and then |
617 | (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT) | |
618 | or else | |
619 | (Is_Class_Wide_Type (Etype (Exp)) | |
620 | and then Scope (PtrT) /= Current_Scope)) | |
621 | then | |
e761d11c | 622 | -- If the allocator was built in place, Ref is already a reference |
26bff3d9 | 623 | -- to the access object initialized to the result of the allocator |
e761d11c AC |
624 | -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call |
625 | -- Remove_Side_Effects for cases where the build-in-place call may | |
626 | -- still be the prefix of the reference (to avoid generating | |
627 | -- duplicate calls). Otherwise, it is the entity associated with | |
628 | -- the object containing the address of the allocated object. | |
26bff3d9 JM |
629 | |
630 | if Built_In_Place then | |
e761d11c | 631 | Remove_Side_Effects (Ref); |
a98838ff | 632 | Obj_Ref := New_Copy_Tree (Ref); |
26bff3d9 | 633 | else |
e4494292 | 634 | Obj_Ref := New_Occurrence_Of (Ref, Loc); |
50878404 AC |
635 | end if; |
636 | ||
b6c8e5be AC |
637 | -- For access to interface types we must generate code to displace |
638 | -- the pointer to the base of the object since the subsequent code | |
639 | -- references components located in the TSD of the object (which | |
640 | -- is associated with the primary dispatch table --see a-tags.ads) | |
641 | -- and also generates code invoking Free, which requires also a | |
642 | -- reference to the base of the unallocated object. | |
643 | ||
cc6f5d75 | 644 | if Is_Interface (DesigT) and then Tagged_Type_Expansion then |
b6c8e5be AC |
645 | Obj_Ref := |
646 | Unchecked_Convert_To (Etype (Obj_Ref), | |
647 | Make_Function_Call (Loc, | |
662c2ad4 RD |
648 | Name => |
649 | New_Occurrence_Of (RTE (RE_Base_Address), Loc), | |
b6c8e5be AC |
650 | Parameter_Associations => New_List ( |
651 | Unchecked_Convert_To (RTE (RE_Address), | |
652 | New_Copy_Tree (Obj_Ref))))); | |
653 | end if; | |
654 | ||
50878404 AC |
655 | -- Step 1: Create the object clean up code |
656 | ||
657 | Stmts := New_List; | |
658 | ||
a98838ff HK |
659 | -- Deallocate the object if the accessibility check fails. This |
660 | -- is done only on targets or profiles that support deallocation. | |
661 | ||
662 | -- Free (Obj_Ref); | |
663 | ||
664 | if RTE_Available (RE_Free) then | |
665 | Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref)); | |
666 | Set_Storage_Pool (Free_Stmt, Pool_Id); | |
667 | ||
668 | Append_To (Stmts, Free_Stmt); | |
669 | ||
670 | -- The target or profile cannot deallocate objects | |
671 | ||
672 | else | |
673 | Free_Stmt := Empty; | |
674 | end if; | |
675 | ||
676 | -- Finalize the object if applicable. Generate: | |
a530b8bb AC |
677 | |
678 | -- [Deep_]Finalize (Obj_Ref.all); | |
679 | ||
7cc7f3aa PMR |
680 | if Needs_Finalization (DesigT) |
681 | and then not No_Heap_Finalization (PtrT) | |
682 | then | |
a98838ff | 683 | Fin_Call := |
cc6f5d75 AC |
684 | Make_Final_Call |
685 | (Obj_Ref => | |
686 | Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)), | |
687 | Typ => DesigT); | |
a98838ff | 688 | |
2168d7cc AC |
689 | -- Guard against a missing [Deep_]Finalize when the designated |
690 | -- type was not properly frozen. | |
691 | ||
692 | if No (Fin_Call) then | |
693 | Fin_Call := Make_Null_Statement (Loc); | |
694 | end if; | |
695 | ||
a98838ff HK |
696 | -- When the target or profile supports deallocation, wrap the |
697 | -- finalization call in a block to ensure proper deallocation | |
698 | -- even if finalization fails. Generate: | |
699 | ||
700 | -- begin | |
701 | -- <Fin_Call> | |
702 | -- exception | |
703 | -- when others => | |
704 | -- <Free_Stmt> | |
705 | -- raise; | |
706 | -- end; | |
707 | ||
708 | if Present (Free_Stmt) then | |
709 | Fin_Call := | |
710 | Make_Block_Statement (Loc, | |
711 | Handled_Statement_Sequence => | |
712 | Make_Handled_Sequence_Of_Statements (Loc, | |
713 | Statements => New_List (Fin_Call), | |
714 | ||
715 | Exception_Handlers => New_List ( | |
716 | Make_Exception_Handler (Loc, | |
717 | Exception_Choices => New_List ( | |
718 | Make_Others_Choice (Loc)), | |
a98838ff HK |
719 | Statements => New_List ( |
720 | New_Copy_Tree (Free_Stmt), | |
721 | Make_Raise_Statement (Loc)))))); | |
722 | end if; | |
723 | ||
724 | Prepend_To (Stmts, Fin_Call); | |
f46faa08 AC |
725 | end if; |
726 | ||
50878404 AC |
727 | -- Signal the accessibility failure through a Program_Error |
728 | ||
729 | Append_To (Stmts, | |
730 | Make_Raise_Program_Error (Loc, | |
e4494292 | 731 | Condition => New_Occurrence_Of (Standard_True, Loc), |
50878404 AC |
732 | Reason => PE_Accessibility_Check_Failed)); |
733 | ||
734 | -- Step 2: Create the accessibility comparison | |
735 | ||
736 | -- Generate: | |
737 | -- Ref'Tag | |
738 | ||
b6c8e5be AC |
739 | Obj_Ref := |
740 | Make_Attribute_Reference (Loc, | |
741 | Prefix => Obj_Ref, | |
742 | Attribute_Name => Name_Tag); | |
f46faa08 | 743 | |
50878404 AC |
744 | -- For tagged types, determine the accessibility level by looking |
745 | -- at the type specific data of the dispatch table. Generate: | |
746 | ||
747 | -- Type_Specific_Data (Address (Ref'Tag)).Access_Level | |
748 | ||
f46faa08 | 749 | if Tagged_Type_Expansion then |
50878404 | 750 | Cond := Build_Get_Access_Level (Loc, Obj_Ref); |
f46faa08 | 751 | |
50878404 AC |
752 | -- Use a runtime call to determine the accessibility level when |
753 | -- compiling on virtual machine targets. Generate: | |
f46faa08 | 754 | |
50878404 | 755 | -- Get_Access_Level (Ref'Tag) |
f46faa08 AC |
756 | |
757 | else | |
50878404 AC |
758 | Cond := |
759 | Make_Function_Call (Loc, | |
760 | Name => | |
e4494292 | 761 | New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc), |
50878404 | 762 | Parameter_Associations => New_List (Obj_Ref)); |
26bff3d9 JM |
763 | end if; |
764 | ||
50878404 AC |
765 | Cond := |
766 | Make_Op_Gt (Loc, | |
767 | Left_Opnd => Cond, | |
768 | Right_Opnd => | |
769 | Make_Integer_Literal (Loc, Type_Access_Level (PtrT))); | |
770 | ||
771 | -- Due to the complexity and side effects of the check, utilize an | |
772 | -- if statement instead of the regular Program_Error circuitry. | |
773 | ||
26bff3d9 | 774 | Insert_Action (N, |
8b1011c0 | 775 | Make_Implicit_If_Statement (N, |
50878404 AC |
776 | Condition => Cond, |
777 | Then_Statements => Stmts)); | |
26bff3d9 JM |
778 | end if; |
779 | end Apply_Accessibility_Check; | |
780 | ||
781 | -- Local variables | |
782 | ||
df3e68b1 HK |
783 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); |
784 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
785 | T : constant Entity_Id := Entity (Indic); | |
2168d7cc | 786 | Adj_Call : Node_Id; |
df3e68b1 HK |
787 | Node : Node_Id; |
788 | Tag_Assign : Node_Id; | |
789 | Temp : Entity_Id; | |
790 | Temp_Decl : Node_Id; | |
fbf5a39b | 791 | |
d26dc4b5 AC |
792 | TagT : Entity_Id := Empty; |
793 | -- Type used as source for tag assignment | |
794 | ||
795 | TagR : Node_Id := Empty; | |
796 | -- Target reference for tag assignment | |
797 | ||
26bff3d9 JM |
798 | -- Start of processing for Expand_Allocator_Expression |
799 | ||
fbf5a39b | 800 | begin |
3bfb3c03 JM |
801 | -- Handle call to C++ constructor |
802 | ||
803 | if Is_CPP_Constructor_Call (Exp) then | |
804 | Make_CPP_Constructor_Call_In_Allocator | |
805 | (Allocator => N, | |
806 | Function_Call => Exp); | |
807 | return; | |
808 | end if; | |
809 | ||
885c4871 | 810 | -- In the case of an Ada 2012 allocator whose initial value comes from a |
63585f75 SB |
811 | -- function call, pass "the accessibility level determined by the point |
812 | -- of call" (AI05-0234) to the function. Conceptually, this belongs in | |
813 | -- Expand_Call but it couldn't be done there (because the Etype of the | |
814 | -- allocator wasn't set then) so we generate the parameter here. See | |
815 | -- the Boolean variable Defer in (a block within) Expand_Call. | |
816 | ||
817 | if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then | |
818 | declare | |
819 | Subp : Entity_Id; | |
820 | ||
821 | begin | |
822 | if Nkind (Name (Exp)) = N_Explicit_Dereference then | |
823 | Subp := Designated_Type (Etype (Prefix (Name (Exp)))); | |
824 | else | |
825 | Subp := Entity (Name (Exp)); | |
826 | end if; | |
827 | ||
57a3fca9 AC |
828 | Subp := Ultimate_Alias (Subp); |
829 | ||
63585f75 SB |
830 | if Present (Extra_Accessibility_Of_Result (Subp)) then |
831 | Add_Extra_Actual_To_Call | |
832 | (Subprogram_Call => Exp, | |
833 | Extra_Formal => Extra_Accessibility_Of_Result (Subp), | |
834 | Extra_Actual => Dynamic_Accessibility_Level (PtrT)); | |
835 | end if; | |
836 | end; | |
837 | end if; | |
838 | ||
f6194278 | 839 | -- Case of tagged type or type requiring finalization |
63585f75 SB |
840 | |
841 | if Is_Tagged_Type (T) or else Needs_Finalization (T) then | |
fadcf313 | 842 | |
685094bf RD |
843 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
844 | -- to a build-in-place function, then access to the allocated object | |
d4dfb005 | 845 | -- must be passed to the function. |
20b5d666 | 846 | |
d4dfb005 | 847 | if Is_Build_In_Place_Function_Call (Exp) then |
20b5d666 | 848 | Make_Build_In_Place_Call_In_Allocator (N, Exp); |
26bff3d9 JM |
849 | Apply_Accessibility_Check (N, Built_In_Place => True); |
850 | return; | |
4ac62786 AC |
851 | |
852 | -- Ada 2005 (AI-318-02): Specialization of the previous case for | |
853 | -- expressions containing a build-in-place function call whose | |
854 | -- returned object covers interface types, and Expr has calls to | |
855 | -- Ada.Tags.Displace to displace the pointer to the returned build- | |
856 | -- in-place object to reference the secondary dispatch table of a | |
857 | -- covered interface type. | |
858 | ||
d4dfb005 | 859 | elsif Present (Unqual_BIP_Iface_Function_Call (Exp)) then |
4ac62786 AC |
860 | Make_Build_In_Place_Iface_Call_In_Allocator (N, Exp); |
861 | Apply_Accessibility_Check (N, Built_In_Place => True); | |
862 | return; | |
20b5d666 JM |
863 | end if; |
864 | ||
ca5af305 AC |
865 | -- Actions inserted before: |
866 | -- Temp : constant ptr_T := new T'(Expression); | |
867 | -- Temp._tag = T'tag; -- when not class-wide | |
868 | -- [Deep_]Adjust (Temp.all); | |
fbf5a39b | 869 | |
ca5af305 | 870 | -- We analyze by hand the new internal allocator to avoid any |
6b6041ec | 871 | -- recursion and inappropriate call to Initialize. |
7324bf49 | 872 | |
20b5d666 JM |
873 | -- We don't want to remove side effects when the expression must be |
874 | -- built in place. In the case of a build-in-place function call, | |
875 | -- that could lead to a duplication of the call, which was already | |
876 | -- substituted for the allocator. | |
877 | ||
26bff3d9 | 878 | if not Aggr_In_Place then |
fbf5a39b AC |
879 | Remove_Side_Effects (Exp); |
880 | end if; | |
881 | ||
e86a3a7e | 882 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
883 | |
884 | -- For a class wide allocation generate the following code: | |
885 | ||
886 | -- type Equiv_Record is record ... end record; | |
887 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
888 | -- temp : PtrT := new CW'(CW!(expr)); | |
889 | ||
890 | if Is_Class_Wide_Type (T) then | |
891 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
892 | ||
26bff3d9 JM |
893 | -- Ada 2005 (AI-251): If the expression is a class-wide interface |
894 | -- object we generate code to move up "this" to reference the | |
895 | -- base of the object before allocating the new object. | |
896 | ||
897 | -- Note that Exp'Address is recursively expanded into a call | |
898 | -- to Base_Address (Exp.Tag) | |
899 | ||
900 | if Is_Class_Wide_Type (Etype (Exp)) | |
901 | and then Is_Interface (Etype (Exp)) | |
1f110335 | 902 | and then Tagged_Type_Expansion |
26bff3d9 JM |
903 | then |
904 | Set_Expression | |
905 | (Expression (N), | |
906 | Unchecked_Convert_To (Entity (Indic), | |
907 | Make_Explicit_Dereference (Loc, | |
908 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
909 | Make_Attribute_Reference (Loc, | |
910 | Prefix => Exp, | |
911 | Attribute_Name => Name_Address))))); | |
26bff3d9 JM |
912 | else |
913 | Set_Expression | |
914 | (Expression (N), | |
915 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
916 | end if; | |
fbf5a39b AC |
917 | |
918 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
919 | end if; | |
920 | ||
df3e68b1 | 921 | -- Processing for allocators returning non-interface types |
fbf5a39b | 922 | |
26bff3d9 JM |
923 | if not Is_Interface (Directly_Designated_Type (PtrT)) then |
924 | if Aggr_In_Place then | |
df3e68b1 | 925 | Temp_Decl := |
26bff3d9 JM |
926 | Make_Object_Declaration (Loc, |
927 | Defining_Identifier => Temp, | |
e4494292 | 928 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
26bff3d9 JM |
929 | Expression => |
930 | Make_Allocator (Loc, | |
df3e68b1 | 931 | Expression => |
e4494292 | 932 | New_Occurrence_Of (Etype (Exp), Loc))); |
fbf5a39b | 933 | |
fad0600d AC |
934 | -- Copy the Comes_From_Source flag for the allocator we just |
935 | -- built, since logically this allocator is a replacement of | |
936 | -- the original allocator node. This is for proper handling of | |
937 | -- restriction No_Implicit_Heap_Allocations. | |
938 | ||
26bff3d9 | 939 | Set_Comes_From_Source |
df3e68b1 | 940 | (Expression (Temp_Decl), Comes_From_Source (N)); |
fbf5a39b | 941 | |
df3e68b1 HK |
942 | Set_No_Initialization (Expression (Temp_Decl)); |
943 | Insert_Action (N, Temp_Decl); | |
fbf5a39b | 944 | |
ca5af305 | 945 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 946 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fad0600d | 947 | |
26bff3d9 JM |
948 | else |
949 | Node := Relocate_Node (N); | |
950 | Set_Analyzed (Node); | |
df3e68b1 HK |
951 | |
952 | Temp_Decl := | |
26bff3d9 JM |
953 | Make_Object_Declaration (Loc, |
954 | Defining_Identifier => Temp, | |
955 | Constant_Present => True, | |
e4494292 | 956 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
df3e68b1 HK |
957 | Expression => Node); |
958 | ||
959 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 960 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
fbf5a39b AC |
961 | end if; |
962 | ||
26bff3d9 JM |
963 | -- Ada 2005 (AI-251): Handle allocators whose designated type is an |
964 | -- interface type. In this case we use the type of the qualified | |
965 | -- expression to allocate the object. | |
966 | ||
fbf5a39b | 967 | else |
26bff3d9 | 968 | declare |
191fcb3a | 969 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); |
26bff3d9 | 970 | New_Decl : Node_Id; |
fbf5a39b | 971 | |
26bff3d9 JM |
972 | begin |
973 | New_Decl := | |
974 | Make_Full_Type_Declaration (Loc, | |
975 | Defining_Identifier => Def_Id, | |
cc6f5d75 | 976 | Type_Definition => |
26bff3d9 JM |
977 | Make_Access_To_Object_Definition (Loc, |
978 | All_Present => True, | |
979 | Null_Exclusion_Present => False, | |
0929eaeb AC |
980 | Constant_Present => |
981 | Is_Access_Constant (Etype (N)), | |
26bff3d9 | 982 | Subtype_Indication => |
e4494292 | 983 | New_Occurrence_Of (Etype (Exp), Loc))); |
26bff3d9 JM |
984 | |
985 | Insert_Action (N, New_Decl); | |
986 | ||
df3e68b1 HK |
987 | -- Inherit the allocation-related attributes from the original |
988 | -- access type. | |
26bff3d9 | 989 | |
24d4b3d5 AC |
990 | Set_Finalization_Master |
991 | (Def_Id, Finalization_Master (PtrT)); | |
df3e68b1 | 992 | |
24d4b3d5 AC |
993 | Set_Associated_Storage_Pool |
994 | (Def_Id, Associated_Storage_Pool (PtrT)); | |
758c442c | 995 | |
26bff3d9 JM |
996 | -- Declare the object using the previous type declaration |
997 | ||
998 | if Aggr_In_Place then | |
df3e68b1 | 999 | Temp_Decl := |
26bff3d9 JM |
1000 | Make_Object_Declaration (Loc, |
1001 | Defining_Identifier => Temp, | |
e4494292 | 1002 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
26bff3d9 JM |
1003 | Expression => |
1004 | Make_Allocator (Loc, | |
e4494292 | 1005 | New_Occurrence_Of (Etype (Exp), Loc))); |
26bff3d9 | 1006 | |
fad0600d AC |
1007 | -- Copy the Comes_From_Source flag for the allocator we just |
1008 | -- built, since logically this allocator is a replacement of | |
1009 | -- the original allocator node. This is for proper handling | |
1010 | -- of restriction No_Implicit_Heap_Allocations. | |
1011 | ||
26bff3d9 | 1012 | Set_Comes_From_Source |
df3e68b1 | 1013 | (Expression (Temp_Decl), Comes_From_Source (N)); |
26bff3d9 | 1014 | |
df3e68b1 HK |
1015 | Set_No_Initialization (Expression (Temp_Decl)); |
1016 | Insert_Action (N, Temp_Decl); | |
26bff3d9 | 1017 | |
ca5af305 | 1018 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1019 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
26bff3d9 | 1020 | |
26bff3d9 JM |
1021 | else |
1022 | Node := Relocate_Node (N); | |
1023 | Set_Analyzed (Node); | |
df3e68b1 HK |
1024 | |
1025 | Temp_Decl := | |
26bff3d9 JM |
1026 | Make_Object_Declaration (Loc, |
1027 | Defining_Identifier => Temp, | |
1028 | Constant_Present => True, | |
e4494292 | 1029 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
df3e68b1 HK |
1030 | Expression => Node); |
1031 | ||
1032 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 1033 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
26bff3d9 JM |
1034 | end if; |
1035 | ||
1036 | -- Generate an additional object containing the address of the | |
1037 | -- returned object. The type of this second object declaration | |
685094bf RD |
1038 | -- is the correct type required for the common processing that |
1039 | -- is still performed by this subprogram. The displacement of | |
1040 | -- this pointer to reference the component associated with the | |
1041 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
1042 | |
1043 | New_Decl := | |
1044 | Make_Object_Declaration (Loc, | |
243cae0a | 1045 | Defining_Identifier => Make_Temporary (Loc, 'P'), |
e4494292 | 1046 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
243cae0a | 1047 | Expression => |
df3e68b1 | 1048 | Unchecked_Convert_To (PtrT, |
e4494292 | 1049 | New_Occurrence_Of (Temp, Loc))); |
26bff3d9 JM |
1050 | |
1051 | Insert_Action (N, New_Decl); | |
1052 | ||
df3e68b1 HK |
1053 | Temp_Decl := New_Decl; |
1054 | Temp := Defining_Identifier (New_Decl); | |
26bff3d9 | 1055 | end; |
758c442c GD |
1056 | end if; |
1057 | ||
26bff3d9 JM |
1058 | -- Generate the tag assignment |
1059 | ||
535a8637 | 1060 | -- Suppress the tag assignment for VM targets because VM tags are |
26bff3d9 JM |
1061 | -- represented implicitly in objects. |
1062 | ||
1f110335 | 1063 | if not Tagged_Type_Expansion then |
26bff3d9 | 1064 | null; |
fbf5a39b | 1065 | |
26bff3d9 JM |
1066 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
1067 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 1068 | |
26bff3d9 JM |
1069 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
1070 | pragma Assert (Is_Class_Wide_Type | |
1071 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
1072 | null; |
1073 | ||
1074 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
1075 | TagT := T; | |
e4494292 | 1076 | TagR := New_Occurrence_Of (Temp, Loc); |
d26dc4b5 AC |
1077 | |
1078 | elsif Is_Private_Type (T) | |
1079 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 1080 | then |
d26dc4b5 | 1081 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
1082 | TagR := |
1083 | Unchecked_Convert_To (Underlying_Type (T), | |
1084 | Make_Explicit_Dereference (Loc, | |
e4494292 | 1085 | Prefix => New_Occurrence_Of (Temp, Loc))); |
d26dc4b5 AC |
1086 | end if; |
1087 | ||
1088 | if Present (TagT) then | |
38171f43 AC |
1089 | declare |
1090 | Full_T : constant Entity_Id := Underlying_Type (TagT); | |
e4494292 | 1091 | |
38171f43 AC |
1092 | begin |
1093 | Tag_Assign := | |
1094 | Make_Assignment_Statement (Loc, | |
cc6f5d75 | 1095 | Name => |
38171f43 | 1096 | Make_Selected_Component (Loc, |
cc6f5d75 | 1097 | Prefix => TagR, |
38171f43 | 1098 | Selector_Name => |
e4494292 RD |
1099 | New_Occurrence_Of |
1100 | (First_Tag_Component (Full_T), Loc)), | |
1101 | ||
38171f43 AC |
1102 | Expression => |
1103 | Unchecked_Convert_To (RTE (RE_Tag), | |
e4494292 | 1104 | New_Occurrence_Of |
38171f43 AC |
1105 | (Elists.Node |
1106 | (First_Elmt (Access_Disp_Table (Full_T))), Loc))); | |
1107 | end; | |
fbf5a39b AC |
1108 | |
1109 | -- The previous assignment has to be done in any case | |
1110 | ||
1111 | Set_Assignment_OK (Name (Tag_Assign)); | |
1112 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
1113 | end if; |
1114 | ||
18431dc5 AC |
1115 | -- Generate an Adjust call if the object will be moved. In Ada 2005, |
1116 | -- the object may be inherently limited, in which case there is no | |
1117 | -- Adjust procedure, and the object is built in place. In Ada 95, the | |
1118 | -- object can be limited but not inherently limited if this allocator | |
1119 | -- came from a return statement (we're allocating the result on the | |
1120 | -- secondary stack). In that case, the object will be moved, so we do | |
3a248f7c BD |
1121 | -- want to Adjust. However, if it's a nonlimited build-in-place |
1122 | -- function call, Adjust is not wanted. | |
18431dc5 AC |
1123 | |
1124 | if Needs_Finalization (DesigT) | |
1125 | and then Needs_Finalization (T) | |
1126 | and then not Aggr_In_Place | |
1127 | and then not Is_Limited_View (T) | |
3a248f7c BD |
1128 | and then not Alloc_For_BIP_Return (N) |
1129 | and then not Is_Build_In_Place_Function_Call (Expression (N)) | |
18431dc5 AC |
1130 | then |
1131 | -- An unchecked conversion is needed in the classwide case because | |
1132 | -- the designated type can be an ancestor of the subtype mark of | |
1133 | -- the allocator. | |
df3e68b1 | 1134 | |
2168d7cc | 1135 | Adj_Call := |
18431dc5 AC |
1136 | Make_Adjust_Call |
1137 | (Obj_Ref => | |
1138 | Unchecked_Convert_To (T, | |
1139 | Make_Explicit_Dereference (Loc, | |
1140 | Prefix => New_Occurrence_Of (Temp, Loc))), | |
2168d7cc AC |
1141 | Typ => T); |
1142 | ||
1143 | if Present (Adj_Call) then | |
1144 | Insert_Action (N, Adj_Call); | |
1145 | end if; | |
18431dc5 | 1146 | end if; |
fbf5a39b | 1147 | |
18431dc5 AC |
1148 | -- Note: the accessibility check must be inserted after the call to |
1149 | -- [Deep_]Adjust to ensure proper completion of the assignment. | |
fbf5a39b | 1150 | |
18431dc5 | 1151 | Apply_Accessibility_Check (Temp); |
fbf5a39b | 1152 | |
e4494292 | 1153 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1154 | Analyze_And_Resolve (N, PtrT); |
1155 | ||
685094bf RD |
1156 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
1157 | -- component containing the secondary dispatch table of the interface | |
1158 | -- type. | |
26bff3d9 JM |
1159 | |
1160 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
1161 | Displace_Allocator_Pointer (N); | |
1162 | end if; | |
1163 | ||
dfbc6cbe AC |
1164 | -- Always force the generation of a temporary for aggregates when |
1165 | -- generating C code, to simplify the work in the code generator. | |
1166 | ||
1167 | elsif Aggr_In_Place | |
c63a2ad6 | 1168 | or else (Modify_Tree_For_C and then Nkind (Exp) = N_Aggregate) |
dfbc6cbe | 1169 | then |
e86a3a7e | 1170 | Temp := Make_Temporary (Loc, 'P', N); |
df3e68b1 | 1171 | Temp_Decl := |
fbf5a39b AC |
1172 | Make_Object_Declaration (Loc, |
1173 | Defining_Identifier => Temp, | |
e4494292 | 1174 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
df3e68b1 HK |
1175 | Expression => |
1176 | Make_Allocator (Loc, | |
e4494292 | 1177 | Expression => New_Occurrence_Of (Etype (Exp), Loc))); |
fbf5a39b | 1178 | |
fad0600d AC |
1179 | -- Copy the Comes_From_Source flag for the allocator we just built, |
1180 | -- since logically this allocator is a replacement of the original | |
1181 | -- allocator node. This is for proper handling of restriction | |
1182 | -- No_Implicit_Heap_Allocations. | |
1183 | ||
fbf5a39b | 1184 | Set_Comes_From_Source |
df3e68b1 HK |
1185 | (Expression (Temp_Decl), Comes_From_Source (N)); |
1186 | ||
1187 | Set_No_Initialization (Expression (Temp_Decl)); | |
1188 | Insert_Action (N, Temp_Decl); | |
1189 | ||
ca5af305 | 1190 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1191 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fbf5a39b | 1192 | |
e4494292 | 1193 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1194 | Analyze_And_Resolve (N, PtrT); |
1195 | ||
533369aa | 1196 | elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then |
51e4c4b9 AC |
1197 | Install_Null_Excluding_Check (Exp); |
1198 | ||
f02b8bb8 | 1199 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
1200 | and then Nkind (Exp) = N_Allocator |
1201 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
1202 | then | |
0da2c8ac | 1203 | -- Apply constraint to designated subtype indication |
fbf5a39b | 1204 | |
cc6f5d75 AC |
1205 | Apply_Constraint_Check |
1206 | (Expression (Exp), Designated_Type (DesigT), No_Sliding => True); | |
fbf5a39b AC |
1207 | |
1208 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
1209 | ||
1210 | -- Propagate constraint_error to enclosing allocator | |
1211 | ||
1212 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
1213 | end if; | |
1df4f514 | 1214 | |
fbf5a39b | 1215 | else |
14f0f659 AC |
1216 | Build_Allocate_Deallocate_Proc (N, True); |
1217 | ||
36c73552 AC |
1218 | -- If we have: |
1219 | -- type A is access T1; | |
1220 | -- X : A := new T2'(...); | |
1221 | -- T1 and T2 can be different subtypes, and we might need to check | |
1222 | -- both constraints. First check against the type of the qualified | |
1223 | -- expression. | |
1224 | ||
1225 | Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
fbf5a39b | 1226 | |
d79e621a | 1227 | if Do_Range_Check (Exp) then |
d79e621a GD |
1228 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); |
1229 | end if; | |
1230 | ||
685094bf RD |
1231 | -- A check is also needed in cases where the designated subtype is |
1232 | -- constrained and differs from the subtype given in the qualified | |
1233 | -- expression. Note that the check on the qualified expression does | |
1234 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 1235 | |
f02b8bb8 | 1236 | if Is_Constrained (DesigT) |
9450205a | 1237 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
1238 | then |
1239 | Apply_Constraint_Check | |
f02b8bb8 | 1240 | (Exp, DesigT, No_Sliding => False); |
d79e621a GD |
1241 | |
1242 | if Do_Range_Check (Exp) then | |
d79e621a GD |
1243 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); |
1244 | end if; | |
f02b8bb8 RD |
1245 | end if; |
1246 | ||
685094bf RD |
1247 | -- For an access to unconstrained packed array, GIGI needs to see an |
1248 | -- expression with a constrained subtype in order to compute the | |
1249 | -- proper size for the allocator. | |
f02b8bb8 RD |
1250 | |
1251 | if Is_Array_Type (T) | |
1252 | and then not Is_Constrained (T) | |
1253 | and then Is_Packed (T) | |
1254 | then | |
1255 | declare | |
191fcb3a | 1256 | ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A'); |
f02b8bb8 RD |
1257 | Internal_Exp : constant Node_Id := Relocate_Node (Exp); |
1258 | begin | |
1259 | Insert_Action (Exp, | |
1260 | Make_Subtype_Declaration (Loc, | |
1261 | Defining_Identifier => ConstrT, | |
25ebc085 AC |
1262 | Subtype_Indication => |
1263 | Make_Subtype_From_Expr (Internal_Exp, T))); | |
f02b8bb8 RD |
1264 | Freeze_Itype (ConstrT, Exp); |
1265 | Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp)); | |
1266 | end; | |
fbf5a39b | 1267 | end if; |
f02b8bb8 | 1268 | |
685094bf RD |
1269 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
1270 | -- to a build-in-place function, then access to the allocated object | |
d4dfb005 | 1271 | -- must be passed to the function. |
20b5d666 | 1272 | |
d4dfb005 | 1273 | if Is_Build_In_Place_Function_Call (Exp) then |
20b5d666 JM |
1274 | Make_Build_In_Place_Call_In_Allocator (N, Exp); |
1275 | end if; | |
fbf5a39b AC |
1276 | end if; |
1277 | ||
1278 | exception | |
1279 | when RE_Not_Available => | |
1280 | return; | |
1281 | end Expand_Allocator_Expression; | |
1282 | ||
70482933 RK |
1283 | ----------------------------- |
1284 | -- Expand_Array_Comparison -- | |
1285 | ----------------------------- | |
1286 | ||
685094bf RD |
1287 | -- Expansion is only required in the case of array types. For the unpacked |
1288 | -- case, an appropriate runtime routine is called. For packed cases, and | |
1289 | -- also in some other cases where a runtime routine cannot be called, the | |
1290 | -- form of the expansion is: | |
70482933 RK |
1291 | |
1292 | -- [body for greater_nn; boolean_expression] | |
1293 | ||
1294 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
1295 | -- Boolean expression depends on the operator involved. | |
1296 | ||
1297 | procedure Expand_Array_Comparison (N : Node_Id) is | |
1298 | Loc : constant Source_Ptr := Sloc (N); | |
1299 | Op1 : Node_Id := Left_Opnd (N); | |
1300 | Op2 : Node_Id := Right_Opnd (N); | |
1301 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 1302 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
1303 | |
1304 | Expr : Node_Id; | |
1305 | Func_Body : Node_Id; | |
1306 | Func_Name : Entity_Id; | |
1307 | ||
fbf5a39b AC |
1308 | Comp : RE_Id; |
1309 | ||
9bc43c53 AC |
1310 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
1311 | -- True for byte addressable target | |
91b1417d | 1312 | |
fbf5a39b | 1313 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
685094bf RD |
1314 | -- Returns True if the length of the given operand is known to be less |
1315 | -- than 4. Returns False if this length is known to be four or greater | |
1316 | -- or is not known at compile time. | |
fbf5a39b AC |
1317 | |
1318 | ------------------------ | |
1319 | -- Length_Less_Than_4 -- | |
1320 | ------------------------ | |
1321 | ||
1322 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
1323 | Otyp : constant Entity_Id := Etype (Opnd); | |
1324 | ||
1325 | begin | |
1326 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
1327 | return String_Literal_Length (Otyp) < 4; | |
1328 | ||
1329 | else | |
1330 | declare | |
1331 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
1332 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
1333 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
1334 | Lov : Uint; | |
1335 | Hiv : Uint; | |
1336 | ||
1337 | begin | |
1338 | if Compile_Time_Known_Value (Lo) then | |
1339 | Lov := Expr_Value (Lo); | |
1340 | else | |
1341 | return False; | |
1342 | end if; | |
1343 | ||
1344 | if Compile_Time_Known_Value (Hi) then | |
1345 | Hiv := Expr_Value (Hi); | |
1346 | else | |
1347 | return False; | |
1348 | end if; | |
1349 | ||
1350 | return Hiv < Lov + 3; | |
1351 | end; | |
1352 | end if; | |
1353 | end Length_Less_Than_4; | |
1354 | ||
1355 | -- Start of processing for Expand_Array_Comparison | |
1356 | ||
70482933 | 1357 | begin |
fbf5a39b AC |
1358 | -- Deal first with unpacked case, where we can call a runtime routine |
1359 | -- except that we avoid this for targets for which are not addressable | |
535a8637 | 1360 | -- by bytes. |
fbf5a39b AC |
1361 | |
1362 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 1363 | and then Byte_Addressable |
fbf5a39b AC |
1364 | then |
1365 | -- The call we generate is: | |
1366 | ||
1367 | -- Compare_Array_xn[_Unaligned] | |
1368 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
1369 | ||
1370 | -- x = U for unsigned, S for signed | |
1371 | -- n = 8,16,32,64 for component size | |
1372 | -- Add _Unaligned if length < 4 and component size is 8. | |
1373 | -- <op> is the standard comparison operator | |
1374 | ||
1375 | if Component_Size (Typ1) = 8 then | |
1376 | if Length_Less_Than_4 (Op1) | |
1377 | or else | |
1378 | Length_Less_Than_4 (Op2) | |
1379 | then | |
1380 | if Is_Unsigned_Type (Ctyp) then | |
1381 | Comp := RE_Compare_Array_U8_Unaligned; | |
1382 | else | |
1383 | Comp := RE_Compare_Array_S8_Unaligned; | |
1384 | end if; | |
1385 | ||
1386 | else | |
1387 | if Is_Unsigned_Type (Ctyp) then | |
1388 | Comp := RE_Compare_Array_U8; | |
1389 | else | |
1390 | Comp := RE_Compare_Array_S8; | |
1391 | end if; | |
1392 | end if; | |
1393 | ||
1394 | elsif Component_Size (Typ1) = 16 then | |
1395 | if Is_Unsigned_Type (Ctyp) then | |
1396 | Comp := RE_Compare_Array_U16; | |
1397 | else | |
1398 | Comp := RE_Compare_Array_S16; | |
1399 | end if; | |
1400 | ||
1401 | elsif Component_Size (Typ1) = 32 then | |
1402 | if Is_Unsigned_Type (Ctyp) then | |
1403 | Comp := RE_Compare_Array_U32; | |
1404 | else | |
1405 | Comp := RE_Compare_Array_S32; | |
1406 | end if; | |
1407 | ||
1408 | else pragma Assert (Component_Size (Typ1) = 64); | |
1409 | if Is_Unsigned_Type (Ctyp) then | |
1410 | Comp := RE_Compare_Array_U64; | |
1411 | else | |
1412 | Comp := RE_Compare_Array_S64; | |
1413 | end if; | |
1414 | end if; | |
1415 | ||
9fe696a3 | 1416 | if RTE_Available (Comp) then |
fbf5a39b | 1417 | |
9fe696a3 | 1418 | -- Expand to a call only if the runtime function is available, |
744c73a5 | 1419 | -- otherwise fall back to inline code. |
fbf5a39b | 1420 | |
9fe696a3 AC |
1421 | Remove_Side_Effects (Op1, Name_Req => True); |
1422 | Remove_Side_Effects (Op2, Name_Req => True); | |
fbf5a39b | 1423 | |
9fe696a3 AC |
1424 | Rewrite (Op1, |
1425 | Make_Function_Call (Sloc (Op1), | |
1426 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
fbf5a39b | 1427 | |
9fe696a3 AC |
1428 | Parameter_Associations => New_List ( |
1429 | Make_Attribute_Reference (Loc, | |
1430 | Prefix => Relocate_Node (Op1), | |
1431 | Attribute_Name => Name_Address), | |
fbf5a39b | 1432 | |
9fe696a3 AC |
1433 | Make_Attribute_Reference (Loc, |
1434 | Prefix => Relocate_Node (Op2), | |
1435 | Attribute_Name => Name_Address), | |
fbf5a39b | 1436 | |
9fe696a3 AC |
1437 | Make_Attribute_Reference (Loc, |
1438 | Prefix => Relocate_Node (Op1), | |
1439 | Attribute_Name => Name_Length), | |
fbf5a39b | 1440 | |
9fe696a3 AC |
1441 | Make_Attribute_Reference (Loc, |
1442 | Prefix => Relocate_Node (Op2), | |
1443 | Attribute_Name => Name_Length)))); | |
1444 | ||
1445 | Rewrite (Op2, | |
1446 | Make_Integer_Literal (Sloc (Op2), | |
1447 | Intval => Uint_0)); | |
1448 | ||
1449 | Analyze_And_Resolve (Op1, Standard_Integer); | |
1450 | Analyze_And_Resolve (Op2, Standard_Integer); | |
1451 | return; | |
1452 | end if; | |
fbf5a39b AC |
1453 | end if; |
1454 | ||
1455 | -- Cases where we cannot make runtime call | |
1456 | ||
70482933 RK |
1457 | -- For (a <= b) we convert to not (a > b) |
1458 | ||
1459 | if Chars (N) = Name_Op_Le then | |
1460 | Rewrite (N, | |
1461 | Make_Op_Not (Loc, | |
1462 | Right_Opnd => | |
1463 | Make_Op_Gt (Loc, | |
1464 | Left_Opnd => Op1, | |
1465 | Right_Opnd => Op2))); | |
1466 | Analyze_And_Resolve (N, Standard_Boolean); | |
1467 | return; | |
1468 | ||
1469 | -- For < the Boolean expression is | |
1470 | -- greater__nn (op2, op1) | |
1471 | ||
1472 | elsif Chars (N) = Name_Op_Lt then | |
1473 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1474 | ||
1475 | -- Switch operands | |
1476 | ||
1477 | Op1 := Right_Opnd (N); | |
1478 | Op2 := Left_Opnd (N); | |
1479 | ||
1480 | -- For (a >= b) we convert to not (a < b) | |
1481 | ||
1482 | elsif Chars (N) = Name_Op_Ge then | |
1483 | Rewrite (N, | |
1484 | Make_Op_Not (Loc, | |
1485 | Right_Opnd => | |
1486 | Make_Op_Lt (Loc, | |
1487 | Left_Opnd => Op1, | |
1488 | Right_Opnd => Op2))); | |
1489 | Analyze_And_Resolve (N, Standard_Boolean); | |
1490 | return; | |
1491 | ||
1492 | -- For > the Boolean expression is | |
1493 | -- greater__nn (op1, op2) | |
1494 | ||
1495 | else | |
1496 | pragma Assert (Chars (N) = Name_Op_Gt); | |
1497 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1498 | end if; | |
1499 | ||
1500 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1501 | Expr := | |
1502 | Make_Function_Call (Loc, | |
e4494292 | 1503 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
1504 | Parameter_Associations => New_List (Op1, Op2)); |
1505 | ||
1506 | Insert_Action (N, Func_Body); | |
1507 | Rewrite (N, Expr); | |
1508 | Analyze_And_Resolve (N, Standard_Boolean); | |
70482933 RK |
1509 | end Expand_Array_Comparison; |
1510 | ||
1511 | --------------------------- | |
1512 | -- Expand_Array_Equality -- | |
1513 | --------------------------- | |
1514 | ||
685094bf RD |
1515 | -- Expand an equality function for multi-dimensional arrays. Here is an |
1516 | -- example of such a function for Nb_Dimension = 2 | |
70482933 | 1517 | |
0da2c8ac | 1518 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 1519 | -- begin |
fbf5a39b AC |
1520 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
1521 | -- and then | |
1522 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
1523 | -- then | |
1524 | -- return True; -- RM 4.5.2(22) | |
1525 | -- end if; | |
0da2c8ac | 1526 | |
fbf5a39b AC |
1527 | -- if A'length (1) /= B'length (1) |
1528 | -- or else | |
1529 | -- A'length (2) /= B'length (2) | |
1530 | -- then | |
1531 | -- return False; -- RM 4.5.2(23) | |
1532 | -- end if; | |
0da2c8ac | 1533 | |
fbf5a39b | 1534 | -- declare |
523456db AC |
1535 | -- A1 : Index_T1 := A'first (1); |
1536 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 1537 | -- begin |
523456db | 1538 | -- loop |
fbf5a39b | 1539 | -- declare |
523456db AC |
1540 | -- A2 : Index_T2 := A'first (2); |
1541 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 1542 | -- begin |
523456db | 1543 | -- loop |
fbf5a39b AC |
1544 | -- if A (A1, A2) /= B (B1, B2) then |
1545 | -- return False; | |
70482933 | 1546 | -- end if; |
0da2c8ac | 1547 | |
523456db AC |
1548 | -- exit when A2 = A'last (2); |
1549 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 1550 | -- B2 := Index_T2'succ (B2); |
70482933 | 1551 | -- end loop; |
fbf5a39b | 1552 | -- end; |
0da2c8ac | 1553 | |
523456db AC |
1554 | -- exit when A1 = A'last (1); |
1555 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 1556 | -- B1 := Index_T1'succ (B1); |
70482933 | 1557 | -- end loop; |
fbf5a39b | 1558 | -- end; |
0da2c8ac | 1559 | |
70482933 RK |
1560 | -- return true; |
1561 | -- end Enn; | |
1562 | ||
685094bf RD |
1563 | -- Note on the formal types used (atyp and btyp). If either of the arrays |
1564 | -- is of a private type, we use the underlying type, and do an unchecked | |
1565 | -- conversion of the actual. If either of the arrays has a bound depending | |
1566 | -- on a discriminant, then we use the base type since otherwise we have an | |
1567 | -- escaped discriminant in the function. | |
0da2c8ac | 1568 | |
685094bf RD |
1569 | -- If both arrays are constrained and have the same bounds, we can generate |
1570 | -- a loop with an explicit iteration scheme using a 'Range attribute over | |
1571 | -- the first array. | |
523456db | 1572 | |
70482933 RK |
1573 | function Expand_Array_Equality |
1574 | (Nod : Node_Id; | |
70482933 RK |
1575 | Lhs : Node_Id; |
1576 | Rhs : Node_Id; | |
0da2c8ac AC |
1577 | Bodies : List_Id; |
1578 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
1579 | is |
1580 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
1581 | Decls : constant List_Id := New_List; |
1582 | Index_List1 : constant List_Id := New_List; | |
1583 | Index_List2 : constant List_Id := New_List; | |
1584 | ||
1dd3915b | 1585 | First_Idx : Node_Id; |
fbf5a39b AC |
1586 | Formals : List_Id; |
1587 | Func_Name : Entity_Id; | |
1588 | Func_Body : Node_Id; | |
70482933 RK |
1589 | |
1590 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
1591 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
1592 | ||
0da2c8ac AC |
1593 | Ltyp : Entity_Id; |
1594 | Rtyp : Entity_Id; | |
1595 | -- The parameter types to be used for the formals | |
1596 | ||
1dd3915b EB |
1597 | New_Lhs : Node_Id; |
1598 | New_Rhs : Node_Id; | |
1599 | -- The LHS and RHS converted to the parameter types | |
1600 | ||
fbf5a39b AC |
1601 | function Arr_Attr |
1602 | (Arr : Entity_Id; | |
1603 | Nam : Name_Id; | |
2e071734 | 1604 | Num : Int) return Node_Id; |
5e1c00fa | 1605 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 1606 | |
70482933 | 1607 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
685094bf | 1608 | -- Create one statement to compare corresponding components, designated |
3b42c566 | 1609 | -- by a full set of indexes. |
70482933 | 1610 | |
0da2c8ac | 1611 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
685094bf RD |
1612 | -- Given one of the arguments, computes the appropriate type to be used |
1613 | -- for that argument in the corresponding function formal | |
0da2c8ac | 1614 | |
fbf5a39b | 1615 | function Handle_One_Dimension |
70482933 | 1616 | (N : Int; |
2e071734 | 1617 | Index : Node_Id) return Node_Id; |
0da2c8ac | 1618 | -- This procedure returns the following code |
fbf5a39b AC |
1619 | -- |
1620 | -- declare | |
523456db | 1621 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 1622 | -- begin |
523456db | 1623 | -- loop |
fbf5a39b | 1624 | -- xxx |
523456db AC |
1625 | -- exit when An = A'Last (N); |
1626 | -- An := Index_T'Succ (An) | |
0da2c8ac | 1627 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
1628 | -- end loop; |
1629 | -- end; | |
1630 | -- | |
3b42c566 | 1631 | -- If both indexes are constrained and identical, the procedure |
523456db AC |
1632 | -- returns a simpler loop: |
1633 | -- | |
1634 | -- for An in A'Range (N) loop | |
1635 | -- xxx | |
1636 | -- end loop | |
0da2c8ac | 1637 | -- |
523456db | 1638 | -- N is the dimension for which we are generating a loop. Index is the |
685094bf RD |
1639 | -- N'th index node, whose Etype is Index_Type_n in the above code. The |
1640 | -- xxx statement is either the loop or declare for the next dimension | |
1641 | -- or if this is the last dimension the comparison of corresponding | |
1642 | -- components of the arrays. | |
fbf5a39b | 1643 | -- |
685094bf | 1644 | -- The actual way the code works is to return the comparison of |
a90bd866 | 1645 | -- corresponding components for the N+1 call. That's neater. |
fbf5a39b AC |
1646 | |
1647 | function Test_Empty_Arrays return Node_Id; | |
1648 | -- This function constructs the test for both arrays being empty | |
1649 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1650 | -- and then | |
1651 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1652 | ||
1653 | function Test_Lengths_Correspond return Node_Id; | |
685094bf RD |
1654 | -- This function constructs the test for arrays having different lengths |
1655 | -- in at least one index position, in which case the resulting code is: | |
fbf5a39b AC |
1656 | |
1657 | -- A'length (1) /= B'length (1) | |
1658 | -- or else | |
1659 | -- A'length (2) /= B'length (2) | |
1660 | -- or else | |
1661 | -- ... | |
1662 | ||
1663 | -------------- | |
1664 | -- Arr_Attr -- | |
1665 | -------------- | |
1666 | ||
1667 | function Arr_Attr | |
1668 | (Arr : Entity_Id; | |
1669 | Nam : Name_Id; | |
2e071734 | 1670 | Num : Int) return Node_Id |
fbf5a39b AC |
1671 | is |
1672 | begin | |
1673 | return | |
1674 | Make_Attribute_Reference (Loc, | |
cc6f5d75 AC |
1675 | Attribute_Name => Nam, |
1676 | Prefix => New_Occurrence_Of (Arr, Loc), | |
1677 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
fbf5a39b | 1678 | end Arr_Attr; |
70482933 RK |
1679 | |
1680 | ------------------------ | |
1681 | -- Component_Equality -- | |
1682 | ------------------------ | |
1683 | ||
1684 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1685 | Test : Node_Id; | |
1686 | L, R : Node_Id; | |
1687 | ||
1688 | begin | |
1689 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1690 | ||
1691 | L := | |
1692 | Make_Indexed_Component (Loc, | |
7675ad4f | 1693 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
1694 | Expressions => Index_List1); |
1695 | ||
1696 | R := | |
1697 | Make_Indexed_Component (Loc, | |
7675ad4f | 1698 | Prefix => Make_Identifier (Loc, Chars (B)), |
70482933 RK |
1699 | Expressions => Index_List2); |
1700 | ||
1701 | Test := Expand_Composite_Equality | |
1702 | (Nod, Component_Type (Typ), L, R, Decls); | |
1703 | ||
a9d8907c JM |
1704 | -- If some (sub)component is an unchecked_union, the whole operation |
1705 | -- will raise program error. | |
8aceda64 AC |
1706 | |
1707 | if Nkind (Test) = N_Raise_Program_Error then | |
a9d8907c JM |
1708 | |
1709 | -- This node is going to be inserted at a location where a | |
685094bf RD |
1710 | -- statement is expected: clear its Etype so analysis will set |
1711 | -- it to the expected Standard_Void_Type. | |
a9d8907c JM |
1712 | |
1713 | Set_Etype (Test, Empty); | |
8aceda64 AC |
1714 | return Test; |
1715 | ||
1716 | else | |
1717 | return | |
1718 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 1719 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), |
8aceda64 | 1720 | Then_Statements => New_List ( |
d766cee3 | 1721 | Make_Simple_Return_Statement (Loc, |
8aceda64 AC |
1722 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
1723 | end if; | |
70482933 RK |
1724 | end Component_Equality; |
1725 | ||
0da2c8ac AC |
1726 | ------------------ |
1727 | -- Get_Arg_Type -- | |
1728 | ------------------ | |
1729 | ||
1730 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1731 | T : Entity_Id; | |
1732 | X : Node_Id; | |
1733 | ||
1734 | begin | |
1735 | T := Etype (N); | |
1736 | ||
1737 | if No (T) then | |
1738 | return Typ; | |
1739 | ||
1740 | else | |
1741 | T := Underlying_Type (T); | |
1742 | ||
1743 | X := First_Index (T); | |
1744 | while Present (X) loop | |
761f7dcb AC |
1745 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) |
1746 | or else | |
1747 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
0da2c8ac AC |
1748 | then |
1749 | T := Base_Type (T); | |
1750 | exit; | |
1751 | end if; | |
1752 | ||
1753 | Next_Index (X); | |
1754 | end loop; | |
1755 | ||
1756 | return T; | |
1757 | end if; | |
1758 | end Get_Arg_Type; | |
1759 | ||
fbf5a39b AC |
1760 | -------------------------- |
1761 | -- Handle_One_Dimension -- | |
1762 | --------------------------- | |
70482933 | 1763 | |
fbf5a39b | 1764 | function Handle_One_Dimension |
70482933 | 1765 | (N : Int; |
2e071734 | 1766 | Index : Node_Id) return Node_Id |
70482933 | 1767 | is |
0da2c8ac | 1768 | Need_Separate_Indexes : constant Boolean := |
761f7dcb | 1769 | Ltyp /= Rtyp or else not Is_Constrained (Ltyp); |
0da2c8ac | 1770 | -- If the index types are identical, and we are working with |
685094bf RD |
1771 | -- constrained types, then we can use the same index for both |
1772 | -- of the arrays. | |
0da2c8ac | 1773 | |
191fcb3a | 1774 | An : constant Entity_Id := Make_Temporary (Loc, 'A'); |
0da2c8ac AC |
1775 | |
1776 | Bn : Entity_Id; | |
1777 | Index_T : Entity_Id; | |
1778 | Stm_List : List_Id; | |
1779 | Loop_Stm : Node_Id; | |
70482933 RK |
1780 | |
1781 | begin | |
0da2c8ac AC |
1782 | if N > Number_Dimensions (Ltyp) then |
1783 | return Component_Equality (Ltyp); | |
fbf5a39b | 1784 | end if; |
70482933 | 1785 | |
0da2c8ac AC |
1786 | -- Case where we generate a loop |
1787 | ||
1788 | Index_T := Base_Type (Etype (Index)); | |
1789 | ||
1790 | if Need_Separate_Indexes then | |
191fcb3a | 1791 | Bn := Make_Temporary (Loc, 'B'); |
0da2c8ac AC |
1792 | else |
1793 | Bn := An; | |
1794 | end if; | |
70482933 | 1795 | |
e4494292 RD |
1796 | Append (New_Occurrence_Of (An, Loc), Index_List1); |
1797 | Append (New_Occurrence_Of (Bn, Loc), Index_List2); | |
70482933 | 1798 | |
0da2c8ac AC |
1799 | Stm_List := New_List ( |
1800 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1801 | |
0da2c8ac | 1802 | if Need_Separate_Indexes then |
a9d8907c | 1803 | |
3b42c566 | 1804 | -- Generate guard for loop, followed by increments of indexes |
523456db AC |
1805 | |
1806 | Append_To (Stm_List, | |
1807 | Make_Exit_Statement (Loc, | |
1808 | Condition => | |
1809 | Make_Op_Eq (Loc, | |
cc6f5d75 | 1810 | Left_Opnd => New_Occurrence_Of (An, Loc), |
523456db AC |
1811 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); |
1812 | ||
1813 | Append_To (Stm_List, | |
1814 | Make_Assignment_Statement (Loc, | |
e4494292 | 1815 | Name => New_Occurrence_Of (An, Loc), |
523456db AC |
1816 | Expression => |
1817 | Make_Attribute_Reference (Loc, | |
e4494292 | 1818 | Prefix => New_Occurrence_Of (Index_T, Loc), |
523456db | 1819 | Attribute_Name => Name_Succ, |
e4494292 RD |
1820 | Expressions => New_List ( |
1821 | New_Occurrence_Of (An, Loc))))); | |
523456db | 1822 | |
0da2c8ac AC |
1823 | Append_To (Stm_List, |
1824 | Make_Assignment_Statement (Loc, | |
e4494292 | 1825 | Name => New_Occurrence_Of (Bn, Loc), |
0da2c8ac AC |
1826 | Expression => |
1827 | Make_Attribute_Reference (Loc, | |
e4494292 | 1828 | Prefix => New_Occurrence_Of (Index_T, Loc), |
0da2c8ac | 1829 | Attribute_Name => Name_Succ, |
e4494292 RD |
1830 | Expressions => New_List ( |
1831 | New_Occurrence_Of (Bn, Loc))))); | |
0da2c8ac AC |
1832 | end if; |
1833 | ||
a9d8907c JM |
1834 | -- If separate indexes, we need a declare block for An and Bn, and a |
1835 | -- loop without an iteration scheme. | |
0da2c8ac AC |
1836 | |
1837 | if Need_Separate_Indexes then | |
523456db AC |
1838 | Loop_Stm := |
1839 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
1840 | ||
0da2c8ac AC |
1841 | return |
1842 | Make_Block_Statement (Loc, | |
1843 | Declarations => New_List ( | |
523456db AC |
1844 | Make_Object_Declaration (Loc, |
1845 | Defining_Identifier => An, | |
e4494292 | 1846 | Object_Definition => New_Occurrence_Of (Index_T, Loc), |
523456db AC |
1847 | Expression => Arr_Attr (A, Name_First, N)), |
1848 | ||
0da2c8ac AC |
1849 | Make_Object_Declaration (Loc, |
1850 | Defining_Identifier => Bn, | |
e4494292 | 1851 | Object_Definition => New_Occurrence_Of (Index_T, Loc), |
0da2c8ac | 1852 | Expression => Arr_Attr (B, Name_First, N))), |
523456db | 1853 | |
0da2c8ac AC |
1854 | Handled_Statement_Sequence => |
1855 | Make_Handled_Sequence_Of_Statements (Loc, | |
1856 | Statements => New_List (Loop_Stm))); | |
1857 | ||
523456db | 1858 | -- If no separate indexes, return loop statement with explicit |
31fde973 | 1859 | -- iteration scheme on its own. |
0da2c8ac AC |
1860 | |
1861 | else | |
523456db AC |
1862 | Loop_Stm := |
1863 | Make_Implicit_Loop_Statement (Nod, | |
1864 | Statements => Stm_List, | |
1865 | Iteration_Scheme => | |
1866 | Make_Iteration_Scheme (Loc, | |
1867 | Loop_Parameter_Specification => | |
1868 | Make_Loop_Parameter_Specification (Loc, | |
1869 | Defining_Identifier => An, | |
1870 | Discrete_Subtype_Definition => | |
1871 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
1872 | return Loop_Stm; |
1873 | end if; | |
fbf5a39b AC |
1874 | end Handle_One_Dimension; |
1875 | ||
1876 | ----------------------- | |
1877 | -- Test_Empty_Arrays -- | |
1878 | ----------------------- | |
1879 | ||
1880 | function Test_Empty_Arrays return Node_Id is | |
1881 | Alist : Node_Id; | |
1882 | Blist : Node_Id; | |
1883 | ||
1884 | Atest : Node_Id; | |
1885 | Btest : Node_Id; | |
70482933 | 1886 | |
fbf5a39b AC |
1887 | begin |
1888 | Alist := Empty; | |
1889 | Blist := Empty; | |
0da2c8ac | 1890 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1891 | Atest := |
1892 | Make_Op_Eq (Loc, | |
1893 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1894 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1895 | ||
1896 | Btest := | |
1897 | Make_Op_Eq (Loc, | |
1898 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
1899 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1900 | ||
1901 | if No (Alist) then | |
1902 | Alist := Atest; | |
1903 | Blist := Btest; | |
70482933 | 1904 | |
fbf5a39b AC |
1905 | else |
1906 | Alist := | |
1907 | Make_Or_Else (Loc, | |
1908 | Left_Opnd => Relocate_Node (Alist), | |
1909 | Right_Opnd => Atest); | |
1910 | ||
1911 | Blist := | |
1912 | Make_Or_Else (Loc, | |
1913 | Left_Opnd => Relocate_Node (Blist), | |
1914 | Right_Opnd => Btest); | |
1915 | end if; | |
1916 | end loop; | |
70482933 | 1917 | |
fbf5a39b AC |
1918 | return |
1919 | Make_And_Then (Loc, | |
1920 | Left_Opnd => Alist, | |
1921 | Right_Opnd => Blist); | |
1922 | end Test_Empty_Arrays; | |
70482933 | 1923 | |
fbf5a39b AC |
1924 | ----------------------------- |
1925 | -- Test_Lengths_Correspond -- | |
1926 | ----------------------------- | |
70482933 | 1927 | |
fbf5a39b AC |
1928 | function Test_Lengths_Correspond return Node_Id is |
1929 | Result : Node_Id; | |
1930 | Rtest : Node_Id; | |
1931 | ||
1932 | begin | |
1933 | Result := Empty; | |
0da2c8ac | 1934 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1935 | Rtest := |
1936 | Make_Op_Ne (Loc, | |
1937 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1938 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
1939 | ||
1940 | if No (Result) then | |
1941 | Result := Rtest; | |
1942 | else | |
1943 | Result := | |
1944 | Make_Or_Else (Loc, | |
1945 | Left_Opnd => Relocate_Node (Result), | |
1946 | Right_Opnd => Rtest); | |
1947 | end if; | |
1948 | end loop; | |
1949 | ||
1950 | return Result; | |
1951 | end Test_Lengths_Correspond; | |
70482933 RK |
1952 | |
1953 | -- Start of processing for Expand_Array_Equality | |
1954 | ||
1955 | begin | |
0da2c8ac AC |
1956 | Ltyp := Get_Arg_Type (Lhs); |
1957 | Rtyp := Get_Arg_Type (Rhs); | |
1958 | ||
685094bf RD |
1959 | -- For now, if the argument types are not the same, go to the base type, |
1960 | -- since the code assumes that the formals have the same type. This is | |
1961 | -- fixable in future ??? | |
0da2c8ac AC |
1962 | |
1963 | if Ltyp /= Rtyp then | |
1964 | Ltyp := Base_Type (Ltyp); | |
1965 | Rtyp := Base_Type (Rtyp); | |
1966 | pragma Assert (Ltyp = Rtyp); | |
1967 | end if; | |
1968 | ||
1dd3915b EB |
1969 | -- If the array type is distinct from the type of the arguments, it |
1970 | -- is the full view of a private type. Apply an unchecked conversion | |
1971 | -- to ensure that analysis of the code below succeeds. | |
1972 | ||
1973 | if No (Etype (Lhs)) | |
1974 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
1975 | then | |
1976 | New_Lhs := OK_Convert_To (Ltyp, Lhs); | |
1977 | else | |
1978 | New_Lhs := Lhs; | |
1979 | end if; | |
1980 | ||
1981 | if No (Etype (Rhs)) | |
1982 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
1983 | then | |
1984 | New_Rhs := OK_Convert_To (Rtyp, Rhs); | |
1985 | else | |
1986 | New_Rhs := Rhs; | |
1987 | end if; | |
1988 | ||
1989 | First_Idx := First_Index (Ltyp); | |
1990 | ||
1991 | -- If optimization is enabled and the array boils down to a couple of | |
1992 | -- consecutive elements, generate a simple conjunction of comparisons | |
1993 | -- which should be easier to optimize by the code generator. | |
1994 | ||
1995 | if Optimization_Level > 0 | |
1996 | and then Ltyp = Rtyp | |
1997 | and then Is_Constrained (Ltyp) | |
1998 | and then Number_Dimensions (Ltyp) = 1 | |
1999 | and then Nkind (First_Idx) = N_Range | |
2000 | and then Compile_Time_Known_Value (Low_Bound (First_Idx)) | |
2001 | and then Compile_Time_Known_Value (High_Bound (First_Idx)) | |
2002 | and then Expr_Value (High_Bound (First_Idx)) = | |
2003 | Expr_Value (Low_Bound (First_Idx)) + 1 | |
2004 | then | |
2005 | declare | |
2006 | Ctyp : constant Entity_Id := Component_Type (Ltyp); | |
2007 | L, R : Node_Id; | |
2008 | TestL, TestH : Node_Id; | |
2009 | Index_List : List_Id; | |
2010 | ||
2011 | begin | |
2012 | Index_List := New_List (New_Copy_Tree (Low_Bound (First_Idx))); | |
2013 | ||
2014 | L := | |
2015 | Make_Indexed_Component (Loc, | |
2016 | Prefix => New_Copy_Tree (New_Lhs), | |
2017 | Expressions => Index_List); | |
2018 | ||
2019 | R := | |
2020 | Make_Indexed_Component (Loc, | |
2021 | Prefix => New_Copy_Tree (New_Rhs), | |
2022 | Expressions => Index_List); | |
2023 | ||
2024 | TestL := Expand_Composite_Equality (Nod, Ctyp, L, R, Bodies); | |
2025 | ||
2026 | Index_List := New_List (New_Copy_Tree (High_Bound (First_Idx))); | |
2027 | ||
2028 | L := | |
2029 | Make_Indexed_Component (Loc, | |
2030 | Prefix => New_Lhs, | |
2031 | Expressions => Index_List); | |
2032 | ||
2033 | R := | |
2034 | Make_Indexed_Component (Loc, | |
2035 | Prefix => New_Rhs, | |
2036 | Expressions => Index_List); | |
2037 | ||
2038 | TestH := Expand_Composite_Equality (Nod, Ctyp, L, R, Bodies); | |
2039 | ||
2040 | return | |
2041 | Make_And_Then (Loc, Left_Opnd => TestL, Right_Opnd => TestH); | |
2042 | end; | |
2043 | end if; | |
2044 | ||
0da2c8ac AC |
2045 | -- Build list of formals for function |
2046 | ||
70482933 RK |
2047 | Formals := New_List ( |
2048 | Make_Parameter_Specification (Loc, | |
2049 | Defining_Identifier => A, | |
e4494292 | 2050 | Parameter_Type => New_Occurrence_Of (Ltyp, Loc)), |
70482933 RK |
2051 | |
2052 | Make_Parameter_Specification (Loc, | |
2053 | Defining_Identifier => B, | |
e4494292 | 2054 | Parameter_Type => New_Occurrence_Of (Rtyp, Loc))); |
70482933 | 2055 | |
191fcb3a | 2056 | Func_Name := Make_Temporary (Loc, 'E'); |
70482933 | 2057 | |
fbf5a39b | 2058 | -- Build statement sequence for function |
70482933 RK |
2059 | |
2060 | Func_Body := | |
2061 | Make_Subprogram_Body (Loc, | |
2062 | Specification => | |
2063 | Make_Function_Specification (Loc, | |
2064 | Defining_Unit_Name => Func_Name, | |
2065 | Parameter_Specifications => Formals, | |
e4494292 | 2066 | Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)), |
fbf5a39b | 2067 | |
eedc5882 | 2068 | Declarations => Decls, |
fbf5a39b | 2069 | |
70482933 RK |
2070 | Handled_Statement_Sequence => |
2071 | Make_Handled_Sequence_Of_Statements (Loc, | |
2072 | Statements => New_List ( | |
fbf5a39b AC |
2073 | |
2074 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 2075 | Condition => Test_Empty_Arrays, |
fbf5a39b | 2076 | Then_Statements => New_List ( |
d766cee3 | 2077 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
2078 | Expression => |
2079 | New_Occurrence_Of (Standard_True, Loc)))), | |
2080 | ||
2081 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 2082 | Condition => Test_Lengths_Correspond, |
fbf5a39b | 2083 | Then_Statements => New_List ( |
d766cee3 | 2084 | Make_Simple_Return_Statement (Loc, |
cc6f5d75 | 2085 | Expression => New_Occurrence_Of (Standard_False, Loc)))), |
fbf5a39b | 2086 | |
1dd3915b | 2087 | Handle_One_Dimension (1, First_Idx), |
fbf5a39b | 2088 | |
d766cee3 | 2089 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
2090 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
2091 | ||
1dd3915b EB |
2092 | Set_Has_Completion (Func_Name, True); |
2093 | Set_Is_Inlined (Func_Name); | |
70482933 | 2094 | |
1dd3915b | 2095 | Append_To (Bodies, Func_Body); |
70482933 | 2096 | |
1dd3915b EB |
2097 | return |
2098 | Make_Function_Call (Loc, | |
2099 | Name => New_Occurrence_Of (Func_Name, Loc), | |
2100 | Parameter_Associations => New_List (New_Lhs, New_Rhs)); | |
70482933 RK |
2101 | end Expand_Array_Equality; |
2102 | ||
2103 | ----------------------------- | |
2104 | -- Expand_Boolean_Operator -- | |
2105 | ----------------------------- | |
2106 | ||
685094bf RD |
2107 | -- Note that we first get the actual subtypes of the operands, since we |
2108 | -- always want to deal with types that have bounds. | |
70482933 RK |
2109 | |
2110 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 2111 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
2112 | |
2113 | begin | |
685094bf RD |
2114 | -- Special case of bit packed array where both operands are known to be |
2115 | -- properly aligned. In this case we use an efficient run time routine | |
2116 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
2117 | |
2118 | if Is_Bit_Packed_Array (Typ) | |
2119 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
2120 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
2121 | then | |
70482933 | 2122 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
2123 | return; |
2124 | end if; | |
70482933 | 2125 | |
a9d8907c JM |
2126 | -- For the normal non-packed case, the general expansion is to build |
2127 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
2128 | -- and then inserting it into the tree. The original operator node is | |
2129 | -- then rewritten as a call to this function. We also use this in the | |
2130 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 2131 | |
a9d8907c JM |
2132 | declare |
2133 | Loc : constant Source_Ptr := Sloc (N); | |
2134 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
076bbec1 | 2135 | R : Node_Id := Relocate_Node (Right_Opnd (N)); |
a9d8907c JM |
2136 | Func_Body : Node_Id; |
2137 | Func_Name : Entity_Id; | |
fbf5a39b | 2138 | |
a9d8907c JM |
2139 | begin |
2140 | Convert_To_Actual_Subtype (L); | |
2141 | Convert_To_Actual_Subtype (R); | |
2142 | Ensure_Defined (Etype (L), N); | |
2143 | Ensure_Defined (Etype (R), N); | |
2144 | Apply_Length_Check (R, Etype (L)); | |
2145 | ||
b4592168 | 2146 | if Nkind (N) = N_Op_Xor then |
076bbec1 ES |
2147 | R := Duplicate_Subexpr (R); |
2148 | Silly_Boolean_Array_Xor_Test (N, R, Etype (L)); | |
b4592168 GD |
2149 | end if; |
2150 | ||
a9d8907c JM |
2151 | if Nkind (Parent (N)) = N_Assignment_Statement |
2152 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
2153 | then | |
2154 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 2155 | |
a9d8907c JM |
2156 | elsif Nkind (Parent (N)) = N_Op_Not |
2157 | and then Nkind (N) = N_Op_And | |
39f0fa29 | 2158 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement |
cc6f5d75 | 2159 | and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
2160 | then |
2161 | return; | |
2162 | else | |
fbf5a39b | 2163 | |
a9d8907c JM |
2164 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
2165 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
2166 | Insert_Action (N, Func_Body); | |
70482933 | 2167 | |
a9d8907c | 2168 | -- Now rewrite the expression with a call |
70482933 | 2169 | |
a9d8907c JM |
2170 | Rewrite (N, |
2171 | Make_Function_Call (Loc, | |
e4494292 | 2172 | Name => New_Occurrence_Of (Func_Name, Loc), |
a9d8907c JM |
2173 | Parameter_Associations => |
2174 | New_List ( | |
2175 | L, | |
2176 | Make_Type_Conversion | |
e4494292 | 2177 | (Loc, New_Occurrence_Of (Etype (L), Loc), R)))); |
70482933 | 2178 | |
a9d8907c JM |
2179 | Analyze_And_Resolve (N, Typ); |
2180 | end if; | |
2181 | end; | |
70482933 RK |
2182 | end Expand_Boolean_Operator; |
2183 | ||
456cbfa5 AC |
2184 | ------------------------------------------------ |
2185 | -- Expand_Compare_Minimize_Eliminate_Overflow -- | |
2186 | ------------------------------------------------ | |
2187 | ||
2188 | procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is | |
2189 | Loc : constant Source_Ptr := Sloc (N); | |
2190 | ||
71fb4dc8 AC |
2191 | Result_Type : constant Entity_Id := Etype (N); |
2192 | -- Capture result type (could be a derived boolean type) | |
2193 | ||
456cbfa5 AC |
2194 | Llo, Lhi : Uint; |
2195 | Rlo, Rhi : Uint; | |
2196 | ||
2197 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
2198 | -- Entity for Long_Long_Integer'Base | |
2199 | ||
15c94a55 | 2200 | Check : constant Overflow_Mode_Type := Overflow_Check_Mode; |
a7f1b24f | 2201 | -- Current overflow checking mode |
456cbfa5 AC |
2202 | |
2203 | procedure Set_True; | |
2204 | procedure Set_False; | |
2205 | -- These procedures rewrite N with an occurrence of Standard_True or | |
2206 | -- Standard_False, and then makes a call to Warn_On_Known_Condition. | |
2207 | ||
2208 | --------------- | |
2209 | -- Set_False -- | |
2210 | --------------- | |
2211 | ||
2212 | procedure Set_False is | |
2213 | begin | |
2214 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
2215 | Warn_On_Known_Condition (N); | |
2216 | end Set_False; | |
2217 | ||
2218 | -------------- | |
2219 | -- Set_True -- | |
2220 | -------------- | |
2221 | ||
2222 | procedure Set_True is | |
2223 | begin | |
2224 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
2225 | Warn_On_Known_Condition (N); | |
2226 | end Set_True; | |
2227 | ||
2228 | -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow | |
2229 | ||
2230 | begin | |
2231 | -- Nothing to do unless we have a comparison operator with operands | |
2232 | -- that are signed integer types, and we are operating in either | |
2233 | -- MINIMIZED or ELIMINATED overflow checking mode. | |
2234 | ||
2235 | if Nkind (N) not in N_Op_Compare | |
2236 | or else Check not in Minimized_Or_Eliminated | |
2237 | or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N))) | |
2238 | then | |
2239 | return; | |
2240 | end if; | |
2241 | ||
2242 | -- OK, this is the case we are interested in. First step is to process | |
2243 | -- our operands using the Minimize_Eliminate circuitry which applies | |
2244 | -- this processing to the two operand subtrees. | |
2245 | ||
a7f1b24f | 2246 | Minimize_Eliminate_Overflows |
c7e152b5 | 2247 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
a7f1b24f | 2248 | Minimize_Eliminate_Overflows |
c7e152b5 | 2249 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
456cbfa5 | 2250 | |
65f7ed64 AC |
2251 | -- See if the range information decides the result of the comparison. |
2252 | -- We can only do this if we in fact have full range information (which | |
2253 | -- won't be the case if either operand is bignum at this stage). | |
456cbfa5 | 2254 | |
65f7ed64 AC |
2255 | if Llo /= No_Uint and then Rlo /= No_Uint then |
2256 | case N_Op_Compare (Nkind (N)) is | |
d8f43ee6 HK |
2257 | when N_Op_Eq => |
2258 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
2259 | Set_True; | |
2260 | elsif Llo > Rhi or else Lhi < Rlo then | |
2261 | Set_False; | |
2262 | end if; | |
456cbfa5 | 2263 | |
d8f43ee6 HK |
2264 | when N_Op_Ge => |
2265 | if Llo >= Rhi then | |
2266 | Set_True; | |
2267 | elsif Lhi < Rlo then | |
2268 | Set_False; | |
2269 | end if; | |
456cbfa5 | 2270 | |
d8f43ee6 HK |
2271 | when N_Op_Gt => |
2272 | if Llo > Rhi then | |
2273 | Set_True; | |
2274 | elsif Lhi <= Rlo then | |
2275 | Set_False; | |
2276 | end if; | |
456cbfa5 | 2277 | |
d8f43ee6 HK |
2278 | when N_Op_Le => |
2279 | if Llo > Rhi then | |
2280 | Set_False; | |
2281 | elsif Lhi <= Rlo then | |
2282 | Set_True; | |
2283 | end if; | |
456cbfa5 | 2284 | |
d8f43ee6 HK |
2285 | when N_Op_Lt => |
2286 | if Llo >= Rhi then | |
2287 | Set_False; | |
2288 | elsif Lhi < Rlo then | |
2289 | Set_True; | |
2290 | end if; | |
456cbfa5 | 2291 | |
d8f43ee6 HK |
2292 | when N_Op_Ne => |
2293 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
2294 | Set_False; | |
2295 | elsif Llo > Rhi or else Lhi < Rlo then | |
2296 | Set_True; | |
2297 | end if; | |
65f7ed64 | 2298 | end case; |
456cbfa5 | 2299 | |
65f7ed64 | 2300 | -- All done if we did the rewrite |
456cbfa5 | 2301 | |
65f7ed64 AC |
2302 | if Nkind (N) not in N_Op_Compare then |
2303 | return; | |
2304 | end if; | |
456cbfa5 AC |
2305 | end if; |
2306 | ||
2307 | -- Otherwise, time to do the comparison | |
2308 | ||
2309 | declare | |
2310 | Ltype : constant Entity_Id := Etype (Left_Opnd (N)); | |
2311 | Rtype : constant Entity_Id := Etype (Right_Opnd (N)); | |
2312 | ||
2313 | begin | |
2314 | -- If the two operands have the same signed integer type we are | |
2315 | -- all set, nothing more to do. This is the case where either | |
2316 | -- both operands were unchanged, or we rewrote both of them to | |
2317 | -- be Long_Long_Integer. | |
2318 | ||
2319 | -- Note: Entity for the comparison may be wrong, but it's not worth | |
2320 | -- the effort to change it, since the back end does not use it. | |
2321 | ||
2322 | if Is_Signed_Integer_Type (Ltype) | |
2323 | and then Base_Type (Ltype) = Base_Type (Rtype) | |
2324 | then | |
2325 | return; | |
2326 | ||
2327 | -- Here if bignums are involved (can only happen in ELIMINATED mode) | |
2328 | ||
2329 | elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then | |
2330 | declare | |
2331 | Left : Node_Id := Left_Opnd (N); | |
2332 | Right : Node_Id := Right_Opnd (N); | |
2333 | -- Bignum references for left and right operands | |
2334 | ||
2335 | begin | |
2336 | if not Is_RTE (Ltype, RE_Bignum) then | |
2337 | Left := Convert_To_Bignum (Left); | |
2338 | elsif not Is_RTE (Rtype, RE_Bignum) then | |
2339 | Right := Convert_To_Bignum (Right); | |
2340 | end if; | |
2341 | ||
71fb4dc8 | 2342 | -- We rewrite our node with: |
456cbfa5 | 2343 | |
71fb4dc8 AC |
2344 | -- do |
2345 | -- Bnn : Result_Type; | |
2346 | -- declare | |
2347 | -- M : Mark_Id := SS_Mark; | |
2348 | -- begin | |
2349 | -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc) | |
2350 | -- SS_Release (M); | |
2351 | -- end; | |
2352 | -- in | |
2353 | -- Bnn | |
2354 | -- end | |
456cbfa5 AC |
2355 | |
2356 | declare | |
71fb4dc8 | 2357 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
456cbfa5 AC |
2358 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); |
2359 | Ent : RE_Id; | |
2360 | ||
2361 | begin | |
2362 | case N_Op_Compare (Nkind (N)) is | |
2363 | when N_Op_Eq => Ent := RE_Big_EQ; | |
2364 | when N_Op_Ge => Ent := RE_Big_GE; | |
2365 | when N_Op_Gt => Ent := RE_Big_GT; | |
2366 | when N_Op_Le => Ent := RE_Big_LE; | |
2367 | when N_Op_Lt => Ent := RE_Big_LT; | |
2368 | when N_Op_Ne => Ent := RE_Big_NE; | |
2369 | end case; | |
2370 | ||
71fb4dc8 | 2371 | -- Insert assignment to Bnn into the bignum block |
456cbfa5 AC |
2372 | |
2373 | Insert_Before | |
2374 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
2375 | Make_Assignment_Statement (Loc, | |
2376 | Name => New_Occurrence_Of (Bnn, Loc), | |
2377 | Expression => | |
2378 | Make_Function_Call (Loc, | |
2379 | Name => | |
2380 | New_Occurrence_Of (RTE (Ent), Loc), | |
2381 | Parameter_Associations => New_List (Left, Right)))); | |
2382 | ||
71fb4dc8 AC |
2383 | -- Now do the rewrite with expression actions |
2384 | ||
2385 | Rewrite (N, | |
2386 | Make_Expression_With_Actions (Loc, | |
2387 | Actions => New_List ( | |
2388 | Make_Object_Declaration (Loc, | |
2389 | Defining_Identifier => Bnn, | |
2390 | Object_Definition => | |
2391 | New_Occurrence_Of (Result_Type, Loc)), | |
2392 | Blk), | |
2393 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
2394 | Analyze_And_Resolve (N, Result_Type); | |
456cbfa5 AC |
2395 | end; |
2396 | end; | |
2397 | ||
2398 | -- No bignums involved, but types are different, so we must have | |
2399 | -- rewritten one of the operands as a Long_Long_Integer but not | |
2400 | -- the other one. | |
2401 | ||
2402 | -- If left operand is Long_Long_Integer, convert right operand | |
2403 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2404 | ||
2405 | elsif Ltype = LLIB then | |
2406 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
2407 | Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks); | |
2408 | return; | |
2409 | ||
2410 | -- If right operand is Long_Long_Integer, convert left operand | |
2411 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2412 | ||
2413 | -- This is the only remaining possibility | |
2414 | ||
2415 | else pragma Assert (Rtype = LLIB); | |
2416 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
2417 | Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks); | |
2418 | return; | |
2419 | end if; | |
2420 | end; | |
2421 | end Expand_Compare_Minimize_Eliminate_Overflow; | |
2422 | ||
70482933 RK |
2423 | ------------------------------- |
2424 | -- Expand_Composite_Equality -- | |
2425 | ------------------------------- | |
2426 | ||
2427 | -- This function is only called for comparing internal fields of composite | |
2428 | -- types when these fields are themselves composites. This is a special | |
2429 | -- case because it is not possible to respect normal Ada visibility rules. | |
2430 | ||
2431 | function Expand_Composite_Equality | |
2432 | (Nod : Node_Id; | |
2433 | Typ : Entity_Id; | |
2434 | Lhs : Node_Id; | |
2435 | Rhs : Node_Id; | |
2e071734 | 2436 | Bodies : List_Id) return Node_Id |
70482933 RK |
2437 | is |
2438 | Loc : constant Source_Ptr := Sloc (Nod); | |
2439 | Full_Type : Entity_Id; | |
70482933 RK |
2440 | Eq_Op : Entity_Id; |
2441 | ||
7efc3f2d AC |
2442 | -- Start of processing for Expand_Composite_Equality |
2443 | ||
70482933 RK |
2444 | begin |
2445 | if Is_Private_Type (Typ) then | |
2446 | Full_Type := Underlying_Type (Typ); | |
2447 | else | |
2448 | Full_Type := Typ; | |
2449 | end if; | |
2450 | ||
ced8450b ES |
2451 | -- If the private type has no completion the context may be the |
2452 | -- expansion of a composite equality for a composite type with some | |
2453 | -- still incomplete components. The expression will not be analyzed | |
2454 | -- until the enclosing type is completed, at which point this will be | |
2455 | -- properly expanded, unless there is a bona fide completion error. | |
70482933 RK |
2456 | |
2457 | if No (Full_Type) then | |
ced8450b | 2458 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 RK |
2459 | end if; |
2460 | ||
2461 | Full_Type := Base_Type (Full_Type); | |
2462 | ||
da1b76c1 HK |
2463 | -- When the base type itself is private, use the full view to expand |
2464 | -- the composite equality. | |
2465 | ||
2466 | if Is_Private_Type (Full_Type) then | |
2467 | Full_Type := Underlying_Type (Full_Type); | |
2468 | end if; | |
2469 | ||
16788d44 RD |
2470 | -- Case of array types |
2471 | ||
70482933 RK |
2472 | if Is_Array_Type (Full_Type) then |
2473 | ||
2474 | -- If the operand is an elementary type other than a floating-point | |
2475 | -- type, then we can simply use the built-in block bitwise equality, | |
2476 | -- since the predefined equality operators always apply and bitwise | |
2477 | -- equality is fine for all these cases. | |
2478 | ||
2479 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
2480 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
2481 | then | |
39ade2f9 | 2482 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 | 2483 | |
685094bf RD |
2484 | -- For composite component types, and floating-point types, use the |
2485 | -- expansion. This deals with tagged component types (where we use | |
0c386027 | 2486 | -- the applicable equality routine) and floating-point (where we |
685094bf RD |
2487 | -- need to worry about negative zeroes), and also the case of any |
2488 | -- composite type recursively containing such fields. | |
70482933 RK |
2489 | |
2490 | else | |
0c386027 EB |
2491 | declare |
2492 | Comp_Typ : Entity_Id; | |
f537fc00 | 2493 | Hi : Node_Id; |
bcad5029 EB |
2494 | Indx : Node_Id; |
2495 | Ityp : Entity_Id; | |
2496 | Lo : Node_Id; | |
0c386027 EB |
2497 | |
2498 | begin | |
2499 | -- Do the comparison in the type (or its full view) and not in | |
2500 | -- its unconstrained base type, because the latter operation is | |
2501 | -- more complex and would also require an unchecked conversion. | |
2502 | ||
2503 | if Is_Private_Type (Typ) then | |
2504 | Comp_Typ := Underlying_Type (Typ); | |
2505 | else | |
2506 | Comp_Typ := Typ; | |
2507 | end if; | |
2508 | ||
2509 | -- Except for the case where the bounds of the type depend on a | |
2510 | -- discriminant, or else we would run into scoping issues. | |
2511 | ||
bcad5029 EB |
2512 | Indx := First_Index (Comp_Typ); |
2513 | while Present (Indx) loop | |
2514 | Ityp := Etype (Indx); | |
2515 | ||
2516 | Lo := Type_Low_Bound (Ityp); | |
2517 | Hi := Type_High_Bound (Ityp); | |
2518 | ||
2519 | if (Nkind (Lo) = N_Identifier | |
2520 | and then Ekind (Entity (Lo)) = E_Discriminant) | |
2521 | or else | |
2522 | (Nkind (Hi) = N_Identifier | |
2523 | and then Ekind (Entity (Hi)) = E_Discriminant) | |
2524 | then | |
2525 | Comp_Typ := Full_Type; | |
2526 | exit; | |
2527 | end if; | |
2528 | ||
2529 | Next_Index (Indx); | |
2530 | end loop; | |
0c386027 EB |
2531 | |
2532 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Comp_Typ); | |
2533 | end; | |
70482933 RK |
2534 | end if; |
2535 | ||
16788d44 RD |
2536 | -- Case of tagged record types |
2537 | ||
70482933 | 2538 | elsif Is_Tagged_Type (Full_Type) then |
59f7c716 JM |
2539 | Eq_Op := Find_Primitive_Eq (Typ); |
2540 | pragma Assert (Present (Eq_Op)); | |
70482933 RK |
2541 | |
2542 | return | |
2543 | Make_Function_Call (Loc, | |
e4494292 | 2544 | Name => New_Occurrence_Of (Eq_Op, Loc), |
70482933 RK |
2545 | Parameter_Associations => |
2546 | New_List | |
2547 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2548 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2549 | ||
16788d44 RD |
2550 | -- Case of untagged record types |
2551 | ||
70482933 | 2552 | elsif Is_Record_Type (Full_Type) then |
fbf5a39b | 2553 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2554 | |
2555 | if Present (Eq_Op) then | |
2556 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2557 | ||
685094bf RD |
2558 | -- Inherited equality from parent type. Convert the actuals to |
2559 | -- match signature of operation. | |
70482933 RK |
2560 | |
2561 | declare | |
fbf5a39b | 2562 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2563 | |
2564 | begin | |
2565 | return | |
2566 | Make_Function_Call (Loc, | |
e4494292 | 2567 | Name => New_Occurrence_Of (Eq_Op, Loc), |
39ade2f9 AC |
2568 | Parameter_Associations => New_List ( |
2569 | OK_Convert_To (T, Lhs), | |
2570 | OK_Convert_To (T, Rhs))); | |
70482933 RK |
2571 | end; |
2572 | ||
2573 | else | |
5d09245e AC |
2574 | -- Comparison between Unchecked_Union components |
2575 | ||
2576 | if Is_Unchecked_Union (Full_Type) then | |
2577 | declare | |
2578 | Lhs_Type : Node_Id := Full_Type; | |
2579 | Rhs_Type : Node_Id := Full_Type; | |
2580 | Lhs_Discr_Val : Node_Id; | |
2581 | Rhs_Discr_Val : Node_Id; | |
2582 | ||
2583 | begin | |
2584 | -- Lhs subtype | |
2585 | ||
2586 | if Nkind (Lhs) = N_Selected_Component then | |
2587 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2588 | end if; | |
2589 | ||
2590 | -- Rhs subtype | |
2591 | ||
2592 | if Nkind (Rhs) = N_Selected_Component then | |
2593 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2594 | end if; | |
2595 | ||
2596 | -- Lhs of the composite equality | |
2597 | ||
2598 | if Is_Constrained (Lhs_Type) then | |
2599 | ||
685094bf | 2600 | -- Since the enclosing record type can never be an |
5d09245e AC |
2601 | -- Unchecked_Union (this code is executed for records |
2602 | -- that do not have variants), we may reference its | |
2603 | -- discriminant(s). | |
2604 | ||
2605 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
2606 | and then Has_Per_Object_Constraint |
2607 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
2608 | then |
2609 | Lhs_Discr_Val := | |
2610 | Make_Selected_Component (Loc, | |
39ade2f9 | 2611 | Prefix => Prefix (Lhs), |
5d09245e | 2612 | Selector_Name => |
39ade2f9 AC |
2613 | New_Copy |
2614 | (Get_Discriminant_Value | |
2615 | (First_Discriminant (Lhs_Type), | |
2616 | Lhs_Type, | |
2617 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
2618 | |
2619 | else | |
39ade2f9 AC |
2620 | Lhs_Discr_Val := |
2621 | New_Copy | |
2622 | (Get_Discriminant_Value | |
2623 | (First_Discriminant (Lhs_Type), | |
2624 | Lhs_Type, | |
2625 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
2626 | |
2627 | end if; | |
2628 | else | |
2629 | -- It is not possible to infer the discriminant since | |
2630 | -- the subtype is not constrained. | |
2631 | ||
8aceda64 | 2632 | return |
5d09245e | 2633 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2634 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2635 | end if; |
2636 | ||
2637 | -- Rhs of the composite equality | |
2638 | ||
2639 | if Is_Constrained (Rhs_Type) then | |
2640 | if Nkind (Rhs) = N_Selected_Component | |
39ade2f9 AC |
2641 | and then Has_Per_Object_Constraint |
2642 | (Entity (Selector_Name (Rhs))) | |
5d09245e AC |
2643 | then |
2644 | Rhs_Discr_Val := | |
2645 | Make_Selected_Component (Loc, | |
39ade2f9 | 2646 | Prefix => Prefix (Rhs), |
5d09245e | 2647 | Selector_Name => |
39ade2f9 AC |
2648 | New_Copy |
2649 | (Get_Discriminant_Value | |
2650 | (First_Discriminant (Rhs_Type), | |
2651 | Rhs_Type, | |
2652 | Stored_Constraint (Rhs_Type)))); | |
5d09245e AC |
2653 | |
2654 | else | |
39ade2f9 AC |
2655 | Rhs_Discr_Val := |
2656 | New_Copy | |
2657 | (Get_Discriminant_Value | |
2658 | (First_Discriminant (Rhs_Type), | |
2659 | Rhs_Type, | |
2660 | Stored_Constraint (Rhs_Type))); | |
5d09245e AC |
2661 | |
2662 | end if; | |
2663 | else | |
8aceda64 | 2664 | return |
5d09245e | 2665 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2666 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2667 | end if; |
2668 | ||
2669 | -- Call the TSS equality function with the inferred | |
2670 | -- discriminant values. | |
2671 | ||
2672 | return | |
2673 | Make_Function_Call (Loc, | |
e4494292 | 2674 | Name => New_Occurrence_Of (Eq_Op, Loc), |
5d09245e AC |
2675 | Parameter_Associations => New_List ( |
2676 | Lhs, | |
2677 | Rhs, | |
2678 | Lhs_Discr_Val, | |
2679 | Rhs_Discr_Val)); | |
2680 | end; | |
d151d6a3 | 2681 | |
316e3a13 RD |
2682 | -- All cases other than comparing Unchecked_Union types |
2683 | ||
d151d6a3 | 2684 | else |
7f1a5156 EB |
2685 | declare |
2686 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
7f1a5156 EB |
2687 | begin |
2688 | return | |
2689 | Make_Function_Call (Loc, | |
316e3a13 RD |
2690 | Name => |
2691 | New_Occurrence_Of (Eq_Op, Loc), | |
7f1a5156 EB |
2692 | Parameter_Associations => New_List ( |
2693 | OK_Convert_To (T, Lhs), | |
2694 | OK_Convert_To (T, Rhs))); | |
2695 | end; | |
5d09245e | 2696 | end if; |
d151d6a3 | 2697 | end if; |
5d09245e | 2698 | |
3058f181 BD |
2699 | -- Equality composes in Ada 2012 for untagged record types. It also |
2700 | -- composes for bounded strings, because they are part of the | |
2701 | -- predefined environment. We could make it compose for bounded | |
2702 | -- strings by making them tagged, or by making sure all subcomponents | |
2703 | -- are set to the same value, even when not used. Instead, we have | |
2704 | -- this special case in the compiler, because it's more efficient. | |
2705 | ||
2706 | elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then | |
5d09245e | 2707 | |
08daa782 | 2708 | -- If no TSS has been created for the type, check whether there is |
7efc3f2d | 2709 | -- a primitive equality declared for it. |
d151d6a3 AC |
2710 | |
2711 | declare | |
bdbb2a40 | 2712 | Op : constant Node_Id := Build_Eq_Call (Typ, Loc, Lhs, Rhs); |
d151d6a3 AC |
2713 | |
2714 | begin | |
a1fc903a AC |
2715 | -- Use user-defined primitive if it exists, otherwise use |
2716 | -- predefined equality. | |
2717 | ||
3058f181 BD |
2718 | if Present (Op) then |
2719 | return Op; | |
7efc3f2d | 2720 | else |
7efc3f2d AC |
2721 | return Make_Op_Eq (Loc, Lhs, Rhs); |
2722 | end if; | |
d151d6a3 AC |
2723 | end; |
2724 | ||
70482933 RK |
2725 | else |
2726 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2727 | end if; | |
2728 | ||
16788d44 | 2729 | -- Non-composite types (always use predefined equality) |
70482933 | 2730 | |
16788d44 | 2731 | else |
70482933 RK |
2732 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
2733 | end if; | |
2734 | end Expand_Composite_Equality; | |
2735 | ||
fdac1f80 AC |
2736 | ------------------------ |
2737 | -- Expand_Concatenate -- | |
2738 | ------------------------ | |
70482933 | 2739 | |
fdac1f80 AC |
2740 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2741 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2742 | |
fdac1f80 AC |
2743 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2744 | -- Result type of concatenation | |
70482933 | 2745 | |
fdac1f80 AC |
2746 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2747 | -- Component type. Elements of this component type can appear as one | |
2748 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2749 | |
ecc4ddde AC |
2750 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2751 | -- Index subtype | |
2752 | ||
2753 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2754 | -- Index type. This is the base type of the index subtype, and is used | |
2755 | -- for all computed bounds (which may be out of range of Istyp in the | |
2756 | -- case of null ranges). | |
70482933 | 2757 | |
46ff89f3 | 2758 | Artyp : Entity_Id; |
fdac1f80 AC |
2759 | -- This is the type we use to do arithmetic to compute the bounds and |
2760 | -- lengths of operands. The choice of this type is a little subtle and | |
2761 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2762 | |
fdac1f80 AC |
2763 | Concatenation_Error : exception; |
2764 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2765 | |
0ac73189 AC |
2766 | Result_May_Be_Null : Boolean := True; |
2767 | -- Reset to False if at least one operand is encountered which is known | |
2768 | -- at compile time to be non-null. Used for handling the special case | |
2769 | -- of setting the high bound to the last operand high bound for a null | |
2770 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2771 | ||
df46b832 | 2772 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2773 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2774 | |
2775 | NN : Nat := 0; | |
a29262fd AC |
2776 | -- Number of operands excluding any known to be null, except that the |
2777 | -- last operand is always retained, in case it provides the bounds for | |
2778 | -- a null result. | |
2779 | ||
a6d25cad | 2780 | Opnd : Node_Id := Empty; |
a29262fd AC |
2781 | -- Current operand being processed in the loop through operands. After |
2782 | -- this loop is complete, always contains the last operand (which is not | |
2783 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2784 | |
2785 | -- Arrays describing the operands, only the first NN entries of each | |
2786 | -- array are set (NN < N when we exclude known null operands). | |
2787 | ||
2788 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2789 | -- True if length of corresponding operand known at compile time | |
2790 | ||
2791 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2792 | -- Set to the corresponding entry in the Opnds list (but note that null |
2793 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2794 | |
2795 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2796 | -- Set to length of operand. Entries in this array are set only if the |
2797 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2798 | |
0ac73189 AC |
2799 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2800 | -- Set to lower bound of operand. Either an integer literal in the case | |
2801 | -- where the bound is known at compile time, else actual lower bound. | |
2802 | -- The operand low bound is of type Ityp. | |
2803 | ||
df46b832 AC |
2804 | Var_Length : array (1 .. N) of Entity_Id; |
2805 | -- Set to an entity of type Natural that contains the length of an | |
2806 | -- operand whose length is not known at compile time. Entries in this | |
2807 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2808 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2809 | |
2810 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2811 | -- The J'th entry in an expression node that represents the total length |
2812 | -- of operands 1 through J. It is either an integer literal node, or a | |
2813 | -- reference to a constant entity with the right value, so it is fine | |
31fde973 | 2814 | -- to just do a Copy_Node to get an appropriate copy. The extra zeroth |
46ff89f3 | 2815 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 | 2816 | |
a6b13d32 | 2817 | Low_Bound : Node_Id := Empty; |
0ac73189 AC |
2818 | -- A tree node representing the low bound of the result (of type Ityp). |
2819 | -- This is either an integer literal node, or an identifier reference to | |
2820 | -- a constant entity initialized to the appropriate value. | |
2821 | ||
a6d25cad | 2822 | Last_Opnd_Low_Bound : Node_Id := Empty; |
88a27b18 AC |
2823 | -- A tree node representing the low bound of the last operand. This |
2824 | -- need only be set if the result could be null. It is used for the | |
2825 | -- special case of setting the right low bound for a null result. | |
2826 | -- This is of type Ityp. | |
2827 | ||
a6d25cad | 2828 | Last_Opnd_High_Bound : Node_Id := Empty; |
a29262fd AC |
2829 | -- A tree node representing the high bound of the last operand. This |
2830 | -- need only be set if the result could be null. It is used for the | |
2831 | -- special case of setting the right high bound for a null result. | |
2832 | -- This is of type Ityp. | |
2833 | ||
dcd5fd67 | 2834 | High_Bound : Node_Id := Empty; |
0ac73189 | 2835 | -- A tree node representing the high bound of the result (of type Ityp) |
df46b832 | 2836 | |
a6b13d32 | 2837 | Result : Node_Id := Empty; |
0ac73189 | 2838 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2839 | |
d0f8d157 | 2840 | Actions : constant List_Id := New_List; |
4c9fe6c7 | 2841 | -- Collect actions to be inserted |
d0f8d157 | 2842 | |
fa969310 | 2843 | Known_Non_Null_Operand_Seen : Boolean; |
308e6f3a | 2844 | -- Set True during generation of the assignments of operands into |
fa969310 AC |
2845 | -- result once an operand known to be non-null has been seen. |
2846 | ||
2df23f66 AC |
2847 | function Library_Level_Target return Boolean; |
2848 | -- Return True if the concatenation is within the expression of the | |
2849 | -- declaration of a library-level object. | |
2850 | ||
fa969310 AC |
2851 | function Make_Artyp_Literal (Val : Nat) return Node_Id; |
2852 | -- This function makes an N_Integer_Literal node that is returned in | |
2853 | -- analyzed form with the type set to Artyp. Importantly this literal | |
2854 | -- is not flagged as static, so that if we do computations with it that | |
2855 | -- result in statically detected out of range conditions, we will not | |
2856 | -- generate error messages but instead warning messages. | |
2857 | ||
46ff89f3 | 2858 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 2859 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
2860 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
2861 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
2862 | |
2863 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 2864 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 2865 | |
2df23f66 AC |
2866 | -------------------------- |
2867 | -- Library_Level_Target -- | |
2868 | -------------------------- | |
2869 | ||
2870 | function Library_Level_Target return Boolean is | |
2871 | P : Node_Id := Parent (Cnode); | |
2872 | ||
2873 | begin | |
2874 | while Present (P) loop | |
2875 | if Nkind (P) = N_Object_Declaration then | |
2876 | return Is_Library_Level_Entity (Defining_Identifier (P)); | |
2877 | ||
2878 | -- Prevent the search from going too far | |
2879 | ||
2880 | elsif Is_Body_Or_Package_Declaration (P) then | |
2881 | return False; | |
2882 | end if; | |
2883 | ||
2884 | P := Parent (P); | |
2885 | end loop; | |
2886 | ||
2887 | return False; | |
2888 | end Library_Level_Target; | |
2889 | ||
fa969310 AC |
2890 | ------------------------ |
2891 | -- Make_Artyp_Literal -- | |
2892 | ------------------------ | |
2893 | ||
2894 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
2895 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
2896 | begin | |
2897 | Set_Etype (Result, Artyp); | |
2898 | Set_Analyzed (Result, True); | |
2899 | Set_Is_Static_Expression (Result, False); | |
2900 | return Result; | |
2901 | end Make_Artyp_Literal; | |
76c597a1 | 2902 | |
fdac1f80 | 2903 | -------------- |
46ff89f3 | 2904 | -- To_Artyp -- |
fdac1f80 AC |
2905 | -------------- |
2906 | ||
46ff89f3 | 2907 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 2908 | begin |
46ff89f3 | 2909 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
2910 | return X; |
2911 | ||
2912 | elsif Is_Enumeration_Type (Ityp) then | |
2913 | return | |
2914 | Make_Attribute_Reference (Loc, | |
2915 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2916 | Attribute_Name => Name_Pos, | |
2917 | Expressions => New_List (X)); | |
2918 | ||
2919 | else | |
46ff89f3 | 2920 | return Convert_To (Artyp, X); |
fdac1f80 | 2921 | end if; |
46ff89f3 | 2922 | end To_Artyp; |
fdac1f80 AC |
2923 | |
2924 | ------------- | |
2925 | -- To_Ityp -- | |
2926 | ------------- | |
2927 | ||
2928 | function To_Ityp (X : Node_Id) return Node_Id is | |
2929 | begin | |
2fc05e3d | 2930 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
2931 | return |
2932 | Make_Attribute_Reference (Loc, | |
2933 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2934 | Attribute_Name => Name_Val, | |
2935 | Expressions => New_List (X)); | |
2936 | ||
2937 | -- Case where we will do a type conversion | |
2938 | ||
2939 | else | |
76c597a1 AC |
2940 | if Ityp = Base_Type (Artyp) then |
2941 | return X; | |
fdac1f80 | 2942 | else |
76c597a1 | 2943 | return Convert_To (Ityp, X); |
fdac1f80 AC |
2944 | end if; |
2945 | end if; | |
2946 | end To_Ityp; | |
2947 | ||
2948 | -- Local Declarations | |
2949 | ||
0ac73189 AC |
2950 | Opnd_Typ : Entity_Id; |
2951 | Ent : Entity_Id; | |
2952 | Len : Uint; | |
2953 | J : Nat; | |
2954 | Clen : Node_Id; | |
2955 | Set : Boolean; | |
70482933 | 2956 | |
f46faa08 AC |
2957 | -- Start of processing for Expand_Concatenate |
2958 | ||
70482933 | 2959 | begin |
fdac1f80 AC |
2960 | -- Choose an appropriate computational type |
2961 | ||
2962 | -- We will be doing calculations of lengths and bounds in this routine | |
2963 | -- and computing one from the other in some cases, e.g. getting the high | |
2964 | -- bound by adding the length-1 to the low bound. | |
2965 | ||
2966 | -- We can't just use the index type, or even its base type for this | |
2967 | -- purpose for two reasons. First it might be an enumeration type which | |
308e6f3a RW |
2968 | -- is not suitable for computations of any kind, and second it may |
2969 | -- simply not have enough range. For example if the index type is | |
2970 | -- -128..+127 then lengths can be up to 256, which is out of range of | |
2971 | -- the type. | |
fdac1f80 AC |
2972 | |
2973 | -- For enumeration types, we can simply use Standard_Integer, this is | |
2974 | -- sufficient since the actual number of enumeration literals cannot | |
2975 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 2976 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
2977 | |
2978 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 2979 | Artyp := Standard_Integer; |
fdac1f80 | 2980 | |
59262ebb AC |
2981 | -- If index type is Positive, we use the standard unsigned type, to give |
2982 | -- more room on the top of the range, obviating the need for an overflow | |
2983 | -- check when creating the upper bound. This is needed to avoid junk | |
2984 | -- overflow checks in the common case of String types. | |
2985 | ||
2986 | -- ??? Disabled for now | |
2987 | ||
2988 | -- elsif Istyp = Standard_Positive then | |
2989 | -- Artyp := Standard_Unsigned; | |
2990 | ||
2fc05e3d AC |
2991 | -- For modular types, we use a 32-bit modular type for types whose size |
2992 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
2993 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 2994 | |
2fc05e3d | 2995 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 2996 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 2997 | Artyp := Standard_Unsigned; |
ecc4ddde | 2998 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 2999 | Artyp := Ityp; |
fdac1f80 | 3000 | else |
46ff89f3 | 3001 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
3002 | end if; |
3003 | ||
2fc05e3d | 3004 | -- Similar treatment for signed types |
fdac1f80 AC |
3005 | |
3006 | else | |
ecc4ddde | 3007 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 3008 | Artyp := Standard_Integer; |
ecc4ddde | 3009 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 3010 | Artyp := Ityp; |
fdac1f80 | 3011 | else |
46ff89f3 | 3012 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
3013 | end if; |
3014 | end if; | |
3015 | ||
fa969310 AC |
3016 | -- Supply dummy entry at start of length array |
3017 | ||
3018 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
3019 | ||
fdac1f80 | 3020 | -- Go through operands setting up the above arrays |
70482933 | 3021 | |
df46b832 AC |
3022 | J := 1; |
3023 | while J <= N loop | |
3024 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 3025 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
3026 | |
3027 | -- The parent got messed up when we put the operands in a list, | |
d347f572 AC |
3028 | -- so now put back the proper parent for the saved operand, that |
3029 | -- is to say the concatenation node, to make sure that each operand | |
3030 | -- is seen as a subexpression, e.g. if actions must be inserted. | |
fdac1f80 | 3031 | |
d347f572 | 3032 | Set_Parent (Opnd, Cnode); |
fdac1f80 AC |
3033 | |
3034 | -- Set will be True when we have setup one entry in the array | |
3035 | ||
df46b832 AC |
3036 | Set := False; |
3037 | ||
fdac1f80 | 3038 | -- Singleton element (or character literal) case |
df46b832 | 3039 | |
0ac73189 | 3040 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
3041 | NN := NN + 1; |
3042 | Operands (NN) := Opnd; | |
3043 | Is_Fixed_Length (NN) := True; | |
3044 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 3045 | Result_May_Be_Null := False; |
fdac1f80 | 3046 | |
a29262fd AC |
3047 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
3048 | -- since we know that the result cannot be null). | |
fdac1f80 | 3049 | |
0ac73189 AC |
3050 | Opnd_Low_Bound (NN) := |
3051 | Make_Attribute_Reference (Loc, | |
e4494292 | 3052 | Prefix => New_Occurrence_Of (Istyp, Loc), |
0ac73189 AC |
3053 | Attribute_Name => Name_First); |
3054 | ||
df46b832 AC |
3055 | Set := True; |
3056 | ||
fdac1f80 | 3057 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
3058 | |
3059 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 3060 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 3061 | |
a29262fd AC |
3062 | if Len /= 0 then |
3063 | Result_May_Be_Null := False; | |
3064 | end if; | |
3065 | ||
88a27b18 | 3066 | -- Capture last operand low and high bound if result could be null |
a29262fd AC |
3067 | |
3068 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3069 | Last_Opnd_Low_Bound := |
3070 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3071 | ||
a29262fd | 3072 | Last_Opnd_High_Bound := |
88a27b18 | 3073 | Make_Op_Subtract (Loc, |
a29262fd AC |
3074 | Left_Opnd => |
3075 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 3076 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
3077 | end if; |
3078 | ||
3079 | -- Skip null string literal | |
fdac1f80 | 3080 | |
0ac73189 | 3081 | if J < N and then Len = 0 then |
df46b832 AC |
3082 | goto Continue; |
3083 | end if; | |
3084 | ||
3085 | NN := NN + 1; | |
3086 | Operands (NN) := Opnd; | |
3087 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
3088 | |
3089 | -- Set length and bounds | |
3090 | ||
df46b832 | 3091 | Fixed_Length (NN) := Len; |
0ac73189 AC |
3092 | |
3093 | Opnd_Low_Bound (NN) := | |
3094 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3095 | ||
df46b832 AC |
3096 | Set := True; |
3097 | ||
3098 | -- All other cases | |
3099 | ||
3100 | else | |
3101 | -- Check constrained case with known bounds | |
3102 | ||
0ac73189 | 3103 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 3104 | declare |
df46b832 AC |
3105 | Index : constant Node_Id := First_Index (Opnd_Typ); |
3106 | Indx_Typ : constant Entity_Id := Etype (Index); | |
3107 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
3108 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
3109 | ||
3110 | begin | |
fdac1f80 AC |
3111 | -- Fixed length constrained array type with known at compile |
3112 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
3113 | |
3114 | if Compile_Time_Known_Value (Lo) | |
3115 | and then | |
3116 | Compile_Time_Known_Value (Hi) | |
3117 | then | |
3118 | declare | |
3119 | Loval : constant Uint := Expr_Value (Lo); | |
3120 | Hival : constant Uint := Expr_Value (Hi); | |
3121 | Len : constant Uint := | |
3122 | UI_Max (Hival - Loval + 1, Uint_0); | |
3123 | ||
3124 | begin | |
0ac73189 AC |
3125 | if Len > 0 then |
3126 | Result_May_Be_Null := False; | |
df46b832 | 3127 | end if; |
0ac73189 | 3128 | |
88a27b18 | 3129 | -- Capture last operand bounds if result could be null |
a29262fd AC |
3130 | |
3131 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3132 | Last_Opnd_Low_Bound := |
3133 | Convert_To (Ityp, | |
3134 | Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
3135 | ||
a29262fd AC |
3136 | Last_Opnd_High_Bound := |
3137 | Convert_To (Ityp, | |
39ade2f9 | 3138 | Make_Integer_Literal (Loc, Expr_Value (Hi))); |
a29262fd AC |
3139 | end if; |
3140 | ||
3141 | -- Exclude null length case unless last operand | |
0ac73189 | 3142 | |
a29262fd | 3143 | if J < N and then Len = 0 then |
0ac73189 AC |
3144 | goto Continue; |
3145 | end if; | |
3146 | ||
3147 | NN := NN + 1; | |
3148 | Operands (NN) := Opnd; | |
3149 | Is_Fixed_Length (NN) := True; | |
3150 | Fixed_Length (NN) := Len; | |
3151 | ||
39ade2f9 AC |
3152 | Opnd_Low_Bound (NN) := |
3153 | To_Ityp | |
3154 | (Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
0ac73189 | 3155 | Set := True; |
df46b832 AC |
3156 | end; |
3157 | end if; | |
3158 | end; | |
3159 | end if; | |
3160 | ||
0ac73189 AC |
3161 | -- All cases where the length is not known at compile time, or the |
3162 | -- special case of an operand which is known to be null but has a | |
3163 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
3164 | |
3165 | if not Set then | |
3166 | NN := NN + 1; | |
0ac73189 AC |
3167 | |
3168 | -- Capture operand bounds | |
3169 | ||
3170 | Opnd_Low_Bound (NN) := | |
3171 | Make_Attribute_Reference (Loc, | |
3172 | Prefix => | |
3173 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3174 | Attribute_Name => Name_First); | |
3175 | ||
88a27b18 AC |
3176 | -- Capture last operand bounds if result could be null |
3177 | ||
a29262fd | 3178 | if J = N and Result_May_Be_Null then |
88a27b18 AC |
3179 | Last_Opnd_Low_Bound := |
3180 | Convert_To (Ityp, | |
3181 | Make_Attribute_Reference (Loc, | |
3182 | Prefix => | |
3183 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3184 | Attribute_Name => Name_First)); | |
3185 | ||
a29262fd AC |
3186 | Last_Opnd_High_Bound := |
3187 | Convert_To (Ityp, | |
3188 | Make_Attribute_Reference (Loc, | |
3189 | Prefix => | |
3190 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3191 | Attribute_Name => Name_Last)); | |
3192 | end if; | |
0ac73189 AC |
3193 | |
3194 | -- Capture length of operand in entity | |
3195 | ||
df46b832 AC |
3196 | Operands (NN) := Opnd; |
3197 | Is_Fixed_Length (NN) := False; | |
3198 | ||
191fcb3a | 3199 | Var_Length (NN) := Make_Temporary (Loc, 'L'); |
df46b832 | 3200 | |
d0f8d157 | 3201 | Append_To (Actions, |
df46b832 AC |
3202 | Make_Object_Declaration (Loc, |
3203 | Defining_Identifier => Var_Length (NN), | |
3204 | Constant_Present => True, | |
39ade2f9 | 3205 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3206 | Expression => |
3207 | Make_Attribute_Reference (Loc, | |
3208 | Prefix => | |
3209 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
d0f8d157 | 3210 | Attribute_Name => Name_Length))); |
df46b832 AC |
3211 | end if; |
3212 | end if; | |
3213 | ||
3214 | -- Set next entry in aggregate length array | |
3215 | ||
3216 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 3217 | -- or a reference to the saved length for variable length. |
df46b832 AC |
3218 | |
3219 | if NN = 1 then | |
3220 | if Is_Fixed_Length (1) then | |
39ade2f9 | 3221 | Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1)); |
df46b832 | 3222 | else |
e4494292 | 3223 | Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc); |
df46b832 AC |
3224 | end if; |
3225 | ||
3226 | -- If entry is fixed length and only fixed lengths so far, make | |
3227 | -- appropriate new integer literal adding new length. | |
3228 | ||
3229 | elsif Is_Fixed_Length (NN) | |
3230 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
3231 | then | |
3232 | Aggr_Length (NN) := | |
3233 | Make_Integer_Literal (Loc, | |
3234 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
3235 | ||
d0f8d157 AC |
3236 | -- All other cases, construct an addition node for the length and |
3237 | -- create an entity initialized to this length. | |
df46b832 AC |
3238 | |
3239 | else | |
191fcb3a | 3240 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 AC |
3241 | |
3242 | if Is_Fixed_Length (NN) then | |
3243 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
3244 | else | |
e4494292 | 3245 | Clen := New_Occurrence_Of (Var_Length (NN), Loc); |
df46b832 AC |
3246 | end if; |
3247 | ||
d0f8d157 | 3248 | Append_To (Actions, |
df46b832 AC |
3249 | Make_Object_Declaration (Loc, |
3250 | Defining_Identifier => Ent, | |
3251 | Constant_Present => True, | |
39ade2f9 | 3252 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3253 | Expression => |
3254 | Make_Op_Add (Loc, | |
683af98c | 3255 | Left_Opnd => New_Copy_Tree (Aggr_Length (NN - 1)), |
d0f8d157 | 3256 | Right_Opnd => Clen))); |
df46b832 | 3257 | |
76c597a1 | 3258 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
3259 | end if; |
3260 | ||
3261 | <<Continue>> | |
3262 | J := J + 1; | |
3263 | end loop; | |
3264 | ||
a29262fd | 3265 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
3266 | |
3267 | if NN = 0 then | |
a29262fd | 3268 | Result := Opnd; |
df46b832 AC |
3269 | goto Done; |
3270 | end if; | |
3271 | ||
3272 | -- If we have only one non-null operand, return it and we are done. | |
3273 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
3274 | -- the sole operand is of the element type, in which case it must be |
3275 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
3276 | -- through the normal general circuit. |
3277 | ||
533369aa | 3278 | if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then |
df46b832 AC |
3279 | Result := Operands (1); |
3280 | goto Done; | |
3281 | end if; | |
3282 | ||
3283 | -- Cases where we have a real concatenation | |
3284 | ||
fdac1f80 AC |
3285 | -- Next step is to find the low bound for the result array that we |
3286 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
3287 | ||
3288 | -- If the ultimate ancestor of the index subtype is a constrained array | |
3289 | -- definition, then the lower bound is that of the index subtype as | |
3290 | -- specified by (RM 4.5.3(6)). | |
3291 | ||
3292 | -- The right test here is to go to the root type, and then the ultimate | |
3293 | -- ancestor is the first subtype of this root type. | |
3294 | ||
3295 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 3296 | Low_Bound := |
fdac1f80 AC |
3297 | Make_Attribute_Reference (Loc, |
3298 | Prefix => | |
3299 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 3300 | Attribute_Name => Name_First); |
df46b832 AC |
3301 | |
3302 | -- If the first operand in the list has known length we know that | |
3303 | -- the lower bound of the result is the lower bound of this operand. | |
3304 | ||
fdac1f80 | 3305 | elsif Is_Fixed_Length (1) then |
0ac73189 | 3306 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
3307 | |
3308 | -- OK, we don't know the lower bound, we have to build a horrible | |
9b16cb57 | 3309 | -- if expression node of the form |
df46b832 AC |
3310 | |
3311 | -- if Cond1'Length /= 0 then | |
0ac73189 | 3312 | -- Opnd1 low bound |
df46b832 AC |
3313 | -- else |
3314 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 3315 | -- Opnd2 low bound |
df46b832 AC |
3316 | -- else |
3317 | -- ... | |
3318 | ||
3319 | -- The nesting ends either when we hit an operand whose length is known | |
3320 | -- at compile time, or on reaching the last operand, whose low bound we | |
3321 | -- take unconditionally whether or not it is null. It's easiest to do | |
3322 | -- this with a recursive procedure: | |
3323 | ||
3324 | else | |
3325 | declare | |
3326 | function Get_Known_Bound (J : Nat) return Node_Id; | |
3327 | -- Returns the lower bound determined by operands J .. NN | |
3328 | ||
3329 | --------------------- | |
3330 | -- Get_Known_Bound -- | |
3331 | --------------------- | |
3332 | ||
3333 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 3334 | begin |
0ac73189 | 3335 | if Is_Fixed_Length (J) or else J = NN then |
683af98c | 3336 | return New_Copy_Tree (Opnd_Low_Bound (J)); |
70482933 RK |
3337 | |
3338 | else | |
df46b832 | 3339 | return |
9b16cb57 | 3340 | Make_If_Expression (Loc, |
df46b832 AC |
3341 | Expressions => New_List ( |
3342 | ||
3343 | Make_Op_Ne (Loc, | |
e4494292 RD |
3344 | Left_Opnd => |
3345 | New_Occurrence_Of (Var_Length (J), Loc), | |
3346 | Right_Opnd => | |
3347 | Make_Integer_Literal (Loc, 0)), | |
df46b832 | 3348 | |
683af98c | 3349 | New_Copy_Tree (Opnd_Low_Bound (J)), |
df46b832 | 3350 | Get_Known_Bound (J + 1))); |
70482933 | 3351 | end if; |
df46b832 | 3352 | end Get_Known_Bound; |
70482933 | 3353 | |
df46b832 | 3354 | begin |
191fcb3a | 3355 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 | 3356 | |
d0f8d157 | 3357 | Append_To (Actions, |
df46b832 AC |
3358 | Make_Object_Declaration (Loc, |
3359 | Defining_Identifier => Ent, | |
3360 | Constant_Present => True, | |
0ac73189 | 3361 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
d0f8d157 | 3362 | Expression => Get_Known_Bound (1))); |
df46b832 | 3363 | |
e4494292 | 3364 | Low_Bound := New_Occurrence_Of (Ent, Loc); |
df46b832 AC |
3365 | end; |
3366 | end if; | |
70482933 | 3367 | |
a6b13d32 AC |
3368 | pragma Assert (Present (Low_Bound)); |
3369 | ||
76c597a1 AC |
3370 | -- Now we can safely compute the upper bound, normally |
3371 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
3372 | |
3373 | High_Bound := | |
cc6f5d75 AC |
3374 | To_Ityp |
3375 | (Make_Op_Add (Loc, | |
683af98c | 3376 | Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)), |
cc6f5d75 AC |
3377 | Right_Opnd => |
3378 | Make_Op_Subtract (Loc, | |
683af98c | 3379 | Left_Opnd => New_Copy_Tree (Aggr_Length (NN)), |
cc6f5d75 | 3380 | Right_Opnd => Make_Artyp_Literal (1)))); |
0ac73189 | 3381 | |
59262ebb | 3382 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
3383 | -- very weird cases, so in the general case we need an overflow check on |
3384 | -- the high bound. We can avoid this for the common case of string types | |
3385 | -- and other types whose index is Positive, since we chose a wider range | |
54740d7d AC |
3386 | -- for the arithmetic type. If checks are suppressed we do not set the |
3387 | -- flag, and possibly superfluous warnings will be omitted. | |
76c597a1 | 3388 | |
54740d7d AC |
3389 | if Istyp /= Standard_Positive |
3390 | and then not Overflow_Checks_Suppressed (Istyp) | |
3391 | then | |
59262ebb AC |
3392 | Activate_Overflow_Check (High_Bound); |
3393 | end if; | |
76c597a1 AC |
3394 | |
3395 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
3396 | -- case the bounds come from the last operand (so that we get the proper |
3397 | -- bounds if the last operand is super-flat). | |
3398 | ||
0ac73189 | 3399 | if Result_May_Be_Null then |
88a27b18 | 3400 | Low_Bound := |
9b16cb57 | 3401 | Make_If_Expression (Loc, |
88a27b18 AC |
3402 | Expressions => New_List ( |
3403 | Make_Op_Eq (Loc, | |
683af98c | 3404 | Left_Opnd => New_Copy_Tree (Aggr_Length (NN)), |
88a27b18 AC |
3405 | Right_Opnd => Make_Artyp_Literal (0)), |
3406 | Last_Opnd_Low_Bound, | |
3407 | Low_Bound)); | |
3408 | ||
0ac73189 | 3409 | High_Bound := |
9b16cb57 | 3410 | Make_If_Expression (Loc, |
0ac73189 AC |
3411 | Expressions => New_List ( |
3412 | Make_Op_Eq (Loc, | |
683af98c | 3413 | Left_Opnd => New_Copy_Tree (Aggr_Length (NN)), |
fa969310 | 3414 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 3415 | Last_Opnd_High_Bound, |
0ac73189 AC |
3416 | High_Bound)); |
3417 | end if; | |
3418 | ||
d0f8d157 AC |
3419 | -- Here is where we insert the saved up actions |
3420 | ||
3421 | Insert_Actions (Cnode, Actions, Suppress => All_Checks); | |
3422 | ||
602a7ec0 AC |
3423 | -- Now we construct an array object with appropriate bounds. We mark |
3424 | -- the target as internal to prevent useless initialization when | |
e526d0c7 AC |
3425 | -- Initialize_Scalars is enabled. Also since this is the actual result |
3426 | -- entity, we make sure we have debug information for the result. | |
70482933 | 3427 | |
191fcb3a | 3428 | Ent := Make_Temporary (Loc, 'S'); |
923ecd0e HK |
3429 | Set_Is_Internal (Ent); |
3430 | Set_Debug_Info_Needed (Ent); | |
70482933 | 3431 | |
76c597a1 | 3432 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
3433 | -- to abort, we want a warning and a runtime constraint error. Note that |
3434 | -- we have arranged that the result will not be treated as a static | |
3435 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 3436 | |
df46b832 AC |
3437 | Insert_Action (Cnode, |
3438 | Make_Object_Declaration (Loc, | |
3439 | Defining_Identifier => Ent, | |
df46b832 AC |
3440 | Object_Definition => |
3441 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 3442 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
3443 | Constraint => |
3444 | Make_Index_Or_Discriminant_Constraint (Loc, | |
3445 | Constraints => New_List ( | |
3446 | Make_Range (Loc, | |
0ac73189 AC |
3447 | Low_Bound => Low_Bound, |
3448 | High_Bound => High_Bound))))), | |
df46b832 AC |
3449 | Suppress => All_Checks); |
3450 | ||
d1f453b7 RD |
3451 | -- If the result of the concatenation appears as the initializing |
3452 | -- expression of an object declaration, we can just rename the | |
3453 | -- result, rather than copying it. | |
3454 | ||
3455 | Set_OK_To_Rename (Ent); | |
3456 | ||
76c597a1 AC |
3457 | -- Catch the static out of range case now |
3458 | ||
3459 | if Raises_Constraint_Error (High_Bound) then | |
3460 | raise Concatenation_Error; | |
3461 | end if; | |
3462 | ||
df46b832 AC |
3463 | -- Now we will generate the assignments to do the actual concatenation |
3464 | ||
bded454f RD |
3465 | -- There is one case in which we will not do this, namely when all the |
3466 | -- following conditions are met: | |
3467 | ||
3468 | -- The result type is Standard.String | |
3469 | ||
3470 | -- There are nine or fewer retained (non-null) operands | |
3471 | ||
2df23f66 AC |
3472 | -- The optimization level is -O0 or the debug flag gnatd.C is set, |
3473 | -- and the debug flag gnatd.c is not set. | |
bded454f RD |
3474 | |
3475 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
3476 | -- available in the run time. | |
3477 | ||
bded454f RD |
3478 | -- If all these conditions are met then we generate a call to the |
3479 | -- relevant concatenation routine. The purpose of this is to avoid | |
3480 | -- undesirable code bloat at -O0. | |
3481 | ||
2df23f66 AC |
3482 | -- If the concatenation is within the declaration of a library-level |
3483 | -- object, we call the built-in concatenation routines to prevent code | |
3484 | -- bloat, regardless of the optimization level. This is space efficient | |
3485 | -- and prevents linking problems when units are compiled with different | |
3486 | -- optimization levels. | |
3487 | ||
bded454f RD |
3488 | if Atyp = Standard_String |
3489 | and then NN in 2 .. 9 | |
2df23f66 AC |
3490 | and then (((Optimization_Level = 0 or else Debug_Flag_Dot_CC) |
3491 | and then not Debug_Flag_Dot_C) | |
3492 | or else Library_Level_Target) | |
bded454f RD |
3493 | then |
3494 | declare | |
3495 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
3496 | (RE_Str_Concat_2, | |
3497 | RE_Str_Concat_3, | |
3498 | RE_Str_Concat_4, | |
3499 | RE_Str_Concat_5, | |
3500 | RE_Str_Concat_6, | |
3501 | RE_Str_Concat_7, | |
3502 | RE_Str_Concat_8, | |
3503 | RE_Str_Concat_9); | |
3504 | ||
3505 | begin | |
3506 | if RTE_Available (RR (NN)) then | |
3507 | declare | |
3508 | Opnds : constant List_Id := | |
3509 | New_List (New_Occurrence_Of (Ent, Loc)); | |
3510 | ||
3511 | begin | |
3512 | for J in 1 .. NN loop | |
3513 | if Is_List_Member (Operands (J)) then | |
3514 | Remove (Operands (J)); | |
3515 | end if; | |
3516 | ||
3517 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
3518 | Append_To (Opnds, | |
3519 | Make_Aggregate (Loc, | |
3520 | Component_Associations => New_List ( | |
3521 | Make_Component_Association (Loc, | |
3522 | Choices => New_List ( | |
3523 | Make_Integer_Literal (Loc, 1)), | |
3524 | Expression => Operands (J))))); | |
3525 | ||
3526 | else | |
3527 | Append_To (Opnds, Operands (J)); | |
3528 | end if; | |
3529 | end loop; | |
3530 | ||
3531 | Insert_Action (Cnode, | |
3532 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 3533 | Name => New_Occurrence_Of (RTE (RR (NN)), Loc), |
bded454f RD |
3534 | Parameter_Associations => Opnds)); |
3535 | ||
e4494292 | 3536 | Result := New_Occurrence_Of (Ent, Loc); |
bded454f RD |
3537 | goto Done; |
3538 | end; | |
3539 | end if; | |
3540 | end; | |
3541 | end if; | |
3542 | ||
3543 | -- Not special case so generate the assignments | |
3544 | ||
76c597a1 AC |
3545 | Known_Non_Null_Operand_Seen := False; |
3546 | ||
df46b832 AC |
3547 | for J in 1 .. NN loop |
3548 | declare | |
3549 | Lo : constant Node_Id := | |
3550 | Make_Op_Add (Loc, | |
683af98c | 3551 | Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)), |
df46b832 AC |
3552 | Right_Opnd => Aggr_Length (J - 1)); |
3553 | ||
3554 | Hi : constant Node_Id := | |
3555 | Make_Op_Add (Loc, | |
683af98c | 3556 | Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)), |
df46b832 AC |
3557 | Right_Opnd => |
3558 | Make_Op_Subtract (Loc, | |
3559 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 3560 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 3561 | |
df46b832 | 3562 | begin |
fdac1f80 AC |
3563 | -- Singleton case, simple assignment |
3564 | ||
3565 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 3566 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
3567 | Insert_Action (Cnode, |
3568 | Make_Assignment_Statement (Loc, | |
3569 | Name => | |
3570 | Make_Indexed_Component (Loc, | |
3571 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 3572 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
3573 | Expression => Operands (J)), |
3574 | Suppress => All_Checks); | |
70482933 | 3575 | |
76c597a1 AC |
3576 | -- Array case, slice assignment, skipped when argument is fixed |
3577 | -- length and known to be null. | |
fdac1f80 | 3578 | |
76c597a1 AC |
3579 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
3580 | declare | |
3581 | Assign : Node_Id := | |
3582 | Make_Assignment_Statement (Loc, | |
3583 | Name => | |
3584 | Make_Slice (Loc, | |
3585 | Prefix => | |
3586 | New_Occurrence_Of (Ent, Loc), | |
3587 | Discrete_Range => | |
3588 | Make_Range (Loc, | |
3589 | Low_Bound => To_Ityp (Lo), | |
3590 | High_Bound => To_Ityp (Hi))), | |
3591 | Expression => Operands (J)); | |
3592 | begin | |
3593 | if Is_Fixed_Length (J) then | |
3594 | Known_Non_Null_Operand_Seen := True; | |
3595 | ||
3596 | elsif not Known_Non_Null_Operand_Seen then | |
3597 | ||
3598 | -- Here if operand length is not statically known and no | |
3599 | -- operand known to be non-null has been processed yet. | |
3600 | -- If operand length is 0, we do not need to perform the | |
3601 | -- assignment, and we must avoid the evaluation of the | |
3602 | -- high bound of the slice, since it may underflow if the | |
3603 | -- low bound is Ityp'First. | |
3604 | ||
3605 | Assign := | |
3606 | Make_Implicit_If_Statement (Cnode, | |
39ade2f9 | 3607 | Condition => |
76c597a1 | 3608 | Make_Op_Ne (Loc, |
39ade2f9 | 3609 | Left_Opnd => |
76c597a1 AC |
3610 | New_Occurrence_Of (Var_Length (J), Loc), |
3611 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
39ade2f9 | 3612 | Then_Statements => New_List (Assign)); |
76c597a1 | 3613 | end if; |
fa969310 | 3614 | |
76c597a1 AC |
3615 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
3616 | end; | |
df46b832 AC |
3617 | end if; |
3618 | end; | |
3619 | end loop; | |
70482933 | 3620 | |
0ac73189 AC |
3621 | -- Finally we build the result, which is a reference to the array object |
3622 | ||
e4494292 | 3623 | Result := New_Occurrence_Of (Ent, Loc); |
70482933 | 3624 | |
df46b832 | 3625 | <<Done>> |
a6b13d32 | 3626 | pragma Assert (Present (Result)); |
df46b832 | 3627 | Rewrite (Cnode, Result); |
fdac1f80 AC |
3628 | Analyze_And_Resolve (Cnode, Atyp); |
3629 | ||
3630 | exception | |
3631 | when Concatenation_Error => | |
76c597a1 AC |
3632 | |
3633 | -- Kill warning generated for the declaration of the static out of | |
3634 | -- range high bound, and instead generate a Constraint_Error with | |
3635 | -- an appropriate specific message. | |
3636 | ||
3637 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
3638 | Apply_Compile_Time_Constraint_Error | |
3639 | (N => Cnode, | |
324ac540 | 3640 | Msg => "concatenation result upper bound out of range??", |
76c597a1 | 3641 | Reason => CE_Range_Check_Failed); |
fdac1f80 | 3642 | end Expand_Concatenate; |
70482933 | 3643 | |
f6194278 RD |
3644 | --------------------------------------------------- |
3645 | -- Expand_Membership_Minimize_Eliminate_Overflow -- | |
3646 | --------------------------------------------------- | |
3647 | ||
3648 | procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is | |
3649 | pragma Assert (Nkind (N) = N_In); | |
3650 | -- Despite the name, this routine applies only to N_In, not to | |
3651 | -- N_Not_In. The latter is always rewritten as not (X in Y). | |
3652 | ||
71fb4dc8 AC |
3653 | Result_Type : constant Entity_Id := Etype (N); |
3654 | -- Capture result type, may be a derived boolean type | |
3655 | ||
b6b5cca8 AC |
3656 | Loc : constant Source_Ptr := Sloc (N); |
3657 | Lop : constant Node_Id := Left_Opnd (N); | |
3658 | Rop : constant Node_Id := Right_Opnd (N); | |
3659 | ||
3660 | -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It | |
3661 | -- is thus tempting to capture these values, but due to the rewrites | |
3662 | -- that occur as a result of overflow checking, these values change | |
3663 | -- as we go along, and it is safe just to always use Etype explicitly. | |
f6194278 RD |
3664 | |
3665 | Restype : constant Entity_Id := Etype (N); | |
3666 | -- Save result type | |
3667 | ||
3668 | Lo, Hi : Uint; | |
d8192289 | 3669 | -- Bounds in Minimize calls, not used currently |
f6194278 RD |
3670 | |
3671 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
3672 | -- Entity for Long_Long_Integer'Base (Standard should export this???) | |
3673 | ||
3674 | begin | |
a7f1b24f | 3675 | Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False); |
f6194278 RD |
3676 | |
3677 | -- If right operand is a subtype name, and the subtype name has no | |
3678 | -- predicate, then we can just replace the right operand with an | |
3679 | -- explicit range T'First .. T'Last, and use the explicit range code. | |
3680 | ||
b6b5cca8 AC |
3681 | if Nkind (Rop) /= N_Range |
3682 | and then No (Predicate_Function (Etype (Rop))) | |
3683 | then | |
3684 | declare | |
3685 | Rtyp : constant Entity_Id := Etype (Rop); | |
3686 | begin | |
3687 | Rewrite (Rop, | |
3688 | Make_Range (Loc, | |
cc6f5d75 | 3689 | Low_Bound => |
b6b5cca8 AC |
3690 | Make_Attribute_Reference (Loc, |
3691 | Attribute_Name => Name_First, | |
e4494292 | 3692 | Prefix => New_Occurrence_Of (Rtyp, Loc)), |
b6b5cca8 AC |
3693 | High_Bound => |
3694 | Make_Attribute_Reference (Loc, | |
3695 | Attribute_Name => Name_Last, | |
e4494292 | 3696 | Prefix => New_Occurrence_Of (Rtyp, Loc)))); |
b6b5cca8 AC |
3697 | Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks); |
3698 | end; | |
f6194278 RD |
3699 | end if; |
3700 | ||
3701 | -- Here for the explicit range case. Note that the bounds of the range | |
3702 | -- have not been processed for minimized or eliminated checks. | |
3703 | ||
3704 | if Nkind (Rop) = N_Range then | |
a7f1b24f | 3705 | Minimize_Eliminate_Overflows |
b6b5cca8 | 3706 | (Low_Bound (Rop), Lo, Hi, Top_Level => False); |
a7f1b24f | 3707 | Minimize_Eliminate_Overflows |
c7e152b5 | 3708 | (High_Bound (Rop), Lo, Hi, Top_Level => False); |
f6194278 RD |
3709 | |
3710 | -- We have A in B .. C, treated as A >= B and then A <= C | |
3711 | ||
3712 | -- Bignum case | |
3713 | ||
b6b5cca8 | 3714 | if Is_RTE (Etype (Lop), RE_Bignum) |
f6194278 RD |
3715 | or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum) |
3716 | or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum) | |
3717 | then | |
3718 | declare | |
3719 | Blk : constant Node_Id := Make_Bignum_Block (Loc); | |
3720 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
71fb4dc8 AC |
3721 | L : constant Entity_Id := |
3722 | Make_Defining_Identifier (Loc, Name_uL); | |
f6194278 RD |
3723 | Lopnd : constant Node_Id := Convert_To_Bignum (Lop); |
3724 | Lbound : constant Node_Id := | |
3725 | Convert_To_Bignum (Low_Bound (Rop)); | |
3726 | Hbound : constant Node_Id := | |
3727 | Convert_To_Bignum (High_Bound (Rop)); | |
3728 | ||
71fb4dc8 AC |
3729 | -- Now we rewrite the membership test node to look like |
3730 | ||
3731 | -- do | |
3732 | -- Bnn : Result_Type; | |
3733 | -- declare | |
3734 | -- M : Mark_Id := SS_Mark; | |
3735 | -- L : Bignum := Lopnd; | |
3736 | -- begin | |
3737 | -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound) | |
3738 | -- SS_Release (M); | |
3739 | -- end; | |
3740 | -- in | |
3741 | -- Bnn | |
3742 | -- end | |
f6194278 RD |
3743 | |
3744 | begin | |
71fb4dc8 AC |
3745 | -- Insert declaration of L into declarations of bignum block |
3746 | ||
f6194278 RD |
3747 | Insert_After |
3748 | (Last (Declarations (Blk)), | |
3749 | Make_Object_Declaration (Loc, | |
71fb4dc8 | 3750 | Defining_Identifier => L, |
f6194278 RD |
3751 | Object_Definition => |
3752 | New_Occurrence_Of (RTE (RE_Bignum), Loc), | |
3753 | Expression => Lopnd)); | |
3754 | ||
71fb4dc8 AC |
3755 | -- Insert assignment to Bnn into expressions of bignum block |
3756 | ||
f6194278 RD |
3757 | Insert_Before |
3758 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
3759 | Make_Assignment_Statement (Loc, | |
3760 | Name => New_Occurrence_Of (Bnn, Loc), | |
3761 | Expression => | |
3762 | Make_And_Then (Loc, | |
cc6f5d75 | 3763 | Left_Opnd => |
f6194278 RD |
3764 | Make_Function_Call (Loc, |
3765 | Name => | |
3766 | New_Occurrence_Of (RTE (RE_Big_GE), Loc), | |
71fb4dc8 AC |
3767 | Parameter_Associations => New_List ( |
3768 | New_Occurrence_Of (L, Loc), | |
3769 | Lbound)), | |
cc6f5d75 | 3770 | |
f6194278 RD |
3771 | Right_Opnd => |
3772 | Make_Function_Call (Loc, | |
3773 | Name => | |
71fb4dc8 AC |
3774 | New_Occurrence_Of (RTE (RE_Big_LE), Loc), |
3775 | Parameter_Associations => New_List ( | |
3776 | New_Occurrence_Of (L, Loc), | |
3777 | Hbound))))); | |
f6194278 | 3778 | |
71fb4dc8 | 3779 | -- Now rewrite the node |
f6194278 | 3780 | |
71fb4dc8 AC |
3781 | Rewrite (N, |
3782 | Make_Expression_With_Actions (Loc, | |
3783 | Actions => New_List ( | |
3784 | Make_Object_Declaration (Loc, | |
3785 | Defining_Identifier => Bnn, | |
3786 | Object_Definition => | |
3787 | New_Occurrence_Of (Result_Type, Loc)), | |
3788 | Blk), | |
3789 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
3790 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
3791 | return; |
3792 | end; | |
3793 | ||
3794 | -- Here if no bignums around | |
3795 | ||
3796 | else | |
3797 | -- Case where types are all the same | |
3798 | ||
b6b5cca8 | 3799 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop))) |
f6194278 | 3800 | and then |
b6b5cca8 | 3801 | Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop))) |
f6194278 RD |
3802 | then |
3803 | null; | |
3804 | ||
3805 | -- If types are not all the same, it means that we have rewritten | |
3806 | -- at least one of them to be of type Long_Long_Integer, and we | |
3807 | -- will convert the other operands to Long_Long_Integer. | |
3808 | ||
3809 | else | |
3810 | Convert_To_And_Rewrite (LLIB, Lop); | |
71fb4dc8 AC |
3811 | Set_Analyzed (Lop, False); |
3812 | Analyze_And_Resolve (Lop, LLIB); | |
3813 | ||
3814 | -- For the right operand, avoid unnecessary recursion into | |
3815 | -- this routine, we know that overflow is not possible. | |
f6194278 RD |
3816 | |
3817 | Convert_To_And_Rewrite (LLIB, Low_Bound (Rop)); | |
3818 | Convert_To_And_Rewrite (LLIB, High_Bound (Rop)); | |
3819 | Set_Analyzed (Rop, False); | |
71fb4dc8 | 3820 | Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check); |
f6194278 RD |
3821 | end if; |
3822 | ||
3823 | -- Now the three operands are of the same signed integer type, | |
b6b5cca8 AC |
3824 | -- so we can use the normal expansion routine for membership, |
3825 | -- setting the flag to prevent recursion into this procedure. | |
f6194278 RD |
3826 | |
3827 | Set_No_Minimize_Eliminate (N); | |
3828 | Expand_N_In (N); | |
3829 | end if; | |
3830 | ||
3831 | -- Right operand is a subtype name and the subtype has a predicate. We | |
f6636994 AC |
3832 | -- have to make sure the predicate is checked, and for that we need to |
3833 | -- use the standard N_In circuitry with appropriate types. | |
f6194278 RD |
3834 | |
3835 | else | |
b6b5cca8 | 3836 | pragma Assert (Present (Predicate_Function (Etype (Rop)))); |
f6194278 RD |
3837 | |
3838 | -- If types are "right", just call Expand_N_In preventing recursion | |
3839 | ||
b6b5cca8 | 3840 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then |
f6194278 RD |
3841 | Set_No_Minimize_Eliminate (N); |
3842 | Expand_N_In (N); | |
3843 | ||
3844 | -- Bignum case | |
3845 | ||
b6b5cca8 | 3846 | elsif Is_RTE (Etype (Lop), RE_Bignum) then |
f6194278 | 3847 | |
71fb4dc8 | 3848 | -- For X in T, we want to rewrite our node as |
f6194278 | 3849 | |
71fb4dc8 AC |
3850 | -- do |
3851 | -- Bnn : Result_Type; | |
f6194278 | 3852 | |
71fb4dc8 AC |
3853 | -- declare |
3854 | -- M : Mark_Id := SS_Mark; | |
3855 | -- Lnn : Long_Long_Integer'Base | |
3856 | -- Nnn : Bignum; | |
f6194278 | 3857 | |
71fb4dc8 AC |
3858 | -- begin |
3859 | -- Nnn := X; | |
3860 | ||
3861 | -- if not Bignum_In_LLI_Range (Nnn) then | |
3862 | -- Bnn := False; | |
3863 | -- else | |
3864 | -- Lnn := From_Bignum (Nnn); | |
3865 | -- Bnn := | |
3866 | -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last) | |
3867 | -- and then T'Base (Lnn) in T; | |
3868 | -- end if; | |
cc6f5d75 AC |
3869 | |
3870 | -- SS_Release (M); | |
71fb4dc8 AC |
3871 | -- end |
3872 | -- in | |
3873 | -- Bnn | |
3874 | -- end | |
f6194278 | 3875 | |
f6636994 | 3876 | -- A bit gruesome, but there doesn't seem to be a simpler way |
f6194278 RD |
3877 | |
3878 | declare | |
b6b5cca8 AC |
3879 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3880 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
3881 | Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N); | |
3882 | Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N); | |
71fb4dc8 AC |
3883 | T : constant Entity_Id := Etype (Rop); |
3884 | TB : constant Entity_Id := Base_Type (T); | |
b6b5cca8 | 3885 | Nin : Node_Id; |
f6194278 RD |
3886 | |
3887 | begin | |
71fb4dc8 | 3888 | -- Mark the last membership operation to prevent recursion |
f6194278 RD |
3889 | |
3890 | Nin := | |
3891 | Make_In (Loc, | |
f6636994 AC |
3892 | Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)), |
3893 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
3894 | Set_No_Minimize_Eliminate (Nin); |
3895 | ||
3896 | -- Now decorate the block | |
3897 | ||
3898 | Insert_After | |
3899 | (Last (Declarations (Blk)), | |
3900 | Make_Object_Declaration (Loc, | |
3901 | Defining_Identifier => Lnn, | |
3902 | Object_Definition => New_Occurrence_Of (LLIB, Loc))); | |
3903 | ||
3904 | Insert_After | |
3905 | (Last (Declarations (Blk)), | |
3906 | Make_Object_Declaration (Loc, | |
3907 | Defining_Identifier => Nnn, | |
3908 | Object_Definition => | |
3909 | New_Occurrence_Of (RTE (RE_Bignum), Loc))); | |
3910 | ||
3911 | Insert_List_Before | |
3912 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
3913 | New_List ( | |
3914 | Make_Assignment_Statement (Loc, | |
3915 | Name => New_Occurrence_Of (Nnn, Loc), | |
3916 | Expression => Relocate_Node (Lop)), | |
3917 | ||
8b1011c0 | 3918 | Make_Implicit_If_Statement (N, |
f6194278 | 3919 | Condition => |
71fb4dc8 AC |
3920 | Make_Op_Not (Loc, |
3921 | Right_Opnd => | |
3922 | Make_Function_Call (Loc, | |
3923 | Name => | |
3924 | New_Occurrence_Of | |
3925 | (RTE (RE_Bignum_In_LLI_Range), Loc), | |
3926 | Parameter_Associations => New_List ( | |
3927 | New_Occurrence_Of (Nnn, Loc)))), | |
f6194278 RD |
3928 | |
3929 | Then_Statements => New_List ( | |
3930 | Make_Assignment_Statement (Loc, | |
3931 | Name => New_Occurrence_Of (Bnn, Loc), | |
3932 | Expression => | |
3933 | New_Occurrence_Of (Standard_False, Loc))), | |
3934 | ||
3935 | Else_Statements => New_List ( | |
3936 | Make_Assignment_Statement (Loc, | |
3937 | Name => New_Occurrence_Of (Lnn, Loc), | |
3938 | Expression => | |
3939 | Make_Function_Call (Loc, | |
3940 | Name => | |
3941 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
3942 | Parameter_Associations => New_List ( | |
3943 | New_Occurrence_Of (Nnn, Loc)))), | |
3944 | ||
3945 | Make_Assignment_Statement (Loc, | |
71fb4dc8 | 3946 | Name => New_Occurrence_Of (Bnn, Loc), |
f6194278 RD |
3947 | Expression => |
3948 | Make_And_Then (Loc, | |
71fb4dc8 | 3949 | Left_Opnd => |
f6194278 | 3950 | Make_In (Loc, |
71fb4dc8 | 3951 | Left_Opnd => New_Occurrence_Of (Lnn, Loc), |
f6194278 | 3952 | Right_Opnd => |
71fb4dc8 AC |
3953 | Make_Range (Loc, |
3954 | Low_Bound => | |
3955 | Convert_To (LLIB, | |
3956 | Make_Attribute_Reference (Loc, | |
3957 | Attribute_Name => Name_First, | |
3958 | Prefix => | |
3959 | New_Occurrence_Of (TB, Loc))), | |
3960 | ||
3961 | High_Bound => | |
3962 | Convert_To (LLIB, | |
3963 | Make_Attribute_Reference (Loc, | |
3964 | Attribute_Name => Name_Last, | |
3965 | Prefix => | |
3966 | New_Occurrence_Of (TB, Loc))))), | |
3967 | ||
f6194278 RD |
3968 | Right_Opnd => Nin)))))); |
3969 | ||
71fb4dc8 | 3970 | -- Now we can do the rewrite |
f6194278 | 3971 | |
71fb4dc8 AC |
3972 | Rewrite (N, |
3973 | Make_Expression_With_Actions (Loc, | |
3974 | Actions => New_List ( | |
3975 | Make_Object_Declaration (Loc, | |
3976 | Defining_Identifier => Bnn, | |
3977 | Object_Definition => | |
3978 | New_Occurrence_Of (Result_Type, Loc)), | |
3979 | Blk), | |
3980 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
3981 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
3982 | return; |
3983 | end; | |
3984 | ||
3985 | -- Not bignum case, but types don't match (this means we rewrote the | |
b6b5cca8 | 3986 | -- left operand to be Long_Long_Integer). |
f6194278 RD |
3987 | |
3988 | else | |
b6b5cca8 | 3989 | pragma Assert (Base_Type (Etype (Lop)) = LLIB); |
f6194278 | 3990 | |
71fb4dc8 AC |
3991 | -- We rewrite the membership test as (where T is the type with |
3992 | -- the predicate, i.e. the type of the right operand) | |
f6194278 | 3993 | |
71fb4dc8 AC |
3994 | -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last) |
3995 | -- and then T'Base (Lop) in T | |
f6194278 RD |
3996 | |
3997 | declare | |
71fb4dc8 AC |
3998 | T : constant Entity_Id := Etype (Rop); |
3999 | TB : constant Entity_Id := Base_Type (T); | |
f6194278 RD |
4000 | Nin : Node_Id; |
4001 | ||
4002 | begin | |
4003 | -- The last membership test is marked to prevent recursion | |
4004 | ||
4005 | Nin := | |
4006 | Make_In (Loc, | |
71fb4dc8 AC |
4007 | Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)), |
4008 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
4009 | Set_No_Minimize_Eliminate (Nin); |
4010 | ||
4011 | -- Now do the rewrite | |
4012 | ||
4013 | Rewrite (N, | |
4014 | Make_And_Then (Loc, | |
71fb4dc8 | 4015 | Left_Opnd => |
f6194278 RD |
4016 | Make_In (Loc, |
4017 | Left_Opnd => Lop, | |
4018 | Right_Opnd => | |
71fb4dc8 AC |
4019 | Make_Range (Loc, |
4020 | Low_Bound => | |
4021 | Convert_To (LLIB, | |
4022 | Make_Attribute_Reference (Loc, | |
4023 | Attribute_Name => Name_First, | |
cc6f5d75 AC |
4024 | Prefix => |
4025 | New_Occurrence_Of (TB, Loc))), | |
71fb4dc8 AC |
4026 | High_Bound => |
4027 | Convert_To (LLIB, | |
4028 | Make_Attribute_Reference (Loc, | |
4029 | Attribute_Name => Name_Last, | |
cc6f5d75 AC |
4030 | Prefix => |
4031 | New_Occurrence_Of (TB, Loc))))), | |
f6194278 | 4032 | Right_Opnd => Nin)); |
71fb4dc8 AC |
4033 | Set_Analyzed (N, False); |
4034 | Analyze_And_Resolve (N, Restype); | |
f6194278 RD |
4035 | end; |
4036 | end if; | |
4037 | end if; | |
4038 | end Expand_Membership_Minimize_Eliminate_Overflow; | |
4039 | ||
c7a494c9 AC |
4040 | --------------------------------- |
4041 | -- Expand_Nonbinary_Modular_Op -- | |
4042 | --------------------------------- | |
05dbb83f | 4043 | |
c7a494c9 | 4044 | procedure Expand_Nonbinary_Modular_Op (N : Node_Id) is |
05dbb83f AC |
4045 | Loc : constant Source_Ptr := Sloc (N); |
4046 | Typ : constant Entity_Id := Etype (N); | |
4047 | ||
4048 | procedure Expand_Modular_Addition; | |
c7a494c9 | 4049 | -- Expand the modular addition, handling the special case of adding a |
05dbb83f AC |
4050 | -- constant. |
4051 | ||
4052 | procedure Expand_Modular_Op; | |
4053 | -- Compute the general rule: (lhs OP rhs) mod Modulus | |
4054 | ||
4055 | procedure Expand_Modular_Subtraction; | |
c7a494c9 | 4056 | -- Expand the modular addition, handling the special case of subtracting |
05dbb83f AC |
4057 | -- a constant. |
4058 | ||
4059 | ----------------------------- | |
4060 | -- Expand_Modular_Addition -- | |
4061 | ----------------------------- | |
4062 | ||
4063 | procedure Expand_Modular_Addition is | |
4064 | begin | |
4065 | -- If this is not the addition of a constant then compute it using | |
4066 | -- the general rule: (lhs + rhs) mod Modulus | |
4067 | ||
4068 | if Nkind (Right_Opnd (N)) /= N_Integer_Literal then | |
4069 | Expand_Modular_Op; | |
4070 | ||
4071 | -- If this is an addition of a constant, convert it to a subtraction | |
4072 | -- plus a conditional expression since we can compute it faster than | |
4073 | -- computing the modulus. | |
4074 | ||
4075 | -- modMinusRhs = Modulus - rhs | |
4076 | -- if lhs < modMinusRhs then lhs + rhs | |
4077 | -- else lhs - modMinusRhs | |
4078 | ||
4079 | else | |
4080 | declare | |
4081 | Mod_Minus_Right : constant Uint := | |
4082 | Modulus (Typ) - Intval (Right_Opnd (N)); | |
4083 | ||
4084 | Exprs : constant List_Id := New_List; | |
4085 | Cond_Expr : constant Node_Id := New_Op_Node (N_Op_Lt, Loc); | |
4086 | Then_Expr : constant Node_Id := New_Op_Node (N_Op_Add, Loc); | |
4087 | Else_Expr : constant Node_Id := New_Op_Node (N_Op_Subtract, | |
4088 | Loc); | |
4089 | begin | |
dfd2da00 ES |
4090 | -- To prevent spurious visibility issues, convert all |
4091 | -- operands to Standard.Unsigned. | |
4092 | ||
05dbb83f | 4093 | Set_Left_Opnd (Cond_Expr, |
dfd2da00 ES |
4094 | Unchecked_Convert_To (Standard_Unsigned, |
4095 | New_Copy_Tree (Left_Opnd (N)))); | |
05dbb83f AC |
4096 | Set_Right_Opnd (Cond_Expr, |
4097 | Make_Integer_Literal (Loc, Mod_Minus_Right)); | |
4098 | Append_To (Exprs, Cond_Expr); | |
4099 | ||
4100 | Set_Left_Opnd (Then_Expr, | |
4101 | Unchecked_Convert_To (Standard_Unsigned, | |
4102 | New_Copy_Tree (Left_Opnd (N)))); | |
4103 | Set_Right_Opnd (Then_Expr, | |
4104 | Make_Integer_Literal (Loc, Intval (Right_Opnd (N)))); | |
4105 | Append_To (Exprs, Then_Expr); | |
4106 | ||
4107 | Set_Left_Opnd (Else_Expr, | |
4108 | Unchecked_Convert_To (Standard_Unsigned, | |
4109 | New_Copy_Tree (Left_Opnd (N)))); | |
4110 | Set_Right_Opnd (Else_Expr, | |
4111 | Make_Integer_Literal (Loc, Mod_Minus_Right)); | |
4112 | Append_To (Exprs, Else_Expr); | |
4113 | ||
4114 | Rewrite (N, | |
4115 | Unchecked_Convert_To (Typ, | |
4116 | Make_If_Expression (Loc, Expressions => Exprs))); | |
4117 | end; | |
4118 | end if; | |
4119 | end Expand_Modular_Addition; | |
4120 | ||
4121 | ----------------------- | |
4122 | -- Expand_Modular_Op -- | |
4123 | ----------------------- | |
4124 | ||
4125 | procedure Expand_Modular_Op is | |
4126 | Op_Expr : constant Node_Id := New_Op_Node (Nkind (N), Loc); | |
4127 | Mod_Expr : constant Node_Id := New_Op_Node (N_Op_Mod, Loc); | |
4128 | ||
184d0451 ES |
4129 | Target_Type : Entity_Id; |
4130 | ||
05dbb83f | 4131 | begin |
c7a494c9 AC |
4132 | -- Convert nonbinary modular type operands into integer values. Thus |
4133 | -- we avoid never-ending loops expanding them, and we also ensure | |
4134 | -- the back end never receives nonbinary modular type expressions. | |
05dbb83f | 4135 | |
9cd7bc5e | 4136 | if Nkind_In (Nkind (N), N_Op_And, N_Op_Or, N_Op_Xor) then |
05dbb83f AC |
4137 | Set_Left_Opnd (Op_Expr, |
4138 | Unchecked_Convert_To (Standard_Unsigned, | |
4139 | New_Copy_Tree (Left_Opnd (N)))); | |
4140 | Set_Right_Opnd (Op_Expr, | |
4141 | Unchecked_Convert_To (Standard_Unsigned, | |
4142 | New_Copy_Tree (Right_Opnd (N)))); | |
4143 | Set_Left_Opnd (Mod_Expr, | |
4144 | Unchecked_Convert_To (Standard_Integer, Op_Expr)); | |
3e720c96 | 4145 | |
05dbb83f | 4146 | else |
c7862167 HK |
4147 | -- If the modulus of the type is larger than Integer'Last use a |
4148 | -- larger type for the operands, to prevent spurious constraint | |
4149 | -- errors on large legal literals of the type. | |
184d0451 ES |
4150 | |
4151 | if Modulus (Etype (N)) > UI_From_Int (Int (Integer'Last)) then | |
4152 | Target_Type := Standard_Long_Integer; | |
4153 | else | |
4154 | Target_Type := Standard_Integer; | |
4155 | end if; | |
4156 | ||
05dbb83f | 4157 | Set_Left_Opnd (Op_Expr, |
184d0451 | 4158 | Unchecked_Convert_To (Target_Type, |
05dbb83f AC |
4159 | New_Copy_Tree (Left_Opnd (N)))); |
4160 | Set_Right_Opnd (Op_Expr, | |
184d0451 | 4161 | Unchecked_Convert_To (Target_Type, |
05dbb83f | 4162 | New_Copy_Tree (Right_Opnd (N)))); |
9fb1e654 AC |
4163 | |
4164 | -- Link this node to the tree to analyze it | |
4165 | ||
a4f4dbdb AC |
4166 | -- If the parent node is an expression with actions we link it to |
4167 | -- N since otherwise Force_Evaluation cannot identify if this node | |
4168 | -- comes from the Expression and rejects generating the temporary. | |
9fb1e654 AC |
4169 | |
4170 | if Nkind (Parent (N)) = N_Expression_With_Actions then | |
4171 | Set_Parent (Op_Expr, N); | |
4172 | ||
4173 | -- Common case | |
4174 | ||
4175 | else | |
4176 | Set_Parent (Op_Expr, Parent (N)); | |
4177 | end if; | |
4178 | ||
4179 | Analyze (Op_Expr); | |
4180 | ||
4181 | -- Force generating a temporary because in the expansion of this | |
4182 | -- expression we may generate code that performs this computation | |
4183 | -- several times. | |
4184 | ||
4185 | Force_Evaluation (Op_Expr, Mode => Strict); | |
4186 | ||
05dbb83f AC |
4187 | Set_Left_Opnd (Mod_Expr, Op_Expr); |
4188 | end if; | |
4189 | ||
4190 | Set_Right_Opnd (Mod_Expr, | |
4191 | Make_Integer_Literal (Loc, Modulus (Typ))); | |
4192 | ||
4193 | Rewrite (N, | |
4194 | Unchecked_Convert_To (Typ, Mod_Expr)); | |
4195 | end Expand_Modular_Op; | |
4196 | ||
4197 | -------------------------------- | |
4198 | -- Expand_Modular_Subtraction -- | |
4199 | -------------------------------- | |
4200 | ||
4201 | procedure Expand_Modular_Subtraction is | |
4202 | begin | |
4203 | -- If this is not the addition of a constant then compute it using | |
4204 | -- the general rule: (lhs + rhs) mod Modulus | |
4205 | ||
4206 | if Nkind (Right_Opnd (N)) /= N_Integer_Literal then | |
4207 | Expand_Modular_Op; | |
4208 | ||
4209 | -- If this is an addition of a constant, convert it to a subtraction | |
4210 | -- plus a conditional expression since we can compute it faster than | |
4211 | -- computing the modulus. | |
4212 | ||
4213 | -- modMinusRhs = Modulus - rhs | |
4214 | -- if lhs < rhs then lhs + modMinusRhs | |
4215 | -- else lhs - rhs | |
4216 | ||
4217 | else | |
4218 | declare | |
4219 | Mod_Minus_Right : constant Uint := | |
4220 | Modulus (Typ) - Intval (Right_Opnd (N)); | |
4221 | ||
4222 | Exprs : constant List_Id := New_List; | |
4223 | Cond_Expr : constant Node_Id := New_Op_Node (N_Op_Lt, Loc); | |
4224 | Then_Expr : constant Node_Id := New_Op_Node (N_Op_Add, Loc); | |
4225 | Else_Expr : constant Node_Id := New_Op_Node (N_Op_Subtract, | |
4226 | Loc); | |
4227 | begin | |
4228 | Set_Left_Opnd (Cond_Expr, | |
dfd2da00 ES |
4229 | Unchecked_Convert_To (Standard_Unsigned, |
4230 | New_Copy_Tree (Left_Opnd (N)))); | |
05dbb83f AC |
4231 | Set_Right_Opnd (Cond_Expr, |
4232 | Make_Integer_Literal (Loc, Intval (Right_Opnd (N)))); | |
4233 | Append_To (Exprs, Cond_Expr); | |
4234 | ||
4235 | Set_Left_Opnd (Then_Expr, | |
4236 | Unchecked_Convert_To (Standard_Unsigned, | |
4237 | New_Copy_Tree (Left_Opnd (N)))); | |
4238 | Set_Right_Opnd (Then_Expr, | |
4239 | Make_Integer_Literal (Loc, Mod_Minus_Right)); | |
4240 | Append_To (Exprs, Then_Expr); | |
4241 | ||
4242 | Set_Left_Opnd (Else_Expr, | |
4243 | Unchecked_Convert_To (Standard_Unsigned, | |
4244 | New_Copy_Tree (Left_Opnd (N)))); | |
4245 | Set_Right_Opnd (Else_Expr, | |
4246 | Unchecked_Convert_To (Standard_Unsigned, | |
4247 | New_Copy_Tree (Right_Opnd (N)))); | |
4248 | Append_To (Exprs, Else_Expr); | |
4249 | ||
4250 | Rewrite (N, | |
4251 | Unchecked_Convert_To (Typ, | |
4252 | Make_If_Expression (Loc, Expressions => Exprs))); | |
4253 | end; | |
4254 | end if; | |
4255 | end Expand_Modular_Subtraction; | |
4256 | ||
c7a494c9 | 4257 | -- Start of processing for Expand_Nonbinary_Modular_Op |
05dbb83f AC |
4258 | |
4259 | begin | |
f4ac86dd PMR |
4260 | -- No action needed if front-end expansion is not required or if we |
4261 | -- have a binary modular operand. | |
05dbb83f | 4262 | |
f4ac86dd | 4263 | if not Expand_Nonbinary_Modular_Ops |
05dbb83f AC |
4264 | or else not Non_Binary_Modulus (Typ) |
4265 | then | |
4266 | return; | |
4267 | end if; | |
4268 | ||
4269 | case Nkind (N) is | |
4270 | when N_Op_Add => | |
4271 | Expand_Modular_Addition; | |
4272 | ||
4273 | when N_Op_Subtract => | |
4274 | Expand_Modular_Subtraction; | |
4275 | ||
4276 | when N_Op_Minus => | |
3e720c96 | 4277 | |
05dbb83f AC |
4278 | -- Expand -expr into (0 - expr) |
4279 | ||
4280 | Rewrite (N, | |
4281 | Make_Op_Subtract (Loc, | |
4282 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
4283 | Right_Opnd => Right_Opnd (N))); | |
4284 | Analyze_And_Resolve (N, Typ); | |
4285 | ||
4286 | when others => | |
4287 | Expand_Modular_Op; | |
4288 | end case; | |
4289 | ||
4290 | Analyze_And_Resolve (N, Typ); | |
c7a494c9 | 4291 | end Expand_Nonbinary_Modular_Op; |
05dbb83f | 4292 | |
70482933 RK |
4293 | ------------------------ |
4294 | -- Expand_N_Allocator -- | |
4295 | ------------------------ | |
4296 | ||
4297 | procedure Expand_N_Allocator (N : Node_Id) is | |
8b1011c0 AC |
4298 | Etyp : constant Entity_Id := Etype (Expression (N)); |
4299 | Loc : constant Source_Ptr := Sloc (N); | |
4300 | PtrT : constant Entity_Id := Etype (N); | |
70482933 | 4301 | |
26bff3d9 JM |
4302 | procedure Rewrite_Coextension (N : Node_Id); |
4303 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 4304 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 4305 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 4306 | |
8aec446b | 4307 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd | 4308 | -- Given a constrained array type E, returns a node representing the |
22862ba6 JM |
4309 | -- code to compute a close approximation of the size in storage elements |
4310 | -- for the given type; for indexes that are modular types we compute | |
4311 | -- 'Last - First (instead of 'Length) because for large arrays computing | |
4312 | -- 'Last -'First + 1 causes overflow. This is done without using the | |
4313 | -- attribute 'Size_In_Storage_Elements (which malfunctions for large | |
7c2a44ae | 4314 | -- sizes ???). |
8aec446b | 4315 | |
26bff3d9 JM |
4316 | ------------------------- |
4317 | -- Rewrite_Coextension -- | |
4318 | ------------------------- | |
4319 | ||
4320 | procedure Rewrite_Coextension (N : Node_Id) is | |
e5a22243 AC |
4321 | Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C'); |
4322 | Temp_Decl : Node_Id; | |
26bff3d9 | 4323 | |
df3e68b1 | 4324 | begin |
26bff3d9 JM |
4325 | -- Generate: |
4326 | -- Cnn : aliased Etyp; | |
4327 | ||
df3e68b1 HK |
4328 | Temp_Decl := |
4329 | Make_Object_Declaration (Loc, | |
4330 | Defining_Identifier => Temp_Id, | |
243cae0a AC |
4331 | Aliased_Present => True, |
4332 | Object_Definition => New_Occurrence_Of (Etyp, Loc)); | |
26bff3d9 | 4333 | |
26bff3d9 | 4334 | if Nkind (Expression (N)) = N_Qualified_Expression then |
df3e68b1 | 4335 | Set_Expression (Temp_Decl, Expression (Expression (N))); |
0669bebe | 4336 | end if; |
26bff3d9 | 4337 | |
e5a22243 | 4338 | Insert_Action (N, Temp_Decl); |
26bff3d9 JM |
4339 | Rewrite (N, |
4340 | Make_Attribute_Reference (Loc, | |
243cae0a | 4341 | Prefix => New_Occurrence_Of (Temp_Id, Loc), |
26bff3d9 JM |
4342 | Attribute_Name => Name_Unrestricted_Access)); |
4343 | ||
4344 | Analyze_And_Resolve (N, PtrT); | |
4345 | end Rewrite_Coextension; | |
0669bebe | 4346 | |
8aec446b AC |
4347 | ------------------------------ |
4348 | -- Size_In_Storage_Elements -- | |
4349 | ------------------------------ | |
4350 | ||
4351 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
4352 | begin | |
4353 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
4354 | -- However, the reason for the existence of this function is | |
4355 | -- to construct a test for sizes too large, which means near the | |
4356 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
4357 | -- is that we get overflows when sizes are greater than 2**31. | |
4358 | ||
507ed3fd | 4359 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
4360 | |
4361 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
4362 | ||
46202729 | 4363 | -- which avoids this problem. All this is a bit bogus, but it does |
8aec446b AC |
4364 | -- mean we catch common cases of trying to allocate arrays that |
4365 | -- are too large, and which in the absence of a check results in | |
4366 | -- undetected chaos ??? | |
4367 | ||
ce532f42 AC |
4368 | -- Note in particular that this is a pessimistic estimate in the |
4369 | -- case of packed array types, where an array element might occupy | |
4370 | -- just a fraction of a storage element??? | |
4371 | ||
507ed3fd | 4372 | declare |
22862ba6 | 4373 | Idx : Node_Id := First_Index (E); |
507ed3fd | 4374 | Len : Node_Id; |
a6b13d32 | 4375 | Res : Node_Id := Empty; |
8aec446b | 4376 | |
507ed3fd AC |
4377 | begin |
4378 | for J in 1 .. Number_Dimensions (E) loop | |
22862ba6 JM |
4379 | |
4380 | if not Is_Modular_Integer_Type (Etype (Idx)) then | |
4381 | Len := | |
4382 | Make_Attribute_Reference (Loc, | |
4383 | Prefix => New_Occurrence_Of (E, Loc), | |
4384 | Attribute_Name => Name_Length, | |
4385 | Expressions => New_List | |
4386 | (Make_Integer_Literal (Loc, J))); | |
4387 | ||
4388 | -- For indexes that are modular types we cannot generate code | |
4389 | -- to compute 'Length since for large arrays 'Last -'First + 1 | |
4390 | -- causes overflow; therefore we compute 'Last - 'First (which | |
4391 | -- is not the exact number of components but it is valid for | |
7c2a44ae | 4392 | -- the purpose of this runtime check on 32-bit targets). |
22862ba6 JM |
4393 | |
4394 | else | |
4395 | declare | |
4396 | Len_Minus_1_Expr : Node_Id; | |
4397 | Test_Gt : Node_Id; | |
4398 | ||
4399 | begin | |
4400 | Test_Gt := | |
4401 | Make_Op_Gt (Loc, | |
4402 | Make_Attribute_Reference (Loc, | |
4403 | Prefix => New_Occurrence_Of (E, Loc), | |
4404 | Attribute_Name => Name_Last, | |
4405 | Expressions => | |
4406 | New_List (Make_Integer_Literal (Loc, J))), | |
4407 | Make_Attribute_Reference (Loc, | |
4408 | Prefix => New_Occurrence_Of (E, Loc), | |
4409 | Attribute_Name => Name_First, | |
4410 | Expressions => | |
4411 | New_List (Make_Integer_Literal (Loc, J)))); | |
4412 | ||
4413 | Len_Minus_1_Expr := | |
4414 | Convert_To (Standard_Unsigned, | |
4415 | Make_Op_Subtract (Loc, | |
4416 | Make_Attribute_Reference (Loc, | |
4417 | Prefix => New_Occurrence_Of (E, Loc), | |
4418 | Attribute_Name => Name_Last, | |
4419 | Expressions => | |
4420 | New_List | |
4421 | (Make_Integer_Literal (Loc, J))), | |
4422 | Make_Attribute_Reference (Loc, | |
4423 | Prefix => New_Occurrence_Of (E, Loc), | |
4424 | Attribute_Name => Name_First, | |
4425 | Expressions => | |
4426 | New_List | |
4427 | (Make_Integer_Literal (Loc, J))))); | |
4428 | ||
4429 | -- Handle superflat arrays, i.e. arrays with such bounds | |
7c2a44ae | 4430 | -- as 4 .. 2, to ensure that the result is correct. |
22862ba6 JM |
4431 | |
4432 | -- Generate: | |
4433 | -- (if X'Last > X'First then X'Last - X'First else 0) | |
4434 | ||
4435 | Len := | |
4436 | Make_If_Expression (Loc, | |
4437 | Expressions => New_List ( | |
4438 | Test_Gt, | |
4439 | Len_Minus_1_Expr, | |
4440 | Make_Integer_Literal (Loc, Uint_0))); | |
4441 | end; | |
4442 | end if; | |
8aec446b | 4443 | |
507ed3fd AC |
4444 | if J = 1 then |
4445 | Res := Len; | |
8aec446b | 4446 | |
507ed3fd | 4447 | else |
a6b13d32 | 4448 | pragma Assert (Present (Res)); |
507ed3fd AC |
4449 | Res := |
4450 | Make_Op_Multiply (Loc, | |
4451 | Left_Opnd => Res, | |
4452 | Right_Opnd => Len); | |
4453 | end if; | |
22862ba6 JM |
4454 | |
4455 | Next_Index (Idx); | |
507ed3fd | 4456 | end loop; |
8aec446b | 4457 | |
8aec446b | 4458 | return |
507ed3fd AC |
4459 | Make_Op_Multiply (Loc, |
4460 | Left_Opnd => Len, | |
4461 | Right_Opnd => | |
4462 | Make_Attribute_Reference (Loc, | |
4463 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
4464 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
4465 | end; | |
8aec446b AC |
4466 | end Size_In_Storage_Elements; |
4467 | ||
8b1011c0 AC |
4468 | -- Local variables |
4469 | ||
70861157 | 4470 | Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT)); |
8b1011c0 AC |
4471 | Desig : Entity_Id; |
4472 | Nod : Node_Id; | |
4473 | Pool : Entity_Id; | |
4474 | Rel_Typ : Entity_Id; | |
4475 | Temp : Entity_Id; | |
4476 | ||
0669bebe GB |
4477 | -- Start of processing for Expand_N_Allocator |
4478 | ||
70482933 | 4479 | begin |
b3889fff | 4480 | -- Warn on the presence of an allocator of an anonymous access type when |
31fde973 | 4481 | -- enabled, except when it's an object declaration at library level. |
b3889fff JS |
4482 | |
4483 | if Warn_On_Anonymous_Allocators | |
4484 | and then Ekind (PtrT) = E_Anonymous_Access_Type | |
943c82d7 JS |
4485 | and then not (Is_Library_Level_Entity (PtrT) |
4486 | and then Nkind (Associated_Node_For_Itype (PtrT)) = | |
4487 | N_Object_Declaration) | |
b3889fff JS |
4488 | then |
4489 | Error_Msg_N ("?use of an anonymous access type allocator", N); | |
4490 | end if; | |
4491 | ||
70482933 RK |
4492 | -- RM E.2.3(22). We enforce that the expected type of an allocator |
4493 | -- shall not be a remote access-to-class-wide-limited-private type | |
4494 | ||
4495 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
4496 | ||
4497 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
4498 | ||
ca5af305 AC |
4499 | -- Processing for anonymous access-to-controlled types. These access |
4500 | -- types receive a special finalization master which appears in the | |
4501 | -- declarations of the enclosing semantic unit. This expansion is done | |
84f4072a JM |
4502 | -- now to ensure that any additional types generated by this routine or |
4503 | -- Expand_Allocator_Expression inherit the proper type attributes. | |
ca5af305 | 4504 | |
84f4072a | 4505 | if (Ekind (PtrT) = E_Anonymous_Access_Type |
533369aa | 4506 | or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT)))) |
ca5af305 AC |
4507 | and then Needs_Finalization (Dtyp) |
4508 | then | |
8b1011c0 AC |
4509 | -- Detect the allocation of an anonymous controlled object where the |
4510 | -- type of the context is named. For example: | |
4511 | ||
4512 | -- procedure Proc (Ptr : Named_Access_Typ); | |
4513 | -- Proc (new Designated_Typ); | |
4514 | ||
4515 | -- Regardless of the anonymous-to-named access type conversion, the | |
4516 | -- lifetime of the object must be associated with the named access | |
0088ba92 | 4517 | -- type. Use the finalization-related attributes of this type. |
8b1011c0 AC |
4518 | |
4519 | if Nkind_In (Parent (N), N_Type_Conversion, | |
4520 | N_Unchecked_Type_Conversion) | |
4521 | and then Ekind_In (Etype (Parent (N)), E_Access_Subtype, | |
4522 | E_Access_Type, | |
4523 | E_General_Access_Type) | |
4524 | then | |
4525 | Rel_Typ := Etype (Parent (N)); | |
4526 | else | |
4527 | Rel_Typ := Empty; | |
4528 | end if; | |
4529 | ||
b254da66 | 4530 | -- Anonymous access-to-controlled types allocate on the global pool. |
535a8637 | 4531 | -- Note that this is a "root type only" attribute. |
ca5af305 | 4532 | |
535a8637 | 4533 | if No (Associated_Storage_Pool (PtrT)) then |
8b1011c0 | 4534 | if Present (Rel_Typ) then |
7a5b62b0 | 4535 | Set_Associated_Storage_Pool |
24d4b3d5 | 4536 | (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ)); |
8b1011c0 | 4537 | else |
7a5b62b0 | 4538 | Set_Associated_Storage_Pool |
24d4b3d5 | 4539 | (Root_Type (PtrT), RTE (RE_Global_Pool_Object)); |
8b1011c0 | 4540 | end if; |
ca5af305 AC |
4541 | end if; |
4542 | ||
4543 | -- The finalization master must be inserted and analyzed as part of | |
5114f3ff | 4544 | -- the current semantic unit. Note that the master is updated when |
24d4b3d5 AC |
4545 | -- analysis changes current units. Note that this is a "root type |
4546 | -- only" attribute. | |
ca5af305 | 4547 | |
5114f3ff | 4548 | if Present (Rel_Typ) then |
24d4b3d5 AC |
4549 | Set_Finalization_Master |
4550 | (Root_Type (PtrT), Finalization_Master (Rel_Typ)); | |
5114f3ff | 4551 | else |
32b794c8 | 4552 | Build_Anonymous_Master (Root_Type (PtrT)); |
ca5af305 AC |
4553 | end if; |
4554 | end if; | |
4555 | ||
4556 | -- Set the storage pool and find the appropriate version of Allocate to | |
8417f4b2 AC |
4557 | -- call. Do not overwrite the storage pool if it is already set, which |
4558 | -- can happen for build-in-place function returns (see | |
200b7162 | 4559 | -- Exp_Ch4.Expand_N_Extended_Return_Statement). |
70482933 | 4560 | |
200b7162 BD |
4561 | if No (Storage_Pool (N)) then |
4562 | Pool := Associated_Storage_Pool (Root_Type (PtrT)); | |
70482933 | 4563 | |
200b7162 BD |
4564 | if Present (Pool) then |
4565 | Set_Storage_Pool (N, Pool); | |
fbf5a39b | 4566 | |
200b7162 | 4567 | if Is_RTE (Pool, RE_SS_Pool) then |
abbfd698 | 4568 | Check_Restriction (No_Secondary_Stack, N); |
535a8637 | 4569 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); |
fbf5a39b | 4570 | |
a8551b5f AC |
4571 | -- In the case of an allocator for a simple storage pool, locate |
4572 | -- and save a reference to the pool type's Allocate routine. | |
4573 | ||
4574 | elsif Present (Get_Rep_Pragma | |
f6205414 | 4575 | (Etype (Pool), Name_Simple_Storage_Pool_Type)) |
a8551b5f AC |
4576 | then |
4577 | declare | |
a8551b5f | 4578 | Pool_Type : constant Entity_Id := Base_Type (Etype (Pool)); |
260359e3 | 4579 | Alloc_Op : Entity_Id; |
a8551b5f | 4580 | begin |
260359e3 | 4581 | Alloc_Op := Get_Name_Entity_Id (Name_Allocate); |
a8551b5f AC |
4582 | while Present (Alloc_Op) loop |
4583 | if Scope (Alloc_Op) = Scope (Pool_Type) | |
4584 | and then Present (First_Formal (Alloc_Op)) | |
4585 | and then Etype (First_Formal (Alloc_Op)) = Pool_Type | |
4586 | then | |
4587 | Set_Procedure_To_Call (N, Alloc_Op); | |
a8551b5f | 4588 | exit; |
260359e3 AC |
4589 | else |
4590 | Alloc_Op := Homonym (Alloc_Op); | |
a8551b5f | 4591 | end if; |
a8551b5f AC |
4592 | end loop; |
4593 | end; | |
4594 | ||
200b7162 BD |
4595 | elsif Is_Class_Wide_Type (Etype (Pool)) then |
4596 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
4597 | ||
4598 | else | |
4599 | Set_Procedure_To_Call (N, | |
4600 | Find_Prim_Op (Etype (Pool), Name_Allocate)); | |
4601 | end if; | |
70482933 RK |
4602 | end if; |
4603 | end if; | |
4604 | ||
685094bf RD |
4605 | -- Under certain circumstances we can replace an allocator by an access |
4606 | -- to statically allocated storage. The conditions, as noted in AARM | |
4607 | -- 3.10 (10c) are as follows: | |
70482933 RK |
4608 | |
4609 | -- Size and initial value is known at compile time | |
4610 | -- Access type is access-to-constant | |
4611 | ||
fbf5a39b AC |
4612 | -- The allocator is not part of a constraint on a record component, |
4613 | -- because in that case the inserted actions are delayed until the | |
4614 | -- record declaration is fully analyzed, which is too late for the | |
4615 | -- analysis of the rewritten allocator. | |
4616 | ||
70482933 RK |
4617 | if Is_Access_Constant (PtrT) |
4618 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
4619 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
243cae0a AC |
4620 | and then Size_Known_At_Compile_Time |
4621 | (Etype (Expression (Expression (N)))) | |
fbf5a39b | 4622 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
4623 | then |
4624 | -- Here we can do the optimization. For the allocator | |
4625 | ||
4626 | -- new x'(y) | |
4627 | ||
4628 | -- We insert an object declaration | |
4629 | ||
4630 | -- Tnn : aliased x := y; | |
4631 | ||
685094bf RD |
4632 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
4633 | -- marked as requiring static allocation. | |
70482933 | 4634 | |
df3e68b1 | 4635 | Temp := Make_Temporary (Loc, 'T', Expression (Expression (N))); |
70482933 RK |
4636 | Desig := Subtype_Mark (Expression (N)); |
4637 | ||
4638 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 4639 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
4640 | -- unconstrained subtype. |
4641 | ||
0da2c8ac AC |
4642 | if Entity (Desig) = Base_Type (Dtyp) then |
4643 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
4644 | end if; |
4645 | ||
4646 | Insert_Action (N, | |
4647 | Make_Object_Declaration (Loc, | |
4648 | Defining_Identifier => Temp, | |
4649 | Aliased_Present => True, | |
4650 | Constant_Present => Is_Access_Constant (PtrT), | |
4651 | Object_Definition => Desig, | |
4652 | Expression => Expression (Expression (N)))); | |
4653 | ||
4654 | Rewrite (N, | |
4655 | Make_Attribute_Reference (Loc, | |
243cae0a | 4656 | Prefix => New_Occurrence_Of (Temp, Loc), |
70482933 RK |
4657 | Attribute_Name => Name_Unrestricted_Access)); |
4658 | ||
4659 | Analyze_And_Resolve (N, PtrT); | |
4660 | ||
685094bf | 4661 | -- We set the variable as statically allocated, since we don't want |
a90bd866 | 4662 | -- it going on the stack of the current procedure. |
70482933 RK |
4663 | |
4664 | Set_Is_Statically_Allocated (Temp); | |
4665 | return; | |
4666 | end if; | |
4667 | ||
0669bebe GB |
4668 | -- Same if the allocator is an access discriminant for a local object: |
4669 | -- instead of an allocator we create a local value and constrain the | |
308e6f3a | 4670 | -- enclosing object with the corresponding access attribute. |
0669bebe | 4671 | |
26bff3d9 JM |
4672 | if Is_Static_Coextension (N) then |
4673 | Rewrite_Coextension (N); | |
0669bebe GB |
4674 | return; |
4675 | end if; | |
4676 | ||
8aec446b AC |
4677 | -- Check for size too large, we do this because the back end misses |
4678 | -- proper checks here and can generate rubbish allocation calls when | |
4679 | -- we are near the limit. We only do this for the 32-bit address case | |
4680 | -- since that is from a practical point of view where we see a problem. | |
4681 | ||
4682 | if System_Address_Size = 32 | |
4683 | and then not Storage_Checks_Suppressed (PtrT) | |
4684 | and then not Storage_Checks_Suppressed (Dtyp) | |
4685 | and then not Storage_Checks_Suppressed (Etyp) | |
4686 | then | |
4687 | -- The check we want to generate should look like | |
4688 | ||
4689 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
4690 | -- raise Storage_Error; | |
4691 | -- end if; | |
4692 | ||
308e6f3a | 4693 | -- where 3.5 gigabytes is a constant large enough to accommodate any |
507ed3fd AC |
4694 | -- reasonable request for. But we can't do it this way because at |
4695 | -- least at the moment we don't compute this attribute right, and | |
4696 | -- can silently give wrong results when the result gets large. Since | |
4697 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 4698 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 4699 | -- value of the attribute ??? |
8aec446b | 4700 | |
22862ba6 JM |
4701 | -- The check on No_Initialization is used here to prevent generating |
4702 | -- this runtime check twice when the allocator is locally replaced by | |
7c2a44ae | 4703 | -- the expander with another one. |
22862ba6 JM |
4704 | |
4705 | if Is_Array_Type (Etyp) and then not No_Initialization (N) then | |
4706 | declare | |
4707 | Cond : Node_Id; | |
4708 | Ins_Nod : Node_Id := N; | |
4709 | Siz_Typ : Entity_Id := Etyp; | |
4710 | Expr : Node_Id; | |
4711 | ||
4712 | begin | |
4713 | -- For unconstrained array types initialized with a qualified | |
4714 | -- expression we use its type to perform this check | |
4715 | ||
4716 | if not Is_Constrained (Etyp) | |
4717 | and then not No_Initialization (N) | |
4718 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
4719 | then | |
4720 | Expr := Expression (Expression (N)); | |
4721 | Siz_Typ := Etype (Expression (Expression (N))); | |
4722 | ||
4723 | -- If the qualified expression has been moved to an internal | |
4724 | -- temporary (to remove side effects) then we must insert | |
4725 | -- the runtime check before its declaration to ensure that | |
4726 | -- the check is performed before the execution of the code | |
4727 | -- computing the qualified expression. | |
4728 | ||
4729 | if Nkind (Expr) = N_Identifier | |
4730 | and then Is_Internal_Name (Chars (Expr)) | |
4731 | and then | |
4732 | Nkind (Parent (Entity (Expr))) = N_Object_Declaration | |
4733 | then | |
4734 | Ins_Nod := Parent (Entity (Expr)); | |
4735 | else | |
4736 | Ins_Nod := Expr; | |
4737 | end if; | |
4738 | end if; | |
4739 | ||
4740 | if Is_Constrained (Siz_Typ) | |
4741 | and then Ekind (Siz_Typ) /= E_String_Literal_Subtype | |
4742 | then | |
7c2a44ae PT |
4743 | -- For CCG targets, the largest array may have up to 2**31-1 |
4744 | -- components (i.e. 2 gigabytes if each array component is | |
4745 | -- one byte). This ensures that fat pointer fields do not | |
22862ba6 | 4746 | -- overflow, since they are 32-bit integer types, and also |
7c2a44ae | 4747 | -- ensures that 'Length can be computed at run time. |
22862ba6 JM |
4748 | |
4749 | if Modify_Tree_For_C then | |
4750 | Cond := | |
4751 | Make_Op_Gt (Loc, | |
4752 | Left_Opnd => Size_In_Storage_Elements (Siz_Typ), | |
4753 | Right_Opnd => Make_Integer_Literal (Loc, | |
4754 | Uint_2 ** 31 - Uint_1)); | |
4755 | ||
4756 | -- For native targets the largest object is 3.5 gigabytes | |
4757 | ||
4758 | else | |
4759 | Cond := | |
4760 | Make_Op_Gt (Loc, | |
4761 | Left_Opnd => Size_In_Storage_Elements (Siz_Typ), | |
4762 | Right_Opnd => Make_Integer_Literal (Loc, | |
4763 | Uint_7 * (Uint_2 ** 29))); | |
4764 | end if; | |
4765 | ||
4766 | Insert_Action (Ins_Nod, | |
4767 | Make_Raise_Storage_Error (Loc, | |
4768 | Condition => Cond, | |
4769 | Reason => SE_Object_Too_Large)); | |
4770 | ||
4771 | if Entity (Cond) = Standard_True then | |
4772 | Error_Msg_N | |
4773 | ("object too large: Storage_Error will be raised at " | |
4774 | & "run time??", N); | |
4775 | end if; | |
4776 | end if; | |
4777 | end; | |
507ed3fd | 4778 | end if; |
8aec446b AC |
4779 | end if; |
4780 | ||
b3181992 GD |
4781 | -- If no storage pool has been specified, or the storage pool |
4782 | -- is System.Pool_Global.Global_Pool_Object, and the restriction | |
b3b26ace AC |
4783 | -- No_Standard_Allocators_After_Elaboration is present, then generate |
4784 | -- a call to Elaboration_Allocators.Check_Standard_Allocator. | |
4785 | ||
4786 | if Nkind (N) = N_Allocator | |
b3181992 GD |
4787 | and then (No (Storage_Pool (N)) |
4788 | or else Is_RTE (Storage_Pool (N), RE_Global_Pool_Object)) | |
b3b26ace AC |
4789 | and then Restriction_Active (No_Standard_Allocators_After_Elaboration) |
4790 | then | |
4791 | Insert_Action (N, | |
4792 | Make_Procedure_Call_Statement (Loc, | |
4793 | Name => | |
4794 | New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc))); | |
4795 | end if; | |
4796 | ||
0da2c8ac | 4797 | -- Handle case of qualified expression (other than optimization above) |
cac5a801 AC |
4798 | -- First apply constraint checks, because the bounds or discriminants |
4799 | -- in the aggregate might not match the subtype mark in the allocator. | |
0da2c8ac | 4800 | |
70482933 | 4801 | if Nkind (Expression (N)) = N_Qualified_Expression then |
0026dd0a | 4802 | declare |
f31dcd99 | 4803 | Exp : constant Node_Id := Expression (Expression (N)); |
0026dd0a | 4804 | Typ : constant Entity_Id := Etype (Expression (N)); |
f31dcd99 | 4805 | |
0026dd0a AC |
4806 | begin |
4807 | Apply_Constraint_Check (Exp, Typ); | |
4808 | Apply_Predicate_Check (Exp, Typ); | |
4809 | end; | |
cac5a801 | 4810 | |
fbf5a39b | 4811 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
4812 | return; |
4813 | end if; | |
fbf5a39b | 4814 | |
26bff3d9 JM |
4815 | -- If the allocator is for a type which requires initialization, and |
4816 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
4817 | -- rather than a qualified expression), then we must generate a call to |
4818 | -- the initialization routine using an expressions action node: | |
70482933 | 4819 | |
26bff3d9 | 4820 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 4821 | |
26bff3d9 JM |
4822 | -- Here ptr_T is the pointer type for the allocator, and T is the |
4823 | -- subtype of the allocator. A special case arises if the designated | |
4824 | -- type of the access type is a task or contains tasks. In this case | |
4825 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
4826 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
6be44a9a | 4827 | -- for details). In addition, if the type T is a task type, then the |
26bff3d9 | 4828 | -- first argument to Init must be converted to the task record type. |
70482933 | 4829 | |
26bff3d9 | 4830 | declare |
529749b9 | 4831 | T : constant Entity_Id := Etype (Expression (N)); |
df3e68b1 HK |
4832 | Args : List_Id; |
4833 | Decls : List_Id; | |
4834 | Decl : Node_Id; | |
4835 | Discr : Elmt_Id; | |
4836 | Init : Entity_Id; | |
4837 | Init_Arg1 : Node_Id; | |
2168d7cc | 4838 | Init_Call : Node_Id; |
df3e68b1 HK |
4839 | Temp_Decl : Node_Id; |
4840 | Temp_Type : Entity_Id; | |
70482933 | 4841 | |
26bff3d9 JM |
4842 | begin |
4843 | if No_Initialization (N) then | |
df3e68b1 HK |
4844 | |
4845 | -- Even though this might be a simple allocation, create a custom | |
535a8637 | 4846 | -- Allocate if the context requires it. |
df3e68b1 | 4847 | |
535a8637 | 4848 | if Present (Finalization_Master (PtrT)) then |
df3e68b1 | 4849 | Build_Allocate_Deallocate_Proc |
ca5af305 | 4850 | (N => N, |
df3e68b1 HK |
4851 | Is_Allocate => True); |
4852 | end if; | |
70482933 | 4853 | |
40016fa7 HK |
4854 | -- Optimize the default allocation of an array object when pragma |
4855 | -- Initialize_Scalars or Normalize_Scalars is in effect. Construct an | |
4856 | -- in-place initialization aggregate which may be convert into a fast | |
4857 | -- memset by the backend. | |
529749b9 HK |
4858 | |
4859 | elsif Init_Or_Norm_Scalars | |
4860 | and then Is_Array_Type (T) | |
40016fa7 HK |
4861 | |
4862 | -- The array must lack atomic components because they are treated | |
4863 | -- as non-static, and as a result the backend will not initialize | |
4864 | -- the memory in one go. | |
4865 | ||
529749b9 | 4866 | and then not Has_Atomic_Components (T) |
40016fa7 HK |
4867 | |
4868 | -- The array must not be packed because the invalid values in | |
4869 | -- System.Scalar_Values are multiples of Storage_Unit. | |
4870 | ||
529749b9 | 4871 | and then not Is_Packed (T) |
40016fa7 HK |
4872 | |
4873 | -- The array must have static non-empty ranges, otherwise the | |
4874 | -- backend cannot initialize the memory in one go. | |
4875 | ||
529749b9 | 4876 | and then Has_Static_Non_Empty_Array_Bounds (T) |
40016fa7 HK |
4877 | |
4878 | -- The optimization is only relevant for arrays of scalar types | |
4879 | ||
529749b9 | 4880 | and then Is_Scalar_Type (Component_Type (T)) |
40016fa7 HK |
4881 | |
4882 | -- Similar to regular array initialization using a type init proc, | |
4883 | -- predicate checks are not performed because the initialization | |
4884 | -- values are intentionally invalid, and may violate the predicate. | |
4885 | ||
4886 | and then not Has_Predicates (Component_Type (T)) | |
4887 | ||
4888 | -- The component type must have a single initialization value | |
4889 | ||
529749b9 HK |
4890 | and then Needs_Simple_Initialization |
4891 | (Typ => Component_Type (T), | |
4892 | Consider_IS => True) | |
4893 | then | |
4894 | Set_Analyzed (N); | |
4895 | Temp := Make_Temporary (Loc, 'P'); | |
4896 | ||
4897 | -- Generate: | |
4898 | -- Temp : Ptr_Typ := new ...; | |
4899 | ||
4900 | Insert_Action | |
4901 | (Assoc_Node => N, | |
4902 | Ins_Action => | |
4903 | Make_Object_Declaration (Loc, | |
4904 | Defining_Identifier => Temp, | |
4905 | Object_Definition => New_Occurrence_Of (PtrT, Loc), | |
4906 | Expression => Relocate_Node (N)), | |
4907 | Suppress => All_Checks); | |
4908 | ||
4909 | -- Generate: | |
4910 | -- Temp.all := (others => ...); | |
4911 | ||
4912 | Insert_Action | |
4913 | (Assoc_Node => N, | |
4914 | Ins_Action => | |
4915 | Make_Assignment_Statement (Loc, | |
4916 | Name => | |
4917 | Make_Explicit_Dereference (Loc, | |
4918 | Prefix => New_Occurrence_Of (Temp, Loc)), | |
4919 | Expression => | |
4920 | Get_Simple_Init_Val | |
4921 | (Typ => T, | |
4922 | N => N, | |
4923 | Size => Esize (Component_Type (T)))), | |
4924 | Suppress => All_Checks); | |
4925 | ||
4926 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
4927 | Analyze_And_Resolve (N, PtrT); | |
4928 | ||
26bff3d9 | 4929 | -- Case of no initialization procedure present |
70482933 | 4930 | |
26bff3d9 | 4931 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 4932 | |
26bff3d9 | 4933 | -- Case of simple initialization required |
70482933 | 4934 | |
26bff3d9 | 4935 | if Needs_Simple_Initialization (T) then |
b4592168 | 4936 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
4937 | Rewrite (Expression (N), |
4938 | Make_Qualified_Expression (Loc, | |
4939 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 4940 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 4941 | |
26bff3d9 JM |
4942 | Analyze_And_Resolve (Expression (Expression (N)), T); |
4943 | Analyze_And_Resolve (Expression (N), T); | |
4944 | Set_Paren_Count (Expression (Expression (N)), 1); | |
4945 | Expand_N_Allocator (N); | |
70482933 | 4946 | |
26bff3d9 | 4947 | -- No initialization required |
70482933 RK |
4948 | |
4949 | else | |
b2c3160c AC |
4950 | Build_Allocate_Deallocate_Proc |
4951 | (N => N, | |
4952 | Is_Allocate => True); | |
26bff3d9 | 4953 | end if; |
70482933 | 4954 | |
26bff3d9 | 4955 | -- Case of initialization procedure present, must be called |
70482933 | 4956 | |
fa528281 JS |
4957 | -- NOTE: There is a *huge* amount of code duplication here from |
4958 | -- Build_Initialization_Call. We should probably refactor??? | |
4959 | ||
26bff3d9 | 4960 | else |
b4592168 | 4961 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 4962 | |
b4592168 GD |
4963 | if not Restriction_Active (No_Default_Initialization) then |
4964 | Init := Base_Init_Proc (T); | |
4965 | Nod := N; | |
191fcb3a | 4966 | Temp := Make_Temporary (Loc, 'P'); |
70482933 | 4967 | |
b4592168 | 4968 | -- Construct argument list for the initialization routine call |
70482933 | 4969 | |
df3e68b1 | 4970 | Init_Arg1 := |
b4592168 | 4971 | Make_Explicit_Dereference (Loc, |
df3e68b1 | 4972 | Prefix => |
e4494292 | 4973 | New_Occurrence_Of (Temp, Loc)); |
df3e68b1 HK |
4974 | |
4975 | Set_Assignment_OK (Init_Arg1); | |
b4592168 | 4976 | Temp_Type := PtrT; |
26bff3d9 | 4977 | |
b4592168 GD |
4978 | -- The initialization procedure expects a specific type. if the |
4979 | -- context is access to class wide, indicate that the object | |
4980 | -- being allocated has the right specific type. | |
70482933 | 4981 | |
b4592168 | 4982 | if Is_Class_Wide_Type (Dtyp) then |
df3e68b1 | 4983 | Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1); |
b4592168 | 4984 | end if; |
70482933 | 4985 | |
b4592168 GD |
4986 | -- If designated type is a concurrent type or if it is private |
4987 | -- type whose definition is a concurrent type, the first | |
4988 | -- argument in the Init routine has to be unchecked conversion | |
4989 | -- to the corresponding record type. If the designated type is | |
243cae0a | 4990 | -- a derived type, also convert the argument to its root type. |
20b5d666 | 4991 | |
b4592168 | 4992 | if Is_Concurrent_Type (T) then |
df3e68b1 HK |
4993 | Init_Arg1 := |
4994 | Unchecked_Convert_To ( | |
4995 | Corresponding_Record_Type (T), Init_Arg1); | |
70482933 | 4996 | |
b4592168 GD |
4997 | elsif Is_Private_Type (T) |
4998 | and then Present (Full_View (T)) | |
4999 | and then Is_Concurrent_Type (Full_View (T)) | |
5000 | then | |
df3e68b1 | 5001 | Init_Arg1 := |
b4592168 | 5002 | Unchecked_Convert_To |
df3e68b1 | 5003 | (Corresponding_Record_Type (Full_View (T)), Init_Arg1); |
70482933 | 5004 | |
b4592168 GD |
5005 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
5006 | declare | |
5007 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
df3e68b1 | 5008 | |
b4592168 | 5009 | begin |
df3e68b1 HK |
5010 | Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1); |
5011 | Set_Etype (Init_Arg1, Ftyp); | |
b4592168 GD |
5012 | end; |
5013 | end if; | |
70482933 | 5014 | |
df3e68b1 | 5015 | Args := New_List (Init_Arg1); |
70482933 | 5016 | |
b4592168 GD |
5017 | -- For the task case, pass the Master_Id of the access type as |
5018 | -- the value of the _Master parameter, and _Chain as the value | |
5019 | -- of the _Chain parameter (_Chain will be defined as part of | |
5020 | -- the generated code for the allocator). | |
70482933 | 5021 | |
b4592168 GD |
5022 | -- In Ada 2005, the context may be a function that returns an |
5023 | -- anonymous access type. In that case the Master_Id has been | |
5024 | -- created when expanding the function declaration. | |
70482933 | 5025 | |
b4592168 GD |
5026 | if Has_Task (T) then |
5027 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 5028 | |
b4592168 GD |
5029 | -- The designated type was an incomplete type, and the |
5030 | -- access type did not get expanded. Salvage it now. | |
70482933 | 5031 | |
b941ae65 | 5032 | if not Restriction_Active (No_Task_Hierarchy) then |
3d67b239 AC |
5033 | if Present (Parent (Base_Type (PtrT))) then |
5034 | Expand_N_Full_Type_Declaration | |
5035 | (Parent (Base_Type (PtrT))); | |
5036 | ||
0d5fbf52 AC |
5037 | -- The only other possibility is an itype. For this |
5038 | -- case, the master must exist in the context. This is | |
5039 | -- the case when the allocator initializes an access | |
5040 | -- component in an init-proc. | |
3d67b239 | 5041 | |
0d5fbf52 | 5042 | else |
3d67b239 AC |
5043 | pragma Assert (Is_Itype (PtrT)); |
5044 | Build_Master_Renaming (PtrT, N); | |
5045 | end if; | |
b941ae65 | 5046 | end if; |
b4592168 | 5047 | end if; |
70482933 | 5048 | |
b4592168 GD |
5049 | -- If the context of the allocator is a declaration or an |
5050 | -- assignment, we can generate a meaningful image for it, | |
5051 | -- even though subsequent assignments might remove the | |
5052 | -- connection between task and entity. We build this image | |
5053 | -- when the left-hand side is a simple variable, a simple | |
5054 | -- indexed assignment or a simple selected component. | |
5055 | ||
5056 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
5057 | declare | |
5058 | Nam : constant Node_Id := Name (Parent (N)); | |
5059 | ||
5060 | begin | |
5061 | if Is_Entity_Name (Nam) then | |
5062 | Decls := | |
5063 | Build_Task_Image_Decls | |
5064 | (Loc, | |
5065 | New_Occurrence_Of | |
5066 | (Entity (Nam), Sloc (Nam)), T); | |
5067 | ||
243cae0a AC |
5068 | elsif Nkind_In (Nam, N_Indexed_Component, |
5069 | N_Selected_Component) | |
b4592168 GD |
5070 | and then Is_Entity_Name (Prefix (Nam)) |
5071 | then | |
5072 | Decls := | |
5073 | Build_Task_Image_Decls | |
5074 | (Loc, Nam, Etype (Prefix (Nam))); | |
5075 | else | |
5076 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
5077 | end if; | |
5078 | end; | |
70482933 | 5079 | |
b4592168 GD |
5080 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
5081 | Decls := | |
5082 | Build_Task_Image_Decls | |
5083 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 5084 | |
b4592168 GD |
5085 | else |
5086 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
5087 | end if; | |
26bff3d9 | 5088 | |
87dc09cb | 5089 | if Restriction_Active (No_Task_Hierarchy) then |
3c1ecd7e AC |
5090 | Append_To (Args, |
5091 | New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc)); | |
87dc09cb AC |
5092 | else |
5093 | Append_To (Args, | |
e4494292 | 5094 | New_Occurrence_Of |
87dc09cb AC |
5095 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); |
5096 | end if; | |
5097 | ||
b4592168 | 5098 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); |
26bff3d9 | 5099 | |
b4592168 GD |
5100 | Decl := Last (Decls); |
5101 | Append_To (Args, | |
5102 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 5103 | |
87dc09cb | 5104 | -- Has_Task is false, Decls not used |
26bff3d9 | 5105 | |
b4592168 GD |
5106 | else |
5107 | Decls := No_List; | |
26bff3d9 JM |
5108 | end if; |
5109 | ||
b4592168 GD |
5110 | -- Add discriminants if discriminated type |
5111 | ||
5112 | declare | |
5113 | Dis : Boolean := False; | |
dcd5fd67 | 5114 | Typ : Entity_Id := Empty; |
b4592168 GD |
5115 | |
5116 | begin | |
5117 | if Has_Discriminants (T) then | |
5118 | Dis := True; | |
5119 | Typ := T; | |
5120 | ||
bac5ba15 AC |
5121 | -- Type may be a private type with no visible discriminants |
5122 | -- in which case check full view if in scope, or the | |
5123 | -- underlying_full_view if dealing with a type whose full | |
5124 | -- view may be derived from a private type whose own full | |
5125 | -- view has discriminants. | |
5126 | ||
5127 | elsif Is_Private_Type (T) then | |
5128 | if Present (Full_View (T)) | |
5129 | and then Has_Discriminants (Full_View (T)) | |
5130 | then | |
5131 | Dis := True; | |
5132 | Typ := Full_View (T); | |
5133 | ||
5134 | elsif Present (Underlying_Full_View (T)) | |
5135 | and then Has_Discriminants (Underlying_Full_View (T)) | |
5136 | then | |
5137 | Dis := True; | |
5138 | Typ := Underlying_Full_View (T); | |
5139 | end if; | |
20b5d666 | 5140 | end if; |
70482933 | 5141 | |
b4592168 | 5142 | if Dis then |
26bff3d9 | 5143 | |
b4592168 | 5144 | -- If the allocated object will be constrained by the |
685094bf RD |
5145 | -- default values for discriminants, then build a subtype |
5146 | -- with those defaults, and change the allocated subtype | |
5147 | -- to that. Note that this happens in fewer cases in Ada | |
5148 | -- 2005 (AI-363). | |
26bff3d9 | 5149 | |
b4592168 GD |
5150 | if not Is_Constrained (Typ) |
5151 | and then Present (Discriminant_Default_Value | |
df3e68b1 | 5152 | (First_Discriminant (Typ))) |
0791fbe9 | 5153 | and then (Ada_Version < Ada_2005 |
cc96a1b8 | 5154 | or else not |
0fbcb11c ES |
5155 | Object_Type_Has_Constrained_Partial_View |
5156 | (Typ, Current_Scope)) | |
20b5d666 | 5157 | then |
b4592168 | 5158 | Typ := Build_Default_Subtype (Typ, N); |
e4494292 | 5159 | Set_Expression (N, New_Occurrence_Of (Typ, Loc)); |
20b5d666 JM |
5160 | end if; |
5161 | ||
b4592168 GD |
5162 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
5163 | while Present (Discr) loop | |
5164 | Nod := Node (Discr); | |
5165 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 5166 | |
b4592168 GD |
5167 | -- AI-416: when the discriminant constraint is an |
5168 | -- anonymous access type make sure an accessibility | |
5169 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 5170 | |
0791fbe9 | 5171 | if Ada_Version >= Ada_2005 |
b4592168 GD |
5172 | and then |
5173 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
5174 | then | |
e84e11ba GD |
5175 | Apply_Accessibility_Check |
5176 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 5177 | end if; |
20b5d666 | 5178 | |
b4592168 GD |
5179 | Next_Elmt (Discr); |
5180 | end loop; | |
5181 | end if; | |
5182 | end; | |
70482933 | 5183 | |
4b985e20 | 5184 | -- We set the allocator as analyzed so that when we analyze |
9b16cb57 RD |
5185 | -- the if expression node, we do not get an unwanted recursive |
5186 | -- expansion of the allocator expression. | |
70482933 | 5187 | |
b4592168 GD |
5188 | Set_Analyzed (N, True); |
5189 | Nod := Relocate_Node (N); | |
70482933 | 5190 | |
b4592168 | 5191 | -- Here is the transformation: |
ca5af305 AC |
5192 | -- input: new Ctrl_Typ |
5193 | -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ; | |
5194 | -- Ctrl_TypIP (Temp.all, ...); | |
5195 | -- [Deep_]Initialize (Temp.all); | |
70482933 | 5196 | |
ca5af305 AC |
5197 | -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and |
5198 | -- is the subtype of the allocator. | |
70482933 | 5199 | |
b4592168 GD |
5200 | Temp_Decl := |
5201 | Make_Object_Declaration (Loc, | |
5202 | Defining_Identifier => Temp, | |
5203 | Constant_Present => True, | |
e4494292 | 5204 | Object_Definition => New_Occurrence_Of (Temp_Type, Loc), |
b4592168 | 5205 | Expression => Nod); |
70482933 | 5206 | |
b4592168 GD |
5207 | Set_Assignment_OK (Temp_Decl); |
5208 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 5209 | |
ca5af305 | 5210 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 5211 | |
b4592168 GD |
5212 | -- If the designated type is a task type or contains tasks, |
5213 | -- create block to activate created tasks, and insert | |
5214 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 5215 | |
b4592168 GD |
5216 | if Has_Task (T) then |
5217 | declare | |
5218 | L : constant List_Id := New_List; | |
5219 | Blk : Node_Id; | |
5220 | begin | |
5221 | Build_Task_Allocate_Block (L, Nod, Args); | |
5222 | Blk := Last (L); | |
5223 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
5224 | Insert_Actions (N, L); | |
5225 | end; | |
70482933 | 5226 | |
b4592168 GD |
5227 | else |
5228 | Insert_Action (N, | |
5229 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 5230 | Name => New_Occurrence_Of (Init, Loc), |
b4592168 GD |
5231 | Parameter_Associations => Args)); |
5232 | end if; | |
70482933 | 5233 | |
048e5cef | 5234 | if Needs_Finalization (T) then |
70482933 | 5235 | |
df3e68b1 HK |
5236 | -- Generate: |
5237 | -- [Deep_]Initialize (Init_Arg1); | |
70482933 | 5238 | |
2168d7cc | 5239 | Init_Call := |
243cae0a AC |
5240 | Make_Init_Call |
5241 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
2168d7cc AC |
5242 | Typ => T); |
5243 | ||
5244 | -- Guard against a missing [Deep_]Initialize when the | |
5245 | -- designated type was not properly frozen. | |
5246 | ||
5247 | if Present (Init_Call) then | |
5248 | Insert_Action (N, Init_Call); | |
5249 | end if; | |
70482933 RK |
5250 | end if; |
5251 | ||
e4494292 | 5252 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
b4592168 GD |
5253 | Analyze_And_Resolve (N, PtrT); |
5254 | end if; | |
26bff3d9 JM |
5255 | end if; |
5256 | end; | |
f82944b7 | 5257 | |
26bff3d9 JM |
5258 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
5259 | -- object that has been rewritten as a reference, we displace "this" | |
5260 | -- to reference properly its secondary dispatch table. | |
5261 | ||
533369aa | 5262 | if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then |
26bff3d9 | 5263 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
5264 | end if; |
5265 | ||
fbf5a39b AC |
5266 | exception |
5267 | when RE_Not_Available => | |
5268 | return; | |
70482933 RK |
5269 | end Expand_N_Allocator; |
5270 | ||
5271 | ----------------------- | |
5272 | -- Expand_N_And_Then -- | |
5273 | ----------------------- | |
5274 | ||
5875f8d6 AC |
5275 | procedure Expand_N_And_Then (N : Node_Id) |
5276 | renames Expand_Short_Circuit_Operator; | |
70482933 | 5277 | |
19d846a0 RD |
5278 | ------------------------------ |
5279 | -- Expand_N_Case_Expression -- | |
5280 | ------------------------------ | |
5281 | ||
5282 | procedure Expand_N_Case_Expression (N : Node_Id) is | |
e44e8a5e AC |
5283 | function Is_Copy_Type (Typ : Entity_Id) return Boolean; |
5284 | -- Return True if we can copy objects of this type when expanding a case | |
5285 | -- expression. | |
5286 | ||
5287 | ------------------ | |
5288 | -- Is_Copy_Type -- | |
5289 | ------------------ | |
5290 | ||
5291 | function Is_Copy_Type (Typ : Entity_Id) return Boolean is | |
5292 | begin | |
e0666fc6 | 5293 | -- If Minimize_Expression_With_Actions is True, we can afford to copy |
e44e8a5e AC |
5294 | -- large objects, as long as they are constrained and not limited. |
5295 | ||
5296 | return | |
5297 | Is_Elementary_Type (Underlying_Type (Typ)) | |
5298 | or else | |
5299 | (Minimize_Expression_With_Actions | |
5300 | and then Is_Constrained (Underlying_Type (Typ)) | |
5b4ce2a0 | 5301 | and then not Is_Limited_Type (Underlying_Type (Typ))); |
e44e8a5e AC |
5302 | end Is_Copy_Type; |
5303 | ||
5304 | -- Local variables | |
5305 | ||
5306 | Loc : constant Source_Ptr := Sloc (N); | |
5307 | Par : constant Node_Id := Parent (N); | |
5308 | Typ : constant Entity_Id := Etype (N); | |
5309 | ||
0da343bc AC |
5310 | Acts : List_Id; |
5311 | Alt : Node_Id; | |
5312 | Case_Stmt : Node_Id; | |
5313 | Decl : Node_Id; | |
5314 | Expr : Node_Id; | |
5315 | Target : Entity_Id; | |
5316 | Target_Typ : Entity_Id; | |
5317 | ||
5318 | In_Predicate : Boolean := False; | |
5319 | -- Flag set when the case expression appears within a predicate | |
5320 | ||
be035558 | 5321 | Optimize_Return_Stmt : Boolean := False; |
0da343bc AC |
5322 | -- Flag set when the case expression can be optimized in the context of |
5323 | -- a simple return statement. | |
19d846a0 | 5324 | |
e44e8a5e AC |
5325 | -- Start of processing for Expand_N_Case_Expression |
5326 | ||
19d846a0 | 5327 | begin |
b6b5cca8 AC |
5328 | -- Check for MINIMIZED/ELIMINATED overflow mode |
5329 | ||
5330 | if Minimized_Eliminated_Overflow_Check (N) then | |
4b1c4f20 RD |
5331 | Apply_Arithmetic_Overflow_Check (N); |
5332 | return; | |
5333 | end if; | |
5334 | ||
21d7ef70 AC |
5335 | -- If the case expression is a predicate specification, and the type |
5336 | -- to which it applies has a static predicate aspect, do not expand, | |
5337 | -- because it will be converted to the proper predicate form later. | |
ff1f1705 AC |
5338 | |
5339 | if Ekind_In (Current_Scope, E_Function, E_Procedure) | |
5340 | and then Is_Predicate_Function (Current_Scope) | |
5341 | then | |
be035558 AC |
5342 | In_Predicate := True; |
5343 | ||
5344 | if Has_Static_Predicate_Aspect (Etype (First_Entity (Current_Scope))) | |
5345 | then | |
5346 | return; | |
5347 | end if; | |
ff1f1705 AC |
5348 | end if; |
5349 | ||
0da343bc | 5350 | -- When the type of the case expression is elementary, expand |
19d846a0 | 5351 | |
0da343bc | 5352 | -- (case X is when A => AX, when B => BX ...) |
19d846a0 | 5353 | |
0da343bc | 5354 | -- into |
19d846a0 RD |
5355 | |
5356 | -- do | |
0da343bc | 5357 | -- Target : Typ; |
19d846a0 RD |
5358 | -- case X is |
5359 | -- when A => | |
be035558 | 5360 | -- Target := AX; |
19d846a0 | 5361 | -- when B => |
be035558 | 5362 | -- Target := BX; |
19d846a0 RD |
5363 | -- ... |
5364 | -- end case; | |
be035558 AC |
5365 | -- in Target end; |
5366 | ||
0da343bc | 5367 | -- In all other cases expand into |
19d846a0 RD |
5368 | |
5369 | -- do | |
0da343bc | 5370 | -- type Ptr_Typ is access all Typ; |
be035558 | 5371 | -- Target : Ptr_Typ; |
19d846a0 RD |
5372 | -- case X is |
5373 | -- when A => | |
be035558 | 5374 | -- Target := AX'Unrestricted_Access; |
19d846a0 | 5375 | -- when B => |
be035558 | 5376 | -- Target := BX'Unrestricted_Access; |
19d846a0 RD |
5377 | -- ... |
5378 | -- end case; | |
be035558 | 5379 | -- in Target.all end; |
19d846a0 | 5380 | |
0da343bc AC |
5381 | -- This approach avoids extra copies of potentially large objects. It |
5382 | -- also allows handling of values of limited or unconstrained types. | |
e0666fc6 | 5383 | -- Note that we do the copy also for constrained, nonlimited types |
e44e8a5e AC |
5384 | -- when minimizing expressions with actions (e.g. when generating C |
5385 | -- code) since it allows us to do the optimization below in more cases. | |
0da343bc AC |
5386 | |
5387 | -- Small optimization: when the case expression appears in the context | |
5388 | -- of a simple return statement, expand into | |
5389 | ||
5390 | -- case X is | |
5391 | -- when A => | |
5392 | -- return AX; | |
5393 | -- when B => | |
5394 | -- return BX; | |
5395 | -- ... | |
5396 | -- end case; | |
5397 | ||
be035558 | 5398 | Case_Stmt := |
19d846a0 RD |
5399 | Make_Case_Statement (Loc, |
5400 | Expression => Expression (N), | |
5401 | Alternatives => New_List); | |
5402 | ||
414c6563 AC |
5403 | -- Preserve the original context for which the case statement is being |
5404 | -- generated. This is needed by the finalization machinery to prevent | |
5405 | -- the premature finalization of controlled objects found within the | |
5406 | -- case statement. | |
5407 | ||
be035558 AC |
5408 | Set_From_Conditional_Expression (Case_Stmt); |
5409 | Acts := New_List; | |
19d846a0 | 5410 | |
e44e8a5e | 5411 | -- Scalar/Copy case |
19d846a0 | 5412 | |
e44e8a5e | 5413 | if Is_Copy_Type (Typ) then |
be035558 AC |
5414 | Target_Typ := Typ; |
5415 | ||
5416 | -- ??? Do not perform the optimization when the return statement is | |
e0666fc6 | 5417 | -- within a predicate function, as this causes spurious errors. Could |
0da343bc AC |
5418 | -- this be a possible mismatch in handling this case somewhere else |
5419 | -- in semantic analysis? | |
be035558 | 5420 | |
0da343bc AC |
5421 | Optimize_Return_Stmt := |
5422 | Nkind (Par) = N_Simple_Return_Statement and then not In_Predicate; | |
5423 | ||
5424 | -- Otherwise create an access type to handle the general case using | |
5425 | -- 'Unrestricted_Access. | |
5426 | ||
5427 | -- Generate: | |
5428 | -- type Ptr_Typ is access all Typ; | |
19d846a0 RD |
5429 | |
5430 | else | |
211e7410 AC |
5431 | if Generate_C_Code then |
5432 | ||
0c3ef0cc GD |
5433 | -- We cannot ensure that correct C code will be generated if any |
5434 | -- temporary is created down the line (to e.g. handle checks or | |
5435 | -- capture values) since we might end up with dangling references | |
5436 | -- to local variables, so better be safe and reject the construct. | |
211e7410 AC |
5437 | |
5438 | Error_Msg_N | |
5439 | ("case expression too complex, use case statement instead", N); | |
5440 | end if; | |
5441 | ||
0da343bc AC |
5442 | Target_Typ := Make_Temporary (Loc, 'P'); |
5443 | ||
be035558 | 5444 | Append_To (Acts, |
19d846a0 | 5445 | Make_Full_Type_Declaration (Loc, |
0da343bc | 5446 | Defining_Identifier => Target_Typ, |
11d59a86 | 5447 | Type_Definition => |
19d846a0 | 5448 | Make_Access_To_Object_Definition (Loc, |
11d59a86 | 5449 | All_Present => True, |
e4494292 | 5450 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); |
19d846a0 RD |
5451 | end if; |
5452 | ||
0da343bc AC |
5453 | -- Create the declaration of the target which captures the value of the |
5454 | -- expression. | |
5455 | ||
5456 | -- Generate: | |
5457 | -- Target : [Ptr_]Typ; | |
5458 | ||
be035558 AC |
5459 | if not Optimize_Return_Stmt then |
5460 | Target := Make_Temporary (Loc, 'T'); | |
27a8f150 | 5461 | |
be035558 AC |
5462 | Decl := |
5463 | Make_Object_Declaration (Loc, | |
5464 | Defining_Identifier => Target, | |
5465 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc)); | |
5466 | Set_No_Initialization (Decl); | |
0da343bc | 5467 | |
be035558 AC |
5468 | Append_To (Acts, Decl); |
5469 | end if; | |
19d846a0 | 5470 | |
0da343bc | 5471 | -- Process the alternatives |
19d846a0 RD |
5472 | |
5473 | Alt := First (Alternatives (N)); | |
5474 | while Present (Alt) loop | |
5475 | declare | |
be035558 AC |
5476 | Alt_Expr : Node_Id := Expression (Alt); |
5477 | Alt_Loc : constant Source_Ptr := Sloc (Alt_Expr); | |
5b4ce2a0 | 5478 | LHS : Node_Id; |
be035558 | 5479 | Stmts : List_Id; |
19d846a0 RD |
5480 | |
5481 | begin | |
0da343bc AC |
5482 | -- Take the unrestricted access of the expression value for non- |
5483 | -- scalar types. This approach avoids big copies and covers the | |
5484 | -- limited and unconstrained cases. | |
5485 | ||
5486 | -- Generate: | |
5487 | -- AX'Unrestricted_Access | |
05dbd302 | 5488 | |
e44e8a5e | 5489 | if not Is_Copy_Type (Typ) then |
be035558 AC |
5490 | Alt_Expr := |
5491 | Make_Attribute_Reference (Alt_Loc, | |
5492 | Prefix => Relocate_Node (Alt_Expr), | |
19d846a0 RD |
5493 | Attribute_Name => Name_Unrestricted_Access); |
5494 | end if; | |
5495 | ||
0da343bc AC |
5496 | -- Generate: |
5497 | -- return AX['Unrestricted_Access]; | |
5498 | ||
be035558 AC |
5499 | if Optimize_Return_Stmt then |
5500 | Stmts := New_List ( | |
5501 | Make_Simple_Return_Statement (Alt_Loc, | |
5502 | Expression => Alt_Expr)); | |
0da343bc AC |
5503 | |
5504 | -- Generate: | |
5505 | -- Target := AX['Unrestricted_Access]; | |
5506 | ||
be035558 | 5507 | else |
5b4ce2a0 HK |
5508 | LHS := New_Occurrence_Of (Target, Loc); |
5509 | Set_Assignment_OK (LHS); | |
5510 | ||
be035558 AC |
5511 | Stmts := New_List ( |
5512 | Make_Assignment_Statement (Alt_Loc, | |
5b4ce2a0 | 5513 | Name => LHS, |
be035558 AC |
5514 | Expression => Alt_Expr)); |
5515 | end if; | |
eaed0c37 AC |
5516 | |
5517 | -- Propagate declarations inserted in the node by Insert_Actions | |
5518 | -- (for example, temporaries generated to remove side effects). | |
5519 | -- These actions must remain attached to the alternative, given | |
5520 | -- that they are generated by the corresponding expression. | |
5521 | ||
be035558 AC |
5522 | if Present (Actions (Alt)) then |
5523 | Prepend_List (Actions (Alt), Stmts); | |
eaed0c37 AC |
5524 | end if; |
5525 | ||
937e9676 AC |
5526 | -- Finalize any transient objects on exit from the alternative. |
5527 | -- This is done only in the return optimization case because | |
5528 | -- otherwise the case expression is converted into an expression | |
5529 | -- with actions which already contains this form of processing. | |
0da343bc AC |
5530 | |
5531 | if Optimize_Return_Stmt then | |
5532 | Process_If_Case_Statements (N, Stmts); | |
5533 | end if; | |
5534 | ||
19d846a0 | 5535 | Append_To |
be035558 | 5536 | (Alternatives (Case_Stmt), |
19d846a0 RD |
5537 | Make_Case_Statement_Alternative (Sloc (Alt), |
5538 | Discrete_Choices => Discrete_Choices (Alt), | |
be035558 | 5539 | Statements => Stmts)); |
19d846a0 RD |
5540 | end; |
5541 | ||
5542 | Next (Alt); | |
5543 | end loop; | |
5544 | ||
0da343bc | 5545 | -- Rewrite the parent return statement as a case statement |
be035558 AC |
5546 | |
5547 | if Optimize_Return_Stmt then | |
be035558 AC |
5548 | Rewrite (Par, Case_Stmt); |
5549 | Analyze (Par); | |
be035558 | 5550 | |
0da343bc | 5551 | -- Otherwise convert the case expression into an expression with actions |
19d846a0 | 5552 | |
19d846a0 | 5553 | else |
0da343bc | 5554 | Append_To (Acts, Case_Stmt); |
19d846a0 | 5555 | |
e44e8a5e | 5556 | if Is_Copy_Type (Typ) then |
0da343bc | 5557 | Expr := New_Occurrence_Of (Target, Loc); |
19d846a0 | 5558 | |
0da343bc AC |
5559 | else |
5560 | Expr := | |
5561 | Make_Explicit_Dereference (Loc, | |
5562 | Prefix => New_Occurrence_Of (Target, Loc)); | |
5563 | end if; | |
5564 | ||
5565 | -- Generate: | |
5566 | -- do | |
5567 | -- ... | |
5568 | -- in Target[.all] end; | |
5569 | ||
5570 | Rewrite (N, | |
5571 | Make_Expression_With_Actions (Loc, | |
5572 | Expression => Expr, | |
5573 | Actions => Acts)); | |
5574 | ||
5575 | Analyze_And_Resolve (N, Typ); | |
5576 | end if; | |
19d846a0 RD |
5577 | end Expand_N_Case_Expression; |
5578 | ||
9b16cb57 RD |
5579 | ----------------------------------- |
5580 | -- Expand_N_Explicit_Dereference -- | |
5581 | ----------------------------------- | |
5582 | ||
5583 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
5584 | begin | |
5585 | -- Insert explicit dereference call for the checked storage pool case | |
5586 | ||
5587 | Insert_Dereference_Action (Prefix (N)); | |
5588 | ||
5589 | -- If the type is an Atomic type for which Atomic_Sync is enabled, then | |
5590 | -- we set the atomic sync flag. | |
5591 | ||
5592 | if Is_Atomic (Etype (N)) | |
5593 | and then not Atomic_Synchronization_Disabled (Etype (N)) | |
5594 | then | |
5595 | Activate_Atomic_Synchronization (N); | |
5596 | end if; | |
5597 | end Expand_N_Explicit_Dereference; | |
5598 | ||
5599 | -------------------------------------- | |
5600 | -- Expand_N_Expression_With_Actions -- | |
5601 | -------------------------------------- | |
5602 | ||
5603 | procedure Expand_N_Expression_With_Actions (N : Node_Id) is | |
e3d9f448 AC |
5604 | Acts : constant List_Id := Actions (N); |
5605 | ||
5606 | procedure Force_Boolean_Evaluation (Expr : Node_Id); | |
5607 | -- Force the evaluation of Boolean expression Expr | |
5608 | ||
4c7e0990 | 5609 | function Process_Action (Act : Node_Id) return Traverse_Result; |
b2c28399 | 5610 | -- Inspect and process a single action of an expression_with_actions for |
937e9676 AC |
5611 | -- transient objects. If such objects are found, the routine generates |
5612 | -- code to clean them up when the context of the expression is evaluated | |
5613 | -- or elaborated. | |
9b16cb57 | 5614 | |
e3d9f448 AC |
5615 | ------------------------------ |
5616 | -- Force_Boolean_Evaluation -- | |
5617 | ------------------------------ | |
5618 | ||
5619 | procedure Force_Boolean_Evaluation (Expr : Node_Id) is | |
5620 | Loc : constant Source_Ptr := Sloc (N); | |
5621 | Flag_Decl : Node_Id; | |
5622 | Flag_Id : Entity_Id; | |
5623 | ||
5624 | begin | |
5625 | -- Relocate the expression to the actions list by capturing its value | |
5626 | -- in a Boolean flag. Generate: | |
5627 | -- Flag : constant Boolean := Expr; | |
5628 | ||
5629 | Flag_Id := Make_Temporary (Loc, 'F'); | |
5630 | ||
5631 | Flag_Decl := | |
5632 | Make_Object_Declaration (Loc, | |
5633 | Defining_Identifier => Flag_Id, | |
5634 | Constant_Present => True, | |
5635 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), | |
5636 | Expression => Relocate_Node (Expr)); | |
5637 | ||
5638 | Append (Flag_Decl, Acts); | |
5639 | Analyze (Flag_Decl); | |
5640 | ||
5641 | -- Replace the expression with a reference to the flag | |
5642 | ||
5643 | Rewrite (Expression (N), New_Occurrence_Of (Flag_Id, Loc)); | |
5644 | Analyze (Expression (N)); | |
5645 | end Force_Boolean_Evaluation; | |
5646 | ||
4c7e0990 AC |
5647 | -------------------- |
5648 | -- Process_Action -- | |
5649 | -------------------- | |
5650 | ||
5651 | function Process_Action (Act : Node_Id) return Traverse_Result is | |
4c7e0990 AC |
5652 | begin |
5653 | if Nkind (Act) = N_Object_Declaration | |
5654 | and then Is_Finalizable_Transient (Act, N) | |
5655 | then | |
937e9676 | 5656 | Process_Transient_In_Expression (Act, N, Acts); |
05344a33 | 5657 | return Skip; |
9b16cb57 | 5658 | |
4c7e0990 AC |
5659 | -- Avoid processing temporary function results multiple times when |
5660 | -- dealing with nested expression_with_actions. | |
9b16cb57 | 5661 | |
4c7e0990 AC |
5662 | elsif Nkind (Act) = N_Expression_With_Actions then |
5663 | return Abandon; | |
5664 | ||
b2c28399 AC |
5665 | -- Do not process temporary function results in loops. This is done |
5666 | -- by Expand_N_Loop_Statement and Build_Finalizer. | |
4c7e0990 AC |
5667 | |
5668 | elsif Nkind (Act) = N_Loop_Statement then | |
5669 | return Abandon; | |
9b16cb57 RD |
5670 | end if; |
5671 | ||
4c7e0990 AC |
5672 | return OK; |
5673 | end Process_Action; | |
9b16cb57 | 5674 | |
4c7e0990 | 5675 | procedure Process_Single_Action is new Traverse_Proc (Process_Action); |
9b16cb57 RD |
5676 | |
5677 | -- Local variables | |
5678 | ||
e3d9f448 | 5679 | Act : Node_Id; |
9b16cb57 RD |
5680 | |
5681 | -- Start of processing for Expand_N_Expression_With_Actions | |
5682 | ||
5683 | begin | |
4b17187f AC |
5684 | -- Do not evaluate the expression when it denotes an entity because the |
5685 | -- expression_with_actions node will be replaced by the reference. | |
5686 | ||
e3d9f448 | 5687 | if Is_Entity_Name (Expression (N)) then |
4b17187f AC |
5688 | null; |
5689 | ||
5690 | -- Do not evaluate the expression when there are no actions because the | |
5691 | -- expression_with_actions node will be replaced by the expression. | |
5692 | ||
5693 | elsif No (Acts) or else Is_Empty_List (Acts) then | |
5694 | null; | |
5695 | ||
5696 | -- Force the evaluation of the expression by capturing its value in a | |
937e9676 AC |
5697 | -- temporary. This ensures that aliases of transient objects do not leak |
5698 | -- to the expression of the expression_with_actions node: | |
4b17187f AC |
5699 | |
5700 | -- do | |
7782ff67 | 5701 | -- Trans_Id : Ctrl_Typ := ...; |
4b17187f AC |
5702 | -- Alias : ... := Trans_Id; |
5703 | -- in ... Alias ... end; | |
5704 | ||
5705 | -- In the example above, Trans_Id cannot be finalized at the end of the | |
5706 | -- actions list because this may affect the alias and the final value of | |
5707 | -- the expression_with_actions. Forcing the evaluation encapsulates the | |
5708 | -- reference to the Alias within the actions list: | |
5709 | ||
5710 | -- do | |
7782ff67 | 5711 | -- Trans_Id : Ctrl_Typ := ...; |
4b17187f AC |
5712 | -- Alias : ... := Trans_Id; |
5713 | -- Val : constant Boolean := ... Alias ...; | |
5714 | -- <finalize Trans_Id> | |
5715 | -- in Val end; | |
e0f63680 | 5716 | |
e3d9f448 | 5717 | -- Once this transformation is performed, it is safe to finalize the |
937e9676 | 5718 | -- transient object at the end of the actions list. |
e3d9f448 AC |
5719 | |
5720 | -- Note that Force_Evaluation does not remove side effects in operators | |
5721 | -- because it assumes that all operands are evaluated and side effect | |
5722 | -- free. This is not the case when an operand depends implicitly on the | |
937e9676 | 5723 | -- transient object through the use of access types. |
e3d9f448 AC |
5724 | |
5725 | elsif Is_Boolean_Type (Etype (Expression (N))) then | |
5726 | Force_Boolean_Evaluation (Expression (N)); | |
5727 | ||
6031f544 | 5728 | -- The expression of an expression_with_actions node may not necessarily |
e3d9f448 AC |
5729 | -- be Boolean when the node appears in an if expression. In this case do |
5730 | -- the usual forced evaluation to encapsulate potential aliasing. | |
4b17187f AC |
5731 | |
5732 | else | |
e3d9f448 | 5733 | Force_Evaluation (Expression (N)); |
4b17187f AC |
5734 | end if; |
5735 | ||
937e9676 AC |
5736 | -- Process all transient objects found within the actions of the EWA |
5737 | -- node. | |
4b17187f AC |
5738 | |
5739 | Act := First (Acts); | |
e0f63680 AC |
5740 | while Present (Act) loop |
5741 | Process_Single_Action (Act); | |
5742 | Next (Act); | |
5743 | end loop; | |
5744 | ||
ebdaa81b | 5745 | -- Deal with case where there are no actions. In this case we simply |
5a521b8a | 5746 | -- rewrite the node with its expression since we don't need the actions |
ebdaa81b AC |
5747 | -- and the specification of this node does not allow a null action list. |
5748 | ||
5a521b8a AC |
5749 | -- Note: we use Rewrite instead of Replace, because Codepeer is using |
5750 | -- the expanded tree and relying on being able to retrieve the original | |
5751 | -- tree in cases like this. This raises a whole lot of issues of whether | |
5752 | -- we have problems elsewhere, which will be addressed in the future??? | |
5753 | ||
4b17187f | 5754 | if Is_Empty_List (Acts) then |
5a521b8a | 5755 | Rewrite (N, Relocate_Node (Expression (N))); |
ebdaa81b | 5756 | end if; |
9b16cb57 RD |
5757 | end Expand_N_Expression_With_Actions; |
5758 | ||
5759 | ---------------------------- | |
5760 | -- Expand_N_If_Expression -- | |
5761 | ---------------------------- | |
70482933 | 5762 | |
4b985e20 | 5763 | -- Deal with limited types and condition actions |
70482933 | 5764 | |
9b16cb57 | 5765 | procedure Expand_N_If_Expression (N : Node_Id) is |
0da343bc AC |
5766 | Cond : constant Node_Id := First (Expressions (N)); |
5767 | Loc : constant Source_Ptr := Sloc (N); | |
5768 | Thenx : constant Node_Id := Next (Cond); | |
5769 | Elsex : constant Node_Id := Next (Thenx); | |
5770 | Typ : constant Entity_Id := Etype (N); | |
c471e2da | 5771 | |
3cebd1c0 | 5772 | Actions : List_Id; |
602a7ec0 | 5773 | Decl : Node_Id; |
3cebd1c0 | 5774 | Expr : Node_Id; |
602a7ec0 AC |
5775 | New_If : Node_Id; |
5776 | New_N : Node_Id; | |
70482933 RK |
5777 | |
5778 | begin | |
b6b5cca8 AC |
5779 | -- Check for MINIMIZED/ELIMINATED overflow mode |
5780 | ||
5781 | if Minimized_Eliminated_Overflow_Check (N) then | |
5782 | Apply_Arithmetic_Overflow_Check (N); | |
5783 | return; | |
5784 | end if; | |
5785 | ||
602a7ec0 | 5786 | -- Fold at compile time if condition known. We have already folded |
9b16cb57 RD |
5787 | -- static if expressions, but it is possible to fold any case in which |
5788 | -- the condition is known at compile time, even though the result is | |
5789 | -- non-static. | |
602a7ec0 AC |
5790 | |
5791 | -- Note that we don't do the fold of such cases in Sem_Elab because | |
5792 | -- it can cause infinite loops with the expander adding a conditional | |
5793 | -- expression, and Sem_Elab circuitry removing it repeatedly. | |
5794 | ||
5795 | if Compile_Time_Known_Value (Cond) then | |
f916243b AC |
5796 | declare |
5797 | function Fold_Known_Value (Cond : Node_Id) return Boolean; | |
0da343bc AC |
5798 | -- Fold at compile time. Assumes condition known. Return True if |
5799 | -- folding occurred, meaning we're done. | |
602a7ec0 | 5800 | |
f916243b AC |
5801 | ---------------------- |
5802 | -- Fold_Known_Value -- | |
5803 | ---------------------- | |
ae77c68b | 5804 | |
f916243b AC |
5805 | function Fold_Known_Value (Cond : Node_Id) return Boolean is |
5806 | begin | |
5807 | if Is_True (Expr_Value (Cond)) then | |
5808 | Expr := Thenx; | |
5809 | Actions := Then_Actions (N); | |
5810 | else | |
5811 | Expr := Elsex; | |
5812 | Actions := Else_Actions (N); | |
5813 | end if; | |
602a7ec0 | 5814 | |
f916243b | 5815 | Remove (Expr); |
602a7ec0 | 5816 | |
f916243b AC |
5817 | if Present (Actions) then |
5818 | ||
7548f2cb AC |
5819 | -- To minimize the use of Expression_With_Actions, just skip |
5820 | -- the optimization as it is not critical for correctness. | |
f916243b AC |
5821 | |
5822 | if Minimize_Expression_With_Actions then | |
5823 | return False; | |
5824 | end if; | |
5825 | ||
5826 | Rewrite (N, | |
5827 | Make_Expression_With_Actions (Loc, | |
5828 | Expression => Relocate_Node (Expr), | |
5829 | Actions => Actions)); | |
5830 | Analyze_And_Resolve (N, Typ); | |
5831 | ||
5832 | else | |
5833 | Rewrite (N, Relocate_Node (Expr)); | |
5834 | end if; | |
5835 | ||
5836 | -- Note that the result is never static (legitimate cases of | |
5837 | -- static if expressions were folded in Sem_Eval). | |
5838 | ||
5839 | Set_Is_Static_Expression (N, False); | |
5840 | return True; | |
5841 | end Fold_Known_Value; | |
5842 | ||
5843 | begin | |
5844 | if Fold_Known_Value (Cond) then | |
5845 | return; | |
5846 | end if; | |
5847 | end; | |
602a7ec0 AC |
5848 | end if; |
5849 | ||
113a9fb6 AC |
5850 | -- If the type is limited, and the back end does not handle limited |
5851 | -- types, then we expand as follows to avoid the possibility of | |
5852 | -- improper copying. | |
ac7120ce | 5853 | |
c471e2da AC |
5854 | -- type Ptr is access all Typ; |
5855 | -- Cnn : Ptr; | |
ac7120ce RD |
5856 | -- if cond then |
5857 | -- <<then actions>> | |
5858 | -- Cnn := then-expr'Unrestricted_Access; | |
5859 | -- else | |
5860 | -- <<else actions>> | |
5861 | -- Cnn := else-expr'Unrestricted_Access; | |
5862 | -- end if; | |
5863 | ||
9b16cb57 | 5864 | -- and replace the if expression by a reference to Cnn.all. |
ac7120ce | 5865 | |
305caf42 AC |
5866 | -- This special case can be skipped if the back end handles limited |
5867 | -- types properly and ensures that no incorrect copies are made. | |
5868 | ||
5869 | if Is_By_Reference_Type (Typ) | |
5870 | and then not Back_End_Handles_Limited_Types | |
5871 | then | |
b2c28399 AC |
5872 | -- When the "then" or "else" expressions involve controlled function |
5873 | -- calls, generated temporaries are chained on the corresponding list | |
5874 | -- of actions. These temporaries need to be finalized after the if | |
5875 | -- expression is evaluated. | |
3cebd1c0 | 5876 | |
0da343bc AC |
5877 | Process_If_Case_Statements (N, Then_Actions (N)); |
5878 | Process_If_Case_Statements (N, Else_Actions (N)); | |
3cebd1c0 | 5879 | |
3fc40cd7 PMR |
5880 | declare |
5881 | Cnn : constant Entity_Id := Make_Temporary (Loc, 'C', N); | |
5882 | Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A'); | |
e201023c | 5883 | |
3fc40cd7 PMR |
5884 | begin |
5885 | -- Generate: | |
5886 | -- type Ann is access all Typ; | |
3cebd1c0 | 5887 | |
3fc40cd7 PMR |
5888 | Insert_Action (N, |
5889 | Make_Full_Type_Declaration (Loc, | |
5890 | Defining_Identifier => Ptr_Typ, | |
5891 | Type_Definition => | |
5892 | Make_Access_To_Object_Definition (Loc, | |
5893 | All_Present => True, | |
5894 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); | |
3cebd1c0 | 5895 | |
3fc40cd7 PMR |
5896 | -- Generate: |
5897 | -- Cnn : Ann; | |
3cebd1c0 | 5898 | |
3fc40cd7 PMR |
5899 | Decl := |
5900 | Make_Object_Declaration (Loc, | |
5901 | Defining_Identifier => Cnn, | |
5902 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc)); | |
3cebd1c0 | 5903 | |
3fc40cd7 PMR |
5904 | -- Generate: |
5905 | -- if Cond then | |
5906 | -- Cnn := <Thenx>'Unrestricted_Access; | |
5907 | -- else | |
5908 | -- Cnn := <Elsex>'Unrestricted_Access; | |
5909 | -- end if; | |
3cebd1c0 | 5910 | |
3fc40cd7 PMR |
5911 | New_If := |
5912 | Make_Implicit_If_Statement (N, | |
5913 | Condition => Relocate_Node (Cond), | |
5914 | Then_Statements => New_List ( | |
5915 | Make_Assignment_Statement (Sloc (Thenx), | |
5916 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
5917 | Expression => | |
5918 | Make_Attribute_Reference (Loc, | |
5919 | Prefix => Relocate_Node (Thenx), | |
5920 | Attribute_Name => Name_Unrestricted_Access))), | |
3cebd1c0 | 5921 | |
3fc40cd7 PMR |
5922 | Else_Statements => New_List ( |
5923 | Make_Assignment_Statement (Sloc (Elsex), | |
5924 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
5925 | Expression => | |
5926 | Make_Attribute_Reference (Loc, | |
5927 | Prefix => Relocate_Node (Elsex), | |
5928 | Attribute_Name => Name_Unrestricted_Access)))); | |
5929 | ||
5930 | -- Preserve the original context for which the if statement is | |
5931 | -- being generated. This is needed by the finalization machinery | |
5932 | -- to prevent the premature finalization of controlled objects | |
5933 | -- found within the if statement. | |
5934 | ||
5935 | Set_From_Conditional_Expression (New_If); | |
5936 | ||
5937 | New_N := | |
5938 | Make_Explicit_Dereference (Loc, | |
5939 | Prefix => New_Occurrence_Of (Cnn, Loc)); | |
5940 | end; | |
fb1949a0 | 5941 | |
113a9fb6 AC |
5942 | -- If the result is an unconstrained array and the if expression is in a |
5943 | -- context other than the initializing expression of the declaration of | |
5944 | -- an object, then we pull out the if expression as follows: | |
5945 | ||
5946 | -- Cnn : constant typ := if-expression | |
5947 | ||
5948 | -- and then replace the if expression with an occurrence of Cnn. This | |
5949 | -- avoids the need in the back end to create on-the-fly variable length | |
5950 | -- temporaries (which it cannot do!) | |
5951 | ||
5952 | -- Note that the test for being in an object declaration avoids doing an | |
5953 | -- unnecessary expansion, and also avoids infinite recursion. | |
5954 | ||
5955 | elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ) | |
5956 | and then (Nkind (Parent (N)) /= N_Object_Declaration | |
5957 | or else Expression (Parent (N)) /= N) | |
5958 | then | |
5959 | declare | |
5960 | Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N); | |
e201023c | 5961 | |
113a9fb6 AC |
5962 | begin |
5963 | Insert_Action (N, | |
5964 | Make_Object_Declaration (Loc, | |
5965 | Defining_Identifier => Cnn, | |
5966 | Constant_Present => True, | |
5967 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
5968 | Expression => Relocate_Node (N), | |
5969 | Has_Init_Expression => True)); | |
5970 | ||
5971 | Rewrite (N, New_Occurrence_Of (Cnn, Loc)); | |
5972 | return; | |
5973 | end; | |
5974 | ||
c471e2da AC |
5975 | -- For other types, we only need to expand if there are other actions |
5976 | -- associated with either branch. | |
5977 | ||
5978 | elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
c471e2da | 5979 | |
0812b84e | 5980 | -- We now wrap the actions into the appropriate expression |
fb1949a0 | 5981 | |
9d4f9832 AC |
5982 | if Minimize_Expression_With_Actions |
5983 | and then (Is_Elementary_Type (Underlying_Type (Typ)) | |
5984 | or else Is_Constrained (Underlying_Type (Typ))) | |
5985 | then | |
f916243b AC |
5986 | -- If we can't use N_Expression_With_Actions nodes, then we insert |
5987 | -- the following sequence of actions (using Insert_Actions): | |
305caf42 | 5988 | |
f916243b AC |
5989 | -- Cnn : typ; |
5990 | -- if cond then | |
5991 | -- <<then actions>> | |
5992 | -- Cnn := then-expr; | |
5993 | -- else | |
5994 | -- <<else actions>> | |
5995 | -- Cnn := else-expr | |
5996 | -- end if; | |
b2c28399 | 5997 | |
f916243b | 5998 | -- and replace the if expression by a reference to Cnn |
305caf42 | 5999 | |
3fc40cd7 PMR |
6000 | declare |
6001 | Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N); | |
e201023c | 6002 | |
3fc40cd7 PMR |
6003 | begin |
6004 | Decl := | |
6005 | Make_Object_Declaration (Loc, | |
6006 | Defining_Identifier => Cnn, | |
6007 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
f916243b | 6008 | |
3fc40cd7 PMR |
6009 | New_If := |
6010 | Make_Implicit_If_Statement (N, | |
6011 | Condition => Relocate_Node (Cond), | |
f916243b | 6012 | |
3fc40cd7 PMR |
6013 | Then_Statements => New_List ( |
6014 | Make_Assignment_Statement (Sloc (Thenx), | |
6015 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
6016 | Expression => Relocate_Node (Thenx))), | |
f916243b | 6017 | |
3fc40cd7 PMR |
6018 | Else_Statements => New_List ( |
6019 | Make_Assignment_Statement (Sloc (Elsex), | |
6020 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
6021 | Expression => Relocate_Node (Elsex)))); | |
f916243b | 6022 | |
3fc40cd7 PMR |
6023 | Set_Assignment_OK (Name (First (Then_Statements (New_If)))); |
6024 | Set_Assignment_OK (Name (First (Else_Statements (New_If)))); | |
f916243b | 6025 | |
3fc40cd7 PMR |
6026 | New_N := New_Occurrence_Of (Cnn, Loc); |
6027 | end; | |
f916243b AC |
6028 | |
6029 | -- Regular path using Expression_With_Actions | |
6030 | ||
6031 | else | |
6032 | if Present (Then_Actions (N)) then | |
6033 | Rewrite (Thenx, | |
6034 | Make_Expression_With_Actions (Sloc (Thenx), | |
6035 | Actions => Then_Actions (N), | |
6036 | Expression => Relocate_Node (Thenx))); | |
6037 | ||
6038 | Set_Then_Actions (N, No_List); | |
6039 | Analyze_And_Resolve (Thenx, Typ); | |
6040 | end if; | |
6041 | ||
6042 | if Present (Else_Actions (N)) then | |
6043 | Rewrite (Elsex, | |
6044 | Make_Expression_With_Actions (Sloc (Elsex), | |
6045 | Actions => Else_Actions (N), | |
6046 | Expression => Relocate_Node (Elsex))); | |
6047 | ||
6048 | Set_Else_Actions (N, No_List); | |
6049 | Analyze_And_Resolve (Elsex, Typ); | |
6050 | end if; | |
6051 | ||
6052 | return; | |
6053 | end if; | |
0812b84e | 6054 | |
b2c28399 AC |
6055 | -- If no actions then no expansion needed, gigi will handle it using the |
6056 | -- same approach as a C conditional expression. | |
305caf42 AC |
6057 | |
6058 | else | |
c471e2da AC |
6059 | return; |
6060 | end if; | |
6061 | ||
305caf42 | 6062 | -- Fall through here for either the limited expansion, or the case of |
e0666fc6 | 6063 | -- inserting actions for nonlimited types. In both these cases, we must |
305caf42 | 6064 | -- move the SLOC of the parent If statement to the newly created one and |
3fc5d116 RD |
6065 | -- change it to the SLOC of the expression which, after expansion, will |
6066 | -- correspond to what is being evaluated. | |
c471e2da | 6067 | |
533369aa | 6068 | if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then |
c471e2da AC |
6069 | Set_Sloc (New_If, Sloc (Parent (N))); |
6070 | Set_Sloc (Parent (N), Loc); | |
6071 | end if; | |
70482933 | 6072 | |
3fc5d116 RD |
6073 | -- Make sure Then_Actions and Else_Actions are appropriately moved |
6074 | -- to the new if statement. | |
6075 | ||
c471e2da AC |
6076 | if Present (Then_Actions (N)) then |
6077 | Insert_List_Before | |
6078 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
70482933 | 6079 | end if; |
c471e2da AC |
6080 | |
6081 | if Present (Else_Actions (N)) then | |
6082 | Insert_List_Before | |
6083 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
6084 | end if; | |
6085 | ||
6086 | Insert_Action (N, Decl); | |
6087 | Insert_Action (N, New_If); | |
6088 | Rewrite (N, New_N); | |
6089 | Analyze_And_Resolve (N, Typ); | |
9b16cb57 | 6090 | end Expand_N_If_Expression; |
35a1c212 | 6091 | |
70482933 RK |
6092 | ----------------- |
6093 | -- Expand_N_In -- | |
6094 | ----------------- | |
6095 | ||
6096 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 | 6097 | Loc : constant Source_Ptr := Sloc (N); |
4818e7b9 | 6098 | Restyp : constant Entity_Id := Etype (N); |
7324bf49 AC |
6099 | Lop : constant Node_Id := Left_Opnd (N); |
6100 | Rop : constant Node_Id := Right_Opnd (N); | |
6101 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 6102 | |
630d30e9 RD |
6103 | procedure Substitute_Valid_Check; |
6104 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
6105 | -- test for the left operand being in range of its subtype. | |
6106 | ||
6107 | ---------------------------- | |
6108 | -- Substitute_Valid_Check -- | |
6109 | ---------------------------- | |
6110 | ||
6111 | procedure Substitute_Valid_Check is | |
356ffab8 AC |
6112 | function Is_OK_Object_Reference (Nod : Node_Id) return Boolean; |
6113 | -- Determine whether arbitrary node Nod denotes a source object that | |
6114 | -- may safely act as prefix of attribute 'Valid. | |
6115 | ||
6116 | ---------------------------- | |
6117 | -- Is_OK_Object_Reference -- | |
6118 | ---------------------------- | |
6119 | ||
6120 | function Is_OK_Object_Reference (Nod : Node_Id) return Boolean is | |
6121 | Obj_Ref : Node_Id; | |
6122 | ||
6123 | begin | |
6124 | -- Inspect the original operand | |
6125 | ||
6126 | Obj_Ref := Original_Node (Nod); | |
6127 | ||
6128 | -- The object reference must be a source construct, otherwise the | |
6129 | -- codefix suggestion may refer to nonexistent code from a user | |
6130 | -- perspective. | |
6131 | ||
6132 | if Comes_From_Source (Obj_Ref) then | |
6133 | ||
6134 | -- Recover the actual object reference. There may be more cases | |
6135 | -- to consider??? | |
6136 | ||
6137 | loop | |
6138 | if Nkind_In (Obj_Ref, N_Type_Conversion, | |
6139 | N_Unchecked_Type_Conversion) | |
6140 | then | |
6141 | Obj_Ref := Expression (Obj_Ref); | |
6142 | else | |
6143 | exit; | |
6144 | end if; | |
6145 | end loop; | |
6146 | ||
6147 | return Is_Object_Reference (Obj_Ref); | |
6148 | end if; | |
6149 | ||
6150 | return False; | |
6151 | end Is_OK_Object_Reference; | |
6152 | ||
6153 | -- Start of processing for Substitute_Valid_Check | |
6154 | ||
630d30e9 | 6155 | begin |
c7532b2d AC |
6156 | Rewrite (N, |
6157 | Make_Attribute_Reference (Loc, | |
6158 | Prefix => Relocate_Node (Lop), | |
6159 | Attribute_Name => Name_Valid)); | |
630d30e9 | 6160 | |
c7532b2d | 6161 | Analyze_And_Resolve (N, Restyp); |
630d30e9 | 6162 | |
356ffab8 AC |
6163 | -- Emit a warning when the left-hand operand of the membership test |
6164 | -- is a source object, otherwise the use of attribute 'Valid would be | |
6165 | -- illegal. The warning is not given when overflow checking is either | |
6166 | -- MINIMIZED or ELIMINATED, as the danger of optimization has been | |
6167 | -- eliminated above. | |
acad3c0a | 6168 | |
356ffab8 AC |
6169 | if Is_OK_Object_Reference (Lop) |
6170 | and then Overflow_Check_Mode not in Minimized_Or_Eliminated | |
6171 | then | |
324ac540 AC |
6172 | Error_Msg_N |
6173 | ("??explicit membership test may be optimized away", N); | |
acad3c0a | 6174 | Error_Msg_N -- CODEFIX |
324ac540 | 6175 | ("\??use ''Valid attribute instead", N); |
acad3c0a | 6176 | end if; |
630d30e9 RD |
6177 | end Substitute_Valid_Check; |
6178 | ||
356ffab8 AC |
6179 | -- Local variables |
6180 | ||
6181 | Ltyp : Entity_Id; | |
6182 | Rtyp : Entity_Id; | |
6183 | ||
630d30e9 RD |
6184 | -- Start of processing for Expand_N_In |
6185 | ||
70482933 | 6186 | begin |
308e6f3a | 6187 | -- If set membership case, expand with separate procedure |
4818e7b9 | 6188 | |
197e4514 | 6189 | if Present (Alternatives (N)) then |
a3068ca6 | 6190 | Expand_Set_Membership (N); |
197e4514 AC |
6191 | return; |
6192 | end if; | |
6193 | ||
4818e7b9 RD |
6194 | -- Not set membership, proceed with expansion |
6195 | ||
6196 | Ltyp := Etype (Left_Opnd (N)); | |
6197 | Rtyp := Etype (Right_Opnd (N)); | |
6198 | ||
5707e389 | 6199 | -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer |
f6194278 RD |
6200 | -- type, then expand with a separate procedure. Note the use of the |
6201 | -- flag No_Minimize_Eliminate to prevent infinite recursion. | |
6202 | ||
a7f1b24f | 6203 | if Overflow_Check_Mode in Minimized_Or_Eliminated |
f6194278 RD |
6204 | and then Is_Signed_Integer_Type (Ltyp) |
6205 | and then not No_Minimize_Eliminate (N) | |
6206 | then | |
6207 | Expand_Membership_Minimize_Eliminate_Overflow (N); | |
6208 | return; | |
6209 | end if; | |
6210 | ||
630d30e9 RD |
6211 | -- Check case of explicit test for an expression in range of its |
6212 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
b6b5cca8 | 6213 | -- test and give a warning for scalar types. |
630d30e9 | 6214 | |
4818e7b9 | 6215 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
6216 | |
6217 | -- Only relevant for source comparisons | |
6218 | ||
6219 | and then Comes_From_Source (N) | |
6220 | ||
6221 | -- In floating-point this is a standard way to check for finite values | |
6222 | -- and using 'Valid would typically be a pessimization. | |
6223 | ||
4818e7b9 | 6224 | and then not Is_Floating_Point_Type (Ltyp) |
b6b5cca8 AC |
6225 | |
6226 | -- Don't give the message unless right operand is a type entity and | |
6227 | -- the type of the left operand matches this type. Note that this | |
6228 | -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow | |
6229 | -- checks have changed the type of the left operand. | |
6230 | ||
630d30e9 | 6231 | and then Nkind (Rop) in N_Has_Entity |
4818e7b9 | 6232 | and then Ltyp = Entity (Rop) |
b6b5cca8 | 6233 | |
b6b5cca8 AC |
6234 | -- Skip this for predicated types, where such expressions are a |
6235 | -- reasonable way of testing if something meets the predicate. | |
6236 | ||
3d6db7f8 | 6237 | and then not Present (Predicate_Function (Ltyp)) |
630d30e9 RD |
6238 | then |
6239 | Substitute_Valid_Check; | |
6240 | return; | |
6241 | end if; | |
6242 | ||
20b5d666 JM |
6243 | -- Do validity check on operands |
6244 | ||
6245 | if Validity_Checks_On and Validity_Check_Operands then | |
6246 | Ensure_Valid (Left_Opnd (N)); | |
6247 | Validity_Check_Range (Right_Opnd (N)); | |
6248 | end if; | |
6249 | ||
630d30e9 | 6250 | -- Case of explicit range |
fbf5a39b AC |
6251 | |
6252 | if Nkind (Rop) = N_Range then | |
6253 | declare | |
630d30e9 RD |
6254 | Lo : constant Node_Id := Low_Bound (Rop); |
6255 | Hi : constant Node_Id := High_Bound (Rop); | |
6256 | ||
6257 | Lo_Orig : constant Node_Id := Original_Node (Lo); | |
6258 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
6259 | ||
c800f862 RD |
6260 | Lcheck : Compare_Result; |
6261 | Ucheck : Compare_Result; | |
fbf5a39b | 6262 | |
d766cee3 RD |
6263 | Warn1 : constant Boolean := |
6264 | Constant_Condition_Warnings | |
c800f862 RD |
6265 | and then Comes_From_Source (N) |
6266 | and then not In_Instance; | |
d766cee3 | 6267 | -- This must be true for any of the optimization warnings, we |
9a0ddeee AC |
6268 | -- clearly want to give them only for source with the flag on. We |
6269 | -- also skip these warnings in an instance since it may be the | |
6270 | -- case that different instantiations have different ranges. | |
d766cee3 RD |
6271 | |
6272 | Warn2 : constant Boolean := | |
6273 | Warn1 | |
6274 | and then Nkind (Original_Node (Rop)) = N_Range | |
6275 | and then Is_Integer_Type (Etype (Lo)); | |
6276 | -- For the case where only one bound warning is elided, we also | |
6277 | -- insist on an explicit range and an integer type. The reason is | |
6278 | -- that the use of enumeration ranges including an end point is | |
9a0ddeee AC |
6279 | -- common, as is the use of a subtype name, one of whose bounds is |
6280 | -- the same as the type of the expression. | |
d766cee3 | 6281 | |
fbf5a39b | 6282 | begin |
c95e0edc | 6283 | -- If test is explicit x'First .. x'Last, replace by valid check |
630d30e9 | 6284 | |
e606088a AC |
6285 | -- Could use some individual comments for this complex test ??? |
6286 | ||
d766cee3 | 6287 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
6288 | |
6289 | -- And left operand is X'First where X matches left operand | |
6290 | -- type (this eliminates cases of type mismatch, including | |
6291 | -- the cases where ELIMINATED/MINIMIZED mode has changed the | |
6292 | -- type of the left operand. | |
6293 | ||
630d30e9 RD |
6294 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
6295 | and then Attribute_Name (Lo_Orig) = Name_First | |
6296 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 6297 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
b6b5cca8 | 6298 | |
cc6f5d75 | 6299 | -- Same tests for right operand |
b6b5cca8 | 6300 | |
630d30e9 RD |
6301 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
6302 | and then Attribute_Name (Hi_Orig) = Name_Last | |
6303 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 6304 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
b6b5cca8 AC |
6305 | |
6306 | -- Relevant only for source cases | |
6307 | ||
630d30e9 RD |
6308 | and then Comes_From_Source (N) |
6309 | then | |
6310 | Substitute_Valid_Check; | |
4818e7b9 | 6311 | goto Leave; |
630d30e9 RD |
6312 | end if; |
6313 | ||
d766cee3 RD |
6314 | -- If bounds of type are known at compile time, and the end points |
6315 | -- are known at compile time and identical, this is another case | |
6316 | -- for substituting a valid test. We only do this for discrete | |
6317 | -- types, since it won't arise in practice for float types. | |
6318 | ||
6319 | if Comes_From_Source (N) | |
6320 | and then Is_Discrete_Type (Ltyp) | |
6321 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
6322 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
6323 | and then Compile_Time_Known_Value (Lo) | |
6324 | and then Compile_Time_Known_Value (Hi) | |
6325 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
6326 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e | 6327 | |
f6194278 RD |
6328 | -- Kill warnings in instances, since they may be cases where we |
6329 | -- have a test in the generic that makes sense with some types | |
6330 | -- and not with other types. | |
94eefd2e | 6331 | |
5b85ad7d PMR |
6332 | -- Similarly, do not rewrite membership as a validity check if |
6333 | -- within the predicate function for the type. | |
6334 | ||
ad277369 ES |
6335 | -- Finally, if the original bounds are type conversions, even |
6336 | -- if they have been folded into constants, there are different | |
6337 | -- types involved and 'Valid is not appropriate. | |
6338 | ||
d766cee3 | 6339 | then |
5b85ad7d PMR |
6340 | if In_Instance |
6341 | or else (Ekind (Current_Scope) = E_Function | |
6342 | and then Is_Predicate_Function (Current_Scope)) | |
6343 | then | |
6344 | null; | |
6345 | ||
ad277369 ES |
6346 | elsif Nkind (Lo_Orig) = N_Type_Conversion |
6347 | or else Nkind (Hi_Orig) = N_Type_Conversion | |
6348 | then | |
6349 | null; | |
6350 | ||
5b85ad7d PMR |
6351 | else |
6352 | Substitute_Valid_Check; | |
6353 | goto Leave; | |
6354 | end if; | |
d766cee3 RD |
6355 | end if; |
6356 | ||
9a0ddeee AC |
6357 | -- If we have an explicit range, do a bit of optimization based on |
6358 | -- range analysis (we may be able to kill one or both checks). | |
630d30e9 | 6359 | |
c800f862 RD |
6360 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
6361 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
6362 | ||
630d30e9 RD |
6363 | -- If either check is known to fail, replace result by False since |
6364 | -- the other check does not matter. Preserve the static flag for | |
6365 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
6366 | |
6367 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 6368 | if Warn1 then |
685bc70f AC |
6369 | Error_Msg_N ("?c?range test optimized away", N); |
6370 | Error_Msg_N ("\?c?value is known to be out of range", N); | |
d766cee3 RD |
6371 | end if; |
6372 | ||
e4494292 | 6373 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 6374 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 6375 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 6376 | goto Leave; |
fbf5a39b | 6377 | |
685094bf RD |
6378 | -- If both checks are known to succeed, replace result by True, |
6379 | -- since we know we are in range. | |
fbf5a39b AC |
6380 | |
6381 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 6382 | if Warn1 then |
685bc70f AC |
6383 | Error_Msg_N ("?c?range test optimized away", N); |
6384 | Error_Msg_N ("\?c?value is known to be in range", N); | |
d766cee3 RD |
6385 | end if; |
6386 | ||
e4494292 | 6387 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 6388 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 6389 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 6390 | goto Leave; |
fbf5a39b | 6391 | |
d766cee3 RD |
6392 | -- If lower bound check succeeds and upper bound check is not |
6393 | -- known to succeed or fail, then replace the range check with | |
6394 | -- a comparison against the upper bound. | |
fbf5a39b AC |
6395 | |
6396 | elsif Lcheck in Compare_GE then | |
94eefd2e | 6397 | if Warn2 and then not In_Instance then |
324ac540 AC |
6398 | Error_Msg_N ("??lower bound test optimized away", Lo); |
6399 | Error_Msg_N ("\??value is known to be in range", Lo); | |
d766cee3 RD |
6400 | end if; |
6401 | ||
fbf5a39b AC |
6402 | Rewrite (N, |
6403 | Make_Op_Le (Loc, | |
6404 | Left_Opnd => Lop, | |
6405 | Right_Opnd => High_Bound (Rop))); | |
4818e7b9 RD |
6406 | Analyze_And_Resolve (N, Restyp); |
6407 | goto Leave; | |
fbf5a39b | 6408 | |
d766cee3 RD |
6409 | -- If upper bound check succeeds and lower bound check is not |
6410 | -- known to succeed or fail, then replace the range check with | |
6411 | -- a comparison against the lower bound. | |
fbf5a39b AC |
6412 | |
6413 | elsif Ucheck in Compare_LE then | |
94eefd2e | 6414 | if Warn2 and then not In_Instance then |
324ac540 AC |
6415 | Error_Msg_N ("??upper bound test optimized away", Hi); |
6416 | Error_Msg_N ("\??value is known to be in range", Hi); | |
d766cee3 RD |
6417 | end if; |
6418 | ||
fbf5a39b AC |
6419 | Rewrite (N, |
6420 | Make_Op_Ge (Loc, | |
6421 | Left_Opnd => Lop, | |
6422 | Right_Opnd => Low_Bound (Rop))); | |
4818e7b9 RD |
6423 | Analyze_And_Resolve (N, Restyp); |
6424 | goto Leave; | |
fbf5a39b | 6425 | end if; |
c800f862 RD |
6426 | |
6427 | -- We couldn't optimize away the range check, but there is one | |
6428 | -- more issue. If we are checking constant conditionals, then we | |
6429 | -- see if we can determine the outcome assuming everything is | |
6430 | -- valid, and if so give an appropriate warning. | |
6431 | ||
6432 | if Warn1 and then not Assume_No_Invalid_Values then | |
6433 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
6434 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
6435 | ||
6436 | -- Result is out of range for valid value | |
6437 | ||
6438 | if Lcheck = LT or else Ucheck = GT then | |
ed2233dc | 6439 | Error_Msg_N |
685bc70f | 6440 | ("?c?value can only be in range if it is invalid", N); |
c800f862 RD |
6441 | |
6442 | -- Result is in range for valid value | |
6443 | ||
6444 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
ed2233dc | 6445 | Error_Msg_N |
685bc70f | 6446 | ("?c?value can only be out of range if it is invalid", N); |
c800f862 RD |
6447 | |
6448 | -- Lower bound check succeeds if value is valid | |
6449 | ||
6450 | elsif Warn2 and then Lcheck in Compare_GE then | |
ed2233dc | 6451 | Error_Msg_N |
685bc70f | 6452 | ("?c?lower bound check only fails if it is invalid", Lo); |
c800f862 RD |
6453 | |
6454 | -- Upper bound check succeeds if value is valid | |
6455 | ||
6456 | elsif Warn2 and then Ucheck in Compare_LE then | |
ed2233dc | 6457 | Error_Msg_N |
685bc70f | 6458 | ("?c?upper bound check only fails for invalid values", Hi); |
c800f862 RD |
6459 | end if; |
6460 | end if; | |
fbf5a39b AC |
6461 | end; |
6462 | ||
6463 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 6464 | |
4818e7b9 | 6465 | goto Leave; |
70482933 RK |
6466 | |
6467 | -- Here right operand is a subtype mark | |
6468 | ||
6469 | else | |
6470 | declare | |
82878151 AC |
6471 | Typ : Entity_Id := Etype (Rop); |
6472 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
6473 | Cond : Node_Id := Empty; | |
6474 | New_N : Node_Id; | |
6475 | Obj : Node_Id := Lop; | |
6476 | SCIL_Node : Node_Id; | |
70482933 RK |
6477 | |
6478 | begin | |
6479 | Remove_Side_Effects (Obj); | |
6480 | ||
6481 | -- For tagged type, do tagged membership operation | |
6482 | ||
6483 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 6484 | |
535a8637 | 6485 | -- No expansion will be performed for VM targets, as the VM |
c7a494c9 | 6486 | -- back ends will handle the membership tests directly. |
70482933 | 6487 | |
1f110335 | 6488 | if Tagged_Type_Expansion then |
82878151 AC |
6489 | Tagged_Membership (N, SCIL_Node, New_N); |
6490 | Rewrite (N, New_N); | |
cc0b3bac | 6491 | Analyze_And_Resolve (N, Restyp, Suppress => All_Checks); |
82878151 AC |
6492 | |
6493 | -- Update decoration of relocated node referenced by the | |
6494 | -- SCIL node. | |
6495 | ||
9a0ddeee | 6496 | if Generate_SCIL and then Present (SCIL_Node) then |
7665e4bd | 6497 | Set_SCIL_Node (N, SCIL_Node); |
82878151 | 6498 | end if; |
70482933 RK |
6499 | end if; |
6500 | ||
4818e7b9 | 6501 | goto Leave; |
70482933 | 6502 | |
c95e0edc | 6503 | -- If type is scalar type, rewrite as x in t'First .. t'Last. |
70482933 | 6504 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
6505 | -- type if they come from the original type definition. Also this |
6506 | -- way we get all the processing above for an explicit range. | |
70482933 | 6507 | |
f6194278 | 6508 | -- Don't do this for predicated types, since in this case we |
a90bd866 | 6509 | -- want to check the predicate. |
c0f136cd | 6510 | |
c7532b2d AC |
6511 | elsif Is_Scalar_Type (Typ) then |
6512 | if No (Predicate_Function (Typ)) then | |
6513 | Rewrite (Rop, | |
6514 | Make_Range (Loc, | |
6515 | Low_Bound => | |
6516 | Make_Attribute_Reference (Loc, | |
6517 | Attribute_Name => Name_First, | |
e4494292 | 6518 | Prefix => New_Occurrence_Of (Typ, Loc)), |
c7532b2d AC |
6519 | |
6520 | High_Bound => | |
6521 | Make_Attribute_Reference (Loc, | |
6522 | Attribute_Name => Name_Last, | |
e4494292 | 6523 | Prefix => New_Occurrence_Of (Typ, Loc)))); |
c7532b2d AC |
6524 | Analyze_And_Resolve (N, Restyp); |
6525 | end if; | |
70482933 | 6526 | |
4818e7b9 | 6527 | goto Leave; |
5d09245e AC |
6528 | |
6529 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
6530 | -- a membership test if the subtype mark denotes a constrained | |
6531 | -- Unchecked_Union subtype and the expression lacks inferable | |
6532 | -- discriminants. | |
6533 | ||
6534 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
6535 | and then Is_Constrained (Typ) | |
6536 | and then not Has_Inferable_Discriminants (Lop) | |
6537 | then | |
6538 | Insert_Action (N, | |
6539 | Make_Raise_Program_Error (Loc, | |
6540 | Reason => PE_Unchecked_Union_Restriction)); | |
6541 | ||
9a0ddeee | 6542 | -- Prevent Gigi from generating incorrect code by rewriting the |
f6194278 | 6543 | -- test as False. What is this undocumented thing about ??? |
5d09245e | 6544 | |
9a0ddeee | 6545 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 6546 | goto Leave; |
70482933 RK |
6547 | end if; |
6548 | ||
fbf5a39b AC |
6549 | -- Here we have a non-scalar type |
6550 | ||
70482933 RK |
6551 | if Is_Acc then |
6552 | Typ := Designated_Type (Typ); | |
6553 | end if; | |
6554 | ||
6555 | if not Is_Constrained (Typ) then | |
e4494292 | 6556 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 6557 | Analyze_And_Resolve (N, Restyp); |
70482933 | 6558 | |
685094bf RD |
6559 | -- For the constrained array case, we have to check the subscripts |
6560 | -- for an exact match if the lengths are non-zero (the lengths | |
6561 | -- must match in any case). | |
70482933 RK |
6562 | |
6563 | elsif Is_Array_Type (Typ) then | |
fbf5a39b | 6564 | Check_Subscripts : declare |
9a0ddeee | 6565 | function Build_Attribute_Reference |
2e071734 AC |
6566 | (E : Node_Id; |
6567 | Nam : Name_Id; | |
6568 | Dim : Nat) return Node_Id; | |
9a0ddeee | 6569 | -- Build attribute reference E'Nam (Dim) |
70482933 | 6570 | |
9a0ddeee AC |
6571 | ------------------------------- |
6572 | -- Build_Attribute_Reference -- | |
6573 | ------------------------------- | |
fbf5a39b | 6574 | |
9a0ddeee | 6575 | function Build_Attribute_Reference |
2e071734 AC |
6576 | (E : Node_Id; |
6577 | Nam : Name_Id; | |
6578 | Dim : Nat) return Node_Id | |
70482933 RK |
6579 | is |
6580 | begin | |
6581 | return | |
6582 | Make_Attribute_Reference (Loc, | |
9a0ddeee | 6583 | Prefix => E, |
70482933 | 6584 | Attribute_Name => Nam, |
9a0ddeee | 6585 | Expressions => New_List ( |
70482933 | 6586 | Make_Integer_Literal (Loc, Dim))); |
9a0ddeee | 6587 | end Build_Attribute_Reference; |
70482933 | 6588 | |
fad0600d | 6589 | -- Start of processing for Check_Subscripts |
fbf5a39b | 6590 | |
70482933 RK |
6591 | begin |
6592 | for J in 1 .. Number_Dimensions (Typ) loop | |
6593 | Evolve_And_Then (Cond, | |
6594 | Make_Op_Eq (Loc, | |
6595 | Left_Opnd => | |
9a0ddeee | 6596 | Build_Attribute_Reference |
fbf5a39b AC |
6597 | (Duplicate_Subexpr_No_Checks (Obj), |
6598 | Name_First, J), | |
70482933 | 6599 | Right_Opnd => |
9a0ddeee | 6600 | Build_Attribute_Reference |
70482933 RK |
6601 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); |
6602 | ||
6603 | Evolve_And_Then (Cond, | |
6604 | Make_Op_Eq (Loc, | |
6605 | Left_Opnd => | |
9a0ddeee | 6606 | Build_Attribute_Reference |
fbf5a39b AC |
6607 | (Duplicate_Subexpr_No_Checks (Obj), |
6608 | Name_Last, J), | |
70482933 | 6609 | Right_Opnd => |
9a0ddeee | 6610 | Build_Attribute_Reference |
70482933 RK |
6611 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); |
6612 | end loop; | |
6613 | ||
6614 | if Is_Acc then | |
fbf5a39b AC |
6615 | Cond := |
6616 | Make_Or_Else (Loc, | |
cc6f5d75 | 6617 | Left_Opnd => |
fbf5a39b AC |
6618 | Make_Op_Eq (Loc, |
6619 | Left_Opnd => Obj, | |
6620 | Right_Opnd => Make_Null (Loc)), | |
6621 | Right_Opnd => Cond); | |
70482933 RK |
6622 | end if; |
6623 | ||
6624 | Rewrite (N, Cond); | |
4818e7b9 | 6625 | Analyze_And_Resolve (N, Restyp); |
fbf5a39b | 6626 | end Check_Subscripts; |
70482933 | 6627 | |
685094bf RD |
6628 | -- These are the cases where constraint checks may be required, |
6629 | -- e.g. records with possible discriminants | |
70482933 RK |
6630 | |
6631 | else | |
6632 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
6633 | -- The expression that is built is the negation of the one that |
6634 | -- is used for checking discriminant constraints. | |
70482933 RK |
6635 | |
6636 | Obj := Relocate_Node (Left_Opnd (N)); | |
6637 | ||
6638 | if Has_Discriminants (Typ) then | |
6639 | Cond := Make_Op_Not (Loc, | |
6640 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
6641 | ||
6642 | if Is_Acc then | |
6643 | Cond := Make_Or_Else (Loc, | |
cc6f5d75 | 6644 | Left_Opnd => |
70482933 RK |
6645 | Make_Op_Eq (Loc, |
6646 | Left_Opnd => Obj, | |
6647 | Right_Opnd => Make_Null (Loc)), | |
6648 | Right_Opnd => Cond); | |
6649 | end if; | |
6650 | ||
6651 | else | |
6652 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
6653 | end if; | |
6654 | ||
6655 | Rewrite (N, Cond); | |
4818e7b9 | 6656 | Analyze_And_Resolve (N, Restyp); |
70482933 | 6657 | end if; |
6cce2156 GD |
6658 | |
6659 | -- Ada 2012 (AI05-0149): Handle membership tests applied to an | |
6660 | -- expression of an anonymous access type. This can involve an | |
6661 | -- accessibility test and a tagged type membership test in the | |
6662 | -- case of tagged designated types. | |
6663 | ||
6664 | if Ada_Version >= Ada_2012 | |
6665 | and then Is_Acc | |
6666 | and then Ekind (Ltyp) = E_Anonymous_Access_Type | |
6667 | then | |
6668 | declare | |
6669 | Expr_Entity : Entity_Id := Empty; | |
6670 | New_N : Node_Id; | |
6671 | Param_Level : Node_Id; | |
6672 | Type_Level : Node_Id; | |
996c8821 | 6673 | |
6cce2156 GD |
6674 | begin |
6675 | if Is_Entity_Name (Lop) then | |
6676 | Expr_Entity := Param_Entity (Lop); | |
996c8821 | 6677 | |
6cce2156 GD |
6678 | if not Present (Expr_Entity) then |
6679 | Expr_Entity := Entity (Lop); | |
6680 | end if; | |
6681 | end if; | |
6682 | ||
6683 | -- If a conversion of the anonymous access value to the | |
6684 | -- tested type would be illegal, then the result is False. | |
6685 | ||
6686 | if not Valid_Conversion | |
6687 | (Lop, Rtyp, Lop, Report_Errs => False) | |
6688 | then | |
6689 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
6690 | Analyze_And_Resolve (N, Restyp); | |
6691 | ||
6692 | -- Apply an accessibility check if the access object has an | |
6693 | -- associated access level and when the level of the type is | |
6694 | -- less deep than the level of the access parameter. This | |
6695 | -- only occur for access parameters and stand-alone objects | |
6696 | -- of an anonymous access type. | |
6697 | ||
6698 | else | |
6699 | if Present (Expr_Entity) | |
996c8821 RD |
6700 | and then |
6701 | Present | |
6702 | (Effective_Extra_Accessibility (Expr_Entity)) | |
6703 | and then UI_Gt (Object_Access_Level (Lop), | |
6704 | Type_Access_Level (Rtyp)) | |
6cce2156 GD |
6705 | then |
6706 | Param_Level := | |
6707 | New_Occurrence_Of | |
d15f9422 | 6708 | (Effective_Extra_Accessibility (Expr_Entity), Loc); |
6cce2156 GD |
6709 | |
6710 | Type_Level := | |
6711 | Make_Integer_Literal (Loc, Type_Access_Level (Rtyp)); | |
6712 | ||
6713 | -- Return True only if the accessibility level of the | |
6714 | -- expression entity is not deeper than the level of | |
6715 | -- the tested access type. | |
6716 | ||
6717 | Rewrite (N, | |
6718 | Make_And_Then (Loc, | |
6719 | Left_Opnd => Relocate_Node (N), | |
6720 | Right_Opnd => Make_Op_Le (Loc, | |
6721 | Left_Opnd => Param_Level, | |
6722 | Right_Opnd => Type_Level))); | |
6723 | ||
6724 | Analyze_And_Resolve (N); | |
6725 | end if; | |
6726 | ||
6727 | -- If the designated type is tagged, do tagged membership | |
6728 | -- operation. | |
6729 | ||
6730 | -- *** NOTE: we have to check not null before doing the | |
6731 | -- tagged membership test (but maybe that can be done | |
6732 | -- inside Tagged_Membership?). | |
6733 | ||
6734 | if Is_Tagged_Type (Typ) then | |
6735 | Rewrite (N, | |
6736 | Make_And_Then (Loc, | |
6737 | Left_Opnd => Relocate_Node (N), | |
6738 | Right_Opnd => | |
6739 | Make_Op_Ne (Loc, | |
6740 | Left_Opnd => Obj, | |
6741 | Right_Opnd => Make_Null (Loc)))); | |
6742 | ||
535a8637 | 6743 | -- No expansion will be performed for VM targets, as |
c7a494c9 | 6744 | -- the VM back ends will handle the membership tests |
69d8d8b4 | 6745 | -- directly. |
6cce2156 GD |
6746 | |
6747 | if Tagged_Type_Expansion then | |
6748 | ||
6749 | -- Note that we have to pass Original_Node, because | |
6750 | -- the membership test might already have been | |
6751 | -- rewritten by earlier parts of membership test. | |
6752 | ||
6753 | Tagged_Membership | |
6754 | (Original_Node (N), SCIL_Node, New_N); | |
6755 | ||
6756 | -- Update decoration of relocated node referenced | |
6757 | -- by the SCIL node. | |
6758 | ||
6759 | if Generate_SCIL and then Present (SCIL_Node) then | |
6760 | Set_SCIL_Node (New_N, SCIL_Node); | |
6761 | end if; | |
6762 | ||
6763 | Rewrite (N, | |
6764 | Make_And_Then (Loc, | |
6765 | Left_Opnd => Relocate_Node (N), | |
6766 | Right_Opnd => New_N)); | |
6767 | ||
6768 | Analyze_And_Resolve (N, Restyp); | |
6769 | end if; | |
6770 | end if; | |
6771 | end if; | |
6772 | end; | |
6773 | end if; | |
70482933 RK |
6774 | end; |
6775 | end if; | |
4818e7b9 RD |
6776 | |
6777 | -- At this point, we have done the processing required for the basic | |
6778 | -- membership test, but not yet dealt with the predicate. | |
6779 | ||
6780 | <<Leave>> | |
6781 | ||
c7532b2d AC |
6782 | -- If a predicate is present, then we do the predicate test, but we |
6783 | -- most certainly want to omit this if we are within the predicate | |
a90bd866 | 6784 | -- function itself, since otherwise we have an infinite recursion. |
3d6db7f8 GD |
6785 | -- The check should also not be emitted when testing against a range |
6786 | -- (the check is only done when the right operand is a subtype; see | |
6787 | -- RM12-4.5.2 (28.1/3-30/3)). | |
4818e7b9 | 6788 | |
444656ce ES |
6789 | Predicate_Check : declare |
6790 | function In_Range_Check return Boolean; | |
6791 | -- Within an expanded range check that may raise Constraint_Error do | |
6792 | -- not generate a predicate check as well. It is redundant because | |
6793 | -- the context will add an explicit predicate check, and it will | |
6794 | -- raise the wrong exception if it fails. | |
6795 | ||
6796 | -------------------- | |
6797 | -- In_Range_Check -- | |
6798 | -------------------- | |
6799 | ||
6800 | function In_Range_Check return Boolean is | |
6801 | P : Node_Id; | |
6802 | begin | |
6803 | P := Parent (N); | |
6804 | while Present (P) loop | |
6805 | if Nkind (P) = N_Raise_Constraint_Error then | |
6806 | return True; | |
6807 | ||
6808 | elsif Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
6809 | or else Nkind (P) = N_Procedure_Call_Statement | |
6810 | or else Nkind (P) in N_Declaration | |
6811 | then | |
6812 | return False; | |
6813 | end if; | |
6814 | ||
6815 | P := Parent (P); | |
6816 | end loop; | |
6817 | ||
6818 | return False; | |
6819 | end In_Range_Check; | |
6820 | ||
6821 | -- Local variables | |
6822 | ||
c7532b2d | 6823 | PFunc : constant Entity_Id := Predicate_Function (Rtyp); |
444656ce ES |
6824 | R_Op : Node_Id; |
6825 | ||
6826 | -- Start of processing for Predicate_Check | |
4818e7b9 | 6827 | |
c7532b2d AC |
6828 | begin |
6829 | if Present (PFunc) | |
6830 | and then Current_Scope /= PFunc | |
3d6db7f8 | 6831 | and then Nkind (Rop) /= N_Range |
c7532b2d | 6832 | then |
444656ce ES |
6833 | if not In_Range_Check then |
6834 | R_Op := Make_Predicate_Call (Rtyp, Lop, Mem => True); | |
6835 | else | |
6836 | R_Op := New_Occurrence_Of (Standard_True, Loc); | |
6837 | end if; | |
6838 | ||
c7532b2d AC |
6839 | Rewrite (N, |
6840 | Make_And_Then (Loc, | |
6841 | Left_Opnd => Relocate_Node (N), | |
444656ce | 6842 | Right_Opnd => R_Op)); |
4818e7b9 | 6843 | |
c7532b2d | 6844 | -- Analyze new expression, mark left operand as analyzed to |
b2009d46 AC |
6845 | -- avoid infinite recursion adding predicate calls. Similarly, |
6846 | -- suppress further range checks on the call. | |
4818e7b9 | 6847 | |
c7532b2d | 6848 | Set_Analyzed (Left_Opnd (N)); |
b2009d46 | 6849 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); |
4818e7b9 | 6850 | |
c7532b2d AC |
6851 | -- All done, skip attempt at compile time determination of result |
6852 | ||
6853 | return; | |
6854 | end if; | |
444656ce | 6855 | end Predicate_Check; |
70482933 RK |
6856 | end Expand_N_In; |
6857 | ||
6858 | -------------------------------- | |
6859 | -- Expand_N_Indexed_Component -- | |
6860 | -------------------------------- | |
6861 | ||
6862 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
6863 | Loc : constant Source_Ptr := Sloc (N); | |
6864 | Typ : constant Entity_Id := Etype (N); | |
6865 | P : constant Node_Id := Prefix (N); | |
6866 | T : constant Entity_Id := Etype (P); | |
5972791c | 6867 | Atp : Entity_Id; |
70482933 RK |
6868 | |
6869 | begin | |
685094bf RD |
6870 | -- A special optimization, if we have an indexed component that is |
6871 | -- selecting from a slice, then we can eliminate the slice, since, for | |
6872 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
6873 | -- the range check required by the slice. The range check for the slice | |
6874 | -- itself has already been generated. The range check for the | |
6875 | -- subscripting operation is ensured by converting the subject to | |
6876 | -- the subtype of the slice. | |
6877 | ||
6878 | -- This optimization not only generates better code, avoiding slice | |
6879 | -- messing especially in the packed case, but more importantly bypasses | |
6880 | -- some problems in handling this peculiar case, for example, the issue | |
6881 | -- of dealing specially with object renamings. | |
70482933 | 6882 | |
45ec05e1 RD |
6883 | if Nkind (P) = N_Slice |
6884 | ||
6885 | -- This optimization is disabled for CodePeer because it can transform | |
6886 | -- an index-check constraint_error into a range-check constraint_error | |
6887 | -- and CodePeer cares about that distinction. | |
6888 | ||
6889 | and then not CodePeer_Mode | |
6890 | then | |
70482933 RK |
6891 | Rewrite (N, |
6892 | Make_Indexed_Component (Loc, | |
cc6f5d75 | 6893 | Prefix => Prefix (P), |
70482933 RK |
6894 | Expressions => New_List ( |
6895 | Convert_To | |
6896 | (Etype (First_Index (Etype (P))), | |
6897 | First (Expressions (N)))))); | |
6898 | Analyze_And_Resolve (N, Typ); | |
6899 | return; | |
6900 | end if; | |
6901 | ||
b4592168 GD |
6902 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
6903 | -- function, then additional actuals must be passed. | |
6904 | ||
d4dfb005 | 6905 | if Is_Build_In_Place_Function_Call (P) then |
b4592168 | 6906 | Make_Build_In_Place_Call_In_Anonymous_Context (P); |
4ac62786 AC |
6907 | |
6908 | -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix | |
6909 | -- containing build-in-place function calls whose returned object covers | |
6910 | -- interface types. | |
6911 | ||
d4dfb005 | 6912 | elsif Present (Unqual_BIP_Iface_Function_Call (P)) then |
4ac62786 | 6913 | Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P); |
b4592168 GD |
6914 | end if; |
6915 | ||
685094bf | 6916 | -- If the prefix is an access type, then we unconditionally rewrite if |
09494c32 | 6917 | -- as an explicit dereference. This simplifies processing for several |
685094bf RD |
6918 | -- cases, including packed array cases and certain cases in which checks |
6919 | -- must be generated. We used to try to do this only when it was | |
6920 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
6921 | |
6922 | if Is_Access_Type (T) then | |
2717634d | 6923 | Insert_Explicit_Dereference (P); |
70482933 | 6924 | Analyze_And_Resolve (P, Designated_Type (T)); |
5972791c AC |
6925 | Atp := Designated_Type (T); |
6926 | else | |
6927 | Atp := T; | |
70482933 RK |
6928 | end if; |
6929 | ||
fbf5a39b AC |
6930 | -- Generate index and validity checks |
6931 | ||
6932 | Generate_Index_Checks (N); | |
6933 | ||
70482933 RK |
6934 | if Validity_Checks_On and then Validity_Check_Subscripts then |
6935 | Apply_Subscript_Validity_Checks (N); | |
6936 | end if; | |
6937 | ||
5972791c AC |
6938 | -- If selecting from an array with atomic components, and atomic sync |
6939 | -- is not suppressed for this array type, set atomic sync flag. | |
6940 | ||
6941 | if (Has_Atomic_Components (Atp) | |
6942 | and then not Atomic_Synchronization_Disabled (Atp)) | |
6943 | or else (Is_Atomic (Typ) | |
6944 | and then not Atomic_Synchronization_Disabled (Typ)) | |
e2f0522e EB |
6945 | or else (Is_Entity_Name (P) |
6946 | and then Has_Atomic_Components (Entity (P)) | |
6947 | and then not Atomic_Synchronization_Disabled (Entity (P))) | |
5972791c | 6948 | then |
4c318253 | 6949 | Activate_Atomic_Synchronization (N); |
5972791c AC |
6950 | end if; |
6951 | ||
b3f75672 | 6952 | -- All done if the prefix is not a packed array implemented specially |
70482933 | 6953 | |
b3f75672 EB |
6954 | if not (Is_Packed (Etype (Prefix (N))) |
6955 | and then Present (Packed_Array_Impl_Type (Etype (Prefix (N))))) | |
6956 | then | |
70482933 RK |
6957 | return; |
6958 | end if; | |
6959 | ||
6960 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 6961 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
6962 | -- we can always use the normal packed element get circuit. |
6963 | ||
6964 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
6965 | Expand_Packed_Element_Reference (N); | |
6966 | return; | |
6967 | end if; | |
6968 | ||
8ca597af RD |
6969 | -- For a reference to a component of a bit packed array, we convert it |
6970 | -- to a reference to the corresponding Packed_Array_Impl_Type. We only | |
6971 | -- want to do this for simple references, and not for: | |
70482933 | 6972 | |
685094bf RD |
6973 | -- Left side of assignment, or prefix of left side of assignment, or |
6974 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
6975 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
6976 | ||
6977 | -- Renaming objects in renaming associations | |
6978 | -- This case is handled when a use of the renamed variable occurs | |
6979 | ||
d21328a0 | 6980 | -- Actual parameters for a subprogram call |
70482933 RK |
6981 | -- This case is handled in Exp_Ch6.Expand_Actuals |
6982 | ||
6983 | -- The second expression in a 'Read attribute reference | |
6984 | ||
47d3b920 | 6985 | -- The prefix of an address or bit or size attribute reference |
70482933 | 6986 | |
e8c84c8f AC |
6987 | -- The following circuit detects these exceptions. Note that we need to |
6988 | -- deal with implicit dereferences when climbing up the parent chain, | |
6989 | -- with the additional difficulty that the type of parents may have yet | |
6990 | -- to be resolved since prefixes are usually resolved first. | |
70482933 RK |
6991 | |
6992 | declare | |
6993 | Child : Node_Id := N; | |
6994 | Parnt : Node_Id := Parent (N); | |
6995 | ||
6996 | begin | |
6997 | loop | |
6998 | if Nkind (Parnt) = N_Unchecked_Expression then | |
6999 | null; | |
7000 | ||
d21328a0 EB |
7001 | elsif Nkind (Parnt) = N_Object_Renaming_Declaration then |
7002 | return; | |
7003 | ||
7004 | elsif Nkind (Parnt) in N_Subprogram_Call | |
70482933 | 7005 | or else (Nkind (Parnt) = N_Parameter_Association |
d21328a0 | 7006 | and then Nkind (Parent (Parnt)) in N_Subprogram_Call) |
70482933 RK |
7007 | then |
7008 | return; | |
7009 | ||
7010 | elsif Nkind (Parnt) = N_Attribute_Reference | |
b69cd36a AC |
7011 | and then Nam_In (Attribute_Name (Parnt), Name_Address, |
7012 | Name_Bit, | |
7013 | Name_Size) | |
70482933 RK |
7014 | and then Prefix (Parnt) = Child |
7015 | then | |
7016 | return; | |
7017 | ||
7018 | elsif Nkind (Parnt) = N_Assignment_Statement | |
7019 | and then Name (Parnt) = Child | |
7020 | then | |
7021 | return; | |
7022 | ||
685094bf RD |
7023 | -- If the expression is an index of an indexed component, it must |
7024 | -- be expanded regardless of context. | |
fbf5a39b AC |
7025 | |
7026 | elsif Nkind (Parnt) = N_Indexed_Component | |
7027 | and then Child /= Prefix (Parnt) | |
7028 | then | |
7029 | Expand_Packed_Element_Reference (N); | |
7030 | return; | |
7031 | ||
7032 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
7033 | and then Name (Parent (Parnt)) = Parnt | |
7034 | then | |
7035 | return; | |
7036 | ||
70482933 RK |
7037 | elsif Nkind (Parnt) = N_Attribute_Reference |
7038 | and then Attribute_Name (Parnt) = Name_Read | |
7039 | and then Next (First (Expressions (Parnt))) = Child | |
7040 | then | |
7041 | return; | |
7042 | ||
e8c84c8f AC |
7043 | elsif Nkind (Parnt) = N_Indexed_Component |
7044 | and then Prefix (Parnt) = Child | |
7045 | then | |
7046 | null; | |
7047 | ||
7048 | elsif Nkind (Parnt) = N_Selected_Component | |
533369aa | 7049 | and then Prefix (Parnt) = Child |
e8c84c8f AC |
7050 | and then not (Present (Etype (Selector_Name (Parnt))) |
7051 | and then | |
7052 | Is_Access_Type (Etype (Selector_Name (Parnt)))) | |
70482933 RK |
7053 | then |
7054 | null; | |
7055 | ||
e8c84c8f AC |
7056 | -- If the parent is a dereference, either implicit or explicit, |
7057 | -- then the packed reference needs to be expanded. | |
7058 | ||
70482933 RK |
7059 | else |
7060 | Expand_Packed_Element_Reference (N); | |
7061 | return; | |
7062 | end if; | |
7063 | ||
685094bf RD |
7064 | -- Keep looking up tree for unchecked expression, or if we are the |
7065 | -- prefix of a possible assignment left side. | |
70482933 RK |
7066 | |
7067 | Child := Parnt; | |
7068 | Parnt := Parent (Child); | |
7069 | end loop; | |
7070 | end; | |
70482933 RK |
7071 | end Expand_N_Indexed_Component; |
7072 | ||
7073 | --------------------- | |
7074 | -- Expand_N_Not_In -- | |
7075 | --------------------- | |
7076 | ||
7077 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
7078 | -- can be done. This avoids needing to duplicate this expansion code. | |
7079 | ||
7080 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
7081 | Loc : constant Source_Ptr := Sloc (N); |
7082 | Typ : constant Entity_Id := Etype (N); | |
7083 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
7084 | |
7085 | begin | |
7086 | Rewrite (N, | |
7087 | Make_Op_Not (Loc, | |
7088 | Right_Opnd => | |
7089 | Make_In (Loc, | |
7090 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 7091 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 7092 | |
197e4514 AC |
7093 | -- If this is a set membership, preserve list of alternatives |
7094 | ||
7095 | Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N))); | |
7096 | ||
d766cee3 | 7097 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 7098 | -- transformations in Expand_N_In). |
630d30e9 RD |
7099 | |
7100 | Set_Comes_From_Source (N, Cfs); | |
7101 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
7102 | ||
8fc789c8 | 7103 | -- Now analyze transformed node |
630d30e9 | 7104 | |
70482933 RK |
7105 | Analyze_And_Resolve (N, Typ); |
7106 | end Expand_N_Not_In; | |
7107 | ||
7108 | ------------------- | |
7109 | -- Expand_N_Null -- | |
7110 | ------------------- | |
7111 | ||
a3f2babd AC |
7112 | -- The only replacement required is for the case of a null of a type that |
7113 | -- is an access to protected subprogram, or a subtype thereof. We represent | |
7114 | -- such access values as a record, and so we must replace the occurrence of | |
7115 | -- null by the equivalent record (with a null address and a null pointer in | |
c7a494c9 | 7116 | -- it), so that the back end creates the proper value. |
70482933 RK |
7117 | |
7118 | procedure Expand_N_Null (N : Node_Id) is | |
7119 | Loc : constant Source_Ptr := Sloc (N); | |
a3f2babd | 7120 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
70482933 RK |
7121 | Agg : Node_Id; |
7122 | ||
7123 | begin | |
26bff3d9 | 7124 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
7125 | Agg := |
7126 | Make_Aggregate (Loc, | |
7127 | Expressions => New_List ( | |
7128 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
7129 | Make_Null (Loc))); | |
7130 | ||
7131 | Rewrite (N, Agg); | |
7132 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
7133 | ||
685094bf RD |
7134 | -- For subsequent semantic analysis, the node must retain its type. |
7135 | -- Gigi in any case replaces this type by the corresponding record | |
7136 | -- type before processing the node. | |
70482933 RK |
7137 | |
7138 | Set_Etype (N, Typ); | |
7139 | end if; | |
fbf5a39b AC |
7140 | |
7141 | exception | |
7142 | when RE_Not_Available => | |
7143 | return; | |
70482933 RK |
7144 | end Expand_N_Null; |
7145 | ||
7146 | --------------------- | |
7147 | -- Expand_N_Op_Abs -- | |
7148 | --------------------- | |
7149 | ||
7150 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
7151 | Loc : constant Source_Ptr := Sloc (N); | |
cc6f5d75 | 7152 | Expr : constant Node_Id := Right_Opnd (N); |
70482933 RK |
7153 | |
7154 | begin | |
7155 | Unary_Op_Validity_Checks (N); | |
7156 | ||
b6b5cca8 AC |
7157 | -- Check for MINIMIZED/ELIMINATED overflow mode |
7158 | ||
7159 | if Minimized_Eliminated_Overflow_Check (N) then | |
7160 | Apply_Arithmetic_Overflow_Check (N); | |
7161 | return; | |
7162 | end if; | |
7163 | ||
70482933 RK |
7164 | -- Deal with software overflow checking |
7165 | ||
d8eb4ac4 | 7166 | if Is_Signed_Integer_Type (Etype (N)) |
533369aa | 7167 | and then Do_Overflow_Check (N) |
70482933 | 7168 | then |
685094bf RD |
7169 | -- The only case to worry about is when the argument is equal to the |
7170 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 7171 | |
fbf5a39b | 7172 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
7173 | |
7174 | -- with the usual Duplicate_Subexpr use coding for expr | |
7175 | ||
fbf5a39b AC |
7176 | Insert_Action (N, |
7177 | Make_Raise_Constraint_Error (Loc, | |
7178 | Condition => | |
7179 | Make_Op_Eq (Loc, | |
70482933 | 7180 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
7181 | Right_Opnd => |
7182 | Make_Attribute_Reference (Loc, | |
cc6f5d75 | 7183 | Prefix => |
fbf5a39b AC |
7184 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), |
7185 | Attribute_Name => Name_First)), | |
7186 | Reason => CE_Overflow_Check_Failed)); | |
c35c40e7 RK |
7187 | |
7188 | Set_Do_Overflow_Check (N, False); | |
fbf5a39b | 7189 | end if; |
70482933 RK |
7190 | end Expand_N_Op_Abs; |
7191 | ||
7192 | --------------------- | |
7193 | -- Expand_N_Op_Add -- | |
7194 | --------------------- | |
7195 | ||
7196 | procedure Expand_N_Op_Add (N : Node_Id) is | |
7197 | Typ : constant Entity_Id := Etype (N); | |
7198 | ||
7199 | begin | |
7200 | Binary_Op_Validity_Checks (N); | |
7201 | ||
b6b5cca8 AC |
7202 | -- Check for MINIMIZED/ELIMINATED overflow mode |
7203 | ||
7204 | if Minimized_Eliminated_Overflow_Check (N) then | |
7205 | Apply_Arithmetic_Overflow_Check (N); | |
7206 | return; | |
7207 | end if; | |
7208 | ||
70482933 RK |
7209 | -- N + 0 = 0 + N = N for integer types |
7210 | ||
7211 | if Is_Integer_Type (Typ) then | |
7212 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
7213 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
7214 | then | |
7215 | Rewrite (N, Left_Opnd (N)); | |
7216 | return; | |
7217 | ||
7218 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
7219 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
7220 | then | |
7221 | Rewrite (N, Right_Opnd (N)); | |
7222 | return; | |
7223 | end if; | |
7224 | end if; | |
7225 | ||
fbf5a39b | 7226 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 7227 | |
761f7dcb | 7228 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 RK |
7229 | Apply_Arithmetic_Overflow_Check (N); |
7230 | return; | |
70482933 | 7231 | end if; |
dfaff97b RD |
7232 | |
7233 | -- Overflow checks for floating-point if -gnateF mode active | |
7234 | ||
7235 | Check_Float_Op_Overflow (N); | |
05dbb83f | 7236 | |
f4ac86dd | 7237 | Expand_Nonbinary_Modular_Op (N); |
70482933 RK |
7238 | end Expand_N_Op_Add; |
7239 | ||
7240 | --------------------- | |
7241 | -- Expand_N_Op_And -- | |
7242 | --------------------- | |
7243 | ||
7244 | procedure Expand_N_Op_And (N : Node_Id) is | |
7245 | Typ : constant Entity_Id := Etype (N); | |
7246 | ||
7247 | begin | |
7248 | Binary_Op_Validity_Checks (N); | |
7249 | ||
7250 | if Is_Array_Type (Etype (N)) then | |
7251 | Expand_Boolean_Operator (N); | |
7252 | ||
7253 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
7254 | Adjust_Condition (Left_Opnd (N)); |
7255 | Adjust_Condition (Right_Opnd (N)); | |
7256 | Set_Etype (N, Standard_Boolean); | |
7257 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
7258 | |
7259 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
7260 | Expand_Intrinsic_Call (N, Entity (N)); | |
05dbb83f AC |
7261 | end if; |
7262 | ||
f4ac86dd | 7263 | Expand_Nonbinary_Modular_Op (N); |
70482933 RK |
7264 | end Expand_N_Op_And; |
7265 | ||
7266 | ------------------------ | |
7267 | -- Expand_N_Op_Concat -- | |
7268 | ------------------------ | |
7269 | ||
7270 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
7271 | Opnds : List_Id; |
7272 | -- List of operands to be concatenated | |
7273 | ||
70482933 | 7274 | Cnode : Node_Id; |
685094bf RD |
7275 | -- Node which is to be replaced by the result of concatenating the nodes |
7276 | -- in the list Opnds. | |
70482933 | 7277 | |
70482933 | 7278 | begin |
fbf5a39b AC |
7279 | -- Ensure validity of both operands |
7280 | ||
70482933 RK |
7281 | Binary_Op_Validity_Checks (N); |
7282 | ||
685094bf RD |
7283 | -- If we are the left operand of a concatenation higher up the tree, |
7284 | -- then do nothing for now, since we want to deal with a series of | |
7285 | -- concatenations as a unit. | |
70482933 RK |
7286 | |
7287 | if Nkind (Parent (N)) = N_Op_Concat | |
7288 | and then N = Left_Opnd (Parent (N)) | |
7289 | then | |
7290 | return; | |
7291 | end if; | |
7292 | ||
7293 | -- We get here with a concatenation whose left operand may be a | |
7294 | -- concatenation itself with a consistent type. We need to process | |
7295 | -- these concatenation operands from left to right, which means | |
7296 | -- from the deepest node in the tree to the highest node. | |
7297 | ||
7298 | Cnode := N; | |
7299 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
7300 | Cnode := Left_Opnd (Cnode); | |
7301 | end loop; | |
7302 | ||
64425dff BD |
7303 | -- Now Cnode is the deepest concatenation, and its parents are the |
7304 | -- concatenation nodes above, so now we process bottom up, doing the | |
64425dff | 7305 | -- operands. |
70482933 | 7306 | |
df46b832 AC |
7307 | -- The outer loop runs more than once if more than one concatenation |
7308 | -- type is involved. | |
70482933 RK |
7309 | |
7310 | Outer : loop | |
7311 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
7312 | Set_Parent (Opnds, N); | |
7313 | ||
df46b832 | 7314 | -- The inner loop gathers concatenation operands |
70482933 RK |
7315 | |
7316 | Inner : while Cnode /= N | |
70482933 RK |
7317 | and then Base_Type (Etype (Cnode)) = |
7318 | Base_Type (Etype (Parent (Cnode))) | |
7319 | loop | |
7320 | Cnode := Parent (Cnode); | |
7321 | Append (Right_Opnd (Cnode), Opnds); | |
7322 | end loop Inner; | |
7323 | ||
43c58950 AC |
7324 | -- Note: The following code is a temporary workaround for N731-034 |
7325 | -- and N829-028 and will be kept until the general issue of internal | |
7326 | -- symbol serialization is addressed. The workaround is kept under a | |
7327 | -- debug switch to avoid permiating into the general case. | |
7328 | ||
7329 | -- Wrap the node to concatenate into an expression actions node to | |
7330 | -- keep it nicely packaged. This is useful in the case of an assert | |
7331 | -- pragma with a concatenation where we want to be able to delete | |
7332 | -- the concatenation and all its expansion stuff. | |
7333 | ||
7334 | if Debug_Flag_Dot_H then | |
7335 | declare | |
683af98c | 7336 | Cnod : constant Node_Id := New_Copy_Tree (Cnode); |
43c58950 AC |
7337 | Typ : constant Entity_Id := Base_Type (Etype (Cnode)); |
7338 | ||
7339 | begin | |
7340 | -- Note: use Rewrite rather than Replace here, so that for | |
7341 | -- example Why_Not_Static can find the original concatenation | |
7342 | -- node OK! | |
7343 | ||
7344 | Rewrite (Cnode, | |
7345 | Make_Expression_With_Actions (Sloc (Cnode), | |
7346 | Actions => New_List (Make_Null_Statement (Sloc (Cnode))), | |
7347 | Expression => Cnod)); | |
7348 | ||
7349 | Expand_Concatenate (Cnod, Opnds); | |
7350 | Analyze_And_Resolve (Cnode, Typ); | |
7351 | end; | |
7352 | ||
7353 | -- Default case | |
7354 | ||
7355 | else | |
7356 | Expand_Concatenate (Cnode, Opnds); | |
7357 | end if; | |
70482933 RK |
7358 | |
7359 | exit Outer when Cnode = N; | |
7360 | Cnode := Parent (Cnode); | |
7361 | end loop Outer; | |
7362 | end Expand_N_Op_Concat; | |
7363 | ||
7364 | ------------------------ | |
7365 | -- Expand_N_Op_Divide -- | |
7366 | ------------------------ | |
7367 | ||
7368 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
7369 | Loc : constant Source_Ptr := Sloc (N); |
7370 | Lopnd : constant Node_Id := Left_Opnd (N); | |
7371 | Ropnd : constant Node_Id := Right_Opnd (N); | |
7372 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
7373 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
7374 | Typ : Entity_Id := Etype (N); | |
7375 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
7376 | and then | |
7377 | Compile_Time_Known_Value (Ropnd); | |
7378 | Rval : Uint; | |
70482933 RK |
7379 | |
7380 | begin | |
7381 | Binary_Op_Validity_Checks (N); | |
7382 | ||
b6b5cca8 AC |
7383 | -- Check for MINIMIZED/ELIMINATED overflow mode |
7384 | ||
7385 | if Minimized_Eliminated_Overflow_Check (N) then | |
7386 | Apply_Arithmetic_Overflow_Check (N); | |
7387 | return; | |
7388 | end if; | |
7389 | ||
7390 | -- Otherwise proceed with expansion of division | |
7391 | ||
f82944b7 JM |
7392 | if Rknow then |
7393 | Rval := Expr_Value (Ropnd); | |
7394 | end if; | |
7395 | ||
70482933 RK |
7396 | -- N / 1 = N for integer types |
7397 | ||
f82944b7 JM |
7398 | if Rknow and then Rval = Uint_1 then |
7399 | Rewrite (N, Lopnd); | |
70482933 RK |
7400 | return; |
7401 | end if; | |
7402 | ||
7403 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
7404 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
7405 | -- operand is an unsigned integer, as required for this to work. | |
7406 | ||
f82944b7 JM |
7407 | if Nkind (Ropnd) = N_Op_Expon |
7408 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
7409 | |
7410 | -- We cannot do this transformation in configurable run time mode if we | |
51bf9bdf | 7411 | -- have 64-bit integers and long shifts are not available. |
fbf5a39b | 7412 | |
761f7dcb | 7413 | and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target) |
70482933 RK |
7414 | then |
7415 | Rewrite (N, | |
7416 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 7417 | Left_Opnd => Lopnd, |
70482933 | 7418 | Right_Opnd => |
f82944b7 | 7419 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
7420 | Analyze_And_Resolve (N, Typ); |
7421 | return; | |
7422 | end if; | |
7423 | ||
7424 | -- Do required fixup of universal fixed operation | |
7425 | ||
7426 | if Typ = Universal_Fixed then | |
7427 | Fixup_Universal_Fixed_Operation (N); | |
7428 | Typ := Etype (N); | |
7429 | end if; | |
7430 | ||
7431 | -- Divisions with fixed-point results | |
7432 | ||
7433 | if Is_Fixed_Point_Type (Typ) then | |
7434 | ||
8223b654 AC |
7435 | -- No special processing if Treat_Fixed_As_Integer is set, since |
7436 | -- from a semantic point of view such operations are simply integer | |
7437 | -- operations and will be treated that way. | |
7438 | ||
7439 | if not Treat_Fixed_As_Integer (N) then | |
7440 | if Is_Integer_Type (Rtyp) then | |
7441 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
7442 | else | |
7443 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
7444 | end if; | |
7445 | end if; | |
7446 | ||
21f30884 AC |
7447 | -- Deal with divide-by-zero check if back end cannot handle them |
7448 | -- and the flag is set indicating that we need such a check. Note | |
7449 | -- that we don't need to bother here with the case of mixed-mode | |
7450 | -- (Right operand an integer type), since these will be rewritten | |
7451 | -- with conversions to a divide with a fixed-point right operand. | |
7452 | ||
8223b654 AC |
7453 | if Nkind (N) = N_Op_Divide |
7454 | and then Do_Division_Check (N) | |
21f30884 AC |
7455 | and then not Backend_Divide_Checks_On_Target |
7456 | and then not Is_Integer_Type (Rtyp) | |
7457 | then | |
7458 | Set_Do_Division_Check (N, False); | |
7459 | Insert_Action (N, | |
7460 | Make_Raise_Constraint_Error (Loc, | |
7461 | Condition => | |
7462 | Make_Op_Eq (Loc, | |
7463 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Ropnd), | |
7464 | Right_Opnd => Make_Real_Literal (Loc, Ureal_0)), | |
7465 | Reason => CE_Divide_By_Zero)); | |
7466 | end if; | |
7467 | ||
685094bf RD |
7468 | -- Other cases of division of fixed-point operands. Again we exclude the |
7469 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 | 7470 | |
761f7dcb | 7471 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) |
70482933 RK |
7472 | and then not Treat_Fixed_As_Integer (N) |
7473 | then | |
7474 | if Is_Integer_Type (Typ) then | |
7475 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
7476 | else | |
7477 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
7478 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
7479 | end if; | |
7480 | ||
685094bf RD |
7481 | -- Mixed-mode operations can appear in a non-static universal context, |
7482 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 7483 | |
533369aa | 7484 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
f82944b7 JM |
7485 | Rewrite (Ropnd, |
7486 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 7487 | |
f82944b7 | 7488 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 | 7489 | |
533369aa | 7490 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
f82944b7 JM |
7491 | Rewrite (Lopnd, |
7492 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 7493 | |
f82944b7 | 7494 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 7495 | |
f02b8bb8 | 7496 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
7497 | |
7498 | elsif Is_Integer_Type (Typ) then | |
a91e9ac7 | 7499 | Apply_Divide_Checks (N); |
70482933 | 7500 | end if; |
dfaff97b RD |
7501 | |
7502 | -- Overflow checks for floating-point if -gnateF mode active | |
7503 | ||
7504 | Check_Float_Op_Overflow (N); | |
05dbb83f | 7505 | |
f4ac86dd | 7506 | Expand_Nonbinary_Modular_Op (N); |
70482933 RK |
7507 | end Expand_N_Op_Divide; |
7508 | ||
7509 | -------------------- | |
7510 | -- Expand_N_Op_Eq -- | |
7511 | -------------------- | |
7512 | ||
7513 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
7514 | Loc : constant Source_Ptr := Sloc (N); |
7515 | Typ : constant Entity_Id := Etype (N); | |
7516 | Lhs : constant Node_Id := Left_Opnd (N); | |
7517 | Rhs : constant Node_Id := Right_Opnd (N); | |
7518 | Bodies : constant List_Id := New_List; | |
7519 | A_Typ : constant Entity_Id := Etype (Lhs); | |
7520 | ||
70482933 RK |
7521 | procedure Build_Equality_Call (Eq : Entity_Id); |
7522 | -- If a constructed equality exists for the type or for its parent, | |
7523 | -- build and analyze call, adding conversions if the operation is | |
7524 | -- inherited. | |
7525 | ||
d7c37f45 SB |
7526 | function Is_Equality (Subp : Entity_Id; |
7527 | Typ : Entity_Id := Empty) return Boolean; | |
7528 | -- Determine whether arbitrary Entity_Id denotes a function with the | |
7529 | -- right name and profile for an equality op, specifically for the | |
7530 | -- base type Typ if Typ is nonempty. | |
7531 | ||
e1a20c09 HK |
7532 | function Find_Equality (Prims : Elist_Id) return Entity_Id; |
7533 | -- Find a primitive equality function within primitive operation list | |
7534 | -- Prims. | |
7535 | ||
d7c37f45 SB |
7536 | function User_Defined_Primitive_Equality_Op |
7537 | (Typ : Entity_Id) return Entity_Id; | |
7538 | -- Find a user-defined primitive equality function for a given untagged | |
7539 | -- record type, ignoring visibility. Return Empty if no such op found. | |
7540 | ||
e1a20c09 | 7541 | function Has_Unconstrained_UU_Component (Typ : Entity_Id) return Boolean; |
8fc789c8 | 7542 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
7543 | -- Unchecked_Union subtype. Typ is a record type. |
7544 | ||
70482933 RK |
7545 | ------------------------- |
7546 | -- Build_Equality_Call -- | |
7547 | ------------------------- | |
7548 | ||
7549 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
7550 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
cc6f5d75 AC |
7551 | L_Exp : Node_Id := Relocate_Node (Lhs); |
7552 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
70482933 RK |
7553 | |
7554 | begin | |
dda38714 AC |
7555 | -- Adjust operands if necessary to comparison type |
7556 | ||
70482933 RK |
7557 | if Base_Type (Op_Type) /= Base_Type (A_Typ) |
7558 | and then not Is_Class_Wide_Type (A_Typ) | |
7559 | then | |
7560 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
7561 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
7562 | end if; | |
7563 | ||
5d09245e AC |
7564 | -- If we have an Unchecked_Union, we need to add the inferred |
7565 | -- discriminant values as actuals in the function call. At this | |
7566 | -- point, the expansion has determined that both operands have | |
7567 | -- inferable discriminants. | |
7568 | ||
7569 | if Is_Unchecked_Union (Op_Type) then | |
7570 | declare | |
fa1608c2 ES |
7571 | Lhs_Type : constant Node_Id := Etype (L_Exp); |
7572 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
7573 | ||
7574 | Lhs_Discr_Vals : Elist_Id; | |
7575 | -- List of inferred discriminant values for left operand. | |
7576 | ||
7577 | Rhs_Discr_Vals : Elist_Id; | |
7578 | -- List of inferred discriminant values for right operand. | |
7579 | ||
7580 | Discr : Entity_Id; | |
5d09245e AC |
7581 | |
7582 | begin | |
fa1608c2 ES |
7583 | Lhs_Discr_Vals := New_Elmt_List; |
7584 | Rhs_Discr_Vals := New_Elmt_List; | |
7585 | ||
5d09245e AC |
7586 | -- Per-object constrained selected components require special |
7587 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 7588 | -- Unchecked_Union, we cannot reference its discriminants |
fa1608c2 ES |
7589 | -- directly. This is why we use the extra parameters of the |
7590 | -- equality function of the enclosing Unchecked_Union. | |
5d09245e AC |
7591 | |
7592 | -- type UU_Type (Discr : Integer := 0) is | |
7593 | -- . . . | |
7594 | -- end record; | |
7595 | -- pragma Unchecked_Union (UU_Type); | |
7596 | ||
7597 | -- 1. Unchecked_Union enclosing record: | |
7598 | ||
7599 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
7600 | -- . . . | |
7601 | -- Comp : UU_Type (Discr); | |
7602 | -- . . . | |
7603 | -- end Enclosing_UU_Type; | |
7604 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
7605 | ||
7606 | -- Obj1 : Enclosing_UU_Type; | |
7607 | -- Obj2 : Enclosing_UU_Type (1); | |
7608 | ||
2717634d | 7609 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
7610 | |
7611 | -- Generated code: | |
7612 | ||
7613 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
7614 | ||
7615 | -- A and B are the formal parameters of the equality function | |
7616 | -- of Enclosing_UU_Type. The function always has two extra | |
fa1608c2 ES |
7617 | -- formals to capture the inferred discriminant values for |
7618 | -- each discriminant of the type. | |
5d09245e AC |
7619 | |
7620 | -- 2. Non-Unchecked_Union enclosing record: | |
7621 | ||
7622 | -- type | |
7623 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
7624 | -- is record | |
7625 | -- . . . | |
7626 | -- Comp : UU_Type (Discr); | |
7627 | -- . . . | |
7628 | -- end Enclosing_Non_UU_Type; | |
7629 | ||
7630 | -- Obj1 : Enclosing_Non_UU_Type; | |
7631 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
7632 | ||
64ac53f4 | 7633 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
7634 | |
7635 | -- Generated code: | |
7636 | ||
7637 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
7638 | -- obj1.discr, obj2.discr)) then | |
7639 | ||
7640 | -- In this case we can directly reference the discriminants of | |
7641 | -- the enclosing record. | |
7642 | ||
fa1608c2 | 7643 | -- Process left operand of equality |
5d09245e AC |
7644 | |
7645 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
7646 | and then |
7647 | Has_Per_Object_Constraint (Entity (Selector_Name (Lhs))) | |
5d09245e | 7648 | then |
fa1608c2 ES |
7649 | -- If enclosing record is an Unchecked_Union, use formals |
7650 | -- corresponding to each discriminant. The name of the | |
7651 | -- formal is that of the discriminant, with added suffix, | |
7652 | -- see Exp_Ch3.Build_Record_Equality for details. | |
5d09245e | 7653 | |
dda38714 | 7654 | if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs)))) |
5d09245e | 7655 | then |
fa1608c2 ES |
7656 | Discr := |
7657 | First_Discriminant | |
7658 | (Scope (Entity (Selector_Name (Lhs)))); | |
7659 | while Present (Discr) loop | |
cc6f5d75 AC |
7660 | Append_Elmt |
7661 | (Make_Identifier (Loc, | |
7662 | Chars => New_External_Name (Chars (Discr), 'A')), | |
7663 | To => Lhs_Discr_Vals); | |
fa1608c2 ES |
7664 | Next_Discriminant (Discr); |
7665 | end loop; | |
5d09245e | 7666 | |
fa1608c2 ES |
7667 | -- If enclosing record is of a non-Unchecked_Union type, it |
7668 | -- is possible to reference its discriminants directly. | |
5d09245e AC |
7669 | |
7670 | else | |
fa1608c2 ES |
7671 | Discr := First_Discriminant (Lhs_Type); |
7672 | while Present (Discr) loop | |
cc6f5d75 AC |
7673 | Append_Elmt |
7674 | (Make_Selected_Component (Loc, | |
7675 | Prefix => Prefix (Lhs), | |
7676 | Selector_Name => | |
7677 | New_Copy | |
7678 | (Get_Discriminant_Value (Discr, | |
7679 | Lhs_Type, | |
7680 | Stored_Constraint (Lhs_Type)))), | |
7681 | To => Lhs_Discr_Vals); | |
fa1608c2 ES |
7682 | Next_Discriminant (Discr); |
7683 | end loop; | |
5d09245e AC |
7684 | end if; |
7685 | ||
fa1608c2 ES |
7686 | -- Otherwise operand is on object with a constrained type. |
7687 | -- Infer the discriminant values from the constraint. | |
5d09245e AC |
7688 | |
7689 | else | |
fa1608c2 ES |
7690 | Discr := First_Discriminant (Lhs_Type); |
7691 | while Present (Discr) loop | |
cc6f5d75 AC |
7692 | Append_Elmt |
7693 | (New_Copy | |
7694 | (Get_Discriminant_Value (Discr, | |
fa1608c2 ES |
7695 | Lhs_Type, |
7696 | Stored_Constraint (Lhs_Type))), | |
cc6f5d75 | 7697 | To => Lhs_Discr_Vals); |
fa1608c2 ES |
7698 | Next_Discriminant (Discr); |
7699 | end loop; | |
5d09245e AC |
7700 | end if; |
7701 | ||
fa1608c2 | 7702 | -- Similar processing for right operand of equality |
5d09245e AC |
7703 | |
7704 | if Nkind (Rhs) = N_Selected_Component | |
533369aa AC |
7705 | and then |
7706 | Has_Per_Object_Constraint (Entity (Selector_Name (Rhs))) | |
5d09245e | 7707 | then |
5e1c00fa | 7708 | if Is_Unchecked_Union |
cc6f5d75 | 7709 | (Scope (Entity (Selector_Name (Rhs)))) |
5d09245e | 7710 | then |
fa1608c2 ES |
7711 | Discr := |
7712 | First_Discriminant | |
7713 | (Scope (Entity (Selector_Name (Rhs)))); | |
7714 | while Present (Discr) loop | |
cc6f5d75 AC |
7715 | Append_Elmt |
7716 | (Make_Identifier (Loc, | |
7717 | Chars => New_External_Name (Chars (Discr), 'B')), | |
7718 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
7719 | Next_Discriminant (Discr); |
7720 | end loop; | |
5d09245e AC |
7721 | |
7722 | else | |
fa1608c2 ES |
7723 | Discr := First_Discriminant (Rhs_Type); |
7724 | while Present (Discr) loop | |
cc6f5d75 AC |
7725 | Append_Elmt |
7726 | (Make_Selected_Component (Loc, | |
7727 | Prefix => Prefix (Rhs), | |
7728 | Selector_Name => | |
7729 | New_Copy (Get_Discriminant_Value | |
7730 | (Discr, | |
7731 | Rhs_Type, | |
7732 | Stored_Constraint (Rhs_Type)))), | |
7733 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
7734 | Next_Discriminant (Discr); |
7735 | end loop; | |
5d09245e | 7736 | end if; |
5d09245e | 7737 | |
fa1608c2 ES |
7738 | else |
7739 | Discr := First_Discriminant (Rhs_Type); | |
7740 | while Present (Discr) loop | |
cc6f5d75 AC |
7741 | Append_Elmt |
7742 | (New_Copy (Get_Discriminant_Value | |
7743 | (Discr, | |
7744 | Rhs_Type, | |
7745 | Stored_Constraint (Rhs_Type))), | |
7746 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
7747 | Next_Discriminant (Discr); |
7748 | end loop; | |
5d09245e AC |
7749 | end if; |
7750 | ||
fa1608c2 ES |
7751 | -- Now merge the list of discriminant values so that values |
7752 | -- of corresponding discriminants are adjacent. | |
7753 | ||
7754 | declare | |
7755 | Params : List_Id; | |
7756 | L_Elmt : Elmt_Id; | |
7757 | R_Elmt : Elmt_Id; | |
7758 | ||
7759 | begin | |
7760 | Params := New_List (L_Exp, R_Exp); | |
7761 | L_Elmt := First_Elmt (Lhs_Discr_Vals); | |
7762 | R_Elmt := First_Elmt (Rhs_Discr_Vals); | |
7763 | while Present (L_Elmt) loop | |
7764 | Append_To (Params, Node (L_Elmt)); | |
7765 | Append_To (Params, Node (R_Elmt)); | |
7766 | Next_Elmt (L_Elmt); | |
7767 | Next_Elmt (R_Elmt); | |
7768 | end loop; | |
7769 | ||
7770 | Rewrite (N, | |
7771 | Make_Function_Call (Loc, | |
e4494292 | 7772 | Name => New_Occurrence_Of (Eq, Loc), |
fa1608c2 ES |
7773 | Parameter_Associations => Params)); |
7774 | end; | |
5d09245e AC |
7775 | end; |
7776 | ||
7777 | -- Normal case, not an unchecked union | |
7778 | ||
7779 | else | |
7780 | Rewrite (N, | |
7781 | Make_Function_Call (Loc, | |
e4494292 | 7782 | Name => New_Occurrence_Of (Eq, Loc), |
5d09245e AC |
7783 | Parameter_Associations => New_List (L_Exp, R_Exp))); |
7784 | end if; | |
70482933 RK |
7785 | |
7786 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7787 | end Build_Equality_Call; | |
7788 | ||
d7c37f45 SB |
7789 | ----------------- |
7790 | -- Is_Equality -- | |
7791 | ----------------- | |
7792 | ||
7793 | function Is_Equality (Subp : Entity_Id; | |
7794 | Typ : Entity_Id := Empty) return Boolean is | |
7795 | Formal_1 : Entity_Id; | |
7796 | Formal_2 : Entity_Id; | |
7797 | begin | |
7798 | -- The equality function carries name "=", returns Boolean, and has | |
7799 | -- exactly two formal parameters of an identical type. | |
7800 | ||
7801 | if Ekind (Subp) = E_Function | |
7802 | and then Chars (Subp) = Name_Op_Eq | |
7803 | and then Base_Type (Etype (Subp)) = Standard_Boolean | |
7804 | then | |
7805 | Formal_1 := First_Formal (Subp); | |
7806 | Formal_2 := Empty; | |
7807 | ||
7808 | if Present (Formal_1) then | |
7809 | Formal_2 := Next_Formal (Formal_1); | |
7810 | end if; | |
7811 | ||
7812 | return | |
7813 | Present (Formal_1) | |
7814 | and then Present (Formal_2) | |
7815 | and then No (Next_Formal (Formal_2)) | |
7816 | and then Base_Type (Etype (Formal_1)) = | |
7817 | Base_Type (Etype (Formal_2)) | |
7818 | and then | |
7819 | (not Present (Typ) | |
7820 | or else Implementation_Base_Type (Etype (Formal_1)) = Typ); | |
7821 | end if; | |
7822 | ||
7823 | return False; | |
7824 | end Is_Equality; | |
7825 | ||
e1a20c09 HK |
7826 | ------------------- |
7827 | -- Find_Equality -- | |
7828 | ------------------- | |
7829 | ||
7830 | function Find_Equality (Prims : Elist_Id) return Entity_Id is | |
0715a2a8 HK |
7831 | function Find_Aliased_Equality (Prim : Entity_Id) return Entity_Id; |
7832 | -- Find an equality in a possible alias chain starting from primitive | |
7833 | -- operation Prim. | |
e1a20c09 | 7834 | |
0715a2a8 HK |
7835 | --------------------------- |
7836 | -- Find_Aliased_Equality -- | |
7837 | --------------------------- | |
e1a20c09 | 7838 | |
0715a2a8 HK |
7839 | function Find_Aliased_Equality (Prim : Entity_Id) return Entity_Id is |
7840 | Candid : Entity_Id; | |
e1a20c09 | 7841 | |
0715a2a8 HK |
7842 | begin |
7843 | -- Inspect each candidate in the alias chain, checking whether it | |
7844 | -- denotes an equality. | |
e1a20c09 | 7845 | |
0715a2a8 HK |
7846 | Candid := Prim; |
7847 | while Present (Candid) loop | |
7848 | if Is_Equality (Candid) then | |
7849 | return Candid; | |
7850 | end if; | |
e1a20c09 | 7851 | |
0715a2a8 HK |
7852 | Candid := Alias (Candid); |
7853 | end loop; | |
e1a20c09 | 7854 | |
0715a2a8 HK |
7855 | return Empty; |
7856 | end Find_Aliased_Equality; | |
e1a20c09 | 7857 | |
0715a2a8 HK |
7858 | -- Local variables |
7859 | ||
7860 | Eq_Prim : Entity_Id; | |
7861 | Prim_Elmt : Elmt_Id; | |
7862 | ||
7863 | -- Start of processing for Find_Equality | |
7864 | ||
7865 | begin | |
7866 | -- Assume that the tagged type lacks an equality | |
7867 | ||
7868 | Eq_Prim := Empty; | |
7869 | ||
7870 | -- Inspect the list of primitives looking for a suitable equality | |
7871 | -- within a possible chain of aliases. | |
7872 | ||
7873 | Prim_Elmt := First_Elmt (Prims); | |
7874 | while Present (Prim_Elmt) and then No (Eq_Prim) loop | |
7875 | Eq_Prim := Find_Aliased_Equality (Node (Prim_Elmt)); | |
7876 | ||
e1a20c09 HK |
7877 | Next_Elmt (Prim_Elmt); |
7878 | end loop; | |
7879 | ||
0715a2a8 | 7880 | -- A tagged type should always have an equality |
e1a20c09 | 7881 | |
0715a2a8 | 7882 | pragma Assert (Present (Eq_Prim)); |
e1a20c09 | 7883 | |
0715a2a8 | 7884 | return Eq_Prim; |
e1a20c09 HK |
7885 | end Find_Equality; |
7886 | ||
d7c37f45 SB |
7887 | ---------------------------------------- |
7888 | -- User_Defined_Primitive_Equality_Op -- | |
7889 | ---------------------------------------- | |
7890 | ||
7891 | function User_Defined_Primitive_Equality_Op | |
7892 | (Typ : Entity_Id) return Entity_Id | |
7893 | is | |
7894 | Enclosing_Scope : constant Node_Id := Scope (Typ); | |
7895 | E : Entity_Id; | |
7896 | begin | |
7897 | -- Prune this search by somehow not looking at decls that precede | |
7898 | -- the declaration of the first view of Typ (which might be a partial | |
7899 | -- view)??? | |
7900 | ||
7901 | for Private_Entities in Boolean loop | |
7902 | if Private_Entities then | |
7903 | if Ekind (Enclosing_Scope) /= E_Package then | |
7904 | exit; | |
7905 | end if; | |
7906 | E := First_Private_Entity (Enclosing_Scope); | |
7907 | ||
7908 | else | |
7909 | E := First_Entity (Enclosing_Scope); | |
7910 | end if; | |
7911 | ||
7912 | while Present (E) loop | |
7913 | if Is_Equality (E, Typ) then | |
7914 | return E; | |
7915 | end if; | |
7916 | E := Next_Entity (E); | |
7917 | end loop; | |
7918 | end loop; | |
7919 | ||
7920 | if Is_Derived_Type (Typ) then | |
7921 | return User_Defined_Primitive_Equality_Op | |
7922 | (Implementation_Base_Type (Etype (Typ))); | |
7923 | end if; | |
7924 | ||
7925 | return Empty; | |
7926 | end User_Defined_Primitive_Equality_Op; | |
7927 | ||
5d09245e AC |
7928 | ------------------------------------ |
7929 | -- Has_Unconstrained_UU_Component -- | |
7930 | ------------------------------------ | |
7931 | ||
7932 | function Has_Unconstrained_UU_Component | |
e1a20c09 | 7933 | (Typ : Entity_Id) return Boolean |
5d09245e AC |
7934 | is |
7935 | Tdef : constant Node_Id := | |
57848bf7 | 7936 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
7937 | Clist : Node_Id; |
7938 | Vpart : Node_Id; | |
7939 | ||
7940 | function Component_Is_Unconstrained_UU | |
7941 | (Comp : Node_Id) return Boolean; | |
7942 | -- Determines whether the subtype of the component is an | |
7943 | -- unconstrained Unchecked_Union. | |
7944 | ||
7945 | function Variant_Is_Unconstrained_UU | |
7946 | (Variant : Node_Id) return Boolean; | |
7947 | -- Determines whether a component of the variant has an unconstrained | |
7948 | -- Unchecked_Union subtype. | |
7949 | ||
7950 | ----------------------------------- | |
7951 | -- Component_Is_Unconstrained_UU -- | |
7952 | ----------------------------------- | |
7953 | ||
7954 | function Component_Is_Unconstrained_UU | |
7955 | (Comp : Node_Id) return Boolean | |
7956 | is | |
7957 | begin | |
7958 | if Nkind (Comp) /= N_Component_Declaration then | |
7959 | return False; | |
7960 | end if; | |
7961 | ||
7962 | declare | |
7963 | Sindic : constant Node_Id := | |
7964 | Subtype_Indication (Component_Definition (Comp)); | |
7965 | ||
7966 | begin | |
7967 | -- Unconstrained nominal type. In the case of a constraint | |
7968 | -- present, the node kind would have been N_Subtype_Indication. | |
7969 | ||
7970 | if Nkind (Sindic) = N_Identifier then | |
7971 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
7972 | end if; | |
7973 | ||
7974 | return False; | |
7975 | end; | |
7976 | end Component_Is_Unconstrained_UU; | |
7977 | ||
7978 | --------------------------------- | |
7979 | -- Variant_Is_Unconstrained_UU -- | |
7980 | --------------------------------- | |
7981 | ||
7982 | function Variant_Is_Unconstrained_UU | |
7983 | (Variant : Node_Id) return Boolean | |
7984 | is | |
7985 | Clist : constant Node_Id := Component_List (Variant); | |
7986 | ||
7987 | begin | |
7988 | if Is_Empty_List (Component_Items (Clist)) then | |
7989 | return False; | |
7990 | end if; | |
7991 | ||
f02b8bb8 RD |
7992 | -- We only need to test one component |
7993 | ||
5d09245e AC |
7994 | declare |
7995 | Comp : Node_Id := First (Component_Items (Clist)); | |
7996 | ||
7997 | begin | |
7998 | while Present (Comp) loop | |
5d09245e AC |
7999 | if Component_Is_Unconstrained_UU (Comp) then |
8000 | return True; | |
8001 | end if; | |
8002 | ||
8003 | Next (Comp); | |
8004 | end loop; | |
8005 | end; | |
8006 | ||
8007 | -- None of the components withing the variant were of | |
8008 | -- unconstrained Unchecked_Union type. | |
8009 | ||
8010 | return False; | |
8011 | end Variant_Is_Unconstrained_UU; | |
8012 | ||
8013 | -- Start of processing for Has_Unconstrained_UU_Component | |
8014 | ||
8015 | begin | |
8016 | if Null_Present (Tdef) then | |
8017 | return False; | |
8018 | end if; | |
8019 | ||
8020 | Clist := Component_List (Tdef); | |
8021 | Vpart := Variant_Part (Clist); | |
8022 | ||
8023 | -- Inspect available components | |
8024 | ||
8025 | if Present (Component_Items (Clist)) then | |
8026 | declare | |
8027 | Comp : Node_Id := First (Component_Items (Clist)); | |
8028 | ||
8029 | begin | |
8030 | while Present (Comp) loop | |
8031 | ||
8fc789c8 | 8032 | -- One component is sufficient |
5d09245e AC |
8033 | |
8034 | if Component_Is_Unconstrained_UU (Comp) then | |
8035 | return True; | |
8036 | end if; | |
8037 | ||
8038 | Next (Comp); | |
8039 | end loop; | |
8040 | end; | |
8041 | end if; | |
8042 | ||
8043 | -- Inspect available components withing variants | |
8044 | ||
8045 | if Present (Vpart) then | |
8046 | declare | |
8047 | Variant : Node_Id := First (Variants (Vpart)); | |
8048 | ||
8049 | begin | |
8050 | while Present (Variant) loop | |
8051 | ||
8fc789c8 | 8052 | -- One component within a variant is sufficient |
5d09245e AC |
8053 | |
8054 | if Variant_Is_Unconstrained_UU (Variant) then | |
8055 | return True; | |
8056 | end if; | |
8057 | ||
8058 | Next (Variant); | |
8059 | end loop; | |
8060 | end; | |
8061 | end if; | |
8062 | ||
8063 | -- Neither the available components, nor the components inside the | |
8064 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
8065 | ||
8066 | return False; | |
8067 | end Has_Unconstrained_UU_Component; | |
8068 | ||
e1a20c09 HK |
8069 | -- Local variables |
8070 | ||
8071 | Typl : Entity_Id; | |
8072 | ||
70482933 RK |
8073 | -- Start of processing for Expand_N_Op_Eq |
8074 | ||
8075 | begin | |
8076 | Binary_Op_Validity_Checks (N); | |
8077 | ||
456cbfa5 AC |
8078 | -- Deal with private types |
8079 | ||
e1a20c09 HK |
8080 | Typl := A_Typ; |
8081 | ||
70482933 RK |
8082 | if Ekind (Typl) = E_Private_Type then |
8083 | Typl := Underlying_Type (Typl); | |
e1a20c09 | 8084 | |
70482933 RK |
8085 | elsif Ekind (Typl) = E_Private_Subtype then |
8086 | Typl := Underlying_Type (Base_Type (Typl)); | |
8087 | end if; | |
8088 | ||
8089 | -- It may happen in error situations that the underlying type is not | |
8090 | -- set. The error will be detected later, here we just defend the | |
8091 | -- expander code. | |
8092 | ||
8093 | if No (Typl) then | |
8094 | return; | |
8095 | end if; | |
8096 | ||
a92230c5 AC |
8097 | -- Now get the implementation base type (note that plain Base_Type here |
8098 | -- might lead us back to the private type, which is not what we want!) | |
8099 | ||
8100 | Typl := Implementation_Base_Type (Typl); | |
70482933 | 8101 | |
dda38714 AC |
8102 | -- Equality between variant records results in a call to a routine |
8103 | -- that has conditional tests of the discriminant value(s), and hence | |
8104 | -- violates the No_Implicit_Conditionals restriction. | |
8105 | ||
8106 | if Has_Variant_Part (Typl) then | |
8107 | declare | |
8108 | Msg : Boolean; | |
8109 | ||
8110 | begin | |
8111 | Check_Restriction (Msg, No_Implicit_Conditionals, N); | |
8112 | ||
8113 | if Msg then | |
8114 | Error_Msg_N | |
8115 | ("\comparison of variant records tests discriminants", N); | |
8116 | return; | |
8117 | end if; | |
8118 | end; | |
8119 | end if; | |
8120 | ||
456cbfa5 | 8121 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 8122 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
8123 | |
8124 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
8125 | ||
8126 | if Nkind (N) /= N_Op_Eq then | |
8127 | return; | |
8128 | end if; | |
8129 | ||
70482933 RK |
8130 | -- Boolean types (requiring handling of non-standard case) |
8131 | ||
f02b8bb8 | 8132 | if Is_Boolean_Type (Typl) then |
70482933 RK |
8133 | Adjust_Condition (Left_Opnd (N)); |
8134 | Adjust_Condition (Right_Opnd (N)); | |
8135 | Set_Etype (N, Standard_Boolean); | |
8136 | Adjust_Result_Type (N, Typ); | |
8137 | ||
8138 | -- Array types | |
8139 | ||
8140 | elsif Is_Array_Type (Typl) then | |
8141 | ||
1033834f RD |
8142 | -- If we are doing full validity checking, and it is possible for the |
8143 | -- array elements to be invalid then expand out array comparisons to | |
8144 | -- make sure that we check the array elements. | |
fbf5a39b | 8145 | |
1033834f RD |
8146 | if Validity_Check_Operands |
8147 | and then not Is_Known_Valid (Component_Type (Typl)) | |
8148 | then | |
fbf5a39b AC |
8149 | declare |
8150 | Save_Force_Validity_Checks : constant Boolean := | |
8151 | Force_Validity_Checks; | |
8152 | begin | |
8153 | Force_Validity_Checks := True; | |
8154 | Rewrite (N, | |
0da2c8ac AC |
8155 | Expand_Array_Equality |
8156 | (N, | |
8157 | Relocate_Node (Lhs), | |
8158 | Relocate_Node (Rhs), | |
8159 | Bodies, | |
8160 | Typl)); | |
8161 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
8162 | Analyze_And_Resolve (N, Standard_Boolean); |
8163 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
8164 | end; | |
8165 | ||
a9d8907c | 8166 | -- Packed case where both operands are known aligned |
70482933 | 8167 | |
a9d8907c JM |
8168 | elsif Is_Bit_Packed_Array (Typl) |
8169 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
8170 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
8171 | then | |
70482933 RK |
8172 | Expand_Packed_Eq (N); |
8173 | ||
5e1c00fa RD |
8174 | -- Where the component type is elementary we can use a block bit |
8175 | -- comparison (if supported on the target) exception in the case | |
8176 | -- of floating-point (negative zero issues require element by | |
f280dd8f | 8177 | -- element comparison), and atomic/VFA types (where we must be sure |
a9d8907c | 8178 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 8179 | |
70482933 RK |
8180 | elsif Is_Elementary_Type (Component_Type (Typl)) |
8181 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
f280dd8f | 8182 | and then not Is_Atomic_Or_VFA (Component_Type (Typl)) |
a9d8907c | 8183 | and then not Is_Possibly_Unaligned_Object (Lhs) |
00907026 | 8184 | and then not Is_Possibly_Unaligned_Slice (Lhs) |
a9d8907c | 8185 | and then not Is_Possibly_Unaligned_Object (Rhs) |
00907026 | 8186 | and then not Is_Possibly_Unaligned_Slice (Rhs) |
fbf5a39b | 8187 | and then Support_Composite_Compare_On_Target |
70482933 RK |
8188 | then |
8189 | null; | |
8190 | ||
685094bf RD |
8191 | -- For composite and floating-point cases, expand equality loop to |
8192 | -- make sure of using proper comparisons for tagged types, and | |
8193 | -- correctly handling the floating-point case. | |
70482933 RK |
8194 | |
8195 | else | |
8196 | Rewrite (N, | |
0da2c8ac AC |
8197 | Expand_Array_Equality |
8198 | (N, | |
8199 | Relocate_Node (Lhs), | |
8200 | Relocate_Node (Rhs), | |
8201 | Bodies, | |
8202 | Typl)); | |
70482933 RK |
8203 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
8204 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
8205 | end if; | |
8206 | ||
8207 | -- Record Types | |
8208 | ||
8209 | elsif Is_Record_Type (Typl) then | |
8210 | ||
8211 | -- For tagged types, use the primitive "=" | |
8212 | ||
8213 | if Is_Tagged_Type (Typl) then | |
8214 | ||
0669bebe GB |
8215 | -- No need to do anything else compiling under restriction |
8216 | -- No_Dispatching_Calls. During the semantic analysis we | |
8217 | -- already notified such violation. | |
8218 | ||
8219 | if Restriction_Active (No_Dispatching_Calls) then | |
8220 | return; | |
8221 | end if; | |
8222 | ||
65641255 JM |
8223 | -- If this is an untagged private type completed with a derivation |
8224 | -- of an untagged private type whose full view is a tagged type, | |
8225 | -- we use the primitive operations of the private type (since it | |
8226 | -- does not have a full view, and also because its equality | |
8227 | -- primitive may have been overridden in its untagged full view). | |
8228 | ||
8229 | if Inherits_From_Tagged_Full_View (A_Typ) then | |
e1a20c09 HK |
8230 | Build_Equality_Call |
8231 | (Find_Equality (Collect_Primitive_Operations (A_Typ))); | |
fbf5a39b AC |
8232 | |
8233 | -- Find the type's predefined equality or an overriding | |
3dddb11e | 8234 | -- user-defined equality. The reason for not simply calling |
fbf5a39b | 8235 | -- Find_Prim_Op here is that there may be a user-defined |
3dddb11e ES |
8236 | -- overloaded equality op that precedes the equality that we |
8237 | -- want, so we have to explicitly search (e.g., there could be | |
8238 | -- an equality with two different parameter types). | |
fbf5a39b | 8239 | |
70482933 | 8240 | else |
fbf5a39b | 8241 | if Is_Class_Wide_Type (Typl) then |
3dddb11e | 8242 | Typl := Find_Specific_Type (Typl); |
fbf5a39b AC |
8243 | end if; |
8244 | ||
e1a20c09 HK |
8245 | Build_Equality_Call |
8246 | (Find_Equality (Primitive_Operations (Typl))); | |
70482933 RK |
8247 | end if; |
8248 | ||
d7c37f45 SB |
8249 | -- See AI12-0101 (which only removes a legality rule) and then |
8250 | -- AI05-0123 (which then applies in the previously illegal case). | |
8251 | -- AI12-0101 is a binding interpretation. | |
8252 | ||
8253 | elsif Ada_Version >= Ada_2012 | |
8254 | and then Present (User_Defined_Primitive_Equality_Op (Typl)) | |
8255 | then | |
8256 | Build_Equality_Call (User_Defined_Primitive_Equality_Op (Typl)); | |
8257 | ||
5d09245e AC |
8258 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
8259 | -- predefined equality operator for a type which has a subcomponent | |
8260 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
8261 | ||
8262 | elsif Has_Unconstrained_UU_Component (Typl) then | |
8263 | Insert_Action (N, | |
8264 | Make_Raise_Program_Error (Loc, | |
8265 | Reason => PE_Unchecked_Union_Restriction)); | |
8266 | ||
8267 | -- Prevent Gigi from generating incorrect code by rewriting the | |
6cb3037c | 8268 | -- equality as a standard False. (is this documented somewhere???) |
5d09245e AC |
8269 | |
8270 | Rewrite (N, | |
8271 | New_Occurrence_Of (Standard_False, Loc)); | |
8272 | ||
8273 | elsif Is_Unchecked_Union (Typl) then | |
8274 | ||
8275 | -- If we can infer the discriminants of the operands, we make a | |
8276 | -- call to the TSS equality function. | |
8277 | ||
8278 | if Has_Inferable_Discriminants (Lhs) | |
8279 | and then | |
8280 | Has_Inferable_Discriminants (Rhs) | |
8281 | then | |
8282 | Build_Equality_Call | |
8283 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
8284 | ||
8285 | else | |
8286 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
8287 | -- the predefined equality operator for an Unchecked_Union type | |
8288 | -- if either of the operands lack inferable discriminants. | |
8289 | ||
8290 | Insert_Action (N, | |
8291 | Make_Raise_Program_Error (Loc, | |
8292 | Reason => PE_Unchecked_Union_Restriction)); | |
8293 | ||
29ad9ea5 AC |
8294 | -- Emit a warning on source equalities only, otherwise the |
8295 | -- message may appear out of place due to internal use. The | |
8296 | -- warning is unconditional because it is required by the | |
8297 | -- language. | |
8298 | ||
8299 | if Comes_From_Source (N) then | |
8300 | Error_Msg_N | |
facfa165 | 8301 | ("Unchecked_Union discriminants cannot be determined??", |
29ad9ea5 AC |
8302 | N); |
8303 | Error_Msg_N | |
facfa165 | 8304 | ("\Program_Error will be raised for equality operation??", |
29ad9ea5 AC |
8305 | N); |
8306 | end if; | |
8307 | ||
5d09245e | 8308 | -- Prevent Gigi from generating incorrect code by rewriting |
6cb3037c | 8309 | -- the equality as a standard False (documented where???). |
5d09245e AC |
8310 | |
8311 | Rewrite (N, | |
8312 | New_Occurrence_Of (Standard_False, Loc)); | |
5d09245e AC |
8313 | end if; |
8314 | ||
70482933 RK |
8315 | -- If a type support function is present (for complex cases), use it |
8316 | ||
fbf5a39b AC |
8317 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
8318 | Build_Equality_Call | |
8319 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 | 8320 | |
8d80ff64 AC |
8321 | -- When comparing two Bounded_Strings, use the primitive equality of |
8322 | -- the root Super_String type. | |
8323 | ||
8324 | elsif Is_Bounded_String (Typl) then | |
e1a20c09 HK |
8325 | Build_Equality_Call |
8326 | (Find_Equality | |
8327 | (Collect_Primitive_Operations (Root_Type (Typl)))); | |
8d80ff64 | 8328 | |
70482933 | 8329 | -- Otherwise expand the component by component equality. Note that |
8fc789c8 | 8330 | -- we never use block-bit comparisons for records, because of the |
c7a494c9 | 8331 | -- problems with gaps. The back end will often be able to recombine |
70482933 RK |
8332 | -- the separate comparisons that we generate here. |
8333 | ||
8334 | else | |
8335 | Remove_Side_Effects (Lhs); | |
8336 | Remove_Side_Effects (Rhs); | |
8337 | Rewrite (N, | |
8338 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
8339 | ||
8340 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
8341 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
8342 | end if; | |
6bc08721 JM |
8343 | |
8344 | -- If unnesting, handle elementary types whose Equivalent_Types are | |
8345 | -- records because there may be padding or undefined fields. | |
8346 | ||
8347 | elsif Unnest_Subprogram_Mode | |
8348 | and then Ekind_In (Typl, E_Class_Wide_Type, | |
8349 | E_Class_Wide_Subtype, | |
8350 | E_Access_Subprogram_Type, | |
8351 | E_Access_Protected_Subprogram_Type, | |
8352 | E_Anonymous_Access_Protected_Subprogram_Type, | |
8353 | E_Access_Subprogram_Type, | |
8354 | E_Exception_Type) | |
8355 | and then Present (Equivalent_Type (Typl)) | |
8356 | and then Is_Record_Type (Equivalent_Type (Typl)) | |
8357 | then | |
8358 | Typl := Equivalent_Type (Typl); | |
8359 | Remove_Side_Effects (Lhs); | |
8360 | Remove_Side_Effects (Rhs); | |
8361 | Rewrite (N, | |
8362 | Expand_Record_Equality (N, Typl, | |
8363 | Unchecked_Convert_To (Typl, Lhs), | |
8364 | Unchecked_Convert_To (Typl, Rhs), | |
8365 | Bodies)); | |
8366 | ||
8367 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
8368 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
70482933 RK |
8369 | end if; |
8370 | ||
d26dc4b5 | 8371 | -- Test if result is known at compile time |
70482933 | 8372 | |
d26dc4b5 | 8373 | Rewrite_Comparison (N); |
f02b8bb8 | 8374 | |
878e58c8 RD |
8375 | -- Special optimization of length comparison |
8376 | ||
0580d807 | 8377 | Optimize_Length_Comparison (N); |
878e58c8 | 8378 | |
088c7e1b | 8379 | -- One more special case: if we have a comparison of X'Result = expr |
878e58c8 | 8380 | -- in floating-point, then if not already there, change expr to be |
088c7e1b | 8381 | -- f'Machine (expr) to eliminate surprise from extra precision. |
878e58c8 RD |
8382 | |
8383 | if Is_Floating_Point_Type (Typl) | |
8384 | and then Nkind (Original_Node (Lhs)) = N_Attribute_Reference | |
8385 | and then Attribute_Name (Original_Node (Lhs)) = Name_Result | |
8386 | then | |
8387 | -- Stick in the Typ'Machine call if not already there | |
8388 | ||
8389 | if Nkind (Rhs) /= N_Attribute_Reference | |
8390 | or else Attribute_Name (Rhs) /= Name_Machine | |
8391 | then | |
8392 | Rewrite (Rhs, | |
8393 | Make_Attribute_Reference (Loc, | |
8394 | Prefix => New_Occurrence_Of (Typl, Loc), | |
8395 | Attribute_Name => Name_Machine, | |
8396 | Expressions => New_List (Relocate_Node (Rhs)))); | |
8397 | Analyze_And_Resolve (Rhs, Typl); | |
8398 | end if; | |
8399 | end if; | |
70482933 RK |
8400 | end Expand_N_Op_Eq; |
8401 | ||
8402 | ----------------------- | |
8403 | -- Expand_N_Op_Expon -- | |
8404 | ----------------------- | |
8405 | ||
8406 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
0bcee275 AC |
8407 | Loc : constant Source_Ptr := Sloc (N); |
8408 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
8409 | Typ : constant Entity_Id := Etype (N); | |
8410 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
8411 | ||
8412 | Bastyp : Entity_Id; | |
70482933 | 8413 | |
83496138 AC |
8414 | function Wrap_MA (Exp : Node_Id) return Node_Id; |
8415 | -- Given an expression Exp, if the root type is Float or Long_Float, | |
8416 | -- then wrap the expression in a call of Bastyp'Machine, to stop any | |
8417 | -- extra precision. This is done to ensure that X**A = X**B when A is | |
8418 | -- a static constant and B is a variable with the same value. For any | |
8419 | -- other type, the node Exp is returned unchanged. | |
8420 | ||
8421 | ------------- | |
8422 | -- Wrap_MA -- | |
8423 | ------------- | |
8424 | ||
8425 | function Wrap_MA (Exp : Node_Id) return Node_Id is | |
8426 | Loc : constant Source_Ptr := Sloc (Exp); | |
0bcee275 | 8427 | |
83496138 AC |
8428 | begin |
8429 | if Rtyp = Standard_Float or else Rtyp = Standard_Long_Float then | |
8430 | return | |
8431 | Make_Attribute_Reference (Loc, | |
8432 | Attribute_Name => Name_Machine, | |
8433 | Prefix => New_Occurrence_Of (Bastyp, Loc), | |
8434 | Expressions => New_List (Relocate_Node (Exp))); | |
8435 | else | |
8436 | return Exp; | |
8437 | end if; | |
8438 | end Wrap_MA; | |
8439 | ||
0bcee275 AC |
8440 | -- Local variables |
8441 | ||
8442 | Base : Node_Id; | |
8443 | Ent : Entity_Id; | |
8444 | Etyp : Entity_Id; | |
8445 | Exp : Node_Id; | |
8446 | Exptyp : Entity_Id; | |
8447 | Expv : Uint; | |
8448 | Rent : RE_Id; | |
8449 | Temp : Node_Id; | |
8450 | Xnode : Node_Id; | |
8451 | ||
904a2ae4 | 8452 | -- Start of processing for Expand_N_Op_Expon |
83496138 | 8453 | |
70482933 RK |
8454 | begin |
8455 | Binary_Op_Validity_Checks (N); | |
8456 | ||
5114f3ff | 8457 | -- CodePeer wants to see the unexpanded N_Op_Expon node |
8f66cda7 | 8458 | |
5114f3ff | 8459 | if CodePeer_Mode then |
8f66cda7 AC |
8460 | return; |
8461 | end if; | |
8462 | ||
904a2ae4 AC |
8463 | -- Relocation of left and right operands must be done after performing |
8464 | -- the validity checks since the generation of validation checks may | |
8465 | -- remove side effects. | |
8466 | ||
8467 | Base := Relocate_Node (Left_Opnd (N)); | |
8468 | Bastyp := Etype (Base); | |
8469 | Exp := Relocate_Node (Right_Opnd (N)); | |
8470 | Exptyp := Etype (Exp); | |
8471 | ||
685094bf RD |
8472 | -- If either operand is of a private type, then we have the use of an |
8473 | -- intrinsic operator, and we get rid of the privateness, by using root | |
8474 | -- types of underlying types for the actual operation. Otherwise the | |
8475 | -- private types will cause trouble if we expand multiplications or | |
8476 | -- shifts etc. We also do this transformation if the result type is | |
8477 | -- different from the base type. | |
07fc65c4 GB |
8478 | |
8479 | if Is_Private_Type (Etype (Base)) | |
8f66cda7 AC |
8480 | or else Is_Private_Type (Typ) |
8481 | or else Is_Private_Type (Exptyp) | |
8482 | or else Rtyp /= Root_Type (Bastyp) | |
07fc65c4 GB |
8483 | then |
8484 | declare | |
8485 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
8486 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
07fc65c4 GB |
8487 | begin |
8488 | Rewrite (N, | |
8489 | Unchecked_Convert_To (Typ, | |
8490 | Make_Op_Expon (Loc, | |
8491 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
8492 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
8493 | Analyze_And_Resolve (N, Typ); | |
8494 | return; | |
8495 | end; | |
8496 | end if; | |
8497 | ||
b6b5cca8 | 8498 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6cb3037c | 8499 | |
b6b5cca8 | 8500 | if Minimized_Eliminated_Overflow_Check (N) then |
6cb3037c AC |
8501 | Apply_Arithmetic_Overflow_Check (N); |
8502 | return; | |
8503 | end if; | |
8504 | ||
cb42ba5d AC |
8505 | -- Test for case of known right argument where we can replace the |
8506 | -- exponentiation by an equivalent expression using multiplication. | |
70482933 | 8507 | |
6c3c671e AC |
8508 | -- Note: use CRT_Safe version of Compile_Time_Known_Value because in |
8509 | -- configurable run-time mode, we may not have the exponentiation | |
8510 | -- routine available, and we don't want the legality of the program | |
8511 | -- to depend on how clever the compiler is in knowing values. | |
8512 | ||
8513 | if CRT_Safe_Compile_Time_Known_Value (Exp) then | |
70482933 RK |
8514 | Expv := Expr_Value (Exp); |
8515 | ||
8516 | -- We only fold small non-negative exponents. You might think we | |
8517 | -- could fold small negative exponents for the real case, but we | |
8518 | -- can't because we are required to raise Constraint_Error for | |
8519 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
83496138 | 8520 | -- See ACVC test C4A012B, and it is not worth generating the test. |
70482933 | 8521 | |
00f45f30 AC |
8522 | -- For small negative exponents, we return the reciprocal of |
8523 | -- the folding of the exponentiation for the opposite (positive) | |
8524 | -- exponent, as required by Ada RM 4.5.6(11/3). | |
8525 | ||
8526 | if abs Expv <= 4 then | |
70482933 RK |
8527 | |
8528 | -- X ** 0 = 1 (or 1.0) | |
8529 | ||
8530 | if Expv = 0 then | |
abcbd24c ST |
8531 | |
8532 | -- Call Remove_Side_Effects to ensure that any side effects | |
8533 | -- in the ignored left operand (in particular function calls | |
8534 | -- to user defined functions) are properly executed. | |
8535 | ||
8536 | Remove_Side_Effects (Base); | |
8537 | ||
70482933 RK |
8538 | if Ekind (Typ) in Integer_Kind then |
8539 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
8540 | else | |
8541 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
8542 | end if; | |
8543 | ||
8544 | -- X ** 1 = X | |
8545 | ||
8546 | elsif Expv = 1 then | |
8547 | Xnode := Base; | |
8548 | ||
8549 | -- X ** 2 = X * X | |
8550 | ||
8551 | elsif Expv = 2 then | |
8552 | Xnode := | |
83496138 AC |
8553 | Wrap_MA ( |
8554 | Make_Op_Multiply (Loc, | |
8555 | Left_Opnd => Duplicate_Subexpr (Base), | |
8556 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base))); | |
70482933 RK |
8557 | |
8558 | -- X ** 3 = X * X * X | |
8559 | ||
8560 | elsif Expv = 3 then | |
8561 | Xnode := | |
83496138 AC |
8562 | Wrap_MA ( |
8563 | Make_Op_Multiply (Loc, | |
8564 | Left_Opnd => | |
8565 | Make_Op_Multiply (Loc, | |
8566 | Left_Opnd => Duplicate_Subexpr (Base), | |
8567 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), | |
8568 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base))); | |
70482933 RK |
8569 | |
8570 | -- X ** 4 -> | |
cb42ba5d AC |
8571 | |
8572 | -- do | |
70482933 | 8573 | -- En : constant base'type := base * base; |
cb42ba5d | 8574 | -- in |
70482933 RK |
8575 | -- En * En |
8576 | ||
00f45f30 | 8577 | elsif Expv = 4 then |
191fcb3a | 8578 | Temp := Make_Temporary (Loc, 'E', Base); |
70482933 | 8579 | |
cb42ba5d AC |
8580 | Xnode := |
8581 | Make_Expression_With_Actions (Loc, | |
8582 | Actions => New_List ( | |
8583 | Make_Object_Declaration (Loc, | |
8584 | Defining_Identifier => Temp, | |
8585 | Constant_Present => True, | |
e4494292 | 8586 | Object_Definition => New_Occurrence_Of (Typ, Loc), |
cb42ba5d | 8587 | Expression => |
83496138 AC |
8588 | Wrap_MA ( |
8589 | Make_Op_Multiply (Loc, | |
8590 | Left_Opnd => | |
8591 | Duplicate_Subexpr (Base), | |
8592 | Right_Opnd => | |
8593 | Duplicate_Subexpr_No_Checks (Base))))), | |
cb42ba5d | 8594 | |
70482933 | 8595 | Expression => |
83496138 AC |
8596 | Wrap_MA ( |
8597 | Make_Op_Multiply (Loc, | |
8598 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
8599 | Right_Opnd => New_Occurrence_Of (Temp, Loc)))); | |
00f45f30 AC |
8600 | |
8601 | -- X ** N = 1.0 / X ** (-N) | |
8602 | -- N in -4 .. -1 | |
8603 | ||
8604 | else | |
8605 | pragma Assert | |
8606 | (Expv = -1 or Expv = -2 or Expv = -3 or Expv = -4); | |
72cdccfa | 8607 | |
00f45f30 AC |
8608 | Xnode := |
8609 | Make_Op_Divide (Loc, | |
8610 | Left_Opnd => | |
8611 | Make_Float_Literal (Loc, | |
8612 | Radix => Uint_1, | |
8613 | Significand => Uint_1, | |
8614 | Exponent => Uint_0), | |
8615 | Right_Opnd => | |
8616 | Make_Op_Expon (Loc, | |
8617 | Left_Opnd => Duplicate_Subexpr (Base), | |
8618 | Right_Opnd => | |
8619 | Make_Integer_Literal (Loc, | |
8620 | Intval => -Expv))); | |
70482933 RK |
8621 | end if; |
8622 | ||
8623 | Rewrite (N, Xnode); | |
8624 | Analyze_And_Resolve (N, Typ); | |
8625 | return; | |
8626 | end if; | |
8627 | end if; | |
8628 | ||
b502ba3c | 8629 | -- Deal with optimizing 2 ** expression to shift where possible |
685094bf | 8630 | |
8b4230c8 AC |
8631 | -- Note: we used to check that Exptyp was an unsigned type. But that is |
8632 | -- an unnecessary check, since if Exp is negative, we have a run-time | |
8633 | -- error that is either caught (so we get the right result) or we have | |
8634 | -- suppressed the check, in which case the code is erroneous anyway. | |
8635 | ||
b502ba3c RD |
8636 | if Is_Integer_Type (Rtyp) |
8637 | ||
c2b2b2d7 | 8638 | -- The base value must be "safe compile-time known", and exactly 2 |
b502ba3c RD |
8639 | |
8640 | and then Nkind (Base) = N_Integer_Literal | |
6c3c671e AC |
8641 | and then CRT_Safe_Compile_Time_Known_Value (Base) |
8642 | and then Expr_Value (Base) = Uint_2 | |
b502ba3c RD |
8643 | |
8644 | -- We only handle cases where the right type is a integer | |
8645 | ||
70482933 RK |
8646 | and then Is_Integer_Type (Root_Type (Exptyp)) |
8647 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
b502ba3c RD |
8648 | |
8649 | -- This transformation is not applicable for a modular type with a | |
a95f708e | 8650 | -- nonbinary modulus because we do not handle modular reduction in |
b502ba3c RD |
8651 | -- a correct manner if we attempt this transformation in this case. |
8652 | ||
8653 | and then not Non_Binary_Modulus (Typ) | |
70482933 | 8654 | then |
b502ba3c RD |
8655 | -- Handle the cases where our parent is a division or multiplication |
8656 | -- specially. In these cases we can convert to using a shift at the | |
8657 | -- parent level if we are not doing overflow checking, since it is | |
8658 | -- too tricky to combine the overflow check at the parent level. | |
70482933 | 8659 | |
b502ba3c RD |
8660 | if not Ovflo |
8661 | and then Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) | |
8662 | then | |
51bf9bdf AC |
8663 | declare |
8664 | P : constant Node_Id := Parent (N); | |
8665 | L : constant Node_Id := Left_Opnd (P); | |
8666 | R : constant Node_Id := Right_Opnd (P); | |
8667 | ||
8668 | begin | |
8669 | if (Nkind (P) = N_Op_Multiply | |
eb9008b7 AC |
8670 | and then |
8671 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
8672 | or else | |
8673 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
8674 | and then not Do_Overflow_Check (P)) | |
8675 | ||
51bf9bdf AC |
8676 | or else |
8677 | (Nkind (P) = N_Op_Divide | |
533369aa AC |
8678 | and then Is_Integer_Type (Etype (L)) |
8679 | and then Is_Unsigned_Type (Etype (L)) | |
8680 | and then R = N | |
8681 | and then not Do_Overflow_Check (P)) | |
51bf9bdf AC |
8682 | then |
8683 | Set_Is_Power_Of_2_For_Shift (N); | |
8684 | return; | |
8685 | end if; | |
8686 | end; | |
8687 | ||
b502ba3c RD |
8688 | -- Here we just have 2 ** N on its own, so we can convert this to a |
8689 | -- shift node. We are prepared to deal with overflow here, and we | |
8690 | -- also have to handle proper modular reduction for binary modular. | |
51bf9bdf | 8691 | |
b502ba3c RD |
8692 | else |
8693 | declare | |
8694 | OK : Boolean; | |
8695 | Lo : Uint; | |
8696 | Hi : Uint; | |
8697 | ||
8698 | MaxS : Uint; | |
8699 | -- Maximum shift count with no overflow | |
8700 | ||
8701 | TestS : Boolean; | |
8702 | -- Set True if we must test the shift count | |
8703 | ||
5389e4ae RD |
8704 | Test_Gt : Node_Id; |
8705 | -- Node for test against TestS | |
8706 | ||
b502ba3c RD |
8707 | begin |
8708 | -- Compute maximum shift based on the underlying size. For a | |
8709 | -- modular type this is one less than the size. | |
8710 | ||
8711 | if Is_Modular_Integer_Type (Typ) then | |
8712 | ||
8713 | -- For modular integer types, this is the size of the value | |
8714 | -- being shifted minus one. Any larger values will cause | |
8715 | -- modular reduction to a result of zero. Note that we do | |
8716 | -- want the RM_Size here (e.g. mod 2 ** 7, we want a result | |
8717 | -- of 6, since 2**7 should be reduced to zero). | |
8718 | ||
8719 | MaxS := RM_Size (Rtyp) - 1; | |
8720 | ||
8721 | -- For signed integer types, we use the size of the value | |
8722 | -- being shifted minus 2. Larger values cause overflow. | |
8723 | ||
8724 | else | |
8725 | MaxS := Esize (Rtyp) - 2; | |
8726 | end if; | |
8727 | ||
8728 | -- Determine range to see if it can be larger than MaxS | |
8729 | ||
8730 | Determine_Range | |
8731 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
8732 | TestS := (not OK) or else Hi > MaxS; | |
8733 | ||
8734 | -- Signed integer case | |
8735 | ||
8736 | if Is_Signed_Integer_Type (Typ) then | |
8737 | ||
8738 | -- Generate overflow check if overflow is active. Note that | |
8739 | -- we can simply ignore the possibility of overflow if the | |
8740 | -- flag is not set (means that overflow cannot happen or | |
8741 | -- that overflow checks are suppressed). | |
8742 | ||
8743 | if Ovflo and TestS then | |
8744 | Insert_Action (N, | |
8745 | Make_Raise_Constraint_Error (Loc, | |
8746 | Condition => | |
8747 | Make_Op_Gt (Loc, | |
8748 | Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)), | |
8749 | Right_Opnd => Make_Integer_Literal (Loc, MaxS)), | |
8750 | Reason => CE_Overflow_Check_Failed)); | |
8751 | end if; | |
8752 | ||
8753 | -- Now rewrite node as Shift_Left (1, right-operand) | |
8754 | ||
8755 | Rewrite (N, | |
8756 | Make_Op_Shift_Left (Loc, | |
8757 | Left_Opnd => Make_Integer_Literal (Loc, Uint_1), | |
8758 | Right_Opnd => Right_Opnd (N))); | |
8759 | ||
8760 | -- Modular integer case | |
8761 | ||
8762 | else pragma Assert (Is_Modular_Integer_Type (Typ)); | |
8763 | ||
8764 | -- If shift count can be greater than MaxS, we need to wrap | |
8765 | -- the shift in a test that will reduce the result value to | |
8766 | -- zero if this shift count is exceeded. | |
8767 | ||
8768 | if TestS then | |
5389e4ae RD |
8769 | |
8770 | -- Note: build node for the comparison first, before we | |
8771 | -- reuse the Right_Opnd, so that we have proper parents | |
8772 | -- in place for the Duplicate_Subexpr call. | |
8773 | ||
8774 | Test_Gt := | |
8775 | Make_Op_Gt (Loc, | |
8776 | Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)), | |
8777 | Right_Opnd => Make_Integer_Literal (Loc, MaxS)); | |
8778 | ||
b502ba3c RD |
8779 | Rewrite (N, |
8780 | Make_If_Expression (Loc, | |
8781 | Expressions => New_List ( | |
5389e4ae | 8782 | Test_Gt, |
b502ba3c | 8783 | Make_Integer_Literal (Loc, Uint_0), |
b502ba3c RD |
8784 | Make_Op_Shift_Left (Loc, |
8785 | Left_Opnd => Make_Integer_Literal (Loc, Uint_1), | |
8786 | Right_Opnd => Right_Opnd (N))))); | |
8787 | ||
8788 | -- If we know shift count cannot be greater than MaxS, then | |
8789 | -- it is safe to just rewrite as a shift with no test. | |
8790 | ||
8791 | else | |
8792 | Rewrite (N, | |
8793 | Make_Op_Shift_Left (Loc, | |
8794 | Left_Opnd => Make_Integer_Literal (Loc, Uint_1), | |
8795 | Right_Opnd => Right_Opnd (N))); | |
8796 | end if; | |
8797 | end if; | |
8798 | ||
8799 | Analyze_And_Resolve (N, Typ); | |
8800 | return; | |
8801 | end; | |
51bf9bdf | 8802 | end if; |
70482933 RK |
8803 | end if; |
8804 | ||
07fc65c4 GB |
8805 | -- Fall through if exponentiation must be done using a runtime routine |
8806 | ||
07fc65c4 | 8807 | -- First deal with modular case |
70482933 RK |
8808 | |
8809 | if Is_Modular_Integer_Type (Rtyp) then | |
8810 | ||
83496138 AC |
8811 | -- Nonbinary modular case, we call the special exponentiation |
8812 | -- routine for the nonbinary case, converting the argument to | |
8813 | -- Long_Long_Integer and passing the modulus value. Then the | |
8814 | -- result is converted back to the base type. | |
70482933 RK |
8815 | |
8816 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
8817 | Rewrite (N, |
8818 | Convert_To (Typ, | |
8819 | Make_Function_Call (Loc, | |
cc6f5d75 AC |
8820 | Name => |
8821 | New_Occurrence_Of (RTE (RE_Exp_Modular), Loc), | |
70482933 | 8822 | Parameter_Associations => New_List ( |
e9daba51 | 8823 | Convert_To (RTE (RE_Unsigned), Base), |
70482933 RK |
8824 | Make_Integer_Literal (Loc, Modulus (Rtyp)), |
8825 | Exp)))); | |
8826 | ||
83496138 AC |
8827 | -- Binary modular case, in this case, we call one of two routines, |
8828 | -- either the unsigned integer case, or the unsigned long long | |
8829 | -- integer case, with a final "and" operation to do the required mod. | |
70482933 RK |
8830 | |
8831 | else | |
8832 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
8833 | Ent := RTE (RE_Exp_Unsigned); | |
8834 | else | |
8835 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
8836 | end if; | |
8837 | ||
8838 | Rewrite (N, | |
8839 | Convert_To (Typ, | |
8840 | Make_Op_And (Loc, | |
cc6f5d75 | 8841 | Left_Opnd => |
70482933 | 8842 | Make_Function_Call (Loc, |
cc6f5d75 | 8843 | Name => New_Occurrence_Of (Ent, Loc), |
70482933 RK |
8844 | Parameter_Associations => New_List ( |
8845 | Convert_To (Etype (First_Formal (Ent)), Base), | |
8846 | Exp)), | |
8847 | Right_Opnd => | |
8848 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
8849 | ||
8850 | end if; | |
8851 | ||
8852 | -- Common exit point for modular type case | |
8853 | ||
8854 | Analyze_And_Resolve (N, Typ); | |
8855 | return; | |
8856 | ||
fbf5a39b AC |
8857 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
8858 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
8859 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 8860 | -- might need certification when a certified run time is required. |
70482933 | 8861 | |
fbf5a39b | 8862 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
8863 | -- checks are required, and one when they are not required, since there |
8864 | -- is a real gain in omitting checks on many machines. | |
70482933 | 8865 | |
fbf5a39b AC |
8866 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
8867 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
761f7dcb AC |
8868 | and then |
8869 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
8870 | or else Rtyp = Universal_Integer | |
70482933 | 8871 | then |
fbf5a39b AC |
8872 | Etyp := Standard_Long_Long_Integer; |
8873 | ||
f96fd197 | 8874 | if Ovflo then |
70482933 RK |
8875 | Rent := RE_Exp_Long_Long_Integer; |
8876 | else | |
8877 | Rent := RE_Exn_Long_Long_Integer; | |
8878 | end if; | |
8879 | ||
fbf5a39b AC |
8880 | elsif Is_Signed_Integer_Type (Rtyp) then |
8881 | Etyp := Standard_Integer; | |
70482933 | 8882 | |
f96fd197 | 8883 | if Ovflo then |
fbf5a39b | 8884 | Rent := RE_Exp_Integer; |
70482933 | 8885 | else |
fbf5a39b | 8886 | Rent := RE_Exn_Integer; |
70482933 | 8887 | end if; |
fbf5a39b | 8888 | |
83496138 AC |
8889 | -- Floating-point cases. We do not need separate routines for the |
8890 | -- overflow case here, since in the case of floating-point, we generate | |
8891 | -- infinities anyway as a rule (either that or we automatically trap | |
8892 | -- overflow), and if there is an infinity generated and a range check | |
8893 | -- is required, the check will fail anyway. | |
8894 | ||
8895 | -- Historical note: we used to convert everything to Long_Long_Float | |
8896 | -- and call a single common routine, but this had the undesirable effect | |
8897 | -- of giving different results for small static exponent values and the | |
8898 | -- same dynamic values. | |
fbf5a39b AC |
8899 | |
8900 | else | |
8901 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
83496138 AC |
8902 | |
8903 | if Rtyp = Standard_Float then | |
8904 | Etyp := Standard_Float; | |
8905 | Rent := RE_Exn_Float; | |
8906 | ||
8907 | elsif Rtyp = Standard_Long_Float then | |
8908 | Etyp := Standard_Long_Float; | |
8909 | Rent := RE_Exn_Long_Float; | |
8910 | ||
8911 | else | |
8912 | Etyp := Standard_Long_Long_Float; | |
8913 | Rent := RE_Exn_Long_Long_Float; | |
8914 | end if; | |
70482933 RK |
8915 | end if; |
8916 | ||
8917 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 8918 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 8919 | |
fbf5a39b | 8920 | if Typ = Etyp |
70482933 RK |
8921 | and then Rtyp /= Universal_Integer |
8922 | and then Rtyp /= Universal_Real | |
8923 | then | |
8924 | Rewrite (N, | |
83496138 AC |
8925 | Wrap_MA ( |
8926 | Make_Function_Call (Loc, | |
8927 | Name => New_Occurrence_Of (RTE (Rent), Loc), | |
8928 | Parameter_Associations => New_List (Base, Exp)))); | |
70482933 RK |
8929 | |
8930 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 8931 | -- required in the universal cases, since the runtime routine is |
1147c704 | 8932 | -- typed using one of the standard types). |
70482933 RK |
8933 | |
8934 | else | |
8935 | Rewrite (N, | |
8936 | Convert_To (Typ, | |
8937 | Make_Function_Call (Loc, | |
e4494292 | 8938 | Name => New_Occurrence_Of (RTE (Rent), Loc), |
70482933 | 8939 | Parameter_Associations => New_List ( |
fbf5a39b | 8940 | Convert_To (Etyp, Base), |
70482933 RK |
8941 | Exp)))); |
8942 | end if; | |
8943 | ||
8944 | Analyze_And_Resolve (N, Typ); | |
8945 | return; | |
8946 | ||
fbf5a39b AC |
8947 | exception |
8948 | when RE_Not_Available => | |
8949 | return; | |
70482933 RK |
8950 | end Expand_N_Op_Expon; |
8951 | ||
8952 | -------------------- | |
8953 | -- Expand_N_Op_Ge -- | |
8954 | -------------------- | |
8955 | ||
8956 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
8957 | Typ : constant Entity_Id := Etype (N); | |
8958 | Op1 : constant Node_Id := Left_Opnd (N); | |
8959 | Op2 : constant Node_Id := Right_Opnd (N); | |
8960 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
8961 | ||
8962 | begin | |
8963 | Binary_Op_Validity_Checks (N); | |
8964 | ||
456cbfa5 | 8965 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 8966 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
8967 | |
8968 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
8969 | ||
8970 | if Nkind (N) /= N_Op_Ge then | |
8971 | return; | |
8972 | end if; | |
8973 | ||
8974 | -- Array type case | |
8975 | ||
f02b8bb8 | 8976 | if Is_Array_Type (Typ1) then |
70482933 RK |
8977 | Expand_Array_Comparison (N); |
8978 | return; | |
8979 | end if; | |
8980 | ||
456cbfa5 AC |
8981 | -- Deal with boolean operands |
8982 | ||
70482933 RK |
8983 | if Is_Boolean_Type (Typ1) then |
8984 | Adjust_Condition (Op1); | |
8985 | Adjust_Condition (Op2); | |
8986 | Set_Etype (N, Standard_Boolean); | |
8987 | Adjust_Result_Type (N, Typ); | |
8988 | end if; | |
8989 | ||
8990 | Rewrite_Comparison (N); | |
f02b8bb8 | 8991 | |
0580d807 | 8992 | Optimize_Length_Comparison (N); |
70482933 RK |
8993 | end Expand_N_Op_Ge; |
8994 | ||
8995 | -------------------- | |
8996 | -- Expand_N_Op_Gt -- | |
8997 | -------------------- | |
8998 | ||
8999 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
9000 | Typ : constant Entity_Id := Etype (N); | |
9001 | Op1 : constant Node_Id := Left_Opnd (N); | |
9002 | Op2 : constant Node_Id := Right_Opnd (N); | |
9003 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
9004 | ||
9005 | begin | |
9006 | Binary_Op_Validity_Checks (N); | |
9007 | ||
456cbfa5 | 9008 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 9009 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
9010 | |
9011 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
9012 | ||
9013 | if Nkind (N) /= N_Op_Gt then | |
9014 | return; | |
9015 | end if; | |
9016 | ||
9017 | -- Deal with array type operands | |
9018 | ||
f02b8bb8 | 9019 | if Is_Array_Type (Typ1) then |
70482933 RK |
9020 | Expand_Array_Comparison (N); |
9021 | return; | |
9022 | end if; | |
9023 | ||
456cbfa5 AC |
9024 | -- Deal with boolean type operands |
9025 | ||
70482933 RK |
9026 | if Is_Boolean_Type (Typ1) then |
9027 | Adjust_Condition (Op1); | |
9028 | Adjust_Condition (Op2); | |
9029 | Set_Etype (N, Standard_Boolean); | |
9030 | Adjust_Result_Type (N, Typ); | |
9031 | end if; | |
9032 | ||
9033 | Rewrite_Comparison (N); | |
f02b8bb8 | 9034 | |
0580d807 | 9035 | Optimize_Length_Comparison (N); |
70482933 RK |
9036 | end Expand_N_Op_Gt; |
9037 | ||
9038 | -------------------- | |
9039 | -- Expand_N_Op_Le -- | |
9040 | -------------------- | |
9041 | ||
9042 | procedure Expand_N_Op_Le (N : Node_Id) is | |
9043 | Typ : constant Entity_Id := Etype (N); | |
9044 | Op1 : constant Node_Id := Left_Opnd (N); | |
9045 | Op2 : constant Node_Id := Right_Opnd (N); | |
9046 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
9047 | ||
9048 | begin | |
9049 | Binary_Op_Validity_Checks (N); | |
9050 | ||
456cbfa5 | 9051 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 9052 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
9053 | |
9054 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
9055 | ||
9056 | if Nkind (N) /= N_Op_Le then | |
9057 | return; | |
9058 | end if; | |
9059 | ||
9060 | -- Deal with array type operands | |
9061 | ||
f02b8bb8 | 9062 | if Is_Array_Type (Typ1) then |
70482933 RK |
9063 | Expand_Array_Comparison (N); |
9064 | return; | |
9065 | end if; | |
9066 | ||
456cbfa5 AC |
9067 | -- Deal with Boolean type operands |
9068 | ||
70482933 RK |
9069 | if Is_Boolean_Type (Typ1) then |
9070 | Adjust_Condition (Op1); | |
9071 | Adjust_Condition (Op2); | |
9072 | Set_Etype (N, Standard_Boolean); | |
9073 | Adjust_Result_Type (N, Typ); | |
9074 | end if; | |
9075 | ||
9076 | Rewrite_Comparison (N); | |
f02b8bb8 | 9077 | |
0580d807 | 9078 | Optimize_Length_Comparison (N); |
70482933 RK |
9079 | end Expand_N_Op_Le; |
9080 | ||
9081 | -------------------- | |
9082 | -- Expand_N_Op_Lt -- | |
9083 | -------------------- | |
9084 | ||
9085 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
9086 | Typ : constant Entity_Id := Etype (N); | |
9087 | Op1 : constant Node_Id := Left_Opnd (N); | |
9088 | Op2 : constant Node_Id := Right_Opnd (N); | |
9089 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
9090 | ||
9091 | begin | |
9092 | Binary_Op_Validity_Checks (N); | |
9093 | ||
456cbfa5 | 9094 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 9095 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
9096 | |
9097 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
9098 | ||
9099 | if Nkind (N) /= N_Op_Lt then | |
9100 | return; | |
9101 | end if; | |
9102 | ||
9103 | -- Deal with array type operands | |
9104 | ||
f02b8bb8 | 9105 | if Is_Array_Type (Typ1) then |
70482933 RK |
9106 | Expand_Array_Comparison (N); |
9107 | return; | |
9108 | end if; | |
9109 | ||
456cbfa5 AC |
9110 | -- Deal with Boolean type operands |
9111 | ||
70482933 RK |
9112 | if Is_Boolean_Type (Typ1) then |
9113 | Adjust_Condition (Op1); | |
9114 | Adjust_Condition (Op2); | |
9115 | Set_Etype (N, Standard_Boolean); | |
9116 | Adjust_Result_Type (N, Typ); | |
9117 | end if; | |
9118 | ||
9119 | Rewrite_Comparison (N); | |
f02b8bb8 | 9120 | |
0580d807 | 9121 | Optimize_Length_Comparison (N); |
70482933 RK |
9122 | end Expand_N_Op_Lt; |
9123 | ||
9124 | ----------------------- | |
9125 | -- Expand_N_Op_Minus -- | |
9126 | ----------------------- | |
9127 | ||
9128 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
9129 | Loc : constant Source_Ptr := Sloc (N); | |
9130 | Typ : constant Entity_Id := Etype (N); | |
9131 | ||
9132 | begin | |
9133 | Unary_Op_Validity_Checks (N); | |
9134 | ||
b6b5cca8 AC |
9135 | -- Check for MINIMIZED/ELIMINATED overflow mode |
9136 | ||
9137 | if Minimized_Eliminated_Overflow_Check (N) then | |
9138 | Apply_Arithmetic_Overflow_Check (N); | |
9139 | return; | |
9140 | end if; | |
9141 | ||
07fc65c4 | 9142 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
9143 | and then Is_Signed_Integer_Type (Etype (N)) |
9144 | and then Do_Overflow_Check (N) | |
9145 | then | |
9146 | -- Software overflow checking expands -expr into (0 - expr) | |
9147 | ||
9148 | Rewrite (N, | |
9149 | Make_Op_Subtract (Loc, | |
9150 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
9151 | Right_Opnd => Right_Opnd (N))); | |
9152 | ||
9153 | Analyze_And_Resolve (N, Typ); | |
70482933 | 9154 | end if; |
05dbb83f | 9155 | |
f4ac86dd | 9156 | Expand_Nonbinary_Modular_Op (N); |
70482933 RK |
9157 | end Expand_N_Op_Minus; |
9158 | ||
9159 | --------------------- | |
9160 | -- Expand_N_Op_Mod -- | |
9161 | --------------------- | |
9162 | ||
9163 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
9164 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 9165 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
9166 | DDC : constant Boolean := Do_Division_Check (N); |
9167 | ||
b6b5cca8 AC |
9168 | Left : Node_Id; |
9169 | Right : Node_Id; | |
9170 | ||
70482933 RK |
9171 | LLB : Uint; |
9172 | Llo : Uint; | |
9173 | Lhi : Uint; | |
9174 | LOK : Boolean; | |
9175 | Rlo : Uint; | |
9176 | Rhi : Uint; | |
9177 | ROK : Boolean; | |
9178 | ||
1033834f RD |
9179 | pragma Warnings (Off, Lhi); |
9180 | ||
70482933 RK |
9181 | begin |
9182 | Binary_Op_Validity_Checks (N); | |
9183 | ||
b6b5cca8 AC |
9184 | -- Check for MINIMIZED/ELIMINATED overflow mode |
9185 | ||
9186 | if Minimized_Eliminated_Overflow_Check (N) then | |
9187 | Apply_Arithmetic_Overflow_Check (N); | |
9188 | return; | |
9189 | end if; | |
9190 | ||
9a6dc470 RD |
9191 | if Is_Integer_Type (Etype (N)) then |
9192 | Apply_Divide_Checks (N); | |
b6b5cca8 AC |
9193 | |
9194 | -- All done if we don't have a MOD any more, which can happen as a | |
9195 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
9196 | ||
9197 | if Nkind (N) /= N_Op_Mod then | |
9198 | return; | |
9199 | end if; | |
9a6dc470 RD |
9200 | end if; |
9201 | ||
b6b5cca8 AC |
9202 | -- Proceed with expansion of mod operator |
9203 | ||
9204 | Left := Left_Opnd (N); | |
9205 | Right := Right_Opnd (N); | |
9206 | ||
5d5e9775 AC |
9207 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
9208 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); | |
70482933 | 9209 | |
2c9f8c0a AC |
9210 | -- Convert mod to rem if operands are both known to be non-negative, or |
9211 | -- both known to be non-positive (these are the cases in which rem and | |
9212 | -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite | |
9213 | -- likely that this will improve the quality of code, (the operation now | |
9214 | -- corresponds to the hardware remainder), and it does not seem likely | |
9215 | -- that it could be harmful. It also avoids some cases of the elaborate | |
9216 | -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %). | |
9217 | ||
9218 | if (LOK and ROK) | |
9219 | and then ((Llo >= 0 and then Rlo >= 0) | |
cc6f5d75 | 9220 | or else |
2c9f8c0a AC |
9221 | (Lhi <= 0 and then Rhi <= 0)) |
9222 | then | |
70482933 RK |
9223 | Rewrite (N, |
9224 | Make_Op_Rem (Sloc (N), | |
9225 | Left_Opnd => Left_Opnd (N), | |
9226 | Right_Opnd => Right_Opnd (N))); | |
9227 | ||
685094bf RD |
9228 | -- Instead of reanalyzing the node we do the analysis manually. This |
9229 | -- avoids anomalies when the replacement is done in an instance and | |
9230 | -- is epsilon more efficient. | |
70482933 RK |
9231 | |
9232 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 9233 | Set_Etype (N, Typ); |
70482933 RK |
9234 | Set_Do_Division_Check (N, DDC); |
9235 | Expand_N_Op_Rem (N); | |
9236 | Set_Analyzed (N); | |
2c9f8c0a | 9237 | return; |
70482933 RK |
9238 | |
9239 | -- Otherwise, normal mod processing | |
9240 | ||
9241 | else | |
fbf5a39b | 9242 | -- Apply optimization x mod 1 = 0. We don't really need that with |
f96fd197 AC |
9243 | -- gcc, but it is useful with other back ends and is certainly |
9244 | -- harmless. | |
fbf5a39b AC |
9245 | |
9246 | if Is_Integer_Type (Etype (N)) | |
9247 | and then Compile_Time_Known_Value (Right) | |
9248 | and then Expr_Value (Right) = Uint_1 | |
9249 | then | |
abcbd24c ST |
9250 | -- Call Remove_Side_Effects to ensure that any side effects in |
9251 | -- the ignored left operand (in particular function calls to | |
9252 | -- user defined functions) are properly executed. | |
9253 | ||
9254 | Remove_Side_Effects (Left); | |
9255 | ||
fbf5a39b AC |
9256 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
9257 | Analyze_And_Resolve (N, Typ); | |
9258 | return; | |
9259 | end if; | |
9260 | ||
2c9f8c0a AC |
9261 | -- If we still have a mod operator and we are in Modify_Tree_For_C |
9262 | -- mode, and we have a signed integer type, then here is where we do | |
9263 | -- the rewrite in terms of Rem. Note this rewrite bypasses the need | |
9264 | -- for the special handling of the annoying case of largest negative | |
9265 | -- number mod minus one. | |
9266 | ||
9267 | if Nkind (N) = N_Op_Mod | |
9268 | and then Is_Signed_Integer_Type (Typ) | |
9269 | and then Modify_Tree_For_C | |
9270 | then | |
9271 | -- In the general case, we expand A mod B as | |
9272 | ||
9273 | -- Tnn : constant typ := A rem B; | |
9274 | -- .. | |
9275 | -- (if (A >= 0) = (B >= 0) then Tnn | |
9276 | -- elsif Tnn = 0 then 0 | |
9277 | -- else Tnn + B) | |
9278 | ||
9279 | -- The comparison can be written simply as A >= 0 if we know that | |
9280 | -- B >= 0 which is a very common case. | |
9281 | ||
9282 | -- An important optimization is when B is known at compile time | |
9283 | -- to be 2**K for some constant. In this case we can simply AND | |
9284 | -- the left operand with the bit string 2**K-1 (i.e. K 1-bits) | |
9285 | -- and that works for both the positive and negative cases. | |
9286 | ||
9287 | declare | |
9288 | P2 : constant Nat := Power_Of_Two (Right); | |
9289 | ||
9290 | begin | |
9291 | if P2 /= 0 then | |
9292 | Rewrite (N, | |
9293 | Unchecked_Convert_To (Typ, | |
9294 | Make_Op_And (Loc, | |
9295 | Left_Opnd => | |
9296 | Unchecked_Convert_To | |
9297 | (Corresponding_Unsigned_Type (Typ), Left), | |
9298 | Right_Opnd => | |
9299 | Make_Integer_Literal (Loc, 2 ** P2 - 1)))); | |
9300 | Analyze_And_Resolve (N, Typ); | |
9301 | return; | |
9302 | end if; | |
9303 | end; | |
9304 | ||
9305 | -- Here for the full rewrite | |
9306 | ||
9307 | declare | |
9308 | Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N); | |
9309 | Cmp : Node_Id; | |
9310 | ||
9311 | begin | |
9312 | Cmp := | |
9313 | Make_Op_Ge (Loc, | |
9314 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
9315 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
9316 | ||
9317 | if not LOK or else Rlo < 0 then | |
9318 | Cmp := | |
9319 | Make_Op_Eq (Loc, | |
9320 | Left_Opnd => Cmp, | |
9321 | Right_Opnd => | |
9322 | Make_Op_Ge (Loc, | |
9323 | Left_Opnd => Duplicate_Subexpr_No_Checks (Right), | |
9324 | Right_Opnd => Make_Integer_Literal (Loc, 0))); | |
9325 | end if; | |
9326 | ||
9327 | Insert_Action (N, | |
9328 | Make_Object_Declaration (Loc, | |
9329 | Defining_Identifier => Tnn, | |
9330 | Constant_Present => True, | |
9331 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
9332 | Expression => | |
9333 | Make_Op_Rem (Loc, | |
9334 | Left_Opnd => Left, | |
9335 | Right_Opnd => Right))); | |
9336 | ||
9337 | Rewrite (N, | |
9338 | Make_If_Expression (Loc, | |
9339 | Expressions => New_List ( | |
9340 | Cmp, | |
9341 | New_Occurrence_Of (Tnn, Loc), | |
9342 | Make_If_Expression (Loc, | |
9343 | Is_Elsif => True, | |
9344 | Expressions => New_List ( | |
9345 | Make_Op_Eq (Loc, | |
9346 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
9347 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
9348 | Make_Integer_Literal (Loc, 0), | |
9349 | Make_Op_Add (Loc, | |
9350 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
9351 | Right_Opnd => | |
9352 | Duplicate_Subexpr_No_Checks (Right))))))); | |
9353 | ||
9354 | Analyze_And_Resolve (N, Typ); | |
9355 | return; | |
9356 | end; | |
9357 | end if; | |
9358 | ||
9359 | -- Deal with annoying case of largest negative number mod minus one. | |
9360 | -- Gigi may not handle this case correctly, because on some targets, | |
9361 | -- the mod value is computed using a divide instruction which gives | |
9362 | -- an overflow trap for this case. | |
b9daa96e AC |
9363 | |
9364 | -- It would be a bit more efficient to figure out which targets | |
9365 | -- this is really needed for, but in practice it is reasonable | |
9366 | -- to do the following special check in all cases, since it means | |
9367 | -- we get a clearer message, and also the overhead is minimal given | |
9368 | -- that division is expensive in any case. | |
70482933 | 9369 | |
685094bf RD |
9370 | -- In fact the check is quite easy, if the right operand is -1, then |
9371 | -- the mod value is always 0, and we can just ignore the left operand | |
9372 | -- completely in this case. | |
70482933 | 9373 | |
9a6dc470 RD |
9374 | -- This only applies if we still have a mod operator. Skip if we |
9375 | -- have already rewritten this (e.g. in the case of eliminated | |
9376 | -- overflow checks which have driven us into bignum mode). | |
fbf5a39b | 9377 | |
9a6dc470 | 9378 | if Nkind (N) = N_Op_Mod then |
70482933 | 9379 | |
9a6dc470 RD |
9380 | -- The operand type may be private (e.g. in the expansion of an |
9381 | -- intrinsic operation) so we must use the underlying type to get | |
9382 | -- the bounds, and convert the literals explicitly. | |
70482933 | 9383 | |
9a6dc470 RD |
9384 | LLB := |
9385 | Expr_Value | |
9386 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
9387 | ||
9388 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
761f7dcb | 9389 | and then ((not LOK) or else (Llo = LLB)) |
9a6dc470 RD |
9390 | then |
9391 | Rewrite (N, | |
9b16cb57 | 9392 | Make_If_Expression (Loc, |
9a6dc470 RD |
9393 | Expressions => New_List ( |
9394 | Make_Op_Eq (Loc, | |
9395 | Left_Opnd => Duplicate_Subexpr (Right), | |
9396 | Right_Opnd => | |
9397 | Unchecked_Convert_To (Typ, | |
9398 | Make_Integer_Literal (Loc, -1))), | |
9399 | Unchecked_Convert_To (Typ, | |
9400 | Make_Integer_Literal (Loc, Uint_0)), | |
9401 | Relocate_Node (N)))); | |
9402 | ||
9403 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
9404 | Analyze_And_Resolve (N, Typ); | |
9405 | end if; | |
70482933 RK |
9406 | end if; |
9407 | end if; | |
9408 | end Expand_N_Op_Mod; | |
9409 | ||
9410 | -------------------------- | |
9411 | -- Expand_N_Op_Multiply -- | |
9412 | -------------------------- | |
9413 | ||
9414 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
9415 | Loc : constant Source_Ptr := Sloc (N); |
9416 | Lop : constant Node_Id := Left_Opnd (N); | |
9417 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 9418 | |
abcbd24c | 9419 | Lp2 : constant Boolean := |
533369aa | 9420 | Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop); |
abcbd24c | 9421 | Rp2 : constant Boolean := |
533369aa | 9422 | Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop); |
fbf5a39b | 9423 | |
70482933 RK |
9424 | Ltyp : constant Entity_Id := Etype (Lop); |
9425 | Rtyp : constant Entity_Id := Etype (Rop); | |
9426 | Typ : Entity_Id := Etype (N); | |
9427 | ||
9428 | begin | |
9429 | Binary_Op_Validity_Checks (N); | |
9430 | ||
b6b5cca8 AC |
9431 | -- Check for MINIMIZED/ELIMINATED overflow mode |
9432 | ||
9433 | if Minimized_Eliminated_Overflow_Check (N) then | |
9434 | Apply_Arithmetic_Overflow_Check (N); | |
9435 | return; | |
9436 | end if; | |
9437 | ||
70482933 RK |
9438 | -- Special optimizations for integer types |
9439 | ||
9440 | if Is_Integer_Type (Typ) then | |
9441 | ||
abcbd24c | 9442 | -- N * 0 = 0 for integer types |
70482933 | 9443 | |
abcbd24c ST |
9444 | if Compile_Time_Known_Value (Rop) |
9445 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 9446 | then |
abcbd24c ST |
9447 | -- Call Remove_Side_Effects to ensure that any side effects in |
9448 | -- the ignored left operand (in particular function calls to | |
9449 | -- user defined functions) are properly executed. | |
9450 | ||
9451 | Remove_Side_Effects (Lop); | |
9452 | ||
9453 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
9454 | Analyze_And_Resolve (N, Typ); | |
9455 | return; | |
9456 | end if; | |
9457 | ||
9458 | -- Similar handling for 0 * N = 0 | |
9459 | ||
9460 | if Compile_Time_Known_Value (Lop) | |
9461 | and then Expr_Value (Lop) = Uint_0 | |
9462 | then | |
9463 | Remove_Side_Effects (Rop); | |
70482933 RK |
9464 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
9465 | Analyze_And_Resolve (N, Typ); | |
9466 | return; | |
9467 | end if; | |
9468 | ||
9469 | -- N * 1 = 1 * N = N for integer types | |
9470 | ||
fbf5a39b AC |
9471 | -- This optimisation is not done if we are going to |
9472 | -- rewrite the product 1 * 2 ** N to a shift. | |
9473 | ||
9474 | if Compile_Time_Known_Value (Rop) | |
9475 | and then Expr_Value (Rop) = Uint_1 | |
9476 | and then not Lp2 | |
70482933 | 9477 | then |
fbf5a39b | 9478 | Rewrite (N, Lop); |
70482933 RK |
9479 | return; |
9480 | ||
fbf5a39b AC |
9481 | elsif Compile_Time_Known_Value (Lop) |
9482 | and then Expr_Value (Lop) = Uint_1 | |
9483 | and then not Rp2 | |
70482933 | 9484 | then |
fbf5a39b | 9485 | Rewrite (N, Rop); |
70482933 RK |
9486 | return; |
9487 | end if; | |
9488 | end if; | |
9489 | ||
70482933 RK |
9490 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
9491 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
9492 | -- operand is an integer, as required for this to work. | |
9493 | ||
fbf5a39b AC |
9494 | if Rp2 then |
9495 | if Lp2 then | |
70482933 | 9496 | |
fbf5a39b | 9497 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
9498 | |
9499 | Rewrite (N, | |
9500 | Make_Op_Expon (Loc, | |
9501 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
9502 | Right_Opnd => | |
9503 | Make_Op_Add (Loc, | |
9504 | Left_Opnd => Right_Opnd (Lop), | |
9505 | Right_Opnd => Right_Opnd (Rop)))); | |
9506 | Analyze_And_Resolve (N, Typ); | |
9507 | return; | |
9508 | ||
9509 | else | |
eefe3761 AC |
9510 | -- If the result is modular, perform the reduction of the result |
9511 | -- appropriately. | |
9512 | ||
9513 | if Is_Modular_Integer_Type (Typ) | |
9514 | and then not Non_Binary_Modulus (Typ) | |
9515 | then | |
9516 | Rewrite (N, | |
573e5dd6 RD |
9517 | Make_Op_And (Loc, |
9518 | Left_Opnd => | |
9519 | Make_Op_Shift_Left (Loc, | |
9520 | Left_Opnd => Lop, | |
9521 | Right_Opnd => | |
9522 | Convert_To (Standard_Natural, Right_Opnd (Rop))), | |
9523 | Right_Opnd => | |
eefe3761 | 9524 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); |
573e5dd6 | 9525 | |
eefe3761 AC |
9526 | else |
9527 | Rewrite (N, | |
9528 | Make_Op_Shift_Left (Loc, | |
9529 | Left_Opnd => Lop, | |
9530 | Right_Opnd => | |
9531 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
9532 | end if; | |
9533 | ||
70482933 RK |
9534 | Analyze_And_Resolve (N, Typ); |
9535 | return; | |
9536 | end if; | |
9537 | ||
9538 | -- Same processing for the operands the other way round | |
9539 | ||
fbf5a39b | 9540 | elsif Lp2 then |
eefe3761 AC |
9541 | if Is_Modular_Integer_Type (Typ) |
9542 | and then not Non_Binary_Modulus (Typ) | |
9543 | then | |
9544 | Rewrite (N, | |
573e5dd6 RD |
9545 | Make_Op_And (Loc, |
9546 | Left_Opnd => | |
9547 | Make_Op_Shift_Left (Loc, | |
9548 | Left_Opnd => Rop, | |
9549 | Right_Opnd => | |
9550 | Convert_To (Standard_Natural, Right_Opnd (Lop))), | |
9551 | Right_Opnd => | |
9552 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); | |
9553 | ||
eefe3761 AC |
9554 | else |
9555 | Rewrite (N, | |
9556 | Make_Op_Shift_Left (Loc, | |
9557 | Left_Opnd => Rop, | |
9558 | Right_Opnd => | |
9559 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
9560 | end if; | |
9561 | ||
70482933 RK |
9562 | Analyze_And_Resolve (N, Typ); |
9563 | return; | |
9564 | end if; | |
9565 | ||
9566 | -- Do required fixup of universal fixed operation | |
9567 | ||
9568 | if Typ = Universal_Fixed then | |
9569 | Fixup_Universal_Fixed_Operation (N); | |
9570 | Typ := Etype (N); | |
9571 | end if; | |
9572 | ||
9573 | -- Multiplications with fixed-point results | |
9574 | ||
9575 | if Is_Fixed_Point_Type (Typ) then | |
9576 | ||
685094bf RD |
9577 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
9578 | -- a semantic point of view such operations are simply integer | |
9579 | -- operations and will be treated that way. | |
70482933 RK |
9580 | |
9581 | if not Treat_Fixed_As_Integer (N) then | |
9582 | ||
9583 | -- Case of fixed * integer => fixed | |
9584 | ||
9585 | if Is_Integer_Type (Rtyp) then | |
9586 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
9587 | ||
9588 | -- Case of integer * fixed => fixed | |
9589 | ||
9590 | elsif Is_Integer_Type (Ltyp) then | |
9591 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
9592 | ||
9593 | -- Case of fixed * fixed => fixed | |
9594 | ||
9595 | else | |
9596 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
9597 | end if; | |
9598 | end if; | |
9599 | ||
685094bf RD |
9600 | -- Other cases of multiplication of fixed-point operands. Again we |
9601 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
9602 | |
9603 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
9604 | and then not Treat_Fixed_As_Integer (N) | |
9605 | then | |
9606 | if Is_Integer_Type (Typ) then | |
9607 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
9608 | else | |
9609 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
9610 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
9611 | end if; | |
9612 | ||
685094bf RD |
9613 | -- Mixed-mode operations can appear in a non-static universal context, |
9614 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 9615 | |
533369aa | 9616 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
70482933 | 9617 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); |
70482933 RK |
9618 | Analyze_And_Resolve (Rop, Universal_Real); |
9619 | ||
533369aa | 9620 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
70482933 | 9621 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); |
70482933 RK |
9622 | Analyze_And_Resolve (Lop, Universal_Real); |
9623 | ||
9624 | -- Non-fixed point cases, check software overflow checking required | |
9625 | ||
9626 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
9627 | Apply_Arithmetic_Overflow_Check (N); | |
9628 | end if; | |
dfaff97b RD |
9629 | |
9630 | -- Overflow checks for floating-point if -gnateF mode active | |
9631 | ||
9632 | Check_Float_Op_Overflow (N); | |
05dbb83f | 9633 | |
f4ac86dd | 9634 | Expand_Nonbinary_Modular_Op (N); |
70482933 RK |
9635 | end Expand_N_Op_Multiply; |
9636 | ||
9637 | -------------------- | |
9638 | -- Expand_N_Op_Ne -- | |
9639 | -------------------- | |
9640 | ||
70482933 | 9641 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 9642 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
9643 | |
9644 | begin | |
60f66f34 GD |
9645 | -- Case of elementary type with standard operator. But if unnesting, |
9646 | -- handle elementary types whose Equivalent_Types are records because | |
9647 | -- there may be padding or undefined fields. | |
70482933 | 9648 | |
f02b8bb8 RD |
9649 | if Is_Elementary_Type (Typ) |
9650 | and then Sloc (Entity (N)) = Standard_Location | |
6bc08721 JM |
9651 | and then not (Ekind_In (Typ, E_Class_Wide_Type, |
9652 | E_Class_Wide_Subtype, | |
9653 | E_Access_Subprogram_Type, | |
9654 | E_Access_Protected_Subprogram_Type, | |
9655 | E_Anonymous_Access_Protected_Subprogram_Type, | |
9656 | E_Access_Subprogram_Type, | |
9657 | E_Exception_Type) | |
9658 | and then Present (Equivalent_Type (Typ)) | |
9659 | and then Is_Record_Type (Equivalent_Type (Typ))) | |
f02b8bb8 RD |
9660 | then |
9661 | Binary_Op_Validity_Checks (N); | |
70482933 | 9662 | |
456cbfa5 | 9663 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if |
60b68e56 | 9664 | -- means we no longer have a /= operation, we are all done. |
456cbfa5 AC |
9665 | |
9666 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
9667 | ||
9668 | if Nkind (N) /= N_Op_Ne then | |
9669 | return; | |
9670 | end if; | |
9671 | ||
f02b8bb8 | 9672 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 9673 | |
f02b8bb8 RD |
9674 | if Is_Boolean_Type (Typ) then |
9675 | Adjust_Condition (Left_Opnd (N)); | |
9676 | Adjust_Condition (Right_Opnd (N)); | |
9677 | Set_Etype (N, Standard_Boolean); | |
9678 | Adjust_Result_Type (N, Typ); | |
9679 | end if; | |
fbf5a39b | 9680 | |
f02b8bb8 RD |
9681 | Rewrite_Comparison (N); |
9682 | ||
f02b8bb8 RD |
9683 | -- For all cases other than elementary types, we rewrite node as the |
9684 | -- negation of an equality operation, and reanalyze. The equality to be | |
9685 | -- used is defined in the same scope and has the same signature. This | |
9686 | -- signature must be set explicitly since in an instance it may not have | |
9687 | -- the same visibility as in the generic unit. This avoids duplicating | |
9688 | -- or factoring the complex code for record/array equality tests etc. | |
9689 | ||
99bba92c AC |
9690 | -- This case is also used for the minimal expansion performed in |
9691 | -- GNATprove mode. | |
9692 | ||
f02b8bb8 RD |
9693 | else |
9694 | declare | |
9695 | Loc : constant Source_Ptr := Sloc (N); | |
9696 | Neg : Node_Id; | |
9697 | Ne : constant Entity_Id := Entity (N); | |
9698 | ||
9699 | begin | |
9700 | Binary_Op_Validity_Checks (N); | |
9701 | ||
9702 | Neg := | |
9703 | Make_Op_Not (Loc, | |
9704 | Right_Opnd => | |
9705 | Make_Op_Eq (Loc, | |
9706 | Left_Opnd => Left_Opnd (N), | |
9707 | Right_Opnd => Right_Opnd (N))); | |
99bba92c AC |
9708 | |
9709 | -- The level of parentheses is useless in GNATprove mode, and | |
9710 | -- bumping its level here leads to wrong columns being used in | |
9711 | -- check messages, hence skip it in this mode. | |
9712 | ||
9713 | if not GNATprove_Mode then | |
9714 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
9715 | end if; | |
f02b8bb8 RD |
9716 | |
9717 | if Scope (Ne) /= Standard_Standard then | |
9718 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
9719 | end if; | |
9720 | ||
4637729f | 9721 | -- For navigation purposes, we want to treat the inequality as an |
f02b8bb8 | 9722 | -- implicit reference to the corresponding equality. Preserve the |
4637729f | 9723 | -- Comes_From_ source flag to generate proper Xref entries. |
f02b8bb8 RD |
9724 | |
9725 | Preserve_Comes_From_Source (Neg, N); | |
9726 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
9727 | Rewrite (N, Neg); | |
9728 | Analyze_And_Resolve (N, Standard_Boolean); | |
9729 | end; | |
9730 | end if; | |
0580d807 | 9731 | |
99bba92c AC |
9732 | -- No need for optimization in GNATprove mode, where we would rather see |
9733 | -- the original source expression. | |
9734 | ||
9735 | if not GNATprove_Mode then | |
9736 | Optimize_Length_Comparison (N); | |
9737 | end if; | |
70482933 RK |
9738 | end Expand_N_Op_Ne; |
9739 | ||
9740 | --------------------- | |
9741 | -- Expand_N_Op_Not -- | |
9742 | --------------------- | |
9743 | ||
685094bf | 9744 | -- If the argument is other than a Boolean array type, there is no special |
7a5b62b0 AC |
9745 | -- expansion required, except for dealing with validity checks, and non- |
9746 | -- standard boolean representations. | |
70482933 | 9747 | |
7a5b62b0 AC |
9748 | -- For the packed array case, we call the special routine in Exp_Pakd, |
9749 | -- except that if the component size is greater than one, we use the | |
9750 | -- standard routine generating a gruesome loop (it is so peculiar to have | |
9751 | -- packed arrays with non-standard Boolean representations anyway, so it | |
9752 | -- does not matter that we do not handle this case efficiently). | |
70482933 | 9753 | |
7a5b62b0 AC |
9754 | -- For the unpacked array case (and for the special packed case where we |
9755 | -- have non standard Booleans, as discussed above), we generate and insert | |
9756 | -- into the tree the following function definition: | |
70482933 RK |
9757 | |
9758 | -- function Nnnn (A : arr) is | |
9759 | -- B : arr; | |
9760 | -- begin | |
9761 | -- for J in a'range loop | |
9762 | -- B (J) := not A (J); | |
9763 | -- end loop; | |
9764 | -- return B; | |
9765 | -- end Nnnn; | |
9766 | ||
9767 | -- Here arr is the actual subtype of the parameter (and hence always | |
9768 | -- constrained). Then we replace the not with a call to this function. | |
9769 | ||
9770 | procedure Expand_N_Op_Not (N : Node_Id) is | |
9771 | Loc : constant Source_Ptr := Sloc (N); | |
9772 | Typ : constant Entity_Id := Etype (N); | |
9773 | Opnd : Node_Id; | |
9774 | Arr : Entity_Id; | |
9775 | A : Entity_Id; | |
9776 | B : Entity_Id; | |
9777 | J : Entity_Id; | |
9778 | A_J : Node_Id; | |
9779 | B_J : Node_Id; | |
9780 | ||
9781 | Func_Name : Entity_Id; | |
9782 | Loop_Statement : Node_Id; | |
9783 | ||
9784 | begin | |
9785 | Unary_Op_Validity_Checks (N); | |
9786 | ||
9787 | -- For boolean operand, deal with non-standard booleans | |
9788 | ||
9789 | if Is_Boolean_Type (Typ) then | |
9790 | Adjust_Condition (Right_Opnd (N)); | |
9791 | Set_Etype (N, Standard_Boolean); | |
9792 | Adjust_Result_Type (N, Typ); | |
9793 | return; | |
9794 | end if; | |
9795 | ||
da94696d | 9796 | -- Only array types need any other processing |
70482933 | 9797 | |
da94696d | 9798 | if not Is_Array_Type (Typ) then |
70482933 RK |
9799 | return; |
9800 | end if; | |
9801 | ||
a9d8907c JM |
9802 | -- Case of array operand. If bit packed with a component size of 1, |
9803 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 9804 | |
a9d8907c JM |
9805 | if Is_Bit_Packed_Array (Typ) |
9806 | and then Component_Size (Typ) = 1 | |
9807 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
9808 | then | |
70482933 RK |
9809 | Expand_Packed_Not (N); |
9810 | return; | |
9811 | end if; | |
9812 | ||
fbf5a39b AC |
9813 | -- Case of array operand which is not bit-packed. If the context is |
9814 | -- a safe assignment, call in-place operation, If context is a larger | |
9815 | -- boolean expression in the context of a safe assignment, expansion is | |
9816 | -- done by enclosing operation. | |
70482933 RK |
9817 | |
9818 | Opnd := Relocate_Node (Right_Opnd (N)); | |
9819 | Convert_To_Actual_Subtype (Opnd); | |
9820 | Arr := Etype (Opnd); | |
9821 | Ensure_Defined (Arr, N); | |
b4592168 | 9822 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 9823 | |
fbf5a39b AC |
9824 | if Nkind (Parent (N)) = N_Assignment_Statement then |
9825 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
9826 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
9827 | return; | |
9828 | ||
5e1c00fa | 9829 | -- Special case the negation of a binary operation |
fbf5a39b | 9830 | |
303b4d58 | 9831 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 9832 | and then Safe_In_Place_Array_Op |
303b4d58 | 9833 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
9834 | then |
9835 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
9836 | return; | |
9837 | end if; | |
9838 | ||
9839 | elsif Nkind (Parent (N)) in N_Binary_Op | |
9840 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
9841 | then | |
9842 | declare | |
9843 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
9844 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
9845 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
9846 | ||
9847 | begin | |
9848 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
fbf5a39b | 9849 | |
aa9a7dd7 AC |
9850 | -- (not A) op (not B) can be reduced to a single call |
9851 | ||
9852 | if N = Op1 and then Nkind (Op2) = N_Op_Not then | |
fbf5a39b AC |
9853 | return; |
9854 | ||
bed8af19 AC |
9855 | elsif N = Op2 and then Nkind (Op1) = N_Op_Not then |
9856 | return; | |
9857 | ||
aa9a7dd7 | 9858 | -- A xor (not B) can also be special-cased |
fbf5a39b | 9859 | |
aa9a7dd7 | 9860 | elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then |
fbf5a39b AC |
9861 | return; |
9862 | end if; | |
9863 | end if; | |
9864 | end; | |
9865 | end if; | |
9866 | ||
70482933 RK |
9867 | A := Make_Defining_Identifier (Loc, Name_uA); |
9868 | B := Make_Defining_Identifier (Loc, Name_uB); | |
9869 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
9870 | ||
9871 | A_J := | |
9872 | Make_Indexed_Component (Loc, | |
e4494292 RD |
9873 | Prefix => New_Occurrence_Of (A, Loc), |
9874 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
9875 | |
9876 | B_J := | |
9877 | Make_Indexed_Component (Loc, | |
e4494292 RD |
9878 | Prefix => New_Occurrence_Of (B, Loc), |
9879 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
9880 | |
9881 | Loop_Statement := | |
9882 | Make_Implicit_Loop_Statement (N, | |
9883 | Identifier => Empty, | |
9884 | ||
9885 | Iteration_Scheme => | |
9886 | Make_Iteration_Scheme (Loc, | |
9887 | Loop_Parameter_Specification => | |
9888 | Make_Loop_Parameter_Specification (Loc, | |
0d901290 | 9889 | Defining_Identifier => J, |
70482933 RK |
9890 | Discrete_Subtype_Definition => |
9891 | Make_Attribute_Reference (Loc, | |
0d901290 | 9892 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
9893 | Attribute_Name => Name_Range))), |
9894 | ||
9895 | Statements => New_List ( | |
9896 | Make_Assignment_Statement (Loc, | |
9897 | Name => B_J, | |
9898 | Expression => Make_Op_Not (Loc, A_J)))); | |
9899 | ||
191fcb3a | 9900 | Func_Name := Make_Temporary (Loc, 'N'); |
70482933 RK |
9901 | Set_Is_Inlined (Func_Name); |
9902 | ||
9903 | Insert_Action (N, | |
9904 | Make_Subprogram_Body (Loc, | |
9905 | Specification => | |
9906 | Make_Function_Specification (Loc, | |
9907 | Defining_Unit_Name => Func_Name, | |
9908 | Parameter_Specifications => New_List ( | |
9909 | Make_Parameter_Specification (Loc, | |
9910 | Defining_Identifier => A, | |
e4494292 RD |
9911 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), |
9912 | Result_Definition => New_Occurrence_Of (Typ, Loc)), | |
70482933 RK |
9913 | |
9914 | Declarations => New_List ( | |
9915 | Make_Object_Declaration (Loc, | |
9916 | Defining_Identifier => B, | |
e4494292 | 9917 | Object_Definition => New_Occurrence_Of (Arr, Loc))), |
70482933 RK |
9918 | |
9919 | Handled_Statement_Sequence => | |
9920 | Make_Handled_Sequence_Of_Statements (Loc, | |
9921 | Statements => New_List ( | |
9922 | Loop_Statement, | |
d766cee3 | 9923 | Make_Simple_Return_Statement (Loc, |
0d901290 | 9924 | Expression => Make_Identifier (Loc, Chars (B))))))); |
70482933 RK |
9925 | |
9926 | Rewrite (N, | |
9927 | Make_Function_Call (Loc, | |
e4494292 | 9928 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
9929 | Parameter_Associations => New_List (Opnd))); |
9930 | ||
9931 | Analyze_And_Resolve (N, Typ); | |
9932 | end Expand_N_Op_Not; | |
9933 | ||
9934 | -------------------- | |
9935 | -- Expand_N_Op_Or -- | |
9936 | -------------------- | |
9937 | ||
9938 | procedure Expand_N_Op_Or (N : Node_Id) is | |
9939 | Typ : constant Entity_Id := Etype (N); | |
9940 | ||
9941 | begin | |
9942 | Binary_Op_Validity_Checks (N); | |
9943 | ||
9944 | if Is_Array_Type (Etype (N)) then | |
9945 | Expand_Boolean_Operator (N); | |
9946 | ||
9947 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
9948 | Adjust_Condition (Left_Opnd (N)); |
9949 | Adjust_Condition (Right_Opnd (N)); | |
9950 | Set_Etype (N, Standard_Boolean); | |
9951 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
9952 | |
9953 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
9954 | Expand_Intrinsic_Call (N, Entity (N)); | |
05dbb83f AC |
9955 | end if; |
9956 | ||
f4ac86dd | 9957 | Expand_Nonbinary_Modular_Op (N); |
70482933 RK |
9958 | end Expand_N_Op_Or; |
9959 | ||
9960 | ---------------------- | |
9961 | -- Expand_N_Op_Plus -- | |
9962 | ---------------------- | |
9963 | ||
9964 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
9965 | begin | |
9966 | Unary_Op_Validity_Checks (N); | |
b6b5cca8 AC |
9967 | |
9968 | -- Check for MINIMIZED/ELIMINATED overflow mode | |
9969 | ||
9970 | if Minimized_Eliminated_Overflow_Check (N) then | |
9971 | Apply_Arithmetic_Overflow_Check (N); | |
9972 | return; | |
9973 | end if; | |
70482933 RK |
9974 | end Expand_N_Op_Plus; |
9975 | ||
9976 | --------------------- | |
9977 | -- Expand_N_Op_Rem -- | |
9978 | --------------------- | |
9979 | ||
9980 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
9981 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 9982 | Typ : constant Entity_Id := Etype (N); |
70482933 | 9983 | |
b6b5cca8 AC |
9984 | Left : Node_Id; |
9985 | Right : Node_Id; | |
70482933 | 9986 | |
5d5e9775 AC |
9987 | Lo : Uint; |
9988 | Hi : Uint; | |
9989 | OK : Boolean; | |
70482933 | 9990 | |
5d5e9775 AC |
9991 | Lneg : Boolean; |
9992 | Rneg : Boolean; | |
9993 | -- Set if corresponding operand can be negative | |
9994 | ||
9995 | pragma Unreferenced (Hi); | |
1033834f | 9996 | |
70482933 RK |
9997 | begin |
9998 | Binary_Op_Validity_Checks (N); | |
9999 | ||
b6b5cca8 AC |
10000 | -- Check for MINIMIZED/ELIMINATED overflow mode |
10001 | ||
10002 | if Minimized_Eliminated_Overflow_Check (N) then | |
10003 | Apply_Arithmetic_Overflow_Check (N); | |
10004 | return; | |
10005 | end if; | |
10006 | ||
70482933 | 10007 | if Is_Integer_Type (Etype (N)) then |
a91e9ac7 | 10008 | Apply_Divide_Checks (N); |
b6b5cca8 AC |
10009 | |
10010 | -- All done if we don't have a REM any more, which can happen as a | |
10011 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
10012 | ||
10013 | if Nkind (N) /= N_Op_Rem then | |
10014 | return; | |
10015 | end if; | |
70482933 RK |
10016 | end if; |
10017 | ||
b6b5cca8 AC |
10018 | -- Proceed with expansion of REM |
10019 | ||
10020 | Left := Left_Opnd (N); | |
10021 | Right := Right_Opnd (N); | |
10022 | ||
685094bf | 10023 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
f96fd197 | 10024 | -- but it is useful with other back ends, and is certainly harmless. |
fbf5a39b AC |
10025 | |
10026 | if Is_Integer_Type (Etype (N)) | |
10027 | and then Compile_Time_Known_Value (Right) | |
10028 | and then Expr_Value (Right) = Uint_1 | |
10029 | then | |
abcbd24c ST |
10030 | -- Call Remove_Side_Effects to ensure that any side effects in the |
10031 | -- ignored left operand (in particular function calls to user defined | |
10032 | -- functions) are properly executed. | |
10033 | ||
10034 | Remove_Side_Effects (Left); | |
10035 | ||
fbf5a39b AC |
10036 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
10037 | Analyze_And_Resolve (N, Typ); | |
10038 | return; | |
10039 | end if; | |
10040 | ||
685094bf | 10041 | -- Deal with annoying case of largest negative number remainder minus |
b9daa96e AC |
10042 | -- one. Gigi may not handle this case correctly, because on some |
10043 | -- targets, the mod value is computed using a divide instruction | |
10044 | -- which gives an overflow trap for this case. | |
10045 | ||
10046 | -- It would be a bit more efficient to figure out which targets this | |
10047 | -- is really needed for, but in practice it is reasonable to do the | |
10048 | -- following special check in all cases, since it means we get a clearer | |
10049 | -- message, and also the overhead is minimal given that division is | |
10050 | -- expensive in any case. | |
70482933 | 10051 | |
685094bf RD |
10052 | -- In fact the check is quite easy, if the right operand is -1, then |
10053 | -- the remainder is always 0, and we can just ignore the left operand | |
10054 | -- completely in this case. | |
70482933 | 10055 | |
5d5e9775 AC |
10056 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); |
10057 | Lneg := (not OK) or else Lo < 0; | |
fbf5a39b | 10058 | |
5d5e9775 AC |
10059 | Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True); |
10060 | Rneg := (not OK) or else Lo < 0; | |
fbf5a39b | 10061 | |
5d5e9775 AC |
10062 | -- We won't mess with trying to find out if the left operand can really |
10063 | -- be the largest negative number (that's a pain in the case of private | |
10064 | -- types and this is really marginal). We will just assume that we need | |
10065 | -- the test if the left operand can be negative at all. | |
fbf5a39b | 10066 | |
5d5e9775 | 10067 | if Lneg and Rneg then |
70482933 | 10068 | Rewrite (N, |
9b16cb57 | 10069 | Make_If_Expression (Loc, |
70482933 RK |
10070 | Expressions => New_List ( |
10071 | Make_Op_Eq (Loc, | |
0d901290 | 10072 | Left_Opnd => Duplicate_Subexpr (Right), |
70482933 | 10073 | Right_Opnd => |
0d901290 | 10074 | Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))), |
70482933 | 10075 | |
fbf5a39b AC |
10076 | Unchecked_Convert_To (Typ, |
10077 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
10078 | |
10079 | Relocate_Node (N)))); | |
10080 | ||
10081 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
10082 | Analyze_And_Resolve (N, Typ); | |
10083 | end if; | |
10084 | end Expand_N_Op_Rem; | |
10085 | ||
10086 | ----------------------------- | |
10087 | -- Expand_N_Op_Rotate_Left -- | |
10088 | ----------------------------- | |
10089 | ||
10090 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
10091 | begin | |
10092 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
10093 | |
10094 | -- If we are in Modify_Tree_For_C mode, there is no rotate left in C, | |
10095 | -- so we rewrite in terms of logical shifts | |
10096 | ||
10097 | -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits) | |
10098 | ||
10099 | -- where Bits is the shift count mod Esize (the mod operation here | |
10100 | -- deals with ludicrous large shift counts, which are apparently OK). | |
10101 | ||
a95f708e | 10102 | -- What about nonbinary modulus ??? |
5216b599 AC |
10103 | |
10104 | declare | |
10105 | Loc : constant Source_Ptr := Sloc (N); | |
10106 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
10107 | Typ : constant Entity_Id := Etype (N); | |
10108 | ||
10109 | begin | |
10110 | if Modify_Tree_For_C then | |
10111 | Rewrite (Right_Opnd (N), | |
10112 | Make_Op_Rem (Loc, | |
10113 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
10114 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
10115 | ||
10116 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
10117 | ||
10118 | Rewrite (N, | |
10119 | Make_Op_Or (Loc, | |
10120 | Left_Opnd => | |
10121 | Make_Op_Shift_Left (Loc, | |
10122 | Left_Opnd => Left_Opnd (N), | |
10123 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 10124 | |
5216b599 AC |
10125 | Right_Opnd => |
10126 | Make_Op_Shift_Right (Loc, | |
10127 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
10128 | Right_Opnd => | |
10129 | Make_Op_Subtract (Loc, | |
10130 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
10131 | Right_Opnd => | |
10132 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
10133 | ||
10134 | Analyze_And_Resolve (N, Typ); | |
10135 | end if; | |
10136 | end; | |
70482933 RK |
10137 | end Expand_N_Op_Rotate_Left; |
10138 | ||
10139 | ------------------------------ | |
10140 | -- Expand_N_Op_Rotate_Right -- | |
10141 | ------------------------------ | |
10142 | ||
10143 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
10144 | begin | |
10145 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
10146 | |
10147 | -- If we are in Modify_Tree_For_C mode, there is no rotate right in C, | |
10148 | -- so we rewrite in terms of logical shifts | |
10149 | ||
10150 | -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits) | |
10151 | ||
10152 | -- where Bits is the shift count mod Esize (the mod operation here | |
10153 | -- deals with ludicrous large shift counts, which are apparently OK). | |
10154 | ||
a95f708e | 10155 | -- What about nonbinary modulus ??? |
5216b599 AC |
10156 | |
10157 | declare | |
10158 | Loc : constant Source_Ptr := Sloc (N); | |
10159 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
10160 | Typ : constant Entity_Id := Etype (N); | |
10161 | ||
10162 | begin | |
10163 | Rewrite (Right_Opnd (N), | |
10164 | Make_Op_Rem (Loc, | |
10165 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
10166 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
10167 | ||
10168 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
10169 | ||
10170 | if Modify_Tree_For_C then | |
10171 | Rewrite (N, | |
10172 | Make_Op_Or (Loc, | |
10173 | Left_Opnd => | |
10174 | Make_Op_Shift_Right (Loc, | |
10175 | Left_Opnd => Left_Opnd (N), | |
10176 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 10177 | |
5216b599 AC |
10178 | Right_Opnd => |
10179 | Make_Op_Shift_Left (Loc, | |
10180 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
10181 | Right_Opnd => | |
10182 | Make_Op_Subtract (Loc, | |
10183 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
10184 | Right_Opnd => | |
10185 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
10186 | ||
10187 | Analyze_And_Resolve (N, Typ); | |
10188 | end if; | |
10189 | end; | |
70482933 RK |
10190 | end Expand_N_Op_Rotate_Right; |
10191 | ||
10192 | ---------------------------- | |
10193 | -- Expand_N_Op_Shift_Left -- | |
10194 | ---------------------------- | |
10195 | ||
e09a5598 AC |
10196 | -- Note: nothing in this routine depends on left as opposed to right shifts |
10197 | -- so we share the routine for expanding shift right operations. | |
10198 | ||
70482933 RK |
10199 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is |
10200 | begin | |
10201 | Binary_Op_Validity_Checks (N); | |
e09a5598 AC |
10202 | |
10203 | -- If we are in Modify_Tree_For_C mode, then ensure that the right | |
10204 | -- operand is not greater than the word size (since that would not | |
10205 | -- be defined properly by the corresponding C shift operator). | |
10206 | ||
10207 | if Modify_Tree_For_C then | |
10208 | declare | |
10209 | Right : constant Node_Id := Right_Opnd (N); | |
10210 | Loc : constant Source_Ptr := Sloc (Right); | |
10211 | Typ : constant Entity_Id := Etype (N); | |
10212 | Siz : constant Uint := Esize (Typ); | |
10213 | Orig : Node_Id; | |
10214 | OK : Boolean; | |
10215 | Lo : Uint; | |
10216 | Hi : Uint; | |
10217 | ||
10218 | begin | |
10219 | if Compile_Time_Known_Value (Right) then | |
10220 | if Expr_Value (Right) >= Siz then | |
10221 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
10222 | Analyze_And_Resolve (N, Typ); | |
10223 | end if; | |
10224 | ||
10225 | -- Not compile time known, find range | |
10226 | ||
10227 | else | |
10228 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); | |
10229 | ||
10230 | -- Nothing to do if known to be OK range, otherwise expand | |
10231 | ||
10232 | if not OK or else Hi >= Siz then | |
10233 | ||
10234 | -- Prevent recursion on copy of shift node | |
10235 | ||
10236 | Orig := Relocate_Node (N); | |
10237 | Set_Analyzed (Orig); | |
10238 | ||
10239 | -- Now do the rewrite | |
10240 | ||
10241 | Rewrite (N, | |
10242 | Make_If_Expression (Loc, | |
10243 | Expressions => New_List ( | |
10244 | Make_Op_Ge (Loc, | |
10245 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
10246 | Right_Opnd => Make_Integer_Literal (Loc, Siz)), | |
10247 | Make_Integer_Literal (Loc, 0), | |
10248 | Orig))); | |
10249 | Analyze_And_Resolve (N, Typ); | |
10250 | end if; | |
10251 | end if; | |
10252 | end; | |
10253 | end if; | |
70482933 RK |
10254 | end Expand_N_Op_Shift_Left; |
10255 | ||
10256 | ----------------------------- | |
10257 | -- Expand_N_Op_Shift_Right -- | |
10258 | ----------------------------- | |
10259 | ||
10260 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
10261 | begin | |
e09a5598 AC |
10262 | -- Share shift left circuit |
10263 | ||
10264 | Expand_N_Op_Shift_Left (N); | |
70482933 RK |
10265 | end Expand_N_Op_Shift_Right; |
10266 | ||
10267 | ---------------------------------------- | |
10268 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
10269 | ---------------------------------------- | |
10270 | ||
10271 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
10272 | begin | |
10273 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
10274 | |
10275 | -- If we are in Modify_Tree_For_C mode, there is no shift right | |
10276 | -- arithmetic in C, so we rewrite in terms of logical shifts. | |
10277 | ||
10278 | -- Shift_Right (Num, Bits) or | |
10279 | -- (if Num >= Sign | |
10280 | -- then not (Shift_Right (Mask, bits)) | |
10281 | -- else 0) | |
10282 | ||
10283 | -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1) | |
10284 | ||
10285 | -- Note: in almost all C compilers it would work to just shift a | |
10286 | -- signed integer right, but it's undefined and we cannot rely on it. | |
10287 | ||
e09a5598 AC |
10288 | -- Note: the above works fine for shift counts greater than or equal |
10289 | -- to the word size, since in this case (not (Shift_Right (Mask, bits))) | |
10290 | -- generates all 1'bits. | |
10291 | ||
a95f708e | 10292 | -- What about nonbinary modulus ??? |
5216b599 AC |
10293 | |
10294 | declare | |
10295 | Loc : constant Source_Ptr := Sloc (N); | |
10296 | Typ : constant Entity_Id := Etype (N); | |
10297 | Sign : constant Uint := 2 ** (Esize (Typ) - 1); | |
10298 | Mask : constant Uint := (2 ** Esize (Typ)) - 1; | |
10299 | Left : constant Node_Id := Left_Opnd (N); | |
10300 | Right : constant Node_Id := Right_Opnd (N); | |
10301 | Maskx : Node_Id; | |
10302 | ||
10303 | begin | |
10304 | if Modify_Tree_For_C then | |
10305 | ||
10306 | -- Here if not (Shift_Right (Mask, bits)) can be computed at | |
10307 | -- compile time as a single constant. | |
10308 | ||
10309 | if Compile_Time_Known_Value (Right) then | |
10310 | declare | |
10311 | Val : constant Uint := Expr_Value (Right); | |
10312 | ||
10313 | begin | |
10314 | if Val >= Esize (Typ) then | |
10315 | Maskx := Make_Integer_Literal (Loc, Mask); | |
10316 | ||
10317 | else | |
10318 | Maskx := | |
10319 | Make_Integer_Literal (Loc, | |
10320 | Intval => Mask - (Mask / (2 ** Expr_Value (Right)))); | |
10321 | end if; | |
10322 | end; | |
10323 | ||
10324 | else | |
10325 | Maskx := | |
10326 | Make_Op_Not (Loc, | |
10327 | Right_Opnd => | |
10328 | Make_Op_Shift_Right (Loc, | |
10329 | Left_Opnd => Make_Integer_Literal (Loc, Mask), | |
10330 | Right_Opnd => Duplicate_Subexpr_No_Checks (Right))); | |
10331 | end if; | |
10332 | ||
10333 | -- Now do the rewrite | |
10334 | ||
10335 | Rewrite (N, | |
10336 | Make_Op_Or (Loc, | |
10337 | Left_Opnd => | |
10338 | Make_Op_Shift_Right (Loc, | |
10339 | Left_Opnd => Left, | |
10340 | Right_Opnd => Right), | |
10341 | Right_Opnd => | |
10342 | Make_If_Expression (Loc, | |
10343 | Expressions => New_List ( | |
10344 | Make_Op_Ge (Loc, | |
10345 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
10346 | Right_Opnd => Make_Integer_Literal (Loc, Sign)), | |
10347 | Maskx, | |
10348 | Make_Integer_Literal (Loc, 0))))); | |
10349 | Analyze_And_Resolve (N, Typ); | |
10350 | end if; | |
10351 | end; | |
70482933 RK |
10352 | end Expand_N_Op_Shift_Right_Arithmetic; |
10353 | ||
10354 | -------------------------- | |
10355 | -- Expand_N_Op_Subtract -- | |
10356 | -------------------------- | |
10357 | ||
10358 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
10359 | Typ : constant Entity_Id := Etype (N); | |
10360 | ||
10361 | begin | |
10362 | Binary_Op_Validity_Checks (N); | |
10363 | ||
b6b5cca8 AC |
10364 | -- Check for MINIMIZED/ELIMINATED overflow mode |
10365 | ||
10366 | if Minimized_Eliminated_Overflow_Check (N) then | |
10367 | Apply_Arithmetic_Overflow_Check (N); | |
10368 | return; | |
10369 | end if; | |
10370 | ||
70482933 RK |
10371 | -- N - 0 = N for integer types |
10372 | ||
10373 | if Is_Integer_Type (Typ) | |
10374 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
10375 | and then Expr_Value (Right_Opnd (N)) = 0 | |
10376 | then | |
10377 | Rewrite (N, Left_Opnd (N)); | |
10378 | return; | |
10379 | end if; | |
10380 | ||
8fc789c8 | 10381 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 10382 | |
761f7dcb | 10383 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 | 10384 | Apply_Arithmetic_Overflow_Check (N); |
70482933 | 10385 | end if; |
dfaff97b RD |
10386 | |
10387 | -- Overflow checks for floating-point if -gnateF mode active | |
10388 | ||
10389 | Check_Float_Op_Overflow (N); | |
05dbb83f | 10390 | |
f4ac86dd | 10391 | Expand_Nonbinary_Modular_Op (N); |
70482933 RK |
10392 | end Expand_N_Op_Subtract; |
10393 | ||
10394 | --------------------- | |
10395 | -- Expand_N_Op_Xor -- | |
10396 | --------------------- | |
10397 | ||
10398 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
10399 | Typ : constant Entity_Id := Etype (N); | |
10400 | ||
10401 | begin | |
10402 | Binary_Op_Validity_Checks (N); | |
10403 | ||
10404 | if Is_Array_Type (Etype (N)) then | |
10405 | Expand_Boolean_Operator (N); | |
10406 | ||
10407 | elsif Is_Boolean_Type (Etype (N)) then | |
10408 | Adjust_Condition (Left_Opnd (N)); | |
10409 | Adjust_Condition (Right_Opnd (N)); | |
10410 | Set_Etype (N, Standard_Boolean); | |
10411 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
10412 | |
10413 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
10414 | Expand_Intrinsic_Call (N, Entity (N)); | |
70482933 | 10415 | end if; |
9cd7bc5e ES |
10416 | |
10417 | Expand_Nonbinary_Modular_Op (N); | |
70482933 RK |
10418 | end Expand_N_Op_Xor; |
10419 | ||
10420 | ---------------------- | |
10421 | -- Expand_N_Or_Else -- | |
10422 | ---------------------- | |
10423 | ||
5875f8d6 AC |
10424 | procedure Expand_N_Or_Else (N : Node_Id) |
10425 | renames Expand_Short_Circuit_Operator; | |
70482933 RK |
10426 | |
10427 | ----------------------------------- | |
10428 | -- Expand_N_Qualified_Expression -- | |
10429 | ----------------------------------- | |
10430 | ||
10431 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
10432 | Operand : constant Node_Id := Expression (N); | |
10433 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
10434 | ||
10435 | begin | |
f82944b7 JM |
10436 | -- Do validity check if validity checking operands |
10437 | ||
533369aa | 10438 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
10439 | Ensure_Valid (Operand); |
10440 | end if; | |
10441 | ||
10442 | -- Apply possible constraint check | |
10443 | ||
70482933 | 10444 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
d79e621a GD |
10445 | |
10446 | if Do_Range_Check (Operand) then | |
d79e621a GD |
10447 | Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed); |
10448 | end if; | |
70482933 RK |
10449 | end Expand_N_Qualified_Expression; |
10450 | ||
a961aa79 AC |
10451 | ------------------------------------ |
10452 | -- Expand_N_Quantified_Expression -- | |
10453 | ------------------------------------ | |
10454 | ||
c0f136cd AC |
10455 | -- We expand: |
10456 | ||
10457 | -- for all X in range => Cond | |
a961aa79 | 10458 | |
c0f136cd | 10459 | -- into: |
a961aa79 | 10460 | |
c0f136cd AC |
10461 | -- T := True; |
10462 | -- for X in range loop | |
10463 | -- if not Cond then | |
10464 | -- T := False; | |
10465 | -- exit; | |
10466 | -- end if; | |
10467 | -- end loop; | |
90c63b09 | 10468 | |
36504e5f | 10469 | -- Similarly, an existentially quantified expression: |
90c63b09 | 10470 | |
c0f136cd | 10471 | -- for some X in range => Cond |
90c63b09 | 10472 | |
c0f136cd | 10473 | -- becomes: |
90c63b09 | 10474 | |
c0f136cd AC |
10475 | -- T := False; |
10476 | -- for X in range loop | |
10477 | -- if Cond then | |
10478 | -- T := True; | |
10479 | -- exit; | |
10480 | -- end if; | |
10481 | -- end loop; | |
90c63b09 | 10482 | |
c0f136cd AC |
10483 | -- In both cases, the iteration may be over a container in which case it is |
10484 | -- given by an iterator specification, not a loop parameter specification. | |
a961aa79 | 10485 | |
c0f136cd | 10486 | procedure Expand_N_Quantified_Expression (N : Node_Id) is |
804670f1 AC |
10487 | Actions : constant List_Id := New_List; |
10488 | For_All : constant Boolean := All_Present (N); | |
10489 | Iter_Spec : constant Node_Id := Iterator_Specification (N); | |
10490 | Loc : constant Source_Ptr := Sloc (N); | |
10491 | Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N); | |
10492 | Cond : Node_Id; | |
10493 | Flag : Entity_Id; | |
10494 | Scheme : Node_Id; | |
10495 | Stmts : List_Id; | |
16b9e3c3 | 10496 | Var : Entity_Id; |
c56a9ba4 | 10497 | |
a961aa79 | 10498 | begin |
16b9e3c3 ES |
10499 | -- Ensure that the bound variable is properly frozen. We must do |
10500 | -- this before expansion because the expression is about to be | |
10501 | -- converted into a loop, and resulting freeze nodes may end up | |
10502 | -- in the wrong place in the tree. | |
10503 | ||
10504 | if Present (Iter_Spec) then | |
10505 | Var := Defining_Identifier (Iter_Spec); | |
10506 | else | |
10507 | Var := Defining_Identifier (Loop_Spec); | |
10508 | end if; | |
10509 | ||
10510 | declare | |
10511 | P : Node_Id := Parent (N); | |
10512 | begin | |
10513 | while Nkind (P) in N_Subexpr loop | |
10514 | P := Parent (P); | |
10515 | end loop; | |
10516 | ||
10517 | Freeze_Before (P, Etype (Var)); | |
10518 | end; | |
10519 | ||
804670f1 AC |
10520 | -- Create the declaration of the flag which tracks the status of the |
10521 | -- quantified expression. Generate: | |
011f9d5d | 10522 | |
804670f1 | 10523 | -- Flag : Boolean := (True | False); |
011f9d5d | 10524 | |
804670f1 | 10525 | Flag := Make_Temporary (Loc, 'T', N); |
011f9d5d | 10526 | |
804670f1 | 10527 | Append_To (Actions, |
90c63b09 | 10528 | Make_Object_Declaration (Loc, |
804670f1 | 10529 | Defining_Identifier => Flag, |
c0f136cd AC |
10530 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
10531 | Expression => | |
804670f1 AC |
10532 | New_Occurrence_Of (Boolean_Literals (For_All), Loc))); |
10533 | ||
10534 | -- Construct the circuitry which tracks the status of the quantified | |
10535 | -- expression. Generate: | |
10536 | ||
10537 | -- if [not] Cond then | |
10538 | -- Flag := (False | True); | |
10539 | -- exit; | |
10540 | -- end if; | |
a961aa79 | 10541 | |
c0f136cd | 10542 | Cond := Relocate_Node (Condition (N)); |
a961aa79 | 10543 | |
804670f1 | 10544 | if For_All then |
c0f136cd | 10545 | Cond := Make_Op_Not (Loc, Cond); |
a961aa79 AC |
10546 | end if; |
10547 | ||
804670f1 | 10548 | Stmts := New_List ( |
c0f136cd AC |
10549 | Make_Implicit_If_Statement (N, |
10550 | Condition => Cond, | |
10551 | Then_Statements => New_List ( | |
10552 | Make_Assignment_Statement (Loc, | |
804670f1 | 10553 | Name => New_Occurrence_Of (Flag, Loc), |
c0f136cd | 10554 | Expression => |
804670f1 AC |
10555 | New_Occurrence_Of (Boolean_Literals (not For_All), Loc)), |
10556 | Make_Exit_Statement (Loc)))); | |
10557 | ||
10558 | -- Build the loop equivalent of the quantified expression | |
c0f136cd | 10559 | |
804670f1 AC |
10560 | if Present (Iter_Spec) then |
10561 | Scheme := | |
011f9d5d | 10562 | Make_Iteration_Scheme (Loc, |
804670f1 | 10563 | Iterator_Specification => Iter_Spec); |
c56a9ba4 | 10564 | else |
804670f1 | 10565 | Scheme := |
011f9d5d | 10566 | Make_Iteration_Scheme (Loc, |
804670f1 | 10567 | Loop_Parameter_Specification => Loop_Spec); |
c56a9ba4 AC |
10568 | end if; |
10569 | ||
a961aa79 AC |
10570 | Append_To (Actions, |
10571 | Make_Loop_Statement (Loc, | |
804670f1 AC |
10572 | Iteration_Scheme => Scheme, |
10573 | Statements => Stmts, | |
c0f136cd | 10574 | End_Label => Empty)); |
a961aa79 | 10575 | |
804670f1 AC |
10576 | -- Transform the quantified expression |
10577 | ||
a961aa79 AC |
10578 | Rewrite (N, |
10579 | Make_Expression_With_Actions (Loc, | |
804670f1 | 10580 | Expression => New_Occurrence_Of (Flag, Loc), |
a961aa79 | 10581 | Actions => Actions)); |
a961aa79 AC |
10582 | Analyze_And_Resolve (N, Standard_Boolean); |
10583 | end Expand_N_Quantified_Expression; | |
10584 | ||
70482933 RK |
10585 | --------------------------------- |
10586 | -- Expand_N_Selected_Component -- | |
10587 | --------------------------------- | |
10588 | ||
70482933 RK |
10589 | procedure Expand_N_Selected_Component (N : Node_Id) is |
10590 | Loc : constant Source_Ptr := Sloc (N); | |
10591 | Par : constant Node_Id := Parent (N); | |
10592 | P : constant Node_Id := Prefix (N); | |
03eb6036 | 10593 | S : constant Node_Id := Selector_Name (N); |
fbf5a39b | 10594 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 10595 | Disc : Entity_Id; |
70482933 | 10596 | New_N : Node_Id; |
fbf5a39b | 10597 | Dcon : Elmt_Id; |
d606f1df | 10598 | Dval : Node_Id; |
70482933 RK |
10599 | |
10600 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
10601 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
10602 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
10603 | -- Don't we have a general routine that does this??? |
10604 | ||
53f29d4f AC |
10605 | function Is_Subtype_Declaration return Boolean; |
10606 | -- The replacement of a discriminant reference by its value is required | |
4317e442 AC |
10607 | -- if this is part of the initialization of an temporary generated by a |
10608 | -- change of representation. This shows up as the construction of a | |
53f29d4f | 10609 | -- discriminant constraint for a subtype declared at the same point as |
4317e442 AC |
10610 | -- the entity in the prefix of the selected component. We recognize this |
10611 | -- case when the context of the reference is: | |
10612 | -- subtype ST is T(Obj.D); | |
10613 | -- where the entity for Obj comes from source, and ST has the same sloc. | |
53f29d4f | 10614 | |
fbf5a39b AC |
10615 | ----------------------- |
10616 | -- In_Left_Hand_Side -- | |
10617 | ----------------------- | |
70482933 RK |
10618 | |
10619 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
10620 | begin | |
fbf5a39b | 10621 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
90c63b09 | 10622 | and then Comp = Name (Parent (Comp))) |
fbf5a39b | 10623 | or else (Present (Parent (Comp)) |
90c63b09 AC |
10624 | and then Nkind (Parent (Comp)) in N_Subexpr |
10625 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
10626 | end In_Left_Hand_Side; |
10627 | ||
53f29d4f AC |
10628 | ----------------------------- |
10629 | -- Is_Subtype_Declaration -- | |
10630 | ----------------------------- | |
10631 | ||
10632 | function Is_Subtype_Declaration return Boolean is | |
10633 | Par : constant Node_Id := Parent (N); | |
53f29d4f AC |
10634 | begin |
10635 | return | |
10636 | Nkind (Par) = N_Index_Or_Discriminant_Constraint | |
10637 | and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration | |
10638 | and then Comes_From_Source (Entity (Prefix (N))) | |
10639 | and then Sloc (Par) = Sloc (Entity (Prefix (N))); | |
10640 | end Is_Subtype_Declaration; | |
10641 | ||
fbf5a39b AC |
10642 | -- Start of processing for Expand_N_Selected_Component |
10643 | ||
70482933 | 10644 | begin |
fbf5a39b AC |
10645 | -- Insert explicit dereference if required |
10646 | ||
10647 | if Is_Access_Type (Ptyp) then | |
702d2020 AC |
10648 | |
10649 | -- First set prefix type to proper access type, in case it currently | |
10650 | -- has a private (non-access) view of this type. | |
10651 | ||
10652 | Set_Etype (P, Ptyp); | |
10653 | ||
fbf5a39b | 10654 | Insert_Explicit_Dereference (P); |
e6f69614 | 10655 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b | 10656 | |
fbf5a39b AC |
10657 | Ptyp := Etype (P); |
10658 | end if; | |
10659 | ||
10660 | -- Deal with discriminant check required | |
10661 | ||
70482933 | 10662 | if Do_Discriminant_Check (N) then |
03eb6036 AC |
10663 | if Present (Discriminant_Checking_Func |
10664 | (Original_Record_Component (Entity (S)))) | |
10665 | then | |
10666 | -- Present the discriminant checking function to the backend, so | |
10667 | -- that it can inline the call to the function. | |
10668 | ||
10669 | Add_Inlined_Body | |
10670 | (Discriminant_Checking_Func | |
cf27c5a2 EB |
10671 | (Original_Record_Component (Entity (S))), |
10672 | N); | |
70482933 | 10673 | |
03eb6036 | 10674 | -- Now reset the flag and generate the call |
70482933 | 10675 | |
03eb6036 AC |
10676 | Set_Do_Discriminant_Check (N, False); |
10677 | Generate_Discriminant_Check (N); | |
70482933 | 10678 | |
03eb6036 AC |
10679 | -- In the case of Unchecked_Union, no discriminant checking is |
10680 | -- actually performed. | |
70482933 | 10681 | |
03eb6036 AC |
10682 | else |
10683 | Set_Do_Discriminant_Check (N, False); | |
10684 | end if; | |
70482933 RK |
10685 | end if; |
10686 | ||
b4592168 GD |
10687 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
10688 | -- function, then additional actuals must be passed. | |
10689 | ||
d4dfb005 | 10690 | if Is_Build_In_Place_Function_Call (P) then |
b4592168 | 10691 | Make_Build_In_Place_Call_In_Anonymous_Context (P); |
4ac62786 AC |
10692 | |
10693 | -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix | |
10694 | -- containing build-in-place function calls whose returned object covers | |
10695 | -- interface types. | |
10696 | ||
d4dfb005 | 10697 | elsif Present (Unqual_BIP_Iface_Function_Call (P)) then |
4ac62786 | 10698 | Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P); |
b4592168 GD |
10699 | end if; |
10700 | ||
fbf5a39b AC |
10701 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
10702 | -- selected component with discriminants. This must be checked during | |
10703 | -- expansion, because during analysis the type of the selector is not | |
10704 | -- known at the point the prefix is analyzed. If the conversion is the | |
10705 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
10706 | |
10707 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
10708 | and then Has_Discriminants (Etype (N)) | |
10709 | and then not In_Left_Hand_Side (N) | |
10710 | then | |
10711 | Force_Evaluation (Prefix (N)); | |
10712 | end if; | |
10713 | ||
10714 | -- Remaining processing applies only if selector is a discriminant | |
10715 | ||
10716 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
10717 | ||
10718 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
10719 | -- we may be able to rewrite the expression with the actual value |
10720 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
10721 | |
10722 | if Is_Record_Type (Ptyp) | |
10723 | and then Has_Discriminants (Ptyp) | |
10724 | and then Is_Constrained (Ptyp) | |
70482933 | 10725 | then |
fbf5a39b | 10726 | -- Do this optimization for discrete types only, and not for |
a90bd866 | 10727 | -- access types (access discriminants get us into trouble). |
70482933 | 10728 | |
fbf5a39b AC |
10729 | if not Is_Discrete_Type (Etype (N)) then |
10730 | null; | |
10731 | ||
356ffab8 | 10732 | -- Don't do this on the left-hand side of an assignment statement. |
0d901290 AC |
10733 | -- Normally one would think that references like this would not |
10734 | -- occur, but they do in generated code, and mean that we really | |
a90bd866 | 10735 | -- do want to assign the discriminant. |
fbf5a39b AC |
10736 | |
10737 | elsif Nkind (Par) = N_Assignment_Statement | |
10738 | and then Name (Par) = N | |
10739 | then | |
10740 | null; | |
10741 | ||
685094bf | 10742 | -- Don't do this optimization for the prefix of an attribute or |
e2534738 | 10743 | -- the name of an object renaming declaration since these are |
685094bf | 10744 | -- contexts where we do not want the value anyway. |
fbf5a39b AC |
10745 | |
10746 | elsif (Nkind (Par) = N_Attribute_Reference | |
533369aa | 10747 | and then Prefix (Par) = N) |
fbf5a39b AC |
10748 | or else Is_Renamed_Object (N) |
10749 | then | |
10750 | null; | |
10751 | ||
10752 | -- Don't do this optimization if we are within the code for a | |
10753 | -- discriminant check, since the whole point of such a check may | |
a90bd866 | 10754 | -- be to verify the condition on which the code below depends. |
fbf5a39b AC |
10755 | |
10756 | elsif Is_In_Discriminant_Check (N) then | |
10757 | null; | |
10758 | ||
10759 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
10760 | -- still one condition that inhibits the optimization below but |
10761 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
10762 | |
10763 | else | |
685094bf RD |
10764 | -- Loop through discriminants to find the matching discriminant |
10765 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
10766 | |
10767 | Disc := First_Discriminant (Ptyp); | |
10768 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
10769 | Discr_Loop : while Present (Dcon) loop | |
d606f1df | 10770 | Dval := Node (Dcon); |
fbf5a39b | 10771 | |
bd949ee2 RD |
10772 | -- Check if this is the matching discriminant and if the |
10773 | -- discriminant value is simple enough to make sense to | |
10774 | -- copy. We don't want to copy complex expressions, and | |
10775 | -- indeed to do so can cause trouble (before we put in | |
10776 | -- this guard, a discriminant expression containing an | |
e7d897b8 | 10777 | -- AND THEN was copied, causing problems for coverage |
c228a069 | 10778 | -- analysis tools). |
bd949ee2 | 10779 | |
53f29d4f AC |
10780 | -- However, if the reference is part of the initialization |
10781 | -- code generated for an object declaration, we must use | |
10782 | -- the discriminant value from the subtype constraint, | |
10783 | -- because the selected component may be a reference to the | |
10784 | -- object being initialized, whose discriminant is not yet | |
10785 | -- set. This only happens in complex cases involving changes | |
10786 | -- or representation. | |
10787 | ||
bd949ee2 RD |
10788 | if Disc = Entity (Selector_Name (N)) |
10789 | and then (Is_Entity_Name (Dval) | |
170b2989 AC |
10790 | or else Compile_Time_Known_Value (Dval) |
10791 | or else Is_Subtype_Declaration) | |
bd949ee2 | 10792 | then |
fbf5a39b AC |
10793 | -- Here we have the matching discriminant. Check for |
10794 | -- the case of a discriminant of a component that is | |
10795 | -- constrained by an outer discriminant, which cannot | |
10796 | -- be optimized away. | |
10797 | ||
d606f1df AC |
10798 | if Denotes_Discriminant |
10799 | (Dval, Check_Concurrent => True) | |
10800 | then | |
10801 | exit Discr_Loop; | |
10802 | ||
10803 | elsif Nkind (Original_Node (Dval)) = N_Selected_Component | |
10804 | and then | |
10805 | Denotes_Discriminant | |
10806 | (Selector_Name (Original_Node (Dval)), True) | |
10807 | then | |
10808 | exit Discr_Loop; | |
10809 | ||
10810 | -- Do not retrieve value if constraint is not static. It | |
10811 | -- is generally not useful, and the constraint may be a | |
10812 | -- rewritten outer discriminant in which case it is in | |
10813 | -- fact incorrect. | |
10814 | ||
10815 | elsif Is_Entity_Name (Dval) | |
d606f1df | 10816 | and then |
533369aa AC |
10817 | Nkind (Parent (Entity (Dval))) = N_Object_Declaration |
10818 | and then Present (Expression (Parent (Entity (Dval)))) | |
10819 | and then not | |
edab6088 | 10820 | Is_OK_Static_Expression |
d606f1df | 10821 | (Expression (Parent (Entity (Dval)))) |
fbf5a39b AC |
10822 | then |
10823 | exit Discr_Loop; | |
70482933 | 10824 | |
685094bf RD |
10825 | -- In the context of a case statement, the expression may |
10826 | -- have the base type of the discriminant, and we need to | |
10827 | -- preserve the constraint to avoid spurious errors on | |
10828 | -- missing cases. | |
70482933 | 10829 | |
fbf5a39b | 10830 | elsif Nkind (Parent (N)) = N_Case_Statement |
d606f1df | 10831 | and then Etype (Dval) /= Etype (Disc) |
70482933 RK |
10832 | then |
10833 | Rewrite (N, | |
10834 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
10835 | Subtype_Mark => |
10836 | New_Occurrence_Of (Etype (Disc), Loc), | |
10837 | Expression => | |
d606f1df | 10838 | New_Copy_Tree (Dval))); |
ffe9aba8 | 10839 | Analyze_And_Resolve (N, Etype (Disc)); |
fbf5a39b AC |
10840 | |
10841 | -- In case that comes out as a static expression, | |
10842 | -- reset it (a selected component is never static). | |
10843 | ||
10844 | Set_Is_Static_Expression (N, False); | |
10845 | return; | |
10846 | ||
10847 | -- Otherwise we can just copy the constraint, but the | |
a90bd866 | 10848 | -- result is certainly not static. In some cases the |
ffe9aba8 AC |
10849 | -- discriminant constraint has been analyzed in the |
10850 | -- context of the original subtype indication, but for | |
10851 | -- itypes the constraint might not have been analyzed | |
10852 | -- yet, and this must be done now. | |
fbf5a39b | 10853 | |
70482933 | 10854 | else |
d606f1df | 10855 | Rewrite (N, New_Copy_Tree (Dval)); |
ffe9aba8 | 10856 | Analyze_And_Resolve (N); |
fbf5a39b AC |
10857 | Set_Is_Static_Expression (N, False); |
10858 | return; | |
70482933 | 10859 | end if; |
70482933 RK |
10860 | end if; |
10861 | ||
fbf5a39b AC |
10862 | Next_Elmt (Dcon); |
10863 | Next_Discriminant (Disc); | |
10864 | end loop Discr_Loop; | |
70482933 | 10865 | |
fbf5a39b AC |
10866 | -- Note: the above loop should always find a matching |
10867 | -- discriminant, but if it does not, we just missed an | |
c228a069 AC |
10868 | -- optimization due to some glitch (perhaps a previous |
10869 | -- error), so ignore. | |
fbf5a39b AC |
10870 | |
10871 | end if; | |
70482933 RK |
10872 | end if; |
10873 | ||
10874 | -- The only remaining processing is in the case of a discriminant of | |
10875 | -- a concurrent object, where we rewrite the prefix to denote the | |
10876 | -- corresponding record type. If the type is derived and has renamed | |
10877 | -- discriminants, use corresponding discriminant, which is the one | |
10878 | -- that appears in the corresponding record. | |
10879 | ||
10880 | if not Is_Concurrent_Type (Ptyp) then | |
10881 | return; | |
10882 | end if; | |
10883 | ||
10884 | Disc := Entity (Selector_Name (N)); | |
10885 | ||
10886 | if Is_Derived_Type (Ptyp) | |
10887 | and then Present (Corresponding_Discriminant (Disc)) | |
10888 | then | |
10889 | Disc := Corresponding_Discriminant (Disc); | |
10890 | end if; | |
10891 | ||
10892 | New_N := | |
10893 | Make_Selected_Component (Loc, | |
10894 | Prefix => | |
10895 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
10896 | New_Copy_Tree (P)), | |
10897 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
10898 | ||
10899 | Rewrite (N, New_N); | |
10900 | Analyze (N); | |
10901 | end if; | |
5972791c | 10902 | |
73fe1679 | 10903 | -- Set Atomic_Sync_Required if necessary for atomic component |
5972791c | 10904 | |
73fe1679 AC |
10905 | if Nkind (N) = N_Selected_Component then |
10906 | declare | |
10907 | E : constant Entity_Id := Entity (Selector_Name (N)); | |
10908 | Set : Boolean; | |
10909 | ||
10910 | begin | |
10911 | -- If component is atomic, but type is not, setting depends on | |
10912 | -- disable/enable state for the component. | |
10913 | ||
10914 | if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then | |
10915 | Set := not Atomic_Synchronization_Disabled (E); | |
10916 | ||
10917 | -- If component is not atomic, but its type is atomic, setting | |
10918 | -- depends on disable/enable state for the type. | |
10919 | ||
10920 | elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
10921 | Set := not Atomic_Synchronization_Disabled (Etype (E)); | |
10922 | ||
10923 | -- If both component and type are atomic, we disable if either | |
10924 | -- component or its type have sync disabled. | |
10925 | ||
10926 | elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
10927 | Set := (not Atomic_Synchronization_Disabled (E)) | |
10928 | and then | |
10929 | (not Atomic_Synchronization_Disabled (Etype (E))); | |
10930 | ||
10931 | else | |
10932 | Set := False; | |
10933 | end if; | |
10934 | ||
10935 | -- Set flag if required | |
10936 | ||
10937 | if Set then | |
10938 | Activate_Atomic_Synchronization (N); | |
10939 | end if; | |
10940 | end; | |
5972791c | 10941 | end if; |
70482933 RK |
10942 | end Expand_N_Selected_Component; |
10943 | ||
10944 | -------------------- | |
10945 | -- Expand_N_Slice -- | |
10946 | -------------------- | |
10947 | ||
10948 | procedure Expand_N_Slice (N : Node_Id) is | |
5ff90f08 AC |
10949 | Loc : constant Source_Ptr := Sloc (N); |
10950 | Typ : constant Entity_Id := Etype (N); | |
fbf5a39b | 10951 | |
81a5b587 | 10952 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
10953 | -- Check whether the argument is an actual for a procedure call, in |
10954 | -- which case the expansion of a bit-packed slice is deferred until the | |
10955 | -- call itself is expanded. The reason this is required is that we might | |
10956 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
10957 | -- that copy out would be missed if we created a temporary here in | |
10958 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
10959 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
10960 | -- is harmless to defer expansion in the IN case, since the call | |
10961 | -- processing will still generate the appropriate copy in operation, | |
10962 | -- which will take care of the slice. | |
81a5b587 | 10963 | |
b01bf852 | 10964 | procedure Make_Temporary_For_Slice; |
685094bf | 10965 | -- Create a named variable for the value of the slice, in cases where |
c7a494c9 | 10966 | -- the back end cannot handle it properly, e.g. when packed types or |
685094bf | 10967 | -- unaligned slices are involved. |
fbf5a39b | 10968 | |
81a5b587 AC |
10969 | ------------------------- |
10970 | -- Is_Procedure_Actual -- | |
10971 | ------------------------- | |
10972 | ||
10973 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
10974 | Par : Node_Id := Parent (N); | |
08aa9a4a | 10975 | |
81a5b587 | 10976 | begin |
81a5b587 | 10977 | loop |
c6a60aa1 RD |
10978 | -- If our parent is a procedure call we can return |
10979 | ||
81a5b587 AC |
10980 | if Nkind (Par) = N_Procedure_Call_Statement then |
10981 | return True; | |
6b6fcd3e | 10982 | |
685094bf RD |
10983 | -- If our parent is a type conversion, keep climbing the tree, |
10984 | -- since a type conversion can be a procedure actual. Also keep | |
10985 | -- climbing if parameter association or a qualified expression, | |
10986 | -- since these are additional cases that do can appear on | |
10987 | -- procedure actuals. | |
6b6fcd3e | 10988 | |
303b4d58 AC |
10989 | elsif Nkind_In (Par, N_Type_Conversion, |
10990 | N_Parameter_Association, | |
10991 | N_Qualified_Expression) | |
c6a60aa1 | 10992 | then |
81a5b587 | 10993 | Par := Parent (Par); |
c6a60aa1 RD |
10994 | |
10995 | -- Any other case is not what we are looking for | |
10996 | ||
10997 | else | |
10998 | return False; | |
81a5b587 AC |
10999 | end if; |
11000 | end loop; | |
81a5b587 AC |
11001 | end Is_Procedure_Actual; |
11002 | ||
b01bf852 AC |
11003 | ------------------------------ |
11004 | -- Make_Temporary_For_Slice -- | |
11005 | ------------------------------ | |
fbf5a39b | 11006 | |
b01bf852 | 11007 | procedure Make_Temporary_For_Slice is |
b01bf852 | 11008 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
5ff90f08 | 11009 | Decl : Node_Id; |
13d923cc | 11010 | |
fbf5a39b AC |
11011 | begin |
11012 | Decl := | |
11013 | Make_Object_Declaration (Loc, | |
11014 | Defining_Identifier => Ent, | |
11015 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
11016 | ||
11017 | Set_No_Initialization (Decl); | |
11018 | ||
11019 | Insert_Actions (N, New_List ( | |
11020 | Decl, | |
11021 | Make_Assignment_Statement (Loc, | |
5ff90f08 | 11022 | Name => New_Occurrence_Of (Ent, Loc), |
fbf5a39b AC |
11023 | Expression => Relocate_Node (N)))); |
11024 | ||
11025 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
11026 | Analyze_And_Resolve (N, Typ); | |
b01bf852 | 11027 | end Make_Temporary_For_Slice; |
fbf5a39b | 11028 | |
5ff90f08 AC |
11029 | -- Local variables |
11030 | ||
800da977 AC |
11031 | Pref : constant Node_Id := Prefix (N); |
11032 | Pref_Typ : Entity_Id := Etype (Pref); | |
5ff90f08 | 11033 | |
fbf5a39b | 11034 | -- Start of processing for Expand_N_Slice |
70482933 RK |
11035 | |
11036 | begin | |
11037 | -- Special handling for access types | |
11038 | ||
5ff90f08 AC |
11039 | if Is_Access_Type (Pref_Typ) then |
11040 | Pref_Typ := Designated_Type (Pref_Typ); | |
70482933 | 11041 | |
5ff90f08 | 11042 | Rewrite (Pref, |
e6f69614 | 11043 | Make_Explicit_Dereference (Sloc (N), |
5ff90f08 | 11044 | Prefix => Relocate_Node (Pref))); |
70482933 | 11045 | |
5ff90f08 | 11046 | Analyze_And_Resolve (Pref, Pref_Typ); |
70482933 RK |
11047 | end if; |
11048 | ||
b4592168 GD |
11049 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
11050 | -- function, then additional actuals must be passed. | |
11051 | ||
d4dfb005 | 11052 | if Is_Build_In_Place_Function_Call (Pref) then |
5ff90f08 | 11053 | Make_Build_In_Place_Call_In_Anonymous_Context (Pref); |
4ac62786 AC |
11054 | |
11055 | -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix | |
11056 | -- containing build-in-place function calls whose returned object covers | |
11057 | -- interface types. | |
11058 | ||
d4dfb005 | 11059 | elsif Present (Unqual_BIP_Iface_Function_Call (Pref)) then |
4ac62786 | 11060 | Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref); |
b4592168 GD |
11061 | end if; |
11062 | ||
70482933 RK |
11063 | -- The remaining case to be handled is packed slices. We can leave |
11064 | -- packed slices as they are in the following situations: | |
11065 | ||
11066 | -- 1. Right or left side of an assignment (we can handle this | |
11067 | -- situation correctly in the assignment statement expansion). | |
11068 | ||
685094bf RD |
11069 | -- 2. Prefix of indexed component (the slide is optimized away in this |
11070 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 11071 | |
685094bf RD |
11072 | -- 3. Object renaming declaration, since we want the name of the |
11073 | -- slice, not the value. | |
70482933 | 11074 | |
685094bf RD |
11075 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
11076 | -- be required, and this is handled in the expansion of call | |
11077 | -- itself. | |
70482933 | 11078 | |
685094bf RD |
11079 | -- 5. Prefix of an address attribute (this is an error which is caught |
11080 | -- elsewhere, and the expansion would interfere with generating the | |
955379e4 EB |
11081 | -- error message) or of a size attribute (because 'Size may change |
11082 | -- when applied to the temporary instead of the slice directly). | |
70482933 | 11083 | |
81a5b587 | 11084 | if not Is_Packed (Typ) then |
08aa9a4a | 11085 | |
685094bf RD |
11086 | -- Apply transformation for actuals of a function call, where |
11087 | -- Expand_Actuals is not used. | |
81a5b587 AC |
11088 | |
11089 | if Nkind (Parent (N)) = N_Function_Call | |
11090 | and then Is_Possibly_Unaligned_Slice (N) | |
11091 | then | |
b01bf852 | 11092 | Make_Temporary_For_Slice; |
81a5b587 AC |
11093 | end if; |
11094 | ||
11095 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
11096 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
533369aa | 11097 | and then Parent (N) = Name (Parent (Parent (N)))) |
70482933 | 11098 | then |
81a5b587 | 11099 | return; |
70482933 | 11100 | |
81a5b587 AC |
11101 | elsif Nkind (Parent (N)) = N_Indexed_Component |
11102 | or else Is_Renamed_Object (N) | |
11103 | or else Is_Procedure_Actual (N) | |
11104 | then | |
11105 | return; | |
70482933 | 11106 | |
91b1417d | 11107 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
955379e4 EB |
11108 | and then (Attribute_Name (Parent (N)) = Name_Address |
11109 | or else Attribute_Name (Parent (N)) = Name_Size) | |
fbf5a39b | 11110 | then |
81a5b587 AC |
11111 | return; |
11112 | ||
11113 | else | |
b01bf852 | 11114 | Make_Temporary_For_Slice; |
70482933 RK |
11115 | end if; |
11116 | end Expand_N_Slice; | |
11117 | ||
11118 | ------------------------------ | |
11119 | -- Expand_N_Type_Conversion -- | |
11120 | ------------------------------ | |
11121 | ||
11122 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
11123 | Loc : constant Source_Ptr := Sloc (N); | |
11124 | Operand : constant Node_Id := Expression (N); | |
1b2f53bb | 11125 | Operand_Acc : Node_Id := Operand; |
8113b0c7 | 11126 | Target_Type : Entity_Id := Etype (N); |
70482933 RK |
11127 | Operand_Type : Entity_Id := Etype (Operand); |
11128 | ||
8113b0c7 EB |
11129 | procedure Discrete_Range_Check; |
11130 | -- Handles generation of range check for discrete target value | |
11131 | ||
70482933 | 11132 | procedure Handle_Changed_Representation; |
685094bf RD |
11133 | -- This is called in the case of record and array type conversions to |
11134 | -- see if there is a change of representation to be handled. Change of | |
11135 | -- representation is actually handled at the assignment statement level, | |
11136 | -- and what this procedure does is rewrite node N conversion as an | |
11137 | -- assignment to temporary. If there is no change of representation, | |
11138 | -- then the conversion node is unchanged. | |
70482933 | 11139 | |
426908f8 RD |
11140 | procedure Raise_Accessibility_Error; |
11141 | -- Called when we know that an accessibility check will fail. Rewrites | |
11142 | -- node N to an appropriate raise statement and outputs warning msgs. | |
91669e7e AC |
11143 | -- The Etype of the raise node is set to Target_Type. Note that in this |
11144 | -- case the rest of the processing should be skipped (i.e. the call to | |
11145 | -- this procedure will be followed by "goto Done"). | |
426908f8 | 11146 | |
70482933 RK |
11147 | procedure Real_Range_Check; |
11148 | -- Handles generation of range check for real target value | |
11149 | ||
d15f9422 AC |
11150 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean; |
11151 | -- True iff Present (Effective_Extra_Accessibility (Id)) successfully | |
11152 | -- evaluates to True. | |
11153 | ||
8113b0c7 EB |
11154 | -------------------------- |
11155 | -- Discrete_Range_Check -- | |
11156 | -------------------------- | |
11157 | ||
43eb2bb6 EB |
11158 | -- Case of conversions to a discrete type. We let Generate_Range_Check |
11159 | -- do the heavy lifting, after converting a fixed-point operand to an | |
11160 | -- appropriate integer type. | |
8113b0c7 EB |
11161 | |
11162 | procedure Discrete_Range_Check is | |
11163 | Expr : Node_Id; | |
11164 | Ityp : Entity_Id; | |
11165 | ||
11166 | begin | |
11167 | -- Nothing to do if conversion was rewritten | |
11168 | ||
11169 | if Nkind (N) /= N_Type_Conversion then | |
11170 | return; | |
11171 | end if; | |
11172 | ||
11173 | Expr := Expression (N); | |
11174 | ||
43eb2bb6 EB |
11175 | -- Nothing to do if range checks suppressed |
11176 | ||
11177 | if Range_Checks_Suppressed (Target_Type) then | |
11178 | return; | |
11179 | end if; | |
11180 | ||
11181 | -- Nothing to do if expression is an entity on which checks have been | |
11182 | -- suppressed. | |
11183 | ||
11184 | if Is_Entity_Name (Expr) | |
11185 | and then Range_Checks_Suppressed (Entity (Expr)) | |
11186 | then | |
11187 | return; | |
11188 | end if; | |
11189 | ||
8113b0c7 EB |
11190 | -- Before we do a range check, we have to deal with treating |
11191 | -- a fixed-point operand as an integer. The way we do this | |
11192 | -- is simply to do an unchecked conversion to an appropriate | |
11193 | -- integer type large enough to hold the result. | |
11194 | ||
11195 | if Is_Fixed_Point_Type (Etype (Expr)) then | |
11196 | if Esize (Base_Type (Etype (Expr))) > Esize (Standard_Integer) then | |
11197 | Ityp := Standard_Long_Long_Integer; | |
11198 | else | |
11199 | Ityp := Standard_Integer; | |
11200 | end if; | |
11201 | ||
11202 | Set_Do_Range_Check (Expr, False); | |
11203 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); | |
11204 | end if; | |
11205 | ||
a7191e01 EB |
11206 | -- Reset overflow flag, since the range check will include |
11207 | -- dealing with possible overflow, and generate the check. | |
11208 | ||
11209 | Set_Do_Overflow_Check (N, False); | |
11210 | ||
8113b0c7 EB |
11211 | Generate_Range_Check (Expr, Target_Type, CE_Range_Check_Failed); |
11212 | end Discrete_Range_Check; | |
11213 | ||
70482933 RK |
11214 | ----------------------------------- |
11215 | -- Handle_Changed_Representation -- | |
11216 | ----------------------------------- | |
11217 | ||
11218 | procedure Handle_Changed_Representation is | |
11219 | Temp : Entity_Id; | |
11220 | Decl : Node_Id; | |
11221 | Odef : Node_Id; | |
70482933 RK |
11222 | N_Ix : Node_Id; |
11223 | Cons : List_Id; | |
11224 | ||
11225 | begin | |
f82944b7 | 11226 | -- Nothing else to do if no change of representation |
70482933 RK |
11227 | |
11228 | if Same_Representation (Operand_Type, Target_Type) then | |
11229 | return; | |
11230 | ||
11231 | -- The real change of representation work is done by the assignment | |
11232 | -- statement processing. So if this type conversion is appearing as | |
11233 | -- the expression of an assignment statement, nothing needs to be | |
11234 | -- done to the conversion. | |
11235 | ||
11236 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
11237 | return; | |
11238 | ||
11239 | -- Otherwise we need to generate a temporary variable, and do the | |
11240 | -- change of representation assignment into that temporary variable. | |
11241 | -- The conversion is then replaced by a reference to this variable. | |
11242 | ||
11243 | else | |
11244 | Cons := No_List; | |
11245 | ||
685094bf | 11246 | -- If type is unconstrained we have to add a constraint, copied |
356ffab8 | 11247 | -- from the actual value of the left-hand side. |
70482933 RK |
11248 | |
11249 | if not Is_Constrained (Target_Type) then | |
11250 | if Has_Discriminants (Operand_Type) then | |
fbf5a39b | 11251 | |
7c15c6dd AC |
11252 | -- A change of representation can only apply to untagged |
11253 | -- types. We need to build the constraint that applies to | |
11254 | -- the target type, using the constraints of the operand. | |
11255 | -- The analysis is complicated if there are both inherited | |
11256 | -- discriminants and constrained discriminants. | |
11257 | -- We iterate over the discriminants of the target, and | |
11258 | -- find the discriminant of the same name: | |
fbf5a39b | 11259 | |
7c15c6dd AC |
11260 | -- a) If there is a corresponding discriminant in the object |
11261 | -- then the value is a selected component of the operand. | |
11262 | ||
11263 | -- b) Otherwise the value of a constrained discriminant is | |
11264 | -- found in the stored constraint of the operand. | |
11265 | ||
11266 | declare | |
11267 | Stored : constant Elist_Id := | |
a4f4dbdb | 11268 | Stored_Constraint (Operand_Type); |
7c15c6dd AC |
11269 | |
11270 | Elmt : Elmt_Id; | |
11271 | ||
11272 | Disc_O : Entity_Id; | |
11273 | -- Discriminant of the operand type. Its value in the | |
a4f4dbdb | 11274 | -- object is captured in a selected component. |
7c15c6dd AC |
11275 | |
11276 | Disc_S : Entity_Id; | |
11277 | -- Stored discriminant of the operand. If present, it | |
11278 | -- corresponds to a constrained discriminant of the | |
11279 | -- parent type. | |
11280 | ||
11281 | Disc_T : Entity_Id; | |
11282 | -- Discriminant of the target type | |
11283 | ||
11284 | begin | |
11285 | Disc_T := First_Discriminant (Target_Type); | |
11286 | Disc_O := First_Discriminant (Operand_Type); | |
11287 | Disc_S := First_Stored_Discriminant (Operand_Type); | |
11288 | ||
11289 | if Present (Stored) then | |
11290 | Elmt := First_Elmt (Stored); | |
5612989e PMR |
11291 | else |
11292 | Elmt := No_Elmt; -- init to avoid warning | |
7c15c6dd AC |
11293 | end if; |
11294 | ||
11295 | Cons := New_List; | |
11296 | while Present (Disc_T) loop | |
11297 | if Present (Disc_O) | |
11298 | and then Chars (Disc_T) = Chars (Disc_O) | |
11299 | then | |
11300 | Append_To (Cons, | |
11301 | Make_Selected_Component (Loc, | |
11302 | Prefix => | |
11303 | Duplicate_Subexpr_Move_Checks (Operand), | |
a4f4dbdb | 11304 | Selector_Name => |
7c15c6dd AC |
11305 | Make_Identifier (Loc, Chars (Disc_O)))); |
11306 | Next_Discriminant (Disc_O); | |
11307 | ||
11308 | elsif Present (Disc_S) then | |
11309 | Append_To (Cons, New_Copy_Tree (Node (Elmt))); | |
11310 | Next_Elmt (Elmt); | |
11311 | end if; | |
11312 | ||
11313 | Next_Discriminant (Disc_T); | |
11314 | end loop; | |
11315 | end; | |
70482933 RK |
11316 | |
11317 | elsif Is_Array_Type (Operand_Type) then | |
11318 | N_Ix := First_Index (Target_Type); | |
11319 | Cons := New_List; | |
11320 | ||
11321 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
11322 | ||
11323 | -- We convert the bounds explicitly. We use an unchecked | |
11324 | -- conversion because bounds checks are done elsewhere. | |
11325 | ||
11326 | Append_To (Cons, | |
11327 | Make_Range (Loc, | |
a4f4dbdb | 11328 | Low_Bound => |
70482933 RK |
11329 | Unchecked_Convert_To (Etype (N_Ix), |
11330 | Make_Attribute_Reference (Loc, | |
a4f4dbdb | 11331 | Prefix => |
fbf5a39b | 11332 | Duplicate_Subexpr_No_Checks |
70482933 RK |
11333 | (Operand, Name_Req => True), |
11334 | Attribute_Name => Name_First, | |
11335 | Expressions => New_List ( | |
11336 | Make_Integer_Literal (Loc, J)))), | |
11337 | ||
11338 | High_Bound => | |
11339 | Unchecked_Convert_To (Etype (N_Ix), | |
11340 | Make_Attribute_Reference (Loc, | |
a4f4dbdb | 11341 | Prefix => |
fbf5a39b | 11342 | Duplicate_Subexpr_No_Checks |
70482933 RK |
11343 | (Operand, Name_Req => True), |
11344 | Attribute_Name => Name_Last, | |
11345 | Expressions => New_List ( | |
11346 | Make_Integer_Literal (Loc, J)))))); | |
11347 | ||
11348 | Next_Index (N_Ix); | |
11349 | end loop; | |
11350 | end if; | |
11351 | end if; | |
11352 | ||
11353 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
11354 | ||
11355 | if Present (Cons) then | |
11356 | Odef := | |
11357 | Make_Subtype_Indication (Loc, | |
11358 | Subtype_Mark => Odef, | |
a4f4dbdb | 11359 | Constraint => |
70482933 RK |
11360 | Make_Index_Or_Discriminant_Constraint (Loc, |
11361 | Constraints => Cons)); | |
11362 | end if; | |
11363 | ||
191fcb3a | 11364 | Temp := Make_Temporary (Loc, 'C'); |
70482933 RK |
11365 | Decl := |
11366 | Make_Object_Declaration (Loc, | |
11367 | Defining_Identifier => Temp, | |
11368 | Object_Definition => Odef); | |
11369 | ||
11370 | Set_No_Initialization (Decl, True); | |
11371 | ||
11372 | -- Insert required actions. It is essential to suppress checks | |
11373 | -- since we have suppressed default initialization, which means | |
11374 | -- that the variable we create may have no discriminants. | |
11375 | ||
11376 | Insert_Actions (N, | |
11377 | New_List ( | |
11378 | Decl, | |
11379 | Make_Assignment_Statement (Loc, | |
a4f4dbdb | 11380 | Name => New_Occurrence_Of (Temp, Loc), |
70482933 RK |
11381 | Expression => Relocate_Node (N))), |
11382 | Suppress => All_Checks); | |
11383 | ||
11384 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
11385 | return; | |
11386 | end if; | |
11387 | end Handle_Changed_Representation; | |
11388 | ||
426908f8 RD |
11389 | ------------------------------- |
11390 | -- Raise_Accessibility_Error -- | |
11391 | ------------------------------- | |
11392 | ||
11393 | procedure Raise_Accessibility_Error is | |
11394 | begin | |
43417b90 | 11395 | Error_Msg_Warn := SPARK_Mode /= On; |
426908f8 RD |
11396 | Rewrite (N, |
11397 | Make_Raise_Program_Error (Sloc (N), | |
11398 | Reason => PE_Accessibility_Check_Failed)); | |
11399 | Set_Etype (N, Target_Type); | |
11400 | ||
4a28b181 AC |
11401 | Error_Msg_N ("<<accessibility check failure", N); |
11402 | Error_Msg_NE ("\<<& [", N, Standard_Program_Error); | |
426908f8 RD |
11403 | end Raise_Accessibility_Error; |
11404 | ||
70482933 RK |
11405 | ---------------------- |
11406 | -- Real_Range_Check -- | |
11407 | ---------------------- | |
11408 | ||
685094bf RD |
11409 | -- Case of conversions to floating-point or fixed-point. If range checks |
11410 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
11411 | |
11412 | -- typ (x) | |
11413 | ||
11414 | -- to | |
11415 | ||
11416 | -- Tnn : typ'Base := typ'Base (x); | |
11417 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
4e896dad | 11418 | -- typ (Tnn) |
70482933 | 11419 | |
685094bf RD |
11420 | -- This is necessary when there is a conversion of integer to float or |
11421 | -- to fixed-point to ensure that the correct checks are made. It is not | |
4e896dad EB |
11422 | -- necessary for the float-to-float case where it is enough to just set |
11423 | -- the Do_Range_Check flag on the expression. | |
fbf5a39b | 11424 | |
70482933 RK |
11425 | procedure Real_Range_Check is |
11426 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
11427 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
11428 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
a98217be ES |
11429 | |
11430 | Conv : Node_Id; | |
a98217be ES |
11431 | Hi_Arg : Node_Id; |
11432 | Hi_Val : Node_Id; | |
f537fc00 HK |
11433 | Lo_Arg : Node_Id; |
11434 | Lo_Val : Node_Id; | |
4e896dad | 11435 | Expr : Entity_Id; |
a98217be | 11436 | Tnn : Entity_Id; |
70482933 RK |
11437 | |
11438 | begin | |
11439 | -- Nothing to do if conversion was rewritten | |
11440 | ||
11441 | if Nkind (N) /= N_Type_Conversion then | |
11442 | return; | |
11443 | end if; | |
11444 | ||
4e896dad EB |
11445 | Expr := Expression (N); |
11446 | ||
11447 | -- Clear the flag once for all | |
11448 | ||
11449 | Set_Do_Range_Check (Expr, False); | |
11450 | ||
685094bf RD |
11451 | -- Nothing to do if range checks suppressed, or target has the same |
11452 | -- range as the base type (or is the base type). | |
70482933 RK |
11453 | |
11454 | if Range_Checks_Suppressed (Target_Type) | |
533369aa | 11455 | or else (Lo = Type_Low_Bound (Btyp) |
70482933 RK |
11456 | and then |
11457 | Hi = Type_High_Bound (Btyp)) | |
11458 | then | |
11459 | return; | |
11460 | end if; | |
11461 | ||
685094bf RD |
11462 | -- Nothing to do if expression is an entity on which checks have been |
11463 | -- suppressed. | |
70482933 | 11464 | |
4e896dad EB |
11465 | if Is_Entity_Name (Expr) |
11466 | and then Range_Checks_Suppressed (Entity (Expr)) | |
11467 | then | |
11468 | return; | |
11469 | end if; | |
11470 | ||
11471 | -- Nothing to do if expression was rewritten into a float-to-float | |
31fde973 | 11472 | -- conversion, since this kind of conversion is handled elsewhere. |
4e896dad EB |
11473 | |
11474 | if Is_Floating_Point_Type (Etype (Expr)) | |
11475 | and then Is_Floating_Point_Type (Target_Type) | |
fbf5a39b AC |
11476 | then |
11477 | return; | |
11478 | end if; | |
11479 | ||
685094bf RD |
11480 | -- Nothing to do if bounds are all static and we can tell that the |
11481 | -- expression is within the bounds of the target. Note that if the | |
11482 | -- operand is of an unconstrained floating-point type, then we do | |
11483 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
11484 | |
11485 | declare | |
4e896dad EB |
11486 | S_Lo : constant Node_Id := Type_Low_Bound (Etype (Expr)); |
11487 | S_Hi : constant Node_Id := Type_High_Bound (Etype (Expr)); | |
fbf5a39b AC |
11488 | |
11489 | begin | |
4e896dad EB |
11490 | if (not Is_Floating_Point_Type (Etype (Expr)) |
11491 | or else Is_Constrained (Etype (Expr))) | |
fbf5a39b AC |
11492 | and then Compile_Time_Known_Value (S_Lo) |
11493 | and then Compile_Time_Known_Value (S_Hi) | |
11494 | and then Compile_Time_Known_Value (Hi) | |
11495 | and then Compile_Time_Known_Value (Lo) | |
11496 | then | |
11497 | declare | |
11498 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
11499 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
11500 | S_Lov : Ureal; | |
11501 | S_Hiv : Ureal; | |
11502 | ||
11503 | begin | |
4e896dad | 11504 | if Is_Real_Type (Etype (Expr)) then |
fbf5a39b AC |
11505 | S_Lov := Expr_Value_R (S_Lo); |
11506 | S_Hiv := Expr_Value_R (S_Hi); | |
11507 | else | |
11508 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
11509 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
11510 | end if; | |
11511 | ||
11512 | if D_Hiv > D_Lov | |
11513 | and then S_Lov >= D_Lov | |
11514 | and then S_Hiv <= D_Hiv | |
11515 | then | |
fbf5a39b AC |
11516 | return; |
11517 | end if; | |
11518 | end; | |
11519 | end if; | |
11520 | end; | |
11521 | ||
fbf5a39b | 11522 | -- Otherwise rewrite the conversion as described above |
70482933 | 11523 | |
4e896dad | 11524 | Conv := Convert_To (Btyp, Expr); |
8113b0c7 | 11525 | |
4e896dad EB |
11526 | -- If a conversion is necessary, then copy the specific flags from |
11527 | -- the original one and also move the Do_Overflow_Check flag since | |
11528 | -- this new conversion is to the base type. | |
70482933 | 11529 | |
4e896dad EB |
11530 | if Nkind (Conv) = N_Type_Conversion then |
11531 | Set_Conversion_OK (Conv, Conversion_OK (N)); | |
11532 | Set_Float_Truncate (Conv, Float_Truncate (N)); | |
11533 | Set_Rounded_Result (Conv, Rounded_Result (N)); | |
70482933 | 11534 | |
4e896dad EB |
11535 | if Do_Overflow_Check (N) then |
11536 | Set_Do_Overflow_Check (Conv); | |
11537 | Set_Do_Overflow_Check (N, False); | |
11538 | end if; | |
70482933 RK |
11539 | end if; |
11540 | ||
191fcb3a | 11541 | Tnn := Make_Temporary (Loc, 'T', Conv); |
70482933 | 11542 | |
a98217be ES |
11543 | -- For a conversion from Float to Fixed where the bounds of the |
11544 | -- fixed-point type are static, we can obtain a more accurate | |
11545 | -- fixed-point value by converting the result of the floating- | |
11546 | -- point expression to an appropriate integer type, and then | |
11547 | -- performing an unchecked conversion to the target fixed-point | |
11548 | -- type. The range check can then use the corresponding integer | |
11549 | -- value of the bounds instead of requiring further conversions. | |
11550 | -- This preserves the identity: | |
11551 | ||
11552 | -- Fix_Val = Fixed_Type (Float_Type (Fix_Val)) | |
11553 | ||
11554 | -- which used to fail when Fix_Val was a bound of the type and | |
11555 | -- the 'Small was not a representable number. | |
11556 | -- This transformation requires an integer type large enough to | |
11557 | -- accommodate a fixed-point value. This will not be the case | |
11558 | -- in systems where Duration is larger than Long_Integer. | |
11559 | ||
11560 | if Is_Ordinary_Fixed_Point_Type (Target_Type) | |
4e896dad EB |
11561 | and then Is_Floating_Point_Type (Etype (Expr)) |
11562 | and then RM_Size (Btyp) <= RM_Size (Standard_Long_Integer) | |
a98217be ES |
11563 | and then Nkind (Lo) = N_Real_Literal |
11564 | and then Nkind (Hi) = N_Real_Literal | |
11565 | then | |
a98217be | 11566 | declare |
4e896dad | 11567 | Expr_Id : constant Entity_Id := Make_Temporary (Loc, 'T', Conv); |
04920bb6 | 11568 | Int_Type : Entity_Id; |
a98217be ES |
11569 | |
11570 | begin | |
4e896dad EB |
11571 | -- Find an integer type of the appropriate size to perform an |
11572 | -- unchecked conversion to the target fixed-point type. | |
11573 | ||
11574 | if RM_Size (Btyp) > RM_Size (Standard_Integer) then | |
a98217be ES |
11575 | Int_Type := Standard_Long_Integer; |
11576 | ||
4e896dad | 11577 | elsif RM_Size (Btyp) > RM_Size (Standard_Short_Integer) then |
a98217be ES |
11578 | Int_Type := Standard_Integer; |
11579 | ||
11580 | else | |
11581 | Int_Type := Standard_Short_Integer; | |
11582 | end if; | |
11583 | ||
04920bb6 | 11584 | -- Generate a temporary with the integer value. Required in the |
4e896dad | 11585 | -- CCG compiler to ensure that run-time checks reference this |
04920bb6 | 11586 | -- integer expression (instead of the resulting fixed-point |
4e896dad | 11587 | -- value because fixed-point values are handled by means of |
04920bb6 JM |
11588 | -- unsigned integer types). |
11589 | ||
11590 | Insert_Action (N, | |
11591 | Make_Object_Declaration (Loc, | |
11592 | Defining_Identifier => Expr_Id, | |
11593 | Object_Definition => New_Occurrence_Of (Int_Type, Loc), | |
11594 | Constant_Present => True, | |
11595 | Expression => | |
11596 | Convert_To (Int_Type, Expression (Conv)))); | |
11597 | ||
a98217be ES |
11598 | -- Create integer objects for range checking of result. |
11599 | ||
f537fc00 HK |
11600 | Lo_Arg := |
11601 | Unchecked_Convert_To | |
11602 | (Int_Type, New_Occurrence_Of (Expr_Id, Loc)); | |
11603 | ||
11604 | Lo_Val := | |
11605 | Make_Integer_Literal (Loc, Corresponding_Integer_Value (Lo)); | |
a98217be | 11606 | |
f537fc00 HK |
11607 | Hi_Arg := |
11608 | Unchecked_Convert_To | |
11609 | (Int_Type, New_Occurrence_Of (Expr_Id, Loc)); | |
11610 | ||
11611 | Hi_Val := | |
11612 | Make_Integer_Literal (Loc, Corresponding_Integer_Value (Hi)); | |
a98217be ES |
11613 | |
11614 | -- Rewrite conversion as an integer conversion of the | |
11615 | -- original floating-point expression, followed by an | |
11616 | -- unchecked conversion to the target fixed-point type. | |
11617 | ||
f537fc00 HK |
11618 | Conv := |
11619 | Make_Unchecked_Type_Conversion (Loc, | |
11620 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
11621 | Expression => New_Occurrence_Of (Expr_Id, Loc)); | |
a98217be ES |
11622 | end; |
11623 | ||
f537fc00 | 11624 | -- All other conversions |
a98217be | 11625 | |
f537fc00 | 11626 | else |
a98217be | 11627 | Lo_Arg := New_Occurrence_Of (Tnn, Loc); |
f537fc00 HK |
11628 | Lo_Val := |
11629 | Make_Attribute_Reference (Loc, | |
11630 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
11631 | Attribute_Name => Name_First); | |
a98217be ES |
11632 | |
11633 | Hi_Arg := New_Occurrence_Of (Tnn, Loc); | |
f537fc00 HK |
11634 | Hi_Val := |
11635 | Make_Attribute_Reference (Loc, | |
11636 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
11637 | Attribute_Name => Name_Last); | |
a98217be ES |
11638 | end if; |
11639 | ||
4e896dad EB |
11640 | -- Build code for range checking. Note that checks are suppressed |
11641 | -- here since we don't want a recursive range check popping up. | |
a98217be | 11642 | |
70482933 RK |
11643 | Insert_Actions (N, New_List ( |
11644 | Make_Object_Declaration (Loc, | |
11645 | Defining_Identifier => Tnn, | |
11646 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
0ac2a660 AC |
11647 | Constant_Present => True, |
11648 | Expression => Conv), | |
f537fc00 | 11649 | |
70482933 | 11650 | Make_Raise_Constraint_Error (Loc, |
f537fc00 HK |
11651 | Condition => |
11652 | Make_Or_Else (Loc, | |
11653 | Left_Opnd => | |
11654 | Make_Op_Lt (Loc, | |
11655 | Left_Opnd => Lo_Arg, | |
11656 | Right_Opnd => Lo_Val), | |
70482933 | 11657 | |
07fc65c4 GB |
11658 | Right_Opnd => |
11659 | Make_Op_Gt (Loc, | |
a98217be ES |
11660 | Left_Opnd => Hi_Arg, |
11661 | Right_Opnd => Hi_Val)), | |
4e896dad EB |
11662 | Reason => CE_Range_Check_Failed)), |
11663 | Suppress => All_Checks); | |
70482933 | 11664 | |
4e896dad | 11665 | Rewrite (Expr, New_Occurrence_Of (Tnn, Loc)); |
70482933 RK |
11666 | end Real_Range_Check; |
11667 | ||
d15f9422 AC |
11668 | ----------------------------- |
11669 | -- Has_Extra_Accessibility -- | |
11670 | ----------------------------- | |
11671 | ||
f537fc00 HK |
11672 | -- Returns true for a formal of an anonymous access type or for an Ada |
11673 | -- 2012-style stand-alone object of an anonymous access type. | |
d15f9422 AC |
11674 | |
11675 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is | |
11676 | begin | |
11677 | if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then | |
11678 | return Present (Effective_Extra_Accessibility (Id)); | |
11679 | else | |
11680 | return False; | |
11681 | end if; | |
11682 | end Has_Extra_Accessibility; | |
11683 | ||
70482933 RK |
11684 | -- Start of processing for Expand_N_Type_Conversion |
11685 | ||
11686 | begin | |
83851b23 | 11687 | -- First remove check marks put by the semantic analysis on the type |
b2502161 AC |
11688 | -- conversion between array types. We need these checks, and they will |
11689 | -- be generated by this expansion routine, but we do not depend on these | |
11690 | -- flags being set, and since we do intend to expand the checks in the | |
11691 | -- front end, we don't want them on the tree passed to the back end. | |
83851b23 AC |
11692 | |
11693 | if Is_Array_Type (Target_Type) then | |
11694 | if Is_Constrained (Target_Type) then | |
11695 | Set_Do_Length_Check (N, False); | |
11696 | else | |
11697 | Set_Do_Range_Check (Operand, False); | |
11698 | end if; | |
11699 | end if; | |
11700 | ||
685094bf | 11701 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 AC |
11702 | -- the conversion completely, it is useless, except that it may carry |
11703 | -- an Assignment_OK attribute, which must be propagated to the operand. | |
70482933 RK |
11704 | |
11705 | if Operand_Type = Target_Type then | |
7b00e31d AC |
11706 | if Assignment_OK (N) then |
11707 | Set_Assignment_OK (Operand); | |
11708 | end if; | |
11709 | ||
fbf5a39b | 11710 | Rewrite (N, Relocate_Node (Operand)); |
e606088a | 11711 | goto Done; |
70482933 RK |
11712 | end if; |
11713 | ||
685094bf RD |
11714 | -- Nothing to do if this is the second argument of read. This is a |
11715 | -- "backwards" conversion that will be handled by the specialized code | |
11716 | -- in attribute processing. | |
70482933 RK |
11717 | |
11718 | if Nkind (Parent (N)) = N_Attribute_Reference | |
11719 | and then Attribute_Name (Parent (N)) = Name_Read | |
11720 | and then Next (First (Expressions (Parent (N)))) = N | |
11721 | then | |
e606088a AC |
11722 | goto Done; |
11723 | end if; | |
11724 | ||
11725 | -- Check for case of converting to a type that has an invariant | |
d89ce432 AC |
11726 | -- associated with it. This requires an invariant check. We insert |
11727 | -- a call: | |
e606088a | 11728 | |
d89ce432 | 11729 | -- invariant_check (typ (expr)) |
e606088a | 11730 | |
d89ce432 AC |
11731 | -- in the code, after removing side effects from the expression. |
11732 | -- This is clearer than replacing the conversion into an expression | |
11733 | -- with actions, because the context may impose additional actions | |
11734 | -- (tag checks, membership tests, etc.) that conflict with this | |
11735 | -- rewriting (used previously). | |
e606088a AC |
11736 | |
11737 | -- Note: the Comes_From_Source check, and then the resetting of this | |
11738 | -- flag prevents what would otherwise be an infinite recursion. | |
11739 | ||
fd0ff1cf RD |
11740 | if Has_Invariants (Target_Type) |
11741 | and then Present (Invariant_Procedure (Target_Type)) | |
e606088a AC |
11742 | and then Comes_From_Source (N) |
11743 | then | |
11744 | Set_Comes_From_Source (N, False); | |
d89ce432 AC |
11745 | Remove_Side_Effects (N); |
11746 | Insert_Action (N, Make_Invariant_Call (Duplicate_Subexpr (N))); | |
e606088a | 11747 | goto Done; |
70482933 RK |
11748 | end if; |
11749 | ||
11750 | -- Here if we may need to expand conversion | |
11751 | ||
eaa826f8 RD |
11752 | -- If the operand of the type conversion is an arithmetic operation on |
11753 | -- signed integers, and the based type of the signed integer type in | |
11754 | -- question is smaller than Standard.Integer, we promote both of the | |
11755 | -- operands to type Integer. | |
11756 | ||
11757 | -- For example, if we have | |
11758 | ||
11759 | -- target-type (opnd1 + opnd2) | |
11760 | ||
11761 | -- and opnd1 and opnd2 are of type short integer, then we rewrite | |
11762 | -- this as: | |
11763 | ||
11764 | -- target-type (integer(opnd1) + integer(opnd2)) | |
11765 | ||
11766 | -- We do this because we are always allowed to compute in a larger type | |
11767 | -- if we do the right thing with the result, and in this case we are | |
11768 | -- going to do a conversion which will do an appropriate check to make | |
11769 | -- sure that things are in range of the target type in any case. This | |
11770 | -- avoids some unnecessary intermediate overflows. | |
11771 | ||
dfcfdc0a AC |
11772 | -- We might consider a similar transformation in the case where the |
11773 | -- target is a real type or a 64-bit integer type, and the operand | |
11774 | -- is an arithmetic operation using a 32-bit integer type. However, | |
11775 | -- we do not bother with this case, because it could cause significant | |
308e6f3a | 11776 | -- inefficiencies on 32-bit machines. On a 64-bit machine it would be |
dfcfdc0a AC |
11777 | -- much cheaper, but we don't want different behavior on 32-bit and |
11778 | -- 64-bit machines. Note that the exclusion of the 64-bit case also | |
11779 | -- handles the configurable run-time cases where 64-bit arithmetic | |
11780 | -- may simply be unavailable. | |
eaa826f8 RD |
11781 | |
11782 | -- Note: this circuit is partially redundant with respect to the circuit | |
11783 | -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in | |
11784 | -- the processing here. Also we still need the Checks circuit, since we | |
11785 | -- have to be sure not to generate junk overflow checks in the first | |
a90bd866 | 11786 | -- place, since it would be trick to remove them here. |
eaa826f8 | 11787 | |
fdfcc663 | 11788 | if Integer_Promotion_Possible (N) then |
eaa826f8 | 11789 | |
fdfcc663 | 11790 | -- All conditions met, go ahead with transformation |
eaa826f8 | 11791 | |
fdfcc663 AC |
11792 | declare |
11793 | Opnd : Node_Id; | |
11794 | L, R : Node_Id; | |
dfcfdc0a | 11795 | |
fdfcc663 AC |
11796 | begin |
11797 | R := | |
11798 | Make_Type_Conversion (Loc, | |
e4494292 | 11799 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 | 11800 | Expression => Relocate_Node (Right_Opnd (Operand))); |
eaa826f8 | 11801 | |
5f3f175d AC |
11802 | Opnd := New_Op_Node (Nkind (Operand), Loc); |
11803 | Set_Right_Opnd (Opnd, R); | |
eaa826f8 | 11804 | |
5f3f175d | 11805 | if Nkind (Operand) in N_Binary_Op then |
fdfcc663 | 11806 | L := |
eaa826f8 | 11807 | Make_Type_Conversion (Loc, |
e4494292 | 11808 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 AC |
11809 | Expression => Relocate_Node (Left_Opnd (Operand))); |
11810 | ||
5f3f175d AC |
11811 | Set_Left_Opnd (Opnd, L); |
11812 | end if; | |
eaa826f8 | 11813 | |
5f3f175d AC |
11814 | Rewrite (N, |
11815 | Make_Type_Conversion (Loc, | |
11816 | Subtype_Mark => Relocate_Node (Subtype_Mark (N)), | |
11817 | Expression => Opnd)); | |
dfcfdc0a | 11818 | |
5f3f175d | 11819 | Analyze_And_Resolve (N, Target_Type); |
e606088a | 11820 | goto Done; |
fdfcc663 AC |
11821 | end; |
11822 | end if; | |
eaa826f8 | 11823 | |
f82944b7 JM |
11824 | -- Do validity check if validity checking operands |
11825 | ||
533369aa | 11826 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
11827 | Ensure_Valid (Operand); |
11828 | end if; | |
11829 | ||
70482933 RK |
11830 | -- Special case of converting from non-standard boolean type |
11831 | ||
11832 | if Is_Boolean_Type (Operand_Type) | |
11833 | and then (Nonzero_Is_True (Operand_Type)) | |
11834 | then | |
11835 | Adjust_Condition (Operand); | |
11836 | Set_Etype (Operand, Standard_Boolean); | |
11837 | Operand_Type := Standard_Boolean; | |
11838 | end if; | |
11839 | ||
11840 | -- Case of converting to an access type | |
11841 | ||
11842 | if Is_Access_Type (Target_Type) then | |
1b2f53bb JS |
11843 | -- In terms of accessibility rules, an anonymous access discriminant |
11844 | -- is not considered separate from its parent object. | |
11845 | ||
11846 | if Nkind (Operand) = N_Selected_Component | |
11847 | and then Ekind (Entity (Selector_Name (Operand))) = E_Discriminant | |
11848 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
11849 | then | |
11850 | Operand_Acc := Original_Node (Prefix (Operand)); | |
11851 | end if; | |
70482933 | 11852 | |
ca0b6141 | 11853 | -- If this type conversion was internally generated by the front end |
904a2ae4 | 11854 | -- to displace the pointer to the object to reference an interface |
ca0b6141 | 11855 | -- type and the original node was an Unrestricted_Access attribute, |
904a2ae4 AC |
11856 | -- then skip applying accessibility checks (because, according to the |
11857 | -- GNAT Reference Manual, this attribute is similar to 'Access except | |
11858 | -- that all accessibility and aliased view checks are omitted). | |
11859 | ||
11860 | if not Comes_From_Source (N) | |
11861 | and then Is_Interface (Designated_Type (Target_Type)) | |
11862 | and then Nkind (Original_Node (N)) = N_Attribute_Reference | |
0bcee275 AC |
11863 | and then Attribute_Name (Original_Node (N)) = |
11864 | Name_Unrestricted_Access | |
904a2ae4 AC |
11865 | then |
11866 | null; | |
11867 | ||
d766cee3 RD |
11868 | -- Apply an accessibility check when the conversion operand is an |
11869 | -- access parameter (or a renaming thereof), unless conversion was | |
6a237c45 | 11870 | -- expanded from an Unchecked_ or Unrestricted_Access attribute, |
683af98c AC |
11871 | -- or for the actual of a class-wide interface parameter. Note that |
11872 | -- other checks may still need to be applied below (such as tagged | |
11873 | -- type checks). | |
70482933 | 11874 | |
1b2f53bb JS |
11875 | elsif Is_Entity_Name (Operand_Acc) |
11876 | and then Has_Extra_Accessibility (Entity (Operand_Acc)) | |
11877 | and then Ekind (Etype (Operand_Acc)) = E_Anonymous_Access_Type | |
d766cee3 RD |
11878 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
11879 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 11880 | then |
6a237c45 | 11881 | if not Comes_From_Source (N) |
683af98c | 11882 | and then Nkind_In (Parent (N), N_Function_Call, |
6d0289b1 HK |
11883 | N_Parameter_Association, |
11884 | N_Procedure_Call_Statement) | |
6a237c45 AC |
11885 | and then Is_Interface (Designated_Type (Target_Type)) |
11886 | and then Is_Class_Wide_Type (Designated_Type (Target_Type)) | |
11887 | then | |
11888 | null; | |
11889 | ||
11890 | else | |
11891 | Apply_Accessibility_Check | |
1b2f53bb | 11892 | (Operand_Acc, Target_Type, Insert_Node => Operand); |
6a237c45 | 11893 | end if; |
70482933 | 11894 | |
e84e11ba | 11895 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
11896 | -- level of the target type, then force Program_Error. Note that this |
11897 | -- can only occur for cases where the attribute is within the body of | |
6c56d9b8 AC |
11898 | -- an instantiation, otherwise the conversion will already have been |
11899 | -- rejected as illegal. | |
11900 | ||
11901 | -- Note: warnings are issued by the analyzer for the instance cases | |
70482933 RK |
11902 | |
11903 | elsif In_Instance_Body | |
6c56d9b8 AC |
11904 | |
11905 | -- The case where the target type is an anonymous access type of | |
11906 | -- a discriminant is excluded, because the level of such a type | |
11907 | -- depends on the context and currently the level returned for such | |
604801a4 | 11908 | -- types is zero, resulting in warnings about check failures |
6c56d9b8 AC |
11909 | -- in certain legal cases involving class-wide interfaces as the |
11910 | -- designated type (some cases, such as return statements, are | |
11911 | -- checked at run time, but not clear if these are handled right | |
11912 | -- in general, see 3.10.2(12/2-12.5/3) ???). | |
11913 | ||
ad5edba5 AC |
11914 | and then |
11915 | not (Ekind (Target_Type) = E_Anonymous_Access_Type | |
11916 | and then Present (Associated_Node_For_Itype (Target_Type)) | |
11917 | and then Nkind (Associated_Node_For_Itype (Target_Type)) = | |
11918 | N_Discriminant_Specification) | |
11919 | and then | |
11920 | Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type) | |
70482933 | 11921 | then |
426908f8 | 11922 | Raise_Accessibility_Error; |
91669e7e | 11923 | goto Done; |
70482933 | 11924 | |
685094bf RD |
11925 | -- When the operand is a selected access discriminant the check needs |
11926 | -- to be made against the level of the object denoted by the prefix | |
11927 | -- of the selected name. Force Program_Error for this case as well | |
11928 | -- (this accessibility violation can only happen if within the body | |
11929 | -- of an instantiation). | |
70482933 RK |
11930 | |
11931 | elsif In_Instance_Body | |
11932 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
11933 | and then Nkind (Operand) = N_Selected_Component | |
ec98bb7d | 11934 | and then Ekind (Entity (Selector_Name (Operand))) = E_Discriminant |
70482933 RK |
11935 | and then Object_Access_Level (Operand) > |
11936 | Type_Access_Level (Target_Type) | |
11937 | then | |
426908f8 | 11938 | Raise_Accessibility_Error; |
e606088a | 11939 | goto Done; |
70482933 RK |
11940 | end if; |
11941 | end if; | |
11942 | ||
11943 | -- Case of conversions of tagged types and access to tagged types | |
11944 | ||
685094bf RD |
11945 | -- When needed, that is to say when the expression is class-wide, Add |
11946 | -- runtime a tag check for (strict) downward conversion by using the | |
11947 | -- membership test, generating: | |
70482933 RK |
11948 | |
11949 | -- [constraint_error when Operand not in Target_Type'Class] | |
11950 | ||
11951 | -- or in the access type case | |
11952 | ||
11953 | -- [constraint_error | |
11954 | -- when Operand /= null | |
11955 | -- and then Operand.all not in | |
11956 | -- Designated_Type (Target_Type)'Class] | |
11957 | ||
11958 | if (Is_Access_Type (Target_Type) | |
11959 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
11960 | or else Is_Tagged_Type (Target_Type) | |
11961 | then | |
685094bf RD |
11962 | -- Do not do any expansion in the access type case if the parent is a |
11963 | -- renaming, since this is an error situation which will be caught by | |
11964 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 | 11965 | |
e7e4d230 | 11966 | if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then |
e606088a | 11967 | goto Done; |
70482933 RK |
11968 | end if; |
11969 | ||
0669bebe | 11970 | -- Otherwise, proceed with processing tagged conversion |
70482933 | 11971 | |
e7e4d230 | 11972 | Tagged_Conversion : declare |
8cea7b64 HK |
11973 | Actual_Op_Typ : Entity_Id; |
11974 | Actual_Targ_Typ : Entity_Id; | |
11975 | Make_Conversion : Boolean := False; | |
11976 | Root_Op_Typ : Entity_Id; | |
70482933 | 11977 | |
8cea7b64 HK |
11978 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
11979 | -- Create a membership check to test whether Operand is a member | |
11980 | -- of Targ_Typ. If the original Target_Type is an access, include | |
11981 | -- a test for null value. The check is inserted at N. | |
11982 | ||
11983 | -------------------- | |
11984 | -- Make_Tag_Check -- | |
11985 | -------------------- | |
11986 | ||
11987 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
11988 | Cond : Node_Id; | |
11989 | ||
11990 | begin | |
11991 | -- Generate: | |
11992 | -- [Constraint_Error | |
11993 | -- when Operand /= null | |
11994 | -- and then Operand.all not in Targ_Typ] | |
11995 | ||
11996 | if Is_Access_Type (Target_Type) then | |
11997 | Cond := | |
11998 | Make_And_Then (Loc, | |
11999 | Left_Opnd => | |
12000 | Make_Op_Ne (Loc, | |
12001 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
12002 | Right_Opnd => Make_Null (Loc)), | |
12003 | ||
12004 | Right_Opnd => | |
12005 | Make_Not_In (Loc, | |
12006 | Left_Opnd => | |
12007 | Make_Explicit_Dereference (Loc, | |
12008 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
e4494292 | 12009 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc))); |
8cea7b64 HK |
12010 | |
12011 | -- Generate: | |
12012 | -- [Constraint_Error when Operand not in Targ_Typ] | |
12013 | ||
12014 | else | |
12015 | Cond := | |
12016 | Make_Not_In (Loc, | |
12017 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
e4494292 | 12018 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)); |
8cea7b64 HK |
12019 | end if; |
12020 | ||
12021 | Insert_Action (N, | |
12022 | Make_Raise_Constraint_Error (Loc, | |
12023 | Condition => Cond, | |
cf9a473e AC |
12024 | Reason => CE_Tag_Check_Failed), |
12025 | Suppress => All_Checks); | |
8cea7b64 HK |
12026 | end Make_Tag_Check; |
12027 | ||
e7e4d230 | 12028 | -- Start of processing for Tagged_Conversion |
70482933 RK |
12029 | |
12030 | begin | |
9732e886 | 12031 | -- Handle entities from the limited view |
852dba80 | 12032 | |
9732e886 | 12033 | if Is_Access_Type (Operand_Type) then |
852dba80 AC |
12034 | Actual_Op_Typ := |
12035 | Available_View (Designated_Type (Operand_Type)); | |
9732e886 JM |
12036 | else |
12037 | Actual_Op_Typ := Operand_Type; | |
12038 | end if; | |
12039 | ||
12040 | if Is_Access_Type (Target_Type) then | |
852dba80 AC |
12041 | Actual_Targ_Typ := |
12042 | Available_View (Designated_Type (Target_Type)); | |
70482933 | 12043 | else |
8cea7b64 | 12044 | Actual_Targ_Typ := Target_Type; |
70482933 RK |
12045 | end if; |
12046 | ||
8cea7b64 HK |
12047 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
12048 | ||
20b5d666 JM |
12049 | -- Ada 2005 (AI-251): Handle interface type conversion |
12050 | ||
3cb9a885 | 12051 | if Is_Interface (Actual_Op_Typ) |
58b81ab0 AC |
12052 | or else |
12053 | Is_Interface (Actual_Targ_Typ) | |
3cb9a885 | 12054 | then |
f6f4d8d4 | 12055 | Expand_Interface_Conversion (N); |
e606088a | 12056 | goto Done; |
20b5d666 JM |
12057 | end if; |
12058 | ||
8cea7b64 | 12059 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 12060 | |
8cea7b64 HK |
12061 | -- Create a runtime tag check for a downward class-wide type |
12062 | -- conversion. | |
70482933 | 12063 | |
8cea7b64 | 12064 | if Is_Class_Wide_Type (Actual_Op_Typ) |
852dba80 | 12065 | and then Actual_Op_Typ /= Actual_Targ_Typ |
8cea7b64 | 12066 | and then Root_Op_Typ /= Actual_Targ_Typ |
4ac2477e JM |
12067 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ, |
12068 | Use_Full_View => True) | |
8cea7b64 HK |
12069 | then |
12070 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
12071 | Make_Conversion := True; | |
12072 | end if; | |
70482933 | 12073 | |
8cea7b64 HK |
12074 | -- AI05-0073: If the result subtype of the function is defined |
12075 | -- by an access_definition designating a specific tagged type | |
12076 | -- T, a check is made that the result value is null or the tag | |
12077 | -- of the object designated by the result value identifies T. | |
12078 | -- Constraint_Error is raised if this check fails. | |
70482933 | 12079 | |
92a7cd46 | 12080 | if Nkind (Parent (N)) = N_Simple_Return_Statement then |
8cea7b64 | 12081 | declare |
e886436a | 12082 | Func : Entity_Id; |
8cea7b64 HK |
12083 | Func_Typ : Entity_Id; |
12084 | ||
12085 | begin | |
e886436a | 12086 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 12087 | |
e886436a | 12088 | Func := Current_Scope; |
8cea7b64 HK |
12089 | while Present (Func) |
12090 | and then Ekind (Func) /= E_Function | |
12091 | loop | |
12092 | Func := Scope (Func); | |
12093 | end loop; | |
12094 | ||
12095 | -- The function's return subtype must be defined using | |
12096 | -- an access definition. | |
12097 | ||
12098 | if Nkind (Result_Definition (Parent (Func))) = | |
12099 | N_Access_Definition | |
12100 | then | |
12101 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
12102 | ||
12103 | -- The return subtype denotes a specific tagged type, | |
12104 | -- in other words, a non class-wide type. | |
12105 | ||
12106 | if Is_Tagged_Type (Func_Typ) | |
12107 | and then not Is_Class_Wide_Type (Func_Typ) | |
12108 | then | |
12109 | Make_Tag_Check (Actual_Targ_Typ); | |
12110 | Make_Conversion := True; | |
12111 | end if; | |
12112 | end if; | |
12113 | end; | |
70482933 RK |
12114 | end if; |
12115 | ||
8cea7b64 HK |
12116 | -- We have generated a tag check for either a class-wide type |
12117 | -- conversion or for AI05-0073. | |
70482933 | 12118 | |
8cea7b64 HK |
12119 | if Make_Conversion then |
12120 | declare | |
12121 | Conv : Node_Id; | |
12122 | begin | |
12123 | Conv := | |
12124 | Make_Unchecked_Type_Conversion (Loc, | |
12125 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
12126 | Expression => Relocate_Node (Expression (N))); | |
12127 | Rewrite (N, Conv); | |
12128 | Analyze_And_Resolve (N, Target_Type); | |
12129 | end; | |
12130 | end if; | |
70482933 | 12131 | end if; |
e7e4d230 | 12132 | end Tagged_Conversion; |
70482933 RK |
12133 | |
12134 | -- Case of other access type conversions | |
12135 | ||
12136 | elsif Is_Access_Type (Target_Type) then | |
12137 | Apply_Constraint_Check (Operand, Target_Type); | |
12138 | ||
12139 | -- Case of conversions from a fixed-point type | |
12140 | ||
685094bf RD |
12141 | -- These conversions require special expansion and processing, found in |
12142 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
12143 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
12144 | -- conversions, which do not need further processing. |
12145 | ||
12146 | elsif Is_Fixed_Point_Type (Operand_Type) | |
12147 | and then not Conversion_OK (N) | |
12148 | then | |
12149 | -- We should never see universal fixed at this case, since the | |
12150 | -- expansion of the constituent divide or multiply should have | |
12151 | -- eliminated the explicit mention of universal fixed. | |
12152 | ||
12153 | pragma Assert (Operand_Type /= Universal_Fixed); | |
12154 | ||
685094bf RD |
12155 | -- Check for special case of the conversion to universal real that |
12156 | -- occurs as a result of the use of a round attribute. In this case, | |
12157 | -- the real type for the conversion is taken from the target type of | |
12158 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
12159 | |
12160 | if Target_Type = Universal_Real | |
12161 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
12162 | and then Attribute_Name (Parent (N)) = Name_Round | |
12163 | then | |
12164 | Set_Rounded_Result (N); | |
12165 | Set_Etype (N, Etype (Parent (N))); | |
8113b0c7 | 12166 | Target_Type := Etype (N); |
70482933 RK |
12167 | end if; |
12168 | ||
8113b0c7 EB |
12169 | if Is_Fixed_Point_Type (Target_Type) then |
12170 | Expand_Convert_Fixed_To_Fixed (N); | |
12171 | Real_Range_Check; | |
241848fd | 12172 | |
8113b0c7 EB |
12173 | elsif Is_Integer_Type (Target_Type) then |
12174 | Expand_Convert_Fixed_To_Integer (N); | |
12175 | Discrete_Range_Check; | |
241848fd | 12176 | |
8113b0c7 EB |
12177 | else |
12178 | pragma Assert (Is_Floating_Point_Type (Target_Type)); | |
12179 | Expand_Convert_Fixed_To_Float (N); | |
12180 | Real_Range_Check; | |
70482933 RK |
12181 | end if; |
12182 | ||
12183 | -- Case of conversions to a fixed-point type | |
12184 | ||
685094bf RD |
12185 | -- These conversions require special expansion and processing, found in |
12186 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
12187 | -- since from a semantic point of view, these are simple integer | |
12188 | -- conversions, which do not need further processing. | |
70482933 RK |
12189 | |
12190 | elsif Is_Fixed_Point_Type (Target_Type) | |
12191 | and then not Conversion_OK (N) | |
12192 | then | |
12193 | if Is_Integer_Type (Operand_Type) then | |
12194 | Expand_Convert_Integer_To_Fixed (N); | |
12195 | Real_Range_Check; | |
12196 | else | |
12197 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
12198 | Expand_Convert_Float_To_Fixed (N); | |
12199 | Real_Range_Check; | |
12200 | end if; | |
12201 | ||
70482933 RK |
12202 | -- Case of array conversions |
12203 | ||
685094bf RD |
12204 | -- Expansion of array conversions, add required length/range checks but |
12205 | -- only do this if there is no change of representation. For handling of | |
12206 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
12207 | |
12208 | elsif Is_Array_Type (Target_Type) then | |
70482933 RK |
12209 | if Is_Constrained (Target_Type) then |
12210 | Apply_Length_Check (Operand, Target_Type); | |
12211 | else | |
12212 | Apply_Range_Check (Operand, Target_Type); | |
12213 | end if; | |
12214 | ||
12215 | Handle_Changed_Representation; | |
12216 | ||
12217 | -- Case of conversions of discriminated types | |
12218 | ||
685094bf RD |
12219 | -- Add required discriminant checks if target is constrained. Again this |
12220 | -- change is skipped if we have a change of representation. | |
70482933 RK |
12221 | |
12222 | elsif Has_Discriminants (Target_Type) | |
12223 | and then Is_Constrained (Target_Type) | |
12224 | then | |
12225 | Apply_Discriminant_Check (Operand, Target_Type); | |
12226 | Handle_Changed_Representation; | |
12227 | ||
12228 | -- Case of all other record conversions. The only processing required | |
12229 | -- is to check for a change of representation requiring the special | |
12230 | -- assignment processing. | |
12231 | ||
12232 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
12233 | |
12234 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
12235 | -- a derived Unchecked_Union type to an unconstrained type that is |
12236 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
12237 | |
12238 | if Is_Derived_Type (Operand_Type) | |
12239 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
12240 | and then not Is_Constrained (Target_Type) | |
12241 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
12242 | and then not Has_Inferable_Discriminants (Operand) | |
12243 | then | |
685094bf | 12244 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e | 12245 | -- Program_Error node, but we give it the target type of the |
6cb3037c | 12246 | -- conversion (is this requirement documented somewhere ???) |
5d09245e AC |
12247 | |
12248 | declare | |
12249 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
12250 | Reason => PE_Unchecked_Union_Restriction); | |
12251 | ||
12252 | begin | |
12253 | Set_Etype (PE, Target_Type); | |
12254 | Rewrite (N, PE); | |
12255 | ||
12256 | end; | |
12257 | else | |
12258 | Handle_Changed_Representation; | |
12259 | end if; | |
70482933 RK |
12260 | |
12261 | -- Case of conversions of enumeration types | |
12262 | ||
12263 | elsif Is_Enumeration_Type (Target_Type) then | |
12264 | ||
12265 | -- Special processing is required if there is a change of | |
e7e4d230 | 12266 | -- representation (from enumeration representation clauses). |
70482933 RK |
12267 | |
12268 | if not Same_Representation (Target_Type, Operand_Type) then | |
12269 | ||
12270 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
12271 | ||
12272 | Rewrite (N, | |
1c66c4f5 AC |
12273 | Make_Attribute_Reference (Loc, |
12274 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
12275 | Attribute_Name => Name_Val, | |
12276 | Expressions => New_List ( | |
12277 | Make_Attribute_Reference (Loc, | |
12278 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
12279 | Attribute_Name => Name_Pos, | |
12280 | Expressions => New_List (Operand))))); | |
70482933 RK |
12281 | |
12282 | Analyze_And_Resolve (N, Target_Type); | |
12283 | end if; | |
70482933 RK |
12284 | end if; |
12285 | ||
685094bf | 12286 | -- At this stage, either the conversion node has been transformed into |
e7e4d230 AC |
12287 | -- some other equivalent expression, or left as a conversion that can be |
12288 | -- handled by Gigi, in the following cases: | |
70482933 RK |
12289 | |
12290 | -- Conversions with no change of representation or type | |
12291 | ||
685094bf RD |
12292 | -- Numeric conversions involving integer, floating- and fixed-point |
12293 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
12294 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 12295 | |
5e1c00fa RD |
12296 | -- No other conversions should be passed to Gigi |
12297 | ||
12298 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 12299 | |
685094bf | 12300 | -- The only remaining step is to generate a range check if we still have |
267c7ff6 EB |
12301 | -- a type conversion at this stage and Do_Range_Check is set. Note that |
12302 | -- we need to deal with at most 8 out of the 9 possible cases of numeric | |
12303 | -- conversions here, because the float-to-integer case is entirely dealt | |
12304 | -- with by Apply_Float_Conversion_Check. | |
fbf5a39b | 12305 | |
8113b0c7 EB |
12306 | if Nkind (N) = N_Type_Conversion |
12307 | and then Do_Range_Check (Expression (N)) | |
12308 | then | |
12309 | -- Float-to-float conversions | |
fbf5a39b | 12310 | |
8113b0c7 | 12311 | if Is_Floating_Point_Type (Target_Type) |
f5655e4a | 12312 | and then Is_Floating_Point_Type (Etype (Expression (N))) |
7b536495 | 12313 | then |
67460d45 EB |
12314 | -- Reset overflow flag, since the range check will include |
12315 | -- dealing with possible overflow, and generate the check. | |
12316 | ||
12317 | Set_Do_Overflow_Check (N, False); | |
12318 | ||
8113b0c7 EB |
12319 | Generate_Range_Check |
12320 | (Expression (N), Target_Type, CE_Range_Check_Failed); | |
fbf5a39b | 12321 | |
8113b0c7 EB |
12322 | -- Discrete-to-discrete conversions or fixed-point-to-discrete |
12323 | -- conversions when Conversion_OK is set. | |
fbf5a39b | 12324 | |
8113b0c7 EB |
12325 | elsif Is_Discrete_Type (Target_Type) |
12326 | and then (Is_Discrete_Type (Etype (Expression (N))) | |
12327 | or else (Is_Fixed_Point_Type (Etype (Expression (N))) | |
12328 | and then Conversion_OK (N))) | |
12329 | then | |
8113b0c7 EB |
12330 | -- If Address is either a source type or target type, |
12331 | -- suppress range check to avoid typing anomalies when | |
12332 | -- it is a visible integer type. | |
7b536495 | 12333 | |
8113b0c7 EB |
12334 | if Is_Descendant_Of_Address (Etype (Expression (N))) |
12335 | or else Is_Descendant_Of_Address (Target_Type) | |
12336 | then | |
12337 | Set_Do_Range_Check (Expression (N), False); | |
12338 | else | |
12339 | Discrete_Range_Check; | |
12340 | end if; | |
7b536495 | 12341 | |
8113b0c7 | 12342 | -- Conversions to floating- or fixed-point when Conversion_OK is set |
7b536495 | 12343 | |
8113b0c7 EB |
12344 | elsif Is_Floating_Point_Type (Target_Type) |
12345 | or else (Is_Fixed_Point_Type (Target_Type) | |
12346 | and then Conversion_OK (N)) | |
12347 | then | |
12348 | Real_Range_Check; | |
7b536495 | 12349 | end if; |
fbf5a39b | 12350 | end if; |
f02b8bb8 | 12351 | |
e606088a AC |
12352 | -- Here at end of processing |
12353 | ||
48f91b44 RD |
12354 | <<Done>> |
12355 | -- Apply predicate check if required. Note that we can't just call | |
12356 | -- Apply_Predicate_Check here, because the type looks right after | |
12357 | -- the conversion and it would omit the check. The Comes_From_Source | |
12358 | -- guard is necessary to prevent infinite recursions when we generate | |
12359 | -- internal conversions for the purpose of checking predicates. | |
12360 | ||
12361 | if Present (Predicate_Function (Target_Type)) | |
8d4611f7 | 12362 | and then not Predicates_Ignored (Target_Type) |
48f91b44 RD |
12363 | and then Target_Type /= Operand_Type |
12364 | and then Comes_From_Source (N) | |
12365 | then | |
00332244 AC |
12366 | declare |
12367 | New_Expr : constant Node_Id := Duplicate_Subexpr (N); | |
12368 | ||
12369 | begin | |
12370 | -- Avoid infinite recursion on the subsequent expansion of | |
6ef13c4f ES |
12371 | -- of the copy of the original type conversion. When needed, |
12372 | -- a range check has already been applied to the expression. | |
00332244 AC |
12373 | |
12374 | Set_Comes_From_Source (New_Expr, False); | |
6ef13c4f ES |
12375 | Insert_Action (N, |
12376 | Make_Predicate_Check (Target_Type, New_Expr), | |
12377 | Suppress => Range_Check); | |
00332244 | 12378 | end; |
48f91b44 | 12379 | end if; |
70482933 RK |
12380 | end Expand_N_Type_Conversion; |
12381 | ||
12382 | ----------------------------------- | |
12383 | -- Expand_N_Unchecked_Expression -- | |
12384 | ----------------------------------- | |
12385 | ||
e7e4d230 | 12386 | -- Remove the unchecked expression node from the tree. Its job was simply |
70482933 | 12387 | -- to make sure that its constituent expression was handled with checks |
604801a4 PT |
12388 | -- off, and now that is done, we can remove it from the tree, and indeed |
12389 | -- must, since Gigi does not expect to see these nodes. | |
70482933 RK |
12390 | |
12391 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
12392 | Exp : constant Node_Id := Expression (N); | |
70482933 | 12393 | begin |
e7e4d230 | 12394 | Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp)); |
70482933 RK |
12395 | Rewrite (N, Exp); |
12396 | end Expand_N_Unchecked_Expression; | |
12397 | ||
12398 | ---------------------------------------- | |
12399 | -- Expand_N_Unchecked_Type_Conversion -- | |
12400 | ---------------------------------------- | |
12401 | ||
685094bf RD |
12402 | -- If this cannot be handled by Gigi and we haven't already made a |
12403 | -- temporary for it, do it now. | |
70482933 RK |
12404 | |
12405 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
12406 | Target_Type : constant Entity_Id := Etype (N); | |
12407 | Operand : constant Node_Id := Expression (N); | |
12408 | Operand_Type : constant Entity_Id := Etype (Operand); | |
12409 | ||
12410 | begin | |
7b00e31d | 12411 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 | 12412 | -- the conversion completely, it is useless, except that it may carry |
e7e4d230 | 12413 | -- an Assignment_OK indication which must be propagated to the operand. |
7b00e31d AC |
12414 | |
12415 | if Operand_Type = Target_Type then | |
13d923cc | 12416 | |
e7e4d230 AC |
12417 | -- Code duplicates Expand_N_Unchecked_Expression above, factor??? |
12418 | ||
7b00e31d AC |
12419 | if Assignment_OK (N) then |
12420 | Set_Assignment_OK (Operand); | |
12421 | end if; | |
12422 | ||
12423 | Rewrite (N, Relocate_Node (Operand)); | |
12424 | return; | |
12425 | end if; | |
12426 | ||
70482933 RK |
12427 | -- If we have a conversion of a compile time known value to a target |
12428 | -- type and the value is in range of the target type, then we can simply | |
12429 | -- replace the construct by an integer literal of the correct type. We | |
12430 | -- only apply this to integer types being converted. Possibly it may | |
12431 | -- apply in other cases, but it is too much trouble to worry about. | |
12432 | ||
12433 | -- Note that we do not do this transformation if the Kill_Range_Check | |
12434 | -- flag is set, since then the value may be outside the expected range. | |
12435 | -- This happens in the Normalize_Scalars case. | |
12436 | ||
20b5d666 JM |
12437 | -- We also skip this if either the target or operand type is biased |
12438 | -- because in this case, the unchecked conversion is supposed to | |
12439 | -- preserve the bit pattern, not the integer value. | |
12440 | ||
70482933 | 12441 | if Is_Integer_Type (Target_Type) |
20b5d666 | 12442 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 12443 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 12444 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
12445 | and then Compile_Time_Known_Value (Operand) |
12446 | and then not Kill_Range_Check (N) | |
12447 | then | |
12448 | declare | |
12449 | Val : constant Uint := Expr_Value (Operand); | |
12450 | ||
12451 | begin | |
12452 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
12453 | and then | |
12454 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
12455 | and then | |
12456 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
12457 | and then | |
12458 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
12459 | then | |
12460 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 12461 | |
685094bf RD |
12462 | -- If Address is the target type, just set the type to avoid a |
12463 | -- spurious type error on the literal when Address is a visible | |
12464 | -- integer type. | |
8a36a0cc | 12465 | |
d9d25d04 | 12466 | if Is_Descendant_Of_Address (Target_Type) then |
8a36a0cc AC |
12467 | Set_Etype (N, Target_Type); |
12468 | else | |
12469 | Analyze_And_Resolve (N, Target_Type); | |
12470 | end if; | |
12471 | ||
70482933 RK |
12472 | return; |
12473 | end if; | |
12474 | end; | |
12475 | end if; | |
12476 | ||
02458cc7 JM |
12477 | -- Generate an extra temporary for cases unsupported by the C backend |
12478 | ||
12479 | if Modify_Tree_For_C then | |
12480 | declare | |
12481 | Source : constant Node_Id := Unqual_Conv (Expression (N)); | |
12482 | Source_Typ : Entity_Id := Get_Full_View (Etype (Source)); | |
12483 | ||
12484 | begin | |
12485 | if Is_Packed_Array (Source_Typ) then | |
12486 | Source_Typ := Packed_Array_Impl_Type (Source_Typ); | |
12487 | end if; | |
12488 | ||
12489 | if Nkind (Source) = N_Function_Call | |
12490 | and then (Is_Composite_Type (Etype (Source)) | |
12491 | or else Is_Composite_Type (Target_Type)) | |
12492 | then | |
12493 | Force_Evaluation (Source); | |
12494 | end if; | |
12495 | end; | |
12496 | end if; | |
12497 | ||
70482933 RK |
12498 | -- Nothing to do if conversion is safe |
12499 | ||
12500 | if Safe_Unchecked_Type_Conversion (N) then | |
12501 | return; | |
12502 | end if; | |
12503 | ||
12504 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
324ac540 | 12505 | -- flag indicates ??? More comments needed here) |
70482933 RK |
12506 | |
12507 | if Assignment_OK (N) then | |
12508 | null; | |
12509 | else | |
12510 | Force_Evaluation (N); | |
12511 | end if; | |
12512 | end Expand_N_Unchecked_Type_Conversion; | |
12513 | ||
12514 | ---------------------------- | |
12515 | -- Expand_Record_Equality -- | |
12516 | ---------------------------- | |
12517 | ||
12518 | -- For non-variant records, Equality is expanded when needed into: | |
12519 | ||
12520 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
12521 | -- and then ... | |
12522 | -- and then Lhs.Discrn = Rhs.Discrn | |
12523 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
12524 | -- and then ... | |
12525 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
12526 | ||
c7a494c9 | 12527 | -- The expression is folded by the back end for adjacent fields. This |
70482933 RK |
12528 | -- function is called for tagged record in only one occasion: for imple- |
12529 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
12530 | -- otherwise the primitive "=" is used directly. | |
12531 | ||
12532 | function Expand_Record_Equality | |
12533 | (Nod : Node_Id; | |
12534 | Typ : Entity_Id; | |
12535 | Lhs : Node_Id; | |
12536 | Rhs : Node_Id; | |
2e071734 | 12537 | Bodies : List_Id) return Node_Id |
70482933 RK |
12538 | is |
12539 | Loc : constant Source_Ptr := Sloc (Nod); | |
12540 | ||
0ab80019 AC |
12541 | Result : Node_Id; |
12542 | C : Entity_Id; | |
12543 | ||
12544 | First_Time : Boolean := True; | |
12545 | ||
6b670dcf AC |
12546 | function Element_To_Compare (C : Entity_Id) return Entity_Id; |
12547 | -- Return the next discriminant or component to compare, starting with | |
12548 | -- C, skipping inherited components. | |
0ab80019 | 12549 | |
6b670dcf AC |
12550 | ------------------------ |
12551 | -- Element_To_Compare -- | |
12552 | ------------------------ | |
70482933 | 12553 | |
6b670dcf AC |
12554 | function Element_To_Compare (C : Entity_Id) return Entity_Id is |
12555 | Comp : Entity_Id; | |
28270211 | 12556 | |
70482933 | 12557 | begin |
6b670dcf | 12558 | Comp := C; |
6b670dcf AC |
12559 | loop |
12560 | -- Exit loop when the next element to be compared is found, or | |
12561 | -- there is no more such element. | |
70482933 | 12562 | |
6b670dcf | 12563 | exit when No (Comp); |
8190087e | 12564 | |
6b670dcf AC |
12565 | exit when Ekind_In (Comp, E_Discriminant, E_Component) |
12566 | and then not ( | |
70482933 | 12567 | |
6b670dcf | 12568 | -- Skip inherited components |
70482933 | 12569 | |
6b670dcf AC |
12570 | -- Note: for a tagged type, we always generate the "=" primitive |
12571 | -- for the base type (not on the first subtype), so the test for | |
12572 | -- Comp /= Original_Record_Component (Comp) is True for | |
12573 | -- inherited components only. | |
24558db8 | 12574 | |
6b670dcf | 12575 | (Is_Tagged_Type (Typ) |
28270211 | 12576 | and then Comp /= Original_Record_Component (Comp)) |
24558db8 | 12577 | |
6b670dcf | 12578 | -- Skip _Tag |
26bff3d9 | 12579 | |
6b670dcf AC |
12580 | or else Chars (Comp) = Name_uTag |
12581 | ||
6b670dcf AC |
12582 | -- Skip interface elements (secondary tags???) |
12583 | ||
12584 | or else Is_Interface (Etype (Comp))); | |
12585 | ||
12586 | Next_Entity (Comp); | |
12587 | end loop; | |
12588 | ||
12589 | return Comp; | |
12590 | end Element_To_Compare; | |
70482933 | 12591 | |
70482933 RK |
12592 | -- Start of processing for Expand_Record_Equality |
12593 | ||
12594 | begin | |
70482933 RK |
12595 | -- Generates the following code: (assuming that Typ has one Discr and |
12596 | -- component C2 is also a record) | |
12597 | ||
63254915 AC |
12598 | -- Lhs.Discr1 = Rhs.Discr1 |
12599 | -- and then Lhs.C1 = Rhs.C1 | |
12600 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
12601 | -- and then ... | |
12602 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
70482933 | 12603 | |
e4494292 | 12604 | Result := New_Occurrence_Of (Standard_True, Loc); |
6b670dcf | 12605 | C := Element_To_Compare (First_Entity (Typ)); |
70482933 | 12606 | while Present (C) loop |
70482933 RK |
12607 | declare |
12608 | New_Lhs : Node_Id; | |
12609 | New_Rhs : Node_Id; | |
8aceda64 | 12610 | Check : Node_Id; |
70482933 RK |
12611 | |
12612 | begin | |
12613 | if First_Time then | |
70482933 RK |
12614 | New_Lhs := Lhs; |
12615 | New_Rhs := Rhs; | |
70482933 RK |
12616 | else |
12617 | New_Lhs := New_Copy_Tree (Lhs); | |
12618 | New_Rhs := New_Copy_Tree (Rhs); | |
12619 | end if; | |
12620 | ||
8aceda64 AC |
12621 | Check := |
12622 | Expand_Composite_Equality (Nod, Etype (C), | |
12623 | Lhs => | |
12624 | Make_Selected_Component (Loc, | |
8d80ff64 | 12625 | Prefix => New_Lhs, |
e4494292 | 12626 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
12627 | Rhs => |
12628 | Make_Selected_Component (Loc, | |
8d80ff64 | 12629 | Prefix => New_Rhs, |
e4494292 | 12630 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
12631 | Bodies => Bodies); |
12632 | ||
12633 | -- If some (sub)component is an unchecked_union, the whole | |
12634 | -- operation will raise program error. | |
12635 | ||
12636 | if Nkind (Check) = N_Raise_Program_Error then | |
12637 | Result := Check; | |
12638 | Set_Etype (Result, Standard_Boolean); | |
12639 | exit; | |
12640 | else | |
63254915 AC |
12641 | if First_Time then |
12642 | Result := Check; | |
12643 | ||
12644 | -- Generate logical "and" for CodePeer to simplify the | |
12645 | -- generated code and analysis. | |
12646 | ||
12647 | elsif CodePeer_Mode then | |
12648 | Result := | |
12649 | Make_Op_And (Loc, | |
12650 | Left_Opnd => Result, | |
12651 | Right_Opnd => Check); | |
12652 | ||
12653 | else | |
12654 | Result := | |
12655 | Make_And_Then (Loc, | |
12656 | Left_Opnd => Result, | |
12657 | Right_Opnd => Check); | |
12658 | end if; | |
8aceda64 | 12659 | end if; |
70482933 RK |
12660 | end; |
12661 | ||
63254915 | 12662 | First_Time := False; |
6b670dcf | 12663 | C := Element_To_Compare (Next_Entity (C)); |
70482933 RK |
12664 | end loop; |
12665 | ||
12666 | return Result; | |
12667 | end Expand_Record_Equality; | |
12668 | ||
a3068ca6 AC |
12669 | --------------------------- |
12670 | -- Expand_Set_Membership -- | |
12671 | --------------------------- | |
12672 | ||
12673 | procedure Expand_Set_Membership (N : Node_Id) is | |
12674 | Lop : constant Node_Id := Left_Opnd (N); | |
12675 | Alt : Node_Id; | |
12676 | Res : Node_Id; | |
12677 | ||
12678 | function Make_Cond (Alt : Node_Id) return Node_Id; | |
12679 | -- If the alternative is a subtype mark, create a simple membership | |
12680 | -- test. Otherwise create an equality test for it. | |
12681 | ||
12682 | --------------- | |
12683 | -- Make_Cond -- | |
12684 | --------------- | |
12685 | ||
12686 | function Make_Cond (Alt : Node_Id) return Node_Id is | |
12687 | Cond : Node_Id; | |
afe9c539 | 12688 | L : constant Node_Id := New_Copy_Tree (Lop); |
a3068ca6 AC |
12689 | R : constant Node_Id := Relocate_Node (Alt); |
12690 | ||
12691 | begin | |
12692 | if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt))) | |
12693 | or else Nkind (Alt) = N_Range | |
12694 | then | |
12695 | Cond := | |
12696 | Make_In (Sloc (Alt), | |
12697 | Left_Opnd => L, | |
12698 | Right_Opnd => R); | |
12699 | else | |
12700 | Cond := | |
12701 | Make_Op_Eq (Sloc (Alt), | |
12702 | Left_Opnd => L, | |
12703 | Right_Opnd => R); | |
12704 | end if; | |
12705 | ||
12706 | return Cond; | |
12707 | end Make_Cond; | |
12708 | ||
12709 | -- Start of processing for Expand_Set_Membership | |
12710 | ||
12711 | begin | |
12712 | Remove_Side_Effects (Lop); | |
12713 | ||
12714 | Alt := Last (Alternatives (N)); | |
12715 | Res := Make_Cond (Alt); | |
12716 | ||
12717 | Prev (Alt); | |
12718 | while Present (Alt) loop | |
12719 | Res := | |
12720 | Make_Or_Else (Sloc (Alt), | |
12721 | Left_Opnd => Make_Cond (Alt), | |
12722 | Right_Opnd => Res); | |
12723 | Prev (Alt); | |
12724 | end loop; | |
12725 | ||
12726 | Rewrite (N, Res); | |
12727 | Analyze_And_Resolve (N, Standard_Boolean); | |
12728 | end Expand_Set_Membership; | |
12729 | ||
5875f8d6 AC |
12730 | ----------------------------------- |
12731 | -- Expand_Short_Circuit_Operator -- | |
12732 | ----------------------------------- | |
12733 | ||
955871d3 AC |
12734 | -- Deal with special expansion if actions are present for the right operand |
12735 | -- and deal with optimizing case of arguments being True or False. We also | |
12736 | -- deal with the special case of non-standard boolean values. | |
5875f8d6 AC |
12737 | |
12738 | procedure Expand_Short_Circuit_Operator (N : Node_Id) is | |
12739 | Loc : constant Source_Ptr := Sloc (N); | |
12740 | Typ : constant Entity_Id := Etype (N); | |
5875f8d6 AC |
12741 | Left : constant Node_Id := Left_Opnd (N); |
12742 | Right : constant Node_Id := Right_Opnd (N); | |
955871d3 | 12743 | LocR : constant Source_Ptr := Sloc (Right); |
5875f8d6 AC |
12744 | Actlist : List_Id; |
12745 | ||
12746 | Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else; | |
12747 | Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value); | |
12748 | -- If Left = Shortcut_Value then Right need not be evaluated | |
12749 | ||
f916243b AC |
12750 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id; |
12751 | -- For Opnd a boolean expression, return a Boolean expression equivalent | |
12752 | -- to Opnd /= Shortcut_Value. | |
12753 | ||
a0766a82 AC |
12754 | function Useful (Actions : List_Id) return Boolean; |
12755 | -- Return True if Actions is not empty and contains useful nodes to | |
12756 | -- process. | |
12757 | ||
f916243b AC |
12758 | -------------------- |
12759 | -- Make_Test_Expr -- | |
12760 | -------------------- | |
12761 | ||
12762 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id is | |
12763 | begin | |
12764 | if Shortcut_Value then | |
12765 | return Make_Op_Not (Sloc (Opnd), Opnd); | |
12766 | else | |
12767 | return Opnd; | |
12768 | end if; | |
12769 | end Make_Test_Expr; | |
12770 | ||
a0766a82 AC |
12771 | ------------ |
12772 | -- Useful -- | |
12773 | ------------ | |
12774 | ||
12775 | function Useful (Actions : List_Id) return Boolean is | |
12776 | L : Node_Id; | |
12777 | begin | |
12778 | if Present (Actions) then | |
12779 | L := First (Actions); | |
12780 | ||
12781 | -- For now "useful" means not N_Variable_Reference_Marker. | |
12782 | -- Consider stripping other nodes in the future. | |
12783 | ||
12784 | while Present (L) loop | |
12785 | if Nkind (L) /= N_Variable_Reference_Marker then | |
12786 | return True; | |
12787 | end if; | |
12788 | ||
12789 | Next (L); | |
12790 | end loop; | |
12791 | end if; | |
12792 | ||
12793 | return False; | |
12794 | end Useful; | |
12795 | ||
f916243b AC |
12796 | -- Local variables |
12797 | ||
12798 | Op_Var : Entity_Id; | |
12799 | -- Entity for a temporary variable holding the value of the operator, | |
12800 | -- used for expansion in the case where actions are present. | |
12801 | ||
12802 | -- Start of processing for Expand_Short_Circuit_Operator | |
12803 | ||
5875f8d6 AC |
12804 | begin |
12805 | -- Deal with non-standard booleans | |
12806 | ||
12807 | if Is_Boolean_Type (Typ) then | |
12808 | Adjust_Condition (Left); | |
12809 | Adjust_Condition (Right); | |
12810 | Set_Etype (N, Standard_Boolean); | |
12811 | end if; | |
12812 | ||
12813 | -- Check for cases where left argument is known to be True or False | |
12814 | ||
12815 | if Compile_Time_Known_Value (Left) then | |
25adc5fb AC |
12816 | |
12817 | -- Mark SCO for left condition as compile time known | |
12818 | ||
12819 | if Generate_SCO and then Comes_From_Source (Left) then | |
12820 | Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True); | |
12821 | end if; | |
12822 | ||
5875f8d6 AC |
12823 | -- Rewrite True AND THEN Right / False OR ELSE Right to Right. |
12824 | -- Any actions associated with Right will be executed unconditionally | |
12825 | -- and can thus be inserted into the tree unconditionally. | |
12826 | ||
12827 | if Expr_Value_E (Left) /= Shortcut_Ent then | |
12828 | if Present (Actions (N)) then | |
12829 | Insert_Actions (N, Actions (N)); | |
12830 | end if; | |
12831 | ||
12832 | Rewrite (N, Right); | |
12833 | ||
12834 | -- Rewrite False AND THEN Right / True OR ELSE Right to Left. | |
12835 | -- In this case we can forget the actions associated with Right, | |
12836 | -- since they will never be executed. | |
12837 | ||
12838 | else | |
12839 | Kill_Dead_Code (Right); | |
12840 | Kill_Dead_Code (Actions (N)); | |
12841 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
12842 | end if; | |
12843 | ||
12844 | Adjust_Result_Type (N, Typ); | |
12845 | return; | |
12846 | end if; | |
12847 | ||
955871d3 AC |
12848 | -- If Actions are present for the right operand, we have to do some |
12849 | -- special processing. We can't just let these actions filter back into | |
12850 | -- code preceding the short circuit (which is what would have happened | |
12851 | -- if we had not trapped them in the short-circuit form), since they | |
12852 | -- must only be executed if the right operand of the short circuit is | |
12853 | -- executed and not otherwise. | |
5875f8d6 | 12854 | |
a0766a82 | 12855 | if Useful (Actions (N)) then |
955871d3 | 12856 | Actlist := Actions (N); |
5875f8d6 | 12857 | |
f916243b AC |
12858 | -- The old approach is to expand: |
12859 | ||
12860 | -- left AND THEN right | |
12861 | ||
12862 | -- into | |
12863 | ||
12864 | -- C : Boolean := False; | |
12865 | -- IF left THEN | |
12866 | -- Actions; | |
12867 | -- IF right THEN | |
12868 | -- C := True; | |
12869 | -- END IF; | |
12870 | -- END IF; | |
12871 | ||
12872 | -- and finally rewrite the operator into a reference to C. Similarly | |
12873 | -- for left OR ELSE right, with negated values. Note that this | |
12874 | -- rewrite causes some difficulties for coverage analysis because | |
12875 | -- of the introduction of the new variable C, which obscures the | |
12876 | -- structure of the test. | |
12877 | ||
12878 | -- We use this "old approach" if Minimize_Expression_With_Actions | |
12879 | -- is True. | |
12880 | ||
12881 | if Minimize_Expression_With_Actions then | |
12882 | Op_Var := Make_Temporary (Loc, 'C', Related_Node => N); | |
12883 | ||
12884 | Insert_Action (N, | |
12885 | Make_Object_Declaration (Loc, | |
12886 | Defining_Identifier => Op_Var, | |
12887 | Object_Definition => | |
12888 | New_Occurrence_Of (Standard_Boolean, Loc), | |
12889 | Expression => | |
12890 | New_Occurrence_Of (Shortcut_Ent, Loc))); | |
12891 | ||
12892 | Append_To (Actlist, | |
12893 | Make_Implicit_If_Statement (Right, | |
12894 | Condition => Make_Test_Expr (Right), | |
12895 | Then_Statements => New_List ( | |
12896 | Make_Assignment_Statement (LocR, | |
12897 | Name => New_Occurrence_Of (Op_Var, LocR), | |
12898 | Expression => | |
12899 | New_Occurrence_Of | |
12900 | (Boolean_Literals (not Shortcut_Value), LocR))))); | |
12901 | ||
12902 | Insert_Action (N, | |
12903 | Make_Implicit_If_Statement (Left, | |
12904 | Condition => Make_Test_Expr (Left), | |
12905 | Then_Statements => Actlist)); | |
12906 | ||
12907 | Rewrite (N, New_Occurrence_Of (Op_Var, Loc)); | |
12908 | Analyze_And_Resolve (N, Standard_Boolean); | |
12909 | ||
12910 | -- The new approach (the default) is to use an | |
12911 | -- Expression_With_Actions node for the right operand of the | |
12912 | -- short-circuit form. Note that this solves the traceability | |
0812b84e | 12913 | -- problems for coverage analysis. |
5875f8d6 | 12914 | |
f916243b AC |
12915 | else |
12916 | Rewrite (Right, | |
12917 | Make_Expression_With_Actions (LocR, | |
12918 | Expression => Relocate_Node (Right), | |
12919 | Actions => Actlist)); | |
4b17187f | 12920 | |
f916243b AC |
12921 | Set_Actions (N, No_List); |
12922 | Analyze_And_Resolve (Right, Standard_Boolean); | |
12923 | end if; | |
955871d3 | 12924 | |
5875f8d6 AC |
12925 | Adjust_Result_Type (N, Typ); |
12926 | return; | |
12927 | end if; | |
12928 | ||
12929 | -- No actions present, check for cases of right argument True/False | |
12930 | ||
12931 | if Compile_Time_Known_Value (Right) then | |
25adc5fb AC |
12932 | |
12933 | -- Mark SCO for left condition as compile time known | |
12934 | ||
12935 | if Generate_SCO and then Comes_From_Source (Right) then | |
12936 | Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True); | |
12937 | end if; | |
12938 | ||
f916243b AC |
12939 | -- Change (Left and then True), (Left or else False) to Left. Note |
12940 | -- that we know there are no actions associated with the right | |
5875f8d6 AC |
12941 | -- operand, since we just checked for this case above. |
12942 | ||
12943 | if Expr_Value_E (Right) /= Shortcut_Ent then | |
12944 | Rewrite (N, Left); | |
12945 | ||
12946 | -- Change (Left and then False), (Left or else True) to Right, | |
12947 | -- making sure to preserve any side effects associated with the Left | |
12948 | -- operand. | |
12949 | ||
12950 | else | |
12951 | Remove_Side_Effects (Left); | |
12952 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
12953 | end if; | |
12954 | end if; | |
12955 | ||
12956 | Adjust_Result_Type (N, Typ); | |
12957 | end Expand_Short_Circuit_Operator; | |
12958 | ||
bdbb2a40 | 12959 | ------------------------------------ |
70482933 RK |
12960 | -- Fixup_Universal_Fixed_Operation -- |
12961 | ------------------------------------- | |
12962 | ||
12963 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
12964 | Conv : constant Node_Id := Parent (N); | |
12965 | ||
12966 | begin | |
12967 | -- We must have a type conversion immediately above us | |
12968 | ||
12969 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
12970 | ||
12971 | -- Normally the type conversion gives our target type. The exception | |
12972 | -- occurs in the case of the Round attribute, where the conversion | |
12973 | -- will be to universal real, and our real type comes from the Round | |
12974 | -- attribute (as well as an indication that we must round the result) | |
12975 | ||
12976 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
12977 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
12978 | then | |
267c7ff6 | 12979 | Set_Etype (N, Base_Type (Etype (Parent (Conv)))); |
70482933 RK |
12980 | Set_Rounded_Result (N); |
12981 | ||
12982 | -- Normal case where type comes from conversion above us | |
12983 | ||
12984 | else | |
267c7ff6 | 12985 | Set_Etype (N, Base_Type (Etype (Conv))); |
70482933 RK |
12986 | end if; |
12987 | end Fixup_Universal_Fixed_Operation; | |
12988 | ||
5d09245e AC |
12989 | --------------------------------- |
12990 | -- Has_Inferable_Discriminants -- | |
12991 | --------------------------------- | |
12992 | ||
12993 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
12994 | ||
12995 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
12996 | -- Determines whether the left-most prefix of a selected component is a | |
12997 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
12998 | ||
12999 | -------------------------------- | |
13000 | -- Prefix_Is_Formal_Parameter -- | |
13001 | -------------------------------- | |
13002 | ||
13003 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
83bb90af | 13004 | Sel_Comp : Node_Id; |
5d09245e AC |
13005 | |
13006 | begin | |
13007 | -- Move to the left-most prefix by climbing up the tree | |
13008 | ||
83bb90af | 13009 | Sel_Comp := N; |
5d09245e AC |
13010 | while Present (Parent (Sel_Comp)) |
13011 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
13012 | loop | |
13013 | Sel_Comp := Parent (Sel_Comp); | |
13014 | end loop; | |
13015 | ||
bb6a856b | 13016 | return Is_Formal (Entity (Prefix (Sel_Comp))); |
5d09245e AC |
13017 | end Prefix_Is_Formal_Parameter; |
13018 | ||
13019 | -- Start of processing for Has_Inferable_Discriminants | |
13020 | ||
13021 | begin | |
5d09245e AC |
13022 | -- For selected components, the subtype of the selector must be a |
13023 | -- constrained Unchecked_Union. If the component is subject to a | |
13024 | -- per-object constraint, then the enclosing object must have inferable | |
13025 | -- discriminants. | |
13026 | ||
83bb90af | 13027 | if Nkind (N) = N_Selected_Component then |
5d09245e AC |
13028 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then |
13029 | ||
13030 | -- A small hack. If we have a per-object constrained selected | |
13031 | -- component of a formal parameter, return True since we do not | |
13032 | -- know the actual parameter association yet. | |
13033 | ||
13034 | if Prefix_Is_Formal_Parameter (N) then | |
13035 | return True; | |
5d09245e AC |
13036 | |
13037 | -- Otherwise, check the enclosing object and the selector | |
13038 | ||
83bb90af TQ |
13039 | else |
13040 | return Has_Inferable_Discriminants (Prefix (N)) | |
13041 | and then Has_Inferable_Discriminants (Selector_Name (N)); | |
13042 | end if; | |
5d09245e AC |
13043 | |
13044 | -- The call to Has_Inferable_Discriminants will determine whether | |
13045 | -- the selector has a constrained Unchecked_Union nominal type. | |
13046 | ||
83bb90af TQ |
13047 | else |
13048 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
13049 | end if; | |
5d09245e AC |
13050 | |
13051 | -- A qualified expression has inferable discriminants if its subtype | |
13052 | -- mark is a constrained Unchecked_Union subtype. | |
13053 | ||
13054 | elsif Nkind (N) = N_Qualified_Expression then | |
053cf994 | 13055 | return Is_Unchecked_Union (Etype (Subtype_Mark (N))) |
5b5b27ad | 13056 | and then Is_Constrained (Etype (Subtype_Mark (N))); |
5d09245e | 13057 | |
83bb90af TQ |
13058 | -- For all other names, it is sufficient to have a constrained |
13059 | -- Unchecked_Union nominal subtype. | |
13060 | ||
13061 | else | |
13062 | return Is_Unchecked_Union (Base_Type (Etype (N))) | |
13063 | and then Is_Constrained (Etype (N)); | |
13064 | end if; | |
5d09245e AC |
13065 | end Has_Inferable_Discriminants; |
13066 | ||
70482933 RK |
13067 | ------------------------------- |
13068 | -- Insert_Dereference_Action -- | |
13069 | ------------------------------- | |
13070 | ||
13071 | procedure Insert_Dereference_Action (N : Node_Id) is | |
70482933 | 13072 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; |
2e071734 AC |
13073 | -- Return true if type of P is derived from Checked_Pool; |
13074 | ||
13075 | ----------------------------- | |
13076 | -- Is_Checked_Storage_Pool -- | |
13077 | ----------------------------- | |
70482933 RK |
13078 | |
13079 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
13080 | T : Entity_Id; | |
761f7dcb | 13081 | |
70482933 RK |
13082 | begin |
13083 | if No (P) then | |
13084 | return False; | |
13085 | end if; | |
13086 | ||
13087 | T := Etype (P); | |
13088 | while T /= Etype (T) loop | |
13089 | if Is_RTE (T, RE_Checked_Pool) then | |
13090 | return True; | |
13091 | else | |
13092 | T := Etype (T); | |
13093 | end if; | |
13094 | end loop; | |
13095 | ||
13096 | return False; | |
13097 | end Is_Checked_Storage_Pool; | |
13098 | ||
b0d71355 HK |
13099 | -- Local variables |
13100 | ||
bb9e2aa2 AC |
13101 | Context : constant Node_Id := Parent (N); |
13102 | Ptr_Typ : constant Entity_Id := Etype (N); | |
13103 | Desig_Typ : constant Entity_Id := | |
13104 | Available_View (Designated_Type (Ptr_Typ)); | |
13105 | Loc : constant Source_Ptr := Sloc (N); | |
13106 | Pool : constant Entity_Id := Associated_Storage_Pool (Ptr_Typ); | |
b0d71355 | 13107 | |
51dcceec AC |
13108 | Addr : Entity_Id; |
13109 | Alig : Entity_Id; | |
13110 | Deref : Node_Id; | |
13111 | Size : Entity_Id; | |
13112 | Size_Bits : Node_Id; | |
13113 | Stmt : Node_Id; | |
b0d71355 | 13114 | |
70482933 RK |
13115 | -- Start of processing for Insert_Dereference_Action |
13116 | ||
13117 | begin | |
bb9e2aa2 | 13118 | pragma Assert (Nkind (Context) = N_Explicit_Dereference); |
e6f69614 | 13119 | |
b0d71355 HK |
13120 | -- Do not re-expand a dereference which has already been processed by |
13121 | -- this routine. | |
13122 | ||
bb9e2aa2 | 13123 | if Has_Dereference_Action (Context) then |
70482933 | 13124 | return; |
70482933 | 13125 | |
b0d71355 HK |
13126 | -- Do not perform this type of expansion for internally-generated |
13127 | -- dereferences. | |
70482933 | 13128 | |
bb9e2aa2 | 13129 | elsif not Comes_From_Source (Original_Node (Context)) then |
b0d71355 | 13130 | return; |
70482933 | 13131 | |
b0d71355 HK |
13132 | -- A dereference action is only applicable to objects which have been |
13133 | -- allocated on a checked pool. | |
70482933 | 13134 | |
b0d71355 HK |
13135 | elsif not Is_Checked_Storage_Pool (Pool) then |
13136 | return; | |
13137 | end if; | |
70482933 | 13138 | |
b0d71355 | 13139 | -- Extract the address of the dereferenced object. Generate: |
8777c5a6 | 13140 | |
b0d71355 | 13141 | -- Addr : System.Address := <N>'Pool_Address; |
70482933 | 13142 | |
b0d71355 | 13143 | Addr := Make_Temporary (Loc, 'P'); |
70482933 | 13144 | |
b0d71355 HK |
13145 | Insert_Action (N, |
13146 | Make_Object_Declaration (Loc, | |
13147 | Defining_Identifier => Addr, | |
13148 | Object_Definition => | |
e4494292 | 13149 | New_Occurrence_Of (RTE (RE_Address), Loc), |
b0d71355 HK |
13150 | Expression => |
13151 | Make_Attribute_Reference (Loc, | |
13152 | Prefix => Duplicate_Subexpr_Move_Checks (N), | |
13153 | Attribute_Name => Name_Pool_Address))); | |
13154 | ||
13155 | -- Calculate the size of the dereferenced object. Generate: | |
8777c5a6 | 13156 | |
b0d71355 HK |
13157 | -- Size : Storage_Count := <N>.all'Size / Storage_Unit; |
13158 | ||
13159 | Deref := | |
13160 | Make_Explicit_Dereference (Loc, | |
13161 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
13162 | Set_Has_Dereference_Action (Deref); | |
70482933 | 13163 | |
51dcceec AC |
13164 | Size_Bits := |
13165 | Make_Attribute_Reference (Loc, | |
13166 | Prefix => Deref, | |
13167 | Attribute_Name => Name_Size); | |
13168 | ||
13169 | -- Special case of an unconstrained array: need to add descriptor size | |
13170 | ||
bb9e2aa2 AC |
13171 | if Is_Array_Type (Desig_Typ) |
13172 | and then not Is_Constrained (First_Subtype (Desig_Typ)) | |
51dcceec AC |
13173 | then |
13174 | Size_Bits := | |
13175 | Make_Op_Add (Loc, | |
13176 | Left_Opnd => | |
13177 | Make_Attribute_Reference (Loc, | |
13178 | Prefix => | |
bb9e2aa2 | 13179 | New_Occurrence_Of (First_Subtype (Desig_Typ), Loc), |
51dcceec AC |
13180 | Attribute_Name => Name_Descriptor_Size), |
13181 | Right_Opnd => Size_Bits); | |
13182 | end if; | |
b0d71355 | 13183 | |
51dcceec | 13184 | Size := Make_Temporary (Loc, 'S'); |
b0d71355 HK |
13185 | Insert_Action (N, |
13186 | Make_Object_Declaration (Loc, | |
13187 | Defining_Identifier => Size, | |
13188 | Object_Definition => | |
e4494292 | 13189 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
b0d71355 HK |
13190 | Expression => |
13191 | Make_Op_Divide (Loc, | |
51dcceec AC |
13192 | Left_Opnd => Size_Bits, |
13193 | Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit)))); | |
70482933 | 13194 | |
b0d71355 HK |
13195 | -- Calculate the alignment of the dereferenced object. Generate: |
13196 | -- Alig : constant Storage_Count := <N>.all'Alignment; | |
70482933 | 13197 | |
b0d71355 HK |
13198 | Deref := |
13199 | Make_Explicit_Dereference (Loc, | |
13200 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
13201 | Set_Has_Dereference_Action (Deref); | |
13202 | ||
13203 | Alig := Make_Temporary (Loc, 'A'); | |
b0d71355 HK |
13204 | Insert_Action (N, |
13205 | Make_Object_Declaration (Loc, | |
13206 | Defining_Identifier => Alig, | |
13207 | Object_Definition => | |
e4494292 | 13208 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
b0d71355 HK |
13209 | Expression => |
13210 | Make_Attribute_Reference (Loc, | |
13211 | Prefix => Deref, | |
13212 | Attribute_Name => Name_Alignment))); | |
13213 | ||
13214 | -- A dereference of a controlled object requires special processing. The | |
13215 | -- finalization machinery requests additional space from the underlying | |
13216 | -- pool to allocate and hide two pointers. As a result, a checked pool | |
13217 | -- may mark the wrong memory as valid. Since checked pools do not have | |
13218 | -- knowledge of hidden pointers, we have to bring the two pointers back | |
13219 | -- in view in order to restore the original state of the object. | |
13220 | ||
bb9e2aa2 AC |
13221 | -- The address manipulation is not performed for access types that are |
13222 | -- subject to pragma No_Heap_Finalization because the two pointers do | |
13223 | -- not exist in the first place. | |
13224 | ||
13225 | if No_Heap_Finalization (Ptr_Typ) then | |
13226 | null; | |
13227 | ||
13228 | elsif Needs_Finalization (Desig_Typ) then | |
b0d71355 HK |
13229 | |
13230 | -- Adjust the address and size of the dereferenced object. Generate: | |
13231 | -- Adjust_Controlled_Dereference (Addr, Size, Alig); | |
13232 | ||
13233 | Stmt := | |
13234 | Make_Procedure_Call_Statement (Loc, | |
13235 | Name => | |
e4494292 | 13236 | New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc), |
b0d71355 | 13237 | Parameter_Associations => New_List ( |
e4494292 RD |
13238 | New_Occurrence_Of (Addr, Loc), |
13239 | New_Occurrence_Of (Size, Loc), | |
13240 | New_Occurrence_Of (Alig, Loc))); | |
b0d71355 HK |
13241 | |
13242 | -- Class-wide types complicate things because we cannot determine | |
13243 | -- statically whether the actual object is truly controlled. We must | |
13244 | -- generate a runtime check to detect this property. Generate: | |
13245 | -- | |
13246 | -- if Needs_Finalization (<N>.all'Tag) then | |
13247 | -- <Stmt>; | |
13248 | -- end if; | |
13249 | ||
bb9e2aa2 | 13250 | if Is_Class_Wide_Type (Desig_Typ) then |
b0d71355 HK |
13251 | Deref := |
13252 | Make_Explicit_Dereference (Loc, | |
13253 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
13254 | Set_Has_Dereference_Action (Deref); | |
13255 | ||
13256 | Stmt := | |
8b1011c0 | 13257 | Make_Implicit_If_Statement (N, |
b0d71355 HK |
13258 | Condition => |
13259 | Make_Function_Call (Loc, | |
13260 | Name => | |
e4494292 | 13261 | New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc), |
b0d71355 HK |
13262 | Parameter_Associations => New_List ( |
13263 | Make_Attribute_Reference (Loc, | |
13264 | Prefix => Deref, | |
13265 | Attribute_Name => Name_Tag))), | |
13266 | Then_Statements => New_List (Stmt)); | |
13267 | end if; | |
13268 | ||
13269 | Insert_Action (N, Stmt); | |
13270 | end if; | |
13271 | ||
13272 | -- Generate: | |
13273 | -- Dereference (Pool, Addr, Size, Alig); | |
13274 | ||
13275 | Insert_Action (N, | |
13276 | Make_Procedure_Call_Statement (Loc, | |
13277 | Name => | |
e4494292 | 13278 | New_Occurrence_Of |
b0d71355 HK |
13279 | (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), |
13280 | Parameter_Associations => New_List ( | |
e4494292 RD |
13281 | New_Occurrence_Of (Pool, Loc), |
13282 | New_Occurrence_Of (Addr, Loc), | |
13283 | New_Occurrence_Of (Size, Loc), | |
13284 | New_Occurrence_Of (Alig, Loc)))); | |
b0d71355 HK |
13285 | |
13286 | -- Mark the explicit dereference as processed to avoid potential | |
13287 | -- infinite expansion. | |
13288 | ||
bb9e2aa2 | 13289 | Set_Has_Dereference_Action (Context); |
70482933 | 13290 | |
fbf5a39b AC |
13291 | exception |
13292 | when RE_Not_Available => | |
13293 | return; | |
70482933 RK |
13294 | end Insert_Dereference_Action; |
13295 | ||
fdfcc663 AC |
13296 | -------------------------------- |
13297 | -- Integer_Promotion_Possible -- | |
13298 | -------------------------------- | |
13299 | ||
13300 | function Integer_Promotion_Possible (N : Node_Id) return Boolean is | |
13301 | Operand : constant Node_Id := Expression (N); | |
13302 | Operand_Type : constant Entity_Id := Etype (Operand); | |
13303 | Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type); | |
13304 | ||
13305 | begin | |
13306 | pragma Assert (Nkind (N) = N_Type_Conversion); | |
13307 | ||
13308 | return | |
13309 | ||
13310 | -- We only do the transformation for source constructs. We assume | |
13311 | -- that the expander knows what it is doing when it generates code. | |
13312 | ||
13313 | Comes_From_Source (N) | |
13314 | ||
13315 | -- If the operand type is Short_Integer or Short_Short_Integer, | |
13316 | -- then we will promote to Integer, which is available on all | |
13317 | -- targets, and is sufficient to ensure no intermediate overflow. | |
13318 | -- Furthermore it is likely to be as efficient or more efficient | |
13319 | -- than using the smaller type for the computation so we do this | |
13320 | -- unconditionally. | |
13321 | ||
13322 | and then | |
13323 | (Root_Operand_Type = Base_Type (Standard_Short_Integer) | |
761f7dcb | 13324 | or else |
fdfcc663 AC |
13325 | Root_Operand_Type = Base_Type (Standard_Short_Short_Integer)) |
13326 | ||
13327 | -- Test for interesting operation, which includes addition, | |
5f3f175d AC |
13328 | -- division, exponentiation, multiplication, subtraction, absolute |
13329 | -- value and unary negation. Unary "+" is omitted since it is a | |
13330 | -- no-op and thus can't overflow. | |
fdfcc663 | 13331 | |
5f3f175d AC |
13332 | and then Nkind_In (Operand, N_Op_Abs, |
13333 | N_Op_Add, | |
fdfcc663 AC |
13334 | N_Op_Divide, |
13335 | N_Op_Expon, | |
13336 | N_Op_Minus, | |
13337 | N_Op_Multiply, | |
13338 | N_Op_Subtract); | |
13339 | end Integer_Promotion_Possible; | |
13340 | ||
70482933 RK |
13341 | ------------------------------ |
13342 | -- Make_Array_Comparison_Op -- | |
13343 | ------------------------------ | |
13344 | ||
13345 | -- This is a hand-coded expansion of the following generic function: | |
13346 | ||
13347 | -- generic | |
13348 | -- type elem is (<>); | |
13349 | -- type index is (<>); | |
13350 | -- type a is array (index range <>) of elem; | |
20b5d666 | 13351 | |
70482933 RK |
13352 | -- function Gnnn (X : a; Y: a) return boolean is |
13353 | -- J : index := Y'first; | |
20b5d666 | 13354 | |
70482933 RK |
13355 | -- begin |
13356 | -- if X'length = 0 then | |
13357 | -- return false; | |
20b5d666 | 13358 | |
70482933 RK |
13359 | -- elsif Y'length = 0 then |
13360 | -- return true; | |
20b5d666 | 13361 | |
70482933 RK |
13362 | -- else |
13363 | -- for I in X'range loop | |
13364 | -- if X (I) = Y (J) then | |
13365 | -- if J = Y'last then | |
13366 | -- exit; | |
13367 | -- else | |
13368 | -- J := index'succ (J); | |
13369 | -- end if; | |
20b5d666 | 13370 | |
70482933 RK |
13371 | -- else |
13372 | -- return X (I) > Y (J); | |
13373 | -- end if; | |
13374 | -- end loop; | |
20b5d666 | 13375 | |
70482933 RK |
13376 | -- return X'length > Y'length; |
13377 | -- end if; | |
13378 | -- end Gnnn; | |
13379 | ||
13380 | -- Note that since we are essentially doing this expansion by hand, we | |
13381 | -- do not need to generate an actual or formal generic part, just the | |
13382 | -- instantiated function itself. | |
13383 | ||
bb012790 AC |
13384 | -- Perhaps we could have the actual generic available in the run-time, |
13385 | -- obtained by rtsfind, and actually expand a real instantiation ??? | |
13386 | ||
70482933 | 13387 | function Make_Array_Comparison_Op |
2e071734 AC |
13388 | (Typ : Entity_Id; |
13389 | Nod : Node_Id) return Node_Id | |
70482933 RK |
13390 | is |
13391 | Loc : constant Source_Ptr := Sloc (Nod); | |
13392 | ||
13393 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
13394 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
13395 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
13396 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
13397 | ||
13398 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
13399 | ||
13400 | Loop_Statement : Node_Id; | |
13401 | Loop_Body : Node_Id; | |
13402 | If_Stat : Node_Id; | |
13403 | Inner_If : Node_Id; | |
13404 | Final_Expr : Node_Id; | |
13405 | Func_Body : Node_Id; | |
13406 | Func_Name : Entity_Id; | |
13407 | Formals : List_Id; | |
13408 | Length1 : Node_Id; | |
13409 | Length2 : Node_Id; | |
13410 | ||
13411 | begin | |
13412 | -- if J = Y'last then | |
13413 | -- exit; | |
13414 | -- else | |
13415 | -- J := index'succ (J); | |
13416 | -- end if; | |
13417 | ||
13418 | Inner_If := | |
13419 | Make_Implicit_If_Statement (Nod, | |
13420 | Condition => | |
13421 | Make_Op_Eq (Loc, | |
e4494292 | 13422 | Left_Opnd => New_Occurrence_Of (J, Loc), |
70482933 RK |
13423 | Right_Opnd => |
13424 | Make_Attribute_Reference (Loc, | |
e4494292 | 13425 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
13426 | Attribute_Name => Name_Last)), |
13427 | ||
13428 | Then_Statements => New_List ( | |
13429 | Make_Exit_Statement (Loc)), | |
13430 | ||
13431 | Else_Statements => | |
13432 | New_List ( | |
13433 | Make_Assignment_Statement (Loc, | |
e4494292 | 13434 | Name => New_Occurrence_Of (J, Loc), |
70482933 RK |
13435 | Expression => |
13436 | Make_Attribute_Reference (Loc, | |
e4494292 | 13437 | Prefix => New_Occurrence_Of (Index, Loc), |
70482933 | 13438 | Attribute_Name => Name_Succ, |
e4494292 | 13439 | Expressions => New_List (New_Occurrence_Of (J, Loc)))))); |
70482933 RK |
13440 | |
13441 | -- if X (I) = Y (J) then | |
13442 | -- if ... end if; | |
13443 | -- else | |
13444 | -- return X (I) > Y (J); | |
13445 | -- end if; | |
13446 | ||
13447 | Loop_Body := | |
13448 | Make_Implicit_If_Statement (Nod, | |
13449 | Condition => | |
13450 | Make_Op_Eq (Loc, | |
13451 | Left_Opnd => | |
13452 | Make_Indexed_Component (Loc, | |
e4494292 RD |
13453 | Prefix => New_Occurrence_Of (X, Loc), |
13454 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
13455 | |
13456 | Right_Opnd => | |
13457 | Make_Indexed_Component (Loc, | |
e4494292 RD |
13458 | Prefix => New_Occurrence_Of (Y, Loc), |
13459 | Expressions => New_List (New_Occurrence_Of (J, Loc)))), | |
70482933 RK |
13460 | |
13461 | Then_Statements => New_List (Inner_If), | |
13462 | ||
13463 | Else_Statements => New_List ( | |
d766cee3 | 13464 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
13465 | Expression => |
13466 | Make_Op_Gt (Loc, | |
13467 | Left_Opnd => | |
13468 | Make_Indexed_Component (Loc, | |
e4494292 RD |
13469 | Prefix => New_Occurrence_Of (X, Loc), |
13470 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
13471 | |
13472 | Right_Opnd => | |
13473 | Make_Indexed_Component (Loc, | |
e4494292 | 13474 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 | 13475 | Expressions => New_List ( |
e4494292 | 13476 | New_Occurrence_Of (J, Loc))))))); |
70482933 RK |
13477 | |
13478 | -- for I in X'range loop | |
13479 | -- if ... end if; | |
13480 | -- end loop; | |
13481 | ||
13482 | Loop_Statement := | |
13483 | Make_Implicit_Loop_Statement (Nod, | |
13484 | Identifier => Empty, | |
13485 | ||
13486 | Iteration_Scheme => | |
13487 | Make_Iteration_Scheme (Loc, | |
13488 | Loop_Parameter_Specification => | |
13489 | Make_Loop_Parameter_Specification (Loc, | |
13490 | Defining_Identifier => I, | |
13491 | Discrete_Subtype_Definition => | |
13492 | Make_Attribute_Reference (Loc, | |
e4494292 | 13493 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
13494 | Attribute_Name => Name_Range))), |
13495 | ||
13496 | Statements => New_List (Loop_Body)); | |
13497 | ||
13498 | -- if X'length = 0 then | |
13499 | -- return false; | |
13500 | -- elsif Y'length = 0 then | |
13501 | -- return true; | |
13502 | -- else | |
13503 | -- for ... loop ... end loop; | |
13504 | -- return X'length > Y'length; | |
13505 | -- end if; | |
13506 | ||
13507 | Length1 := | |
13508 | Make_Attribute_Reference (Loc, | |
e4494292 | 13509 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
13510 | Attribute_Name => Name_Length); |
13511 | ||
13512 | Length2 := | |
13513 | Make_Attribute_Reference (Loc, | |
e4494292 | 13514 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
13515 | Attribute_Name => Name_Length); |
13516 | ||
13517 | Final_Expr := | |
13518 | Make_Op_Gt (Loc, | |
13519 | Left_Opnd => Length1, | |
13520 | Right_Opnd => Length2); | |
13521 | ||
13522 | If_Stat := | |
13523 | Make_Implicit_If_Statement (Nod, | |
13524 | Condition => | |
13525 | Make_Op_Eq (Loc, | |
13526 | Left_Opnd => | |
13527 | Make_Attribute_Reference (Loc, | |
e4494292 | 13528 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
13529 | Attribute_Name => Name_Length), |
13530 | Right_Opnd => | |
13531 | Make_Integer_Literal (Loc, 0)), | |
13532 | ||
13533 | Then_Statements => | |
13534 | New_List ( | |
d766cee3 | 13535 | Make_Simple_Return_Statement (Loc, |
e4494292 | 13536 | Expression => New_Occurrence_Of (Standard_False, Loc))), |
70482933 RK |
13537 | |
13538 | Elsif_Parts => New_List ( | |
13539 | Make_Elsif_Part (Loc, | |
13540 | Condition => | |
13541 | Make_Op_Eq (Loc, | |
13542 | Left_Opnd => | |
13543 | Make_Attribute_Reference (Loc, | |
e4494292 | 13544 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
13545 | Attribute_Name => Name_Length), |
13546 | Right_Opnd => | |
13547 | Make_Integer_Literal (Loc, 0)), | |
13548 | ||
13549 | Then_Statements => | |
13550 | New_List ( | |
d766cee3 | 13551 | Make_Simple_Return_Statement (Loc, |
e4494292 | 13552 | Expression => New_Occurrence_Of (Standard_True, Loc))))), |
70482933 RK |
13553 | |
13554 | Else_Statements => New_List ( | |
13555 | Loop_Statement, | |
d766cee3 | 13556 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
13557 | Expression => Final_Expr))); |
13558 | ||
13559 | -- (X : a; Y: a) | |
13560 | ||
13561 | Formals := New_List ( | |
13562 | Make_Parameter_Specification (Loc, | |
13563 | Defining_Identifier => X, | |
e4494292 | 13564 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
13565 | |
13566 | Make_Parameter_Specification (Loc, | |
13567 | Defining_Identifier => Y, | |
e4494292 | 13568 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 RK |
13569 | |
13570 | -- function Gnnn (...) return boolean is | |
13571 | -- J : index := Y'first; | |
13572 | -- begin | |
13573 | -- if ... end if; | |
13574 | -- end Gnnn; | |
13575 | ||
191fcb3a | 13576 | Func_Name := Make_Temporary (Loc, 'G'); |
70482933 RK |
13577 | |
13578 | Func_Body := | |
13579 | Make_Subprogram_Body (Loc, | |
13580 | Specification => | |
13581 | Make_Function_Specification (Loc, | |
13582 | Defining_Unit_Name => Func_Name, | |
13583 | Parameter_Specifications => Formals, | |
e4494292 | 13584 | Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)), |
70482933 RK |
13585 | |
13586 | Declarations => New_List ( | |
13587 | Make_Object_Declaration (Loc, | |
13588 | Defining_Identifier => J, | |
e4494292 | 13589 | Object_Definition => New_Occurrence_Of (Index, Loc), |
70482933 RK |
13590 | Expression => |
13591 | Make_Attribute_Reference (Loc, | |
e4494292 | 13592 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
13593 | Attribute_Name => Name_First))), |
13594 | ||
13595 | Handled_Statement_Sequence => | |
13596 | Make_Handled_Sequence_Of_Statements (Loc, | |
13597 | Statements => New_List (If_Stat))); | |
13598 | ||
13599 | return Func_Body; | |
70482933 RK |
13600 | end Make_Array_Comparison_Op; |
13601 | ||
13602 | --------------------------- | |
13603 | -- Make_Boolean_Array_Op -- | |
13604 | --------------------------- | |
13605 | ||
685094bf RD |
13606 | -- For logical operations on boolean arrays, expand in line the following, |
13607 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
13608 | |
13609 | -- function Annn (A : typ; B: typ) return typ is | |
13610 | -- C : typ; | |
13611 | -- begin | |
13612 | -- for J in A'range loop | |
13613 | -- C (J) := A (J) op B (J); | |
13614 | -- end loop; | |
13615 | -- return C; | |
13616 | -- end Annn; | |
13617 | ||
13618 | -- Here typ is the boolean array type | |
13619 | ||
13620 | function Make_Boolean_Array_Op | |
2e071734 AC |
13621 | (Typ : Entity_Id; |
13622 | N : Node_Id) return Node_Id | |
70482933 RK |
13623 | is |
13624 | Loc : constant Source_Ptr := Sloc (N); | |
13625 | ||
13626 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
13627 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
13628 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
13629 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
13630 | ||
13631 | A_J : Node_Id; | |
13632 | B_J : Node_Id; | |
13633 | C_J : Node_Id; | |
13634 | Op : Node_Id; | |
13635 | ||
13636 | Formals : List_Id; | |
13637 | Func_Name : Entity_Id; | |
13638 | Func_Body : Node_Id; | |
13639 | Loop_Statement : Node_Id; | |
13640 | ||
13641 | begin | |
13642 | A_J := | |
13643 | Make_Indexed_Component (Loc, | |
e4494292 RD |
13644 | Prefix => New_Occurrence_Of (A, Loc), |
13645 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
13646 | |
13647 | B_J := | |
13648 | Make_Indexed_Component (Loc, | |
e4494292 RD |
13649 | Prefix => New_Occurrence_Of (B, Loc), |
13650 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
13651 | |
13652 | C_J := | |
13653 | Make_Indexed_Component (Loc, | |
e4494292 RD |
13654 | Prefix => New_Occurrence_Of (C, Loc), |
13655 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
13656 | |
13657 | if Nkind (N) = N_Op_And then | |
13658 | Op := | |
13659 | Make_Op_And (Loc, | |
13660 | Left_Opnd => A_J, | |
13661 | Right_Opnd => B_J); | |
13662 | ||
13663 | elsif Nkind (N) = N_Op_Or then | |
13664 | Op := | |
13665 | Make_Op_Or (Loc, | |
13666 | Left_Opnd => A_J, | |
13667 | Right_Opnd => B_J); | |
13668 | ||
13669 | else | |
13670 | Op := | |
13671 | Make_Op_Xor (Loc, | |
13672 | Left_Opnd => A_J, | |
13673 | Right_Opnd => B_J); | |
13674 | end if; | |
13675 | ||
13676 | Loop_Statement := | |
13677 | Make_Implicit_Loop_Statement (N, | |
13678 | Identifier => Empty, | |
13679 | ||
13680 | Iteration_Scheme => | |
13681 | Make_Iteration_Scheme (Loc, | |
13682 | Loop_Parameter_Specification => | |
13683 | Make_Loop_Parameter_Specification (Loc, | |
13684 | Defining_Identifier => J, | |
13685 | Discrete_Subtype_Definition => | |
13686 | Make_Attribute_Reference (Loc, | |
e4494292 | 13687 | Prefix => New_Occurrence_Of (A, Loc), |
70482933 RK |
13688 | Attribute_Name => Name_Range))), |
13689 | ||
13690 | Statements => New_List ( | |
13691 | Make_Assignment_Statement (Loc, | |
13692 | Name => C_J, | |
13693 | Expression => Op))); | |
13694 | ||
13695 | Formals := New_List ( | |
13696 | Make_Parameter_Specification (Loc, | |
13697 | Defining_Identifier => A, | |
e4494292 | 13698 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
13699 | |
13700 | Make_Parameter_Specification (Loc, | |
13701 | Defining_Identifier => B, | |
e4494292 | 13702 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 | 13703 | |
191fcb3a | 13704 | Func_Name := Make_Temporary (Loc, 'A'); |
70482933 RK |
13705 | Set_Is_Inlined (Func_Name); |
13706 | ||
13707 | Func_Body := | |
13708 | Make_Subprogram_Body (Loc, | |
13709 | Specification => | |
13710 | Make_Function_Specification (Loc, | |
13711 | Defining_Unit_Name => Func_Name, | |
13712 | Parameter_Specifications => Formals, | |
e4494292 | 13713 | Result_Definition => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
13714 | |
13715 | Declarations => New_List ( | |
13716 | Make_Object_Declaration (Loc, | |
13717 | Defining_Identifier => C, | |
e4494292 | 13718 | Object_Definition => New_Occurrence_Of (Typ, Loc))), |
70482933 RK |
13719 | |
13720 | Handled_Statement_Sequence => | |
13721 | Make_Handled_Sequence_Of_Statements (Loc, | |
13722 | Statements => New_List ( | |
13723 | Loop_Statement, | |
d766cee3 | 13724 | Make_Simple_Return_Statement (Loc, |
e4494292 | 13725 | Expression => New_Occurrence_Of (C, Loc))))); |
70482933 RK |
13726 | |
13727 | return Func_Body; | |
13728 | end Make_Boolean_Array_Op; | |
13729 | ||
b6b5cca8 AC |
13730 | ----------------------------------------- |
13731 | -- Minimized_Eliminated_Overflow_Check -- | |
13732 | ----------------------------------------- | |
13733 | ||
13734 | function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is | |
13735 | begin | |
13736 | return | |
13737 | Is_Signed_Integer_Type (Etype (N)) | |
a7f1b24f | 13738 | and then Overflow_Check_Mode in Minimized_Or_Eliminated; |
b6b5cca8 AC |
13739 | end Minimized_Eliminated_Overflow_Check; |
13740 | ||
0580d807 AC |
13741 | -------------------------------- |
13742 | -- Optimize_Length_Comparison -- | |
13743 | -------------------------------- | |
13744 | ||
13745 | procedure Optimize_Length_Comparison (N : Node_Id) is | |
13746 | Loc : constant Source_Ptr := Sloc (N); | |
13747 | Typ : constant Entity_Id := Etype (N); | |
13748 | Result : Node_Id; | |
13749 | ||
13750 | Left : Node_Id; | |
13751 | Right : Node_Id; | |
13752 | -- First and Last attribute reference nodes, which end up as left and | |
13753 | -- right operands of the optimized result. | |
13754 | ||
13755 | Is_Zero : Boolean; | |
13756 | -- True for comparison operand of zero | |
13757 | ||
13758 | Comp : Node_Id; | |
13759 | -- Comparison operand, set only if Is_Zero is false | |
13760 | ||
dcd5fd67 | 13761 | Ent : Entity_Id := Empty; |
0580d807 AC |
13762 | -- Entity whose length is being compared |
13763 | ||
dcd5fd67 | 13764 | Index : Node_Id := Empty; |
0580d807 AC |
13765 | -- Integer_Literal node for length attribute expression, or Empty |
13766 | -- if there is no such expression present. | |
13767 | ||
13768 | Ityp : Entity_Id; | |
13769 | -- Type of array index to which 'Length is applied | |
13770 | ||
13771 | Op : Node_Kind := Nkind (N); | |
13772 | -- Kind of comparison operator, gets flipped if operands backwards | |
13773 | ||
13774 | function Is_Optimizable (N : Node_Id) return Boolean; | |
abcd9db2 AC |
13775 | -- Tests N to see if it is an optimizable comparison value (defined as |
13776 | -- constant zero or one, or something else where the value is known to | |
13777 | -- be positive and in the range of 32-bits, and where the corresponding | |
13778 | -- Length value is also known to be 32-bits. If result is true, sets | |
13779 | -- Is_Zero, Ityp, and Comp accordingly. | |
0580d807 AC |
13780 | |
13781 | function Is_Entity_Length (N : Node_Id) return Boolean; | |
13782 | -- Tests if N is a length attribute applied to a simple entity. If so, | |
13783 | -- returns True, and sets Ent to the entity, and Index to the integer | |
13784 | -- literal provided as an attribute expression, or to Empty if none. | |
13785 | -- Also returns True if the expression is a generated type conversion | |
13786 | -- whose expression is of the desired form. This latter case arises | |
13787 | -- when Apply_Universal_Integer_Attribute_Check installs a conversion | |
13788 | -- to check for being in range, which is not needed in this context. | |
13789 | -- Returns False if neither condition holds. | |
13790 | ||
13791 | function Prepare_64 (N : Node_Id) return Node_Id; | |
13792 | -- Given a discrete expression, returns a Long_Long_Integer typed | |
13793 | -- expression representing the underlying value of the expression. | |
13794 | -- This is done with an unchecked conversion to the result type. We | |
13795 | -- use unchecked conversion to handle the enumeration type case. | |
13796 | ||
13797 | ---------------------- | |
13798 | -- Is_Entity_Length -- | |
13799 | ---------------------- | |
13800 | ||
13801 | function Is_Entity_Length (N : Node_Id) return Boolean is | |
13802 | begin | |
13803 | if Nkind (N) = N_Attribute_Reference | |
13804 | and then Attribute_Name (N) = Name_Length | |
13805 | and then Is_Entity_Name (Prefix (N)) | |
13806 | then | |
13807 | Ent := Entity (Prefix (N)); | |
13808 | ||
13809 | if Present (Expressions (N)) then | |
13810 | Index := First (Expressions (N)); | |
13811 | else | |
13812 | Index := Empty; | |
13813 | end if; | |
13814 | ||
13815 | return True; | |
13816 | ||
13817 | elsif Nkind (N) = N_Type_Conversion | |
13818 | and then not Comes_From_Source (N) | |
13819 | then | |
13820 | return Is_Entity_Length (Expression (N)); | |
13821 | ||
13822 | else | |
13823 | return False; | |
13824 | end if; | |
13825 | end Is_Entity_Length; | |
13826 | ||
13827 | -------------------- | |
13828 | -- Is_Optimizable -- | |
13829 | -------------------- | |
13830 | ||
13831 | function Is_Optimizable (N : Node_Id) return Boolean is | |
13832 | Val : Uint; | |
13833 | OK : Boolean; | |
13834 | Lo : Uint; | |
13835 | Hi : Uint; | |
13836 | Indx : Node_Id; | |
13837 | ||
13838 | begin | |
13839 | if Compile_Time_Known_Value (N) then | |
13840 | Val := Expr_Value (N); | |
13841 | ||
13842 | if Val = Uint_0 then | |
13843 | Is_Zero := True; | |
13844 | Comp := Empty; | |
13845 | return True; | |
13846 | ||
13847 | elsif Val = Uint_1 then | |
13848 | Is_Zero := False; | |
13849 | Comp := Empty; | |
13850 | return True; | |
13851 | end if; | |
13852 | end if; | |
13853 | ||
13854 | -- Here we have to make sure of being within 32-bits | |
13855 | ||
13856 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); | |
13857 | ||
13858 | if not OK | |
abcd9db2 | 13859 | or else Lo < Uint_1 |
0580d807 AC |
13860 | or else Hi > UI_From_Int (Int'Last) |
13861 | then | |
13862 | return False; | |
13863 | end if; | |
13864 | ||
abcd9db2 AC |
13865 | -- Comparison value was within range, so now we must check the index |
13866 | -- value to make sure it is also within 32-bits. | |
0580d807 AC |
13867 | |
13868 | Indx := First_Index (Etype (Ent)); | |
13869 | ||
13870 | if Present (Index) then | |
13871 | for J in 2 .. UI_To_Int (Intval (Index)) loop | |
13872 | Next_Index (Indx); | |
13873 | end loop; | |
13874 | end if; | |
13875 | ||
13876 | Ityp := Etype (Indx); | |
13877 | ||
13878 | if Esize (Ityp) > 32 then | |
13879 | return False; | |
13880 | end if; | |
13881 | ||
13882 | Is_Zero := False; | |
13883 | Comp := N; | |
13884 | return True; | |
13885 | end Is_Optimizable; | |
13886 | ||
13887 | ---------------- | |
13888 | -- Prepare_64 -- | |
13889 | ---------------- | |
13890 | ||
13891 | function Prepare_64 (N : Node_Id) return Node_Id is | |
13892 | begin | |
13893 | return Unchecked_Convert_To (Standard_Long_Long_Integer, N); | |
13894 | end Prepare_64; | |
13895 | ||
13896 | -- Start of processing for Optimize_Length_Comparison | |
13897 | ||
13898 | begin | |
13899 | -- Nothing to do if not a comparison | |
13900 | ||
13901 | if Op not in N_Op_Compare then | |
13902 | return; | |
13903 | end if; | |
13904 | ||
f96fd197 | 13905 | -- Nothing to do if special -gnatd.P debug flag set. |
0580d807 | 13906 | |
f96fd197 | 13907 | if Debug_Flag_Dot_PP then |
0580d807 AC |
13908 | return; |
13909 | end if; | |
13910 | ||
13911 | -- Ent'Length op 0/1 | |
13912 | ||
13913 | if Is_Entity_Length (Left_Opnd (N)) | |
13914 | and then Is_Optimizable (Right_Opnd (N)) | |
13915 | then | |
13916 | null; | |
13917 | ||
13918 | -- 0/1 op Ent'Length | |
13919 | ||
13920 | elsif Is_Entity_Length (Right_Opnd (N)) | |
13921 | and then Is_Optimizable (Left_Opnd (N)) | |
13922 | then | |
13923 | -- Flip comparison to opposite sense | |
13924 | ||
13925 | case Op is | |
13926 | when N_Op_Lt => Op := N_Op_Gt; | |
13927 | when N_Op_Le => Op := N_Op_Ge; | |
13928 | when N_Op_Gt => Op := N_Op_Lt; | |
13929 | when N_Op_Ge => Op := N_Op_Le; | |
13930 | when others => null; | |
13931 | end case; | |
13932 | ||
13933 | -- Else optimization not possible | |
13934 | ||
13935 | else | |
13936 | return; | |
13937 | end if; | |
13938 | ||
13939 | -- Fall through if we will do the optimization | |
13940 | ||
13941 | -- Cases to handle: | |
13942 | ||
13943 | -- X'Length = 0 => X'First > X'Last | |
13944 | -- X'Length = 1 => X'First = X'Last | |
13945 | -- X'Length = n => X'First + (n - 1) = X'Last | |
13946 | ||
13947 | -- X'Length /= 0 => X'First <= X'Last | |
13948 | -- X'Length /= 1 => X'First /= X'Last | |
13949 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
13950 | ||
13951 | -- X'Length >= 0 => always true, warn | |
13952 | -- X'Length >= 1 => X'First <= X'Last | |
13953 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
13954 | ||
13955 | -- X'Length > 0 => X'First <= X'Last | |
13956 | -- X'Length > 1 => X'First < X'Last | |
13957 | -- X'Length > n => X'First + (n - 1) < X'Last | |
13958 | ||
13959 | -- X'Length <= 0 => X'First > X'Last (warn, could be =) | |
13960 | -- X'Length <= 1 => X'First >= X'Last | |
13961 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
13962 | ||
13963 | -- X'Length < 0 => always false (warn) | |
13964 | -- X'Length < 1 => X'First > X'Last | |
13965 | -- X'Length < n => X'First + (n - 1) > X'Last | |
13966 | ||
13967 | -- Note: for the cases of n (not constant 0,1), we require that the | |
13968 | -- corresponding index type be integer or shorter (i.e. not 64-bit), | |
13969 | -- and the same for the comparison value. Then we do the comparison | |
13970 | -- using 64-bit arithmetic (actually long long integer), so that we | |
13971 | -- cannot have overflow intefering with the result. | |
13972 | ||
13973 | -- First deal with warning cases | |
13974 | ||
13975 | if Is_Zero then | |
13976 | case Op is | |
13977 | ||
13978 | -- X'Length >= 0 | |
13979 | ||
13980 | when N_Op_Ge => | |
13981 | Rewrite (N, | |
13982 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc))); | |
13983 | Analyze_And_Resolve (N, Typ); | |
13984 | Warn_On_Known_Condition (N); | |
13985 | return; | |
13986 | ||
13987 | -- X'Length < 0 | |
13988 | ||
13989 | when N_Op_Lt => | |
13990 | Rewrite (N, | |
13991 | Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc))); | |
13992 | Analyze_And_Resolve (N, Typ); | |
13993 | Warn_On_Known_Condition (N); | |
13994 | return; | |
13995 | ||
13996 | when N_Op_Le => | |
13997 | if Constant_Condition_Warnings | |
13998 | and then Comes_From_Source (Original_Node (N)) | |
13999 | then | |
324ac540 | 14000 | Error_Msg_N ("could replace by ""'=""?c?", N); |
0580d807 AC |
14001 | end if; |
14002 | ||
14003 | Op := N_Op_Eq; | |
14004 | ||
14005 | when others => | |
14006 | null; | |
14007 | end case; | |
14008 | end if; | |
14009 | ||
14010 | -- Build the First reference we will use | |
14011 | ||
14012 | Left := | |
14013 | Make_Attribute_Reference (Loc, | |
14014 | Prefix => New_Occurrence_Of (Ent, Loc), | |
14015 | Attribute_Name => Name_First); | |
14016 | ||
14017 | if Present (Index) then | |
14018 | Set_Expressions (Left, New_List (New_Copy (Index))); | |
14019 | end if; | |
14020 | ||
14021 | -- If general value case, then do the addition of (n - 1), and | |
14022 | -- also add the needed conversions to type Long_Long_Integer. | |
14023 | ||
14024 | if Present (Comp) then | |
14025 | Left := | |
14026 | Make_Op_Add (Loc, | |
14027 | Left_Opnd => Prepare_64 (Left), | |
14028 | Right_Opnd => | |
14029 | Make_Op_Subtract (Loc, | |
14030 | Left_Opnd => Prepare_64 (Comp), | |
14031 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
14032 | end if; | |
14033 | ||
14034 | -- Build the Last reference we will use | |
14035 | ||
14036 | Right := | |
14037 | Make_Attribute_Reference (Loc, | |
14038 | Prefix => New_Occurrence_Of (Ent, Loc), | |
14039 | Attribute_Name => Name_Last); | |
14040 | ||
14041 | if Present (Index) then | |
14042 | Set_Expressions (Right, New_List (New_Copy (Index))); | |
14043 | end if; | |
14044 | ||
14045 | -- If general operand, convert Last reference to Long_Long_Integer | |
14046 | ||
14047 | if Present (Comp) then | |
14048 | Right := Prepare_64 (Right); | |
14049 | end if; | |
14050 | ||
14051 | -- Check for cases to optimize | |
14052 | ||
14053 | -- X'Length = 0 => X'First > X'Last | |
14054 | -- X'Length < 1 => X'First > X'Last | |
14055 | -- X'Length < n => X'First + (n - 1) > X'Last | |
14056 | ||
14057 | if (Is_Zero and then Op = N_Op_Eq) | |
14058 | or else (not Is_Zero and then Op = N_Op_Lt) | |
14059 | then | |
14060 | Result := | |
14061 | Make_Op_Gt (Loc, | |
14062 | Left_Opnd => Left, | |
14063 | Right_Opnd => Right); | |
14064 | ||
14065 | -- X'Length = 1 => X'First = X'Last | |
14066 | -- X'Length = n => X'First + (n - 1) = X'Last | |
14067 | ||
14068 | elsif not Is_Zero and then Op = N_Op_Eq then | |
14069 | Result := | |
14070 | Make_Op_Eq (Loc, | |
14071 | Left_Opnd => Left, | |
14072 | Right_Opnd => Right); | |
14073 | ||
14074 | -- X'Length /= 0 => X'First <= X'Last | |
14075 | -- X'Length > 0 => X'First <= X'Last | |
14076 | ||
14077 | elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then | |
14078 | Result := | |
14079 | Make_Op_Le (Loc, | |
14080 | Left_Opnd => Left, | |
14081 | Right_Opnd => Right); | |
14082 | ||
14083 | -- X'Length /= 1 => X'First /= X'Last | |
14084 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
14085 | ||
14086 | elsif not Is_Zero and then Op = N_Op_Ne then | |
14087 | Result := | |
14088 | Make_Op_Ne (Loc, | |
14089 | Left_Opnd => Left, | |
14090 | Right_Opnd => Right); | |
14091 | ||
14092 | -- X'Length >= 1 => X'First <= X'Last | |
14093 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
14094 | ||
14095 | elsif not Is_Zero and then Op = N_Op_Ge then | |
14096 | Result := | |
14097 | Make_Op_Le (Loc, | |
14098 | Left_Opnd => Left, | |
9dd8f36f | 14099 | Right_Opnd => Right); |
0580d807 AC |
14100 | |
14101 | -- X'Length > 1 => X'First < X'Last | |
14102 | -- X'Length > n => X'First + (n = 1) < X'Last | |
14103 | ||
14104 | elsif not Is_Zero and then Op = N_Op_Gt then | |
14105 | Result := | |
14106 | Make_Op_Lt (Loc, | |
14107 | Left_Opnd => Left, | |
14108 | Right_Opnd => Right); | |
14109 | ||
14110 | -- X'Length <= 1 => X'First >= X'Last | |
14111 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
14112 | ||
14113 | elsif not Is_Zero and then Op = N_Op_Le then | |
14114 | Result := | |
14115 | Make_Op_Ge (Loc, | |
14116 | Left_Opnd => Left, | |
14117 | Right_Opnd => Right); | |
14118 | ||
14119 | -- Should not happen at this stage | |
14120 | ||
14121 | else | |
14122 | raise Program_Error; | |
14123 | end if; | |
14124 | ||
14125 | -- Rewrite and finish up | |
14126 | ||
14127 | Rewrite (N, Result); | |
14128 | Analyze_And_Resolve (N, Typ); | |
14129 | return; | |
14130 | end Optimize_Length_Comparison; | |
14131 | ||
0da343bc AC |
14132 | -------------------------------- |
14133 | -- Process_If_Case_Statements -- | |
14134 | -------------------------------- | |
14135 | ||
14136 | procedure Process_If_Case_Statements (N : Node_Id; Stmts : List_Id) is | |
14137 | Decl : Node_Id; | |
14138 | ||
14139 | begin | |
14140 | Decl := First (Stmts); | |
14141 | while Present (Decl) loop | |
14142 | if Nkind (Decl) = N_Object_Declaration | |
14143 | and then Is_Finalizable_Transient (Decl, N) | |
14144 | then | |
937e9676 | 14145 | Process_Transient_In_Expression (Decl, N, Stmts); |
0da343bc AC |
14146 | end if; |
14147 | ||
14148 | Next (Decl); | |
14149 | end loop; | |
14150 | end Process_If_Case_Statements; | |
14151 | ||
937e9676 AC |
14152 | ------------------------------------- |
14153 | -- Process_Transient_In_Expression -- | |
14154 | ------------------------------------- | |
b2c28399 | 14155 | |
937e9676 AC |
14156 | procedure Process_Transient_In_Expression |
14157 | (Obj_Decl : Node_Id; | |
14158 | Expr : Node_Id; | |
14159 | Stmts : List_Id) | |
7782ff67 | 14160 | is |
937e9676 AC |
14161 | Loc : constant Source_Ptr := Sloc (Obj_Decl); |
14162 | Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl); | |
0da343bc | 14163 | |
937e9676 | 14164 | Hook_Context : constant Node_Id := Find_Hook_Context (Expr); |
4b17187f AC |
14165 | -- The node on which to insert the hook as an action. This is usually |
14166 | -- the innermost enclosing non-transient construct. | |
064f4527 | 14167 | |
937e9676 AC |
14168 | Fin_Call : Node_Id; |
14169 | Hook_Assign : Node_Id; | |
14170 | Hook_Clear : Node_Id; | |
14171 | Hook_Decl : Node_Id; | |
14172 | Hook_Insert : Node_Id; | |
14173 | Ptr_Decl : Node_Id; | |
14174 | ||
4b17187f AC |
14175 | Fin_Context : Node_Id; |
14176 | -- The node after which to insert the finalization actions of the | |
937e9676 | 14177 | -- transient object. |
b2c28399 | 14178 | |
8942b30c | 14179 | begin |
937e9676 AC |
14180 | pragma Assert (Nkind_In (Expr, N_Case_Expression, |
14181 | N_Expression_With_Actions, | |
14182 | N_If_Expression)); | |
7782ff67 AC |
14183 | |
14184 | -- When the context is a Boolean evaluation, all three nodes capture the | |
14185 | -- result of their computation in a local temporary: | |
14186 | ||
14187 | -- do | |
14188 | -- Trans_Id : Ctrl_Typ := ...; | |
14189 | -- Result : constant Boolean := ... Trans_Id ...; | |
14190 | -- <finalize Trans_Id> | |
14191 | -- in Result end; | |
14192 | ||
937e9676 AC |
14193 | -- As a result, the finalization of any transient objects can safely |
14194 | -- take place after the result capture. | |
7782ff67 AC |
14195 | |
14196 | -- ??? could this be extended to elementary types? | |
14197 | ||
937e9676 | 14198 | if Is_Boolean_Type (Etype (Expr)) then |
7782ff67 AC |
14199 | Fin_Context := Last (Stmts); |
14200 | ||
937e9676 AC |
14201 | -- Otherwise the immediate context may not be safe enough to carry |
14202 | -- out transient object finalization due to aliasing and nesting of | |
14203 | -- constructs. Insert calls to [Deep_]Finalize after the innermost | |
7782ff67 AC |
14204 | -- enclosing non-transient construct. |
14205 | ||
8942b30c | 14206 | else |
4b17187f | 14207 | Fin_Context := Hook_Context; |
8942b30c | 14208 | end if; |
064f4527 | 14209 | |
937e9676 AC |
14210 | -- Mark the transient object as successfully processed to avoid double |
14211 | -- finalization. | |
b2c28399 | 14212 | |
937e9676 | 14213 | Set_Is_Finalized_Transient (Obj_Id); |
b2c28399 | 14214 | |
937e9676 AC |
14215 | -- Construct all the pieces necessary to hook and finalize a transient |
14216 | -- object. | |
b2c28399 | 14217 | |
937e9676 AC |
14218 | Build_Transient_Object_Statements |
14219 | (Obj_Decl => Obj_Decl, | |
14220 | Fin_Call => Fin_Call, | |
14221 | Hook_Assign => Hook_Assign, | |
14222 | Hook_Clear => Hook_Clear, | |
14223 | Hook_Decl => Hook_Decl, | |
14224 | Ptr_Decl => Ptr_Decl, | |
14225 | Finalize_Obj => False); | |
b2c28399 | 14226 | |
937e9676 AC |
14227 | -- Add the access type which provides a reference to the transient |
14228 | -- object. Generate: | |
b2c28399 | 14229 | |
937e9676 | 14230 | -- type Ptr_Typ is access all Desig_Typ; |
b2c28399 | 14231 | |
937e9676 AC |
14232 | Insert_Action (Hook_Context, Ptr_Decl); |
14233 | ||
14234 | -- Add the temporary which acts as a hook to the transient object. | |
14235 | -- Generate: | |
b2c28399 | 14236 | |
4b17187f | 14237 | -- Hook : Ptr_Id := null; |
b2c28399 | 14238 | |
937e9676 | 14239 | Insert_Action (Hook_Context, Hook_Decl); |
b2c28399 | 14240 | |
937e9676 AC |
14241 | -- When the transient object is initialized by an aggregate, the hook |
14242 | -- must capture the object after the last aggregate assignment takes | |
14243 | -- place. Only then is the object considered initialized. Generate: | |
b2c28399 | 14244 | |
937e9676 | 14245 | -- Hook := Ptr_Typ (Obj_Id); |
b2c28399 | 14246 | -- <or> |
4b17187f | 14247 | -- Hook := Obj_Id'Unrestricted_Access; |
b2c28399 | 14248 | |
937e9676 | 14249 | if Ekind_In (Obj_Id, E_Constant, E_Variable) |
97779c34 AC |
14250 | and then Present (Last_Aggregate_Assignment (Obj_Id)) |
14251 | then | |
4b17187f | 14252 | Hook_Insert := Last_Aggregate_Assignment (Obj_Id); |
97779c34 AC |
14253 | |
14254 | -- Otherwise the hook seizes the related object immediately | |
14255 | ||
14256 | else | |
937e9676 | 14257 | Hook_Insert := Obj_Decl; |
97779c34 AC |
14258 | end if; |
14259 | ||
937e9676 | 14260 | Insert_After_And_Analyze (Hook_Insert, Hook_Assign); |
b2c28399 AC |
14261 | |
14262 | -- When the node is part of a return statement, there is no need to | |
14263 | -- insert a finalization call, as the general finalization mechanism | |
937e9676 AC |
14264 | -- (see Build_Finalizer) would take care of the transient object on |
14265 | -- subprogram exit. Note that it would also be impossible to insert the | |
14266 | -- finalization code after the return statement as this will render it | |
14267 | -- unreachable. | |
b2c28399 | 14268 | |
4b17187f AC |
14269 | if Nkind (Fin_Context) = N_Simple_Return_Statement then |
14270 | null; | |
b2c28399 | 14271 | |
937e9676 AC |
14272 | -- Finalize the hook after the context has been evaluated. Generate: |
14273 | ||
14274 | -- if Hook /= null then | |
14275 | -- [Deep_]Finalize (Hook.all); | |
14276 | -- Hook := null; | |
14277 | -- end if; | |
b2c28399 | 14278 | |
4b17187f AC |
14279 | else |
14280 | Insert_Action_After (Fin_Context, | |
937e9676 | 14281 | Make_Implicit_If_Statement (Obj_Decl, |
4b17187f AC |
14282 | Condition => |
14283 | Make_Op_Ne (Loc, | |
937e9676 AC |
14284 | Left_Opnd => |
14285 | New_Occurrence_Of (Defining_Entity (Hook_Decl), Loc), | |
4b17187f AC |
14286 | Right_Opnd => Make_Null (Loc)), |
14287 | ||
14288 | Then_Statements => New_List ( | |
937e9676 AC |
14289 | Fin_Call, |
14290 | Hook_Clear))); | |
b2c28399 | 14291 | end if; |
937e9676 | 14292 | end Process_Transient_In_Expression; |
b2c28399 | 14293 | |
70482933 RK |
14294 | ------------------------ |
14295 | -- Rewrite_Comparison -- | |
14296 | ------------------------ | |
14297 | ||
14298 | procedure Rewrite_Comparison (N : Node_Id) is | |
634a926b | 14299 | Typ : constant Entity_Id := Etype (N); |
c800f862 | 14300 | |
634a926b AC |
14301 | False_Result : Boolean; |
14302 | True_Result : Boolean; | |
c800f862 | 14303 | |
d26dc4b5 AC |
14304 | begin |
14305 | if Nkind (N) = N_Type_Conversion then | |
14306 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 14307 | return; |
70482933 | 14308 | |
d26dc4b5 | 14309 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
14310 | return; |
14311 | end if; | |
70482933 | 14312 | |
634a926b AC |
14313 | -- Determine the potential outcome of the comparison assuming that the |
14314 | -- operands are valid and emit a warning when the comparison evaluates | |
14315 | -- to True or False only in the presence of invalid values. | |
c800f862 | 14316 | |
634a926b | 14317 | Warn_On_Constant_Valid_Condition (N); |
70482933 | 14318 | |
634a926b AC |
14319 | -- Determine the potential outcome of the comparison assuming that the |
14320 | -- operands are not valid. | |
f02b8bb8 | 14321 | |
634a926b AC |
14322 | Test_Comparison |
14323 | (Op => N, | |
14324 | Assume_Valid => False, | |
14325 | True_Result => True_Result, | |
14326 | False_Result => False_Result); | |
c800f862 | 14327 | |
634a926b | 14328 | -- The outcome is a decisive False or True, rewrite the operator |
c800f862 | 14329 | |
634a926b AC |
14330 | if False_Result or True_Result then |
14331 | Rewrite (N, | |
14332 | Convert_To (Typ, | |
14333 | New_Occurrence_Of (Boolean_Literals (True_Result), Sloc (N)))); | |
c800f862 | 14334 | |
634a926b AC |
14335 | Analyze_And_Resolve (N, Typ); |
14336 | Warn_On_Known_Condition (N); | |
14337 | end if; | |
70482933 RK |
14338 | end Rewrite_Comparison; |
14339 | ||
fbf5a39b AC |
14340 | ---------------------------- |
14341 | -- Safe_In_Place_Array_Op -- | |
14342 | ---------------------------- | |
14343 | ||
14344 | function Safe_In_Place_Array_Op | |
2e071734 AC |
14345 | (Lhs : Node_Id; |
14346 | Op1 : Node_Id; | |
14347 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
14348 | is |
14349 | Target : Entity_Id; | |
14350 | ||
14351 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
14352 | -- Operand is safe if it cannot overlap part of the target of the | |
14353 | -- operation. If the operand and the target are identical, the operand | |
14354 | -- is safe. The operand can be empty in the case of negation. | |
14355 | ||
14356 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 14357 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
14358 | |
14359 | ------------------ | |
14360 | -- Is_Unaliased -- | |
14361 | ------------------ | |
14362 | ||
14363 | function Is_Unaliased (N : Node_Id) return Boolean is | |
14364 | begin | |
14365 | return | |
14366 | Is_Entity_Name (N) | |
14367 | and then No (Address_Clause (Entity (N))) | |
14368 | and then No (Renamed_Object (Entity (N))); | |
14369 | end Is_Unaliased; | |
14370 | ||
14371 | --------------------- | |
14372 | -- Is_Safe_Operand -- | |
14373 | --------------------- | |
14374 | ||
14375 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
14376 | begin | |
14377 | if No (Op) then | |
14378 | return True; | |
14379 | ||
14380 | elsif Is_Entity_Name (Op) then | |
14381 | return Is_Unaliased (Op); | |
14382 | ||
303b4d58 | 14383 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
14384 | return Is_Unaliased (Prefix (Op)); |
14385 | ||
14386 | elsif Nkind (Op) = N_Slice then | |
14387 | return | |
14388 | Is_Unaliased (Prefix (Op)) | |
14389 | and then Entity (Prefix (Op)) /= Target; | |
14390 | ||
14391 | elsif Nkind (Op) = N_Op_Not then | |
14392 | return Is_Safe_Operand (Right_Opnd (Op)); | |
14393 | ||
14394 | else | |
14395 | return False; | |
14396 | end if; | |
14397 | end Is_Safe_Operand; | |
14398 | ||
b6b5cca8 | 14399 | -- Start of processing for Safe_In_Place_Array_Op |
fbf5a39b AC |
14400 | |
14401 | begin | |
685094bf RD |
14402 | -- Skip this processing if the component size is different from system |
14403 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b | 14404 | |
eaa826f8 | 14405 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then |
fbf5a39b AC |
14406 | return False; |
14407 | ||
fbf5a39b AC |
14408 | -- Cannot do in place stuff if non-standard Boolean representation |
14409 | ||
eaa826f8 | 14410 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then |
fbf5a39b AC |
14411 | return False; |
14412 | ||
14413 | elsif not Is_Unaliased (Lhs) then | |
14414 | return False; | |
e7e4d230 | 14415 | |
fbf5a39b AC |
14416 | else |
14417 | Target := Entity (Lhs); | |
e7e4d230 | 14418 | return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2); |
fbf5a39b AC |
14419 | end if; |
14420 | end Safe_In_Place_Array_Op; | |
14421 | ||
70482933 RK |
14422 | ----------------------- |
14423 | -- Tagged_Membership -- | |
14424 | ----------------------- | |
14425 | ||
685094bf RD |
14426 | -- There are two different cases to consider depending on whether the right |
14427 | -- operand is a class-wide type or not. If not we just compare the actual | |
14428 | -- tag of the left expr to the target type tag: | |
70482933 RK |
14429 | -- |
14430 | -- Left_Expr.Tag = Right_Type'Tag; | |
14431 | -- | |
685094bf RD |
14432 | -- If it is a class-wide type we use the RT function CW_Membership which is |
14433 | -- usually implemented by looking in the ancestor tables contained in the | |
14434 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 14435 | |
0669bebe GB |
14436 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
14437 | -- function IW_Membership which is usually implemented by looking in the | |
14438 | -- table of abstract interface types plus the ancestor table contained in | |
14439 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
14440 | ||
82878151 AC |
14441 | procedure Tagged_Membership |
14442 | (N : Node_Id; | |
14443 | SCIL_Node : out Node_Id; | |
14444 | Result : out Node_Id) | |
14445 | is | |
70482933 RK |
14446 | Left : constant Node_Id := Left_Opnd (N); |
14447 | Right : constant Node_Id := Right_Opnd (N); | |
14448 | Loc : constant Source_Ptr := Sloc (N); | |
14449 | ||
38171f43 | 14450 | Full_R_Typ : Entity_Id; |
70482933 | 14451 | Left_Type : Entity_Id; |
82878151 | 14452 | New_Node : Node_Id; |
70482933 RK |
14453 | Right_Type : Entity_Id; |
14454 | Obj_Tag : Node_Id; | |
14455 | ||
14456 | begin | |
82878151 AC |
14457 | SCIL_Node := Empty; |
14458 | ||
852dba80 AC |
14459 | -- Handle entities from the limited view |
14460 | ||
14461 | Left_Type := Available_View (Etype (Left)); | |
14462 | Right_Type := Available_View (Etype (Right)); | |
70482933 | 14463 | |
6cce2156 GD |
14464 | -- In the case where the type is an access type, the test is applied |
14465 | -- using the designated types (needed in Ada 2012 for implicit anonymous | |
14466 | -- access conversions, for AI05-0149). | |
14467 | ||
14468 | if Is_Access_Type (Right_Type) then | |
14469 | Left_Type := Designated_Type (Left_Type); | |
14470 | Right_Type := Designated_Type (Right_Type); | |
14471 | end if; | |
14472 | ||
70482933 RK |
14473 | if Is_Class_Wide_Type (Left_Type) then |
14474 | Left_Type := Root_Type (Left_Type); | |
14475 | end if; | |
14476 | ||
38171f43 AC |
14477 | if Is_Class_Wide_Type (Right_Type) then |
14478 | Full_R_Typ := Underlying_Type (Root_Type (Right_Type)); | |
14479 | else | |
14480 | Full_R_Typ := Underlying_Type (Right_Type); | |
14481 | end if; | |
14482 | ||
70482933 RK |
14483 | Obj_Tag := |
14484 | Make_Selected_Component (Loc, | |
14485 | Prefix => Relocate_Node (Left), | |
a9d8907c | 14486 | Selector_Name => |
e4494292 | 14487 | New_Occurrence_Of (First_Tag_Component (Left_Type), Loc)); |
70482933 | 14488 | |
25409c3c | 14489 | if Is_Class_Wide_Type (Right_Type) or else Is_Interface (Left_Type) then |
758c442c | 14490 | |
0669bebe GB |
14491 | -- No need to issue a run-time check if we statically know that the |
14492 | -- result of this membership test is always true. For example, | |
14493 | -- considering the following declarations: | |
14494 | ||
14495 | -- type Iface is interface; | |
14496 | -- type T is tagged null record; | |
14497 | -- type DT is new T and Iface with null record; | |
14498 | ||
14499 | -- Obj1 : T; | |
14500 | -- Obj2 : DT; | |
14501 | ||
14502 | -- These membership tests are always true: | |
14503 | ||
14504 | -- Obj1 in T'Class | |
14505 | -- Obj2 in T'Class; | |
14506 | -- Obj2 in Iface'Class; | |
14507 | ||
14508 | -- We do not need to handle cases where the membership is illegal. | |
14509 | -- For example: | |
14510 | ||
14511 | -- Obj1 in DT'Class; -- Compile time error | |
14512 | -- Obj1 in Iface'Class; -- Compile time error | |
14513 | ||
fa2538c7 JM |
14514 | if not Is_Interface (Left_Type) |
14515 | and then not Is_Class_Wide_Type (Left_Type) | |
4ac2477e JM |
14516 | and then (Is_Ancestor (Etype (Right_Type), Left_Type, |
14517 | Use_Full_View => True) | |
533369aa AC |
14518 | or else (Is_Interface (Etype (Right_Type)) |
14519 | and then Interface_Present_In_Ancestor | |
761f7dcb AC |
14520 | (Typ => Left_Type, |
14521 | Iface => Etype (Right_Type)))) | |
0669bebe | 14522 | then |
e4494292 | 14523 | Result := New_Occurrence_Of (Standard_True, Loc); |
82878151 | 14524 | return; |
0669bebe GB |
14525 | end if; |
14526 | ||
758c442c GD |
14527 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
14528 | ||
630d30e9 RD |
14529 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
14530 | ||
0669bebe | 14531 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
14532 | |
14533 | or else Is_Interface (Left_Type) | |
14534 | then | |
dfd99a80 TQ |
14535 | -- Issue error if IW_Membership operation not available in a |
14536 | -- configurable run time setting. | |
14537 | ||
14538 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
14539 | Error_Msg_CRT |
14540 | ("dynamic membership test on interface types", N); | |
82878151 AC |
14541 | Result := Empty; |
14542 | return; | |
dfd99a80 TQ |
14543 | end if; |
14544 | ||
82878151 | 14545 | Result := |
758c442c GD |
14546 | Make_Function_Call (Loc, |
14547 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
14548 | Parameter_Associations => New_List ( | |
14549 | Make_Attribute_Reference (Loc, | |
14550 | Prefix => Obj_Tag, | |
14551 | Attribute_Name => Name_Address), | |
e4494292 | 14552 | New_Occurrence_Of ( |
38171f43 | 14553 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), |
758c442c GD |
14554 | Loc))); |
14555 | ||
14556 | -- Ada 95: Normal case | |
14557 | ||
14558 | else | |
82878151 AC |
14559 | Build_CW_Membership (Loc, |
14560 | Obj_Tag_Node => Obj_Tag, | |
14561 | Typ_Tag_Node => | |
e4494292 | 14562 | New_Occurrence_Of ( |
38171f43 | 14563 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc), |
82878151 AC |
14564 | Related_Nod => N, |
14565 | New_Node => New_Node); | |
14566 | ||
14567 | -- Generate the SCIL node for this class-wide membership test. | |
14568 | -- Done here because the previous call to Build_CW_Membership | |
14569 | -- relocates Obj_Tag. | |
14570 | ||
14571 | if Generate_SCIL then | |
14572 | SCIL_Node := Make_SCIL_Membership_Test (Sloc (N)); | |
14573 | Set_SCIL_Entity (SCIL_Node, Etype (Right_Type)); | |
14574 | Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag); | |
14575 | end if; | |
14576 | ||
14577 | Result := New_Node; | |
758c442c GD |
14578 | end if; |
14579 | ||
0669bebe GB |
14580 | -- Right_Type is not a class-wide type |
14581 | ||
70482933 | 14582 | else |
0669bebe GB |
14583 | -- No need to check the tag of the object if Right_Typ is abstract |
14584 | ||
14585 | if Is_Abstract_Type (Right_Type) then | |
e4494292 | 14586 | Result := New_Occurrence_Of (Standard_False, Loc); |
0669bebe GB |
14587 | |
14588 | else | |
82878151 | 14589 | Result := |
0669bebe GB |
14590 | Make_Op_Eq (Loc, |
14591 | Left_Opnd => Obj_Tag, | |
14592 | Right_Opnd => | |
e4494292 | 14593 | New_Occurrence_Of |
38171f43 | 14594 | (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc)); |
0669bebe | 14595 | end if; |
70482933 | 14596 | end if; |
70482933 RK |
14597 | end Tagged_Membership; |
14598 | ||
14599 | ------------------------------ | |
14600 | -- Unary_Op_Validity_Checks -- | |
14601 | ------------------------------ | |
14602 | ||
14603 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
14604 | begin | |
14605 | if Validity_Checks_On and Validity_Check_Operands then | |
14606 | Ensure_Valid (Right_Opnd (N)); | |
14607 | end if; | |
14608 | end Unary_Op_Validity_Checks; | |
14609 | ||
14610 | end Exp_Ch4; |