]>
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 | -- -- | |
da574a86 | 9 | -- Copyright (C) 1992-2014, 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; |
df3e68b1 | 47 | with Lib; use Lib; |
26bff3d9 | 48 | with Namet; use Namet; |
70482933 RK |
49 | with Nlists; use Nlists; |
50 | with Nmake; use Nmake; | |
51 | with Opt; use Opt; | |
25adc5fb | 52 | with Par_SCO; use Par_SCO; |
0669bebe GB |
53 | with Restrict; use Restrict; |
54 | with Rident; use Rident; | |
70482933 RK |
55 | with Rtsfind; use Rtsfind; |
56 | with Sem; use Sem; | |
a4100e55 | 57 | with Sem_Aux; use Sem_Aux; |
70482933 | 58 | with Sem_Cat; use Sem_Cat; |
5d09245e | 59 | with Sem_Ch3; use Sem_Ch3; |
11fa950b | 60 | with Sem_Ch8; use Sem_Ch8; |
70482933 RK |
61 | with Sem_Ch13; use Sem_Ch13; |
62 | with Sem_Eval; use Sem_Eval; | |
63 | with Sem_Res; use Sem_Res; | |
64 | with Sem_Type; use Sem_Type; | |
65 | with Sem_Util; use Sem_Util; | |
07fc65c4 | 66 | with Sem_Warn; use Sem_Warn; |
70482933 | 67 | with Sinfo; use Sinfo; |
70482933 RK |
68 | with Snames; use Snames; |
69 | with Stand; use Stand; | |
7665e4bd | 70 | with SCIL_LL; use SCIL_LL; |
07fc65c4 | 71 | with Targparm; use Targparm; |
70482933 RK |
72 | with Tbuild; use Tbuild; |
73 | with Ttypes; use Ttypes; | |
74 | with Uintp; use Uintp; | |
75 | with Urealp; use Urealp; | |
76 | with Validsw; use Validsw; | |
77 | ||
78 | package body Exp_Ch4 is | |
79 | ||
15ce9ca2 AC |
80 | ----------------------- |
81 | -- Local Subprograms -- | |
82 | ----------------------- | |
70482933 RK |
83 | |
84 | procedure Binary_Op_Validity_Checks (N : Node_Id); | |
85 | pragma Inline (Binary_Op_Validity_Checks); | |
86 | -- Performs validity checks for a binary operator | |
87 | ||
fbf5a39b AC |
88 | procedure Build_Boolean_Array_Proc_Call |
89 | (N : Node_Id; | |
90 | Op1 : Node_Id; | |
91 | Op2 : Node_Id); | |
303b4d58 | 92 | -- If a boolean array assignment can be done in place, build call to |
fbf5a39b AC |
93 | -- corresponding library procedure. |
94 | ||
11fa950b AC |
95 | function Current_Anonymous_Master return Entity_Id; |
96 | -- Return the entity of the heterogeneous finalization master belonging to | |
97 | -- the current unit (either function, package or procedure). This master | |
98 | -- services all anonymous access-to-controlled types. If the current unit | |
99 | -- does not have such master, create one. | |
df3e68b1 | 100 | |
26bff3d9 JM |
101 | procedure Displace_Allocator_Pointer (N : Node_Id); |
102 | -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and | |
103 | -- Expand_Allocator_Expression. Allocating class-wide interface objects | |
104 | -- this routine displaces the pointer to the allocated object to reference | |
105 | -- the component referencing the corresponding secondary dispatch table. | |
106 | ||
fbf5a39b AC |
107 | procedure Expand_Allocator_Expression (N : Node_Id); |
108 | -- Subsidiary to Expand_N_Allocator, for the case when the expression | |
109 | -- is a qualified expression or an aggregate. | |
110 | ||
70482933 RK |
111 | procedure Expand_Array_Comparison (N : Node_Id); |
112 | -- This routine handles expansion of the comparison operators (N_Op_Lt, | |
113 | -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic | |
114 | -- code for these operators is similar, differing only in the details of | |
fbf5a39b AC |
115 | -- the actual comparison call that is made. Special processing (call a |
116 | -- run-time routine) | |
70482933 RK |
117 | |
118 | function Expand_Array_Equality | |
119 | (Nod : Node_Id; | |
70482933 RK |
120 | Lhs : Node_Id; |
121 | Rhs : Node_Id; | |
0da2c8ac AC |
122 | Bodies : List_Id; |
123 | Typ : Entity_Id) return Node_Id; | |
70482933 | 124 | -- Expand an array equality into a call to a function implementing this |
685094bf RD |
125 | -- equality, and a call to it. Loc is the location for the generated nodes. |
126 | -- Lhs and Rhs are the array expressions to be compared. Bodies is a list | |
127 | -- on which to attach bodies of local functions that are created in the | |
128 | -- process. It is the responsibility of the caller to insert those bodies | |
129 | -- at the right place. Nod provides the Sloc value for the generated code. | |
130 | -- Normally the types used for the generated equality routine are taken | |
131 | -- from Lhs and Rhs. However, in some situations of generated code, the | |
132 | -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies | |
133 | -- the type to be used for the formal parameters. | |
70482933 RK |
134 | |
135 | procedure Expand_Boolean_Operator (N : Node_Id); | |
685094bf RD |
136 | -- Common expansion processing for Boolean operators (And, Or, Xor) for the |
137 | -- case of array type arguments. | |
70482933 | 138 | |
5875f8d6 AC |
139 | procedure Expand_Short_Circuit_Operator (N : Node_Id); |
140 | -- Common expansion processing for short-circuit boolean operators | |
141 | ||
456cbfa5 | 142 | procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id); |
5707e389 AC |
143 | -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is |
144 | -- where we allow comparison of "out of range" values. | |
456cbfa5 | 145 | |
70482933 RK |
146 | function Expand_Composite_Equality |
147 | (Nod : Node_Id; | |
148 | Typ : Entity_Id; | |
149 | Lhs : Node_Id; | |
150 | Rhs : Node_Id; | |
2e071734 | 151 | Bodies : List_Id) return Node_Id; |
685094bf RD |
152 | -- Local recursive function used to expand equality for nested composite |
153 | -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which | |
d26d790d AC |
154 | -- to attach bodies of local functions that are created in the process. It |
155 | -- is the responsibility of the caller to insert those bodies at the right | |
156 | -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are | |
157 | -- the left and right sides for the comparison, and Typ is the type of the | |
158 | -- objects to compare. | |
70482933 | 159 | |
fdac1f80 AC |
160 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id); |
161 | -- Routine to expand concatenation of a sequence of two or more operands | |
162 | -- (in the list Operands) and replace node Cnode with the result of the | |
163 | -- concatenation. The operands can be of any appropriate type, and can | |
164 | -- include both arrays and singleton elements. | |
70482933 | 165 | |
f6194278 | 166 | procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id); |
5707e389 AC |
167 | -- N is an N_In membership test mode, with the overflow check mode set to |
168 | -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed | |
169 | -- integer type. This is a case where top level processing is required to | |
170 | -- handle overflow checks in subtrees. | |
f6194278 | 171 | |
70482933 | 172 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); |
685094bf RD |
173 | -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal |
174 | -- fixed. We do not have such a type at runtime, so the purpose of this | |
175 | -- routine is to find the real type by looking up the tree. We also | |
176 | -- determine if the operation must be rounded. | |
70482933 | 177 | |
5d09245e AC |
178 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean; |
179 | -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable | |
180 | -- discriminants if it has a constrained nominal type, unless the object | |
181 | -- is a component of an enclosing Unchecked_Union object that is subject | |
182 | -- to a per-object constraint and the enclosing object lacks inferable | |
183 | -- discriminants. | |
184 | -- | |
185 | -- An expression of an Unchecked_Union type has inferable discriminants | |
186 | -- if it is either a name of an object with inferable discriminants or a | |
187 | -- qualified expression whose subtype mark denotes a constrained subtype. | |
188 | ||
70482933 | 189 | procedure Insert_Dereference_Action (N : Node_Id); |
e6f69614 AC |
190 | -- N is an expression whose type is an access. When the type of the |
191 | -- associated storage pool is derived from Checked_Pool, generate a | |
192 | -- call to the 'Dereference' primitive operation. | |
70482933 RK |
193 | |
194 | function Make_Array_Comparison_Op | |
2e071734 AC |
195 | (Typ : Entity_Id; |
196 | Nod : Node_Id) return Node_Id; | |
685094bf RD |
197 | -- Comparisons between arrays are expanded in line. This function produces |
198 | -- the body of the implementation of (a > b), where a and b are one- | |
199 | -- dimensional arrays of some discrete type. The original node is then | |
200 | -- expanded into the appropriate call to this function. Nod provides the | |
201 | -- Sloc value for the generated code. | |
70482933 RK |
202 | |
203 | function Make_Boolean_Array_Op | |
2e071734 AC |
204 | (Typ : Entity_Id; |
205 | N : Node_Id) return Node_Id; | |
685094bf RD |
206 | -- Boolean operations on boolean arrays are expanded in line. This function |
207 | -- produce the body for the node N, which is (a and b), (a or b), or (a xor | |
208 | -- b). It is used only the normal case and not the packed case. The type | |
209 | -- involved, Typ, is the Boolean array type, and the logical operations in | |
210 | -- the body are simple boolean operations. Note that Typ is always a | |
211 | -- constrained type (the caller has ensured this by using | |
212 | -- Convert_To_Actual_Subtype if necessary). | |
70482933 | 213 | |
b6b5cca8 | 214 | function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean; |
a7f1b24f RD |
215 | -- For signed arithmetic operations when the current overflow mode is |
216 | -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks | |
217 | -- as the first thing we do. We then return. We count on the recursive | |
218 | -- apparatus for overflow checks to call us back with an equivalent | |
219 | -- operation that is in CHECKED mode, avoiding a recursive entry into this | |
220 | -- routine, and that is when we will proceed with the expansion of the | |
221 | -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do | |
222 | -- these optimizations without first making this check, since there may be | |
223 | -- operands further down the tree that are relying on the recursive calls | |
224 | -- triggered by the top level nodes to properly process overflow checking | |
225 | -- and remaining expansion on these nodes. Note that this call back may be | |
226 | -- skipped if the operation is done in Bignum mode but that's fine, since | |
227 | -- the Bignum call takes care of everything. | |
b6b5cca8 | 228 | |
0580d807 AC |
229 | procedure Optimize_Length_Comparison (N : Node_Id); |
230 | -- Given an expression, if it is of the form X'Length op N (or the other | |
231 | -- way round), where N is known at compile time to be 0 or 1, and X is a | |
232 | -- simple entity, and op is a comparison operator, optimizes it into a | |
233 | -- comparison of First and Last. | |
234 | ||
b2c28399 AC |
235 | procedure Process_Transient_Object |
236 | (Decl : Node_Id; | |
237 | Rel_Node : Node_Id); | |
238 | -- Subsidiary routine to the expansion of expression_with_actions and if | |
239 | -- expressions. Generate all the necessary code to finalize a transient | |
240 | -- controlled object when the enclosing context is elaborated or evaluated. | |
241 | -- Decl denotes the declaration of the transient controlled object which is | |
242 | -- usually the result of a controlled function call. Rel_Node denotes the | |
243 | -- context, either an expression_with_actions or an if expression. | |
244 | ||
70482933 | 245 | procedure Rewrite_Comparison (N : Node_Id); |
20b5d666 | 246 | -- If N is the node for a comparison whose outcome can be determined at |
d26dc4b5 AC |
247 | -- compile time, then the node N can be rewritten with True or False. If |
248 | -- the outcome cannot be determined at compile time, the call has no | |
249 | -- effect. If N is a type conversion, then this processing is applied to | |
250 | -- its expression. If N is neither comparison nor a type conversion, the | |
251 | -- call has no effect. | |
70482933 | 252 | |
82878151 AC |
253 | procedure Tagged_Membership |
254 | (N : Node_Id; | |
255 | SCIL_Node : out Node_Id; | |
256 | Result : out Node_Id); | |
70482933 RK |
257 | -- Construct the expression corresponding to the tagged membership test. |
258 | -- Deals with a second operand being (or not) a class-wide type. | |
259 | ||
fbf5a39b | 260 | function Safe_In_Place_Array_Op |
2e071734 AC |
261 | (Lhs : Node_Id; |
262 | Op1 : Node_Id; | |
263 | Op2 : Node_Id) return Boolean; | |
685094bf RD |
264 | -- In the context of an assignment, where the right-hand side is a boolean |
265 | -- operation on arrays, check whether operation can be performed in place. | |
fbf5a39b | 266 | |
70482933 RK |
267 | procedure Unary_Op_Validity_Checks (N : Node_Id); |
268 | pragma Inline (Unary_Op_Validity_Checks); | |
269 | -- Performs validity checks for a unary operator | |
270 | ||
271 | ------------------------------- | |
272 | -- Binary_Op_Validity_Checks -- | |
273 | ------------------------------- | |
274 | ||
275 | procedure Binary_Op_Validity_Checks (N : Node_Id) is | |
276 | begin | |
277 | if Validity_Checks_On and Validity_Check_Operands then | |
278 | Ensure_Valid (Left_Opnd (N)); | |
279 | Ensure_Valid (Right_Opnd (N)); | |
280 | end if; | |
281 | end Binary_Op_Validity_Checks; | |
282 | ||
fbf5a39b AC |
283 | ------------------------------------ |
284 | -- Build_Boolean_Array_Proc_Call -- | |
285 | ------------------------------------ | |
286 | ||
287 | procedure Build_Boolean_Array_Proc_Call | |
288 | (N : Node_Id; | |
289 | Op1 : Node_Id; | |
290 | Op2 : Node_Id) | |
291 | is | |
292 | Loc : constant Source_Ptr := Sloc (N); | |
293 | Kind : constant Node_Kind := Nkind (Expression (N)); | |
294 | Target : constant Node_Id := | |
295 | Make_Attribute_Reference (Loc, | |
296 | Prefix => Name (N), | |
297 | Attribute_Name => Name_Address); | |
298 | ||
bed8af19 | 299 | Arg1 : Node_Id := Op1; |
fbf5a39b AC |
300 | Arg2 : Node_Id := Op2; |
301 | Call_Node : Node_Id; | |
302 | Proc_Name : Entity_Id; | |
303 | ||
304 | begin | |
305 | if Kind = N_Op_Not then | |
306 | if Nkind (Op1) in N_Binary_Op then | |
307 | ||
5e1c00fa | 308 | -- Use negated version of the binary operators |
fbf5a39b AC |
309 | |
310 | if Nkind (Op1) = N_Op_And then | |
311 | Proc_Name := RTE (RE_Vector_Nand); | |
312 | ||
313 | elsif Nkind (Op1) = N_Op_Or then | |
314 | Proc_Name := RTE (RE_Vector_Nor); | |
315 | ||
316 | else pragma Assert (Nkind (Op1) = N_Op_Xor); | |
317 | Proc_Name := RTE (RE_Vector_Xor); | |
318 | end if; | |
319 | ||
320 | Call_Node := | |
321 | Make_Procedure_Call_Statement (Loc, | |
322 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
323 | ||
324 | Parameter_Associations => New_List ( | |
325 | Target, | |
326 | Make_Attribute_Reference (Loc, | |
327 | Prefix => Left_Opnd (Op1), | |
328 | Attribute_Name => Name_Address), | |
329 | ||
330 | Make_Attribute_Reference (Loc, | |
331 | Prefix => Right_Opnd (Op1), | |
332 | Attribute_Name => Name_Address), | |
333 | ||
334 | Make_Attribute_Reference (Loc, | |
335 | Prefix => Left_Opnd (Op1), | |
336 | Attribute_Name => Name_Length))); | |
337 | ||
338 | else | |
339 | Proc_Name := RTE (RE_Vector_Not); | |
340 | ||
341 | Call_Node := | |
342 | Make_Procedure_Call_Statement (Loc, | |
343 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
344 | Parameter_Associations => New_List ( | |
345 | Target, | |
346 | ||
347 | Make_Attribute_Reference (Loc, | |
348 | Prefix => Op1, | |
349 | Attribute_Name => Name_Address), | |
350 | ||
351 | Make_Attribute_Reference (Loc, | |
352 | Prefix => Op1, | |
353 | Attribute_Name => Name_Length))); | |
354 | end if; | |
355 | ||
356 | else | |
357 | -- We use the following equivalences: | |
358 | ||
359 | -- (not X) or (not Y) = not (X and Y) = Nand (X, Y) | |
360 | -- (not X) and (not Y) = not (X or Y) = Nor (X, Y) | |
361 | -- (not X) xor (not Y) = X xor Y | |
362 | -- X xor (not Y) = not (X xor Y) = Nxor (X, Y) | |
363 | ||
364 | if Nkind (Op1) = N_Op_Not then | |
bed8af19 AC |
365 | Arg1 := Right_Opnd (Op1); |
366 | Arg2 := Right_Opnd (Op2); | |
533369aa | 367 | |
fbf5a39b AC |
368 | if Kind = N_Op_And then |
369 | Proc_Name := RTE (RE_Vector_Nor); | |
fbf5a39b AC |
370 | elsif Kind = N_Op_Or then |
371 | Proc_Name := RTE (RE_Vector_Nand); | |
fbf5a39b AC |
372 | else |
373 | Proc_Name := RTE (RE_Vector_Xor); | |
374 | end if; | |
375 | ||
376 | else | |
377 | if Kind = N_Op_And then | |
378 | Proc_Name := RTE (RE_Vector_And); | |
fbf5a39b AC |
379 | elsif Kind = N_Op_Or then |
380 | Proc_Name := RTE (RE_Vector_Or); | |
fbf5a39b AC |
381 | elsif Nkind (Op2) = N_Op_Not then |
382 | Proc_Name := RTE (RE_Vector_Nxor); | |
383 | Arg2 := Right_Opnd (Op2); | |
fbf5a39b AC |
384 | else |
385 | Proc_Name := RTE (RE_Vector_Xor); | |
386 | end if; | |
387 | end if; | |
388 | ||
389 | Call_Node := | |
390 | Make_Procedure_Call_Statement (Loc, | |
391 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
392 | Parameter_Associations => New_List ( | |
393 | Target, | |
955871d3 AC |
394 | Make_Attribute_Reference (Loc, |
395 | Prefix => Arg1, | |
396 | Attribute_Name => Name_Address), | |
397 | Make_Attribute_Reference (Loc, | |
398 | Prefix => Arg2, | |
399 | Attribute_Name => Name_Address), | |
400 | Make_Attribute_Reference (Loc, | |
a8ef12e5 | 401 | Prefix => Arg1, |
955871d3 | 402 | Attribute_Name => Name_Length))); |
fbf5a39b AC |
403 | end if; |
404 | ||
405 | Rewrite (N, Call_Node); | |
406 | Analyze (N); | |
407 | ||
408 | exception | |
409 | when RE_Not_Available => | |
410 | return; | |
411 | end Build_Boolean_Array_Proc_Call; | |
412 | ||
11fa950b AC |
413 | ------------------------------ |
414 | -- Current_Anonymous_Master -- | |
415 | ------------------------------ | |
df3e68b1 | 416 | |
11fa950b | 417 | function Current_Anonymous_Master return Entity_Id is |
2c17ca0a AC |
418 | Decls : List_Id; |
419 | Loc : Source_Ptr; | |
420 | Subp_Body : Node_Id; | |
421 | Unit_Decl : Node_Id; | |
422 | Unit_Id : Entity_Id; | |
df3e68b1 | 423 | |
ca5af305 | 424 | begin |
11fa950b AC |
425 | Unit_Id := Cunit_Entity (Current_Sem_Unit); |
426 | ||
427 | -- Find the entity of the current unit | |
428 | ||
429 | if Ekind (Unit_Id) = E_Subprogram_Body then | |
430 | ||
431 | -- When processing subprogram bodies, the proper scope is always that | |
432 | -- of the spec. | |
433 | ||
434 | Subp_Body := Unit_Id; | |
435 | while Present (Subp_Body) | |
436 | and then Nkind (Subp_Body) /= N_Subprogram_Body | |
437 | loop | |
438 | Subp_Body := Parent (Subp_Body); | |
439 | end loop; | |
440 | ||
441 | Unit_Id := Corresponding_Spec (Subp_Body); | |
442 | end if; | |
443 | ||
444 | Loc := Sloc (Unit_Id); | |
445 | Unit_Decl := Unit (Cunit (Current_Sem_Unit)); | |
446 | ||
447 | -- Find the declarations list of the current unit | |
448 | ||
449 | if Nkind (Unit_Decl) = N_Package_Declaration then | |
450 | Unit_Decl := Specification (Unit_Decl); | |
451 | Decls := Visible_Declarations (Unit_Decl); | |
df3e68b1 | 452 | |
ca5af305 | 453 | if No (Decls) then |
11fa950b AC |
454 | Decls := New_List (Make_Null_Statement (Loc)); |
455 | Set_Visible_Declarations (Unit_Decl, Decls); | |
df3e68b1 | 456 | |
ca5af305 | 457 | elsif Is_Empty_List (Decls) then |
11fa950b | 458 | Append_To (Decls, Make_Null_Statement (Loc)); |
df3e68b1 HK |
459 | end if; |
460 | ||
ca5af305 | 461 | else |
11fa950b | 462 | Decls := Declarations (Unit_Decl); |
f553e7bc | 463 | |
ca5af305 | 464 | if No (Decls) then |
11fa950b AC |
465 | Decls := New_List (Make_Null_Statement (Loc)); |
466 | Set_Declarations (Unit_Decl, Decls); | |
df3e68b1 | 467 | |
ca5af305 | 468 | elsif Is_Empty_List (Decls) then |
11fa950b | 469 | Append_To (Decls, Make_Null_Statement (Loc)); |
ca5af305 | 470 | end if; |
df3e68b1 HK |
471 | end if; |
472 | ||
11fa950b AC |
473 | -- The current unit has an existing anonymous master, traverse its |
474 | -- declarations and locate the entity. | |
df3e68b1 | 475 | |
11fa950b | 476 | if Has_Anonymous_Master (Unit_Id) then |
2c17ca0a AC |
477 | declare |
478 | Decl : Node_Id; | |
479 | Fin_Mas_Id : Entity_Id; | |
df3e68b1 | 480 | |
2c17ca0a AC |
481 | begin |
482 | Decl := First (Decls); | |
483 | while Present (Decl) loop | |
df3e68b1 | 484 | |
2c17ca0a AC |
485 | -- Look for the first variable in the declarations whole type |
486 | -- is Finalization_Master. | |
df3e68b1 | 487 | |
2c17ca0a AC |
488 | if Nkind (Decl) = N_Object_Declaration then |
489 | Fin_Mas_Id := Defining_Identifier (Decl); | |
490 | ||
491 | if Ekind (Fin_Mas_Id) = E_Variable | |
492 | and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master) | |
493 | then | |
494 | return Fin_Mas_Id; | |
495 | end if; | |
496 | end if; | |
497 | ||
498 | Next (Decl); | |
499 | end loop; | |
500 | ||
501 | -- The master was not found even though the unit was labeled as | |
502 | -- having one. | |
df3e68b1 | 503 | |
2c17ca0a AC |
504 | raise Program_Error; |
505 | end; | |
11fa950b AC |
506 | |
507 | -- Create a new anonymous master | |
508 | ||
509 | else | |
510 | declare | |
511 | First_Decl : constant Node_Id := First (Decls); | |
512 | Action : Node_Id; | |
2c17ca0a | 513 | Fin_Mas_Id : Entity_Id; |
df3e68b1 | 514 | |
11fa950b AC |
515 | begin |
516 | -- Since the master and its associated initialization is inserted | |
517 | -- at top level, use the scope of the unit when analyzing. | |
518 | ||
519 | Push_Scope (Unit_Id); | |
520 | ||
521 | -- Create the finalization master | |
522 | ||
523 | Fin_Mas_Id := | |
524 | Make_Defining_Identifier (Loc, | |
525 | Chars => New_External_Name (Chars (Unit_Id), "AM")); | |
526 | ||
527 | -- Generate: | |
528 | -- <Fin_Mas_Id> : Finalization_Master; | |
529 | ||
530 | Action := | |
531 | Make_Object_Declaration (Loc, | |
532 | Defining_Identifier => Fin_Mas_Id, | |
533 | Object_Definition => | |
e4494292 | 534 | New_Occurrence_Of (RTE (RE_Finalization_Master), Loc)); |
11fa950b AC |
535 | |
536 | Insert_Before_And_Analyze (First_Decl, Action); | |
537 | ||
538 | -- Mark the unit to prevent the generation of multiple masters | |
539 | ||
540 | Set_Has_Anonymous_Master (Unit_Id); | |
541 | ||
542 | -- Do not set the base pool and mode of operation on .NET/JVM | |
543 | -- since those targets do not support pools and all VM masters | |
544 | -- are heterogeneous by default. | |
545 | ||
546 | if VM_Target = No_VM then | |
547 | ||
548 | -- Generate: | |
549 | -- Set_Base_Pool | |
550 | -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access); | |
551 | ||
552 | Action := | |
553 | Make_Procedure_Call_Statement (Loc, | |
554 | Name => | |
e4494292 | 555 | New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc), |
11fa950b AC |
556 | |
557 | Parameter_Associations => New_List ( | |
e4494292 | 558 | New_Occurrence_Of (Fin_Mas_Id, Loc), |
11fa950b AC |
559 | Make_Attribute_Reference (Loc, |
560 | Prefix => | |
e4494292 | 561 | New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc), |
11fa950b AC |
562 | Attribute_Name => Name_Unrestricted_Access))); |
563 | ||
564 | Insert_Before_And_Analyze (First_Decl, Action); | |
565 | ||
566 | -- Generate: | |
567 | -- Set_Is_Heterogeneous (<Fin_Mas_Id>); | |
568 | ||
569 | Action := | |
570 | Make_Procedure_Call_Statement (Loc, | |
571 | Name => | |
e4494292 | 572 | New_Occurrence_Of (RTE (RE_Set_Is_Heterogeneous), Loc), |
11fa950b | 573 | Parameter_Associations => New_List ( |
e4494292 | 574 | New_Occurrence_Of (Fin_Mas_Id, Loc))); |
11fa950b AC |
575 | |
576 | Insert_Before_And_Analyze (First_Decl, Action); | |
577 | end if; | |
578 | ||
579 | -- Restore the original state of the scope stack | |
580 | ||
581 | Pop_Scope; | |
582 | ||
583 | return Fin_Mas_Id; | |
584 | end; | |
585 | end if; | |
586 | end Current_Anonymous_Master; | |
df3e68b1 | 587 | |
26bff3d9 JM |
588 | -------------------------------- |
589 | -- Displace_Allocator_Pointer -- | |
590 | -------------------------------- | |
591 | ||
592 | procedure Displace_Allocator_Pointer (N : Node_Id) is | |
593 | Loc : constant Source_Ptr := Sloc (N); | |
594 | Orig_Node : constant Node_Id := Original_Node (N); | |
595 | Dtyp : Entity_Id; | |
596 | Etyp : Entity_Id; | |
597 | PtrT : Entity_Id; | |
598 | ||
599 | begin | |
303b4d58 AC |
600 | -- Do nothing in case of VM targets: the virtual machine will handle |
601 | -- interfaces directly. | |
602 | ||
1f110335 | 603 | if not Tagged_Type_Expansion then |
303b4d58 AC |
604 | return; |
605 | end if; | |
606 | ||
26bff3d9 JM |
607 | pragma Assert (Nkind (N) = N_Identifier |
608 | and then Nkind (Orig_Node) = N_Allocator); | |
609 | ||
610 | PtrT := Etype (Orig_Node); | |
d6a24cdb | 611 | Dtyp := Available_View (Designated_Type (PtrT)); |
26bff3d9 JM |
612 | Etyp := Etype (Expression (Orig_Node)); |
613 | ||
533369aa AC |
614 | if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then |
615 | ||
26bff3d9 JM |
616 | -- If the type of the allocator expression is not an interface type |
617 | -- we can generate code to reference the record component containing | |
618 | -- the pointer to the secondary dispatch table. | |
619 | ||
620 | if not Is_Interface (Etyp) then | |
621 | declare | |
622 | Saved_Typ : constant Entity_Id := Etype (Orig_Node); | |
623 | ||
624 | begin | |
625 | -- 1) Get access to the allocated object | |
626 | ||
627 | Rewrite (N, | |
5972791c | 628 | Make_Explicit_Dereference (Loc, Relocate_Node (N))); |
26bff3d9 JM |
629 | Set_Etype (N, Etyp); |
630 | Set_Analyzed (N); | |
631 | ||
632 | -- 2) Add the conversion to displace the pointer to reference | |
633 | -- the secondary dispatch table. | |
634 | ||
635 | Rewrite (N, Convert_To (Dtyp, Relocate_Node (N))); | |
636 | Analyze_And_Resolve (N, Dtyp); | |
637 | ||
638 | -- 3) The 'access to the secondary dispatch table will be used | |
639 | -- as the value returned by the allocator. | |
640 | ||
641 | Rewrite (N, | |
642 | Make_Attribute_Reference (Loc, | |
643 | Prefix => Relocate_Node (N), | |
644 | Attribute_Name => Name_Access)); | |
645 | Set_Etype (N, Saved_Typ); | |
646 | Set_Analyzed (N); | |
647 | end; | |
648 | ||
649 | -- If the type of the allocator expression is an interface type we | |
650 | -- generate a run-time call to displace "this" to reference the | |
651 | -- component containing the pointer to the secondary dispatch table | |
652 | -- or else raise Constraint_Error if the actual object does not | |
533369aa | 653 | -- implement the target interface. This case corresponds to the |
26bff3d9 JM |
654 | -- following example: |
655 | ||
8fc789c8 | 656 | -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is |
26bff3d9 JM |
657 | -- begin |
658 | -- return new Iface_2'Class'(Obj); | |
659 | -- end Op; | |
660 | ||
661 | else | |
662 | Rewrite (N, | |
663 | Unchecked_Convert_To (PtrT, | |
664 | Make_Function_Call (Loc, | |
e4494292 | 665 | Name => New_Occurrence_Of (RTE (RE_Displace), Loc), |
26bff3d9 JM |
666 | Parameter_Associations => New_List ( |
667 | Unchecked_Convert_To (RTE (RE_Address), | |
668 | Relocate_Node (N)), | |
669 | ||
670 | New_Occurrence_Of | |
671 | (Elists.Node | |
672 | (First_Elmt | |
673 | (Access_Disp_Table (Etype (Base_Type (Dtyp))))), | |
674 | Loc))))); | |
675 | Analyze_And_Resolve (N, PtrT); | |
676 | end if; | |
677 | end if; | |
678 | end Displace_Allocator_Pointer; | |
679 | ||
fbf5a39b AC |
680 | --------------------------------- |
681 | -- Expand_Allocator_Expression -- | |
682 | --------------------------------- | |
683 | ||
684 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
f02b8bb8 RD |
685 | Loc : constant Source_Ptr := Sloc (N); |
686 | Exp : constant Node_Id := Expression (Expression (N)); | |
f02b8bb8 RD |
687 | PtrT : constant Entity_Id := Etype (N); |
688 | DesigT : constant Entity_Id := Designated_Type (PtrT); | |
26bff3d9 JM |
689 | |
690 | procedure Apply_Accessibility_Check | |
691 | (Ref : Node_Id; | |
692 | Built_In_Place : Boolean := False); | |
693 | -- Ada 2005 (AI-344): For an allocator with a class-wide designated | |
685094bf RD |
694 | -- type, generate an accessibility check to verify that the level of the |
695 | -- type of the created object is not deeper than the level of the access | |
50878404 | 696 | -- type. If the type of the qualified expression is class-wide, then |
685094bf RD |
697 | -- always generate the check (except in the case where it is known to be |
698 | -- unnecessary, see comment below). Otherwise, only generate the check | |
699 | -- if the level of the qualified expression type is statically deeper | |
700 | -- than the access type. | |
701 | -- | |
702 | -- Although the static accessibility will generally have been performed | |
703 | -- as a legality check, it won't have been done in cases where the | |
704 | -- allocator appears in generic body, so a run-time check is needed in | |
705 | -- general. One special case is when the access type is declared in the | |
706 | -- same scope as the class-wide allocator, in which case the check can | |
707 | -- never fail, so it need not be generated. | |
708 | -- | |
709 | -- As an open issue, there seem to be cases where the static level | |
710 | -- associated with the class-wide object's underlying type is not | |
711 | -- sufficient to perform the proper accessibility check, such as for | |
712 | -- allocators in nested subprograms or accept statements initialized by | |
713 | -- class-wide formals when the actual originates outside at a deeper | |
714 | -- static level. The nested subprogram case might require passing | |
715 | -- accessibility levels along with class-wide parameters, and the task | |
716 | -- case seems to be an actual gap in the language rules that needs to | |
717 | -- be fixed by the ARG. ??? | |
26bff3d9 JM |
718 | |
719 | ------------------------------- | |
720 | -- Apply_Accessibility_Check -- | |
721 | ------------------------------- | |
722 | ||
723 | procedure Apply_Accessibility_Check | |
724 | (Ref : Node_Id; | |
725 | Built_In_Place : Boolean := False) | |
726 | is | |
a98838ff HK |
727 | Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT); |
728 | Cond : Node_Id; | |
729 | Fin_Call : Node_Id; | |
730 | Free_Stmt : Node_Id; | |
731 | Obj_Ref : Node_Id; | |
732 | Stmts : List_Id; | |
26bff3d9 JM |
733 | |
734 | begin | |
0791fbe9 | 735 | if Ada_Version >= Ada_2005 |
26bff3d9 | 736 | and then Is_Class_Wide_Type (DesigT) |
a98838ff | 737 | and then (Tagged_Type_Expansion or else VM_Target /= No_VM) |
3217f71e | 738 | and then not Scope_Suppress.Suppress (Accessibility_Check) |
26bff3d9 JM |
739 | and then |
740 | (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT) | |
741 | or else | |
742 | (Is_Class_Wide_Type (Etype (Exp)) | |
743 | and then Scope (PtrT) /= Current_Scope)) | |
744 | then | |
e761d11c | 745 | -- If the allocator was built in place, Ref is already a reference |
26bff3d9 | 746 | -- to the access object initialized to the result of the allocator |
e761d11c AC |
747 | -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call |
748 | -- Remove_Side_Effects for cases where the build-in-place call may | |
749 | -- still be the prefix of the reference (to avoid generating | |
750 | -- duplicate calls). Otherwise, it is the entity associated with | |
751 | -- the object containing the address of the allocated object. | |
26bff3d9 JM |
752 | |
753 | if Built_In_Place then | |
e761d11c | 754 | Remove_Side_Effects (Ref); |
a98838ff | 755 | Obj_Ref := New_Copy_Tree (Ref); |
26bff3d9 | 756 | else |
e4494292 | 757 | Obj_Ref := New_Occurrence_Of (Ref, Loc); |
50878404 AC |
758 | end if; |
759 | ||
b6c8e5be AC |
760 | -- For access to interface types we must generate code to displace |
761 | -- the pointer to the base of the object since the subsequent code | |
762 | -- references components located in the TSD of the object (which | |
763 | -- is associated with the primary dispatch table --see a-tags.ads) | |
764 | -- and also generates code invoking Free, which requires also a | |
765 | -- reference to the base of the unallocated object. | |
766 | ||
cc6f5d75 | 767 | if Is_Interface (DesigT) and then Tagged_Type_Expansion then |
b6c8e5be AC |
768 | Obj_Ref := |
769 | Unchecked_Convert_To (Etype (Obj_Ref), | |
770 | Make_Function_Call (Loc, | |
662c2ad4 RD |
771 | Name => |
772 | New_Occurrence_Of (RTE (RE_Base_Address), Loc), | |
b6c8e5be AC |
773 | Parameter_Associations => New_List ( |
774 | Unchecked_Convert_To (RTE (RE_Address), | |
775 | New_Copy_Tree (Obj_Ref))))); | |
776 | end if; | |
777 | ||
50878404 AC |
778 | -- Step 1: Create the object clean up code |
779 | ||
780 | Stmts := New_List; | |
781 | ||
a98838ff HK |
782 | -- Deallocate the object if the accessibility check fails. This |
783 | -- is done only on targets or profiles that support deallocation. | |
784 | ||
785 | -- Free (Obj_Ref); | |
786 | ||
787 | if RTE_Available (RE_Free) then | |
788 | Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref)); | |
789 | Set_Storage_Pool (Free_Stmt, Pool_Id); | |
790 | ||
791 | Append_To (Stmts, Free_Stmt); | |
792 | ||
793 | -- The target or profile cannot deallocate objects | |
794 | ||
795 | else | |
796 | Free_Stmt := Empty; | |
797 | end if; | |
798 | ||
799 | -- Finalize the object if applicable. Generate: | |
a530b8bb AC |
800 | |
801 | -- [Deep_]Finalize (Obj_Ref.all); | |
802 | ||
2cbac6c6 | 803 | if Needs_Finalization (DesigT) then |
a98838ff | 804 | Fin_Call := |
cc6f5d75 AC |
805 | Make_Final_Call |
806 | (Obj_Ref => | |
807 | Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)), | |
808 | Typ => DesigT); | |
a98838ff HK |
809 | |
810 | -- When the target or profile supports deallocation, wrap the | |
811 | -- finalization call in a block to ensure proper deallocation | |
812 | -- even if finalization fails. Generate: | |
813 | ||
814 | -- begin | |
815 | -- <Fin_Call> | |
816 | -- exception | |
817 | -- when others => | |
818 | -- <Free_Stmt> | |
819 | -- raise; | |
820 | -- end; | |
821 | ||
822 | if Present (Free_Stmt) then | |
823 | Fin_Call := | |
824 | Make_Block_Statement (Loc, | |
825 | Handled_Statement_Sequence => | |
826 | Make_Handled_Sequence_Of_Statements (Loc, | |
827 | Statements => New_List (Fin_Call), | |
828 | ||
829 | Exception_Handlers => New_List ( | |
830 | Make_Exception_Handler (Loc, | |
831 | Exception_Choices => New_List ( | |
832 | Make_Others_Choice (Loc)), | |
833 | ||
834 | Statements => New_List ( | |
835 | New_Copy_Tree (Free_Stmt), | |
836 | Make_Raise_Statement (Loc)))))); | |
837 | end if; | |
838 | ||
839 | Prepend_To (Stmts, Fin_Call); | |
f46faa08 AC |
840 | end if; |
841 | ||
50878404 AC |
842 | -- Signal the accessibility failure through a Program_Error |
843 | ||
844 | Append_To (Stmts, | |
845 | Make_Raise_Program_Error (Loc, | |
e4494292 | 846 | Condition => New_Occurrence_Of (Standard_True, Loc), |
50878404 AC |
847 | Reason => PE_Accessibility_Check_Failed)); |
848 | ||
849 | -- Step 2: Create the accessibility comparison | |
850 | ||
851 | -- Generate: | |
852 | -- Ref'Tag | |
853 | ||
b6c8e5be AC |
854 | Obj_Ref := |
855 | Make_Attribute_Reference (Loc, | |
856 | Prefix => Obj_Ref, | |
857 | Attribute_Name => Name_Tag); | |
f46faa08 | 858 | |
50878404 AC |
859 | -- For tagged types, determine the accessibility level by looking |
860 | -- at the type specific data of the dispatch table. Generate: | |
861 | ||
862 | -- Type_Specific_Data (Address (Ref'Tag)).Access_Level | |
863 | ||
f46faa08 | 864 | if Tagged_Type_Expansion then |
50878404 | 865 | Cond := Build_Get_Access_Level (Loc, Obj_Ref); |
f46faa08 | 866 | |
50878404 AC |
867 | -- Use a runtime call to determine the accessibility level when |
868 | -- compiling on virtual machine targets. Generate: | |
f46faa08 | 869 | |
50878404 | 870 | -- Get_Access_Level (Ref'Tag) |
f46faa08 AC |
871 | |
872 | else | |
50878404 AC |
873 | Cond := |
874 | Make_Function_Call (Loc, | |
875 | Name => | |
e4494292 | 876 | New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc), |
50878404 | 877 | Parameter_Associations => New_List (Obj_Ref)); |
26bff3d9 JM |
878 | end if; |
879 | ||
50878404 AC |
880 | Cond := |
881 | Make_Op_Gt (Loc, | |
882 | Left_Opnd => Cond, | |
883 | Right_Opnd => | |
884 | Make_Integer_Literal (Loc, Type_Access_Level (PtrT))); | |
885 | ||
886 | -- Due to the complexity and side effects of the check, utilize an | |
887 | -- if statement instead of the regular Program_Error circuitry. | |
888 | ||
26bff3d9 | 889 | Insert_Action (N, |
8b1011c0 | 890 | Make_Implicit_If_Statement (N, |
50878404 AC |
891 | Condition => Cond, |
892 | Then_Statements => Stmts)); | |
26bff3d9 JM |
893 | end if; |
894 | end Apply_Accessibility_Check; | |
895 | ||
896 | -- Local variables | |
897 | ||
df3e68b1 HK |
898 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); |
899 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
900 | T : constant Entity_Id := Entity (Indic); | |
901 | Node : Node_Id; | |
902 | Tag_Assign : Node_Id; | |
903 | Temp : Entity_Id; | |
904 | Temp_Decl : Node_Id; | |
fbf5a39b | 905 | |
d26dc4b5 AC |
906 | TagT : Entity_Id := Empty; |
907 | -- Type used as source for tag assignment | |
908 | ||
909 | TagR : Node_Id := Empty; | |
910 | -- Target reference for tag assignment | |
911 | ||
26bff3d9 JM |
912 | -- Start of processing for Expand_Allocator_Expression |
913 | ||
fbf5a39b | 914 | begin |
3bfb3c03 JM |
915 | -- Handle call to C++ constructor |
916 | ||
917 | if Is_CPP_Constructor_Call (Exp) then | |
918 | Make_CPP_Constructor_Call_In_Allocator | |
919 | (Allocator => N, | |
920 | Function_Call => Exp); | |
921 | return; | |
922 | end if; | |
923 | ||
885c4871 | 924 | -- In the case of an Ada 2012 allocator whose initial value comes from a |
63585f75 SB |
925 | -- function call, pass "the accessibility level determined by the point |
926 | -- of call" (AI05-0234) to the function. Conceptually, this belongs in | |
927 | -- Expand_Call but it couldn't be done there (because the Etype of the | |
928 | -- allocator wasn't set then) so we generate the parameter here. See | |
929 | -- the Boolean variable Defer in (a block within) Expand_Call. | |
930 | ||
931 | if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then | |
932 | declare | |
933 | Subp : Entity_Id; | |
934 | ||
935 | begin | |
936 | if Nkind (Name (Exp)) = N_Explicit_Dereference then | |
937 | Subp := Designated_Type (Etype (Prefix (Name (Exp)))); | |
938 | else | |
939 | Subp := Entity (Name (Exp)); | |
940 | end if; | |
941 | ||
57a3fca9 AC |
942 | Subp := Ultimate_Alias (Subp); |
943 | ||
63585f75 SB |
944 | if Present (Extra_Accessibility_Of_Result (Subp)) then |
945 | Add_Extra_Actual_To_Call | |
946 | (Subprogram_Call => Exp, | |
947 | Extra_Formal => Extra_Accessibility_Of_Result (Subp), | |
948 | Extra_Actual => Dynamic_Accessibility_Level (PtrT)); | |
949 | end if; | |
950 | end; | |
951 | end if; | |
952 | ||
f6194278 | 953 | -- Case of tagged type or type requiring finalization |
63585f75 SB |
954 | |
955 | if Is_Tagged_Type (T) or else Needs_Finalization (T) then | |
fadcf313 | 956 | |
685094bf RD |
957 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
958 | -- to a build-in-place function, then access to the allocated object | |
959 | -- must be passed to the function. Currently we limit such functions | |
960 | -- to those with constrained limited result subtypes, but eventually | |
961 | -- we plan to expand the allowed forms of functions that are treated | |
962 | -- as build-in-place. | |
20b5d666 | 963 | |
0791fbe9 | 964 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
965 | and then Is_Build_In_Place_Function_Call (Exp) |
966 | then | |
967 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
26bff3d9 JM |
968 | Apply_Accessibility_Check (N, Built_In_Place => True); |
969 | return; | |
20b5d666 JM |
970 | end if; |
971 | ||
ca5af305 AC |
972 | -- Actions inserted before: |
973 | -- Temp : constant ptr_T := new T'(Expression); | |
974 | -- Temp._tag = T'tag; -- when not class-wide | |
975 | -- [Deep_]Adjust (Temp.all); | |
fbf5a39b | 976 | |
ca5af305 | 977 | -- We analyze by hand the new internal allocator to avoid any |
6b6041ec | 978 | -- recursion and inappropriate call to Initialize. |
7324bf49 | 979 | |
20b5d666 JM |
980 | -- We don't want to remove side effects when the expression must be |
981 | -- built in place. In the case of a build-in-place function call, | |
982 | -- that could lead to a duplication of the call, which was already | |
983 | -- substituted for the allocator. | |
984 | ||
26bff3d9 | 985 | if not Aggr_In_Place then |
fbf5a39b AC |
986 | Remove_Side_Effects (Exp); |
987 | end if; | |
988 | ||
e86a3a7e | 989 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
990 | |
991 | -- For a class wide allocation generate the following code: | |
992 | ||
993 | -- type Equiv_Record is record ... end record; | |
994 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
995 | -- temp : PtrT := new CW'(CW!(expr)); | |
996 | ||
997 | if Is_Class_Wide_Type (T) then | |
998 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
999 | ||
26bff3d9 JM |
1000 | -- Ada 2005 (AI-251): If the expression is a class-wide interface |
1001 | -- object we generate code to move up "this" to reference the | |
1002 | -- base of the object before allocating the new object. | |
1003 | ||
1004 | -- Note that Exp'Address is recursively expanded into a call | |
1005 | -- to Base_Address (Exp.Tag) | |
1006 | ||
1007 | if Is_Class_Wide_Type (Etype (Exp)) | |
1008 | and then Is_Interface (Etype (Exp)) | |
1f110335 | 1009 | and then Tagged_Type_Expansion |
26bff3d9 JM |
1010 | then |
1011 | Set_Expression | |
1012 | (Expression (N), | |
1013 | Unchecked_Convert_To (Entity (Indic), | |
1014 | Make_Explicit_Dereference (Loc, | |
1015 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
1016 | Make_Attribute_Reference (Loc, | |
1017 | Prefix => Exp, | |
1018 | Attribute_Name => Name_Address))))); | |
26bff3d9 JM |
1019 | else |
1020 | Set_Expression | |
1021 | (Expression (N), | |
1022 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
1023 | end if; | |
fbf5a39b AC |
1024 | |
1025 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
1026 | end if; | |
1027 | ||
df3e68b1 | 1028 | -- Processing for allocators returning non-interface types |
fbf5a39b | 1029 | |
26bff3d9 JM |
1030 | if not Is_Interface (Directly_Designated_Type (PtrT)) then |
1031 | if Aggr_In_Place then | |
df3e68b1 | 1032 | Temp_Decl := |
26bff3d9 JM |
1033 | Make_Object_Declaration (Loc, |
1034 | Defining_Identifier => Temp, | |
e4494292 | 1035 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
26bff3d9 JM |
1036 | Expression => |
1037 | Make_Allocator (Loc, | |
df3e68b1 | 1038 | Expression => |
e4494292 | 1039 | New_Occurrence_Of (Etype (Exp), Loc))); |
fbf5a39b | 1040 | |
fad0600d AC |
1041 | -- Copy the Comes_From_Source flag for the allocator we just |
1042 | -- built, since logically this allocator is a replacement of | |
1043 | -- the original allocator node. This is for proper handling of | |
1044 | -- restriction No_Implicit_Heap_Allocations. | |
1045 | ||
26bff3d9 | 1046 | Set_Comes_From_Source |
df3e68b1 | 1047 | (Expression (Temp_Decl), Comes_From_Source (N)); |
fbf5a39b | 1048 | |
df3e68b1 HK |
1049 | Set_No_Initialization (Expression (Temp_Decl)); |
1050 | Insert_Action (N, Temp_Decl); | |
fbf5a39b | 1051 | |
ca5af305 | 1052 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1053 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fad0600d | 1054 | |
d3f70b35 | 1055 | -- Attach the object to the associated finalization master. |
deb8dacc HK |
1056 | -- This is done manually on .NET/JVM since those compilers do |
1057 | -- no support pools and can't benefit from internally generated | |
1058 | -- Allocate / Deallocate procedures. | |
1059 | ||
1060 | if VM_Target /= No_VM | |
1061 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1062 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1063 | then |
1064 | Insert_Action (N, | |
cc6f5d75 AC |
1065 | Make_Attach_Call |
1066 | (Obj_Ref => New_Occurrence_Of (Temp, Loc), | |
1067 | Ptr_Typ => PtrT)); | |
deb8dacc HK |
1068 | end if; |
1069 | ||
26bff3d9 JM |
1070 | else |
1071 | Node := Relocate_Node (N); | |
1072 | Set_Analyzed (Node); | |
df3e68b1 HK |
1073 | |
1074 | Temp_Decl := | |
26bff3d9 JM |
1075 | Make_Object_Declaration (Loc, |
1076 | Defining_Identifier => Temp, | |
1077 | Constant_Present => True, | |
e4494292 | 1078 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
df3e68b1 HK |
1079 | Expression => Node); |
1080 | ||
1081 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 1082 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
deb8dacc | 1083 | |
d3f70b35 | 1084 | -- Attach the object to the associated finalization master. |
deb8dacc HK |
1085 | -- This is done manually on .NET/JVM since those compilers do |
1086 | -- no support pools and can't benefit from internally generated | |
1087 | -- Allocate / Deallocate procedures. | |
1088 | ||
1089 | if VM_Target /= No_VM | |
1090 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1091 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1092 | then |
1093 | Insert_Action (N, | |
cc6f5d75 AC |
1094 | Make_Attach_Call |
1095 | (Obj_Ref => New_Occurrence_Of (Temp, Loc), | |
1096 | Ptr_Typ => PtrT)); | |
deb8dacc | 1097 | end if; |
fbf5a39b AC |
1098 | end if; |
1099 | ||
26bff3d9 JM |
1100 | -- Ada 2005 (AI-251): Handle allocators whose designated type is an |
1101 | -- interface type. In this case we use the type of the qualified | |
1102 | -- expression to allocate the object. | |
1103 | ||
fbf5a39b | 1104 | else |
26bff3d9 | 1105 | declare |
191fcb3a | 1106 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); |
26bff3d9 | 1107 | New_Decl : Node_Id; |
fbf5a39b | 1108 | |
26bff3d9 JM |
1109 | begin |
1110 | New_Decl := | |
1111 | Make_Full_Type_Declaration (Loc, | |
1112 | Defining_Identifier => Def_Id, | |
cc6f5d75 | 1113 | Type_Definition => |
26bff3d9 JM |
1114 | Make_Access_To_Object_Definition (Loc, |
1115 | All_Present => True, | |
1116 | Null_Exclusion_Present => False, | |
0929eaeb AC |
1117 | Constant_Present => |
1118 | Is_Access_Constant (Etype (N)), | |
26bff3d9 | 1119 | Subtype_Indication => |
e4494292 | 1120 | New_Occurrence_Of (Etype (Exp), Loc))); |
26bff3d9 JM |
1121 | |
1122 | Insert_Action (N, New_Decl); | |
1123 | ||
df3e68b1 HK |
1124 | -- Inherit the allocation-related attributes from the original |
1125 | -- access type. | |
26bff3d9 | 1126 | |
24d4b3d5 AC |
1127 | Set_Finalization_Master |
1128 | (Def_Id, Finalization_Master (PtrT)); | |
df3e68b1 | 1129 | |
24d4b3d5 AC |
1130 | Set_Associated_Storage_Pool |
1131 | (Def_Id, Associated_Storage_Pool (PtrT)); | |
758c442c | 1132 | |
26bff3d9 JM |
1133 | -- Declare the object using the previous type declaration |
1134 | ||
1135 | if Aggr_In_Place then | |
df3e68b1 | 1136 | Temp_Decl := |
26bff3d9 JM |
1137 | Make_Object_Declaration (Loc, |
1138 | Defining_Identifier => Temp, | |
e4494292 | 1139 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
26bff3d9 JM |
1140 | Expression => |
1141 | Make_Allocator (Loc, | |
e4494292 | 1142 | New_Occurrence_Of (Etype (Exp), Loc))); |
26bff3d9 | 1143 | |
fad0600d AC |
1144 | -- Copy the Comes_From_Source flag for the allocator we just |
1145 | -- built, since logically this allocator is a replacement of | |
1146 | -- the original allocator node. This is for proper handling | |
1147 | -- of restriction No_Implicit_Heap_Allocations. | |
1148 | ||
26bff3d9 | 1149 | Set_Comes_From_Source |
df3e68b1 | 1150 | (Expression (Temp_Decl), Comes_From_Source (N)); |
26bff3d9 | 1151 | |
df3e68b1 HK |
1152 | Set_No_Initialization (Expression (Temp_Decl)); |
1153 | Insert_Action (N, Temp_Decl); | |
26bff3d9 | 1154 | |
ca5af305 | 1155 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1156 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
26bff3d9 | 1157 | |
26bff3d9 JM |
1158 | else |
1159 | Node := Relocate_Node (N); | |
1160 | Set_Analyzed (Node); | |
df3e68b1 HK |
1161 | |
1162 | Temp_Decl := | |
26bff3d9 JM |
1163 | Make_Object_Declaration (Loc, |
1164 | Defining_Identifier => Temp, | |
1165 | Constant_Present => True, | |
e4494292 | 1166 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
df3e68b1 HK |
1167 | Expression => Node); |
1168 | ||
1169 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 1170 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
26bff3d9 JM |
1171 | end if; |
1172 | ||
1173 | -- Generate an additional object containing the address of the | |
1174 | -- returned object. The type of this second object declaration | |
685094bf RD |
1175 | -- is the correct type required for the common processing that |
1176 | -- is still performed by this subprogram. The displacement of | |
1177 | -- this pointer to reference the component associated with the | |
1178 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
1179 | |
1180 | New_Decl := | |
1181 | Make_Object_Declaration (Loc, | |
243cae0a | 1182 | Defining_Identifier => Make_Temporary (Loc, 'P'), |
e4494292 | 1183 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
243cae0a | 1184 | Expression => |
df3e68b1 | 1185 | Unchecked_Convert_To (PtrT, |
e4494292 | 1186 | New_Occurrence_Of (Temp, Loc))); |
26bff3d9 JM |
1187 | |
1188 | Insert_Action (N, New_Decl); | |
1189 | ||
df3e68b1 HK |
1190 | Temp_Decl := New_Decl; |
1191 | Temp := Defining_Identifier (New_Decl); | |
26bff3d9 | 1192 | end; |
758c442c GD |
1193 | end if; |
1194 | ||
26bff3d9 JM |
1195 | Apply_Accessibility_Check (Temp); |
1196 | ||
1197 | -- Generate the tag assignment | |
1198 | ||
1199 | -- Suppress the tag assignment when VM_Target because VM tags are | |
1200 | -- represented implicitly in objects. | |
1201 | ||
1f110335 | 1202 | if not Tagged_Type_Expansion then |
26bff3d9 | 1203 | null; |
fbf5a39b | 1204 | |
26bff3d9 JM |
1205 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
1206 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 1207 | |
26bff3d9 JM |
1208 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
1209 | pragma Assert (Is_Class_Wide_Type | |
1210 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
1211 | null; |
1212 | ||
1213 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
1214 | TagT := T; | |
e4494292 | 1215 | TagR := New_Occurrence_Of (Temp, Loc); |
d26dc4b5 AC |
1216 | |
1217 | elsif Is_Private_Type (T) | |
1218 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 1219 | then |
d26dc4b5 | 1220 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
1221 | TagR := |
1222 | Unchecked_Convert_To (Underlying_Type (T), | |
1223 | Make_Explicit_Dereference (Loc, | |
e4494292 | 1224 | Prefix => New_Occurrence_Of (Temp, Loc))); |
d26dc4b5 AC |
1225 | end if; |
1226 | ||
1227 | if Present (TagT) then | |
38171f43 AC |
1228 | declare |
1229 | Full_T : constant Entity_Id := Underlying_Type (TagT); | |
e4494292 | 1230 | |
38171f43 AC |
1231 | begin |
1232 | Tag_Assign := | |
1233 | Make_Assignment_Statement (Loc, | |
cc6f5d75 | 1234 | Name => |
38171f43 | 1235 | Make_Selected_Component (Loc, |
cc6f5d75 | 1236 | Prefix => TagR, |
38171f43 | 1237 | Selector_Name => |
e4494292 RD |
1238 | New_Occurrence_Of |
1239 | (First_Tag_Component (Full_T), Loc)), | |
1240 | ||
38171f43 AC |
1241 | Expression => |
1242 | Unchecked_Convert_To (RTE (RE_Tag), | |
e4494292 | 1243 | New_Occurrence_Of |
38171f43 AC |
1244 | (Elists.Node |
1245 | (First_Elmt (Access_Disp_Table (Full_T))), Loc))); | |
1246 | end; | |
fbf5a39b AC |
1247 | |
1248 | -- The previous assignment has to be done in any case | |
1249 | ||
1250 | Set_Assignment_OK (Name (Tag_Assign)); | |
1251 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
1252 | end if; |
1253 | ||
533369aa AC |
1254 | if Needs_Finalization (DesigT) and then Needs_Finalization (T) then |
1255 | ||
df3e68b1 HK |
1256 | -- Generate an Adjust call if the object will be moved. In Ada |
1257 | -- 2005, the object may be inherently limited, in which case | |
1258 | -- there is no Adjust procedure, and the object is built in | |
1259 | -- place. In Ada 95, the object can be limited but not | |
1260 | -- inherently limited if this allocator came from a return | |
1261 | -- statement (we're allocating the result on the secondary | |
1262 | -- stack). In that case, the object will be moved, so we _do_ | |
1263 | -- want to Adjust. | |
1264 | ||
1265 | if not Aggr_In_Place | |
51245e2d | 1266 | and then not Is_Limited_View (T) |
df3e68b1 HK |
1267 | then |
1268 | Insert_Action (N, | |
fbf5a39b | 1269 | |
533369aa AC |
1270 | -- An unchecked conversion is needed in the classwide case |
1271 | -- because the designated type can be an ancestor of the | |
1272 | -- subtype mark of the allocator. | |
fbf5a39b | 1273 | |
533369aa AC |
1274 | Make_Adjust_Call |
1275 | (Obj_Ref => | |
1276 | Unchecked_Convert_To (T, | |
1277 | Make_Explicit_Dereference (Loc, | |
e4494292 | 1278 | Prefix => New_Occurrence_Of (Temp, Loc))), |
533369aa | 1279 | Typ => T)); |
df3e68b1 | 1280 | end if; |
b254da66 AC |
1281 | |
1282 | -- Generate: | |
1283 | -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access); | |
1284 | ||
2bfa5484 | 1285 | -- Do not generate this call in the following cases: |
c5f5123f | 1286 | |
2bfa5484 HK |
1287 | -- * .NET/JVM - these targets do not support address arithmetic |
1288 | -- and unchecked conversion, key elements of Finalize_Address. | |
c5f5123f | 1289 | |
2bfa5484 HK |
1290 | -- * CodePeer mode - TSS primitive Finalize_Address is not |
1291 | -- created in this mode. | |
b254da66 AC |
1292 | |
1293 | if VM_Target = No_VM | |
1294 | and then not CodePeer_Mode | |
1295 | and then Present (Finalization_Master (PtrT)) | |
f7bb41af AC |
1296 | and then Present (Temp_Decl) |
1297 | and then Nkind (Expression (Temp_Decl)) = N_Allocator | |
b254da66 AC |
1298 | then |
1299 | Insert_Action (N, | |
1300 | Make_Set_Finalize_Address_Call | |
1301 | (Loc => Loc, | |
1302 | Typ => T, | |
1303 | Ptr_Typ => PtrT)); | |
1304 | end if; | |
fbf5a39b AC |
1305 | end if; |
1306 | ||
e4494292 | 1307 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1308 | Analyze_And_Resolve (N, PtrT); |
1309 | ||
685094bf RD |
1310 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
1311 | -- component containing the secondary dispatch table of the interface | |
1312 | -- type. | |
26bff3d9 JM |
1313 | |
1314 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
1315 | Displace_Allocator_Pointer (N); | |
1316 | end if; | |
1317 | ||
fbf5a39b | 1318 | elsif Aggr_In_Place then |
e86a3a7e | 1319 | Temp := Make_Temporary (Loc, 'P', N); |
df3e68b1 | 1320 | Temp_Decl := |
fbf5a39b AC |
1321 | Make_Object_Declaration (Loc, |
1322 | Defining_Identifier => Temp, | |
e4494292 | 1323 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
df3e68b1 HK |
1324 | Expression => |
1325 | Make_Allocator (Loc, | |
e4494292 | 1326 | Expression => New_Occurrence_Of (Etype (Exp), Loc))); |
fbf5a39b | 1327 | |
fad0600d AC |
1328 | -- Copy the Comes_From_Source flag for the allocator we just built, |
1329 | -- since logically this allocator is a replacement of the original | |
1330 | -- allocator node. This is for proper handling of restriction | |
1331 | -- No_Implicit_Heap_Allocations. | |
1332 | ||
fbf5a39b | 1333 | Set_Comes_From_Source |
df3e68b1 HK |
1334 | (Expression (Temp_Decl), Comes_From_Source (N)); |
1335 | ||
1336 | Set_No_Initialization (Expression (Temp_Decl)); | |
1337 | Insert_Action (N, Temp_Decl); | |
1338 | ||
ca5af305 | 1339 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1340 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fbf5a39b | 1341 | |
d3f70b35 AC |
1342 | -- Attach the object to the associated finalization master. Thisis |
1343 | -- done manually on .NET/JVM since those compilers do no support | |
deb8dacc HK |
1344 | -- pools and cannot benefit from internally generated Allocate and |
1345 | -- Deallocate procedures. | |
1346 | ||
1347 | if VM_Target /= No_VM | |
1348 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1349 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1350 | then |
1351 | Insert_Action (N, | |
243cae0a | 1352 | Make_Attach_Call |
e4494292 | 1353 | (Obj_Ref => New_Occurrence_Of (Temp, Loc), |
243cae0a | 1354 | Ptr_Typ => PtrT)); |
deb8dacc HK |
1355 | end if; |
1356 | ||
e4494292 | 1357 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1358 | Analyze_And_Resolve (N, PtrT); |
1359 | ||
533369aa | 1360 | elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then |
51e4c4b9 AC |
1361 | Install_Null_Excluding_Check (Exp); |
1362 | ||
f02b8bb8 | 1363 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
1364 | and then Nkind (Exp) = N_Allocator |
1365 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
1366 | then | |
0da2c8ac | 1367 | -- Apply constraint to designated subtype indication |
fbf5a39b | 1368 | |
cc6f5d75 AC |
1369 | Apply_Constraint_Check |
1370 | (Expression (Exp), Designated_Type (DesigT), No_Sliding => True); | |
fbf5a39b AC |
1371 | |
1372 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
1373 | ||
1374 | -- Propagate constraint_error to enclosing allocator | |
1375 | ||
1376 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
1377 | end if; | |
1df4f514 | 1378 | |
fbf5a39b | 1379 | else |
14f0f659 AC |
1380 | Build_Allocate_Deallocate_Proc (N, True); |
1381 | ||
36c73552 AC |
1382 | -- If we have: |
1383 | -- type A is access T1; | |
1384 | -- X : A := new T2'(...); | |
1385 | -- T1 and T2 can be different subtypes, and we might need to check | |
1386 | -- both constraints. First check against the type of the qualified | |
1387 | -- expression. | |
1388 | ||
1389 | Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
fbf5a39b | 1390 | |
d79e621a | 1391 | if Do_Range_Check (Exp) then |
d79e621a GD |
1392 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); |
1393 | end if; | |
1394 | ||
685094bf RD |
1395 | -- A check is also needed in cases where the designated subtype is |
1396 | -- constrained and differs from the subtype given in the qualified | |
1397 | -- expression. Note that the check on the qualified expression does | |
1398 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 1399 | |
f02b8bb8 | 1400 | if Is_Constrained (DesigT) |
9450205a | 1401 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
1402 | then |
1403 | Apply_Constraint_Check | |
f02b8bb8 | 1404 | (Exp, DesigT, No_Sliding => False); |
d79e621a GD |
1405 | |
1406 | if Do_Range_Check (Exp) then | |
d79e621a GD |
1407 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); |
1408 | end if; | |
f02b8bb8 RD |
1409 | end if; |
1410 | ||
685094bf RD |
1411 | -- For an access to unconstrained packed array, GIGI needs to see an |
1412 | -- expression with a constrained subtype in order to compute the | |
1413 | -- proper size for the allocator. | |
f02b8bb8 RD |
1414 | |
1415 | if Is_Array_Type (T) | |
1416 | and then not Is_Constrained (T) | |
1417 | and then Is_Packed (T) | |
1418 | then | |
1419 | declare | |
191fcb3a | 1420 | ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A'); |
f02b8bb8 RD |
1421 | Internal_Exp : constant Node_Id := Relocate_Node (Exp); |
1422 | begin | |
1423 | Insert_Action (Exp, | |
1424 | Make_Subtype_Declaration (Loc, | |
1425 | Defining_Identifier => ConstrT, | |
25ebc085 AC |
1426 | Subtype_Indication => |
1427 | Make_Subtype_From_Expr (Internal_Exp, T))); | |
f02b8bb8 RD |
1428 | Freeze_Itype (ConstrT, Exp); |
1429 | Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp)); | |
1430 | end; | |
fbf5a39b | 1431 | end if; |
f02b8bb8 | 1432 | |
685094bf RD |
1433 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
1434 | -- to a build-in-place function, then access to the allocated object | |
1435 | -- must be passed to the function. Currently we limit such functions | |
1436 | -- to those with constrained limited result subtypes, but eventually | |
1437 | -- we plan to expand the allowed forms of functions that are treated | |
1438 | -- as build-in-place. | |
20b5d666 | 1439 | |
0791fbe9 | 1440 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
1441 | and then Is_Build_In_Place_Function_Call (Exp) |
1442 | then | |
1443 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
1444 | end if; | |
fbf5a39b AC |
1445 | end if; |
1446 | ||
1447 | exception | |
1448 | when RE_Not_Available => | |
1449 | return; | |
1450 | end Expand_Allocator_Expression; | |
1451 | ||
70482933 RK |
1452 | ----------------------------- |
1453 | -- Expand_Array_Comparison -- | |
1454 | ----------------------------- | |
1455 | ||
685094bf RD |
1456 | -- Expansion is only required in the case of array types. For the unpacked |
1457 | -- case, an appropriate runtime routine is called. For packed cases, and | |
1458 | -- also in some other cases where a runtime routine cannot be called, the | |
1459 | -- form of the expansion is: | |
70482933 RK |
1460 | |
1461 | -- [body for greater_nn; boolean_expression] | |
1462 | ||
1463 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
1464 | -- Boolean expression depends on the operator involved. | |
1465 | ||
1466 | procedure Expand_Array_Comparison (N : Node_Id) is | |
1467 | Loc : constant Source_Ptr := Sloc (N); | |
1468 | Op1 : Node_Id := Left_Opnd (N); | |
1469 | Op2 : Node_Id := Right_Opnd (N); | |
1470 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 1471 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
1472 | |
1473 | Expr : Node_Id; | |
1474 | Func_Body : Node_Id; | |
1475 | Func_Name : Entity_Id; | |
1476 | ||
fbf5a39b AC |
1477 | Comp : RE_Id; |
1478 | ||
9bc43c53 AC |
1479 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
1480 | -- True for byte addressable target | |
91b1417d | 1481 | |
fbf5a39b | 1482 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
685094bf RD |
1483 | -- Returns True if the length of the given operand is known to be less |
1484 | -- than 4. Returns False if this length is known to be four or greater | |
1485 | -- or is not known at compile time. | |
fbf5a39b AC |
1486 | |
1487 | ------------------------ | |
1488 | -- Length_Less_Than_4 -- | |
1489 | ------------------------ | |
1490 | ||
1491 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
1492 | Otyp : constant Entity_Id := Etype (Opnd); | |
1493 | ||
1494 | begin | |
1495 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
1496 | return String_Literal_Length (Otyp) < 4; | |
1497 | ||
1498 | else | |
1499 | declare | |
1500 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
1501 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
1502 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
1503 | Lov : Uint; | |
1504 | Hiv : Uint; | |
1505 | ||
1506 | begin | |
1507 | if Compile_Time_Known_Value (Lo) then | |
1508 | Lov := Expr_Value (Lo); | |
1509 | else | |
1510 | return False; | |
1511 | end if; | |
1512 | ||
1513 | if Compile_Time_Known_Value (Hi) then | |
1514 | Hiv := Expr_Value (Hi); | |
1515 | else | |
1516 | return False; | |
1517 | end if; | |
1518 | ||
1519 | return Hiv < Lov + 3; | |
1520 | end; | |
1521 | end if; | |
1522 | end Length_Less_Than_4; | |
1523 | ||
1524 | -- Start of processing for Expand_Array_Comparison | |
1525 | ||
70482933 | 1526 | begin |
fbf5a39b AC |
1527 | -- Deal first with unpacked case, where we can call a runtime routine |
1528 | -- except that we avoid this for targets for which are not addressable | |
26bff3d9 | 1529 | -- by bytes, and for the JVM/CIL, since they do not support direct |
fbf5a39b AC |
1530 | -- addressing of array components. |
1531 | ||
1532 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 1533 | and then Byte_Addressable |
26bff3d9 | 1534 | and then VM_Target = No_VM |
fbf5a39b AC |
1535 | then |
1536 | -- The call we generate is: | |
1537 | ||
1538 | -- Compare_Array_xn[_Unaligned] | |
1539 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
1540 | ||
1541 | -- x = U for unsigned, S for signed | |
1542 | -- n = 8,16,32,64 for component size | |
1543 | -- Add _Unaligned if length < 4 and component size is 8. | |
1544 | -- <op> is the standard comparison operator | |
1545 | ||
1546 | if Component_Size (Typ1) = 8 then | |
1547 | if Length_Less_Than_4 (Op1) | |
1548 | or else | |
1549 | Length_Less_Than_4 (Op2) | |
1550 | then | |
1551 | if Is_Unsigned_Type (Ctyp) then | |
1552 | Comp := RE_Compare_Array_U8_Unaligned; | |
1553 | else | |
1554 | Comp := RE_Compare_Array_S8_Unaligned; | |
1555 | end if; | |
1556 | ||
1557 | else | |
1558 | if Is_Unsigned_Type (Ctyp) then | |
1559 | Comp := RE_Compare_Array_U8; | |
1560 | else | |
1561 | Comp := RE_Compare_Array_S8; | |
1562 | end if; | |
1563 | end if; | |
1564 | ||
1565 | elsif Component_Size (Typ1) = 16 then | |
1566 | if Is_Unsigned_Type (Ctyp) then | |
1567 | Comp := RE_Compare_Array_U16; | |
1568 | else | |
1569 | Comp := RE_Compare_Array_S16; | |
1570 | end if; | |
1571 | ||
1572 | elsif Component_Size (Typ1) = 32 then | |
1573 | if Is_Unsigned_Type (Ctyp) then | |
1574 | Comp := RE_Compare_Array_U32; | |
1575 | else | |
1576 | Comp := RE_Compare_Array_S32; | |
1577 | end if; | |
1578 | ||
1579 | else pragma Assert (Component_Size (Typ1) = 64); | |
1580 | if Is_Unsigned_Type (Ctyp) then | |
1581 | Comp := RE_Compare_Array_U64; | |
1582 | else | |
1583 | Comp := RE_Compare_Array_S64; | |
1584 | end if; | |
1585 | end if; | |
1586 | ||
1587 | Remove_Side_Effects (Op1, Name_Req => True); | |
1588 | Remove_Side_Effects (Op2, Name_Req => True); | |
1589 | ||
1590 | Rewrite (Op1, | |
1591 | Make_Function_Call (Sloc (Op1), | |
1592 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
1593 | ||
1594 | Parameter_Associations => New_List ( | |
1595 | Make_Attribute_Reference (Loc, | |
1596 | Prefix => Relocate_Node (Op1), | |
1597 | Attribute_Name => Name_Address), | |
1598 | ||
1599 | Make_Attribute_Reference (Loc, | |
1600 | Prefix => Relocate_Node (Op2), | |
1601 | Attribute_Name => Name_Address), | |
1602 | ||
1603 | Make_Attribute_Reference (Loc, | |
1604 | Prefix => Relocate_Node (Op1), | |
1605 | Attribute_Name => Name_Length), | |
1606 | ||
1607 | Make_Attribute_Reference (Loc, | |
1608 | Prefix => Relocate_Node (Op2), | |
1609 | Attribute_Name => Name_Length)))); | |
1610 | ||
1611 | Rewrite (Op2, | |
1612 | Make_Integer_Literal (Sloc (Op2), | |
1613 | Intval => Uint_0)); | |
1614 | ||
1615 | Analyze_And_Resolve (Op1, Standard_Integer); | |
1616 | Analyze_And_Resolve (Op2, Standard_Integer); | |
1617 | return; | |
1618 | end if; | |
1619 | ||
1620 | -- Cases where we cannot make runtime call | |
1621 | ||
70482933 RK |
1622 | -- For (a <= b) we convert to not (a > b) |
1623 | ||
1624 | if Chars (N) = Name_Op_Le then | |
1625 | Rewrite (N, | |
1626 | Make_Op_Not (Loc, | |
1627 | Right_Opnd => | |
1628 | Make_Op_Gt (Loc, | |
1629 | Left_Opnd => Op1, | |
1630 | Right_Opnd => Op2))); | |
1631 | Analyze_And_Resolve (N, Standard_Boolean); | |
1632 | return; | |
1633 | ||
1634 | -- For < the Boolean expression is | |
1635 | -- greater__nn (op2, op1) | |
1636 | ||
1637 | elsif Chars (N) = Name_Op_Lt then | |
1638 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1639 | ||
1640 | -- Switch operands | |
1641 | ||
1642 | Op1 := Right_Opnd (N); | |
1643 | Op2 := Left_Opnd (N); | |
1644 | ||
1645 | -- For (a >= b) we convert to not (a < b) | |
1646 | ||
1647 | elsif Chars (N) = Name_Op_Ge then | |
1648 | Rewrite (N, | |
1649 | Make_Op_Not (Loc, | |
1650 | Right_Opnd => | |
1651 | Make_Op_Lt (Loc, | |
1652 | Left_Opnd => Op1, | |
1653 | Right_Opnd => Op2))); | |
1654 | Analyze_And_Resolve (N, Standard_Boolean); | |
1655 | return; | |
1656 | ||
1657 | -- For > the Boolean expression is | |
1658 | -- greater__nn (op1, op2) | |
1659 | ||
1660 | else | |
1661 | pragma Assert (Chars (N) = Name_Op_Gt); | |
1662 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1663 | end if; | |
1664 | ||
1665 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1666 | Expr := | |
1667 | Make_Function_Call (Loc, | |
e4494292 | 1668 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
1669 | Parameter_Associations => New_List (Op1, Op2)); |
1670 | ||
1671 | Insert_Action (N, Func_Body); | |
1672 | Rewrite (N, Expr); | |
1673 | Analyze_And_Resolve (N, Standard_Boolean); | |
1674 | ||
fbf5a39b AC |
1675 | exception |
1676 | when RE_Not_Available => | |
1677 | return; | |
70482933 RK |
1678 | end Expand_Array_Comparison; |
1679 | ||
1680 | --------------------------- | |
1681 | -- Expand_Array_Equality -- | |
1682 | --------------------------- | |
1683 | ||
685094bf RD |
1684 | -- Expand an equality function for multi-dimensional arrays. Here is an |
1685 | -- example of such a function for Nb_Dimension = 2 | |
70482933 | 1686 | |
0da2c8ac | 1687 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 1688 | -- begin |
fbf5a39b AC |
1689 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
1690 | -- and then | |
1691 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
1692 | -- then | |
1693 | -- return True; -- RM 4.5.2(22) | |
1694 | -- end if; | |
0da2c8ac | 1695 | |
fbf5a39b AC |
1696 | -- if A'length (1) /= B'length (1) |
1697 | -- or else | |
1698 | -- A'length (2) /= B'length (2) | |
1699 | -- then | |
1700 | -- return False; -- RM 4.5.2(23) | |
1701 | -- end if; | |
0da2c8ac | 1702 | |
fbf5a39b | 1703 | -- declare |
523456db AC |
1704 | -- A1 : Index_T1 := A'first (1); |
1705 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 1706 | -- begin |
523456db | 1707 | -- loop |
fbf5a39b | 1708 | -- declare |
523456db AC |
1709 | -- A2 : Index_T2 := A'first (2); |
1710 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 1711 | -- begin |
523456db | 1712 | -- loop |
fbf5a39b AC |
1713 | -- if A (A1, A2) /= B (B1, B2) then |
1714 | -- return False; | |
70482933 | 1715 | -- end if; |
0da2c8ac | 1716 | |
523456db AC |
1717 | -- exit when A2 = A'last (2); |
1718 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 1719 | -- B2 := Index_T2'succ (B2); |
70482933 | 1720 | -- end loop; |
fbf5a39b | 1721 | -- end; |
0da2c8ac | 1722 | |
523456db AC |
1723 | -- exit when A1 = A'last (1); |
1724 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 1725 | -- B1 := Index_T1'succ (B1); |
70482933 | 1726 | -- end loop; |
fbf5a39b | 1727 | -- end; |
0da2c8ac | 1728 | |
70482933 RK |
1729 | -- return true; |
1730 | -- end Enn; | |
1731 | ||
685094bf RD |
1732 | -- Note on the formal types used (atyp and btyp). If either of the arrays |
1733 | -- is of a private type, we use the underlying type, and do an unchecked | |
1734 | -- conversion of the actual. If either of the arrays has a bound depending | |
1735 | -- on a discriminant, then we use the base type since otherwise we have an | |
1736 | -- escaped discriminant in the function. | |
0da2c8ac | 1737 | |
685094bf RD |
1738 | -- If both arrays are constrained and have the same bounds, we can generate |
1739 | -- a loop with an explicit iteration scheme using a 'Range attribute over | |
1740 | -- the first array. | |
523456db | 1741 | |
70482933 RK |
1742 | function Expand_Array_Equality |
1743 | (Nod : Node_Id; | |
70482933 RK |
1744 | Lhs : Node_Id; |
1745 | Rhs : Node_Id; | |
0da2c8ac AC |
1746 | Bodies : List_Id; |
1747 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
1748 | is |
1749 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
1750 | Decls : constant List_Id := New_List; |
1751 | Index_List1 : constant List_Id := New_List; | |
1752 | Index_List2 : constant List_Id := New_List; | |
1753 | ||
1754 | Actuals : List_Id; | |
1755 | Formals : List_Id; | |
1756 | Func_Name : Entity_Id; | |
1757 | Func_Body : Node_Id; | |
70482933 RK |
1758 | |
1759 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
1760 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
1761 | ||
0da2c8ac AC |
1762 | Ltyp : Entity_Id; |
1763 | Rtyp : Entity_Id; | |
1764 | -- The parameter types to be used for the formals | |
1765 | ||
fbf5a39b AC |
1766 | function Arr_Attr |
1767 | (Arr : Entity_Id; | |
1768 | Nam : Name_Id; | |
2e071734 | 1769 | Num : Int) return Node_Id; |
5e1c00fa | 1770 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 1771 | |
70482933 | 1772 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
685094bf | 1773 | -- Create one statement to compare corresponding components, designated |
3b42c566 | 1774 | -- by a full set of indexes. |
70482933 | 1775 | |
0da2c8ac | 1776 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
685094bf RD |
1777 | -- Given one of the arguments, computes the appropriate type to be used |
1778 | -- for that argument in the corresponding function formal | |
0da2c8ac | 1779 | |
fbf5a39b | 1780 | function Handle_One_Dimension |
70482933 | 1781 | (N : Int; |
2e071734 | 1782 | Index : Node_Id) return Node_Id; |
0da2c8ac | 1783 | -- This procedure returns the following code |
fbf5a39b AC |
1784 | -- |
1785 | -- declare | |
523456db | 1786 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 1787 | -- begin |
523456db | 1788 | -- loop |
fbf5a39b | 1789 | -- xxx |
523456db AC |
1790 | -- exit when An = A'Last (N); |
1791 | -- An := Index_T'Succ (An) | |
0da2c8ac | 1792 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
1793 | -- end loop; |
1794 | -- end; | |
1795 | -- | |
3b42c566 | 1796 | -- If both indexes are constrained and identical, the procedure |
523456db AC |
1797 | -- returns a simpler loop: |
1798 | -- | |
1799 | -- for An in A'Range (N) loop | |
1800 | -- xxx | |
1801 | -- end loop | |
0da2c8ac | 1802 | -- |
523456db | 1803 | -- N is the dimension for which we are generating a loop. Index is the |
685094bf RD |
1804 | -- N'th index node, whose Etype is Index_Type_n in the above code. The |
1805 | -- xxx statement is either the loop or declare for the next dimension | |
1806 | -- or if this is the last dimension the comparison of corresponding | |
1807 | -- components of the arrays. | |
fbf5a39b | 1808 | -- |
685094bf | 1809 | -- The actual way the code works is to return the comparison of |
a90bd866 | 1810 | -- corresponding components for the N+1 call. That's neater. |
fbf5a39b AC |
1811 | |
1812 | function Test_Empty_Arrays return Node_Id; | |
1813 | -- This function constructs the test for both arrays being empty | |
1814 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1815 | -- and then | |
1816 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1817 | ||
1818 | function Test_Lengths_Correspond return Node_Id; | |
685094bf RD |
1819 | -- This function constructs the test for arrays having different lengths |
1820 | -- in at least one index position, in which case the resulting code is: | |
fbf5a39b AC |
1821 | |
1822 | -- A'length (1) /= B'length (1) | |
1823 | -- or else | |
1824 | -- A'length (2) /= B'length (2) | |
1825 | -- or else | |
1826 | -- ... | |
1827 | ||
1828 | -------------- | |
1829 | -- Arr_Attr -- | |
1830 | -------------- | |
1831 | ||
1832 | function Arr_Attr | |
1833 | (Arr : Entity_Id; | |
1834 | Nam : Name_Id; | |
2e071734 | 1835 | Num : Int) return Node_Id |
fbf5a39b AC |
1836 | is |
1837 | begin | |
1838 | return | |
1839 | Make_Attribute_Reference (Loc, | |
cc6f5d75 AC |
1840 | Attribute_Name => Nam, |
1841 | Prefix => New_Occurrence_Of (Arr, Loc), | |
1842 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
fbf5a39b | 1843 | end Arr_Attr; |
70482933 RK |
1844 | |
1845 | ------------------------ | |
1846 | -- Component_Equality -- | |
1847 | ------------------------ | |
1848 | ||
1849 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1850 | Test : Node_Id; | |
1851 | L, R : Node_Id; | |
1852 | ||
1853 | begin | |
1854 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1855 | ||
1856 | L := | |
1857 | Make_Indexed_Component (Loc, | |
7675ad4f | 1858 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
1859 | Expressions => Index_List1); |
1860 | ||
1861 | R := | |
1862 | Make_Indexed_Component (Loc, | |
7675ad4f | 1863 | Prefix => Make_Identifier (Loc, Chars (B)), |
70482933 RK |
1864 | Expressions => Index_List2); |
1865 | ||
1866 | Test := Expand_Composite_Equality | |
1867 | (Nod, Component_Type (Typ), L, R, Decls); | |
1868 | ||
a9d8907c JM |
1869 | -- If some (sub)component is an unchecked_union, the whole operation |
1870 | -- will raise program error. | |
8aceda64 AC |
1871 | |
1872 | if Nkind (Test) = N_Raise_Program_Error then | |
a9d8907c JM |
1873 | |
1874 | -- This node is going to be inserted at a location where a | |
685094bf RD |
1875 | -- statement is expected: clear its Etype so analysis will set |
1876 | -- it to the expected Standard_Void_Type. | |
a9d8907c JM |
1877 | |
1878 | Set_Etype (Test, Empty); | |
8aceda64 AC |
1879 | return Test; |
1880 | ||
1881 | else | |
1882 | return | |
1883 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 1884 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), |
8aceda64 | 1885 | Then_Statements => New_List ( |
d766cee3 | 1886 | Make_Simple_Return_Statement (Loc, |
8aceda64 AC |
1887 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
1888 | end if; | |
70482933 RK |
1889 | end Component_Equality; |
1890 | ||
0da2c8ac AC |
1891 | ------------------ |
1892 | -- Get_Arg_Type -- | |
1893 | ------------------ | |
1894 | ||
1895 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1896 | T : Entity_Id; | |
1897 | X : Node_Id; | |
1898 | ||
1899 | begin | |
1900 | T := Etype (N); | |
1901 | ||
1902 | if No (T) then | |
1903 | return Typ; | |
1904 | ||
1905 | else | |
1906 | T := Underlying_Type (T); | |
1907 | ||
1908 | X := First_Index (T); | |
1909 | while Present (X) loop | |
761f7dcb AC |
1910 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) |
1911 | or else | |
1912 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
0da2c8ac AC |
1913 | then |
1914 | T := Base_Type (T); | |
1915 | exit; | |
1916 | end if; | |
1917 | ||
1918 | Next_Index (X); | |
1919 | end loop; | |
1920 | ||
1921 | return T; | |
1922 | end if; | |
1923 | end Get_Arg_Type; | |
1924 | ||
fbf5a39b AC |
1925 | -------------------------- |
1926 | -- Handle_One_Dimension -- | |
1927 | --------------------------- | |
70482933 | 1928 | |
fbf5a39b | 1929 | function Handle_One_Dimension |
70482933 | 1930 | (N : Int; |
2e071734 | 1931 | Index : Node_Id) return Node_Id |
70482933 | 1932 | is |
0da2c8ac | 1933 | Need_Separate_Indexes : constant Boolean := |
761f7dcb | 1934 | Ltyp /= Rtyp or else not Is_Constrained (Ltyp); |
0da2c8ac | 1935 | -- If the index types are identical, and we are working with |
685094bf RD |
1936 | -- constrained types, then we can use the same index for both |
1937 | -- of the arrays. | |
0da2c8ac | 1938 | |
191fcb3a | 1939 | An : constant Entity_Id := Make_Temporary (Loc, 'A'); |
0da2c8ac AC |
1940 | |
1941 | Bn : Entity_Id; | |
1942 | Index_T : Entity_Id; | |
1943 | Stm_List : List_Id; | |
1944 | Loop_Stm : Node_Id; | |
70482933 RK |
1945 | |
1946 | begin | |
0da2c8ac AC |
1947 | if N > Number_Dimensions (Ltyp) then |
1948 | return Component_Equality (Ltyp); | |
fbf5a39b | 1949 | end if; |
70482933 | 1950 | |
0da2c8ac AC |
1951 | -- Case where we generate a loop |
1952 | ||
1953 | Index_T := Base_Type (Etype (Index)); | |
1954 | ||
1955 | if Need_Separate_Indexes then | |
191fcb3a | 1956 | Bn := Make_Temporary (Loc, 'B'); |
0da2c8ac AC |
1957 | else |
1958 | Bn := An; | |
1959 | end if; | |
70482933 | 1960 | |
e4494292 RD |
1961 | Append (New_Occurrence_Of (An, Loc), Index_List1); |
1962 | Append (New_Occurrence_Of (Bn, Loc), Index_List2); | |
70482933 | 1963 | |
0da2c8ac AC |
1964 | Stm_List := New_List ( |
1965 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1966 | |
0da2c8ac | 1967 | if Need_Separate_Indexes then |
a9d8907c | 1968 | |
3b42c566 | 1969 | -- Generate guard for loop, followed by increments of indexes |
523456db AC |
1970 | |
1971 | Append_To (Stm_List, | |
1972 | Make_Exit_Statement (Loc, | |
1973 | Condition => | |
1974 | Make_Op_Eq (Loc, | |
cc6f5d75 | 1975 | Left_Opnd => New_Occurrence_Of (An, Loc), |
523456db AC |
1976 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); |
1977 | ||
1978 | Append_To (Stm_List, | |
1979 | Make_Assignment_Statement (Loc, | |
e4494292 | 1980 | Name => New_Occurrence_Of (An, Loc), |
523456db AC |
1981 | Expression => |
1982 | Make_Attribute_Reference (Loc, | |
e4494292 | 1983 | Prefix => New_Occurrence_Of (Index_T, Loc), |
523456db | 1984 | Attribute_Name => Name_Succ, |
e4494292 RD |
1985 | Expressions => New_List ( |
1986 | New_Occurrence_Of (An, Loc))))); | |
523456db | 1987 | |
0da2c8ac AC |
1988 | Append_To (Stm_List, |
1989 | Make_Assignment_Statement (Loc, | |
e4494292 | 1990 | Name => New_Occurrence_Of (Bn, Loc), |
0da2c8ac AC |
1991 | Expression => |
1992 | Make_Attribute_Reference (Loc, | |
e4494292 | 1993 | Prefix => New_Occurrence_Of (Index_T, Loc), |
0da2c8ac | 1994 | Attribute_Name => Name_Succ, |
e4494292 RD |
1995 | Expressions => New_List ( |
1996 | New_Occurrence_Of (Bn, Loc))))); | |
0da2c8ac AC |
1997 | end if; |
1998 | ||
a9d8907c JM |
1999 | -- If separate indexes, we need a declare block for An and Bn, and a |
2000 | -- loop without an iteration scheme. | |
0da2c8ac AC |
2001 | |
2002 | if Need_Separate_Indexes then | |
523456db AC |
2003 | Loop_Stm := |
2004 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
2005 | ||
0da2c8ac AC |
2006 | return |
2007 | Make_Block_Statement (Loc, | |
2008 | Declarations => New_List ( | |
523456db AC |
2009 | Make_Object_Declaration (Loc, |
2010 | Defining_Identifier => An, | |
e4494292 | 2011 | Object_Definition => New_Occurrence_Of (Index_T, Loc), |
523456db AC |
2012 | Expression => Arr_Attr (A, Name_First, N)), |
2013 | ||
0da2c8ac AC |
2014 | Make_Object_Declaration (Loc, |
2015 | Defining_Identifier => Bn, | |
e4494292 | 2016 | Object_Definition => New_Occurrence_Of (Index_T, Loc), |
0da2c8ac | 2017 | Expression => Arr_Attr (B, Name_First, N))), |
523456db | 2018 | |
0da2c8ac AC |
2019 | Handled_Statement_Sequence => |
2020 | Make_Handled_Sequence_Of_Statements (Loc, | |
2021 | Statements => New_List (Loop_Stm))); | |
2022 | ||
523456db AC |
2023 | -- If no separate indexes, return loop statement with explicit |
2024 | -- iteration scheme on its own | |
0da2c8ac AC |
2025 | |
2026 | else | |
523456db AC |
2027 | Loop_Stm := |
2028 | Make_Implicit_Loop_Statement (Nod, | |
2029 | Statements => Stm_List, | |
2030 | Iteration_Scheme => | |
2031 | Make_Iteration_Scheme (Loc, | |
2032 | Loop_Parameter_Specification => | |
2033 | Make_Loop_Parameter_Specification (Loc, | |
2034 | Defining_Identifier => An, | |
2035 | Discrete_Subtype_Definition => | |
2036 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
2037 | return Loop_Stm; |
2038 | end if; | |
fbf5a39b AC |
2039 | end Handle_One_Dimension; |
2040 | ||
2041 | ----------------------- | |
2042 | -- Test_Empty_Arrays -- | |
2043 | ----------------------- | |
2044 | ||
2045 | function Test_Empty_Arrays return Node_Id is | |
2046 | Alist : Node_Id; | |
2047 | Blist : Node_Id; | |
2048 | ||
2049 | Atest : Node_Id; | |
2050 | Btest : Node_Id; | |
70482933 | 2051 | |
fbf5a39b AC |
2052 | begin |
2053 | Alist := Empty; | |
2054 | Blist := Empty; | |
0da2c8ac | 2055 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
2056 | Atest := |
2057 | Make_Op_Eq (Loc, | |
2058 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
2059 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
2060 | ||
2061 | Btest := | |
2062 | Make_Op_Eq (Loc, | |
2063 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
2064 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
2065 | ||
2066 | if No (Alist) then | |
2067 | Alist := Atest; | |
2068 | Blist := Btest; | |
70482933 | 2069 | |
fbf5a39b AC |
2070 | else |
2071 | Alist := | |
2072 | Make_Or_Else (Loc, | |
2073 | Left_Opnd => Relocate_Node (Alist), | |
2074 | Right_Opnd => Atest); | |
2075 | ||
2076 | Blist := | |
2077 | Make_Or_Else (Loc, | |
2078 | Left_Opnd => Relocate_Node (Blist), | |
2079 | Right_Opnd => Btest); | |
2080 | end if; | |
2081 | end loop; | |
70482933 | 2082 | |
fbf5a39b AC |
2083 | return |
2084 | Make_And_Then (Loc, | |
2085 | Left_Opnd => Alist, | |
2086 | Right_Opnd => Blist); | |
2087 | end Test_Empty_Arrays; | |
70482933 | 2088 | |
fbf5a39b AC |
2089 | ----------------------------- |
2090 | -- Test_Lengths_Correspond -- | |
2091 | ----------------------------- | |
70482933 | 2092 | |
fbf5a39b AC |
2093 | function Test_Lengths_Correspond return Node_Id is |
2094 | Result : Node_Id; | |
2095 | Rtest : Node_Id; | |
2096 | ||
2097 | begin | |
2098 | Result := Empty; | |
0da2c8ac | 2099 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
2100 | Rtest := |
2101 | Make_Op_Ne (Loc, | |
2102 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
2103 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
2104 | ||
2105 | if No (Result) then | |
2106 | Result := Rtest; | |
2107 | else | |
2108 | Result := | |
2109 | Make_Or_Else (Loc, | |
2110 | Left_Opnd => Relocate_Node (Result), | |
2111 | Right_Opnd => Rtest); | |
2112 | end if; | |
2113 | end loop; | |
2114 | ||
2115 | return Result; | |
2116 | end Test_Lengths_Correspond; | |
70482933 RK |
2117 | |
2118 | -- Start of processing for Expand_Array_Equality | |
2119 | ||
2120 | begin | |
0da2c8ac AC |
2121 | Ltyp := Get_Arg_Type (Lhs); |
2122 | Rtyp := Get_Arg_Type (Rhs); | |
2123 | ||
685094bf RD |
2124 | -- For now, if the argument types are not the same, go to the base type, |
2125 | -- since the code assumes that the formals have the same type. This is | |
2126 | -- fixable in future ??? | |
0da2c8ac AC |
2127 | |
2128 | if Ltyp /= Rtyp then | |
2129 | Ltyp := Base_Type (Ltyp); | |
2130 | Rtyp := Base_Type (Rtyp); | |
2131 | pragma Assert (Ltyp = Rtyp); | |
2132 | end if; | |
2133 | ||
2134 | -- Build list of formals for function | |
2135 | ||
70482933 RK |
2136 | Formals := New_List ( |
2137 | Make_Parameter_Specification (Loc, | |
2138 | Defining_Identifier => A, | |
e4494292 | 2139 | Parameter_Type => New_Occurrence_Of (Ltyp, Loc)), |
70482933 RK |
2140 | |
2141 | Make_Parameter_Specification (Loc, | |
2142 | Defining_Identifier => B, | |
e4494292 | 2143 | Parameter_Type => New_Occurrence_Of (Rtyp, Loc))); |
70482933 | 2144 | |
191fcb3a | 2145 | Func_Name := Make_Temporary (Loc, 'E'); |
70482933 | 2146 | |
fbf5a39b | 2147 | -- Build statement sequence for function |
70482933 RK |
2148 | |
2149 | Func_Body := | |
2150 | Make_Subprogram_Body (Loc, | |
2151 | Specification => | |
2152 | Make_Function_Specification (Loc, | |
2153 | Defining_Unit_Name => Func_Name, | |
2154 | Parameter_Specifications => Formals, | |
e4494292 | 2155 | Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)), |
fbf5a39b AC |
2156 | |
2157 | Declarations => Decls, | |
2158 | ||
70482933 RK |
2159 | Handled_Statement_Sequence => |
2160 | Make_Handled_Sequence_Of_Statements (Loc, | |
2161 | Statements => New_List ( | |
fbf5a39b AC |
2162 | |
2163 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 2164 | Condition => Test_Empty_Arrays, |
fbf5a39b | 2165 | Then_Statements => New_List ( |
d766cee3 | 2166 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
2167 | Expression => |
2168 | New_Occurrence_Of (Standard_True, Loc)))), | |
2169 | ||
2170 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 2171 | Condition => Test_Lengths_Correspond, |
fbf5a39b | 2172 | Then_Statements => New_List ( |
d766cee3 | 2173 | Make_Simple_Return_Statement (Loc, |
cc6f5d75 | 2174 | Expression => New_Occurrence_Of (Standard_False, Loc)))), |
fbf5a39b | 2175 | |
0da2c8ac | 2176 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 2177 | |
d766cee3 | 2178 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
2179 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
2180 | ||
2181 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 2182 | Set_Is_Inlined (Func_Name); |
70482933 | 2183 | |
685094bf RD |
2184 | -- If the array type is distinct from the type of the arguments, it |
2185 | -- is the full view of a private type. Apply an unchecked conversion | |
2186 | -- to insure that analysis of the call succeeds. | |
70482933 | 2187 | |
0da2c8ac AC |
2188 | declare |
2189 | L, R : Node_Id; | |
2190 | ||
2191 | begin | |
2192 | L := Lhs; | |
2193 | R := Rhs; | |
2194 | ||
2195 | if No (Etype (Lhs)) | |
2196 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
2197 | then | |
2198 | L := OK_Convert_To (Ltyp, Lhs); | |
2199 | end if; | |
2200 | ||
2201 | if No (Etype (Rhs)) | |
2202 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
2203 | then | |
2204 | R := OK_Convert_To (Rtyp, Rhs); | |
2205 | end if; | |
2206 | ||
2207 | Actuals := New_List (L, R); | |
2208 | end; | |
70482933 RK |
2209 | |
2210 | Append_To (Bodies, Func_Body); | |
2211 | ||
2212 | return | |
2213 | Make_Function_Call (Loc, | |
e4494292 | 2214 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
2215 | Parameter_Associations => Actuals); |
2216 | end Expand_Array_Equality; | |
2217 | ||
2218 | ----------------------------- | |
2219 | -- Expand_Boolean_Operator -- | |
2220 | ----------------------------- | |
2221 | ||
685094bf RD |
2222 | -- Note that we first get the actual subtypes of the operands, since we |
2223 | -- always want to deal with types that have bounds. | |
70482933 RK |
2224 | |
2225 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 2226 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
2227 | |
2228 | begin | |
685094bf RD |
2229 | -- Special case of bit packed array where both operands are known to be |
2230 | -- properly aligned. In this case we use an efficient run time routine | |
2231 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
2232 | |
2233 | if Is_Bit_Packed_Array (Typ) | |
2234 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
2235 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
2236 | then | |
70482933 | 2237 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
2238 | return; |
2239 | end if; | |
70482933 | 2240 | |
a9d8907c JM |
2241 | -- For the normal non-packed case, the general expansion is to build |
2242 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
2243 | -- and then inserting it into the tree. The original operator node is | |
2244 | -- then rewritten as a call to this function. We also use this in the | |
2245 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 2246 | |
a9d8907c JM |
2247 | declare |
2248 | Loc : constant Source_Ptr := Sloc (N); | |
2249 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
2250 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
2251 | Func_Body : Node_Id; | |
2252 | Func_Name : Entity_Id; | |
fbf5a39b | 2253 | |
a9d8907c JM |
2254 | begin |
2255 | Convert_To_Actual_Subtype (L); | |
2256 | Convert_To_Actual_Subtype (R); | |
2257 | Ensure_Defined (Etype (L), N); | |
2258 | Ensure_Defined (Etype (R), N); | |
2259 | Apply_Length_Check (R, Etype (L)); | |
2260 | ||
b4592168 GD |
2261 | if Nkind (N) = N_Op_Xor then |
2262 | Silly_Boolean_Array_Xor_Test (N, Etype (L)); | |
2263 | end if; | |
2264 | ||
a9d8907c JM |
2265 | if Nkind (Parent (N)) = N_Assignment_Statement |
2266 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
2267 | then | |
2268 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 2269 | |
a9d8907c JM |
2270 | elsif Nkind (Parent (N)) = N_Op_Not |
2271 | and then Nkind (N) = N_Op_And | |
cc6f5d75 | 2272 | and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
2273 | then |
2274 | return; | |
2275 | else | |
fbf5a39b | 2276 | |
a9d8907c JM |
2277 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
2278 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
2279 | Insert_Action (N, Func_Body); | |
70482933 | 2280 | |
a9d8907c | 2281 | -- Now rewrite the expression with a call |
70482933 | 2282 | |
a9d8907c JM |
2283 | Rewrite (N, |
2284 | Make_Function_Call (Loc, | |
e4494292 | 2285 | Name => New_Occurrence_Of (Func_Name, Loc), |
a9d8907c JM |
2286 | Parameter_Associations => |
2287 | New_List ( | |
2288 | L, | |
2289 | Make_Type_Conversion | |
e4494292 | 2290 | (Loc, New_Occurrence_Of (Etype (L), Loc), R)))); |
70482933 | 2291 | |
a9d8907c JM |
2292 | Analyze_And_Resolve (N, Typ); |
2293 | end if; | |
2294 | end; | |
70482933 RK |
2295 | end Expand_Boolean_Operator; |
2296 | ||
456cbfa5 AC |
2297 | ------------------------------------------------ |
2298 | -- Expand_Compare_Minimize_Eliminate_Overflow -- | |
2299 | ------------------------------------------------ | |
2300 | ||
2301 | procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is | |
2302 | Loc : constant Source_Ptr := Sloc (N); | |
2303 | ||
71fb4dc8 AC |
2304 | Result_Type : constant Entity_Id := Etype (N); |
2305 | -- Capture result type (could be a derived boolean type) | |
2306 | ||
456cbfa5 AC |
2307 | Llo, Lhi : Uint; |
2308 | Rlo, Rhi : Uint; | |
2309 | ||
2310 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
2311 | -- Entity for Long_Long_Integer'Base | |
2312 | ||
15c94a55 | 2313 | Check : constant Overflow_Mode_Type := Overflow_Check_Mode; |
a7f1b24f | 2314 | -- Current overflow checking mode |
456cbfa5 AC |
2315 | |
2316 | procedure Set_True; | |
2317 | procedure Set_False; | |
2318 | -- These procedures rewrite N with an occurrence of Standard_True or | |
2319 | -- Standard_False, and then makes a call to Warn_On_Known_Condition. | |
2320 | ||
2321 | --------------- | |
2322 | -- Set_False -- | |
2323 | --------------- | |
2324 | ||
2325 | procedure Set_False is | |
2326 | begin | |
2327 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
2328 | Warn_On_Known_Condition (N); | |
2329 | end Set_False; | |
2330 | ||
2331 | -------------- | |
2332 | -- Set_True -- | |
2333 | -------------- | |
2334 | ||
2335 | procedure Set_True is | |
2336 | begin | |
2337 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
2338 | Warn_On_Known_Condition (N); | |
2339 | end Set_True; | |
2340 | ||
2341 | -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow | |
2342 | ||
2343 | begin | |
2344 | -- Nothing to do unless we have a comparison operator with operands | |
2345 | -- that are signed integer types, and we are operating in either | |
2346 | -- MINIMIZED or ELIMINATED overflow checking mode. | |
2347 | ||
2348 | if Nkind (N) not in N_Op_Compare | |
2349 | or else Check not in Minimized_Or_Eliminated | |
2350 | or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N))) | |
2351 | then | |
2352 | return; | |
2353 | end if; | |
2354 | ||
2355 | -- OK, this is the case we are interested in. First step is to process | |
2356 | -- our operands using the Minimize_Eliminate circuitry which applies | |
2357 | -- this processing to the two operand subtrees. | |
2358 | ||
a7f1b24f | 2359 | Minimize_Eliminate_Overflows |
c7e152b5 | 2360 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
a7f1b24f | 2361 | Minimize_Eliminate_Overflows |
c7e152b5 | 2362 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
456cbfa5 | 2363 | |
65f7ed64 AC |
2364 | -- See if the range information decides the result of the comparison. |
2365 | -- We can only do this if we in fact have full range information (which | |
2366 | -- won't be the case if either operand is bignum at this stage). | |
456cbfa5 | 2367 | |
65f7ed64 AC |
2368 | if Llo /= No_Uint and then Rlo /= No_Uint then |
2369 | case N_Op_Compare (Nkind (N)) is | |
456cbfa5 AC |
2370 | when N_Op_Eq => |
2371 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
2372 | Set_True; | |
a40ada7e | 2373 | elsif Llo > Rhi or else Lhi < Rlo then |
456cbfa5 AC |
2374 | Set_False; |
2375 | end if; | |
2376 | ||
2377 | when N_Op_Ge => | |
2378 | if Llo >= Rhi then | |
2379 | Set_True; | |
2380 | elsif Lhi < Rlo then | |
2381 | Set_False; | |
2382 | end if; | |
2383 | ||
2384 | when N_Op_Gt => | |
2385 | if Llo > Rhi then | |
2386 | Set_True; | |
2387 | elsif Lhi <= Rlo then | |
2388 | Set_False; | |
2389 | end if; | |
2390 | ||
2391 | when N_Op_Le => | |
2392 | if Llo > Rhi then | |
2393 | Set_False; | |
2394 | elsif Lhi <= Rlo then | |
2395 | Set_True; | |
2396 | end if; | |
2397 | ||
2398 | when N_Op_Lt => | |
2399 | if Llo >= Rhi then | |
456cbfa5 | 2400 | Set_False; |
b6b5cca8 AC |
2401 | elsif Lhi < Rlo then |
2402 | Set_True; | |
456cbfa5 AC |
2403 | end if; |
2404 | ||
2405 | when N_Op_Ne => | |
2406 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
456cbfa5 | 2407 | Set_False; |
a40ada7e RD |
2408 | elsif Llo > Rhi or else Lhi < Rlo then |
2409 | Set_True; | |
456cbfa5 | 2410 | end if; |
65f7ed64 | 2411 | end case; |
456cbfa5 | 2412 | |
65f7ed64 | 2413 | -- All done if we did the rewrite |
456cbfa5 | 2414 | |
65f7ed64 AC |
2415 | if Nkind (N) not in N_Op_Compare then |
2416 | return; | |
2417 | end if; | |
456cbfa5 AC |
2418 | end if; |
2419 | ||
2420 | -- Otherwise, time to do the comparison | |
2421 | ||
2422 | declare | |
2423 | Ltype : constant Entity_Id := Etype (Left_Opnd (N)); | |
2424 | Rtype : constant Entity_Id := Etype (Right_Opnd (N)); | |
2425 | ||
2426 | begin | |
2427 | -- If the two operands have the same signed integer type we are | |
2428 | -- all set, nothing more to do. This is the case where either | |
2429 | -- both operands were unchanged, or we rewrote both of them to | |
2430 | -- be Long_Long_Integer. | |
2431 | ||
2432 | -- Note: Entity for the comparison may be wrong, but it's not worth | |
2433 | -- the effort to change it, since the back end does not use it. | |
2434 | ||
2435 | if Is_Signed_Integer_Type (Ltype) | |
2436 | and then Base_Type (Ltype) = Base_Type (Rtype) | |
2437 | then | |
2438 | return; | |
2439 | ||
2440 | -- Here if bignums are involved (can only happen in ELIMINATED mode) | |
2441 | ||
2442 | elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then | |
2443 | declare | |
2444 | Left : Node_Id := Left_Opnd (N); | |
2445 | Right : Node_Id := Right_Opnd (N); | |
2446 | -- Bignum references for left and right operands | |
2447 | ||
2448 | begin | |
2449 | if not Is_RTE (Ltype, RE_Bignum) then | |
2450 | Left := Convert_To_Bignum (Left); | |
2451 | elsif not Is_RTE (Rtype, RE_Bignum) then | |
2452 | Right := Convert_To_Bignum (Right); | |
2453 | end if; | |
2454 | ||
71fb4dc8 | 2455 | -- We rewrite our node with: |
456cbfa5 | 2456 | |
71fb4dc8 AC |
2457 | -- do |
2458 | -- Bnn : Result_Type; | |
2459 | -- declare | |
2460 | -- M : Mark_Id := SS_Mark; | |
2461 | -- begin | |
2462 | -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc) | |
2463 | -- SS_Release (M); | |
2464 | -- end; | |
2465 | -- in | |
2466 | -- Bnn | |
2467 | -- end | |
456cbfa5 AC |
2468 | |
2469 | declare | |
71fb4dc8 | 2470 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
456cbfa5 AC |
2471 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); |
2472 | Ent : RE_Id; | |
2473 | ||
2474 | begin | |
2475 | case N_Op_Compare (Nkind (N)) is | |
2476 | when N_Op_Eq => Ent := RE_Big_EQ; | |
2477 | when N_Op_Ge => Ent := RE_Big_GE; | |
2478 | when N_Op_Gt => Ent := RE_Big_GT; | |
2479 | when N_Op_Le => Ent := RE_Big_LE; | |
2480 | when N_Op_Lt => Ent := RE_Big_LT; | |
2481 | when N_Op_Ne => Ent := RE_Big_NE; | |
2482 | end case; | |
2483 | ||
71fb4dc8 | 2484 | -- Insert assignment to Bnn into the bignum block |
456cbfa5 AC |
2485 | |
2486 | Insert_Before | |
2487 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
2488 | Make_Assignment_Statement (Loc, | |
2489 | Name => New_Occurrence_Of (Bnn, Loc), | |
2490 | Expression => | |
2491 | Make_Function_Call (Loc, | |
2492 | Name => | |
2493 | New_Occurrence_Of (RTE (Ent), Loc), | |
2494 | Parameter_Associations => New_List (Left, Right)))); | |
2495 | ||
71fb4dc8 AC |
2496 | -- Now do the rewrite with expression actions |
2497 | ||
2498 | Rewrite (N, | |
2499 | Make_Expression_With_Actions (Loc, | |
2500 | Actions => New_List ( | |
2501 | Make_Object_Declaration (Loc, | |
2502 | Defining_Identifier => Bnn, | |
2503 | Object_Definition => | |
2504 | New_Occurrence_Of (Result_Type, Loc)), | |
2505 | Blk), | |
2506 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
2507 | Analyze_And_Resolve (N, Result_Type); | |
456cbfa5 AC |
2508 | end; |
2509 | end; | |
2510 | ||
2511 | -- No bignums involved, but types are different, so we must have | |
2512 | -- rewritten one of the operands as a Long_Long_Integer but not | |
2513 | -- the other one. | |
2514 | ||
2515 | -- If left operand is Long_Long_Integer, convert right operand | |
2516 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2517 | ||
2518 | elsif Ltype = LLIB then | |
2519 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
2520 | Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks); | |
2521 | return; | |
2522 | ||
2523 | -- If right operand is Long_Long_Integer, convert left operand | |
2524 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2525 | ||
2526 | -- This is the only remaining possibility | |
2527 | ||
2528 | else pragma Assert (Rtype = LLIB); | |
2529 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
2530 | Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks); | |
2531 | return; | |
2532 | end if; | |
2533 | end; | |
2534 | end Expand_Compare_Minimize_Eliminate_Overflow; | |
2535 | ||
70482933 RK |
2536 | ------------------------------- |
2537 | -- Expand_Composite_Equality -- | |
2538 | ------------------------------- | |
2539 | ||
2540 | -- This function is only called for comparing internal fields of composite | |
2541 | -- types when these fields are themselves composites. This is a special | |
2542 | -- case because it is not possible to respect normal Ada visibility rules. | |
2543 | ||
2544 | function Expand_Composite_Equality | |
2545 | (Nod : Node_Id; | |
2546 | Typ : Entity_Id; | |
2547 | Lhs : Node_Id; | |
2548 | Rhs : Node_Id; | |
2e071734 | 2549 | Bodies : List_Id) return Node_Id |
70482933 RK |
2550 | is |
2551 | Loc : constant Source_Ptr := Sloc (Nod); | |
2552 | Full_Type : Entity_Id; | |
2553 | Prim : Elmt_Id; | |
2554 | Eq_Op : Entity_Id; | |
2555 | ||
7efc3f2d AC |
2556 | function Find_Primitive_Eq return Node_Id; |
2557 | -- AI05-0123: Locate primitive equality for type if it exists, and | |
2558 | -- build the corresponding call. If operation is abstract, replace | |
2559 | -- call with an explicit raise. Return Empty if there is no primitive. | |
2560 | ||
2561 | ----------------------- | |
2562 | -- Find_Primitive_Eq -- | |
2563 | ----------------------- | |
2564 | ||
2565 | function Find_Primitive_Eq return Node_Id is | |
2566 | Prim_E : Elmt_Id; | |
2567 | Prim : Node_Id; | |
2568 | ||
2569 | begin | |
2570 | Prim_E := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2571 | while Present (Prim_E) loop | |
2572 | Prim := Node (Prim_E); | |
2573 | ||
2574 | -- Locate primitive equality with the right signature | |
2575 | ||
2576 | if Chars (Prim) = Name_Op_Eq | |
2577 | and then Etype (First_Formal (Prim)) = | |
39ade2f9 | 2578 | Etype (Next_Formal (First_Formal (Prim))) |
7efc3f2d AC |
2579 | and then Etype (Prim) = Standard_Boolean |
2580 | then | |
2581 | if Is_Abstract_Subprogram (Prim) then | |
2582 | return | |
2583 | Make_Raise_Program_Error (Loc, | |
2584 | Reason => PE_Explicit_Raise); | |
2585 | ||
2586 | else | |
2587 | return | |
2588 | Make_Function_Call (Loc, | |
e4494292 | 2589 | Name => New_Occurrence_Of (Prim, Loc), |
7efc3f2d AC |
2590 | Parameter_Associations => New_List (Lhs, Rhs)); |
2591 | end if; | |
2592 | end if; | |
2593 | ||
2594 | Next_Elmt (Prim_E); | |
2595 | end loop; | |
2596 | ||
2597 | -- If not found, predefined operation will be used | |
2598 | ||
2599 | return Empty; | |
2600 | end Find_Primitive_Eq; | |
2601 | ||
2602 | -- Start of processing for Expand_Composite_Equality | |
2603 | ||
70482933 RK |
2604 | begin |
2605 | if Is_Private_Type (Typ) then | |
2606 | Full_Type := Underlying_Type (Typ); | |
2607 | else | |
2608 | Full_Type := Typ; | |
2609 | end if; | |
2610 | ||
ced8450b ES |
2611 | -- If the private type has no completion the context may be the |
2612 | -- expansion of a composite equality for a composite type with some | |
2613 | -- still incomplete components. The expression will not be analyzed | |
2614 | -- until the enclosing type is completed, at which point this will be | |
2615 | -- properly expanded, unless there is a bona fide completion error. | |
70482933 RK |
2616 | |
2617 | if No (Full_Type) then | |
ced8450b | 2618 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 RK |
2619 | end if; |
2620 | ||
2621 | Full_Type := Base_Type (Full_Type); | |
2622 | ||
da1b76c1 HK |
2623 | -- When the base type itself is private, use the full view to expand |
2624 | -- the composite equality. | |
2625 | ||
2626 | if Is_Private_Type (Full_Type) then | |
2627 | Full_Type := Underlying_Type (Full_Type); | |
2628 | end if; | |
2629 | ||
16788d44 RD |
2630 | -- Case of array types |
2631 | ||
70482933 RK |
2632 | if Is_Array_Type (Full_Type) then |
2633 | ||
2634 | -- If the operand is an elementary type other than a floating-point | |
2635 | -- type, then we can simply use the built-in block bitwise equality, | |
2636 | -- since the predefined equality operators always apply and bitwise | |
2637 | -- equality is fine for all these cases. | |
2638 | ||
2639 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
2640 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
2641 | then | |
39ade2f9 | 2642 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 | 2643 | |
685094bf RD |
2644 | -- For composite component types, and floating-point types, use the |
2645 | -- expansion. This deals with tagged component types (where we use | |
2646 | -- the applicable equality routine) and floating-point, (where we | |
2647 | -- need to worry about negative zeroes), and also the case of any | |
2648 | -- composite type recursively containing such fields. | |
70482933 RK |
2649 | |
2650 | else | |
0da2c8ac | 2651 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
2652 | end if; |
2653 | ||
16788d44 RD |
2654 | -- Case of tagged record types |
2655 | ||
70482933 RK |
2656 | elsif Is_Tagged_Type (Full_Type) then |
2657 | ||
2658 | -- Call the primitive operation "=" of this type | |
2659 | ||
2660 | if Is_Class_Wide_Type (Full_Type) then | |
2661 | Full_Type := Root_Type (Full_Type); | |
2662 | end if; | |
2663 | ||
685094bf RD |
2664 | -- If this is derived from an untagged private type completed with a |
2665 | -- tagged type, it does not have a full view, so we use the primitive | |
2666 | -- operations of the private type. This check should no longer be | |
2667 | -- necessary when these types receive their full views ??? | |
70482933 RK |
2668 | |
2669 | if Is_Private_Type (Typ) | |
2670 | and then not Is_Tagged_Type (Typ) | |
2671 | and then not Is_Controlled (Typ) | |
2672 | and then Is_Derived_Type (Typ) | |
2673 | and then No (Full_View (Typ)) | |
2674 | then | |
2675 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2676 | else | |
2677 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
2678 | end if; | |
2679 | ||
2680 | loop | |
2681 | Eq_Op := Node (Prim); | |
2682 | exit when Chars (Eq_Op) = Name_Op_Eq | |
2683 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
2684 | Etype (Next_Formal (First_Formal (Eq_Op))) |
2685 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
2686 | Next_Elmt (Prim); |
2687 | pragma Assert (Present (Prim)); | |
2688 | end loop; | |
2689 | ||
2690 | Eq_Op := Node (Prim); | |
2691 | ||
2692 | return | |
2693 | Make_Function_Call (Loc, | |
e4494292 | 2694 | Name => New_Occurrence_Of (Eq_Op, Loc), |
70482933 RK |
2695 | Parameter_Associations => |
2696 | New_List | |
2697 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2698 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2699 | ||
16788d44 RD |
2700 | -- Case of untagged record types |
2701 | ||
70482933 | 2702 | elsif Is_Record_Type (Full_Type) then |
fbf5a39b | 2703 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2704 | |
2705 | if Present (Eq_Op) then | |
2706 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2707 | ||
685094bf RD |
2708 | -- Inherited equality from parent type. Convert the actuals to |
2709 | -- match signature of operation. | |
70482933 RK |
2710 | |
2711 | declare | |
fbf5a39b | 2712 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2713 | |
2714 | begin | |
2715 | return | |
2716 | Make_Function_Call (Loc, | |
e4494292 | 2717 | Name => New_Occurrence_Of (Eq_Op, Loc), |
39ade2f9 AC |
2718 | Parameter_Associations => New_List ( |
2719 | OK_Convert_To (T, Lhs), | |
2720 | OK_Convert_To (T, Rhs))); | |
70482933 RK |
2721 | end; |
2722 | ||
2723 | else | |
5d09245e AC |
2724 | -- Comparison between Unchecked_Union components |
2725 | ||
2726 | if Is_Unchecked_Union (Full_Type) then | |
2727 | declare | |
2728 | Lhs_Type : Node_Id := Full_Type; | |
2729 | Rhs_Type : Node_Id := Full_Type; | |
2730 | Lhs_Discr_Val : Node_Id; | |
2731 | Rhs_Discr_Val : Node_Id; | |
2732 | ||
2733 | begin | |
2734 | -- Lhs subtype | |
2735 | ||
2736 | if Nkind (Lhs) = N_Selected_Component then | |
2737 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2738 | end if; | |
2739 | ||
2740 | -- Rhs subtype | |
2741 | ||
2742 | if Nkind (Rhs) = N_Selected_Component then | |
2743 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2744 | end if; | |
2745 | ||
2746 | -- Lhs of the composite equality | |
2747 | ||
2748 | if Is_Constrained (Lhs_Type) then | |
2749 | ||
685094bf | 2750 | -- Since the enclosing record type can never be an |
5d09245e AC |
2751 | -- Unchecked_Union (this code is executed for records |
2752 | -- that do not have variants), we may reference its | |
2753 | -- discriminant(s). | |
2754 | ||
2755 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
2756 | and then Has_Per_Object_Constraint |
2757 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
2758 | then |
2759 | Lhs_Discr_Val := | |
2760 | Make_Selected_Component (Loc, | |
39ade2f9 | 2761 | Prefix => Prefix (Lhs), |
5d09245e | 2762 | Selector_Name => |
39ade2f9 AC |
2763 | New_Copy |
2764 | (Get_Discriminant_Value | |
2765 | (First_Discriminant (Lhs_Type), | |
2766 | Lhs_Type, | |
2767 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
2768 | |
2769 | else | |
39ade2f9 AC |
2770 | Lhs_Discr_Val := |
2771 | New_Copy | |
2772 | (Get_Discriminant_Value | |
2773 | (First_Discriminant (Lhs_Type), | |
2774 | Lhs_Type, | |
2775 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
2776 | |
2777 | end if; | |
2778 | else | |
2779 | -- It is not possible to infer the discriminant since | |
2780 | -- the subtype is not constrained. | |
2781 | ||
8aceda64 | 2782 | return |
5d09245e | 2783 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2784 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2785 | end if; |
2786 | ||
2787 | -- Rhs of the composite equality | |
2788 | ||
2789 | if Is_Constrained (Rhs_Type) then | |
2790 | if Nkind (Rhs) = N_Selected_Component | |
39ade2f9 AC |
2791 | and then Has_Per_Object_Constraint |
2792 | (Entity (Selector_Name (Rhs))) | |
5d09245e AC |
2793 | then |
2794 | Rhs_Discr_Val := | |
2795 | Make_Selected_Component (Loc, | |
39ade2f9 | 2796 | Prefix => Prefix (Rhs), |
5d09245e | 2797 | Selector_Name => |
39ade2f9 AC |
2798 | New_Copy |
2799 | (Get_Discriminant_Value | |
2800 | (First_Discriminant (Rhs_Type), | |
2801 | Rhs_Type, | |
2802 | Stored_Constraint (Rhs_Type)))); | |
5d09245e AC |
2803 | |
2804 | else | |
39ade2f9 AC |
2805 | Rhs_Discr_Val := |
2806 | New_Copy | |
2807 | (Get_Discriminant_Value | |
2808 | (First_Discriminant (Rhs_Type), | |
2809 | Rhs_Type, | |
2810 | Stored_Constraint (Rhs_Type))); | |
5d09245e AC |
2811 | |
2812 | end if; | |
2813 | else | |
8aceda64 | 2814 | return |
5d09245e | 2815 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2816 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2817 | end if; |
2818 | ||
2819 | -- Call the TSS equality function with the inferred | |
2820 | -- discriminant values. | |
2821 | ||
2822 | return | |
2823 | Make_Function_Call (Loc, | |
e4494292 | 2824 | Name => New_Occurrence_Of (Eq_Op, Loc), |
5d09245e AC |
2825 | Parameter_Associations => New_List ( |
2826 | Lhs, | |
2827 | Rhs, | |
2828 | Lhs_Discr_Val, | |
2829 | Rhs_Discr_Val)); | |
2830 | end; | |
d151d6a3 | 2831 | |
316e3a13 RD |
2832 | -- All cases other than comparing Unchecked_Union types |
2833 | ||
d151d6a3 | 2834 | else |
7f1a5156 EB |
2835 | declare |
2836 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
7f1a5156 EB |
2837 | begin |
2838 | return | |
2839 | Make_Function_Call (Loc, | |
316e3a13 RD |
2840 | Name => |
2841 | New_Occurrence_Of (Eq_Op, Loc), | |
7f1a5156 EB |
2842 | Parameter_Associations => New_List ( |
2843 | OK_Convert_To (T, Lhs), | |
2844 | OK_Convert_To (T, Rhs))); | |
2845 | end; | |
5d09245e | 2846 | end if; |
d151d6a3 | 2847 | end if; |
5d09245e | 2848 | |
3058f181 BD |
2849 | -- Equality composes in Ada 2012 for untagged record types. It also |
2850 | -- composes for bounded strings, because they are part of the | |
2851 | -- predefined environment. We could make it compose for bounded | |
2852 | -- strings by making them tagged, or by making sure all subcomponents | |
2853 | -- are set to the same value, even when not used. Instead, we have | |
2854 | -- this special case in the compiler, because it's more efficient. | |
2855 | ||
2856 | elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then | |
5d09245e | 2857 | |
08daa782 | 2858 | -- If no TSS has been created for the type, check whether there is |
7efc3f2d | 2859 | -- a primitive equality declared for it. |
d151d6a3 AC |
2860 | |
2861 | declare | |
3058f181 | 2862 | Op : constant Node_Id := Find_Primitive_Eq; |
d151d6a3 AC |
2863 | |
2864 | begin | |
a1fc903a AC |
2865 | -- Use user-defined primitive if it exists, otherwise use |
2866 | -- predefined equality. | |
2867 | ||
3058f181 BD |
2868 | if Present (Op) then |
2869 | return Op; | |
7efc3f2d | 2870 | else |
7efc3f2d AC |
2871 | return Make_Op_Eq (Loc, Lhs, Rhs); |
2872 | end if; | |
d151d6a3 AC |
2873 | end; |
2874 | ||
70482933 RK |
2875 | else |
2876 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2877 | end if; | |
2878 | ||
16788d44 | 2879 | -- Non-composite types (always use predefined equality) |
70482933 | 2880 | |
16788d44 | 2881 | else |
70482933 RK |
2882 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
2883 | end if; | |
2884 | end Expand_Composite_Equality; | |
2885 | ||
fdac1f80 AC |
2886 | ------------------------ |
2887 | -- Expand_Concatenate -- | |
2888 | ------------------------ | |
70482933 | 2889 | |
fdac1f80 AC |
2890 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2891 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2892 | |
fdac1f80 AC |
2893 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2894 | -- Result type of concatenation | |
70482933 | 2895 | |
fdac1f80 AC |
2896 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2897 | -- Component type. Elements of this component type can appear as one | |
2898 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2899 | |
ecc4ddde AC |
2900 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2901 | -- Index subtype | |
2902 | ||
2903 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2904 | -- Index type. This is the base type of the index subtype, and is used | |
2905 | -- for all computed bounds (which may be out of range of Istyp in the | |
2906 | -- case of null ranges). | |
70482933 | 2907 | |
46ff89f3 | 2908 | Artyp : Entity_Id; |
fdac1f80 AC |
2909 | -- This is the type we use to do arithmetic to compute the bounds and |
2910 | -- lengths of operands. The choice of this type is a little subtle and | |
2911 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2912 | |
fdac1f80 AC |
2913 | Concatenation_Error : exception; |
2914 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2915 | |
0ac73189 AC |
2916 | Result_May_Be_Null : Boolean := True; |
2917 | -- Reset to False if at least one operand is encountered which is known | |
2918 | -- at compile time to be non-null. Used for handling the special case | |
2919 | -- of setting the high bound to the last operand high bound for a null | |
2920 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2921 | ||
df46b832 | 2922 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2923 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2924 | |
2925 | NN : Nat := 0; | |
a29262fd AC |
2926 | -- Number of operands excluding any known to be null, except that the |
2927 | -- last operand is always retained, in case it provides the bounds for | |
2928 | -- a null result. | |
2929 | ||
2930 | Opnd : Node_Id; | |
2931 | -- Current operand being processed in the loop through operands. After | |
2932 | -- this loop is complete, always contains the last operand (which is not | |
2933 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2934 | |
2935 | -- Arrays describing the operands, only the first NN entries of each | |
2936 | -- array are set (NN < N when we exclude known null operands). | |
2937 | ||
2938 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2939 | -- True if length of corresponding operand known at compile time | |
2940 | ||
2941 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2942 | -- Set to the corresponding entry in the Opnds list (but note that null |
2943 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2944 | |
2945 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2946 | -- Set to length of operand. Entries in this array are set only if the |
2947 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2948 | |
0ac73189 AC |
2949 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2950 | -- Set to lower bound of operand. Either an integer literal in the case | |
2951 | -- where the bound is known at compile time, else actual lower bound. | |
2952 | -- The operand low bound is of type Ityp. | |
2953 | ||
df46b832 AC |
2954 | Var_Length : array (1 .. N) of Entity_Id; |
2955 | -- Set to an entity of type Natural that contains the length of an | |
2956 | -- operand whose length is not known at compile time. Entries in this | |
2957 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2958 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2959 | |
2960 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2961 | -- The J'th entry in an expression node that represents the total length |
2962 | -- of operands 1 through J. It is either an integer literal node, or a | |
2963 | -- reference to a constant entity with the right value, so it is fine | |
2964 | -- to just do a Copy_Node to get an appropriate copy. The extra zero'th | |
46ff89f3 | 2965 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 AC |
2966 | |
2967 | Low_Bound : Node_Id; | |
0ac73189 AC |
2968 | -- A tree node representing the low bound of the result (of type Ityp). |
2969 | -- This is either an integer literal node, or an identifier reference to | |
2970 | -- a constant entity initialized to the appropriate value. | |
2971 | ||
88a27b18 AC |
2972 | Last_Opnd_Low_Bound : Node_Id; |
2973 | -- A tree node representing the low bound of the last operand. This | |
2974 | -- need only be set if the result could be null. It is used for the | |
2975 | -- special case of setting the right low bound for a null result. | |
2976 | -- This is of type Ityp. | |
2977 | ||
a29262fd AC |
2978 | Last_Opnd_High_Bound : Node_Id; |
2979 | -- A tree node representing the high bound of the last operand. This | |
2980 | -- need only be set if the result could be null. It is used for the | |
2981 | -- special case of setting the right high bound for a null result. | |
2982 | -- This is of type Ityp. | |
2983 | ||
0ac73189 AC |
2984 | High_Bound : Node_Id; |
2985 | -- A tree node representing the high bound of the result (of type Ityp) | |
df46b832 AC |
2986 | |
2987 | Result : Node_Id; | |
0ac73189 | 2988 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2989 | |
d0f8d157 | 2990 | Actions : constant List_Id := New_List; |
4c9fe6c7 | 2991 | -- Collect actions to be inserted |
d0f8d157 | 2992 | |
fa969310 | 2993 | Known_Non_Null_Operand_Seen : Boolean; |
308e6f3a | 2994 | -- Set True during generation of the assignments of operands into |
fa969310 AC |
2995 | -- result once an operand known to be non-null has been seen. |
2996 | ||
2997 | function Make_Artyp_Literal (Val : Nat) return Node_Id; | |
2998 | -- This function makes an N_Integer_Literal node that is returned in | |
2999 | -- analyzed form with the type set to Artyp. Importantly this literal | |
3000 | -- is not flagged as static, so that if we do computations with it that | |
3001 | -- result in statically detected out of range conditions, we will not | |
3002 | -- generate error messages but instead warning messages. | |
3003 | ||
46ff89f3 | 3004 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 3005 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
3006 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
3007 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
3008 | |
3009 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 3010 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 3011 | |
fa969310 AC |
3012 | ------------------------ |
3013 | -- Make_Artyp_Literal -- | |
3014 | ------------------------ | |
3015 | ||
3016 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
3017 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
3018 | begin | |
3019 | Set_Etype (Result, Artyp); | |
3020 | Set_Analyzed (Result, True); | |
3021 | Set_Is_Static_Expression (Result, False); | |
3022 | return Result; | |
3023 | end Make_Artyp_Literal; | |
76c597a1 | 3024 | |
fdac1f80 | 3025 | -------------- |
46ff89f3 | 3026 | -- To_Artyp -- |
fdac1f80 AC |
3027 | -------------- |
3028 | ||
46ff89f3 | 3029 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 3030 | begin |
46ff89f3 | 3031 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
3032 | return X; |
3033 | ||
3034 | elsif Is_Enumeration_Type (Ityp) then | |
3035 | return | |
3036 | Make_Attribute_Reference (Loc, | |
3037 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
3038 | Attribute_Name => Name_Pos, | |
3039 | Expressions => New_List (X)); | |
3040 | ||
3041 | else | |
46ff89f3 | 3042 | return Convert_To (Artyp, X); |
fdac1f80 | 3043 | end if; |
46ff89f3 | 3044 | end To_Artyp; |
fdac1f80 AC |
3045 | |
3046 | ------------- | |
3047 | -- To_Ityp -- | |
3048 | ------------- | |
3049 | ||
3050 | function To_Ityp (X : Node_Id) return Node_Id is | |
3051 | begin | |
2fc05e3d | 3052 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
3053 | return |
3054 | Make_Attribute_Reference (Loc, | |
3055 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
3056 | Attribute_Name => Name_Val, | |
3057 | Expressions => New_List (X)); | |
3058 | ||
3059 | -- Case where we will do a type conversion | |
3060 | ||
3061 | else | |
76c597a1 AC |
3062 | if Ityp = Base_Type (Artyp) then |
3063 | return X; | |
fdac1f80 | 3064 | else |
76c597a1 | 3065 | return Convert_To (Ityp, X); |
fdac1f80 AC |
3066 | end if; |
3067 | end if; | |
3068 | end To_Ityp; | |
3069 | ||
3070 | -- Local Declarations | |
3071 | ||
00ba7be8 AC |
3072 | Lib_Level_Target : constant Boolean := |
3073 | Nkind (Parent (Cnode)) = N_Object_Declaration | |
3074 | and then | |
3075 | Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode))); | |
3076 | ||
3077 | -- If the concatenation declares a library level entity, we call the | |
3078 | -- built-in concatenation routines to prevent code bloat, regardless | |
3079 | -- of optimization level. This is space-efficient, and prevent linking | |
3080 | -- problems when units are compiled with different optimizations. | |
3081 | ||
0ac73189 AC |
3082 | Opnd_Typ : Entity_Id; |
3083 | Ent : Entity_Id; | |
3084 | Len : Uint; | |
3085 | J : Nat; | |
3086 | Clen : Node_Id; | |
3087 | Set : Boolean; | |
70482933 | 3088 | |
f46faa08 AC |
3089 | -- Start of processing for Expand_Concatenate |
3090 | ||
70482933 | 3091 | begin |
fdac1f80 AC |
3092 | -- Choose an appropriate computational type |
3093 | ||
3094 | -- We will be doing calculations of lengths and bounds in this routine | |
3095 | -- and computing one from the other in some cases, e.g. getting the high | |
3096 | -- bound by adding the length-1 to the low bound. | |
3097 | ||
3098 | -- We can't just use the index type, or even its base type for this | |
3099 | -- purpose for two reasons. First it might be an enumeration type which | |
308e6f3a RW |
3100 | -- is not suitable for computations of any kind, and second it may |
3101 | -- simply not have enough range. For example if the index type is | |
3102 | -- -128..+127 then lengths can be up to 256, which is out of range of | |
3103 | -- the type. | |
fdac1f80 AC |
3104 | |
3105 | -- For enumeration types, we can simply use Standard_Integer, this is | |
3106 | -- sufficient since the actual number of enumeration literals cannot | |
3107 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 3108 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
3109 | |
3110 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 3111 | Artyp := Standard_Integer; |
fdac1f80 | 3112 | |
59262ebb AC |
3113 | -- If index type is Positive, we use the standard unsigned type, to give |
3114 | -- more room on the top of the range, obviating the need for an overflow | |
3115 | -- check when creating the upper bound. This is needed to avoid junk | |
3116 | -- overflow checks in the common case of String types. | |
3117 | ||
3118 | -- ??? Disabled for now | |
3119 | ||
3120 | -- elsif Istyp = Standard_Positive then | |
3121 | -- Artyp := Standard_Unsigned; | |
3122 | ||
2fc05e3d AC |
3123 | -- For modular types, we use a 32-bit modular type for types whose size |
3124 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
3125 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 3126 | |
2fc05e3d | 3127 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 3128 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 3129 | Artyp := Standard_Unsigned; |
ecc4ddde | 3130 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 3131 | Artyp := Ityp; |
fdac1f80 | 3132 | else |
46ff89f3 | 3133 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
3134 | end if; |
3135 | ||
2fc05e3d | 3136 | -- Similar treatment for signed types |
fdac1f80 AC |
3137 | |
3138 | else | |
ecc4ddde | 3139 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 3140 | Artyp := Standard_Integer; |
ecc4ddde | 3141 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 3142 | Artyp := Ityp; |
fdac1f80 | 3143 | else |
46ff89f3 | 3144 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
3145 | end if; |
3146 | end if; | |
3147 | ||
fa969310 AC |
3148 | -- Supply dummy entry at start of length array |
3149 | ||
3150 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
3151 | ||
fdac1f80 | 3152 | -- Go through operands setting up the above arrays |
70482933 | 3153 | |
df46b832 AC |
3154 | J := 1; |
3155 | while J <= N loop | |
3156 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 3157 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
3158 | |
3159 | -- The parent got messed up when we put the operands in a list, | |
d347f572 AC |
3160 | -- so now put back the proper parent for the saved operand, that |
3161 | -- is to say the concatenation node, to make sure that each operand | |
3162 | -- is seen as a subexpression, e.g. if actions must be inserted. | |
fdac1f80 | 3163 | |
d347f572 | 3164 | Set_Parent (Opnd, Cnode); |
fdac1f80 AC |
3165 | |
3166 | -- Set will be True when we have setup one entry in the array | |
3167 | ||
df46b832 AC |
3168 | Set := False; |
3169 | ||
fdac1f80 | 3170 | -- Singleton element (or character literal) case |
df46b832 | 3171 | |
0ac73189 | 3172 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
3173 | NN := NN + 1; |
3174 | Operands (NN) := Opnd; | |
3175 | Is_Fixed_Length (NN) := True; | |
3176 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 3177 | Result_May_Be_Null := False; |
fdac1f80 | 3178 | |
a29262fd AC |
3179 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
3180 | -- since we know that the result cannot be null). | |
fdac1f80 | 3181 | |
0ac73189 AC |
3182 | Opnd_Low_Bound (NN) := |
3183 | Make_Attribute_Reference (Loc, | |
e4494292 | 3184 | Prefix => New_Occurrence_Of (Istyp, Loc), |
0ac73189 AC |
3185 | Attribute_Name => Name_First); |
3186 | ||
df46b832 AC |
3187 | Set := True; |
3188 | ||
fdac1f80 | 3189 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
3190 | |
3191 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 3192 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 3193 | |
a29262fd AC |
3194 | if Len /= 0 then |
3195 | Result_May_Be_Null := False; | |
3196 | end if; | |
3197 | ||
88a27b18 | 3198 | -- Capture last operand low and high bound if result could be null |
a29262fd AC |
3199 | |
3200 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3201 | Last_Opnd_Low_Bound := |
3202 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3203 | ||
a29262fd | 3204 | Last_Opnd_High_Bound := |
88a27b18 | 3205 | Make_Op_Subtract (Loc, |
a29262fd AC |
3206 | Left_Opnd => |
3207 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 3208 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
3209 | end if; |
3210 | ||
3211 | -- Skip null string literal | |
fdac1f80 | 3212 | |
0ac73189 | 3213 | if J < N and then Len = 0 then |
df46b832 AC |
3214 | goto Continue; |
3215 | end if; | |
3216 | ||
3217 | NN := NN + 1; | |
3218 | Operands (NN) := Opnd; | |
3219 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
3220 | |
3221 | -- Set length and bounds | |
3222 | ||
df46b832 | 3223 | Fixed_Length (NN) := Len; |
0ac73189 AC |
3224 | |
3225 | Opnd_Low_Bound (NN) := | |
3226 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3227 | ||
df46b832 AC |
3228 | Set := True; |
3229 | ||
3230 | -- All other cases | |
3231 | ||
3232 | else | |
3233 | -- Check constrained case with known bounds | |
3234 | ||
0ac73189 | 3235 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 3236 | declare |
df46b832 AC |
3237 | Index : constant Node_Id := First_Index (Opnd_Typ); |
3238 | Indx_Typ : constant Entity_Id := Etype (Index); | |
3239 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
3240 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
3241 | ||
3242 | begin | |
fdac1f80 AC |
3243 | -- Fixed length constrained array type with known at compile |
3244 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
3245 | |
3246 | if Compile_Time_Known_Value (Lo) | |
3247 | and then | |
3248 | Compile_Time_Known_Value (Hi) | |
3249 | then | |
3250 | declare | |
3251 | Loval : constant Uint := Expr_Value (Lo); | |
3252 | Hival : constant Uint := Expr_Value (Hi); | |
3253 | Len : constant Uint := | |
3254 | UI_Max (Hival - Loval + 1, Uint_0); | |
3255 | ||
3256 | begin | |
0ac73189 AC |
3257 | if Len > 0 then |
3258 | Result_May_Be_Null := False; | |
df46b832 | 3259 | end if; |
0ac73189 | 3260 | |
88a27b18 | 3261 | -- Capture last operand bounds if result could be null |
a29262fd AC |
3262 | |
3263 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3264 | Last_Opnd_Low_Bound := |
3265 | Convert_To (Ityp, | |
3266 | Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
3267 | ||
a29262fd AC |
3268 | Last_Opnd_High_Bound := |
3269 | Convert_To (Ityp, | |
39ade2f9 | 3270 | Make_Integer_Literal (Loc, Expr_Value (Hi))); |
a29262fd AC |
3271 | end if; |
3272 | ||
3273 | -- Exclude null length case unless last operand | |
0ac73189 | 3274 | |
a29262fd | 3275 | if J < N and then Len = 0 then |
0ac73189 AC |
3276 | goto Continue; |
3277 | end if; | |
3278 | ||
3279 | NN := NN + 1; | |
3280 | Operands (NN) := Opnd; | |
3281 | Is_Fixed_Length (NN) := True; | |
3282 | Fixed_Length (NN) := Len; | |
3283 | ||
39ade2f9 AC |
3284 | Opnd_Low_Bound (NN) := |
3285 | To_Ityp | |
3286 | (Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
0ac73189 | 3287 | Set := True; |
df46b832 AC |
3288 | end; |
3289 | end if; | |
3290 | end; | |
3291 | end if; | |
3292 | ||
0ac73189 AC |
3293 | -- All cases where the length is not known at compile time, or the |
3294 | -- special case of an operand which is known to be null but has a | |
3295 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
3296 | |
3297 | if not Set then | |
3298 | NN := NN + 1; | |
0ac73189 AC |
3299 | |
3300 | -- Capture operand bounds | |
3301 | ||
3302 | Opnd_Low_Bound (NN) := | |
3303 | Make_Attribute_Reference (Loc, | |
3304 | Prefix => | |
3305 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3306 | Attribute_Name => Name_First); | |
3307 | ||
88a27b18 AC |
3308 | -- Capture last operand bounds if result could be null |
3309 | ||
a29262fd | 3310 | if J = N and Result_May_Be_Null then |
88a27b18 AC |
3311 | Last_Opnd_Low_Bound := |
3312 | Convert_To (Ityp, | |
3313 | Make_Attribute_Reference (Loc, | |
3314 | Prefix => | |
3315 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3316 | Attribute_Name => Name_First)); | |
3317 | ||
a29262fd AC |
3318 | Last_Opnd_High_Bound := |
3319 | Convert_To (Ityp, | |
3320 | Make_Attribute_Reference (Loc, | |
3321 | Prefix => | |
3322 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3323 | Attribute_Name => Name_Last)); | |
3324 | end if; | |
0ac73189 AC |
3325 | |
3326 | -- Capture length of operand in entity | |
3327 | ||
df46b832 AC |
3328 | Operands (NN) := Opnd; |
3329 | Is_Fixed_Length (NN) := False; | |
3330 | ||
191fcb3a | 3331 | Var_Length (NN) := Make_Temporary (Loc, 'L'); |
df46b832 | 3332 | |
d0f8d157 | 3333 | Append_To (Actions, |
df46b832 AC |
3334 | Make_Object_Declaration (Loc, |
3335 | Defining_Identifier => Var_Length (NN), | |
3336 | Constant_Present => True, | |
39ade2f9 | 3337 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3338 | Expression => |
3339 | Make_Attribute_Reference (Loc, | |
3340 | Prefix => | |
3341 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
d0f8d157 | 3342 | Attribute_Name => Name_Length))); |
df46b832 AC |
3343 | end if; |
3344 | end if; | |
3345 | ||
3346 | -- Set next entry in aggregate length array | |
3347 | ||
3348 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 3349 | -- or a reference to the saved length for variable length. |
df46b832 AC |
3350 | |
3351 | if NN = 1 then | |
3352 | if Is_Fixed_Length (1) then | |
39ade2f9 | 3353 | Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1)); |
df46b832 | 3354 | else |
e4494292 | 3355 | Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc); |
df46b832 AC |
3356 | end if; |
3357 | ||
3358 | -- If entry is fixed length and only fixed lengths so far, make | |
3359 | -- appropriate new integer literal adding new length. | |
3360 | ||
3361 | elsif Is_Fixed_Length (NN) | |
3362 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
3363 | then | |
3364 | Aggr_Length (NN) := | |
3365 | Make_Integer_Literal (Loc, | |
3366 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
3367 | ||
d0f8d157 AC |
3368 | -- All other cases, construct an addition node for the length and |
3369 | -- create an entity initialized to this length. | |
df46b832 AC |
3370 | |
3371 | else | |
191fcb3a | 3372 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 AC |
3373 | |
3374 | if Is_Fixed_Length (NN) then | |
3375 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
3376 | else | |
e4494292 | 3377 | Clen := New_Occurrence_Of (Var_Length (NN), Loc); |
df46b832 AC |
3378 | end if; |
3379 | ||
d0f8d157 | 3380 | Append_To (Actions, |
df46b832 AC |
3381 | Make_Object_Declaration (Loc, |
3382 | Defining_Identifier => Ent, | |
3383 | Constant_Present => True, | |
39ade2f9 | 3384 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3385 | Expression => |
3386 | Make_Op_Add (Loc, | |
3387 | Left_Opnd => New_Copy (Aggr_Length (NN - 1)), | |
d0f8d157 | 3388 | Right_Opnd => Clen))); |
df46b832 | 3389 | |
76c597a1 | 3390 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
3391 | end if; |
3392 | ||
3393 | <<Continue>> | |
3394 | J := J + 1; | |
3395 | end loop; | |
3396 | ||
a29262fd | 3397 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
3398 | |
3399 | if NN = 0 then | |
a29262fd | 3400 | Result := Opnd; |
df46b832 AC |
3401 | goto Done; |
3402 | end if; | |
3403 | ||
3404 | -- If we have only one non-null operand, return it and we are done. | |
3405 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
3406 | -- the sole operand is of the element type, in which case it must be |
3407 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
3408 | -- through the normal general circuit. |
3409 | ||
533369aa | 3410 | if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then |
df46b832 AC |
3411 | Result := Operands (1); |
3412 | goto Done; | |
3413 | end if; | |
3414 | ||
3415 | -- Cases where we have a real concatenation | |
3416 | ||
fdac1f80 AC |
3417 | -- Next step is to find the low bound for the result array that we |
3418 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
3419 | ||
3420 | -- If the ultimate ancestor of the index subtype is a constrained array | |
3421 | -- definition, then the lower bound is that of the index subtype as | |
3422 | -- specified by (RM 4.5.3(6)). | |
3423 | ||
3424 | -- The right test here is to go to the root type, and then the ultimate | |
3425 | -- ancestor is the first subtype of this root type. | |
3426 | ||
3427 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 3428 | Low_Bound := |
fdac1f80 AC |
3429 | Make_Attribute_Reference (Loc, |
3430 | Prefix => | |
3431 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 3432 | Attribute_Name => Name_First); |
df46b832 AC |
3433 | |
3434 | -- If the first operand in the list has known length we know that | |
3435 | -- the lower bound of the result is the lower bound of this operand. | |
3436 | ||
fdac1f80 | 3437 | elsif Is_Fixed_Length (1) then |
0ac73189 | 3438 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
3439 | |
3440 | -- OK, we don't know the lower bound, we have to build a horrible | |
9b16cb57 | 3441 | -- if expression node of the form |
df46b832 AC |
3442 | |
3443 | -- if Cond1'Length /= 0 then | |
0ac73189 | 3444 | -- Opnd1 low bound |
df46b832 AC |
3445 | -- else |
3446 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 3447 | -- Opnd2 low bound |
df46b832 AC |
3448 | -- else |
3449 | -- ... | |
3450 | ||
3451 | -- The nesting ends either when we hit an operand whose length is known | |
3452 | -- at compile time, or on reaching the last operand, whose low bound we | |
3453 | -- take unconditionally whether or not it is null. It's easiest to do | |
3454 | -- this with a recursive procedure: | |
3455 | ||
3456 | else | |
3457 | declare | |
3458 | function Get_Known_Bound (J : Nat) return Node_Id; | |
3459 | -- Returns the lower bound determined by operands J .. NN | |
3460 | ||
3461 | --------------------- | |
3462 | -- Get_Known_Bound -- | |
3463 | --------------------- | |
3464 | ||
3465 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 3466 | begin |
0ac73189 AC |
3467 | if Is_Fixed_Length (J) or else J = NN then |
3468 | return New_Copy (Opnd_Low_Bound (J)); | |
70482933 RK |
3469 | |
3470 | else | |
df46b832 | 3471 | return |
9b16cb57 | 3472 | Make_If_Expression (Loc, |
df46b832 AC |
3473 | Expressions => New_List ( |
3474 | ||
3475 | Make_Op_Ne (Loc, | |
e4494292 RD |
3476 | Left_Opnd => |
3477 | New_Occurrence_Of (Var_Length (J), Loc), | |
3478 | Right_Opnd => | |
3479 | Make_Integer_Literal (Loc, 0)), | |
df46b832 | 3480 | |
0ac73189 | 3481 | New_Copy (Opnd_Low_Bound (J)), |
df46b832 | 3482 | Get_Known_Bound (J + 1))); |
70482933 | 3483 | end if; |
df46b832 | 3484 | end Get_Known_Bound; |
70482933 | 3485 | |
df46b832 | 3486 | begin |
191fcb3a | 3487 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 | 3488 | |
d0f8d157 | 3489 | Append_To (Actions, |
df46b832 AC |
3490 | Make_Object_Declaration (Loc, |
3491 | Defining_Identifier => Ent, | |
3492 | Constant_Present => True, | |
0ac73189 | 3493 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
d0f8d157 | 3494 | Expression => Get_Known_Bound (1))); |
df46b832 | 3495 | |
e4494292 | 3496 | Low_Bound := New_Occurrence_Of (Ent, Loc); |
df46b832 AC |
3497 | end; |
3498 | end if; | |
70482933 | 3499 | |
76c597a1 AC |
3500 | -- Now we can safely compute the upper bound, normally |
3501 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
3502 | |
3503 | High_Bound := | |
cc6f5d75 AC |
3504 | To_Ityp |
3505 | (Make_Op_Add (Loc, | |
3506 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), | |
3507 | Right_Opnd => | |
3508 | Make_Op_Subtract (Loc, | |
3509 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
3510 | Right_Opnd => Make_Artyp_Literal (1)))); | |
0ac73189 | 3511 | |
59262ebb | 3512 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
3513 | -- very weird cases, so in the general case we need an overflow check on |
3514 | -- the high bound. We can avoid this for the common case of string types | |
3515 | -- and other types whose index is Positive, since we chose a wider range | |
3516 | -- for the arithmetic type. | |
76c597a1 | 3517 | |
59262ebb AC |
3518 | if Istyp /= Standard_Positive then |
3519 | Activate_Overflow_Check (High_Bound); | |
3520 | end if; | |
76c597a1 AC |
3521 | |
3522 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
3523 | -- case the bounds come from the last operand (so that we get the proper |
3524 | -- bounds if the last operand is super-flat). | |
3525 | ||
0ac73189 | 3526 | if Result_May_Be_Null then |
88a27b18 | 3527 | Low_Bound := |
9b16cb57 | 3528 | Make_If_Expression (Loc, |
88a27b18 AC |
3529 | Expressions => New_List ( |
3530 | Make_Op_Eq (Loc, | |
3531 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
3532 | Right_Opnd => Make_Artyp_Literal (0)), | |
3533 | Last_Opnd_Low_Bound, | |
3534 | Low_Bound)); | |
3535 | ||
0ac73189 | 3536 | High_Bound := |
9b16cb57 | 3537 | Make_If_Expression (Loc, |
0ac73189 AC |
3538 | Expressions => New_List ( |
3539 | Make_Op_Eq (Loc, | |
3540 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 3541 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 3542 | Last_Opnd_High_Bound, |
0ac73189 AC |
3543 | High_Bound)); |
3544 | end if; | |
3545 | ||
d0f8d157 AC |
3546 | -- Here is where we insert the saved up actions |
3547 | ||
3548 | Insert_Actions (Cnode, Actions, Suppress => All_Checks); | |
3549 | ||
602a7ec0 AC |
3550 | -- Now we construct an array object with appropriate bounds. We mark |
3551 | -- the target as internal to prevent useless initialization when | |
e526d0c7 AC |
3552 | -- Initialize_Scalars is enabled. Also since this is the actual result |
3553 | -- entity, we make sure we have debug information for the result. | |
70482933 | 3554 | |
191fcb3a | 3555 | Ent := Make_Temporary (Loc, 'S'); |
008f6fd3 | 3556 | Set_Is_Internal (Ent); |
e526d0c7 | 3557 | Set_Needs_Debug_Info (Ent); |
70482933 | 3558 | |
76c597a1 | 3559 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
3560 | -- to abort, we want a warning and a runtime constraint error. Note that |
3561 | -- we have arranged that the result will not be treated as a static | |
3562 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 3563 | |
df46b832 AC |
3564 | Insert_Action (Cnode, |
3565 | Make_Object_Declaration (Loc, | |
3566 | Defining_Identifier => Ent, | |
df46b832 AC |
3567 | Object_Definition => |
3568 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 3569 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
3570 | Constraint => |
3571 | Make_Index_Or_Discriminant_Constraint (Loc, | |
3572 | Constraints => New_List ( | |
3573 | Make_Range (Loc, | |
0ac73189 AC |
3574 | Low_Bound => Low_Bound, |
3575 | High_Bound => High_Bound))))), | |
df46b832 AC |
3576 | Suppress => All_Checks); |
3577 | ||
d1f453b7 RD |
3578 | -- If the result of the concatenation appears as the initializing |
3579 | -- expression of an object declaration, we can just rename the | |
3580 | -- result, rather than copying it. | |
3581 | ||
3582 | Set_OK_To_Rename (Ent); | |
3583 | ||
76c597a1 AC |
3584 | -- Catch the static out of range case now |
3585 | ||
3586 | if Raises_Constraint_Error (High_Bound) then | |
3587 | raise Concatenation_Error; | |
3588 | end if; | |
3589 | ||
df46b832 AC |
3590 | -- Now we will generate the assignments to do the actual concatenation |
3591 | ||
bded454f RD |
3592 | -- There is one case in which we will not do this, namely when all the |
3593 | -- following conditions are met: | |
3594 | ||
3595 | -- The result type is Standard.String | |
3596 | ||
3597 | -- There are nine or fewer retained (non-null) operands | |
3598 | ||
ffec8e81 | 3599 | -- The optimization level is -O0 |
bded454f RD |
3600 | |
3601 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
3602 | -- available in the run time. | |
3603 | ||
3604 | -- The debug flag gnatd.c is not set | |
3605 | ||
3606 | -- If all these conditions are met then we generate a call to the | |
3607 | -- relevant concatenation routine. The purpose of this is to avoid | |
3608 | -- undesirable code bloat at -O0. | |
3609 | ||
3610 | if Atyp = Standard_String | |
3611 | and then NN in 2 .. 9 | |
00ba7be8 | 3612 | and then (Lib_Level_Target |
62a64085 | 3613 | or else ((Optimization_Level = 0 or else Debug_Flag_Dot_CC) |
cc6f5d75 | 3614 | and then not Debug_Flag_Dot_C)) |
bded454f RD |
3615 | then |
3616 | declare | |
3617 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
3618 | (RE_Str_Concat_2, | |
3619 | RE_Str_Concat_3, | |
3620 | RE_Str_Concat_4, | |
3621 | RE_Str_Concat_5, | |
3622 | RE_Str_Concat_6, | |
3623 | RE_Str_Concat_7, | |
3624 | RE_Str_Concat_8, | |
3625 | RE_Str_Concat_9); | |
3626 | ||
3627 | begin | |
3628 | if RTE_Available (RR (NN)) then | |
3629 | declare | |
3630 | Opnds : constant List_Id := | |
3631 | New_List (New_Occurrence_Of (Ent, Loc)); | |
3632 | ||
3633 | begin | |
3634 | for J in 1 .. NN loop | |
3635 | if Is_List_Member (Operands (J)) then | |
3636 | Remove (Operands (J)); | |
3637 | end if; | |
3638 | ||
3639 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
3640 | Append_To (Opnds, | |
3641 | Make_Aggregate (Loc, | |
3642 | Component_Associations => New_List ( | |
3643 | Make_Component_Association (Loc, | |
3644 | Choices => New_List ( | |
3645 | Make_Integer_Literal (Loc, 1)), | |
3646 | Expression => Operands (J))))); | |
3647 | ||
3648 | else | |
3649 | Append_To (Opnds, Operands (J)); | |
3650 | end if; | |
3651 | end loop; | |
3652 | ||
3653 | Insert_Action (Cnode, | |
3654 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 3655 | Name => New_Occurrence_Of (RTE (RR (NN)), Loc), |
bded454f RD |
3656 | Parameter_Associations => Opnds)); |
3657 | ||
e4494292 | 3658 | Result := New_Occurrence_Of (Ent, Loc); |
bded454f RD |
3659 | goto Done; |
3660 | end; | |
3661 | end if; | |
3662 | end; | |
3663 | end if; | |
3664 | ||
3665 | -- Not special case so generate the assignments | |
3666 | ||
76c597a1 AC |
3667 | Known_Non_Null_Operand_Seen := False; |
3668 | ||
df46b832 AC |
3669 | for J in 1 .. NN loop |
3670 | declare | |
3671 | Lo : constant Node_Id := | |
3672 | Make_Op_Add (Loc, | |
46ff89f3 | 3673 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3674 | Right_Opnd => Aggr_Length (J - 1)); |
3675 | ||
3676 | Hi : constant Node_Id := | |
3677 | Make_Op_Add (Loc, | |
46ff89f3 | 3678 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3679 | Right_Opnd => |
3680 | Make_Op_Subtract (Loc, | |
3681 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 3682 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 3683 | |
df46b832 | 3684 | begin |
fdac1f80 AC |
3685 | -- Singleton case, simple assignment |
3686 | ||
3687 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 3688 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
3689 | Insert_Action (Cnode, |
3690 | Make_Assignment_Statement (Loc, | |
3691 | Name => | |
3692 | Make_Indexed_Component (Loc, | |
3693 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 3694 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
3695 | Expression => Operands (J)), |
3696 | Suppress => All_Checks); | |
70482933 | 3697 | |
76c597a1 AC |
3698 | -- Array case, slice assignment, skipped when argument is fixed |
3699 | -- length and known to be null. | |
fdac1f80 | 3700 | |
76c597a1 AC |
3701 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
3702 | declare | |
3703 | Assign : Node_Id := | |
3704 | Make_Assignment_Statement (Loc, | |
3705 | Name => | |
3706 | Make_Slice (Loc, | |
3707 | Prefix => | |
3708 | New_Occurrence_Of (Ent, Loc), | |
3709 | Discrete_Range => | |
3710 | Make_Range (Loc, | |
3711 | Low_Bound => To_Ityp (Lo), | |
3712 | High_Bound => To_Ityp (Hi))), | |
3713 | Expression => Operands (J)); | |
3714 | begin | |
3715 | if Is_Fixed_Length (J) then | |
3716 | Known_Non_Null_Operand_Seen := True; | |
3717 | ||
3718 | elsif not Known_Non_Null_Operand_Seen then | |
3719 | ||
3720 | -- Here if operand length is not statically known and no | |
3721 | -- operand known to be non-null has been processed yet. | |
3722 | -- If operand length is 0, we do not need to perform the | |
3723 | -- assignment, and we must avoid the evaluation of the | |
3724 | -- high bound of the slice, since it may underflow if the | |
3725 | -- low bound is Ityp'First. | |
3726 | ||
3727 | Assign := | |
3728 | Make_Implicit_If_Statement (Cnode, | |
39ade2f9 | 3729 | Condition => |
76c597a1 | 3730 | Make_Op_Ne (Loc, |
39ade2f9 | 3731 | Left_Opnd => |
76c597a1 AC |
3732 | New_Occurrence_Of (Var_Length (J), Loc), |
3733 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
39ade2f9 | 3734 | Then_Statements => New_List (Assign)); |
76c597a1 | 3735 | end if; |
fa969310 | 3736 | |
76c597a1 AC |
3737 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
3738 | end; | |
df46b832 AC |
3739 | end if; |
3740 | end; | |
3741 | end loop; | |
70482933 | 3742 | |
0ac73189 AC |
3743 | -- Finally we build the result, which is a reference to the array object |
3744 | ||
e4494292 | 3745 | Result := New_Occurrence_Of (Ent, Loc); |
70482933 | 3746 | |
df46b832 AC |
3747 | <<Done>> |
3748 | Rewrite (Cnode, Result); | |
fdac1f80 AC |
3749 | Analyze_And_Resolve (Cnode, Atyp); |
3750 | ||
3751 | exception | |
3752 | when Concatenation_Error => | |
76c597a1 AC |
3753 | |
3754 | -- Kill warning generated for the declaration of the static out of | |
3755 | -- range high bound, and instead generate a Constraint_Error with | |
3756 | -- an appropriate specific message. | |
3757 | ||
3758 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
3759 | Apply_Compile_Time_Constraint_Error | |
3760 | (N => Cnode, | |
324ac540 | 3761 | Msg => "concatenation result upper bound out of range??", |
76c597a1 | 3762 | Reason => CE_Range_Check_Failed); |
fdac1f80 | 3763 | end Expand_Concatenate; |
70482933 | 3764 | |
f6194278 RD |
3765 | --------------------------------------------------- |
3766 | -- Expand_Membership_Minimize_Eliminate_Overflow -- | |
3767 | --------------------------------------------------- | |
3768 | ||
3769 | procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is | |
3770 | pragma Assert (Nkind (N) = N_In); | |
3771 | -- Despite the name, this routine applies only to N_In, not to | |
3772 | -- N_Not_In. The latter is always rewritten as not (X in Y). | |
3773 | ||
71fb4dc8 AC |
3774 | Result_Type : constant Entity_Id := Etype (N); |
3775 | -- Capture result type, may be a derived boolean type | |
3776 | ||
b6b5cca8 AC |
3777 | Loc : constant Source_Ptr := Sloc (N); |
3778 | Lop : constant Node_Id := Left_Opnd (N); | |
3779 | Rop : constant Node_Id := Right_Opnd (N); | |
3780 | ||
3781 | -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It | |
3782 | -- is thus tempting to capture these values, but due to the rewrites | |
3783 | -- that occur as a result of overflow checking, these values change | |
3784 | -- as we go along, and it is safe just to always use Etype explicitly. | |
f6194278 RD |
3785 | |
3786 | Restype : constant Entity_Id := Etype (N); | |
3787 | -- Save result type | |
3788 | ||
3789 | Lo, Hi : Uint; | |
d8192289 | 3790 | -- Bounds in Minimize calls, not used currently |
f6194278 RD |
3791 | |
3792 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
3793 | -- Entity for Long_Long_Integer'Base (Standard should export this???) | |
3794 | ||
3795 | begin | |
a7f1b24f | 3796 | Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False); |
f6194278 RD |
3797 | |
3798 | -- If right operand is a subtype name, and the subtype name has no | |
3799 | -- predicate, then we can just replace the right operand with an | |
3800 | -- explicit range T'First .. T'Last, and use the explicit range code. | |
3801 | ||
b6b5cca8 AC |
3802 | if Nkind (Rop) /= N_Range |
3803 | and then No (Predicate_Function (Etype (Rop))) | |
3804 | then | |
3805 | declare | |
3806 | Rtyp : constant Entity_Id := Etype (Rop); | |
3807 | begin | |
3808 | Rewrite (Rop, | |
3809 | Make_Range (Loc, | |
cc6f5d75 | 3810 | Low_Bound => |
b6b5cca8 AC |
3811 | Make_Attribute_Reference (Loc, |
3812 | Attribute_Name => Name_First, | |
e4494292 | 3813 | Prefix => New_Occurrence_Of (Rtyp, Loc)), |
b6b5cca8 AC |
3814 | High_Bound => |
3815 | Make_Attribute_Reference (Loc, | |
3816 | Attribute_Name => Name_Last, | |
e4494292 | 3817 | Prefix => New_Occurrence_Of (Rtyp, Loc)))); |
b6b5cca8 AC |
3818 | Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks); |
3819 | end; | |
f6194278 RD |
3820 | end if; |
3821 | ||
3822 | -- Here for the explicit range case. Note that the bounds of the range | |
3823 | -- have not been processed for minimized or eliminated checks. | |
3824 | ||
3825 | if Nkind (Rop) = N_Range then | |
a7f1b24f | 3826 | Minimize_Eliminate_Overflows |
b6b5cca8 | 3827 | (Low_Bound (Rop), Lo, Hi, Top_Level => False); |
a7f1b24f | 3828 | Minimize_Eliminate_Overflows |
c7e152b5 | 3829 | (High_Bound (Rop), Lo, Hi, Top_Level => False); |
f6194278 RD |
3830 | |
3831 | -- We have A in B .. C, treated as A >= B and then A <= C | |
3832 | ||
3833 | -- Bignum case | |
3834 | ||
b6b5cca8 | 3835 | if Is_RTE (Etype (Lop), RE_Bignum) |
f6194278 RD |
3836 | or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum) |
3837 | or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum) | |
3838 | then | |
3839 | declare | |
3840 | Blk : constant Node_Id := Make_Bignum_Block (Loc); | |
3841 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
71fb4dc8 AC |
3842 | L : constant Entity_Id := |
3843 | Make_Defining_Identifier (Loc, Name_uL); | |
f6194278 RD |
3844 | Lopnd : constant Node_Id := Convert_To_Bignum (Lop); |
3845 | Lbound : constant Node_Id := | |
3846 | Convert_To_Bignum (Low_Bound (Rop)); | |
3847 | Hbound : constant Node_Id := | |
3848 | Convert_To_Bignum (High_Bound (Rop)); | |
3849 | ||
71fb4dc8 AC |
3850 | -- Now we rewrite the membership test node to look like |
3851 | ||
3852 | -- do | |
3853 | -- Bnn : Result_Type; | |
3854 | -- declare | |
3855 | -- M : Mark_Id := SS_Mark; | |
3856 | -- L : Bignum := Lopnd; | |
3857 | -- begin | |
3858 | -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound) | |
3859 | -- SS_Release (M); | |
3860 | -- end; | |
3861 | -- in | |
3862 | -- Bnn | |
3863 | -- end | |
f6194278 RD |
3864 | |
3865 | begin | |
71fb4dc8 AC |
3866 | -- Insert declaration of L into declarations of bignum block |
3867 | ||
f6194278 RD |
3868 | Insert_After |
3869 | (Last (Declarations (Blk)), | |
3870 | Make_Object_Declaration (Loc, | |
71fb4dc8 | 3871 | Defining_Identifier => L, |
f6194278 RD |
3872 | Object_Definition => |
3873 | New_Occurrence_Of (RTE (RE_Bignum), Loc), | |
3874 | Expression => Lopnd)); | |
3875 | ||
71fb4dc8 AC |
3876 | -- Insert assignment to Bnn into expressions of bignum block |
3877 | ||
f6194278 RD |
3878 | Insert_Before |
3879 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
3880 | Make_Assignment_Statement (Loc, | |
3881 | Name => New_Occurrence_Of (Bnn, Loc), | |
3882 | Expression => | |
3883 | Make_And_Then (Loc, | |
cc6f5d75 | 3884 | Left_Opnd => |
f6194278 RD |
3885 | Make_Function_Call (Loc, |
3886 | Name => | |
3887 | New_Occurrence_Of (RTE (RE_Big_GE), Loc), | |
71fb4dc8 AC |
3888 | Parameter_Associations => New_List ( |
3889 | New_Occurrence_Of (L, Loc), | |
3890 | Lbound)), | |
cc6f5d75 | 3891 | |
f6194278 RD |
3892 | Right_Opnd => |
3893 | Make_Function_Call (Loc, | |
3894 | Name => | |
71fb4dc8 AC |
3895 | New_Occurrence_Of (RTE (RE_Big_LE), Loc), |
3896 | Parameter_Associations => New_List ( | |
3897 | New_Occurrence_Of (L, Loc), | |
3898 | Hbound))))); | |
f6194278 | 3899 | |
71fb4dc8 | 3900 | -- Now rewrite the node |
f6194278 | 3901 | |
71fb4dc8 AC |
3902 | Rewrite (N, |
3903 | Make_Expression_With_Actions (Loc, | |
3904 | Actions => New_List ( | |
3905 | Make_Object_Declaration (Loc, | |
3906 | Defining_Identifier => Bnn, | |
3907 | Object_Definition => | |
3908 | New_Occurrence_Of (Result_Type, Loc)), | |
3909 | Blk), | |
3910 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
3911 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
3912 | return; |
3913 | end; | |
3914 | ||
3915 | -- Here if no bignums around | |
3916 | ||
3917 | else | |
3918 | -- Case where types are all the same | |
3919 | ||
b6b5cca8 | 3920 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop))) |
f6194278 | 3921 | and then |
b6b5cca8 | 3922 | Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop))) |
f6194278 RD |
3923 | then |
3924 | null; | |
3925 | ||
3926 | -- If types are not all the same, it means that we have rewritten | |
3927 | -- at least one of them to be of type Long_Long_Integer, and we | |
3928 | -- will convert the other operands to Long_Long_Integer. | |
3929 | ||
3930 | else | |
3931 | Convert_To_And_Rewrite (LLIB, Lop); | |
71fb4dc8 AC |
3932 | Set_Analyzed (Lop, False); |
3933 | Analyze_And_Resolve (Lop, LLIB); | |
3934 | ||
3935 | -- For the right operand, avoid unnecessary recursion into | |
3936 | -- this routine, we know that overflow is not possible. | |
f6194278 RD |
3937 | |
3938 | Convert_To_And_Rewrite (LLIB, Low_Bound (Rop)); | |
3939 | Convert_To_And_Rewrite (LLIB, High_Bound (Rop)); | |
3940 | Set_Analyzed (Rop, False); | |
71fb4dc8 | 3941 | Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check); |
f6194278 RD |
3942 | end if; |
3943 | ||
3944 | -- Now the three operands are of the same signed integer type, | |
b6b5cca8 AC |
3945 | -- so we can use the normal expansion routine for membership, |
3946 | -- setting the flag to prevent recursion into this procedure. | |
f6194278 RD |
3947 | |
3948 | Set_No_Minimize_Eliminate (N); | |
3949 | Expand_N_In (N); | |
3950 | end if; | |
3951 | ||
3952 | -- Right operand is a subtype name and the subtype has a predicate. We | |
f6636994 AC |
3953 | -- have to make sure the predicate is checked, and for that we need to |
3954 | -- use the standard N_In circuitry with appropriate types. | |
f6194278 RD |
3955 | |
3956 | else | |
b6b5cca8 | 3957 | pragma Assert (Present (Predicate_Function (Etype (Rop)))); |
f6194278 RD |
3958 | |
3959 | -- If types are "right", just call Expand_N_In preventing recursion | |
3960 | ||
b6b5cca8 | 3961 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then |
f6194278 RD |
3962 | Set_No_Minimize_Eliminate (N); |
3963 | Expand_N_In (N); | |
3964 | ||
3965 | -- Bignum case | |
3966 | ||
b6b5cca8 | 3967 | elsif Is_RTE (Etype (Lop), RE_Bignum) then |
f6194278 | 3968 | |
71fb4dc8 | 3969 | -- For X in T, we want to rewrite our node as |
f6194278 | 3970 | |
71fb4dc8 AC |
3971 | -- do |
3972 | -- Bnn : Result_Type; | |
f6194278 | 3973 | |
71fb4dc8 AC |
3974 | -- declare |
3975 | -- M : Mark_Id := SS_Mark; | |
3976 | -- Lnn : Long_Long_Integer'Base | |
3977 | -- Nnn : Bignum; | |
f6194278 | 3978 | |
71fb4dc8 AC |
3979 | -- begin |
3980 | -- Nnn := X; | |
3981 | ||
3982 | -- if not Bignum_In_LLI_Range (Nnn) then | |
3983 | -- Bnn := False; | |
3984 | -- else | |
3985 | -- Lnn := From_Bignum (Nnn); | |
3986 | -- Bnn := | |
3987 | -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last) | |
3988 | -- and then T'Base (Lnn) in T; | |
3989 | -- end if; | |
cc6f5d75 AC |
3990 | |
3991 | -- SS_Release (M); | |
71fb4dc8 AC |
3992 | -- end |
3993 | -- in | |
3994 | -- Bnn | |
3995 | -- end | |
f6194278 | 3996 | |
f6636994 | 3997 | -- A bit gruesome, but there doesn't seem to be a simpler way |
f6194278 RD |
3998 | |
3999 | declare | |
b6b5cca8 AC |
4000 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
4001 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
4002 | Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N); | |
4003 | Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N); | |
71fb4dc8 AC |
4004 | T : constant Entity_Id := Etype (Rop); |
4005 | TB : constant Entity_Id := Base_Type (T); | |
b6b5cca8 | 4006 | Nin : Node_Id; |
f6194278 RD |
4007 | |
4008 | begin | |
71fb4dc8 | 4009 | -- Mark the last membership operation to prevent recursion |
f6194278 RD |
4010 | |
4011 | Nin := | |
4012 | Make_In (Loc, | |
f6636994 AC |
4013 | Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)), |
4014 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
4015 | Set_No_Minimize_Eliminate (Nin); |
4016 | ||
4017 | -- Now decorate the block | |
4018 | ||
4019 | Insert_After | |
4020 | (Last (Declarations (Blk)), | |
4021 | Make_Object_Declaration (Loc, | |
4022 | Defining_Identifier => Lnn, | |
4023 | Object_Definition => New_Occurrence_Of (LLIB, Loc))); | |
4024 | ||
4025 | Insert_After | |
4026 | (Last (Declarations (Blk)), | |
4027 | Make_Object_Declaration (Loc, | |
4028 | Defining_Identifier => Nnn, | |
4029 | Object_Definition => | |
4030 | New_Occurrence_Of (RTE (RE_Bignum), Loc))); | |
4031 | ||
4032 | Insert_List_Before | |
4033 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
4034 | New_List ( | |
4035 | Make_Assignment_Statement (Loc, | |
4036 | Name => New_Occurrence_Of (Nnn, Loc), | |
4037 | Expression => Relocate_Node (Lop)), | |
4038 | ||
8b1011c0 | 4039 | Make_Implicit_If_Statement (N, |
f6194278 | 4040 | Condition => |
71fb4dc8 AC |
4041 | Make_Op_Not (Loc, |
4042 | Right_Opnd => | |
4043 | Make_Function_Call (Loc, | |
4044 | Name => | |
4045 | New_Occurrence_Of | |
4046 | (RTE (RE_Bignum_In_LLI_Range), Loc), | |
4047 | Parameter_Associations => New_List ( | |
4048 | New_Occurrence_Of (Nnn, Loc)))), | |
f6194278 RD |
4049 | |
4050 | Then_Statements => New_List ( | |
4051 | Make_Assignment_Statement (Loc, | |
4052 | Name => New_Occurrence_Of (Bnn, Loc), | |
4053 | Expression => | |
4054 | New_Occurrence_Of (Standard_False, Loc))), | |
4055 | ||
4056 | Else_Statements => New_List ( | |
4057 | Make_Assignment_Statement (Loc, | |
4058 | Name => New_Occurrence_Of (Lnn, Loc), | |
4059 | Expression => | |
4060 | Make_Function_Call (Loc, | |
4061 | Name => | |
4062 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4063 | Parameter_Associations => New_List ( | |
4064 | New_Occurrence_Of (Nnn, Loc)))), | |
4065 | ||
4066 | Make_Assignment_Statement (Loc, | |
71fb4dc8 | 4067 | Name => New_Occurrence_Of (Bnn, Loc), |
f6194278 RD |
4068 | Expression => |
4069 | Make_And_Then (Loc, | |
71fb4dc8 | 4070 | Left_Opnd => |
f6194278 | 4071 | Make_In (Loc, |
71fb4dc8 | 4072 | Left_Opnd => New_Occurrence_Of (Lnn, Loc), |
f6194278 | 4073 | Right_Opnd => |
71fb4dc8 AC |
4074 | Make_Range (Loc, |
4075 | Low_Bound => | |
4076 | Convert_To (LLIB, | |
4077 | Make_Attribute_Reference (Loc, | |
4078 | Attribute_Name => Name_First, | |
4079 | Prefix => | |
4080 | New_Occurrence_Of (TB, Loc))), | |
4081 | ||
4082 | High_Bound => | |
4083 | Convert_To (LLIB, | |
4084 | Make_Attribute_Reference (Loc, | |
4085 | Attribute_Name => Name_Last, | |
4086 | Prefix => | |
4087 | New_Occurrence_Of (TB, Loc))))), | |
4088 | ||
f6194278 RD |
4089 | Right_Opnd => Nin)))))); |
4090 | ||
71fb4dc8 | 4091 | -- Now we can do the rewrite |
f6194278 | 4092 | |
71fb4dc8 AC |
4093 | Rewrite (N, |
4094 | Make_Expression_With_Actions (Loc, | |
4095 | Actions => New_List ( | |
4096 | Make_Object_Declaration (Loc, | |
4097 | Defining_Identifier => Bnn, | |
4098 | Object_Definition => | |
4099 | New_Occurrence_Of (Result_Type, Loc)), | |
4100 | Blk), | |
4101 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
4102 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
4103 | return; |
4104 | end; | |
4105 | ||
4106 | -- Not bignum case, but types don't match (this means we rewrote the | |
b6b5cca8 | 4107 | -- left operand to be Long_Long_Integer). |
f6194278 RD |
4108 | |
4109 | else | |
b6b5cca8 | 4110 | pragma Assert (Base_Type (Etype (Lop)) = LLIB); |
f6194278 | 4111 | |
71fb4dc8 AC |
4112 | -- We rewrite the membership test as (where T is the type with |
4113 | -- the predicate, i.e. the type of the right operand) | |
f6194278 | 4114 | |
71fb4dc8 AC |
4115 | -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last) |
4116 | -- and then T'Base (Lop) in T | |
f6194278 RD |
4117 | |
4118 | declare | |
71fb4dc8 AC |
4119 | T : constant Entity_Id := Etype (Rop); |
4120 | TB : constant Entity_Id := Base_Type (T); | |
f6194278 RD |
4121 | Nin : Node_Id; |
4122 | ||
4123 | begin | |
4124 | -- The last membership test is marked to prevent recursion | |
4125 | ||
4126 | Nin := | |
4127 | Make_In (Loc, | |
71fb4dc8 AC |
4128 | Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)), |
4129 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
4130 | Set_No_Minimize_Eliminate (Nin); |
4131 | ||
4132 | -- Now do the rewrite | |
4133 | ||
4134 | Rewrite (N, | |
4135 | Make_And_Then (Loc, | |
71fb4dc8 | 4136 | Left_Opnd => |
f6194278 RD |
4137 | Make_In (Loc, |
4138 | Left_Opnd => Lop, | |
4139 | Right_Opnd => | |
71fb4dc8 AC |
4140 | Make_Range (Loc, |
4141 | Low_Bound => | |
4142 | Convert_To (LLIB, | |
4143 | Make_Attribute_Reference (Loc, | |
4144 | Attribute_Name => Name_First, | |
cc6f5d75 AC |
4145 | Prefix => |
4146 | New_Occurrence_Of (TB, Loc))), | |
71fb4dc8 AC |
4147 | High_Bound => |
4148 | Convert_To (LLIB, | |
4149 | Make_Attribute_Reference (Loc, | |
4150 | Attribute_Name => Name_Last, | |
cc6f5d75 AC |
4151 | Prefix => |
4152 | New_Occurrence_Of (TB, Loc))))), | |
f6194278 | 4153 | Right_Opnd => Nin)); |
71fb4dc8 AC |
4154 | Set_Analyzed (N, False); |
4155 | Analyze_And_Resolve (N, Restype); | |
f6194278 RD |
4156 | end; |
4157 | end if; | |
4158 | end if; | |
4159 | end Expand_Membership_Minimize_Eliminate_Overflow; | |
4160 | ||
70482933 RK |
4161 | ------------------------ |
4162 | -- Expand_N_Allocator -- | |
4163 | ------------------------ | |
4164 | ||
4165 | procedure Expand_N_Allocator (N : Node_Id) is | |
8b1011c0 AC |
4166 | Etyp : constant Entity_Id := Etype (Expression (N)); |
4167 | Loc : constant Source_Ptr := Sloc (N); | |
4168 | PtrT : constant Entity_Id := Etype (N); | |
70482933 | 4169 | |
26bff3d9 JM |
4170 | procedure Rewrite_Coextension (N : Node_Id); |
4171 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 4172 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 4173 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 4174 | |
8aec446b | 4175 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd AC |
4176 | -- Given a constrained array type E, returns a node representing the |
4177 | -- code to compute the size in storage elements for the given type. | |
205c14b0 | 4178 | -- This is done without using the attribute (which malfunctions for |
507ed3fd | 4179 | -- large sizes ???) |
8aec446b | 4180 | |
26bff3d9 JM |
4181 | ------------------------- |
4182 | -- Rewrite_Coextension -- | |
4183 | ------------------------- | |
4184 | ||
4185 | procedure Rewrite_Coextension (N : Node_Id) is | |
e5a22243 AC |
4186 | Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C'); |
4187 | Temp_Decl : Node_Id; | |
26bff3d9 | 4188 | |
df3e68b1 | 4189 | begin |
26bff3d9 JM |
4190 | -- Generate: |
4191 | -- Cnn : aliased Etyp; | |
4192 | ||
df3e68b1 HK |
4193 | Temp_Decl := |
4194 | Make_Object_Declaration (Loc, | |
4195 | Defining_Identifier => Temp_Id, | |
243cae0a AC |
4196 | Aliased_Present => True, |
4197 | Object_Definition => New_Occurrence_Of (Etyp, Loc)); | |
26bff3d9 | 4198 | |
26bff3d9 | 4199 | if Nkind (Expression (N)) = N_Qualified_Expression then |
df3e68b1 | 4200 | Set_Expression (Temp_Decl, Expression (Expression (N))); |
0669bebe | 4201 | end if; |
26bff3d9 | 4202 | |
e5a22243 | 4203 | Insert_Action (N, Temp_Decl); |
26bff3d9 JM |
4204 | Rewrite (N, |
4205 | Make_Attribute_Reference (Loc, | |
243cae0a | 4206 | Prefix => New_Occurrence_Of (Temp_Id, Loc), |
26bff3d9 JM |
4207 | Attribute_Name => Name_Unrestricted_Access)); |
4208 | ||
4209 | Analyze_And_Resolve (N, PtrT); | |
4210 | end Rewrite_Coextension; | |
0669bebe | 4211 | |
8aec446b AC |
4212 | ------------------------------ |
4213 | -- Size_In_Storage_Elements -- | |
4214 | ------------------------------ | |
4215 | ||
4216 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
4217 | begin | |
4218 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
4219 | -- However, the reason for the existence of this function is | |
4220 | -- to construct a test for sizes too large, which means near the | |
4221 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
4222 | -- is that we get overflows when sizes are greater than 2**31. | |
4223 | ||
507ed3fd | 4224 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
4225 | |
4226 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
4227 | ||
46202729 | 4228 | -- which avoids this problem. All this is a bit bogus, but it does |
8aec446b AC |
4229 | -- mean we catch common cases of trying to allocate arrays that |
4230 | -- are too large, and which in the absence of a check results in | |
4231 | -- undetected chaos ??? | |
4232 | ||
ce532f42 AC |
4233 | -- Note in particular that this is a pessimistic estimate in the |
4234 | -- case of packed array types, where an array element might occupy | |
4235 | -- just a fraction of a storage element??? | |
4236 | ||
507ed3fd AC |
4237 | declare |
4238 | Len : Node_Id; | |
4239 | Res : Node_Id; | |
8aec446b | 4240 | |
507ed3fd AC |
4241 | begin |
4242 | for J in 1 .. Number_Dimensions (E) loop | |
4243 | Len := | |
4244 | Make_Attribute_Reference (Loc, | |
4245 | Prefix => New_Occurrence_Of (E, Loc), | |
4246 | Attribute_Name => Name_Length, | |
243cae0a | 4247 | Expressions => New_List (Make_Integer_Literal (Loc, J))); |
8aec446b | 4248 | |
507ed3fd AC |
4249 | if J = 1 then |
4250 | Res := Len; | |
8aec446b | 4251 | |
507ed3fd AC |
4252 | else |
4253 | Res := | |
4254 | Make_Op_Multiply (Loc, | |
4255 | Left_Opnd => Res, | |
4256 | Right_Opnd => Len); | |
4257 | end if; | |
4258 | end loop; | |
8aec446b | 4259 | |
8aec446b | 4260 | return |
507ed3fd AC |
4261 | Make_Op_Multiply (Loc, |
4262 | Left_Opnd => Len, | |
4263 | Right_Opnd => | |
4264 | Make_Attribute_Reference (Loc, | |
4265 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
4266 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
4267 | end; | |
8aec446b AC |
4268 | end Size_In_Storage_Elements; |
4269 | ||
8b1011c0 AC |
4270 | -- Local variables |
4271 | ||
70861157 | 4272 | Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT)); |
8b1011c0 AC |
4273 | Desig : Entity_Id; |
4274 | Nod : Node_Id; | |
4275 | Pool : Entity_Id; | |
4276 | Rel_Typ : Entity_Id; | |
4277 | Temp : Entity_Id; | |
4278 | ||
0669bebe GB |
4279 | -- Start of processing for Expand_N_Allocator |
4280 | ||
70482933 RK |
4281 | begin |
4282 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
4283 | -- shall not be a remote access-to-class-wide-limited-private type | |
4284 | ||
4285 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
4286 | ||
4287 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
4288 | ||
ca5af305 AC |
4289 | -- Processing for anonymous access-to-controlled types. These access |
4290 | -- types receive a special finalization master which appears in the | |
4291 | -- declarations of the enclosing semantic unit. This expansion is done | |
84f4072a JM |
4292 | -- now to ensure that any additional types generated by this routine or |
4293 | -- Expand_Allocator_Expression inherit the proper type attributes. | |
ca5af305 | 4294 | |
84f4072a | 4295 | if (Ekind (PtrT) = E_Anonymous_Access_Type |
533369aa | 4296 | or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT)))) |
ca5af305 AC |
4297 | and then Needs_Finalization (Dtyp) |
4298 | then | |
8b1011c0 AC |
4299 | -- Detect the allocation of an anonymous controlled object where the |
4300 | -- type of the context is named. For example: | |
4301 | ||
4302 | -- procedure Proc (Ptr : Named_Access_Typ); | |
4303 | -- Proc (new Designated_Typ); | |
4304 | ||
4305 | -- Regardless of the anonymous-to-named access type conversion, the | |
4306 | -- lifetime of the object must be associated with the named access | |
0088ba92 | 4307 | -- type. Use the finalization-related attributes of this type. |
8b1011c0 AC |
4308 | |
4309 | if Nkind_In (Parent (N), N_Type_Conversion, | |
4310 | N_Unchecked_Type_Conversion) | |
4311 | and then Ekind_In (Etype (Parent (N)), E_Access_Subtype, | |
4312 | E_Access_Type, | |
4313 | E_General_Access_Type) | |
4314 | then | |
4315 | Rel_Typ := Etype (Parent (N)); | |
4316 | else | |
4317 | Rel_Typ := Empty; | |
4318 | end if; | |
4319 | ||
b254da66 AC |
4320 | -- Anonymous access-to-controlled types allocate on the global pool. |
4321 | -- Do not set this attribute on .NET/JVM since those targets do not | |
24d4b3d5 | 4322 | -- support pools. Note that this is a "root type only" attribute. |
ca5af305 | 4323 | |
bde73c6b | 4324 | if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then |
8b1011c0 | 4325 | if Present (Rel_Typ) then |
7a5b62b0 | 4326 | Set_Associated_Storage_Pool |
24d4b3d5 | 4327 | (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ)); |
8b1011c0 | 4328 | else |
7a5b62b0 | 4329 | Set_Associated_Storage_Pool |
24d4b3d5 | 4330 | (Root_Type (PtrT), RTE (RE_Global_Pool_Object)); |
8b1011c0 | 4331 | end if; |
ca5af305 AC |
4332 | end if; |
4333 | ||
4334 | -- The finalization master must be inserted and analyzed as part of | |
5114f3ff | 4335 | -- the current semantic unit. Note that the master is updated when |
24d4b3d5 AC |
4336 | -- analysis changes current units. Note that this is a "root type |
4337 | -- only" attribute. | |
ca5af305 | 4338 | |
5114f3ff | 4339 | if Present (Rel_Typ) then |
24d4b3d5 AC |
4340 | Set_Finalization_Master |
4341 | (Root_Type (PtrT), Finalization_Master (Rel_Typ)); | |
5114f3ff | 4342 | else |
24d4b3d5 AC |
4343 | Set_Finalization_Master |
4344 | (Root_Type (PtrT), Current_Anonymous_Master); | |
ca5af305 AC |
4345 | end if; |
4346 | end if; | |
4347 | ||
4348 | -- Set the storage pool and find the appropriate version of Allocate to | |
8417f4b2 AC |
4349 | -- call. Do not overwrite the storage pool if it is already set, which |
4350 | -- can happen for build-in-place function returns (see | |
200b7162 | 4351 | -- Exp_Ch4.Expand_N_Extended_Return_Statement). |
70482933 | 4352 | |
200b7162 BD |
4353 | if No (Storage_Pool (N)) then |
4354 | Pool := Associated_Storage_Pool (Root_Type (PtrT)); | |
70482933 | 4355 | |
200b7162 BD |
4356 | if Present (Pool) then |
4357 | Set_Storage_Pool (N, Pool); | |
fbf5a39b | 4358 | |
200b7162 BD |
4359 | if Is_RTE (Pool, RE_SS_Pool) then |
4360 | if VM_Target = No_VM then | |
4361 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); | |
4362 | end if; | |
fbf5a39b | 4363 | |
a8551b5f AC |
4364 | -- In the case of an allocator for a simple storage pool, locate |
4365 | -- and save a reference to the pool type's Allocate routine. | |
4366 | ||
4367 | elsif Present (Get_Rep_Pragma | |
f6205414 | 4368 | (Etype (Pool), Name_Simple_Storage_Pool_Type)) |
a8551b5f AC |
4369 | then |
4370 | declare | |
a8551b5f | 4371 | Pool_Type : constant Entity_Id := Base_Type (Etype (Pool)); |
260359e3 | 4372 | Alloc_Op : Entity_Id; |
a8551b5f | 4373 | begin |
260359e3 | 4374 | Alloc_Op := Get_Name_Entity_Id (Name_Allocate); |
a8551b5f AC |
4375 | while Present (Alloc_Op) loop |
4376 | if Scope (Alloc_Op) = Scope (Pool_Type) | |
4377 | and then Present (First_Formal (Alloc_Op)) | |
4378 | and then Etype (First_Formal (Alloc_Op)) = Pool_Type | |
4379 | then | |
4380 | Set_Procedure_To_Call (N, Alloc_Op); | |
a8551b5f | 4381 | exit; |
260359e3 AC |
4382 | else |
4383 | Alloc_Op := Homonym (Alloc_Op); | |
a8551b5f | 4384 | end if; |
a8551b5f AC |
4385 | end loop; |
4386 | end; | |
4387 | ||
200b7162 BD |
4388 | elsif Is_Class_Wide_Type (Etype (Pool)) then |
4389 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
4390 | ||
4391 | else | |
4392 | Set_Procedure_To_Call (N, | |
4393 | Find_Prim_Op (Etype (Pool), Name_Allocate)); | |
4394 | end if; | |
70482933 RK |
4395 | end if; |
4396 | end if; | |
4397 | ||
685094bf RD |
4398 | -- Under certain circumstances we can replace an allocator by an access |
4399 | -- to statically allocated storage. The conditions, as noted in AARM | |
4400 | -- 3.10 (10c) are as follows: | |
70482933 RK |
4401 | |
4402 | -- Size and initial value is known at compile time | |
4403 | -- Access type is access-to-constant | |
4404 | ||
fbf5a39b AC |
4405 | -- The allocator is not part of a constraint on a record component, |
4406 | -- because in that case the inserted actions are delayed until the | |
4407 | -- record declaration is fully analyzed, which is too late for the | |
4408 | -- analysis of the rewritten allocator. | |
4409 | ||
70482933 RK |
4410 | if Is_Access_Constant (PtrT) |
4411 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
4412 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
243cae0a AC |
4413 | and then Size_Known_At_Compile_Time |
4414 | (Etype (Expression (Expression (N)))) | |
fbf5a39b | 4415 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
4416 | then |
4417 | -- Here we can do the optimization. For the allocator | |
4418 | ||
4419 | -- new x'(y) | |
4420 | ||
4421 | -- We insert an object declaration | |
4422 | ||
4423 | -- Tnn : aliased x := y; | |
4424 | ||
685094bf RD |
4425 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
4426 | -- marked as requiring static allocation. | |
70482933 | 4427 | |
df3e68b1 | 4428 | Temp := Make_Temporary (Loc, 'T', Expression (Expression (N))); |
70482933 RK |
4429 | Desig := Subtype_Mark (Expression (N)); |
4430 | ||
4431 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 4432 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
4433 | -- unconstrained subtype. |
4434 | ||
0da2c8ac AC |
4435 | if Entity (Desig) = Base_Type (Dtyp) then |
4436 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
4437 | end if; |
4438 | ||
4439 | Insert_Action (N, | |
4440 | Make_Object_Declaration (Loc, | |
4441 | Defining_Identifier => Temp, | |
4442 | Aliased_Present => True, | |
4443 | Constant_Present => Is_Access_Constant (PtrT), | |
4444 | Object_Definition => Desig, | |
4445 | Expression => Expression (Expression (N)))); | |
4446 | ||
4447 | Rewrite (N, | |
4448 | Make_Attribute_Reference (Loc, | |
243cae0a | 4449 | Prefix => New_Occurrence_Of (Temp, Loc), |
70482933 RK |
4450 | Attribute_Name => Name_Unrestricted_Access)); |
4451 | ||
4452 | Analyze_And_Resolve (N, PtrT); | |
4453 | ||
685094bf | 4454 | -- We set the variable as statically allocated, since we don't want |
a90bd866 | 4455 | -- it going on the stack of the current procedure. |
70482933 RK |
4456 | |
4457 | Set_Is_Statically_Allocated (Temp); | |
4458 | return; | |
4459 | end if; | |
4460 | ||
0669bebe GB |
4461 | -- Same if the allocator is an access discriminant for a local object: |
4462 | -- instead of an allocator we create a local value and constrain the | |
308e6f3a | 4463 | -- enclosing object with the corresponding access attribute. |
0669bebe | 4464 | |
26bff3d9 JM |
4465 | if Is_Static_Coextension (N) then |
4466 | Rewrite_Coextension (N); | |
0669bebe GB |
4467 | return; |
4468 | end if; | |
4469 | ||
8aec446b AC |
4470 | -- Check for size too large, we do this because the back end misses |
4471 | -- proper checks here and can generate rubbish allocation calls when | |
4472 | -- we are near the limit. We only do this for the 32-bit address case | |
4473 | -- since that is from a practical point of view where we see a problem. | |
4474 | ||
4475 | if System_Address_Size = 32 | |
4476 | and then not Storage_Checks_Suppressed (PtrT) | |
4477 | and then not Storage_Checks_Suppressed (Dtyp) | |
4478 | and then not Storage_Checks_Suppressed (Etyp) | |
4479 | then | |
4480 | -- The check we want to generate should look like | |
4481 | ||
4482 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
4483 | -- raise Storage_Error; | |
4484 | -- end if; | |
4485 | ||
308e6f3a | 4486 | -- where 3.5 gigabytes is a constant large enough to accommodate any |
507ed3fd AC |
4487 | -- reasonable request for. But we can't do it this way because at |
4488 | -- least at the moment we don't compute this attribute right, and | |
4489 | -- can silently give wrong results when the result gets large. Since | |
4490 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 4491 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 4492 | -- value of the attribute ??? |
8aec446b | 4493 | |
507ed3fd AC |
4494 | if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then |
4495 | Insert_Action (N, | |
4496 | Make_Raise_Storage_Error (Loc, | |
4497 | Condition => | |
4498 | Make_Op_Gt (Loc, | |
4499 | Left_Opnd => Size_In_Storage_Elements (Etyp), | |
4500 | Right_Opnd => | |
243cae0a | 4501 | Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))), |
507ed3fd AC |
4502 | Reason => SE_Object_Too_Large)); |
4503 | end if; | |
8aec446b AC |
4504 | end if; |
4505 | ||
b3b26ace AC |
4506 | -- If no storage pool has been specified and we have the restriction |
4507 | -- No_Standard_Allocators_After_Elaboration is present, then generate | |
4508 | -- a call to Elaboration_Allocators.Check_Standard_Allocator. | |
4509 | ||
4510 | if Nkind (N) = N_Allocator | |
4511 | and then No (Storage_Pool (N)) | |
4512 | and then Restriction_Active (No_Standard_Allocators_After_Elaboration) | |
4513 | then | |
4514 | Insert_Action (N, | |
4515 | Make_Procedure_Call_Statement (Loc, | |
4516 | Name => | |
4517 | New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc))); | |
4518 | end if; | |
4519 | ||
0da2c8ac | 4520 | -- Handle case of qualified expression (other than optimization above) |
cac5a801 AC |
4521 | -- First apply constraint checks, because the bounds or discriminants |
4522 | -- in the aggregate might not match the subtype mark in the allocator. | |
0da2c8ac | 4523 | |
70482933 | 4524 | if Nkind (Expression (N)) = N_Qualified_Expression then |
cac5a801 AC |
4525 | Apply_Constraint_Check |
4526 | (Expression (Expression (N)), Etype (Expression (N))); | |
4527 | ||
fbf5a39b | 4528 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
4529 | return; |
4530 | end if; | |
fbf5a39b | 4531 | |
26bff3d9 JM |
4532 | -- If the allocator is for a type which requires initialization, and |
4533 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
4534 | -- rather than a qualified expression), then we must generate a call to |
4535 | -- the initialization routine using an expressions action node: | |
70482933 | 4536 | |
26bff3d9 | 4537 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 4538 | |
26bff3d9 JM |
4539 | -- Here ptr_T is the pointer type for the allocator, and T is the |
4540 | -- subtype of the allocator. A special case arises if the designated | |
4541 | -- type of the access type is a task or contains tasks. In this case | |
4542 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
4543 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
6be44a9a | 4544 | -- for details). In addition, if the type T is a task type, then the |
26bff3d9 | 4545 | -- first argument to Init must be converted to the task record type. |
70482933 | 4546 | |
26bff3d9 | 4547 | declare |
df3e68b1 HK |
4548 | T : constant Entity_Id := Entity (Expression (N)); |
4549 | Args : List_Id; | |
4550 | Decls : List_Id; | |
4551 | Decl : Node_Id; | |
4552 | Discr : Elmt_Id; | |
4553 | Init : Entity_Id; | |
4554 | Init_Arg1 : Node_Id; | |
4555 | Temp_Decl : Node_Id; | |
4556 | Temp_Type : Entity_Id; | |
70482933 | 4557 | |
26bff3d9 JM |
4558 | begin |
4559 | if No_Initialization (N) then | |
df3e68b1 HK |
4560 | |
4561 | -- Even though this might be a simple allocation, create a custom | |
deb8dacc HK |
4562 | -- Allocate if the context requires it. Since .NET/JVM compilers |
4563 | -- do not support pools, this step is skipped. | |
df3e68b1 | 4564 | |
deb8dacc | 4565 | if VM_Target = No_VM |
d3f70b35 | 4566 | and then Present (Finalization_Master (PtrT)) |
deb8dacc | 4567 | then |
df3e68b1 | 4568 | Build_Allocate_Deallocate_Proc |
ca5af305 | 4569 | (N => N, |
df3e68b1 HK |
4570 | Is_Allocate => True); |
4571 | end if; | |
70482933 | 4572 | |
26bff3d9 | 4573 | -- Case of no initialization procedure present |
70482933 | 4574 | |
26bff3d9 | 4575 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 4576 | |
26bff3d9 | 4577 | -- Case of simple initialization required |
70482933 | 4578 | |
26bff3d9 | 4579 | if Needs_Simple_Initialization (T) then |
b4592168 | 4580 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
4581 | Rewrite (Expression (N), |
4582 | Make_Qualified_Expression (Loc, | |
4583 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 4584 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 4585 | |
26bff3d9 JM |
4586 | Analyze_And_Resolve (Expression (Expression (N)), T); |
4587 | Analyze_And_Resolve (Expression (N), T); | |
4588 | Set_Paren_Count (Expression (Expression (N)), 1); | |
4589 | Expand_N_Allocator (N); | |
70482933 | 4590 | |
26bff3d9 | 4591 | -- No initialization required |
70482933 RK |
4592 | |
4593 | else | |
26bff3d9 JM |
4594 | null; |
4595 | end if; | |
70482933 | 4596 | |
26bff3d9 | 4597 | -- Case of initialization procedure present, must be called |
70482933 | 4598 | |
26bff3d9 | 4599 | else |
b4592168 | 4600 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 4601 | |
b4592168 GD |
4602 | if not Restriction_Active (No_Default_Initialization) then |
4603 | Init := Base_Init_Proc (T); | |
4604 | Nod := N; | |
191fcb3a | 4605 | Temp := Make_Temporary (Loc, 'P'); |
70482933 | 4606 | |
b4592168 | 4607 | -- Construct argument list for the initialization routine call |
70482933 | 4608 | |
df3e68b1 | 4609 | Init_Arg1 := |
b4592168 | 4610 | Make_Explicit_Dereference (Loc, |
df3e68b1 | 4611 | Prefix => |
e4494292 | 4612 | New_Occurrence_Of (Temp, Loc)); |
df3e68b1 HK |
4613 | |
4614 | Set_Assignment_OK (Init_Arg1); | |
b4592168 | 4615 | Temp_Type := PtrT; |
26bff3d9 | 4616 | |
b4592168 GD |
4617 | -- The initialization procedure expects a specific type. if the |
4618 | -- context is access to class wide, indicate that the object | |
4619 | -- being allocated has the right specific type. | |
70482933 | 4620 | |
b4592168 | 4621 | if Is_Class_Wide_Type (Dtyp) then |
df3e68b1 | 4622 | Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1); |
b4592168 | 4623 | end if; |
70482933 | 4624 | |
b4592168 GD |
4625 | -- If designated type is a concurrent type or if it is private |
4626 | -- type whose definition is a concurrent type, the first | |
4627 | -- argument in the Init routine has to be unchecked conversion | |
4628 | -- to the corresponding record type. If the designated type is | |
243cae0a | 4629 | -- a derived type, also convert the argument to its root type. |
20b5d666 | 4630 | |
b4592168 | 4631 | if Is_Concurrent_Type (T) then |
df3e68b1 HK |
4632 | Init_Arg1 := |
4633 | Unchecked_Convert_To ( | |
4634 | Corresponding_Record_Type (T), Init_Arg1); | |
70482933 | 4635 | |
b4592168 GD |
4636 | elsif Is_Private_Type (T) |
4637 | and then Present (Full_View (T)) | |
4638 | and then Is_Concurrent_Type (Full_View (T)) | |
4639 | then | |
df3e68b1 | 4640 | Init_Arg1 := |
b4592168 | 4641 | Unchecked_Convert_To |
df3e68b1 | 4642 | (Corresponding_Record_Type (Full_View (T)), Init_Arg1); |
70482933 | 4643 | |
b4592168 GD |
4644 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
4645 | declare | |
4646 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
df3e68b1 | 4647 | |
b4592168 | 4648 | begin |
df3e68b1 HK |
4649 | Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1); |
4650 | Set_Etype (Init_Arg1, Ftyp); | |
b4592168 GD |
4651 | end; |
4652 | end if; | |
70482933 | 4653 | |
df3e68b1 | 4654 | Args := New_List (Init_Arg1); |
70482933 | 4655 | |
b4592168 GD |
4656 | -- For the task case, pass the Master_Id of the access type as |
4657 | -- the value of the _Master parameter, and _Chain as the value | |
4658 | -- of the _Chain parameter (_Chain will be defined as part of | |
4659 | -- the generated code for the allocator). | |
70482933 | 4660 | |
b4592168 GD |
4661 | -- In Ada 2005, the context may be a function that returns an |
4662 | -- anonymous access type. In that case the Master_Id has been | |
4663 | -- created when expanding the function declaration. | |
70482933 | 4664 | |
b4592168 GD |
4665 | if Has_Task (T) then |
4666 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 4667 | |
b4592168 GD |
4668 | -- The designated type was an incomplete type, and the |
4669 | -- access type did not get expanded. Salvage it now. | |
70482933 | 4670 | |
b941ae65 | 4671 | if not Restriction_Active (No_Task_Hierarchy) then |
3d67b239 AC |
4672 | if Present (Parent (Base_Type (PtrT))) then |
4673 | Expand_N_Full_Type_Declaration | |
4674 | (Parent (Base_Type (PtrT))); | |
4675 | ||
0d5fbf52 AC |
4676 | -- The only other possibility is an itype. For this |
4677 | -- case, the master must exist in the context. This is | |
4678 | -- the case when the allocator initializes an access | |
4679 | -- component in an init-proc. | |
3d67b239 | 4680 | |
0d5fbf52 | 4681 | else |
3d67b239 AC |
4682 | pragma Assert (Is_Itype (PtrT)); |
4683 | Build_Master_Renaming (PtrT, N); | |
4684 | end if; | |
b941ae65 | 4685 | end if; |
b4592168 | 4686 | end if; |
70482933 | 4687 | |
b4592168 GD |
4688 | -- If the context of the allocator is a declaration or an |
4689 | -- assignment, we can generate a meaningful image for it, | |
4690 | -- even though subsequent assignments might remove the | |
4691 | -- connection between task and entity. We build this image | |
4692 | -- when the left-hand side is a simple variable, a simple | |
4693 | -- indexed assignment or a simple selected component. | |
4694 | ||
4695 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
4696 | declare | |
4697 | Nam : constant Node_Id := Name (Parent (N)); | |
4698 | ||
4699 | begin | |
4700 | if Is_Entity_Name (Nam) then | |
4701 | Decls := | |
4702 | Build_Task_Image_Decls | |
4703 | (Loc, | |
4704 | New_Occurrence_Of | |
4705 | (Entity (Nam), Sloc (Nam)), T); | |
4706 | ||
243cae0a AC |
4707 | elsif Nkind_In (Nam, N_Indexed_Component, |
4708 | N_Selected_Component) | |
b4592168 GD |
4709 | and then Is_Entity_Name (Prefix (Nam)) |
4710 | then | |
4711 | Decls := | |
4712 | Build_Task_Image_Decls | |
4713 | (Loc, Nam, Etype (Prefix (Nam))); | |
4714 | else | |
4715 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
4716 | end if; | |
4717 | end; | |
70482933 | 4718 | |
b4592168 GD |
4719 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
4720 | Decls := | |
4721 | Build_Task_Image_Decls | |
4722 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 4723 | |
b4592168 GD |
4724 | else |
4725 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
4726 | end if; | |
26bff3d9 | 4727 | |
87dc09cb | 4728 | if Restriction_Active (No_Task_Hierarchy) then |
3c1ecd7e AC |
4729 | Append_To (Args, |
4730 | New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc)); | |
87dc09cb AC |
4731 | else |
4732 | Append_To (Args, | |
e4494292 | 4733 | New_Occurrence_Of |
87dc09cb AC |
4734 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); |
4735 | end if; | |
4736 | ||
b4592168 | 4737 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); |
26bff3d9 | 4738 | |
b4592168 GD |
4739 | Decl := Last (Decls); |
4740 | Append_To (Args, | |
4741 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 4742 | |
87dc09cb | 4743 | -- Has_Task is false, Decls not used |
26bff3d9 | 4744 | |
b4592168 GD |
4745 | else |
4746 | Decls := No_List; | |
26bff3d9 JM |
4747 | end if; |
4748 | ||
b4592168 GD |
4749 | -- Add discriminants if discriminated type |
4750 | ||
4751 | declare | |
4752 | Dis : Boolean := False; | |
4753 | Typ : Entity_Id; | |
4754 | ||
4755 | begin | |
4756 | if Has_Discriminants (T) then | |
4757 | Dis := True; | |
4758 | Typ := T; | |
4759 | ||
4760 | elsif Is_Private_Type (T) | |
4761 | and then Present (Full_View (T)) | |
4762 | and then Has_Discriminants (Full_View (T)) | |
20b5d666 | 4763 | then |
b4592168 GD |
4764 | Dis := True; |
4765 | Typ := Full_View (T); | |
20b5d666 | 4766 | end if; |
70482933 | 4767 | |
b4592168 | 4768 | if Dis then |
26bff3d9 | 4769 | |
b4592168 | 4770 | -- If the allocated object will be constrained by the |
685094bf RD |
4771 | -- default values for discriminants, then build a subtype |
4772 | -- with those defaults, and change the allocated subtype | |
4773 | -- to that. Note that this happens in fewer cases in Ada | |
4774 | -- 2005 (AI-363). | |
26bff3d9 | 4775 | |
b4592168 GD |
4776 | if not Is_Constrained (Typ) |
4777 | and then Present (Discriminant_Default_Value | |
df3e68b1 | 4778 | (First_Discriminant (Typ))) |
0791fbe9 | 4779 | and then (Ada_Version < Ada_2005 |
cc96a1b8 | 4780 | or else not |
0fbcb11c ES |
4781 | Object_Type_Has_Constrained_Partial_View |
4782 | (Typ, Current_Scope)) | |
20b5d666 | 4783 | then |
b4592168 | 4784 | Typ := Build_Default_Subtype (Typ, N); |
e4494292 | 4785 | Set_Expression (N, New_Occurrence_Of (Typ, Loc)); |
20b5d666 JM |
4786 | end if; |
4787 | ||
b4592168 GD |
4788 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
4789 | while Present (Discr) loop | |
4790 | Nod := Node (Discr); | |
4791 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 4792 | |
b4592168 GD |
4793 | -- AI-416: when the discriminant constraint is an |
4794 | -- anonymous access type make sure an accessibility | |
4795 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 4796 | |
0791fbe9 | 4797 | if Ada_Version >= Ada_2005 |
b4592168 GD |
4798 | and then |
4799 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
4800 | then | |
e84e11ba GD |
4801 | Apply_Accessibility_Check |
4802 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 4803 | end if; |
20b5d666 | 4804 | |
b4592168 GD |
4805 | Next_Elmt (Discr); |
4806 | end loop; | |
4807 | end if; | |
4808 | end; | |
70482933 | 4809 | |
4b985e20 | 4810 | -- We set the allocator as analyzed so that when we analyze |
9b16cb57 RD |
4811 | -- the if expression node, we do not get an unwanted recursive |
4812 | -- expansion of the allocator expression. | |
70482933 | 4813 | |
b4592168 GD |
4814 | Set_Analyzed (N, True); |
4815 | Nod := Relocate_Node (N); | |
70482933 | 4816 | |
b4592168 | 4817 | -- Here is the transformation: |
ca5af305 AC |
4818 | -- input: new Ctrl_Typ |
4819 | -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ; | |
4820 | -- Ctrl_TypIP (Temp.all, ...); | |
4821 | -- [Deep_]Initialize (Temp.all); | |
70482933 | 4822 | |
ca5af305 AC |
4823 | -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and |
4824 | -- is the subtype of the allocator. | |
70482933 | 4825 | |
b4592168 GD |
4826 | Temp_Decl := |
4827 | Make_Object_Declaration (Loc, | |
4828 | Defining_Identifier => Temp, | |
4829 | Constant_Present => True, | |
e4494292 | 4830 | Object_Definition => New_Occurrence_Of (Temp_Type, Loc), |
b4592168 | 4831 | Expression => Nod); |
70482933 | 4832 | |
b4592168 GD |
4833 | Set_Assignment_OK (Temp_Decl); |
4834 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 4835 | |
ca5af305 | 4836 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 4837 | |
b4592168 GD |
4838 | -- If the designated type is a task type or contains tasks, |
4839 | -- create block to activate created tasks, and insert | |
4840 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 4841 | |
b4592168 GD |
4842 | if Has_Task (T) then |
4843 | declare | |
4844 | L : constant List_Id := New_List; | |
4845 | Blk : Node_Id; | |
4846 | begin | |
4847 | Build_Task_Allocate_Block (L, Nod, Args); | |
4848 | Blk := Last (L); | |
4849 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
4850 | Insert_Actions (N, L); | |
4851 | end; | |
70482933 | 4852 | |
b4592168 GD |
4853 | else |
4854 | Insert_Action (N, | |
4855 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 4856 | Name => New_Occurrence_Of (Init, Loc), |
b4592168 GD |
4857 | Parameter_Associations => Args)); |
4858 | end if; | |
70482933 | 4859 | |
048e5cef | 4860 | if Needs_Finalization (T) then |
70482933 | 4861 | |
df3e68b1 HK |
4862 | -- Generate: |
4863 | -- [Deep_]Initialize (Init_Arg1); | |
70482933 | 4864 | |
df3e68b1 | 4865 | Insert_Action (N, |
243cae0a AC |
4866 | Make_Init_Call |
4867 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
4868 | Typ => T)); | |
b4592168 | 4869 | |
b254da66 | 4870 | if Present (Finalization_Master (PtrT)) then |
deb8dacc | 4871 | |
b254da66 AC |
4872 | -- Special processing for .NET/JVM, the allocated object |
4873 | -- is attached to the finalization master. Generate: | |
deb8dacc | 4874 | |
b254da66 | 4875 | -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1)); |
deb8dacc | 4876 | |
b254da66 AC |
4877 | -- Types derived from [Limited_]Controlled are the only |
4878 | -- ones considered since they have fields Prev and Next. | |
4879 | ||
e0c32166 AC |
4880 | if VM_Target /= No_VM then |
4881 | if Is_Controlled (T) then | |
4882 | Insert_Action (N, | |
4883 | Make_Attach_Call | |
4884 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
4885 | Ptr_Typ => PtrT)); | |
4886 | end if; | |
b254da66 AC |
4887 | |
4888 | -- Default case, generate: | |
4889 | ||
4890 | -- Set_Finalize_Address | |
4891 | -- (<PtrT>FM, <T>FD'Unrestricted_Access); | |
4892 | ||
5114f3ff AC |
4893 | -- Do not generate this call in CodePeer mode, as TSS |
4894 | -- primitive Finalize_Address is not created in this | |
4895 | -- mode. | |
b254da66 | 4896 | |
5114f3ff | 4897 | elsif not CodePeer_Mode then |
b254da66 AC |
4898 | Insert_Action (N, |
4899 | Make_Set_Finalize_Address_Call | |
4900 | (Loc => Loc, | |
4901 | Typ => T, | |
4902 | Ptr_Typ => PtrT)); | |
4903 | end if; | |
b4592168 | 4904 | end if; |
70482933 RK |
4905 | end if; |
4906 | ||
e4494292 | 4907 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
b4592168 GD |
4908 | Analyze_And_Resolve (N, PtrT); |
4909 | end if; | |
26bff3d9 JM |
4910 | end if; |
4911 | end; | |
f82944b7 | 4912 | |
26bff3d9 JM |
4913 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
4914 | -- object that has been rewritten as a reference, we displace "this" | |
4915 | -- to reference properly its secondary dispatch table. | |
4916 | ||
533369aa | 4917 | if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then |
26bff3d9 | 4918 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
4919 | end if; |
4920 | ||
fbf5a39b AC |
4921 | exception |
4922 | when RE_Not_Available => | |
4923 | return; | |
70482933 RK |
4924 | end Expand_N_Allocator; |
4925 | ||
4926 | ----------------------- | |
4927 | -- Expand_N_And_Then -- | |
4928 | ----------------------- | |
4929 | ||
5875f8d6 AC |
4930 | procedure Expand_N_And_Then (N : Node_Id) |
4931 | renames Expand_Short_Circuit_Operator; | |
70482933 | 4932 | |
19d846a0 RD |
4933 | ------------------------------ |
4934 | -- Expand_N_Case_Expression -- | |
4935 | ------------------------------ | |
4936 | ||
4937 | procedure Expand_N_Case_Expression (N : Node_Id) is | |
4938 | Loc : constant Source_Ptr := Sloc (N); | |
4939 | Typ : constant Entity_Id := Etype (N); | |
4940 | Cstmt : Node_Id; | |
27a8f150 | 4941 | Decl : Node_Id; |
19d846a0 RD |
4942 | Tnn : Entity_Id; |
4943 | Pnn : Entity_Id; | |
4944 | Actions : List_Id; | |
4945 | Ttyp : Entity_Id; | |
4946 | Alt : Node_Id; | |
4947 | Fexp : Node_Id; | |
4948 | ||
4949 | begin | |
b6b5cca8 AC |
4950 | -- Check for MINIMIZED/ELIMINATED overflow mode |
4951 | ||
4952 | if Minimized_Eliminated_Overflow_Check (N) then | |
4b1c4f20 RD |
4953 | Apply_Arithmetic_Overflow_Check (N); |
4954 | return; | |
4955 | end if; | |
4956 | ||
ff1f1705 AC |
4957 | -- If the case expression is a predicate specification, do not |
4958 | -- expand, because it will be converted to the proper predicate | |
4959 | -- form when building the predicate function. | |
4960 | ||
4961 | if Ekind_In (Current_Scope, E_Function, E_Procedure) | |
4962 | and then Is_Predicate_Function (Current_Scope) | |
4963 | then | |
4964 | return; | |
4965 | end if; | |
4966 | ||
19d846a0 RD |
4967 | -- We expand |
4968 | ||
4969 | -- case X is when A => AX, when B => BX ... | |
4970 | ||
4971 | -- to | |
4972 | ||
4973 | -- do | |
4974 | -- Tnn : typ; | |
4975 | -- case X is | |
4976 | -- when A => | |
4977 | -- Tnn := AX; | |
4978 | -- when B => | |
4979 | -- Tnn := BX; | |
4980 | -- ... | |
4981 | -- end case; | |
4982 | -- in Tnn end; | |
4983 | ||
4984 | -- However, this expansion is wrong for limited types, and also | |
4985 | -- wrong for unconstrained types (since the bounds may not be the | |
4986 | -- same in all branches). Furthermore it involves an extra copy | |
4987 | -- for large objects. So we take care of this by using the following | |
2492305b | 4988 | -- modified expansion for non-elementary types: |
19d846a0 RD |
4989 | |
4990 | -- do | |
4991 | -- type Pnn is access all typ; | |
4992 | -- Tnn : Pnn; | |
4993 | -- case X is | |
4994 | -- when A => | |
4995 | -- T := AX'Unrestricted_Access; | |
4996 | -- when B => | |
4997 | -- T := BX'Unrestricted_Access; | |
4998 | -- ... | |
4999 | -- end case; | |
5000 | -- in Tnn.all end; | |
5001 | ||
5002 | Cstmt := | |
5003 | Make_Case_Statement (Loc, | |
5004 | Expression => Expression (N), | |
5005 | Alternatives => New_List); | |
5006 | ||
414c6563 AC |
5007 | -- Preserve the original context for which the case statement is being |
5008 | -- generated. This is needed by the finalization machinery to prevent | |
5009 | -- the premature finalization of controlled objects found within the | |
5010 | -- case statement. | |
5011 | ||
5012 | Set_From_Conditional_Expression (Cstmt); | |
5013 | ||
19d846a0 RD |
5014 | Actions := New_List; |
5015 | ||
5016 | -- Scalar case | |
5017 | ||
2492305b | 5018 | if Is_Elementary_Type (Typ) then |
19d846a0 RD |
5019 | Ttyp := Typ; |
5020 | ||
5021 | else | |
5022 | Pnn := Make_Temporary (Loc, 'P'); | |
5023 | Append_To (Actions, | |
5024 | Make_Full_Type_Declaration (Loc, | |
5025 | Defining_Identifier => Pnn, | |
11d59a86 | 5026 | Type_Definition => |
19d846a0 | 5027 | Make_Access_To_Object_Definition (Loc, |
11d59a86 | 5028 | All_Present => True, |
e4494292 | 5029 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); |
19d846a0 RD |
5030 | Ttyp := Pnn; |
5031 | end if; | |
5032 | ||
5033 | Tnn := Make_Temporary (Loc, 'T'); | |
27a8f150 AC |
5034 | |
5035 | -- Create declaration for target of expression, and indicate that it | |
5036 | -- does not require initialization. | |
5037 | ||
11d59a86 AC |
5038 | Decl := |
5039 | Make_Object_Declaration (Loc, | |
19d846a0 | 5040 | Defining_Identifier => Tnn, |
27a8f150 AC |
5041 | Object_Definition => New_Occurrence_Of (Ttyp, Loc)); |
5042 | Set_No_Initialization (Decl); | |
5043 | Append_To (Actions, Decl); | |
19d846a0 RD |
5044 | |
5045 | -- Now process the alternatives | |
5046 | ||
5047 | Alt := First (Alternatives (N)); | |
5048 | while Present (Alt) loop | |
5049 | declare | |
eaed0c37 AC |
5050 | Aexp : Node_Id := Expression (Alt); |
5051 | Aloc : constant Source_Ptr := Sloc (Aexp); | |
5052 | Stats : List_Id; | |
19d846a0 RD |
5053 | |
5054 | begin | |
eaed0c37 AC |
5055 | -- As described above, take Unrestricted_Access for case of non- |
5056 | -- scalar types, to avoid big copies, and special cases. | |
05dbd302 | 5057 | |
2492305b | 5058 | if not Is_Elementary_Type (Typ) then |
19d846a0 RD |
5059 | Aexp := |
5060 | Make_Attribute_Reference (Aloc, | |
5061 | Prefix => Relocate_Node (Aexp), | |
5062 | Attribute_Name => Name_Unrestricted_Access); | |
5063 | end if; | |
5064 | ||
eaed0c37 AC |
5065 | Stats := New_List ( |
5066 | Make_Assignment_Statement (Aloc, | |
5067 | Name => New_Occurrence_Of (Tnn, Loc), | |
5068 | Expression => Aexp)); | |
5069 | ||
5070 | -- Propagate declarations inserted in the node by Insert_Actions | |
5071 | -- (for example, temporaries generated to remove side effects). | |
5072 | -- These actions must remain attached to the alternative, given | |
5073 | -- that they are generated by the corresponding expression. | |
5074 | ||
5075 | if Present (Sinfo.Actions (Alt)) then | |
5076 | Prepend_List (Sinfo.Actions (Alt), Stats); | |
5077 | end if; | |
5078 | ||
19d846a0 RD |
5079 | Append_To |
5080 | (Alternatives (Cstmt), | |
5081 | Make_Case_Statement_Alternative (Sloc (Alt), | |
5082 | Discrete_Choices => Discrete_Choices (Alt), | |
eaed0c37 | 5083 | Statements => Stats)); |
19d846a0 RD |
5084 | end; |
5085 | ||
5086 | Next (Alt); | |
5087 | end loop; | |
5088 | ||
5089 | Append_To (Actions, Cstmt); | |
5090 | ||
5091 | -- Construct and return final expression with actions | |
5092 | ||
2492305b | 5093 | if Is_Elementary_Type (Typ) then |
19d846a0 RD |
5094 | Fexp := New_Occurrence_Of (Tnn, Loc); |
5095 | else | |
5096 | Fexp := | |
5097 | Make_Explicit_Dereference (Loc, | |
5098 | Prefix => New_Occurrence_Of (Tnn, Loc)); | |
5099 | end if; | |
5100 | ||
5101 | Rewrite (N, | |
5102 | Make_Expression_With_Actions (Loc, | |
5103 | Expression => Fexp, | |
5104 | Actions => Actions)); | |
5105 | ||
5106 | Analyze_And_Resolve (N, Typ); | |
5107 | end Expand_N_Case_Expression; | |
5108 | ||
9b16cb57 RD |
5109 | ----------------------------------- |
5110 | -- Expand_N_Explicit_Dereference -- | |
5111 | ----------------------------------- | |
5112 | ||
5113 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
5114 | begin | |
5115 | -- Insert explicit dereference call for the checked storage pool case | |
5116 | ||
5117 | Insert_Dereference_Action (Prefix (N)); | |
5118 | ||
5119 | -- If the type is an Atomic type for which Atomic_Sync is enabled, then | |
5120 | -- we set the atomic sync flag. | |
5121 | ||
5122 | if Is_Atomic (Etype (N)) | |
5123 | and then not Atomic_Synchronization_Disabled (Etype (N)) | |
5124 | then | |
5125 | Activate_Atomic_Synchronization (N); | |
5126 | end if; | |
5127 | end Expand_N_Explicit_Dereference; | |
5128 | ||
5129 | -------------------------------------- | |
5130 | -- Expand_N_Expression_With_Actions -- | |
5131 | -------------------------------------- | |
5132 | ||
5133 | procedure Expand_N_Expression_With_Actions (N : Node_Id) is | |
3a845e07 | 5134 | |
4c7e0990 | 5135 | function Process_Action (Act : Node_Id) return Traverse_Result; |
b2c28399 AC |
5136 | -- Inspect and process a single action of an expression_with_actions for |
5137 | -- transient controlled objects. If such objects are found, the routine | |
5138 | -- generates code to clean them up when the context of the expression is | |
5139 | -- evaluated or elaborated. | |
9b16cb57 | 5140 | |
4c7e0990 AC |
5141 | -------------------- |
5142 | -- Process_Action -- | |
5143 | -------------------- | |
5144 | ||
5145 | function Process_Action (Act : Node_Id) return Traverse_Result is | |
4c7e0990 AC |
5146 | begin |
5147 | if Nkind (Act) = N_Object_Declaration | |
5148 | and then Is_Finalizable_Transient (Act, N) | |
5149 | then | |
b2c28399 AC |
5150 | Process_Transient_Object (Act, N); |
5151 | return Abandon; | |
9b16cb57 | 5152 | |
4c7e0990 AC |
5153 | -- Avoid processing temporary function results multiple times when |
5154 | -- dealing with nested expression_with_actions. | |
9b16cb57 | 5155 | |
4c7e0990 AC |
5156 | elsif Nkind (Act) = N_Expression_With_Actions then |
5157 | return Abandon; | |
5158 | ||
b2c28399 AC |
5159 | -- Do not process temporary function results in loops. This is done |
5160 | -- by Expand_N_Loop_Statement and Build_Finalizer. | |
4c7e0990 AC |
5161 | |
5162 | elsif Nkind (Act) = N_Loop_Statement then | |
5163 | return Abandon; | |
9b16cb57 RD |
5164 | end if; |
5165 | ||
4c7e0990 AC |
5166 | return OK; |
5167 | end Process_Action; | |
9b16cb57 | 5168 | |
4c7e0990 | 5169 | procedure Process_Single_Action is new Traverse_Proc (Process_Action); |
9b16cb57 RD |
5170 | |
5171 | -- Local variables | |
5172 | ||
4c7e0990 | 5173 | Act : Node_Id; |
9b16cb57 RD |
5174 | |
5175 | -- Start of processing for Expand_N_Expression_With_Actions | |
5176 | ||
5177 | begin | |
e0f63680 AC |
5178 | -- Process the actions as described above |
5179 | ||
4c7e0990 | 5180 | Act := First (Actions (N)); |
e0f63680 AC |
5181 | while Present (Act) loop |
5182 | Process_Single_Action (Act); | |
5183 | Next (Act); | |
5184 | end loop; | |
5185 | ||
ebdaa81b | 5186 | -- Deal with case where there are no actions. In this case we simply |
5a521b8a | 5187 | -- rewrite the node with its expression since we don't need the actions |
ebdaa81b AC |
5188 | -- and the specification of this node does not allow a null action list. |
5189 | ||
5a521b8a AC |
5190 | -- Note: we use Rewrite instead of Replace, because Codepeer is using |
5191 | -- the expanded tree and relying on being able to retrieve the original | |
5192 | -- tree in cases like this. This raises a whole lot of issues of whether | |
5193 | -- we have problems elsewhere, which will be addressed in the future??? | |
5194 | ||
e0f63680 | 5195 | if Is_Empty_List (Actions (N)) then |
5a521b8a | 5196 | Rewrite (N, Relocate_Node (Expression (N))); |
ebdaa81b | 5197 | end if; |
9b16cb57 RD |
5198 | end Expand_N_Expression_With_Actions; |
5199 | ||
5200 | ---------------------------- | |
5201 | -- Expand_N_If_Expression -- | |
5202 | ---------------------------- | |
70482933 | 5203 | |
4b985e20 | 5204 | -- Deal with limited types and condition actions |
70482933 | 5205 | |
9b16cb57 | 5206 | procedure Expand_N_If_Expression (N : Node_Id) is |
b2c28399 AC |
5207 | procedure Process_Actions (Actions : List_Id); |
5208 | -- Inspect and process a single action list of an if expression for | |
5209 | -- transient controlled objects. If such objects are found, the routine | |
5210 | -- generates code to clean them up when the context of the expression is | |
5211 | -- evaluated or elaborated. | |
3cebd1c0 | 5212 | |
b2c28399 AC |
5213 | --------------------- |
5214 | -- Process_Actions -- | |
5215 | --------------------- | |
3cebd1c0 | 5216 | |
b2c28399 AC |
5217 | procedure Process_Actions (Actions : List_Id) is |
5218 | Act : Node_Id; | |
3cebd1c0 AC |
5219 | |
5220 | begin | |
b2c28399 AC |
5221 | Act := First (Actions); |
5222 | while Present (Act) loop | |
5223 | if Nkind (Act) = N_Object_Declaration | |
5224 | and then Is_Finalizable_Transient (Act, N) | |
5225 | then | |
5226 | Process_Transient_Object (Act, N); | |
5227 | end if; | |
3cebd1c0 | 5228 | |
b2c28399 AC |
5229 | Next (Act); |
5230 | end loop; | |
5231 | end Process_Actions; | |
3cebd1c0 AC |
5232 | |
5233 | -- Local variables | |
5234 | ||
70482933 RK |
5235 | Loc : constant Source_Ptr := Sloc (N); |
5236 | Cond : constant Node_Id := First (Expressions (N)); | |
5237 | Thenx : constant Node_Id := Next (Cond); | |
5238 | Elsex : constant Node_Id := Next (Thenx); | |
5239 | Typ : constant Entity_Id := Etype (N); | |
c471e2da | 5240 | |
3cebd1c0 | 5241 | Actions : List_Id; |
602a7ec0 AC |
5242 | Cnn : Entity_Id; |
5243 | Decl : Node_Id; | |
3cebd1c0 | 5244 | Expr : Node_Id; |
602a7ec0 AC |
5245 | New_If : Node_Id; |
5246 | New_N : Node_Id; | |
b2c28399 | 5247 | Ptr_Typ : Entity_Id; |
70482933 | 5248 | |
a53c5613 AC |
5249 | -- Start of processing for Expand_N_If_Expression |
5250 | ||
70482933 | 5251 | begin |
b6b5cca8 AC |
5252 | -- Check for MINIMIZED/ELIMINATED overflow mode |
5253 | ||
5254 | if Minimized_Eliminated_Overflow_Check (N) then | |
5255 | Apply_Arithmetic_Overflow_Check (N); | |
5256 | return; | |
5257 | end if; | |
5258 | ||
602a7ec0 | 5259 | -- Fold at compile time if condition known. We have already folded |
9b16cb57 RD |
5260 | -- static if expressions, but it is possible to fold any case in which |
5261 | -- the condition is known at compile time, even though the result is | |
5262 | -- non-static. | |
602a7ec0 AC |
5263 | |
5264 | -- Note that we don't do the fold of such cases in Sem_Elab because | |
5265 | -- it can cause infinite loops with the expander adding a conditional | |
5266 | -- expression, and Sem_Elab circuitry removing it repeatedly. | |
5267 | ||
5268 | if Compile_Time_Known_Value (Cond) then | |
5269 | if Is_True (Expr_Value (Cond)) then | |
cc6f5d75 | 5270 | Expr := Thenx; |
602a7ec0 AC |
5271 | Actions := Then_Actions (N); |
5272 | else | |
cc6f5d75 | 5273 | Expr := Elsex; |
602a7ec0 AC |
5274 | Actions := Else_Actions (N); |
5275 | end if; | |
5276 | ||
5277 | Remove (Expr); | |
ae77c68b AC |
5278 | |
5279 | if Present (Actions) then | |
ae77c68b AC |
5280 | Rewrite (N, |
5281 | Make_Expression_With_Actions (Loc, | |
5282 | Expression => Relocate_Node (Expr), | |
5283 | Actions => Actions)); | |
5284 | Analyze_And_Resolve (N, Typ); | |
ae77c68b AC |
5285 | else |
5286 | Rewrite (N, Relocate_Node (Expr)); | |
5287 | end if; | |
602a7ec0 AC |
5288 | |
5289 | -- Note that the result is never static (legitimate cases of static | |
9b16cb57 | 5290 | -- if expressions were folded in Sem_Eval). |
602a7ec0 AC |
5291 | |
5292 | Set_Is_Static_Expression (N, False); | |
5293 | return; | |
5294 | end if; | |
5295 | ||
113a9fb6 AC |
5296 | -- If the type is limited, and the back end does not handle limited |
5297 | -- types, then we expand as follows to avoid the possibility of | |
5298 | -- improper copying. | |
ac7120ce | 5299 | |
c471e2da AC |
5300 | -- type Ptr is access all Typ; |
5301 | -- Cnn : Ptr; | |
ac7120ce RD |
5302 | -- if cond then |
5303 | -- <<then actions>> | |
5304 | -- Cnn := then-expr'Unrestricted_Access; | |
5305 | -- else | |
5306 | -- <<else actions>> | |
5307 | -- Cnn := else-expr'Unrestricted_Access; | |
5308 | -- end if; | |
5309 | ||
9b16cb57 | 5310 | -- and replace the if expression by a reference to Cnn.all. |
ac7120ce | 5311 | |
305caf42 AC |
5312 | -- This special case can be skipped if the back end handles limited |
5313 | -- types properly and ensures that no incorrect copies are made. | |
5314 | ||
5315 | if Is_By_Reference_Type (Typ) | |
5316 | and then not Back_End_Handles_Limited_Types | |
5317 | then | |
b2c28399 AC |
5318 | -- When the "then" or "else" expressions involve controlled function |
5319 | -- calls, generated temporaries are chained on the corresponding list | |
5320 | -- of actions. These temporaries need to be finalized after the if | |
5321 | -- expression is evaluated. | |
3cebd1c0 | 5322 | |
b2c28399 AC |
5323 | Process_Actions (Then_Actions (N)); |
5324 | Process_Actions (Else_Actions (N)); | |
3cebd1c0 | 5325 | |
b2c28399 AC |
5326 | -- Generate: |
5327 | -- type Ann is access all Typ; | |
3cebd1c0 | 5328 | |
b2c28399 | 5329 | Ptr_Typ := Make_Temporary (Loc, 'A'); |
3cebd1c0 | 5330 | |
b2c28399 AC |
5331 | Insert_Action (N, |
5332 | Make_Full_Type_Declaration (Loc, | |
5333 | Defining_Identifier => Ptr_Typ, | |
5334 | Type_Definition => | |
5335 | Make_Access_To_Object_Definition (Loc, | |
5336 | All_Present => True, | |
e4494292 | 5337 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); |
3cebd1c0 | 5338 | |
b2c28399 AC |
5339 | -- Generate: |
5340 | -- Cnn : Ann; | |
3cebd1c0 | 5341 | |
b2c28399 | 5342 | Cnn := Make_Temporary (Loc, 'C', N); |
3cebd1c0 | 5343 | |
b2c28399 AC |
5344 | Decl := |
5345 | Make_Object_Declaration (Loc, | |
5346 | Defining_Identifier => Cnn, | |
5347 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc)); | |
3cebd1c0 | 5348 | |
b2c28399 AC |
5349 | -- Generate: |
5350 | -- if Cond then | |
5351 | -- Cnn := <Thenx>'Unrestricted_Access; | |
5352 | -- else | |
5353 | -- Cnn := <Elsex>'Unrestricted_Access; | |
5354 | -- end if; | |
3cebd1c0 | 5355 | |
b2c28399 AC |
5356 | New_If := |
5357 | Make_Implicit_If_Statement (N, | |
5358 | Condition => Relocate_Node (Cond), | |
5359 | Then_Statements => New_List ( | |
5360 | Make_Assignment_Statement (Sloc (Thenx), | |
e4494292 | 5361 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), |
b2c28399 AC |
5362 | Expression => |
5363 | Make_Attribute_Reference (Loc, | |
5364 | Prefix => Relocate_Node (Thenx), | |
5365 | Attribute_Name => Name_Unrestricted_Access))), | |
3cebd1c0 | 5366 | |
b2c28399 AC |
5367 | Else_Statements => New_List ( |
5368 | Make_Assignment_Statement (Sloc (Elsex), | |
e4494292 | 5369 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), |
b2c28399 AC |
5370 | Expression => |
5371 | Make_Attribute_Reference (Loc, | |
5372 | Prefix => Relocate_Node (Elsex), | |
5373 | Attribute_Name => Name_Unrestricted_Access)))); | |
3cebd1c0 | 5374 | |
414c6563 AC |
5375 | -- Preserve the original context for which the if statement is being |
5376 | -- generated. This is needed by the finalization machinery to prevent | |
5377 | -- the premature finalization of controlled objects found within the | |
5378 | -- if statement. | |
5379 | ||
5380 | Set_From_Conditional_Expression (New_If); | |
5381 | ||
5382 | New_N := | |
5383 | Make_Explicit_Dereference (Loc, | |
5384 | Prefix => New_Occurrence_Of (Cnn, Loc)); | |
fb1949a0 | 5385 | |
113a9fb6 AC |
5386 | -- If the result is an unconstrained array and the if expression is in a |
5387 | -- context other than the initializing expression of the declaration of | |
5388 | -- an object, then we pull out the if expression as follows: | |
5389 | ||
5390 | -- Cnn : constant typ := if-expression | |
5391 | ||
5392 | -- and then replace the if expression with an occurrence of Cnn. This | |
5393 | -- avoids the need in the back end to create on-the-fly variable length | |
5394 | -- temporaries (which it cannot do!) | |
5395 | ||
5396 | -- Note that the test for being in an object declaration avoids doing an | |
5397 | -- unnecessary expansion, and also avoids infinite recursion. | |
5398 | ||
5399 | elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ) | |
5400 | and then (Nkind (Parent (N)) /= N_Object_Declaration | |
5401 | or else Expression (Parent (N)) /= N) | |
5402 | then | |
5403 | declare | |
5404 | Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N); | |
5405 | begin | |
5406 | Insert_Action (N, | |
5407 | Make_Object_Declaration (Loc, | |
5408 | Defining_Identifier => Cnn, | |
5409 | Constant_Present => True, | |
5410 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
5411 | Expression => Relocate_Node (N), | |
5412 | Has_Init_Expression => True)); | |
5413 | ||
5414 | Rewrite (N, New_Occurrence_Of (Cnn, Loc)); | |
5415 | return; | |
5416 | end; | |
5417 | ||
c471e2da AC |
5418 | -- For other types, we only need to expand if there are other actions |
5419 | -- associated with either branch. | |
5420 | ||
5421 | elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
c471e2da | 5422 | |
0812b84e | 5423 | -- We now wrap the actions into the appropriate expression |
fb1949a0 | 5424 | |
0812b84e AC |
5425 | if Present (Then_Actions (N)) then |
5426 | Rewrite (Thenx, | |
b2c28399 AC |
5427 | Make_Expression_With_Actions (Sloc (Thenx), |
5428 | Actions => Then_Actions (N), | |
5429 | Expression => Relocate_Node (Thenx))); | |
5430 | ||
0812b84e AC |
5431 | Set_Then_Actions (N, No_List); |
5432 | Analyze_And_Resolve (Thenx, Typ); | |
5433 | end if; | |
305caf42 | 5434 | |
0812b84e AC |
5435 | if Present (Else_Actions (N)) then |
5436 | Rewrite (Elsex, | |
b2c28399 AC |
5437 | Make_Expression_With_Actions (Sloc (Elsex), |
5438 | Actions => Else_Actions (N), | |
5439 | Expression => Relocate_Node (Elsex))); | |
5440 | ||
0812b84e AC |
5441 | Set_Else_Actions (N, No_List); |
5442 | Analyze_And_Resolve (Elsex, Typ); | |
305caf42 AC |
5443 | end if; |
5444 | ||
0812b84e AC |
5445 | return; |
5446 | ||
b2c28399 AC |
5447 | -- If no actions then no expansion needed, gigi will handle it using the |
5448 | -- same approach as a C conditional expression. | |
305caf42 AC |
5449 | |
5450 | else | |
c471e2da AC |
5451 | return; |
5452 | end if; | |
5453 | ||
305caf42 AC |
5454 | -- Fall through here for either the limited expansion, or the case of |
5455 | -- inserting actions for non-limited types. In both these cases, we must | |
5456 | -- move the SLOC of the parent If statement to the newly created one and | |
3fc5d116 RD |
5457 | -- change it to the SLOC of the expression which, after expansion, will |
5458 | -- correspond to what is being evaluated. | |
c471e2da | 5459 | |
533369aa | 5460 | if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then |
c471e2da AC |
5461 | Set_Sloc (New_If, Sloc (Parent (N))); |
5462 | Set_Sloc (Parent (N), Loc); | |
5463 | end if; | |
70482933 | 5464 | |
3fc5d116 RD |
5465 | -- Make sure Then_Actions and Else_Actions are appropriately moved |
5466 | -- to the new if statement. | |
5467 | ||
c471e2da AC |
5468 | if Present (Then_Actions (N)) then |
5469 | Insert_List_Before | |
5470 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
70482933 | 5471 | end if; |
c471e2da AC |
5472 | |
5473 | if Present (Else_Actions (N)) then | |
5474 | Insert_List_Before | |
5475 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
5476 | end if; | |
5477 | ||
5478 | Insert_Action (N, Decl); | |
5479 | Insert_Action (N, New_If); | |
5480 | Rewrite (N, New_N); | |
5481 | Analyze_And_Resolve (N, Typ); | |
9b16cb57 | 5482 | end Expand_N_If_Expression; |
35a1c212 | 5483 | |
70482933 RK |
5484 | ----------------- |
5485 | -- Expand_N_In -- | |
5486 | ----------------- | |
5487 | ||
5488 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 | 5489 | Loc : constant Source_Ptr := Sloc (N); |
4818e7b9 | 5490 | Restyp : constant Entity_Id := Etype (N); |
7324bf49 AC |
5491 | Lop : constant Node_Id := Left_Opnd (N); |
5492 | Rop : constant Node_Id := Right_Opnd (N); | |
5493 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 5494 | |
4818e7b9 RD |
5495 | Ltyp : Entity_Id; |
5496 | Rtyp : Entity_Id; | |
5497 | ||
630d30e9 RD |
5498 | procedure Substitute_Valid_Check; |
5499 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
5500 | -- test for the left operand being in range of its subtype. | |
5501 | ||
5502 | ---------------------------- | |
5503 | -- Substitute_Valid_Check -- | |
5504 | ---------------------------- | |
5505 | ||
5506 | procedure Substitute_Valid_Check is | |
5507 | begin | |
c7532b2d AC |
5508 | Rewrite (N, |
5509 | Make_Attribute_Reference (Loc, | |
5510 | Prefix => Relocate_Node (Lop), | |
5511 | Attribute_Name => Name_Valid)); | |
630d30e9 | 5512 | |
c7532b2d | 5513 | Analyze_And_Resolve (N, Restyp); |
630d30e9 | 5514 | |
acad3c0a AC |
5515 | -- Give warning unless overflow checking is MINIMIZED or ELIMINATED, |
5516 | -- in which case, this usage makes sense, and in any case, we have | |
5517 | -- actually eliminated the danger of optimization above. | |
5518 | ||
a7f1b24f | 5519 | if Overflow_Check_Mode not in Minimized_Or_Eliminated then |
324ac540 AC |
5520 | Error_Msg_N |
5521 | ("??explicit membership test may be optimized away", N); | |
acad3c0a | 5522 | Error_Msg_N -- CODEFIX |
324ac540 | 5523 | ("\??use ''Valid attribute instead", N); |
acad3c0a AC |
5524 | end if; |
5525 | ||
c7532b2d | 5526 | return; |
630d30e9 RD |
5527 | end Substitute_Valid_Check; |
5528 | ||
5529 | -- Start of processing for Expand_N_In | |
5530 | ||
70482933 | 5531 | begin |
308e6f3a | 5532 | -- If set membership case, expand with separate procedure |
4818e7b9 | 5533 | |
197e4514 | 5534 | if Present (Alternatives (N)) then |
a3068ca6 | 5535 | Expand_Set_Membership (N); |
197e4514 AC |
5536 | return; |
5537 | end if; | |
5538 | ||
4818e7b9 RD |
5539 | -- Not set membership, proceed with expansion |
5540 | ||
5541 | Ltyp := Etype (Left_Opnd (N)); | |
5542 | Rtyp := Etype (Right_Opnd (N)); | |
5543 | ||
5707e389 | 5544 | -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer |
f6194278 RD |
5545 | -- type, then expand with a separate procedure. Note the use of the |
5546 | -- flag No_Minimize_Eliminate to prevent infinite recursion. | |
5547 | ||
a7f1b24f | 5548 | if Overflow_Check_Mode in Minimized_Or_Eliminated |
f6194278 RD |
5549 | and then Is_Signed_Integer_Type (Ltyp) |
5550 | and then not No_Minimize_Eliminate (N) | |
5551 | then | |
5552 | Expand_Membership_Minimize_Eliminate_Overflow (N); | |
5553 | return; | |
5554 | end if; | |
5555 | ||
630d30e9 RD |
5556 | -- Check case of explicit test for an expression in range of its |
5557 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
b6b5cca8 | 5558 | -- test and give a warning for scalar types. |
630d30e9 | 5559 | |
4818e7b9 | 5560 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
5561 | |
5562 | -- Only relevant for source comparisons | |
5563 | ||
5564 | and then Comes_From_Source (N) | |
5565 | ||
5566 | -- In floating-point this is a standard way to check for finite values | |
5567 | -- and using 'Valid would typically be a pessimization. | |
5568 | ||
4818e7b9 | 5569 | and then not Is_Floating_Point_Type (Ltyp) |
b6b5cca8 AC |
5570 | |
5571 | -- Don't give the message unless right operand is a type entity and | |
5572 | -- the type of the left operand matches this type. Note that this | |
5573 | -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow | |
5574 | -- checks have changed the type of the left operand. | |
5575 | ||
630d30e9 | 5576 | and then Nkind (Rop) in N_Has_Entity |
4818e7b9 | 5577 | and then Ltyp = Entity (Rop) |
b6b5cca8 AC |
5578 | |
5579 | -- Skip in VM mode, where we have no sense of invalid values. The | |
5580 | -- warning still seems relevant, but not important enough to worry. | |
5581 | ||
26bff3d9 | 5582 | and then VM_Target = No_VM |
b6b5cca8 AC |
5583 | |
5584 | -- Skip this for predicated types, where such expressions are a | |
5585 | -- reasonable way of testing if something meets the predicate. | |
5586 | ||
3d6db7f8 | 5587 | and then not Present (Predicate_Function (Ltyp)) |
630d30e9 RD |
5588 | then |
5589 | Substitute_Valid_Check; | |
5590 | return; | |
5591 | end if; | |
5592 | ||
20b5d666 JM |
5593 | -- Do validity check on operands |
5594 | ||
5595 | if Validity_Checks_On and Validity_Check_Operands then | |
5596 | Ensure_Valid (Left_Opnd (N)); | |
5597 | Validity_Check_Range (Right_Opnd (N)); | |
5598 | end if; | |
5599 | ||
630d30e9 | 5600 | -- Case of explicit range |
fbf5a39b AC |
5601 | |
5602 | if Nkind (Rop) = N_Range then | |
5603 | declare | |
630d30e9 RD |
5604 | Lo : constant Node_Id := Low_Bound (Rop); |
5605 | Hi : constant Node_Id := High_Bound (Rop); | |
5606 | ||
5607 | Lo_Orig : constant Node_Id := Original_Node (Lo); | |
5608 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
5609 | ||
c800f862 RD |
5610 | Lcheck : Compare_Result; |
5611 | Ucheck : Compare_Result; | |
fbf5a39b | 5612 | |
d766cee3 RD |
5613 | Warn1 : constant Boolean := |
5614 | Constant_Condition_Warnings | |
c800f862 RD |
5615 | and then Comes_From_Source (N) |
5616 | and then not In_Instance; | |
d766cee3 | 5617 | -- This must be true for any of the optimization warnings, we |
9a0ddeee AC |
5618 | -- clearly want to give them only for source with the flag on. We |
5619 | -- also skip these warnings in an instance since it may be the | |
5620 | -- case that different instantiations have different ranges. | |
d766cee3 RD |
5621 | |
5622 | Warn2 : constant Boolean := | |
5623 | Warn1 | |
5624 | and then Nkind (Original_Node (Rop)) = N_Range | |
5625 | and then Is_Integer_Type (Etype (Lo)); | |
5626 | -- For the case where only one bound warning is elided, we also | |
5627 | -- insist on an explicit range and an integer type. The reason is | |
5628 | -- that the use of enumeration ranges including an end point is | |
9a0ddeee AC |
5629 | -- common, as is the use of a subtype name, one of whose bounds is |
5630 | -- the same as the type of the expression. | |
d766cee3 | 5631 | |
fbf5a39b | 5632 | begin |
c95e0edc | 5633 | -- If test is explicit x'First .. x'Last, replace by valid check |
630d30e9 | 5634 | |
e606088a AC |
5635 | -- Could use some individual comments for this complex test ??? |
5636 | ||
d766cee3 | 5637 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
5638 | |
5639 | -- And left operand is X'First where X matches left operand | |
5640 | -- type (this eliminates cases of type mismatch, including | |
5641 | -- the cases where ELIMINATED/MINIMIZED mode has changed the | |
5642 | -- type of the left operand. | |
5643 | ||
630d30e9 RD |
5644 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
5645 | and then Attribute_Name (Lo_Orig) = Name_First | |
5646 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 5647 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
b6b5cca8 | 5648 | |
cc6f5d75 | 5649 | -- Same tests for right operand |
b6b5cca8 | 5650 | |
630d30e9 RD |
5651 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
5652 | and then Attribute_Name (Hi_Orig) = Name_Last | |
5653 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 5654 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
b6b5cca8 AC |
5655 | |
5656 | -- Relevant only for source cases | |
5657 | ||
630d30e9 | 5658 | and then Comes_From_Source (N) |
b6b5cca8 AC |
5659 | |
5660 | -- Omit for VM cases, where we don't have invalid values | |
5661 | ||
26bff3d9 | 5662 | and then VM_Target = No_VM |
630d30e9 RD |
5663 | then |
5664 | Substitute_Valid_Check; | |
4818e7b9 | 5665 | goto Leave; |
630d30e9 RD |
5666 | end if; |
5667 | ||
d766cee3 RD |
5668 | -- If bounds of type are known at compile time, and the end points |
5669 | -- are known at compile time and identical, this is another case | |
5670 | -- for substituting a valid test. We only do this for discrete | |
5671 | -- types, since it won't arise in practice for float types. | |
5672 | ||
5673 | if Comes_From_Source (N) | |
5674 | and then Is_Discrete_Type (Ltyp) | |
5675 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
5676 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
5677 | and then Compile_Time_Known_Value (Lo) | |
5678 | and then Compile_Time_Known_Value (Hi) | |
5679 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
5680 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e | 5681 | |
f6194278 RD |
5682 | -- Kill warnings in instances, since they may be cases where we |
5683 | -- have a test in the generic that makes sense with some types | |
5684 | -- and not with other types. | |
94eefd2e RD |
5685 | |
5686 | and then not In_Instance | |
d766cee3 RD |
5687 | then |
5688 | Substitute_Valid_Check; | |
4818e7b9 | 5689 | goto Leave; |
d766cee3 RD |
5690 | end if; |
5691 | ||
9a0ddeee AC |
5692 | -- If we have an explicit range, do a bit of optimization based on |
5693 | -- range analysis (we may be able to kill one or both checks). | |
630d30e9 | 5694 | |
c800f862 RD |
5695 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
5696 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
5697 | ||
630d30e9 RD |
5698 | -- If either check is known to fail, replace result by False since |
5699 | -- the other check does not matter. Preserve the static flag for | |
5700 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
5701 | |
5702 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 5703 | if Warn1 then |
685bc70f AC |
5704 | Error_Msg_N ("?c?range test optimized away", N); |
5705 | Error_Msg_N ("\?c?value is known to be out of range", N); | |
d766cee3 RD |
5706 | end if; |
5707 | ||
e4494292 | 5708 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 5709 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 5710 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 5711 | goto Leave; |
fbf5a39b | 5712 | |
685094bf RD |
5713 | -- If both checks are known to succeed, replace result by True, |
5714 | -- since we know we are in range. | |
fbf5a39b AC |
5715 | |
5716 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 5717 | if Warn1 then |
685bc70f AC |
5718 | Error_Msg_N ("?c?range test optimized away", N); |
5719 | Error_Msg_N ("\?c?value is known to be in range", N); | |
d766cee3 RD |
5720 | end if; |
5721 | ||
e4494292 | 5722 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 5723 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 5724 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 5725 | goto Leave; |
fbf5a39b | 5726 | |
d766cee3 RD |
5727 | -- If lower bound check succeeds and upper bound check is not |
5728 | -- known to succeed or fail, then replace the range check with | |
5729 | -- a comparison against the upper bound. | |
fbf5a39b AC |
5730 | |
5731 | elsif Lcheck in Compare_GE then | |
94eefd2e | 5732 | if Warn2 and then not In_Instance then |
324ac540 AC |
5733 | Error_Msg_N ("??lower bound test optimized away", Lo); |
5734 | Error_Msg_N ("\??value is known to be in range", Lo); | |
d766cee3 RD |
5735 | end if; |
5736 | ||
fbf5a39b AC |
5737 | Rewrite (N, |
5738 | Make_Op_Le (Loc, | |
5739 | Left_Opnd => Lop, | |
5740 | Right_Opnd => High_Bound (Rop))); | |
4818e7b9 RD |
5741 | Analyze_And_Resolve (N, Restyp); |
5742 | goto Leave; | |
fbf5a39b | 5743 | |
d766cee3 RD |
5744 | -- If upper bound check succeeds and lower bound check is not |
5745 | -- known to succeed or fail, then replace the range check with | |
5746 | -- a comparison against the lower bound. | |
fbf5a39b AC |
5747 | |
5748 | elsif Ucheck in Compare_LE then | |
94eefd2e | 5749 | if Warn2 and then not In_Instance then |
324ac540 AC |
5750 | Error_Msg_N ("??upper bound test optimized away", Hi); |
5751 | Error_Msg_N ("\??value is known to be in range", Hi); | |
d766cee3 RD |
5752 | end if; |
5753 | ||
fbf5a39b AC |
5754 | Rewrite (N, |
5755 | Make_Op_Ge (Loc, | |
5756 | Left_Opnd => Lop, | |
5757 | Right_Opnd => Low_Bound (Rop))); | |
4818e7b9 RD |
5758 | Analyze_And_Resolve (N, Restyp); |
5759 | goto Leave; | |
fbf5a39b | 5760 | end if; |
c800f862 RD |
5761 | |
5762 | -- We couldn't optimize away the range check, but there is one | |
5763 | -- more issue. If we are checking constant conditionals, then we | |
5764 | -- see if we can determine the outcome assuming everything is | |
5765 | -- valid, and if so give an appropriate warning. | |
5766 | ||
5767 | if Warn1 and then not Assume_No_Invalid_Values then | |
5768 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
5769 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
5770 | ||
5771 | -- Result is out of range for valid value | |
5772 | ||
5773 | if Lcheck = LT or else Ucheck = GT then | |
ed2233dc | 5774 | Error_Msg_N |
685bc70f | 5775 | ("?c?value can only be in range if it is invalid", N); |
c800f862 RD |
5776 | |
5777 | -- Result is in range for valid value | |
5778 | ||
5779 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
ed2233dc | 5780 | Error_Msg_N |
685bc70f | 5781 | ("?c?value can only be out of range if it is invalid", N); |
c800f862 RD |
5782 | |
5783 | -- Lower bound check succeeds if value is valid | |
5784 | ||
5785 | elsif Warn2 and then Lcheck in Compare_GE then | |
ed2233dc | 5786 | Error_Msg_N |
685bc70f | 5787 | ("?c?lower bound check only fails if it is invalid", Lo); |
c800f862 RD |
5788 | |
5789 | -- Upper bound check succeeds if value is valid | |
5790 | ||
5791 | elsif Warn2 and then Ucheck in Compare_LE then | |
ed2233dc | 5792 | Error_Msg_N |
685bc70f | 5793 | ("?c?upper bound check only fails for invalid values", Hi); |
c800f862 RD |
5794 | end if; |
5795 | end if; | |
fbf5a39b AC |
5796 | end; |
5797 | ||
5798 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 5799 | |
4818e7b9 | 5800 | goto Leave; |
70482933 RK |
5801 | |
5802 | -- Here right operand is a subtype mark | |
5803 | ||
5804 | else | |
5805 | declare | |
82878151 AC |
5806 | Typ : Entity_Id := Etype (Rop); |
5807 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
5808 | Cond : Node_Id := Empty; | |
5809 | New_N : Node_Id; | |
5810 | Obj : Node_Id := Lop; | |
5811 | SCIL_Node : Node_Id; | |
70482933 RK |
5812 | |
5813 | begin | |
5814 | Remove_Side_Effects (Obj); | |
5815 | ||
5816 | -- For tagged type, do tagged membership operation | |
5817 | ||
5818 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 5819 | |
26bff3d9 JM |
5820 | -- No expansion will be performed when VM_Target, as the VM |
5821 | -- back-ends will handle the membership tests directly (tags | |
5822 | -- are not explicitly represented in Java objects, so the | |
5823 | -- normal tagged membership expansion is not what we want). | |
70482933 | 5824 | |
1f110335 | 5825 | if Tagged_Type_Expansion then |
82878151 AC |
5826 | Tagged_Membership (N, SCIL_Node, New_N); |
5827 | Rewrite (N, New_N); | |
4818e7b9 | 5828 | Analyze_And_Resolve (N, Restyp); |
82878151 AC |
5829 | |
5830 | -- Update decoration of relocated node referenced by the | |
5831 | -- SCIL node. | |
5832 | ||
9a0ddeee | 5833 | if Generate_SCIL and then Present (SCIL_Node) then |
7665e4bd | 5834 | Set_SCIL_Node (N, SCIL_Node); |
82878151 | 5835 | end if; |
70482933 RK |
5836 | end if; |
5837 | ||
4818e7b9 | 5838 | goto Leave; |
70482933 | 5839 | |
c95e0edc | 5840 | -- If type is scalar type, rewrite as x in t'First .. t'Last. |
70482933 | 5841 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
5842 | -- type if they come from the original type definition. Also this |
5843 | -- way we get all the processing above for an explicit range. | |
70482933 | 5844 | |
f6194278 | 5845 | -- Don't do this for predicated types, since in this case we |
a90bd866 | 5846 | -- want to check the predicate. |
c0f136cd | 5847 | |
c7532b2d AC |
5848 | elsif Is_Scalar_Type (Typ) then |
5849 | if No (Predicate_Function (Typ)) then | |
5850 | Rewrite (Rop, | |
5851 | Make_Range (Loc, | |
5852 | Low_Bound => | |
5853 | Make_Attribute_Reference (Loc, | |
5854 | Attribute_Name => Name_First, | |
e4494292 | 5855 | Prefix => New_Occurrence_Of (Typ, Loc)), |
c7532b2d AC |
5856 | |
5857 | High_Bound => | |
5858 | Make_Attribute_Reference (Loc, | |
5859 | Attribute_Name => Name_Last, | |
e4494292 | 5860 | Prefix => New_Occurrence_Of (Typ, Loc)))); |
c7532b2d AC |
5861 | Analyze_And_Resolve (N, Restyp); |
5862 | end if; | |
70482933 | 5863 | |
4818e7b9 | 5864 | goto Leave; |
5d09245e AC |
5865 | |
5866 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
5867 | -- a membership test if the subtype mark denotes a constrained | |
5868 | -- Unchecked_Union subtype and the expression lacks inferable | |
5869 | -- discriminants. | |
5870 | ||
5871 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
5872 | and then Is_Constrained (Typ) | |
5873 | and then not Has_Inferable_Discriminants (Lop) | |
5874 | then | |
5875 | Insert_Action (N, | |
5876 | Make_Raise_Program_Error (Loc, | |
5877 | Reason => PE_Unchecked_Union_Restriction)); | |
5878 | ||
9a0ddeee | 5879 | -- Prevent Gigi from generating incorrect code by rewriting the |
f6194278 | 5880 | -- test as False. What is this undocumented thing about ??? |
5d09245e | 5881 | |
9a0ddeee | 5882 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 5883 | goto Leave; |
70482933 RK |
5884 | end if; |
5885 | ||
fbf5a39b AC |
5886 | -- Here we have a non-scalar type |
5887 | ||
70482933 RK |
5888 | if Is_Acc then |
5889 | Typ := Designated_Type (Typ); | |
5890 | end if; | |
5891 | ||
5892 | if not Is_Constrained (Typ) then | |
e4494292 | 5893 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 5894 | Analyze_And_Resolve (N, Restyp); |
70482933 | 5895 | |
685094bf RD |
5896 | -- For the constrained array case, we have to check the subscripts |
5897 | -- for an exact match if the lengths are non-zero (the lengths | |
5898 | -- must match in any case). | |
70482933 RK |
5899 | |
5900 | elsif Is_Array_Type (Typ) then | |
fbf5a39b | 5901 | Check_Subscripts : declare |
9a0ddeee | 5902 | function Build_Attribute_Reference |
2e071734 AC |
5903 | (E : Node_Id; |
5904 | Nam : Name_Id; | |
5905 | Dim : Nat) return Node_Id; | |
9a0ddeee | 5906 | -- Build attribute reference E'Nam (Dim) |
70482933 | 5907 | |
9a0ddeee AC |
5908 | ------------------------------- |
5909 | -- Build_Attribute_Reference -- | |
5910 | ------------------------------- | |
fbf5a39b | 5911 | |
9a0ddeee | 5912 | function Build_Attribute_Reference |
2e071734 AC |
5913 | (E : Node_Id; |
5914 | Nam : Name_Id; | |
5915 | Dim : Nat) return Node_Id | |
70482933 RK |
5916 | is |
5917 | begin | |
5918 | return | |
5919 | Make_Attribute_Reference (Loc, | |
9a0ddeee | 5920 | Prefix => E, |
70482933 | 5921 | Attribute_Name => Nam, |
9a0ddeee | 5922 | Expressions => New_List ( |
70482933 | 5923 | Make_Integer_Literal (Loc, Dim))); |
9a0ddeee | 5924 | end Build_Attribute_Reference; |
70482933 | 5925 | |
fad0600d | 5926 | -- Start of processing for Check_Subscripts |
fbf5a39b | 5927 | |
70482933 RK |
5928 | begin |
5929 | for J in 1 .. Number_Dimensions (Typ) loop | |
5930 | Evolve_And_Then (Cond, | |
5931 | Make_Op_Eq (Loc, | |
5932 | Left_Opnd => | |
9a0ddeee | 5933 | Build_Attribute_Reference |
fbf5a39b AC |
5934 | (Duplicate_Subexpr_No_Checks (Obj), |
5935 | Name_First, J), | |
70482933 | 5936 | Right_Opnd => |
9a0ddeee | 5937 | Build_Attribute_Reference |
70482933 RK |
5938 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); |
5939 | ||
5940 | Evolve_And_Then (Cond, | |
5941 | Make_Op_Eq (Loc, | |
5942 | Left_Opnd => | |
9a0ddeee | 5943 | Build_Attribute_Reference |
fbf5a39b AC |
5944 | (Duplicate_Subexpr_No_Checks (Obj), |
5945 | Name_Last, J), | |
70482933 | 5946 | Right_Opnd => |
9a0ddeee | 5947 | Build_Attribute_Reference |
70482933 RK |
5948 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); |
5949 | end loop; | |
5950 | ||
5951 | if Is_Acc then | |
fbf5a39b AC |
5952 | Cond := |
5953 | Make_Or_Else (Loc, | |
cc6f5d75 | 5954 | Left_Opnd => |
fbf5a39b AC |
5955 | Make_Op_Eq (Loc, |
5956 | Left_Opnd => Obj, | |
5957 | Right_Opnd => Make_Null (Loc)), | |
5958 | Right_Opnd => Cond); | |
70482933 RK |
5959 | end if; |
5960 | ||
5961 | Rewrite (N, Cond); | |
4818e7b9 | 5962 | Analyze_And_Resolve (N, Restyp); |
fbf5a39b | 5963 | end Check_Subscripts; |
70482933 | 5964 | |
685094bf RD |
5965 | -- These are the cases where constraint checks may be required, |
5966 | -- e.g. records with possible discriminants | |
70482933 RK |
5967 | |
5968 | else | |
5969 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
5970 | -- The expression that is built is the negation of the one that |
5971 | -- is used for checking discriminant constraints. | |
70482933 RK |
5972 | |
5973 | Obj := Relocate_Node (Left_Opnd (N)); | |
5974 | ||
5975 | if Has_Discriminants (Typ) then | |
5976 | Cond := Make_Op_Not (Loc, | |
5977 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
5978 | ||
5979 | if Is_Acc then | |
5980 | Cond := Make_Or_Else (Loc, | |
cc6f5d75 | 5981 | Left_Opnd => |
70482933 RK |
5982 | Make_Op_Eq (Loc, |
5983 | Left_Opnd => Obj, | |
5984 | Right_Opnd => Make_Null (Loc)), | |
5985 | Right_Opnd => Cond); | |
5986 | end if; | |
5987 | ||
5988 | else | |
5989 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
5990 | end if; | |
5991 | ||
5992 | Rewrite (N, Cond); | |
4818e7b9 | 5993 | Analyze_And_Resolve (N, Restyp); |
70482933 | 5994 | end if; |
6cce2156 GD |
5995 | |
5996 | -- Ada 2012 (AI05-0149): Handle membership tests applied to an | |
5997 | -- expression of an anonymous access type. This can involve an | |
5998 | -- accessibility test and a tagged type membership test in the | |
5999 | -- case of tagged designated types. | |
6000 | ||
6001 | if Ada_Version >= Ada_2012 | |
6002 | and then Is_Acc | |
6003 | and then Ekind (Ltyp) = E_Anonymous_Access_Type | |
6004 | then | |
6005 | declare | |
6006 | Expr_Entity : Entity_Id := Empty; | |
6007 | New_N : Node_Id; | |
6008 | Param_Level : Node_Id; | |
6009 | Type_Level : Node_Id; | |
996c8821 | 6010 | |
6cce2156 GD |
6011 | begin |
6012 | if Is_Entity_Name (Lop) then | |
6013 | Expr_Entity := Param_Entity (Lop); | |
996c8821 | 6014 | |
6cce2156 GD |
6015 | if not Present (Expr_Entity) then |
6016 | Expr_Entity := Entity (Lop); | |
6017 | end if; | |
6018 | end if; | |
6019 | ||
6020 | -- If a conversion of the anonymous access value to the | |
6021 | -- tested type would be illegal, then the result is False. | |
6022 | ||
6023 | if not Valid_Conversion | |
6024 | (Lop, Rtyp, Lop, Report_Errs => False) | |
6025 | then | |
6026 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
6027 | Analyze_And_Resolve (N, Restyp); | |
6028 | ||
6029 | -- Apply an accessibility check if the access object has an | |
6030 | -- associated access level and when the level of the type is | |
6031 | -- less deep than the level of the access parameter. This | |
6032 | -- only occur for access parameters and stand-alone objects | |
6033 | -- of an anonymous access type. | |
6034 | ||
6035 | else | |
6036 | if Present (Expr_Entity) | |
996c8821 RD |
6037 | and then |
6038 | Present | |
6039 | (Effective_Extra_Accessibility (Expr_Entity)) | |
6040 | and then UI_Gt (Object_Access_Level (Lop), | |
6041 | Type_Access_Level (Rtyp)) | |
6cce2156 GD |
6042 | then |
6043 | Param_Level := | |
6044 | New_Occurrence_Of | |
d15f9422 | 6045 | (Effective_Extra_Accessibility (Expr_Entity), Loc); |
6cce2156 GD |
6046 | |
6047 | Type_Level := | |
6048 | Make_Integer_Literal (Loc, Type_Access_Level (Rtyp)); | |
6049 | ||
6050 | -- Return True only if the accessibility level of the | |
6051 | -- expression entity is not deeper than the level of | |
6052 | -- the tested access type. | |
6053 | ||
6054 | Rewrite (N, | |
6055 | Make_And_Then (Loc, | |
6056 | Left_Opnd => Relocate_Node (N), | |
6057 | Right_Opnd => Make_Op_Le (Loc, | |
6058 | Left_Opnd => Param_Level, | |
6059 | Right_Opnd => Type_Level))); | |
6060 | ||
6061 | Analyze_And_Resolve (N); | |
6062 | end if; | |
6063 | ||
6064 | -- If the designated type is tagged, do tagged membership | |
6065 | -- operation. | |
6066 | ||
6067 | -- *** NOTE: we have to check not null before doing the | |
6068 | -- tagged membership test (but maybe that can be done | |
6069 | -- inside Tagged_Membership?). | |
6070 | ||
6071 | if Is_Tagged_Type (Typ) then | |
6072 | Rewrite (N, | |
6073 | Make_And_Then (Loc, | |
6074 | Left_Opnd => Relocate_Node (N), | |
6075 | Right_Opnd => | |
6076 | Make_Op_Ne (Loc, | |
6077 | Left_Opnd => Obj, | |
6078 | Right_Opnd => Make_Null (Loc)))); | |
6079 | ||
6080 | -- No expansion will be performed when VM_Target, as | |
6081 | -- the VM back-ends will handle the membership tests | |
6082 | -- directly (tags are not explicitly represented in | |
6083 | -- Java objects, so the normal tagged membership | |
6084 | -- expansion is not what we want). | |
6085 | ||
6086 | if Tagged_Type_Expansion then | |
6087 | ||
6088 | -- Note that we have to pass Original_Node, because | |
6089 | -- the membership test might already have been | |
6090 | -- rewritten by earlier parts of membership test. | |
6091 | ||
6092 | Tagged_Membership | |
6093 | (Original_Node (N), SCIL_Node, New_N); | |
6094 | ||
6095 | -- Update decoration of relocated node referenced | |
6096 | -- by the SCIL node. | |
6097 | ||
6098 | if Generate_SCIL and then Present (SCIL_Node) then | |
6099 | Set_SCIL_Node (New_N, SCIL_Node); | |
6100 | end if; | |
6101 | ||
6102 | Rewrite (N, | |
6103 | Make_And_Then (Loc, | |
6104 | Left_Opnd => Relocate_Node (N), | |
6105 | Right_Opnd => New_N)); | |
6106 | ||
6107 | Analyze_And_Resolve (N, Restyp); | |
6108 | end if; | |
6109 | end if; | |
6110 | end if; | |
6111 | end; | |
6112 | end if; | |
70482933 RK |
6113 | end; |
6114 | end if; | |
4818e7b9 RD |
6115 | |
6116 | -- At this point, we have done the processing required for the basic | |
6117 | -- membership test, but not yet dealt with the predicate. | |
6118 | ||
6119 | <<Leave>> | |
6120 | ||
c7532b2d AC |
6121 | -- If a predicate is present, then we do the predicate test, but we |
6122 | -- most certainly want to omit this if we are within the predicate | |
a90bd866 | 6123 | -- function itself, since otherwise we have an infinite recursion. |
3d6db7f8 GD |
6124 | -- The check should also not be emitted when testing against a range |
6125 | -- (the check is only done when the right operand is a subtype; see | |
6126 | -- RM12-4.5.2 (28.1/3-30/3)). | |
4818e7b9 | 6127 | |
c7532b2d AC |
6128 | declare |
6129 | PFunc : constant Entity_Id := Predicate_Function (Rtyp); | |
4818e7b9 | 6130 | |
c7532b2d AC |
6131 | begin |
6132 | if Present (PFunc) | |
6133 | and then Current_Scope /= PFunc | |
3d6db7f8 | 6134 | and then Nkind (Rop) /= N_Range |
c7532b2d AC |
6135 | then |
6136 | Rewrite (N, | |
6137 | Make_And_Then (Loc, | |
6138 | Left_Opnd => Relocate_Node (N), | |
fc142f63 | 6139 | Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True))); |
4818e7b9 | 6140 | |
c7532b2d | 6141 | -- Analyze new expression, mark left operand as analyzed to |
b2009d46 AC |
6142 | -- avoid infinite recursion adding predicate calls. Similarly, |
6143 | -- suppress further range checks on the call. | |
4818e7b9 | 6144 | |
c7532b2d | 6145 | Set_Analyzed (Left_Opnd (N)); |
b2009d46 | 6146 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); |
4818e7b9 | 6147 | |
c7532b2d AC |
6148 | -- All done, skip attempt at compile time determination of result |
6149 | ||
6150 | return; | |
6151 | end if; | |
6152 | end; | |
70482933 RK |
6153 | end Expand_N_In; |
6154 | ||
6155 | -------------------------------- | |
6156 | -- Expand_N_Indexed_Component -- | |
6157 | -------------------------------- | |
6158 | ||
6159 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
6160 | Loc : constant Source_Ptr := Sloc (N); | |
6161 | Typ : constant Entity_Id := Etype (N); | |
6162 | P : constant Node_Id := Prefix (N); | |
6163 | T : constant Entity_Id := Etype (P); | |
5972791c | 6164 | Atp : Entity_Id; |
70482933 RK |
6165 | |
6166 | begin | |
685094bf RD |
6167 | -- A special optimization, if we have an indexed component that is |
6168 | -- selecting from a slice, then we can eliminate the slice, since, for | |
6169 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
6170 | -- the range check required by the slice. The range check for the slice | |
6171 | -- itself has already been generated. The range check for the | |
6172 | -- subscripting operation is ensured by converting the subject to | |
6173 | -- the subtype of the slice. | |
6174 | ||
6175 | -- This optimization not only generates better code, avoiding slice | |
6176 | -- messing especially in the packed case, but more importantly bypasses | |
6177 | -- some problems in handling this peculiar case, for example, the issue | |
6178 | -- of dealing specially with object renamings. | |
70482933 | 6179 | |
45ec05e1 RD |
6180 | if Nkind (P) = N_Slice |
6181 | ||
6182 | -- This optimization is disabled for CodePeer because it can transform | |
6183 | -- an index-check constraint_error into a range-check constraint_error | |
6184 | -- and CodePeer cares about that distinction. | |
6185 | ||
6186 | and then not CodePeer_Mode | |
6187 | then | |
70482933 RK |
6188 | Rewrite (N, |
6189 | Make_Indexed_Component (Loc, | |
cc6f5d75 | 6190 | Prefix => Prefix (P), |
70482933 RK |
6191 | Expressions => New_List ( |
6192 | Convert_To | |
6193 | (Etype (First_Index (Etype (P))), | |
6194 | First (Expressions (N)))))); | |
6195 | Analyze_And_Resolve (N, Typ); | |
6196 | return; | |
6197 | end if; | |
6198 | ||
b4592168 GD |
6199 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
6200 | -- function, then additional actuals must be passed. | |
6201 | ||
0791fbe9 | 6202 | if Ada_Version >= Ada_2005 |
b4592168 GD |
6203 | and then Is_Build_In_Place_Function_Call (P) |
6204 | then | |
6205 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
6206 | end if; | |
6207 | ||
685094bf | 6208 | -- If the prefix is an access type, then we unconditionally rewrite if |
09494c32 | 6209 | -- as an explicit dereference. This simplifies processing for several |
685094bf RD |
6210 | -- cases, including packed array cases and certain cases in which checks |
6211 | -- must be generated. We used to try to do this only when it was | |
6212 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
6213 | |
6214 | if Is_Access_Type (T) then | |
2717634d | 6215 | Insert_Explicit_Dereference (P); |
70482933 | 6216 | Analyze_And_Resolve (P, Designated_Type (T)); |
5972791c AC |
6217 | Atp := Designated_Type (T); |
6218 | else | |
6219 | Atp := T; | |
70482933 RK |
6220 | end if; |
6221 | ||
fbf5a39b AC |
6222 | -- Generate index and validity checks |
6223 | ||
6224 | Generate_Index_Checks (N); | |
6225 | ||
70482933 RK |
6226 | if Validity_Checks_On and then Validity_Check_Subscripts then |
6227 | Apply_Subscript_Validity_Checks (N); | |
6228 | end if; | |
6229 | ||
5972791c AC |
6230 | -- If selecting from an array with atomic components, and atomic sync |
6231 | -- is not suppressed for this array type, set atomic sync flag. | |
6232 | ||
6233 | if (Has_Atomic_Components (Atp) | |
6234 | and then not Atomic_Synchronization_Disabled (Atp)) | |
6235 | or else (Is_Atomic (Typ) | |
6236 | and then not Atomic_Synchronization_Disabled (Typ)) | |
6237 | then | |
4c318253 | 6238 | Activate_Atomic_Synchronization (N); |
5972791c AC |
6239 | end if; |
6240 | ||
70482933 RK |
6241 | -- All done for the non-packed case |
6242 | ||
6243 | if not Is_Packed (Etype (Prefix (N))) then | |
6244 | return; | |
6245 | end if; | |
6246 | ||
6247 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 6248 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
6249 | -- we can always use the normal packed element get circuit. |
6250 | ||
6251 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
6252 | Expand_Packed_Element_Reference (N); | |
6253 | return; | |
6254 | end if; | |
6255 | ||
8ca597af RD |
6256 | -- For a reference to a component of a bit packed array, we convert it |
6257 | -- to a reference to the corresponding Packed_Array_Impl_Type. We only | |
6258 | -- want to do this for simple references, and not for: | |
70482933 | 6259 | |
685094bf RD |
6260 | -- Left side of assignment, or prefix of left side of assignment, or |
6261 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
6262 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
6263 | ||
6264 | -- Renaming objects in renaming associations | |
6265 | -- This case is handled when a use of the renamed variable occurs | |
6266 | ||
6267 | -- Actual parameters for a procedure call | |
6268 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
6269 | ||
6270 | -- The second expression in a 'Read attribute reference | |
6271 | ||
47d3b920 | 6272 | -- The prefix of an address or bit or size attribute reference |
70482933 RK |
6273 | |
6274 | -- The following circuit detects these exceptions | |
6275 | ||
6276 | declare | |
6277 | Child : Node_Id := N; | |
6278 | Parnt : Node_Id := Parent (N); | |
6279 | ||
6280 | begin | |
6281 | loop | |
6282 | if Nkind (Parnt) = N_Unchecked_Expression then | |
6283 | null; | |
6284 | ||
303b4d58 AC |
6285 | elsif Nkind_In (Parnt, N_Object_Renaming_Declaration, |
6286 | N_Procedure_Call_Statement) | |
70482933 RK |
6287 | or else (Nkind (Parnt) = N_Parameter_Association |
6288 | and then | |
6289 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
6290 | then | |
6291 | return; | |
6292 | ||
6293 | elsif Nkind (Parnt) = N_Attribute_Reference | |
b69cd36a AC |
6294 | and then Nam_In (Attribute_Name (Parnt), Name_Address, |
6295 | Name_Bit, | |
6296 | Name_Size) | |
70482933 RK |
6297 | and then Prefix (Parnt) = Child |
6298 | then | |
6299 | return; | |
6300 | ||
6301 | elsif Nkind (Parnt) = N_Assignment_Statement | |
6302 | and then Name (Parnt) = Child | |
6303 | then | |
6304 | return; | |
6305 | ||
685094bf RD |
6306 | -- If the expression is an index of an indexed component, it must |
6307 | -- be expanded regardless of context. | |
fbf5a39b AC |
6308 | |
6309 | elsif Nkind (Parnt) = N_Indexed_Component | |
6310 | and then Child /= Prefix (Parnt) | |
6311 | then | |
6312 | Expand_Packed_Element_Reference (N); | |
6313 | return; | |
6314 | ||
6315 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
6316 | and then Name (Parent (Parnt)) = Parnt | |
6317 | then | |
6318 | return; | |
6319 | ||
70482933 RK |
6320 | elsif Nkind (Parnt) = N_Attribute_Reference |
6321 | and then Attribute_Name (Parnt) = Name_Read | |
6322 | and then Next (First (Expressions (Parnt))) = Child | |
6323 | then | |
6324 | return; | |
6325 | ||
303b4d58 | 6326 | elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component) |
533369aa | 6327 | and then Prefix (Parnt) = Child |
70482933 RK |
6328 | then |
6329 | null; | |
6330 | ||
6331 | else | |
6332 | Expand_Packed_Element_Reference (N); | |
6333 | return; | |
6334 | end if; | |
6335 | ||
685094bf RD |
6336 | -- Keep looking up tree for unchecked expression, or if we are the |
6337 | -- prefix of a possible assignment left side. | |
70482933 RK |
6338 | |
6339 | Child := Parnt; | |
6340 | Parnt := Parent (Child); | |
6341 | end loop; | |
6342 | end; | |
70482933 RK |
6343 | end Expand_N_Indexed_Component; |
6344 | ||
6345 | --------------------- | |
6346 | -- Expand_N_Not_In -- | |
6347 | --------------------- | |
6348 | ||
6349 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
6350 | -- can be done. This avoids needing to duplicate this expansion code. | |
6351 | ||
6352 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
6353 | Loc : constant Source_Ptr := Sloc (N); |
6354 | Typ : constant Entity_Id := Etype (N); | |
6355 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
6356 | |
6357 | begin | |
6358 | Rewrite (N, | |
6359 | Make_Op_Not (Loc, | |
6360 | Right_Opnd => | |
6361 | Make_In (Loc, | |
6362 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 6363 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 6364 | |
197e4514 AC |
6365 | -- If this is a set membership, preserve list of alternatives |
6366 | ||
6367 | Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N))); | |
6368 | ||
d766cee3 | 6369 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 6370 | -- transformations in Expand_N_In). |
630d30e9 RD |
6371 | |
6372 | Set_Comes_From_Source (N, Cfs); | |
6373 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
6374 | ||
8fc789c8 | 6375 | -- Now analyze transformed node |
630d30e9 | 6376 | |
70482933 RK |
6377 | Analyze_And_Resolve (N, Typ); |
6378 | end Expand_N_Not_In; | |
6379 | ||
6380 | ------------------- | |
6381 | -- Expand_N_Null -- | |
6382 | ------------------- | |
6383 | ||
a3f2babd AC |
6384 | -- The only replacement required is for the case of a null of a type that |
6385 | -- is an access to protected subprogram, or a subtype thereof. We represent | |
6386 | -- such access values as a record, and so we must replace the occurrence of | |
6387 | -- null by the equivalent record (with a null address and a null pointer in | |
6388 | -- it), so that the backend creates the proper value. | |
70482933 RK |
6389 | |
6390 | procedure Expand_N_Null (N : Node_Id) is | |
6391 | Loc : constant Source_Ptr := Sloc (N); | |
a3f2babd | 6392 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
70482933 RK |
6393 | Agg : Node_Id; |
6394 | ||
6395 | begin | |
26bff3d9 | 6396 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
6397 | Agg := |
6398 | Make_Aggregate (Loc, | |
6399 | Expressions => New_List ( | |
6400 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
6401 | Make_Null (Loc))); | |
6402 | ||
6403 | Rewrite (N, Agg); | |
6404 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
6405 | ||
685094bf RD |
6406 | -- For subsequent semantic analysis, the node must retain its type. |
6407 | -- Gigi in any case replaces this type by the corresponding record | |
6408 | -- type before processing the node. | |
70482933 RK |
6409 | |
6410 | Set_Etype (N, Typ); | |
6411 | end if; | |
fbf5a39b AC |
6412 | |
6413 | exception | |
6414 | when RE_Not_Available => | |
6415 | return; | |
70482933 RK |
6416 | end Expand_N_Null; |
6417 | ||
6418 | --------------------- | |
6419 | -- Expand_N_Op_Abs -- | |
6420 | --------------------- | |
6421 | ||
6422 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
6423 | Loc : constant Source_Ptr := Sloc (N); | |
cc6f5d75 | 6424 | Expr : constant Node_Id := Right_Opnd (N); |
70482933 RK |
6425 | |
6426 | begin | |
6427 | Unary_Op_Validity_Checks (N); | |
6428 | ||
b6b5cca8 AC |
6429 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6430 | ||
6431 | if Minimized_Eliminated_Overflow_Check (N) then | |
6432 | Apply_Arithmetic_Overflow_Check (N); | |
6433 | return; | |
6434 | end if; | |
6435 | ||
70482933 RK |
6436 | -- Deal with software overflow checking |
6437 | ||
07fc65c4 | 6438 | if not Backend_Overflow_Checks_On_Target |
533369aa AC |
6439 | and then Is_Signed_Integer_Type (Etype (N)) |
6440 | and then Do_Overflow_Check (N) | |
70482933 | 6441 | then |
685094bf RD |
6442 | -- The only case to worry about is when the argument is equal to the |
6443 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 6444 | |
fbf5a39b | 6445 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
6446 | |
6447 | -- with the usual Duplicate_Subexpr use coding for expr | |
6448 | ||
fbf5a39b AC |
6449 | Insert_Action (N, |
6450 | Make_Raise_Constraint_Error (Loc, | |
6451 | Condition => | |
6452 | Make_Op_Eq (Loc, | |
70482933 | 6453 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
6454 | Right_Opnd => |
6455 | Make_Attribute_Reference (Loc, | |
cc6f5d75 | 6456 | Prefix => |
fbf5a39b AC |
6457 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), |
6458 | Attribute_Name => Name_First)), | |
6459 | Reason => CE_Overflow_Check_Failed)); | |
6460 | end if; | |
70482933 RK |
6461 | end Expand_N_Op_Abs; |
6462 | ||
6463 | --------------------- | |
6464 | -- Expand_N_Op_Add -- | |
6465 | --------------------- | |
6466 | ||
6467 | procedure Expand_N_Op_Add (N : Node_Id) is | |
6468 | Typ : constant Entity_Id := Etype (N); | |
6469 | ||
6470 | begin | |
6471 | Binary_Op_Validity_Checks (N); | |
6472 | ||
b6b5cca8 AC |
6473 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6474 | ||
6475 | if Minimized_Eliminated_Overflow_Check (N) then | |
6476 | Apply_Arithmetic_Overflow_Check (N); | |
6477 | return; | |
6478 | end if; | |
6479 | ||
70482933 RK |
6480 | -- N + 0 = 0 + N = N for integer types |
6481 | ||
6482 | if Is_Integer_Type (Typ) then | |
6483 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
6484 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
6485 | then | |
6486 | Rewrite (N, Left_Opnd (N)); | |
6487 | return; | |
6488 | ||
6489 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
6490 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
6491 | then | |
6492 | Rewrite (N, Right_Opnd (N)); | |
6493 | return; | |
6494 | end if; | |
6495 | end if; | |
6496 | ||
fbf5a39b | 6497 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 6498 | |
761f7dcb | 6499 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 RK |
6500 | Apply_Arithmetic_Overflow_Check (N); |
6501 | return; | |
70482933 | 6502 | end if; |
dfaff97b RD |
6503 | |
6504 | -- Overflow checks for floating-point if -gnateF mode active | |
6505 | ||
6506 | Check_Float_Op_Overflow (N); | |
70482933 RK |
6507 | end Expand_N_Op_Add; |
6508 | ||
6509 | --------------------- | |
6510 | -- Expand_N_Op_And -- | |
6511 | --------------------- | |
6512 | ||
6513 | procedure Expand_N_Op_And (N : Node_Id) is | |
6514 | Typ : constant Entity_Id := Etype (N); | |
6515 | ||
6516 | begin | |
6517 | Binary_Op_Validity_Checks (N); | |
6518 | ||
6519 | if Is_Array_Type (Etype (N)) then | |
6520 | Expand_Boolean_Operator (N); | |
6521 | ||
6522 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
6523 | Adjust_Condition (Left_Opnd (N)); |
6524 | Adjust_Condition (Right_Opnd (N)); | |
6525 | Set_Etype (N, Standard_Boolean); | |
6526 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
6527 | |
6528 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
6529 | Expand_Intrinsic_Call (N, Entity (N)); | |
6530 | ||
70482933 RK |
6531 | end if; |
6532 | end Expand_N_Op_And; | |
6533 | ||
6534 | ------------------------ | |
6535 | -- Expand_N_Op_Concat -- | |
6536 | ------------------------ | |
6537 | ||
6538 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
6539 | Opnds : List_Id; |
6540 | -- List of operands to be concatenated | |
6541 | ||
70482933 | 6542 | Cnode : Node_Id; |
685094bf RD |
6543 | -- Node which is to be replaced by the result of concatenating the nodes |
6544 | -- in the list Opnds. | |
70482933 | 6545 | |
70482933 | 6546 | begin |
fbf5a39b AC |
6547 | -- Ensure validity of both operands |
6548 | ||
70482933 RK |
6549 | Binary_Op_Validity_Checks (N); |
6550 | ||
685094bf RD |
6551 | -- If we are the left operand of a concatenation higher up the tree, |
6552 | -- then do nothing for now, since we want to deal with a series of | |
6553 | -- concatenations as a unit. | |
70482933 RK |
6554 | |
6555 | if Nkind (Parent (N)) = N_Op_Concat | |
6556 | and then N = Left_Opnd (Parent (N)) | |
6557 | then | |
6558 | return; | |
6559 | end if; | |
6560 | ||
6561 | -- We get here with a concatenation whose left operand may be a | |
6562 | -- concatenation itself with a consistent type. We need to process | |
6563 | -- these concatenation operands from left to right, which means | |
6564 | -- from the deepest node in the tree to the highest node. | |
6565 | ||
6566 | Cnode := N; | |
6567 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
6568 | Cnode := Left_Opnd (Cnode); | |
6569 | end loop; | |
6570 | ||
64425dff BD |
6571 | -- Now Cnode is the deepest concatenation, and its parents are the |
6572 | -- concatenation nodes above, so now we process bottom up, doing the | |
64425dff | 6573 | -- operands. |
70482933 | 6574 | |
df46b832 AC |
6575 | -- The outer loop runs more than once if more than one concatenation |
6576 | -- type is involved. | |
70482933 RK |
6577 | |
6578 | Outer : loop | |
6579 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
6580 | Set_Parent (Opnds, N); | |
6581 | ||
df46b832 | 6582 | -- The inner loop gathers concatenation operands |
70482933 RK |
6583 | |
6584 | Inner : while Cnode /= N | |
70482933 RK |
6585 | and then Base_Type (Etype (Cnode)) = |
6586 | Base_Type (Etype (Parent (Cnode))) | |
6587 | loop | |
6588 | Cnode := Parent (Cnode); | |
6589 | Append (Right_Opnd (Cnode), Opnds); | |
6590 | end loop Inner; | |
6591 | ||
43c58950 AC |
6592 | -- Note: The following code is a temporary workaround for N731-034 |
6593 | -- and N829-028 and will be kept until the general issue of internal | |
6594 | -- symbol serialization is addressed. The workaround is kept under a | |
6595 | -- debug switch to avoid permiating into the general case. | |
6596 | ||
6597 | -- Wrap the node to concatenate into an expression actions node to | |
6598 | -- keep it nicely packaged. This is useful in the case of an assert | |
6599 | -- pragma with a concatenation where we want to be able to delete | |
6600 | -- the concatenation and all its expansion stuff. | |
6601 | ||
6602 | if Debug_Flag_Dot_H then | |
6603 | declare | |
6604 | Cnod : constant Node_Id := Relocate_Node (Cnode); | |
6605 | Typ : constant Entity_Id := Base_Type (Etype (Cnode)); | |
6606 | ||
6607 | begin | |
6608 | -- Note: use Rewrite rather than Replace here, so that for | |
6609 | -- example Why_Not_Static can find the original concatenation | |
6610 | -- node OK! | |
6611 | ||
6612 | Rewrite (Cnode, | |
6613 | Make_Expression_With_Actions (Sloc (Cnode), | |
6614 | Actions => New_List (Make_Null_Statement (Sloc (Cnode))), | |
6615 | Expression => Cnod)); | |
6616 | ||
6617 | Expand_Concatenate (Cnod, Opnds); | |
6618 | Analyze_And_Resolve (Cnode, Typ); | |
6619 | end; | |
6620 | ||
6621 | -- Default case | |
6622 | ||
6623 | else | |
6624 | Expand_Concatenate (Cnode, Opnds); | |
6625 | end if; | |
70482933 RK |
6626 | |
6627 | exit Outer when Cnode = N; | |
6628 | Cnode := Parent (Cnode); | |
6629 | end loop Outer; | |
6630 | end Expand_N_Op_Concat; | |
6631 | ||
6632 | ------------------------ | |
6633 | -- Expand_N_Op_Divide -- | |
6634 | ------------------------ | |
6635 | ||
6636 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
6637 | Loc : constant Source_Ptr := Sloc (N); |
6638 | Lopnd : constant Node_Id := Left_Opnd (N); | |
6639 | Ropnd : constant Node_Id := Right_Opnd (N); | |
6640 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
6641 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
6642 | Typ : Entity_Id := Etype (N); | |
6643 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
6644 | and then | |
6645 | Compile_Time_Known_Value (Ropnd); | |
6646 | Rval : Uint; | |
70482933 RK |
6647 | |
6648 | begin | |
6649 | Binary_Op_Validity_Checks (N); | |
6650 | ||
b6b5cca8 AC |
6651 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6652 | ||
6653 | if Minimized_Eliminated_Overflow_Check (N) then | |
6654 | Apply_Arithmetic_Overflow_Check (N); | |
6655 | return; | |
6656 | end if; | |
6657 | ||
6658 | -- Otherwise proceed with expansion of division | |
6659 | ||
f82944b7 JM |
6660 | if Rknow then |
6661 | Rval := Expr_Value (Ropnd); | |
6662 | end if; | |
6663 | ||
70482933 RK |
6664 | -- N / 1 = N for integer types |
6665 | ||
f82944b7 JM |
6666 | if Rknow and then Rval = Uint_1 then |
6667 | Rewrite (N, Lopnd); | |
70482933 RK |
6668 | return; |
6669 | end if; | |
6670 | ||
6671 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
6672 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
6673 | -- operand is an unsigned integer, as required for this to work. | |
6674 | ||
f82944b7 JM |
6675 | if Nkind (Ropnd) = N_Op_Expon |
6676 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
6677 | |
6678 | -- We cannot do this transformation in configurable run time mode if we | |
51bf9bdf | 6679 | -- have 64-bit integers and long shifts are not available. |
fbf5a39b | 6680 | |
761f7dcb | 6681 | and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target) |
70482933 RK |
6682 | then |
6683 | Rewrite (N, | |
6684 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 6685 | Left_Opnd => Lopnd, |
70482933 | 6686 | Right_Opnd => |
f82944b7 | 6687 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
6688 | Analyze_And_Resolve (N, Typ); |
6689 | return; | |
6690 | end if; | |
6691 | ||
6692 | -- Do required fixup of universal fixed operation | |
6693 | ||
6694 | if Typ = Universal_Fixed then | |
6695 | Fixup_Universal_Fixed_Operation (N); | |
6696 | Typ := Etype (N); | |
6697 | end if; | |
6698 | ||
6699 | -- Divisions with fixed-point results | |
6700 | ||
6701 | if Is_Fixed_Point_Type (Typ) then | |
6702 | ||
685094bf RD |
6703 | -- No special processing if Treat_Fixed_As_Integer is set, since |
6704 | -- from a semantic point of view such operations are simply integer | |
6705 | -- operations and will be treated that way. | |
70482933 RK |
6706 | |
6707 | if not Treat_Fixed_As_Integer (N) then | |
6708 | if Is_Integer_Type (Rtyp) then | |
6709 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
6710 | else | |
6711 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
6712 | end if; | |
6713 | end if; | |
6714 | ||
685094bf RD |
6715 | -- Other cases of division of fixed-point operands. Again we exclude the |
6716 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 | 6717 | |
761f7dcb | 6718 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) |
70482933 RK |
6719 | and then not Treat_Fixed_As_Integer (N) |
6720 | then | |
6721 | if Is_Integer_Type (Typ) then | |
6722 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
6723 | else | |
6724 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
6725 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
6726 | end if; | |
6727 | ||
685094bf RD |
6728 | -- Mixed-mode operations can appear in a non-static universal context, |
6729 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 6730 | |
533369aa | 6731 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
f82944b7 JM |
6732 | Rewrite (Ropnd, |
6733 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 6734 | |
f82944b7 | 6735 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 | 6736 | |
533369aa | 6737 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
f82944b7 JM |
6738 | Rewrite (Lopnd, |
6739 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 6740 | |
f82944b7 | 6741 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 6742 | |
f02b8bb8 | 6743 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
6744 | |
6745 | elsif Is_Integer_Type (Typ) then | |
a91e9ac7 | 6746 | Apply_Divide_Checks (N); |
70482933 | 6747 | end if; |
dfaff97b RD |
6748 | |
6749 | -- Overflow checks for floating-point if -gnateF mode active | |
6750 | ||
6751 | Check_Float_Op_Overflow (N); | |
70482933 RK |
6752 | end Expand_N_Op_Divide; |
6753 | ||
6754 | -------------------- | |
6755 | -- Expand_N_Op_Eq -- | |
6756 | -------------------- | |
6757 | ||
6758 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
6759 | Loc : constant Source_Ptr := Sloc (N); |
6760 | Typ : constant Entity_Id := Etype (N); | |
6761 | Lhs : constant Node_Id := Left_Opnd (N); | |
6762 | Rhs : constant Node_Id := Right_Opnd (N); | |
6763 | Bodies : constant List_Id := New_List; | |
6764 | A_Typ : constant Entity_Id := Etype (Lhs); | |
6765 | ||
70482933 RK |
6766 | Typl : Entity_Id := A_Typ; |
6767 | Op_Name : Entity_Id; | |
6768 | Prim : Elmt_Id; | |
70482933 RK |
6769 | |
6770 | procedure Build_Equality_Call (Eq : Entity_Id); | |
6771 | -- If a constructed equality exists for the type or for its parent, | |
6772 | -- build and analyze call, adding conversions if the operation is | |
6773 | -- inherited. | |
6774 | ||
5d09245e | 6775 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
8fc789c8 | 6776 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
6777 | -- Unchecked_Union subtype. Typ is a record type. |
6778 | ||
70482933 RK |
6779 | ------------------------- |
6780 | -- Build_Equality_Call -- | |
6781 | ------------------------- | |
6782 | ||
6783 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
6784 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
cc6f5d75 AC |
6785 | L_Exp : Node_Id := Relocate_Node (Lhs); |
6786 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
70482933 RK |
6787 | |
6788 | begin | |
dda38714 AC |
6789 | -- Adjust operands if necessary to comparison type |
6790 | ||
70482933 RK |
6791 | if Base_Type (Op_Type) /= Base_Type (A_Typ) |
6792 | and then not Is_Class_Wide_Type (A_Typ) | |
6793 | then | |
6794 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
6795 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
6796 | end if; | |
6797 | ||
5d09245e AC |
6798 | -- If we have an Unchecked_Union, we need to add the inferred |
6799 | -- discriminant values as actuals in the function call. At this | |
6800 | -- point, the expansion has determined that both operands have | |
6801 | -- inferable discriminants. | |
6802 | ||
6803 | if Is_Unchecked_Union (Op_Type) then | |
6804 | declare | |
fa1608c2 ES |
6805 | Lhs_Type : constant Node_Id := Etype (L_Exp); |
6806 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
6807 | ||
6808 | Lhs_Discr_Vals : Elist_Id; | |
6809 | -- List of inferred discriminant values for left operand. | |
6810 | ||
6811 | Rhs_Discr_Vals : Elist_Id; | |
6812 | -- List of inferred discriminant values for right operand. | |
6813 | ||
6814 | Discr : Entity_Id; | |
5d09245e AC |
6815 | |
6816 | begin | |
fa1608c2 ES |
6817 | Lhs_Discr_Vals := New_Elmt_List; |
6818 | Rhs_Discr_Vals := New_Elmt_List; | |
6819 | ||
5d09245e AC |
6820 | -- Per-object constrained selected components require special |
6821 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 6822 | -- Unchecked_Union, we cannot reference its discriminants |
fa1608c2 ES |
6823 | -- directly. This is why we use the extra parameters of the |
6824 | -- equality function of the enclosing Unchecked_Union. | |
5d09245e AC |
6825 | |
6826 | -- type UU_Type (Discr : Integer := 0) is | |
6827 | -- . . . | |
6828 | -- end record; | |
6829 | -- pragma Unchecked_Union (UU_Type); | |
6830 | ||
6831 | -- 1. Unchecked_Union enclosing record: | |
6832 | ||
6833 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
6834 | -- . . . | |
6835 | -- Comp : UU_Type (Discr); | |
6836 | -- . . . | |
6837 | -- end Enclosing_UU_Type; | |
6838 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
6839 | ||
6840 | -- Obj1 : Enclosing_UU_Type; | |
6841 | -- Obj2 : Enclosing_UU_Type (1); | |
6842 | ||
2717634d | 6843 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
6844 | |
6845 | -- Generated code: | |
6846 | ||
6847 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
6848 | ||
6849 | -- A and B are the formal parameters of the equality function | |
6850 | -- of Enclosing_UU_Type. The function always has two extra | |
fa1608c2 ES |
6851 | -- formals to capture the inferred discriminant values for |
6852 | -- each discriminant of the type. | |
5d09245e AC |
6853 | |
6854 | -- 2. Non-Unchecked_Union enclosing record: | |
6855 | ||
6856 | -- type | |
6857 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
6858 | -- is record | |
6859 | -- . . . | |
6860 | -- Comp : UU_Type (Discr); | |
6861 | -- . . . | |
6862 | -- end Enclosing_Non_UU_Type; | |
6863 | ||
6864 | -- Obj1 : Enclosing_Non_UU_Type; | |
6865 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
6866 | ||
630d30e9 | 6867 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
6868 | |
6869 | -- Generated code: | |
6870 | ||
6871 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
6872 | -- obj1.discr, obj2.discr)) then | |
6873 | ||
6874 | -- In this case we can directly reference the discriminants of | |
6875 | -- the enclosing record. | |
6876 | ||
fa1608c2 | 6877 | -- Process left operand of equality |
5d09245e AC |
6878 | |
6879 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
6880 | and then |
6881 | Has_Per_Object_Constraint (Entity (Selector_Name (Lhs))) | |
5d09245e | 6882 | then |
fa1608c2 ES |
6883 | -- If enclosing record is an Unchecked_Union, use formals |
6884 | -- corresponding to each discriminant. The name of the | |
6885 | -- formal is that of the discriminant, with added suffix, | |
6886 | -- see Exp_Ch3.Build_Record_Equality for details. | |
5d09245e | 6887 | |
dda38714 | 6888 | if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs)))) |
5d09245e | 6889 | then |
fa1608c2 ES |
6890 | Discr := |
6891 | First_Discriminant | |
6892 | (Scope (Entity (Selector_Name (Lhs)))); | |
6893 | while Present (Discr) loop | |
cc6f5d75 AC |
6894 | Append_Elmt |
6895 | (Make_Identifier (Loc, | |
6896 | Chars => New_External_Name (Chars (Discr), 'A')), | |
6897 | To => Lhs_Discr_Vals); | |
fa1608c2 ES |
6898 | Next_Discriminant (Discr); |
6899 | end loop; | |
5d09245e | 6900 | |
fa1608c2 ES |
6901 | -- If enclosing record is of a non-Unchecked_Union type, it |
6902 | -- is possible to reference its discriminants directly. | |
5d09245e AC |
6903 | |
6904 | else | |
fa1608c2 ES |
6905 | Discr := First_Discriminant (Lhs_Type); |
6906 | while Present (Discr) loop | |
cc6f5d75 AC |
6907 | Append_Elmt |
6908 | (Make_Selected_Component (Loc, | |
6909 | Prefix => Prefix (Lhs), | |
6910 | Selector_Name => | |
6911 | New_Copy | |
6912 | (Get_Discriminant_Value (Discr, | |
6913 | Lhs_Type, | |
6914 | Stored_Constraint (Lhs_Type)))), | |
6915 | To => Lhs_Discr_Vals); | |
fa1608c2 ES |
6916 | Next_Discriminant (Discr); |
6917 | end loop; | |
5d09245e AC |
6918 | end if; |
6919 | ||
fa1608c2 ES |
6920 | -- Otherwise operand is on object with a constrained type. |
6921 | -- Infer the discriminant values from the constraint. | |
5d09245e AC |
6922 | |
6923 | else | |
fa1608c2 ES |
6924 | |
6925 | Discr := First_Discriminant (Lhs_Type); | |
6926 | while Present (Discr) loop | |
cc6f5d75 AC |
6927 | Append_Elmt |
6928 | (New_Copy | |
6929 | (Get_Discriminant_Value (Discr, | |
fa1608c2 ES |
6930 | Lhs_Type, |
6931 | Stored_Constraint (Lhs_Type))), | |
cc6f5d75 | 6932 | To => Lhs_Discr_Vals); |
fa1608c2 ES |
6933 | Next_Discriminant (Discr); |
6934 | end loop; | |
5d09245e AC |
6935 | end if; |
6936 | ||
fa1608c2 | 6937 | -- Similar processing for right operand of equality |
5d09245e AC |
6938 | |
6939 | if Nkind (Rhs) = N_Selected_Component | |
533369aa AC |
6940 | and then |
6941 | Has_Per_Object_Constraint (Entity (Selector_Name (Rhs))) | |
5d09245e | 6942 | then |
5e1c00fa | 6943 | if Is_Unchecked_Union |
cc6f5d75 | 6944 | (Scope (Entity (Selector_Name (Rhs)))) |
5d09245e | 6945 | then |
fa1608c2 ES |
6946 | Discr := |
6947 | First_Discriminant | |
6948 | (Scope (Entity (Selector_Name (Rhs)))); | |
6949 | while Present (Discr) loop | |
cc6f5d75 AC |
6950 | Append_Elmt |
6951 | (Make_Identifier (Loc, | |
6952 | Chars => New_External_Name (Chars (Discr), 'B')), | |
6953 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
6954 | Next_Discriminant (Discr); |
6955 | end loop; | |
5d09245e AC |
6956 | |
6957 | else | |
fa1608c2 ES |
6958 | Discr := First_Discriminant (Rhs_Type); |
6959 | while Present (Discr) loop | |
cc6f5d75 AC |
6960 | Append_Elmt |
6961 | (Make_Selected_Component (Loc, | |
6962 | Prefix => Prefix (Rhs), | |
6963 | Selector_Name => | |
6964 | New_Copy (Get_Discriminant_Value | |
6965 | (Discr, | |
6966 | Rhs_Type, | |
6967 | Stored_Constraint (Rhs_Type)))), | |
6968 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
6969 | Next_Discriminant (Discr); |
6970 | end loop; | |
5d09245e | 6971 | end if; |
5d09245e | 6972 | |
fa1608c2 ES |
6973 | else |
6974 | Discr := First_Discriminant (Rhs_Type); | |
6975 | while Present (Discr) loop | |
cc6f5d75 AC |
6976 | Append_Elmt |
6977 | (New_Copy (Get_Discriminant_Value | |
6978 | (Discr, | |
6979 | Rhs_Type, | |
6980 | Stored_Constraint (Rhs_Type))), | |
6981 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
6982 | Next_Discriminant (Discr); |
6983 | end loop; | |
5d09245e AC |
6984 | end if; |
6985 | ||
fa1608c2 ES |
6986 | -- Now merge the list of discriminant values so that values |
6987 | -- of corresponding discriminants are adjacent. | |
6988 | ||
6989 | declare | |
6990 | Params : List_Id; | |
6991 | L_Elmt : Elmt_Id; | |
6992 | R_Elmt : Elmt_Id; | |
6993 | ||
6994 | begin | |
6995 | Params := New_List (L_Exp, R_Exp); | |
6996 | L_Elmt := First_Elmt (Lhs_Discr_Vals); | |
6997 | R_Elmt := First_Elmt (Rhs_Discr_Vals); | |
6998 | while Present (L_Elmt) loop | |
6999 | Append_To (Params, Node (L_Elmt)); | |
7000 | Append_To (Params, Node (R_Elmt)); | |
7001 | Next_Elmt (L_Elmt); | |
7002 | Next_Elmt (R_Elmt); | |
7003 | end loop; | |
7004 | ||
7005 | Rewrite (N, | |
7006 | Make_Function_Call (Loc, | |
e4494292 | 7007 | Name => New_Occurrence_Of (Eq, Loc), |
fa1608c2 ES |
7008 | Parameter_Associations => Params)); |
7009 | end; | |
5d09245e AC |
7010 | end; |
7011 | ||
7012 | -- Normal case, not an unchecked union | |
7013 | ||
7014 | else | |
7015 | Rewrite (N, | |
7016 | Make_Function_Call (Loc, | |
e4494292 | 7017 | Name => New_Occurrence_Of (Eq, Loc), |
5d09245e AC |
7018 | Parameter_Associations => New_List (L_Exp, R_Exp))); |
7019 | end if; | |
70482933 RK |
7020 | |
7021 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7022 | end Build_Equality_Call; | |
7023 | ||
5d09245e AC |
7024 | ------------------------------------ |
7025 | -- Has_Unconstrained_UU_Component -- | |
7026 | ------------------------------------ | |
7027 | ||
7028 | function Has_Unconstrained_UU_Component | |
7029 | (Typ : Node_Id) return Boolean | |
7030 | is | |
7031 | Tdef : constant Node_Id := | |
57848bf7 | 7032 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
7033 | Clist : Node_Id; |
7034 | Vpart : Node_Id; | |
7035 | ||
7036 | function Component_Is_Unconstrained_UU | |
7037 | (Comp : Node_Id) return Boolean; | |
7038 | -- Determines whether the subtype of the component is an | |
7039 | -- unconstrained Unchecked_Union. | |
7040 | ||
7041 | function Variant_Is_Unconstrained_UU | |
7042 | (Variant : Node_Id) return Boolean; | |
7043 | -- Determines whether a component of the variant has an unconstrained | |
7044 | -- Unchecked_Union subtype. | |
7045 | ||
7046 | ----------------------------------- | |
7047 | -- Component_Is_Unconstrained_UU -- | |
7048 | ----------------------------------- | |
7049 | ||
7050 | function Component_Is_Unconstrained_UU | |
7051 | (Comp : Node_Id) return Boolean | |
7052 | is | |
7053 | begin | |
7054 | if Nkind (Comp) /= N_Component_Declaration then | |
7055 | return False; | |
7056 | end if; | |
7057 | ||
7058 | declare | |
7059 | Sindic : constant Node_Id := | |
7060 | Subtype_Indication (Component_Definition (Comp)); | |
7061 | ||
7062 | begin | |
7063 | -- Unconstrained nominal type. In the case of a constraint | |
7064 | -- present, the node kind would have been N_Subtype_Indication. | |
7065 | ||
7066 | if Nkind (Sindic) = N_Identifier then | |
7067 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
7068 | end if; | |
7069 | ||
7070 | return False; | |
7071 | end; | |
7072 | end Component_Is_Unconstrained_UU; | |
7073 | ||
7074 | --------------------------------- | |
7075 | -- Variant_Is_Unconstrained_UU -- | |
7076 | --------------------------------- | |
7077 | ||
7078 | function Variant_Is_Unconstrained_UU | |
7079 | (Variant : Node_Id) return Boolean | |
7080 | is | |
7081 | Clist : constant Node_Id := Component_List (Variant); | |
7082 | ||
7083 | begin | |
7084 | if Is_Empty_List (Component_Items (Clist)) then | |
7085 | return False; | |
7086 | end if; | |
7087 | ||
f02b8bb8 RD |
7088 | -- We only need to test one component |
7089 | ||
5d09245e AC |
7090 | declare |
7091 | Comp : Node_Id := First (Component_Items (Clist)); | |
7092 | ||
7093 | begin | |
7094 | while Present (Comp) loop | |
5d09245e AC |
7095 | if Component_Is_Unconstrained_UU (Comp) then |
7096 | return True; | |
7097 | end if; | |
7098 | ||
7099 | Next (Comp); | |
7100 | end loop; | |
7101 | end; | |
7102 | ||
7103 | -- None of the components withing the variant were of | |
7104 | -- unconstrained Unchecked_Union type. | |
7105 | ||
7106 | return False; | |
7107 | end Variant_Is_Unconstrained_UU; | |
7108 | ||
7109 | -- Start of processing for Has_Unconstrained_UU_Component | |
7110 | ||
7111 | begin | |
7112 | if Null_Present (Tdef) then | |
7113 | return False; | |
7114 | end if; | |
7115 | ||
7116 | Clist := Component_List (Tdef); | |
7117 | Vpart := Variant_Part (Clist); | |
7118 | ||
7119 | -- Inspect available components | |
7120 | ||
7121 | if Present (Component_Items (Clist)) then | |
7122 | declare | |
7123 | Comp : Node_Id := First (Component_Items (Clist)); | |
7124 | ||
7125 | begin | |
7126 | while Present (Comp) loop | |
7127 | ||
8fc789c8 | 7128 | -- One component is sufficient |
5d09245e AC |
7129 | |
7130 | if Component_Is_Unconstrained_UU (Comp) then | |
7131 | return True; | |
7132 | end if; | |
7133 | ||
7134 | Next (Comp); | |
7135 | end loop; | |
7136 | end; | |
7137 | end if; | |
7138 | ||
7139 | -- Inspect available components withing variants | |
7140 | ||
7141 | if Present (Vpart) then | |
7142 | declare | |
7143 | Variant : Node_Id := First (Variants (Vpart)); | |
7144 | ||
7145 | begin | |
7146 | while Present (Variant) loop | |
7147 | ||
8fc789c8 | 7148 | -- One component within a variant is sufficient |
5d09245e AC |
7149 | |
7150 | if Variant_Is_Unconstrained_UU (Variant) then | |
7151 | return True; | |
7152 | end if; | |
7153 | ||
7154 | Next (Variant); | |
7155 | end loop; | |
7156 | end; | |
7157 | end if; | |
7158 | ||
7159 | -- Neither the available components, nor the components inside the | |
7160 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
7161 | ||
7162 | return False; | |
7163 | end Has_Unconstrained_UU_Component; | |
7164 | ||
70482933 RK |
7165 | -- Start of processing for Expand_N_Op_Eq |
7166 | ||
7167 | begin | |
7168 | Binary_Op_Validity_Checks (N); | |
7169 | ||
456cbfa5 AC |
7170 | -- Deal with private types |
7171 | ||
70482933 RK |
7172 | if Ekind (Typl) = E_Private_Type then |
7173 | Typl := Underlying_Type (Typl); | |
70482933 RK |
7174 | elsif Ekind (Typl) = E_Private_Subtype then |
7175 | Typl := Underlying_Type (Base_Type (Typl)); | |
f02b8bb8 RD |
7176 | else |
7177 | null; | |
70482933 RK |
7178 | end if; |
7179 | ||
7180 | -- It may happen in error situations that the underlying type is not | |
7181 | -- set. The error will be detected later, here we just defend the | |
7182 | -- expander code. | |
7183 | ||
7184 | if No (Typl) then | |
7185 | return; | |
7186 | end if; | |
7187 | ||
a92230c5 AC |
7188 | -- Now get the implementation base type (note that plain Base_Type here |
7189 | -- might lead us back to the private type, which is not what we want!) | |
7190 | ||
7191 | Typl := Implementation_Base_Type (Typl); | |
70482933 | 7192 | |
dda38714 AC |
7193 | -- Equality between variant records results in a call to a routine |
7194 | -- that has conditional tests of the discriminant value(s), and hence | |
7195 | -- violates the No_Implicit_Conditionals restriction. | |
7196 | ||
7197 | if Has_Variant_Part (Typl) then | |
7198 | declare | |
7199 | Msg : Boolean; | |
7200 | ||
7201 | begin | |
7202 | Check_Restriction (Msg, No_Implicit_Conditionals, N); | |
7203 | ||
7204 | if Msg then | |
7205 | Error_Msg_N | |
7206 | ("\comparison of variant records tests discriminants", N); | |
7207 | return; | |
7208 | end if; | |
7209 | end; | |
7210 | end if; | |
7211 | ||
456cbfa5 | 7212 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7213 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7214 | |
7215 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7216 | ||
7217 | if Nkind (N) /= N_Op_Eq then | |
7218 | return; | |
7219 | end if; | |
7220 | ||
70482933 RK |
7221 | -- Boolean types (requiring handling of non-standard case) |
7222 | ||
f02b8bb8 | 7223 | if Is_Boolean_Type (Typl) then |
70482933 RK |
7224 | Adjust_Condition (Left_Opnd (N)); |
7225 | Adjust_Condition (Right_Opnd (N)); | |
7226 | Set_Etype (N, Standard_Boolean); | |
7227 | Adjust_Result_Type (N, Typ); | |
7228 | ||
7229 | -- Array types | |
7230 | ||
7231 | elsif Is_Array_Type (Typl) then | |
7232 | ||
1033834f RD |
7233 | -- If we are doing full validity checking, and it is possible for the |
7234 | -- array elements to be invalid then expand out array comparisons to | |
7235 | -- make sure that we check the array elements. | |
fbf5a39b | 7236 | |
1033834f RD |
7237 | if Validity_Check_Operands |
7238 | and then not Is_Known_Valid (Component_Type (Typl)) | |
7239 | then | |
fbf5a39b AC |
7240 | declare |
7241 | Save_Force_Validity_Checks : constant Boolean := | |
7242 | Force_Validity_Checks; | |
7243 | begin | |
7244 | Force_Validity_Checks := True; | |
7245 | Rewrite (N, | |
0da2c8ac AC |
7246 | Expand_Array_Equality |
7247 | (N, | |
7248 | Relocate_Node (Lhs), | |
7249 | Relocate_Node (Rhs), | |
7250 | Bodies, | |
7251 | Typl)); | |
7252 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
7253 | Analyze_And_Resolve (N, Standard_Boolean); |
7254 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
7255 | end; | |
7256 | ||
a9d8907c | 7257 | -- Packed case where both operands are known aligned |
70482933 | 7258 | |
a9d8907c JM |
7259 | elsif Is_Bit_Packed_Array (Typl) |
7260 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
7261 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
7262 | then | |
70482933 RK |
7263 | Expand_Packed_Eq (N); |
7264 | ||
5e1c00fa RD |
7265 | -- Where the component type is elementary we can use a block bit |
7266 | -- comparison (if supported on the target) exception in the case | |
7267 | -- of floating-point (negative zero issues require element by | |
7268 | -- element comparison), and atomic types (where we must be sure | |
a9d8907c | 7269 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 7270 | |
70482933 RK |
7271 | elsif Is_Elementary_Type (Component_Type (Typl)) |
7272 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 7273 | and then not Is_Atomic (Component_Type (Typl)) |
a9d8907c JM |
7274 | and then not Is_Possibly_Unaligned_Object (Lhs) |
7275 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
fbf5a39b | 7276 | and then Support_Composite_Compare_On_Target |
70482933 RK |
7277 | then |
7278 | null; | |
7279 | ||
685094bf RD |
7280 | -- For composite and floating-point cases, expand equality loop to |
7281 | -- make sure of using proper comparisons for tagged types, and | |
7282 | -- correctly handling the floating-point case. | |
70482933 RK |
7283 | |
7284 | else | |
7285 | Rewrite (N, | |
0da2c8ac AC |
7286 | Expand_Array_Equality |
7287 | (N, | |
7288 | Relocate_Node (Lhs), | |
7289 | Relocate_Node (Rhs), | |
7290 | Bodies, | |
7291 | Typl)); | |
70482933 RK |
7292 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
7293 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7294 | end if; | |
7295 | ||
7296 | -- Record Types | |
7297 | ||
7298 | elsif Is_Record_Type (Typl) then | |
7299 | ||
7300 | -- For tagged types, use the primitive "=" | |
7301 | ||
7302 | if Is_Tagged_Type (Typl) then | |
7303 | ||
0669bebe GB |
7304 | -- No need to do anything else compiling under restriction |
7305 | -- No_Dispatching_Calls. During the semantic analysis we | |
7306 | -- already notified such violation. | |
7307 | ||
7308 | if Restriction_Active (No_Dispatching_Calls) then | |
7309 | return; | |
7310 | end if; | |
7311 | ||
685094bf RD |
7312 | -- If this is derived from an untagged private type completed with |
7313 | -- a tagged type, it does not have a full view, so we use the | |
7314 | -- primitive operations of the private type. This check should no | |
7315 | -- longer be necessary when these types get their full views??? | |
70482933 RK |
7316 | |
7317 | if Is_Private_Type (A_Typ) | |
7318 | and then not Is_Tagged_Type (A_Typ) | |
7319 | and then Is_Derived_Type (A_Typ) | |
7320 | and then No (Full_View (A_Typ)) | |
7321 | then | |
685094bf RD |
7322 | -- Search for equality operation, checking that the operands |
7323 | -- have the same type. Note that we must find a matching entry, | |
a90bd866 | 7324 | -- or something is very wrong. |
2e071734 | 7325 | |
70482933 RK |
7326 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
7327 | ||
2e071734 AC |
7328 | while Present (Prim) loop |
7329 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7330 | and then Etype (First_Formal (Node (Prim))) = | |
7331 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
7332 | and then | |
7333 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
7334 | ||
70482933 | 7335 | Next_Elmt (Prim); |
70482933 RK |
7336 | end loop; |
7337 | ||
2e071734 | 7338 | pragma Assert (Present (Prim)); |
70482933 | 7339 | Op_Name := Node (Prim); |
fbf5a39b AC |
7340 | |
7341 | -- Find the type's predefined equality or an overriding | |
3dddb11e | 7342 | -- user-defined equality. The reason for not simply calling |
fbf5a39b | 7343 | -- Find_Prim_Op here is that there may be a user-defined |
3dddb11e ES |
7344 | -- overloaded equality op that precedes the equality that we |
7345 | -- want, so we have to explicitly search (e.g., there could be | |
7346 | -- an equality with two different parameter types). | |
fbf5a39b | 7347 | |
70482933 | 7348 | else |
fbf5a39b | 7349 | if Is_Class_Wide_Type (Typl) then |
3dddb11e | 7350 | Typl := Find_Specific_Type (Typl); |
fbf5a39b AC |
7351 | end if; |
7352 | ||
7353 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
7354 | while Present (Prim) loop |
7355 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7356 | and then Etype (First_Formal (Node (Prim))) = | |
7357 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
7358 | and then |
7359 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
7360 | |
7361 | Next_Elmt (Prim); | |
fbf5a39b AC |
7362 | end loop; |
7363 | ||
2e071734 | 7364 | pragma Assert (Present (Prim)); |
fbf5a39b | 7365 | Op_Name := Node (Prim); |
70482933 RK |
7366 | end if; |
7367 | ||
7368 | Build_Equality_Call (Op_Name); | |
7369 | ||
5d09245e AC |
7370 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
7371 | -- predefined equality operator for a type which has a subcomponent | |
7372 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
7373 | ||
7374 | elsif Has_Unconstrained_UU_Component (Typl) then | |
7375 | Insert_Action (N, | |
7376 | Make_Raise_Program_Error (Loc, | |
7377 | Reason => PE_Unchecked_Union_Restriction)); | |
7378 | ||
7379 | -- Prevent Gigi from generating incorrect code by rewriting the | |
6cb3037c | 7380 | -- equality as a standard False. (is this documented somewhere???) |
5d09245e AC |
7381 | |
7382 | Rewrite (N, | |
7383 | New_Occurrence_Of (Standard_False, Loc)); | |
7384 | ||
7385 | elsif Is_Unchecked_Union (Typl) then | |
7386 | ||
7387 | -- If we can infer the discriminants of the operands, we make a | |
7388 | -- call to the TSS equality function. | |
7389 | ||
7390 | if Has_Inferable_Discriminants (Lhs) | |
7391 | and then | |
7392 | Has_Inferable_Discriminants (Rhs) | |
7393 | then | |
7394 | Build_Equality_Call | |
7395 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
7396 | ||
7397 | else | |
7398 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
7399 | -- the predefined equality operator for an Unchecked_Union type | |
7400 | -- if either of the operands lack inferable discriminants. | |
7401 | ||
7402 | Insert_Action (N, | |
7403 | Make_Raise_Program_Error (Loc, | |
7404 | Reason => PE_Unchecked_Union_Restriction)); | |
7405 | ||
29ad9ea5 AC |
7406 | -- Emit a warning on source equalities only, otherwise the |
7407 | -- message may appear out of place due to internal use. The | |
7408 | -- warning is unconditional because it is required by the | |
7409 | -- language. | |
7410 | ||
7411 | if Comes_From_Source (N) then | |
7412 | Error_Msg_N | |
facfa165 | 7413 | ("Unchecked_Union discriminants cannot be determined??", |
29ad9ea5 AC |
7414 | N); |
7415 | Error_Msg_N | |
facfa165 | 7416 | ("\Program_Error will be raised for equality operation??", |
29ad9ea5 AC |
7417 | N); |
7418 | end if; | |
7419 | ||
5d09245e | 7420 | -- Prevent Gigi from generating incorrect code by rewriting |
6cb3037c | 7421 | -- the equality as a standard False (documented where???). |
5d09245e AC |
7422 | |
7423 | Rewrite (N, | |
7424 | New_Occurrence_Of (Standard_False, Loc)); | |
5d09245e AC |
7425 | end if; |
7426 | ||
70482933 RK |
7427 | -- If a type support function is present (for complex cases), use it |
7428 | ||
fbf5a39b AC |
7429 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
7430 | Build_Equality_Call | |
7431 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 | 7432 | |
8d80ff64 AC |
7433 | -- When comparing two Bounded_Strings, use the primitive equality of |
7434 | -- the root Super_String type. | |
7435 | ||
7436 | elsif Is_Bounded_String (Typl) then | |
7437 | Prim := | |
7438 | First_Elmt (Collect_Primitive_Operations (Root_Type (Typl))); | |
7439 | ||
7440 | while Present (Prim) loop | |
7441 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7442 | and then Etype (First_Formal (Node (Prim))) = | |
7443 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
7444 | and then Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
7445 | ||
7446 | Next_Elmt (Prim); | |
7447 | end loop; | |
7448 | ||
7449 | -- A Super_String type should always have a primitive equality | |
7450 | ||
7451 | pragma Assert (Present (Prim)); | |
7452 | Build_Equality_Call (Node (Prim)); | |
7453 | ||
70482933 | 7454 | -- Otherwise expand the component by component equality. Note that |
8fc789c8 | 7455 | -- we never use block-bit comparisons for records, because of the |
70482933 RK |
7456 | -- problems with gaps. The backend will often be able to recombine |
7457 | -- the separate comparisons that we generate here. | |
7458 | ||
7459 | else | |
7460 | Remove_Side_Effects (Lhs); | |
7461 | Remove_Side_Effects (Rhs); | |
7462 | Rewrite (N, | |
7463 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
7464 | ||
7465 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
7466 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7467 | end if; | |
7468 | end if; | |
7469 | ||
d26dc4b5 | 7470 | -- Test if result is known at compile time |
70482933 | 7471 | |
d26dc4b5 | 7472 | Rewrite_Comparison (N); |
f02b8bb8 | 7473 | |
0580d807 | 7474 | Optimize_Length_Comparison (N); |
70482933 RK |
7475 | end Expand_N_Op_Eq; |
7476 | ||
7477 | ----------------------- | |
7478 | -- Expand_N_Op_Expon -- | |
7479 | ----------------------- | |
7480 | ||
7481 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
7482 | Loc : constant Source_Ptr := Sloc (N); | |
7483 | Typ : constant Entity_Id := Etype (N); | |
7484 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
7485 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 7486 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
7487 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
7488 | Exptyp : constant Entity_Id := Etype (Exp); | |
7489 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
7490 | Expv : Uint; | |
70482933 RK |
7491 | Temp : Node_Id; |
7492 | Rent : RE_Id; | |
7493 | Ent : Entity_Id; | |
fbf5a39b | 7494 | Etyp : Entity_Id; |
cb42ba5d | 7495 | Xnode : Node_Id; |
70482933 RK |
7496 | |
7497 | begin | |
7498 | Binary_Op_Validity_Checks (N); | |
7499 | ||
5114f3ff | 7500 | -- CodePeer wants to see the unexpanded N_Op_Expon node |
8f66cda7 | 7501 | |
5114f3ff | 7502 | if CodePeer_Mode then |
8f66cda7 AC |
7503 | return; |
7504 | end if; | |
7505 | ||
685094bf RD |
7506 | -- If either operand is of a private type, then we have the use of an |
7507 | -- intrinsic operator, and we get rid of the privateness, by using root | |
7508 | -- types of underlying types for the actual operation. Otherwise the | |
7509 | -- private types will cause trouble if we expand multiplications or | |
7510 | -- shifts etc. We also do this transformation if the result type is | |
7511 | -- different from the base type. | |
07fc65c4 GB |
7512 | |
7513 | if Is_Private_Type (Etype (Base)) | |
8f66cda7 AC |
7514 | or else Is_Private_Type (Typ) |
7515 | or else Is_Private_Type (Exptyp) | |
7516 | or else Rtyp /= Root_Type (Bastyp) | |
07fc65c4 GB |
7517 | then |
7518 | declare | |
7519 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
7520 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
07fc65c4 GB |
7521 | begin |
7522 | Rewrite (N, | |
7523 | Unchecked_Convert_To (Typ, | |
7524 | Make_Op_Expon (Loc, | |
7525 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
7526 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
7527 | Analyze_And_Resolve (N, Typ); | |
7528 | return; | |
7529 | end; | |
7530 | end if; | |
7531 | ||
b6b5cca8 | 7532 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6cb3037c | 7533 | |
b6b5cca8 | 7534 | if Minimized_Eliminated_Overflow_Check (N) then |
6cb3037c AC |
7535 | Apply_Arithmetic_Overflow_Check (N); |
7536 | return; | |
7537 | end if; | |
7538 | ||
cb42ba5d AC |
7539 | -- Test for case of known right argument where we can replace the |
7540 | -- exponentiation by an equivalent expression using multiplication. | |
70482933 | 7541 | |
6c3c671e AC |
7542 | -- Note: use CRT_Safe version of Compile_Time_Known_Value because in |
7543 | -- configurable run-time mode, we may not have the exponentiation | |
7544 | -- routine available, and we don't want the legality of the program | |
7545 | -- to depend on how clever the compiler is in knowing values. | |
7546 | ||
7547 | if CRT_Safe_Compile_Time_Known_Value (Exp) then | |
70482933 RK |
7548 | Expv := Expr_Value (Exp); |
7549 | ||
7550 | -- We only fold small non-negative exponents. You might think we | |
7551 | -- could fold small negative exponents for the real case, but we | |
7552 | -- can't because we are required to raise Constraint_Error for | |
7553 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
7554 | -- See ACVC test C4A012B. | |
7555 | ||
7556 | if Expv >= 0 and then Expv <= 4 then | |
7557 | ||
7558 | -- X ** 0 = 1 (or 1.0) | |
7559 | ||
7560 | if Expv = 0 then | |
abcbd24c ST |
7561 | |
7562 | -- Call Remove_Side_Effects to ensure that any side effects | |
7563 | -- in the ignored left operand (in particular function calls | |
7564 | -- to user defined functions) are properly executed. | |
7565 | ||
7566 | Remove_Side_Effects (Base); | |
7567 | ||
70482933 RK |
7568 | if Ekind (Typ) in Integer_Kind then |
7569 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
7570 | else | |
7571 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
7572 | end if; | |
7573 | ||
7574 | -- X ** 1 = X | |
7575 | ||
7576 | elsif Expv = 1 then | |
7577 | Xnode := Base; | |
7578 | ||
7579 | -- X ** 2 = X * X | |
7580 | ||
7581 | elsif Expv = 2 then | |
7582 | Xnode := | |
7583 | Make_Op_Multiply (Loc, | |
7584 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 7585 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
7586 | |
7587 | -- X ** 3 = X * X * X | |
7588 | ||
7589 | elsif Expv = 3 then | |
7590 | Xnode := | |
7591 | Make_Op_Multiply (Loc, | |
7592 | Left_Opnd => | |
7593 | Make_Op_Multiply (Loc, | |
7594 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
7595 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
7596 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
7597 | |
7598 | -- X ** 4 -> | |
cb42ba5d AC |
7599 | |
7600 | -- do | |
70482933 | 7601 | -- En : constant base'type := base * base; |
cb42ba5d | 7602 | -- in |
70482933 RK |
7603 | -- En * En |
7604 | ||
cb42ba5d AC |
7605 | else |
7606 | pragma Assert (Expv = 4); | |
191fcb3a | 7607 | Temp := Make_Temporary (Loc, 'E', Base); |
70482933 | 7608 | |
cb42ba5d AC |
7609 | Xnode := |
7610 | Make_Expression_With_Actions (Loc, | |
7611 | Actions => New_List ( | |
7612 | Make_Object_Declaration (Loc, | |
7613 | Defining_Identifier => Temp, | |
7614 | Constant_Present => True, | |
e4494292 | 7615 | Object_Definition => New_Occurrence_Of (Typ, Loc), |
cb42ba5d AC |
7616 | Expression => |
7617 | Make_Op_Multiply (Loc, | |
7618 | Left_Opnd => | |
7619 | Duplicate_Subexpr (Base), | |
7620 | Right_Opnd => | |
7621 | Duplicate_Subexpr_No_Checks (Base)))), | |
7622 | ||
70482933 RK |
7623 | Expression => |
7624 | Make_Op_Multiply (Loc, | |
e4494292 RD |
7625 | Left_Opnd => New_Occurrence_Of (Temp, Loc), |
7626 | Right_Opnd => New_Occurrence_Of (Temp, Loc))); | |
70482933 RK |
7627 | end if; |
7628 | ||
7629 | Rewrite (N, Xnode); | |
7630 | Analyze_And_Resolve (N, Typ); | |
7631 | return; | |
7632 | end if; | |
7633 | end if; | |
7634 | ||
7635 | -- Case of (2 ** expression) appearing as an argument of an integer | |
7636 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 7637 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
7638 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
7639 | -- of the higher level node converts it into a shift. | |
7640 | ||
51bf9bdf AC |
7641 | -- Another case is 2 ** N in any other context. We simply convert |
7642 | -- this to 1 * 2 ** N, and then the above transformation applies. | |
7643 | ||
685094bf RD |
7644 | -- Note: this transformation is not applicable for a modular type with |
7645 | -- a non-binary modulus in the multiplication case, since we get a wrong | |
7646 | -- result if the shift causes an overflow before the modular reduction. | |
7647 | ||
8b4230c8 AC |
7648 | -- Note: we used to check that Exptyp was an unsigned type. But that is |
7649 | -- an unnecessary check, since if Exp is negative, we have a run-time | |
7650 | -- error that is either caught (so we get the right result) or we have | |
7651 | -- suppressed the check, in which case the code is erroneous anyway. | |
7652 | ||
70482933 | 7653 | if Nkind (Base) = N_Integer_Literal |
6c3c671e AC |
7654 | and then CRT_Safe_Compile_Time_Known_Value (Base) |
7655 | and then Expr_Value (Base) = Uint_2 | |
70482933 RK |
7656 | and then Is_Integer_Type (Root_Type (Exptyp)) |
7657 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
70482933 | 7658 | and then not Ovflo |
70482933 | 7659 | then |
51bf9bdf | 7660 | -- First the multiply and divide cases |
70482933 | 7661 | |
51bf9bdf AC |
7662 | if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then |
7663 | declare | |
7664 | P : constant Node_Id := Parent (N); | |
7665 | L : constant Node_Id := Left_Opnd (P); | |
7666 | R : constant Node_Id := Right_Opnd (P); | |
7667 | ||
7668 | begin | |
7669 | if (Nkind (P) = N_Op_Multiply | |
7670 | and then not Non_Binary_Modulus (Typ) | |
7671 | and then | |
7672 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
7673 | or else | |
7674 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
7675 | and then not Do_Overflow_Check (P)) | |
7676 | or else | |
7677 | (Nkind (P) = N_Op_Divide | |
533369aa AC |
7678 | and then Is_Integer_Type (Etype (L)) |
7679 | and then Is_Unsigned_Type (Etype (L)) | |
7680 | and then R = N | |
7681 | and then not Do_Overflow_Check (P)) | |
51bf9bdf AC |
7682 | then |
7683 | Set_Is_Power_Of_2_For_Shift (N); | |
7684 | return; | |
7685 | end if; | |
7686 | end; | |
7687 | ||
7688 | -- Now the other cases | |
7689 | ||
7690 | elsif not Non_Binary_Modulus (Typ) then | |
7691 | Rewrite (N, | |
7692 | Make_Op_Multiply (Loc, | |
7693 | Left_Opnd => Make_Integer_Literal (Loc, 1), | |
7694 | Right_Opnd => Relocate_Node (N))); | |
7695 | Analyze_And_Resolve (N, Typ); | |
7696 | return; | |
7697 | end if; | |
70482933 RK |
7698 | end if; |
7699 | ||
07fc65c4 GB |
7700 | -- Fall through if exponentiation must be done using a runtime routine |
7701 | ||
07fc65c4 | 7702 | -- First deal with modular case |
70482933 RK |
7703 | |
7704 | if Is_Modular_Integer_Type (Rtyp) then | |
7705 | ||
7706 | -- Non-binary case, we call the special exponentiation routine for | |
7707 | -- the non-binary case, converting the argument to Long_Long_Integer | |
7708 | -- and passing the modulus value. Then the result is converted back | |
7709 | -- to the base type. | |
7710 | ||
7711 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
7712 | Rewrite (N, |
7713 | Convert_To (Typ, | |
7714 | Make_Function_Call (Loc, | |
cc6f5d75 AC |
7715 | Name => |
7716 | New_Occurrence_Of (RTE (RE_Exp_Modular), Loc), | |
70482933 | 7717 | Parameter_Associations => New_List ( |
e9daba51 | 7718 | Convert_To (RTE (RE_Unsigned), Base), |
70482933 RK |
7719 | Make_Integer_Literal (Loc, Modulus (Rtyp)), |
7720 | Exp)))); | |
7721 | ||
685094bf RD |
7722 | -- Binary case, in this case, we call one of two routines, either the |
7723 | -- unsigned integer case, or the unsigned long long integer case, | |
7724 | -- with a final "and" operation to do the required mod. | |
70482933 RK |
7725 | |
7726 | else | |
7727 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
7728 | Ent := RTE (RE_Exp_Unsigned); | |
7729 | else | |
7730 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
7731 | end if; | |
7732 | ||
7733 | Rewrite (N, | |
7734 | Convert_To (Typ, | |
7735 | Make_Op_And (Loc, | |
cc6f5d75 | 7736 | Left_Opnd => |
70482933 | 7737 | Make_Function_Call (Loc, |
cc6f5d75 | 7738 | Name => New_Occurrence_Of (Ent, Loc), |
70482933 RK |
7739 | Parameter_Associations => New_List ( |
7740 | Convert_To (Etype (First_Formal (Ent)), Base), | |
7741 | Exp)), | |
7742 | Right_Opnd => | |
7743 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
7744 | ||
7745 | end if; | |
7746 | ||
7747 | -- Common exit point for modular type case | |
7748 | ||
7749 | Analyze_And_Resolve (N, Typ); | |
7750 | return; | |
7751 | ||
fbf5a39b AC |
7752 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
7753 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
7754 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 7755 | -- might need certification when a certified run time is required. |
70482933 | 7756 | |
fbf5a39b | 7757 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
7758 | -- checks are required, and one when they are not required, since there |
7759 | -- is a real gain in omitting checks on many machines. | |
70482933 | 7760 | |
fbf5a39b AC |
7761 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
7762 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
761f7dcb AC |
7763 | and then |
7764 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
7765 | or else Rtyp = Universal_Integer | |
70482933 | 7766 | then |
fbf5a39b AC |
7767 | Etyp := Standard_Long_Long_Integer; |
7768 | ||
ebb6b0bd AC |
7769 | -- Overflow checking is the only choice on the AAMP target, where |
7770 | -- arithmetic instructions check overflow automatically, so only | |
7771 | -- one version of the exponentiation unit is needed. | |
7772 | ||
1037b0f4 | 7773 | if Ovflo or AAMP_On_Target then |
70482933 RK |
7774 | Rent := RE_Exp_Long_Long_Integer; |
7775 | else | |
7776 | Rent := RE_Exn_Long_Long_Integer; | |
7777 | end if; | |
7778 | ||
fbf5a39b AC |
7779 | elsif Is_Signed_Integer_Type (Rtyp) then |
7780 | Etyp := Standard_Integer; | |
70482933 | 7781 | |
ebb6b0bd AC |
7782 | -- Overflow checking is the only choice on the AAMP target, where |
7783 | -- arithmetic instructions check overflow automatically, so only | |
7784 | -- one version of the exponentiation unit is needed. | |
7785 | ||
1037b0f4 | 7786 | if Ovflo or AAMP_On_Target then |
fbf5a39b | 7787 | Rent := RE_Exp_Integer; |
70482933 | 7788 | else |
fbf5a39b | 7789 | Rent := RE_Exn_Integer; |
70482933 | 7790 | end if; |
fbf5a39b AC |
7791 | |
7792 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
7793 | -- need separate routines for the overflow case here, since in the case | |
7794 | -- of floating-point, we generate infinities anyway as a rule (either | |
7795 | -- that or we automatically trap overflow), and if there is an infinity | |
7796 | -- generated and a range check is required, the check will fail anyway. | |
7797 | ||
7798 | else | |
7799 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
7800 | Etyp := Standard_Long_Long_Float; | |
7801 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
7802 | end if; |
7803 | ||
7804 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 7805 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 7806 | |
fbf5a39b | 7807 | if Typ = Etyp |
70482933 RK |
7808 | and then Rtyp /= Universal_Integer |
7809 | and then Rtyp /= Universal_Real | |
7810 | then | |
7811 | Rewrite (N, | |
7812 | Make_Function_Call (Loc, | |
e4494292 | 7813 | Name => New_Occurrence_Of (RTE (Rent), Loc), |
70482933 RK |
7814 | Parameter_Associations => New_List (Base, Exp))); |
7815 | ||
7816 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 7817 | -- required in the universal cases, since the runtime routine is |
1147c704 | 7818 | -- typed using one of the standard types). |
70482933 RK |
7819 | |
7820 | else | |
7821 | Rewrite (N, | |
7822 | Convert_To (Typ, | |
7823 | Make_Function_Call (Loc, | |
e4494292 | 7824 | Name => New_Occurrence_Of (RTE (Rent), Loc), |
70482933 | 7825 | Parameter_Associations => New_List ( |
fbf5a39b | 7826 | Convert_To (Etyp, Base), |
70482933 RK |
7827 | Exp)))); |
7828 | end if; | |
7829 | ||
7830 | Analyze_And_Resolve (N, Typ); | |
7831 | return; | |
7832 | ||
fbf5a39b AC |
7833 | exception |
7834 | when RE_Not_Available => | |
7835 | return; | |
70482933 RK |
7836 | end Expand_N_Op_Expon; |
7837 | ||
7838 | -------------------- | |
7839 | -- Expand_N_Op_Ge -- | |
7840 | -------------------- | |
7841 | ||
7842 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
7843 | Typ : constant Entity_Id := Etype (N); | |
7844 | Op1 : constant Node_Id := Left_Opnd (N); | |
7845 | Op2 : constant Node_Id := Right_Opnd (N); | |
7846 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7847 | ||
7848 | begin | |
7849 | Binary_Op_Validity_Checks (N); | |
7850 | ||
456cbfa5 | 7851 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7852 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7853 | |
7854 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7855 | ||
7856 | if Nkind (N) /= N_Op_Ge then | |
7857 | return; | |
7858 | end if; | |
7859 | ||
7860 | -- Array type case | |
7861 | ||
f02b8bb8 | 7862 | if Is_Array_Type (Typ1) then |
70482933 RK |
7863 | Expand_Array_Comparison (N); |
7864 | return; | |
7865 | end if; | |
7866 | ||
456cbfa5 AC |
7867 | -- Deal with boolean operands |
7868 | ||
70482933 RK |
7869 | if Is_Boolean_Type (Typ1) then |
7870 | Adjust_Condition (Op1); | |
7871 | Adjust_Condition (Op2); | |
7872 | Set_Etype (N, Standard_Boolean); | |
7873 | Adjust_Result_Type (N, Typ); | |
7874 | end if; | |
7875 | ||
7876 | Rewrite_Comparison (N); | |
f02b8bb8 | 7877 | |
0580d807 | 7878 | Optimize_Length_Comparison (N); |
70482933 RK |
7879 | end Expand_N_Op_Ge; |
7880 | ||
7881 | -------------------- | |
7882 | -- Expand_N_Op_Gt -- | |
7883 | -------------------- | |
7884 | ||
7885 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
7886 | Typ : constant Entity_Id := Etype (N); | |
7887 | Op1 : constant Node_Id := Left_Opnd (N); | |
7888 | Op2 : constant Node_Id := Right_Opnd (N); | |
7889 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7890 | ||
7891 | begin | |
7892 | Binary_Op_Validity_Checks (N); | |
7893 | ||
456cbfa5 | 7894 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7895 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7896 | |
7897 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7898 | ||
7899 | if Nkind (N) /= N_Op_Gt then | |
7900 | return; | |
7901 | end if; | |
7902 | ||
7903 | -- Deal with array type operands | |
7904 | ||
f02b8bb8 | 7905 | if Is_Array_Type (Typ1) then |
70482933 RK |
7906 | Expand_Array_Comparison (N); |
7907 | return; | |
7908 | end if; | |
7909 | ||
456cbfa5 AC |
7910 | -- Deal with boolean type operands |
7911 | ||
70482933 RK |
7912 | if Is_Boolean_Type (Typ1) then |
7913 | Adjust_Condition (Op1); | |
7914 | Adjust_Condition (Op2); | |
7915 | Set_Etype (N, Standard_Boolean); | |
7916 | Adjust_Result_Type (N, Typ); | |
7917 | end if; | |
7918 | ||
7919 | Rewrite_Comparison (N); | |
f02b8bb8 | 7920 | |
0580d807 | 7921 | Optimize_Length_Comparison (N); |
70482933 RK |
7922 | end Expand_N_Op_Gt; |
7923 | ||
7924 | -------------------- | |
7925 | -- Expand_N_Op_Le -- | |
7926 | -------------------- | |
7927 | ||
7928 | procedure Expand_N_Op_Le (N : Node_Id) is | |
7929 | Typ : constant Entity_Id := Etype (N); | |
7930 | Op1 : constant Node_Id := Left_Opnd (N); | |
7931 | Op2 : constant Node_Id := Right_Opnd (N); | |
7932 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7933 | ||
7934 | begin | |
7935 | Binary_Op_Validity_Checks (N); | |
7936 | ||
456cbfa5 | 7937 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7938 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7939 | |
7940 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7941 | ||
7942 | if Nkind (N) /= N_Op_Le then | |
7943 | return; | |
7944 | end if; | |
7945 | ||
7946 | -- Deal with array type operands | |
7947 | ||
f02b8bb8 | 7948 | if Is_Array_Type (Typ1) then |
70482933 RK |
7949 | Expand_Array_Comparison (N); |
7950 | return; | |
7951 | end if; | |
7952 | ||
456cbfa5 AC |
7953 | -- Deal with Boolean type operands |
7954 | ||
70482933 RK |
7955 | if Is_Boolean_Type (Typ1) then |
7956 | Adjust_Condition (Op1); | |
7957 | Adjust_Condition (Op2); | |
7958 | Set_Etype (N, Standard_Boolean); | |
7959 | Adjust_Result_Type (N, Typ); | |
7960 | end if; | |
7961 | ||
7962 | Rewrite_Comparison (N); | |
f02b8bb8 | 7963 | |
0580d807 | 7964 | Optimize_Length_Comparison (N); |
70482933 RK |
7965 | end Expand_N_Op_Le; |
7966 | ||
7967 | -------------------- | |
7968 | -- Expand_N_Op_Lt -- | |
7969 | -------------------- | |
7970 | ||
7971 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
7972 | Typ : constant Entity_Id := Etype (N); | |
7973 | Op1 : constant Node_Id := Left_Opnd (N); | |
7974 | Op2 : constant Node_Id := Right_Opnd (N); | |
7975 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7976 | ||
7977 | begin | |
7978 | Binary_Op_Validity_Checks (N); | |
7979 | ||
456cbfa5 | 7980 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7981 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7982 | |
7983 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7984 | ||
7985 | if Nkind (N) /= N_Op_Lt then | |
7986 | return; | |
7987 | end if; | |
7988 | ||
7989 | -- Deal with array type operands | |
7990 | ||
f02b8bb8 | 7991 | if Is_Array_Type (Typ1) then |
70482933 RK |
7992 | Expand_Array_Comparison (N); |
7993 | return; | |
7994 | end if; | |
7995 | ||
456cbfa5 AC |
7996 | -- Deal with Boolean type operands |
7997 | ||
70482933 RK |
7998 | if Is_Boolean_Type (Typ1) then |
7999 | Adjust_Condition (Op1); | |
8000 | Adjust_Condition (Op2); | |
8001 | Set_Etype (N, Standard_Boolean); | |
8002 | Adjust_Result_Type (N, Typ); | |
8003 | end if; | |
8004 | ||
8005 | Rewrite_Comparison (N); | |
f02b8bb8 | 8006 | |
0580d807 | 8007 | Optimize_Length_Comparison (N); |
70482933 RK |
8008 | end Expand_N_Op_Lt; |
8009 | ||
8010 | ----------------------- | |
8011 | -- Expand_N_Op_Minus -- | |
8012 | ----------------------- | |
8013 | ||
8014 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
8015 | Loc : constant Source_Ptr := Sloc (N); | |
8016 | Typ : constant Entity_Id := Etype (N); | |
8017 | ||
8018 | begin | |
8019 | Unary_Op_Validity_Checks (N); | |
8020 | ||
b6b5cca8 AC |
8021 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8022 | ||
8023 | if Minimized_Eliminated_Overflow_Check (N) then | |
8024 | Apply_Arithmetic_Overflow_Check (N); | |
8025 | return; | |
8026 | end if; | |
8027 | ||
07fc65c4 | 8028 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
8029 | and then Is_Signed_Integer_Type (Etype (N)) |
8030 | and then Do_Overflow_Check (N) | |
8031 | then | |
8032 | -- Software overflow checking expands -expr into (0 - expr) | |
8033 | ||
8034 | Rewrite (N, | |
8035 | Make_Op_Subtract (Loc, | |
8036 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
8037 | Right_Opnd => Right_Opnd (N))); | |
8038 | ||
8039 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
8040 | end if; |
8041 | end Expand_N_Op_Minus; | |
8042 | ||
8043 | --------------------- | |
8044 | -- Expand_N_Op_Mod -- | |
8045 | --------------------- | |
8046 | ||
8047 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
8048 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 8049 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
8050 | DDC : constant Boolean := Do_Division_Check (N); |
8051 | ||
b6b5cca8 AC |
8052 | Left : Node_Id; |
8053 | Right : Node_Id; | |
8054 | ||
70482933 RK |
8055 | LLB : Uint; |
8056 | Llo : Uint; | |
8057 | Lhi : Uint; | |
8058 | LOK : Boolean; | |
8059 | Rlo : Uint; | |
8060 | Rhi : Uint; | |
8061 | ROK : Boolean; | |
8062 | ||
1033834f RD |
8063 | pragma Warnings (Off, Lhi); |
8064 | ||
70482933 RK |
8065 | begin |
8066 | Binary_Op_Validity_Checks (N); | |
8067 | ||
b6b5cca8 AC |
8068 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8069 | ||
8070 | if Minimized_Eliminated_Overflow_Check (N) then | |
8071 | Apply_Arithmetic_Overflow_Check (N); | |
8072 | return; | |
8073 | end if; | |
8074 | ||
9a6dc470 RD |
8075 | if Is_Integer_Type (Etype (N)) then |
8076 | Apply_Divide_Checks (N); | |
b6b5cca8 AC |
8077 | |
8078 | -- All done if we don't have a MOD any more, which can happen as a | |
8079 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
8080 | ||
8081 | if Nkind (N) /= N_Op_Mod then | |
8082 | return; | |
8083 | end if; | |
9a6dc470 RD |
8084 | end if; |
8085 | ||
b6b5cca8 AC |
8086 | -- Proceed with expansion of mod operator |
8087 | ||
8088 | Left := Left_Opnd (N); | |
8089 | Right := Right_Opnd (N); | |
8090 | ||
5d5e9775 AC |
8091 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
8092 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); | |
70482933 | 8093 | |
2c9f8c0a AC |
8094 | -- Convert mod to rem if operands are both known to be non-negative, or |
8095 | -- both known to be non-positive (these are the cases in which rem and | |
8096 | -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite | |
8097 | -- likely that this will improve the quality of code, (the operation now | |
8098 | -- corresponds to the hardware remainder), and it does not seem likely | |
8099 | -- that it could be harmful. It also avoids some cases of the elaborate | |
8100 | -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %). | |
8101 | ||
8102 | if (LOK and ROK) | |
8103 | and then ((Llo >= 0 and then Rlo >= 0) | |
cc6f5d75 | 8104 | or else |
2c9f8c0a AC |
8105 | (Lhi <= 0 and then Rhi <= 0)) |
8106 | then | |
70482933 RK |
8107 | Rewrite (N, |
8108 | Make_Op_Rem (Sloc (N), | |
8109 | Left_Opnd => Left_Opnd (N), | |
8110 | Right_Opnd => Right_Opnd (N))); | |
8111 | ||
685094bf RD |
8112 | -- Instead of reanalyzing the node we do the analysis manually. This |
8113 | -- avoids anomalies when the replacement is done in an instance and | |
8114 | -- is epsilon more efficient. | |
70482933 RK |
8115 | |
8116 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 8117 | Set_Etype (N, Typ); |
70482933 RK |
8118 | Set_Do_Division_Check (N, DDC); |
8119 | Expand_N_Op_Rem (N); | |
8120 | Set_Analyzed (N); | |
2c9f8c0a | 8121 | return; |
70482933 RK |
8122 | |
8123 | -- Otherwise, normal mod processing | |
8124 | ||
8125 | else | |
fbf5a39b AC |
8126 | -- Apply optimization x mod 1 = 0. We don't really need that with |
8127 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
8128 | -- certainly harmless. | |
8129 | ||
8130 | if Is_Integer_Type (Etype (N)) | |
8131 | and then Compile_Time_Known_Value (Right) | |
8132 | and then Expr_Value (Right) = Uint_1 | |
8133 | then | |
abcbd24c ST |
8134 | -- Call Remove_Side_Effects to ensure that any side effects in |
8135 | -- the ignored left operand (in particular function calls to | |
8136 | -- user defined functions) are properly executed. | |
8137 | ||
8138 | Remove_Side_Effects (Left); | |
8139 | ||
fbf5a39b AC |
8140 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
8141 | Analyze_And_Resolve (N, Typ); | |
8142 | return; | |
8143 | end if; | |
8144 | ||
2c9f8c0a AC |
8145 | -- If we still have a mod operator and we are in Modify_Tree_For_C |
8146 | -- mode, and we have a signed integer type, then here is where we do | |
8147 | -- the rewrite in terms of Rem. Note this rewrite bypasses the need | |
8148 | -- for the special handling of the annoying case of largest negative | |
8149 | -- number mod minus one. | |
8150 | ||
8151 | if Nkind (N) = N_Op_Mod | |
8152 | and then Is_Signed_Integer_Type (Typ) | |
8153 | and then Modify_Tree_For_C | |
8154 | then | |
8155 | -- In the general case, we expand A mod B as | |
8156 | ||
8157 | -- Tnn : constant typ := A rem B; | |
8158 | -- .. | |
8159 | -- (if (A >= 0) = (B >= 0) then Tnn | |
8160 | -- elsif Tnn = 0 then 0 | |
8161 | -- else Tnn + B) | |
8162 | ||
8163 | -- The comparison can be written simply as A >= 0 if we know that | |
8164 | -- B >= 0 which is a very common case. | |
8165 | ||
8166 | -- An important optimization is when B is known at compile time | |
8167 | -- to be 2**K for some constant. In this case we can simply AND | |
8168 | -- the left operand with the bit string 2**K-1 (i.e. K 1-bits) | |
8169 | -- and that works for both the positive and negative cases. | |
8170 | ||
8171 | declare | |
8172 | P2 : constant Nat := Power_Of_Two (Right); | |
8173 | ||
8174 | begin | |
8175 | if P2 /= 0 then | |
8176 | Rewrite (N, | |
8177 | Unchecked_Convert_To (Typ, | |
8178 | Make_Op_And (Loc, | |
8179 | Left_Opnd => | |
8180 | Unchecked_Convert_To | |
8181 | (Corresponding_Unsigned_Type (Typ), Left), | |
8182 | Right_Opnd => | |
8183 | Make_Integer_Literal (Loc, 2 ** P2 - 1)))); | |
8184 | Analyze_And_Resolve (N, Typ); | |
8185 | return; | |
8186 | end if; | |
8187 | end; | |
8188 | ||
8189 | -- Here for the full rewrite | |
8190 | ||
8191 | declare | |
8192 | Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N); | |
8193 | Cmp : Node_Id; | |
8194 | ||
8195 | begin | |
8196 | Cmp := | |
8197 | Make_Op_Ge (Loc, | |
8198 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
8199 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
8200 | ||
8201 | if not LOK or else Rlo < 0 then | |
8202 | Cmp := | |
8203 | Make_Op_Eq (Loc, | |
8204 | Left_Opnd => Cmp, | |
8205 | Right_Opnd => | |
8206 | Make_Op_Ge (Loc, | |
8207 | Left_Opnd => Duplicate_Subexpr_No_Checks (Right), | |
8208 | Right_Opnd => Make_Integer_Literal (Loc, 0))); | |
8209 | end if; | |
8210 | ||
8211 | Insert_Action (N, | |
8212 | Make_Object_Declaration (Loc, | |
8213 | Defining_Identifier => Tnn, | |
8214 | Constant_Present => True, | |
8215 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
8216 | Expression => | |
8217 | Make_Op_Rem (Loc, | |
8218 | Left_Opnd => Left, | |
8219 | Right_Opnd => Right))); | |
8220 | ||
8221 | Rewrite (N, | |
8222 | Make_If_Expression (Loc, | |
8223 | Expressions => New_List ( | |
8224 | Cmp, | |
8225 | New_Occurrence_Of (Tnn, Loc), | |
8226 | Make_If_Expression (Loc, | |
8227 | Is_Elsif => True, | |
8228 | Expressions => New_List ( | |
8229 | Make_Op_Eq (Loc, | |
8230 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8231 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
8232 | Make_Integer_Literal (Loc, 0), | |
8233 | Make_Op_Add (Loc, | |
8234 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8235 | Right_Opnd => | |
8236 | Duplicate_Subexpr_No_Checks (Right))))))); | |
8237 | ||
8238 | Analyze_And_Resolve (N, Typ); | |
8239 | return; | |
8240 | end; | |
8241 | end if; | |
8242 | ||
8243 | -- Deal with annoying case of largest negative number mod minus one. | |
8244 | -- Gigi may not handle this case correctly, because on some targets, | |
8245 | -- the mod value is computed using a divide instruction which gives | |
8246 | -- an overflow trap for this case. | |
b9daa96e AC |
8247 | |
8248 | -- It would be a bit more efficient to figure out which targets | |
8249 | -- this is really needed for, but in practice it is reasonable | |
8250 | -- to do the following special check in all cases, since it means | |
8251 | -- we get a clearer message, and also the overhead is minimal given | |
8252 | -- that division is expensive in any case. | |
70482933 | 8253 | |
685094bf RD |
8254 | -- In fact the check is quite easy, if the right operand is -1, then |
8255 | -- the mod value is always 0, and we can just ignore the left operand | |
8256 | -- completely in this case. | |
70482933 | 8257 | |
9a6dc470 RD |
8258 | -- This only applies if we still have a mod operator. Skip if we |
8259 | -- have already rewritten this (e.g. in the case of eliminated | |
8260 | -- overflow checks which have driven us into bignum mode). | |
fbf5a39b | 8261 | |
9a6dc470 | 8262 | if Nkind (N) = N_Op_Mod then |
70482933 | 8263 | |
9a6dc470 RD |
8264 | -- The operand type may be private (e.g. in the expansion of an |
8265 | -- intrinsic operation) so we must use the underlying type to get | |
8266 | -- the bounds, and convert the literals explicitly. | |
70482933 | 8267 | |
9a6dc470 RD |
8268 | LLB := |
8269 | Expr_Value | |
8270 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
8271 | ||
8272 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
761f7dcb | 8273 | and then ((not LOK) or else (Llo = LLB)) |
9a6dc470 RD |
8274 | then |
8275 | Rewrite (N, | |
9b16cb57 | 8276 | Make_If_Expression (Loc, |
9a6dc470 RD |
8277 | Expressions => New_List ( |
8278 | Make_Op_Eq (Loc, | |
8279 | Left_Opnd => Duplicate_Subexpr (Right), | |
8280 | Right_Opnd => | |
8281 | Unchecked_Convert_To (Typ, | |
8282 | Make_Integer_Literal (Loc, -1))), | |
8283 | Unchecked_Convert_To (Typ, | |
8284 | Make_Integer_Literal (Loc, Uint_0)), | |
8285 | Relocate_Node (N)))); | |
8286 | ||
8287 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
8288 | Analyze_And_Resolve (N, Typ); | |
8289 | end if; | |
70482933 RK |
8290 | end if; |
8291 | end if; | |
8292 | end Expand_N_Op_Mod; | |
8293 | ||
8294 | -------------------------- | |
8295 | -- Expand_N_Op_Multiply -- | |
8296 | -------------------------- | |
8297 | ||
8298 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
8299 | Loc : constant Source_Ptr := Sloc (N); |
8300 | Lop : constant Node_Id := Left_Opnd (N); | |
8301 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 8302 | |
abcbd24c | 8303 | Lp2 : constant Boolean := |
533369aa | 8304 | Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop); |
abcbd24c | 8305 | Rp2 : constant Boolean := |
533369aa | 8306 | Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop); |
fbf5a39b | 8307 | |
70482933 RK |
8308 | Ltyp : constant Entity_Id := Etype (Lop); |
8309 | Rtyp : constant Entity_Id := Etype (Rop); | |
8310 | Typ : Entity_Id := Etype (N); | |
8311 | ||
8312 | begin | |
8313 | Binary_Op_Validity_Checks (N); | |
8314 | ||
b6b5cca8 AC |
8315 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8316 | ||
8317 | if Minimized_Eliminated_Overflow_Check (N) then | |
8318 | Apply_Arithmetic_Overflow_Check (N); | |
8319 | return; | |
8320 | end if; | |
8321 | ||
70482933 RK |
8322 | -- Special optimizations for integer types |
8323 | ||
8324 | if Is_Integer_Type (Typ) then | |
8325 | ||
abcbd24c | 8326 | -- N * 0 = 0 for integer types |
70482933 | 8327 | |
abcbd24c ST |
8328 | if Compile_Time_Known_Value (Rop) |
8329 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 8330 | then |
abcbd24c ST |
8331 | -- Call Remove_Side_Effects to ensure that any side effects in |
8332 | -- the ignored left operand (in particular function calls to | |
8333 | -- user defined functions) are properly executed. | |
8334 | ||
8335 | Remove_Side_Effects (Lop); | |
8336 | ||
8337 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
8338 | Analyze_And_Resolve (N, Typ); | |
8339 | return; | |
8340 | end if; | |
8341 | ||
8342 | -- Similar handling for 0 * N = 0 | |
8343 | ||
8344 | if Compile_Time_Known_Value (Lop) | |
8345 | and then Expr_Value (Lop) = Uint_0 | |
8346 | then | |
8347 | Remove_Side_Effects (Rop); | |
70482933 RK |
8348 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
8349 | Analyze_And_Resolve (N, Typ); | |
8350 | return; | |
8351 | end if; | |
8352 | ||
8353 | -- N * 1 = 1 * N = N for integer types | |
8354 | ||
fbf5a39b AC |
8355 | -- This optimisation is not done if we are going to |
8356 | -- rewrite the product 1 * 2 ** N to a shift. | |
8357 | ||
8358 | if Compile_Time_Known_Value (Rop) | |
8359 | and then Expr_Value (Rop) = Uint_1 | |
8360 | and then not Lp2 | |
70482933 | 8361 | then |
fbf5a39b | 8362 | Rewrite (N, Lop); |
70482933 RK |
8363 | return; |
8364 | ||
fbf5a39b AC |
8365 | elsif Compile_Time_Known_Value (Lop) |
8366 | and then Expr_Value (Lop) = Uint_1 | |
8367 | and then not Rp2 | |
70482933 | 8368 | then |
fbf5a39b | 8369 | Rewrite (N, Rop); |
70482933 RK |
8370 | return; |
8371 | end if; | |
8372 | end if; | |
8373 | ||
70482933 RK |
8374 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
8375 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
8376 | -- operand is an integer, as required for this to work. | |
8377 | ||
fbf5a39b AC |
8378 | if Rp2 then |
8379 | if Lp2 then | |
70482933 | 8380 | |
fbf5a39b | 8381 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
8382 | |
8383 | Rewrite (N, | |
8384 | Make_Op_Expon (Loc, | |
8385 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
8386 | Right_Opnd => | |
8387 | Make_Op_Add (Loc, | |
8388 | Left_Opnd => Right_Opnd (Lop), | |
8389 | Right_Opnd => Right_Opnd (Rop)))); | |
8390 | Analyze_And_Resolve (N, Typ); | |
8391 | return; | |
8392 | ||
8393 | else | |
eefe3761 AC |
8394 | -- If the result is modular, perform the reduction of the result |
8395 | -- appropriately. | |
8396 | ||
8397 | if Is_Modular_Integer_Type (Typ) | |
8398 | and then not Non_Binary_Modulus (Typ) | |
8399 | then | |
8400 | Rewrite (N, | |
573e5dd6 RD |
8401 | Make_Op_And (Loc, |
8402 | Left_Opnd => | |
8403 | Make_Op_Shift_Left (Loc, | |
8404 | Left_Opnd => Lop, | |
8405 | Right_Opnd => | |
8406 | Convert_To (Standard_Natural, Right_Opnd (Rop))), | |
8407 | Right_Opnd => | |
eefe3761 | 8408 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); |
573e5dd6 | 8409 | |
eefe3761 AC |
8410 | else |
8411 | Rewrite (N, | |
8412 | Make_Op_Shift_Left (Loc, | |
8413 | Left_Opnd => Lop, | |
8414 | Right_Opnd => | |
8415 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
8416 | end if; | |
8417 | ||
70482933 RK |
8418 | Analyze_And_Resolve (N, Typ); |
8419 | return; | |
8420 | end if; | |
8421 | ||
8422 | -- Same processing for the operands the other way round | |
8423 | ||
fbf5a39b | 8424 | elsif Lp2 then |
eefe3761 AC |
8425 | if Is_Modular_Integer_Type (Typ) |
8426 | and then not Non_Binary_Modulus (Typ) | |
8427 | then | |
8428 | Rewrite (N, | |
573e5dd6 RD |
8429 | Make_Op_And (Loc, |
8430 | Left_Opnd => | |
8431 | Make_Op_Shift_Left (Loc, | |
8432 | Left_Opnd => Rop, | |
8433 | Right_Opnd => | |
8434 | Convert_To (Standard_Natural, Right_Opnd (Lop))), | |
8435 | Right_Opnd => | |
8436 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); | |
8437 | ||
eefe3761 AC |
8438 | else |
8439 | Rewrite (N, | |
8440 | Make_Op_Shift_Left (Loc, | |
8441 | Left_Opnd => Rop, | |
8442 | Right_Opnd => | |
8443 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
8444 | end if; | |
8445 | ||
70482933 RK |
8446 | Analyze_And_Resolve (N, Typ); |
8447 | return; | |
8448 | end if; | |
8449 | ||
8450 | -- Do required fixup of universal fixed operation | |
8451 | ||
8452 | if Typ = Universal_Fixed then | |
8453 | Fixup_Universal_Fixed_Operation (N); | |
8454 | Typ := Etype (N); | |
8455 | end if; | |
8456 | ||
8457 | -- Multiplications with fixed-point results | |
8458 | ||
8459 | if Is_Fixed_Point_Type (Typ) then | |
8460 | ||
685094bf RD |
8461 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
8462 | -- a semantic point of view such operations are simply integer | |
8463 | -- operations and will be treated that way. | |
70482933 RK |
8464 | |
8465 | if not Treat_Fixed_As_Integer (N) then | |
8466 | ||
8467 | -- Case of fixed * integer => fixed | |
8468 | ||
8469 | if Is_Integer_Type (Rtyp) then | |
8470 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
8471 | ||
8472 | -- Case of integer * fixed => fixed | |
8473 | ||
8474 | elsif Is_Integer_Type (Ltyp) then | |
8475 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
8476 | ||
8477 | -- Case of fixed * fixed => fixed | |
8478 | ||
8479 | else | |
8480 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
8481 | end if; | |
8482 | end if; | |
8483 | ||
685094bf RD |
8484 | -- Other cases of multiplication of fixed-point operands. Again we |
8485 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
8486 | |
8487 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
8488 | and then not Treat_Fixed_As_Integer (N) | |
8489 | then | |
8490 | if Is_Integer_Type (Typ) then | |
8491 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
8492 | else | |
8493 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
8494 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
8495 | end if; | |
8496 | ||
685094bf RD |
8497 | -- Mixed-mode operations can appear in a non-static universal context, |
8498 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 8499 | |
533369aa | 8500 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
70482933 | 8501 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); |
70482933 RK |
8502 | Analyze_And_Resolve (Rop, Universal_Real); |
8503 | ||
533369aa | 8504 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
70482933 | 8505 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); |
70482933 RK |
8506 | Analyze_And_Resolve (Lop, Universal_Real); |
8507 | ||
8508 | -- Non-fixed point cases, check software overflow checking required | |
8509 | ||
8510 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
8511 | Apply_Arithmetic_Overflow_Check (N); | |
8512 | end if; | |
dfaff97b RD |
8513 | |
8514 | -- Overflow checks for floating-point if -gnateF mode active | |
8515 | ||
8516 | Check_Float_Op_Overflow (N); | |
70482933 RK |
8517 | end Expand_N_Op_Multiply; |
8518 | ||
8519 | -------------------- | |
8520 | -- Expand_N_Op_Ne -- | |
8521 | -------------------- | |
8522 | ||
70482933 | 8523 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 8524 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
8525 | |
8526 | begin | |
f02b8bb8 | 8527 | -- Case of elementary type with standard operator |
70482933 | 8528 | |
f02b8bb8 RD |
8529 | if Is_Elementary_Type (Typ) |
8530 | and then Sloc (Entity (N)) = Standard_Location | |
8531 | then | |
8532 | Binary_Op_Validity_Checks (N); | |
70482933 | 8533 | |
456cbfa5 | 8534 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if |
60b68e56 | 8535 | -- means we no longer have a /= operation, we are all done. |
456cbfa5 AC |
8536 | |
8537 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
8538 | ||
8539 | if Nkind (N) /= N_Op_Ne then | |
8540 | return; | |
8541 | end if; | |
8542 | ||
f02b8bb8 | 8543 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 8544 | |
f02b8bb8 RD |
8545 | if Is_Boolean_Type (Typ) then |
8546 | Adjust_Condition (Left_Opnd (N)); | |
8547 | Adjust_Condition (Right_Opnd (N)); | |
8548 | Set_Etype (N, Standard_Boolean); | |
8549 | Adjust_Result_Type (N, Typ); | |
8550 | end if; | |
fbf5a39b | 8551 | |
f02b8bb8 RD |
8552 | Rewrite_Comparison (N); |
8553 | ||
f02b8bb8 RD |
8554 | -- For all cases other than elementary types, we rewrite node as the |
8555 | -- negation of an equality operation, and reanalyze. The equality to be | |
8556 | -- used is defined in the same scope and has the same signature. This | |
8557 | -- signature must be set explicitly since in an instance it may not have | |
8558 | -- the same visibility as in the generic unit. This avoids duplicating | |
8559 | -- or factoring the complex code for record/array equality tests etc. | |
8560 | ||
8561 | else | |
8562 | declare | |
8563 | Loc : constant Source_Ptr := Sloc (N); | |
8564 | Neg : Node_Id; | |
8565 | Ne : constant Entity_Id := Entity (N); | |
8566 | ||
8567 | begin | |
8568 | Binary_Op_Validity_Checks (N); | |
8569 | ||
8570 | Neg := | |
8571 | Make_Op_Not (Loc, | |
8572 | Right_Opnd => | |
8573 | Make_Op_Eq (Loc, | |
8574 | Left_Opnd => Left_Opnd (N), | |
8575 | Right_Opnd => Right_Opnd (N))); | |
8576 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
8577 | ||
8578 | if Scope (Ne) /= Standard_Standard then | |
8579 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
8580 | end if; | |
8581 | ||
4637729f | 8582 | -- For navigation purposes, we want to treat the inequality as an |
f02b8bb8 | 8583 | -- implicit reference to the corresponding equality. Preserve the |
4637729f | 8584 | -- Comes_From_ source flag to generate proper Xref entries. |
f02b8bb8 RD |
8585 | |
8586 | Preserve_Comes_From_Source (Neg, N); | |
8587 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
8588 | Rewrite (N, Neg); | |
8589 | Analyze_And_Resolve (N, Standard_Boolean); | |
8590 | end; | |
8591 | end if; | |
0580d807 AC |
8592 | |
8593 | Optimize_Length_Comparison (N); | |
70482933 RK |
8594 | end Expand_N_Op_Ne; |
8595 | ||
8596 | --------------------- | |
8597 | -- Expand_N_Op_Not -- | |
8598 | --------------------- | |
8599 | ||
685094bf | 8600 | -- If the argument is other than a Boolean array type, there is no special |
7a5b62b0 AC |
8601 | -- expansion required, except for dealing with validity checks, and non- |
8602 | -- standard boolean representations. | |
70482933 | 8603 | |
7a5b62b0 AC |
8604 | -- For the packed array case, we call the special routine in Exp_Pakd, |
8605 | -- except that if the component size is greater than one, we use the | |
8606 | -- standard routine generating a gruesome loop (it is so peculiar to have | |
8607 | -- packed arrays with non-standard Boolean representations anyway, so it | |
8608 | -- does not matter that we do not handle this case efficiently). | |
70482933 | 8609 | |
7a5b62b0 AC |
8610 | -- For the unpacked array case (and for the special packed case where we |
8611 | -- have non standard Booleans, as discussed above), we generate and insert | |
8612 | -- into the tree the following function definition: | |
70482933 RK |
8613 | |
8614 | -- function Nnnn (A : arr) is | |
8615 | -- B : arr; | |
8616 | -- begin | |
8617 | -- for J in a'range loop | |
8618 | -- B (J) := not A (J); | |
8619 | -- end loop; | |
8620 | -- return B; | |
8621 | -- end Nnnn; | |
8622 | ||
8623 | -- Here arr is the actual subtype of the parameter (and hence always | |
8624 | -- constrained). Then we replace the not with a call to this function. | |
8625 | ||
8626 | procedure Expand_N_Op_Not (N : Node_Id) is | |
8627 | Loc : constant Source_Ptr := Sloc (N); | |
8628 | Typ : constant Entity_Id := Etype (N); | |
8629 | Opnd : Node_Id; | |
8630 | Arr : Entity_Id; | |
8631 | A : Entity_Id; | |
8632 | B : Entity_Id; | |
8633 | J : Entity_Id; | |
8634 | A_J : Node_Id; | |
8635 | B_J : Node_Id; | |
8636 | ||
8637 | Func_Name : Entity_Id; | |
8638 | Loop_Statement : Node_Id; | |
8639 | ||
8640 | begin | |
8641 | Unary_Op_Validity_Checks (N); | |
8642 | ||
8643 | -- For boolean operand, deal with non-standard booleans | |
8644 | ||
8645 | if Is_Boolean_Type (Typ) then | |
8646 | Adjust_Condition (Right_Opnd (N)); | |
8647 | Set_Etype (N, Standard_Boolean); | |
8648 | Adjust_Result_Type (N, Typ); | |
8649 | return; | |
8650 | end if; | |
8651 | ||
da94696d | 8652 | -- Only array types need any other processing |
70482933 | 8653 | |
da94696d | 8654 | if not Is_Array_Type (Typ) then |
70482933 RK |
8655 | return; |
8656 | end if; | |
8657 | ||
a9d8907c JM |
8658 | -- Case of array operand. If bit packed with a component size of 1, |
8659 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 8660 | |
a9d8907c JM |
8661 | if Is_Bit_Packed_Array (Typ) |
8662 | and then Component_Size (Typ) = 1 | |
8663 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
8664 | then | |
70482933 RK |
8665 | Expand_Packed_Not (N); |
8666 | return; | |
8667 | end if; | |
8668 | ||
fbf5a39b AC |
8669 | -- Case of array operand which is not bit-packed. If the context is |
8670 | -- a safe assignment, call in-place operation, If context is a larger | |
8671 | -- boolean expression in the context of a safe assignment, expansion is | |
8672 | -- done by enclosing operation. | |
70482933 RK |
8673 | |
8674 | Opnd := Relocate_Node (Right_Opnd (N)); | |
8675 | Convert_To_Actual_Subtype (Opnd); | |
8676 | Arr := Etype (Opnd); | |
8677 | Ensure_Defined (Arr, N); | |
b4592168 | 8678 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 8679 | |
fbf5a39b AC |
8680 | if Nkind (Parent (N)) = N_Assignment_Statement then |
8681 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
8682 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
8683 | return; | |
8684 | ||
5e1c00fa | 8685 | -- Special case the negation of a binary operation |
fbf5a39b | 8686 | |
303b4d58 | 8687 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 8688 | and then Safe_In_Place_Array_Op |
303b4d58 | 8689 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
8690 | then |
8691 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
8692 | return; | |
8693 | end if; | |
8694 | ||
8695 | elsif Nkind (Parent (N)) in N_Binary_Op | |
8696 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
8697 | then | |
8698 | declare | |
8699 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
8700 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
8701 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
8702 | ||
8703 | begin | |
8704 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
fbf5a39b | 8705 | |
aa9a7dd7 AC |
8706 | -- (not A) op (not B) can be reduced to a single call |
8707 | ||
8708 | if N = Op1 and then Nkind (Op2) = N_Op_Not then | |
fbf5a39b AC |
8709 | return; |
8710 | ||
bed8af19 AC |
8711 | elsif N = Op2 and then Nkind (Op1) = N_Op_Not then |
8712 | return; | |
8713 | ||
aa9a7dd7 | 8714 | -- A xor (not B) can also be special-cased |
fbf5a39b | 8715 | |
aa9a7dd7 | 8716 | elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then |
fbf5a39b AC |
8717 | return; |
8718 | end if; | |
8719 | end if; | |
8720 | end; | |
8721 | end if; | |
8722 | ||
70482933 RK |
8723 | A := Make_Defining_Identifier (Loc, Name_uA); |
8724 | B := Make_Defining_Identifier (Loc, Name_uB); | |
8725 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
8726 | ||
8727 | A_J := | |
8728 | Make_Indexed_Component (Loc, | |
e4494292 RD |
8729 | Prefix => New_Occurrence_Of (A, Loc), |
8730 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
8731 | |
8732 | B_J := | |
8733 | Make_Indexed_Component (Loc, | |
e4494292 RD |
8734 | Prefix => New_Occurrence_Of (B, Loc), |
8735 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
8736 | |
8737 | Loop_Statement := | |
8738 | Make_Implicit_Loop_Statement (N, | |
8739 | Identifier => Empty, | |
8740 | ||
8741 | Iteration_Scheme => | |
8742 | Make_Iteration_Scheme (Loc, | |
8743 | Loop_Parameter_Specification => | |
8744 | Make_Loop_Parameter_Specification (Loc, | |
0d901290 | 8745 | Defining_Identifier => J, |
70482933 RK |
8746 | Discrete_Subtype_Definition => |
8747 | Make_Attribute_Reference (Loc, | |
0d901290 | 8748 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
8749 | Attribute_Name => Name_Range))), |
8750 | ||
8751 | Statements => New_List ( | |
8752 | Make_Assignment_Statement (Loc, | |
8753 | Name => B_J, | |
8754 | Expression => Make_Op_Not (Loc, A_J)))); | |
8755 | ||
191fcb3a | 8756 | Func_Name := Make_Temporary (Loc, 'N'); |
70482933 RK |
8757 | Set_Is_Inlined (Func_Name); |
8758 | ||
8759 | Insert_Action (N, | |
8760 | Make_Subprogram_Body (Loc, | |
8761 | Specification => | |
8762 | Make_Function_Specification (Loc, | |
8763 | Defining_Unit_Name => Func_Name, | |
8764 | Parameter_Specifications => New_List ( | |
8765 | Make_Parameter_Specification (Loc, | |
8766 | Defining_Identifier => A, | |
e4494292 RD |
8767 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), |
8768 | Result_Definition => New_Occurrence_Of (Typ, Loc)), | |
70482933 RK |
8769 | |
8770 | Declarations => New_List ( | |
8771 | Make_Object_Declaration (Loc, | |
8772 | Defining_Identifier => B, | |
e4494292 | 8773 | Object_Definition => New_Occurrence_Of (Arr, Loc))), |
70482933 RK |
8774 | |
8775 | Handled_Statement_Sequence => | |
8776 | Make_Handled_Sequence_Of_Statements (Loc, | |
8777 | Statements => New_List ( | |
8778 | Loop_Statement, | |
d766cee3 | 8779 | Make_Simple_Return_Statement (Loc, |
0d901290 | 8780 | Expression => Make_Identifier (Loc, Chars (B))))))); |
70482933 RK |
8781 | |
8782 | Rewrite (N, | |
8783 | Make_Function_Call (Loc, | |
e4494292 | 8784 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
8785 | Parameter_Associations => New_List (Opnd))); |
8786 | ||
8787 | Analyze_And_Resolve (N, Typ); | |
8788 | end Expand_N_Op_Not; | |
8789 | ||
8790 | -------------------- | |
8791 | -- Expand_N_Op_Or -- | |
8792 | -------------------- | |
8793 | ||
8794 | procedure Expand_N_Op_Or (N : Node_Id) is | |
8795 | Typ : constant Entity_Id := Etype (N); | |
8796 | ||
8797 | begin | |
8798 | Binary_Op_Validity_Checks (N); | |
8799 | ||
8800 | if Is_Array_Type (Etype (N)) then | |
8801 | Expand_Boolean_Operator (N); | |
8802 | ||
8803 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
8804 | Adjust_Condition (Left_Opnd (N)); |
8805 | Adjust_Condition (Right_Opnd (N)); | |
8806 | Set_Etype (N, Standard_Boolean); | |
8807 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
8808 | |
8809 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
8810 | Expand_Intrinsic_Call (N, Entity (N)); | |
8811 | ||
70482933 RK |
8812 | end if; |
8813 | end Expand_N_Op_Or; | |
8814 | ||
8815 | ---------------------- | |
8816 | -- Expand_N_Op_Plus -- | |
8817 | ---------------------- | |
8818 | ||
8819 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
8820 | begin | |
8821 | Unary_Op_Validity_Checks (N); | |
b6b5cca8 AC |
8822 | |
8823 | -- Check for MINIMIZED/ELIMINATED overflow mode | |
8824 | ||
8825 | if Minimized_Eliminated_Overflow_Check (N) then | |
8826 | Apply_Arithmetic_Overflow_Check (N); | |
8827 | return; | |
8828 | end if; | |
70482933 RK |
8829 | end Expand_N_Op_Plus; |
8830 | ||
8831 | --------------------- | |
8832 | -- Expand_N_Op_Rem -- | |
8833 | --------------------- | |
8834 | ||
8835 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
8836 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 8837 | Typ : constant Entity_Id := Etype (N); |
70482933 | 8838 | |
b6b5cca8 AC |
8839 | Left : Node_Id; |
8840 | Right : Node_Id; | |
70482933 | 8841 | |
5d5e9775 AC |
8842 | Lo : Uint; |
8843 | Hi : Uint; | |
8844 | OK : Boolean; | |
70482933 | 8845 | |
5d5e9775 AC |
8846 | Lneg : Boolean; |
8847 | Rneg : Boolean; | |
8848 | -- Set if corresponding operand can be negative | |
8849 | ||
8850 | pragma Unreferenced (Hi); | |
1033834f | 8851 | |
70482933 RK |
8852 | begin |
8853 | Binary_Op_Validity_Checks (N); | |
8854 | ||
b6b5cca8 AC |
8855 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8856 | ||
8857 | if Minimized_Eliminated_Overflow_Check (N) then | |
8858 | Apply_Arithmetic_Overflow_Check (N); | |
8859 | return; | |
8860 | end if; | |
8861 | ||
70482933 | 8862 | if Is_Integer_Type (Etype (N)) then |
a91e9ac7 | 8863 | Apply_Divide_Checks (N); |
b6b5cca8 AC |
8864 | |
8865 | -- All done if we don't have a REM any more, which can happen as a | |
8866 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
8867 | ||
8868 | if Nkind (N) /= N_Op_Rem then | |
8869 | return; | |
8870 | end if; | |
70482933 RK |
8871 | end if; |
8872 | ||
b6b5cca8 AC |
8873 | -- Proceed with expansion of REM |
8874 | ||
8875 | Left := Left_Opnd (N); | |
8876 | Right := Right_Opnd (N); | |
8877 | ||
685094bf RD |
8878 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
8879 | -- but it is useful with other back ends (e.g. AAMP), and is certainly | |
8880 | -- harmless. | |
fbf5a39b AC |
8881 | |
8882 | if Is_Integer_Type (Etype (N)) | |
8883 | and then Compile_Time_Known_Value (Right) | |
8884 | and then Expr_Value (Right) = Uint_1 | |
8885 | then | |
abcbd24c ST |
8886 | -- Call Remove_Side_Effects to ensure that any side effects in the |
8887 | -- ignored left operand (in particular function calls to user defined | |
8888 | -- functions) are properly executed. | |
8889 | ||
8890 | Remove_Side_Effects (Left); | |
8891 | ||
fbf5a39b AC |
8892 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
8893 | Analyze_And_Resolve (N, Typ); | |
8894 | return; | |
8895 | end if; | |
8896 | ||
685094bf | 8897 | -- Deal with annoying case of largest negative number remainder minus |
b9daa96e AC |
8898 | -- one. Gigi may not handle this case correctly, because on some |
8899 | -- targets, the mod value is computed using a divide instruction | |
8900 | -- which gives an overflow trap for this case. | |
8901 | ||
8902 | -- It would be a bit more efficient to figure out which targets this | |
8903 | -- is really needed for, but in practice it is reasonable to do the | |
8904 | -- following special check in all cases, since it means we get a clearer | |
8905 | -- message, and also the overhead is minimal given that division is | |
8906 | -- expensive in any case. | |
70482933 | 8907 | |
685094bf RD |
8908 | -- In fact the check is quite easy, if the right operand is -1, then |
8909 | -- the remainder is always 0, and we can just ignore the left operand | |
8910 | -- completely in this case. | |
70482933 | 8911 | |
5d5e9775 AC |
8912 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); |
8913 | Lneg := (not OK) or else Lo < 0; | |
fbf5a39b | 8914 | |
5d5e9775 AC |
8915 | Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True); |
8916 | Rneg := (not OK) or else Lo < 0; | |
fbf5a39b | 8917 | |
5d5e9775 AC |
8918 | -- We won't mess with trying to find out if the left operand can really |
8919 | -- be the largest negative number (that's a pain in the case of private | |
8920 | -- types and this is really marginal). We will just assume that we need | |
8921 | -- the test if the left operand can be negative at all. | |
fbf5a39b | 8922 | |
5d5e9775 | 8923 | if Lneg and Rneg then |
70482933 | 8924 | Rewrite (N, |
9b16cb57 | 8925 | Make_If_Expression (Loc, |
70482933 RK |
8926 | Expressions => New_List ( |
8927 | Make_Op_Eq (Loc, | |
0d901290 | 8928 | Left_Opnd => Duplicate_Subexpr (Right), |
70482933 | 8929 | Right_Opnd => |
0d901290 | 8930 | Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))), |
70482933 | 8931 | |
fbf5a39b AC |
8932 | Unchecked_Convert_To (Typ, |
8933 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
8934 | |
8935 | Relocate_Node (N)))); | |
8936 | ||
8937 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
8938 | Analyze_And_Resolve (N, Typ); | |
8939 | end if; | |
8940 | end Expand_N_Op_Rem; | |
8941 | ||
8942 | ----------------------------- | |
8943 | -- Expand_N_Op_Rotate_Left -- | |
8944 | ----------------------------- | |
8945 | ||
8946 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
8947 | begin | |
8948 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
8949 | |
8950 | -- If we are in Modify_Tree_For_C mode, there is no rotate left in C, | |
8951 | -- so we rewrite in terms of logical shifts | |
8952 | ||
8953 | -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits) | |
8954 | ||
8955 | -- where Bits is the shift count mod Esize (the mod operation here | |
8956 | -- deals with ludicrous large shift counts, which are apparently OK). | |
8957 | ||
8958 | -- What about non-binary modulus ??? | |
8959 | ||
8960 | declare | |
8961 | Loc : constant Source_Ptr := Sloc (N); | |
8962 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
8963 | Typ : constant Entity_Id := Etype (N); | |
8964 | ||
8965 | begin | |
8966 | if Modify_Tree_For_C then | |
8967 | Rewrite (Right_Opnd (N), | |
8968 | Make_Op_Rem (Loc, | |
8969 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
8970 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
8971 | ||
8972 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
8973 | ||
8974 | Rewrite (N, | |
8975 | Make_Op_Or (Loc, | |
8976 | Left_Opnd => | |
8977 | Make_Op_Shift_Left (Loc, | |
8978 | Left_Opnd => Left_Opnd (N), | |
8979 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 8980 | |
5216b599 AC |
8981 | Right_Opnd => |
8982 | Make_Op_Shift_Right (Loc, | |
8983 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
8984 | Right_Opnd => | |
8985 | Make_Op_Subtract (Loc, | |
8986 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
8987 | Right_Opnd => | |
8988 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
8989 | ||
8990 | Analyze_And_Resolve (N, Typ); | |
8991 | end if; | |
8992 | end; | |
70482933 RK |
8993 | end Expand_N_Op_Rotate_Left; |
8994 | ||
8995 | ------------------------------ | |
8996 | -- Expand_N_Op_Rotate_Right -- | |
8997 | ------------------------------ | |
8998 | ||
8999 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
9000 | begin | |
9001 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
9002 | |
9003 | -- If we are in Modify_Tree_For_C mode, there is no rotate right in C, | |
9004 | -- so we rewrite in terms of logical shifts | |
9005 | ||
9006 | -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits) | |
9007 | ||
9008 | -- where Bits is the shift count mod Esize (the mod operation here | |
9009 | -- deals with ludicrous large shift counts, which are apparently OK). | |
9010 | ||
9011 | -- What about non-binary modulus ??? | |
9012 | ||
9013 | declare | |
9014 | Loc : constant Source_Ptr := Sloc (N); | |
9015 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
9016 | Typ : constant Entity_Id := Etype (N); | |
9017 | ||
9018 | begin | |
9019 | Rewrite (Right_Opnd (N), | |
9020 | Make_Op_Rem (Loc, | |
9021 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
9022 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
9023 | ||
9024 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
9025 | ||
9026 | if Modify_Tree_For_C then | |
9027 | Rewrite (N, | |
9028 | Make_Op_Or (Loc, | |
9029 | Left_Opnd => | |
9030 | Make_Op_Shift_Right (Loc, | |
9031 | Left_Opnd => Left_Opnd (N), | |
9032 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 9033 | |
5216b599 AC |
9034 | Right_Opnd => |
9035 | Make_Op_Shift_Left (Loc, | |
9036 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
9037 | Right_Opnd => | |
9038 | Make_Op_Subtract (Loc, | |
9039 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
9040 | Right_Opnd => | |
9041 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
9042 | ||
9043 | Analyze_And_Resolve (N, Typ); | |
9044 | end if; | |
9045 | end; | |
70482933 RK |
9046 | end Expand_N_Op_Rotate_Right; |
9047 | ||
9048 | ---------------------------- | |
9049 | -- Expand_N_Op_Shift_Left -- | |
9050 | ---------------------------- | |
9051 | ||
e09a5598 AC |
9052 | -- Note: nothing in this routine depends on left as opposed to right shifts |
9053 | -- so we share the routine for expanding shift right operations. | |
9054 | ||
70482933 RK |
9055 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is |
9056 | begin | |
9057 | Binary_Op_Validity_Checks (N); | |
e09a5598 AC |
9058 | |
9059 | -- If we are in Modify_Tree_For_C mode, then ensure that the right | |
9060 | -- operand is not greater than the word size (since that would not | |
9061 | -- be defined properly by the corresponding C shift operator). | |
9062 | ||
9063 | if Modify_Tree_For_C then | |
9064 | declare | |
9065 | Right : constant Node_Id := Right_Opnd (N); | |
9066 | Loc : constant Source_Ptr := Sloc (Right); | |
9067 | Typ : constant Entity_Id := Etype (N); | |
9068 | Siz : constant Uint := Esize (Typ); | |
9069 | Orig : Node_Id; | |
9070 | OK : Boolean; | |
9071 | Lo : Uint; | |
9072 | Hi : Uint; | |
9073 | ||
9074 | begin | |
9075 | if Compile_Time_Known_Value (Right) then | |
9076 | if Expr_Value (Right) >= Siz then | |
9077 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
9078 | Analyze_And_Resolve (N, Typ); | |
9079 | end if; | |
9080 | ||
9081 | -- Not compile time known, find range | |
9082 | ||
9083 | else | |
9084 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); | |
9085 | ||
9086 | -- Nothing to do if known to be OK range, otherwise expand | |
9087 | ||
9088 | if not OK or else Hi >= Siz then | |
9089 | ||
9090 | -- Prevent recursion on copy of shift node | |
9091 | ||
9092 | Orig := Relocate_Node (N); | |
9093 | Set_Analyzed (Orig); | |
9094 | ||
9095 | -- Now do the rewrite | |
9096 | ||
9097 | Rewrite (N, | |
9098 | Make_If_Expression (Loc, | |
9099 | Expressions => New_List ( | |
9100 | Make_Op_Ge (Loc, | |
9101 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
9102 | Right_Opnd => Make_Integer_Literal (Loc, Siz)), | |
9103 | Make_Integer_Literal (Loc, 0), | |
9104 | Orig))); | |
9105 | Analyze_And_Resolve (N, Typ); | |
9106 | end if; | |
9107 | end if; | |
9108 | end; | |
9109 | end if; | |
70482933 RK |
9110 | end Expand_N_Op_Shift_Left; |
9111 | ||
9112 | ----------------------------- | |
9113 | -- Expand_N_Op_Shift_Right -- | |
9114 | ----------------------------- | |
9115 | ||
9116 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
9117 | begin | |
e09a5598 AC |
9118 | -- Share shift left circuit |
9119 | ||
9120 | Expand_N_Op_Shift_Left (N); | |
70482933 RK |
9121 | end Expand_N_Op_Shift_Right; |
9122 | ||
9123 | ---------------------------------------- | |
9124 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
9125 | ---------------------------------------- | |
9126 | ||
9127 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
9128 | begin | |
9129 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
9130 | |
9131 | -- If we are in Modify_Tree_For_C mode, there is no shift right | |
9132 | -- arithmetic in C, so we rewrite in terms of logical shifts. | |
9133 | ||
9134 | -- Shift_Right (Num, Bits) or | |
9135 | -- (if Num >= Sign | |
9136 | -- then not (Shift_Right (Mask, bits)) | |
9137 | -- else 0) | |
9138 | ||
9139 | -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1) | |
9140 | ||
9141 | -- Note: in almost all C compilers it would work to just shift a | |
9142 | -- signed integer right, but it's undefined and we cannot rely on it. | |
9143 | ||
e09a5598 AC |
9144 | -- Note: the above works fine for shift counts greater than or equal |
9145 | -- to the word size, since in this case (not (Shift_Right (Mask, bits))) | |
9146 | -- generates all 1'bits. | |
9147 | ||
5216b599 AC |
9148 | -- What about non-binary modulus ??? |
9149 | ||
9150 | declare | |
9151 | Loc : constant Source_Ptr := Sloc (N); | |
9152 | Typ : constant Entity_Id := Etype (N); | |
9153 | Sign : constant Uint := 2 ** (Esize (Typ) - 1); | |
9154 | Mask : constant Uint := (2 ** Esize (Typ)) - 1; | |
9155 | Left : constant Node_Id := Left_Opnd (N); | |
9156 | Right : constant Node_Id := Right_Opnd (N); | |
9157 | Maskx : Node_Id; | |
9158 | ||
9159 | begin | |
9160 | if Modify_Tree_For_C then | |
9161 | ||
9162 | -- Here if not (Shift_Right (Mask, bits)) can be computed at | |
9163 | -- compile time as a single constant. | |
9164 | ||
9165 | if Compile_Time_Known_Value (Right) then | |
9166 | declare | |
9167 | Val : constant Uint := Expr_Value (Right); | |
9168 | ||
9169 | begin | |
9170 | if Val >= Esize (Typ) then | |
9171 | Maskx := Make_Integer_Literal (Loc, Mask); | |
9172 | ||
9173 | else | |
9174 | Maskx := | |
9175 | Make_Integer_Literal (Loc, | |
9176 | Intval => Mask - (Mask / (2 ** Expr_Value (Right)))); | |
9177 | end if; | |
9178 | end; | |
9179 | ||
9180 | else | |
9181 | Maskx := | |
9182 | Make_Op_Not (Loc, | |
9183 | Right_Opnd => | |
9184 | Make_Op_Shift_Right (Loc, | |
9185 | Left_Opnd => Make_Integer_Literal (Loc, Mask), | |
9186 | Right_Opnd => Duplicate_Subexpr_No_Checks (Right))); | |
9187 | end if; | |
9188 | ||
9189 | -- Now do the rewrite | |
9190 | ||
9191 | Rewrite (N, | |
9192 | Make_Op_Or (Loc, | |
9193 | Left_Opnd => | |
9194 | Make_Op_Shift_Right (Loc, | |
9195 | Left_Opnd => Left, | |
9196 | Right_Opnd => Right), | |
9197 | Right_Opnd => | |
9198 | Make_If_Expression (Loc, | |
9199 | Expressions => New_List ( | |
9200 | Make_Op_Ge (Loc, | |
9201 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
9202 | Right_Opnd => Make_Integer_Literal (Loc, Sign)), | |
9203 | Maskx, | |
9204 | Make_Integer_Literal (Loc, 0))))); | |
9205 | Analyze_And_Resolve (N, Typ); | |
9206 | end if; | |
9207 | end; | |
70482933 RK |
9208 | end Expand_N_Op_Shift_Right_Arithmetic; |
9209 | ||
9210 | -------------------------- | |
9211 | -- Expand_N_Op_Subtract -- | |
9212 | -------------------------- | |
9213 | ||
9214 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
9215 | Typ : constant Entity_Id := Etype (N); | |
9216 | ||
9217 | begin | |
9218 | Binary_Op_Validity_Checks (N); | |
9219 | ||
b6b5cca8 AC |
9220 | -- Check for MINIMIZED/ELIMINATED overflow mode |
9221 | ||
9222 | if Minimized_Eliminated_Overflow_Check (N) then | |
9223 | Apply_Arithmetic_Overflow_Check (N); | |
9224 | return; | |
9225 | end if; | |
9226 | ||
70482933 RK |
9227 | -- N - 0 = N for integer types |
9228 | ||
9229 | if Is_Integer_Type (Typ) | |
9230 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
9231 | and then Expr_Value (Right_Opnd (N)) = 0 | |
9232 | then | |
9233 | Rewrite (N, Left_Opnd (N)); | |
9234 | return; | |
9235 | end if; | |
9236 | ||
8fc789c8 | 9237 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 9238 | |
761f7dcb | 9239 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 | 9240 | Apply_Arithmetic_Overflow_Check (N); |
70482933 | 9241 | end if; |
dfaff97b RD |
9242 | |
9243 | -- Overflow checks for floating-point if -gnateF mode active | |
9244 | ||
9245 | Check_Float_Op_Overflow (N); | |
70482933 RK |
9246 | end Expand_N_Op_Subtract; |
9247 | ||
9248 | --------------------- | |
9249 | -- Expand_N_Op_Xor -- | |
9250 | --------------------- | |
9251 | ||
9252 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
9253 | Typ : constant Entity_Id := Etype (N); | |
9254 | ||
9255 | begin | |
9256 | Binary_Op_Validity_Checks (N); | |
9257 | ||
9258 | if Is_Array_Type (Etype (N)) then | |
9259 | Expand_Boolean_Operator (N); | |
9260 | ||
9261 | elsif Is_Boolean_Type (Etype (N)) then | |
9262 | Adjust_Condition (Left_Opnd (N)); | |
9263 | Adjust_Condition (Right_Opnd (N)); | |
9264 | Set_Etype (N, Standard_Boolean); | |
9265 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
9266 | |
9267 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
9268 | Expand_Intrinsic_Call (N, Entity (N)); | |
9269 | ||
70482933 RK |
9270 | end if; |
9271 | end Expand_N_Op_Xor; | |
9272 | ||
9273 | ---------------------- | |
9274 | -- Expand_N_Or_Else -- | |
9275 | ---------------------- | |
9276 | ||
5875f8d6 AC |
9277 | procedure Expand_N_Or_Else (N : Node_Id) |
9278 | renames Expand_Short_Circuit_Operator; | |
70482933 RK |
9279 | |
9280 | ----------------------------------- | |
9281 | -- Expand_N_Qualified_Expression -- | |
9282 | ----------------------------------- | |
9283 | ||
9284 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
9285 | Operand : constant Node_Id := Expression (N); | |
9286 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
9287 | ||
9288 | begin | |
f82944b7 JM |
9289 | -- Do validity check if validity checking operands |
9290 | ||
533369aa | 9291 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
9292 | Ensure_Valid (Operand); |
9293 | end if; | |
9294 | ||
9295 | -- Apply possible constraint check | |
9296 | ||
70482933 | 9297 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
d79e621a GD |
9298 | |
9299 | if Do_Range_Check (Operand) then | |
9300 | Set_Do_Range_Check (Operand, False); | |
9301 | Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed); | |
9302 | end if; | |
70482933 RK |
9303 | end Expand_N_Qualified_Expression; |
9304 | ||
a961aa79 AC |
9305 | ------------------------------------ |
9306 | -- Expand_N_Quantified_Expression -- | |
9307 | ------------------------------------ | |
9308 | ||
c0f136cd AC |
9309 | -- We expand: |
9310 | ||
9311 | -- for all X in range => Cond | |
a961aa79 | 9312 | |
c0f136cd | 9313 | -- into: |
a961aa79 | 9314 | |
c0f136cd AC |
9315 | -- T := True; |
9316 | -- for X in range loop | |
9317 | -- if not Cond then | |
9318 | -- T := False; | |
9319 | -- exit; | |
9320 | -- end if; | |
9321 | -- end loop; | |
90c63b09 | 9322 | |
36504e5f | 9323 | -- Similarly, an existentially quantified expression: |
90c63b09 | 9324 | |
c0f136cd | 9325 | -- for some X in range => Cond |
90c63b09 | 9326 | |
c0f136cd | 9327 | -- becomes: |
90c63b09 | 9328 | |
c0f136cd AC |
9329 | -- T := False; |
9330 | -- for X in range loop | |
9331 | -- if Cond then | |
9332 | -- T := True; | |
9333 | -- exit; | |
9334 | -- end if; | |
9335 | -- end loop; | |
90c63b09 | 9336 | |
c0f136cd AC |
9337 | -- In both cases, the iteration may be over a container in which case it is |
9338 | -- given by an iterator specification, not a loop parameter specification. | |
a961aa79 | 9339 | |
c0f136cd | 9340 | procedure Expand_N_Quantified_Expression (N : Node_Id) is |
804670f1 AC |
9341 | Actions : constant List_Id := New_List; |
9342 | For_All : constant Boolean := All_Present (N); | |
9343 | Iter_Spec : constant Node_Id := Iterator_Specification (N); | |
9344 | Loc : constant Source_Ptr := Sloc (N); | |
9345 | Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N); | |
9346 | Cond : Node_Id; | |
9347 | Flag : Entity_Id; | |
9348 | Scheme : Node_Id; | |
9349 | Stmts : List_Id; | |
c56a9ba4 | 9350 | |
a961aa79 | 9351 | begin |
804670f1 AC |
9352 | -- Create the declaration of the flag which tracks the status of the |
9353 | -- quantified expression. Generate: | |
011f9d5d | 9354 | |
804670f1 | 9355 | -- Flag : Boolean := (True | False); |
011f9d5d | 9356 | |
804670f1 | 9357 | Flag := Make_Temporary (Loc, 'T', N); |
011f9d5d | 9358 | |
804670f1 | 9359 | Append_To (Actions, |
90c63b09 | 9360 | Make_Object_Declaration (Loc, |
804670f1 | 9361 | Defining_Identifier => Flag, |
c0f136cd AC |
9362 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
9363 | Expression => | |
804670f1 AC |
9364 | New_Occurrence_Of (Boolean_Literals (For_All), Loc))); |
9365 | ||
9366 | -- Construct the circuitry which tracks the status of the quantified | |
9367 | -- expression. Generate: | |
9368 | ||
9369 | -- if [not] Cond then | |
9370 | -- Flag := (False | True); | |
9371 | -- exit; | |
9372 | -- end if; | |
a961aa79 | 9373 | |
c0f136cd | 9374 | Cond := Relocate_Node (Condition (N)); |
a961aa79 | 9375 | |
804670f1 | 9376 | if For_All then |
c0f136cd | 9377 | Cond := Make_Op_Not (Loc, Cond); |
a961aa79 AC |
9378 | end if; |
9379 | ||
804670f1 | 9380 | Stmts := New_List ( |
c0f136cd AC |
9381 | Make_Implicit_If_Statement (N, |
9382 | Condition => Cond, | |
9383 | Then_Statements => New_List ( | |
9384 | Make_Assignment_Statement (Loc, | |
804670f1 | 9385 | Name => New_Occurrence_Of (Flag, Loc), |
c0f136cd | 9386 | Expression => |
804670f1 AC |
9387 | New_Occurrence_Of (Boolean_Literals (not For_All), Loc)), |
9388 | Make_Exit_Statement (Loc)))); | |
9389 | ||
9390 | -- Build the loop equivalent of the quantified expression | |
c0f136cd | 9391 | |
804670f1 AC |
9392 | if Present (Iter_Spec) then |
9393 | Scheme := | |
011f9d5d | 9394 | Make_Iteration_Scheme (Loc, |
804670f1 | 9395 | Iterator_Specification => Iter_Spec); |
c56a9ba4 | 9396 | else |
804670f1 | 9397 | Scheme := |
011f9d5d | 9398 | Make_Iteration_Scheme (Loc, |
804670f1 | 9399 | Loop_Parameter_Specification => Loop_Spec); |
c56a9ba4 AC |
9400 | end if; |
9401 | ||
a961aa79 AC |
9402 | Append_To (Actions, |
9403 | Make_Loop_Statement (Loc, | |
804670f1 AC |
9404 | Iteration_Scheme => Scheme, |
9405 | Statements => Stmts, | |
c0f136cd | 9406 | End_Label => Empty)); |
a961aa79 | 9407 | |
804670f1 AC |
9408 | -- Transform the quantified expression |
9409 | ||
a961aa79 AC |
9410 | Rewrite (N, |
9411 | Make_Expression_With_Actions (Loc, | |
804670f1 | 9412 | Expression => New_Occurrence_Of (Flag, Loc), |
a961aa79 | 9413 | Actions => Actions)); |
a961aa79 AC |
9414 | Analyze_And_Resolve (N, Standard_Boolean); |
9415 | end Expand_N_Quantified_Expression; | |
9416 | ||
70482933 RK |
9417 | --------------------------------- |
9418 | -- Expand_N_Selected_Component -- | |
9419 | --------------------------------- | |
9420 | ||
70482933 RK |
9421 | procedure Expand_N_Selected_Component (N : Node_Id) is |
9422 | Loc : constant Source_Ptr := Sloc (N); | |
9423 | Par : constant Node_Id := Parent (N); | |
9424 | P : constant Node_Id := Prefix (N); | |
03eb6036 | 9425 | S : constant Node_Id := Selector_Name (N); |
fbf5a39b | 9426 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 9427 | Disc : Entity_Id; |
70482933 | 9428 | New_N : Node_Id; |
fbf5a39b | 9429 | Dcon : Elmt_Id; |
d606f1df | 9430 | Dval : Node_Id; |
70482933 RK |
9431 | |
9432 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
9433 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
9434 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
9435 | -- Don't we have a general routine that does this??? |
9436 | ||
53f29d4f AC |
9437 | function Is_Subtype_Declaration return Boolean; |
9438 | -- The replacement of a discriminant reference by its value is required | |
4317e442 AC |
9439 | -- if this is part of the initialization of an temporary generated by a |
9440 | -- change of representation. This shows up as the construction of a | |
53f29d4f | 9441 | -- discriminant constraint for a subtype declared at the same point as |
4317e442 AC |
9442 | -- the entity in the prefix of the selected component. We recognize this |
9443 | -- case when the context of the reference is: | |
9444 | -- subtype ST is T(Obj.D); | |
9445 | -- where the entity for Obj comes from source, and ST has the same sloc. | |
53f29d4f | 9446 | |
fbf5a39b AC |
9447 | ----------------------- |
9448 | -- In_Left_Hand_Side -- | |
9449 | ----------------------- | |
70482933 RK |
9450 | |
9451 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
9452 | begin | |
fbf5a39b | 9453 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
90c63b09 | 9454 | and then Comp = Name (Parent (Comp))) |
fbf5a39b | 9455 | or else (Present (Parent (Comp)) |
90c63b09 AC |
9456 | and then Nkind (Parent (Comp)) in N_Subexpr |
9457 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
9458 | end In_Left_Hand_Side; |
9459 | ||
53f29d4f AC |
9460 | ----------------------------- |
9461 | -- Is_Subtype_Declaration -- | |
9462 | ----------------------------- | |
9463 | ||
9464 | function Is_Subtype_Declaration return Boolean is | |
9465 | Par : constant Node_Id := Parent (N); | |
53f29d4f AC |
9466 | begin |
9467 | return | |
9468 | Nkind (Par) = N_Index_Or_Discriminant_Constraint | |
9469 | and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration | |
9470 | and then Comes_From_Source (Entity (Prefix (N))) | |
9471 | and then Sloc (Par) = Sloc (Entity (Prefix (N))); | |
9472 | end Is_Subtype_Declaration; | |
9473 | ||
fbf5a39b AC |
9474 | -- Start of processing for Expand_N_Selected_Component |
9475 | ||
70482933 | 9476 | begin |
fbf5a39b AC |
9477 | -- Insert explicit dereference if required |
9478 | ||
9479 | if Is_Access_Type (Ptyp) then | |
702d2020 AC |
9480 | |
9481 | -- First set prefix type to proper access type, in case it currently | |
9482 | -- has a private (non-access) view of this type. | |
9483 | ||
9484 | Set_Etype (P, Ptyp); | |
9485 | ||
fbf5a39b | 9486 | Insert_Explicit_Dereference (P); |
e6f69614 | 9487 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
9488 | |
9489 | if Ekind (Etype (P)) = E_Private_Subtype | |
9490 | and then Is_For_Access_Subtype (Etype (P)) | |
9491 | then | |
9492 | Set_Etype (P, Base_Type (Etype (P))); | |
9493 | end if; | |
9494 | ||
9495 | Ptyp := Etype (P); | |
9496 | end if; | |
9497 | ||
9498 | -- Deal with discriminant check required | |
9499 | ||
70482933 | 9500 | if Do_Discriminant_Check (N) then |
03eb6036 AC |
9501 | if Present (Discriminant_Checking_Func |
9502 | (Original_Record_Component (Entity (S)))) | |
9503 | then | |
9504 | -- Present the discriminant checking function to the backend, so | |
9505 | -- that it can inline the call to the function. | |
9506 | ||
9507 | Add_Inlined_Body | |
9508 | (Discriminant_Checking_Func | |
9509 | (Original_Record_Component (Entity (S)))); | |
70482933 | 9510 | |
03eb6036 | 9511 | -- Now reset the flag and generate the call |
70482933 | 9512 | |
03eb6036 AC |
9513 | Set_Do_Discriminant_Check (N, False); |
9514 | Generate_Discriminant_Check (N); | |
70482933 | 9515 | |
03eb6036 AC |
9516 | -- In the case of Unchecked_Union, no discriminant checking is |
9517 | -- actually performed. | |
70482933 | 9518 | |
03eb6036 AC |
9519 | else |
9520 | Set_Do_Discriminant_Check (N, False); | |
9521 | end if; | |
70482933 RK |
9522 | end if; |
9523 | ||
b4592168 GD |
9524 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
9525 | -- function, then additional actuals must be passed. | |
9526 | ||
0791fbe9 | 9527 | if Ada_Version >= Ada_2005 |
b4592168 GD |
9528 | and then Is_Build_In_Place_Function_Call (P) |
9529 | then | |
9530 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
9531 | end if; | |
9532 | ||
fbf5a39b AC |
9533 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
9534 | -- selected component with discriminants. This must be checked during | |
9535 | -- expansion, because during analysis the type of the selector is not | |
9536 | -- known at the point the prefix is analyzed. If the conversion is the | |
9537 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
9538 | |
9539 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
9540 | and then Has_Discriminants (Etype (N)) | |
9541 | and then not In_Left_Hand_Side (N) | |
9542 | then | |
9543 | Force_Evaluation (Prefix (N)); | |
9544 | end if; | |
9545 | ||
9546 | -- Remaining processing applies only if selector is a discriminant | |
9547 | ||
9548 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
9549 | ||
9550 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
9551 | -- we may be able to rewrite the expression with the actual value |
9552 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
9553 | |
9554 | if Is_Record_Type (Ptyp) | |
9555 | and then Has_Discriminants (Ptyp) | |
9556 | and then Is_Constrained (Ptyp) | |
70482933 | 9557 | then |
fbf5a39b | 9558 | -- Do this optimization for discrete types only, and not for |
a90bd866 | 9559 | -- access types (access discriminants get us into trouble). |
70482933 | 9560 | |
fbf5a39b AC |
9561 | if not Is_Discrete_Type (Etype (N)) then |
9562 | null; | |
9563 | ||
9564 | -- Don't do this on the left hand of an assignment statement. | |
0d901290 AC |
9565 | -- Normally one would think that references like this would not |
9566 | -- occur, but they do in generated code, and mean that we really | |
a90bd866 | 9567 | -- do want to assign the discriminant. |
fbf5a39b AC |
9568 | |
9569 | elsif Nkind (Par) = N_Assignment_Statement | |
9570 | and then Name (Par) = N | |
9571 | then | |
9572 | null; | |
9573 | ||
685094bf | 9574 | -- Don't do this optimization for the prefix of an attribute or |
e2534738 | 9575 | -- the name of an object renaming declaration since these are |
685094bf | 9576 | -- contexts where we do not want the value anyway. |
fbf5a39b AC |
9577 | |
9578 | elsif (Nkind (Par) = N_Attribute_Reference | |
533369aa | 9579 | and then Prefix (Par) = N) |
fbf5a39b AC |
9580 | or else Is_Renamed_Object (N) |
9581 | then | |
9582 | null; | |
9583 | ||
9584 | -- Don't do this optimization if we are within the code for a | |
9585 | -- discriminant check, since the whole point of such a check may | |
a90bd866 | 9586 | -- be to verify the condition on which the code below depends. |
fbf5a39b AC |
9587 | |
9588 | elsif Is_In_Discriminant_Check (N) then | |
9589 | null; | |
9590 | ||
9591 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
9592 | -- still one condition that inhibits the optimization below but |
9593 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
9594 | |
9595 | else | |
685094bf RD |
9596 | -- Loop through discriminants to find the matching discriminant |
9597 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
9598 | |
9599 | Disc := First_Discriminant (Ptyp); | |
9600 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
9601 | Discr_Loop : while Present (Dcon) loop | |
d606f1df | 9602 | Dval := Node (Dcon); |
fbf5a39b | 9603 | |
bd949ee2 RD |
9604 | -- Check if this is the matching discriminant and if the |
9605 | -- discriminant value is simple enough to make sense to | |
9606 | -- copy. We don't want to copy complex expressions, and | |
9607 | -- indeed to do so can cause trouble (before we put in | |
9608 | -- this guard, a discriminant expression containing an | |
e7d897b8 | 9609 | -- AND THEN was copied, causing problems for coverage |
c228a069 | 9610 | -- analysis tools). |
bd949ee2 | 9611 | |
53f29d4f AC |
9612 | -- However, if the reference is part of the initialization |
9613 | -- code generated for an object declaration, we must use | |
9614 | -- the discriminant value from the subtype constraint, | |
9615 | -- because the selected component may be a reference to the | |
9616 | -- object being initialized, whose discriminant is not yet | |
9617 | -- set. This only happens in complex cases involving changes | |
9618 | -- or representation. | |
9619 | ||
bd949ee2 RD |
9620 | if Disc = Entity (Selector_Name (N)) |
9621 | and then (Is_Entity_Name (Dval) | |
170b2989 AC |
9622 | or else Compile_Time_Known_Value (Dval) |
9623 | or else Is_Subtype_Declaration) | |
bd949ee2 | 9624 | then |
fbf5a39b AC |
9625 | -- Here we have the matching discriminant. Check for |
9626 | -- the case of a discriminant of a component that is | |
9627 | -- constrained by an outer discriminant, which cannot | |
9628 | -- be optimized away. | |
9629 | ||
d606f1df AC |
9630 | if Denotes_Discriminant |
9631 | (Dval, Check_Concurrent => True) | |
9632 | then | |
9633 | exit Discr_Loop; | |
9634 | ||
9635 | elsif Nkind (Original_Node (Dval)) = N_Selected_Component | |
9636 | and then | |
9637 | Denotes_Discriminant | |
9638 | (Selector_Name (Original_Node (Dval)), True) | |
9639 | then | |
9640 | exit Discr_Loop; | |
9641 | ||
9642 | -- Do not retrieve value if constraint is not static. It | |
9643 | -- is generally not useful, and the constraint may be a | |
9644 | -- rewritten outer discriminant in which case it is in | |
9645 | -- fact incorrect. | |
9646 | ||
9647 | elsif Is_Entity_Name (Dval) | |
d606f1df | 9648 | and then |
533369aa AC |
9649 | Nkind (Parent (Entity (Dval))) = N_Object_Declaration |
9650 | and then Present (Expression (Parent (Entity (Dval)))) | |
9651 | and then not | |
edab6088 | 9652 | Is_OK_Static_Expression |
d606f1df | 9653 | (Expression (Parent (Entity (Dval)))) |
fbf5a39b AC |
9654 | then |
9655 | exit Discr_Loop; | |
70482933 | 9656 | |
685094bf RD |
9657 | -- In the context of a case statement, the expression may |
9658 | -- have the base type of the discriminant, and we need to | |
9659 | -- preserve the constraint to avoid spurious errors on | |
9660 | -- missing cases. | |
70482933 | 9661 | |
fbf5a39b | 9662 | elsif Nkind (Parent (N)) = N_Case_Statement |
d606f1df | 9663 | and then Etype (Dval) /= Etype (Disc) |
70482933 RK |
9664 | then |
9665 | Rewrite (N, | |
9666 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
9667 | Subtype_Mark => |
9668 | New_Occurrence_Of (Etype (Disc), Loc), | |
9669 | Expression => | |
d606f1df | 9670 | New_Copy_Tree (Dval))); |
ffe9aba8 | 9671 | Analyze_And_Resolve (N, Etype (Disc)); |
fbf5a39b AC |
9672 | |
9673 | -- In case that comes out as a static expression, | |
9674 | -- reset it (a selected component is never static). | |
9675 | ||
9676 | Set_Is_Static_Expression (N, False); | |
9677 | return; | |
9678 | ||
9679 | -- Otherwise we can just copy the constraint, but the | |
a90bd866 | 9680 | -- result is certainly not static. In some cases the |
ffe9aba8 AC |
9681 | -- discriminant constraint has been analyzed in the |
9682 | -- context of the original subtype indication, but for | |
9683 | -- itypes the constraint might not have been analyzed | |
9684 | -- yet, and this must be done now. | |
fbf5a39b | 9685 | |
70482933 | 9686 | else |
d606f1df | 9687 | Rewrite (N, New_Copy_Tree (Dval)); |
ffe9aba8 | 9688 | Analyze_And_Resolve (N); |
fbf5a39b AC |
9689 | Set_Is_Static_Expression (N, False); |
9690 | return; | |
70482933 | 9691 | end if; |
70482933 RK |
9692 | end if; |
9693 | ||
fbf5a39b AC |
9694 | Next_Elmt (Dcon); |
9695 | Next_Discriminant (Disc); | |
9696 | end loop Discr_Loop; | |
70482933 | 9697 | |
fbf5a39b AC |
9698 | -- Note: the above loop should always find a matching |
9699 | -- discriminant, but if it does not, we just missed an | |
c228a069 AC |
9700 | -- optimization due to some glitch (perhaps a previous |
9701 | -- error), so ignore. | |
fbf5a39b AC |
9702 | |
9703 | end if; | |
70482933 RK |
9704 | end if; |
9705 | ||
9706 | -- The only remaining processing is in the case of a discriminant of | |
9707 | -- a concurrent object, where we rewrite the prefix to denote the | |
9708 | -- corresponding record type. If the type is derived and has renamed | |
9709 | -- discriminants, use corresponding discriminant, which is the one | |
9710 | -- that appears in the corresponding record. | |
9711 | ||
9712 | if not Is_Concurrent_Type (Ptyp) then | |
9713 | return; | |
9714 | end if; | |
9715 | ||
9716 | Disc := Entity (Selector_Name (N)); | |
9717 | ||
9718 | if Is_Derived_Type (Ptyp) | |
9719 | and then Present (Corresponding_Discriminant (Disc)) | |
9720 | then | |
9721 | Disc := Corresponding_Discriminant (Disc); | |
9722 | end if; | |
9723 | ||
9724 | New_N := | |
9725 | Make_Selected_Component (Loc, | |
9726 | Prefix => | |
9727 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
9728 | New_Copy_Tree (P)), | |
9729 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
9730 | ||
9731 | Rewrite (N, New_N); | |
9732 | Analyze (N); | |
9733 | end if; | |
5972791c | 9734 | |
73fe1679 | 9735 | -- Set Atomic_Sync_Required if necessary for atomic component |
5972791c | 9736 | |
73fe1679 AC |
9737 | if Nkind (N) = N_Selected_Component then |
9738 | declare | |
9739 | E : constant Entity_Id := Entity (Selector_Name (N)); | |
9740 | Set : Boolean; | |
9741 | ||
9742 | begin | |
9743 | -- If component is atomic, but type is not, setting depends on | |
9744 | -- disable/enable state for the component. | |
9745 | ||
9746 | if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then | |
9747 | Set := not Atomic_Synchronization_Disabled (E); | |
9748 | ||
9749 | -- If component is not atomic, but its type is atomic, setting | |
9750 | -- depends on disable/enable state for the type. | |
9751 | ||
9752 | elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
9753 | Set := not Atomic_Synchronization_Disabled (Etype (E)); | |
9754 | ||
9755 | -- If both component and type are atomic, we disable if either | |
9756 | -- component or its type have sync disabled. | |
9757 | ||
9758 | elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
9759 | Set := (not Atomic_Synchronization_Disabled (E)) | |
9760 | and then | |
9761 | (not Atomic_Synchronization_Disabled (Etype (E))); | |
9762 | ||
9763 | else | |
9764 | Set := False; | |
9765 | end if; | |
9766 | ||
9767 | -- Set flag if required | |
9768 | ||
9769 | if Set then | |
9770 | Activate_Atomic_Synchronization (N); | |
9771 | end if; | |
9772 | end; | |
5972791c | 9773 | end if; |
70482933 RK |
9774 | end Expand_N_Selected_Component; |
9775 | ||
9776 | -------------------- | |
9777 | -- Expand_N_Slice -- | |
9778 | -------------------- | |
9779 | ||
9780 | procedure Expand_N_Slice (N : Node_Id) is | |
5ff90f08 AC |
9781 | Loc : constant Source_Ptr := Sloc (N); |
9782 | Typ : constant Entity_Id := Etype (N); | |
fbf5a39b | 9783 | |
81a5b587 | 9784 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
9785 | -- Check whether the argument is an actual for a procedure call, in |
9786 | -- which case the expansion of a bit-packed slice is deferred until the | |
9787 | -- call itself is expanded. The reason this is required is that we might | |
9788 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
9789 | -- that copy out would be missed if we created a temporary here in | |
9790 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
9791 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
9792 | -- is harmless to defer expansion in the IN case, since the call | |
9793 | -- processing will still generate the appropriate copy in operation, | |
9794 | -- which will take care of the slice. | |
81a5b587 | 9795 | |
b01bf852 | 9796 | procedure Make_Temporary_For_Slice; |
685094bf RD |
9797 | -- Create a named variable for the value of the slice, in cases where |
9798 | -- the back-end cannot handle it properly, e.g. when packed types or | |
9799 | -- unaligned slices are involved. | |
fbf5a39b | 9800 | |
81a5b587 AC |
9801 | ------------------------- |
9802 | -- Is_Procedure_Actual -- | |
9803 | ------------------------- | |
9804 | ||
9805 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
9806 | Par : Node_Id := Parent (N); | |
08aa9a4a | 9807 | |
81a5b587 | 9808 | begin |
81a5b587 | 9809 | loop |
c6a60aa1 RD |
9810 | -- If our parent is a procedure call we can return |
9811 | ||
81a5b587 AC |
9812 | if Nkind (Par) = N_Procedure_Call_Statement then |
9813 | return True; | |
6b6fcd3e | 9814 | |
685094bf RD |
9815 | -- If our parent is a type conversion, keep climbing the tree, |
9816 | -- since a type conversion can be a procedure actual. Also keep | |
9817 | -- climbing if parameter association or a qualified expression, | |
9818 | -- since these are additional cases that do can appear on | |
9819 | -- procedure actuals. | |
6b6fcd3e | 9820 | |
303b4d58 AC |
9821 | elsif Nkind_In (Par, N_Type_Conversion, |
9822 | N_Parameter_Association, | |
9823 | N_Qualified_Expression) | |
c6a60aa1 | 9824 | then |
81a5b587 | 9825 | Par := Parent (Par); |
c6a60aa1 RD |
9826 | |
9827 | -- Any other case is not what we are looking for | |
9828 | ||
9829 | else | |
9830 | return False; | |
81a5b587 AC |
9831 | end if; |
9832 | end loop; | |
81a5b587 AC |
9833 | end Is_Procedure_Actual; |
9834 | ||
b01bf852 AC |
9835 | ------------------------------ |
9836 | -- Make_Temporary_For_Slice -- | |
9837 | ------------------------------ | |
fbf5a39b | 9838 | |
b01bf852 | 9839 | procedure Make_Temporary_For_Slice is |
b01bf852 | 9840 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
5ff90f08 | 9841 | Decl : Node_Id; |
13d923cc | 9842 | |
fbf5a39b AC |
9843 | begin |
9844 | Decl := | |
9845 | Make_Object_Declaration (Loc, | |
9846 | Defining_Identifier => Ent, | |
9847 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
9848 | ||
9849 | Set_No_Initialization (Decl); | |
9850 | ||
9851 | Insert_Actions (N, New_List ( | |
9852 | Decl, | |
9853 | Make_Assignment_Statement (Loc, | |
5ff90f08 | 9854 | Name => New_Occurrence_Of (Ent, Loc), |
fbf5a39b AC |
9855 | Expression => Relocate_Node (N)))); |
9856 | ||
9857 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
9858 | Analyze_And_Resolve (N, Typ); | |
b01bf852 | 9859 | end Make_Temporary_For_Slice; |
fbf5a39b | 9860 | |
5ff90f08 AC |
9861 | -- Local variables |
9862 | ||
800da977 AC |
9863 | Pref : constant Node_Id := Prefix (N); |
9864 | Pref_Typ : Entity_Id := Etype (Pref); | |
5ff90f08 | 9865 | |
fbf5a39b | 9866 | -- Start of processing for Expand_N_Slice |
70482933 RK |
9867 | |
9868 | begin | |
9869 | -- Special handling for access types | |
9870 | ||
5ff90f08 AC |
9871 | if Is_Access_Type (Pref_Typ) then |
9872 | Pref_Typ := Designated_Type (Pref_Typ); | |
70482933 | 9873 | |
5ff90f08 | 9874 | Rewrite (Pref, |
e6f69614 | 9875 | Make_Explicit_Dereference (Sloc (N), |
5ff90f08 | 9876 | Prefix => Relocate_Node (Pref))); |
70482933 | 9877 | |
5ff90f08 | 9878 | Analyze_And_Resolve (Pref, Pref_Typ); |
70482933 RK |
9879 | end if; |
9880 | ||
b4592168 GD |
9881 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
9882 | -- function, then additional actuals must be passed. | |
9883 | ||
0791fbe9 | 9884 | if Ada_Version >= Ada_2005 |
5ff90f08 | 9885 | and then Is_Build_In_Place_Function_Call (Pref) |
b4592168 | 9886 | then |
5ff90f08 | 9887 | Make_Build_In_Place_Call_In_Anonymous_Context (Pref); |
b4592168 GD |
9888 | end if; |
9889 | ||
70482933 RK |
9890 | -- The remaining case to be handled is packed slices. We can leave |
9891 | -- packed slices as they are in the following situations: | |
9892 | ||
9893 | -- 1. Right or left side of an assignment (we can handle this | |
9894 | -- situation correctly in the assignment statement expansion). | |
9895 | ||
685094bf RD |
9896 | -- 2. Prefix of indexed component (the slide is optimized away in this |
9897 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 9898 | |
685094bf RD |
9899 | -- 3. Object renaming declaration, since we want the name of the |
9900 | -- slice, not the value. | |
70482933 | 9901 | |
685094bf RD |
9902 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
9903 | -- be required, and this is handled in the expansion of call | |
9904 | -- itself. | |
70482933 | 9905 | |
685094bf RD |
9906 | -- 5. Prefix of an address attribute (this is an error which is caught |
9907 | -- elsewhere, and the expansion would interfere with generating the | |
9908 | -- error message). | |
70482933 | 9909 | |
81a5b587 | 9910 | if not Is_Packed (Typ) then |
08aa9a4a | 9911 | |
685094bf RD |
9912 | -- Apply transformation for actuals of a function call, where |
9913 | -- Expand_Actuals is not used. | |
81a5b587 AC |
9914 | |
9915 | if Nkind (Parent (N)) = N_Function_Call | |
9916 | and then Is_Possibly_Unaligned_Slice (N) | |
9917 | then | |
b01bf852 | 9918 | Make_Temporary_For_Slice; |
81a5b587 AC |
9919 | end if; |
9920 | ||
9921 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
9922 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
533369aa | 9923 | and then Parent (N) = Name (Parent (Parent (N)))) |
70482933 | 9924 | then |
81a5b587 | 9925 | return; |
70482933 | 9926 | |
81a5b587 AC |
9927 | elsif Nkind (Parent (N)) = N_Indexed_Component |
9928 | or else Is_Renamed_Object (N) | |
9929 | or else Is_Procedure_Actual (N) | |
9930 | then | |
9931 | return; | |
70482933 | 9932 | |
91b1417d AC |
9933 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
9934 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 9935 | then |
81a5b587 AC |
9936 | return; |
9937 | ||
9938 | else | |
b01bf852 | 9939 | Make_Temporary_For_Slice; |
70482933 RK |
9940 | end if; |
9941 | end Expand_N_Slice; | |
9942 | ||
9943 | ------------------------------ | |
9944 | -- Expand_N_Type_Conversion -- | |
9945 | ------------------------------ | |
9946 | ||
9947 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
9948 | Loc : constant Source_Ptr := Sloc (N); | |
9949 | Operand : constant Node_Id := Expression (N); | |
9950 | Target_Type : constant Entity_Id := Etype (N); | |
9951 | Operand_Type : Entity_Id := Etype (Operand); | |
9952 | ||
9953 | procedure Handle_Changed_Representation; | |
685094bf RD |
9954 | -- This is called in the case of record and array type conversions to |
9955 | -- see if there is a change of representation to be handled. Change of | |
9956 | -- representation is actually handled at the assignment statement level, | |
9957 | -- and what this procedure does is rewrite node N conversion as an | |
9958 | -- assignment to temporary. If there is no change of representation, | |
9959 | -- then the conversion node is unchanged. | |
70482933 | 9960 | |
426908f8 RD |
9961 | procedure Raise_Accessibility_Error; |
9962 | -- Called when we know that an accessibility check will fail. Rewrites | |
9963 | -- node N to an appropriate raise statement and outputs warning msgs. | |
9964 | -- The Etype of the raise node is set to Target_Type. | |
9965 | ||
70482933 RK |
9966 | procedure Real_Range_Check; |
9967 | -- Handles generation of range check for real target value | |
9968 | ||
d15f9422 AC |
9969 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean; |
9970 | -- True iff Present (Effective_Extra_Accessibility (Id)) successfully | |
9971 | -- evaluates to True. | |
9972 | ||
70482933 RK |
9973 | ----------------------------------- |
9974 | -- Handle_Changed_Representation -- | |
9975 | ----------------------------------- | |
9976 | ||
9977 | procedure Handle_Changed_Representation is | |
9978 | Temp : Entity_Id; | |
9979 | Decl : Node_Id; | |
9980 | Odef : Node_Id; | |
9981 | Disc : Node_Id; | |
9982 | N_Ix : Node_Id; | |
9983 | Cons : List_Id; | |
9984 | ||
9985 | begin | |
f82944b7 | 9986 | -- Nothing else to do if no change of representation |
70482933 RK |
9987 | |
9988 | if Same_Representation (Operand_Type, Target_Type) then | |
9989 | return; | |
9990 | ||
9991 | -- The real change of representation work is done by the assignment | |
9992 | -- statement processing. So if this type conversion is appearing as | |
9993 | -- the expression of an assignment statement, nothing needs to be | |
9994 | -- done to the conversion. | |
9995 | ||
9996 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
9997 | return; | |
9998 | ||
9999 | -- Otherwise we need to generate a temporary variable, and do the | |
10000 | -- change of representation assignment into that temporary variable. | |
10001 | -- The conversion is then replaced by a reference to this variable. | |
10002 | ||
10003 | else | |
10004 | Cons := No_List; | |
10005 | ||
685094bf RD |
10006 | -- If type is unconstrained we have to add a constraint, copied |
10007 | -- from the actual value of the left hand side. | |
70482933 RK |
10008 | |
10009 | if not Is_Constrained (Target_Type) then | |
10010 | if Has_Discriminants (Operand_Type) then | |
10011 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
10012 | |
10013 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
10014 | Disc := First_Stored_Discriminant (Operand_Type); | |
10015 | end if; | |
10016 | ||
70482933 RK |
10017 | Cons := New_List; |
10018 | while Present (Disc) loop | |
10019 | Append_To (Cons, | |
10020 | Make_Selected_Component (Loc, | |
7675ad4f AC |
10021 | Prefix => |
10022 | Duplicate_Subexpr_Move_Checks (Operand), | |
70482933 RK |
10023 | Selector_Name => |
10024 | Make_Identifier (Loc, Chars (Disc)))); | |
10025 | Next_Discriminant (Disc); | |
10026 | end loop; | |
10027 | ||
10028 | elsif Is_Array_Type (Operand_Type) then | |
10029 | N_Ix := First_Index (Target_Type); | |
10030 | Cons := New_List; | |
10031 | ||
10032 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
10033 | ||
10034 | -- We convert the bounds explicitly. We use an unchecked | |
10035 | -- conversion because bounds checks are done elsewhere. | |
10036 | ||
10037 | Append_To (Cons, | |
10038 | Make_Range (Loc, | |
10039 | Low_Bound => | |
10040 | Unchecked_Convert_To (Etype (N_Ix), | |
10041 | Make_Attribute_Reference (Loc, | |
10042 | Prefix => | |
fbf5a39b | 10043 | Duplicate_Subexpr_No_Checks |
70482933 RK |
10044 | (Operand, Name_Req => True), |
10045 | Attribute_Name => Name_First, | |
10046 | Expressions => New_List ( | |
10047 | Make_Integer_Literal (Loc, J)))), | |
10048 | ||
10049 | High_Bound => | |
10050 | Unchecked_Convert_To (Etype (N_Ix), | |
10051 | Make_Attribute_Reference (Loc, | |
10052 | Prefix => | |
fbf5a39b | 10053 | Duplicate_Subexpr_No_Checks |
70482933 RK |
10054 | (Operand, Name_Req => True), |
10055 | Attribute_Name => Name_Last, | |
10056 | Expressions => New_List ( | |
10057 | Make_Integer_Literal (Loc, J)))))); | |
10058 | ||
10059 | Next_Index (N_Ix); | |
10060 | end loop; | |
10061 | end if; | |
10062 | end if; | |
10063 | ||
10064 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
10065 | ||
10066 | if Present (Cons) then | |
10067 | Odef := | |
10068 | Make_Subtype_Indication (Loc, | |
10069 | Subtype_Mark => Odef, | |
10070 | Constraint => | |
10071 | Make_Index_Or_Discriminant_Constraint (Loc, | |
10072 | Constraints => Cons)); | |
10073 | end if; | |
10074 | ||
191fcb3a | 10075 | Temp := Make_Temporary (Loc, 'C'); |
70482933 RK |
10076 | Decl := |
10077 | Make_Object_Declaration (Loc, | |
10078 | Defining_Identifier => Temp, | |
10079 | Object_Definition => Odef); | |
10080 | ||
10081 | Set_No_Initialization (Decl, True); | |
10082 | ||
10083 | -- Insert required actions. It is essential to suppress checks | |
10084 | -- since we have suppressed default initialization, which means | |
10085 | -- that the variable we create may have no discriminants. | |
10086 | ||
10087 | Insert_Actions (N, | |
10088 | New_List ( | |
10089 | Decl, | |
10090 | Make_Assignment_Statement (Loc, | |
10091 | Name => New_Occurrence_Of (Temp, Loc), | |
10092 | Expression => Relocate_Node (N))), | |
10093 | Suppress => All_Checks); | |
10094 | ||
10095 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
10096 | return; | |
10097 | end if; | |
10098 | end Handle_Changed_Representation; | |
10099 | ||
426908f8 RD |
10100 | ------------------------------- |
10101 | -- Raise_Accessibility_Error -- | |
10102 | ------------------------------- | |
10103 | ||
10104 | procedure Raise_Accessibility_Error is | |
10105 | begin | |
43417b90 | 10106 | Error_Msg_Warn := SPARK_Mode /= On; |
426908f8 RD |
10107 | Rewrite (N, |
10108 | Make_Raise_Program_Error (Sloc (N), | |
10109 | Reason => PE_Accessibility_Check_Failed)); | |
10110 | Set_Etype (N, Target_Type); | |
10111 | ||
4a28b181 AC |
10112 | Error_Msg_N ("<<accessibility check failure", N); |
10113 | Error_Msg_NE ("\<<& [", N, Standard_Program_Error); | |
426908f8 RD |
10114 | end Raise_Accessibility_Error; |
10115 | ||
70482933 RK |
10116 | ---------------------- |
10117 | -- Real_Range_Check -- | |
10118 | ---------------------- | |
10119 | ||
685094bf RD |
10120 | -- Case of conversions to floating-point or fixed-point. If range checks |
10121 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
10122 | |
10123 | -- typ (x) | |
10124 | ||
10125 | -- to | |
10126 | ||
10127 | -- Tnn : typ'Base := typ'Base (x); | |
10128 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
10129 | -- Tnn | |
10130 | ||
685094bf RD |
10131 | -- This is necessary when there is a conversion of integer to float or |
10132 | -- to fixed-point to ensure that the correct checks are made. It is not | |
10133 | -- necessary for float to float where it is enough to simply set the | |
10134 | -- Do_Range_Check flag. | |
fbf5a39b | 10135 | |
70482933 RK |
10136 | procedure Real_Range_Check is |
10137 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
10138 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
10139 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 10140 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
10141 | Conv : Node_Id; |
10142 | Tnn : Entity_Id; | |
10143 | ||
10144 | begin | |
10145 | -- Nothing to do if conversion was rewritten | |
10146 | ||
10147 | if Nkind (N) /= N_Type_Conversion then | |
10148 | return; | |
10149 | end if; | |
10150 | ||
685094bf RD |
10151 | -- Nothing to do if range checks suppressed, or target has the same |
10152 | -- range as the base type (or is the base type). | |
70482933 RK |
10153 | |
10154 | if Range_Checks_Suppressed (Target_Type) | |
533369aa | 10155 | or else (Lo = Type_Low_Bound (Btyp) |
70482933 RK |
10156 | and then |
10157 | Hi = Type_High_Bound (Btyp)) | |
10158 | then | |
10159 | return; | |
10160 | end if; | |
10161 | ||
685094bf RD |
10162 | -- Nothing to do if expression is an entity on which checks have been |
10163 | -- suppressed. | |
70482933 | 10164 | |
fbf5a39b AC |
10165 | if Is_Entity_Name (Operand) |
10166 | and then Range_Checks_Suppressed (Entity (Operand)) | |
10167 | then | |
10168 | return; | |
10169 | end if; | |
10170 | ||
685094bf RD |
10171 | -- Nothing to do if bounds are all static and we can tell that the |
10172 | -- expression is within the bounds of the target. Note that if the | |
10173 | -- operand is of an unconstrained floating-point type, then we do | |
10174 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
10175 | |
10176 | declare | |
f02b8bb8 RD |
10177 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); |
10178 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
fbf5a39b AC |
10179 | |
10180 | begin | |
10181 | if (not Is_Floating_Point_Type (Xtyp) | |
10182 | or else Is_Constrained (Xtyp)) | |
10183 | and then Compile_Time_Known_Value (S_Lo) | |
10184 | and then Compile_Time_Known_Value (S_Hi) | |
10185 | and then Compile_Time_Known_Value (Hi) | |
10186 | and then Compile_Time_Known_Value (Lo) | |
10187 | then | |
10188 | declare | |
10189 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
10190 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
10191 | S_Lov : Ureal; | |
10192 | S_Hiv : Ureal; | |
10193 | ||
10194 | begin | |
10195 | if Is_Real_Type (Xtyp) then | |
10196 | S_Lov := Expr_Value_R (S_Lo); | |
10197 | S_Hiv := Expr_Value_R (S_Hi); | |
10198 | else | |
10199 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
10200 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
10201 | end if; | |
10202 | ||
10203 | if D_Hiv > D_Lov | |
10204 | and then S_Lov >= D_Lov | |
10205 | and then S_Hiv <= D_Hiv | |
10206 | then | |
8b034336 AC |
10207 | -- Unset the range check flag on the current value of |
10208 | -- Expression (N), since the captured Operand may have | |
10209 | -- been rewritten (such as for the case of a conversion | |
10210 | -- to a fixed-point type). | |
10211 | ||
10212 | Set_Do_Range_Check (Expression (N), False); | |
10213 | ||
fbf5a39b AC |
10214 | return; |
10215 | end if; | |
10216 | end; | |
10217 | end if; | |
10218 | end; | |
10219 | ||
10220 | -- For float to float conversions, we are done | |
10221 | ||
10222 | if Is_Floating_Point_Type (Xtyp) | |
10223 | and then | |
10224 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
10225 | then |
10226 | return; | |
10227 | end if; | |
10228 | ||
fbf5a39b | 10229 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
10230 | |
10231 | Conv := Relocate_Node (N); | |
eaa826f8 | 10232 | Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); |
70482933 RK |
10233 | Set_Etype (Conv, Btyp); |
10234 | ||
f02b8bb8 RD |
10235 | -- Enable overflow except for case of integer to float conversions, |
10236 | -- where it is never required, since we can never have overflow in | |
10237 | -- this case. | |
70482933 | 10238 | |
fbf5a39b AC |
10239 | if not Is_Integer_Type (Etype (Operand)) then |
10240 | Enable_Overflow_Check (Conv); | |
70482933 RK |
10241 | end if; |
10242 | ||
191fcb3a | 10243 | Tnn := Make_Temporary (Loc, 'T', Conv); |
70482933 RK |
10244 | |
10245 | Insert_Actions (N, New_List ( | |
10246 | Make_Object_Declaration (Loc, | |
10247 | Defining_Identifier => Tnn, | |
10248 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
0ac2a660 AC |
10249 | Constant_Present => True, |
10250 | Expression => Conv), | |
70482933 RK |
10251 | |
10252 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
10253 | Condition => |
10254 | Make_Or_Else (Loc, | |
10255 | Left_Opnd => | |
10256 | Make_Op_Lt (Loc, | |
10257 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
10258 | Right_Opnd => | |
10259 | Make_Attribute_Reference (Loc, | |
10260 | Attribute_Name => Name_First, | |
10261 | Prefix => | |
10262 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 10263 | |
07fc65c4 GB |
10264 | Right_Opnd => |
10265 | Make_Op_Gt (Loc, | |
10266 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
10267 | Right_Opnd => | |
10268 | Make_Attribute_Reference (Loc, | |
10269 | Attribute_Name => Name_Last, | |
10270 | Prefix => | |
10271 | New_Occurrence_Of (Target_Type, Loc)))), | |
10272 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
10273 | |
10274 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
10275 | Analyze_And_Resolve (N, Btyp); | |
10276 | end Real_Range_Check; | |
10277 | ||
d15f9422 AC |
10278 | ----------------------------- |
10279 | -- Has_Extra_Accessibility -- | |
10280 | ----------------------------- | |
10281 | ||
10282 | -- Returns true for a formal of an anonymous access type or for | |
10283 | -- an Ada 2012-style stand-alone object of an anonymous access type. | |
10284 | ||
10285 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is | |
10286 | begin | |
10287 | if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then | |
10288 | return Present (Effective_Extra_Accessibility (Id)); | |
10289 | else | |
10290 | return False; | |
10291 | end if; | |
10292 | end Has_Extra_Accessibility; | |
10293 | ||
70482933 RK |
10294 | -- Start of processing for Expand_N_Type_Conversion |
10295 | ||
10296 | begin | |
83851b23 | 10297 | -- First remove check marks put by the semantic analysis on the type |
b2502161 AC |
10298 | -- conversion between array types. We need these checks, and they will |
10299 | -- be generated by this expansion routine, but we do not depend on these | |
10300 | -- flags being set, and since we do intend to expand the checks in the | |
10301 | -- front end, we don't want them on the tree passed to the back end. | |
83851b23 AC |
10302 | |
10303 | if Is_Array_Type (Target_Type) then | |
10304 | if Is_Constrained (Target_Type) then | |
10305 | Set_Do_Length_Check (N, False); | |
10306 | else | |
10307 | Set_Do_Range_Check (Operand, False); | |
10308 | end if; | |
10309 | end if; | |
10310 | ||
685094bf | 10311 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 AC |
10312 | -- the conversion completely, it is useless, except that it may carry |
10313 | -- an Assignment_OK attribute, which must be propagated to the operand. | |
70482933 RK |
10314 | |
10315 | if Operand_Type = Target_Type then | |
7b00e31d AC |
10316 | if Assignment_OK (N) then |
10317 | Set_Assignment_OK (Operand); | |
10318 | end if; | |
10319 | ||
fbf5a39b | 10320 | Rewrite (N, Relocate_Node (Operand)); |
e606088a | 10321 | goto Done; |
70482933 RK |
10322 | end if; |
10323 | ||
685094bf RD |
10324 | -- Nothing to do if this is the second argument of read. This is a |
10325 | -- "backwards" conversion that will be handled by the specialized code | |
10326 | -- in attribute processing. | |
70482933 RK |
10327 | |
10328 | if Nkind (Parent (N)) = N_Attribute_Reference | |
10329 | and then Attribute_Name (Parent (N)) = Name_Read | |
10330 | and then Next (First (Expressions (Parent (N)))) = N | |
10331 | then | |
e606088a AC |
10332 | goto Done; |
10333 | end if; | |
10334 | ||
10335 | -- Check for case of converting to a type that has an invariant | |
10336 | -- associated with it. This required an invariant check. We convert | |
10337 | ||
10338 | -- typ (expr) | |
10339 | ||
10340 | -- into | |
10341 | ||
10342 | -- do invariant_check (typ (expr)) in typ (expr); | |
10343 | ||
10344 | -- using Duplicate_Subexpr to avoid multiple side effects | |
10345 | ||
10346 | -- Note: the Comes_From_Source check, and then the resetting of this | |
10347 | -- flag prevents what would otherwise be an infinite recursion. | |
10348 | ||
fd0ff1cf RD |
10349 | if Has_Invariants (Target_Type) |
10350 | and then Present (Invariant_Procedure (Target_Type)) | |
e606088a AC |
10351 | and then Comes_From_Source (N) |
10352 | then | |
10353 | Set_Comes_From_Source (N, False); | |
10354 | Rewrite (N, | |
10355 | Make_Expression_With_Actions (Loc, | |
10356 | Actions => New_List ( | |
10357 | Make_Invariant_Call (Duplicate_Subexpr (N))), | |
10358 | Expression => Duplicate_Subexpr_No_Checks (N))); | |
10359 | Analyze_And_Resolve (N, Target_Type); | |
10360 | goto Done; | |
70482933 RK |
10361 | end if; |
10362 | ||
10363 | -- Here if we may need to expand conversion | |
10364 | ||
eaa826f8 RD |
10365 | -- If the operand of the type conversion is an arithmetic operation on |
10366 | -- signed integers, and the based type of the signed integer type in | |
10367 | -- question is smaller than Standard.Integer, we promote both of the | |
10368 | -- operands to type Integer. | |
10369 | ||
10370 | -- For example, if we have | |
10371 | ||
10372 | -- target-type (opnd1 + opnd2) | |
10373 | ||
10374 | -- and opnd1 and opnd2 are of type short integer, then we rewrite | |
10375 | -- this as: | |
10376 | ||
10377 | -- target-type (integer(opnd1) + integer(opnd2)) | |
10378 | ||
10379 | -- We do this because we are always allowed to compute in a larger type | |
10380 | -- if we do the right thing with the result, and in this case we are | |
10381 | -- going to do a conversion which will do an appropriate check to make | |
10382 | -- sure that things are in range of the target type in any case. This | |
10383 | -- avoids some unnecessary intermediate overflows. | |
10384 | ||
dfcfdc0a AC |
10385 | -- We might consider a similar transformation in the case where the |
10386 | -- target is a real type or a 64-bit integer type, and the operand | |
10387 | -- is an arithmetic operation using a 32-bit integer type. However, | |
10388 | -- we do not bother with this case, because it could cause significant | |
308e6f3a | 10389 | -- inefficiencies on 32-bit machines. On a 64-bit machine it would be |
dfcfdc0a AC |
10390 | -- much cheaper, but we don't want different behavior on 32-bit and |
10391 | -- 64-bit machines. Note that the exclusion of the 64-bit case also | |
10392 | -- handles the configurable run-time cases where 64-bit arithmetic | |
10393 | -- may simply be unavailable. | |
eaa826f8 RD |
10394 | |
10395 | -- Note: this circuit is partially redundant with respect to the circuit | |
10396 | -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in | |
10397 | -- the processing here. Also we still need the Checks circuit, since we | |
10398 | -- have to be sure not to generate junk overflow checks in the first | |
a90bd866 | 10399 | -- place, since it would be trick to remove them here. |
eaa826f8 | 10400 | |
fdfcc663 | 10401 | if Integer_Promotion_Possible (N) then |
eaa826f8 | 10402 | |
fdfcc663 | 10403 | -- All conditions met, go ahead with transformation |
eaa826f8 | 10404 | |
fdfcc663 AC |
10405 | declare |
10406 | Opnd : Node_Id; | |
10407 | L, R : Node_Id; | |
dfcfdc0a | 10408 | |
fdfcc663 AC |
10409 | begin |
10410 | R := | |
10411 | Make_Type_Conversion (Loc, | |
e4494292 | 10412 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 | 10413 | Expression => Relocate_Node (Right_Opnd (Operand))); |
eaa826f8 | 10414 | |
5f3f175d AC |
10415 | Opnd := New_Op_Node (Nkind (Operand), Loc); |
10416 | Set_Right_Opnd (Opnd, R); | |
eaa826f8 | 10417 | |
5f3f175d | 10418 | if Nkind (Operand) in N_Binary_Op then |
fdfcc663 | 10419 | L := |
eaa826f8 | 10420 | Make_Type_Conversion (Loc, |
e4494292 | 10421 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 AC |
10422 | Expression => Relocate_Node (Left_Opnd (Operand))); |
10423 | ||
5f3f175d AC |
10424 | Set_Left_Opnd (Opnd, L); |
10425 | end if; | |
eaa826f8 | 10426 | |
5f3f175d AC |
10427 | Rewrite (N, |
10428 | Make_Type_Conversion (Loc, | |
10429 | Subtype_Mark => Relocate_Node (Subtype_Mark (N)), | |
10430 | Expression => Opnd)); | |
dfcfdc0a | 10431 | |
5f3f175d | 10432 | Analyze_And_Resolve (N, Target_Type); |
e606088a | 10433 | goto Done; |
fdfcc663 AC |
10434 | end; |
10435 | end if; | |
eaa826f8 | 10436 | |
f82944b7 JM |
10437 | -- Do validity check if validity checking operands |
10438 | ||
533369aa | 10439 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
10440 | Ensure_Valid (Operand); |
10441 | end if; | |
10442 | ||
70482933 RK |
10443 | -- Special case of converting from non-standard boolean type |
10444 | ||
10445 | if Is_Boolean_Type (Operand_Type) | |
10446 | and then (Nonzero_Is_True (Operand_Type)) | |
10447 | then | |
10448 | Adjust_Condition (Operand); | |
10449 | Set_Etype (Operand, Standard_Boolean); | |
10450 | Operand_Type := Standard_Boolean; | |
10451 | end if; | |
10452 | ||
10453 | -- Case of converting to an access type | |
10454 | ||
10455 | if Is_Access_Type (Target_Type) then | |
10456 | ||
d766cee3 RD |
10457 | -- Apply an accessibility check when the conversion operand is an |
10458 | -- access parameter (or a renaming thereof), unless conversion was | |
e84e11ba GD |
10459 | -- expanded from an Unchecked_ or Unrestricted_Access attribute. |
10460 | -- Note that other checks may still need to be applied below (such | |
10461 | -- as tagged type checks). | |
70482933 RK |
10462 | |
10463 | if Is_Entity_Name (Operand) | |
d15f9422 | 10464 | and then Has_Extra_Accessibility (Entity (Operand)) |
70482933 | 10465 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type |
d766cee3 RD |
10466 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
10467 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 10468 | then |
e84e11ba GD |
10469 | Apply_Accessibility_Check |
10470 | (Operand, Target_Type, Insert_Node => Operand); | |
70482933 | 10471 | |
e84e11ba | 10472 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
10473 | -- level of the target type, then force Program_Error. Note that this |
10474 | -- can only occur for cases where the attribute is within the body of | |
6c56d9b8 AC |
10475 | -- an instantiation, otherwise the conversion will already have been |
10476 | -- rejected as illegal. | |
10477 | ||
10478 | -- Note: warnings are issued by the analyzer for the instance cases | |
70482933 RK |
10479 | |
10480 | elsif In_Instance_Body | |
6c56d9b8 AC |
10481 | |
10482 | -- The case where the target type is an anonymous access type of | |
10483 | -- a discriminant is excluded, because the level of such a type | |
10484 | -- depends on the context and currently the level returned for such | |
10485 | -- types is zero, resulting in warnings about about check failures | |
10486 | -- in certain legal cases involving class-wide interfaces as the | |
10487 | -- designated type (some cases, such as return statements, are | |
10488 | -- checked at run time, but not clear if these are handled right | |
10489 | -- in general, see 3.10.2(12/2-12.5/3) ???). | |
10490 | ||
ad5edba5 AC |
10491 | and then |
10492 | not (Ekind (Target_Type) = E_Anonymous_Access_Type | |
10493 | and then Present (Associated_Node_For_Itype (Target_Type)) | |
10494 | and then Nkind (Associated_Node_For_Itype (Target_Type)) = | |
10495 | N_Discriminant_Specification) | |
10496 | and then | |
10497 | Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type) | |
70482933 | 10498 | then |
426908f8 | 10499 | Raise_Accessibility_Error; |
70482933 | 10500 | |
685094bf RD |
10501 | -- When the operand is a selected access discriminant the check needs |
10502 | -- to be made against the level of the object denoted by the prefix | |
10503 | -- of the selected name. Force Program_Error for this case as well | |
10504 | -- (this accessibility violation can only happen if within the body | |
10505 | -- of an instantiation). | |
70482933 RK |
10506 | |
10507 | elsif In_Instance_Body | |
10508 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
10509 | and then Nkind (Operand) = N_Selected_Component | |
10510 | and then Object_Access_Level (Operand) > | |
10511 | Type_Access_Level (Target_Type) | |
10512 | then | |
426908f8 | 10513 | Raise_Accessibility_Error; |
e606088a | 10514 | goto Done; |
70482933 RK |
10515 | end if; |
10516 | end if; | |
10517 | ||
10518 | -- Case of conversions of tagged types and access to tagged types | |
10519 | ||
685094bf RD |
10520 | -- When needed, that is to say when the expression is class-wide, Add |
10521 | -- runtime a tag check for (strict) downward conversion by using the | |
10522 | -- membership test, generating: | |
70482933 RK |
10523 | |
10524 | -- [constraint_error when Operand not in Target_Type'Class] | |
10525 | ||
10526 | -- or in the access type case | |
10527 | ||
10528 | -- [constraint_error | |
10529 | -- when Operand /= null | |
10530 | -- and then Operand.all not in | |
10531 | -- Designated_Type (Target_Type)'Class] | |
10532 | ||
10533 | if (Is_Access_Type (Target_Type) | |
10534 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
10535 | or else Is_Tagged_Type (Target_Type) | |
10536 | then | |
685094bf RD |
10537 | -- Do not do any expansion in the access type case if the parent is a |
10538 | -- renaming, since this is an error situation which will be caught by | |
10539 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 | 10540 | |
e7e4d230 | 10541 | if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then |
e606088a | 10542 | goto Done; |
70482933 RK |
10543 | end if; |
10544 | ||
0669bebe | 10545 | -- Otherwise, proceed with processing tagged conversion |
70482933 | 10546 | |
e7e4d230 | 10547 | Tagged_Conversion : declare |
8cea7b64 HK |
10548 | Actual_Op_Typ : Entity_Id; |
10549 | Actual_Targ_Typ : Entity_Id; | |
10550 | Make_Conversion : Boolean := False; | |
10551 | Root_Op_Typ : Entity_Id; | |
70482933 | 10552 | |
8cea7b64 HK |
10553 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
10554 | -- Create a membership check to test whether Operand is a member | |
10555 | -- of Targ_Typ. If the original Target_Type is an access, include | |
10556 | -- a test for null value. The check is inserted at N. | |
10557 | ||
10558 | -------------------- | |
10559 | -- Make_Tag_Check -- | |
10560 | -------------------- | |
10561 | ||
10562 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
10563 | Cond : Node_Id; | |
10564 | ||
10565 | begin | |
10566 | -- Generate: | |
10567 | -- [Constraint_Error | |
10568 | -- when Operand /= null | |
10569 | -- and then Operand.all not in Targ_Typ] | |
10570 | ||
10571 | if Is_Access_Type (Target_Type) then | |
10572 | Cond := | |
10573 | Make_And_Then (Loc, | |
10574 | Left_Opnd => | |
10575 | Make_Op_Ne (Loc, | |
10576 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
10577 | Right_Opnd => Make_Null (Loc)), | |
10578 | ||
10579 | Right_Opnd => | |
10580 | Make_Not_In (Loc, | |
10581 | Left_Opnd => | |
10582 | Make_Explicit_Dereference (Loc, | |
10583 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
e4494292 | 10584 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc))); |
8cea7b64 HK |
10585 | |
10586 | -- Generate: | |
10587 | -- [Constraint_Error when Operand not in Targ_Typ] | |
10588 | ||
10589 | else | |
10590 | Cond := | |
10591 | Make_Not_In (Loc, | |
10592 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
e4494292 | 10593 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)); |
8cea7b64 HK |
10594 | end if; |
10595 | ||
10596 | Insert_Action (N, | |
10597 | Make_Raise_Constraint_Error (Loc, | |
10598 | Condition => Cond, | |
10599 | Reason => CE_Tag_Check_Failed)); | |
10600 | end Make_Tag_Check; | |
10601 | ||
e7e4d230 | 10602 | -- Start of processing for Tagged_Conversion |
70482933 RK |
10603 | |
10604 | begin | |
9732e886 | 10605 | -- Handle entities from the limited view |
852dba80 | 10606 | |
9732e886 | 10607 | if Is_Access_Type (Operand_Type) then |
852dba80 AC |
10608 | Actual_Op_Typ := |
10609 | Available_View (Designated_Type (Operand_Type)); | |
9732e886 JM |
10610 | else |
10611 | Actual_Op_Typ := Operand_Type; | |
10612 | end if; | |
10613 | ||
10614 | if Is_Access_Type (Target_Type) then | |
852dba80 AC |
10615 | Actual_Targ_Typ := |
10616 | Available_View (Designated_Type (Target_Type)); | |
70482933 | 10617 | else |
8cea7b64 | 10618 | Actual_Targ_Typ := Target_Type; |
70482933 RK |
10619 | end if; |
10620 | ||
8cea7b64 HK |
10621 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
10622 | ||
20b5d666 JM |
10623 | -- Ada 2005 (AI-251): Handle interface type conversion |
10624 | ||
3cb9a885 | 10625 | if Is_Interface (Actual_Op_Typ) |
58b81ab0 AC |
10626 | or else |
10627 | Is_Interface (Actual_Targ_Typ) | |
3cb9a885 | 10628 | then |
f6f4d8d4 | 10629 | Expand_Interface_Conversion (N); |
e606088a | 10630 | goto Done; |
20b5d666 JM |
10631 | end if; |
10632 | ||
8cea7b64 | 10633 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 10634 | |
8cea7b64 HK |
10635 | -- Create a runtime tag check for a downward class-wide type |
10636 | -- conversion. | |
70482933 | 10637 | |
8cea7b64 | 10638 | if Is_Class_Wide_Type (Actual_Op_Typ) |
852dba80 | 10639 | and then Actual_Op_Typ /= Actual_Targ_Typ |
8cea7b64 | 10640 | and then Root_Op_Typ /= Actual_Targ_Typ |
4ac2477e JM |
10641 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ, |
10642 | Use_Full_View => True) | |
8cea7b64 HK |
10643 | then |
10644 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
10645 | Make_Conversion := True; | |
10646 | end if; | |
70482933 | 10647 | |
8cea7b64 HK |
10648 | -- AI05-0073: If the result subtype of the function is defined |
10649 | -- by an access_definition designating a specific tagged type | |
10650 | -- T, a check is made that the result value is null or the tag | |
10651 | -- of the object designated by the result value identifies T. | |
10652 | -- Constraint_Error is raised if this check fails. | |
70482933 | 10653 | |
92a7cd46 | 10654 | if Nkind (Parent (N)) = N_Simple_Return_Statement then |
8cea7b64 | 10655 | declare |
e886436a | 10656 | Func : Entity_Id; |
8cea7b64 HK |
10657 | Func_Typ : Entity_Id; |
10658 | ||
10659 | begin | |
e886436a | 10660 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 10661 | |
e886436a | 10662 | Func := Current_Scope; |
8cea7b64 HK |
10663 | while Present (Func) |
10664 | and then Ekind (Func) /= E_Function | |
10665 | loop | |
10666 | Func := Scope (Func); | |
10667 | end loop; | |
10668 | ||
10669 | -- The function's return subtype must be defined using | |
10670 | -- an access definition. | |
10671 | ||
10672 | if Nkind (Result_Definition (Parent (Func))) = | |
10673 | N_Access_Definition | |
10674 | then | |
10675 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
10676 | ||
10677 | -- The return subtype denotes a specific tagged type, | |
10678 | -- in other words, a non class-wide type. | |
10679 | ||
10680 | if Is_Tagged_Type (Func_Typ) | |
10681 | and then not Is_Class_Wide_Type (Func_Typ) | |
10682 | then | |
10683 | Make_Tag_Check (Actual_Targ_Typ); | |
10684 | Make_Conversion := True; | |
10685 | end if; | |
10686 | end if; | |
10687 | end; | |
70482933 RK |
10688 | end if; |
10689 | ||
8cea7b64 HK |
10690 | -- We have generated a tag check for either a class-wide type |
10691 | -- conversion or for AI05-0073. | |
70482933 | 10692 | |
8cea7b64 HK |
10693 | if Make_Conversion then |
10694 | declare | |
10695 | Conv : Node_Id; | |
10696 | begin | |
10697 | Conv := | |
10698 | Make_Unchecked_Type_Conversion (Loc, | |
10699 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
10700 | Expression => Relocate_Node (Expression (N))); | |
10701 | Rewrite (N, Conv); | |
10702 | Analyze_And_Resolve (N, Target_Type); | |
10703 | end; | |
10704 | end if; | |
70482933 | 10705 | end if; |
e7e4d230 | 10706 | end Tagged_Conversion; |
70482933 RK |
10707 | |
10708 | -- Case of other access type conversions | |
10709 | ||
10710 | elsif Is_Access_Type (Target_Type) then | |
10711 | Apply_Constraint_Check (Operand, Target_Type); | |
10712 | ||
10713 | -- Case of conversions from a fixed-point type | |
10714 | ||
685094bf RD |
10715 | -- These conversions require special expansion and processing, found in |
10716 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
10717 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
10718 | -- conversions, which do not need further processing. |
10719 | ||
10720 | elsif Is_Fixed_Point_Type (Operand_Type) | |
10721 | and then not Conversion_OK (N) | |
10722 | then | |
10723 | -- We should never see universal fixed at this case, since the | |
10724 | -- expansion of the constituent divide or multiply should have | |
10725 | -- eliminated the explicit mention of universal fixed. | |
10726 | ||
10727 | pragma Assert (Operand_Type /= Universal_Fixed); | |
10728 | ||
685094bf RD |
10729 | -- Check for special case of the conversion to universal real that |
10730 | -- occurs as a result of the use of a round attribute. In this case, | |
10731 | -- the real type for the conversion is taken from the target type of | |
10732 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
10733 | |
10734 | if Target_Type = Universal_Real | |
10735 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
10736 | and then Attribute_Name (Parent (N)) = Name_Round | |
10737 | then | |
10738 | Set_Rounded_Result (N); | |
10739 | Set_Etype (N, Etype (Parent (N))); | |
10740 | end if; | |
10741 | ||
10742 | -- Otherwise do correct fixed-conversion, but skip these if the | |
e7e4d230 AC |
10743 | -- Conversion_OK flag is set, because from a semantic point of view |
10744 | -- these are simple integer conversions needing no further processing | |
10745 | -- (the backend will simply treat them as integers). | |
70482933 RK |
10746 | |
10747 | if not Conversion_OK (N) then | |
10748 | if Is_Fixed_Point_Type (Etype (N)) then | |
10749 | Expand_Convert_Fixed_To_Fixed (N); | |
10750 | Real_Range_Check; | |
10751 | ||
10752 | elsif Is_Integer_Type (Etype (N)) then | |
10753 | Expand_Convert_Fixed_To_Integer (N); | |
10754 | ||
10755 | else | |
10756 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
10757 | Expand_Convert_Fixed_To_Float (N); | |
10758 | Real_Range_Check; | |
10759 | end if; | |
10760 | end if; | |
10761 | ||
10762 | -- Case of conversions to a fixed-point type | |
10763 | ||
685094bf RD |
10764 | -- These conversions require special expansion and processing, found in |
10765 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
10766 | -- since from a semantic point of view, these are simple integer | |
10767 | -- conversions, which do not need further processing. | |
70482933 RK |
10768 | |
10769 | elsif Is_Fixed_Point_Type (Target_Type) | |
10770 | and then not Conversion_OK (N) | |
10771 | then | |
10772 | if Is_Integer_Type (Operand_Type) then | |
10773 | Expand_Convert_Integer_To_Fixed (N); | |
10774 | Real_Range_Check; | |
10775 | else | |
10776 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
10777 | Expand_Convert_Float_To_Fixed (N); | |
10778 | Real_Range_Check; | |
10779 | end if; | |
10780 | ||
10781 | -- Case of float-to-integer conversions | |
10782 | ||
10783 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
10784 | -- since semantically the fixed-point target is treated as though it | |
10785 | -- were an integer in such cases. | |
10786 | ||
10787 | elsif Is_Floating_Point_Type (Operand_Type) | |
10788 | and then | |
10789 | (Is_Integer_Type (Target_Type) | |
10790 | or else | |
10791 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
10792 | then | |
70482933 RK |
10793 | -- One more check here, gcc is still not able to do conversions of |
10794 | -- this type with proper overflow checking, and so gigi is doing an | |
10795 | -- approximation of what is required by doing floating-point compares | |
10796 | -- with the end-point. But that can lose precision in some cases, and | |
f02b8bb8 | 10797 | -- give a wrong result. Converting the operand to Universal_Real is |
70482933 | 10798 | -- helpful, but still does not catch all cases with 64-bit integers |
e7e4d230 | 10799 | -- on targets with only 64-bit floats. |
0669bebe GB |
10800 | |
10801 | -- The above comment seems obsoleted by Apply_Float_Conversion_Check | |
10802 | -- Can this code be removed ??? | |
70482933 | 10803 | |
fbf5a39b AC |
10804 | if Do_Range_Check (Operand) then |
10805 | Rewrite (Operand, | |
70482933 RK |
10806 | Make_Type_Conversion (Loc, |
10807 | Subtype_Mark => | |
f02b8bb8 | 10808 | New_Occurrence_Of (Universal_Real, Loc), |
70482933 | 10809 | Expression => |
fbf5a39b | 10810 | Relocate_Node (Operand))); |
70482933 | 10811 | |
f02b8bb8 | 10812 | Set_Etype (Operand, Universal_Real); |
fbf5a39b AC |
10813 | Enable_Range_Check (Operand); |
10814 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
10815 | end if; |
10816 | ||
10817 | -- Case of array conversions | |
10818 | ||
685094bf RD |
10819 | -- Expansion of array conversions, add required length/range checks but |
10820 | -- only do this if there is no change of representation. For handling of | |
10821 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
10822 | |
10823 | elsif Is_Array_Type (Target_Type) then | |
70482933 RK |
10824 | if Is_Constrained (Target_Type) then |
10825 | Apply_Length_Check (Operand, Target_Type); | |
10826 | else | |
10827 | Apply_Range_Check (Operand, Target_Type); | |
10828 | end if; | |
10829 | ||
10830 | Handle_Changed_Representation; | |
10831 | ||
10832 | -- Case of conversions of discriminated types | |
10833 | ||
685094bf RD |
10834 | -- Add required discriminant checks if target is constrained. Again this |
10835 | -- change is skipped if we have a change of representation. | |
70482933 RK |
10836 | |
10837 | elsif Has_Discriminants (Target_Type) | |
10838 | and then Is_Constrained (Target_Type) | |
10839 | then | |
10840 | Apply_Discriminant_Check (Operand, Target_Type); | |
10841 | Handle_Changed_Representation; | |
10842 | ||
10843 | -- Case of all other record conversions. The only processing required | |
10844 | -- is to check for a change of representation requiring the special | |
10845 | -- assignment processing. | |
10846 | ||
10847 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
10848 | |
10849 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
10850 | -- a derived Unchecked_Union type to an unconstrained type that is |
10851 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
10852 | |
10853 | if Is_Derived_Type (Operand_Type) | |
10854 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
10855 | and then not Is_Constrained (Target_Type) | |
10856 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
10857 | and then not Has_Inferable_Discriminants (Operand) | |
10858 | then | |
685094bf | 10859 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e | 10860 | -- Program_Error node, but we give it the target type of the |
6cb3037c | 10861 | -- conversion (is this requirement documented somewhere ???) |
5d09245e AC |
10862 | |
10863 | declare | |
10864 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
10865 | Reason => PE_Unchecked_Union_Restriction); | |
10866 | ||
10867 | begin | |
10868 | Set_Etype (PE, Target_Type); | |
10869 | Rewrite (N, PE); | |
10870 | ||
10871 | end; | |
10872 | else | |
10873 | Handle_Changed_Representation; | |
10874 | end if; | |
70482933 RK |
10875 | |
10876 | -- Case of conversions of enumeration types | |
10877 | ||
10878 | elsif Is_Enumeration_Type (Target_Type) then | |
10879 | ||
10880 | -- Special processing is required if there is a change of | |
e7e4d230 | 10881 | -- representation (from enumeration representation clauses). |
70482933 RK |
10882 | |
10883 | if not Same_Representation (Target_Type, Operand_Type) then | |
10884 | ||
10885 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
10886 | ||
10887 | Rewrite (N, | |
1c66c4f5 AC |
10888 | Make_Attribute_Reference (Loc, |
10889 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
10890 | Attribute_Name => Name_Val, | |
10891 | Expressions => New_List ( | |
10892 | Make_Attribute_Reference (Loc, | |
10893 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
10894 | Attribute_Name => Name_Pos, | |
10895 | Expressions => New_List (Operand))))); | |
70482933 RK |
10896 | |
10897 | Analyze_And_Resolve (N, Target_Type); | |
10898 | end if; | |
10899 | ||
10900 | -- Case of conversions to floating-point | |
10901 | ||
10902 | elsif Is_Floating_Point_Type (Target_Type) then | |
10903 | Real_Range_Check; | |
70482933 RK |
10904 | end if; |
10905 | ||
685094bf | 10906 | -- At this stage, either the conversion node has been transformed into |
e7e4d230 AC |
10907 | -- some other equivalent expression, or left as a conversion that can be |
10908 | -- handled by Gigi, in the following cases: | |
70482933 RK |
10909 | |
10910 | -- Conversions with no change of representation or type | |
10911 | ||
685094bf RD |
10912 | -- Numeric conversions involving integer, floating- and fixed-point |
10913 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
10914 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 10915 | |
5e1c00fa RD |
10916 | -- No other conversions should be passed to Gigi |
10917 | ||
10918 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 10919 | |
685094bf RD |
10920 | -- The only remaining step is to generate a range check if we still have |
10921 | -- a type conversion at this stage and Do_Range_Check is set. For now we | |
f5655e4a AC |
10922 | -- do this only for conversions of discrete types and for float-to-float |
10923 | -- conversions. | |
fbf5a39b | 10924 | |
7b536495 | 10925 | if Nkind (N) = N_Type_Conversion then |
fbf5a39b | 10926 | |
f5655e4a AC |
10927 | -- For now we only support floating-point cases where both source |
10928 | -- and target are floating-point types. Conversions where the source | |
10929 | -- and target involve integer or fixed-point types are still TBD, | |
10930 | -- though not clear whether those can even happen at this point, due | |
10931 | -- to transformations above. ??? | |
fbf5a39b | 10932 | |
7b536495 | 10933 | if Is_Floating_Point_Type (Etype (N)) |
f5655e4a | 10934 | and then Is_Floating_Point_Type (Etype (Expression (N))) |
7b536495 AC |
10935 | then |
10936 | if Do_Range_Check (Expression (N)) | |
10937 | and then Is_Floating_Point_Type (Target_Type) | |
10938 | then | |
10939 | Generate_Range_Check | |
10940 | (Expression (N), Target_Type, CE_Range_Check_Failed); | |
10941 | end if; | |
fbf5a39b | 10942 | |
f5655e4a AC |
10943 | -- Discrete-to-discrete conversions |
10944 | ||
7b536495 AC |
10945 | elsif Is_Discrete_Type (Etype (N)) then |
10946 | declare | |
10947 | Expr : constant Node_Id := Expression (N); | |
10948 | Ftyp : Entity_Id; | |
10949 | Ityp : Entity_Id; | |
fbf5a39b | 10950 | |
7b536495 AC |
10951 | begin |
10952 | if Do_Range_Check (Expr) | |
10953 | and then Is_Discrete_Type (Etype (Expr)) | |
fbf5a39b | 10954 | then |
7b536495 | 10955 | Set_Do_Range_Check (Expr, False); |
fbf5a39b | 10956 | |
7b536495 AC |
10957 | -- Before we do a range check, we have to deal with treating |
10958 | -- a fixed-point operand as an integer. The way we do this | |
10959 | -- is simply to do an unchecked conversion to an appropriate | |
10960 | -- integer type large enough to hold the result. | |
fbf5a39b | 10961 | |
7b536495 AC |
10962 | -- This code is not active yet, because we are only dealing |
10963 | -- with discrete types so far ??? | |
fbf5a39b | 10964 | |
7b536495 AC |
10965 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer |
10966 | and then Treat_Fixed_As_Integer (Expr) | |
10967 | then | |
10968 | Ftyp := Base_Type (Etype (Expr)); | |
fbf5a39b | 10969 | |
7b536495 AC |
10970 | if Esize (Ftyp) >= Esize (Standard_Integer) then |
10971 | Ityp := Standard_Long_Long_Integer; | |
10972 | else | |
10973 | Ityp := Standard_Integer; | |
10974 | end if; | |
edab6088 | 10975 | |
7b536495 AC |
10976 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); |
10977 | end if; | |
10978 | ||
10979 | -- Reset overflow flag, since the range check will include | |
10980 | -- dealing with possible overflow, and generate the check. | |
10981 | -- If Address is either a source type or target type, | |
10982 | -- suppress range check to avoid typing anomalies when | |
10983 | -- it is a visible integer type. | |
10984 | ||
10985 | Set_Do_Overflow_Check (N, False); | |
10986 | ||
10987 | if not Is_Descendent_Of_Address (Etype (Expr)) | |
10988 | and then not Is_Descendent_Of_Address (Target_Type) | |
10989 | then | |
10990 | Generate_Range_Check | |
10991 | (Expr, Target_Type, CE_Range_Check_Failed); | |
10992 | end if; | |
8a36a0cc | 10993 | end if; |
7b536495 AC |
10994 | end; |
10995 | end if; | |
fbf5a39b | 10996 | end if; |
f02b8bb8 | 10997 | |
e606088a AC |
10998 | -- Here at end of processing |
10999 | ||
48f91b44 RD |
11000 | <<Done>> |
11001 | -- Apply predicate check if required. Note that we can't just call | |
11002 | -- Apply_Predicate_Check here, because the type looks right after | |
11003 | -- the conversion and it would omit the check. The Comes_From_Source | |
11004 | -- guard is necessary to prevent infinite recursions when we generate | |
11005 | -- internal conversions for the purpose of checking predicates. | |
11006 | ||
11007 | if Present (Predicate_Function (Target_Type)) | |
11008 | and then Target_Type /= Operand_Type | |
11009 | and then Comes_From_Source (N) | |
11010 | then | |
00332244 AC |
11011 | declare |
11012 | New_Expr : constant Node_Id := Duplicate_Subexpr (N); | |
11013 | ||
11014 | begin | |
11015 | -- Avoid infinite recursion on the subsequent expansion of | |
11016 | -- of the copy of the original type conversion. | |
11017 | ||
11018 | Set_Comes_From_Source (New_Expr, False); | |
11019 | Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr)); | |
11020 | end; | |
48f91b44 | 11021 | end if; |
70482933 RK |
11022 | end Expand_N_Type_Conversion; |
11023 | ||
11024 | ----------------------------------- | |
11025 | -- Expand_N_Unchecked_Expression -- | |
11026 | ----------------------------------- | |
11027 | ||
e7e4d230 | 11028 | -- Remove the unchecked expression node from the tree. Its job was simply |
70482933 RK |
11029 | -- to make sure that its constituent expression was handled with checks |
11030 | -- off, and now that that is done, we can remove it from the tree, and | |
e7e4d230 | 11031 | -- indeed must, since Gigi does not expect to see these nodes. |
70482933 RK |
11032 | |
11033 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
11034 | Exp : constant Node_Id := Expression (N); | |
70482933 | 11035 | begin |
e7e4d230 | 11036 | Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp)); |
70482933 RK |
11037 | Rewrite (N, Exp); |
11038 | end Expand_N_Unchecked_Expression; | |
11039 | ||
11040 | ---------------------------------------- | |
11041 | -- Expand_N_Unchecked_Type_Conversion -- | |
11042 | ---------------------------------------- | |
11043 | ||
685094bf RD |
11044 | -- If this cannot be handled by Gigi and we haven't already made a |
11045 | -- temporary for it, do it now. | |
70482933 RK |
11046 | |
11047 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
11048 | Target_Type : constant Entity_Id := Etype (N); | |
11049 | Operand : constant Node_Id := Expression (N); | |
11050 | Operand_Type : constant Entity_Id := Etype (Operand); | |
11051 | ||
11052 | begin | |
7b00e31d | 11053 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 | 11054 | -- the conversion completely, it is useless, except that it may carry |
e7e4d230 | 11055 | -- an Assignment_OK indication which must be propagated to the operand. |
7b00e31d AC |
11056 | |
11057 | if Operand_Type = Target_Type then | |
13d923cc | 11058 | |
e7e4d230 AC |
11059 | -- Code duplicates Expand_N_Unchecked_Expression above, factor??? |
11060 | ||
7b00e31d AC |
11061 | if Assignment_OK (N) then |
11062 | Set_Assignment_OK (Operand); | |
11063 | end if; | |
11064 | ||
11065 | Rewrite (N, Relocate_Node (Operand)); | |
11066 | return; | |
11067 | end if; | |
11068 | ||
70482933 RK |
11069 | -- If we have a conversion of a compile time known value to a target |
11070 | -- type and the value is in range of the target type, then we can simply | |
11071 | -- replace the construct by an integer literal of the correct type. We | |
11072 | -- only apply this to integer types being converted. Possibly it may | |
11073 | -- apply in other cases, but it is too much trouble to worry about. | |
11074 | ||
11075 | -- Note that we do not do this transformation if the Kill_Range_Check | |
11076 | -- flag is set, since then the value may be outside the expected range. | |
11077 | -- This happens in the Normalize_Scalars case. | |
11078 | ||
20b5d666 JM |
11079 | -- We also skip this if either the target or operand type is biased |
11080 | -- because in this case, the unchecked conversion is supposed to | |
11081 | -- preserve the bit pattern, not the integer value. | |
11082 | ||
70482933 | 11083 | if Is_Integer_Type (Target_Type) |
20b5d666 | 11084 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 11085 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 11086 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
11087 | and then Compile_Time_Known_Value (Operand) |
11088 | and then not Kill_Range_Check (N) | |
11089 | then | |
11090 | declare | |
11091 | Val : constant Uint := Expr_Value (Operand); | |
11092 | ||
11093 | begin | |
11094 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
11095 | and then | |
11096 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
11097 | and then | |
11098 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
11099 | and then | |
11100 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
11101 | then | |
11102 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 11103 | |
685094bf RD |
11104 | -- If Address is the target type, just set the type to avoid a |
11105 | -- spurious type error on the literal when Address is a visible | |
11106 | -- integer type. | |
8a36a0cc AC |
11107 | |
11108 | if Is_Descendent_Of_Address (Target_Type) then | |
11109 | Set_Etype (N, Target_Type); | |
11110 | else | |
11111 | Analyze_And_Resolve (N, Target_Type); | |
11112 | end if; | |
11113 | ||
70482933 RK |
11114 | return; |
11115 | end if; | |
11116 | end; | |
11117 | end if; | |
11118 | ||
11119 | -- Nothing to do if conversion is safe | |
11120 | ||
11121 | if Safe_Unchecked_Type_Conversion (N) then | |
11122 | return; | |
11123 | end if; | |
11124 | ||
11125 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
324ac540 | 11126 | -- flag indicates ??? More comments needed here) |
70482933 RK |
11127 | |
11128 | if Assignment_OK (N) then | |
11129 | null; | |
11130 | else | |
11131 | Force_Evaluation (N); | |
11132 | end if; | |
11133 | end Expand_N_Unchecked_Type_Conversion; | |
11134 | ||
11135 | ---------------------------- | |
11136 | -- Expand_Record_Equality -- | |
11137 | ---------------------------- | |
11138 | ||
11139 | -- For non-variant records, Equality is expanded when needed into: | |
11140 | ||
11141 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
11142 | -- and then ... | |
11143 | -- and then Lhs.Discrn = Rhs.Discrn | |
11144 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
11145 | -- and then ... | |
11146 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
11147 | ||
11148 | -- The expression is folded by the back-end for adjacent fields. This | |
11149 | -- function is called for tagged record in only one occasion: for imple- | |
11150 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
11151 | -- otherwise the primitive "=" is used directly. | |
11152 | ||
11153 | function Expand_Record_Equality | |
11154 | (Nod : Node_Id; | |
11155 | Typ : Entity_Id; | |
11156 | Lhs : Node_Id; | |
11157 | Rhs : Node_Id; | |
2e071734 | 11158 | Bodies : List_Id) return Node_Id |
70482933 RK |
11159 | is |
11160 | Loc : constant Source_Ptr := Sloc (Nod); | |
11161 | ||
0ab80019 AC |
11162 | Result : Node_Id; |
11163 | C : Entity_Id; | |
11164 | ||
11165 | First_Time : Boolean := True; | |
11166 | ||
6b670dcf AC |
11167 | function Element_To_Compare (C : Entity_Id) return Entity_Id; |
11168 | -- Return the next discriminant or component to compare, starting with | |
11169 | -- C, skipping inherited components. | |
0ab80019 | 11170 | |
6b670dcf AC |
11171 | ------------------------ |
11172 | -- Element_To_Compare -- | |
11173 | ------------------------ | |
70482933 | 11174 | |
6b670dcf AC |
11175 | function Element_To_Compare (C : Entity_Id) return Entity_Id is |
11176 | Comp : Entity_Id; | |
28270211 | 11177 | |
70482933 | 11178 | begin |
6b670dcf | 11179 | Comp := C; |
6b670dcf AC |
11180 | loop |
11181 | -- Exit loop when the next element to be compared is found, or | |
11182 | -- there is no more such element. | |
70482933 | 11183 | |
6b670dcf | 11184 | exit when No (Comp); |
8190087e | 11185 | |
6b670dcf AC |
11186 | exit when Ekind_In (Comp, E_Discriminant, E_Component) |
11187 | and then not ( | |
70482933 | 11188 | |
6b670dcf | 11189 | -- Skip inherited components |
70482933 | 11190 | |
6b670dcf AC |
11191 | -- Note: for a tagged type, we always generate the "=" primitive |
11192 | -- for the base type (not on the first subtype), so the test for | |
11193 | -- Comp /= Original_Record_Component (Comp) is True for | |
11194 | -- inherited components only. | |
24558db8 | 11195 | |
6b670dcf | 11196 | (Is_Tagged_Type (Typ) |
28270211 | 11197 | and then Comp /= Original_Record_Component (Comp)) |
24558db8 | 11198 | |
6b670dcf | 11199 | -- Skip _Tag |
26bff3d9 | 11200 | |
6b670dcf AC |
11201 | or else Chars (Comp) = Name_uTag |
11202 | ||
11203 | -- The .NET/JVM version of type Root_Controlled contains two | |
11204 | -- fields which should not be considered part of the object. To | |
11205 | -- achieve proper equiality between two controlled objects on | |
11206 | -- .NET/JVM, skip _Parent whenever it has type Root_Controlled. | |
11207 | ||
11208 | or else (Chars (Comp) = Name_uParent | |
28270211 AC |
11209 | and then VM_Target /= No_VM |
11210 | and then Etype (Comp) = RTE (RE_Root_Controlled)) | |
6b670dcf AC |
11211 | |
11212 | -- Skip interface elements (secondary tags???) | |
11213 | ||
11214 | or else Is_Interface (Etype (Comp))); | |
11215 | ||
11216 | Next_Entity (Comp); | |
11217 | end loop; | |
11218 | ||
11219 | return Comp; | |
11220 | end Element_To_Compare; | |
70482933 | 11221 | |
70482933 RK |
11222 | -- Start of processing for Expand_Record_Equality |
11223 | ||
11224 | begin | |
70482933 RK |
11225 | -- Generates the following code: (assuming that Typ has one Discr and |
11226 | -- component C2 is also a record) | |
11227 | ||
11228 | -- True | |
11229 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
11230 | -- and then Lhs.C1 = Rhs.C1 | |
11231 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
11232 | -- and then ... | |
11233 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
11234 | ||
e4494292 | 11235 | Result := New_Occurrence_Of (Standard_True, Loc); |
6b670dcf | 11236 | C := Element_To_Compare (First_Entity (Typ)); |
70482933 | 11237 | while Present (C) loop |
70482933 RK |
11238 | declare |
11239 | New_Lhs : Node_Id; | |
11240 | New_Rhs : Node_Id; | |
8aceda64 | 11241 | Check : Node_Id; |
70482933 RK |
11242 | |
11243 | begin | |
11244 | if First_Time then | |
11245 | First_Time := False; | |
11246 | New_Lhs := Lhs; | |
11247 | New_Rhs := Rhs; | |
70482933 RK |
11248 | else |
11249 | New_Lhs := New_Copy_Tree (Lhs); | |
11250 | New_Rhs := New_Copy_Tree (Rhs); | |
11251 | end if; | |
11252 | ||
8aceda64 AC |
11253 | Check := |
11254 | Expand_Composite_Equality (Nod, Etype (C), | |
11255 | Lhs => | |
11256 | Make_Selected_Component (Loc, | |
8d80ff64 | 11257 | Prefix => New_Lhs, |
e4494292 | 11258 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
11259 | Rhs => |
11260 | Make_Selected_Component (Loc, | |
8d80ff64 | 11261 | Prefix => New_Rhs, |
e4494292 | 11262 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
11263 | Bodies => Bodies); |
11264 | ||
11265 | -- If some (sub)component is an unchecked_union, the whole | |
11266 | -- operation will raise program error. | |
11267 | ||
11268 | if Nkind (Check) = N_Raise_Program_Error then | |
11269 | Result := Check; | |
11270 | Set_Etype (Result, Standard_Boolean); | |
11271 | exit; | |
11272 | else | |
11273 | Result := | |
11274 | Make_And_Then (Loc, | |
11275 | Left_Opnd => Result, | |
11276 | Right_Opnd => Check); | |
11277 | end if; | |
70482933 RK |
11278 | end; |
11279 | ||
6b670dcf | 11280 | C := Element_To_Compare (Next_Entity (C)); |
70482933 RK |
11281 | end loop; |
11282 | ||
11283 | return Result; | |
11284 | end Expand_Record_Equality; | |
11285 | ||
a3068ca6 AC |
11286 | --------------------------- |
11287 | -- Expand_Set_Membership -- | |
11288 | --------------------------- | |
11289 | ||
11290 | procedure Expand_Set_Membership (N : Node_Id) is | |
11291 | Lop : constant Node_Id := Left_Opnd (N); | |
11292 | Alt : Node_Id; | |
11293 | Res : Node_Id; | |
11294 | ||
11295 | function Make_Cond (Alt : Node_Id) return Node_Id; | |
11296 | -- If the alternative is a subtype mark, create a simple membership | |
11297 | -- test. Otherwise create an equality test for it. | |
11298 | ||
11299 | --------------- | |
11300 | -- Make_Cond -- | |
11301 | --------------- | |
11302 | ||
11303 | function Make_Cond (Alt : Node_Id) return Node_Id is | |
11304 | Cond : Node_Id; | |
11305 | L : constant Node_Id := New_Copy (Lop); | |
11306 | R : constant Node_Id := Relocate_Node (Alt); | |
11307 | ||
11308 | begin | |
11309 | if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt))) | |
11310 | or else Nkind (Alt) = N_Range | |
11311 | then | |
11312 | Cond := | |
11313 | Make_In (Sloc (Alt), | |
11314 | Left_Opnd => L, | |
11315 | Right_Opnd => R); | |
11316 | else | |
11317 | Cond := | |
11318 | Make_Op_Eq (Sloc (Alt), | |
11319 | Left_Opnd => L, | |
11320 | Right_Opnd => R); | |
11321 | end if; | |
11322 | ||
11323 | return Cond; | |
11324 | end Make_Cond; | |
11325 | ||
11326 | -- Start of processing for Expand_Set_Membership | |
11327 | ||
11328 | begin | |
11329 | Remove_Side_Effects (Lop); | |
11330 | ||
11331 | Alt := Last (Alternatives (N)); | |
11332 | Res := Make_Cond (Alt); | |
11333 | ||
11334 | Prev (Alt); | |
11335 | while Present (Alt) loop | |
11336 | Res := | |
11337 | Make_Or_Else (Sloc (Alt), | |
11338 | Left_Opnd => Make_Cond (Alt), | |
11339 | Right_Opnd => Res); | |
11340 | Prev (Alt); | |
11341 | end loop; | |
11342 | ||
11343 | Rewrite (N, Res); | |
11344 | Analyze_And_Resolve (N, Standard_Boolean); | |
11345 | end Expand_Set_Membership; | |
11346 | ||
5875f8d6 AC |
11347 | ----------------------------------- |
11348 | -- Expand_Short_Circuit_Operator -- | |
11349 | ----------------------------------- | |
11350 | ||
955871d3 AC |
11351 | -- Deal with special expansion if actions are present for the right operand |
11352 | -- and deal with optimizing case of arguments being True or False. We also | |
11353 | -- deal with the special case of non-standard boolean values. | |
5875f8d6 AC |
11354 | |
11355 | procedure Expand_Short_Circuit_Operator (N : Node_Id) is | |
11356 | Loc : constant Source_Ptr := Sloc (N); | |
11357 | Typ : constant Entity_Id := Etype (N); | |
5875f8d6 AC |
11358 | Left : constant Node_Id := Left_Opnd (N); |
11359 | Right : constant Node_Id := Right_Opnd (N); | |
955871d3 | 11360 | LocR : constant Source_Ptr := Sloc (Right); |
5875f8d6 AC |
11361 | Actlist : List_Id; |
11362 | ||
11363 | Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else; | |
11364 | Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value); | |
11365 | -- If Left = Shortcut_Value then Right need not be evaluated | |
11366 | ||
5875f8d6 AC |
11367 | begin |
11368 | -- Deal with non-standard booleans | |
11369 | ||
11370 | if Is_Boolean_Type (Typ) then | |
11371 | Adjust_Condition (Left); | |
11372 | Adjust_Condition (Right); | |
11373 | Set_Etype (N, Standard_Boolean); | |
11374 | end if; | |
11375 | ||
11376 | -- Check for cases where left argument is known to be True or False | |
11377 | ||
11378 | if Compile_Time_Known_Value (Left) then | |
25adc5fb AC |
11379 | |
11380 | -- Mark SCO for left condition as compile time known | |
11381 | ||
11382 | if Generate_SCO and then Comes_From_Source (Left) then | |
11383 | Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True); | |
11384 | end if; | |
11385 | ||
5875f8d6 AC |
11386 | -- Rewrite True AND THEN Right / False OR ELSE Right to Right. |
11387 | -- Any actions associated with Right will be executed unconditionally | |
11388 | -- and can thus be inserted into the tree unconditionally. | |
11389 | ||
11390 | if Expr_Value_E (Left) /= Shortcut_Ent then | |
11391 | if Present (Actions (N)) then | |
11392 | Insert_Actions (N, Actions (N)); | |
11393 | end if; | |
11394 | ||
11395 | Rewrite (N, Right); | |
11396 | ||
11397 | -- Rewrite False AND THEN Right / True OR ELSE Right to Left. | |
11398 | -- In this case we can forget the actions associated with Right, | |
11399 | -- since they will never be executed. | |
11400 | ||
11401 | else | |
11402 | Kill_Dead_Code (Right); | |
11403 | Kill_Dead_Code (Actions (N)); | |
11404 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
11405 | end if; | |
11406 | ||
11407 | Adjust_Result_Type (N, Typ); | |
11408 | return; | |
11409 | end if; | |
11410 | ||
955871d3 AC |
11411 | -- If Actions are present for the right operand, we have to do some |
11412 | -- special processing. We can't just let these actions filter back into | |
11413 | -- code preceding the short circuit (which is what would have happened | |
11414 | -- if we had not trapped them in the short-circuit form), since they | |
11415 | -- must only be executed if the right operand of the short circuit is | |
11416 | -- executed and not otherwise. | |
5875f8d6 | 11417 | |
955871d3 AC |
11418 | if Present (Actions (N)) then |
11419 | Actlist := Actions (N); | |
5875f8d6 | 11420 | |
0812b84e AC |
11421 | -- We now use an Expression_With_Actions node for the right operand |
11422 | -- of the short-circuit form. Note that this solves the traceability | |
11423 | -- problems for coverage analysis. | |
5875f8d6 | 11424 | |
0812b84e AC |
11425 | Rewrite (Right, |
11426 | Make_Expression_With_Actions (LocR, | |
11427 | Expression => Relocate_Node (Right), | |
11428 | Actions => Actlist)); | |
11429 | Set_Actions (N, No_List); | |
11430 | Analyze_And_Resolve (Right, Standard_Boolean); | |
955871d3 | 11431 | |
5875f8d6 AC |
11432 | Adjust_Result_Type (N, Typ); |
11433 | return; | |
11434 | end if; | |
11435 | ||
11436 | -- No actions present, check for cases of right argument True/False | |
11437 | ||
11438 | if Compile_Time_Known_Value (Right) then | |
25adc5fb AC |
11439 | |
11440 | -- Mark SCO for left condition as compile time known | |
11441 | ||
11442 | if Generate_SCO and then Comes_From_Source (Right) then | |
11443 | Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True); | |
11444 | end if; | |
11445 | ||
5875f8d6 AC |
11446 | -- Change (Left and then True), (Left or else False) to Left. |
11447 | -- Note that we know there are no actions associated with the right | |
11448 | -- operand, since we just checked for this case above. | |
11449 | ||
11450 | if Expr_Value_E (Right) /= Shortcut_Ent then | |
11451 | Rewrite (N, Left); | |
11452 | ||
11453 | -- Change (Left and then False), (Left or else True) to Right, | |
11454 | -- making sure to preserve any side effects associated with the Left | |
11455 | -- operand. | |
11456 | ||
11457 | else | |
11458 | Remove_Side_Effects (Left); | |
11459 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
11460 | end if; | |
11461 | end if; | |
11462 | ||
11463 | Adjust_Result_Type (N, Typ); | |
11464 | end Expand_Short_Circuit_Operator; | |
11465 | ||
70482933 RK |
11466 | ------------------------------------- |
11467 | -- Fixup_Universal_Fixed_Operation -- | |
11468 | ------------------------------------- | |
11469 | ||
11470 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
11471 | Conv : constant Node_Id := Parent (N); | |
11472 | ||
11473 | begin | |
11474 | -- We must have a type conversion immediately above us | |
11475 | ||
11476 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
11477 | ||
11478 | -- Normally the type conversion gives our target type. The exception | |
11479 | -- occurs in the case of the Round attribute, where the conversion | |
11480 | -- will be to universal real, and our real type comes from the Round | |
11481 | -- attribute (as well as an indication that we must round the result) | |
11482 | ||
11483 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
11484 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
11485 | then | |
11486 | Set_Etype (N, Etype (Parent (Conv))); | |
11487 | Set_Rounded_Result (N); | |
11488 | ||
11489 | -- Normal case where type comes from conversion above us | |
11490 | ||
11491 | else | |
11492 | Set_Etype (N, Etype (Conv)); | |
11493 | end if; | |
11494 | end Fixup_Universal_Fixed_Operation; | |
11495 | ||
5d09245e AC |
11496 | --------------------------------- |
11497 | -- Has_Inferable_Discriminants -- | |
11498 | --------------------------------- | |
11499 | ||
11500 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
11501 | ||
11502 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
11503 | -- Determines whether the left-most prefix of a selected component is a | |
11504 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
11505 | ||
11506 | -------------------------------- | |
11507 | -- Prefix_Is_Formal_Parameter -- | |
11508 | -------------------------------- | |
11509 | ||
11510 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
83bb90af | 11511 | Sel_Comp : Node_Id; |
5d09245e AC |
11512 | |
11513 | begin | |
11514 | -- Move to the left-most prefix by climbing up the tree | |
11515 | ||
83bb90af | 11516 | Sel_Comp := N; |
5d09245e AC |
11517 | while Present (Parent (Sel_Comp)) |
11518 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
11519 | loop | |
11520 | Sel_Comp := Parent (Sel_Comp); | |
11521 | end loop; | |
11522 | ||
11523 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
11524 | end Prefix_Is_Formal_Parameter; | |
11525 | ||
11526 | -- Start of processing for Has_Inferable_Discriminants | |
11527 | ||
11528 | begin | |
5d09245e AC |
11529 | -- For selected components, the subtype of the selector must be a |
11530 | -- constrained Unchecked_Union. If the component is subject to a | |
11531 | -- per-object constraint, then the enclosing object must have inferable | |
11532 | -- discriminants. | |
11533 | ||
83bb90af | 11534 | if Nkind (N) = N_Selected_Component then |
5d09245e AC |
11535 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then |
11536 | ||
11537 | -- A small hack. If we have a per-object constrained selected | |
11538 | -- component of a formal parameter, return True since we do not | |
11539 | -- know the actual parameter association yet. | |
11540 | ||
11541 | if Prefix_Is_Formal_Parameter (N) then | |
11542 | return True; | |
5d09245e AC |
11543 | |
11544 | -- Otherwise, check the enclosing object and the selector | |
11545 | ||
83bb90af TQ |
11546 | else |
11547 | return Has_Inferable_Discriminants (Prefix (N)) | |
11548 | and then Has_Inferable_Discriminants (Selector_Name (N)); | |
11549 | end if; | |
5d09245e AC |
11550 | |
11551 | -- The call to Has_Inferable_Discriminants will determine whether | |
11552 | -- the selector has a constrained Unchecked_Union nominal type. | |
11553 | ||
83bb90af TQ |
11554 | else |
11555 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
11556 | end if; | |
5d09245e AC |
11557 | |
11558 | -- A qualified expression has inferable discriminants if its subtype | |
11559 | -- mark is a constrained Unchecked_Union subtype. | |
11560 | ||
11561 | elsif Nkind (N) = N_Qualified_Expression then | |
053cf994 | 11562 | return Is_Unchecked_Union (Etype (Subtype_Mark (N))) |
5b5b27ad | 11563 | and then Is_Constrained (Etype (Subtype_Mark (N))); |
5d09245e | 11564 | |
83bb90af TQ |
11565 | -- For all other names, it is sufficient to have a constrained |
11566 | -- Unchecked_Union nominal subtype. | |
11567 | ||
11568 | else | |
11569 | return Is_Unchecked_Union (Base_Type (Etype (N))) | |
11570 | and then Is_Constrained (Etype (N)); | |
11571 | end if; | |
5d09245e AC |
11572 | end Has_Inferable_Discriminants; |
11573 | ||
70482933 RK |
11574 | ------------------------------- |
11575 | -- Insert_Dereference_Action -- | |
11576 | ------------------------------- | |
11577 | ||
11578 | procedure Insert_Dereference_Action (N : Node_Id) is | |
8777c5a6 | 11579 | |
70482933 | 11580 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; |
2e071734 AC |
11581 | -- Return true if type of P is derived from Checked_Pool; |
11582 | ||
11583 | ----------------------------- | |
11584 | -- Is_Checked_Storage_Pool -- | |
11585 | ----------------------------- | |
70482933 RK |
11586 | |
11587 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
11588 | T : Entity_Id; | |
761f7dcb | 11589 | |
70482933 RK |
11590 | begin |
11591 | if No (P) then | |
11592 | return False; | |
11593 | end if; | |
11594 | ||
11595 | T := Etype (P); | |
11596 | while T /= Etype (T) loop | |
11597 | if Is_RTE (T, RE_Checked_Pool) then | |
11598 | return True; | |
11599 | else | |
11600 | T := Etype (T); | |
11601 | end if; | |
11602 | end loop; | |
11603 | ||
11604 | return False; | |
11605 | end Is_Checked_Storage_Pool; | |
11606 | ||
b0d71355 HK |
11607 | -- Local variables |
11608 | ||
11609 | Typ : constant Entity_Id := Etype (N); | |
11610 | Desig : constant Entity_Id := Available_View (Designated_Type (Typ)); | |
11611 | Loc : constant Source_Ptr := Sloc (N); | |
11612 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
11613 | Pnod : constant Node_Id := Parent (N); | |
11614 | ||
51dcceec AC |
11615 | Addr : Entity_Id; |
11616 | Alig : Entity_Id; | |
11617 | Deref : Node_Id; | |
11618 | Size : Entity_Id; | |
11619 | Size_Bits : Node_Id; | |
11620 | Stmt : Node_Id; | |
b0d71355 | 11621 | |
70482933 RK |
11622 | -- Start of processing for Insert_Dereference_Action |
11623 | ||
11624 | begin | |
e6f69614 AC |
11625 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
11626 | ||
b0d71355 HK |
11627 | -- Do not re-expand a dereference which has already been processed by |
11628 | -- this routine. | |
11629 | ||
11630 | if Has_Dereference_Action (Pnod) then | |
70482933 | 11631 | return; |
70482933 | 11632 | |
b0d71355 HK |
11633 | -- Do not perform this type of expansion for internally-generated |
11634 | -- dereferences. | |
70482933 | 11635 | |
b0d71355 HK |
11636 | elsif not Comes_From_Source (Original_Node (Pnod)) then |
11637 | return; | |
70482933 | 11638 | |
b0d71355 HK |
11639 | -- A dereference action is only applicable to objects which have been |
11640 | -- allocated on a checked pool. | |
70482933 | 11641 | |
b0d71355 HK |
11642 | elsif not Is_Checked_Storage_Pool (Pool) then |
11643 | return; | |
11644 | end if; | |
70482933 | 11645 | |
b0d71355 | 11646 | -- Extract the address of the dereferenced object. Generate: |
8777c5a6 | 11647 | |
b0d71355 | 11648 | -- Addr : System.Address := <N>'Pool_Address; |
70482933 | 11649 | |
b0d71355 | 11650 | Addr := Make_Temporary (Loc, 'P'); |
70482933 | 11651 | |
b0d71355 HK |
11652 | Insert_Action (N, |
11653 | Make_Object_Declaration (Loc, | |
11654 | Defining_Identifier => Addr, | |
11655 | Object_Definition => | |
e4494292 | 11656 | New_Occurrence_Of (RTE (RE_Address), Loc), |
b0d71355 HK |
11657 | Expression => |
11658 | Make_Attribute_Reference (Loc, | |
11659 | Prefix => Duplicate_Subexpr_Move_Checks (N), | |
11660 | Attribute_Name => Name_Pool_Address))); | |
11661 | ||
11662 | -- Calculate the size of the dereferenced object. Generate: | |
8777c5a6 | 11663 | |
b0d71355 HK |
11664 | -- Size : Storage_Count := <N>.all'Size / Storage_Unit; |
11665 | ||
11666 | Deref := | |
11667 | Make_Explicit_Dereference (Loc, | |
11668 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11669 | Set_Has_Dereference_Action (Deref); | |
70482933 | 11670 | |
51dcceec AC |
11671 | Size_Bits := |
11672 | Make_Attribute_Reference (Loc, | |
11673 | Prefix => Deref, | |
11674 | Attribute_Name => Name_Size); | |
11675 | ||
11676 | -- Special case of an unconstrained array: need to add descriptor size | |
11677 | ||
11678 | if Is_Array_Type (Desig) | |
11679 | and then not Is_Constrained (First_Subtype (Desig)) | |
11680 | then | |
11681 | Size_Bits := | |
11682 | Make_Op_Add (Loc, | |
11683 | Left_Opnd => | |
11684 | Make_Attribute_Reference (Loc, | |
11685 | Prefix => | |
11686 | New_Occurrence_Of (First_Subtype (Desig), Loc), | |
11687 | Attribute_Name => Name_Descriptor_Size), | |
11688 | Right_Opnd => Size_Bits); | |
11689 | end if; | |
b0d71355 | 11690 | |
51dcceec | 11691 | Size := Make_Temporary (Loc, 'S'); |
b0d71355 HK |
11692 | Insert_Action (N, |
11693 | Make_Object_Declaration (Loc, | |
11694 | Defining_Identifier => Size, | |
11695 | Object_Definition => | |
e4494292 | 11696 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
b0d71355 HK |
11697 | Expression => |
11698 | Make_Op_Divide (Loc, | |
51dcceec AC |
11699 | Left_Opnd => Size_Bits, |
11700 | Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit)))); | |
70482933 | 11701 | |
b0d71355 HK |
11702 | -- Calculate the alignment of the dereferenced object. Generate: |
11703 | -- Alig : constant Storage_Count := <N>.all'Alignment; | |
70482933 | 11704 | |
b0d71355 HK |
11705 | Deref := |
11706 | Make_Explicit_Dereference (Loc, | |
11707 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11708 | Set_Has_Dereference_Action (Deref); | |
11709 | ||
11710 | Alig := Make_Temporary (Loc, 'A'); | |
b0d71355 HK |
11711 | Insert_Action (N, |
11712 | Make_Object_Declaration (Loc, | |
11713 | Defining_Identifier => Alig, | |
11714 | Object_Definition => | |
e4494292 | 11715 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
b0d71355 HK |
11716 | Expression => |
11717 | Make_Attribute_Reference (Loc, | |
11718 | Prefix => Deref, | |
11719 | Attribute_Name => Name_Alignment))); | |
11720 | ||
11721 | -- A dereference of a controlled object requires special processing. The | |
11722 | -- finalization machinery requests additional space from the underlying | |
11723 | -- pool to allocate and hide two pointers. As a result, a checked pool | |
11724 | -- may mark the wrong memory as valid. Since checked pools do not have | |
11725 | -- knowledge of hidden pointers, we have to bring the two pointers back | |
11726 | -- in view in order to restore the original state of the object. | |
11727 | ||
11728 | if Needs_Finalization (Desig) then | |
11729 | ||
11730 | -- Adjust the address and size of the dereferenced object. Generate: | |
11731 | -- Adjust_Controlled_Dereference (Addr, Size, Alig); | |
11732 | ||
11733 | Stmt := | |
11734 | Make_Procedure_Call_Statement (Loc, | |
11735 | Name => | |
e4494292 | 11736 | New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc), |
b0d71355 | 11737 | Parameter_Associations => New_List ( |
e4494292 RD |
11738 | New_Occurrence_Of (Addr, Loc), |
11739 | New_Occurrence_Of (Size, Loc), | |
11740 | New_Occurrence_Of (Alig, Loc))); | |
b0d71355 HK |
11741 | |
11742 | -- Class-wide types complicate things because we cannot determine | |
11743 | -- statically whether the actual object is truly controlled. We must | |
11744 | -- generate a runtime check to detect this property. Generate: | |
11745 | -- | |
11746 | -- if Needs_Finalization (<N>.all'Tag) then | |
11747 | -- <Stmt>; | |
11748 | -- end if; | |
11749 | ||
11750 | if Is_Class_Wide_Type (Desig) then | |
11751 | Deref := | |
11752 | Make_Explicit_Dereference (Loc, | |
11753 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11754 | Set_Has_Dereference_Action (Deref); | |
11755 | ||
11756 | Stmt := | |
8b1011c0 | 11757 | Make_Implicit_If_Statement (N, |
b0d71355 HK |
11758 | Condition => |
11759 | Make_Function_Call (Loc, | |
11760 | Name => | |
e4494292 | 11761 | New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc), |
b0d71355 HK |
11762 | Parameter_Associations => New_List ( |
11763 | Make_Attribute_Reference (Loc, | |
11764 | Prefix => Deref, | |
11765 | Attribute_Name => Name_Tag))), | |
11766 | Then_Statements => New_List (Stmt)); | |
11767 | end if; | |
11768 | ||
11769 | Insert_Action (N, Stmt); | |
11770 | end if; | |
11771 | ||
11772 | -- Generate: | |
11773 | -- Dereference (Pool, Addr, Size, Alig); | |
11774 | ||
11775 | Insert_Action (N, | |
11776 | Make_Procedure_Call_Statement (Loc, | |
11777 | Name => | |
e4494292 | 11778 | New_Occurrence_Of |
b0d71355 HK |
11779 | (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), |
11780 | Parameter_Associations => New_List ( | |
e4494292 RD |
11781 | New_Occurrence_Of (Pool, Loc), |
11782 | New_Occurrence_Of (Addr, Loc), | |
11783 | New_Occurrence_Of (Size, Loc), | |
11784 | New_Occurrence_Of (Alig, Loc)))); | |
b0d71355 HK |
11785 | |
11786 | -- Mark the explicit dereference as processed to avoid potential | |
11787 | -- infinite expansion. | |
11788 | ||
11789 | Set_Has_Dereference_Action (Pnod); | |
70482933 | 11790 | |
fbf5a39b AC |
11791 | exception |
11792 | when RE_Not_Available => | |
11793 | return; | |
70482933 RK |
11794 | end Insert_Dereference_Action; |
11795 | ||
fdfcc663 AC |
11796 | -------------------------------- |
11797 | -- Integer_Promotion_Possible -- | |
11798 | -------------------------------- | |
11799 | ||
11800 | function Integer_Promotion_Possible (N : Node_Id) return Boolean is | |
11801 | Operand : constant Node_Id := Expression (N); | |
11802 | Operand_Type : constant Entity_Id := Etype (Operand); | |
11803 | Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type); | |
11804 | ||
11805 | begin | |
11806 | pragma Assert (Nkind (N) = N_Type_Conversion); | |
11807 | ||
11808 | return | |
11809 | ||
11810 | -- We only do the transformation for source constructs. We assume | |
11811 | -- that the expander knows what it is doing when it generates code. | |
11812 | ||
11813 | Comes_From_Source (N) | |
11814 | ||
11815 | -- If the operand type is Short_Integer or Short_Short_Integer, | |
11816 | -- then we will promote to Integer, which is available on all | |
11817 | -- targets, and is sufficient to ensure no intermediate overflow. | |
11818 | -- Furthermore it is likely to be as efficient or more efficient | |
11819 | -- than using the smaller type for the computation so we do this | |
11820 | -- unconditionally. | |
11821 | ||
11822 | and then | |
11823 | (Root_Operand_Type = Base_Type (Standard_Short_Integer) | |
761f7dcb | 11824 | or else |
fdfcc663 AC |
11825 | Root_Operand_Type = Base_Type (Standard_Short_Short_Integer)) |
11826 | ||
11827 | -- Test for interesting operation, which includes addition, | |
5f3f175d AC |
11828 | -- division, exponentiation, multiplication, subtraction, absolute |
11829 | -- value and unary negation. Unary "+" is omitted since it is a | |
11830 | -- no-op and thus can't overflow. | |
fdfcc663 | 11831 | |
5f3f175d AC |
11832 | and then Nkind_In (Operand, N_Op_Abs, |
11833 | N_Op_Add, | |
fdfcc663 AC |
11834 | N_Op_Divide, |
11835 | N_Op_Expon, | |
11836 | N_Op_Minus, | |
11837 | N_Op_Multiply, | |
11838 | N_Op_Subtract); | |
11839 | end Integer_Promotion_Possible; | |
11840 | ||
70482933 RK |
11841 | ------------------------------ |
11842 | -- Make_Array_Comparison_Op -- | |
11843 | ------------------------------ | |
11844 | ||
11845 | -- This is a hand-coded expansion of the following generic function: | |
11846 | ||
11847 | -- generic | |
11848 | -- type elem is (<>); | |
11849 | -- type index is (<>); | |
11850 | -- type a is array (index range <>) of elem; | |
20b5d666 | 11851 | |
70482933 RK |
11852 | -- function Gnnn (X : a; Y: a) return boolean is |
11853 | -- J : index := Y'first; | |
20b5d666 | 11854 | |
70482933 RK |
11855 | -- begin |
11856 | -- if X'length = 0 then | |
11857 | -- return false; | |
20b5d666 | 11858 | |
70482933 RK |
11859 | -- elsif Y'length = 0 then |
11860 | -- return true; | |
20b5d666 | 11861 | |
70482933 RK |
11862 | -- else |
11863 | -- for I in X'range loop | |
11864 | -- if X (I) = Y (J) then | |
11865 | -- if J = Y'last then | |
11866 | -- exit; | |
11867 | -- else | |
11868 | -- J := index'succ (J); | |
11869 | -- end if; | |
20b5d666 | 11870 | |
70482933 RK |
11871 | -- else |
11872 | -- return X (I) > Y (J); | |
11873 | -- end if; | |
11874 | -- end loop; | |
20b5d666 | 11875 | |
70482933 RK |
11876 | -- return X'length > Y'length; |
11877 | -- end if; | |
11878 | -- end Gnnn; | |
11879 | ||
11880 | -- Note that since we are essentially doing this expansion by hand, we | |
11881 | -- do not need to generate an actual or formal generic part, just the | |
11882 | -- instantiated function itself. | |
11883 | ||
bb012790 AC |
11884 | -- Perhaps we could have the actual generic available in the run-time, |
11885 | -- obtained by rtsfind, and actually expand a real instantiation ??? | |
11886 | ||
70482933 | 11887 | function Make_Array_Comparison_Op |
2e071734 AC |
11888 | (Typ : Entity_Id; |
11889 | Nod : Node_Id) return Node_Id | |
70482933 RK |
11890 | is |
11891 | Loc : constant Source_Ptr := Sloc (Nod); | |
11892 | ||
11893 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
11894 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
11895 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
11896 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
11897 | ||
11898 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
11899 | ||
11900 | Loop_Statement : Node_Id; | |
11901 | Loop_Body : Node_Id; | |
11902 | If_Stat : Node_Id; | |
11903 | Inner_If : Node_Id; | |
11904 | Final_Expr : Node_Id; | |
11905 | Func_Body : Node_Id; | |
11906 | Func_Name : Entity_Id; | |
11907 | Formals : List_Id; | |
11908 | Length1 : Node_Id; | |
11909 | Length2 : Node_Id; | |
11910 | ||
11911 | begin | |
11912 | -- if J = Y'last then | |
11913 | -- exit; | |
11914 | -- else | |
11915 | -- J := index'succ (J); | |
11916 | -- end if; | |
11917 | ||
11918 | Inner_If := | |
11919 | Make_Implicit_If_Statement (Nod, | |
11920 | Condition => | |
11921 | Make_Op_Eq (Loc, | |
e4494292 | 11922 | Left_Opnd => New_Occurrence_Of (J, Loc), |
70482933 RK |
11923 | Right_Opnd => |
11924 | Make_Attribute_Reference (Loc, | |
e4494292 | 11925 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
11926 | Attribute_Name => Name_Last)), |
11927 | ||
11928 | Then_Statements => New_List ( | |
11929 | Make_Exit_Statement (Loc)), | |
11930 | ||
11931 | Else_Statements => | |
11932 | New_List ( | |
11933 | Make_Assignment_Statement (Loc, | |
e4494292 | 11934 | Name => New_Occurrence_Of (J, Loc), |
70482933 RK |
11935 | Expression => |
11936 | Make_Attribute_Reference (Loc, | |
e4494292 | 11937 | Prefix => New_Occurrence_Of (Index, Loc), |
70482933 | 11938 | Attribute_Name => Name_Succ, |
e4494292 | 11939 | Expressions => New_List (New_Occurrence_Of (J, Loc)))))); |
70482933 RK |
11940 | |
11941 | -- if X (I) = Y (J) then | |
11942 | -- if ... end if; | |
11943 | -- else | |
11944 | -- return X (I) > Y (J); | |
11945 | -- end if; | |
11946 | ||
11947 | Loop_Body := | |
11948 | Make_Implicit_If_Statement (Nod, | |
11949 | Condition => | |
11950 | Make_Op_Eq (Loc, | |
11951 | Left_Opnd => | |
11952 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11953 | Prefix => New_Occurrence_Of (X, Loc), |
11954 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
11955 | |
11956 | Right_Opnd => | |
11957 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11958 | Prefix => New_Occurrence_Of (Y, Loc), |
11959 | Expressions => New_List (New_Occurrence_Of (J, Loc)))), | |
70482933 RK |
11960 | |
11961 | Then_Statements => New_List (Inner_If), | |
11962 | ||
11963 | Else_Statements => New_List ( | |
d766cee3 | 11964 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
11965 | Expression => |
11966 | Make_Op_Gt (Loc, | |
11967 | Left_Opnd => | |
11968 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11969 | Prefix => New_Occurrence_Of (X, Loc), |
11970 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
11971 | |
11972 | Right_Opnd => | |
11973 | Make_Indexed_Component (Loc, | |
e4494292 | 11974 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 | 11975 | Expressions => New_List ( |
e4494292 | 11976 | New_Occurrence_Of (J, Loc))))))); |
70482933 RK |
11977 | |
11978 | -- for I in X'range loop | |
11979 | -- if ... end if; | |
11980 | -- end loop; | |
11981 | ||
11982 | Loop_Statement := | |
11983 | Make_Implicit_Loop_Statement (Nod, | |
11984 | Identifier => Empty, | |
11985 | ||
11986 | Iteration_Scheme => | |
11987 | Make_Iteration_Scheme (Loc, | |
11988 | Loop_Parameter_Specification => | |
11989 | Make_Loop_Parameter_Specification (Loc, | |
11990 | Defining_Identifier => I, | |
11991 | Discrete_Subtype_Definition => | |
11992 | Make_Attribute_Reference (Loc, | |
e4494292 | 11993 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
11994 | Attribute_Name => Name_Range))), |
11995 | ||
11996 | Statements => New_List (Loop_Body)); | |
11997 | ||
11998 | -- if X'length = 0 then | |
11999 | -- return false; | |
12000 | -- elsif Y'length = 0 then | |
12001 | -- return true; | |
12002 | -- else | |
12003 | -- for ... loop ... end loop; | |
12004 | -- return X'length > Y'length; | |
12005 | -- end if; | |
12006 | ||
12007 | Length1 := | |
12008 | Make_Attribute_Reference (Loc, | |
e4494292 | 12009 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
12010 | Attribute_Name => Name_Length); |
12011 | ||
12012 | Length2 := | |
12013 | Make_Attribute_Reference (Loc, | |
e4494292 | 12014 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
12015 | Attribute_Name => Name_Length); |
12016 | ||
12017 | Final_Expr := | |
12018 | Make_Op_Gt (Loc, | |
12019 | Left_Opnd => Length1, | |
12020 | Right_Opnd => Length2); | |
12021 | ||
12022 | If_Stat := | |
12023 | Make_Implicit_If_Statement (Nod, | |
12024 | Condition => | |
12025 | Make_Op_Eq (Loc, | |
12026 | Left_Opnd => | |
12027 | Make_Attribute_Reference (Loc, | |
e4494292 | 12028 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
12029 | Attribute_Name => Name_Length), |
12030 | Right_Opnd => | |
12031 | Make_Integer_Literal (Loc, 0)), | |
12032 | ||
12033 | Then_Statements => | |
12034 | New_List ( | |
d766cee3 | 12035 | Make_Simple_Return_Statement (Loc, |
e4494292 | 12036 | Expression => New_Occurrence_Of (Standard_False, Loc))), |
70482933 RK |
12037 | |
12038 | Elsif_Parts => New_List ( | |
12039 | Make_Elsif_Part (Loc, | |
12040 | Condition => | |
12041 | Make_Op_Eq (Loc, | |
12042 | Left_Opnd => | |
12043 | Make_Attribute_Reference (Loc, | |
e4494292 | 12044 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
12045 | Attribute_Name => Name_Length), |
12046 | Right_Opnd => | |
12047 | Make_Integer_Literal (Loc, 0)), | |
12048 | ||
12049 | Then_Statements => | |
12050 | New_List ( | |
d766cee3 | 12051 | Make_Simple_Return_Statement (Loc, |
e4494292 | 12052 | Expression => New_Occurrence_Of (Standard_True, Loc))))), |
70482933 RK |
12053 | |
12054 | Else_Statements => New_List ( | |
12055 | Loop_Statement, | |
d766cee3 | 12056 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
12057 | Expression => Final_Expr))); |
12058 | ||
12059 | -- (X : a; Y: a) | |
12060 | ||
12061 | Formals := New_List ( | |
12062 | Make_Parameter_Specification (Loc, | |
12063 | Defining_Identifier => X, | |
e4494292 | 12064 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
12065 | |
12066 | Make_Parameter_Specification (Loc, | |
12067 | Defining_Identifier => Y, | |
e4494292 | 12068 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 RK |
12069 | |
12070 | -- function Gnnn (...) return boolean is | |
12071 | -- J : index := Y'first; | |
12072 | -- begin | |
12073 | -- if ... end if; | |
12074 | -- end Gnnn; | |
12075 | ||
191fcb3a | 12076 | Func_Name := Make_Temporary (Loc, 'G'); |
70482933 RK |
12077 | |
12078 | Func_Body := | |
12079 | Make_Subprogram_Body (Loc, | |
12080 | Specification => | |
12081 | Make_Function_Specification (Loc, | |
12082 | Defining_Unit_Name => Func_Name, | |
12083 | Parameter_Specifications => Formals, | |
e4494292 | 12084 | Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)), |
70482933 RK |
12085 | |
12086 | Declarations => New_List ( | |
12087 | Make_Object_Declaration (Loc, | |
12088 | Defining_Identifier => J, | |
e4494292 | 12089 | Object_Definition => New_Occurrence_Of (Index, Loc), |
70482933 RK |
12090 | Expression => |
12091 | Make_Attribute_Reference (Loc, | |
e4494292 | 12092 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
12093 | Attribute_Name => Name_First))), |
12094 | ||
12095 | Handled_Statement_Sequence => | |
12096 | Make_Handled_Sequence_Of_Statements (Loc, | |
12097 | Statements => New_List (If_Stat))); | |
12098 | ||
12099 | return Func_Body; | |
70482933 RK |
12100 | end Make_Array_Comparison_Op; |
12101 | ||
12102 | --------------------------- | |
12103 | -- Make_Boolean_Array_Op -- | |
12104 | --------------------------- | |
12105 | ||
685094bf RD |
12106 | -- For logical operations on boolean arrays, expand in line the following, |
12107 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
12108 | |
12109 | -- function Annn (A : typ; B: typ) return typ is | |
12110 | -- C : typ; | |
12111 | -- begin | |
12112 | -- for J in A'range loop | |
12113 | -- C (J) := A (J) op B (J); | |
12114 | -- end loop; | |
12115 | -- return C; | |
12116 | -- end Annn; | |
12117 | ||
12118 | -- Here typ is the boolean array type | |
12119 | ||
12120 | function Make_Boolean_Array_Op | |
2e071734 AC |
12121 | (Typ : Entity_Id; |
12122 | N : Node_Id) return Node_Id | |
70482933 RK |
12123 | is |
12124 | Loc : constant Source_Ptr := Sloc (N); | |
12125 | ||
12126 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
12127 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
12128 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
12129 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
12130 | ||
12131 | A_J : Node_Id; | |
12132 | B_J : Node_Id; | |
12133 | C_J : Node_Id; | |
12134 | Op : Node_Id; | |
12135 | ||
12136 | Formals : List_Id; | |
12137 | Func_Name : Entity_Id; | |
12138 | Func_Body : Node_Id; | |
12139 | Loop_Statement : Node_Id; | |
12140 | ||
12141 | begin | |
12142 | A_J := | |
12143 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12144 | Prefix => New_Occurrence_Of (A, Loc), |
12145 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12146 | |
12147 | B_J := | |
12148 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12149 | Prefix => New_Occurrence_Of (B, Loc), |
12150 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12151 | |
12152 | C_J := | |
12153 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12154 | Prefix => New_Occurrence_Of (C, Loc), |
12155 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12156 | |
12157 | if Nkind (N) = N_Op_And then | |
12158 | Op := | |
12159 | Make_Op_And (Loc, | |
12160 | Left_Opnd => A_J, | |
12161 | Right_Opnd => B_J); | |
12162 | ||
12163 | elsif Nkind (N) = N_Op_Or then | |
12164 | Op := | |
12165 | Make_Op_Or (Loc, | |
12166 | Left_Opnd => A_J, | |
12167 | Right_Opnd => B_J); | |
12168 | ||
12169 | else | |
12170 | Op := | |
12171 | Make_Op_Xor (Loc, | |
12172 | Left_Opnd => A_J, | |
12173 | Right_Opnd => B_J); | |
12174 | end if; | |
12175 | ||
12176 | Loop_Statement := | |
12177 | Make_Implicit_Loop_Statement (N, | |
12178 | Identifier => Empty, | |
12179 | ||
12180 | Iteration_Scheme => | |
12181 | Make_Iteration_Scheme (Loc, | |
12182 | Loop_Parameter_Specification => | |
12183 | Make_Loop_Parameter_Specification (Loc, | |
12184 | Defining_Identifier => J, | |
12185 | Discrete_Subtype_Definition => | |
12186 | Make_Attribute_Reference (Loc, | |
e4494292 | 12187 | Prefix => New_Occurrence_Of (A, Loc), |
70482933 RK |
12188 | Attribute_Name => Name_Range))), |
12189 | ||
12190 | Statements => New_List ( | |
12191 | Make_Assignment_Statement (Loc, | |
12192 | Name => C_J, | |
12193 | Expression => Op))); | |
12194 | ||
12195 | Formals := New_List ( | |
12196 | Make_Parameter_Specification (Loc, | |
12197 | Defining_Identifier => A, | |
e4494292 | 12198 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
12199 | |
12200 | Make_Parameter_Specification (Loc, | |
12201 | Defining_Identifier => B, | |
e4494292 | 12202 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 | 12203 | |
191fcb3a | 12204 | Func_Name := Make_Temporary (Loc, 'A'); |
70482933 RK |
12205 | Set_Is_Inlined (Func_Name); |
12206 | ||
12207 | Func_Body := | |
12208 | Make_Subprogram_Body (Loc, | |
12209 | Specification => | |
12210 | Make_Function_Specification (Loc, | |
12211 | Defining_Unit_Name => Func_Name, | |
12212 | Parameter_Specifications => Formals, | |
e4494292 | 12213 | Result_Definition => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
12214 | |
12215 | Declarations => New_List ( | |
12216 | Make_Object_Declaration (Loc, | |
12217 | Defining_Identifier => C, | |
e4494292 | 12218 | Object_Definition => New_Occurrence_Of (Typ, Loc))), |
70482933 RK |
12219 | |
12220 | Handled_Statement_Sequence => | |
12221 | Make_Handled_Sequence_Of_Statements (Loc, | |
12222 | Statements => New_List ( | |
12223 | Loop_Statement, | |
d766cee3 | 12224 | Make_Simple_Return_Statement (Loc, |
e4494292 | 12225 | Expression => New_Occurrence_Of (C, Loc))))); |
70482933 RK |
12226 | |
12227 | return Func_Body; | |
12228 | end Make_Boolean_Array_Op; | |
12229 | ||
b6b5cca8 AC |
12230 | ----------------------------------------- |
12231 | -- Minimized_Eliminated_Overflow_Check -- | |
12232 | ----------------------------------------- | |
12233 | ||
12234 | function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is | |
12235 | begin | |
12236 | return | |
12237 | Is_Signed_Integer_Type (Etype (N)) | |
a7f1b24f | 12238 | and then Overflow_Check_Mode in Minimized_Or_Eliminated; |
b6b5cca8 AC |
12239 | end Minimized_Eliminated_Overflow_Check; |
12240 | ||
0580d807 AC |
12241 | -------------------------------- |
12242 | -- Optimize_Length_Comparison -- | |
12243 | -------------------------------- | |
12244 | ||
12245 | procedure Optimize_Length_Comparison (N : Node_Id) is | |
12246 | Loc : constant Source_Ptr := Sloc (N); | |
12247 | Typ : constant Entity_Id := Etype (N); | |
12248 | Result : Node_Id; | |
12249 | ||
12250 | Left : Node_Id; | |
12251 | Right : Node_Id; | |
12252 | -- First and Last attribute reference nodes, which end up as left and | |
12253 | -- right operands of the optimized result. | |
12254 | ||
12255 | Is_Zero : Boolean; | |
12256 | -- True for comparison operand of zero | |
12257 | ||
12258 | Comp : Node_Id; | |
12259 | -- Comparison operand, set only if Is_Zero is false | |
12260 | ||
12261 | Ent : Entity_Id; | |
12262 | -- Entity whose length is being compared | |
12263 | ||
12264 | Index : Node_Id; | |
12265 | -- Integer_Literal node for length attribute expression, or Empty | |
12266 | -- if there is no such expression present. | |
12267 | ||
12268 | Ityp : Entity_Id; | |
12269 | -- Type of array index to which 'Length is applied | |
12270 | ||
12271 | Op : Node_Kind := Nkind (N); | |
12272 | -- Kind of comparison operator, gets flipped if operands backwards | |
12273 | ||
12274 | function Is_Optimizable (N : Node_Id) return Boolean; | |
abcd9db2 AC |
12275 | -- Tests N to see if it is an optimizable comparison value (defined as |
12276 | -- constant zero or one, or something else where the value is known to | |
12277 | -- be positive and in the range of 32-bits, and where the corresponding | |
12278 | -- Length value is also known to be 32-bits. If result is true, sets | |
12279 | -- Is_Zero, Ityp, and Comp accordingly. | |
0580d807 AC |
12280 | |
12281 | function Is_Entity_Length (N : Node_Id) return Boolean; | |
12282 | -- Tests if N is a length attribute applied to a simple entity. If so, | |
12283 | -- returns True, and sets Ent to the entity, and Index to the integer | |
12284 | -- literal provided as an attribute expression, or to Empty if none. | |
12285 | -- Also returns True if the expression is a generated type conversion | |
12286 | -- whose expression is of the desired form. This latter case arises | |
12287 | -- when Apply_Universal_Integer_Attribute_Check installs a conversion | |
12288 | -- to check for being in range, which is not needed in this context. | |
12289 | -- Returns False if neither condition holds. | |
12290 | ||
12291 | function Prepare_64 (N : Node_Id) return Node_Id; | |
12292 | -- Given a discrete expression, returns a Long_Long_Integer typed | |
12293 | -- expression representing the underlying value of the expression. | |
12294 | -- This is done with an unchecked conversion to the result type. We | |
12295 | -- use unchecked conversion to handle the enumeration type case. | |
12296 | ||
12297 | ---------------------- | |
12298 | -- Is_Entity_Length -- | |
12299 | ---------------------- | |
12300 | ||
12301 | function Is_Entity_Length (N : Node_Id) return Boolean is | |
12302 | begin | |
12303 | if Nkind (N) = N_Attribute_Reference | |
12304 | and then Attribute_Name (N) = Name_Length | |
12305 | and then Is_Entity_Name (Prefix (N)) | |
12306 | then | |
12307 | Ent := Entity (Prefix (N)); | |
12308 | ||
12309 | if Present (Expressions (N)) then | |
12310 | Index := First (Expressions (N)); | |
12311 | else | |
12312 | Index := Empty; | |
12313 | end if; | |
12314 | ||
12315 | return True; | |
12316 | ||
12317 | elsif Nkind (N) = N_Type_Conversion | |
12318 | and then not Comes_From_Source (N) | |
12319 | then | |
12320 | return Is_Entity_Length (Expression (N)); | |
12321 | ||
12322 | else | |
12323 | return False; | |
12324 | end if; | |
12325 | end Is_Entity_Length; | |
12326 | ||
12327 | -------------------- | |
12328 | -- Is_Optimizable -- | |
12329 | -------------------- | |
12330 | ||
12331 | function Is_Optimizable (N : Node_Id) return Boolean is | |
12332 | Val : Uint; | |
12333 | OK : Boolean; | |
12334 | Lo : Uint; | |
12335 | Hi : Uint; | |
12336 | Indx : Node_Id; | |
12337 | ||
12338 | begin | |
12339 | if Compile_Time_Known_Value (N) then | |
12340 | Val := Expr_Value (N); | |
12341 | ||
12342 | if Val = Uint_0 then | |
12343 | Is_Zero := True; | |
12344 | Comp := Empty; | |
12345 | return True; | |
12346 | ||
12347 | elsif Val = Uint_1 then | |
12348 | Is_Zero := False; | |
12349 | Comp := Empty; | |
12350 | return True; | |
12351 | end if; | |
12352 | end if; | |
12353 | ||
12354 | -- Here we have to make sure of being within 32-bits | |
12355 | ||
12356 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); | |
12357 | ||
12358 | if not OK | |
abcd9db2 | 12359 | or else Lo < Uint_1 |
0580d807 AC |
12360 | or else Hi > UI_From_Int (Int'Last) |
12361 | then | |
12362 | return False; | |
12363 | end if; | |
12364 | ||
abcd9db2 AC |
12365 | -- Comparison value was within range, so now we must check the index |
12366 | -- value to make sure it is also within 32-bits. | |
0580d807 AC |
12367 | |
12368 | Indx := First_Index (Etype (Ent)); | |
12369 | ||
12370 | if Present (Index) then | |
12371 | for J in 2 .. UI_To_Int (Intval (Index)) loop | |
12372 | Next_Index (Indx); | |
12373 | end loop; | |
12374 | end if; | |
12375 | ||
12376 | Ityp := Etype (Indx); | |
12377 | ||
12378 | if Esize (Ityp) > 32 then | |
12379 | return False; | |
12380 | end if; | |
12381 | ||
12382 | Is_Zero := False; | |
12383 | Comp := N; | |
12384 | return True; | |
12385 | end Is_Optimizable; | |
12386 | ||
12387 | ---------------- | |
12388 | -- Prepare_64 -- | |
12389 | ---------------- | |
12390 | ||
12391 | function Prepare_64 (N : Node_Id) return Node_Id is | |
12392 | begin | |
12393 | return Unchecked_Convert_To (Standard_Long_Long_Integer, N); | |
12394 | end Prepare_64; | |
12395 | ||
12396 | -- Start of processing for Optimize_Length_Comparison | |
12397 | ||
12398 | begin | |
12399 | -- Nothing to do if not a comparison | |
12400 | ||
12401 | if Op not in N_Op_Compare then | |
12402 | return; | |
12403 | end if; | |
12404 | ||
12405 | -- Nothing to do if special -gnatd.P debug flag set | |
12406 | ||
12407 | if Debug_Flag_Dot_PP then | |
12408 | return; | |
12409 | end if; | |
12410 | ||
12411 | -- Ent'Length op 0/1 | |
12412 | ||
12413 | if Is_Entity_Length (Left_Opnd (N)) | |
12414 | and then Is_Optimizable (Right_Opnd (N)) | |
12415 | then | |
12416 | null; | |
12417 | ||
12418 | -- 0/1 op Ent'Length | |
12419 | ||
12420 | elsif Is_Entity_Length (Right_Opnd (N)) | |
12421 | and then Is_Optimizable (Left_Opnd (N)) | |
12422 | then | |
12423 | -- Flip comparison to opposite sense | |
12424 | ||
12425 | case Op is | |
12426 | when N_Op_Lt => Op := N_Op_Gt; | |
12427 | when N_Op_Le => Op := N_Op_Ge; | |
12428 | when N_Op_Gt => Op := N_Op_Lt; | |
12429 | when N_Op_Ge => Op := N_Op_Le; | |
12430 | when others => null; | |
12431 | end case; | |
12432 | ||
12433 | -- Else optimization not possible | |
12434 | ||
12435 | else | |
12436 | return; | |
12437 | end if; | |
12438 | ||
12439 | -- Fall through if we will do the optimization | |
12440 | ||
12441 | -- Cases to handle: | |
12442 | ||
12443 | -- X'Length = 0 => X'First > X'Last | |
12444 | -- X'Length = 1 => X'First = X'Last | |
12445 | -- X'Length = n => X'First + (n - 1) = X'Last | |
12446 | ||
12447 | -- X'Length /= 0 => X'First <= X'Last | |
12448 | -- X'Length /= 1 => X'First /= X'Last | |
12449 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
12450 | ||
12451 | -- X'Length >= 0 => always true, warn | |
12452 | -- X'Length >= 1 => X'First <= X'Last | |
12453 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
12454 | ||
12455 | -- X'Length > 0 => X'First <= X'Last | |
12456 | -- X'Length > 1 => X'First < X'Last | |
12457 | -- X'Length > n => X'First + (n - 1) < X'Last | |
12458 | ||
12459 | -- X'Length <= 0 => X'First > X'Last (warn, could be =) | |
12460 | -- X'Length <= 1 => X'First >= X'Last | |
12461 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
12462 | ||
12463 | -- X'Length < 0 => always false (warn) | |
12464 | -- X'Length < 1 => X'First > X'Last | |
12465 | -- X'Length < n => X'First + (n - 1) > X'Last | |
12466 | ||
12467 | -- Note: for the cases of n (not constant 0,1), we require that the | |
12468 | -- corresponding index type be integer or shorter (i.e. not 64-bit), | |
12469 | -- and the same for the comparison value. Then we do the comparison | |
12470 | -- using 64-bit arithmetic (actually long long integer), so that we | |
12471 | -- cannot have overflow intefering with the result. | |
12472 | ||
12473 | -- First deal with warning cases | |
12474 | ||
12475 | if Is_Zero then | |
12476 | case Op is | |
12477 | ||
12478 | -- X'Length >= 0 | |
12479 | ||
12480 | when N_Op_Ge => | |
12481 | Rewrite (N, | |
12482 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc))); | |
12483 | Analyze_And_Resolve (N, Typ); | |
12484 | Warn_On_Known_Condition (N); | |
12485 | return; | |
12486 | ||
12487 | -- X'Length < 0 | |
12488 | ||
12489 | when N_Op_Lt => | |
12490 | Rewrite (N, | |
12491 | Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc))); | |
12492 | Analyze_And_Resolve (N, Typ); | |
12493 | Warn_On_Known_Condition (N); | |
12494 | return; | |
12495 | ||
12496 | when N_Op_Le => | |
12497 | if Constant_Condition_Warnings | |
12498 | and then Comes_From_Source (Original_Node (N)) | |
12499 | then | |
324ac540 | 12500 | Error_Msg_N ("could replace by ""'=""?c?", N); |
0580d807 AC |
12501 | end if; |
12502 | ||
12503 | Op := N_Op_Eq; | |
12504 | ||
12505 | when others => | |
12506 | null; | |
12507 | end case; | |
12508 | end if; | |
12509 | ||
12510 | -- Build the First reference we will use | |
12511 | ||
12512 | Left := | |
12513 | Make_Attribute_Reference (Loc, | |
12514 | Prefix => New_Occurrence_Of (Ent, Loc), | |
12515 | Attribute_Name => Name_First); | |
12516 | ||
12517 | if Present (Index) then | |
12518 | Set_Expressions (Left, New_List (New_Copy (Index))); | |
12519 | end if; | |
12520 | ||
12521 | -- If general value case, then do the addition of (n - 1), and | |
12522 | -- also add the needed conversions to type Long_Long_Integer. | |
12523 | ||
12524 | if Present (Comp) then | |
12525 | Left := | |
12526 | Make_Op_Add (Loc, | |
12527 | Left_Opnd => Prepare_64 (Left), | |
12528 | Right_Opnd => | |
12529 | Make_Op_Subtract (Loc, | |
12530 | Left_Opnd => Prepare_64 (Comp), | |
12531 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
12532 | end if; | |
12533 | ||
12534 | -- Build the Last reference we will use | |
12535 | ||
12536 | Right := | |
12537 | Make_Attribute_Reference (Loc, | |
12538 | Prefix => New_Occurrence_Of (Ent, Loc), | |
12539 | Attribute_Name => Name_Last); | |
12540 | ||
12541 | if Present (Index) then | |
12542 | Set_Expressions (Right, New_List (New_Copy (Index))); | |
12543 | end if; | |
12544 | ||
12545 | -- If general operand, convert Last reference to Long_Long_Integer | |
12546 | ||
12547 | if Present (Comp) then | |
12548 | Right := Prepare_64 (Right); | |
12549 | end if; | |
12550 | ||
12551 | -- Check for cases to optimize | |
12552 | ||
12553 | -- X'Length = 0 => X'First > X'Last | |
12554 | -- X'Length < 1 => X'First > X'Last | |
12555 | -- X'Length < n => X'First + (n - 1) > X'Last | |
12556 | ||
12557 | if (Is_Zero and then Op = N_Op_Eq) | |
12558 | or else (not Is_Zero and then Op = N_Op_Lt) | |
12559 | then | |
12560 | Result := | |
12561 | Make_Op_Gt (Loc, | |
12562 | Left_Opnd => Left, | |
12563 | Right_Opnd => Right); | |
12564 | ||
12565 | -- X'Length = 1 => X'First = X'Last | |
12566 | -- X'Length = n => X'First + (n - 1) = X'Last | |
12567 | ||
12568 | elsif not Is_Zero and then Op = N_Op_Eq then | |
12569 | Result := | |
12570 | Make_Op_Eq (Loc, | |
12571 | Left_Opnd => Left, | |
12572 | Right_Opnd => Right); | |
12573 | ||
12574 | -- X'Length /= 0 => X'First <= X'Last | |
12575 | -- X'Length > 0 => X'First <= X'Last | |
12576 | ||
12577 | elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then | |
12578 | Result := | |
12579 | Make_Op_Le (Loc, | |
12580 | Left_Opnd => Left, | |
12581 | Right_Opnd => Right); | |
12582 | ||
12583 | -- X'Length /= 1 => X'First /= X'Last | |
12584 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
12585 | ||
12586 | elsif not Is_Zero and then Op = N_Op_Ne then | |
12587 | Result := | |
12588 | Make_Op_Ne (Loc, | |
12589 | Left_Opnd => Left, | |
12590 | Right_Opnd => Right); | |
12591 | ||
12592 | -- X'Length >= 1 => X'First <= X'Last | |
12593 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
12594 | ||
12595 | elsif not Is_Zero and then Op = N_Op_Ge then | |
12596 | Result := | |
12597 | Make_Op_Le (Loc, | |
12598 | Left_Opnd => Left, | |
12599 | Right_Opnd => Right); | |
12600 | ||
12601 | -- X'Length > 1 => X'First < X'Last | |
12602 | -- X'Length > n => X'First + (n = 1) < X'Last | |
12603 | ||
12604 | elsif not Is_Zero and then Op = N_Op_Gt then | |
12605 | Result := | |
12606 | Make_Op_Lt (Loc, | |
12607 | Left_Opnd => Left, | |
12608 | Right_Opnd => Right); | |
12609 | ||
12610 | -- X'Length <= 1 => X'First >= X'Last | |
12611 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
12612 | ||
12613 | elsif not Is_Zero and then Op = N_Op_Le then | |
12614 | Result := | |
12615 | Make_Op_Ge (Loc, | |
12616 | Left_Opnd => Left, | |
12617 | Right_Opnd => Right); | |
12618 | ||
12619 | -- Should not happen at this stage | |
12620 | ||
12621 | else | |
12622 | raise Program_Error; | |
12623 | end if; | |
12624 | ||
12625 | -- Rewrite and finish up | |
12626 | ||
12627 | Rewrite (N, Result); | |
12628 | Analyze_And_Resolve (N, Typ); | |
12629 | return; | |
12630 | end Optimize_Length_Comparison; | |
12631 | ||
b2c28399 AC |
12632 | ------------------------------ |
12633 | -- Process_Transient_Object -- | |
12634 | ------------------------------ | |
12635 | ||
12636 | procedure Process_Transient_Object | |
12637 | (Decl : Node_Id; | |
12638 | Rel_Node : Node_Id) | |
12639 | is | |
8942b30c AC |
12640 | Loc : constant Source_Ptr := Sloc (Decl); |
12641 | Obj_Id : constant Entity_Id := Defining_Identifier (Decl); | |
12642 | Obj_Typ : constant Node_Id := Etype (Obj_Id); | |
12643 | Desig_Typ : Entity_Id; | |
12644 | Expr : Node_Id; | |
12645 | Fin_Stmts : List_Id; | |
12646 | Ptr_Id : Entity_Id; | |
12647 | Temp_Id : Entity_Id; | |
12648 | Temp_Ins : Node_Id; | |
12649 | ||
9ab5d86b | 12650 | Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node); |
8942b30c AC |
12651 | -- Node on which to insert the hook pointer (as an action): the |
12652 | -- innermost enclosing non-transient scope. | |
b2c28399 | 12653 | |
064f4527 TQ |
12654 | Finalization_Context : Node_Id; |
12655 | -- Node after which to insert finalization actions | |
12656 | ||
12657 | Finalize_Always : Boolean; | |
9ab5d86b RD |
12658 | -- If False, call to finalizer includes a test of whether the hook |
12659 | -- pointer is null. | |
b2c28399 | 12660 | |
8942b30c AC |
12661 | begin |
12662 | -- Step 0: determine where to attach finalization actions in the tree | |
064f4527 | 12663 | |
8942b30c AC |
12664 | -- Special case for Boolean EWAs: capture expression in a temporary, |
12665 | -- whose declaration will serve as the context around which to insert | |
12666 | -- finalization code. The finalization thus remains local to the | |
12667 | -- specific condition being evaluated. | |
064f4527 | 12668 | |
8942b30c | 12669 | if Is_Boolean_Type (Etype (Rel_Node)) then |
064f4527 | 12670 | |
9ab5d86b RD |
12671 | -- In this case, the finalization context is chosen so that we know |
12672 | -- at finalization point that the hook pointer is never null, so no | |
12673 | -- need for a test, we can call the finalizer unconditionally, except | |
12674 | -- in the case where the object is created in a specific branch of a | |
12675 | -- conditional expression. | |
064f4527 | 12676 | |
8942b30c | 12677 | Finalize_Always := |
c5c780e6 HK |
12678 | not Within_Case_Or_If_Expression (Rel_Node) |
12679 | and then not Nkind_In | |
12680 | (Original_Node (Rel_Node), N_Case_Expression, | |
12681 | N_If_Expression); | |
064f4527 | 12682 | |
8942b30c AC |
12683 | declare |
12684 | Loc : constant Source_Ptr := Sloc (Rel_Node); | |
12685 | Temp : constant Entity_Id := Make_Temporary (Loc, 'E', Rel_Node); | |
b2c28399 | 12686 | |
8942b30c AC |
12687 | begin |
12688 | Append_To (Actions (Rel_Node), | |
12689 | Make_Object_Declaration (Loc, | |
12690 | Defining_Identifier => Temp, | |
12691 | Constant_Present => True, | |
12692 | Object_Definition => | |
12693 | New_Occurrence_Of (Etype (Rel_Node), Loc), | |
12694 | Expression => Expression (Rel_Node))); | |
12695 | Finalization_Context := Last (Actions (Rel_Node)); | |
b2c28399 | 12696 | |
8942b30c | 12697 | Analyze (Last (Actions (Rel_Node))); |
b2c28399 | 12698 | |
8942b30c AC |
12699 | Set_Expression (Rel_Node, New_Occurrence_Of (Temp, Loc)); |
12700 | Analyze (Expression (Rel_Node)); | |
12701 | end; | |
b2c28399 | 12702 | |
8942b30c AC |
12703 | else |
12704 | Finalize_Always := False; | |
12705 | Finalization_Context := Hook_Context; | |
12706 | end if; | |
064f4527 | 12707 | |
b2c28399 AC |
12708 | -- Step 1: Create the access type which provides a reference to the |
12709 | -- transient controlled object. | |
12710 | ||
12711 | if Is_Access_Type (Obj_Typ) then | |
12712 | Desig_Typ := Directly_Designated_Type (Obj_Typ); | |
12713 | else | |
12714 | Desig_Typ := Obj_Typ; | |
12715 | end if; | |
12716 | ||
12717 | Desig_Typ := Base_Type (Desig_Typ); | |
12718 | ||
12719 | -- Generate: | |
12720 | -- Ann : access [all] <Desig_Typ>; | |
12721 | ||
12722 | Ptr_Id := Make_Temporary (Loc, 'A'); | |
12723 | ||
064f4527 | 12724 | Insert_Action (Hook_Context, |
b2c28399 AC |
12725 | Make_Full_Type_Declaration (Loc, |
12726 | Defining_Identifier => Ptr_Id, | |
12727 | Type_Definition => | |
12728 | Make_Access_To_Object_Definition (Loc, | |
12729 | All_Present => Ekind (Obj_Typ) = E_General_Access_Type, | |
e4494292 | 12730 | Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)))); |
b2c28399 AC |
12731 | |
12732 | -- Step 2: Create a temporary which acts as a hook to the transient | |
12733 | -- controlled object. Generate: | |
12734 | ||
12735 | -- Temp : Ptr_Id := null; | |
12736 | ||
12737 | Temp_Id := Make_Temporary (Loc, 'T'); | |
12738 | ||
064f4527 | 12739 | Insert_Action (Hook_Context, |
b2c28399 AC |
12740 | Make_Object_Declaration (Loc, |
12741 | Defining_Identifier => Temp_Id, | |
e4494292 | 12742 | Object_Definition => New_Occurrence_Of (Ptr_Id, Loc))); |
b2c28399 AC |
12743 | |
12744 | -- Mark the temporary as created for the purposes of exporting the | |
12745 | -- transient controlled object out of the expression_with_action or if | |
12746 | -- expression. This signals the machinery in Build_Finalizer to treat | |
12747 | -- this case specially. | |
12748 | ||
12749 | Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl); | |
12750 | ||
12751 | -- Step 3: Hook the transient object to the temporary | |
12752 | ||
a7d08a38 AC |
12753 | -- This must be inserted right after the object declaration, so that |
12754 | -- the assignment is executed if, and only if, the object is actually | |
12755 | -- created (whereas the declaration of the hook pointer, and the | |
12756 | -- finalization call, may be inserted at an outer level, and may | |
12757 | -- remain unused for some executions, if the actual creation of | |
12758 | -- the object is conditional). | |
12759 | ||
b2c28399 AC |
12760 | -- The use of unchecked conversion / unrestricted access is needed to |
12761 | -- avoid an accessibility violation. Note that the finalization code is | |
12762 | -- structured in such a way that the "hook" is processed only when it | |
12763 | -- points to an existing object. | |
12764 | ||
12765 | if Is_Access_Type (Obj_Typ) then | |
e4494292 RD |
12766 | Expr := |
12767 | Unchecked_Convert_To (Ptr_Id, New_Occurrence_Of (Obj_Id, Loc)); | |
b2c28399 AC |
12768 | else |
12769 | Expr := | |
12770 | Make_Attribute_Reference (Loc, | |
e4494292 | 12771 | Prefix => New_Occurrence_Of (Obj_Id, Loc), |
b2c28399 AC |
12772 | Attribute_Name => Name_Unrestricted_Access); |
12773 | end if; | |
12774 | ||
12775 | -- Generate: | |
12776 | -- Temp := Ptr_Id (Obj_Id); | |
12777 | -- <or> | |
12778 | -- Temp := Obj_Id'Unrestricted_Access; | |
12779 | ||
97779c34 AC |
12780 | -- When the transient object is initialized by an aggregate, the hook |
12781 | -- must capture the object after the last component assignment takes | |
12782 | -- place. Only then is the object fully initialized. | |
12783 | ||
12784 | if Ekind (Obj_Id) = E_Variable | |
12785 | and then Present (Last_Aggregate_Assignment (Obj_Id)) | |
12786 | then | |
12787 | Temp_Ins := Last_Aggregate_Assignment (Obj_Id); | |
12788 | ||
12789 | -- Otherwise the hook seizes the related object immediately | |
12790 | ||
12791 | else | |
12792 | Temp_Ins := Decl; | |
12793 | end if; | |
12794 | ||
12795 | Insert_After_And_Analyze (Temp_Ins, | |
a7d08a38 | 12796 | Make_Assignment_Statement (Loc, |
e4494292 | 12797 | Name => New_Occurrence_Of (Temp_Id, Loc), |
a7d08a38 | 12798 | Expression => Expr)); |
b2c28399 AC |
12799 | |
12800 | -- Step 4: Finalize the transient controlled object after the context | |
12801 | -- has been evaluated/elaborated. Generate: | |
12802 | ||
12803 | -- if Temp /= null then | |
12804 | -- [Deep_]Finalize (Temp.all); | |
12805 | -- Temp := null; | |
12806 | -- end if; | |
12807 | ||
12808 | -- When the node is part of a return statement, there is no need to | |
12809 | -- insert a finalization call, as the general finalization mechanism | |
12810 | -- (see Build_Finalizer) would take care of the transient controlled | |
12811 | -- object on subprogram exit. Note that it would also be impossible to | |
12812 | -- insert the finalization code after the return statement as this will | |
12813 | -- render it unreachable. | |
12814 | ||
064f4527 TQ |
12815 | if Nkind (Finalization_Context) /= N_Simple_Return_Statement then |
12816 | Fin_Stmts := New_List ( | |
12817 | Make_Final_Call | |
12818 | (Obj_Ref => | |
12819 | Make_Explicit_Dereference (Loc, | |
e4494292 | 12820 | Prefix => New_Occurrence_Of (Temp_Id, Loc)), |
064f4527 | 12821 | Typ => Desig_Typ), |
b2c28399 | 12822 | |
064f4527 | 12823 | Make_Assignment_Statement (Loc, |
e4494292 | 12824 | Name => New_Occurrence_Of (Temp_Id, Loc), |
064f4527 | 12825 | Expression => Make_Null (Loc))); |
b2c28399 | 12826 | |
064f4527 TQ |
12827 | if not Finalize_Always then |
12828 | Fin_Stmts := New_List ( | |
12829 | Make_Implicit_If_Statement (Decl, | |
12830 | Condition => | |
12831 | Make_Op_Ne (Loc, | |
e4494292 | 12832 | Left_Opnd => New_Occurrence_Of (Temp_Id, Loc), |
064f4527 TQ |
12833 | Right_Opnd => Make_Null (Loc)), |
12834 | Then_Statements => Fin_Stmts)); | |
12835 | end if; | |
b2c28399 | 12836 | |
064f4527 | 12837 | Insert_Actions_After (Finalization_Context, Fin_Stmts); |
b2c28399 AC |
12838 | end if; |
12839 | end Process_Transient_Object; | |
12840 | ||
70482933 RK |
12841 | ------------------------ |
12842 | -- Rewrite_Comparison -- | |
12843 | ------------------------ | |
12844 | ||
12845 | procedure Rewrite_Comparison (N : Node_Id) is | |
c800f862 RD |
12846 | Warning_Generated : Boolean := False; |
12847 | -- Set to True if first pass with Assume_Valid generates a warning in | |
12848 | -- which case we skip the second pass to avoid warning overloaded. | |
12849 | ||
12850 | Result : Node_Id; | |
12851 | -- Set to Standard_True or Standard_False | |
12852 | ||
d26dc4b5 AC |
12853 | begin |
12854 | if Nkind (N) = N_Type_Conversion then | |
12855 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 12856 | return; |
70482933 | 12857 | |
d26dc4b5 | 12858 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
12859 | return; |
12860 | end if; | |
70482933 | 12861 | |
c800f862 RD |
12862 | -- Now start looking at the comparison in detail. We potentially go |
12863 | -- through this loop twice. The first time, Assume_Valid is set False | |
12864 | -- in the call to Compile_Time_Compare. If this call results in a | |
12865 | -- clear result of always True or Always False, that's decisive and | |
12866 | -- we are done. Otherwise we repeat the processing with Assume_Valid | |
e7e4d230 | 12867 | -- set to True to generate additional warnings. We can skip that step |
c800f862 RD |
12868 | -- if Constant_Condition_Warnings is False. |
12869 | ||
12870 | for AV in False .. True loop | |
12871 | declare | |
12872 | Typ : constant Entity_Id := Etype (N); | |
12873 | Op1 : constant Node_Id := Left_Opnd (N); | |
12874 | Op2 : constant Node_Id := Right_Opnd (N); | |
70482933 | 12875 | |
c800f862 RD |
12876 | Res : constant Compare_Result := |
12877 | Compile_Time_Compare (Op1, Op2, Assume_Valid => AV); | |
12878 | -- Res indicates if compare outcome can be compile time determined | |
f02b8bb8 | 12879 | |
c800f862 RD |
12880 | True_Result : Boolean; |
12881 | False_Result : Boolean; | |
f02b8bb8 | 12882 | |
c800f862 RD |
12883 | begin |
12884 | case N_Op_Compare (Nkind (N)) is | |
d26dc4b5 AC |
12885 | when N_Op_Eq => |
12886 | True_Result := Res = EQ; | |
12887 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
12888 | ||
12889 | when N_Op_Ge => | |
12890 | True_Result := Res in Compare_GE; | |
12891 | False_Result := Res = LT; | |
12892 | ||
12893 | if Res = LE | |
12894 | and then Constant_Condition_Warnings | |
12895 | and then Comes_From_Source (Original_Node (N)) | |
12896 | and then Nkind (Original_Node (N)) = N_Op_Ge | |
12897 | and then not In_Instance | |
d26dc4b5 | 12898 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 12899 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 12900 | then |
ed2233dc | 12901 | Error_Msg_N |
324ac540 AC |
12902 | ("can never be greater than, could replace by ""'=""?c?", |
12903 | N); | |
c800f862 | 12904 | Warning_Generated := True; |
d26dc4b5 | 12905 | end if; |
70482933 | 12906 | |
d26dc4b5 AC |
12907 | when N_Op_Gt => |
12908 | True_Result := Res = GT; | |
12909 | False_Result := Res in Compare_LE; | |
12910 | ||
12911 | when N_Op_Lt => | |
12912 | True_Result := Res = LT; | |
12913 | False_Result := Res in Compare_GE; | |
12914 | ||
12915 | when N_Op_Le => | |
12916 | True_Result := Res in Compare_LE; | |
12917 | False_Result := Res = GT; | |
12918 | ||
12919 | if Res = GE | |
12920 | and then Constant_Condition_Warnings | |
12921 | and then Comes_From_Source (Original_Node (N)) | |
12922 | and then Nkind (Original_Node (N)) = N_Op_Le | |
12923 | and then not In_Instance | |
d26dc4b5 | 12924 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 12925 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 12926 | then |
ed2233dc | 12927 | Error_Msg_N |
324ac540 | 12928 | ("can never be less than, could replace by ""'=""?c?", N); |
c800f862 | 12929 | Warning_Generated := True; |
d26dc4b5 | 12930 | end if; |
70482933 | 12931 | |
d26dc4b5 AC |
12932 | when N_Op_Ne => |
12933 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
12934 | False_Result := Res = EQ; | |
c800f862 | 12935 | end case; |
d26dc4b5 | 12936 | |
c800f862 RD |
12937 | -- If this is the first iteration, then we actually convert the |
12938 | -- comparison into True or False, if the result is certain. | |
d26dc4b5 | 12939 | |
c800f862 RD |
12940 | if AV = False then |
12941 | if True_Result or False_Result then | |
21791d97 | 12942 | Result := Boolean_Literals (True_Result); |
c800f862 RD |
12943 | Rewrite (N, |
12944 | Convert_To (Typ, | |
12945 | New_Occurrence_Of (Result, Sloc (N)))); | |
12946 | Analyze_And_Resolve (N, Typ); | |
12947 | Warn_On_Known_Condition (N); | |
12948 | return; | |
12949 | end if; | |
12950 | ||
12951 | -- If this is the second iteration (AV = True), and the original | |
e7e4d230 AC |
12952 | -- node comes from source and we are not in an instance, then give |
12953 | -- a warning if we know result would be True or False. Note: we | |
12954 | -- know Constant_Condition_Warnings is set if we get here. | |
c800f862 RD |
12955 | |
12956 | elsif Comes_From_Source (Original_Node (N)) | |
12957 | and then not In_Instance | |
12958 | then | |
12959 | if True_Result then | |
ed2233dc | 12960 | Error_Msg_N |
324ac540 | 12961 | ("condition can only be False if invalid values present??", |
c800f862 RD |
12962 | N); |
12963 | elsif False_Result then | |
ed2233dc | 12964 | Error_Msg_N |
324ac540 | 12965 | ("condition can only be True if invalid values present??", |
c800f862 RD |
12966 | N); |
12967 | end if; | |
12968 | end if; | |
12969 | end; | |
12970 | ||
12971 | -- Skip second iteration if not warning on constant conditions or | |
e7e4d230 AC |
12972 | -- if the first iteration already generated a warning of some kind or |
12973 | -- if we are in any case assuming all values are valid (so that the | |
12974 | -- first iteration took care of the valid case). | |
c800f862 RD |
12975 | |
12976 | exit when not Constant_Condition_Warnings; | |
12977 | exit when Warning_Generated; | |
12978 | exit when Assume_No_Invalid_Values; | |
12979 | end loop; | |
70482933 RK |
12980 | end Rewrite_Comparison; |
12981 | ||
fbf5a39b AC |
12982 | ---------------------------- |
12983 | -- Safe_In_Place_Array_Op -- | |
12984 | ---------------------------- | |
12985 | ||
12986 | function Safe_In_Place_Array_Op | |
2e071734 AC |
12987 | (Lhs : Node_Id; |
12988 | Op1 : Node_Id; | |
12989 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
12990 | is |
12991 | Target : Entity_Id; | |
12992 | ||
12993 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
12994 | -- Operand is safe if it cannot overlap part of the target of the | |
12995 | -- operation. If the operand and the target are identical, the operand | |
12996 | -- is safe. The operand can be empty in the case of negation. | |
12997 | ||
12998 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 12999 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
13000 | |
13001 | ------------------ | |
13002 | -- Is_Unaliased -- | |
13003 | ------------------ | |
13004 | ||
13005 | function Is_Unaliased (N : Node_Id) return Boolean is | |
13006 | begin | |
13007 | return | |
13008 | Is_Entity_Name (N) | |
13009 | and then No (Address_Clause (Entity (N))) | |
13010 | and then No (Renamed_Object (Entity (N))); | |
13011 | end Is_Unaliased; | |
13012 | ||
13013 | --------------------- | |
13014 | -- Is_Safe_Operand -- | |
13015 | --------------------- | |
13016 | ||
13017 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
13018 | begin | |
13019 | if No (Op) then | |
13020 | return True; | |
13021 | ||
13022 | elsif Is_Entity_Name (Op) then | |
13023 | return Is_Unaliased (Op); | |
13024 | ||
303b4d58 | 13025 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
13026 | return Is_Unaliased (Prefix (Op)); |
13027 | ||
13028 | elsif Nkind (Op) = N_Slice then | |
13029 | return | |
13030 | Is_Unaliased (Prefix (Op)) | |
13031 | and then Entity (Prefix (Op)) /= Target; | |
13032 | ||
13033 | elsif Nkind (Op) = N_Op_Not then | |
13034 | return Is_Safe_Operand (Right_Opnd (Op)); | |
13035 | ||
13036 | else | |
13037 | return False; | |
13038 | end if; | |
13039 | end Is_Safe_Operand; | |
13040 | ||
b6b5cca8 | 13041 | -- Start of processing for Safe_In_Place_Array_Op |
fbf5a39b AC |
13042 | |
13043 | begin | |
685094bf RD |
13044 | -- Skip this processing if the component size is different from system |
13045 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b | 13046 | |
eaa826f8 | 13047 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then |
fbf5a39b AC |
13048 | return False; |
13049 | ||
26bff3d9 | 13050 | -- Cannot do in place stuff on VM_Target since cannot pass addresses |
fbf5a39b | 13051 | |
26bff3d9 | 13052 | elsif VM_Target /= No_VM then |
fbf5a39b AC |
13053 | return False; |
13054 | ||
13055 | -- Cannot do in place stuff if non-standard Boolean representation | |
13056 | ||
eaa826f8 | 13057 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then |
fbf5a39b AC |
13058 | return False; |
13059 | ||
13060 | elsif not Is_Unaliased (Lhs) then | |
13061 | return False; | |
e7e4d230 | 13062 | |
fbf5a39b AC |
13063 | else |
13064 | Target := Entity (Lhs); | |
e7e4d230 | 13065 | return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2); |
fbf5a39b AC |
13066 | end if; |
13067 | end Safe_In_Place_Array_Op; | |
13068 | ||
70482933 RK |
13069 | ----------------------- |
13070 | -- Tagged_Membership -- | |
13071 | ----------------------- | |
13072 | ||
685094bf RD |
13073 | -- There are two different cases to consider depending on whether the right |
13074 | -- operand is a class-wide type or not. If not we just compare the actual | |
13075 | -- tag of the left expr to the target type tag: | |
70482933 RK |
13076 | -- |
13077 | -- Left_Expr.Tag = Right_Type'Tag; | |
13078 | -- | |
685094bf RD |
13079 | -- If it is a class-wide type we use the RT function CW_Membership which is |
13080 | -- usually implemented by looking in the ancestor tables contained in the | |
13081 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 13082 | |
0669bebe GB |
13083 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
13084 | -- function IW_Membership which is usually implemented by looking in the | |
13085 | -- table of abstract interface types plus the ancestor table contained in | |
13086 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
13087 | ||
82878151 AC |
13088 | procedure Tagged_Membership |
13089 | (N : Node_Id; | |
13090 | SCIL_Node : out Node_Id; | |
13091 | Result : out Node_Id) | |
13092 | is | |
70482933 RK |
13093 | Left : constant Node_Id := Left_Opnd (N); |
13094 | Right : constant Node_Id := Right_Opnd (N); | |
13095 | Loc : constant Source_Ptr := Sloc (N); | |
13096 | ||
38171f43 | 13097 | Full_R_Typ : Entity_Id; |
70482933 | 13098 | Left_Type : Entity_Id; |
82878151 | 13099 | New_Node : Node_Id; |
70482933 RK |
13100 | Right_Type : Entity_Id; |
13101 | Obj_Tag : Node_Id; | |
13102 | ||
13103 | begin | |
82878151 AC |
13104 | SCIL_Node := Empty; |
13105 | ||
852dba80 AC |
13106 | -- Handle entities from the limited view |
13107 | ||
13108 | Left_Type := Available_View (Etype (Left)); | |
13109 | Right_Type := Available_View (Etype (Right)); | |
70482933 | 13110 | |
6cce2156 GD |
13111 | -- In the case where the type is an access type, the test is applied |
13112 | -- using the designated types (needed in Ada 2012 for implicit anonymous | |
13113 | -- access conversions, for AI05-0149). | |
13114 | ||
13115 | if Is_Access_Type (Right_Type) then | |
13116 | Left_Type := Designated_Type (Left_Type); | |
13117 | Right_Type := Designated_Type (Right_Type); | |
13118 | end if; | |
13119 | ||
70482933 RK |
13120 | if Is_Class_Wide_Type (Left_Type) then |
13121 | Left_Type := Root_Type (Left_Type); | |
13122 | end if; | |
13123 | ||
38171f43 AC |
13124 | if Is_Class_Wide_Type (Right_Type) then |
13125 | Full_R_Typ := Underlying_Type (Root_Type (Right_Type)); | |
13126 | else | |
13127 | Full_R_Typ := Underlying_Type (Right_Type); | |
13128 | end if; | |
13129 | ||
70482933 RK |
13130 | Obj_Tag := |
13131 | Make_Selected_Component (Loc, | |
13132 | Prefix => Relocate_Node (Left), | |
a9d8907c | 13133 | Selector_Name => |
e4494292 | 13134 | New_Occurrence_Of (First_Tag_Component (Left_Type), Loc)); |
70482933 RK |
13135 | |
13136 | if Is_Class_Wide_Type (Right_Type) then | |
758c442c | 13137 | |
0669bebe GB |
13138 | -- No need to issue a run-time check if we statically know that the |
13139 | -- result of this membership test is always true. For example, | |
13140 | -- considering the following declarations: | |
13141 | ||
13142 | -- type Iface is interface; | |
13143 | -- type T is tagged null record; | |
13144 | -- type DT is new T and Iface with null record; | |
13145 | ||
13146 | -- Obj1 : T; | |
13147 | -- Obj2 : DT; | |
13148 | ||
13149 | -- These membership tests are always true: | |
13150 | ||
13151 | -- Obj1 in T'Class | |
13152 | -- Obj2 in T'Class; | |
13153 | -- Obj2 in Iface'Class; | |
13154 | ||
13155 | -- We do not need to handle cases where the membership is illegal. | |
13156 | -- For example: | |
13157 | ||
13158 | -- Obj1 in DT'Class; -- Compile time error | |
13159 | -- Obj1 in Iface'Class; -- Compile time error | |
13160 | ||
13161 | if not Is_Class_Wide_Type (Left_Type) | |
4ac2477e JM |
13162 | and then (Is_Ancestor (Etype (Right_Type), Left_Type, |
13163 | Use_Full_View => True) | |
533369aa AC |
13164 | or else (Is_Interface (Etype (Right_Type)) |
13165 | and then Interface_Present_In_Ancestor | |
761f7dcb AC |
13166 | (Typ => Left_Type, |
13167 | Iface => Etype (Right_Type)))) | |
0669bebe | 13168 | then |
e4494292 | 13169 | Result := New_Occurrence_Of (Standard_True, Loc); |
82878151 | 13170 | return; |
0669bebe GB |
13171 | end if; |
13172 | ||
758c442c GD |
13173 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
13174 | ||
630d30e9 RD |
13175 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
13176 | ||
0669bebe | 13177 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
13178 | |
13179 | or else Is_Interface (Left_Type) | |
13180 | then | |
dfd99a80 TQ |
13181 | -- Issue error if IW_Membership operation not available in a |
13182 | -- configurable run time setting. | |
13183 | ||
13184 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
13185 | Error_Msg_CRT |
13186 | ("dynamic membership test on interface types", N); | |
82878151 AC |
13187 | Result := Empty; |
13188 | return; | |
dfd99a80 TQ |
13189 | end if; |
13190 | ||
82878151 | 13191 | Result := |
758c442c GD |
13192 | Make_Function_Call (Loc, |
13193 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
13194 | Parameter_Associations => New_List ( | |
13195 | Make_Attribute_Reference (Loc, | |
13196 | Prefix => Obj_Tag, | |
13197 | Attribute_Name => Name_Address), | |
e4494292 | 13198 | New_Occurrence_Of ( |
38171f43 | 13199 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), |
758c442c GD |
13200 | Loc))); |
13201 | ||
13202 | -- Ada 95: Normal case | |
13203 | ||
13204 | else | |
82878151 AC |
13205 | Build_CW_Membership (Loc, |
13206 | Obj_Tag_Node => Obj_Tag, | |
13207 | Typ_Tag_Node => | |
e4494292 | 13208 | New_Occurrence_Of ( |
38171f43 | 13209 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc), |
82878151 AC |
13210 | Related_Nod => N, |
13211 | New_Node => New_Node); | |
13212 | ||
13213 | -- Generate the SCIL node for this class-wide membership test. | |
13214 | -- Done here because the previous call to Build_CW_Membership | |
13215 | -- relocates Obj_Tag. | |
13216 | ||
13217 | if Generate_SCIL then | |
13218 | SCIL_Node := Make_SCIL_Membership_Test (Sloc (N)); | |
13219 | Set_SCIL_Entity (SCIL_Node, Etype (Right_Type)); | |
13220 | Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag); | |
13221 | end if; | |
13222 | ||
13223 | Result := New_Node; | |
758c442c GD |
13224 | end if; |
13225 | ||
0669bebe GB |
13226 | -- Right_Type is not a class-wide type |
13227 | ||
70482933 | 13228 | else |
0669bebe GB |
13229 | -- No need to check the tag of the object if Right_Typ is abstract |
13230 | ||
13231 | if Is_Abstract_Type (Right_Type) then | |
e4494292 | 13232 | Result := New_Occurrence_Of (Standard_False, Loc); |
0669bebe GB |
13233 | |
13234 | else | |
82878151 | 13235 | Result := |
0669bebe GB |
13236 | Make_Op_Eq (Loc, |
13237 | Left_Opnd => Obj_Tag, | |
13238 | Right_Opnd => | |
e4494292 | 13239 | New_Occurrence_Of |
38171f43 | 13240 | (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc)); |
0669bebe | 13241 | end if; |
70482933 | 13242 | end if; |
70482933 RK |
13243 | end Tagged_Membership; |
13244 | ||
13245 | ------------------------------ | |
13246 | -- Unary_Op_Validity_Checks -- | |
13247 | ------------------------------ | |
13248 | ||
13249 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
13250 | begin | |
13251 | if Validity_Checks_On and Validity_Check_Operands then | |
13252 | Ensure_Valid (Right_Opnd (N)); | |
13253 | end if; | |
13254 | end Unary_Op_Validity_Checks; | |
13255 | ||
13256 | end Exp_Ch4; |