]>
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 | |
154 | -- to attach bodies of local functions that are created in the process. | |
3058f181 | 155 | -- It is the responsibility of the caller to insert those bodies at the |
685094bf RD |
156 | -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs |
157 | -- are the left and right sides for the comparison, and Typ is the type of | |
3058f181 | 158 | -- the 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 | |
d3f70b35 | 1127 | Set_Finalization_Master (Def_Id, Finalization_Master (PtrT)); |
df3e68b1 HK |
1128 | |
1129 | Set_Associated_Storage_Pool (Def_Id, | |
1130 | Associated_Storage_Pool (PtrT)); | |
758c442c | 1131 | |
26bff3d9 JM |
1132 | -- Declare the object using the previous type declaration |
1133 | ||
1134 | if Aggr_In_Place then | |
df3e68b1 | 1135 | Temp_Decl := |
26bff3d9 JM |
1136 | Make_Object_Declaration (Loc, |
1137 | Defining_Identifier => Temp, | |
e4494292 | 1138 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
26bff3d9 JM |
1139 | Expression => |
1140 | Make_Allocator (Loc, | |
e4494292 | 1141 | New_Occurrence_Of (Etype (Exp), Loc))); |
26bff3d9 | 1142 | |
fad0600d AC |
1143 | -- Copy the Comes_From_Source flag for the allocator we just |
1144 | -- built, since logically this allocator is a replacement of | |
1145 | -- the original allocator node. This is for proper handling | |
1146 | -- of restriction No_Implicit_Heap_Allocations. | |
1147 | ||
26bff3d9 | 1148 | Set_Comes_From_Source |
df3e68b1 | 1149 | (Expression (Temp_Decl), Comes_From_Source (N)); |
26bff3d9 | 1150 | |
df3e68b1 HK |
1151 | Set_No_Initialization (Expression (Temp_Decl)); |
1152 | Insert_Action (N, Temp_Decl); | |
26bff3d9 | 1153 | |
ca5af305 | 1154 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1155 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
26bff3d9 | 1156 | |
26bff3d9 JM |
1157 | else |
1158 | Node := Relocate_Node (N); | |
1159 | Set_Analyzed (Node); | |
df3e68b1 HK |
1160 | |
1161 | Temp_Decl := | |
26bff3d9 JM |
1162 | Make_Object_Declaration (Loc, |
1163 | Defining_Identifier => Temp, | |
1164 | Constant_Present => True, | |
e4494292 | 1165 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
df3e68b1 HK |
1166 | Expression => Node); |
1167 | ||
1168 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 1169 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
26bff3d9 JM |
1170 | end if; |
1171 | ||
1172 | -- Generate an additional object containing the address of the | |
1173 | -- returned object. The type of this second object declaration | |
685094bf RD |
1174 | -- is the correct type required for the common processing that |
1175 | -- is still performed by this subprogram. The displacement of | |
1176 | -- this pointer to reference the component associated with the | |
1177 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
1178 | |
1179 | New_Decl := | |
1180 | Make_Object_Declaration (Loc, | |
243cae0a | 1181 | Defining_Identifier => Make_Temporary (Loc, 'P'), |
e4494292 | 1182 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
243cae0a | 1183 | Expression => |
df3e68b1 | 1184 | Unchecked_Convert_To (PtrT, |
e4494292 | 1185 | New_Occurrence_Of (Temp, Loc))); |
26bff3d9 JM |
1186 | |
1187 | Insert_Action (N, New_Decl); | |
1188 | ||
df3e68b1 HK |
1189 | Temp_Decl := New_Decl; |
1190 | Temp := Defining_Identifier (New_Decl); | |
26bff3d9 | 1191 | end; |
758c442c GD |
1192 | end if; |
1193 | ||
26bff3d9 JM |
1194 | Apply_Accessibility_Check (Temp); |
1195 | ||
1196 | -- Generate the tag assignment | |
1197 | ||
1198 | -- Suppress the tag assignment when VM_Target because VM tags are | |
1199 | -- represented implicitly in objects. | |
1200 | ||
1f110335 | 1201 | if not Tagged_Type_Expansion then |
26bff3d9 | 1202 | null; |
fbf5a39b | 1203 | |
26bff3d9 JM |
1204 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
1205 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 1206 | |
26bff3d9 JM |
1207 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
1208 | pragma Assert (Is_Class_Wide_Type | |
1209 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
1210 | null; |
1211 | ||
1212 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
1213 | TagT := T; | |
e4494292 | 1214 | TagR := New_Occurrence_Of (Temp, Loc); |
d26dc4b5 AC |
1215 | |
1216 | elsif Is_Private_Type (T) | |
1217 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 1218 | then |
d26dc4b5 | 1219 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
1220 | TagR := |
1221 | Unchecked_Convert_To (Underlying_Type (T), | |
1222 | Make_Explicit_Dereference (Loc, | |
e4494292 | 1223 | Prefix => New_Occurrence_Of (Temp, Loc))); |
d26dc4b5 AC |
1224 | end if; |
1225 | ||
1226 | if Present (TagT) then | |
38171f43 AC |
1227 | declare |
1228 | Full_T : constant Entity_Id := Underlying_Type (TagT); | |
e4494292 | 1229 | |
38171f43 AC |
1230 | begin |
1231 | Tag_Assign := | |
1232 | Make_Assignment_Statement (Loc, | |
cc6f5d75 | 1233 | Name => |
38171f43 | 1234 | Make_Selected_Component (Loc, |
cc6f5d75 | 1235 | Prefix => TagR, |
38171f43 | 1236 | Selector_Name => |
e4494292 RD |
1237 | New_Occurrence_Of |
1238 | (First_Tag_Component (Full_T), Loc)), | |
1239 | ||
38171f43 AC |
1240 | Expression => |
1241 | Unchecked_Convert_To (RTE (RE_Tag), | |
e4494292 | 1242 | New_Occurrence_Of |
38171f43 AC |
1243 | (Elists.Node |
1244 | (First_Elmt (Access_Disp_Table (Full_T))), Loc))); | |
1245 | end; | |
fbf5a39b AC |
1246 | |
1247 | -- The previous assignment has to be done in any case | |
1248 | ||
1249 | Set_Assignment_OK (Name (Tag_Assign)); | |
1250 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
1251 | end if; |
1252 | ||
533369aa AC |
1253 | if Needs_Finalization (DesigT) and then Needs_Finalization (T) then |
1254 | ||
df3e68b1 HK |
1255 | -- Generate an Adjust call if the object will be moved. In Ada |
1256 | -- 2005, the object may be inherently limited, in which case | |
1257 | -- there is no Adjust procedure, and the object is built in | |
1258 | -- place. In Ada 95, the object can be limited but not | |
1259 | -- inherently limited if this allocator came from a return | |
1260 | -- statement (we're allocating the result on the secondary | |
1261 | -- stack). In that case, the object will be moved, so we _do_ | |
1262 | -- want to Adjust. | |
1263 | ||
1264 | if not Aggr_In_Place | |
51245e2d | 1265 | and then not Is_Limited_View (T) |
df3e68b1 HK |
1266 | then |
1267 | Insert_Action (N, | |
fbf5a39b | 1268 | |
533369aa AC |
1269 | -- An unchecked conversion is needed in the classwide case |
1270 | -- because the designated type can be an ancestor of the | |
1271 | -- subtype mark of the allocator. | |
fbf5a39b | 1272 | |
533369aa AC |
1273 | Make_Adjust_Call |
1274 | (Obj_Ref => | |
1275 | Unchecked_Convert_To (T, | |
1276 | Make_Explicit_Dereference (Loc, | |
e4494292 | 1277 | Prefix => New_Occurrence_Of (Temp, Loc))), |
533369aa | 1278 | Typ => T)); |
df3e68b1 | 1279 | end if; |
b254da66 AC |
1280 | |
1281 | -- Generate: | |
1282 | -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access); | |
1283 | ||
2bfa5484 | 1284 | -- Do not generate this call in the following cases: |
c5f5123f | 1285 | |
2bfa5484 HK |
1286 | -- * .NET/JVM - these targets do not support address arithmetic |
1287 | -- and unchecked conversion, key elements of Finalize_Address. | |
c5f5123f | 1288 | |
2bfa5484 HK |
1289 | -- * CodePeer mode - TSS primitive Finalize_Address is not |
1290 | -- created in this mode. | |
b254da66 AC |
1291 | |
1292 | if VM_Target = No_VM | |
1293 | and then not CodePeer_Mode | |
1294 | and then Present (Finalization_Master (PtrT)) | |
f7bb41af AC |
1295 | and then Present (Temp_Decl) |
1296 | and then Nkind (Expression (Temp_Decl)) = N_Allocator | |
b254da66 AC |
1297 | then |
1298 | Insert_Action (N, | |
1299 | Make_Set_Finalize_Address_Call | |
1300 | (Loc => Loc, | |
1301 | Typ => T, | |
1302 | Ptr_Typ => PtrT)); | |
1303 | end if; | |
fbf5a39b AC |
1304 | end if; |
1305 | ||
e4494292 | 1306 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1307 | Analyze_And_Resolve (N, PtrT); |
1308 | ||
685094bf RD |
1309 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
1310 | -- component containing the secondary dispatch table of the interface | |
1311 | -- type. | |
26bff3d9 JM |
1312 | |
1313 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
1314 | Displace_Allocator_Pointer (N); | |
1315 | end if; | |
1316 | ||
fbf5a39b | 1317 | elsif Aggr_In_Place then |
e86a3a7e | 1318 | Temp := Make_Temporary (Loc, 'P', N); |
df3e68b1 | 1319 | Temp_Decl := |
fbf5a39b AC |
1320 | Make_Object_Declaration (Loc, |
1321 | Defining_Identifier => Temp, | |
e4494292 | 1322 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
df3e68b1 HK |
1323 | Expression => |
1324 | Make_Allocator (Loc, | |
e4494292 | 1325 | Expression => New_Occurrence_Of (Etype (Exp), Loc))); |
fbf5a39b | 1326 | |
fad0600d AC |
1327 | -- Copy the Comes_From_Source flag for the allocator we just built, |
1328 | -- since logically this allocator is a replacement of the original | |
1329 | -- allocator node. This is for proper handling of restriction | |
1330 | -- No_Implicit_Heap_Allocations. | |
1331 | ||
fbf5a39b | 1332 | Set_Comes_From_Source |
df3e68b1 HK |
1333 | (Expression (Temp_Decl), Comes_From_Source (N)); |
1334 | ||
1335 | Set_No_Initialization (Expression (Temp_Decl)); | |
1336 | Insert_Action (N, Temp_Decl); | |
1337 | ||
ca5af305 | 1338 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1339 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fbf5a39b | 1340 | |
d3f70b35 AC |
1341 | -- Attach the object to the associated finalization master. Thisis |
1342 | -- done manually on .NET/JVM since those compilers do no support | |
deb8dacc HK |
1343 | -- pools and cannot benefit from internally generated Allocate and |
1344 | -- Deallocate procedures. | |
1345 | ||
1346 | if VM_Target /= No_VM | |
1347 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1348 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1349 | then |
1350 | Insert_Action (N, | |
243cae0a | 1351 | Make_Attach_Call |
e4494292 | 1352 | (Obj_Ref => New_Occurrence_Of (Temp, Loc), |
243cae0a | 1353 | Ptr_Typ => PtrT)); |
deb8dacc HK |
1354 | end if; |
1355 | ||
e4494292 | 1356 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1357 | Analyze_And_Resolve (N, PtrT); |
1358 | ||
533369aa | 1359 | elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then |
51e4c4b9 AC |
1360 | Install_Null_Excluding_Check (Exp); |
1361 | ||
f02b8bb8 | 1362 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
1363 | and then Nkind (Exp) = N_Allocator |
1364 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
1365 | then | |
0da2c8ac | 1366 | -- Apply constraint to designated subtype indication |
fbf5a39b | 1367 | |
cc6f5d75 AC |
1368 | Apply_Constraint_Check |
1369 | (Expression (Exp), Designated_Type (DesigT), No_Sliding => True); | |
fbf5a39b AC |
1370 | |
1371 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
1372 | ||
1373 | -- Propagate constraint_error to enclosing allocator | |
1374 | ||
1375 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
1376 | end if; | |
1df4f514 | 1377 | |
fbf5a39b | 1378 | else |
14f0f659 AC |
1379 | Build_Allocate_Deallocate_Proc (N, True); |
1380 | ||
36c73552 AC |
1381 | -- If we have: |
1382 | -- type A is access T1; | |
1383 | -- X : A := new T2'(...); | |
1384 | -- T1 and T2 can be different subtypes, and we might need to check | |
1385 | -- both constraints. First check against the type of the qualified | |
1386 | -- expression. | |
1387 | ||
1388 | Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
fbf5a39b | 1389 | |
d79e621a | 1390 | if Do_Range_Check (Exp) then |
d79e621a GD |
1391 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); |
1392 | end if; | |
1393 | ||
685094bf RD |
1394 | -- A check is also needed in cases where the designated subtype is |
1395 | -- constrained and differs from the subtype given in the qualified | |
1396 | -- expression. Note that the check on the qualified expression does | |
1397 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 1398 | |
f02b8bb8 | 1399 | if Is_Constrained (DesigT) |
9450205a | 1400 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
1401 | then |
1402 | Apply_Constraint_Check | |
f02b8bb8 | 1403 | (Exp, DesigT, No_Sliding => False); |
d79e621a GD |
1404 | |
1405 | if Do_Range_Check (Exp) then | |
d79e621a GD |
1406 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); |
1407 | end if; | |
f02b8bb8 RD |
1408 | end if; |
1409 | ||
685094bf RD |
1410 | -- For an access to unconstrained packed array, GIGI needs to see an |
1411 | -- expression with a constrained subtype in order to compute the | |
1412 | -- proper size for the allocator. | |
f02b8bb8 RD |
1413 | |
1414 | if Is_Array_Type (T) | |
1415 | and then not Is_Constrained (T) | |
1416 | and then Is_Packed (T) | |
1417 | then | |
1418 | declare | |
191fcb3a | 1419 | ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A'); |
f02b8bb8 RD |
1420 | Internal_Exp : constant Node_Id := Relocate_Node (Exp); |
1421 | begin | |
1422 | Insert_Action (Exp, | |
1423 | Make_Subtype_Declaration (Loc, | |
1424 | Defining_Identifier => ConstrT, | |
25ebc085 AC |
1425 | Subtype_Indication => |
1426 | Make_Subtype_From_Expr (Internal_Exp, T))); | |
f02b8bb8 RD |
1427 | Freeze_Itype (ConstrT, Exp); |
1428 | Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp)); | |
1429 | end; | |
fbf5a39b | 1430 | end if; |
f02b8bb8 | 1431 | |
685094bf RD |
1432 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
1433 | -- to a build-in-place function, then access to the allocated object | |
1434 | -- must be passed to the function. Currently we limit such functions | |
1435 | -- to those with constrained limited result subtypes, but eventually | |
1436 | -- we plan to expand the allowed forms of functions that are treated | |
1437 | -- as build-in-place. | |
20b5d666 | 1438 | |
0791fbe9 | 1439 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
1440 | and then Is_Build_In_Place_Function_Call (Exp) |
1441 | then | |
1442 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
1443 | end if; | |
fbf5a39b AC |
1444 | end if; |
1445 | ||
1446 | exception | |
1447 | when RE_Not_Available => | |
1448 | return; | |
1449 | end Expand_Allocator_Expression; | |
1450 | ||
70482933 RK |
1451 | ----------------------------- |
1452 | -- Expand_Array_Comparison -- | |
1453 | ----------------------------- | |
1454 | ||
685094bf RD |
1455 | -- Expansion is only required in the case of array types. For the unpacked |
1456 | -- case, an appropriate runtime routine is called. For packed cases, and | |
1457 | -- also in some other cases where a runtime routine cannot be called, the | |
1458 | -- form of the expansion is: | |
70482933 RK |
1459 | |
1460 | -- [body for greater_nn; boolean_expression] | |
1461 | ||
1462 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
1463 | -- Boolean expression depends on the operator involved. | |
1464 | ||
1465 | procedure Expand_Array_Comparison (N : Node_Id) is | |
1466 | Loc : constant Source_Ptr := Sloc (N); | |
1467 | Op1 : Node_Id := Left_Opnd (N); | |
1468 | Op2 : Node_Id := Right_Opnd (N); | |
1469 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 1470 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
1471 | |
1472 | Expr : Node_Id; | |
1473 | Func_Body : Node_Id; | |
1474 | Func_Name : Entity_Id; | |
1475 | ||
fbf5a39b AC |
1476 | Comp : RE_Id; |
1477 | ||
9bc43c53 AC |
1478 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
1479 | -- True for byte addressable target | |
91b1417d | 1480 | |
fbf5a39b | 1481 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
685094bf RD |
1482 | -- Returns True if the length of the given operand is known to be less |
1483 | -- than 4. Returns False if this length is known to be four or greater | |
1484 | -- or is not known at compile time. | |
fbf5a39b AC |
1485 | |
1486 | ------------------------ | |
1487 | -- Length_Less_Than_4 -- | |
1488 | ------------------------ | |
1489 | ||
1490 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
1491 | Otyp : constant Entity_Id := Etype (Opnd); | |
1492 | ||
1493 | begin | |
1494 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
1495 | return String_Literal_Length (Otyp) < 4; | |
1496 | ||
1497 | else | |
1498 | declare | |
1499 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
1500 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
1501 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
1502 | Lov : Uint; | |
1503 | Hiv : Uint; | |
1504 | ||
1505 | begin | |
1506 | if Compile_Time_Known_Value (Lo) then | |
1507 | Lov := Expr_Value (Lo); | |
1508 | else | |
1509 | return False; | |
1510 | end if; | |
1511 | ||
1512 | if Compile_Time_Known_Value (Hi) then | |
1513 | Hiv := Expr_Value (Hi); | |
1514 | else | |
1515 | return False; | |
1516 | end if; | |
1517 | ||
1518 | return Hiv < Lov + 3; | |
1519 | end; | |
1520 | end if; | |
1521 | end Length_Less_Than_4; | |
1522 | ||
1523 | -- Start of processing for Expand_Array_Comparison | |
1524 | ||
70482933 | 1525 | begin |
fbf5a39b AC |
1526 | -- Deal first with unpacked case, where we can call a runtime routine |
1527 | -- except that we avoid this for targets for which are not addressable | |
26bff3d9 | 1528 | -- by bytes, and for the JVM/CIL, since they do not support direct |
fbf5a39b AC |
1529 | -- addressing of array components. |
1530 | ||
1531 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 1532 | and then Byte_Addressable |
26bff3d9 | 1533 | and then VM_Target = No_VM |
fbf5a39b AC |
1534 | then |
1535 | -- The call we generate is: | |
1536 | ||
1537 | -- Compare_Array_xn[_Unaligned] | |
1538 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
1539 | ||
1540 | -- x = U for unsigned, S for signed | |
1541 | -- n = 8,16,32,64 for component size | |
1542 | -- Add _Unaligned if length < 4 and component size is 8. | |
1543 | -- <op> is the standard comparison operator | |
1544 | ||
1545 | if Component_Size (Typ1) = 8 then | |
1546 | if Length_Less_Than_4 (Op1) | |
1547 | or else | |
1548 | Length_Less_Than_4 (Op2) | |
1549 | then | |
1550 | if Is_Unsigned_Type (Ctyp) then | |
1551 | Comp := RE_Compare_Array_U8_Unaligned; | |
1552 | else | |
1553 | Comp := RE_Compare_Array_S8_Unaligned; | |
1554 | end if; | |
1555 | ||
1556 | else | |
1557 | if Is_Unsigned_Type (Ctyp) then | |
1558 | Comp := RE_Compare_Array_U8; | |
1559 | else | |
1560 | Comp := RE_Compare_Array_S8; | |
1561 | end if; | |
1562 | end if; | |
1563 | ||
1564 | elsif Component_Size (Typ1) = 16 then | |
1565 | if Is_Unsigned_Type (Ctyp) then | |
1566 | Comp := RE_Compare_Array_U16; | |
1567 | else | |
1568 | Comp := RE_Compare_Array_S16; | |
1569 | end if; | |
1570 | ||
1571 | elsif Component_Size (Typ1) = 32 then | |
1572 | if Is_Unsigned_Type (Ctyp) then | |
1573 | Comp := RE_Compare_Array_U32; | |
1574 | else | |
1575 | Comp := RE_Compare_Array_S32; | |
1576 | end if; | |
1577 | ||
1578 | else pragma Assert (Component_Size (Typ1) = 64); | |
1579 | if Is_Unsigned_Type (Ctyp) then | |
1580 | Comp := RE_Compare_Array_U64; | |
1581 | else | |
1582 | Comp := RE_Compare_Array_S64; | |
1583 | end if; | |
1584 | end if; | |
1585 | ||
1586 | Remove_Side_Effects (Op1, Name_Req => True); | |
1587 | Remove_Side_Effects (Op2, Name_Req => True); | |
1588 | ||
1589 | Rewrite (Op1, | |
1590 | Make_Function_Call (Sloc (Op1), | |
1591 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
1592 | ||
1593 | Parameter_Associations => New_List ( | |
1594 | Make_Attribute_Reference (Loc, | |
1595 | Prefix => Relocate_Node (Op1), | |
1596 | Attribute_Name => Name_Address), | |
1597 | ||
1598 | Make_Attribute_Reference (Loc, | |
1599 | Prefix => Relocate_Node (Op2), | |
1600 | Attribute_Name => Name_Address), | |
1601 | ||
1602 | Make_Attribute_Reference (Loc, | |
1603 | Prefix => Relocate_Node (Op1), | |
1604 | Attribute_Name => Name_Length), | |
1605 | ||
1606 | Make_Attribute_Reference (Loc, | |
1607 | Prefix => Relocate_Node (Op2), | |
1608 | Attribute_Name => Name_Length)))); | |
1609 | ||
1610 | Rewrite (Op2, | |
1611 | Make_Integer_Literal (Sloc (Op2), | |
1612 | Intval => Uint_0)); | |
1613 | ||
1614 | Analyze_And_Resolve (Op1, Standard_Integer); | |
1615 | Analyze_And_Resolve (Op2, Standard_Integer); | |
1616 | return; | |
1617 | end if; | |
1618 | ||
1619 | -- Cases where we cannot make runtime call | |
1620 | ||
70482933 RK |
1621 | -- For (a <= b) we convert to not (a > b) |
1622 | ||
1623 | if Chars (N) = Name_Op_Le then | |
1624 | Rewrite (N, | |
1625 | Make_Op_Not (Loc, | |
1626 | Right_Opnd => | |
1627 | Make_Op_Gt (Loc, | |
1628 | Left_Opnd => Op1, | |
1629 | Right_Opnd => Op2))); | |
1630 | Analyze_And_Resolve (N, Standard_Boolean); | |
1631 | return; | |
1632 | ||
1633 | -- For < the Boolean expression is | |
1634 | -- greater__nn (op2, op1) | |
1635 | ||
1636 | elsif Chars (N) = Name_Op_Lt then | |
1637 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1638 | ||
1639 | -- Switch operands | |
1640 | ||
1641 | Op1 := Right_Opnd (N); | |
1642 | Op2 := Left_Opnd (N); | |
1643 | ||
1644 | -- For (a >= b) we convert to not (a < b) | |
1645 | ||
1646 | elsif Chars (N) = Name_Op_Ge then | |
1647 | Rewrite (N, | |
1648 | Make_Op_Not (Loc, | |
1649 | Right_Opnd => | |
1650 | Make_Op_Lt (Loc, | |
1651 | Left_Opnd => Op1, | |
1652 | Right_Opnd => Op2))); | |
1653 | Analyze_And_Resolve (N, Standard_Boolean); | |
1654 | return; | |
1655 | ||
1656 | -- For > the Boolean expression is | |
1657 | -- greater__nn (op1, op2) | |
1658 | ||
1659 | else | |
1660 | pragma Assert (Chars (N) = Name_Op_Gt); | |
1661 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1662 | end if; | |
1663 | ||
1664 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1665 | Expr := | |
1666 | Make_Function_Call (Loc, | |
e4494292 | 1667 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
1668 | Parameter_Associations => New_List (Op1, Op2)); |
1669 | ||
1670 | Insert_Action (N, Func_Body); | |
1671 | Rewrite (N, Expr); | |
1672 | Analyze_And_Resolve (N, Standard_Boolean); | |
1673 | ||
fbf5a39b AC |
1674 | exception |
1675 | when RE_Not_Available => | |
1676 | return; | |
70482933 RK |
1677 | end Expand_Array_Comparison; |
1678 | ||
1679 | --------------------------- | |
1680 | -- Expand_Array_Equality -- | |
1681 | --------------------------- | |
1682 | ||
685094bf RD |
1683 | -- Expand an equality function for multi-dimensional arrays. Here is an |
1684 | -- example of such a function for Nb_Dimension = 2 | |
70482933 | 1685 | |
0da2c8ac | 1686 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 1687 | -- begin |
fbf5a39b AC |
1688 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
1689 | -- and then | |
1690 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
1691 | -- then | |
1692 | -- return True; -- RM 4.5.2(22) | |
1693 | -- end if; | |
0da2c8ac | 1694 | |
fbf5a39b AC |
1695 | -- if A'length (1) /= B'length (1) |
1696 | -- or else | |
1697 | -- A'length (2) /= B'length (2) | |
1698 | -- then | |
1699 | -- return False; -- RM 4.5.2(23) | |
1700 | -- end if; | |
0da2c8ac | 1701 | |
fbf5a39b | 1702 | -- declare |
523456db AC |
1703 | -- A1 : Index_T1 := A'first (1); |
1704 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 1705 | -- begin |
523456db | 1706 | -- loop |
fbf5a39b | 1707 | -- declare |
523456db AC |
1708 | -- A2 : Index_T2 := A'first (2); |
1709 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 1710 | -- begin |
523456db | 1711 | -- loop |
fbf5a39b AC |
1712 | -- if A (A1, A2) /= B (B1, B2) then |
1713 | -- return False; | |
70482933 | 1714 | -- end if; |
0da2c8ac | 1715 | |
523456db AC |
1716 | -- exit when A2 = A'last (2); |
1717 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 1718 | -- B2 := Index_T2'succ (B2); |
70482933 | 1719 | -- end loop; |
fbf5a39b | 1720 | -- end; |
0da2c8ac | 1721 | |
523456db AC |
1722 | -- exit when A1 = A'last (1); |
1723 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 1724 | -- B1 := Index_T1'succ (B1); |
70482933 | 1725 | -- end loop; |
fbf5a39b | 1726 | -- end; |
0da2c8ac | 1727 | |
70482933 RK |
1728 | -- return true; |
1729 | -- end Enn; | |
1730 | ||
685094bf RD |
1731 | -- Note on the formal types used (atyp and btyp). If either of the arrays |
1732 | -- is of a private type, we use the underlying type, and do an unchecked | |
1733 | -- conversion of the actual. If either of the arrays has a bound depending | |
1734 | -- on a discriminant, then we use the base type since otherwise we have an | |
1735 | -- escaped discriminant in the function. | |
0da2c8ac | 1736 | |
685094bf RD |
1737 | -- If both arrays are constrained and have the same bounds, we can generate |
1738 | -- a loop with an explicit iteration scheme using a 'Range attribute over | |
1739 | -- the first array. | |
523456db | 1740 | |
70482933 RK |
1741 | function Expand_Array_Equality |
1742 | (Nod : Node_Id; | |
70482933 RK |
1743 | Lhs : Node_Id; |
1744 | Rhs : Node_Id; | |
0da2c8ac AC |
1745 | Bodies : List_Id; |
1746 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
1747 | is |
1748 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
1749 | Decls : constant List_Id := New_List; |
1750 | Index_List1 : constant List_Id := New_List; | |
1751 | Index_List2 : constant List_Id := New_List; | |
1752 | ||
1753 | Actuals : List_Id; | |
1754 | Formals : List_Id; | |
1755 | Func_Name : Entity_Id; | |
1756 | Func_Body : Node_Id; | |
70482933 RK |
1757 | |
1758 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
1759 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
1760 | ||
0da2c8ac AC |
1761 | Ltyp : Entity_Id; |
1762 | Rtyp : Entity_Id; | |
1763 | -- The parameter types to be used for the formals | |
1764 | ||
fbf5a39b AC |
1765 | function Arr_Attr |
1766 | (Arr : Entity_Id; | |
1767 | Nam : Name_Id; | |
2e071734 | 1768 | Num : Int) return Node_Id; |
5e1c00fa | 1769 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 1770 | |
70482933 | 1771 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
685094bf | 1772 | -- Create one statement to compare corresponding components, designated |
3b42c566 | 1773 | -- by a full set of indexes. |
70482933 | 1774 | |
0da2c8ac | 1775 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
685094bf RD |
1776 | -- Given one of the arguments, computes the appropriate type to be used |
1777 | -- for that argument in the corresponding function formal | |
0da2c8ac | 1778 | |
fbf5a39b | 1779 | function Handle_One_Dimension |
70482933 | 1780 | (N : Int; |
2e071734 | 1781 | Index : Node_Id) return Node_Id; |
0da2c8ac | 1782 | -- This procedure returns the following code |
fbf5a39b AC |
1783 | -- |
1784 | -- declare | |
523456db | 1785 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 1786 | -- begin |
523456db | 1787 | -- loop |
fbf5a39b | 1788 | -- xxx |
523456db AC |
1789 | -- exit when An = A'Last (N); |
1790 | -- An := Index_T'Succ (An) | |
0da2c8ac | 1791 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
1792 | -- end loop; |
1793 | -- end; | |
1794 | -- | |
3b42c566 | 1795 | -- If both indexes are constrained and identical, the procedure |
523456db AC |
1796 | -- returns a simpler loop: |
1797 | -- | |
1798 | -- for An in A'Range (N) loop | |
1799 | -- xxx | |
1800 | -- end loop | |
0da2c8ac | 1801 | -- |
523456db | 1802 | -- N is the dimension for which we are generating a loop. Index is the |
685094bf RD |
1803 | -- N'th index node, whose Etype is Index_Type_n in the above code. The |
1804 | -- xxx statement is either the loop or declare for the next dimension | |
1805 | -- or if this is the last dimension the comparison of corresponding | |
1806 | -- components of the arrays. | |
fbf5a39b | 1807 | -- |
685094bf | 1808 | -- The actual way the code works is to return the comparison of |
a90bd866 | 1809 | -- corresponding components for the N+1 call. That's neater. |
fbf5a39b AC |
1810 | |
1811 | function Test_Empty_Arrays return Node_Id; | |
1812 | -- This function constructs the test for both arrays being empty | |
1813 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1814 | -- and then | |
1815 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1816 | ||
1817 | function Test_Lengths_Correspond return Node_Id; | |
685094bf RD |
1818 | -- This function constructs the test for arrays having different lengths |
1819 | -- in at least one index position, in which case the resulting code is: | |
fbf5a39b AC |
1820 | |
1821 | -- A'length (1) /= B'length (1) | |
1822 | -- or else | |
1823 | -- A'length (2) /= B'length (2) | |
1824 | -- or else | |
1825 | -- ... | |
1826 | ||
1827 | -------------- | |
1828 | -- Arr_Attr -- | |
1829 | -------------- | |
1830 | ||
1831 | function Arr_Attr | |
1832 | (Arr : Entity_Id; | |
1833 | Nam : Name_Id; | |
2e071734 | 1834 | Num : Int) return Node_Id |
fbf5a39b AC |
1835 | is |
1836 | begin | |
1837 | return | |
1838 | Make_Attribute_Reference (Loc, | |
cc6f5d75 AC |
1839 | Attribute_Name => Nam, |
1840 | Prefix => New_Occurrence_Of (Arr, Loc), | |
1841 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
fbf5a39b | 1842 | end Arr_Attr; |
70482933 RK |
1843 | |
1844 | ------------------------ | |
1845 | -- Component_Equality -- | |
1846 | ------------------------ | |
1847 | ||
1848 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1849 | Test : Node_Id; | |
1850 | L, R : Node_Id; | |
1851 | ||
1852 | begin | |
1853 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1854 | ||
1855 | L := | |
1856 | Make_Indexed_Component (Loc, | |
7675ad4f | 1857 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
1858 | Expressions => Index_List1); |
1859 | ||
1860 | R := | |
1861 | Make_Indexed_Component (Loc, | |
7675ad4f | 1862 | Prefix => Make_Identifier (Loc, Chars (B)), |
70482933 RK |
1863 | Expressions => Index_List2); |
1864 | ||
1865 | Test := Expand_Composite_Equality | |
1866 | (Nod, Component_Type (Typ), L, R, Decls); | |
1867 | ||
a9d8907c JM |
1868 | -- If some (sub)component is an unchecked_union, the whole operation |
1869 | -- will raise program error. | |
8aceda64 AC |
1870 | |
1871 | if Nkind (Test) = N_Raise_Program_Error then | |
a9d8907c JM |
1872 | |
1873 | -- This node is going to be inserted at a location where a | |
685094bf RD |
1874 | -- statement is expected: clear its Etype so analysis will set |
1875 | -- it to the expected Standard_Void_Type. | |
a9d8907c JM |
1876 | |
1877 | Set_Etype (Test, Empty); | |
8aceda64 AC |
1878 | return Test; |
1879 | ||
1880 | else | |
1881 | return | |
1882 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 1883 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), |
8aceda64 | 1884 | Then_Statements => New_List ( |
d766cee3 | 1885 | Make_Simple_Return_Statement (Loc, |
8aceda64 AC |
1886 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
1887 | end if; | |
70482933 RK |
1888 | end Component_Equality; |
1889 | ||
0da2c8ac AC |
1890 | ------------------ |
1891 | -- Get_Arg_Type -- | |
1892 | ------------------ | |
1893 | ||
1894 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1895 | T : Entity_Id; | |
1896 | X : Node_Id; | |
1897 | ||
1898 | begin | |
1899 | T := Etype (N); | |
1900 | ||
1901 | if No (T) then | |
1902 | return Typ; | |
1903 | ||
1904 | else | |
1905 | T := Underlying_Type (T); | |
1906 | ||
1907 | X := First_Index (T); | |
1908 | while Present (X) loop | |
761f7dcb AC |
1909 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) |
1910 | or else | |
1911 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
0da2c8ac AC |
1912 | then |
1913 | T := Base_Type (T); | |
1914 | exit; | |
1915 | end if; | |
1916 | ||
1917 | Next_Index (X); | |
1918 | end loop; | |
1919 | ||
1920 | return T; | |
1921 | end if; | |
1922 | end Get_Arg_Type; | |
1923 | ||
fbf5a39b AC |
1924 | -------------------------- |
1925 | -- Handle_One_Dimension -- | |
1926 | --------------------------- | |
70482933 | 1927 | |
fbf5a39b | 1928 | function Handle_One_Dimension |
70482933 | 1929 | (N : Int; |
2e071734 | 1930 | Index : Node_Id) return Node_Id |
70482933 | 1931 | is |
0da2c8ac | 1932 | Need_Separate_Indexes : constant Boolean := |
761f7dcb | 1933 | Ltyp /= Rtyp or else not Is_Constrained (Ltyp); |
0da2c8ac | 1934 | -- If the index types are identical, and we are working with |
685094bf RD |
1935 | -- constrained types, then we can use the same index for both |
1936 | -- of the arrays. | |
0da2c8ac | 1937 | |
191fcb3a | 1938 | An : constant Entity_Id := Make_Temporary (Loc, 'A'); |
0da2c8ac AC |
1939 | |
1940 | Bn : Entity_Id; | |
1941 | Index_T : Entity_Id; | |
1942 | Stm_List : List_Id; | |
1943 | Loop_Stm : Node_Id; | |
70482933 RK |
1944 | |
1945 | begin | |
0da2c8ac AC |
1946 | if N > Number_Dimensions (Ltyp) then |
1947 | return Component_Equality (Ltyp); | |
fbf5a39b | 1948 | end if; |
70482933 | 1949 | |
0da2c8ac AC |
1950 | -- Case where we generate a loop |
1951 | ||
1952 | Index_T := Base_Type (Etype (Index)); | |
1953 | ||
1954 | if Need_Separate_Indexes then | |
191fcb3a | 1955 | Bn := Make_Temporary (Loc, 'B'); |
0da2c8ac AC |
1956 | else |
1957 | Bn := An; | |
1958 | end if; | |
70482933 | 1959 | |
e4494292 RD |
1960 | Append (New_Occurrence_Of (An, Loc), Index_List1); |
1961 | Append (New_Occurrence_Of (Bn, Loc), Index_List2); | |
70482933 | 1962 | |
0da2c8ac AC |
1963 | Stm_List := New_List ( |
1964 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1965 | |
0da2c8ac | 1966 | if Need_Separate_Indexes then |
a9d8907c | 1967 | |
3b42c566 | 1968 | -- Generate guard for loop, followed by increments of indexes |
523456db AC |
1969 | |
1970 | Append_To (Stm_List, | |
1971 | Make_Exit_Statement (Loc, | |
1972 | Condition => | |
1973 | Make_Op_Eq (Loc, | |
cc6f5d75 | 1974 | Left_Opnd => New_Occurrence_Of (An, Loc), |
523456db AC |
1975 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); |
1976 | ||
1977 | Append_To (Stm_List, | |
1978 | Make_Assignment_Statement (Loc, | |
e4494292 | 1979 | Name => New_Occurrence_Of (An, Loc), |
523456db AC |
1980 | Expression => |
1981 | Make_Attribute_Reference (Loc, | |
e4494292 | 1982 | Prefix => New_Occurrence_Of (Index_T, Loc), |
523456db | 1983 | Attribute_Name => Name_Succ, |
e4494292 RD |
1984 | Expressions => New_List ( |
1985 | New_Occurrence_Of (An, Loc))))); | |
523456db | 1986 | |
0da2c8ac AC |
1987 | Append_To (Stm_List, |
1988 | Make_Assignment_Statement (Loc, | |
e4494292 | 1989 | Name => New_Occurrence_Of (Bn, Loc), |
0da2c8ac AC |
1990 | Expression => |
1991 | Make_Attribute_Reference (Loc, | |
e4494292 | 1992 | Prefix => New_Occurrence_Of (Index_T, Loc), |
0da2c8ac | 1993 | Attribute_Name => Name_Succ, |
e4494292 RD |
1994 | Expressions => New_List ( |
1995 | New_Occurrence_Of (Bn, Loc))))); | |
0da2c8ac AC |
1996 | end if; |
1997 | ||
a9d8907c JM |
1998 | -- If separate indexes, we need a declare block for An and Bn, and a |
1999 | -- loop without an iteration scheme. | |
0da2c8ac AC |
2000 | |
2001 | if Need_Separate_Indexes then | |
523456db AC |
2002 | Loop_Stm := |
2003 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
2004 | ||
0da2c8ac AC |
2005 | return |
2006 | Make_Block_Statement (Loc, | |
2007 | Declarations => New_List ( | |
523456db AC |
2008 | Make_Object_Declaration (Loc, |
2009 | Defining_Identifier => An, | |
e4494292 | 2010 | Object_Definition => New_Occurrence_Of (Index_T, Loc), |
523456db AC |
2011 | Expression => Arr_Attr (A, Name_First, N)), |
2012 | ||
0da2c8ac AC |
2013 | Make_Object_Declaration (Loc, |
2014 | Defining_Identifier => Bn, | |
e4494292 | 2015 | Object_Definition => New_Occurrence_Of (Index_T, Loc), |
0da2c8ac | 2016 | Expression => Arr_Attr (B, Name_First, N))), |
523456db | 2017 | |
0da2c8ac AC |
2018 | Handled_Statement_Sequence => |
2019 | Make_Handled_Sequence_Of_Statements (Loc, | |
2020 | Statements => New_List (Loop_Stm))); | |
2021 | ||
523456db AC |
2022 | -- If no separate indexes, return loop statement with explicit |
2023 | -- iteration scheme on its own | |
0da2c8ac AC |
2024 | |
2025 | else | |
523456db AC |
2026 | Loop_Stm := |
2027 | Make_Implicit_Loop_Statement (Nod, | |
2028 | Statements => Stm_List, | |
2029 | Iteration_Scheme => | |
2030 | Make_Iteration_Scheme (Loc, | |
2031 | Loop_Parameter_Specification => | |
2032 | Make_Loop_Parameter_Specification (Loc, | |
2033 | Defining_Identifier => An, | |
2034 | Discrete_Subtype_Definition => | |
2035 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
2036 | return Loop_Stm; |
2037 | end if; | |
fbf5a39b AC |
2038 | end Handle_One_Dimension; |
2039 | ||
2040 | ----------------------- | |
2041 | -- Test_Empty_Arrays -- | |
2042 | ----------------------- | |
2043 | ||
2044 | function Test_Empty_Arrays return Node_Id is | |
2045 | Alist : Node_Id; | |
2046 | Blist : Node_Id; | |
2047 | ||
2048 | Atest : Node_Id; | |
2049 | Btest : Node_Id; | |
70482933 | 2050 | |
fbf5a39b AC |
2051 | begin |
2052 | Alist := Empty; | |
2053 | Blist := Empty; | |
0da2c8ac | 2054 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
2055 | Atest := |
2056 | Make_Op_Eq (Loc, | |
2057 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
2058 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
2059 | ||
2060 | Btest := | |
2061 | Make_Op_Eq (Loc, | |
2062 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
2063 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
2064 | ||
2065 | if No (Alist) then | |
2066 | Alist := Atest; | |
2067 | Blist := Btest; | |
70482933 | 2068 | |
fbf5a39b AC |
2069 | else |
2070 | Alist := | |
2071 | Make_Or_Else (Loc, | |
2072 | Left_Opnd => Relocate_Node (Alist), | |
2073 | Right_Opnd => Atest); | |
2074 | ||
2075 | Blist := | |
2076 | Make_Or_Else (Loc, | |
2077 | Left_Opnd => Relocate_Node (Blist), | |
2078 | Right_Opnd => Btest); | |
2079 | end if; | |
2080 | end loop; | |
70482933 | 2081 | |
fbf5a39b AC |
2082 | return |
2083 | Make_And_Then (Loc, | |
2084 | Left_Opnd => Alist, | |
2085 | Right_Opnd => Blist); | |
2086 | end Test_Empty_Arrays; | |
70482933 | 2087 | |
fbf5a39b AC |
2088 | ----------------------------- |
2089 | -- Test_Lengths_Correspond -- | |
2090 | ----------------------------- | |
70482933 | 2091 | |
fbf5a39b AC |
2092 | function Test_Lengths_Correspond return Node_Id is |
2093 | Result : Node_Id; | |
2094 | Rtest : Node_Id; | |
2095 | ||
2096 | begin | |
2097 | Result := Empty; | |
0da2c8ac | 2098 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
2099 | Rtest := |
2100 | Make_Op_Ne (Loc, | |
2101 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
2102 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
2103 | ||
2104 | if No (Result) then | |
2105 | Result := Rtest; | |
2106 | else | |
2107 | Result := | |
2108 | Make_Or_Else (Loc, | |
2109 | Left_Opnd => Relocate_Node (Result), | |
2110 | Right_Opnd => Rtest); | |
2111 | end if; | |
2112 | end loop; | |
2113 | ||
2114 | return Result; | |
2115 | end Test_Lengths_Correspond; | |
70482933 RK |
2116 | |
2117 | -- Start of processing for Expand_Array_Equality | |
2118 | ||
2119 | begin | |
0da2c8ac AC |
2120 | Ltyp := Get_Arg_Type (Lhs); |
2121 | Rtyp := Get_Arg_Type (Rhs); | |
2122 | ||
685094bf RD |
2123 | -- For now, if the argument types are not the same, go to the base type, |
2124 | -- since the code assumes that the formals have the same type. This is | |
2125 | -- fixable in future ??? | |
0da2c8ac AC |
2126 | |
2127 | if Ltyp /= Rtyp then | |
2128 | Ltyp := Base_Type (Ltyp); | |
2129 | Rtyp := Base_Type (Rtyp); | |
2130 | pragma Assert (Ltyp = Rtyp); | |
2131 | end if; | |
2132 | ||
2133 | -- Build list of formals for function | |
2134 | ||
70482933 RK |
2135 | Formals := New_List ( |
2136 | Make_Parameter_Specification (Loc, | |
2137 | Defining_Identifier => A, | |
e4494292 | 2138 | Parameter_Type => New_Occurrence_Of (Ltyp, Loc)), |
70482933 RK |
2139 | |
2140 | Make_Parameter_Specification (Loc, | |
2141 | Defining_Identifier => B, | |
e4494292 | 2142 | Parameter_Type => New_Occurrence_Of (Rtyp, Loc))); |
70482933 | 2143 | |
191fcb3a | 2144 | Func_Name := Make_Temporary (Loc, 'E'); |
70482933 | 2145 | |
fbf5a39b | 2146 | -- Build statement sequence for function |
70482933 RK |
2147 | |
2148 | Func_Body := | |
2149 | Make_Subprogram_Body (Loc, | |
2150 | Specification => | |
2151 | Make_Function_Specification (Loc, | |
2152 | Defining_Unit_Name => Func_Name, | |
2153 | Parameter_Specifications => Formals, | |
e4494292 | 2154 | Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)), |
fbf5a39b AC |
2155 | |
2156 | Declarations => Decls, | |
2157 | ||
70482933 RK |
2158 | Handled_Statement_Sequence => |
2159 | Make_Handled_Sequence_Of_Statements (Loc, | |
2160 | Statements => New_List ( | |
fbf5a39b AC |
2161 | |
2162 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 2163 | Condition => Test_Empty_Arrays, |
fbf5a39b | 2164 | Then_Statements => New_List ( |
d766cee3 | 2165 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
2166 | Expression => |
2167 | New_Occurrence_Of (Standard_True, Loc)))), | |
2168 | ||
2169 | Make_Implicit_If_Statement (Nod, | |
cc6f5d75 | 2170 | Condition => Test_Lengths_Correspond, |
fbf5a39b | 2171 | Then_Statements => New_List ( |
d766cee3 | 2172 | Make_Simple_Return_Statement (Loc, |
cc6f5d75 | 2173 | Expression => New_Occurrence_Of (Standard_False, Loc)))), |
fbf5a39b | 2174 | |
0da2c8ac | 2175 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 2176 | |
d766cee3 | 2177 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
2178 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
2179 | ||
2180 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 2181 | Set_Is_Inlined (Func_Name); |
70482933 | 2182 | |
685094bf RD |
2183 | -- If the array type is distinct from the type of the arguments, it |
2184 | -- is the full view of a private type. Apply an unchecked conversion | |
2185 | -- to insure that analysis of the call succeeds. | |
70482933 | 2186 | |
0da2c8ac AC |
2187 | declare |
2188 | L, R : Node_Id; | |
2189 | ||
2190 | begin | |
2191 | L := Lhs; | |
2192 | R := Rhs; | |
2193 | ||
2194 | if No (Etype (Lhs)) | |
2195 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
2196 | then | |
2197 | L := OK_Convert_To (Ltyp, Lhs); | |
2198 | end if; | |
2199 | ||
2200 | if No (Etype (Rhs)) | |
2201 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
2202 | then | |
2203 | R := OK_Convert_To (Rtyp, Rhs); | |
2204 | end if; | |
2205 | ||
2206 | Actuals := New_List (L, R); | |
2207 | end; | |
70482933 RK |
2208 | |
2209 | Append_To (Bodies, Func_Body); | |
2210 | ||
2211 | return | |
2212 | Make_Function_Call (Loc, | |
e4494292 | 2213 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
2214 | Parameter_Associations => Actuals); |
2215 | end Expand_Array_Equality; | |
2216 | ||
2217 | ----------------------------- | |
2218 | -- Expand_Boolean_Operator -- | |
2219 | ----------------------------- | |
2220 | ||
685094bf RD |
2221 | -- Note that we first get the actual subtypes of the operands, since we |
2222 | -- always want to deal with types that have bounds. | |
70482933 RK |
2223 | |
2224 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 2225 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
2226 | |
2227 | begin | |
685094bf RD |
2228 | -- Special case of bit packed array where both operands are known to be |
2229 | -- properly aligned. In this case we use an efficient run time routine | |
2230 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
2231 | |
2232 | if Is_Bit_Packed_Array (Typ) | |
2233 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
2234 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
2235 | then | |
70482933 | 2236 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
2237 | return; |
2238 | end if; | |
70482933 | 2239 | |
a9d8907c JM |
2240 | -- For the normal non-packed case, the general expansion is to build |
2241 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
2242 | -- and then inserting it into the tree. The original operator node is | |
2243 | -- then rewritten as a call to this function. We also use this in the | |
2244 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 2245 | |
a9d8907c JM |
2246 | declare |
2247 | Loc : constant Source_Ptr := Sloc (N); | |
2248 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
2249 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
2250 | Func_Body : Node_Id; | |
2251 | Func_Name : Entity_Id; | |
fbf5a39b | 2252 | |
a9d8907c JM |
2253 | begin |
2254 | Convert_To_Actual_Subtype (L); | |
2255 | Convert_To_Actual_Subtype (R); | |
2256 | Ensure_Defined (Etype (L), N); | |
2257 | Ensure_Defined (Etype (R), N); | |
2258 | Apply_Length_Check (R, Etype (L)); | |
2259 | ||
b4592168 GD |
2260 | if Nkind (N) = N_Op_Xor then |
2261 | Silly_Boolean_Array_Xor_Test (N, Etype (L)); | |
2262 | end if; | |
2263 | ||
a9d8907c JM |
2264 | if Nkind (Parent (N)) = N_Assignment_Statement |
2265 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
2266 | then | |
2267 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 2268 | |
a9d8907c JM |
2269 | elsif Nkind (Parent (N)) = N_Op_Not |
2270 | and then Nkind (N) = N_Op_And | |
cc6f5d75 | 2271 | and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
2272 | then |
2273 | return; | |
2274 | else | |
fbf5a39b | 2275 | |
a9d8907c JM |
2276 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
2277 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
2278 | Insert_Action (N, Func_Body); | |
70482933 | 2279 | |
a9d8907c | 2280 | -- Now rewrite the expression with a call |
70482933 | 2281 | |
a9d8907c JM |
2282 | Rewrite (N, |
2283 | Make_Function_Call (Loc, | |
e4494292 | 2284 | Name => New_Occurrence_Of (Func_Name, Loc), |
a9d8907c JM |
2285 | Parameter_Associations => |
2286 | New_List ( | |
2287 | L, | |
2288 | Make_Type_Conversion | |
e4494292 | 2289 | (Loc, New_Occurrence_Of (Etype (L), Loc), R)))); |
70482933 | 2290 | |
a9d8907c JM |
2291 | Analyze_And_Resolve (N, Typ); |
2292 | end if; | |
2293 | end; | |
70482933 RK |
2294 | end Expand_Boolean_Operator; |
2295 | ||
456cbfa5 AC |
2296 | ------------------------------------------------ |
2297 | -- Expand_Compare_Minimize_Eliminate_Overflow -- | |
2298 | ------------------------------------------------ | |
2299 | ||
2300 | procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is | |
2301 | Loc : constant Source_Ptr := Sloc (N); | |
2302 | ||
71fb4dc8 AC |
2303 | Result_Type : constant Entity_Id := Etype (N); |
2304 | -- Capture result type (could be a derived boolean type) | |
2305 | ||
456cbfa5 AC |
2306 | Llo, Lhi : Uint; |
2307 | Rlo, Rhi : Uint; | |
2308 | ||
2309 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
2310 | -- Entity for Long_Long_Integer'Base | |
2311 | ||
15c94a55 | 2312 | Check : constant Overflow_Mode_Type := Overflow_Check_Mode; |
a7f1b24f | 2313 | -- Current overflow checking mode |
456cbfa5 AC |
2314 | |
2315 | procedure Set_True; | |
2316 | procedure Set_False; | |
2317 | -- These procedures rewrite N with an occurrence of Standard_True or | |
2318 | -- Standard_False, and then makes a call to Warn_On_Known_Condition. | |
2319 | ||
2320 | --------------- | |
2321 | -- Set_False -- | |
2322 | --------------- | |
2323 | ||
2324 | procedure Set_False is | |
2325 | begin | |
2326 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
2327 | Warn_On_Known_Condition (N); | |
2328 | end Set_False; | |
2329 | ||
2330 | -------------- | |
2331 | -- Set_True -- | |
2332 | -------------- | |
2333 | ||
2334 | procedure Set_True is | |
2335 | begin | |
2336 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
2337 | Warn_On_Known_Condition (N); | |
2338 | end Set_True; | |
2339 | ||
2340 | -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow | |
2341 | ||
2342 | begin | |
2343 | -- Nothing to do unless we have a comparison operator with operands | |
2344 | -- that are signed integer types, and we are operating in either | |
2345 | -- MINIMIZED or ELIMINATED overflow checking mode. | |
2346 | ||
2347 | if Nkind (N) not in N_Op_Compare | |
2348 | or else Check not in Minimized_Or_Eliminated | |
2349 | or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N))) | |
2350 | then | |
2351 | return; | |
2352 | end if; | |
2353 | ||
2354 | -- OK, this is the case we are interested in. First step is to process | |
2355 | -- our operands using the Minimize_Eliminate circuitry which applies | |
2356 | -- this processing to the two operand subtrees. | |
2357 | ||
a7f1b24f | 2358 | Minimize_Eliminate_Overflows |
c7e152b5 | 2359 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
a7f1b24f | 2360 | Minimize_Eliminate_Overflows |
c7e152b5 | 2361 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
456cbfa5 | 2362 | |
65f7ed64 AC |
2363 | -- See if the range information decides the result of the comparison. |
2364 | -- We can only do this if we in fact have full range information (which | |
2365 | -- won't be the case if either operand is bignum at this stage). | |
456cbfa5 | 2366 | |
65f7ed64 AC |
2367 | if Llo /= No_Uint and then Rlo /= No_Uint then |
2368 | case N_Op_Compare (Nkind (N)) is | |
456cbfa5 AC |
2369 | when N_Op_Eq => |
2370 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
2371 | Set_True; | |
a40ada7e | 2372 | elsif Llo > Rhi or else Lhi < Rlo then |
456cbfa5 AC |
2373 | Set_False; |
2374 | end if; | |
2375 | ||
2376 | when N_Op_Ge => | |
2377 | if Llo >= Rhi then | |
2378 | Set_True; | |
2379 | elsif Lhi < Rlo then | |
2380 | Set_False; | |
2381 | end if; | |
2382 | ||
2383 | when N_Op_Gt => | |
2384 | if Llo > Rhi then | |
2385 | Set_True; | |
2386 | elsif Lhi <= Rlo then | |
2387 | Set_False; | |
2388 | end if; | |
2389 | ||
2390 | when N_Op_Le => | |
2391 | if Llo > Rhi then | |
2392 | Set_False; | |
2393 | elsif Lhi <= Rlo then | |
2394 | Set_True; | |
2395 | end if; | |
2396 | ||
2397 | when N_Op_Lt => | |
2398 | if Llo >= Rhi then | |
456cbfa5 | 2399 | Set_False; |
b6b5cca8 AC |
2400 | elsif Lhi < Rlo then |
2401 | Set_True; | |
456cbfa5 AC |
2402 | end if; |
2403 | ||
2404 | when N_Op_Ne => | |
2405 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
456cbfa5 | 2406 | Set_False; |
a40ada7e RD |
2407 | elsif Llo > Rhi or else Lhi < Rlo then |
2408 | Set_True; | |
456cbfa5 | 2409 | end if; |
65f7ed64 | 2410 | end case; |
456cbfa5 | 2411 | |
65f7ed64 | 2412 | -- All done if we did the rewrite |
456cbfa5 | 2413 | |
65f7ed64 AC |
2414 | if Nkind (N) not in N_Op_Compare then |
2415 | return; | |
2416 | end if; | |
456cbfa5 AC |
2417 | end if; |
2418 | ||
2419 | -- Otherwise, time to do the comparison | |
2420 | ||
2421 | declare | |
2422 | Ltype : constant Entity_Id := Etype (Left_Opnd (N)); | |
2423 | Rtype : constant Entity_Id := Etype (Right_Opnd (N)); | |
2424 | ||
2425 | begin | |
2426 | -- If the two operands have the same signed integer type we are | |
2427 | -- all set, nothing more to do. This is the case where either | |
2428 | -- both operands were unchanged, or we rewrote both of them to | |
2429 | -- be Long_Long_Integer. | |
2430 | ||
2431 | -- Note: Entity for the comparison may be wrong, but it's not worth | |
2432 | -- the effort to change it, since the back end does not use it. | |
2433 | ||
2434 | if Is_Signed_Integer_Type (Ltype) | |
2435 | and then Base_Type (Ltype) = Base_Type (Rtype) | |
2436 | then | |
2437 | return; | |
2438 | ||
2439 | -- Here if bignums are involved (can only happen in ELIMINATED mode) | |
2440 | ||
2441 | elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then | |
2442 | declare | |
2443 | Left : Node_Id := Left_Opnd (N); | |
2444 | Right : Node_Id := Right_Opnd (N); | |
2445 | -- Bignum references for left and right operands | |
2446 | ||
2447 | begin | |
2448 | if not Is_RTE (Ltype, RE_Bignum) then | |
2449 | Left := Convert_To_Bignum (Left); | |
2450 | elsif not Is_RTE (Rtype, RE_Bignum) then | |
2451 | Right := Convert_To_Bignum (Right); | |
2452 | end if; | |
2453 | ||
71fb4dc8 | 2454 | -- We rewrite our node with: |
456cbfa5 | 2455 | |
71fb4dc8 AC |
2456 | -- do |
2457 | -- Bnn : Result_Type; | |
2458 | -- declare | |
2459 | -- M : Mark_Id := SS_Mark; | |
2460 | -- begin | |
2461 | -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc) | |
2462 | -- SS_Release (M); | |
2463 | -- end; | |
2464 | -- in | |
2465 | -- Bnn | |
2466 | -- end | |
456cbfa5 AC |
2467 | |
2468 | declare | |
71fb4dc8 | 2469 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
456cbfa5 AC |
2470 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); |
2471 | Ent : RE_Id; | |
2472 | ||
2473 | begin | |
2474 | case N_Op_Compare (Nkind (N)) is | |
2475 | when N_Op_Eq => Ent := RE_Big_EQ; | |
2476 | when N_Op_Ge => Ent := RE_Big_GE; | |
2477 | when N_Op_Gt => Ent := RE_Big_GT; | |
2478 | when N_Op_Le => Ent := RE_Big_LE; | |
2479 | when N_Op_Lt => Ent := RE_Big_LT; | |
2480 | when N_Op_Ne => Ent := RE_Big_NE; | |
2481 | end case; | |
2482 | ||
71fb4dc8 | 2483 | -- Insert assignment to Bnn into the bignum block |
456cbfa5 AC |
2484 | |
2485 | Insert_Before | |
2486 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
2487 | Make_Assignment_Statement (Loc, | |
2488 | Name => New_Occurrence_Of (Bnn, Loc), | |
2489 | Expression => | |
2490 | Make_Function_Call (Loc, | |
2491 | Name => | |
2492 | New_Occurrence_Of (RTE (Ent), Loc), | |
2493 | Parameter_Associations => New_List (Left, Right)))); | |
2494 | ||
71fb4dc8 AC |
2495 | -- Now do the rewrite with expression actions |
2496 | ||
2497 | Rewrite (N, | |
2498 | Make_Expression_With_Actions (Loc, | |
2499 | Actions => New_List ( | |
2500 | Make_Object_Declaration (Loc, | |
2501 | Defining_Identifier => Bnn, | |
2502 | Object_Definition => | |
2503 | New_Occurrence_Of (Result_Type, Loc)), | |
2504 | Blk), | |
2505 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
2506 | Analyze_And_Resolve (N, Result_Type); | |
456cbfa5 AC |
2507 | end; |
2508 | end; | |
2509 | ||
2510 | -- No bignums involved, but types are different, so we must have | |
2511 | -- rewritten one of the operands as a Long_Long_Integer but not | |
2512 | -- the other one. | |
2513 | ||
2514 | -- If left operand is Long_Long_Integer, convert right operand | |
2515 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2516 | ||
2517 | elsif Ltype = LLIB then | |
2518 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
2519 | Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks); | |
2520 | return; | |
2521 | ||
2522 | -- If right operand is Long_Long_Integer, convert left operand | |
2523 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2524 | ||
2525 | -- This is the only remaining possibility | |
2526 | ||
2527 | else pragma Assert (Rtype = LLIB); | |
2528 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
2529 | Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks); | |
2530 | return; | |
2531 | end if; | |
2532 | end; | |
2533 | end Expand_Compare_Minimize_Eliminate_Overflow; | |
2534 | ||
70482933 RK |
2535 | ------------------------------- |
2536 | -- Expand_Composite_Equality -- | |
2537 | ------------------------------- | |
2538 | ||
2539 | -- This function is only called for comparing internal fields of composite | |
2540 | -- types when these fields are themselves composites. This is a special | |
2541 | -- case because it is not possible to respect normal Ada visibility rules. | |
2542 | ||
2543 | function Expand_Composite_Equality | |
2544 | (Nod : Node_Id; | |
2545 | Typ : Entity_Id; | |
2546 | Lhs : Node_Id; | |
2547 | Rhs : Node_Id; | |
2e071734 | 2548 | Bodies : List_Id) return Node_Id |
70482933 RK |
2549 | is |
2550 | Loc : constant Source_Ptr := Sloc (Nod); | |
2551 | Full_Type : Entity_Id; | |
2552 | Prim : Elmt_Id; | |
2553 | Eq_Op : Entity_Id; | |
2554 | ||
7efc3f2d AC |
2555 | function Find_Primitive_Eq return Node_Id; |
2556 | -- AI05-0123: Locate primitive equality for type if it exists, and | |
2557 | -- build the corresponding call. If operation is abstract, replace | |
2558 | -- call with an explicit raise. Return Empty if there is no primitive. | |
2559 | ||
2560 | ----------------------- | |
2561 | -- Find_Primitive_Eq -- | |
2562 | ----------------------- | |
2563 | ||
2564 | function Find_Primitive_Eq return Node_Id is | |
2565 | Prim_E : Elmt_Id; | |
2566 | Prim : Node_Id; | |
2567 | ||
2568 | begin | |
2569 | Prim_E := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2570 | while Present (Prim_E) loop | |
2571 | Prim := Node (Prim_E); | |
2572 | ||
2573 | -- Locate primitive equality with the right signature | |
2574 | ||
2575 | if Chars (Prim) = Name_Op_Eq | |
2576 | and then Etype (First_Formal (Prim)) = | |
39ade2f9 | 2577 | Etype (Next_Formal (First_Formal (Prim))) |
7efc3f2d AC |
2578 | and then Etype (Prim) = Standard_Boolean |
2579 | then | |
2580 | if Is_Abstract_Subprogram (Prim) then | |
2581 | return | |
2582 | Make_Raise_Program_Error (Loc, | |
2583 | Reason => PE_Explicit_Raise); | |
2584 | ||
2585 | else | |
2586 | return | |
2587 | Make_Function_Call (Loc, | |
e4494292 | 2588 | Name => New_Occurrence_Of (Prim, Loc), |
7efc3f2d AC |
2589 | Parameter_Associations => New_List (Lhs, Rhs)); |
2590 | end if; | |
2591 | end if; | |
2592 | ||
2593 | Next_Elmt (Prim_E); | |
2594 | end loop; | |
2595 | ||
2596 | -- If not found, predefined operation will be used | |
2597 | ||
2598 | return Empty; | |
2599 | end Find_Primitive_Eq; | |
2600 | ||
2601 | -- Start of processing for Expand_Composite_Equality | |
2602 | ||
70482933 RK |
2603 | begin |
2604 | if Is_Private_Type (Typ) then | |
2605 | Full_Type := Underlying_Type (Typ); | |
2606 | else | |
2607 | Full_Type := Typ; | |
2608 | end if; | |
2609 | ||
ced8450b ES |
2610 | -- If the private type has no completion the context may be the |
2611 | -- expansion of a composite equality for a composite type with some | |
2612 | -- still incomplete components. The expression will not be analyzed | |
2613 | -- until the enclosing type is completed, at which point this will be | |
2614 | -- properly expanded, unless there is a bona fide completion error. | |
70482933 RK |
2615 | |
2616 | if No (Full_Type) then | |
ced8450b | 2617 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 RK |
2618 | end if; |
2619 | ||
2620 | Full_Type := Base_Type (Full_Type); | |
2621 | ||
da1b76c1 HK |
2622 | -- When the base type itself is private, use the full view to expand |
2623 | -- the composite equality. | |
2624 | ||
2625 | if Is_Private_Type (Full_Type) then | |
2626 | Full_Type := Underlying_Type (Full_Type); | |
2627 | end if; | |
2628 | ||
16788d44 RD |
2629 | -- Case of array types |
2630 | ||
70482933 RK |
2631 | if Is_Array_Type (Full_Type) then |
2632 | ||
2633 | -- If the operand is an elementary type other than a floating-point | |
2634 | -- type, then we can simply use the built-in block bitwise equality, | |
2635 | -- since the predefined equality operators always apply and bitwise | |
2636 | -- equality is fine for all these cases. | |
2637 | ||
2638 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
2639 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
2640 | then | |
39ade2f9 | 2641 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 | 2642 | |
685094bf RD |
2643 | -- For composite component types, and floating-point types, use the |
2644 | -- expansion. This deals with tagged component types (where we use | |
2645 | -- the applicable equality routine) and floating-point, (where we | |
2646 | -- need to worry about negative zeroes), and also the case of any | |
2647 | -- composite type recursively containing such fields. | |
70482933 RK |
2648 | |
2649 | else | |
0da2c8ac | 2650 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
2651 | end if; |
2652 | ||
16788d44 RD |
2653 | -- Case of tagged record types |
2654 | ||
70482933 RK |
2655 | elsif Is_Tagged_Type (Full_Type) then |
2656 | ||
2657 | -- Call the primitive operation "=" of this type | |
2658 | ||
2659 | if Is_Class_Wide_Type (Full_Type) then | |
2660 | Full_Type := Root_Type (Full_Type); | |
2661 | end if; | |
2662 | ||
685094bf RD |
2663 | -- If this is derived from an untagged private type completed with a |
2664 | -- tagged type, it does not have a full view, so we use the primitive | |
2665 | -- operations of the private type. This check should no longer be | |
2666 | -- necessary when these types receive their full views ??? | |
70482933 RK |
2667 | |
2668 | if Is_Private_Type (Typ) | |
2669 | and then not Is_Tagged_Type (Typ) | |
2670 | and then not Is_Controlled (Typ) | |
2671 | and then Is_Derived_Type (Typ) | |
2672 | and then No (Full_View (Typ)) | |
2673 | then | |
2674 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2675 | else | |
2676 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
2677 | end if; | |
2678 | ||
2679 | loop | |
2680 | Eq_Op := Node (Prim); | |
2681 | exit when Chars (Eq_Op) = Name_Op_Eq | |
2682 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
2683 | Etype (Next_Formal (First_Formal (Eq_Op))) |
2684 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
2685 | Next_Elmt (Prim); |
2686 | pragma Assert (Present (Prim)); | |
2687 | end loop; | |
2688 | ||
2689 | Eq_Op := Node (Prim); | |
2690 | ||
2691 | return | |
2692 | Make_Function_Call (Loc, | |
e4494292 | 2693 | Name => New_Occurrence_Of (Eq_Op, Loc), |
70482933 RK |
2694 | Parameter_Associations => |
2695 | New_List | |
2696 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2697 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2698 | ||
16788d44 RD |
2699 | -- Case of untagged record types |
2700 | ||
70482933 | 2701 | elsif Is_Record_Type (Full_Type) then |
fbf5a39b | 2702 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2703 | |
2704 | if Present (Eq_Op) then | |
2705 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2706 | ||
685094bf RD |
2707 | -- Inherited equality from parent type. Convert the actuals to |
2708 | -- match signature of operation. | |
70482933 RK |
2709 | |
2710 | declare | |
fbf5a39b | 2711 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2712 | |
2713 | begin | |
2714 | return | |
2715 | Make_Function_Call (Loc, | |
e4494292 | 2716 | Name => New_Occurrence_Of (Eq_Op, Loc), |
39ade2f9 AC |
2717 | Parameter_Associations => New_List ( |
2718 | OK_Convert_To (T, Lhs), | |
2719 | OK_Convert_To (T, Rhs))); | |
70482933 RK |
2720 | end; |
2721 | ||
2722 | else | |
5d09245e AC |
2723 | -- Comparison between Unchecked_Union components |
2724 | ||
2725 | if Is_Unchecked_Union (Full_Type) then | |
2726 | declare | |
2727 | Lhs_Type : Node_Id := Full_Type; | |
2728 | Rhs_Type : Node_Id := Full_Type; | |
2729 | Lhs_Discr_Val : Node_Id; | |
2730 | Rhs_Discr_Val : Node_Id; | |
2731 | ||
2732 | begin | |
2733 | -- Lhs subtype | |
2734 | ||
2735 | if Nkind (Lhs) = N_Selected_Component then | |
2736 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2737 | end if; | |
2738 | ||
2739 | -- Rhs subtype | |
2740 | ||
2741 | if Nkind (Rhs) = N_Selected_Component then | |
2742 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2743 | end if; | |
2744 | ||
2745 | -- Lhs of the composite equality | |
2746 | ||
2747 | if Is_Constrained (Lhs_Type) then | |
2748 | ||
685094bf | 2749 | -- Since the enclosing record type can never be an |
5d09245e AC |
2750 | -- Unchecked_Union (this code is executed for records |
2751 | -- that do not have variants), we may reference its | |
2752 | -- discriminant(s). | |
2753 | ||
2754 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
2755 | and then Has_Per_Object_Constraint |
2756 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
2757 | then |
2758 | Lhs_Discr_Val := | |
2759 | Make_Selected_Component (Loc, | |
39ade2f9 | 2760 | Prefix => Prefix (Lhs), |
5d09245e | 2761 | Selector_Name => |
39ade2f9 AC |
2762 | New_Copy |
2763 | (Get_Discriminant_Value | |
2764 | (First_Discriminant (Lhs_Type), | |
2765 | Lhs_Type, | |
2766 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
2767 | |
2768 | else | |
39ade2f9 AC |
2769 | Lhs_Discr_Val := |
2770 | New_Copy | |
2771 | (Get_Discriminant_Value | |
2772 | (First_Discriminant (Lhs_Type), | |
2773 | Lhs_Type, | |
2774 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
2775 | |
2776 | end if; | |
2777 | else | |
2778 | -- It is not possible to infer the discriminant since | |
2779 | -- the subtype is not constrained. | |
2780 | ||
8aceda64 | 2781 | return |
5d09245e | 2782 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2783 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2784 | end if; |
2785 | ||
2786 | -- Rhs of the composite equality | |
2787 | ||
2788 | if Is_Constrained (Rhs_Type) then | |
2789 | if Nkind (Rhs) = N_Selected_Component | |
39ade2f9 AC |
2790 | and then Has_Per_Object_Constraint |
2791 | (Entity (Selector_Name (Rhs))) | |
5d09245e AC |
2792 | then |
2793 | Rhs_Discr_Val := | |
2794 | Make_Selected_Component (Loc, | |
39ade2f9 | 2795 | Prefix => Prefix (Rhs), |
5d09245e | 2796 | Selector_Name => |
39ade2f9 AC |
2797 | New_Copy |
2798 | (Get_Discriminant_Value | |
2799 | (First_Discriminant (Rhs_Type), | |
2800 | Rhs_Type, | |
2801 | Stored_Constraint (Rhs_Type)))); | |
5d09245e AC |
2802 | |
2803 | else | |
39ade2f9 AC |
2804 | Rhs_Discr_Val := |
2805 | New_Copy | |
2806 | (Get_Discriminant_Value | |
2807 | (First_Discriminant (Rhs_Type), | |
2808 | Rhs_Type, | |
2809 | Stored_Constraint (Rhs_Type))); | |
5d09245e AC |
2810 | |
2811 | end if; | |
2812 | else | |
8aceda64 | 2813 | return |
5d09245e | 2814 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2815 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2816 | end if; |
2817 | ||
2818 | -- Call the TSS equality function with the inferred | |
2819 | -- discriminant values. | |
2820 | ||
2821 | return | |
2822 | Make_Function_Call (Loc, | |
e4494292 | 2823 | Name => New_Occurrence_Of (Eq_Op, Loc), |
5d09245e AC |
2824 | Parameter_Associations => New_List ( |
2825 | Lhs, | |
2826 | Rhs, | |
2827 | Lhs_Discr_Val, | |
2828 | Rhs_Discr_Val)); | |
2829 | end; | |
d151d6a3 AC |
2830 | |
2831 | else | |
7f1a5156 EB |
2832 | declare |
2833 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); | |
2834 | ||
2835 | begin | |
2836 | return | |
2837 | Make_Function_Call (Loc, | |
2838 | Name => New_Occurrence_Of (Eq_Op, Loc), | |
2839 | Parameter_Associations => New_List ( | |
2840 | OK_Convert_To (T, Lhs), | |
2841 | OK_Convert_To (T, Rhs))); | |
2842 | end; | |
5d09245e | 2843 | end if; |
d151d6a3 | 2844 | end if; |
5d09245e | 2845 | |
3058f181 BD |
2846 | -- Equality composes in Ada 2012 for untagged record types. It also |
2847 | -- composes for bounded strings, because they are part of the | |
2848 | -- predefined environment. We could make it compose for bounded | |
2849 | -- strings by making them tagged, or by making sure all subcomponents | |
2850 | -- are set to the same value, even when not used. Instead, we have | |
2851 | -- this special case in the compiler, because it's more efficient. | |
2852 | ||
2853 | elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then | |
5d09245e | 2854 | |
08daa782 | 2855 | -- If no TSS has been created for the type, check whether there is |
7efc3f2d | 2856 | -- a primitive equality declared for it. |
d151d6a3 AC |
2857 | |
2858 | declare | |
3058f181 | 2859 | Op : constant Node_Id := Find_Primitive_Eq; |
d151d6a3 AC |
2860 | |
2861 | begin | |
a1fc903a AC |
2862 | -- Use user-defined primitive if it exists, otherwise use |
2863 | -- predefined equality. | |
2864 | ||
3058f181 BD |
2865 | if Present (Op) then |
2866 | return Op; | |
7efc3f2d | 2867 | else |
7efc3f2d AC |
2868 | return Make_Op_Eq (Loc, Lhs, Rhs); |
2869 | end if; | |
d151d6a3 AC |
2870 | end; |
2871 | ||
70482933 RK |
2872 | else |
2873 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2874 | end if; | |
2875 | ||
16788d44 | 2876 | -- Non-composite types (always use predefined equality) |
70482933 | 2877 | |
16788d44 | 2878 | else |
70482933 RK |
2879 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
2880 | end if; | |
2881 | end Expand_Composite_Equality; | |
2882 | ||
fdac1f80 AC |
2883 | ------------------------ |
2884 | -- Expand_Concatenate -- | |
2885 | ------------------------ | |
70482933 | 2886 | |
fdac1f80 AC |
2887 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2888 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2889 | |
fdac1f80 AC |
2890 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2891 | -- Result type of concatenation | |
70482933 | 2892 | |
fdac1f80 AC |
2893 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2894 | -- Component type. Elements of this component type can appear as one | |
2895 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2896 | |
ecc4ddde AC |
2897 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2898 | -- Index subtype | |
2899 | ||
2900 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2901 | -- Index type. This is the base type of the index subtype, and is used | |
2902 | -- for all computed bounds (which may be out of range of Istyp in the | |
2903 | -- case of null ranges). | |
70482933 | 2904 | |
46ff89f3 | 2905 | Artyp : Entity_Id; |
fdac1f80 AC |
2906 | -- This is the type we use to do arithmetic to compute the bounds and |
2907 | -- lengths of operands. The choice of this type is a little subtle and | |
2908 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2909 | |
fdac1f80 AC |
2910 | Concatenation_Error : exception; |
2911 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2912 | |
0ac73189 AC |
2913 | Result_May_Be_Null : Boolean := True; |
2914 | -- Reset to False if at least one operand is encountered which is known | |
2915 | -- at compile time to be non-null. Used for handling the special case | |
2916 | -- of setting the high bound to the last operand high bound for a null | |
2917 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2918 | ||
df46b832 | 2919 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2920 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2921 | |
2922 | NN : Nat := 0; | |
a29262fd AC |
2923 | -- Number of operands excluding any known to be null, except that the |
2924 | -- last operand is always retained, in case it provides the bounds for | |
2925 | -- a null result. | |
2926 | ||
2927 | Opnd : Node_Id; | |
2928 | -- Current operand being processed in the loop through operands. After | |
2929 | -- this loop is complete, always contains the last operand (which is not | |
2930 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2931 | |
2932 | -- Arrays describing the operands, only the first NN entries of each | |
2933 | -- array are set (NN < N when we exclude known null operands). | |
2934 | ||
2935 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2936 | -- True if length of corresponding operand known at compile time | |
2937 | ||
2938 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2939 | -- Set to the corresponding entry in the Opnds list (but note that null |
2940 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2941 | |
2942 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2943 | -- Set to length of operand. Entries in this array are set only if the |
2944 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2945 | |
0ac73189 AC |
2946 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2947 | -- Set to lower bound of operand. Either an integer literal in the case | |
2948 | -- where the bound is known at compile time, else actual lower bound. | |
2949 | -- The operand low bound is of type Ityp. | |
2950 | ||
df46b832 AC |
2951 | Var_Length : array (1 .. N) of Entity_Id; |
2952 | -- Set to an entity of type Natural that contains the length of an | |
2953 | -- operand whose length is not known at compile time. Entries in this | |
2954 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2955 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2956 | |
2957 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2958 | -- The J'th entry in an expression node that represents the total length |
2959 | -- of operands 1 through J. It is either an integer literal node, or a | |
2960 | -- reference to a constant entity with the right value, so it is fine | |
2961 | -- to just do a Copy_Node to get an appropriate copy. The extra zero'th | |
46ff89f3 | 2962 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 AC |
2963 | |
2964 | Low_Bound : Node_Id; | |
0ac73189 AC |
2965 | -- A tree node representing the low bound of the result (of type Ityp). |
2966 | -- This is either an integer literal node, or an identifier reference to | |
2967 | -- a constant entity initialized to the appropriate value. | |
2968 | ||
88a27b18 AC |
2969 | Last_Opnd_Low_Bound : Node_Id; |
2970 | -- A tree node representing the low bound of the last operand. This | |
2971 | -- need only be set if the result could be null. It is used for the | |
2972 | -- special case of setting the right low bound for a null result. | |
2973 | -- This is of type Ityp. | |
2974 | ||
a29262fd AC |
2975 | Last_Opnd_High_Bound : Node_Id; |
2976 | -- A tree node representing the high bound of the last operand. This | |
2977 | -- need only be set if the result could be null. It is used for the | |
2978 | -- special case of setting the right high bound for a null result. | |
2979 | -- This is of type Ityp. | |
2980 | ||
0ac73189 AC |
2981 | High_Bound : Node_Id; |
2982 | -- A tree node representing the high bound of the result (of type Ityp) | |
df46b832 AC |
2983 | |
2984 | Result : Node_Id; | |
0ac73189 | 2985 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2986 | |
d0f8d157 | 2987 | Actions : constant List_Id := New_List; |
4c9fe6c7 | 2988 | -- Collect actions to be inserted |
d0f8d157 | 2989 | |
fa969310 | 2990 | Known_Non_Null_Operand_Seen : Boolean; |
308e6f3a | 2991 | -- Set True during generation of the assignments of operands into |
fa969310 AC |
2992 | -- result once an operand known to be non-null has been seen. |
2993 | ||
2994 | function Make_Artyp_Literal (Val : Nat) return Node_Id; | |
2995 | -- This function makes an N_Integer_Literal node that is returned in | |
2996 | -- analyzed form with the type set to Artyp. Importantly this literal | |
2997 | -- is not flagged as static, so that if we do computations with it that | |
2998 | -- result in statically detected out of range conditions, we will not | |
2999 | -- generate error messages but instead warning messages. | |
3000 | ||
46ff89f3 | 3001 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 3002 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
3003 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
3004 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
3005 | |
3006 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 3007 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 3008 | |
fa969310 AC |
3009 | ------------------------ |
3010 | -- Make_Artyp_Literal -- | |
3011 | ------------------------ | |
3012 | ||
3013 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
3014 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
3015 | begin | |
3016 | Set_Etype (Result, Artyp); | |
3017 | Set_Analyzed (Result, True); | |
3018 | Set_Is_Static_Expression (Result, False); | |
3019 | return Result; | |
3020 | end Make_Artyp_Literal; | |
76c597a1 | 3021 | |
fdac1f80 | 3022 | -------------- |
46ff89f3 | 3023 | -- To_Artyp -- |
fdac1f80 AC |
3024 | -------------- |
3025 | ||
46ff89f3 | 3026 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 3027 | begin |
46ff89f3 | 3028 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
3029 | return X; |
3030 | ||
3031 | elsif Is_Enumeration_Type (Ityp) then | |
3032 | return | |
3033 | Make_Attribute_Reference (Loc, | |
3034 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
3035 | Attribute_Name => Name_Pos, | |
3036 | Expressions => New_List (X)); | |
3037 | ||
3038 | else | |
46ff89f3 | 3039 | return Convert_To (Artyp, X); |
fdac1f80 | 3040 | end if; |
46ff89f3 | 3041 | end To_Artyp; |
fdac1f80 AC |
3042 | |
3043 | ------------- | |
3044 | -- To_Ityp -- | |
3045 | ------------- | |
3046 | ||
3047 | function To_Ityp (X : Node_Id) return Node_Id is | |
3048 | begin | |
2fc05e3d | 3049 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
3050 | return |
3051 | Make_Attribute_Reference (Loc, | |
3052 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
3053 | Attribute_Name => Name_Val, | |
3054 | Expressions => New_List (X)); | |
3055 | ||
3056 | -- Case where we will do a type conversion | |
3057 | ||
3058 | else | |
76c597a1 AC |
3059 | if Ityp = Base_Type (Artyp) then |
3060 | return X; | |
fdac1f80 | 3061 | else |
76c597a1 | 3062 | return Convert_To (Ityp, X); |
fdac1f80 AC |
3063 | end if; |
3064 | end if; | |
3065 | end To_Ityp; | |
3066 | ||
3067 | -- Local Declarations | |
3068 | ||
00ba7be8 AC |
3069 | Lib_Level_Target : constant Boolean := |
3070 | Nkind (Parent (Cnode)) = N_Object_Declaration | |
3071 | and then | |
3072 | Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode))); | |
3073 | ||
3074 | -- If the concatenation declares a library level entity, we call the | |
3075 | -- built-in concatenation routines to prevent code bloat, regardless | |
3076 | -- of optimization level. This is space-efficient, and prevent linking | |
3077 | -- problems when units are compiled with different optimizations. | |
3078 | ||
0ac73189 AC |
3079 | Opnd_Typ : Entity_Id; |
3080 | Ent : Entity_Id; | |
3081 | Len : Uint; | |
3082 | J : Nat; | |
3083 | Clen : Node_Id; | |
3084 | Set : Boolean; | |
70482933 | 3085 | |
f46faa08 AC |
3086 | -- Start of processing for Expand_Concatenate |
3087 | ||
70482933 | 3088 | begin |
fdac1f80 AC |
3089 | -- Choose an appropriate computational type |
3090 | ||
3091 | -- We will be doing calculations of lengths and bounds in this routine | |
3092 | -- and computing one from the other in some cases, e.g. getting the high | |
3093 | -- bound by adding the length-1 to the low bound. | |
3094 | ||
3095 | -- We can't just use the index type, or even its base type for this | |
3096 | -- purpose for two reasons. First it might be an enumeration type which | |
308e6f3a RW |
3097 | -- is not suitable for computations of any kind, and second it may |
3098 | -- simply not have enough range. For example if the index type is | |
3099 | -- -128..+127 then lengths can be up to 256, which is out of range of | |
3100 | -- the type. | |
fdac1f80 AC |
3101 | |
3102 | -- For enumeration types, we can simply use Standard_Integer, this is | |
3103 | -- sufficient since the actual number of enumeration literals cannot | |
3104 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 3105 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
3106 | |
3107 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 3108 | Artyp := Standard_Integer; |
fdac1f80 | 3109 | |
59262ebb AC |
3110 | -- If index type is Positive, we use the standard unsigned type, to give |
3111 | -- more room on the top of the range, obviating the need for an overflow | |
3112 | -- check when creating the upper bound. This is needed to avoid junk | |
3113 | -- overflow checks in the common case of String types. | |
3114 | ||
3115 | -- ??? Disabled for now | |
3116 | ||
3117 | -- elsif Istyp = Standard_Positive then | |
3118 | -- Artyp := Standard_Unsigned; | |
3119 | ||
2fc05e3d AC |
3120 | -- For modular types, we use a 32-bit modular type for types whose size |
3121 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
3122 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 3123 | |
2fc05e3d | 3124 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 3125 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 3126 | Artyp := Standard_Unsigned; |
ecc4ddde | 3127 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 3128 | Artyp := Ityp; |
fdac1f80 | 3129 | else |
46ff89f3 | 3130 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
3131 | end if; |
3132 | ||
2fc05e3d | 3133 | -- Similar treatment for signed types |
fdac1f80 AC |
3134 | |
3135 | else | |
ecc4ddde | 3136 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 3137 | Artyp := Standard_Integer; |
ecc4ddde | 3138 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 3139 | Artyp := Ityp; |
fdac1f80 | 3140 | else |
46ff89f3 | 3141 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
3142 | end if; |
3143 | end if; | |
3144 | ||
fa969310 AC |
3145 | -- Supply dummy entry at start of length array |
3146 | ||
3147 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
3148 | ||
fdac1f80 | 3149 | -- Go through operands setting up the above arrays |
70482933 | 3150 | |
df46b832 AC |
3151 | J := 1; |
3152 | while J <= N loop | |
3153 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 3154 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
3155 | |
3156 | -- The parent got messed up when we put the operands in a list, | |
d347f572 AC |
3157 | -- so now put back the proper parent for the saved operand, that |
3158 | -- is to say the concatenation node, to make sure that each operand | |
3159 | -- is seen as a subexpression, e.g. if actions must be inserted. | |
fdac1f80 | 3160 | |
d347f572 | 3161 | Set_Parent (Opnd, Cnode); |
fdac1f80 AC |
3162 | |
3163 | -- Set will be True when we have setup one entry in the array | |
3164 | ||
df46b832 AC |
3165 | Set := False; |
3166 | ||
fdac1f80 | 3167 | -- Singleton element (or character literal) case |
df46b832 | 3168 | |
0ac73189 | 3169 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
3170 | NN := NN + 1; |
3171 | Operands (NN) := Opnd; | |
3172 | Is_Fixed_Length (NN) := True; | |
3173 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 3174 | Result_May_Be_Null := False; |
fdac1f80 | 3175 | |
a29262fd AC |
3176 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
3177 | -- since we know that the result cannot be null). | |
fdac1f80 | 3178 | |
0ac73189 AC |
3179 | Opnd_Low_Bound (NN) := |
3180 | Make_Attribute_Reference (Loc, | |
e4494292 | 3181 | Prefix => New_Occurrence_Of (Istyp, Loc), |
0ac73189 AC |
3182 | Attribute_Name => Name_First); |
3183 | ||
df46b832 AC |
3184 | Set := True; |
3185 | ||
fdac1f80 | 3186 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
3187 | |
3188 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 3189 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 3190 | |
a29262fd AC |
3191 | if Len /= 0 then |
3192 | Result_May_Be_Null := False; | |
3193 | end if; | |
3194 | ||
88a27b18 | 3195 | -- Capture last operand low and high bound if result could be null |
a29262fd AC |
3196 | |
3197 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3198 | Last_Opnd_Low_Bound := |
3199 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3200 | ||
a29262fd | 3201 | Last_Opnd_High_Bound := |
88a27b18 | 3202 | Make_Op_Subtract (Loc, |
a29262fd AC |
3203 | Left_Opnd => |
3204 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 3205 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
3206 | end if; |
3207 | ||
3208 | -- Skip null string literal | |
fdac1f80 | 3209 | |
0ac73189 | 3210 | if J < N and then Len = 0 then |
df46b832 AC |
3211 | goto Continue; |
3212 | end if; | |
3213 | ||
3214 | NN := NN + 1; | |
3215 | Operands (NN) := Opnd; | |
3216 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
3217 | |
3218 | -- Set length and bounds | |
3219 | ||
df46b832 | 3220 | Fixed_Length (NN) := Len; |
0ac73189 AC |
3221 | |
3222 | Opnd_Low_Bound (NN) := | |
3223 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3224 | ||
df46b832 AC |
3225 | Set := True; |
3226 | ||
3227 | -- All other cases | |
3228 | ||
3229 | else | |
3230 | -- Check constrained case with known bounds | |
3231 | ||
0ac73189 | 3232 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 3233 | declare |
df46b832 AC |
3234 | Index : constant Node_Id := First_Index (Opnd_Typ); |
3235 | Indx_Typ : constant Entity_Id := Etype (Index); | |
3236 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
3237 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
3238 | ||
3239 | begin | |
fdac1f80 AC |
3240 | -- Fixed length constrained array type with known at compile |
3241 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
3242 | |
3243 | if Compile_Time_Known_Value (Lo) | |
3244 | and then | |
3245 | Compile_Time_Known_Value (Hi) | |
3246 | then | |
3247 | declare | |
3248 | Loval : constant Uint := Expr_Value (Lo); | |
3249 | Hival : constant Uint := Expr_Value (Hi); | |
3250 | Len : constant Uint := | |
3251 | UI_Max (Hival - Loval + 1, Uint_0); | |
3252 | ||
3253 | begin | |
0ac73189 AC |
3254 | if Len > 0 then |
3255 | Result_May_Be_Null := False; | |
df46b832 | 3256 | end if; |
0ac73189 | 3257 | |
88a27b18 | 3258 | -- Capture last operand bounds if result could be null |
a29262fd AC |
3259 | |
3260 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3261 | Last_Opnd_Low_Bound := |
3262 | Convert_To (Ityp, | |
3263 | Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
3264 | ||
a29262fd AC |
3265 | Last_Opnd_High_Bound := |
3266 | Convert_To (Ityp, | |
39ade2f9 | 3267 | Make_Integer_Literal (Loc, Expr_Value (Hi))); |
a29262fd AC |
3268 | end if; |
3269 | ||
3270 | -- Exclude null length case unless last operand | |
0ac73189 | 3271 | |
a29262fd | 3272 | if J < N and then Len = 0 then |
0ac73189 AC |
3273 | goto Continue; |
3274 | end if; | |
3275 | ||
3276 | NN := NN + 1; | |
3277 | Operands (NN) := Opnd; | |
3278 | Is_Fixed_Length (NN) := True; | |
3279 | Fixed_Length (NN) := Len; | |
3280 | ||
39ade2f9 AC |
3281 | Opnd_Low_Bound (NN) := |
3282 | To_Ityp | |
3283 | (Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
0ac73189 | 3284 | Set := True; |
df46b832 AC |
3285 | end; |
3286 | end if; | |
3287 | end; | |
3288 | end if; | |
3289 | ||
0ac73189 AC |
3290 | -- All cases where the length is not known at compile time, or the |
3291 | -- special case of an operand which is known to be null but has a | |
3292 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
3293 | |
3294 | if not Set then | |
3295 | NN := NN + 1; | |
0ac73189 AC |
3296 | |
3297 | -- Capture operand bounds | |
3298 | ||
3299 | Opnd_Low_Bound (NN) := | |
3300 | Make_Attribute_Reference (Loc, | |
3301 | Prefix => | |
3302 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3303 | Attribute_Name => Name_First); | |
3304 | ||
88a27b18 AC |
3305 | -- Capture last operand bounds if result could be null |
3306 | ||
a29262fd | 3307 | if J = N and Result_May_Be_Null then |
88a27b18 AC |
3308 | Last_Opnd_Low_Bound := |
3309 | Convert_To (Ityp, | |
3310 | Make_Attribute_Reference (Loc, | |
3311 | Prefix => | |
3312 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3313 | Attribute_Name => Name_First)); | |
3314 | ||
a29262fd AC |
3315 | Last_Opnd_High_Bound := |
3316 | Convert_To (Ityp, | |
3317 | Make_Attribute_Reference (Loc, | |
3318 | Prefix => | |
3319 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3320 | Attribute_Name => Name_Last)); | |
3321 | end if; | |
0ac73189 AC |
3322 | |
3323 | -- Capture length of operand in entity | |
3324 | ||
df46b832 AC |
3325 | Operands (NN) := Opnd; |
3326 | Is_Fixed_Length (NN) := False; | |
3327 | ||
191fcb3a | 3328 | Var_Length (NN) := Make_Temporary (Loc, 'L'); |
df46b832 | 3329 | |
d0f8d157 | 3330 | Append_To (Actions, |
df46b832 AC |
3331 | Make_Object_Declaration (Loc, |
3332 | Defining_Identifier => Var_Length (NN), | |
3333 | Constant_Present => True, | |
39ade2f9 | 3334 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3335 | Expression => |
3336 | Make_Attribute_Reference (Loc, | |
3337 | Prefix => | |
3338 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
d0f8d157 | 3339 | Attribute_Name => Name_Length))); |
df46b832 AC |
3340 | end if; |
3341 | end if; | |
3342 | ||
3343 | -- Set next entry in aggregate length array | |
3344 | ||
3345 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 3346 | -- or a reference to the saved length for variable length. |
df46b832 AC |
3347 | |
3348 | if NN = 1 then | |
3349 | if Is_Fixed_Length (1) then | |
39ade2f9 | 3350 | Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1)); |
df46b832 | 3351 | else |
e4494292 | 3352 | Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc); |
df46b832 AC |
3353 | end if; |
3354 | ||
3355 | -- If entry is fixed length and only fixed lengths so far, make | |
3356 | -- appropriate new integer literal adding new length. | |
3357 | ||
3358 | elsif Is_Fixed_Length (NN) | |
3359 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
3360 | then | |
3361 | Aggr_Length (NN) := | |
3362 | Make_Integer_Literal (Loc, | |
3363 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
3364 | ||
d0f8d157 AC |
3365 | -- All other cases, construct an addition node for the length and |
3366 | -- create an entity initialized to this length. | |
df46b832 AC |
3367 | |
3368 | else | |
191fcb3a | 3369 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 AC |
3370 | |
3371 | if Is_Fixed_Length (NN) then | |
3372 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
3373 | else | |
e4494292 | 3374 | Clen := New_Occurrence_Of (Var_Length (NN), Loc); |
df46b832 AC |
3375 | end if; |
3376 | ||
d0f8d157 | 3377 | Append_To (Actions, |
df46b832 AC |
3378 | Make_Object_Declaration (Loc, |
3379 | Defining_Identifier => Ent, | |
3380 | Constant_Present => True, | |
39ade2f9 | 3381 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3382 | Expression => |
3383 | Make_Op_Add (Loc, | |
3384 | Left_Opnd => New_Copy (Aggr_Length (NN - 1)), | |
d0f8d157 | 3385 | Right_Opnd => Clen))); |
df46b832 | 3386 | |
76c597a1 | 3387 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
3388 | end if; |
3389 | ||
3390 | <<Continue>> | |
3391 | J := J + 1; | |
3392 | end loop; | |
3393 | ||
a29262fd | 3394 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
3395 | |
3396 | if NN = 0 then | |
a29262fd | 3397 | Result := Opnd; |
df46b832 AC |
3398 | goto Done; |
3399 | end if; | |
3400 | ||
3401 | -- If we have only one non-null operand, return it and we are done. | |
3402 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
3403 | -- the sole operand is of the element type, in which case it must be |
3404 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
3405 | -- through the normal general circuit. |
3406 | ||
533369aa | 3407 | if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then |
df46b832 AC |
3408 | Result := Operands (1); |
3409 | goto Done; | |
3410 | end if; | |
3411 | ||
3412 | -- Cases where we have a real concatenation | |
3413 | ||
fdac1f80 AC |
3414 | -- Next step is to find the low bound for the result array that we |
3415 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
3416 | ||
3417 | -- If the ultimate ancestor of the index subtype is a constrained array | |
3418 | -- definition, then the lower bound is that of the index subtype as | |
3419 | -- specified by (RM 4.5.3(6)). | |
3420 | ||
3421 | -- The right test here is to go to the root type, and then the ultimate | |
3422 | -- ancestor is the first subtype of this root type. | |
3423 | ||
3424 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 3425 | Low_Bound := |
fdac1f80 AC |
3426 | Make_Attribute_Reference (Loc, |
3427 | Prefix => | |
3428 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 3429 | Attribute_Name => Name_First); |
df46b832 AC |
3430 | |
3431 | -- If the first operand in the list has known length we know that | |
3432 | -- the lower bound of the result is the lower bound of this operand. | |
3433 | ||
fdac1f80 | 3434 | elsif Is_Fixed_Length (1) then |
0ac73189 | 3435 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
3436 | |
3437 | -- OK, we don't know the lower bound, we have to build a horrible | |
9b16cb57 | 3438 | -- if expression node of the form |
df46b832 AC |
3439 | |
3440 | -- if Cond1'Length /= 0 then | |
0ac73189 | 3441 | -- Opnd1 low bound |
df46b832 AC |
3442 | -- else |
3443 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 3444 | -- Opnd2 low bound |
df46b832 AC |
3445 | -- else |
3446 | -- ... | |
3447 | ||
3448 | -- The nesting ends either when we hit an operand whose length is known | |
3449 | -- at compile time, or on reaching the last operand, whose low bound we | |
3450 | -- take unconditionally whether or not it is null. It's easiest to do | |
3451 | -- this with a recursive procedure: | |
3452 | ||
3453 | else | |
3454 | declare | |
3455 | function Get_Known_Bound (J : Nat) return Node_Id; | |
3456 | -- Returns the lower bound determined by operands J .. NN | |
3457 | ||
3458 | --------------------- | |
3459 | -- Get_Known_Bound -- | |
3460 | --------------------- | |
3461 | ||
3462 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 3463 | begin |
0ac73189 AC |
3464 | if Is_Fixed_Length (J) or else J = NN then |
3465 | return New_Copy (Opnd_Low_Bound (J)); | |
70482933 RK |
3466 | |
3467 | else | |
df46b832 | 3468 | return |
9b16cb57 | 3469 | Make_If_Expression (Loc, |
df46b832 AC |
3470 | Expressions => New_List ( |
3471 | ||
3472 | Make_Op_Ne (Loc, | |
e4494292 RD |
3473 | Left_Opnd => |
3474 | New_Occurrence_Of (Var_Length (J), Loc), | |
3475 | Right_Opnd => | |
3476 | Make_Integer_Literal (Loc, 0)), | |
df46b832 | 3477 | |
0ac73189 | 3478 | New_Copy (Opnd_Low_Bound (J)), |
df46b832 | 3479 | Get_Known_Bound (J + 1))); |
70482933 | 3480 | end if; |
df46b832 | 3481 | end Get_Known_Bound; |
70482933 | 3482 | |
df46b832 | 3483 | begin |
191fcb3a | 3484 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 | 3485 | |
d0f8d157 | 3486 | Append_To (Actions, |
df46b832 AC |
3487 | Make_Object_Declaration (Loc, |
3488 | Defining_Identifier => Ent, | |
3489 | Constant_Present => True, | |
0ac73189 | 3490 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
d0f8d157 | 3491 | Expression => Get_Known_Bound (1))); |
df46b832 | 3492 | |
e4494292 | 3493 | Low_Bound := New_Occurrence_Of (Ent, Loc); |
df46b832 AC |
3494 | end; |
3495 | end if; | |
70482933 | 3496 | |
76c597a1 AC |
3497 | -- Now we can safely compute the upper bound, normally |
3498 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
3499 | |
3500 | High_Bound := | |
cc6f5d75 AC |
3501 | To_Ityp |
3502 | (Make_Op_Add (Loc, | |
3503 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), | |
3504 | Right_Opnd => | |
3505 | Make_Op_Subtract (Loc, | |
3506 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
3507 | Right_Opnd => Make_Artyp_Literal (1)))); | |
0ac73189 | 3508 | |
59262ebb | 3509 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
3510 | -- very weird cases, so in the general case we need an overflow check on |
3511 | -- the high bound. We can avoid this for the common case of string types | |
3512 | -- and other types whose index is Positive, since we chose a wider range | |
3513 | -- for the arithmetic type. | |
76c597a1 | 3514 | |
59262ebb AC |
3515 | if Istyp /= Standard_Positive then |
3516 | Activate_Overflow_Check (High_Bound); | |
3517 | end if; | |
76c597a1 AC |
3518 | |
3519 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
3520 | -- case the bounds come from the last operand (so that we get the proper |
3521 | -- bounds if the last operand is super-flat). | |
3522 | ||
0ac73189 | 3523 | if Result_May_Be_Null then |
88a27b18 | 3524 | Low_Bound := |
9b16cb57 | 3525 | Make_If_Expression (Loc, |
88a27b18 AC |
3526 | Expressions => New_List ( |
3527 | Make_Op_Eq (Loc, | |
3528 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
3529 | Right_Opnd => Make_Artyp_Literal (0)), | |
3530 | Last_Opnd_Low_Bound, | |
3531 | Low_Bound)); | |
3532 | ||
0ac73189 | 3533 | High_Bound := |
9b16cb57 | 3534 | Make_If_Expression (Loc, |
0ac73189 AC |
3535 | Expressions => New_List ( |
3536 | Make_Op_Eq (Loc, | |
3537 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 3538 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 3539 | Last_Opnd_High_Bound, |
0ac73189 AC |
3540 | High_Bound)); |
3541 | end if; | |
3542 | ||
d0f8d157 AC |
3543 | -- Here is where we insert the saved up actions |
3544 | ||
3545 | Insert_Actions (Cnode, Actions, Suppress => All_Checks); | |
3546 | ||
602a7ec0 AC |
3547 | -- Now we construct an array object with appropriate bounds. We mark |
3548 | -- the target as internal to prevent useless initialization when | |
e526d0c7 AC |
3549 | -- Initialize_Scalars is enabled. Also since this is the actual result |
3550 | -- entity, we make sure we have debug information for the result. | |
70482933 | 3551 | |
191fcb3a | 3552 | Ent := Make_Temporary (Loc, 'S'); |
008f6fd3 | 3553 | Set_Is_Internal (Ent); |
e526d0c7 | 3554 | Set_Needs_Debug_Info (Ent); |
70482933 | 3555 | |
76c597a1 | 3556 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
3557 | -- to abort, we want a warning and a runtime constraint error. Note that |
3558 | -- we have arranged that the result will not be treated as a static | |
3559 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 3560 | |
df46b832 AC |
3561 | Insert_Action (Cnode, |
3562 | Make_Object_Declaration (Loc, | |
3563 | Defining_Identifier => Ent, | |
df46b832 AC |
3564 | Object_Definition => |
3565 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 3566 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
3567 | Constraint => |
3568 | Make_Index_Or_Discriminant_Constraint (Loc, | |
3569 | Constraints => New_List ( | |
3570 | Make_Range (Loc, | |
0ac73189 AC |
3571 | Low_Bound => Low_Bound, |
3572 | High_Bound => High_Bound))))), | |
df46b832 AC |
3573 | Suppress => All_Checks); |
3574 | ||
d1f453b7 RD |
3575 | -- If the result of the concatenation appears as the initializing |
3576 | -- expression of an object declaration, we can just rename the | |
3577 | -- result, rather than copying it. | |
3578 | ||
3579 | Set_OK_To_Rename (Ent); | |
3580 | ||
76c597a1 AC |
3581 | -- Catch the static out of range case now |
3582 | ||
3583 | if Raises_Constraint_Error (High_Bound) then | |
3584 | raise Concatenation_Error; | |
3585 | end if; | |
3586 | ||
df46b832 AC |
3587 | -- Now we will generate the assignments to do the actual concatenation |
3588 | ||
bded454f RD |
3589 | -- There is one case in which we will not do this, namely when all the |
3590 | -- following conditions are met: | |
3591 | ||
3592 | -- The result type is Standard.String | |
3593 | ||
3594 | -- There are nine or fewer retained (non-null) operands | |
3595 | ||
ffec8e81 | 3596 | -- The optimization level is -O0 |
bded454f RD |
3597 | |
3598 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
3599 | -- available in the run time. | |
3600 | ||
3601 | -- The debug flag gnatd.c is not set | |
3602 | ||
3603 | -- If all these conditions are met then we generate a call to the | |
3604 | -- relevant concatenation routine. The purpose of this is to avoid | |
3605 | -- undesirable code bloat at -O0. | |
3606 | ||
3607 | if Atyp = Standard_String | |
3608 | and then NN in 2 .. 9 | |
00ba7be8 | 3609 | and then (Lib_Level_Target |
cc6f5d75 AC |
3610 | or else ((Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC) |
3611 | and then not Debug_Flag_Dot_C)) | |
bded454f RD |
3612 | then |
3613 | declare | |
3614 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
3615 | (RE_Str_Concat_2, | |
3616 | RE_Str_Concat_3, | |
3617 | RE_Str_Concat_4, | |
3618 | RE_Str_Concat_5, | |
3619 | RE_Str_Concat_6, | |
3620 | RE_Str_Concat_7, | |
3621 | RE_Str_Concat_8, | |
3622 | RE_Str_Concat_9); | |
3623 | ||
3624 | begin | |
3625 | if RTE_Available (RR (NN)) then | |
3626 | declare | |
3627 | Opnds : constant List_Id := | |
3628 | New_List (New_Occurrence_Of (Ent, Loc)); | |
3629 | ||
3630 | begin | |
3631 | for J in 1 .. NN loop | |
3632 | if Is_List_Member (Operands (J)) then | |
3633 | Remove (Operands (J)); | |
3634 | end if; | |
3635 | ||
3636 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
3637 | Append_To (Opnds, | |
3638 | Make_Aggregate (Loc, | |
3639 | Component_Associations => New_List ( | |
3640 | Make_Component_Association (Loc, | |
3641 | Choices => New_List ( | |
3642 | Make_Integer_Literal (Loc, 1)), | |
3643 | Expression => Operands (J))))); | |
3644 | ||
3645 | else | |
3646 | Append_To (Opnds, Operands (J)); | |
3647 | end if; | |
3648 | end loop; | |
3649 | ||
3650 | Insert_Action (Cnode, | |
3651 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 3652 | Name => New_Occurrence_Of (RTE (RR (NN)), Loc), |
bded454f RD |
3653 | Parameter_Associations => Opnds)); |
3654 | ||
e4494292 | 3655 | Result := New_Occurrence_Of (Ent, Loc); |
bded454f RD |
3656 | goto Done; |
3657 | end; | |
3658 | end if; | |
3659 | end; | |
3660 | end if; | |
3661 | ||
3662 | -- Not special case so generate the assignments | |
3663 | ||
76c597a1 AC |
3664 | Known_Non_Null_Operand_Seen := False; |
3665 | ||
df46b832 AC |
3666 | for J in 1 .. NN loop |
3667 | declare | |
3668 | Lo : constant Node_Id := | |
3669 | Make_Op_Add (Loc, | |
46ff89f3 | 3670 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3671 | Right_Opnd => Aggr_Length (J - 1)); |
3672 | ||
3673 | Hi : constant Node_Id := | |
3674 | Make_Op_Add (Loc, | |
46ff89f3 | 3675 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3676 | Right_Opnd => |
3677 | Make_Op_Subtract (Loc, | |
3678 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 3679 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 3680 | |
df46b832 | 3681 | begin |
fdac1f80 AC |
3682 | -- Singleton case, simple assignment |
3683 | ||
3684 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 3685 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
3686 | Insert_Action (Cnode, |
3687 | Make_Assignment_Statement (Loc, | |
3688 | Name => | |
3689 | Make_Indexed_Component (Loc, | |
3690 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 3691 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
3692 | Expression => Operands (J)), |
3693 | Suppress => All_Checks); | |
70482933 | 3694 | |
76c597a1 AC |
3695 | -- Array case, slice assignment, skipped when argument is fixed |
3696 | -- length and known to be null. | |
fdac1f80 | 3697 | |
76c597a1 AC |
3698 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
3699 | declare | |
3700 | Assign : Node_Id := | |
3701 | Make_Assignment_Statement (Loc, | |
3702 | Name => | |
3703 | Make_Slice (Loc, | |
3704 | Prefix => | |
3705 | New_Occurrence_Of (Ent, Loc), | |
3706 | Discrete_Range => | |
3707 | Make_Range (Loc, | |
3708 | Low_Bound => To_Ityp (Lo), | |
3709 | High_Bound => To_Ityp (Hi))), | |
3710 | Expression => Operands (J)); | |
3711 | begin | |
3712 | if Is_Fixed_Length (J) then | |
3713 | Known_Non_Null_Operand_Seen := True; | |
3714 | ||
3715 | elsif not Known_Non_Null_Operand_Seen then | |
3716 | ||
3717 | -- Here if operand length is not statically known and no | |
3718 | -- operand known to be non-null has been processed yet. | |
3719 | -- If operand length is 0, we do not need to perform the | |
3720 | -- assignment, and we must avoid the evaluation of the | |
3721 | -- high bound of the slice, since it may underflow if the | |
3722 | -- low bound is Ityp'First. | |
3723 | ||
3724 | Assign := | |
3725 | Make_Implicit_If_Statement (Cnode, | |
39ade2f9 | 3726 | Condition => |
76c597a1 | 3727 | Make_Op_Ne (Loc, |
39ade2f9 | 3728 | Left_Opnd => |
76c597a1 AC |
3729 | New_Occurrence_Of (Var_Length (J), Loc), |
3730 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
39ade2f9 | 3731 | Then_Statements => New_List (Assign)); |
76c597a1 | 3732 | end if; |
fa969310 | 3733 | |
76c597a1 AC |
3734 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
3735 | end; | |
df46b832 AC |
3736 | end if; |
3737 | end; | |
3738 | end loop; | |
70482933 | 3739 | |
0ac73189 AC |
3740 | -- Finally we build the result, which is a reference to the array object |
3741 | ||
e4494292 | 3742 | Result := New_Occurrence_Of (Ent, Loc); |
70482933 | 3743 | |
df46b832 AC |
3744 | <<Done>> |
3745 | Rewrite (Cnode, Result); | |
fdac1f80 AC |
3746 | Analyze_And_Resolve (Cnode, Atyp); |
3747 | ||
3748 | exception | |
3749 | when Concatenation_Error => | |
76c597a1 AC |
3750 | |
3751 | -- Kill warning generated for the declaration of the static out of | |
3752 | -- range high bound, and instead generate a Constraint_Error with | |
3753 | -- an appropriate specific message. | |
3754 | ||
3755 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
3756 | Apply_Compile_Time_Constraint_Error | |
3757 | (N => Cnode, | |
324ac540 | 3758 | Msg => "concatenation result upper bound out of range??", |
76c597a1 | 3759 | Reason => CE_Range_Check_Failed); |
fdac1f80 | 3760 | end Expand_Concatenate; |
70482933 | 3761 | |
f6194278 RD |
3762 | --------------------------------------------------- |
3763 | -- Expand_Membership_Minimize_Eliminate_Overflow -- | |
3764 | --------------------------------------------------- | |
3765 | ||
3766 | procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is | |
3767 | pragma Assert (Nkind (N) = N_In); | |
3768 | -- Despite the name, this routine applies only to N_In, not to | |
3769 | -- N_Not_In. The latter is always rewritten as not (X in Y). | |
3770 | ||
71fb4dc8 AC |
3771 | Result_Type : constant Entity_Id := Etype (N); |
3772 | -- Capture result type, may be a derived boolean type | |
3773 | ||
b6b5cca8 AC |
3774 | Loc : constant Source_Ptr := Sloc (N); |
3775 | Lop : constant Node_Id := Left_Opnd (N); | |
3776 | Rop : constant Node_Id := Right_Opnd (N); | |
3777 | ||
3778 | -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It | |
3779 | -- is thus tempting to capture these values, but due to the rewrites | |
3780 | -- that occur as a result of overflow checking, these values change | |
3781 | -- as we go along, and it is safe just to always use Etype explicitly. | |
f6194278 RD |
3782 | |
3783 | Restype : constant Entity_Id := Etype (N); | |
3784 | -- Save result type | |
3785 | ||
3786 | Lo, Hi : Uint; | |
d8192289 | 3787 | -- Bounds in Minimize calls, not used currently |
f6194278 RD |
3788 | |
3789 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
3790 | -- Entity for Long_Long_Integer'Base (Standard should export this???) | |
3791 | ||
3792 | begin | |
a7f1b24f | 3793 | Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False); |
f6194278 RD |
3794 | |
3795 | -- If right operand is a subtype name, and the subtype name has no | |
3796 | -- predicate, then we can just replace the right operand with an | |
3797 | -- explicit range T'First .. T'Last, and use the explicit range code. | |
3798 | ||
b6b5cca8 AC |
3799 | if Nkind (Rop) /= N_Range |
3800 | and then No (Predicate_Function (Etype (Rop))) | |
3801 | then | |
3802 | declare | |
3803 | Rtyp : constant Entity_Id := Etype (Rop); | |
3804 | begin | |
3805 | Rewrite (Rop, | |
3806 | Make_Range (Loc, | |
cc6f5d75 | 3807 | Low_Bound => |
b6b5cca8 AC |
3808 | Make_Attribute_Reference (Loc, |
3809 | Attribute_Name => Name_First, | |
e4494292 | 3810 | Prefix => New_Occurrence_Of (Rtyp, Loc)), |
b6b5cca8 AC |
3811 | High_Bound => |
3812 | Make_Attribute_Reference (Loc, | |
3813 | Attribute_Name => Name_Last, | |
e4494292 | 3814 | Prefix => New_Occurrence_Of (Rtyp, Loc)))); |
b6b5cca8 AC |
3815 | Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks); |
3816 | end; | |
f6194278 RD |
3817 | end if; |
3818 | ||
3819 | -- Here for the explicit range case. Note that the bounds of the range | |
3820 | -- have not been processed for minimized or eliminated checks. | |
3821 | ||
3822 | if Nkind (Rop) = N_Range then | |
a7f1b24f | 3823 | Minimize_Eliminate_Overflows |
b6b5cca8 | 3824 | (Low_Bound (Rop), Lo, Hi, Top_Level => False); |
a7f1b24f | 3825 | Minimize_Eliminate_Overflows |
c7e152b5 | 3826 | (High_Bound (Rop), Lo, Hi, Top_Level => False); |
f6194278 RD |
3827 | |
3828 | -- We have A in B .. C, treated as A >= B and then A <= C | |
3829 | ||
3830 | -- Bignum case | |
3831 | ||
b6b5cca8 | 3832 | if Is_RTE (Etype (Lop), RE_Bignum) |
f6194278 RD |
3833 | or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum) |
3834 | or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum) | |
3835 | then | |
3836 | declare | |
3837 | Blk : constant Node_Id := Make_Bignum_Block (Loc); | |
3838 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
71fb4dc8 AC |
3839 | L : constant Entity_Id := |
3840 | Make_Defining_Identifier (Loc, Name_uL); | |
f6194278 RD |
3841 | Lopnd : constant Node_Id := Convert_To_Bignum (Lop); |
3842 | Lbound : constant Node_Id := | |
3843 | Convert_To_Bignum (Low_Bound (Rop)); | |
3844 | Hbound : constant Node_Id := | |
3845 | Convert_To_Bignum (High_Bound (Rop)); | |
3846 | ||
71fb4dc8 AC |
3847 | -- Now we rewrite the membership test node to look like |
3848 | ||
3849 | -- do | |
3850 | -- Bnn : Result_Type; | |
3851 | -- declare | |
3852 | -- M : Mark_Id := SS_Mark; | |
3853 | -- L : Bignum := Lopnd; | |
3854 | -- begin | |
3855 | -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound) | |
3856 | -- SS_Release (M); | |
3857 | -- end; | |
3858 | -- in | |
3859 | -- Bnn | |
3860 | -- end | |
f6194278 RD |
3861 | |
3862 | begin | |
71fb4dc8 AC |
3863 | -- Insert declaration of L into declarations of bignum block |
3864 | ||
f6194278 RD |
3865 | Insert_After |
3866 | (Last (Declarations (Blk)), | |
3867 | Make_Object_Declaration (Loc, | |
71fb4dc8 | 3868 | Defining_Identifier => L, |
f6194278 RD |
3869 | Object_Definition => |
3870 | New_Occurrence_Of (RTE (RE_Bignum), Loc), | |
3871 | Expression => Lopnd)); | |
3872 | ||
71fb4dc8 AC |
3873 | -- Insert assignment to Bnn into expressions of bignum block |
3874 | ||
f6194278 RD |
3875 | Insert_Before |
3876 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
3877 | Make_Assignment_Statement (Loc, | |
3878 | Name => New_Occurrence_Of (Bnn, Loc), | |
3879 | Expression => | |
3880 | Make_And_Then (Loc, | |
cc6f5d75 | 3881 | Left_Opnd => |
f6194278 RD |
3882 | Make_Function_Call (Loc, |
3883 | Name => | |
3884 | New_Occurrence_Of (RTE (RE_Big_GE), Loc), | |
71fb4dc8 AC |
3885 | Parameter_Associations => New_List ( |
3886 | New_Occurrence_Of (L, Loc), | |
3887 | Lbound)), | |
cc6f5d75 | 3888 | |
f6194278 RD |
3889 | Right_Opnd => |
3890 | Make_Function_Call (Loc, | |
3891 | Name => | |
71fb4dc8 AC |
3892 | New_Occurrence_Of (RTE (RE_Big_LE), Loc), |
3893 | Parameter_Associations => New_List ( | |
3894 | New_Occurrence_Of (L, Loc), | |
3895 | Hbound))))); | |
f6194278 | 3896 | |
71fb4dc8 | 3897 | -- Now rewrite the node |
f6194278 | 3898 | |
71fb4dc8 AC |
3899 | Rewrite (N, |
3900 | Make_Expression_With_Actions (Loc, | |
3901 | Actions => New_List ( | |
3902 | Make_Object_Declaration (Loc, | |
3903 | Defining_Identifier => Bnn, | |
3904 | Object_Definition => | |
3905 | New_Occurrence_Of (Result_Type, Loc)), | |
3906 | Blk), | |
3907 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
3908 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
3909 | return; |
3910 | end; | |
3911 | ||
3912 | -- Here if no bignums around | |
3913 | ||
3914 | else | |
3915 | -- Case where types are all the same | |
3916 | ||
b6b5cca8 | 3917 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop))) |
f6194278 | 3918 | and then |
b6b5cca8 | 3919 | Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop))) |
f6194278 RD |
3920 | then |
3921 | null; | |
3922 | ||
3923 | -- If types are not all the same, it means that we have rewritten | |
3924 | -- at least one of them to be of type Long_Long_Integer, and we | |
3925 | -- will convert the other operands to Long_Long_Integer. | |
3926 | ||
3927 | else | |
3928 | Convert_To_And_Rewrite (LLIB, Lop); | |
71fb4dc8 AC |
3929 | Set_Analyzed (Lop, False); |
3930 | Analyze_And_Resolve (Lop, LLIB); | |
3931 | ||
3932 | -- For the right operand, avoid unnecessary recursion into | |
3933 | -- this routine, we know that overflow is not possible. | |
f6194278 RD |
3934 | |
3935 | Convert_To_And_Rewrite (LLIB, Low_Bound (Rop)); | |
3936 | Convert_To_And_Rewrite (LLIB, High_Bound (Rop)); | |
3937 | Set_Analyzed (Rop, False); | |
71fb4dc8 | 3938 | Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check); |
f6194278 RD |
3939 | end if; |
3940 | ||
3941 | -- Now the three operands are of the same signed integer type, | |
b6b5cca8 AC |
3942 | -- so we can use the normal expansion routine for membership, |
3943 | -- setting the flag to prevent recursion into this procedure. | |
f6194278 RD |
3944 | |
3945 | Set_No_Minimize_Eliminate (N); | |
3946 | Expand_N_In (N); | |
3947 | end if; | |
3948 | ||
3949 | -- Right operand is a subtype name and the subtype has a predicate. We | |
f6636994 AC |
3950 | -- have to make sure the predicate is checked, and for that we need to |
3951 | -- use the standard N_In circuitry with appropriate types. | |
f6194278 RD |
3952 | |
3953 | else | |
b6b5cca8 | 3954 | pragma Assert (Present (Predicate_Function (Etype (Rop)))); |
f6194278 RD |
3955 | |
3956 | -- If types are "right", just call Expand_N_In preventing recursion | |
3957 | ||
b6b5cca8 | 3958 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then |
f6194278 RD |
3959 | Set_No_Minimize_Eliminate (N); |
3960 | Expand_N_In (N); | |
3961 | ||
3962 | -- Bignum case | |
3963 | ||
b6b5cca8 | 3964 | elsif Is_RTE (Etype (Lop), RE_Bignum) then |
f6194278 | 3965 | |
71fb4dc8 | 3966 | -- For X in T, we want to rewrite our node as |
f6194278 | 3967 | |
71fb4dc8 AC |
3968 | -- do |
3969 | -- Bnn : Result_Type; | |
f6194278 | 3970 | |
71fb4dc8 AC |
3971 | -- declare |
3972 | -- M : Mark_Id := SS_Mark; | |
3973 | -- Lnn : Long_Long_Integer'Base | |
3974 | -- Nnn : Bignum; | |
f6194278 | 3975 | |
71fb4dc8 AC |
3976 | -- begin |
3977 | -- Nnn := X; | |
3978 | ||
3979 | -- if not Bignum_In_LLI_Range (Nnn) then | |
3980 | -- Bnn := False; | |
3981 | -- else | |
3982 | -- Lnn := From_Bignum (Nnn); | |
3983 | -- Bnn := | |
3984 | -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last) | |
3985 | -- and then T'Base (Lnn) in T; | |
3986 | -- end if; | |
cc6f5d75 AC |
3987 | |
3988 | -- SS_Release (M); | |
71fb4dc8 AC |
3989 | -- end |
3990 | -- in | |
3991 | -- Bnn | |
3992 | -- end | |
f6194278 | 3993 | |
f6636994 | 3994 | -- A bit gruesome, but there doesn't seem to be a simpler way |
f6194278 RD |
3995 | |
3996 | declare | |
b6b5cca8 AC |
3997 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3998 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
3999 | Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N); | |
4000 | Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N); | |
71fb4dc8 AC |
4001 | T : constant Entity_Id := Etype (Rop); |
4002 | TB : constant Entity_Id := Base_Type (T); | |
b6b5cca8 | 4003 | Nin : Node_Id; |
f6194278 RD |
4004 | |
4005 | begin | |
71fb4dc8 | 4006 | -- Mark the last membership operation to prevent recursion |
f6194278 RD |
4007 | |
4008 | Nin := | |
4009 | Make_In (Loc, | |
f6636994 AC |
4010 | Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)), |
4011 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
4012 | Set_No_Minimize_Eliminate (Nin); |
4013 | ||
4014 | -- Now decorate the block | |
4015 | ||
4016 | Insert_After | |
4017 | (Last (Declarations (Blk)), | |
4018 | Make_Object_Declaration (Loc, | |
4019 | Defining_Identifier => Lnn, | |
4020 | Object_Definition => New_Occurrence_Of (LLIB, Loc))); | |
4021 | ||
4022 | Insert_After | |
4023 | (Last (Declarations (Blk)), | |
4024 | Make_Object_Declaration (Loc, | |
4025 | Defining_Identifier => Nnn, | |
4026 | Object_Definition => | |
4027 | New_Occurrence_Of (RTE (RE_Bignum), Loc))); | |
4028 | ||
4029 | Insert_List_Before | |
4030 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
4031 | New_List ( | |
4032 | Make_Assignment_Statement (Loc, | |
4033 | Name => New_Occurrence_Of (Nnn, Loc), | |
4034 | Expression => Relocate_Node (Lop)), | |
4035 | ||
8b1011c0 | 4036 | Make_Implicit_If_Statement (N, |
f6194278 | 4037 | Condition => |
71fb4dc8 AC |
4038 | Make_Op_Not (Loc, |
4039 | Right_Opnd => | |
4040 | Make_Function_Call (Loc, | |
4041 | Name => | |
4042 | New_Occurrence_Of | |
4043 | (RTE (RE_Bignum_In_LLI_Range), Loc), | |
4044 | Parameter_Associations => New_List ( | |
4045 | New_Occurrence_Of (Nnn, Loc)))), | |
f6194278 RD |
4046 | |
4047 | Then_Statements => New_List ( | |
4048 | Make_Assignment_Statement (Loc, | |
4049 | Name => New_Occurrence_Of (Bnn, Loc), | |
4050 | Expression => | |
4051 | New_Occurrence_Of (Standard_False, Loc))), | |
4052 | ||
4053 | Else_Statements => New_List ( | |
4054 | Make_Assignment_Statement (Loc, | |
4055 | Name => New_Occurrence_Of (Lnn, Loc), | |
4056 | Expression => | |
4057 | Make_Function_Call (Loc, | |
4058 | Name => | |
4059 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4060 | Parameter_Associations => New_List ( | |
4061 | New_Occurrence_Of (Nnn, Loc)))), | |
4062 | ||
4063 | Make_Assignment_Statement (Loc, | |
71fb4dc8 | 4064 | Name => New_Occurrence_Of (Bnn, Loc), |
f6194278 RD |
4065 | Expression => |
4066 | Make_And_Then (Loc, | |
71fb4dc8 | 4067 | Left_Opnd => |
f6194278 | 4068 | Make_In (Loc, |
71fb4dc8 | 4069 | Left_Opnd => New_Occurrence_Of (Lnn, Loc), |
f6194278 | 4070 | Right_Opnd => |
71fb4dc8 AC |
4071 | Make_Range (Loc, |
4072 | Low_Bound => | |
4073 | Convert_To (LLIB, | |
4074 | Make_Attribute_Reference (Loc, | |
4075 | Attribute_Name => Name_First, | |
4076 | Prefix => | |
4077 | New_Occurrence_Of (TB, Loc))), | |
4078 | ||
4079 | High_Bound => | |
4080 | Convert_To (LLIB, | |
4081 | Make_Attribute_Reference (Loc, | |
4082 | Attribute_Name => Name_Last, | |
4083 | Prefix => | |
4084 | New_Occurrence_Of (TB, Loc))))), | |
4085 | ||
f6194278 RD |
4086 | Right_Opnd => Nin)))))); |
4087 | ||
71fb4dc8 | 4088 | -- Now we can do the rewrite |
f6194278 | 4089 | |
71fb4dc8 AC |
4090 | Rewrite (N, |
4091 | Make_Expression_With_Actions (Loc, | |
4092 | Actions => New_List ( | |
4093 | Make_Object_Declaration (Loc, | |
4094 | Defining_Identifier => Bnn, | |
4095 | Object_Definition => | |
4096 | New_Occurrence_Of (Result_Type, Loc)), | |
4097 | Blk), | |
4098 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
4099 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
4100 | return; |
4101 | end; | |
4102 | ||
4103 | -- Not bignum case, but types don't match (this means we rewrote the | |
b6b5cca8 | 4104 | -- left operand to be Long_Long_Integer). |
f6194278 RD |
4105 | |
4106 | else | |
b6b5cca8 | 4107 | pragma Assert (Base_Type (Etype (Lop)) = LLIB); |
f6194278 | 4108 | |
71fb4dc8 AC |
4109 | -- We rewrite the membership test as (where T is the type with |
4110 | -- the predicate, i.e. the type of the right operand) | |
f6194278 | 4111 | |
71fb4dc8 AC |
4112 | -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last) |
4113 | -- and then T'Base (Lop) in T | |
f6194278 RD |
4114 | |
4115 | declare | |
71fb4dc8 AC |
4116 | T : constant Entity_Id := Etype (Rop); |
4117 | TB : constant Entity_Id := Base_Type (T); | |
f6194278 RD |
4118 | Nin : Node_Id; |
4119 | ||
4120 | begin | |
4121 | -- The last membership test is marked to prevent recursion | |
4122 | ||
4123 | Nin := | |
4124 | Make_In (Loc, | |
71fb4dc8 AC |
4125 | Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)), |
4126 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
4127 | Set_No_Minimize_Eliminate (Nin); |
4128 | ||
4129 | -- Now do the rewrite | |
4130 | ||
4131 | Rewrite (N, | |
4132 | Make_And_Then (Loc, | |
71fb4dc8 | 4133 | Left_Opnd => |
f6194278 RD |
4134 | Make_In (Loc, |
4135 | Left_Opnd => Lop, | |
4136 | Right_Opnd => | |
71fb4dc8 AC |
4137 | Make_Range (Loc, |
4138 | Low_Bound => | |
4139 | Convert_To (LLIB, | |
4140 | Make_Attribute_Reference (Loc, | |
4141 | Attribute_Name => Name_First, | |
cc6f5d75 AC |
4142 | Prefix => |
4143 | New_Occurrence_Of (TB, Loc))), | |
71fb4dc8 AC |
4144 | High_Bound => |
4145 | Convert_To (LLIB, | |
4146 | Make_Attribute_Reference (Loc, | |
4147 | Attribute_Name => Name_Last, | |
cc6f5d75 AC |
4148 | Prefix => |
4149 | New_Occurrence_Of (TB, Loc))))), | |
f6194278 | 4150 | Right_Opnd => Nin)); |
71fb4dc8 AC |
4151 | Set_Analyzed (N, False); |
4152 | Analyze_And_Resolve (N, Restype); | |
f6194278 RD |
4153 | end; |
4154 | end if; | |
4155 | end if; | |
4156 | end Expand_Membership_Minimize_Eliminate_Overflow; | |
4157 | ||
70482933 RK |
4158 | ------------------------ |
4159 | -- Expand_N_Allocator -- | |
4160 | ------------------------ | |
4161 | ||
4162 | procedure Expand_N_Allocator (N : Node_Id) is | |
8b1011c0 AC |
4163 | Etyp : constant Entity_Id := Etype (Expression (N)); |
4164 | Loc : constant Source_Ptr := Sloc (N); | |
4165 | PtrT : constant Entity_Id := Etype (N); | |
70482933 | 4166 | |
26bff3d9 JM |
4167 | procedure Rewrite_Coextension (N : Node_Id); |
4168 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 4169 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 4170 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 4171 | |
8aec446b | 4172 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd AC |
4173 | -- Given a constrained array type E, returns a node representing the |
4174 | -- code to compute the size in storage elements for the given type. | |
205c14b0 | 4175 | -- This is done without using the attribute (which malfunctions for |
507ed3fd | 4176 | -- large sizes ???) |
8aec446b | 4177 | |
26bff3d9 JM |
4178 | ------------------------- |
4179 | -- Rewrite_Coextension -- | |
4180 | ------------------------- | |
4181 | ||
4182 | procedure Rewrite_Coextension (N : Node_Id) is | |
e5a22243 AC |
4183 | Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C'); |
4184 | Temp_Decl : Node_Id; | |
26bff3d9 | 4185 | |
df3e68b1 | 4186 | begin |
26bff3d9 JM |
4187 | -- Generate: |
4188 | -- Cnn : aliased Etyp; | |
4189 | ||
df3e68b1 HK |
4190 | Temp_Decl := |
4191 | Make_Object_Declaration (Loc, | |
4192 | Defining_Identifier => Temp_Id, | |
243cae0a AC |
4193 | Aliased_Present => True, |
4194 | Object_Definition => New_Occurrence_Of (Etyp, Loc)); | |
26bff3d9 | 4195 | |
26bff3d9 | 4196 | if Nkind (Expression (N)) = N_Qualified_Expression then |
df3e68b1 | 4197 | Set_Expression (Temp_Decl, Expression (Expression (N))); |
0669bebe | 4198 | end if; |
26bff3d9 | 4199 | |
e5a22243 | 4200 | Insert_Action (N, Temp_Decl); |
26bff3d9 JM |
4201 | Rewrite (N, |
4202 | Make_Attribute_Reference (Loc, | |
243cae0a | 4203 | Prefix => New_Occurrence_Of (Temp_Id, Loc), |
26bff3d9 JM |
4204 | Attribute_Name => Name_Unrestricted_Access)); |
4205 | ||
4206 | Analyze_And_Resolve (N, PtrT); | |
4207 | end Rewrite_Coextension; | |
0669bebe | 4208 | |
8aec446b AC |
4209 | ------------------------------ |
4210 | -- Size_In_Storage_Elements -- | |
4211 | ------------------------------ | |
4212 | ||
4213 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
4214 | begin | |
4215 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
4216 | -- However, the reason for the existence of this function is | |
4217 | -- to construct a test for sizes too large, which means near the | |
4218 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
4219 | -- is that we get overflows when sizes are greater than 2**31. | |
4220 | ||
507ed3fd | 4221 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
4222 | |
4223 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
4224 | ||
46202729 | 4225 | -- which avoids this problem. All this is a bit bogus, but it does |
8aec446b AC |
4226 | -- mean we catch common cases of trying to allocate arrays that |
4227 | -- are too large, and which in the absence of a check results in | |
4228 | -- undetected chaos ??? | |
4229 | ||
ce532f42 AC |
4230 | -- Note in particular that this is a pessimistic estimate in the |
4231 | -- case of packed array types, where an array element might occupy | |
4232 | -- just a fraction of a storage element??? | |
4233 | ||
507ed3fd AC |
4234 | declare |
4235 | Len : Node_Id; | |
4236 | Res : Node_Id; | |
8aec446b | 4237 | |
507ed3fd AC |
4238 | begin |
4239 | for J in 1 .. Number_Dimensions (E) loop | |
4240 | Len := | |
4241 | Make_Attribute_Reference (Loc, | |
4242 | Prefix => New_Occurrence_Of (E, Loc), | |
4243 | Attribute_Name => Name_Length, | |
243cae0a | 4244 | Expressions => New_List (Make_Integer_Literal (Loc, J))); |
8aec446b | 4245 | |
507ed3fd AC |
4246 | if J = 1 then |
4247 | Res := Len; | |
8aec446b | 4248 | |
507ed3fd AC |
4249 | else |
4250 | Res := | |
4251 | Make_Op_Multiply (Loc, | |
4252 | Left_Opnd => Res, | |
4253 | Right_Opnd => Len); | |
4254 | end if; | |
4255 | end loop; | |
8aec446b | 4256 | |
8aec446b | 4257 | return |
507ed3fd AC |
4258 | Make_Op_Multiply (Loc, |
4259 | Left_Opnd => Len, | |
4260 | Right_Opnd => | |
4261 | Make_Attribute_Reference (Loc, | |
4262 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
4263 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
4264 | end; | |
8aec446b AC |
4265 | end Size_In_Storage_Elements; |
4266 | ||
8b1011c0 AC |
4267 | -- Local variables |
4268 | ||
70861157 | 4269 | Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT)); |
8b1011c0 AC |
4270 | Desig : Entity_Id; |
4271 | Nod : Node_Id; | |
4272 | Pool : Entity_Id; | |
4273 | Rel_Typ : Entity_Id; | |
4274 | Temp : Entity_Id; | |
4275 | ||
0669bebe GB |
4276 | -- Start of processing for Expand_N_Allocator |
4277 | ||
70482933 RK |
4278 | begin |
4279 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
4280 | -- shall not be a remote access-to-class-wide-limited-private type | |
4281 | ||
4282 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
4283 | ||
4284 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
4285 | ||
ca5af305 AC |
4286 | -- Processing for anonymous access-to-controlled types. These access |
4287 | -- types receive a special finalization master which appears in the | |
4288 | -- declarations of the enclosing semantic unit. This expansion is done | |
84f4072a JM |
4289 | -- now to ensure that any additional types generated by this routine or |
4290 | -- Expand_Allocator_Expression inherit the proper type attributes. | |
ca5af305 | 4291 | |
84f4072a | 4292 | if (Ekind (PtrT) = E_Anonymous_Access_Type |
533369aa | 4293 | or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT)))) |
ca5af305 AC |
4294 | and then Needs_Finalization (Dtyp) |
4295 | then | |
8b1011c0 AC |
4296 | -- Detect the allocation of an anonymous controlled object where the |
4297 | -- type of the context is named. For example: | |
4298 | ||
4299 | -- procedure Proc (Ptr : Named_Access_Typ); | |
4300 | -- Proc (new Designated_Typ); | |
4301 | ||
4302 | -- Regardless of the anonymous-to-named access type conversion, the | |
4303 | -- lifetime of the object must be associated with the named access | |
0088ba92 | 4304 | -- type. Use the finalization-related attributes of this type. |
8b1011c0 AC |
4305 | |
4306 | if Nkind_In (Parent (N), N_Type_Conversion, | |
4307 | N_Unchecked_Type_Conversion) | |
4308 | and then Ekind_In (Etype (Parent (N)), E_Access_Subtype, | |
4309 | E_Access_Type, | |
4310 | E_General_Access_Type) | |
4311 | then | |
4312 | Rel_Typ := Etype (Parent (N)); | |
4313 | else | |
4314 | Rel_Typ := Empty; | |
4315 | end if; | |
4316 | ||
b254da66 AC |
4317 | -- Anonymous access-to-controlled types allocate on the global pool. |
4318 | -- Do not set this attribute on .NET/JVM since those targets do not | |
4319 | -- support pools. | |
ca5af305 | 4320 | |
bde73c6b | 4321 | if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then |
8b1011c0 | 4322 | if Present (Rel_Typ) then |
7a5b62b0 AC |
4323 | Set_Associated_Storage_Pool |
4324 | (PtrT, Associated_Storage_Pool (Rel_Typ)); | |
8b1011c0 | 4325 | else |
7a5b62b0 AC |
4326 | Set_Associated_Storage_Pool |
4327 | (PtrT, RTE (RE_Global_Pool_Object)); | |
8b1011c0 | 4328 | end if; |
ca5af305 AC |
4329 | end if; |
4330 | ||
4331 | -- The finalization master must be inserted and analyzed as part of | |
5114f3ff AC |
4332 | -- the current semantic unit. Note that the master is updated when |
4333 | -- analysis changes current units. | |
ca5af305 | 4334 | |
5114f3ff AC |
4335 | if Present (Rel_Typ) then |
4336 | Set_Finalization_Master (PtrT, Finalization_Master (Rel_Typ)); | |
4337 | else | |
4338 | Set_Finalization_Master (PtrT, Current_Anonymous_Master); | |
ca5af305 AC |
4339 | end if; |
4340 | end if; | |
4341 | ||
4342 | -- Set the storage pool and find the appropriate version of Allocate to | |
8417f4b2 AC |
4343 | -- call. Do not overwrite the storage pool if it is already set, which |
4344 | -- can happen for build-in-place function returns (see | |
200b7162 | 4345 | -- Exp_Ch4.Expand_N_Extended_Return_Statement). |
70482933 | 4346 | |
200b7162 BD |
4347 | if No (Storage_Pool (N)) then |
4348 | Pool := Associated_Storage_Pool (Root_Type (PtrT)); | |
70482933 | 4349 | |
200b7162 BD |
4350 | if Present (Pool) then |
4351 | Set_Storage_Pool (N, Pool); | |
fbf5a39b | 4352 | |
200b7162 BD |
4353 | if Is_RTE (Pool, RE_SS_Pool) then |
4354 | if VM_Target = No_VM then | |
4355 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); | |
4356 | end if; | |
fbf5a39b | 4357 | |
a8551b5f AC |
4358 | -- In the case of an allocator for a simple storage pool, locate |
4359 | -- and save a reference to the pool type's Allocate routine. | |
4360 | ||
4361 | elsif Present (Get_Rep_Pragma | |
f6205414 | 4362 | (Etype (Pool), Name_Simple_Storage_Pool_Type)) |
a8551b5f AC |
4363 | then |
4364 | declare | |
a8551b5f | 4365 | Pool_Type : constant Entity_Id := Base_Type (Etype (Pool)); |
260359e3 | 4366 | Alloc_Op : Entity_Id; |
a8551b5f | 4367 | begin |
260359e3 | 4368 | Alloc_Op := Get_Name_Entity_Id (Name_Allocate); |
a8551b5f AC |
4369 | while Present (Alloc_Op) loop |
4370 | if Scope (Alloc_Op) = Scope (Pool_Type) | |
4371 | and then Present (First_Formal (Alloc_Op)) | |
4372 | and then Etype (First_Formal (Alloc_Op)) = Pool_Type | |
4373 | then | |
4374 | Set_Procedure_To_Call (N, Alloc_Op); | |
a8551b5f | 4375 | exit; |
260359e3 AC |
4376 | else |
4377 | Alloc_Op := Homonym (Alloc_Op); | |
a8551b5f | 4378 | end if; |
a8551b5f AC |
4379 | end loop; |
4380 | end; | |
4381 | ||
200b7162 BD |
4382 | elsif Is_Class_Wide_Type (Etype (Pool)) then |
4383 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
4384 | ||
4385 | else | |
4386 | Set_Procedure_To_Call (N, | |
4387 | Find_Prim_Op (Etype (Pool), Name_Allocate)); | |
4388 | end if; | |
70482933 RK |
4389 | end if; |
4390 | end if; | |
4391 | ||
685094bf RD |
4392 | -- Under certain circumstances we can replace an allocator by an access |
4393 | -- to statically allocated storage. The conditions, as noted in AARM | |
4394 | -- 3.10 (10c) are as follows: | |
70482933 RK |
4395 | |
4396 | -- Size and initial value is known at compile time | |
4397 | -- Access type is access-to-constant | |
4398 | ||
fbf5a39b AC |
4399 | -- The allocator is not part of a constraint on a record component, |
4400 | -- because in that case the inserted actions are delayed until the | |
4401 | -- record declaration is fully analyzed, which is too late for the | |
4402 | -- analysis of the rewritten allocator. | |
4403 | ||
70482933 RK |
4404 | if Is_Access_Constant (PtrT) |
4405 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
4406 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
243cae0a AC |
4407 | and then Size_Known_At_Compile_Time |
4408 | (Etype (Expression (Expression (N)))) | |
fbf5a39b | 4409 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
4410 | then |
4411 | -- Here we can do the optimization. For the allocator | |
4412 | ||
4413 | -- new x'(y) | |
4414 | ||
4415 | -- We insert an object declaration | |
4416 | ||
4417 | -- Tnn : aliased x := y; | |
4418 | ||
685094bf RD |
4419 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
4420 | -- marked as requiring static allocation. | |
70482933 | 4421 | |
df3e68b1 | 4422 | Temp := Make_Temporary (Loc, 'T', Expression (Expression (N))); |
70482933 RK |
4423 | Desig := Subtype_Mark (Expression (N)); |
4424 | ||
4425 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 4426 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
4427 | -- unconstrained subtype. |
4428 | ||
0da2c8ac AC |
4429 | if Entity (Desig) = Base_Type (Dtyp) then |
4430 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
4431 | end if; |
4432 | ||
4433 | Insert_Action (N, | |
4434 | Make_Object_Declaration (Loc, | |
4435 | Defining_Identifier => Temp, | |
4436 | Aliased_Present => True, | |
4437 | Constant_Present => Is_Access_Constant (PtrT), | |
4438 | Object_Definition => Desig, | |
4439 | Expression => Expression (Expression (N)))); | |
4440 | ||
4441 | Rewrite (N, | |
4442 | Make_Attribute_Reference (Loc, | |
243cae0a | 4443 | Prefix => New_Occurrence_Of (Temp, Loc), |
70482933 RK |
4444 | Attribute_Name => Name_Unrestricted_Access)); |
4445 | ||
4446 | Analyze_And_Resolve (N, PtrT); | |
4447 | ||
685094bf | 4448 | -- We set the variable as statically allocated, since we don't want |
a90bd866 | 4449 | -- it going on the stack of the current procedure. |
70482933 RK |
4450 | |
4451 | Set_Is_Statically_Allocated (Temp); | |
4452 | return; | |
4453 | end if; | |
4454 | ||
0669bebe GB |
4455 | -- Same if the allocator is an access discriminant for a local object: |
4456 | -- instead of an allocator we create a local value and constrain the | |
308e6f3a | 4457 | -- enclosing object with the corresponding access attribute. |
0669bebe | 4458 | |
26bff3d9 JM |
4459 | if Is_Static_Coextension (N) then |
4460 | Rewrite_Coextension (N); | |
0669bebe GB |
4461 | return; |
4462 | end if; | |
4463 | ||
8aec446b AC |
4464 | -- Check for size too large, we do this because the back end misses |
4465 | -- proper checks here and can generate rubbish allocation calls when | |
4466 | -- we are near the limit. We only do this for the 32-bit address case | |
4467 | -- since that is from a practical point of view where we see a problem. | |
4468 | ||
4469 | if System_Address_Size = 32 | |
4470 | and then not Storage_Checks_Suppressed (PtrT) | |
4471 | and then not Storage_Checks_Suppressed (Dtyp) | |
4472 | and then not Storage_Checks_Suppressed (Etyp) | |
4473 | then | |
4474 | -- The check we want to generate should look like | |
4475 | ||
4476 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
4477 | -- raise Storage_Error; | |
4478 | -- end if; | |
4479 | ||
308e6f3a | 4480 | -- where 3.5 gigabytes is a constant large enough to accommodate any |
507ed3fd AC |
4481 | -- reasonable request for. But we can't do it this way because at |
4482 | -- least at the moment we don't compute this attribute right, and | |
4483 | -- can silently give wrong results when the result gets large. Since | |
4484 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 4485 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 4486 | -- value of the attribute ??? |
8aec446b | 4487 | |
507ed3fd AC |
4488 | if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then |
4489 | Insert_Action (N, | |
4490 | Make_Raise_Storage_Error (Loc, | |
4491 | Condition => | |
4492 | Make_Op_Gt (Loc, | |
4493 | Left_Opnd => Size_In_Storage_Elements (Etyp), | |
4494 | Right_Opnd => | |
243cae0a | 4495 | Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))), |
507ed3fd AC |
4496 | Reason => SE_Object_Too_Large)); |
4497 | end if; | |
8aec446b AC |
4498 | end if; |
4499 | ||
b3b26ace AC |
4500 | -- If no storage pool has been specified and we have the restriction |
4501 | -- No_Standard_Allocators_After_Elaboration is present, then generate | |
4502 | -- a call to Elaboration_Allocators.Check_Standard_Allocator. | |
4503 | ||
4504 | if Nkind (N) = N_Allocator | |
4505 | and then No (Storage_Pool (N)) | |
4506 | and then Restriction_Active (No_Standard_Allocators_After_Elaboration) | |
4507 | then | |
4508 | Insert_Action (N, | |
4509 | Make_Procedure_Call_Statement (Loc, | |
4510 | Name => | |
4511 | New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc))); | |
4512 | end if; | |
4513 | ||
0da2c8ac | 4514 | -- Handle case of qualified expression (other than optimization above) |
cac5a801 AC |
4515 | -- First apply constraint checks, because the bounds or discriminants |
4516 | -- in the aggregate might not match the subtype mark in the allocator. | |
0da2c8ac | 4517 | |
70482933 | 4518 | if Nkind (Expression (N)) = N_Qualified_Expression then |
cac5a801 AC |
4519 | Apply_Constraint_Check |
4520 | (Expression (Expression (N)), Etype (Expression (N))); | |
4521 | ||
fbf5a39b | 4522 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
4523 | return; |
4524 | end if; | |
fbf5a39b | 4525 | |
26bff3d9 JM |
4526 | -- If the allocator is for a type which requires initialization, and |
4527 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
4528 | -- rather than a qualified expression), then we must generate a call to |
4529 | -- the initialization routine using an expressions action node: | |
70482933 | 4530 | |
26bff3d9 | 4531 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 4532 | |
26bff3d9 JM |
4533 | -- Here ptr_T is the pointer type for the allocator, and T is the |
4534 | -- subtype of the allocator. A special case arises if the designated | |
4535 | -- type of the access type is a task or contains tasks. In this case | |
4536 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
4537 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
6be44a9a | 4538 | -- for details). In addition, if the type T is a task type, then the |
26bff3d9 | 4539 | -- first argument to Init must be converted to the task record type. |
70482933 | 4540 | |
26bff3d9 | 4541 | declare |
df3e68b1 HK |
4542 | T : constant Entity_Id := Entity (Expression (N)); |
4543 | Args : List_Id; | |
4544 | Decls : List_Id; | |
4545 | Decl : Node_Id; | |
4546 | Discr : Elmt_Id; | |
4547 | Init : Entity_Id; | |
4548 | Init_Arg1 : Node_Id; | |
4549 | Temp_Decl : Node_Id; | |
4550 | Temp_Type : Entity_Id; | |
70482933 | 4551 | |
26bff3d9 JM |
4552 | begin |
4553 | if No_Initialization (N) then | |
df3e68b1 HK |
4554 | |
4555 | -- Even though this might be a simple allocation, create a custom | |
deb8dacc HK |
4556 | -- Allocate if the context requires it. Since .NET/JVM compilers |
4557 | -- do not support pools, this step is skipped. | |
df3e68b1 | 4558 | |
deb8dacc | 4559 | if VM_Target = No_VM |
d3f70b35 | 4560 | and then Present (Finalization_Master (PtrT)) |
deb8dacc | 4561 | then |
df3e68b1 | 4562 | Build_Allocate_Deallocate_Proc |
ca5af305 | 4563 | (N => N, |
df3e68b1 HK |
4564 | Is_Allocate => True); |
4565 | end if; | |
70482933 | 4566 | |
26bff3d9 | 4567 | -- Case of no initialization procedure present |
70482933 | 4568 | |
26bff3d9 | 4569 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 4570 | |
26bff3d9 | 4571 | -- Case of simple initialization required |
70482933 | 4572 | |
26bff3d9 | 4573 | if Needs_Simple_Initialization (T) then |
b4592168 | 4574 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
4575 | Rewrite (Expression (N), |
4576 | Make_Qualified_Expression (Loc, | |
4577 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 4578 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 4579 | |
26bff3d9 JM |
4580 | Analyze_And_Resolve (Expression (Expression (N)), T); |
4581 | Analyze_And_Resolve (Expression (N), T); | |
4582 | Set_Paren_Count (Expression (Expression (N)), 1); | |
4583 | Expand_N_Allocator (N); | |
70482933 | 4584 | |
26bff3d9 | 4585 | -- No initialization required |
70482933 RK |
4586 | |
4587 | else | |
26bff3d9 JM |
4588 | null; |
4589 | end if; | |
70482933 | 4590 | |
26bff3d9 | 4591 | -- Case of initialization procedure present, must be called |
70482933 | 4592 | |
26bff3d9 | 4593 | else |
b4592168 | 4594 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 4595 | |
b4592168 GD |
4596 | if not Restriction_Active (No_Default_Initialization) then |
4597 | Init := Base_Init_Proc (T); | |
4598 | Nod := N; | |
191fcb3a | 4599 | Temp := Make_Temporary (Loc, 'P'); |
70482933 | 4600 | |
b4592168 | 4601 | -- Construct argument list for the initialization routine call |
70482933 | 4602 | |
df3e68b1 | 4603 | Init_Arg1 := |
b4592168 | 4604 | Make_Explicit_Dereference (Loc, |
df3e68b1 | 4605 | Prefix => |
e4494292 | 4606 | New_Occurrence_Of (Temp, Loc)); |
df3e68b1 HK |
4607 | |
4608 | Set_Assignment_OK (Init_Arg1); | |
b4592168 | 4609 | Temp_Type := PtrT; |
26bff3d9 | 4610 | |
b4592168 GD |
4611 | -- The initialization procedure expects a specific type. if the |
4612 | -- context is access to class wide, indicate that the object | |
4613 | -- being allocated has the right specific type. | |
70482933 | 4614 | |
b4592168 | 4615 | if Is_Class_Wide_Type (Dtyp) then |
df3e68b1 | 4616 | Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1); |
b4592168 | 4617 | end if; |
70482933 | 4618 | |
b4592168 GD |
4619 | -- If designated type is a concurrent type or if it is private |
4620 | -- type whose definition is a concurrent type, the first | |
4621 | -- argument in the Init routine has to be unchecked conversion | |
4622 | -- to the corresponding record type. If the designated type is | |
243cae0a | 4623 | -- a derived type, also convert the argument to its root type. |
20b5d666 | 4624 | |
b4592168 | 4625 | if Is_Concurrent_Type (T) then |
df3e68b1 HK |
4626 | Init_Arg1 := |
4627 | Unchecked_Convert_To ( | |
4628 | Corresponding_Record_Type (T), Init_Arg1); | |
70482933 | 4629 | |
b4592168 GD |
4630 | elsif Is_Private_Type (T) |
4631 | and then Present (Full_View (T)) | |
4632 | and then Is_Concurrent_Type (Full_View (T)) | |
4633 | then | |
df3e68b1 | 4634 | Init_Arg1 := |
b4592168 | 4635 | Unchecked_Convert_To |
df3e68b1 | 4636 | (Corresponding_Record_Type (Full_View (T)), Init_Arg1); |
70482933 | 4637 | |
b4592168 GD |
4638 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
4639 | declare | |
4640 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
df3e68b1 | 4641 | |
b4592168 | 4642 | begin |
df3e68b1 HK |
4643 | Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1); |
4644 | Set_Etype (Init_Arg1, Ftyp); | |
b4592168 GD |
4645 | end; |
4646 | end if; | |
70482933 | 4647 | |
df3e68b1 | 4648 | Args := New_List (Init_Arg1); |
70482933 | 4649 | |
b4592168 GD |
4650 | -- For the task case, pass the Master_Id of the access type as |
4651 | -- the value of the _Master parameter, and _Chain as the value | |
4652 | -- of the _Chain parameter (_Chain will be defined as part of | |
4653 | -- the generated code for the allocator). | |
70482933 | 4654 | |
b4592168 GD |
4655 | -- In Ada 2005, the context may be a function that returns an |
4656 | -- anonymous access type. In that case the Master_Id has been | |
4657 | -- created when expanding the function declaration. | |
70482933 | 4658 | |
b4592168 GD |
4659 | if Has_Task (T) then |
4660 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 4661 | |
b4592168 GD |
4662 | -- The designated type was an incomplete type, and the |
4663 | -- access type did not get expanded. Salvage it now. | |
70482933 | 4664 | |
b941ae65 | 4665 | if not Restriction_Active (No_Task_Hierarchy) then |
3d67b239 AC |
4666 | if Present (Parent (Base_Type (PtrT))) then |
4667 | Expand_N_Full_Type_Declaration | |
4668 | (Parent (Base_Type (PtrT))); | |
4669 | ||
0d5fbf52 AC |
4670 | -- The only other possibility is an itype. For this |
4671 | -- case, the master must exist in the context. This is | |
4672 | -- the case when the allocator initializes an access | |
4673 | -- component in an init-proc. | |
3d67b239 | 4674 | |
0d5fbf52 | 4675 | else |
3d67b239 AC |
4676 | pragma Assert (Is_Itype (PtrT)); |
4677 | Build_Master_Renaming (PtrT, N); | |
4678 | end if; | |
b941ae65 | 4679 | end if; |
b4592168 | 4680 | end if; |
70482933 | 4681 | |
b4592168 GD |
4682 | -- If the context of the allocator is a declaration or an |
4683 | -- assignment, we can generate a meaningful image for it, | |
4684 | -- even though subsequent assignments might remove the | |
4685 | -- connection between task and entity. We build this image | |
4686 | -- when the left-hand side is a simple variable, a simple | |
4687 | -- indexed assignment or a simple selected component. | |
4688 | ||
4689 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
4690 | declare | |
4691 | Nam : constant Node_Id := Name (Parent (N)); | |
4692 | ||
4693 | begin | |
4694 | if Is_Entity_Name (Nam) then | |
4695 | Decls := | |
4696 | Build_Task_Image_Decls | |
4697 | (Loc, | |
4698 | New_Occurrence_Of | |
4699 | (Entity (Nam), Sloc (Nam)), T); | |
4700 | ||
243cae0a AC |
4701 | elsif Nkind_In (Nam, N_Indexed_Component, |
4702 | N_Selected_Component) | |
b4592168 GD |
4703 | and then Is_Entity_Name (Prefix (Nam)) |
4704 | then | |
4705 | Decls := | |
4706 | Build_Task_Image_Decls | |
4707 | (Loc, Nam, Etype (Prefix (Nam))); | |
4708 | else | |
4709 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
4710 | end if; | |
4711 | end; | |
70482933 | 4712 | |
b4592168 GD |
4713 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
4714 | Decls := | |
4715 | Build_Task_Image_Decls | |
4716 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 4717 | |
b4592168 GD |
4718 | else |
4719 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
4720 | end if; | |
26bff3d9 | 4721 | |
87dc09cb | 4722 | if Restriction_Active (No_Task_Hierarchy) then |
3c1ecd7e AC |
4723 | Append_To (Args, |
4724 | New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc)); | |
87dc09cb AC |
4725 | else |
4726 | Append_To (Args, | |
e4494292 | 4727 | New_Occurrence_Of |
87dc09cb AC |
4728 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); |
4729 | end if; | |
4730 | ||
b4592168 | 4731 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); |
26bff3d9 | 4732 | |
b4592168 GD |
4733 | Decl := Last (Decls); |
4734 | Append_To (Args, | |
4735 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 4736 | |
87dc09cb | 4737 | -- Has_Task is false, Decls not used |
26bff3d9 | 4738 | |
b4592168 GD |
4739 | else |
4740 | Decls := No_List; | |
26bff3d9 JM |
4741 | end if; |
4742 | ||
b4592168 GD |
4743 | -- Add discriminants if discriminated type |
4744 | ||
4745 | declare | |
4746 | Dis : Boolean := False; | |
4747 | Typ : Entity_Id; | |
4748 | ||
4749 | begin | |
4750 | if Has_Discriminants (T) then | |
4751 | Dis := True; | |
4752 | Typ := T; | |
4753 | ||
4754 | elsif Is_Private_Type (T) | |
4755 | and then Present (Full_View (T)) | |
4756 | and then Has_Discriminants (Full_View (T)) | |
20b5d666 | 4757 | then |
b4592168 GD |
4758 | Dis := True; |
4759 | Typ := Full_View (T); | |
20b5d666 | 4760 | end if; |
70482933 | 4761 | |
b4592168 | 4762 | if Dis then |
26bff3d9 | 4763 | |
b4592168 | 4764 | -- If the allocated object will be constrained by the |
685094bf RD |
4765 | -- default values for discriminants, then build a subtype |
4766 | -- with those defaults, and change the allocated subtype | |
4767 | -- to that. Note that this happens in fewer cases in Ada | |
4768 | -- 2005 (AI-363). | |
26bff3d9 | 4769 | |
b4592168 GD |
4770 | if not Is_Constrained (Typ) |
4771 | and then Present (Discriminant_Default_Value | |
df3e68b1 | 4772 | (First_Discriminant (Typ))) |
0791fbe9 | 4773 | and then (Ada_Version < Ada_2005 |
cc96a1b8 | 4774 | or else not |
0fbcb11c ES |
4775 | Object_Type_Has_Constrained_Partial_View |
4776 | (Typ, Current_Scope)) | |
20b5d666 | 4777 | then |
b4592168 | 4778 | Typ := Build_Default_Subtype (Typ, N); |
e4494292 | 4779 | Set_Expression (N, New_Occurrence_Of (Typ, Loc)); |
20b5d666 JM |
4780 | end if; |
4781 | ||
b4592168 GD |
4782 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
4783 | while Present (Discr) loop | |
4784 | Nod := Node (Discr); | |
4785 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 4786 | |
b4592168 GD |
4787 | -- AI-416: when the discriminant constraint is an |
4788 | -- anonymous access type make sure an accessibility | |
4789 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 4790 | |
0791fbe9 | 4791 | if Ada_Version >= Ada_2005 |
b4592168 GD |
4792 | and then |
4793 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
4794 | then | |
e84e11ba GD |
4795 | Apply_Accessibility_Check |
4796 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 4797 | end if; |
20b5d666 | 4798 | |
b4592168 GD |
4799 | Next_Elmt (Discr); |
4800 | end loop; | |
4801 | end if; | |
4802 | end; | |
70482933 | 4803 | |
4b985e20 | 4804 | -- We set the allocator as analyzed so that when we analyze |
9b16cb57 RD |
4805 | -- the if expression node, we do not get an unwanted recursive |
4806 | -- expansion of the allocator expression. | |
70482933 | 4807 | |
b4592168 GD |
4808 | Set_Analyzed (N, True); |
4809 | Nod := Relocate_Node (N); | |
70482933 | 4810 | |
b4592168 | 4811 | -- Here is the transformation: |
ca5af305 AC |
4812 | -- input: new Ctrl_Typ |
4813 | -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ; | |
4814 | -- Ctrl_TypIP (Temp.all, ...); | |
4815 | -- [Deep_]Initialize (Temp.all); | |
70482933 | 4816 | |
ca5af305 AC |
4817 | -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and |
4818 | -- is the subtype of the allocator. | |
70482933 | 4819 | |
b4592168 GD |
4820 | Temp_Decl := |
4821 | Make_Object_Declaration (Loc, | |
4822 | Defining_Identifier => Temp, | |
4823 | Constant_Present => True, | |
e4494292 | 4824 | Object_Definition => New_Occurrence_Of (Temp_Type, Loc), |
b4592168 | 4825 | Expression => Nod); |
70482933 | 4826 | |
b4592168 GD |
4827 | Set_Assignment_OK (Temp_Decl); |
4828 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 4829 | |
ca5af305 | 4830 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 4831 | |
b4592168 GD |
4832 | -- If the designated type is a task type or contains tasks, |
4833 | -- create block to activate created tasks, and insert | |
4834 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 4835 | |
b4592168 GD |
4836 | if Has_Task (T) then |
4837 | declare | |
4838 | L : constant List_Id := New_List; | |
4839 | Blk : Node_Id; | |
4840 | begin | |
4841 | Build_Task_Allocate_Block (L, Nod, Args); | |
4842 | Blk := Last (L); | |
4843 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
4844 | Insert_Actions (N, L); | |
4845 | end; | |
70482933 | 4846 | |
b4592168 GD |
4847 | else |
4848 | Insert_Action (N, | |
4849 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 4850 | Name => New_Occurrence_Of (Init, Loc), |
b4592168 GD |
4851 | Parameter_Associations => Args)); |
4852 | end if; | |
70482933 | 4853 | |
048e5cef | 4854 | if Needs_Finalization (T) then |
70482933 | 4855 | |
df3e68b1 HK |
4856 | -- Generate: |
4857 | -- [Deep_]Initialize (Init_Arg1); | |
70482933 | 4858 | |
df3e68b1 | 4859 | Insert_Action (N, |
243cae0a AC |
4860 | Make_Init_Call |
4861 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
4862 | Typ => T)); | |
b4592168 | 4863 | |
b254da66 | 4864 | if Present (Finalization_Master (PtrT)) then |
deb8dacc | 4865 | |
b254da66 AC |
4866 | -- Special processing for .NET/JVM, the allocated object |
4867 | -- is attached to the finalization master. Generate: | |
deb8dacc | 4868 | |
b254da66 | 4869 | -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1)); |
deb8dacc | 4870 | |
b254da66 AC |
4871 | -- Types derived from [Limited_]Controlled are the only |
4872 | -- ones considered since they have fields Prev and Next. | |
4873 | ||
e0c32166 AC |
4874 | if VM_Target /= No_VM then |
4875 | if Is_Controlled (T) then | |
4876 | Insert_Action (N, | |
4877 | Make_Attach_Call | |
4878 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
4879 | Ptr_Typ => PtrT)); | |
4880 | end if; | |
b254da66 AC |
4881 | |
4882 | -- Default case, generate: | |
4883 | ||
4884 | -- Set_Finalize_Address | |
4885 | -- (<PtrT>FM, <T>FD'Unrestricted_Access); | |
4886 | ||
5114f3ff AC |
4887 | -- Do not generate this call in CodePeer mode, as TSS |
4888 | -- primitive Finalize_Address is not created in this | |
4889 | -- mode. | |
b254da66 | 4890 | |
5114f3ff | 4891 | elsif not CodePeer_Mode then |
b254da66 AC |
4892 | Insert_Action (N, |
4893 | Make_Set_Finalize_Address_Call | |
4894 | (Loc => Loc, | |
4895 | Typ => T, | |
4896 | Ptr_Typ => PtrT)); | |
4897 | end if; | |
b4592168 | 4898 | end if; |
70482933 RK |
4899 | end if; |
4900 | ||
e4494292 | 4901 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
b4592168 GD |
4902 | Analyze_And_Resolve (N, PtrT); |
4903 | end if; | |
26bff3d9 JM |
4904 | end if; |
4905 | end; | |
f82944b7 | 4906 | |
26bff3d9 JM |
4907 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
4908 | -- object that has been rewritten as a reference, we displace "this" | |
4909 | -- to reference properly its secondary dispatch table. | |
4910 | ||
533369aa | 4911 | if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then |
26bff3d9 | 4912 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
4913 | end if; |
4914 | ||
fbf5a39b AC |
4915 | exception |
4916 | when RE_Not_Available => | |
4917 | return; | |
70482933 RK |
4918 | end Expand_N_Allocator; |
4919 | ||
4920 | ----------------------- | |
4921 | -- Expand_N_And_Then -- | |
4922 | ----------------------- | |
4923 | ||
5875f8d6 AC |
4924 | procedure Expand_N_And_Then (N : Node_Id) |
4925 | renames Expand_Short_Circuit_Operator; | |
70482933 | 4926 | |
19d846a0 RD |
4927 | ------------------------------ |
4928 | -- Expand_N_Case_Expression -- | |
4929 | ------------------------------ | |
4930 | ||
4931 | procedure Expand_N_Case_Expression (N : Node_Id) is | |
4932 | Loc : constant Source_Ptr := Sloc (N); | |
4933 | Typ : constant Entity_Id := Etype (N); | |
4934 | Cstmt : Node_Id; | |
27a8f150 | 4935 | Decl : Node_Id; |
19d846a0 RD |
4936 | Tnn : Entity_Id; |
4937 | Pnn : Entity_Id; | |
4938 | Actions : List_Id; | |
4939 | Ttyp : Entity_Id; | |
4940 | Alt : Node_Id; | |
4941 | Fexp : Node_Id; | |
4942 | ||
4943 | begin | |
b6b5cca8 AC |
4944 | -- Check for MINIMIZED/ELIMINATED overflow mode |
4945 | ||
4946 | if Minimized_Eliminated_Overflow_Check (N) then | |
4b1c4f20 RD |
4947 | Apply_Arithmetic_Overflow_Check (N); |
4948 | return; | |
4949 | end if; | |
4950 | ||
ff1f1705 AC |
4951 | -- If the case expression is a predicate specification, do not |
4952 | -- expand, because it will be converted to the proper predicate | |
4953 | -- form when building the predicate function. | |
4954 | ||
4955 | if Ekind_In (Current_Scope, E_Function, E_Procedure) | |
4956 | and then Is_Predicate_Function (Current_Scope) | |
4957 | then | |
4958 | return; | |
4959 | end if; | |
4960 | ||
19d846a0 RD |
4961 | -- We expand |
4962 | ||
4963 | -- case X is when A => AX, when B => BX ... | |
4964 | ||
4965 | -- to | |
4966 | ||
4967 | -- do | |
4968 | -- Tnn : typ; | |
4969 | -- case X is | |
4970 | -- when A => | |
4971 | -- Tnn := AX; | |
4972 | -- when B => | |
4973 | -- Tnn := BX; | |
4974 | -- ... | |
4975 | -- end case; | |
4976 | -- in Tnn end; | |
4977 | ||
4978 | -- However, this expansion is wrong for limited types, and also | |
4979 | -- wrong for unconstrained types (since the bounds may not be the | |
4980 | -- same in all branches). Furthermore it involves an extra copy | |
4981 | -- for large objects. So we take care of this by using the following | |
2492305b | 4982 | -- modified expansion for non-elementary types: |
19d846a0 RD |
4983 | |
4984 | -- do | |
4985 | -- type Pnn is access all typ; | |
4986 | -- Tnn : Pnn; | |
4987 | -- case X is | |
4988 | -- when A => | |
4989 | -- T := AX'Unrestricted_Access; | |
4990 | -- when B => | |
4991 | -- T := BX'Unrestricted_Access; | |
4992 | -- ... | |
4993 | -- end case; | |
4994 | -- in Tnn.all end; | |
4995 | ||
4996 | Cstmt := | |
4997 | Make_Case_Statement (Loc, | |
4998 | Expression => Expression (N), | |
4999 | Alternatives => New_List); | |
5000 | ||
414c6563 AC |
5001 | -- Preserve the original context for which the case statement is being |
5002 | -- generated. This is needed by the finalization machinery to prevent | |
5003 | -- the premature finalization of controlled objects found within the | |
5004 | -- case statement. | |
5005 | ||
5006 | Set_From_Conditional_Expression (Cstmt); | |
5007 | ||
19d846a0 RD |
5008 | Actions := New_List; |
5009 | ||
5010 | -- Scalar case | |
5011 | ||
2492305b | 5012 | if Is_Elementary_Type (Typ) then |
19d846a0 RD |
5013 | Ttyp := Typ; |
5014 | ||
5015 | else | |
5016 | Pnn := Make_Temporary (Loc, 'P'); | |
5017 | Append_To (Actions, | |
5018 | Make_Full_Type_Declaration (Loc, | |
5019 | Defining_Identifier => Pnn, | |
11d59a86 | 5020 | Type_Definition => |
19d846a0 | 5021 | Make_Access_To_Object_Definition (Loc, |
11d59a86 | 5022 | All_Present => True, |
e4494292 | 5023 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); |
19d846a0 RD |
5024 | Ttyp := Pnn; |
5025 | end if; | |
5026 | ||
5027 | Tnn := Make_Temporary (Loc, 'T'); | |
27a8f150 AC |
5028 | |
5029 | -- Create declaration for target of expression, and indicate that it | |
5030 | -- does not require initialization. | |
5031 | ||
11d59a86 AC |
5032 | Decl := |
5033 | Make_Object_Declaration (Loc, | |
19d846a0 | 5034 | Defining_Identifier => Tnn, |
27a8f150 AC |
5035 | Object_Definition => New_Occurrence_Of (Ttyp, Loc)); |
5036 | Set_No_Initialization (Decl); | |
5037 | Append_To (Actions, Decl); | |
19d846a0 RD |
5038 | |
5039 | -- Now process the alternatives | |
5040 | ||
5041 | Alt := First (Alternatives (N)); | |
5042 | while Present (Alt) loop | |
5043 | declare | |
eaed0c37 AC |
5044 | Aexp : Node_Id := Expression (Alt); |
5045 | Aloc : constant Source_Ptr := Sloc (Aexp); | |
5046 | Stats : List_Id; | |
19d846a0 RD |
5047 | |
5048 | begin | |
eaed0c37 AC |
5049 | -- As described above, take Unrestricted_Access for case of non- |
5050 | -- scalar types, to avoid big copies, and special cases. | |
05dbd302 | 5051 | |
2492305b | 5052 | if not Is_Elementary_Type (Typ) then |
19d846a0 RD |
5053 | Aexp := |
5054 | Make_Attribute_Reference (Aloc, | |
5055 | Prefix => Relocate_Node (Aexp), | |
5056 | Attribute_Name => Name_Unrestricted_Access); | |
5057 | end if; | |
5058 | ||
eaed0c37 AC |
5059 | Stats := New_List ( |
5060 | Make_Assignment_Statement (Aloc, | |
5061 | Name => New_Occurrence_Of (Tnn, Loc), | |
5062 | Expression => Aexp)); | |
5063 | ||
5064 | -- Propagate declarations inserted in the node by Insert_Actions | |
5065 | -- (for example, temporaries generated to remove side effects). | |
5066 | -- These actions must remain attached to the alternative, given | |
5067 | -- that they are generated by the corresponding expression. | |
5068 | ||
5069 | if Present (Sinfo.Actions (Alt)) then | |
5070 | Prepend_List (Sinfo.Actions (Alt), Stats); | |
5071 | end if; | |
5072 | ||
19d846a0 RD |
5073 | Append_To |
5074 | (Alternatives (Cstmt), | |
5075 | Make_Case_Statement_Alternative (Sloc (Alt), | |
5076 | Discrete_Choices => Discrete_Choices (Alt), | |
eaed0c37 | 5077 | Statements => Stats)); |
19d846a0 RD |
5078 | end; |
5079 | ||
5080 | Next (Alt); | |
5081 | end loop; | |
5082 | ||
5083 | Append_To (Actions, Cstmt); | |
5084 | ||
5085 | -- Construct and return final expression with actions | |
5086 | ||
2492305b | 5087 | if Is_Elementary_Type (Typ) then |
19d846a0 RD |
5088 | Fexp := New_Occurrence_Of (Tnn, Loc); |
5089 | else | |
5090 | Fexp := | |
5091 | Make_Explicit_Dereference (Loc, | |
5092 | Prefix => New_Occurrence_Of (Tnn, Loc)); | |
5093 | end if; | |
5094 | ||
5095 | Rewrite (N, | |
5096 | Make_Expression_With_Actions (Loc, | |
5097 | Expression => Fexp, | |
5098 | Actions => Actions)); | |
5099 | ||
5100 | Analyze_And_Resolve (N, Typ); | |
5101 | end Expand_N_Case_Expression; | |
5102 | ||
9b16cb57 RD |
5103 | ----------------------------------- |
5104 | -- Expand_N_Explicit_Dereference -- | |
5105 | ----------------------------------- | |
5106 | ||
5107 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
5108 | begin | |
5109 | -- Insert explicit dereference call for the checked storage pool case | |
5110 | ||
5111 | Insert_Dereference_Action (Prefix (N)); | |
5112 | ||
5113 | -- If the type is an Atomic type for which Atomic_Sync is enabled, then | |
5114 | -- we set the atomic sync flag. | |
5115 | ||
5116 | if Is_Atomic (Etype (N)) | |
5117 | and then not Atomic_Synchronization_Disabled (Etype (N)) | |
5118 | then | |
5119 | Activate_Atomic_Synchronization (N); | |
5120 | end if; | |
5121 | end Expand_N_Explicit_Dereference; | |
5122 | ||
5123 | -------------------------------------- | |
5124 | -- Expand_N_Expression_With_Actions -- | |
5125 | -------------------------------------- | |
5126 | ||
5127 | procedure Expand_N_Expression_With_Actions (N : Node_Id) is | |
3a845e07 | 5128 | |
4c7e0990 | 5129 | function Process_Action (Act : Node_Id) return Traverse_Result; |
b2c28399 AC |
5130 | -- Inspect and process a single action of an expression_with_actions for |
5131 | -- transient controlled objects. If such objects are found, the routine | |
5132 | -- generates code to clean them up when the context of the expression is | |
5133 | -- evaluated or elaborated. | |
9b16cb57 | 5134 | |
4c7e0990 AC |
5135 | -------------------- |
5136 | -- Process_Action -- | |
5137 | -------------------- | |
5138 | ||
5139 | function Process_Action (Act : Node_Id) return Traverse_Result is | |
4c7e0990 AC |
5140 | begin |
5141 | if Nkind (Act) = N_Object_Declaration | |
5142 | and then Is_Finalizable_Transient (Act, N) | |
5143 | then | |
b2c28399 AC |
5144 | Process_Transient_Object (Act, N); |
5145 | return Abandon; | |
9b16cb57 | 5146 | |
4c7e0990 AC |
5147 | -- Avoid processing temporary function results multiple times when |
5148 | -- dealing with nested expression_with_actions. | |
9b16cb57 | 5149 | |
4c7e0990 AC |
5150 | elsif Nkind (Act) = N_Expression_With_Actions then |
5151 | return Abandon; | |
5152 | ||
b2c28399 AC |
5153 | -- Do not process temporary function results in loops. This is done |
5154 | -- by Expand_N_Loop_Statement and Build_Finalizer. | |
4c7e0990 AC |
5155 | |
5156 | elsif Nkind (Act) = N_Loop_Statement then | |
5157 | return Abandon; | |
9b16cb57 RD |
5158 | end if; |
5159 | ||
4c7e0990 AC |
5160 | return OK; |
5161 | end Process_Action; | |
9b16cb57 | 5162 | |
4c7e0990 | 5163 | procedure Process_Single_Action is new Traverse_Proc (Process_Action); |
9b16cb57 RD |
5164 | |
5165 | -- Local variables | |
5166 | ||
4c7e0990 | 5167 | Act : Node_Id; |
9b16cb57 RD |
5168 | |
5169 | -- Start of processing for Expand_N_Expression_With_Actions | |
5170 | ||
5171 | begin | |
e0f63680 AC |
5172 | -- Process the actions as described above |
5173 | ||
4c7e0990 | 5174 | Act := First (Actions (N)); |
e0f63680 AC |
5175 | while Present (Act) loop |
5176 | Process_Single_Action (Act); | |
5177 | Next (Act); | |
5178 | end loop; | |
5179 | ||
ebdaa81b | 5180 | -- Deal with case where there are no actions. In this case we simply |
5a521b8a | 5181 | -- rewrite the node with its expression since we don't need the actions |
ebdaa81b AC |
5182 | -- and the specification of this node does not allow a null action list. |
5183 | ||
5a521b8a AC |
5184 | -- Note: we use Rewrite instead of Replace, because Codepeer is using |
5185 | -- the expanded tree and relying on being able to retrieve the original | |
5186 | -- tree in cases like this. This raises a whole lot of issues of whether | |
5187 | -- we have problems elsewhere, which will be addressed in the future??? | |
5188 | ||
e0f63680 | 5189 | if Is_Empty_List (Actions (N)) then |
5a521b8a | 5190 | Rewrite (N, Relocate_Node (Expression (N))); |
ebdaa81b | 5191 | end if; |
9b16cb57 RD |
5192 | end Expand_N_Expression_With_Actions; |
5193 | ||
5194 | ---------------------------- | |
5195 | -- Expand_N_If_Expression -- | |
5196 | ---------------------------- | |
70482933 | 5197 | |
4b985e20 | 5198 | -- Deal with limited types and condition actions |
70482933 | 5199 | |
9b16cb57 | 5200 | procedure Expand_N_If_Expression (N : Node_Id) is |
b2c28399 AC |
5201 | procedure Process_Actions (Actions : List_Id); |
5202 | -- Inspect and process a single action list of an if expression for | |
5203 | -- transient controlled objects. If such objects are found, the routine | |
5204 | -- generates code to clean them up when the context of the expression is | |
5205 | -- evaluated or elaborated. | |
3cebd1c0 | 5206 | |
b2c28399 AC |
5207 | --------------------- |
5208 | -- Process_Actions -- | |
5209 | --------------------- | |
3cebd1c0 | 5210 | |
b2c28399 AC |
5211 | procedure Process_Actions (Actions : List_Id) is |
5212 | Act : Node_Id; | |
3cebd1c0 AC |
5213 | |
5214 | begin | |
b2c28399 AC |
5215 | Act := First (Actions); |
5216 | while Present (Act) loop | |
5217 | if Nkind (Act) = N_Object_Declaration | |
5218 | and then Is_Finalizable_Transient (Act, N) | |
5219 | then | |
5220 | Process_Transient_Object (Act, N); | |
5221 | end if; | |
3cebd1c0 | 5222 | |
b2c28399 AC |
5223 | Next (Act); |
5224 | end loop; | |
5225 | end Process_Actions; | |
3cebd1c0 AC |
5226 | |
5227 | -- Local variables | |
5228 | ||
70482933 RK |
5229 | Loc : constant Source_Ptr := Sloc (N); |
5230 | Cond : constant Node_Id := First (Expressions (N)); | |
5231 | Thenx : constant Node_Id := Next (Cond); | |
5232 | Elsex : constant Node_Id := Next (Thenx); | |
5233 | Typ : constant Entity_Id := Etype (N); | |
c471e2da | 5234 | |
3cebd1c0 | 5235 | Actions : List_Id; |
602a7ec0 AC |
5236 | Cnn : Entity_Id; |
5237 | Decl : Node_Id; | |
3cebd1c0 | 5238 | Expr : Node_Id; |
602a7ec0 AC |
5239 | New_If : Node_Id; |
5240 | New_N : Node_Id; | |
b2c28399 | 5241 | Ptr_Typ : Entity_Id; |
70482933 | 5242 | |
a53c5613 AC |
5243 | -- Start of processing for Expand_N_If_Expression |
5244 | ||
70482933 | 5245 | begin |
b6b5cca8 AC |
5246 | -- Check for MINIMIZED/ELIMINATED overflow mode |
5247 | ||
5248 | if Minimized_Eliminated_Overflow_Check (N) then | |
5249 | Apply_Arithmetic_Overflow_Check (N); | |
5250 | return; | |
5251 | end if; | |
5252 | ||
602a7ec0 | 5253 | -- Fold at compile time if condition known. We have already folded |
9b16cb57 RD |
5254 | -- static if expressions, but it is possible to fold any case in which |
5255 | -- the condition is known at compile time, even though the result is | |
5256 | -- non-static. | |
602a7ec0 AC |
5257 | |
5258 | -- Note that we don't do the fold of such cases in Sem_Elab because | |
5259 | -- it can cause infinite loops with the expander adding a conditional | |
5260 | -- expression, and Sem_Elab circuitry removing it repeatedly. | |
5261 | ||
5262 | if Compile_Time_Known_Value (Cond) then | |
5263 | if Is_True (Expr_Value (Cond)) then | |
cc6f5d75 | 5264 | Expr := Thenx; |
602a7ec0 AC |
5265 | Actions := Then_Actions (N); |
5266 | else | |
cc6f5d75 | 5267 | Expr := Elsex; |
602a7ec0 AC |
5268 | Actions := Else_Actions (N); |
5269 | end if; | |
5270 | ||
5271 | Remove (Expr); | |
ae77c68b AC |
5272 | |
5273 | if Present (Actions) then | |
ae77c68b AC |
5274 | Rewrite (N, |
5275 | Make_Expression_With_Actions (Loc, | |
5276 | Expression => Relocate_Node (Expr), | |
5277 | Actions => Actions)); | |
5278 | Analyze_And_Resolve (N, Typ); | |
ae77c68b AC |
5279 | else |
5280 | Rewrite (N, Relocate_Node (Expr)); | |
5281 | end if; | |
602a7ec0 AC |
5282 | |
5283 | -- Note that the result is never static (legitimate cases of static | |
9b16cb57 | 5284 | -- if expressions were folded in Sem_Eval). |
602a7ec0 AC |
5285 | |
5286 | Set_Is_Static_Expression (N, False); | |
5287 | return; | |
5288 | end if; | |
5289 | ||
113a9fb6 AC |
5290 | -- If the type is limited, and the back end does not handle limited |
5291 | -- types, then we expand as follows to avoid the possibility of | |
5292 | -- improper copying. | |
ac7120ce | 5293 | |
c471e2da AC |
5294 | -- type Ptr is access all Typ; |
5295 | -- Cnn : Ptr; | |
ac7120ce RD |
5296 | -- if cond then |
5297 | -- <<then actions>> | |
5298 | -- Cnn := then-expr'Unrestricted_Access; | |
5299 | -- else | |
5300 | -- <<else actions>> | |
5301 | -- Cnn := else-expr'Unrestricted_Access; | |
5302 | -- end if; | |
5303 | ||
9b16cb57 | 5304 | -- and replace the if expression by a reference to Cnn.all. |
ac7120ce | 5305 | |
305caf42 AC |
5306 | -- This special case can be skipped if the back end handles limited |
5307 | -- types properly and ensures that no incorrect copies are made. | |
5308 | ||
5309 | if Is_By_Reference_Type (Typ) | |
5310 | and then not Back_End_Handles_Limited_Types | |
5311 | then | |
b2c28399 AC |
5312 | -- When the "then" or "else" expressions involve controlled function |
5313 | -- calls, generated temporaries are chained on the corresponding list | |
5314 | -- of actions. These temporaries need to be finalized after the if | |
5315 | -- expression is evaluated. | |
3cebd1c0 | 5316 | |
b2c28399 AC |
5317 | Process_Actions (Then_Actions (N)); |
5318 | Process_Actions (Else_Actions (N)); | |
3cebd1c0 | 5319 | |
b2c28399 AC |
5320 | -- Generate: |
5321 | -- type Ann is access all Typ; | |
3cebd1c0 | 5322 | |
b2c28399 | 5323 | Ptr_Typ := Make_Temporary (Loc, 'A'); |
3cebd1c0 | 5324 | |
b2c28399 AC |
5325 | Insert_Action (N, |
5326 | Make_Full_Type_Declaration (Loc, | |
5327 | Defining_Identifier => Ptr_Typ, | |
5328 | Type_Definition => | |
5329 | Make_Access_To_Object_Definition (Loc, | |
5330 | All_Present => True, | |
e4494292 | 5331 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); |
3cebd1c0 | 5332 | |
b2c28399 AC |
5333 | -- Generate: |
5334 | -- Cnn : Ann; | |
3cebd1c0 | 5335 | |
b2c28399 | 5336 | Cnn := Make_Temporary (Loc, 'C', N); |
3cebd1c0 | 5337 | |
b2c28399 AC |
5338 | Decl := |
5339 | Make_Object_Declaration (Loc, | |
5340 | Defining_Identifier => Cnn, | |
5341 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc)); | |
3cebd1c0 | 5342 | |
b2c28399 AC |
5343 | -- Generate: |
5344 | -- if Cond then | |
5345 | -- Cnn := <Thenx>'Unrestricted_Access; | |
5346 | -- else | |
5347 | -- Cnn := <Elsex>'Unrestricted_Access; | |
5348 | -- end if; | |
3cebd1c0 | 5349 | |
b2c28399 AC |
5350 | New_If := |
5351 | Make_Implicit_If_Statement (N, | |
5352 | Condition => Relocate_Node (Cond), | |
5353 | Then_Statements => New_List ( | |
5354 | Make_Assignment_Statement (Sloc (Thenx), | |
e4494292 | 5355 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), |
b2c28399 AC |
5356 | Expression => |
5357 | Make_Attribute_Reference (Loc, | |
5358 | Prefix => Relocate_Node (Thenx), | |
5359 | Attribute_Name => Name_Unrestricted_Access))), | |
3cebd1c0 | 5360 | |
b2c28399 AC |
5361 | Else_Statements => New_List ( |
5362 | Make_Assignment_Statement (Sloc (Elsex), | |
e4494292 | 5363 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), |
b2c28399 AC |
5364 | Expression => |
5365 | Make_Attribute_Reference (Loc, | |
5366 | Prefix => Relocate_Node (Elsex), | |
5367 | Attribute_Name => Name_Unrestricted_Access)))); | |
3cebd1c0 | 5368 | |
414c6563 AC |
5369 | -- Preserve the original context for which the if statement is being |
5370 | -- generated. This is needed by the finalization machinery to prevent | |
5371 | -- the premature finalization of controlled objects found within the | |
5372 | -- if statement. | |
5373 | ||
5374 | Set_From_Conditional_Expression (New_If); | |
5375 | ||
5376 | New_N := | |
5377 | Make_Explicit_Dereference (Loc, | |
5378 | Prefix => New_Occurrence_Of (Cnn, Loc)); | |
fb1949a0 | 5379 | |
113a9fb6 AC |
5380 | -- If the result is an unconstrained array and the if expression is in a |
5381 | -- context other than the initializing expression of the declaration of | |
5382 | -- an object, then we pull out the if expression as follows: | |
5383 | ||
5384 | -- Cnn : constant typ := if-expression | |
5385 | ||
5386 | -- and then replace the if expression with an occurrence of Cnn. This | |
5387 | -- avoids the need in the back end to create on-the-fly variable length | |
5388 | -- temporaries (which it cannot do!) | |
5389 | ||
5390 | -- Note that the test for being in an object declaration avoids doing an | |
5391 | -- unnecessary expansion, and also avoids infinite recursion. | |
5392 | ||
5393 | elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ) | |
5394 | and then (Nkind (Parent (N)) /= N_Object_Declaration | |
5395 | or else Expression (Parent (N)) /= N) | |
5396 | then | |
5397 | declare | |
5398 | Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N); | |
5399 | begin | |
5400 | Insert_Action (N, | |
5401 | Make_Object_Declaration (Loc, | |
5402 | Defining_Identifier => Cnn, | |
5403 | Constant_Present => True, | |
5404 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
5405 | Expression => Relocate_Node (N), | |
5406 | Has_Init_Expression => True)); | |
5407 | ||
5408 | Rewrite (N, New_Occurrence_Of (Cnn, Loc)); | |
5409 | return; | |
5410 | end; | |
5411 | ||
c471e2da AC |
5412 | -- For other types, we only need to expand if there are other actions |
5413 | -- associated with either branch. | |
5414 | ||
5415 | elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
c471e2da | 5416 | |
0812b84e | 5417 | -- We now wrap the actions into the appropriate expression |
fb1949a0 | 5418 | |
0812b84e AC |
5419 | if Present (Then_Actions (N)) then |
5420 | Rewrite (Thenx, | |
b2c28399 AC |
5421 | Make_Expression_With_Actions (Sloc (Thenx), |
5422 | Actions => Then_Actions (N), | |
5423 | Expression => Relocate_Node (Thenx))); | |
5424 | ||
0812b84e AC |
5425 | Set_Then_Actions (N, No_List); |
5426 | Analyze_And_Resolve (Thenx, Typ); | |
5427 | end if; | |
305caf42 | 5428 | |
0812b84e AC |
5429 | if Present (Else_Actions (N)) then |
5430 | Rewrite (Elsex, | |
b2c28399 AC |
5431 | Make_Expression_With_Actions (Sloc (Elsex), |
5432 | Actions => Else_Actions (N), | |
5433 | Expression => Relocate_Node (Elsex))); | |
5434 | ||
0812b84e AC |
5435 | Set_Else_Actions (N, No_List); |
5436 | Analyze_And_Resolve (Elsex, Typ); | |
305caf42 AC |
5437 | end if; |
5438 | ||
0812b84e AC |
5439 | return; |
5440 | ||
b2c28399 AC |
5441 | -- If no actions then no expansion needed, gigi will handle it using the |
5442 | -- same approach as a C conditional expression. | |
305caf42 AC |
5443 | |
5444 | else | |
c471e2da AC |
5445 | return; |
5446 | end if; | |
5447 | ||
305caf42 AC |
5448 | -- Fall through here for either the limited expansion, or the case of |
5449 | -- inserting actions for non-limited types. In both these cases, we must | |
5450 | -- move the SLOC of the parent If statement to the newly created one and | |
3fc5d116 RD |
5451 | -- change it to the SLOC of the expression which, after expansion, will |
5452 | -- correspond to what is being evaluated. | |
c471e2da | 5453 | |
533369aa | 5454 | if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then |
c471e2da AC |
5455 | Set_Sloc (New_If, Sloc (Parent (N))); |
5456 | Set_Sloc (Parent (N), Loc); | |
5457 | end if; | |
70482933 | 5458 | |
3fc5d116 RD |
5459 | -- Make sure Then_Actions and Else_Actions are appropriately moved |
5460 | -- to the new if statement. | |
5461 | ||
c471e2da AC |
5462 | if Present (Then_Actions (N)) then |
5463 | Insert_List_Before | |
5464 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
70482933 | 5465 | end if; |
c471e2da AC |
5466 | |
5467 | if Present (Else_Actions (N)) then | |
5468 | Insert_List_Before | |
5469 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
5470 | end if; | |
5471 | ||
5472 | Insert_Action (N, Decl); | |
5473 | Insert_Action (N, New_If); | |
5474 | Rewrite (N, New_N); | |
5475 | Analyze_And_Resolve (N, Typ); | |
9b16cb57 | 5476 | end Expand_N_If_Expression; |
35a1c212 | 5477 | |
70482933 RK |
5478 | ----------------- |
5479 | -- Expand_N_In -- | |
5480 | ----------------- | |
5481 | ||
5482 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 | 5483 | Loc : constant Source_Ptr := Sloc (N); |
4818e7b9 | 5484 | Restyp : constant Entity_Id := Etype (N); |
7324bf49 AC |
5485 | Lop : constant Node_Id := Left_Opnd (N); |
5486 | Rop : constant Node_Id := Right_Opnd (N); | |
5487 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 5488 | |
4818e7b9 RD |
5489 | Ltyp : Entity_Id; |
5490 | Rtyp : Entity_Id; | |
5491 | ||
630d30e9 RD |
5492 | procedure Substitute_Valid_Check; |
5493 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
5494 | -- test for the left operand being in range of its subtype. | |
5495 | ||
5496 | ---------------------------- | |
5497 | -- Substitute_Valid_Check -- | |
5498 | ---------------------------- | |
5499 | ||
5500 | procedure Substitute_Valid_Check is | |
5501 | begin | |
c7532b2d AC |
5502 | Rewrite (N, |
5503 | Make_Attribute_Reference (Loc, | |
5504 | Prefix => Relocate_Node (Lop), | |
5505 | Attribute_Name => Name_Valid)); | |
630d30e9 | 5506 | |
c7532b2d | 5507 | Analyze_And_Resolve (N, Restyp); |
630d30e9 | 5508 | |
acad3c0a AC |
5509 | -- Give warning unless overflow checking is MINIMIZED or ELIMINATED, |
5510 | -- in which case, this usage makes sense, and in any case, we have | |
5511 | -- actually eliminated the danger of optimization above. | |
5512 | ||
a7f1b24f | 5513 | if Overflow_Check_Mode not in Minimized_Or_Eliminated then |
324ac540 AC |
5514 | Error_Msg_N |
5515 | ("??explicit membership test may be optimized away", N); | |
acad3c0a | 5516 | Error_Msg_N -- CODEFIX |
324ac540 | 5517 | ("\??use ''Valid attribute instead", N); |
acad3c0a AC |
5518 | end if; |
5519 | ||
c7532b2d | 5520 | return; |
630d30e9 RD |
5521 | end Substitute_Valid_Check; |
5522 | ||
5523 | -- Start of processing for Expand_N_In | |
5524 | ||
70482933 | 5525 | begin |
308e6f3a | 5526 | -- If set membership case, expand with separate procedure |
4818e7b9 | 5527 | |
197e4514 | 5528 | if Present (Alternatives (N)) then |
a3068ca6 | 5529 | Expand_Set_Membership (N); |
197e4514 AC |
5530 | return; |
5531 | end if; | |
5532 | ||
4818e7b9 RD |
5533 | -- Not set membership, proceed with expansion |
5534 | ||
5535 | Ltyp := Etype (Left_Opnd (N)); | |
5536 | Rtyp := Etype (Right_Opnd (N)); | |
5537 | ||
5707e389 | 5538 | -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer |
f6194278 RD |
5539 | -- type, then expand with a separate procedure. Note the use of the |
5540 | -- flag No_Minimize_Eliminate to prevent infinite recursion. | |
5541 | ||
a7f1b24f | 5542 | if Overflow_Check_Mode in Minimized_Or_Eliminated |
f6194278 RD |
5543 | and then Is_Signed_Integer_Type (Ltyp) |
5544 | and then not No_Minimize_Eliminate (N) | |
5545 | then | |
5546 | Expand_Membership_Minimize_Eliminate_Overflow (N); | |
5547 | return; | |
5548 | end if; | |
5549 | ||
630d30e9 RD |
5550 | -- Check case of explicit test for an expression in range of its |
5551 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
b6b5cca8 | 5552 | -- test and give a warning for scalar types. |
630d30e9 | 5553 | |
4818e7b9 | 5554 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
5555 | |
5556 | -- Only relevant for source comparisons | |
5557 | ||
5558 | and then Comes_From_Source (N) | |
5559 | ||
5560 | -- In floating-point this is a standard way to check for finite values | |
5561 | -- and using 'Valid would typically be a pessimization. | |
5562 | ||
4818e7b9 | 5563 | and then not Is_Floating_Point_Type (Ltyp) |
b6b5cca8 AC |
5564 | |
5565 | -- Don't give the message unless right operand is a type entity and | |
5566 | -- the type of the left operand matches this type. Note that this | |
5567 | -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow | |
5568 | -- checks have changed the type of the left operand. | |
5569 | ||
630d30e9 | 5570 | and then Nkind (Rop) in N_Has_Entity |
4818e7b9 | 5571 | and then Ltyp = Entity (Rop) |
b6b5cca8 AC |
5572 | |
5573 | -- Skip in VM mode, where we have no sense of invalid values. The | |
5574 | -- warning still seems relevant, but not important enough to worry. | |
5575 | ||
26bff3d9 | 5576 | and then VM_Target = No_VM |
b6b5cca8 AC |
5577 | |
5578 | -- Skip this for predicated types, where such expressions are a | |
5579 | -- reasonable way of testing if something meets the predicate. | |
5580 | ||
3d6db7f8 | 5581 | and then not Present (Predicate_Function (Ltyp)) |
630d30e9 RD |
5582 | then |
5583 | Substitute_Valid_Check; | |
5584 | return; | |
5585 | end if; | |
5586 | ||
20b5d666 JM |
5587 | -- Do validity check on operands |
5588 | ||
5589 | if Validity_Checks_On and Validity_Check_Operands then | |
5590 | Ensure_Valid (Left_Opnd (N)); | |
5591 | Validity_Check_Range (Right_Opnd (N)); | |
5592 | end if; | |
5593 | ||
630d30e9 | 5594 | -- Case of explicit range |
fbf5a39b AC |
5595 | |
5596 | if Nkind (Rop) = N_Range then | |
5597 | declare | |
630d30e9 RD |
5598 | Lo : constant Node_Id := Low_Bound (Rop); |
5599 | Hi : constant Node_Id := High_Bound (Rop); | |
5600 | ||
5601 | Lo_Orig : constant Node_Id := Original_Node (Lo); | |
5602 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
5603 | ||
c800f862 RD |
5604 | Lcheck : Compare_Result; |
5605 | Ucheck : Compare_Result; | |
fbf5a39b | 5606 | |
d766cee3 RD |
5607 | Warn1 : constant Boolean := |
5608 | Constant_Condition_Warnings | |
c800f862 RD |
5609 | and then Comes_From_Source (N) |
5610 | and then not In_Instance; | |
d766cee3 | 5611 | -- This must be true for any of the optimization warnings, we |
9a0ddeee AC |
5612 | -- clearly want to give them only for source with the flag on. We |
5613 | -- also skip these warnings in an instance since it may be the | |
5614 | -- case that different instantiations have different ranges. | |
d766cee3 RD |
5615 | |
5616 | Warn2 : constant Boolean := | |
5617 | Warn1 | |
5618 | and then Nkind (Original_Node (Rop)) = N_Range | |
5619 | and then Is_Integer_Type (Etype (Lo)); | |
5620 | -- For the case where only one bound warning is elided, we also | |
5621 | -- insist on an explicit range and an integer type. The reason is | |
5622 | -- that the use of enumeration ranges including an end point is | |
9a0ddeee AC |
5623 | -- common, as is the use of a subtype name, one of whose bounds is |
5624 | -- the same as the type of the expression. | |
d766cee3 | 5625 | |
fbf5a39b | 5626 | begin |
c95e0edc | 5627 | -- If test is explicit x'First .. x'Last, replace by valid check |
630d30e9 | 5628 | |
e606088a AC |
5629 | -- Could use some individual comments for this complex test ??? |
5630 | ||
d766cee3 | 5631 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
5632 | |
5633 | -- And left operand is X'First where X matches left operand | |
5634 | -- type (this eliminates cases of type mismatch, including | |
5635 | -- the cases where ELIMINATED/MINIMIZED mode has changed the | |
5636 | -- type of the left operand. | |
5637 | ||
630d30e9 RD |
5638 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
5639 | and then Attribute_Name (Lo_Orig) = Name_First | |
5640 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 5641 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
b6b5cca8 | 5642 | |
cc6f5d75 | 5643 | -- Same tests for right operand |
b6b5cca8 | 5644 | |
630d30e9 RD |
5645 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
5646 | and then Attribute_Name (Hi_Orig) = Name_Last | |
5647 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 5648 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
b6b5cca8 AC |
5649 | |
5650 | -- Relevant only for source cases | |
5651 | ||
630d30e9 | 5652 | and then Comes_From_Source (N) |
b6b5cca8 AC |
5653 | |
5654 | -- Omit for VM cases, where we don't have invalid values | |
5655 | ||
26bff3d9 | 5656 | and then VM_Target = No_VM |
630d30e9 RD |
5657 | then |
5658 | Substitute_Valid_Check; | |
4818e7b9 | 5659 | goto Leave; |
630d30e9 RD |
5660 | end if; |
5661 | ||
d766cee3 RD |
5662 | -- If bounds of type are known at compile time, and the end points |
5663 | -- are known at compile time and identical, this is another case | |
5664 | -- for substituting a valid test. We only do this for discrete | |
5665 | -- types, since it won't arise in practice for float types. | |
5666 | ||
5667 | if Comes_From_Source (N) | |
5668 | and then Is_Discrete_Type (Ltyp) | |
5669 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
5670 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
5671 | and then Compile_Time_Known_Value (Lo) | |
5672 | and then Compile_Time_Known_Value (Hi) | |
5673 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
5674 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e | 5675 | |
f6194278 RD |
5676 | -- Kill warnings in instances, since they may be cases where we |
5677 | -- have a test in the generic that makes sense with some types | |
5678 | -- and not with other types. | |
94eefd2e RD |
5679 | |
5680 | and then not In_Instance | |
d766cee3 RD |
5681 | then |
5682 | Substitute_Valid_Check; | |
4818e7b9 | 5683 | goto Leave; |
d766cee3 RD |
5684 | end if; |
5685 | ||
9a0ddeee AC |
5686 | -- If we have an explicit range, do a bit of optimization based on |
5687 | -- range analysis (we may be able to kill one or both checks). | |
630d30e9 | 5688 | |
c800f862 RD |
5689 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
5690 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
5691 | ||
630d30e9 RD |
5692 | -- If either check is known to fail, replace result by False since |
5693 | -- the other check does not matter. Preserve the static flag for | |
5694 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
5695 | |
5696 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 5697 | if Warn1 then |
685bc70f AC |
5698 | Error_Msg_N ("?c?range test optimized away", N); |
5699 | Error_Msg_N ("\?c?value is known to be out of range", N); | |
d766cee3 RD |
5700 | end if; |
5701 | ||
e4494292 | 5702 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 5703 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 5704 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 5705 | goto Leave; |
fbf5a39b | 5706 | |
685094bf RD |
5707 | -- If both checks are known to succeed, replace result by True, |
5708 | -- since we know we are in range. | |
fbf5a39b AC |
5709 | |
5710 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 5711 | if Warn1 then |
685bc70f AC |
5712 | Error_Msg_N ("?c?range test optimized away", N); |
5713 | Error_Msg_N ("\?c?value is known to be in range", N); | |
d766cee3 RD |
5714 | end if; |
5715 | ||
e4494292 | 5716 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 5717 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 5718 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 5719 | goto Leave; |
fbf5a39b | 5720 | |
d766cee3 RD |
5721 | -- If lower bound check succeeds and upper bound check is not |
5722 | -- known to succeed or fail, then replace the range check with | |
5723 | -- a comparison against the upper bound. | |
fbf5a39b AC |
5724 | |
5725 | elsif Lcheck in Compare_GE then | |
94eefd2e | 5726 | if Warn2 and then not In_Instance then |
324ac540 AC |
5727 | Error_Msg_N ("??lower bound test optimized away", Lo); |
5728 | Error_Msg_N ("\??value is known to be in range", Lo); | |
d766cee3 RD |
5729 | end if; |
5730 | ||
fbf5a39b AC |
5731 | Rewrite (N, |
5732 | Make_Op_Le (Loc, | |
5733 | Left_Opnd => Lop, | |
5734 | Right_Opnd => High_Bound (Rop))); | |
4818e7b9 RD |
5735 | Analyze_And_Resolve (N, Restyp); |
5736 | goto Leave; | |
fbf5a39b | 5737 | |
d766cee3 RD |
5738 | -- If upper bound check succeeds and lower bound check is not |
5739 | -- known to succeed or fail, then replace the range check with | |
5740 | -- a comparison against the lower bound. | |
fbf5a39b AC |
5741 | |
5742 | elsif Ucheck in Compare_LE then | |
94eefd2e | 5743 | if Warn2 and then not In_Instance then |
324ac540 AC |
5744 | Error_Msg_N ("??upper bound test optimized away", Hi); |
5745 | Error_Msg_N ("\??value is known to be in range", Hi); | |
d766cee3 RD |
5746 | end if; |
5747 | ||
fbf5a39b AC |
5748 | Rewrite (N, |
5749 | Make_Op_Ge (Loc, | |
5750 | Left_Opnd => Lop, | |
5751 | Right_Opnd => Low_Bound (Rop))); | |
4818e7b9 RD |
5752 | Analyze_And_Resolve (N, Restyp); |
5753 | goto Leave; | |
fbf5a39b | 5754 | end if; |
c800f862 RD |
5755 | |
5756 | -- We couldn't optimize away the range check, but there is one | |
5757 | -- more issue. If we are checking constant conditionals, then we | |
5758 | -- see if we can determine the outcome assuming everything is | |
5759 | -- valid, and if so give an appropriate warning. | |
5760 | ||
5761 | if Warn1 and then not Assume_No_Invalid_Values then | |
5762 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
5763 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
5764 | ||
5765 | -- Result is out of range for valid value | |
5766 | ||
5767 | if Lcheck = LT or else Ucheck = GT then | |
ed2233dc | 5768 | Error_Msg_N |
685bc70f | 5769 | ("?c?value can only be in range if it is invalid", N); |
c800f862 RD |
5770 | |
5771 | -- Result is in range for valid value | |
5772 | ||
5773 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
ed2233dc | 5774 | Error_Msg_N |
685bc70f | 5775 | ("?c?value can only be out of range if it is invalid", N); |
c800f862 RD |
5776 | |
5777 | -- Lower bound check succeeds if value is valid | |
5778 | ||
5779 | elsif Warn2 and then Lcheck in Compare_GE then | |
ed2233dc | 5780 | Error_Msg_N |
685bc70f | 5781 | ("?c?lower bound check only fails if it is invalid", Lo); |
c800f862 RD |
5782 | |
5783 | -- Upper bound check succeeds if value is valid | |
5784 | ||
5785 | elsif Warn2 and then Ucheck in Compare_LE then | |
ed2233dc | 5786 | Error_Msg_N |
685bc70f | 5787 | ("?c?upper bound check only fails for invalid values", Hi); |
c800f862 RD |
5788 | end if; |
5789 | end if; | |
fbf5a39b AC |
5790 | end; |
5791 | ||
5792 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 5793 | |
4818e7b9 | 5794 | goto Leave; |
70482933 RK |
5795 | |
5796 | -- Here right operand is a subtype mark | |
5797 | ||
5798 | else | |
5799 | declare | |
82878151 AC |
5800 | Typ : Entity_Id := Etype (Rop); |
5801 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
5802 | Cond : Node_Id := Empty; | |
5803 | New_N : Node_Id; | |
5804 | Obj : Node_Id := Lop; | |
5805 | SCIL_Node : Node_Id; | |
70482933 RK |
5806 | |
5807 | begin | |
5808 | Remove_Side_Effects (Obj); | |
5809 | ||
5810 | -- For tagged type, do tagged membership operation | |
5811 | ||
5812 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 5813 | |
26bff3d9 JM |
5814 | -- No expansion will be performed when VM_Target, as the VM |
5815 | -- back-ends will handle the membership tests directly (tags | |
5816 | -- are not explicitly represented in Java objects, so the | |
5817 | -- normal tagged membership expansion is not what we want). | |
70482933 | 5818 | |
1f110335 | 5819 | if Tagged_Type_Expansion then |
82878151 AC |
5820 | Tagged_Membership (N, SCIL_Node, New_N); |
5821 | Rewrite (N, New_N); | |
4818e7b9 | 5822 | Analyze_And_Resolve (N, Restyp); |
82878151 AC |
5823 | |
5824 | -- Update decoration of relocated node referenced by the | |
5825 | -- SCIL node. | |
5826 | ||
9a0ddeee | 5827 | if Generate_SCIL and then Present (SCIL_Node) then |
7665e4bd | 5828 | Set_SCIL_Node (N, SCIL_Node); |
82878151 | 5829 | end if; |
70482933 RK |
5830 | end if; |
5831 | ||
4818e7b9 | 5832 | goto Leave; |
70482933 | 5833 | |
c95e0edc | 5834 | -- If type is scalar type, rewrite as x in t'First .. t'Last. |
70482933 | 5835 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
5836 | -- type if they come from the original type definition. Also this |
5837 | -- way we get all the processing above for an explicit range. | |
70482933 | 5838 | |
f6194278 | 5839 | -- Don't do this for predicated types, since in this case we |
a90bd866 | 5840 | -- want to check the predicate. |
c0f136cd | 5841 | |
c7532b2d AC |
5842 | elsif Is_Scalar_Type (Typ) then |
5843 | if No (Predicate_Function (Typ)) then | |
5844 | Rewrite (Rop, | |
5845 | Make_Range (Loc, | |
5846 | Low_Bound => | |
5847 | Make_Attribute_Reference (Loc, | |
5848 | Attribute_Name => Name_First, | |
e4494292 | 5849 | Prefix => New_Occurrence_Of (Typ, Loc)), |
c7532b2d AC |
5850 | |
5851 | High_Bound => | |
5852 | Make_Attribute_Reference (Loc, | |
5853 | Attribute_Name => Name_Last, | |
e4494292 | 5854 | Prefix => New_Occurrence_Of (Typ, Loc)))); |
c7532b2d AC |
5855 | Analyze_And_Resolve (N, Restyp); |
5856 | end if; | |
70482933 | 5857 | |
4818e7b9 | 5858 | goto Leave; |
5d09245e AC |
5859 | |
5860 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
5861 | -- a membership test if the subtype mark denotes a constrained | |
5862 | -- Unchecked_Union subtype and the expression lacks inferable | |
5863 | -- discriminants. | |
5864 | ||
5865 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
5866 | and then Is_Constrained (Typ) | |
5867 | and then not Has_Inferable_Discriminants (Lop) | |
5868 | then | |
5869 | Insert_Action (N, | |
5870 | Make_Raise_Program_Error (Loc, | |
5871 | Reason => PE_Unchecked_Union_Restriction)); | |
5872 | ||
9a0ddeee | 5873 | -- Prevent Gigi from generating incorrect code by rewriting the |
f6194278 | 5874 | -- test as False. What is this undocumented thing about ??? |
5d09245e | 5875 | |
9a0ddeee | 5876 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 5877 | goto Leave; |
70482933 RK |
5878 | end if; |
5879 | ||
fbf5a39b AC |
5880 | -- Here we have a non-scalar type |
5881 | ||
70482933 RK |
5882 | if Is_Acc then |
5883 | Typ := Designated_Type (Typ); | |
5884 | end if; | |
5885 | ||
5886 | if not Is_Constrained (Typ) then | |
e4494292 | 5887 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 5888 | Analyze_And_Resolve (N, Restyp); |
70482933 | 5889 | |
685094bf RD |
5890 | -- For the constrained array case, we have to check the subscripts |
5891 | -- for an exact match if the lengths are non-zero (the lengths | |
5892 | -- must match in any case). | |
70482933 RK |
5893 | |
5894 | elsif Is_Array_Type (Typ) then | |
fbf5a39b | 5895 | Check_Subscripts : declare |
9a0ddeee | 5896 | function Build_Attribute_Reference |
2e071734 AC |
5897 | (E : Node_Id; |
5898 | Nam : Name_Id; | |
5899 | Dim : Nat) return Node_Id; | |
9a0ddeee | 5900 | -- Build attribute reference E'Nam (Dim) |
70482933 | 5901 | |
9a0ddeee AC |
5902 | ------------------------------- |
5903 | -- Build_Attribute_Reference -- | |
5904 | ------------------------------- | |
fbf5a39b | 5905 | |
9a0ddeee | 5906 | function Build_Attribute_Reference |
2e071734 AC |
5907 | (E : Node_Id; |
5908 | Nam : Name_Id; | |
5909 | Dim : Nat) return Node_Id | |
70482933 RK |
5910 | is |
5911 | begin | |
5912 | return | |
5913 | Make_Attribute_Reference (Loc, | |
9a0ddeee | 5914 | Prefix => E, |
70482933 | 5915 | Attribute_Name => Nam, |
9a0ddeee | 5916 | Expressions => New_List ( |
70482933 | 5917 | Make_Integer_Literal (Loc, Dim))); |
9a0ddeee | 5918 | end Build_Attribute_Reference; |
70482933 | 5919 | |
fad0600d | 5920 | -- Start of processing for Check_Subscripts |
fbf5a39b | 5921 | |
70482933 RK |
5922 | begin |
5923 | for J in 1 .. Number_Dimensions (Typ) loop | |
5924 | Evolve_And_Then (Cond, | |
5925 | Make_Op_Eq (Loc, | |
5926 | Left_Opnd => | |
9a0ddeee | 5927 | Build_Attribute_Reference |
fbf5a39b AC |
5928 | (Duplicate_Subexpr_No_Checks (Obj), |
5929 | Name_First, J), | |
70482933 | 5930 | Right_Opnd => |
9a0ddeee | 5931 | Build_Attribute_Reference |
70482933 RK |
5932 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); |
5933 | ||
5934 | Evolve_And_Then (Cond, | |
5935 | Make_Op_Eq (Loc, | |
5936 | Left_Opnd => | |
9a0ddeee | 5937 | Build_Attribute_Reference |
fbf5a39b AC |
5938 | (Duplicate_Subexpr_No_Checks (Obj), |
5939 | Name_Last, J), | |
70482933 | 5940 | Right_Opnd => |
9a0ddeee | 5941 | Build_Attribute_Reference |
70482933 RK |
5942 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); |
5943 | end loop; | |
5944 | ||
5945 | if Is_Acc then | |
fbf5a39b AC |
5946 | Cond := |
5947 | Make_Or_Else (Loc, | |
cc6f5d75 | 5948 | Left_Opnd => |
fbf5a39b AC |
5949 | Make_Op_Eq (Loc, |
5950 | Left_Opnd => Obj, | |
5951 | Right_Opnd => Make_Null (Loc)), | |
5952 | Right_Opnd => Cond); | |
70482933 RK |
5953 | end if; |
5954 | ||
5955 | Rewrite (N, Cond); | |
4818e7b9 | 5956 | Analyze_And_Resolve (N, Restyp); |
fbf5a39b | 5957 | end Check_Subscripts; |
70482933 | 5958 | |
685094bf RD |
5959 | -- These are the cases where constraint checks may be required, |
5960 | -- e.g. records with possible discriminants | |
70482933 RK |
5961 | |
5962 | else | |
5963 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
5964 | -- The expression that is built is the negation of the one that |
5965 | -- is used for checking discriminant constraints. | |
70482933 RK |
5966 | |
5967 | Obj := Relocate_Node (Left_Opnd (N)); | |
5968 | ||
5969 | if Has_Discriminants (Typ) then | |
5970 | Cond := Make_Op_Not (Loc, | |
5971 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
5972 | ||
5973 | if Is_Acc then | |
5974 | Cond := Make_Or_Else (Loc, | |
cc6f5d75 | 5975 | Left_Opnd => |
70482933 RK |
5976 | Make_Op_Eq (Loc, |
5977 | Left_Opnd => Obj, | |
5978 | Right_Opnd => Make_Null (Loc)), | |
5979 | Right_Opnd => Cond); | |
5980 | end if; | |
5981 | ||
5982 | else | |
5983 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
5984 | end if; | |
5985 | ||
5986 | Rewrite (N, Cond); | |
4818e7b9 | 5987 | Analyze_And_Resolve (N, Restyp); |
70482933 | 5988 | end if; |
6cce2156 GD |
5989 | |
5990 | -- Ada 2012 (AI05-0149): Handle membership tests applied to an | |
5991 | -- expression of an anonymous access type. This can involve an | |
5992 | -- accessibility test and a tagged type membership test in the | |
5993 | -- case of tagged designated types. | |
5994 | ||
5995 | if Ada_Version >= Ada_2012 | |
5996 | and then Is_Acc | |
5997 | and then Ekind (Ltyp) = E_Anonymous_Access_Type | |
5998 | then | |
5999 | declare | |
6000 | Expr_Entity : Entity_Id := Empty; | |
6001 | New_N : Node_Id; | |
6002 | Param_Level : Node_Id; | |
6003 | Type_Level : Node_Id; | |
996c8821 | 6004 | |
6cce2156 GD |
6005 | begin |
6006 | if Is_Entity_Name (Lop) then | |
6007 | Expr_Entity := Param_Entity (Lop); | |
996c8821 | 6008 | |
6cce2156 GD |
6009 | if not Present (Expr_Entity) then |
6010 | Expr_Entity := Entity (Lop); | |
6011 | end if; | |
6012 | end if; | |
6013 | ||
6014 | -- If a conversion of the anonymous access value to the | |
6015 | -- tested type would be illegal, then the result is False. | |
6016 | ||
6017 | if not Valid_Conversion | |
6018 | (Lop, Rtyp, Lop, Report_Errs => False) | |
6019 | then | |
6020 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
6021 | Analyze_And_Resolve (N, Restyp); | |
6022 | ||
6023 | -- Apply an accessibility check if the access object has an | |
6024 | -- associated access level and when the level of the type is | |
6025 | -- less deep than the level of the access parameter. This | |
6026 | -- only occur for access parameters and stand-alone objects | |
6027 | -- of an anonymous access type. | |
6028 | ||
6029 | else | |
6030 | if Present (Expr_Entity) | |
996c8821 RD |
6031 | and then |
6032 | Present | |
6033 | (Effective_Extra_Accessibility (Expr_Entity)) | |
6034 | and then UI_Gt (Object_Access_Level (Lop), | |
6035 | Type_Access_Level (Rtyp)) | |
6cce2156 GD |
6036 | then |
6037 | Param_Level := | |
6038 | New_Occurrence_Of | |
d15f9422 | 6039 | (Effective_Extra_Accessibility (Expr_Entity), Loc); |
6cce2156 GD |
6040 | |
6041 | Type_Level := | |
6042 | Make_Integer_Literal (Loc, Type_Access_Level (Rtyp)); | |
6043 | ||
6044 | -- Return True only if the accessibility level of the | |
6045 | -- expression entity is not deeper than the level of | |
6046 | -- the tested access type. | |
6047 | ||
6048 | Rewrite (N, | |
6049 | Make_And_Then (Loc, | |
6050 | Left_Opnd => Relocate_Node (N), | |
6051 | Right_Opnd => Make_Op_Le (Loc, | |
6052 | Left_Opnd => Param_Level, | |
6053 | Right_Opnd => Type_Level))); | |
6054 | ||
6055 | Analyze_And_Resolve (N); | |
6056 | end if; | |
6057 | ||
6058 | -- If the designated type is tagged, do tagged membership | |
6059 | -- operation. | |
6060 | ||
6061 | -- *** NOTE: we have to check not null before doing the | |
6062 | -- tagged membership test (but maybe that can be done | |
6063 | -- inside Tagged_Membership?). | |
6064 | ||
6065 | if Is_Tagged_Type (Typ) then | |
6066 | Rewrite (N, | |
6067 | Make_And_Then (Loc, | |
6068 | Left_Opnd => Relocate_Node (N), | |
6069 | Right_Opnd => | |
6070 | Make_Op_Ne (Loc, | |
6071 | Left_Opnd => Obj, | |
6072 | Right_Opnd => Make_Null (Loc)))); | |
6073 | ||
6074 | -- No expansion will be performed when VM_Target, as | |
6075 | -- the VM back-ends will handle the membership tests | |
6076 | -- directly (tags are not explicitly represented in | |
6077 | -- Java objects, so the normal tagged membership | |
6078 | -- expansion is not what we want). | |
6079 | ||
6080 | if Tagged_Type_Expansion then | |
6081 | ||
6082 | -- Note that we have to pass Original_Node, because | |
6083 | -- the membership test might already have been | |
6084 | -- rewritten by earlier parts of membership test. | |
6085 | ||
6086 | Tagged_Membership | |
6087 | (Original_Node (N), SCIL_Node, New_N); | |
6088 | ||
6089 | -- Update decoration of relocated node referenced | |
6090 | -- by the SCIL node. | |
6091 | ||
6092 | if Generate_SCIL and then Present (SCIL_Node) then | |
6093 | Set_SCIL_Node (New_N, SCIL_Node); | |
6094 | end if; | |
6095 | ||
6096 | Rewrite (N, | |
6097 | Make_And_Then (Loc, | |
6098 | Left_Opnd => Relocate_Node (N), | |
6099 | Right_Opnd => New_N)); | |
6100 | ||
6101 | Analyze_And_Resolve (N, Restyp); | |
6102 | end if; | |
6103 | end if; | |
6104 | end if; | |
6105 | end; | |
6106 | end if; | |
70482933 RK |
6107 | end; |
6108 | end if; | |
4818e7b9 RD |
6109 | |
6110 | -- At this point, we have done the processing required for the basic | |
6111 | -- membership test, but not yet dealt with the predicate. | |
6112 | ||
6113 | <<Leave>> | |
6114 | ||
c7532b2d AC |
6115 | -- If a predicate is present, then we do the predicate test, but we |
6116 | -- most certainly want to omit this if we are within the predicate | |
a90bd866 | 6117 | -- function itself, since otherwise we have an infinite recursion. |
3d6db7f8 GD |
6118 | -- The check should also not be emitted when testing against a range |
6119 | -- (the check is only done when the right operand is a subtype; see | |
6120 | -- RM12-4.5.2 (28.1/3-30/3)). | |
4818e7b9 | 6121 | |
c7532b2d AC |
6122 | declare |
6123 | PFunc : constant Entity_Id := Predicate_Function (Rtyp); | |
4818e7b9 | 6124 | |
c7532b2d AC |
6125 | begin |
6126 | if Present (PFunc) | |
6127 | and then Current_Scope /= PFunc | |
3d6db7f8 | 6128 | and then Nkind (Rop) /= N_Range |
c7532b2d AC |
6129 | then |
6130 | Rewrite (N, | |
6131 | Make_And_Then (Loc, | |
6132 | Left_Opnd => Relocate_Node (N), | |
fc142f63 | 6133 | Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True))); |
4818e7b9 | 6134 | |
c7532b2d | 6135 | -- Analyze new expression, mark left operand as analyzed to |
b2009d46 AC |
6136 | -- avoid infinite recursion adding predicate calls. Similarly, |
6137 | -- suppress further range checks on the call. | |
4818e7b9 | 6138 | |
c7532b2d | 6139 | Set_Analyzed (Left_Opnd (N)); |
b2009d46 | 6140 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); |
4818e7b9 | 6141 | |
c7532b2d AC |
6142 | -- All done, skip attempt at compile time determination of result |
6143 | ||
6144 | return; | |
6145 | end if; | |
6146 | end; | |
70482933 RK |
6147 | end Expand_N_In; |
6148 | ||
6149 | -------------------------------- | |
6150 | -- Expand_N_Indexed_Component -- | |
6151 | -------------------------------- | |
6152 | ||
6153 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
6154 | Loc : constant Source_Ptr := Sloc (N); | |
6155 | Typ : constant Entity_Id := Etype (N); | |
6156 | P : constant Node_Id := Prefix (N); | |
6157 | T : constant Entity_Id := Etype (P); | |
5972791c | 6158 | Atp : Entity_Id; |
70482933 RK |
6159 | |
6160 | begin | |
685094bf RD |
6161 | -- A special optimization, if we have an indexed component that is |
6162 | -- selecting from a slice, then we can eliminate the slice, since, for | |
6163 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
6164 | -- the range check required by the slice. The range check for the slice | |
6165 | -- itself has already been generated. The range check for the | |
6166 | -- subscripting operation is ensured by converting the subject to | |
6167 | -- the subtype of the slice. | |
6168 | ||
6169 | -- This optimization not only generates better code, avoiding slice | |
6170 | -- messing especially in the packed case, but more importantly bypasses | |
6171 | -- some problems in handling this peculiar case, for example, the issue | |
6172 | -- of dealing specially with object renamings. | |
70482933 | 6173 | |
45ec05e1 RD |
6174 | if Nkind (P) = N_Slice |
6175 | ||
6176 | -- This optimization is disabled for CodePeer because it can transform | |
6177 | -- an index-check constraint_error into a range-check constraint_error | |
6178 | -- and CodePeer cares about that distinction. | |
6179 | ||
6180 | and then not CodePeer_Mode | |
6181 | then | |
70482933 RK |
6182 | Rewrite (N, |
6183 | Make_Indexed_Component (Loc, | |
cc6f5d75 | 6184 | Prefix => Prefix (P), |
70482933 RK |
6185 | Expressions => New_List ( |
6186 | Convert_To | |
6187 | (Etype (First_Index (Etype (P))), | |
6188 | First (Expressions (N)))))); | |
6189 | Analyze_And_Resolve (N, Typ); | |
6190 | return; | |
6191 | end if; | |
6192 | ||
b4592168 GD |
6193 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
6194 | -- function, then additional actuals must be passed. | |
6195 | ||
0791fbe9 | 6196 | if Ada_Version >= Ada_2005 |
b4592168 GD |
6197 | and then Is_Build_In_Place_Function_Call (P) |
6198 | then | |
6199 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
6200 | end if; | |
6201 | ||
685094bf | 6202 | -- If the prefix is an access type, then we unconditionally rewrite if |
09494c32 | 6203 | -- as an explicit dereference. This simplifies processing for several |
685094bf RD |
6204 | -- cases, including packed array cases and certain cases in which checks |
6205 | -- must be generated. We used to try to do this only when it was | |
6206 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
6207 | |
6208 | if Is_Access_Type (T) then | |
2717634d | 6209 | Insert_Explicit_Dereference (P); |
70482933 | 6210 | Analyze_And_Resolve (P, Designated_Type (T)); |
5972791c AC |
6211 | Atp := Designated_Type (T); |
6212 | else | |
6213 | Atp := T; | |
70482933 RK |
6214 | end if; |
6215 | ||
fbf5a39b AC |
6216 | -- Generate index and validity checks |
6217 | ||
6218 | Generate_Index_Checks (N); | |
6219 | ||
70482933 RK |
6220 | if Validity_Checks_On and then Validity_Check_Subscripts then |
6221 | Apply_Subscript_Validity_Checks (N); | |
6222 | end if; | |
6223 | ||
5972791c AC |
6224 | -- If selecting from an array with atomic components, and atomic sync |
6225 | -- is not suppressed for this array type, set atomic sync flag. | |
6226 | ||
6227 | if (Has_Atomic_Components (Atp) | |
6228 | and then not Atomic_Synchronization_Disabled (Atp)) | |
6229 | or else (Is_Atomic (Typ) | |
6230 | and then not Atomic_Synchronization_Disabled (Typ)) | |
6231 | then | |
4c318253 | 6232 | Activate_Atomic_Synchronization (N); |
5972791c AC |
6233 | end if; |
6234 | ||
70482933 RK |
6235 | -- All done for the non-packed case |
6236 | ||
6237 | if not Is_Packed (Etype (Prefix (N))) then | |
6238 | return; | |
6239 | end if; | |
6240 | ||
6241 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 6242 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
6243 | -- we can always use the normal packed element get circuit. |
6244 | ||
6245 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
6246 | Expand_Packed_Element_Reference (N); | |
6247 | return; | |
6248 | end if; | |
6249 | ||
8ca597af RD |
6250 | -- For a reference to a component of a bit packed array, we convert it |
6251 | -- to a reference to the corresponding Packed_Array_Impl_Type. We only | |
6252 | -- want to do this for simple references, and not for: | |
70482933 | 6253 | |
685094bf RD |
6254 | -- Left side of assignment, or prefix of left side of assignment, or |
6255 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
6256 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
6257 | ||
6258 | -- Renaming objects in renaming associations | |
6259 | -- This case is handled when a use of the renamed variable occurs | |
6260 | ||
6261 | -- Actual parameters for a procedure call | |
6262 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
6263 | ||
6264 | -- The second expression in a 'Read attribute reference | |
6265 | ||
47d3b920 | 6266 | -- The prefix of an address or bit or size attribute reference |
70482933 RK |
6267 | |
6268 | -- The following circuit detects these exceptions | |
6269 | ||
6270 | declare | |
6271 | Child : Node_Id := N; | |
6272 | Parnt : Node_Id := Parent (N); | |
6273 | ||
6274 | begin | |
6275 | loop | |
6276 | if Nkind (Parnt) = N_Unchecked_Expression then | |
6277 | null; | |
6278 | ||
303b4d58 AC |
6279 | elsif Nkind_In (Parnt, N_Object_Renaming_Declaration, |
6280 | N_Procedure_Call_Statement) | |
70482933 RK |
6281 | or else (Nkind (Parnt) = N_Parameter_Association |
6282 | and then | |
6283 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
6284 | then | |
6285 | return; | |
6286 | ||
6287 | elsif Nkind (Parnt) = N_Attribute_Reference | |
b69cd36a AC |
6288 | and then Nam_In (Attribute_Name (Parnt), Name_Address, |
6289 | Name_Bit, | |
6290 | Name_Size) | |
70482933 RK |
6291 | and then Prefix (Parnt) = Child |
6292 | then | |
6293 | return; | |
6294 | ||
6295 | elsif Nkind (Parnt) = N_Assignment_Statement | |
6296 | and then Name (Parnt) = Child | |
6297 | then | |
6298 | return; | |
6299 | ||
685094bf RD |
6300 | -- If the expression is an index of an indexed component, it must |
6301 | -- be expanded regardless of context. | |
fbf5a39b AC |
6302 | |
6303 | elsif Nkind (Parnt) = N_Indexed_Component | |
6304 | and then Child /= Prefix (Parnt) | |
6305 | then | |
6306 | Expand_Packed_Element_Reference (N); | |
6307 | return; | |
6308 | ||
6309 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
6310 | and then Name (Parent (Parnt)) = Parnt | |
6311 | then | |
6312 | return; | |
6313 | ||
70482933 RK |
6314 | elsif Nkind (Parnt) = N_Attribute_Reference |
6315 | and then Attribute_Name (Parnt) = Name_Read | |
6316 | and then Next (First (Expressions (Parnt))) = Child | |
6317 | then | |
6318 | return; | |
6319 | ||
303b4d58 | 6320 | elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component) |
533369aa | 6321 | and then Prefix (Parnt) = Child |
70482933 RK |
6322 | then |
6323 | null; | |
6324 | ||
6325 | else | |
6326 | Expand_Packed_Element_Reference (N); | |
6327 | return; | |
6328 | end if; | |
6329 | ||
685094bf RD |
6330 | -- Keep looking up tree for unchecked expression, or if we are the |
6331 | -- prefix of a possible assignment left side. | |
70482933 RK |
6332 | |
6333 | Child := Parnt; | |
6334 | Parnt := Parent (Child); | |
6335 | end loop; | |
6336 | end; | |
70482933 RK |
6337 | end Expand_N_Indexed_Component; |
6338 | ||
6339 | --------------------- | |
6340 | -- Expand_N_Not_In -- | |
6341 | --------------------- | |
6342 | ||
6343 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
6344 | -- can be done. This avoids needing to duplicate this expansion code. | |
6345 | ||
6346 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
6347 | Loc : constant Source_Ptr := Sloc (N); |
6348 | Typ : constant Entity_Id := Etype (N); | |
6349 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
6350 | |
6351 | begin | |
6352 | Rewrite (N, | |
6353 | Make_Op_Not (Loc, | |
6354 | Right_Opnd => | |
6355 | Make_In (Loc, | |
6356 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 6357 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 6358 | |
197e4514 AC |
6359 | -- If this is a set membership, preserve list of alternatives |
6360 | ||
6361 | Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N))); | |
6362 | ||
d766cee3 | 6363 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 6364 | -- transformations in Expand_N_In). |
630d30e9 RD |
6365 | |
6366 | Set_Comes_From_Source (N, Cfs); | |
6367 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
6368 | ||
8fc789c8 | 6369 | -- Now analyze transformed node |
630d30e9 | 6370 | |
70482933 RK |
6371 | Analyze_And_Resolve (N, Typ); |
6372 | end Expand_N_Not_In; | |
6373 | ||
6374 | ------------------- | |
6375 | -- Expand_N_Null -- | |
6376 | ------------------- | |
6377 | ||
a3f2babd AC |
6378 | -- The only replacement required is for the case of a null of a type that |
6379 | -- is an access to protected subprogram, or a subtype thereof. We represent | |
6380 | -- such access values as a record, and so we must replace the occurrence of | |
6381 | -- null by the equivalent record (with a null address and a null pointer in | |
6382 | -- it), so that the backend creates the proper value. | |
70482933 RK |
6383 | |
6384 | procedure Expand_N_Null (N : Node_Id) is | |
6385 | Loc : constant Source_Ptr := Sloc (N); | |
a3f2babd | 6386 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
70482933 RK |
6387 | Agg : Node_Id; |
6388 | ||
6389 | begin | |
26bff3d9 | 6390 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
6391 | Agg := |
6392 | Make_Aggregate (Loc, | |
6393 | Expressions => New_List ( | |
6394 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
6395 | Make_Null (Loc))); | |
6396 | ||
6397 | Rewrite (N, Agg); | |
6398 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
6399 | ||
685094bf RD |
6400 | -- For subsequent semantic analysis, the node must retain its type. |
6401 | -- Gigi in any case replaces this type by the corresponding record | |
6402 | -- type before processing the node. | |
70482933 RK |
6403 | |
6404 | Set_Etype (N, Typ); | |
6405 | end if; | |
fbf5a39b AC |
6406 | |
6407 | exception | |
6408 | when RE_Not_Available => | |
6409 | return; | |
70482933 RK |
6410 | end Expand_N_Null; |
6411 | ||
6412 | --------------------- | |
6413 | -- Expand_N_Op_Abs -- | |
6414 | --------------------- | |
6415 | ||
6416 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
6417 | Loc : constant Source_Ptr := Sloc (N); | |
cc6f5d75 | 6418 | Expr : constant Node_Id := Right_Opnd (N); |
70482933 RK |
6419 | |
6420 | begin | |
6421 | Unary_Op_Validity_Checks (N); | |
6422 | ||
b6b5cca8 AC |
6423 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6424 | ||
6425 | if Minimized_Eliminated_Overflow_Check (N) then | |
6426 | Apply_Arithmetic_Overflow_Check (N); | |
6427 | return; | |
6428 | end if; | |
6429 | ||
70482933 RK |
6430 | -- Deal with software overflow checking |
6431 | ||
07fc65c4 | 6432 | if not Backend_Overflow_Checks_On_Target |
533369aa AC |
6433 | and then Is_Signed_Integer_Type (Etype (N)) |
6434 | and then Do_Overflow_Check (N) | |
70482933 | 6435 | then |
685094bf RD |
6436 | -- The only case to worry about is when the argument is equal to the |
6437 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 6438 | |
fbf5a39b | 6439 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
6440 | |
6441 | -- with the usual Duplicate_Subexpr use coding for expr | |
6442 | ||
fbf5a39b AC |
6443 | Insert_Action (N, |
6444 | Make_Raise_Constraint_Error (Loc, | |
6445 | Condition => | |
6446 | Make_Op_Eq (Loc, | |
70482933 | 6447 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
6448 | Right_Opnd => |
6449 | Make_Attribute_Reference (Loc, | |
cc6f5d75 | 6450 | Prefix => |
fbf5a39b AC |
6451 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), |
6452 | Attribute_Name => Name_First)), | |
6453 | Reason => CE_Overflow_Check_Failed)); | |
6454 | end if; | |
70482933 RK |
6455 | end Expand_N_Op_Abs; |
6456 | ||
6457 | --------------------- | |
6458 | -- Expand_N_Op_Add -- | |
6459 | --------------------- | |
6460 | ||
6461 | procedure Expand_N_Op_Add (N : Node_Id) is | |
6462 | Typ : constant Entity_Id := Etype (N); | |
6463 | ||
6464 | begin | |
6465 | Binary_Op_Validity_Checks (N); | |
6466 | ||
b6b5cca8 AC |
6467 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6468 | ||
6469 | if Minimized_Eliminated_Overflow_Check (N) then | |
6470 | Apply_Arithmetic_Overflow_Check (N); | |
6471 | return; | |
6472 | end if; | |
6473 | ||
70482933 RK |
6474 | -- N + 0 = 0 + N = N for integer types |
6475 | ||
6476 | if Is_Integer_Type (Typ) then | |
6477 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
6478 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
6479 | then | |
6480 | Rewrite (N, Left_Opnd (N)); | |
6481 | return; | |
6482 | ||
6483 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
6484 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
6485 | then | |
6486 | Rewrite (N, Right_Opnd (N)); | |
6487 | return; | |
6488 | end if; | |
6489 | end if; | |
6490 | ||
fbf5a39b | 6491 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 6492 | |
761f7dcb | 6493 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 RK |
6494 | Apply_Arithmetic_Overflow_Check (N); |
6495 | return; | |
70482933 RK |
6496 | end if; |
6497 | end Expand_N_Op_Add; | |
6498 | ||
6499 | --------------------- | |
6500 | -- Expand_N_Op_And -- | |
6501 | --------------------- | |
6502 | ||
6503 | procedure Expand_N_Op_And (N : Node_Id) is | |
6504 | Typ : constant Entity_Id := Etype (N); | |
6505 | ||
6506 | begin | |
6507 | Binary_Op_Validity_Checks (N); | |
6508 | ||
6509 | if Is_Array_Type (Etype (N)) then | |
6510 | Expand_Boolean_Operator (N); | |
6511 | ||
6512 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
6513 | Adjust_Condition (Left_Opnd (N)); |
6514 | Adjust_Condition (Right_Opnd (N)); | |
6515 | Set_Etype (N, Standard_Boolean); | |
6516 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
6517 | |
6518 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
6519 | Expand_Intrinsic_Call (N, Entity (N)); | |
6520 | ||
70482933 RK |
6521 | end if; |
6522 | end Expand_N_Op_And; | |
6523 | ||
6524 | ------------------------ | |
6525 | -- Expand_N_Op_Concat -- | |
6526 | ------------------------ | |
6527 | ||
6528 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
6529 | Opnds : List_Id; |
6530 | -- List of operands to be concatenated | |
6531 | ||
70482933 | 6532 | Cnode : Node_Id; |
685094bf RD |
6533 | -- Node which is to be replaced by the result of concatenating the nodes |
6534 | -- in the list Opnds. | |
70482933 | 6535 | |
70482933 | 6536 | begin |
fbf5a39b AC |
6537 | -- Ensure validity of both operands |
6538 | ||
70482933 RK |
6539 | Binary_Op_Validity_Checks (N); |
6540 | ||
685094bf RD |
6541 | -- If we are the left operand of a concatenation higher up the tree, |
6542 | -- then do nothing for now, since we want to deal with a series of | |
6543 | -- concatenations as a unit. | |
70482933 RK |
6544 | |
6545 | if Nkind (Parent (N)) = N_Op_Concat | |
6546 | and then N = Left_Opnd (Parent (N)) | |
6547 | then | |
6548 | return; | |
6549 | end if; | |
6550 | ||
6551 | -- We get here with a concatenation whose left operand may be a | |
6552 | -- concatenation itself with a consistent type. We need to process | |
6553 | -- these concatenation operands from left to right, which means | |
6554 | -- from the deepest node in the tree to the highest node. | |
6555 | ||
6556 | Cnode := N; | |
6557 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
6558 | Cnode := Left_Opnd (Cnode); | |
6559 | end loop; | |
6560 | ||
64425dff BD |
6561 | -- Now Cnode is the deepest concatenation, and its parents are the |
6562 | -- concatenation nodes above, so now we process bottom up, doing the | |
64425dff | 6563 | -- operands. |
70482933 | 6564 | |
df46b832 AC |
6565 | -- The outer loop runs more than once if more than one concatenation |
6566 | -- type is involved. | |
70482933 RK |
6567 | |
6568 | Outer : loop | |
6569 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
6570 | Set_Parent (Opnds, N); | |
6571 | ||
df46b832 | 6572 | -- The inner loop gathers concatenation operands |
70482933 RK |
6573 | |
6574 | Inner : while Cnode /= N | |
70482933 RK |
6575 | and then Base_Type (Etype (Cnode)) = |
6576 | Base_Type (Etype (Parent (Cnode))) | |
6577 | loop | |
6578 | Cnode := Parent (Cnode); | |
6579 | Append (Right_Opnd (Cnode), Opnds); | |
6580 | end loop Inner; | |
6581 | ||
68bab0fd | 6582 | Expand_Concatenate (Cnode, Opnds); |
70482933 RK |
6583 | |
6584 | exit Outer when Cnode = N; | |
6585 | Cnode := Parent (Cnode); | |
6586 | end loop Outer; | |
6587 | end Expand_N_Op_Concat; | |
6588 | ||
6589 | ------------------------ | |
6590 | -- Expand_N_Op_Divide -- | |
6591 | ------------------------ | |
6592 | ||
6593 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
6594 | Loc : constant Source_Ptr := Sloc (N); |
6595 | Lopnd : constant Node_Id := Left_Opnd (N); | |
6596 | Ropnd : constant Node_Id := Right_Opnd (N); | |
6597 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
6598 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
6599 | Typ : Entity_Id := Etype (N); | |
6600 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
6601 | and then | |
6602 | Compile_Time_Known_Value (Ropnd); | |
6603 | Rval : Uint; | |
70482933 RK |
6604 | |
6605 | begin | |
6606 | Binary_Op_Validity_Checks (N); | |
6607 | ||
b6b5cca8 AC |
6608 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6609 | ||
6610 | if Minimized_Eliminated_Overflow_Check (N) then | |
6611 | Apply_Arithmetic_Overflow_Check (N); | |
6612 | return; | |
6613 | end if; | |
6614 | ||
6615 | -- Otherwise proceed with expansion of division | |
6616 | ||
f82944b7 JM |
6617 | if Rknow then |
6618 | Rval := Expr_Value (Ropnd); | |
6619 | end if; | |
6620 | ||
70482933 RK |
6621 | -- N / 1 = N for integer types |
6622 | ||
f82944b7 JM |
6623 | if Rknow and then Rval = Uint_1 then |
6624 | Rewrite (N, Lopnd); | |
70482933 RK |
6625 | return; |
6626 | end if; | |
6627 | ||
6628 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
6629 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
6630 | -- operand is an unsigned integer, as required for this to work. | |
6631 | ||
f82944b7 JM |
6632 | if Nkind (Ropnd) = N_Op_Expon |
6633 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
6634 | |
6635 | -- We cannot do this transformation in configurable run time mode if we | |
51bf9bdf | 6636 | -- have 64-bit integers and long shifts are not available. |
fbf5a39b | 6637 | |
761f7dcb | 6638 | and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target) |
70482933 RK |
6639 | then |
6640 | Rewrite (N, | |
6641 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 6642 | Left_Opnd => Lopnd, |
70482933 | 6643 | Right_Opnd => |
f82944b7 | 6644 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
6645 | Analyze_And_Resolve (N, Typ); |
6646 | return; | |
6647 | end if; | |
6648 | ||
6649 | -- Do required fixup of universal fixed operation | |
6650 | ||
6651 | if Typ = Universal_Fixed then | |
6652 | Fixup_Universal_Fixed_Operation (N); | |
6653 | Typ := Etype (N); | |
6654 | end if; | |
6655 | ||
6656 | -- Divisions with fixed-point results | |
6657 | ||
6658 | if Is_Fixed_Point_Type (Typ) then | |
6659 | ||
685094bf RD |
6660 | -- No special processing if Treat_Fixed_As_Integer is set, since |
6661 | -- from a semantic point of view such operations are simply integer | |
6662 | -- operations and will be treated that way. | |
70482933 RK |
6663 | |
6664 | if not Treat_Fixed_As_Integer (N) then | |
6665 | if Is_Integer_Type (Rtyp) then | |
6666 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
6667 | else | |
6668 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
6669 | end if; | |
6670 | end if; | |
6671 | ||
685094bf RD |
6672 | -- Other cases of division of fixed-point operands. Again we exclude the |
6673 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 | 6674 | |
761f7dcb | 6675 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) |
70482933 RK |
6676 | and then not Treat_Fixed_As_Integer (N) |
6677 | then | |
6678 | if Is_Integer_Type (Typ) then | |
6679 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
6680 | else | |
6681 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
6682 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
6683 | end if; | |
6684 | ||
685094bf RD |
6685 | -- Mixed-mode operations can appear in a non-static universal context, |
6686 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 6687 | |
533369aa | 6688 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
f82944b7 JM |
6689 | Rewrite (Ropnd, |
6690 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 6691 | |
f82944b7 | 6692 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 | 6693 | |
533369aa | 6694 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
f82944b7 JM |
6695 | Rewrite (Lopnd, |
6696 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 6697 | |
f82944b7 | 6698 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 6699 | |
f02b8bb8 | 6700 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
6701 | |
6702 | elsif Is_Integer_Type (Typ) then | |
a91e9ac7 | 6703 | Apply_Divide_Checks (N); |
70482933 RK |
6704 | end if; |
6705 | end Expand_N_Op_Divide; | |
6706 | ||
6707 | -------------------- | |
6708 | -- Expand_N_Op_Eq -- | |
6709 | -------------------- | |
6710 | ||
6711 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
6712 | Loc : constant Source_Ptr := Sloc (N); |
6713 | Typ : constant Entity_Id := Etype (N); | |
6714 | Lhs : constant Node_Id := Left_Opnd (N); | |
6715 | Rhs : constant Node_Id := Right_Opnd (N); | |
6716 | Bodies : constant List_Id := New_List; | |
6717 | A_Typ : constant Entity_Id := Etype (Lhs); | |
6718 | ||
70482933 RK |
6719 | Typl : Entity_Id := A_Typ; |
6720 | Op_Name : Entity_Id; | |
6721 | Prim : Elmt_Id; | |
70482933 RK |
6722 | |
6723 | procedure Build_Equality_Call (Eq : Entity_Id); | |
6724 | -- If a constructed equality exists for the type or for its parent, | |
6725 | -- build and analyze call, adding conversions if the operation is | |
6726 | -- inherited. | |
6727 | ||
5d09245e | 6728 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
8fc789c8 | 6729 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
6730 | -- Unchecked_Union subtype. Typ is a record type. |
6731 | ||
70482933 RK |
6732 | ------------------------- |
6733 | -- Build_Equality_Call -- | |
6734 | ------------------------- | |
6735 | ||
6736 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
6737 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
cc6f5d75 AC |
6738 | L_Exp : Node_Id := Relocate_Node (Lhs); |
6739 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
70482933 RK |
6740 | |
6741 | begin | |
dda38714 AC |
6742 | -- Adjust operands if necessary to comparison type |
6743 | ||
70482933 RK |
6744 | if Base_Type (Op_Type) /= Base_Type (A_Typ) |
6745 | and then not Is_Class_Wide_Type (A_Typ) | |
6746 | then | |
6747 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
6748 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
6749 | end if; | |
6750 | ||
5d09245e AC |
6751 | -- If we have an Unchecked_Union, we need to add the inferred |
6752 | -- discriminant values as actuals in the function call. At this | |
6753 | -- point, the expansion has determined that both operands have | |
6754 | -- inferable discriminants. | |
6755 | ||
6756 | if Is_Unchecked_Union (Op_Type) then | |
6757 | declare | |
fa1608c2 ES |
6758 | Lhs_Type : constant Node_Id := Etype (L_Exp); |
6759 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
6760 | ||
6761 | Lhs_Discr_Vals : Elist_Id; | |
6762 | -- List of inferred discriminant values for left operand. | |
6763 | ||
6764 | Rhs_Discr_Vals : Elist_Id; | |
6765 | -- List of inferred discriminant values for right operand. | |
6766 | ||
6767 | Discr : Entity_Id; | |
5d09245e AC |
6768 | |
6769 | begin | |
fa1608c2 ES |
6770 | Lhs_Discr_Vals := New_Elmt_List; |
6771 | Rhs_Discr_Vals := New_Elmt_List; | |
6772 | ||
5d09245e AC |
6773 | -- Per-object constrained selected components require special |
6774 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 6775 | -- Unchecked_Union, we cannot reference its discriminants |
fa1608c2 ES |
6776 | -- directly. This is why we use the extra parameters of the |
6777 | -- equality function of the enclosing Unchecked_Union. | |
5d09245e AC |
6778 | |
6779 | -- type UU_Type (Discr : Integer := 0) is | |
6780 | -- . . . | |
6781 | -- end record; | |
6782 | -- pragma Unchecked_Union (UU_Type); | |
6783 | ||
6784 | -- 1. Unchecked_Union enclosing record: | |
6785 | ||
6786 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
6787 | -- . . . | |
6788 | -- Comp : UU_Type (Discr); | |
6789 | -- . . . | |
6790 | -- end Enclosing_UU_Type; | |
6791 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
6792 | ||
6793 | -- Obj1 : Enclosing_UU_Type; | |
6794 | -- Obj2 : Enclosing_UU_Type (1); | |
6795 | ||
2717634d | 6796 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
6797 | |
6798 | -- Generated code: | |
6799 | ||
6800 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
6801 | ||
6802 | -- A and B are the formal parameters of the equality function | |
6803 | -- of Enclosing_UU_Type. The function always has two extra | |
fa1608c2 ES |
6804 | -- formals to capture the inferred discriminant values for |
6805 | -- each discriminant of the type. | |
5d09245e AC |
6806 | |
6807 | -- 2. Non-Unchecked_Union enclosing record: | |
6808 | ||
6809 | -- type | |
6810 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
6811 | -- is record | |
6812 | -- . . . | |
6813 | -- Comp : UU_Type (Discr); | |
6814 | -- . . . | |
6815 | -- end Enclosing_Non_UU_Type; | |
6816 | ||
6817 | -- Obj1 : Enclosing_Non_UU_Type; | |
6818 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
6819 | ||
630d30e9 | 6820 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
6821 | |
6822 | -- Generated code: | |
6823 | ||
6824 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
6825 | -- obj1.discr, obj2.discr)) then | |
6826 | ||
6827 | -- In this case we can directly reference the discriminants of | |
6828 | -- the enclosing record. | |
6829 | ||
fa1608c2 | 6830 | -- Process left operand of equality |
5d09245e AC |
6831 | |
6832 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
6833 | and then |
6834 | Has_Per_Object_Constraint (Entity (Selector_Name (Lhs))) | |
5d09245e | 6835 | then |
fa1608c2 ES |
6836 | -- If enclosing record is an Unchecked_Union, use formals |
6837 | -- corresponding to each discriminant. The name of the | |
6838 | -- formal is that of the discriminant, with added suffix, | |
6839 | -- see Exp_Ch3.Build_Record_Equality for details. | |
5d09245e | 6840 | |
dda38714 | 6841 | if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs)))) |
5d09245e | 6842 | then |
fa1608c2 ES |
6843 | Discr := |
6844 | First_Discriminant | |
6845 | (Scope (Entity (Selector_Name (Lhs)))); | |
6846 | while Present (Discr) loop | |
cc6f5d75 AC |
6847 | Append_Elmt |
6848 | (Make_Identifier (Loc, | |
6849 | Chars => New_External_Name (Chars (Discr), 'A')), | |
6850 | To => Lhs_Discr_Vals); | |
fa1608c2 ES |
6851 | Next_Discriminant (Discr); |
6852 | end loop; | |
5d09245e | 6853 | |
fa1608c2 ES |
6854 | -- If enclosing record is of a non-Unchecked_Union type, it |
6855 | -- is possible to reference its discriminants directly. | |
5d09245e AC |
6856 | |
6857 | else | |
fa1608c2 ES |
6858 | Discr := First_Discriminant (Lhs_Type); |
6859 | while Present (Discr) loop | |
cc6f5d75 AC |
6860 | Append_Elmt |
6861 | (Make_Selected_Component (Loc, | |
6862 | Prefix => Prefix (Lhs), | |
6863 | Selector_Name => | |
6864 | New_Copy | |
6865 | (Get_Discriminant_Value (Discr, | |
6866 | Lhs_Type, | |
6867 | Stored_Constraint (Lhs_Type)))), | |
6868 | To => Lhs_Discr_Vals); | |
fa1608c2 ES |
6869 | Next_Discriminant (Discr); |
6870 | end loop; | |
5d09245e AC |
6871 | end if; |
6872 | ||
fa1608c2 ES |
6873 | -- Otherwise operand is on object with a constrained type. |
6874 | -- Infer the discriminant values from the constraint. | |
5d09245e AC |
6875 | |
6876 | else | |
fa1608c2 ES |
6877 | |
6878 | Discr := First_Discriminant (Lhs_Type); | |
6879 | while Present (Discr) loop | |
cc6f5d75 AC |
6880 | Append_Elmt |
6881 | (New_Copy | |
6882 | (Get_Discriminant_Value (Discr, | |
fa1608c2 ES |
6883 | Lhs_Type, |
6884 | Stored_Constraint (Lhs_Type))), | |
cc6f5d75 | 6885 | To => Lhs_Discr_Vals); |
fa1608c2 ES |
6886 | Next_Discriminant (Discr); |
6887 | end loop; | |
5d09245e AC |
6888 | end if; |
6889 | ||
fa1608c2 | 6890 | -- Similar processing for right operand of equality |
5d09245e AC |
6891 | |
6892 | if Nkind (Rhs) = N_Selected_Component | |
533369aa AC |
6893 | and then |
6894 | Has_Per_Object_Constraint (Entity (Selector_Name (Rhs))) | |
5d09245e | 6895 | then |
5e1c00fa | 6896 | if Is_Unchecked_Union |
cc6f5d75 | 6897 | (Scope (Entity (Selector_Name (Rhs)))) |
5d09245e | 6898 | then |
fa1608c2 ES |
6899 | Discr := |
6900 | First_Discriminant | |
6901 | (Scope (Entity (Selector_Name (Rhs)))); | |
6902 | while Present (Discr) loop | |
cc6f5d75 AC |
6903 | Append_Elmt |
6904 | (Make_Identifier (Loc, | |
6905 | Chars => New_External_Name (Chars (Discr), 'B')), | |
6906 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
6907 | Next_Discriminant (Discr); |
6908 | end loop; | |
5d09245e AC |
6909 | |
6910 | else | |
fa1608c2 ES |
6911 | Discr := First_Discriminant (Rhs_Type); |
6912 | while Present (Discr) loop | |
cc6f5d75 AC |
6913 | Append_Elmt |
6914 | (Make_Selected_Component (Loc, | |
6915 | Prefix => Prefix (Rhs), | |
6916 | Selector_Name => | |
6917 | New_Copy (Get_Discriminant_Value | |
6918 | (Discr, | |
6919 | Rhs_Type, | |
6920 | Stored_Constraint (Rhs_Type)))), | |
6921 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
6922 | Next_Discriminant (Discr); |
6923 | end loop; | |
5d09245e | 6924 | end if; |
5d09245e | 6925 | |
fa1608c2 ES |
6926 | else |
6927 | Discr := First_Discriminant (Rhs_Type); | |
6928 | while Present (Discr) loop | |
cc6f5d75 AC |
6929 | Append_Elmt |
6930 | (New_Copy (Get_Discriminant_Value | |
6931 | (Discr, | |
6932 | Rhs_Type, | |
6933 | Stored_Constraint (Rhs_Type))), | |
6934 | To => Rhs_Discr_Vals); | |
fa1608c2 ES |
6935 | Next_Discriminant (Discr); |
6936 | end loop; | |
5d09245e AC |
6937 | end if; |
6938 | ||
fa1608c2 ES |
6939 | -- Now merge the list of discriminant values so that values |
6940 | -- of corresponding discriminants are adjacent. | |
6941 | ||
6942 | declare | |
6943 | Params : List_Id; | |
6944 | L_Elmt : Elmt_Id; | |
6945 | R_Elmt : Elmt_Id; | |
6946 | ||
6947 | begin | |
6948 | Params := New_List (L_Exp, R_Exp); | |
6949 | L_Elmt := First_Elmt (Lhs_Discr_Vals); | |
6950 | R_Elmt := First_Elmt (Rhs_Discr_Vals); | |
6951 | while Present (L_Elmt) loop | |
6952 | Append_To (Params, Node (L_Elmt)); | |
6953 | Append_To (Params, Node (R_Elmt)); | |
6954 | Next_Elmt (L_Elmt); | |
6955 | Next_Elmt (R_Elmt); | |
6956 | end loop; | |
6957 | ||
6958 | Rewrite (N, | |
6959 | Make_Function_Call (Loc, | |
e4494292 | 6960 | Name => New_Occurrence_Of (Eq, Loc), |
fa1608c2 ES |
6961 | Parameter_Associations => Params)); |
6962 | end; | |
5d09245e AC |
6963 | end; |
6964 | ||
6965 | -- Normal case, not an unchecked union | |
6966 | ||
6967 | else | |
6968 | Rewrite (N, | |
6969 | Make_Function_Call (Loc, | |
e4494292 | 6970 | Name => New_Occurrence_Of (Eq, Loc), |
5d09245e AC |
6971 | Parameter_Associations => New_List (L_Exp, R_Exp))); |
6972 | end if; | |
70482933 RK |
6973 | |
6974 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
6975 | end Build_Equality_Call; | |
6976 | ||
5d09245e AC |
6977 | ------------------------------------ |
6978 | -- Has_Unconstrained_UU_Component -- | |
6979 | ------------------------------------ | |
6980 | ||
6981 | function Has_Unconstrained_UU_Component | |
6982 | (Typ : Node_Id) return Boolean | |
6983 | is | |
6984 | Tdef : constant Node_Id := | |
57848bf7 | 6985 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
6986 | Clist : Node_Id; |
6987 | Vpart : Node_Id; | |
6988 | ||
6989 | function Component_Is_Unconstrained_UU | |
6990 | (Comp : Node_Id) return Boolean; | |
6991 | -- Determines whether the subtype of the component is an | |
6992 | -- unconstrained Unchecked_Union. | |
6993 | ||
6994 | function Variant_Is_Unconstrained_UU | |
6995 | (Variant : Node_Id) return Boolean; | |
6996 | -- Determines whether a component of the variant has an unconstrained | |
6997 | -- Unchecked_Union subtype. | |
6998 | ||
6999 | ----------------------------------- | |
7000 | -- Component_Is_Unconstrained_UU -- | |
7001 | ----------------------------------- | |
7002 | ||
7003 | function Component_Is_Unconstrained_UU | |
7004 | (Comp : Node_Id) return Boolean | |
7005 | is | |
7006 | begin | |
7007 | if Nkind (Comp) /= N_Component_Declaration then | |
7008 | return False; | |
7009 | end if; | |
7010 | ||
7011 | declare | |
7012 | Sindic : constant Node_Id := | |
7013 | Subtype_Indication (Component_Definition (Comp)); | |
7014 | ||
7015 | begin | |
7016 | -- Unconstrained nominal type. In the case of a constraint | |
7017 | -- present, the node kind would have been N_Subtype_Indication. | |
7018 | ||
7019 | if Nkind (Sindic) = N_Identifier then | |
7020 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
7021 | end if; | |
7022 | ||
7023 | return False; | |
7024 | end; | |
7025 | end Component_Is_Unconstrained_UU; | |
7026 | ||
7027 | --------------------------------- | |
7028 | -- Variant_Is_Unconstrained_UU -- | |
7029 | --------------------------------- | |
7030 | ||
7031 | function Variant_Is_Unconstrained_UU | |
7032 | (Variant : Node_Id) return Boolean | |
7033 | is | |
7034 | Clist : constant Node_Id := Component_List (Variant); | |
7035 | ||
7036 | begin | |
7037 | if Is_Empty_List (Component_Items (Clist)) then | |
7038 | return False; | |
7039 | end if; | |
7040 | ||
f02b8bb8 RD |
7041 | -- We only need to test one component |
7042 | ||
5d09245e AC |
7043 | declare |
7044 | Comp : Node_Id := First (Component_Items (Clist)); | |
7045 | ||
7046 | begin | |
7047 | while Present (Comp) loop | |
5d09245e AC |
7048 | if Component_Is_Unconstrained_UU (Comp) then |
7049 | return True; | |
7050 | end if; | |
7051 | ||
7052 | Next (Comp); | |
7053 | end loop; | |
7054 | end; | |
7055 | ||
7056 | -- None of the components withing the variant were of | |
7057 | -- unconstrained Unchecked_Union type. | |
7058 | ||
7059 | return False; | |
7060 | end Variant_Is_Unconstrained_UU; | |
7061 | ||
7062 | -- Start of processing for Has_Unconstrained_UU_Component | |
7063 | ||
7064 | begin | |
7065 | if Null_Present (Tdef) then | |
7066 | return False; | |
7067 | end if; | |
7068 | ||
7069 | Clist := Component_List (Tdef); | |
7070 | Vpart := Variant_Part (Clist); | |
7071 | ||
7072 | -- Inspect available components | |
7073 | ||
7074 | if Present (Component_Items (Clist)) then | |
7075 | declare | |
7076 | Comp : Node_Id := First (Component_Items (Clist)); | |
7077 | ||
7078 | begin | |
7079 | while Present (Comp) loop | |
7080 | ||
8fc789c8 | 7081 | -- One component is sufficient |
5d09245e AC |
7082 | |
7083 | if Component_Is_Unconstrained_UU (Comp) then | |
7084 | return True; | |
7085 | end if; | |
7086 | ||
7087 | Next (Comp); | |
7088 | end loop; | |
7089 | end; | |
7090 | end if; | |
7091 | ||
7092 | -- Inspect available components withing variants | |
7093 | ||
7094 | if Present (Vpart) then | |
7095 | declare | |
7096 | Variant : Node_Id := First (Variants (Vpart)); | |
7097 | ||
7098 | begin | |
7099 | while Present (Variant) loop | |
7100 | ||
8fc789c8 | 7101 | -- One component within a variant is sufficient |
5d09245e AC |
7102 | |
7103 | if Variant_Is_Unconstrained_UU (Variant) then | |
7104 | return True; | |
7105 | end if; | |
7106 | ||
7107 | Next (Variant); | |
7108 | end loop; | |
7109 | end; | |
7110 | end if; | |
7111 | ||
7112 | -- Neither the available components, nor the components inside the | |
7113 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
7114 | ||
7115 | return False; | |
7116 | end Has_Unconstrained_UU_Component; | |
7117 | ||
70482933 RK |
7118 | -- Start of processing for Expand_N_Op_Eq |
7119 | ||
7120 | begin | |
7121 | Binary_Op_Validity_Checks (N); | |
7122 | ||
456cbfa5 AC |
7123 | -- Deal with private types |
7124 | ||
70482933 RK |
7125 | if Ekind (Typl) = E_Private_Type then |
7126 | Typl := Underlying_Type (Typl); | |
70482933 RK |
7127 | elsif Ekind (Typl) = E_Private_Subtype then |
7128 | Typl := Underlying_Type (Base_Type (Typl)); | |
f02b8bb8 RD |
7129 | else |
7130 | null; | |
70482933 RK |
7131 | end if; |
7132 | ||
7133 | -- It may happen in error situations that the underlying type is not | |
7134 | -- set. The error will be detected later, here we just defend the | |
7135 | -- expander code. | |
7136 | ||
7137 | if No (Typl) then | |
7138 | return; | |
7139 | end if; | |
7140 | ||
7141 | Typl := Base_Type (Typl); | |
7142 | ||
dda38714 AC |
7143 | -- Equality between variant records results in a call to a routine |
7144 | -- that has conditional tests of the discriminant value(s), and hence | |
7145 | -- violates the No_Implicit_Conditionals restriction. | |
7146 | ||
7147 | if Has_Variant_Part (Typl) then | |
7148 | declare | |
7149 | Msg : Boolean; | |
7150 | ||
7151 | begin | |
7152 | Check_Restriction (Msg, No_Implicit_Conditionals, N); | |
7153 | ||
7154 | if Msg then | |
7155 | Error_Msg_N | |
7156 | ("\comparison of variant records tests discriminants", N); | |
7157 | return; | |
7158 | end if; | |
7159 | end; | |
7160 | end if; | |
7161 | ||
456cbfa5 | 7162 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7163 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7164 | |
7165 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7166 | ||
7167 | if Nkind (N) /= N_Op_Eq then | |
7168 | return; | |
7169 | end if; | |
7170 | ||
70482933 RK |
7171 | -- Boolean types (requiring handling of non-standard case) |
7172 | ||
f02b8bb8 | 7173 | if Is_Boolean_Type (Typl) then |
70482933 RK |
7174 | Adjust_Condition (Left_Opnd (N)); |
7175 | Adjust_Condition (Right_Opnd (N)); | |
7176 | Set_Etype (N, Standard_Boolean); | |
7177 | Adjust_Result_Type (N, Typ); | |
7178 | ||
7179 | -- Array types | |
7180 | ||
7181 | elsif Is_Array_Type (Typl) then | |
7182 | ||
1033834f RD |
7183 | -- If we are doing full validity checking, and it is possible for the |
7184 | -- array elements to be invalid then expand out array comparisons to | |
7185 | -- make sure that we check the array elements. | |
fbf5a39b | 7186 | |
1033834f RD |
7187 | if Validity_Check_Operands |
7188 | and then not Is_Known_Valid (Component_Type (Typl)) | |
7189 | then | |
fbf5a39b AC |
7190 | declare |
7191 | Save_Force_Validity_Checks : constant Boolean := | |
7192 | Force_Validity_Checks; | |
7193 | begin | |
7194 | Force_Validity_Checks := True; | |
7195 | Rewrite (N, | |
0da2c8ac AC |
7196 | Expand_Array_Equality |
7197 | (N, | |
7198 | Relocate_Node (Lhs), | |
7199 | Relocate_Node (Rhs), | |
7200 | Bodies, | |
7201 | Typl)); | |
7202 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
7203 | Analyze_And_Resolve (N, Standard_Boolean); |
7204 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
7205 | end; | |
7206 | ||
a9d8907c | 7207 | -- Packed case where both operands are known aligned |
70482933 | 7208 | |
a9d8907c JM |
7209 | elsif Is_Bit_Packed_Array (Typl) |
7210 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
7211 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
7212 | then | |
70482933 RK |
7213 | Expand_Packed_Eq (N); |
7214 | ||
5e1c00fa RD |
7215 | -- Where the component type is elementary we can use a block bit |
7216 | -- comparison (if supported on the target) exception in the case | |
7217 | -- of floating-point (negative zero issues require element by | |
7218 | -- element comparison), and atomic types (where we must be sure | |
a9d8907c | 7219 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 7220 | |
70482933 RK |
7221 | elsif Is_Elementary_Type (Component_Type (Typl)) |
7222 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 7223 | and then not Is_Atomic (Component_Type (Typl)) |
a9d8907c JM |
7224 | and then not Is_Possibly_Unaligned_Object (Lhs) |
7225 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
fbf5a39b | 7226 | and then Support_Composite_Compare_On_Target |
70482933 RK |
7227 | then |
7228 | null; | |
7229 | ||
685094bf RD |
7230 | -- For composite and floating-point cases, expand equality loop to |
7231 | -- make sure of using proper comparisons for tagged types, and | |
7232 | -- correctly handling the floating-point case. | |
70482933 RK |
7233 | |
7234 | else | |
7235 | Rewrite (N, | |
0da2c8ac AC |
7236 | Expand_Array_Equality |
7237 | (N, | |
7238 | Relocate_Node (Lhs), | |
7239 | Relocate_Node (Rhs), | |
7240 | Bodies, | |
7241 | Typl)); | |
70482933 RK |
7242 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
7243 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7244 | end if; | |
7245 | ||
7246 | -- Record Types | |
7247 | ||
7248 | elsif Is_Record_Type (Typl) then | |
7249 | ||
7250 | -- For tagged types, use the primitive "=" | |
7251 | ||
7252 | if Is_Tagged_Type (Typl) then | |
7253 | ||
0669bebe GB |
7254 | -- No need to do anything else compiling under restriction |
7255 | -- No_Dispatching_Calls. During the semantic analysis we | |
7256 | -- already notified such violation. | |
7257 | ||
7258 | if Restriction_Active (No_Dispatching_Calls) then | |
7259 | return; | |
7260 | end if; | |
7261 | ||
685094bf RD |
7262 | -- If this is derived from an untagged private type completed with |
7263 | -- a tagged type, it does not have a full view, so we use the | |
7264 | -- primitive operations of the private type. This check should no | |
7265 | -- longer be necessary when these types get their full views??? | |
70482933 RK |
7266 | |
7267 | if Is_Private_Type (A_Typ) | |
7268 | and then not Is_Tagged_Type (A_Typ) | |
7269 | and then Is_Derived_Type (A_Typ) | |
7270 | and then No (Full_View (A_Typ)) | |
7271 | then | |
685094bf RD |
7272 | -- Search for equality operation, checking that the operands |
7273 | -- have the same type. Note that we must find a matching entry, | |
a90bd866 | 7274 | -- or something is very wrong. |
2e071734 | 7275 | |
70482933 RK |
7276 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
7277 | ||
2e071734 AC |
7278 | while Present (Prim) loop |
7279 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7280 | and then Etype (First_Formal (Node (Prim))) = | |
7281 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
7282 | and then | |
7283 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
7284 | ||
70482933 | 7285 | Next_Elmt (Prim); |
70482933 RK |
7286 | end loop; |
7287 | ||
2e071734 | 7288 | pragma Assert (Present (Prim)); |
70482933 | 7289 | Op_Name := Node (Prim); |
fbf5a39b AC |
7290 | |
7291 | -- Find the type's predefined equality or an overriding | |
3dddb11e | 7292 | -- user-defined equality. The reason for not simply calling |
fbf5a39b | 7293 | -- Find_Prim_Op here is that there may be a user-defined |
3dddb11e ES |
7294 | -- overloaded equality op that precedes the equality that we |
7295 | -- want, so we have to explicitly search (e.g., there could be | |
7296 | -- an equality with two different parameter types). | |
fbf5a39b | 7297 | |
70482933 | 7298 | else |
fbf5a39b | 7299 | if Is_Class_Wide_Type (Typl) then |
3dddb11e | 7300 | Typl := Find_Specific_Type (Typl); |
fbf5a39b AC |
7301 | end if; |
7302 | ||
7303 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
7304 | while Present (Prim) loop |
7305 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7306 | and then Etype (First_Formal (Node (Prim))) = | |
7307 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
7308 | and then |
7309 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
7310 | |
7311 | Next_Elmt (Prim); | |
fbf5a39b AC |
7312 | end loop; |
7313 | ||
2e071734 | 7314 | pragma Assert (Present (Prim)); |
fbf5a39b | 7315 | Op_Name := Node (Prim); |
70482933 RK |
7316 | end if; |
7317 | ||
7318 | Build_Equality_Call (Op_Name); | |
7319 | ||
5d09245e AC |
7320 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
7321 | -- predefined equality operator for a type which has a subcomponent | |
7322 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
7323 | ||
7324 | elsif Has_Unconstrained_UU_Component (Typl) then | |
7325 | Insert_Action (N, | |
7326 | Make_Raise_Program_Error (Loc, | |
7327 | Reason => PE_Unchecked_Union_Restriction)); | |
7328 | ||
7329 | -- Prevent Gigi from generating incorrect code by rewriting the | |
6cb3037c | 7330 | -- equality as a standard False. (is this documented somewhere???) |
5d09245e AC |
7331 | |
7332 | Rewrite (N, | |
7333 | New_Occurrence_Of (Standard_False, Loc)); | |
7334 | ||
7335 | elsif Is_Unchecked_Union (Typl) then | |
7336 | ||
7337 | -- If we can infer the discriminants of the operands, we make a | |
7338 | -- call to the TSS equality function. | |
7339 | ||
7340 | if Has_Inferable_Discriminants (Lhs) | |
7341 | and then | |
7342 | Has_Inferable_Discriminants (Rhs) | |
7343 | then | |
7344 | Build_Equality_Call | |
7345 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
7346 | ||
7347 | else | |
7348 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
7349 | -- the predefined equality operator for an Unchecked_Union type | |
7350 | -- if either of the operands lack inferable discriminants. | |
7351 | ||
7352 | Insert_Action (N, | |
7353 | Make_Raise_Program_Error (Loc, | |
7354 | Reason => PE_Unchecked_Union_Restriction)); | |
7355 | ||
29ad9ea5 AC |
7356 | -- Emit a warning on source equalities only, otherwise the |
7357 | -- message may appear out of place due to internal use. The | |
7358 | -- warning is unconditional because it is required by the | |
7359 | -- language. | |
7360 | ||
7361 | if Comes_From_Source (N) then | |
7362 | Error_Msg_N | |
facfa165 | 7363 | ("Unchecked_Union discriminants cannot be determined??", |
29ad9ea5 AC |
7364 | N); |
7365 | Error_Msg_N | |
facfa165 | 7366 | ("\Program_Error will be raised for equality operation??", |
29ad9ea5 AC |
7367 | N); |
7368 | end if; | |
7369 | ||
5d09245e | 7370 | -- Prevent Gigi from generating incorrect code by rewriting |
6cb3037c | 7371 | -- the equality as a standard False (documented where???). |
5d09245e AC |
7372 | |
7373 | Rewrite (N, | |
7374 | New_Occurrence_Of (Standard_False, Loc)); | |
5d09245e AC |
7375 | end if; |
7376 | ||
70482933 RK |
7377 | -- If a type support function is present (for complex cases), use it |
7378 | ||
fbf5a39b AC |
7379 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
7380 | Build_Equality_Call | |
7381 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 | 7382 | |
8d80ff64 AC |
7383 | -- When comparing two Bounded_Strings, use the primitive equality of |
7384 | -- the root Super_String type. | |
7385 | ||
7386 | elsif Is_Bounded_String (Typl) then | |
7387 | Prim := | |
7388 | First_Elmt (Collect_Primitive_Operations (Root_Type (Typl))); | |
7389 | ||
7390 | while Present (Prim) loop | |
7391 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7392 | and then Etype (First_Formal (Node (Prim))) = | |
7393 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
7394 | and then Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
7395 | ||
7396 | Next_Elmt (Prim); | |
7397 | end loop; | |
7398 | ||
7399 | -- A Super_String type should always have a primitive equality | |
7400 | ||
7401 | pragma Assert (Present (Prim)); | |
7402 | Build_Equality_Call (Node (Prim)); | |
7403 | ||
70482933 | 7404 | -- Otherwise expand the component by component equality. Note that |
8fc789c8 | 7405 | -- we never use block-bit comparisons for records, because of the |
70482933 RK |
7406 | -- problems with gaps. The backend will often be able to recombine |
7407 | -- the separate comparisons that we generate here. | |
7408 | ||
7409 | else | |
7410 | Remove_Side_Effects (Lhs); | |
7411 | Remove_Side_Effects (Rhs); | |
7412 | Rewrite (N, | |
7413 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
7414 | ||
7415 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
7416 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7417 | end if; | |
7418 | end if; | |
7419 | ||
d26dc4b5 | 7420 | -- Test if result is known at compile time |
70482933 | 7421 | |
d26dc4b5 | 7422 | Rewrite_Comparison (N); |
f02b8bb8 | 7423 | |
0580d807 | 7424 | Optimize_Length_Comparison (N); |
70482933 RK |
7425 | end Expand_N_Op_Eq; |
7426 | ||
7427 | ----------------------- | |
7428 | -- Expand_N_Op_Expon -- | |
7429 | ----------------------- | |
7430 | ||
7431 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
7432 | Loc : constant Source_Ptr := Sloc (N); | |
7433 | Typ : constant Entity_Id := Etype (N); | |
7434 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
7435 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 7436 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
7437 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
7438 | Exptyp : constant Entity_Id := Etype (Exp); | |
7439 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
7440 | Expv : Uint; | |
70482933 RK |
7441 | Temp : Node_Id; |
7442 | Rent : RE_Id; | |
7443 | Ent : Entity_Id; | |
fbf5a39b | 7444 | Etyp : Entity_Id; |
cb42ba5d | 7445 | Xnode : Node_Id; |
70482933 RK |
7446 | |
7447 | begin | |
7448 | Binary_Op_Validity_Checks (N); | |
7449 | ||
5114f3ff | 7450 | -- CodePeer wants to see the unexpanded N_Op_Expon node |
8f66cda7 | 7451 | |
5114f3ff | 7452 | if CodePeer_Mode then |
8f66cda7 AC |
7453 | return; |
7454 | end if; | |
7455 | ||
685094bf RD |
7456 | -- If either operand is of a private type, then we have the use of an |
7457 | -- intrinsic operator, and we get rid of the privateness, by using root | |
7458 | -- types of underlying types for the actual operation. Otherwise the | |
7459 | -- private types will cause trouble if we expand multiplications or | |
7460 | -- shifts etc. We also do this transformation if the result type is | |
7461 | -- different from the base type. | |
07fc65c4 GB |
7462 | |
7463 | if Is_Private_Type (Etype (Base)) | |
8f66cda7 AC |
7464 | or else Is_Private_Type (Typ) |
7465 | or else Is_Private_Type (Exptyp) | |
7466 | or else Rtyp /= Root_Type (Bastyp) | |
07fc65c4 GB |
7467 | then |
7468 | declare | |
7469 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
7470 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
07fc65c4 GB |
7471 | begin |
7472 | Rewrite (N, | |
7473 | Unchecked_Convert_To (Typ, | |
7474 | Make_Op_Expon (Loc, | |
7475 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
7476 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
7477 | Analyze_And_Resolve (N, Typ); | |
7478 | return; | |
7479 | end; | |
7480 | end if; | |
7481 | ||
b6b5cca8 | 7482 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6cb3037c | 7483 | |
b6b5cca8 | 7484 | if Minimized_Eliminated_Overflow_Check (N) then |
6cb3037c AC |
7485 | Apply_Arithmetic_Overflow_Check (N); |
7486 | return; | |
7487 | end if; | |
7488 | ||
cb42ba5d AC |
7489 | -- Test for case of known right argument where we can replace the |
7490 | -- exponentiation by an equivalent expression using multiplication. | |
70482933 | 7491 | |
6c3c671e AC |
7492 | -- Note: use CRT_Safe version of Compile_Time_Known_Value because in |
7493 | -- configurable run-time mode, we may not have the exponentiation | |
7494 | -- routine available, and we don't want the legality of the program | |
7495 | -- to depend on how clever the compiler is in knowing values. | |
7496 | ||
7497 | if CRT_Safe_Compile_Time_Known_Value (Exp) then | |
70482933 RK |
7498 | Expv := Expr_Value (Exp); |
7499 | ||
7500 | -- We only fold small non-negative exponents. You might think we | |
7501 | -- could fold small negative exponents for the real case, but we | |
7502 | -- can't because we are required to raise Constraint_Error for | |
7503 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
7504 | -- See ACVC test C4A012B. | |
7505 | ||
7506 | if Expv >= 0 and then Expv <= 4 then | |
7507 | ||
7508 | -- X ** 0 = 1 (or 1.0) | |
7509 | ||
7510 | if Expv = 0 then | |
abcbd24c ST |
7511 | |
7512 | -- Call Remove_Side_Effects to ensure that any side effects | |
7513 | -- in the ignored left operand (in particular function calls | |
7514 | -- to user defined functions) are properly executed. | |
7515 | ||
7516 | Remove_Side_Effects (Base); | |
7517 | ||
70482933 RK |
7518 | if Ekind (Typ) in Integer_Kind then |
7519 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
7520 | else | |
7521 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
7522 | end if; | |
7523 | ||
7524 | -- X ** 1 = X | |
7525 | ||
7526 | elsif Expv = 1 then | |
7527 | Xnode := Base; | |
7528 | ||
7529 | -- X ** 2 = X * X | |
7530 | ||
7531 | elsif Expv = 2 then | |
7532 | Xnode := | |
7533 | Make_Op_Multiply (Loc, | |
7534 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 7535 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
7536 | |
7537 | -- X ** 3 = X * X * X | |
7538 | ||
7539 | elsif Expv = 3 then | |
7540 | Xnode := | |
7541 | Make_Op_Multiply (Loc, | |
7542 | Left_Opnd => | |
7543 | Make_Op_Multiply (Loc, | |
7544 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
7545 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
7546 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
7547 | |
7548 | -- X ** 4 -> | |
cb42ba5d AC |
7549 | |
7550 | -- do | |
70482933 | 7551 | -- En : constant base'type := base * base; |
cb42ba5d | 7552 | -- in |
70482933 RK |
7553 | -- En * En |
7554 | ||
cb42ba5d AC |
7555 | else |
7556 | pragma Assert (Expv = 4); | |
191fcb3a | 7557 | Temp := Make_Temporary (Loc, 'E', Base); |
70482933 | 7558 | |
cb42ba5d AC |
7559 | Xnode := |
7560 | Make_Expression_With_Actions (Loc, | |
7561 | Actions => New_List ( | |
7562 | Make_Object_Declaration (Loc, | |
7563 | Defining_Identifier => Temp, | |
7564 | Constant_Present => True, | |
e4494292 | 7565 | Object_Definition => New_Occurrence_Of (Typ, Loc), |
cb42ba5d AC |
7566 | Expression => |
7567 | Make_Op_Multiply (Loc, | |
7568 | Left_Opnd => | |
7569 | Duplicate_Subexpr (Base), | |
7570 | Right_Opnd => | |
7571 | Duplicate_Subexpr_No_Checks (Base)))), | |
7572 | ||
70482933 RK |
7573 | Expression => |
7574 | Make_Op_Multiply (Loc, | |
e4494292 RD |
7575 | Left_Opnd => New_Occurrence_Of (Temp, Loc), |
7576 | Right_Opnd => New_Occurrence_Of (Temp, Loc))); | |
70482933 RK |
7577 | end if; |
7578 | ||
7579 | Rewrite (N, Xnode); | |
7580 | Analyze_And_Resolve (N, Typ); | |
7581 | return; | |
7582 | end if; | |
7583 | end if; | |
7584 | ||
7585 | -- Case of (2 ** expression) appearing as an argument of an integer | |
7586 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 7587 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
7588 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
7589 | -- of the higher level node converts it into a shift. | |
7590 | ||
51bf9bdf AC |
7591 | -- Another case is 2 ** N in any other context. We simply convert |
7592 | -- this to 1 * 2 ** N, and then the above transformation applies. | |
7593 | ||
685094bf RD |
7594 | -- Note: this transformation is not applicable for a modular type with |
7595 | -- a non-binary modulus in the multiplication case, since we get a wrong | |
7596 | -- result if the shift causes an overflow before the modular reduction. | |
7597 | ||
8b4230c8 AC |
7598 | -- Note: we used to check that Exptyp was an unsigned type. But that is |
7599 | -- an unnecessary check, since if Exp is negative, we have a run-time | |
7600 | -- error that is either caught (so we get the right result) or we have | |
7601 | -- suppressed the check, in which case the code is erroneous anyway. | |
7602 | ||
70482933 | 7603 | if Nkind (Base) = N_Integer_Literal |
6c3c671e AC |
7604 | and then CRT_Safe_Compile_Time_Known_Value (Base) |
7605 | and then Expr_Value (Base) = Uint_2 | |
70482933 RK |
7606 | and then Is_Integer_Type (Root_Type (Exptyp)) |
7607 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
70482933 | 7608 | and then not Ovflo |
70482933 | 7609 | then |
51bf9bdf | 7610 | -- First the multiply and divide cases |
70482933 | 7611 | |
51bf9bdf AC |
7612 | if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then |
7613 | declare | |
7614 | P : constant Node_Id := Parent (N); | |
7615 | L : constant Node_Id := Left_Opnd (P); | |
7616 | R : constant Node_Id := Right_Opnd (P); | |
7617 | ||
7618 | begin | |
7619 | if (Nkind (P) = N_Op_Multiply | |
7620 | and then not Non_Binary_Modulus (Typ) | |
7621 | and then | |
7622 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
7623 | or else | |
7624 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
7625 | and then not Do_Overflow_Check (P)) | |
7626 | or else | |
7627 | (Nkind (P) = N_Op_Divide | |
533369aa AC |
7628 | and then Is_Integer_Type (Etype (L)) |
7629 | and then Is_Unsigned_Type (Etype (L)) | |
7630 | and then R = N | |
7631 | and then not Do_Overflow_Check (P)) | |
51bf9bdf AC |
7632 | then |
7633 | Set_Is_Power_Of_2_For_Shift (N); | |
7634 | return; | |
7635 | end if; | |
7636 | end; | |
7637 | ||
7638 | -- Now the other cases | |
7639 | ||
7640 | elsif not Non_Binary_Modulus (Typ) then | |
7641 | Rewrite (N, | |
7642 | Make_Op_Multiply (Loc, | |
7643 | Left_Opnd => Make_Integer_Literal (Loc, 1), | |
7644 | Right_Opnd => Relocate_Node (N))); | |
7645 | Analyze_And_Resolve (N, Typ); | |
7646 | return; | |
7647 | end if; | |
70482933 RK |
7648 | end if; |
7649 | ||
07fc65c4 GB |
7650 | -- Fall through if exponentiation must be done using a runtime routine |
7651 | ||
07fc65c4 | 7652 | -- First deal with modular case |
70482933 RK |
7653 | |
7654 | if Is_Modular_Integer_Type (Rtyp) then | |
7655 | ||
7656 | -- Non-binary case, we call the special exponentiation routine for | |
7657 | -- the non-binary case, converting the argument to Long_Long_Integer | |
7658 | -- and passing the modulus value. Then the result is converted back | |
7659 | -- to the base type. | |
7660 | ||
7661 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
7662 | Rewrite (N, |
7663 | Convert_To (Typ, | |
7664 | Make_Function_Call (Loc, | |
cc6f5d75 AC |
7665 | Name => |
7666 | New_Occurrence_Of (RTE (RE_Exp_Modular), Loc), | |
70482933 | 7667 | Parameter_Associations => New_List ( |
e9daba51 | 7668 | Convert_To (RTE (RE_Unsigned), Base), |
70482933 RK |
7669 | Make_Integer_Literal (Loc, Modulus (Rtyp)), |
7670 | Exp)))); | |
7671 | ||
685094bf RD |
7672 | -- Binary case, in this case, we call one of two routines, either the |
7673 | -- unsigned integer case, or the unsigned long long integer case, | |
7674 | -- with a final "and" operation to do the required mod. | |
70482933 RK |
7675 | |
7676 | else | |
7677 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
7678 | Ent := RTE (RE_Exp_Unsigned); | |
7679 | else | |
7680 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
7681 | end if; | |
7682 | ||
7683 | Rewrite (N, | |
7684 | Convert_To (Typ, | |
7685 | Make_Op_And (Loc, | |
cc6f5d75 | 7686 | Left_Opnd => |
70482933 | 7687 | Make_Function_Call (Loc, |
cc6f5d75 | 7688 | Name => New_Occurrence_Of (Ent, Loc), |
70482933 RK |
7689 | Parameter_Associations => New_List ( |
7690 | Convert_To (Etype (First_Formal (Ent)), Base), | |
7691 | Exp)), | |
7692 | Right_Opnd => | |
7693 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
7694 | ||
7695 | end if; | |
7696 | ||
7697 | -- Common exit point for modular type case | |
7698 | ||
7699 | Analyze_And_Resolve (N, Typ); | |
7700 | return; | |
7701 | ||
fbf5a39b AC |
7702 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
7703 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
7704 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 7705 | -- might need certification when a certified run time is required. |
70482933 | 7706 | |
fbf5a39b | 7707 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
7708 | -- checks are required, and one when they are not required, since there |
7709 | -- is a real gain in omitting checks on many machines. | |
70482933 | 7710 | |
fbf5a39b AC |
7711 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
7712 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
761f7dcb AC |
7713 | and then |
7714 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
7715 | or else Rtyp = Universal_Integer | |
70482933 | 7716 | then |
fbf5a39b AC |
7717 | Etyp := Standard_Long_Long_Integer; |
7718 | ||
ebb6b0bd AC |
7719 | -- Overflow checking is the only choice on the AAMP target, where |
7720 | -- arithmetic instructions check overflow automatically, so only | |
7721 | -- one version of the exponentiation unit is needed. | |
7722 | ||
1037b0f4 | 7723 | if Ovflo or AAMP_On_Target then |
70482933 RK |
7724 | Rent := RE_Exp_Long_Long_Integer; |
7725 | else | |
7726 | Rent := RE_Exn_Long_Long_Integer; | |
7727 | end if; | |
7728 | ||
fbf5a39b AC |
7729 | elsif Is_Signed_Integer_Type (Rtyp) then |
7730 | Etyp := Standard_Integer; | |
70482933 | 7731 | |
ebb6b0bd AC |
7732 | -- Overflow checking is the only choice on the AAMP target, where |
7733 | -- arithmetic instructions check overflow automatically, so only | |
7734 | -- one version of the exponentiation unit is needed. | |
7735 | ||
1037b0f4 | 7736 | if Ovflo or AAMP_On_Target then |
fbf5a39b | 7737 | Rent := RE_Exp_Integer; |
70482933 | 7738 | else |
fbf5a39b | 7739 | Rent := RE_Exn_Integer; |
70482933 | 7740 | end if; |
fbf5a39b AC |
7741 | |
7742 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
7743 | -- need separate routines for the overflow case here, since in the case | |
7744 | -- of floating-point, we generate infinities anyway as a rule (either | |
7745 | -- that or we automatically trap overflow), and if there is an infinity | |
7746 | -- generated and a range check is required, the check will fail anyway. | |
7747 | ||
7748 | else | |
7749 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
7750 | Etyp := Standard_Long_Long_Float; | |
7751 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
7752 | end if; |
7753 | ||
7754 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 7755 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 7756 | |
fbf5a39b | 7757 | if Typ = Etyp |
70482933 RK |
7758 | and then Rtyp /= Universal_Integer |
7759 | and then Rtyp /= Universal_Real | |
7760 | then | |
7761 | Rewrite (N, | |
7762 | Make_Function_Call (Loc, | |
e4494292 | 7763 | Name => New_Occurrence_Of (RTE (Rent), Loc), |
70482933 RK |
7764 | Parameter_Associations => New_List (Base, Exp))); |
7765 | ||
7766 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 7767 | -- required in the universal cases, since the runtime routine is |
1147c704 | 7768 | -- typed using one of the standard types). |
70482933 RK |
7769 | |
7770 | else | |
7771 | Rewrite (N, | |
7772 | Convert_To (Typ, | |
7773 | Make_Function_Call (Loc, | |
e4494292 | 7774 | Name => New_Occurrence_Of (RTE (Rent), Loc), |
70482933 | 7775 | Parameter_Associations => New_List ( |
fbf5a39b | 7776 | Convert_To (Etyp, Base), |
70482933 RK |
7777 | Exp)))); |
7778 | end if; | |
7779 | ||
7780 | Analyze_And_Resolve (N, Typ); | |
7781 | return; | |
7782 | ||
fbf5a39b AC |
7783 | exception |
7784 | when RE_Not_Available => | |
7785 | return; | |
70482933 RK |
7786 | end Expand_N_Op_Expon; |
7787 | ||
7788 | -------------------- | |
7789 | -- Expand_N_Op_Ge -- | |
7790 | -------------------- | |
7791 | ||
7792 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
7793 | Typ : constant Entity_Id := Etype (N); | |
7794 | Op1 : constant Node_Id := Left_Opnd (N); | |
7795 | Op2 : constant Node_Id := Right_Opnd (N); | |
7796 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7797 | ||
7798 | begin | |
7799 | Binary_Op_Validity_Checks (N); | |
7800 | ||
456cbfa5 | 7801 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7802 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7803 | |
7804 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7805 | ||
7806 | if Nkind (N) /= N_Op_Ge then | |
7807 | return; | |
7808 | end if; | |
7809 | ||
7810 | -- Array type case | |
7811 | ||
f02b8bb8 | 7812 | if Is_Array_Type (Typ1) then |
70482933 RK |
7813 | Expand_Array_Comparison (N); |
7814 | return; | |
7815 | end if; | |
7816 | ||
456cbfa5 AC |
7817 | -- Deal with boolean operands |
7818 | ||
70482933 RK |
7819 | if Is_Boolean_Type (Typ1) then |
7820 | Adjust_Condition (Op1); | |
7821 | Adjust_Condition (Op2); | |
7822 | Set_Etype (N, Standard_Boolean); | |
7823 | Adjust_Result_Type (N, Typ); | |
7824 | end if; | |
7825 | ||
7826 | Rewrite_Comparison (N); | |
f02b8bb8 | 7827 | |
0580d807 | 7828 | Optimize_Length_Comparison (N); |
70482933 RK |
7829 | end Expand_N_Op_Ge; |
7830 | ||
7831 | -------------------- | |
7832 | -- Expand_N_Op_Gt -- | |
7833 | -------------------- | |
7834 | ||
7835 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
7836 | Typ : constant Entity_Id := Etype (N); | |
7837 | Op1 : constant Node_Id := Left_Opnd (N); | |
7838 | Op2 : constant Node_Id := Right_Opnd (N); | |
7839 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7840 | ||
7841 | begin | |
7842 | Binary_Op_Validity_Checks (N); | |
7843 | ||
456cbfa5 | 7844 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7845 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7846 | |
7847 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7848 | ||
7849 | if Nkind (N) /= N_Op_Gt then | |
7850 | return; | |
7851 | end if; | |
7852 | ||
7853 | -- Deal with array type operands | |
7854 | ||
f02b8bb8 | 7855 | if Is_Array_Type (Typ1) then |
70482933 RK |
7856 | Expand_Array_Comparison (N); |
7857 | return; | |
7858 | end if; | |
7859 | ||
456cbfa5 AC |
7860 | -- Deal with boolean type operands |
7861 | ||
70482933 RK |
7862 | if Is_Boolean_Type (Typ1) then |
7863 | Adjust_Condition (Op1); | |
7864 | Adjust_Condition (Op2); | |
7865 | Set_Etype (N, Standard_Boolean); | |
7866 | Adjust_Result_Type (N, Typ); | |
7867 | end if; | |
7868 | ||
7869 | Rewrite_Comparison (N); | |
f02b8bb8 | 7870 | |
0580d807 | 7871 | Optimize_Length_Comparison (N); |
70482933 RK |
7872 | end Expand_N_Op_Gt; |
7873 | ||
7874 | -------------------- | |
7875 | -- Expand_N_Op_Le -- | |
7876 | -------------------- | |
7877 | ||
7878 | procedure Expand_N_Op_Le (N : Node_Id) is | |
7879 | Typ : constant Entity_Id := Etype (N); | |
7880 | Op1 : constant Node_Id := Left_Opnd (N); | |
7881 | Op2 : constant Node_Id := Right_Opnd (N); | |
7882 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7883 | ||
7884 | begin | |
7885 | Binary_Op_Validity_Checks (N); | |
7886 | ||
456cbfa5 | 7887 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7888 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7889 | |
7890 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7891 | ||
7892 | if Nkind (N) /= N_Op_Le then | |
7893 | return; | |
7894 | end if; | |
7895 | ||
7896 | -- Deal with array type operands | |
7897 | ||
f02b8bb8 | 7898 | if Is_Array_Type (Typ1) then |
70482933 RK |
7899 | Expand_Array_Comparison (N); |
7900 | return; | |
7901 | end if; | |
7902 | ||
456cbfa5 AC |
7903 | -- Deal with Boolean type operands |
7904 | ||
70482933 RK |
7905 | if Is_Boolean_Type (Typ1) then |
7906 | Adjust_Condition (Op1); | |
7907 | Adjust_Condition (Op2); | |
7908 | Set_Etype (N, Standard_Boolean); | |
7909 | Adjust_Result_Type (N, Typ); | |
7910 | end if; | |
7911 | ||
7912 | Rewrite_Comparison (N); | |
f02b8bb8 | 7913 | |
0580d807 | 7914 | Optimize_Length_Comparison (N); |
70482933 RK |
7915 | end Expand_N_Op_Le; |
7916 | ||
7917 | -------------------- | |
7918 | -- Expand_N_Op_Lt -- | |
7919 | -------------------- | |
7920 | ||
7921 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
7922 | Typ : constant Entity_Id := Etype (N); | |
7923 | Op1 : constant Node_Id := Left_Opnd (N); | |
7924 | Op2 : constant Node_Id := Right_Opnd (N); | |
7925 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7926 | ||
7927 | begin | |
7928 | Binary_Op_Validity_Checks (N); | |
7929 | ||
456cbfa5 | 7930 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7931 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7932 | |
7933 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7934 | ||
7935 | if Nkind (N) /= N_Op_Lt then | |
7936 | return; | |
7937 | end if; | |
7938 | ||
7939 | -- Deal with array type operands | |
7940 | ||
f02b8bb8 | 7941 | if Is_Array_Type (Typ1) then |
70482933 RK |
7942 | Expand_Array_Comparison (N); |
7943 | return; | |
7944 | end if; | |
7945 | ||
456cbfa5 AC |
7946 | -- Deal with Boolean type operands |
7947 | ||
70482933 RK |
7948 | if Is_Boolean_Type (Typ1) then |
7949 | Adjust_Condition (Op1); | |
7950 | Adjust_Condition (Op2); | |
7951 | Set_Etype (N, Standard_Boolean); | |
7952 | Adjust_Result_Type (N, Typ); | |
7953 | end if; | |
7954 | ||
7955 | Rewrite_Comparison (N); | |
f02b8bb8 | 7956 | |
0580d807 | 7957 | Optimize_Length_Comparison (N); |
70482933 RK |
7958 | end Expand_N_Op_Lt; |
7959 | ||
7960 | ----------------------- | |
7961 | -- Expand_N_Op_Minus -- | |
7962 | ----------------------- | |
7963 | ||
7964 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
7965 | Loc : constant Source_Ptr := Sloc (N); | |
7966 | Typ : constant Entity_Id := Etype (N); | |
7967 | ||
7968 | begin | |
7969 | Unary_Op_Validity_Checks (N); | |
7970 | ||
b6b5cca8 AC |
7971 | -- Check for MINIMIZED/ELIMINATED overflow mode |
7972 | ||
7973 | if Minimized_Eliminated_Overflow_Check (N) then | |
7974 | Apply_Arithmetic_Overflow_Check (N); | |
7975 | return; | |
7976 | end if; | |
7977 | ||
07fc65c4 | 7978 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
7979 | and then Is_Signed_Integer_Type (Etype (N)) |
7980 | and then Do_Overflow_Check (N) | |
7981 | then | |
7982 | -- Software overflow checking expands -expr into (0 - expr) | |
7983 | ||
7984 | Rewrite (N, | |
7985 | Make_Op_Subtract (Loc, | |
7986 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
7987 | Right_Opnd => Right_Opnd (N))); | |
7988 | ||
7989 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
7990 | end if; |
7991 | end Expand_N_Op_Minus; | |
7992 | ||
7993 | --------------------- | |
7994 | -- Expand_N_Op_Mod -- | |
7995 | --------------------- | |
7996 | ||
7997 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
7998 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 7999 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
8000 | DDC : constant Boolean := Do_Division_Check (N); |
8001 | ||
b6b5cca8 AC |
8002 | Left : Node_Id; |
8003 | Right : Node_Id; | |
8004 | ||
70482933 RK |
8005 | LLB : Uint; |
8006 | Llo : Uint; | |
8007 | Lhi : Uint; | |
8008 | LOK : Boolean; | |
8009 | Rlo : Uint; | |
8010 | Rhi : Uint; | |
8011 | ROK : Boolean; | |
8012 | ||
1033834f RD |
8013 | pragma Warnings (Off, Lhi); |
8014 | ||
70482933 RK |
8015 | begin |
8016 | Binary_Op_Validity_Checks (N); | |
8017 | ||
b6b5cca8 AC |
8018 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8019 | ||
8020 | if Minimized_Eliminated_Overflow_Check (N) then | |
8021 | Apply_Arithmetic_Overflow_Check (N); | |
8022 | return; | |
8023 | end if; | |
8024 | ||
9a6dc470 RD |
8025 | if Is_Integer_Type (Etype (N)) then |
8026 | Apply_Divide_Checks (N); | |
b6b5cca8 AC |
8027 | |
8028 | -- All done if we don't have a MOD any more, which can happen as a | |
8029 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
8030 | ||
8031 | if Nkind (N) /= N_Op_Mod then | |
8032 | return; | |
8033 | end if; | |
9a6dc470 RD |
8034 | end if; |
8035 | ||
b6b5cca8 AC |
8036 | -- Proceed with expansion of mod operator |
8037 | ||
8038 | Left := Left_Opnd (N); | |
8039 | Right := Right_Opnd (N); | |
8040 | ||
5d5e9775 AC |
8041 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
8042 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); | |
70482933 | 8043 | |
2c9f8c0a AC |
8044 | -- Convert mod to rem if operands are both known to be non-negative, or |
8045 | -- both known to be non-positive (these are the cases in which rem and | |
8046 | -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite | |
8047 | -- likely that this will improve the quality of code, (the operation now | |
8048 | -- corresponds to the hardware remainder), and it does not seem likely | |
8049 | -- that it could be harmful. It also avoids some cases of the elaborate | |
8050 | -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %). | |
8051 | ||
8052 | if (LOK and ROK) | |
8053 | and then ((Llo >= 0 and then Rlo >= 0) | |
cc6f5d75 | 8054 | or else |
2c9f8c0a AC |
8055 | (Lhi <= 0 and then Rhi <= 0)) |
8056 | then | |
70482933 RK |
8057 | Rewrite (N, |
8058 | Make_Op_Rem (Sloc (N), | |
8059 | Left_Opnd => Left_Opnd (N), | |
8060 | Right_Opnd => Right_Opnd (N))); | |
8061 | ||
685094bf RD |
8062 | -- Instead of reanalyzing the node we do the analysis manually. This |
8063 | -- avoids anomalies when the replacement is done in an instance and | |
8064 | -- is epsilon more efficient. | |
70482933 RK |
8065 | |
8066 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 8067 | Set_Etype (N, Typ); |
70482933 RK |
8068 | Set_Do_Division_Check (N, DDC); |
8069 | Expand_N_Op_Rem (N); | |
8070 | Set_Analyzed (N); | |
2c9f8c0a | 8071 | return; |
70482933 RK |
8072 | |
8073 | -- Otherwise, normal mod processing | |
8074 | ||
8075 | else | |
fbf5a39b AC |
8076 | -- Apply optimization x mod 1 = 0. We don't really need that with |
8077 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
8078 | -- certainly harmless. | |
8079 | ||
8080 | if Is_Integer_Type (Etype (N)) | |
8081 | and then Compile_Time_Known_Value (Right) | |
8082 | and then Expr_Value (Right) = Uint_1 | |
8083 | then | |
abcbd24c ST |
8084 | -- Call Remove_Side_Effects to ensure that any side effects in |
8085 | -- the ignored left operand (in particular function calls to | |
8086 | -- user defined functions) are properly executed. | |
8087 | ||
8088 | Remove_Side_Effects (Left); | |
8089 | ||
fbf5a39b AC |
8090 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
8091 | Analyze_And_Resolve (N, Typ); | |
8092 | return; | |
8093 | end if; | |
8094 | ||
2c9f8c0a AC |
8095 | -- If we still have a mod operator and we are in Modify_Tree_For_C |
8096 | -- mode, and we have a signed integer type, then here is where we do | |
8097 | -- the rewrite in terms of Rem. Note this rewrite bypasses the need | |
8098 | -- for the special handling of the annoying case of largest negative | |
8099 | -- number mod minus one. | |
8100 | ||
8101 | if Nkind (N) = N_Op_Mod | |
8102 | and then Is_Signed_Integer_Type (Typ) | |
8103 | and then Modify_Tree_For_C | |
8104 | then | |
8105 | -- In the general case, we expand A mod B as | |
8106 | ||
8107 | -- Tnn : constant typ := A rem B; | |
8108 | -- .. | |
8109 | -- (if (A >= 0) = (B >= 0) then Tnn | |
8110 | -- elsif Tnn = 0 then 0 | |
8111 | -- else Tnn + B) | |
8112 | ||
8113 | -- The comparison can be written simply as A >= 0 if we know that | |
8114 | -- B >= 0 which is a very common case. | |
8115 | ||
8116 | -- An important optimization is when B is known at compile time | |
8117 | -- to be 2**K for some constant. In this case we can simply AND | |
8118 | -- the left operand with the bit string 2**K-1 (i.e. K 1-bits) | |
8119 | -- and that works for both the positive and negative cases. | |
8120 | ||
8121 | declare | |
8122 | P2 : constant Nat := Power_Of_Two (Right); | |
8123 | ||
8124 | begin | |
8125 | if P2 /= 0 then | |
8126 | Rewrite (N, | |
8127 | Unchecked_Convert_To (Typ, | |
8128 | Make_Op_And (Loc, | |
8129 | Left_Opnd => | |
8130 | Unchecked_Convert_To | |
8131 | (Corresponding_Unsigned_Type (Typ), Left), | |
8132 | Right_Opnd => | |
8133 | Make_Integer_Literal (Loc, 2 ** P2 - 1)))); | |
8134 | Analyze_And_Resolve (N, Typ); | |
8135 | return; | |
8136 | end if; | |
8137 | end; | |
8138 | ||
8139 | -- Here for the full rewrite | |
8140 | ||
8141 | declare | |
8142 | Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N); | |
8143 | Cmp : Node_Id; | |
8144 | ||
8145 | begin | |
8146 | Cmp := | |
8147 | Make_Op_Ge (Loc, | |
8148 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
8149 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
8150 | ||
8151 | if not LOK or else Rlo < 0 then | |
8152 | Cmp := | |
8153 | Make_Op_Eq (Loc, | |
8154 | Left_Opnd => Cmp, | |
8155 | Right_Opnd => | |
8156 | Make_Op_Ge (Loc, | |
8157 | Left_Opnd => Duplicate_Subexpr_No_Checks (Right), | |
8158 | Right_Opnd => Make_Integer_Literal (Loc, 0))); | |
8159 | end if; | |
8160 | ||
8161 | Insert_Action (N, | |
8162 | Make_Object_Declaration (Loc, | |
8163 | Defining_Identifier => Tnn, | |
8164 | Constant_Present => True, | |
8165 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
8166 | Expression => | |
8167 | Make_Op_Rem (Loc, | |
8168 | Left_Opnd => Left, | |
8169 | Right_Opnd => Right))); | |
8170 | ||
8171 | Rewrite (N, | |
8172 | Make_If_Expression (Loc, | |
8173 | Expressions => New_List ( | |
8174 | Cmp, | |
8175 | New_Occurrence_Of (Tnn, Loc), | |
8176 | Make_If_Expression (Loc, | |
8177 | Is_Elsif => True, | |
8178 | Expressions => New_List ( | |
8179 | Make_Op_Eq (Loc, | |
8180 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8181 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
8182 | Make_Integer_Literal (Loc, 0), | |
8183 | Make_Op_Add (Loc, | |
8184 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8185 | Right_Opnd => | |
8186 | Duplicate_Subexpr_No_Checks (Right))))))); | |
8187 | ||
8188 | Analyze_And_Resolve (N, Typ); | |
8189 | return; | |
8190 | end; | |
8191 | end if; | |
8192 | ||
8193 | -- Deal with annoying case of largest negative number mod minus one. | |
8194 | -- Gigi may not handle this case correctly, because on some targets, | |
8195 | -- the mod value is computed using a divide instruction which gives | |
8196 | -- an overflow trap for this case. | |
b9daa96e AC |
8197 | |
8198 | -- It would be a bit more efficient to figure out which targets | |
8199 | -- this is really needed for, but in practice it is reasonable | |
8200 | -- to do the following special check in all cases, since it means | |
8201 | -- we get a clearer message, and also the overhead is minimal given | |
8202 | -- that division is expensive in any case. | |
70482933 | 8203 | |
685094bf RD |
8204 | -- In fact the check is quite easy, if the right operand is -1, then |
8205 | -- the mod value is always 0, and we can just ignore the left operand | |
8206 | -- completely in this case. | |
70482933 | 8207 | |
9a6dc470 RD |
8208 | -- This only applies if we still have a mod operator. Skip if we |
8209 | -- have already rewritten this (e.g. in the case of eliminated | |
8210 | -- overflow checks which have driven us into bignum mode). | |
fbf5a39b | 8211 | |
9a6dc470 | 8212 | if Nkind (N) = N_Op_Mod then |
70482933 | 8213 | |
9a6dc470 RD |
8214 | -- The operand type may be private (e.g. in the expansion of an |
8215 | -- intrinsic operation) so we must use the underlying type to get | |
8216 | -- the bounds, and convert the literals explicitly. | |
70482933 | 8217 | |
9a6dc470 RD |
8218 | LLB := |
8219 | Expr_Value | |
8220 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
8221 | ||
8222 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
761f7dcb | 8223 | and then ((not LOK) or else (Llo = LLB)) |
9a6dc470 RD |
8224 | then |
8225 | Rewrite (N, | |
9b16cb57 | 8226 | Make_If_Expression (Loc, |
9a6dc470 RD |
8227 | Expressions => New_List ( |
8228 | Make_Op_Eq (Loc, | |
8229 | Left_Opnd => Duplicate_Subexpr (Right), | |
8230 | Right_Opnd => | |
8231 | Unchecked_Convert_To (Typ, | |
8232 | Make_Integer_Literal (Loc, -1))), | |
8233 | Unchecked_Convert_To (Typ, | |
8234 | Make_Integer_Literal (Loc, Uint_0)), | |
8235 | Relocate_Node (N)))); | |
8236 | ||
8237 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
8238 | Analyze_And_Resolve (N, Typ); | |
8239 | end if; | |
70482933 RK |
8240 | end if; |
8241 | end if; | |
8242 | end Expand_N_Op_Mod; | |
8243 | ||
8244 | -------------------------- | |
8245 | -- Expand_N_Op_Multiply -- | |
8246 | -------------------------- | |
8247 | ||
8248 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
8249 | Loc : constant Source_Ptr := Sloc (N); |
8250 | Lop : constant Node_Id := Left_Opnd (N); | |
8251 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 8252 | |
abcbd24c | 8253 | Lp2 : constant Boolean := |
533369aa | 8254 | Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop); |
abcbd24c | 8255 | Rp2 : constant Boolean := |
533369aa | 8256 | Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop); |
fbf5a39b | 8257 | |
70482933 RK |
8258 | Ltyp : constant Entity_Id := Etype (Lop); |
8259 | Rtyp : constant Entity_Id := Etype (Rop); | |
8260 | Typ : Entity_Id := Etype (N); | |
8261 | ||
8262 | begin | |
8263 | Binary_Op_Validity_Checks (N); | |
8264 | ||
b6b5cca8 AC |
8265 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8266 | ||
8267 | if Minimized_Eliminated_Overflow_Check (N) then | |
8268 | Apply_Arithmetic_Overflow_Check (N); | |
8269 | return; | |
8270 | end if; | |
8271 | ||
70482933 RK |
8272 | -- Special optimizations for integer types |
8273 | ||
8274 | if Is_Integer_Type (Typ) then | |
8275 | ||
abcbd24c | 8276 | -- N * 0 = 0 for integer types |
70482933 | 8277 | |
abcbd24c ST |
8278 | if Compile_Time_Known_Value (Rop) |
8279 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 8280 | then |
abcbd24c ST |
8281 | -- Call Remove_Side_Effects to ensure that any side effects in |
8282 | -- the ignored left operand (in particular function calls to | |
8283 | -- user defined functions) are properly executed. | |
8284 | ||
8285 | Remove_Side_Effects (Lop); | |
8286 | ||
8287 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
8288 | Analyze_And_Resolve (N, Typ); | |
8289 | return; | |
8290 | end if; | |
8291 | ||
8292 | -- Similar handling for 0 * N = 0 | |
8293 | ||
8294 | if Compile_Time_Known_Value (Lop) | |
8295 | and then Expr_Value (Lop) = Uint_0 | |
8296 | then | |
8297 | Remove_Side_Effects (Rop); | |
70482933 RK |
8298 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
8299 | Analyze_And_Resolve (N, Typ); | |
8300 | return; | |
8301 | end if; | |
8302 | ||
8303 | -- N * 1 = 1 * N = N for integer types | |
8304 | ||
fbf5a39b AC |
8305 | -- This optimisation is not done if we are going to |
8306 | -- rewrite the product 1 * 2 ** N to a shift. | |
8307 | ||
8308 | if Compile_Time_Known_Value (Rop) | |
8309 | and then Expr_Value (Rop) = Uint_1 | |
8310 | and then not Lp2 | |
70482933 | 8311 | then |
fbf5a39b | 8312 | Rewrite (N, Lop); |
70482933 RK |
8313 | return; |
8314 | ||
fbf5a39b AC |
8315 | elsif Compile_Time_Known_Value (Lop) |
8316 | and then Expr_Value (Lop) = Uint_1 | |
8317 | and then not Rp2 | |
70482933 | 8318 | then |
fbf5a39b | 8319 | Rewrite (N, Rop); |
70482933 RK |
8320 | return; |
8321 | end if; | |
8322 | end if; | |
8323 | ||
70482933 RK |
8324 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
8325 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
8326 | -- operand is an integer, as required for this to work. | |
8327 | ||
fbf5a39b AC |
8328 | if Rp2 then |
8329 | if Lp2 then | |
70482933 | 8330 | |
fbf5a39b | 8331 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
8332 | |
8333 | Rewrite (N, | |
8334 | Make_Op_Expon (Loc, | |
8335 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
8336 | Right_Opnd => | |
8337 | Make_Op_Add (Loc, | |
8338 | Left_Opnd => Right_Opnd (Lop), | |
8339 | Right_Opnd => Right_Opnd (Rop)))); | |
8340 | Analyze_And_Resolve (N, Typ); | |
8341 | return; | |
8342 | ||
8343 | else | |
eefe3761 AC |
8344 | -- If the result is modular, perform the reduction of the result |
8345 | -- appropriately. | |
8346 | ||
8347 | if Is_Modular_Integer_Type (Typ) | |
8348 | and then not Non_Binary_Modulus (Typ) | |
8349 | then | |
8350 | Rewrite (N, | |
573e5dd6 RD |
8351 | Make_Op_And (Loc, |
8352 | Left_Opnd => | |
8353 | Make_Op_Shift_Left (Loc, | |
8354 | Left_Opnd => Lop, | |
8355 | Right_Opnd => | |
8356 | Convert_To (Standard_Natural, Right_Opnd (Rop))), | |
8357 | Right_Opnd => | |
eefe3761 | 8358 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); |
573e5dd6 | 8359 | |
eefe3761 AC |
8360 | else |
8361 | Rewrite (N, | |
8362 | Make_Op_Shift_Left (Loc, | |
8363 | Left_Opnd => Lop, | |
8364 | Right_Opnd => | |
8365 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
8366 | end if; | |
8367 | ||
70482933 RK |
8368 | Analyze_And_Resolve (N, Typ); |
8369 | return; | |
8370 | end if; | |
8371 | ||
8372 | -- Same processing for the operands the other way round | |
8373 | ||
fbf5a39b | 8374 | elsif Lp2 then |
eefe3761 AC |
8375 | if Is_Modular_Integer_Type (Typ) |
8376 | and then not Non_Binary_Modulus (Typ) | |
8377 | then | |
8378 | Rewrite (N, | |
573e5dd6 RD |
8379 | Make_Op_And (Loc, |
8380 | Left_Opnd => | |
8381 | Make_Op_Shift_Left (Loc, | |
8382 | Left_Opnd => Rop, | |
8383 | Right_Opnd => | |
8384 | Convert_To (Standard_Natural, Right_Opnd (Lop))), | |
8385 | Right_Opnd => | |
8386 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); | |
8387 | ||
eefe3761 AC |
8388 | else |
8389 | Rewrite (N, | |
8390 | Make_Op_Shift_Left (Loc, | |
8391 | Left_Opnd => Rop, | |
8392 | Right_Opnd => | |
8393 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
8394 | end if; | |
8395 | ||
70482933 RK |
8396 | Analyze_And_Resolve (N, Typ); |
8397 | return; | |
8398 | end if; | |
8399 | ||
8400 | -- Do required fixup of universal fixed operation | |
8401 | ||
8402 | if Typ = Universal_Fixed then | |
8403 | Fixup_Universal_Fixed_Operation (N); | |
8404 | Typ := Etype (N); | |
8405 | end if; | |
8406 | ||
8407 | -- Multiplications with fixed-point results | |
8408 | ||
8409 | if Is_Fixed_Point_Type (Typ) then | |
8410 | ||
685094bf RD |
8411 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
8412 | -- a semantic point of view such operations are simply integer | |
8413 | -- operations and will be treated that way. | |
70482933 RK |
8414 | |
8415 | if not Treat_Fixed_As_Integer (N) then | |
8416 | ||
8417 | -- Case of fixed * integer => fixed | |
8418 | ||
8419 | if Is_Integer_Type (Rtyp) then | |
8420 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
8421 | ||
8422 | -- Case of integer * fixed => fixed | |
8423 | ||
8424 | elsif Is_Integer_Type (Ltyp) then | |
8425 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
8426 | ||
8427 | -- Case of fixed * fixed => fixed | |
8428 | ||
8429 | else | |
8430 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
8431 | end if; | |
8432 | end if; | |
8433 | ||
685094bf RD |
8434 | -- Other cases of multiplication of fixed-point operands. Again we |
8435 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
8436 | |
8437 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
8438 | and then not Treat_Fixed_As_Integer (N) | |
8439 | then | |
8440 | if Is_Integer_Type (Typ) then | |
8441 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
8442 | else | |
8443 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
8444 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
8445 | end if; | |
8446 | ||
685094bf RD |
8447 | -- Mixed-mode operations can appear in a non-static universal context, |
8448 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 8449 | |
533369aa | 8450 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
70482933 | 8451 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); |
70482933 RK |
8452 | Analyze_And_Resolve (Rop, Universal_Real); |
8453 | ||
533369aa | 8454 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
70482933 | 8455 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); |
70482933 RK |
8456 | Analyze_And_Resolve (Lop, Universal_Real); |
8457 | ||
8458 | -- Non-fixed point cases, check software overflow checking required | |
8459 | ||
8460 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
8461 | Apply_Arithmetic_Overflow_Check (N); | |
8462 | end if; | |
8463 | end Expand_N_Op_Multiply; | |
8464 | ||
8465 | -------------------- | |
8466 | -- Expand_N_Op_Ne -- | |
8467 | -------------------- | |
8468 | ||
70482933 | 8469 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 8470 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
8471 | |
8472 | begin | |
f02b8bb8 | 8473 | -- Case of elementary type with standard operator |
70482933 | 8474 | |
f02b8bb8 RD |
8475 | if Is_Elementary_Type (Typ) |
8476 | and then Sloc (Entity (N)) = Standard_Location | |
8477 | then | |
8478 | Binary_Op_Validity_Checks (N); | |
70482933 | 8479 | |
456cbfa5 | 8480 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if |
60b68e56 | 8481 | -- means we no longer have a /= operation, we are all done. |
456cbfa5 AC |
8482 | |
8483 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
8484 | ||
8485 | if Nkind (N) /= N_Op_Ne then | |
8486 | return; | |
8487 | end if; | |
8488 | ||
f02b8bb8 | 8489 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 8490 | |
f02b8bb8 RD |
8491 | if Is_Boolean_Type (Typ) then |
8492 | Adjust_Condition (Left_Opnd (N)); | |
8493 | Adjust_Condition (Right_Opnd (N)); | |
8494 | Set_Etype (N, Standard_Boolean); | |
8495 | Adjust_Result_Type (N, Typ); | |
8496 | end if; | |
fbf5a39b | 8497 | |
f02b8bb8 RD |
8498 | Rewrite_Comparison (N); |
8499 | ||
f02b8bb8 RD |
8500 | -- For all cases other than elementary types, we rewrite node as the |
8501 | -- negation of an equality operation, and reanalyze. The equality to be | |
8502 | -- used is defined in the same scope and has the same signature. This | |
8503 | -- signature must be set explicitly since in an instance it may not have | |
8504 | -- the same visibility as in the generic unit. This avoids duplicating | |
8505 | -- or factoring the complex code for record/array equality tests etc. | |
8506 | ||
8507 | else | |
8508 | declare | |
8509 | Loc : constant Source_Ptr := Sloc (N); | |
8510 | Neg : Node_Id; | |
8511 | Ne : constant Entity_Id := Entity (N); | |
8512 | ||
8513 | begin | |
8514 | Binary_Op_Validity_Checks (N); | |
8515 | ||
8516 | Neg := | |
8517 | Make_Op_Not (Loc, | |
8518 | Right_Opnd => | |
8519 | Make_Op_Eq (Loc, | |
8520 | Left_Opnd => Left_Opnd (N), | |
8521 | Right_Opnd => Right_Opnd (N))); | |
8522 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
8523 | ||
8524 | if Scope (Ne) /= Standard_Standard then | |
8525 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
8526 | end if; | |
8527 | ||
4637729f | 8528 | -- For navigation purposes, we want to treat the inequality as an |
f02b8bb8 | 8529 | -- implicit reference to the corresponding equality. Preserve the |
4637729f | 8530 | -- Comes_From_ source flag to generate proper Xref entries. |
f02b8bb8 RD |
8531 | |
8532 | Preserve_Comes_From_Source (Neg, N); | |
8533 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
8534 | Rewrite (N, Neg); | |
8535 | Analyze_And_Resolve (N, Standard_Boolean); | |
8536 | end; | |
8537 | end if; | |
0580d807 AC |
8538 | |
8539 | Optimize_Length_Comparison (N); | |
70482933 RK |
8540 | end Expand_N_Op_Ne; |
8541 | ||
8542 | --------------------- | |
8543 | -- Expand_N_Op_Not -- | |
8544 | --------------------- | |
8545 | ||
685094bf | 8546 | -- If the argument is other than a Boolean array type, there is no special |
7a5b62b0 AC |
8547 | -- expansion required, except for dealing with validity checks, and non- |
8548 | -- standard boolean representations. | |
70482933 | 8549 | |
7a5b62b0 AC |
8550 | -- For the packed array case, we call the special routine in Exp_Pakd, |
8551 | -- except that if the component size is greater than one, we use the | |
8552 | -- standard routine generating a gruesome loop (it is so peculiar to have | |
8553 | -- packed arrays with non-standard Boolean representations anyway, so it | |
8554 | -- does not matter that we do not handle this case efficiently). | |
70482933 | 8555 | |
7a5b62b0 AC |
8556 | -- For the unpacked array case (and for the special packed case where we |
8557 | -- have non standard Booleans, as discussed above), we generate and insert | |
8558 | -- into the tree the following function definition: | |
70482933 RK |
8559 | |
8560 | -- function Nnnn (A : arr) is | |
8561 | -- B : arr; | |
8562 | -- begin | |
8563 | -- for J in a'range loop | |
8564 | -- B (J) := not A (J); | |
8565 | -- end loop; | |
8566 | -- return B; | |
8567 | -- end Nnnn; | |
8568 | ||
8569 | -- Here arr is the actual subtype of the parameter (and hence always | |
8570 | -- constrained). Then we replace the not with a call to this function. | |
8571 | ||
8572 | procedure Expand_N_Op_Not (N : Node_Id) is | |
8573 | Loc : constant Source_Ptr := Sloc (N); | |
8574 | Typ : constant Entity_Id := Etype (N); | |
8575 | Opnd : Node_Id; | |
8576 | Arr : Entity_Id; | |
8577 | A : Entity_Id; | |
8578 | B : Entity_Id; | |
8579 | J : Entity_Id; | |
8580 | A_J : Node_Id; | |
8581 | B_J : Node_Id; | |
8582 | ||
8583 | Func_Name : Entity_Id; | |
8584 | Loop_Statement : Node_Id; | |
8585 | ||
8586 | begin | |
8587 | Unary_Op_Validity_Checks (N); | |
8588 | ||
8589 | -- For boolean operand, deal with non-standard booleans | |
8590 | ||
8591 | if Is_Boolean_Type (Typ) then | |
8592 | Adjust_Condition (Right_Opnd (N)); | |
8593 | Set_Etype (N, Standard_Boolean); | |
8594 | Adjust_Result_Type (N, Typ); | |
8595 | return; | |
8596 | end if; | |
8597 | ||
da94696d | 8598 | -- Only array types need any other processing |
70482933 | 8599 | |
da94696d | 8600 | if not Is_Array_Type (Typ) then |
70482933 RK |
8601 | return; |
8602 | end if; | |
8603 | ||
a9d8907c JM |
8604 | -- Case of array operand. If bit packed with a component size of 1, |
8605 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 8606 | |
a9d8907c JM |
8607 | if Is_Bit_Packed_Array (Typ) |
8608 | and then Component_Size (Typ) = 1 | |
8609 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
8610 | then | |
70482933 RK |
8611 | Expand_Packed_Not (N); |
8612 | return; | |
8613 | end if; | |
8614 | ||
fbf5a39b AC |
8615 | -- Case of array operand which is not bit-packed. If the context is |
8616 | -- a safe assignment, call in-place operation, If context is a larger | |
8617 | -- boolean expression in the context of a safe assignment, expansion is | |
8618 | -- done by enclosing operation. | |
70482933 RK |
8619 | |
8620 | Opnd := Relocate_Node (Right_Opnd (N)); | |
8621 | Convert_To_Actual_Subtype (Opnd); | |
8622 | Arr := Etype (Opnd); | |
8623 | Ensure_Defined (Arr, N); | |
b4592168 | 8624 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 8625 | |
fbf5a39b AC |
8626 | if Nkind (Parent (N)) = N_Assignment_Statement then |
8627 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
8628 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
8629 | return; | |
8630 | ||
5e1c00fa | 8631 | -- Special case the negation of a binary operation |
fbf5a39b | 8632 | |
303b4d58 | 8633 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 8634 | and then Safe_In_Place_Array_Op |
303b4d58 | 8635 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
8636 | then |
8637 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
8638 | return; | |
8639 | end if; | |
8640 | ||
8641 | elsif Nkind (Parent (N)) in N_Binary_Op | |
8642 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
8643 | then | |
8644 | declare | |
8645 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
8646 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
8647 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
8648 | ||
8649 | begin | |
8650 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
fbf5a39b | 8651 | |
aa9a7dd7 AC |
8652 | -- (not A) op (not B) can be reduced to a single call |
8653 | ||
8654 | if N = Op1 and then Nkind (Op2) = N_Op_Not then | |
fbf5a39b AC |
8655 | return; |
8656 | ||
bed8af19 AC |
8657 | elsif N = Op2 and then Nkind (Op1) = N_Op_Not then |
8658 | return; | |
8659 | ||
aa9a7dd7 | 8660 | -- A xor (not B) can also be special-cased |
fbf5a39b | 8661 | |
aa9a7dd7 | 8662 | elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then |
fbf5a39b AC |
8663 | return; |
8664 | end if; | |
8665 | end if; | |
8666 | end; | |
8667 | end if; | |
8668 | ||
70482933 RK |
8669 | A := Make_Defining_Identifier (Loc, Name_uA); |
8670 | B := Make_Defining_Identifier (Loc, Name_uB); | |
8671 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
8672 | ||
8673 | A_J := | |
8674 | Make_Indexed_Component (Loc, | |
e4494292 RD |
8675 | Prefix => New_Occurrence_Of (A, Loc), |
8676 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
8677 | |
8678 | B_J := | |
8679 | Make_Indexed_Component (Loc, | |
e4494292 RD |
8680 | Prefix => New_Occurrence_Of (B, Loc), |
8681 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
8682 | |
8683 | Loop_Statement := | |
8684 | Make_Implicit_Loop_Statement (N, | |
8685 | Identifier => Empty, | |
8686 | ||
8687 | Iteration_Scheme => | |
8688 | Make_Iteration_Scheme (Loc, | |
8689 | Loop_Parameter_Specification => | |
8690 | Make_Loop_Parameter_Specification (Loc, | |
0d901290 | 8691 | Defining_Identifier => J, |
70482933 RK |
8692 | Discrete_Subtype_Definition => |
8693 | Make_Attribute_Reference (Loc, | |
0d901290 | 8694 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
8695 | Attribute_Name => Name_Range))), |
8696 | ||
8697 | Statements => New_List ( | |
8698 | Make_Assignment_Statement (Loc, | |
8699 | Name => B_J, | |
8700 | Expression => Make_Op_Not (Loc, A_J)))); | |
8701 | ||
191fcb3a | 8702 | Func_Name := Make_Temporary (Loc, 'N'); |
70482933 RK |
8703 | Set_Is_Inlined (Func_Name); |
8704 | ||
8705 | Insert_Action (N, | |
8706 | Make_Subprogram_Body (Loc, | |
8707 | Specification => | |
8708 | Make_Function_Specification (Loc, | |
8709 | Defining_Unit_Name => Func_Name, | |
8710 | Parameter_Specifications => New_List ( | |
8711 | Make_Parameter_Specification (Loc, | |
8712 | Defining_Identifier => A, | |
e4494292 RD |
8713 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), |
8714 | Result_Definition => New_Occurrence_Of (Typ, Loc)), | |
70482933 RK |
8715 | |
8716 | Declarations => New_List ( | |
8717 | Make_Object_Declaration (Loc, | |
8718 | Defining_Identifier => B, | |
e4494292 | 8719 | Object_Definition => New_Occurrence_Of (Arr, Loc))), |
70482933 RK |
8720 | |
8721 | Handled_Statement_Sequence => | |
8722 | Make_Handled_Sequence_Of_Statements (Loc, | |
8723 | Statements => New_List ( | |
8724 | Loop_Statement, | |
d766cee3 | 8725 | Make_Simple_Return_Statement (Loc, |
0d901290 | 8726 | Expression => Make_Identifier (Loc, Chars (B))))))); |
70482933 RK |
8727 | |
8728 | Rewrite (N, | |
8729 | Make_Function_Call (Loc, | |
e4494292 | 8730 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
8731 | Parameter_Associations => New_List (Opnd))); |
8732 | ||
8733 | Analyze_And_Resolve (N, Typ); | |
8734 | end Expand_N_Op_Not; | |
8735 | ||
8736 | -------------------- | |
8737 | -- Expand_N_Op_Or -- | |
8738 | -------------------- | |
8739 | ||
8740 | procedure Expand_N_Op_Or (N : Node_Id) is | |
8741 | Typ : constant Entity_Id := Etype (N); | |
8742 | ||
8743 | begin | |
8744 | Binary_Op_Validity_Checks (N); | |
8745 | ||
8746 | if Is_Array_Type (Etype (N)) then | |
8747 | Expand_Boolean_Operator (N); | |
8748 | ||
8749 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
8750 | Adjust_Condition (Left_Opnd (N)); |
8751 | Adjust_Condition (Right_Opnd (N)); | |
8752 | Set_Etype (N, Standard_Boolean); | |
8753 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
8754 | |
8755 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
8756 | Expand_Intrinsic_Call (N, Entity (N)); | |
8757 | ||
70482933 RK |
8758 | end if; |
8759 | end Expand_N_Op_Or; | |
8760 | ||
8761 | ---------------------- | |
8762 | -- Expand_N_Op_Plus -- | |
8763 | ---------------------- | |
8764 | ||
8765 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
8766 | begin | |
8767 | Unary_Op_Validity_Checks (N); | |
b6b5cca8 AC |
8768 | |
8769 | -- Check for MINIMIZED/ELIMINATED overflow mode | |
8770 | ||
8771 | if Minimized_Eliminated_Overflow_Check (N) then | |
8772 | Apply_Arithmetic_Overflow_Check (N); | |
8773 | return; | |
8774 | end if; | |
70482933 RK |
8775 | end Expand_N_Op_Plus; |
8776 | ||
8777 | --------------------- | |
8778 | -- Expand_N_Op_Rem -- | |
8779 | --------------------- | |
8780 | ||
8781 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
8782 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 8783 | Typ : constant Entity_Id := Etype (N); |
70482933 | 8784 | |
b6b5cca8 AC |
8785 | Left : Node_Id; |
8786 | Right : Node_Id; | |
70482933 | 8787 | |
5d5e9775 AC |
8788 | Lo : Uint; |
8789 | Hi : Uint; | |
8790 | OK : Boolean; | |
70482933 | 8791 | |
5d5e9775 AC |
8792 | Lneg : Boolean; |
8793 | Rneg : Boolean; | |
8794 | -- Set if corresponding operand can be negative | |
8795 | ||
8796 | pragma Unreferenced (Hi); | |
1033834f | 8797 | |
70482933 RK |
8798 | begin |
8799 | Binary_Op_Validity_Checks (N); | |
8800 | ||
b6b5cca8 AC |
8801 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8802 | ||
8803 | if Minimized_Eliminated_Overflow_Check (N) then | |
8804 | Apply_Arithmetic_Overflow_Check (N); | |
8805 | return; | |
8806 | end if; | |
8807 | ||
70482933 | 8808 | if Is_Integer_Type (Etype (N)) then |
a91e9ac7 | 8809 | Apply_Divide_Checks (N); |
b6b5cca8 AC |
8810 | |
8811 | -- All done if we don't have a REM any more, which can happen as a | |
8812 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
8813 | ||
8814 | if Nkind (N) /= N_Op_Rem then | |
8815 | return; | |
8816 | end if; | |
70482933 RK |
8817 | end if; |
8818 | ||
b6b5cca8 AC |
8819 | -- Proceed with expansion of REM |
8820 | ||
8821 | Left := Left_Opnd (N); | |
8822 | Right := Right_Opnd (N); | |
8823 | ||
685094bf RD |
8824 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
8825 | -- but it is useful with other back ends (e.g. AAMP), and is certainly | |
8826 | -- harmless. | |
fbf5a39b AC |
8827 | |
8828 | if Is_Integer_Type (Etype (N)) | |
8829 | and then Compile_Time_Known_Value (Right) | |
8830 | and then Expr_Value (Right) = Uint_1 | |
8831 | then | |
abcbd24c ST |
8832 | -- Call Remove_Side_Effects to ensure that any side effects in the |
8833 | -- ignored left operand (in particular function calls to user defined | |
8834 | -- functions) are properly executed. | |
8835 | ||
8836 | Remove_Side_Effects (Left); | |
8837 | ||
fbf5a39b AC |
8838 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
8839 | Analyze_And_Resolve (N, Typ); | |
8840 | return; | |
8841 | end if; | |
8842 | ||
685094bf | 8843 | -- Deal with annoying case of largest negative number remainder minus |
b9daa96e AC |
8844 | -- one. Gigi may not handle this case correctly, because on some |
8845 | -- targets, the mod value is computed using a divide instruction | |
8846 | -- which gives an overflow trap for this case. | |
8847 | ||
8848 | -- It would be a bit more efficient to figure out which targets this | |
8849 | -- is really needed for, but in practice it is reasonable to do the | |
8850 | -- following special check in all cases, since it means we get a clearer | |
8851 | -- message, and also the overhead is minimal given that division is | |
8852 | -- expensive in any case. | |
70482933 | 8853 | |
685094bf RD |
8854 | -- In fact the check is quite easy, if the right operand is -1, then |
8855 | -- the remainder is always 0, and we can just ignore the left operand | |
8856 | -- completely in this case. | |
70482933 | 8857 | |
5d5e9775 AC |
8858 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); |
8859 | Lneg := (not OK) or else Lo < 0; | |
fbf5a39b | 8860 | |
5d5e9775 AC |
8861 | Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True); |
8862 | Rneg := (not OK) or else Lo < 0; | |
fbf5a39b | 8863 | |
5d5e9775 AC |
8864 | -- We won't mess with trying to find out if the left operand can really |
8865 | -- be the largest negative number (that's a pain in the case of private | |
8866 | -- types and this is really marginal). We will just assume that we need | |
8867 | -- the test if the left operand can be negative at all. | |
fbf5a39b | 8868 | |
5d5e9775 | 8869 | if Lneg and Rneg then |
70482933 | 8870 | Rewrite (N, |
9b16cb57 | 8871 | Make_If_Expression (Loc, |
70482933 RK |
8872 | Expressions => New_List ( |
8873 | Make_Op_Eq (Loc, | |
0d901290 | 8874 | Left_Opnd => Duplicate_Subexpr (Right), |
70482933 | 8875 | Right_Opnd => |
0d901290 | 8876 | Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))), |
70482933 | 8877 | |
fbf5a39b AC |
8878 | Unchecked_Convert_To (Typ, |
8879 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
8880 | |
8881 | Relocate_Node (N)))); | |
8882 | ||
8883 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
8884 | Analyze_And_Resolve (N, Typ); | |
8885 | end if; | |
8886 | end Expand_N_Op_Rem; | |
8887 | ||
8888 | ----------------------------- | |
8889 | -- Expand_N_Op_Rotate_Left -- | |
8890 | ----------------------------- | |
8891 | ||
8892 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
8893 | begin | |
8894 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
8895 | |
8896 | -- If we are in Modify_Tree_For_C mode, there is no rotate left in C, | |
8897 | -- so we rewrite in terms of logical shifts | |
8898 | ||
8899 | -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits) | |
8900 | ||
8901 | -- where Bits is the shift count mod Esize (the mod operation here | |
8902 | -- deals with ludicrous large shift counts, which are apparently OK). | |
8903 | ||
8904 | -- What about non-binary modulus ??? | |
8905 | ||
8906 | declare | |
8907 | Loc : constant Source_Ptr := Sloc (N); | |
8908 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
8909 | Typ : constant Entity_Id := Etype (N); | |
8910 | ||
8911 | begin | |
8912 | if Modify_Tree_For_C then | |
8913 | Rewrite (Right_Opnd (N), | |
8914 | Make_Op_Rem (Loc, | |
8915 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
8916 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
8917 | ||
8918 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
8919 | ||
8920 | Rewrite (N, | |
8921 | Make_Op_Or (Loc, | |
8922 | Left_Opnd => | |
8923 | Make_Op_Shift_Left (Loc, | |
8924 | Left_Opnd => Left_Opnd (N), | |
8925 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 8926 | |
5216b599 AC |
8927 | Right_Opnd => |
8928 | Make_Op_Shift_Right (Loc, | |
8929 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
8930 | Right_Opnd => | |
8931 | Make_Op_Subtract (Loc, | |
8932 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
8933 | Right_Opnd => | |
8934 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
8935 | ||
8936 | Analyze_And_Resolve (N, Typ); | |
8937 | end if; | |
8938 | end; | |
70482933 RK |
8939 | end Expand_N_Op_Rotate_Left; |
8940 | ||
8941 | ------------------------------ | |
8942 | -- Expand_N_Op_Rotate_Right -- | |
8943 | ------------------------------ | |
8944 | ||
8945 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
8946 | begin | |
8947 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
8948 | |
8949 | -- If we are in Modify_Tree_For_C mode, there is no rotate right in C, | |
8950 | -- so we rewrite in terms of logical shifts | |
8951 | ||
8952 | -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits) | |
8953 | ||
8954 | -- where Bits is the shift count mod Esize (the mod operation here | |
8955 | -- deals with ludicrous large shift counts, which are apparently OK). | |
8956 | ||
8957 | -- What about non-binary modulus ??? | |
8958 | ||
8959 | declare | |
8960 | Loc : constant Source_Ptr := Sloc (N); | |
8961 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
8962 | Typ : constant Entity_Id := Etype (N); | |
8963 | ||
8964 | begin | |
8965 | Rewrite (Right_Opnd (N), | |
8966 | Make_Op_Rem (Loc, | |
8967 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
8968 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
8969 | ||
8970 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
8971 | ||
8972 | if Modify_Tree_For_C then | |
8973 | Rewrite (N, | |
8974 | Make_Op_Or (Loc, | |
8975 | Left_Opnd => | |
8976 | Make_Op_Shift_Right (Loc, | |
8977 | Left_Opnd => Left_Opnd (N), | |
8978 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 8979 | |
5216b599 AC |
8980 | Right_Opnd => |
8981 | Make_Op_Shift_Left (Loc, | |
8982 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
8983 | Right_Opnd => | |
8984 | Make_Op_Subtract (Loc, | |
8985 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
8986 | Right_Opnd => | |
8987 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
8988 | ||
8989 | Analyze_And_Resolve (N, Typ); | |
8990 | end if; | |
8991 | end; | |
70482933 RK |
8992 | end Expand_N_Op_Rotate_Right; |
8993 | ||
8994 | ---------------------------- | |
8995 | -- Expand_N_Op_Shift_Left -- | |
8996 | ---------------------------- | |
8997 | ||
e09a5598 AC |
8998 | -- Note: nothing in this routine depends on left as opposed to right shifts |
8999 | -- so we share the routine for expanding shift right operations. | |
9000 | ||
70482933 RK |
9001 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is |
9002 | begin | |
9003 | Binary_Op_Validity_Checks (N); | |
e09a5598 AC |
9004 | |
9005 | -- If we are in Modify_Tree_For_C mode, then ensure that the right | |
9006 | -- operand is not greater than the word size (since that would not | |
9007 | -- be defined properly by the corresponding C shift operator). | |
9008 | ||
9009 | if Modify_Tree_For_C then | |
9010 | declare | |
9011 | Right : constant Node_Id := Right_Opnd (N); | |
9012 | Loc : constant Source_Ptr := Sloc (Right); | |
9013 | Typ : constant Entity_Id := Etype (N); | |
9014 | Siz : constant Uint := Esize (Typ); | |
9015 | Orig : Node_Id; | |
9016 | OK : Boolean; | |
9017 | Lo : Uint; | |
9018 | Hi : Uint; | |
9019 | ||
9020 | begin | |
9021 | if Compile_Time_Known_Value (Right) then | |
9022 | if Expr_Value (Right) >= Siz then | |
9023 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
9024 | Analyze_And_Resolve (N, Typ); | |
9025 | end if; | |
9026 | ||
9027 | -- Not compile time known, find range | |
9028 | ||
9029 | else | |
9030 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); | |
9031 | ||
9032 | -- Nothing to do if known to be OK range, otherwise expand | |
9033 | ||
9034 | if not OK or else Hi >= Siz then | |
9035 | ||
9036 | -- Prevent recursion on copy of shift node | |
9037 | ||
9038 | Orig := Relocate_Node (N); | |
9039 | Set_Analyzed (Orig); | |
9040 | ||
9041 | -- Now do the rewrite | |
9042 | ||
9043 | Rewrite (N, | |
9044 | Make_If_Expression (Loc, | |
9045 | Expressions => New_List ( | |
9046 | Make_Op_Ge (Loc, | |
9047 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
9048 | Right_Opnd => Make_Integer_Literal (Loc, Siz)), | |
9049 | Make_Integer_Literal (Loc, 0), | |
9050 | Orig))); | |
9051 | Analyze_And_Resolve (N, Typ); | |
9052 | end if; | |
9053 | end if; | |
9054 | end; | |
9055 | end if; | |
70482933 RK |
9056 | end Expand_N_Op_Shift_Left; |
9057 | ||
9058 | ----------------------------- | |
9059 | -- Expand_N_Op_Shift_Right -- | |
9060 | ----------------------------- | |
9061 | ||
9062 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
9063 | begin | |
e09a5598 AC |
9064 | -- Share shift left circuit |
9065 | ||
9066 | Expand_N_Op_Shift_Left (N); | |
70482933 RK |
9067 | end Expand_N_Op_Shift_Right; |
9068 | ||
9069 | ---------------------------------------- | |
9070 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
9071 | ---------------------------------------- | |
9072 | ||
9073 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
9074 | begin | |
9075 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
9076 | |
9077 | -- If we are in Modify_Tree_For_C mode, there is no shift right | |
9078 | -- arithmetic in C, so we rewrite in terms of logical shifts. | |
9079 | ||
9080 | -- Shift_Right (Num, Bits) or | |
9081 | -- (if Num >= Sign | |
9082 | -- then not (Shift_Right (Mask, bits)) | |
9083 | -- else 0) | |
9084 | ||
9085 | -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1) | |
9086 | ||
9087 | -- Note: in almost all C compilers it would work to just shift a | |
9088 | -- signed integer right, but it's undefined and we cannot rely on it. | |
9089 | ||
e09a5598 AC |
9090 | -- Note: the above works fine for shift counts greater than or equal |
9091 | -- to the word size, since in this case (not (Shift_Right (Mask, bits))) | |
9092 | -- generates all 1'bits. | |
9093 | ||
5216b599 AC |
9094 | -- What about non-binary modulus ??? |
9095 | ||
9096 | declare | |
9097 | Loc : constant Source_Ptr := Sloc (N); | |
9098 | Typ : constant Entity_Id := Etype (N); | |
9099 | Sign : constant Uint := 2 ** (Esize (Typ) - 1); | |
9100 | Mask : constant Uint := (2 ** Esize (Typ)) - 1; | |
9101 | Left : constant Node_Id := Left_Opnd (N); | |
9102 | Right : constant Node_Id := Right_Opnd (N); | |
9103 | Maskx : Node_Id; | |
9104 | ||
9105 | begin | |
9106 | if Modify_Tree_For_C then | |
9107 | ||
9108 | -- Here if not (Shift_Right (Mask, bits)) can be computed at | |
9109 | -- compile time as a single constant. | |
9110 | ||
9111 | if Compile_Time_Known_Value (Right) then | |
9112 | declare | |
9113 | Val : constant Uint := Expr_Value (Right); | |
9114 | ||
9115 | begin | |
9116 | if Val >= Esize (Typ) then | |
9117 | Maskx := Make_Integer_Literal (Loc, Mask); | |
9118 | ||
9119 | else | |
9120 | Maskx := | |
9121 | Make_Integer_Literal (Loc, | |
9122 | Intval => Mask - (Mask / (2 ** Expr_Value (Right)))); | |
9123 | end if; | |
9124 | end; | |
9125 | ||
9126 | else | |
9127 | Maskx := | |
9128 | Make_Op_Not (Loc, | |
9129 | Right_Opnd => | |
9130 | Make_Op_Shift_Right (Loc, | |
9131 | Left_Opnd => Make_Integer_Literal (Loc, Mask), | |
9132 | Right_Opnd => Duplicate_Subexpr_No_Checks (Right))); | |
9133 | end if; | |
9134 | ||
9135 | -- Now do the rewrite | |
9136 | ||
9137 | Rewrite (N, | |
9138 | Make_Op_Or (Loc, | |
9139 | Left_Opnd => | |
9140 | Make_Op_Shift_Right (Loc, | |
9141 | Left_Opnd => Left, | |
9142 | Right_Opnd => Right), | |
9143 | Right_Opnd => | |
9144 | Make_If_Expression (Loc, | |
9145 | Expressions => New_List ( | |
9146 | Make_Op_Ge (Loc, | |
9147 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
9148 | Right_Opnd => Make_Integer_Literal (Loc, Sign)), | |
9149 | Maskx, | |
9150 | Make_Integer_Literal (Loc, 0))))); | |
9151 | Analyze_And_Resolve (N, Typ); | |
9152 | end if; | |
9153 | end; | |
70482933 RK |
9154 | end Expand_N_Op_Shift_Right_Arithmetic; |
9155 | ||
9156 | -------------------------- | |
9157 | -- Expand_N_Op_Subtract -- | |
9158 | -------------------------- | |
9159 | ||
9160 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
9161 | Typ : constant Entity_Id := Etype (N); | |
9162 | ||
9163 | begin | |
9164 | Binary_Op_Validity_Checks (N); | |
9165 | ||
b6b5cca8 AC |
9166 | -- Check for MINIMIZED/ELIMINATED overflow mode |
9167 | ||
9168 | if Minimized_Eliminated_Overflow_Check (N) then | |
9169 | Apply_Arithmetic_Overflow_Check (N); | |
9170 | return; | |
9171 | end if; | |
9172 | ||
70482933 RK |
9173 | -- N - 0 = N for integer types |
9174 | ||
9175 | if Is_Integer_Type (Typ) | |
9176 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
9177 | and then Expr_Value (Right_Opnd (N)) = 0 | |
9178 | then | |
9179 | Rewrite (N, Left_Opnd (N)); | |
9180 | return; | |
9181 | end if; | |
9182 | ||
8fc789c8 | 9183 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 9184 | |
761f7dcb | 9185 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 | 9186 | Apply_Arithmetic_Overflow_Check (N); |
70482933 RK |
9187 | end if; |
9188 | end Expand_N_Op_Subtract; | |
9189 | ||
9190 | --------------------- | |
9191 | -- Expand_N_Op_Xor -- | |
9192 | --------------------- | |
9193 | ||
9194 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
9195 | Typ : constant Entity_Id := Etype (N); | |
9196 | ||
9197 | begin | |
9198 | Binary_Op_Validity_Checks (N); | |
9199 | ||
9200 | if Is_Array_Type (Etype (N)) then | |
9201 | Expand_Boolean_Operator (N); | |
9202 | ||
9203 | elsif Is_Boolean_Type (Etype (N)) then | |
9204 | Adjust_Condition (Left_Opnd (N)); | |
9205 | Adjust_Condition (Right_Opnd (N)); | |
9206 | Set_Etype (N, Standard_Boolean); | |
9207 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
9208 | |
9209 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
9210 | Expand_Intrinsic_Call (N, Entity (N)); | |
9211 | ||
70482933 RK |
9212 | end if; |
9213 | end Expand_N_Op_Xor; | |
9214 | ||
9215 | ---------------------- | |
9216 | -- Expand_N_Or_Else -- | |
9217 | ---------------------- | |
9218 | ||
5875f8d6 AC |
9219 | procedure Expand_N_Or_Else (N : Node_Id) |
9220 | renames Expand_Short_Circuit_Operator; | |
70482933 RK |
9221 | |
9222 | ----------------------------------- | |
9223 | -- Expand_N_Qualified_Expression -- | |
9224 | ----------------------------------- | |
9225 | ||
9226 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
9227 | Operand : constant Node_Id := Expression (N); | |
9228 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
9229 | ||
9230 | begin | |
f82944b7 JM |
9231 | -- Do validity check if validity checking operands |
9232 | ||
533369aa | 9233 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
9234 | Ensure_Valid (Operand); |
9235 | end if; | |
9236 | ||
9237 | -- Apply possible constraint check | |
9238 | ||
70482933 | 9239 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
d79e621a GD |
9240 | |
9241 | if Do_Range_Check (Operand) then | |
9242 | Set_Do_Range_Check (Operand, False); | |
9243 | Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed); | |
9244 | end if; | |
70482933 RK |
9245 | end Expand_N_Qualified_Expression; |
9246 | ||
a961aa79 AC |
9247 | ------------------------------------ |
9248 | -- Expand_N_Quantified_Expression -- | |
9249 | ------------------------------------ | |
9250 | ||
c0f136cd AC |
9251 | -- We expand: |
9252 | ||
9253 | -- for all X in range => Cond | |
a961aa79 | 9254 | |
c0f136cd | 9255 | -- into: |
a961aa79 | 9256 | |
c0f136cd AC |
9257 | -- T := True; |
9258 | -- for X in range loop | |
9259 | -- if not Cond then | |
9260 | -- T := False; | |
9261 | -- exit; | |
9262 | -- end if; | |
9263 | -- end loop; | |
90c63b09 | 9264 | |
36504e5f | 9265 | -- Similarly, an existentially quantified expression: |
90c63b09 | 9266 | |
c0f136cd | 9267 | -- for some X in range => Cond |
90c63b09 | 9268 | |
c0f136cd | 9269 | -- becomes: |
90c63b09 | 9270 | |
c0f136cd AC |
9271 | -- T := False; |
9272 | -- for X in range loop | |
9273 | -- if Cond then | |
9274 | -- T := True; | |
9275 | -- exit; | |
9276 | -- end if; | |
9277 | -- end loop; | |
90c63b09 | 9278 | |
c0f136cd AC |
9279 | -- In both cases, the iteration may be over a container in which case it is |
9280 | -- given by an iterator specification, not a loop parameter specification. | |
a961aa79 | 9281 | |
c0f136cd | 9282 | procedure Expand_N_Quantified_Expression (N : Node_Id) is |
804670f1 AC |
9283 | Actions : constant List_Id := New_List; |
9284 | For_All : constant Boolean := All_Present (N); | |
9285 | Iter_Spec : constant Node_Id := Iterator_Specification (N); | |
9286 | Loc : constant Source_Ptr := Sloc (N); | |
9287 | Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N); | |
9288 | Cond : Node_Id; | |
9289 | Flag : Entity_Id; | |
9290 | Scheme : Node_Id; | |
9291 | Stmts : List_Id; | |
c56a9ba4 | 9292 | |
a961aa79 | 9293 | begin |
804670f1 AC |
9294 | -- Create the declaration of the flag which tracks the status of the |
9295 | -- quantified expression. Generate: | |
011f9d5d | 9296 | |
804670f1 | 9297 | -- Flag : Boolean := (True | False); |
011f9d5d | 9298 | |
804670f1 | 9299 | Flag := Make_Temporary (Loc, 'T', N); |
011f9d5d | 9300 | |
804670f1 | 9301 | Append_To (Actions, |
90c63b09 | 9302 | Make_Object_Declaration (Loc, |
804670f1 | 9303 | Defining_Identifier => Flag, |
c0f136cd AC |
9304 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
9305 | Expression => | |
804670f1 AC |
9306 | New_Occurrence_Of (Boolean_Literals (For_All), Loc))); |
9307 | ||
9308 | -- Construct the circuitry which tracks the status of the quantified | |
9309 | -- expression. Generate: | |
9310 | ||
9311 | -- if [not] Cond then | |
9312 | -- Flag := (False | True); | |
9313 | -- exit; | |
9314 | -- end if; | |
a961aa79 | 9315 | |
c0f136cd | 9316 | Cond := Relocate_Node (Condition (N)); |
a961aa79 | 9317 | |
804670f1 | 9318 | if For_All then |
c0f136cd | 9319 | Cond := Make_Op_Not (Loc, Cond); |
a961aa79 AC |
9320 | end if; |
9321 | ||
804670f1 | 9322 | Stmts := New_List ( |
c0f136cd AC |
9323 | Make_Implicit_If_Statement (N, |
9324 | Condition => Cond, | |
9325 | Then_Statements => New_List ( | |
9326 | Make_Assignment_Statement (Loc, | |
804670f1 | 9327 | Name => New_Occurrence_Of (Flag, Loc), |
c0f136cd | 9328 | Expression => |
804670f1 AC |
9329 | New_Occurrence_Of (Boolean_Literals (not For_All), Loc)), |
9330 | Make_Exit_Statement (Loc)))); | |
9331 | ||
9332 | -- Build the loop equivalent of the quantified expression | |
c0f136cd | 9333 | |
804670f1 AC |
9334 | if Present (Iter_Spec) then |
9335 | Scheme := | |
011f9d5d | 9336 | Make_Iteration_Scheme (Loc, |
804670f1 | 9337 | Iterator_Specification => Iter_Spec); |
c56a9ba4 | 9338 | else |
804670f1 | 9339 | Scheme := |
011f9d5d | 9340 | Make_Iteration_Scheme (Loc, |
804670f1 | 9341 | Loop_Parameter_Specification => Loop_Spec); |
c56a9ba4 AC |
9342 | end if; |
9343 | ||
a961aa79 AC |
9344 | Append_To (Actions, |
9345 | Make_Loop_Statement (Loc, | |
804670f1 AC |
9346 | Iteration_Scheme => Scheme, |
9347 | Statements => Stmts, | |
c0f136cd | 9348 | End_Label => Empty)); |
a961aa79 | 9349 | |
804670f1 AC |
9350 | -- Transform the quantified expression |
9351 | ||
a961aa79 AC |
9352 | Rewrite (N, |
9353 | Make_Expression_With_Actions (Loc, | |
804670f1 | 9354 | Expression => New_Occurrence_Of (Flag, Loc), |
a961aa79 | 9355 | Actions => Actions)); |
a961aa79 AC |
9356 | Analyze_And_Resolve (N, Standard_Boolean); |
9357 | end Expand_N_Quantified_Expression; | |
9358 | ||
70482933 RK |
9359 | --------------------------------- |
9360 | -- Expand_N_Selected_Component -- | |
9361 | --------------------------------- | |
9362 | ||
70482933 RK |
9363 | procedure Expand_N_Selected_Component (N : Node_Id) is |
9364 | Loc : constant Source_Ptr := Sloc (N); | |
9365 | Par : constant Node_Id := Parent (N); | |
9366 | P : constant Node_Id := Prefix (N); | |
03eb6036 | 9367 | S : constant Node_Id := Selector_Name (N); |
fbf5a39b | 9368 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 9369 | Disc : Entity_Id; |
70482933 | 9370 | New_N : Node_Id; |
fbf5a39b | 9371 | Dcon : Elmt_Id; |
d606f1df | 9372 | Dval : Node_Id; |
70482933 RK |
9373 | |
9374 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
9375 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
9376 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
9377 | -- Don't we have a general routine that does this??? |
9378 | ||
53f29d4f AC |
9379 | function Is_Subtype_Declaration return Boolean; |
9380 | -- The replacement of a discriminant reference by its value is required | |
4317e442 AC |
9381 | -- if this is part of the initialization of an temporary generated by a |
9382 | -- change of representation. This shows up as the construction of a | |
53f29d4f | 9383 | -- discriminant constraint for a subtype declared at the same point as |
4317e442 AC |
9384 | -- the entity in the prefix of the selected component. We recognize this |
9385 | -- case when the context of the reference is: | |
9386 | -- subtype ST is T(Obj.D); | |
9387 | -- where the entity for Obj comes from source, and ST has the same sloc. | |
53f29d4f | 9388 | |
fbf5a39b AC |
9389 | ----------------------- |
9390 | -- In_Left_Hand_Side -- | |
9391 | ----------------------- | |
70482933 RK |
9392 | |
9393 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
9394 | begin | |
fbf5a39b | 9395 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
90c63b09 | 9396 | and then Comp = Name (Parent (Comp))) |
fbf5a39b | 9397 | or else (Present (Parent (Comp)) |
90c63b09 AC |
9398 | and then Nkind (Parent (Comp)) in N_Subexpr |
9399 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
9400 | end In_Left_Hand_Side; |
9401 | ||
53f29d4f AC |
9402 | ----------------------------- |
9403 | -- Is_Subtype_Declaration -- | |
9404 | ----------------------------- | |
9405 | ||
9406 | function Is_Subtype_Declaration return Boolean is | |
9407 | Par : constant Node_Id := Parent (N); | |
53f29d4f AC |
9408 | begin |
9409 | return | |
9410 | Nkind (Par) = N_Index_Or_Discriminant_Constraint | |
9411 | and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration | |
9412 | and then Comes_From_Source (Entity (Prefix (N))) | |
9413 | and then Sloc (Par) = Sloc (Entity (Prefix (N))); | |
9414 | end Is_Subtype_Declaration; | |
9415 | ||
fbf5a39b AC |
9416 | -- Start of processing for Expand_N_Selected_Component |
9417 | ||
70482933 | 9418 | begin |
fbf5a39b AC |
9419 | -- Insert explicit dereference if required |
9420 | ||
9421 | if Is_Access_Type (Ptyp) then | |
702d2020 AC |
9422 | |
9423 | -- First set prefix type to proper access type, in case it currently | |
9424 | -- has a private (non-access) view of this type. | |
9425 | ||
9426 | Set_Etype (P, Ptyp); | |
9427 | ||
fbf5a39b | 9428 | Insert_Explicit_Dereference (P); |
e6f69614 | 9429 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
9430 | |
9431 | if Ekind (Etype (P)) = E_Private_Subtype | |
9432 | and then Is_For_Access_Subtype (Etype (P)) | |
9433 | then | |
9434 | Set_Etype (P, Base_Type (Etype (P))); | |
9435 | end if; | |
9436 | ||
9437 | Ptyp := Etype (P); | |
9438 | end if; | |
9439 | ||
9440 | -- Deal with discriminant check required | |
9441 | ||
70482933 | 9442 | if Do_Discriminant_Check (N) then |
03eb6036 AC |
9443 | if Present (Discriminant_Checking_Func |
9444 | (Original_Record_Component (Entity (S)))) | |
9445 | then | |
9446 | -- Present the discriminant checking function to the backend, so | |
9447 | -- that it can inline the call to the function. | |
9448 | ||
9449 | Add_Inlined_Body | |
9450 | (Discriminant_Checking_Func | |
9451 | (Original_Record_Component (Entity (S)))); | |
70482933 | 9452 | |
03eb6036 | 9453 | -- Now reset the flag and generate the call |
70482933 | 9454 | |
03eb6036 AC |
9455 | Set_Do_Discriminant_Check (N, False); |
9456 | Generate_Discriminant_Check (N); | |
70482933 | 9457 | |
03eb6036 AC |
9458 | -- In the case of Unchecked_Union, no discriminant checking is |
9459 | -- actually performed. | |
70482933 | 9460 | |
03eb6036 AC |
9461 | else |
9462 | Set_Do_Discriminant_Check (N, False); | |
9463 | end if; | |
70482933 RK |
9464 | end if; |
9465 | ||
b4592168 GD |
9466 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
9467 | -- function, then additional actuals must be passed. | |
9468 | ||
0791fbe9 | 9469 | if Ada_Version >= Ada_2005 |
b4592168 GD |
9470 | and then Is_Build_In_Place_Function_Call (P) |
9471 | then | |
9472 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
9473 | end if; | |
9474 | ||
fbf5a39b AC |
9475 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
9476 | -- selected component with discriminants. This must be checked during | |
9477 | -- expansion, because during analysis the type of the selector is not | |
9478 | -- known at the point the prefix is analyzed. If the conversion is the | |
9479 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
9480 | |
9481 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
9482 | and then Has_Discriminants (Etype (N)) | |
9483 | and then not In_Left_Hand_Side (N) | |
9484 | then | |
9485 | Force_Evaluation (Prefix (N)); | |
9486 | end if; | |
9487 | ||
9488 | -- Remaining processing applies only if selector is a discriminant | |
9489 | ||
9490 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
9491 | ||
9492 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
9493 | -- we may be able to rewrite the expression with the actual value |
9494 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
9495 | |
9496 | if Is_Record_Type (Ptyp) | |
9497 | and then Has_Discriminants (Ptyp) | |
9498 | and then Is_Constrained (Ptyp) | |
70482933 | 9499 | then |
fbf5a39b | 9500 | -- Do this optimization for discrete types only, and not for |
a90bd866 | 9501 | -- access types (access discriminants get us into trouble). |
70482933 | 9502 | |
fbf5a39b AC |
9503 | if not Is_Discrete_Type (Etype (N)) then |
9504 | null; | |
9505 | ||
9506 | -- Don't do this on the left hand of an assignment statement. | |
0d901290 AC |
9507 | -- Normally one would think that references like this would not |
9508 | -- occur, but they do in generated code, and mean that we really | |
a90bd866 | 9509 | -- do want to assign the discriminant. |
fbf5a39b AC |
9510 | |
9511 | elsif Nkind (Par) = N_Assignment_Statement | |
9512 | and then Name (Par) = N | |
9513 | then | |
9514 | null; | |
9515 | ||
685094bf | 9516 | -- Don't do this optimization for the prefix of an attribute or |
e2534738 | 9517 | -- the name of an object renaming declaration since these are |
685094bf | 9518 | -- contexts where we do not want the value anyway. |
fbf5a39b AC |
9519 | |
9520 | elsif (Nkind (Par) = N_Attribute_Reference | |
533369aa | 9521 | and then Prefix (Par) = N) |
fbf5a39b AC |
9522 | or else Is_Renamed_Object (N) |
9523 | then | |
9524 | null; | |
9525 | ||
9526 | -- Don't do this optimization if we are within the code for a | |
9527 | -- discriminant check, since the whole point of such a check may | |
a90bd866 | 9528 | -- be to verify the condition on which the code below depends. |
fbf5a39b AC |
9529 | |
9530 | elsif Is_In_Discriminant_Check (N) then | |
9531 | null; | |
9532 | ||
9533 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
9534 | -- still one condition that inhibits the optimization below but |
9535 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
9536 | |
9537 | else | |
685094bf RD |
9538 | -- Loop through discriminants to find the matching discriminant |
9539 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
9540 | |
9541 | Disc := First_Discriminant (Ptyp); | |
9542 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
9543 | Discr_Loop : while Present (Dcon) loop | |
d606f1df | 9544 | Dval := Node (Dcon); |
fbf5a39b | 9545 | |
bd949ee2 RD |
9546 | -- Check if this is the matching discriminant and if the |
9547 | -- discriminant value is simple enough to make sense to | |
9548 | -- copy. We don't want to copy complex expressions, and | |
9549 | -- indeed to do so can cause trouble (before we put in | |
9550 | -- this guard, a discriminant expression containing an | |
e7d897b8 | 9551 | -- AND THEN was copied, causing problems for coverage |
c228a069 | 9552 | -- analysis tools). |
bd949ee2 | 9553 | |
53f29d4f AC |
9554 | -- However, if the reference is part of the initialization |
9555 | -- code generated for an object declaration, we must use | |
9556 | -- the discriminant value from the subtype constraint, | |
9557 | -- because the selected component may be a reference to the | |
9558 | -- object being initialized, whose discriminant is not yet | |
9559 | -- set. This only happens in complex cases involving changes | |
9560 | -- or representation. | |
9561 | ||
bd949ee2 RD |
9562 | if Disc = Entity (Selector_Name (N)) |
9563 | and then (Is_Entity_Name (Dval) | |
170b2989 AC |
9564 | or else Compile_Time_Known_Value (Dval) |
9565 | or else Is_Subtype_Declaration) | |
bd949ee2 | 9566 | then |
fbf5a39b AC |
9567 | -- Here we have the matching discriminant. Check for |
9568 | -- the case of a discriminant of a component that is | |
9569 | -- constrained by an outer discriminant, which cannot | |
9570 | -- be optimized away. | |
9571 | ||
d606f1df AC |
9572 | if Denotes_Discriminant |
9573 | (Dval, Check_Concurrent => True) | |
9574 | then | |
9575 | exit Discr_Loop; | |
9576 | ||
9577 | elsif Nkind (Original_Node (Dval)) = N_Selected_Component | |
9578 | and then | |
9579 | Denotes_Discriminant | |
9580 | (Selector_Name (Original_Node (Dval)), True) | |
9581 | then | |
9582 | exit Discr_Loop; | |
9583 | ||
9584 | -- Do not retrieve value if constraint is not static. It | |
9585 | -- is generally not useful, and the constraint may be a | |
9586 | -- rewritten outer discriminant in which case it is in | |
9587 | -- fact incorrect. | |
9588 | ||
9589 | elsif Is_Entity_Name (Dval) | |
d606f1df | 9590 | and then |
533369aa AC |
9591 | Nkind (Parent (Entity (Dval))) = N_Object_Declaration |
9592 | and then Present (Expression (Parent (Entity (Dval)))) | |
9593 | and then not | |
edab6088 | 9594 | Is_OK_Static_Expression |
d606f1df | 9595 | (Expression (Parent (Entity (Dval)))) |
fbf5a39b AC |
9596 | then |
9597 | exit Discr_Loop; | |
70482933 | 9598 | |
685094bf RD |
9599 | -- In the context of a case statement, the expression may |
9600 | -- have the base type of the discriminant, and we need to | |
9601 | -- preserve the constraint to avoid spurious errors on | |
9602 | -- missing cases. | |
70482933 | 9603 | |
fbf5a39b | 9604 | elsif Nkind (Parent (N)) = N_Case_Statement |
d606f1df | 9605 | and then Etype (Dval) /= Etype (Disc) |
70482933 RK |
9606 | then |
9607 | Rewrite (N, | |
9608 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
9609 | Subtype_Mark => |
9610 | New_Occurrence_Of (Etype (Disc), Loc), | |
9611 | Expression => | |
d606f1df | 9612 | New_Copy_Tree (Dval))); |
ffe9aba8 | 9613 | Analyze_And_Resolve (N, Etype (Disc)); |
fbf5a39b AC |
9614 | |
9615 | -- In case that comes out as a static expression, | |
9616 | -- reset it (a selected component is never static). | |
9617 | ||
9618 | Set_Is_Static_Expression (N, False); | |
9619 | return; | |
9620 | ||
9621 | -- Otherwise we can just copy the constraint, but the | |
a90bd866 | 9622 | -- result is certainly not static. In some cases the |
ffe9aba8 AC |
9623 | -- discriminant constraint has been analyzed in the |
9624 | -- context of the original subtype indication, but for | |
9625 | -- itypes the constraint might not have been analyzed | |
9626 | -- yet, and this must be done now. | |
fbf5a39b | 9627 | |
70482933 | 9628 | else |
d606f1df | 9629 | Rewrite (N, New_Copy_Tree (Dval)); |
ffe9aba8 | 9630 | Analyze_And_Resolve (N); |
fbf5a39b AC |
9631 | Set_Is_Static_Expression (N, False); |
9632 | return; | |
70482933 | 9633 | end if; |
70482933 RK |
9634 | end if; |
9635 | ||
fbf5a39b AC |
9636 | Next_Elmt (Dcon); |
9637 | Next_Discriminant (Disc); | |
9638 | end loop Discr_Loop; | |
70482933 | 9639 | |
fbf5a39b AC |
9640 | -- Note: the above loop should always find a matching |
9641 | -- discriminant, but if it does not, we just missed an | |
c228a069 AC |
9642 | -- optimization due to some glitch (perhaps a previous |
9643 | -- error), so ignore. | |
fbf5a39b AC |
9644 | |
9645 | end if; | |
70482933 RK |
9646 | end if; |
9647 | ||
9648 | -- The only remaining processing is in the case of a discriminant of | |
9649 | -- a concurrent object, where we rewrite the prefix to denote the | |
9650 | -- corresponding record type. If the type is derived and has renamed | |
9651 | -- discriminants, use corresponding discriminant, which is the one | |
9652 | -- that appears in the corresponding record. | |
9653 | ||
9654 | if not Is_Concurrent_Type (Ptyp) then | |
9655 | return; | |
9656 | end if; | |
9657 | ||
9658 | Disc := Entity (Selector_Name (N)); | |
9659 | ||
9660 | if Is_Derived_Type (Ptyp) | |
9661 | and then Present (Corresponding_Discriminant (Disc)) | |
9662 | then | |
9663 | Disc := Corresponding_Discriminant (Disc); | |
9664 | end if; | |
9665 | ||
9666 | New_N := | |
9667 | Make_Selected_Component (Loc, | |
9668 | Prefix => | |
9669 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
9670 | New_Copy_Tree (P)), | |
9671 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
9672 | ||
9673 | Rewrite (N, New_N); | |
9674 | Analyze (N); | |
9675 | end if; | |
5972791c | 9676 | |
73fe1679 | 9677 | -- Set Atomic_Sync_Required if necessary for atomic component |
5972791c | 9678 | |
73fe1679 AC |
9679 | if Nkind (N) = N_Selected_Component then |
9680 | declare | |
9681 | E : constant Entity_Id := Entity (Selector_Name (N)); | |
9682 | Set : Boolean; | |
9683 | ||
9684 | begin | |
9685 | -- If component is atomic, but type is not, setting depends on | |
9686 | -- disable/enable state for the component. | |
9687 | ||
9688 | if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then | |
9689 | Set := not Atomic_Synchronization_Disabled (E); | |
9690 | ||
9691 | -- If component is not atomic, but its type is atomic, setting | |
9692 | -- depends on disable/enable state for the type. | |
9693 | ||
9694 | elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
9695 | Set := not Atomic_Synchronization_Disabled (Etype (E)); | |
9696 | ||
9697 | -- If both component and type are atomic, we disable if either | |
9698 | -- component or its type have sync disabled. | |
9699 | ||
9700 | elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
9701 | Set := (not Atomic_Synchronization_Disabled (E)) | |
9702 | and then | |
9703 | (not Atomic_Synchronization_Disabled (Etype (E))); | |
9704 | ||
9705 | else | |
9706 | Set := False; | |
9707 | end if; | |
9708 | ||
9709 | -- Set flag if required | |
9710 | ||
9711 | if Set then | |
9712 | Activate_Atomic_Synchronization (N); | |
9713 | end if; | |
9714 | end; | |
5972791c | 9715 | end if; |
70482933 RK |
9716 | end Expand_N_Selected_Component; |
9717 | ||
9718 | -------------------- | |
9719 | -- Expand_N_Slice -- | |
9720 | -------------------- | |
9721 | ||
9722 | procedure Expand_N_Slice (N : Node_Id) is | |
5ff90f08 AC |
9723 | Loc : constant Source_Ptr := Sloc (N); |
9724 | Typ : constant Entity_Id := Etype (N); | |
fbf5a39b | 9725 | |
81a5b587 | 9726 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
9727 | -- Check whether the argument is an actual for a procedure call, in |
9728 | -- which case the expansion of a bit-packed slice is deferred until the | |
9729 | -- call itself is expanded. The reason this is required is that we might | |
9730 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
9731 | -- that copy out would be missed if we created a temporary here in | |
9732 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
9733 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
9734 | -- is harmless to defer expansion in the IN case, since the call | |
9735 | -- processing will still generate the appropriate copy in operation, | |
9736 | -- which will take care of the slice. | |
81a5b587 | 9737 | |
b01bf852 | 9738 | procedure Make_Temporary_For_Slice; |
685094bf RD |
9739 | -- Create a named variable for the value of the slice, in cases where |
9740 | -- the back-end cannot handle it properly, e.g. when packed types or | |
9741 | -- unaligned slices are involved. | |
fbf5a39b | 9742 | |
81a5b587 AC |
9743 | ------------------------- |
9744 | -- Is_Procedure_Actual -- | |
9745 | ------------------------- | |
9746 | ||
9747 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
9748 | Par : Node_Id := Parent (N); | |
08aa9a4a | 9749 | |
81a5b587 | 9750 | begin |
81a5b587 | 9751 | loop |
c6a60aa1 RD |
9752 | -- If our parent is a procedure call we can return |
9753 | ||
81a5b587 AC |
9754 | if Nkind (Par) = N_Procedure_Call_Statement then |
9755 | return True; | |
6b6fcd3e | 9756 | |
685094bf RD |
9757 | -- If our parent is a type conversion, keep climbing the tree, |
9758 | -- since a type conversion can be a procedure actual. Also keep | |
9759 | -- climbing if parameter association or a qualified expression, | |
9760 | -- since these are additional cases that do can appear on | |
9761 | -- procedure actuals. | |
6b6fcd3e | 9762 | |
303b4d58 AC |
9763 | elsif Nkind_In (Par, N_Type_Conversion, |
9764 | N_Parameter_Association, | |
9765 | N_Qualified_Expression) | |
c6a60aa1 | 9766 | then |
81a5b587 | 9767 | Par := Parent (Par); |
c6a60aa1 RD |
9768 | |
9769 | -- Any other case is not what we are looking for | |
9770 | ||
9771 | else | |
9772 | return False; | |
81a5b587 AC |
9773 | end if; |
9774 | end loop; | |
81a5b587 AC |
9775 | end Is_Procedure_Actual; |
9776 | ||
b01bf852 AC |
9777 | ------------------------------ |
9778 | -- Make_Temporary_For_Slice -- | |
9779 | ------------------------------ | |
fbf5a39b | 9780 | |
b01bf852 | 9781 | procedure Make_Temporary_For_Slice is |
b01bf852 | 9782 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
5ff90f08 | 9783 | Decl : Node_Id; |
13d923cc | 9784 | |
fbf5a39b AC |
9785 | begin |
9786 | Decl := | |
9787 | Make_Object_Declaration (Loc, | |
9788 | Defining_Identifier => Ent, | |
9789 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
9790 | ||
9791 | Set_No_Initialization (Decl); | |
9792 | ||
9793 | Insert_Actions (N, New_List ( | |
9794 | Decl, | |
9795 | Make_Assignment_Statement (Loc, | |
5ff90f08 | 9796 | Name => New_Occurrence_Of (Ent, Loc), |
fbf5a39b AC |
9797 | Expression => Relocate_Node (N)))); |
9798 | ||
9799 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
9800 | Analyze_And_Resolve (N, Typ); | |
b01bf852 | 9801 | end Make_Temporary_For_Slice; |
fbf5a39b | 9802 | |
5ff90f08 AC |
9803 | -- Local variables |
9804 | ||
800da977 AC |
9805 | Pref : constant Node_Id := Prefix (N); |
9806 | Pref_Typ : Entity_Id := Etype (Pref); | |
5ff90f08 | 9807 | |
fbf5a39b | 9808 | -- Start of processing for Expand_N_Slice |
70482933 RK |
9809 | |
9810 | begin | |
9811 | -- Special handling for access types | |
9812 | ||
5ff90f08 AC |
9813 | if Is_Access_Type (Pref_Typ) then |
9814 | Pref_Typ := Designated_Type (Pref_Typ); | |
70482933 | 9815 | |
5ff90f08 | 9816 | Rewrite (Pref, |
e6f69614 | 9817 | Make_Explicit_Dereference (Sloc (N), |
5ff90f08 | 9818 | Prefix => Relocate_Node (Pref))); |
70482933 | 9819 | |
5ff90f08 | 9820 | Analyze_And_Resolve (Pref, Pref_Typ); |
70482933 RK |
9821 | end if; |
9822 | ||
b4592168 GD |
9823 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
9824 | -- function, then additional actuals must be passed. | |
9825 | ||
0791fbe9 | 9826 | if Ada_Version >= Ada_2005 |
5ff90f08 | 9827 | and then Is_Build_In_Place_Function_Call (Pref) |
b4592168 | 9828 | then |
5ff90f08 | 9829 | Make_Build_In_Place_Call_In_Anonymous_Context (Pref); |
b4592168 GD |
9830 | end if; |
9831 | ||
70482933 RK |
9832 | -- The remaining case to be handled is packed slices. We can leave |
9833 | -- packed slices as they are in the following situations: | |
9834 | ||
9835 | -- 1. Right or left side of an assignment (we can handle this | |
9836 | -- situation correctly in the assignment statement expansion). | |
9837 | ||
685094bf RD |
9838 | -- 2. Prefix of indexed component (the slide is optimized away in this |
9839 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 9840 | |
685094bf RD |
9841 | -- 3. Object renaming declaration, since we want the name of the |
9842 | -- slice, not the value. | |
70482933 | 9843 | |
685094bf RD |
9844 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
9845 | -- be required, and this is handled in the expansion of call | |
9846 | -- itself. | |
70482933 | 9847 | |
685094bf RD |
9848 | -- 5. Prefix of an address attribute (this is an error which is caught |
9849 | -- elsewhere, and the expansion would interfere with generating the | |
9850 | -- error message). | |
70482933 | 9851 | |
81a5b587 | 9852 | if not Is_Packed (Typ) then |
08aa9a4a | 9853 | |
685094bf RD |
9854 | -- Apply transformation for actuals of a function call, where |
9855 | -- Expand_Actuals is not used. | |
81a5b587 AC |
9856 | |
9857 | if Nkind (Parent (N)) = N_Function_Call | |
9858 | and then Is_Possibly_Unaligned_Slice (N) | |
9859 | then | |
b01bf852 | 9860 | Make_Temporary_For_Slice; |
81a5b587 AC |
9861 | end if; |
9862 | ||
9863 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
9864 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
533369aa | 9865 | and then Parent (N) = Name (Parent (Parent (N)))) |
70482933 | 9866 | then |
81a5b587 | 9867 | return; |
70482933 | 9868 | |
81a5b587 AC |
9869 | elsif Nkind (Parent (N)) = N_Indexed_Component |
9870 | or else Is_Renamed_Object (N) | |
9871 | or else Is_Procedure_Actual (N) | |
9872 | then | |
9873 | return; | |
70482933 | 9874 | |
91b1417d AC |
9875 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
9876 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 9877 | then |
81a5b587 AC |
9878 | return; |
9879 | ||
9880 | else | |
b01bf852 | 9881 | Make_Temporary_For_Slice; |
70482933 RK |
9882 | end if; |
9883 | end Expand_N_Slice; | |
9884 | ||
9885 | ------------------------------ | |
9886 | -- Expand_N_Type_Conversion -- | |
9887 | ------------------------------ | |
9888 | ||
9889 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
9890 | Loc : constant Source_Ptr := Sloc (N); | |
9891 | Operand : constant Node_Id := Expression (N); | |
9892 | Target_Type : constant Entity_Id := Etype (N); | |
9893 | Operand_Type : Entity_Id := Etype (Operand); | |
9894 | ||
9895 | procedure Handle_Changed_Representation; | |
685094bf RD |
9896 | -- This is called in the case of record and array type conversions to |
9897 | -- see if there is a change of representation to be handled. Change of | |
9898 | -- representation is actually handled at the assignment statement level, | |
9899 | -- and what this procedure does is rewrite node N conversion as an | |
9900 | -- assignment to temporary. If there is no change of representation, | |
9901 | -- then the conversion node is unchanged. | |
70482933 | 9902 | |
426908f8 RD |
9903 | procedure Raise_Accessibility_Error; |
9904 | -- Called when we know that an accessibility check will fail. Rewrites | |
9905 | -- node N to an appropriate raise statement and outputs warning msgs. | |
9906 | -- The Etype of the raise node is set to Target_Type. | |
9907 | ||
70482933 RK |
9908 | procedure Real_Range_Check; |
9909 | -- Handles generation of range check for real target value | |
9910 | ||
d15f9422 AC |
9911 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean; |
9912 | -- True iff Present (Effective_Extra_Accessibility (Id)) successfully | |
9913 | -- evaluates to True. | |
9914 | ||
70482933 RK |
9915 | ----------------------------------- |
9916 | -- Handle_Changed_Representation -- | |
9917 | ----------------------------------- | |
9918 | ||
9919 | procedure Handle_Changed_Representation is | |
9920 | Temp : Entity_Id; | |
9921 | Decl : Node_Id; | |
9922 | Odef : Node_Id; | |
9923 | Disc : Node_Id; | |
9924 | N_Ix : Node_Id; | |
9925 | Cons : List_Id; | |
9926 | ||
9927 | begin | |
f82944b7 | 9928 | -- Nothing else to do if no change of representation |
70482933 RK |
9929 | |
9930 | if Same_Representation (Operand_Type, Target_Type) then | |
9931 | return; | |
9932 | ||
9933 | -- The real change of representation work is done by the assignment | |
9934 | -- statement processing. So if this type conversion is appearing as | |
9935 | -- the expression of an assignment statement, nothing needs to be | |
9936 | -- done to the conversion. | |
9937 | ||
9938 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
9939 | return; | |
9940 | ||
9941 | -- Otherwise we need to generate a temporary variable, and do the | |
9942 | -- change of representation assignment into that temporary variable. | |
9943 | -- The conversion is then replaced by a reference to this variable. | |
9944 | ||
9945 | else | |
9946 | Cons := No_List; | |
9947 | ||
685094bf RD |
9948 | -- If type is unconstrained we have to add a constraint, copied |
9949 | -- from the actual value of the left hand side. | |
70482933 RK |
9950 | |
9951 | if not Is_Constrained (Target_Type) then | |
9952 | if Has_Discriminants (Operand_Type) then | |
9953 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
9954 | |
9955 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
9956 | Disc := First_Stored_Discriminant (Operand_Type); | |
9957 | end if; | |
9958 | ||
70482933 RK |
9959 | Cons := New_List; |
9960 | while Present (Disc) loop | |
9961 | Append_To (Cons, | |
9962 | Make_Selected_Component (Loc, | |
7675ad4f AC |
9963 | Prefix => |
9964 | Duplicate_Subexpr_Move_Checks (Operand), | |
70482933 RK |
9965 | Selector_Name => |
9966 | Make_Identifier (Loc, Chars (Disc)))); | |
9967 | Next_Discriminant (Disc); | |
9968 | end loop; | |
9969 | ||
9970 | elsif Is_Array_Type (Operand_Type) then | |
9971 | N_Ix := First_Index (Target_Type); | |
9972 | Cons := New_List; | |
9973 | ||
9974 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
9975 | ||
9976 | -- We convert the bounds explicitly. We use an unchecked | |
9977 | -- conversion because bounds checks are done elsewhere. | |
9978 | ||
9979 | Append_To (Cons, | |
9980 | Make_Range (Loc, | |
9981 | Low_Bound => | |
9982 | Unchecked_Convert_To (Etype (N_Ix), | |
9983 | Make_Attribute_Reference (Loc, | |
9984 | Prefix => | |
fbf5a39b | 9985 | Duplicate_Subexpr_No_Checks |
70482933 RK |
9986 | (Operand, Name_Req => True), |
9987 | Attribute_Name => Name_First, | |
9988 | Expressions => New_List ( | |
9989 | Make_Integer_Literal (Loc, J)))), | |
9990 | ||
9991 | High_Bound => | |
9992 | Unchecked_Convert_To (Etype (N_Ix), | |
9993 | Make_Attribute_Reference (Loc, | |
9994 | Prefix => | |
fbf5a39b | 9995 | Duplicate_Subexpr_No_Checks |
70482933 RK |
9996 | (Operand, Name_Req => True), |
9997 | Attribute_Name => Name_Last, | |
9998 | Expressions => New_List ( | |
9999 | Make_Integer_Literal (Loc, J)))))); | |
10000 | ||
10001 | Next_Index (N_Ix); | |
10002 | end loop; | |
10003 | end if; | |
10004 | end if; | |
10005 | ||
10006 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
10007 | ||
10008 | if Present (Cons) then | |
10009 | Odef := | |
10010 | Make_Subtype_Indication (Loc, | |
10011 | Subtype_Mark => Odef, | |
10012 | Constraint => | |
10013 | Make_Index_Or_Discriminant_Constraint (Loc, | |
10014 | Constraints => Cons)); | |
10015 | end if; | |
10016 | ||
191fcb3a | 10017 | Temp := Make_Temporary (Loc, 'C'); |
70482933 RK |
10018 | Decl := |
10019 | Make_Object_Declaration (Loc, | |
10020 | Defining_Identifier => Temp, | |
10021 | Object_Definition => Odef); | |
10022 | ||
10023 | Set_No_Initialization (Decl, True); | |
10024 | ||
10025 | -- Insert required actions. It is essential to suppress checks | |
10026 | -- since we have suppressed default initialization, which means | |
10027 | -- that the variable we create may have no discriminants. | |
10028 | ||
10029 | Insert_Actions (N, | |
10030 | New_List ( | |
10031 | Decl, | |
10032 | Make_Assignment_Statement (Loc, | |
10033 | Name => New_Occurrence_Of (Temp, Loc), | |
10034 | Expression => Relocate_Node (N))), | |
10035 | Suppress => All_Checks); | |
10036 | ||
10037 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
10038 | return; | |
10039 | end if; | |
10040 | end Handle_Changed_Representation; | |
10041 | ||
426908f8 RD |
10042 | ------------------------------- |
10043 | -- Raise_Accessibility_Error -- | |
10044 | ------------------------------- | |
10045 | ||
10046 | procedure Raise_Accessibility_Error is | |
10047 | begin | |
43417b90 | 10048 | Error_Msg_Warn := SPARK_Mode /= On; |
426908f8 RD |
10049 | Rewrite (N, |
10050 | Make_Raise_Program_Error (Sloc (N), | |
10051 | Reason => PE_Accessibility_Check_Failed)); | |
10052 | Set_Etype (N, Target_Type); | |
10053 | ||
4a28b181 AC |
10054 | Error_Msg_N ("<<accessibility check failure", N); |
10055 | Error_Msg_NE ("\<<& [", N, Standard_Program_Error); | |
426908f8 RD |
10056 | end Raise_Accessibility_Error; |
10057 | ||
70482933 RK |
10058 | ---------------------- |
10059 | -- Real_Range_Check -- | |
10060 | ---------------------- | |
10061 | ||
685094bf RD |
10062 | -- Case of conversions to floating-point or fixed-point. If range checks |
10063 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
10064 | |
10065 | -- typ (x) | |
10066 | ||
10067 | -- to | |
10068 | ||
10069 | -- Tnn : typ'Base := typ'Base (x); | |
10070 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
10071 | -- Tnn | |
10072 | ||
685094bf RD |
10073 | -- This is necessary when there is a conversion of integer to float or |
10074 | -- to fixed-point to ensure that the correct checks are made. It is not | |
10075 | -- necessary for float to float where it is enough to simply set the | |
10076 | -- Do_Range_Check flag. | |
fbf5a39b | 10077 | |
70482933 RK |
10078 | procedure Real_Range_Check is |
10079 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
10080 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
10081 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 10082 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
10083 | Conv : Node_Id; |
10084 | Tnn : Entity_Id; | |
10085 | ||
10086 | begin | |
10087 | -- Nothing to do if conversion was rewritten | |
10088 | ||
10089 | if Nkind (N) /= N_Type_Conversion then | |
10090 | return; | |
10091 | end if; | |
10092 | ||
685094bf RD |
10093 | -- Nothing to do if range checks suppressed, or target has the same |
10094 | -- range as the base type (or is the base type). | |
70482933 RK |
10095 | |
10096 | if Range_Checks_Suppressed (Target_Type) | |
533369aa | 10097 | or else (Lo = Type_Low_Bound (Btyp) |
70482933 RK |
10098 | and then |
10099 | Hi = Type_High_Bound (Btyp)) | |
10100 | then | |
10101 | return; | |
10102 | end if; | |
10103 | ||
685094bf RD |
10104 | -- Nothing to do if expression is an entity on which checks have been |
10105 | -- suppressed. | |
70482933 | 10106 | |
fbf5a39b AC |
10107 | if Is_Entity_Name (Operand) |
10108 | and then Range_Checks_Suppressed (Entity (Operand)) | |
10109 | then | |
10110 | return; | |
10111 | end if; | |
10112 | ||
685094bf RD |
10113 | -- Nothing to do if bounds are all static and we can tell that the |
10114 | -- expression is within the bounds of the target. Note that if the | |
10115 | -- operand is of an unconstrained floating-point type, then we do | |
10116 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
10117 | |
10118 | declare | |
f02b8bb8 RD |
10119 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); |
10120 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
fbf5a39b AC |
10121 | |
10122 | begin | |
10123 | if (not Is_Floating_Point_Type (Xtyp) | |
10124 | or else Is_Constrained (Xtyp)) | |
10125 | and then Compile_Time_Known_Value (S_Lo) | |
10126 | and then Compile_Time_Known_Value (S_Hi) | |
10127 | and then Compile_Time_Known_Value (Hi) | |
10128 | and then Compile_Time_Known_Value (Lo) | |
10129 | then | |
10130 | declare | |
10131 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
10132 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
10133 | S_Lov : Ureal; | |
10134 | S_Hiv : Ureal; | |
10135 | ||
10136 | begin | |
10137 | if Is_Real_Type (Xtyp) then | |
10138 | S_Lov := Expr_Value_R (S_Lo); | |
10139 | S_Hiv := Expr_Value_R (S_Hi); | |
10140 | else | |
10141 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
10142 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
10143 | end if; | |
10144 | ||
10145 | if D_Hiv > D_Lov | |
10146 | and then S_Lov >= D_Lov | |
10147 | and then S_Hiv <= D_Hiv | |
10148 | then | |
8b034336 AC |
10149 | -- Unset the range check flag on the current value of |
10150 | -- Expression (N), since the captured Operand may have | |
10151 | -- been rewritten (such as for the case of a conversion | |
10152 | -- to a fixed-point type). | |
10153 | ||
10154 | Set_Do_Range_Check (Expression (N), False); | |
10155 | ||
fbf5a39b AC |
10156 | return; |
10157 | end if; | |
10158 | end; | |
10159 | end if; | |
10160 | end; | |
10161 | ||
10162 | -- For float to float conversions, we are done | |
10163 | ||
10164 | if Is_Floating_Point_Type (Xtyp) | |
10165 | and then | |
10166 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
10167 | then |
10168 | return; | |
10169 | end if; | |
10170 | ||
fbf5a39b | 10171 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
10172 | |
10173 | Conv := Relocate_Node (N); | |
eaa826f8 | 10174 | Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); |
70482933 RK |
10175 | Set_Etype (Conv, Btyp); |
10176 | ||
f02b8bb8 RD |
10177 | -- Enable overflow except for case of integer to float conversions, |
10178 | -- where it is never required, since we can never have overflow in | |
10179 | -- this case. | |
70482933 | 10180 | |
fbf5a39b AC |
10181 | if not Is_Integer_Type (Etype (Operand)) then |
10182 | Enable_Overflow_Check (Conv); | |
70482933 RK |
10183 | end if; |
10184 | ||
191fcb3a | 10185 | Tnn := Make_Temporary (Loc, 'T', Conv); |
70482933 RK |
10186 | |
10187 | Insert_Actions (N, New_List ( | |
10188 | Make_Object_Declaration (Loc, | |
10189 | Defining_Identifier => Tnn, | |
10190 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
0ac2a660 AC |
10191 | Constant_Present => True, |
10192 | Expression => Conv), | |
70482933 RK |
10193 | |
10194 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
10195 | Condition => |
10196 | Make_Or_Else (Loc, | |
10197 | Left_Opnd => | |
10198 | Make_Op_Lt (Loc, | |
10199 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
10200 | Right_Opnd => | |
10201 | Make_Attribute_Reference (Loc, | |
10202 | Attribute_Name => Name_First, | |
10203 | Prefix => | |
10204 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 10205 | |
07fc65c4 GB |
10206 | Right_Opnd => |
10207 | Make_Op_Gt (Loc, | |
10208 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
10209 | Right_Opnd => | |
10210 | Make_Attribute_Reference (Loc, | |
10211 | Attribute_Name => Name_Last, | |
10212 | Prefix => | |
10213 | New_Occurrence_Of (Target_Type, Loc)))), | |
10214 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
10215 | |
10216 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
10217 | Analyze_And_Resolve (N, Btyp); | |
10218 | end Real_Range_Check; | |
10219 | ||
d15f9422 AC |
10220 | ----------------------------- |
10221 | -- Has_Extra_Accessibility -- | |
10222 | ----------------------------- | |
10223 | ||
10224 | -- Returns true for a formal of an anonymous access type or for | |
10225 | -- an Ada 2012-style stand-alone object of an anonymous access type. | |
10226 | ||
10227 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is | |
10228 | begin | |
10229 | if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then | |
10230 | return Present (Effective_Extra_Accessibility (Id)); | |
10231 | else | |
10232 | return False; | |
10233 | end if; | |
10234 | end Has_Extra_Accessibility; | |
10235 | ||
70482933 RK |
10236 | -- Start of processing for Expand_N_Type_Conversion |
10237 | ||
10238 | begin | |
83851b23 | 10239 | -- First remove check marks put by the semantic analysis on the type |
b2502161 AC |
10240 | -- conversion between array types. We need these checks, and they will |
10241 | -- be generated by this expansion routine, but we do not depend on these | |
10242 | -- flags being set, and since we do intend to expand the checks in the | |
10243 | -- front end, we don't want them on the tree passed to the back end. | |
83851b23 AC |
10244 | |
10245 | if Is_Array_Type (Target_Type) then | |
10246 | if Is_Constrained (Target_Type) then | |
10247 | Set_Do_Length_Check (N, False); | |
10248 | else | |
10249 | Set_Do_Range_Check (Operand, False); | |
10250 | end if; | |
10251 | end if; | |
10252 | ||
685094bf | 10253 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 AC |
10254 | -- the conversion completely, it is useless, except that it may carry |
10255 | -- an Assignment_OK attribute, which must be propagated to the operand. | |
70482933 RK |
10256 | |
10257 | if Operand_Type = Target_Type then | |
7b00e31d AC |
10258 | if Assignment_OK (N) then |
10259 | Set_Assignment_OK (Operand); | |
10260 | end if; | |
10261 | ||
fbf5a39b | 10262 | Rewrite (N, Relocate_Node (Operand)); |
e606088a | 10263 | goto Done; |
70482933 RK |
10264 | end if; |
10265 | ||
685094bf RD |
10266 | -- Nothing to do if this is the second argument of read. This is a |
10267 | -- "backwards" conversion that will be handled by the specialized code | |
10268 | -- in attribute processing. | |
70482933 RK |
10269 | |
10270 | if Nkind (Parent (N)) = N_Attribute_Reference | |
10271 | and then Attribute_Name (Parent (N)) = Name_Read | |
10272 | and then Next (First (Expressions (Parent (N)))) = N | |
10273 | then | |
e606088a AC |
10274 | goto Done; |
10275 | end if; | |
10276 | ||
10277 | -- Check for case of converting to a type that has an invariant | |
10278 | -- associated with it. This required an invariant check. We convert | |
10279 | ||
10280 | -- typ (expr) | |
10281 | ||
10282 | -- into | |
10283 | ||
10284 | -- do invariant_check (typ (expr)) in typ (expr); | |
10285 | ||
10286 | -- using Duplicate_Subexpr to avoid multiple side effects | |
10287 | ||
10288 | -- Note: the Comes_From_Source check, and then the resetting of this | |
10289 | -- flag prevents what would otherwise be an infinite recursion. | |
10290 | ||
fd0ff1cf RD |
10291 | if Has_Invariants (Target_Type) |
10292 | and then Present (Invariant_Procedure (Target_Type)) | |
e606088a AC |
10293 | and then Comes_From_Source (N) |
10294 | then | |
10295 | Set_Comes_From_Source (N, False); | |
10296 | Rewrite (N, | |
10297 | Make_Expression_With_Actions (Loc, | |
10298 | Actions => New_List ( | |
10299 | Make_Invariant_Call (Duplicate_Subexpr (N))), | |
10300 | Expression => Duplicate_Subexpr_No_Checks (N))); | |
10301 | Analyze_And_Resolve (N, Target_Type); | |
10302 | goto Done; | |
70482933 RK |
10303 | end if; |
10304 | ||
10305 | -- Here if we may need to expand conversion | |
10306 | ||
eaa826f8 RD |
10307 | -- If the operand of the type conversion is an arithmetic operation on |
10308 | -- signed integers, and the based type of the signed integer type in | |
10309 | -- question is smaller than Standard.Integer, we promote both of the | |
10310 | -- operands to type Integer. | |
10311 | ||
10312 | -- For example, if we have | |
10313 | ||
10314 | -- target-type (opnd1 + opnd2) | |
10315 | ||
10316 | -- and opnd1 and opnd2 are of type short integer, then we rewrite | |
10317 | -- this as: | |
10318 | ||
10319 | -- target-type (integer(opnd1) + integer(opnd2)) | |
10320 | ||
10321 | -- We do this because we are always allowed to compute in a larger type | |
10322 | -- if we do the right thing with the result, and in this case we are | |
10323 | -- going to do a conversion which will do an appropriate check to make | |
10324 | -- sure that things are in range of the target type in any case. This | |
10325 | -- avoids some unnecessary intermediate overflows. | |
10326 | ||
dfcfdc0a AC |
10327 | -- We might consider a similar transformation in the case where the |
10328 | -- target is a real type or a 64-bit integer type, and the operand | |
10329 | -- is an arithmetic operation using a 32-bit integer type. However, | |
10330 | -- we do not bother with this case, because it could cause significant | |
308e6f3a | 10331 | -- inefficiencies on 32-bit machines. On a 64-bit machine it would be |
dfcfdc0a AC |
10332 | -- much cheaper, but we don't want different behavior on 32-bit and |
10333 | -- 64-bit machines. Note that the exclusion of the 64-bit case also | |
10334 | -- handles the configurable run-time cases where 64-bit arithmetic | |
10335 | -- may simply be unavailable. | |
eaa826f8 RD |
10336 | |
10337 | -- Note: this circuit is partially redundant with respect to the circuit | |
10338 | -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in | |
10339 | -- the processing here. Also we still need the Checks circuit, since we | |
10340 | -- have to be sure not to generate junk overflow checks in the first | |
a90bd866 | 10341 | -- place, since it would be trick to remove them here. |
eaa826f8 | 10342 | |
fdfcc663 | 10343 | if Integer_Promotion_Possible (N) then |
eaa826f8 | 10344 | |
fdfcc663 | 10345 | -- All conditions met, go ahead with transformation |
eaa826f8 | 10346 | |
fdfcc663 AC |
10347 | declare |
10348 | Opnd : Node_Id; | |
10349 | L, R : Node_Id; | |
dfcfdc0a | 10350 | |
fdfcc663 AC |
10351 | begin |
10352 | R := | |
10353 | Make_Type_Conversion (Loc, | |
e4494292 | 10354 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 | 10355 | Expression => Relocate_Node (Right_Opnd (Operand))); |
eaa826f8 | 10356 | |
5f3f175d AC |
10357 | Opnd := New_Op_Node (Nkind (Operand), Loc); |
10358 | Set_Right_Opnd (Opnd, R); | |
eaa826f8 | 10359 | |
5f3f175d | 10360 | if Nkind (Operand) in N_Binary_Op then |
fdfcc663 | 10361 | L := |
eaa826f8 | 10362 | Make_Type_Conversion (Loc, |
e4494292 | 10363 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 AC |
10364 | Expression => Relocate_Node (Left_Opnd (Operand))); |
10365 | ||
5f3f175d AC |
10366 | Set_Left_Opnd (Opnd, L); |
10367 | end if; | |
eaa826f8 | 10368 | |
5f3f175d AC |
10369 | Rewrite (N, |
10370 | Make_Type_Conversion (Loc, | |
10371 | Subtype_Mark => Relocate_Node (Subtype_Mark (N)), | |
10372 | Expression => Opnd)); | |
dfcfdc0a | 10373 | |
5f3f175d | 10374 | Analyze_And_Resolve (N, Target_Type); |
e606088a | 10375 | goto Done; |
fdfcc663 AC |
10376 | end; |
10377 | end if; | |
eaa826f8 | 10378 | |
f82944b7 JM |
10379 | -- Do validity check if validity checking operands |
10380 | ||
533369aa | 10381 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
10382 | Ensure_Valid (Operand); |
10383 | end if; | |
10384 | ||
70482933 RK |
10385 | -- Special case of converting from non-standard boolean type |
10386 | ||
10387 | if Is_Boolean_Type (Operand_Type) | |
10388 | and then (Nonzero_Is_True (Operand_Type)) | |
10389 | then | |
10390 | Adjust_Condition (Operand); | |
10391 | Set_Etype (Operand, Standard_Boolean); | |
10392 | Operand_Type := Standard_Boolean; | |
10393 | end if; | |
10394 | ||
10395 | -- Case of converting to an access type | |
10396 | ||
10397 | if Is_Access_Type (Target_Type) then | |
10398 | ||
d766cee3 RD |
10399 | -- Apply an accessibility check when the conversion operand is an |
10400 | -- access parameter (or a renaming thereof), unless conversion was | |
e84e11ba GD |
10401 | -- expanded from an Unchecked_ or Unrestricted_Access attribute. |
10402 | -- Note that other checks may still need to be applied below (such | |
10403 | -- as tagged type checks). | |
70482933 RK |
10404 | |
10405 | if Is_Entity_Name (Operand) | |
d15f9422 | 10406 | and then Has_Extra_Accessibility (Entity (Operand)) |
70482933 | 10407 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type |
d766cee3 RD |
10408 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
10409 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 10410 | then |
e84e11ba GD |
10411 | Apply_Accessibility_Check |
10412 | (Operand, Target_Type, Insert_Node => Operand); | |
70482933 | 10413 | |
e84e11ba | 10414 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
10415 | -- level of the target type, then force Program_Error. Note that this |
10416 | -- can only occur for cases where the attribute is within the body of | |
10417 | -- an instantiation (otherwise the conversion will already have been | |
10418 | -- rejected as illegal). Note: warnings are issued by the analyzer | |
10419 | -- for the instance cases. | |
70482933 RK |
10420 | |
10421 | elsif In_Instance_Body | |
07fc65c4 GB |
10422 | and then Type_Access_Level (Operand_Type) > |
10423 | Type_Access_Level (Target_Type) | |
70482933 | 10424 | then |
426908f8 | 10425 | Raise_Accessibility_Error; |
70482933 | 10426 | |
685094bf RD |
10427 | -- When the operand is a selected access discriminant the check needs |
10428 | -- to be made against the level of the object denoted by the prefix | |
10429 | -- of the selected name. Force Program_Error for this case as well | |
10430 | -- (this accessibility violation can only happen if within the body | |
10431 | -- of an instantiation). | |
70482933 RK |
10432 | |
10433 | elsif In_Instance_Body | |
10434 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
10435 | and then Nkind (Operand) = N_Selected_Component | |
10436 | and then Object_Access_Level (Operand) > | |
10437 | Type_Access_Level (Target_Type) | |
10438 | then | |
426908f8 | 10439 | Raise_Accessibility_Error; |
e606088a | 10440 | goto Done; |
70482933 RK |
10441 | end if; |
10442 | end if; | |
10443 | ||
10444 | -- Case of conversions of tagged types and access to tagged types | |
10445 | ||
685094bf RD |
10446 | -- When needed, that is to say when the expression is class-wide, Add |
10447 | -- runtime a tag check for (strict) downward conversion by using the | |
10448 | -- membership test, generating: | |
70482933 RK |
10449 | |
10450 | -- [constraint_error when Operand not in Target_Type'Class] | |
10451 | ||
10452 | -- or in the access type case | |
10453 | ||
10454 | -- [constraint_error | |
10455 | -- when Operand /= null | |
10456 | -- and then Operand.all not in | |
10457 | -- Designated_Type (Target_Type)'Class] | |
10458 | ||
10459 | if (Is_Access_Type (Target_Type) | |
10460 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
10461 | or else Is_Tagged_Type (Target_Type) | |
10462 | then | |
685094bf RD |
10463 | -- Do not do any expansion in the access type case if the parent is a |
10464 | -- renaming, since this is an error situation which will be caught by | |
10465 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 | 10466 | |
e7e4d230 | 10467 | if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then |
e606088a | 10468 | goto Done; |
70482933 RK |
10469 | end if; |
10470 | ||
0669bebe | 10471 | -- Otherwise, proceed with processing tagged conversion |
70482933 | 10472 | |
e7e4d230 | 10473 | Tagged_Conversion : declare |
8cea7b64 HK |
10474 | Actual_Op_Typ : Entity_Id; |
10475 | Actual_Targ_Typ : Entity_Id; | |
10476 | Make_Conversion : Boolean := False; | |
10477 | Root_Op_Typ : Entity_Id; | |
70482933 | 10478 | |
8cea7b64 HK |
10479 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
10480 | -- Create a membership check to test whether Operand is a member | |
10481 | -- of Targ_Typ. If the original Target_Type is an access, include | |
10482 | -- a test for null value. The check is inserted at N. | |
10483 | ||
10484 | -------------------- | |
10485 | -- Make_Tag_Check -- | |
10486 | -------------------- | |
10487 | ||
10488 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
10489 | Cond : Node_Id; | |
10490 | ||
10491 | begin | |
10492 | -- Generate: | |
10493 | -- [Constraint_Error | |
10494 | -- when Operand /= null | |
10495 | -- and then Operand.all not in Targ_Typ] | |
10496 | ||
10497 | if Is_Access_Type (Target_Type) then | |
10498 | Cond := | |
10499 | Make_And_Then (Loc, | |
10500 | Left_Opnd => | |
10501 | Make_Op_Ne (Loc, | |
10502 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
10503 | Right_Opnd => Make_Null (Loc)), | |
10504 | ||
10505 | Right_Opnd => | |
10506 | Make_Not_In (Loc, | |
10507 | Left_Opnd => | |
10508 | Make_Explicit_Dereference (Loc, | |
10509 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
e4494292 | 10510 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc))); |
8cea7b64 HK |
10511 | |
10512 | -- Generate: | |
10513 | -- [Constraint_Error when Operand not in Targ_Typ] | |
10514 | ||
10515 | else | |
10516 | Cond := | |
10517 | Make_Not_In (Loc, | |
10518 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
e4494292 | 10519 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)); |
8cea7b64 HK |
10520 | end if; |
10521 | ||
10522 | Insert_Action (N, | |
10523 | Make_Raise_Constraint_Error (Loc, | |
10524 | Condition => Cond, | |
10525 | Reason => CE_Tag_Check_Failed)); | |
10526 | end Make_Tag_Check; | |
10527 | ||
e7e4d230 | 10528 | -- Start of processing for Tagged_Conversion |
70482933 RK |
10529 | |
10530 | begin | |
9732e886 | 10531 | -- Handle entities from the limited view |
852dba80 | 10532 | |
9732e886 | 10533 | if Is_Access_Type (Operand_Type) then |
852dba80 AC |
10534 | Actual_Op_Typ := |
10535 | Available_View (Designated_Type (Operand_Type)); | |
9732e886 JM |
10536 | else |
10537 | Actual_Op_Typ := Operand_Type; | |
10538 | end if; | |
10539 | ||
10540 | if Is_Access_Type (Target_Type) then | |
852dba80 AC |
10541 | Actual_Targ_Typ := |
10542 | Available_View (Designated_Type (Target_Type)); | |
70482933 | 10543 | else |
8cea7b64 | 10544 | Actual_Targ_Typ := Target_Type; |
70482933 RK |
10545 | end if; |
10546 | ||
8cea7b64 HK |
10547 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
10548 | ||
20b5d666 JM |
10549 | -- Ada 2005 (AI-251): Handle interface type conversion |
10550 | ||
8cea7b64 | 10551 | if Is_Interface (Actual_Op_Typ) then |
f6f4d8d4 | 10552 | Expand_Interface_Conversion (N); |
e606088a | 10553 | goto Done; |
20b5d666 JM |
10554 | end if; |
10555 | ||
8cea7b64 | 10556 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 10557 | |
8cea7b64 HK |
10558 | -- Create a runtime tag check for a downward class-wide type |
10559 | -- conversion. | |
70482933 | 10560 | |
8cea7b64 | 10561 | if Is_Class_Wide_Type (Actual_Op_Typ) |
852dba80 | 10562 | and then Actual_Op_Typ /= Actual_Targ_Typ |
8cea7b64 | 10563 | and then Root_Op_Typ /= Actual_Targ_Typ |
4ac2477e JM |
10564 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ, |
10565 | Use_Full_View => True) | |
8cea7b64 HK |
10566 | then |
10567 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
10568 | Make_Conversion := True; | |
10569 | end if; | |
70482933 | 10570 | |
8cea7b64 HK |
10571 | -- AI05-0073: If the result subtype of the function is defined |
10572 | -- by an access_definition designating a specific tagged type | |
10573 | -- T, a check is made that the result value is null or the tag | |
10574 | -- of the object designated by the result value identifies T. | |
10575 | -- Constraint_Error is raised if this check fails. | |
70482933 | 10576 | |
92a7cd46 | 10577 | if Nkind (Parent (N)) = N_Simple_Return_Statement then |
8cea7b64 | 10578 | declare |
e886436a | 10579 | Func : Entity_Id; |
8cea7b64 HK |
10580 | Func_Typ : Entity_Id; |
10581 | ||
10582 | begin | |
e886436a | 10583 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 10584 | |
e886436a | 10585 | Func := Current_Scope; |
8cea7b64 HK |
10586 | while Present (Func) |
10587 | and then Ekind (Func) /= E_Function | |
10588 | loop | |
10589 | Func := Scope (Func); | |
10590 | end loop; | |
10591 | ||
10592 | -- The function's return subtype must be defined using | |
10593 | -- an access definition. | |
10594 | ||
10595 | if Nkind (Result_Definition (Parent (Func))) = | |
10596 | N_Access_Definition | |
10597 | then | |
10598 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
10599 | ||
10600 | -- The return subtype denotes a specific tagged type, | |
10601 | -- in other words, a non class-wide type. | |
10602 | ||
10603 | if Is_Tagged_Type (Func_Typ) | |
10604 | and then not Is_Class_Wide_Type (Func_Typ) | |
10605 | then | |
10606 | Make_Tag_Check (Actual_Targ_Typ); | |
10607 | Make_Conversion := True; | |
10608 | end if; | |
10609 | end if; | |
10610 | end; | |
70482933 RK |
10611 | end if; |
10612 | ||
8cea7b64 HK |
10613 | -- We have generated a tag check for either a class-wide type |
10614 | -- conversion or for AI05-0073. | |
70482933 | 10615 | |
8cea7b64 HK |
10616 | if Make_Conversion then |
10617 | declare | |
10618 | Conv : Node_Id; | |
10619 | begin | |
10620 | Conv := | |
10621 | Make_Unchecked_Type_Conversion (Loc, | |
10622 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
10623 | Expression => Relocate_Node (Expression (N))); | |
10624 | Rewrite (N, Conv); | |
10625 | Analyze_And_Resolve (N, Target_Type); | |
10626 | end; | |
10627 | end if; | |
70482933 | 10628 | end if; |
e7e4d230 | 10629 | end Tagged_Conversion; |
70482933 RK |
10630 | |
10631 | -- Case of other access type conversions | |
10632 | ||
10633 | elsif Is_Access_Type (Target_Type) then | |
10634 | Apply_Constraint_Check (Operand, Target_Type); | |
10635 | ||
10636 | -- Case of conversions from a fixed-point type | |
10637 | ||
685094bf RD |
10638 | -- These conversions require special expansion and processing, found in |
10639 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
10640 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
10641 | -- conversions, which do not need further processing. |
10642 | ||
10643 | elsif Is_Fixed_Point_Type (Operand_Type) | |
10644 | and then not Conversion_OK (N) | |
10645 | then | |
10646 | -- We should never see universal fixed at this case, since the | |
10647 | -- expansion of the constituent divide or multiply should have | |
10648 | -- eliminated the explicit mention of universal fixed. | |
10649 | ||
10650 | pragma Assert (Operand_Type /= Universal_Fixed); | |
10651 | ||
685094bf RD |
10652 | -- Check for special case of the conversion to universal real that |
10653 | -- occurs as a result of the use of a round attribute. In this case, | |
10654 | -- the real type for the conversion is taken from the target type of | |
10655 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
10656 | |
10657 | if Target_Type = Universal_Real | |
10658 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
10659 | and then Attribute_Name (Parent (N)) = Name_Round | |
10660 | then | |
10661 | Set_Rounded_Result (N); | |
10662 | Set_Etype (N, Etype (Parent (N))); | |
10663 | end if; | |
10664 | ||
10665 | -- Otherwise do correct fixed-conversion, but skip these if the | |
e7e4d230 AC |
10666 | -- Conversion_OK flag is set, because from a semantic point of view |
10667 | -- these are simple integer conversions needing no further processing | |
10668 | -- (the backend will simply treat them as integers). | |
70482933 RK |
10669 | |
10670 | if not Conversion_OK (N) then | |
10671 | if Is_Fixed_Point_Type (Etype (N)) then | |
10672 | Expand_Convert_Fixed_To_Fixed (N); | |
10673 | Real_Range_Check; | |
10674 | ||
10675 | elsif Is_Integer_Type (Etype (N)) then | |
10676 | Expand_Convert_Fixed_To_Integer (N); | |
10677 | ||
10678 | else | |
10679 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
10680 | Expand_Convert_Fixed_To_Float (N); | |
10681 | Real_Range_Check; | |
10682 | end if; | |
10683 | end if; | |
10684 | ||
10685 | -- Case of conversions to a fixed-point type | |
10686 | ||
685094bf RD |
10687 | -- These conversions require special expansion and processing, found in |
10688 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
10689 | -- since from a semantic point of view, these are simple integer | |
10690 | -- conversions, which do not need further processing. | |
70482933 RK |
10691 | |
10692 | elsif Is_Fixed_Point_Type (Target_Type) | |
10693 | and then not Conversion_OK (N) | |
10694 | then | |
10695 | if Is_Integer_Type (Operand_Type) then | |
10696 | Expand_Convert_Integer_To_Fixed (N); | |
10697 | Real_Range_Check; | |
10698 | else | |
10699 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
10700 | Expand_Convert_Float_To_Fixed (N); | |
10701 | Real_Range_Check; | |
10702 | end if; | |
10703 | ||
10704 | -- Case of float-to-integer conversions | |
10705 | ||
10706 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
10707 | -- since semantically the fixed-point target is treated as though it | |
10708 | -- were an integer in such cases. | |
10709 | ||
10710 | elsif Is_Floating_Point_Type (Operand_Type) | |
10711 | and then | |
10712 | (Is_Integer_Type (Target_Type) | |
10713 | or else | |
10714 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
10715 | then | |
70482933 RK |
10716 | -- One more check here, gcc is still not able to do conversions of |
10717 | -- this type with proper overflow checking, and so gigi is doing an | |
10718 | -- approximation of what is required by doing floating-point compares | |
10719 | -- with the end-point. But that can lose precision in some cases, and | |
f02b8bb8 | 10720 | -- give a wrong result. Converting the operand to Universal_Real is |
70482933 | 10721 | -- helpful, but still does not catch all cases with 64-bit integers |
e7e4d230 | 10722 | -- on targets with only 64-bit floats. |
0669bebe GB |
10723 | |
10724 | -- The above comment seems obsoleted by Apply_Float_Conversion_Check | |
10725 | -- Can this code be removed ??? | |
70482933 | 10726 | |
fbf5a39b AC |
10727 | if Do_Range_Check (Operand) then |
10728 | Rewrite (Operand, | |
70482933 RK |
10729 | Make_Type_Conversion (Loc, |
10730 | Subtype_Mark => | |
f02b8bb8 | 10731 | New_Occurrence_Of (Universal_Real, Loc), |
70482933 | 10732 | Expression => |
fbf5a39b | 10733 | Relocate_Node (Operand))); |
70482933 | 10734 | |
f02b8bb8 | 10735 | Set_Etype (Operand, Universal_Real); |
fbf5a39b AC |
10736 | Enable_Range_Check (Operand); |
10737 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
10738 | end if; |
10739 | ||
10740 | -- Case of array conversions | |
10741 | ||
685094bf RD |
10742 | -- Expansion of array conversions, add required length/range checks but |
10743 | -- only do this if there is no change of representation. For handling of | |
10744 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
10745 | |
10746 | elsif Is_Array_Type (Target_Type) then | |
70482933 RK |
10747 | if Is_Constrained (Target_Type) then |
10748 | Apply_Length_Check (Operand, Target_Type); | |
10749 | else | |
10750 | Apply_Range_Check (Operand, Target_Type); | |
10751 | end if; | |
10752 | ||
10753 | Handle_Changed_Representation; | |
10754 | ||
10755 | -- Case of conversions of discriminated types | |
10756 | ||
685094bf RD |
10757 | -- Add required discriminant checks if target is constrained. Again this |
10758 | -- change is skipped if we have a change of representation. | |
70482933 RK |
10759 | |
10760 | elsif Has_Discriminants (Target_Type) | |
10761 | and then Is_Constrained (Target_Type) | |
10762 | then | |
10763 | Apply_Discriminant_Check (Operand, Target_Type); | |
10764 | Handle_Changed_Representation; | |
10765 | ||
10766 | -- Case of all other record conversions. The only processing required | |
10767 | -- is to check for a change of representation requiring the special | |
10768 | -- assignment processing. | |
10769 | ||
10770 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
10771 | |
10772 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
10773 | -- a derived Unchecked_Union type to an unconstrained type that is |
10774 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
10775 | |
10776 | if Is_Derived_Type (Operand_Type) | |
10777 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
10778 | and then not Is_Constrained (Target_Type) | |
10779 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
10780 | and then not Has_Inferable_Discriminants (Operand) | |
10781 | then | |
685094bf | 10782 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e | 10783 | -- Program_Error node, but we give it the target type of the |
6cb3037c | 10784 | -- conversion (is this requirement documented somewhere ???) |
5d09245e AC |
10785 | |
10786 | declare | |
10787 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
10788 | Reason => PE_Unchecked_Union_Restriction); | |
10789 | ||
10790 | begin | |
10791 | Set_Etype (PE, Target_Type); | |
10792 | Rewrite (N, PE); | |
10793 | ||
10794 | end; | |
10795 | else | |
10796 | Handle_Changed_Representation; | |
10797 | end if; | |
70482933 RK |
10798 | |
10799 | -- Case of conversions of enumeration types | |
10800 | ||
10801 | elsif Is_Enumeration_Type (Target_Type) then | |
10802 | ||
10803 | -- Special processing is required if there is a change of | |
e7e4d230 | 10804 | -- representation (from enumeration representation clauses). |
70482933 RK |
10805 | |
10806 | if not Same_Representation (Target_Type, Operand_Type) then | |
10807 | ||
10808 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
10809 | ||
10810 | Rewrite (N, | |
1c66c4f5 AC |
10811 | Make_Attribute_Reference (Loc, |
10812 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
10813 | Attribute_Name => Name_Val, | |
10814 | Expressions => New_List ( | |
10815 | Make_Attribute_Reference (Loc, | |
10816 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
10817 | Attribute_Name => Name_Pos, | |
10818 | Expressions => New_List (Operand))))); | |
70482933 RK |
10819 | |
10820 | Analyze_And_Resolve (N, Target_Type); | |
10821 | end if; | |
10822 | ||
10823 | -- Case of conversions to floating-point | |
10824 | ||
10825 | elsif Is_Floating_Point_Type (Target_Type) then | |
10826 | Real_Range_Check; | |
70482933 RK |
10827 | end if; |
10828 | ||
685094bf | 10829 | -- At this stage, either the conversion node has been transformed into |
e7e4d230 AC |
10830 | -- some other equivalent expression, or left as a conversion that can be |
10831 | -- handled by Gigi, in the following cases: | |
70482933 RK |
10832 | |
10833 | -- Conversions with no change of representation or type | |
10834 | ||
685094bf RD |
10835 | -- Numeric conversions involving integer, floating- and fixed-point |
10836 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
10837 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 10838 | |
5e1c00fa RD |
10839 | -- No other conversions should be passed to Gigi |
10840 | ||
10841 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 10842 | |
685094bf RD |
10843 | -- The only remaining step is to generate a range check if we still have |
10844 | -- a type conversion at this stage and Do_Range_Check is set. For now we | |
7b536495 AC |
10845 | -- do this only for conversions of discrete types and for floating-point |
10846 | -- conversions where the base types of source and target are the same. | |
fbf5a39b | 10847 | |
7b536495 | 10848 | if Nkind (N) = N_Type_Conversion then |
fbf5a39b | 10849 | |
7b536495 AC |
10850 | -- For now we only support floating-point cases where the base types |
10851 | -- of the target type and source expression are the same, so there's | |
10852 | -- potentially only a range check. Conversions where the source and | |
10853 | -- target have different base types are still TBD. ??? | |
fbf5a39b | 10854 | |
7b536495 AC |
10855 | if Is_Floating_Point_Type (Etype (N)) |
10856 | and then | |
10857 | Base_Type (Etype (N)) = Base_Type (Etype (Expression (N))) | |
10858 | then | |
10859 | if Do_Range_Check (Expression (N)) | |
10860 | and then Is_Floating_Point_Type (Target_Type) | |
10861 | then | |
10862 | Generate_Range_Check | |
10863 | (Expression (N), Target_Type, CE_Range_Check_Failed); | |
10864 | end if; | |
fbf5a39b | 10865 | |
7b536495 AC |
10866 | elsif Is_Discrete_Type (Etype (N)) then |
10867 | declare | |
10868 | Expr : constant Node_Id := Expression (N); | |
10869 | Ftyp : Entity_Id; | |
10870 | Ityp : Entity_Id; | |
fbf5a39b | 10871 | |
7b536495 AC |
10872 | begin |
10873 | if Do_Range_Check (Expr) | |
10874 | and then Is_Discrete_Type (Etype (Expr)) | |
fbf5a39b | 10875 | then |
7b536495 | 10876 | Set_Do_Range_Check (Expr, False); |
fbf5a39b | 10877 | |
7b536495 AC |
10878 | -- Before we do a range check, we have to deal with treating |
10879 | -- a fixed-point operand as an integer. The way we do this | |
10880 | -- is simply to do an unchecked conversion to an appropriate | |
10881 | -- integer type large enough to hold the result. | |
fbf5a39b | 10882 | |
7b536495 AC |
10883 | -- This code is not active yet, because we are only dealing |
10884 | -- with discrete types so far ??? | |
fbf5a39b | 10885 | |
7b536495 AC |
10886 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer |
10887 | and then Treat_Fixed_As_Integer (Expr) | |
10888 | then | |
10889 | Ftyp := Base_Type (Etype (Expr)); | |
fbf5a39b | 10890 | |
7b536495 AC |
10891 | if Esize (Ftyp) >= Esize (Standard_Integer) then |
10892 | Ityp := Standard_Long_Long_Integer; | |
10893 | else | |
10894 | Ityp := Standard_Integer; | |
10895 | end if; | |
edab6088 | 10896 | |
7b536495 AC |
10897 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); |
10898 | end if; | |
10899 | ||
10900 | -- Reset overflow flag, since the range check will include | |
10901 | -- dealing with possible overflow, and generate the check. | |
10902 | -- If Address is either a source type or target type, | |
10903 | -- suppress range check to avoid typing anomalies when | |
10904 | -- it is a visible integer type. | |
10905 | ||
10906 | Set_Do_Overflow_Check (N, False); | |
10907 | ||
10908 | if not Is_Descendent_Of_Address (Etype (Expr)) | |
10909 | and then not Is_Descendent_Of_Address (Target_Type) | |
10910 | then | |
10911 | Generate_Range_Check | |
10912 | (Expr, Target_Type, CE_Range_Check_Failed); | |
10913 | end if; | |
8a36a0cc | 10914 | end if; |
7b536495 AC |
10915 | end; |
10916 | end if; | |
fbf5a39b | 10917 | end if; |
f02b8bb8 | 10918 | |
e606088a AC |
10919 | -- Here at end of processing |
10920 | ||
48f91b44 RD |
10921 | <<Done>> |
10922 | -- Apply predicate check if required. Note that we can't just call | |
10923 | -- Apply_Predicate_Check here, because the type looks right after | |
10924 | -- the conversion and it would omit the check. The Comes_From_Source | |
10925 | -- guard is necessary to prevent infinite recursions when we generate | |
10926 | -- internal conversions for the purpose of checking predicates. | |
10927 | ||
10928 | if Present (Predicate_Function (Target_Type)) | |
10929 | and then Target_Type /= Operand_Type | |
10930 | and then Comes_From_Source (N) | |
10931 | then | |
00332244 AC |
10932 | declare |
10933 | New_Expr : constant Node_Id := Duplicate_Subexpr (N); | |
10934 | ||
10935 | begin | |
10936 | -- Avoid infinite recursion on the subsequent expansion of | |
10937 | -- of the copy of the original type conversion. | |
10938 | ||
10939 | Set_Comes_From_Source (New_Expr, False); | |
10940 | Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr)); | |
10941 | end; | |
48f91b44 | 10942 | end if; |
70482933 RK |
10943 | end Expand_N_Type_Conversion; |
10944 | ||
10945 | ----------------------------------- | |
10946 | -- Expand_N_Unchecked_Expression -- | |
10947 | ----------------------------------- | |
10948 | ||
e7e4d230 | 10949 | -- Remove the unchecked expression node from the tree. Its job was simply |
70482933 RK |
10950 | -- to make sure that its constituent expression was handled with checks |
10951 | -- off, and now that that is done, we can remove it from the tree, and | |
e7e4d230 | 10952 | -- indeed must, since Gigi does not expect to see these nodes. |
70482933 RK |
10953 | |
10954 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
10955 | Exp : constant Node_Id := Expression (N); | |
70482933 | 10956 | begin |
e7e4d230 | 10957 | Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp)); |
70482933 RK |
10958 | Rewrite (N, Exp); |
10959 | end Expand_N_Unchecked_Expression; | |
10960 | ||
10961 | ---------------------------------------- | |
10962 | -- Expand_N_Unchecked_Type_Conversion -- | |
10963 | ---------------------------------------- | |
10964 | ||
685094bf RD |
10965 | -- If this cannot be handled by Gigi and we haven't already made a |
10966 | -- temporary for it, do it now. | |
70482933 RK |
10967 | |
10968 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
10969 | Target_Type : constant Entity_Id := Etype (N); | |
10970 | Operand : constant Node_Id := Expression (N); | |
10971 | Operand_Type : constant Entity_Id := Etype (Operand); | |
10972 | ||
10973 | begin | |
7b00e31d | 10974 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 | 10975 | -- the conversion completely, it is useless, except that it may carry |
e7e4d230 | 10976 | -- an Assignment_OK indication which must be propagated to the operand. |
7b00e31d AC |
10977 | |
10978 | if Operand_Type = Target_Type then | |
13d923cc | 10979 | |
e7e4d230 AC |
10980 | -- Code duplicates Expand_N_Unchecked_Expression above, factor??? |
10981 | ||
7b00e31d AC |
10982 | if Assignment_OK (N) then |
10983 | Set_Assignment_OK (Operand); | |
10984 | end if; | |
10985 | ||
10986 | Rewrite (N, Relocate_Node (Operand)); | |
10987 | return; | |
10988 | end if; | |
10989 | ||
70482933 RK |
10990 | -- If we have a conversion of a compile time known value to a target |
10991 | -- type and the value is in range of the target type, then we can simply | |
10992 | -- replace the construct by an integer literal of the correct type. We | |
10993 | -- only apply this to integer types being converted. Possibly it may | |
10994 | -- apply in other cases, but it is too much trouble to worry about. | |
10995 | ||
10996 | -- Note that we do not do this transformation if the Kill_Range_Check | |
10997 | -- flag is set, since then the value may be outside the expected range. | |
10998 | -- This happens in the Normalize_Scalars case. | |
10999 | ||
20b5d666 JM |
11000 | -- We also skip this if either the target or operand type is biased |
11001 | -- because in this case, the unchecked conversion is supposed to | |
11002 | -- preserve the bit pattern, not the integer value. | |
11003 | ||
70482933 | 11004 | if Is_Integer_Type (Target_Type) |
20b5d666 | 11005 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 11006 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 11007 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
11008 | and then Compile_Time_Known_Value (Operand) |
11009 | and then not Kill_Range_Check (N) | |
11010 | then | |
11011 | declare | |
11012 | Val : constant Uint := Expr_Value (Operand); | |
11013 | ||
11014 | begin | |
11015 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
11016 | and then | |
11017 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
11018 | and then | |
11019 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
11020 | and then | |
11021 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
11022 | then | |
11023 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 11024 | |
685094bf RD |
11025 | -- If Address is the target type, just set the type to avoid a |
11026 | -- spurious type error on the literal when Address is a visible | |
11027 | -- integer type. | |
8a36a0cc AC |
11028 | |
11029 | if Is_Descendent_Of_Address (Target_Type) then | |
11030 | Set_Etype (N, Target_Type); | |
11031 | else | |
11032 | Analyze_And_Resolve (N, Target_Type); | |
11033 | end if; | |
11034 | ||
70482933 RK |
11035 | return; |
11036 | end if; | |
11037 | end; | |
11038 | end if; | |
11039 | ||
11040 | -- Nothing to do if conversion is safe | |
11041 | ||
11042 | if Safe_Unchecked_Type_Conversion (N) then | |
11043 | return; | |
11044 | end if; | |
11045 | ||
11046 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
324ac540 | 11047 | -- flag indicates ??? More comments needed here) |
70482933 RK |
11048 | |
11049 | if Assignment_OK (N) then | |
11050 | null; | |
11051 | else | |
11052 | Force_Evaluation (N); | |
11053 | end if; | |
11054 | end Expand_N_Unchecked_Type_Conversion; | |
11055 | ||
11056 | ---------------------------- | |
11057 | -- Expand_Record_Equality -- | |
11058 | ---------------------------- | |
11059 | ||
11060 | -- For non-variant records, Equality is expanded when needed into: | |
11061 | ||
11062 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
11063 | -- and then ... | |
11064 | -- and then Lhs.Discrn = Rhs.Discrn | |
11065 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
11066 | -- and then ... | |
11067 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
11068 | ||
11069 | -- The expression is folded by the back-end for adjacent fields. This | |
11070 | -- function is called for tagged record in only one occasion: for imple- | |
11071 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
11072 | -- otherwise the primitive "=" is used directly. | |
11073 | ||
11074 | function Expand_Record_Equality | |
11075 | (Nod : Node_Id; | |
11076 | Typ : Entity_Id; | |
11077 | Lhs : Node_Id; | |
11078 | Rhs : Node_Id; | |
2e071734 | 11079 | Bodies : List_Id) return Node_Id |
70482933 RK |
11080 | is |
11081 | Loc : constant Source_Ptr := Sloc (Nod); | |
11082 | ||
0ab80019 AC |
11083 | Result : Node_Id; |
11084 | C : Entity_Id; | |
11085 | ||
11086 | First_Time : Boolean := True; | |
11087 | ||
6b670dcf AC |
11088 | function Element_To_Compare (C : Entity_Id) return Entity_Id; |
11089 | -- Return the next discriminant or component to compare, starting with | |
11090 | -- C, skipping inherited components. | |
0ab80019 | 11091 | |
6b670dcf AC |
11092 | ------------------------ |
11093 | -- Element_To_Compare -- | |
11094 | ------------------------ | |
70482933 | 11095 | |
6b670dcf AC |
11096 | function Element_To_Compare (C : Entity_Id) return Entity_Id is |
11097 | Comp : Entity_Id; | |
28270211 | 11098 | |
70482933 | 11099 | begin |
6b670dcf | 11100 | Comp := C; |
6b670dcf AC |
11101 | loop |
11102 | -- Exit loop when the next element to be compared is found, or | |
11103 | -- there is no more such element. | |
70482933 | 11104 | |
6b670dcf | 11105 | exit when No (Comp); |
8190087e | 11106 | |
6b670dcf AC |
11107 | exit when Ekind_In (Comp, E_Discriminant, E_Component) |
11108 | and then not ( | |
70482933 | 11109 | |
6b670dcf | 11110 | -- Skip inherited components |
70482933 | 11111 | |
6b670dcf AC |
11112 | -- Note: for a tagged type, we always generate the "=" primitive |
11113 | -- for the base type (not on the first subtype), so the test for | |
11114 | -- Comp /= Original_Record_Component (Comp) is True for | |
11115 | -- inherited components only. | |
24558db8 | 11116 | |
6b670dcf | 11117 | (Is_Tagged_Type (Typ) |
28270211 | 11118 | and then Comp /= Original_Record_Component (Comp)) |
24558db8 | 11119 | |
6b670dcf | 11120 | -- Skip _Tag |
26bff3d9 | 11121 | |
6b670dcf AC |
11122 | or else Chars (Comp) = Name_uTag |
11123 | ||
11124 | -- The .NET/JVM version of type Root_Controlled contains two | |
11125 | -- fields which should not be considered part of the object. To | |
11126 | -- achieve proper equiality between two controlled objects on | |
11127 | -- .NET/JVM, skip _Parent whenever it has type Root_Controlled. | |
11128 | ||
11129 | or else (Chars (Comp) = Name_uParent | |
28270211 AC |
11130 | and then VM_Target /= No_VM |
11131 | and then Etype (Comp) = RTE (RE_Root_Controlled)) | |
6b670dcf AC |
11132 | |
11133 | -- Skip interface elements (secondary tags???) | |
11134 | ||
11135 | or else Is_Interface (Etype (Comp))); | |
11136 | ||
11137 | Next_Entity (Comp); | |
11138 | end loop; | |
11139 | ||
11140 | return Comp; | |
11141 | end Element_To_Compare; | |
70482933 | 11142 | |
70482933 RK |
11143 | -- Start of processing for Expand_Record_Equality |
11144 | ||
11145 | begin | |
70482933 RK |
11146 | -- Generates the following code: (assuming that Typ has one Discr and |
11147 | -- component C2 is also a record) | |
11148 | ||
11149 | -- True | |
11150 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
11151 | -- and then Lhs.C1 = Rhs.C1 | |
11152 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
11153 | -- and then ... | |
11154 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
11155 | ||
e4494292 | 11156 | Result := New_Occurrence_Of (Standard_True, Loc); |
6b670dcf | 11157 | C := Element_To_Compare (First_Entity (Typ)); |
70482933 | 11158 | while Present (C) loop |
70482933 RK |
11159 | declare |
11160 | New_Lhs : Node_Id; | |
11161 | New_Rhs : Node_Id; | |
8aceda64 | 11162 | Check : Node_Id; |
70482933 RK |
11163 | |
11164 | begin | |
11165 | if First_Time then | |
11166 | First_Time := False; | |
11167 | New_Lhs := Lhs; | |
11168 | New_Rhs := Rhs; | |
70482933 RK |
11169 | else |
11170 | New_Lhs := New_Copy_Tree (Lhs); | |
11171 | New_Rhs := New_Copy_Tree (Rhs); | |
11172 | end if; | |
11173 | ||
8aceda64 AC |
11174 | Check := |
11175 | Expand_Composite_Equality (Nod, Etype (C), | |
11176 | Lhs => | |
11177 | Make_Selected_Component (Loc, | |
8d80ff64 | 11178 | Prefix => New_Lhs, |
e4494292 | 11179 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
11180 | Rhs => |
11181 | Make_Selected_Component (Loc, | |
8d80ff64 | 11182 | Prefix => New_Rhs, |
e4494292 | 11183 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
11184 | Bodies => Bodies); |
11185 | ||
11186 | -- If some (sub)component is an unchecked_union, the whole | |
11187 | -- operation will raise program error. | |
11188 | ||
11189 | if Nkind (Check) = N_Raise_Program_Error then | |
11190 | Result := Check; | |
11191 | Set_Etype (Result, Standard_Boolean); | |
11192 | exit; | |
11193 | else | |
11194 | Result := | |
11195 | Make_And_Then (Loc, | |
11196 | Left_Opnd => Result, | |
11197 | Right_Opnd => Check); | |
11198 | end if; | |
70482933 RK |
11199 | end; |
11200 | ||
6b670dcf | 11201 | C := Element_To_Compare (Next_Entity (C)); |
70482933 RK |
11202 | end loop; |
11203 | ||
11204 | return Result; | |
11205 | end Expand_Record_Equality; | |
11206 | ||
a3068ca6 AC |
11207 | --------------------------- |
11208 | -- Expand_Set_Membership -- | |
11209 | --------------------------- | |
11210 | ||
11211 | procedure Expand_Set_Membership (N : Node_Id) is | |
11212 | Lop : constant Node_Id := Left_Opnd (N); | |
11213 | Alt : Node_Id; | |
11214 | Res : Node_Id; | |
11215 | ||
11216 | function Make_Cond (Alt : Node_Id) return Node_Id; | |
11217 | -- If the alternative is a subtype mark, create a simple membership | |
11218 | -- test. Otherwise create an equality test for it. | |
11219 | ||
11220 | --------------- | |
11221 | -- Make_Cond -- | |
11222 | --------------- | |
11223 | ||
11224 | function Make_Cond (Alt : Node_Id) return Node_Id is | |
11225 | Cond : Node_Id; | |
11226 | L : constant Node_Id := New_Copy (Lop); | |
11227 | R : constant Node_Id := Relocate_Node (Alt); | |
11228 | ||
11229 | begin | |
11230 | if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt))) | |
11231 | or else Nkind (Alt) = N_Range | |
11232 | then | |
11233 | Cond := | |
11234 | Make_In (Sloc (Alt), | |
11235 | Left_Opnd => L, | |
11236 | Right_Opnd => R); | |
11237 | else | |
11238 | Cond := | |
11239 | Make_Op_Eq (Sloc (Alt), | |
11240 | Left_Opnd => L, | |
11241 | Right_Opnd => R); | |
11242 | end if; | |
11243 | ||
11244 | return Cond; | |
11245 | end Make_Cond; | |
11246 | ||
11247 | -- Start of processing for Expand_Set_Membership | |
11248 | ||
11249 | begin | |
11250 | Remove_Side_Effects (Lop); | |
11251 | ||
11252 | Alt := Last (Alternatives (N)); | |
11253 | Res := Make_Cond (Alt); | |
11254 | ||
11255 | Prev (Alt); | |
11256 | while Present (Alt) loop | |
11257 | Res := | |
11258 | Make_Or_Else (Sloc (Alt), | |
11259 | Left_Opnd => Make_Cond (Alt), | |
11260 | Right_Opnd => Res); | |
11261 | Prev (Alt); | |
11262 | end loop; | |
11263 | ||
11264 | Rewrite (N, Res); | |
11265 | Analyze_And_Resolve (N, Standard_Boolean); | |
11266 | end Expand_Set_Membership; | |
11267 | ||
5875f8d6 AC |
11268 | ----------------------------------- |
11269 | -- Expand_Short_Circuit_Operator -- | |
11270 | ----------------------------------- | |
11271 | ||
955871d3 AC |
11272 | -- Deal with special expansion if actions are present for the right operand |
11273 | -- and deal with optimizing case of arguments being True or False. We also | |
11274 | -- deal with the special case of non-standard boolean values. | |
5875f8d6 AC |
11275 | |
11276 | procedure Expand_Short_Circuit_Operator (N : Node_Id) is | |
11277 | Loc : constant Source_Ptr := Sloc (N); | |
11278 | Typ : constant Entity_Id := Etype (N); | |
5875f8d6 AC |
11279 | Left : constant Node_Id := Left_Opnd (N); |
11280 | Right : constant Node_Id := Right_Opnd (N); | |
955871d3 | 11281 | LocR : constant Source_Ptr := Sloc (Right); |
5875f8d6 AC |
11282 | Actlist : List_Id; |
11283 | ||
11284 | Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else; | |
11285 | Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value); | |
11286 | -- If Left = Shortcut_Value then Right need not be evaluated | |
11287 | ||
5875f8d6 AC |
11288 | begin |
11289 | -- Deal with non-standard booleans | |
11290 | ||
11291 | if Is_Boolean_Type (Typ) then | |
11292 | Adjust_Condition (Left); | |
11293 | Adjust_Condition (Right); | |
11294 | Set_Etype (N, Standard_Boolean); | |
11295 | end if; | |
11296 | ||
11297 | -- Check for cases where left argument is known to be True or False | |
11298 | ||
11299 | if Compile_Time_Known_Value (Left) then | |
25adc5fb AC |
11300 | |
11301 | -- Mark SCO for left condition as compile time known | |
11302 | ||
11303 | if Generate_SCO and then Comes_From_Source (Left) then | |
11304 | Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True); | |
11305 | end if; | |
11306 | ||
5875f8d6 AC |
11307 | -- Rewrite True AND THEN Right / False OR ELSE Right to Right. |
11308 | -- Any actions associated with Right will be executed unconditionally | |
11309 | -- and can thus be inserted into the tree unconditionally. | |
11310 | ||
11311 | if Expr_Value_E (Left) /= Shortcut_Ent then | |
11312 | if Present (Actions (N)) then | |
11313 | Insert_Actions (N, Actions (N)); | |
11314 | end if; | |
11315 | ||
11316 | Rewrite (N, Right); | |
11317 | ||
11318 | -- Rewrite False AND THEN Right / True OR ELSE Right to Left. | |
11319 | -- In this case we can forget the actions associated with Right, | |
11320 | -- since they will never be executed. | |
11321 | ||
11322 | else | |
11323 | Kill_Dead_Code (Right); | |
11324 | Kill_Dead_Code (Actions (N)); | |
11325 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
11326 | end if; | |
11327 | ||
11328 | Adjust_Result_Type (N, Typ); | |
11329 | return; | |
11330 | end if; | |
11331 | ||
955871d3 AC |
11332 | -- If Actions are present for the right operand, we have to do some |
11333 | -- special processing. We can't just let these actions filter back into | |
11334 | -- code preceding the short circuit (which is what would have happened | |
11335 | -- if we had not trapped them in the short-circuit form), since they | |
11336 | -- must only be executed if the right operand of the short circuit is | |
11337 | -- executed and not otherwise. | |
5875f8d6 | 11338 | |
955871d3 AC |
11339 | if Present (Actions (N)) then |
11340 | Actlist := Actions (N); | |
5875f8d6 | 11341 | |
0812b84e AC |
11342 | -- We now use an Expression_With_Actions node for the right operand |
11343 | -- of the short-circuit form. Note that this solves the traceability | |
11344 | -- problems for coverage analysis. | |
5875f8d6 | 11345 | |
0812b84e AC |
11346 | Rewrite (Right, |
11347 | Make_Expression_With_Actions (LocR, | |
11348 | Expression => Relocate_Node (Right), | |
11349 | Actions => Actlist)); | |
11350 | Set_Actions (N, No_List); | |
11351 | Analyze_And_Resolve (Right, Standard_Boolean); | |
955871d3 | 11352 | |
5875f8d6 AC |
11353 | Adjust_Result_Type (N, Typ); |
11354 | return; | |
11355 | end if; | |
11356 | ||
11357 | -- No actions present, check for cases of right argument True/False | |
11358 | ||
11359 | if Compile_Time_Known_Value (Right) then | |
25adc5fb AC |
11360 | |
11361 | -- Mark SCO for left condition as compile time known | |
11362 | ||
11363 | if Generate_SCO and then Comes_From_Source (Right) then | |
11364 | Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True); | |
11365 | end if; | |
11366 | ||
5875f8d6 AC |
11367 | -- Change (Left and then True), (Left or else False) to Left. |
11368 | -- Note that we know there are no actions associated with the right | |
11369 | -- operand, since we just checked for this case above. | |
11370 | ||
11371 | if Expr_Value_E (Right) /= Shortcut_Ent then | |
11372 | Rewrite (N, Left); | |
11373 | ||
11374 | -- Change (Left and then False), (Left or else True) to Right, | |
11375 | -- making sure to preserve any side effects associated with the Left | |
11376 | -- operand. | |
11377 | ||
11378 | else | |
11379 | Remove_Side_Effects (Left); | |
11380 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
11381 | end if; | |
11382 | end if; | |
11383 | ||
11384 | Adjust_Result_Type (N, Typ); | |
11385 | end Expand_Short_Circuit_Operator; | |
11386 | ||
70482933 RK |
11387 | ------------------------------------- |
11388 | -- Fixup_Universal_Fixed_Operation -- | |
11389 | ------------------------------------- | |
11390 | ||
11391 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
11392 | Conv : constant Node_Id := Parent (N); | |
11393 | ||
11394 | begin | |
11395 | -- We must have a type conversion immediately above us | |
11396 | ||
11397 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
11398 | ||
11399 | -- Normally the type conversion gives our target type. The exception | |
11400 | -- occurs in the case of the Round attribute, where the conversion | |
11401 | -- will be to universal real, and our real type comes from the Round | |
11402 | -- attribute (as well as an indication that we must round the result) | |
11403 | ||
11404 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
11405 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
11406 | then | |
11407 | Set_Etype (N, Etype (Parent (Conv))); | |
11408 | Set_Rounded_Result (N); | |
11409 | ||
11410 | -- Normal case where type comes from conversion above us | |
11411 | ||
11412 | else | |
11413 | Set_Etype (N, Etype (Conv)); | |
11414 | end if; | |
11415 | end Fixup_Universal_Fixed_Operation; | |
11416 | ||
5d09245e AC |
11417 | --------------------------------- |
11418 | -- Has_Inferable_Discriminants -- | |
11419 | --------------------------------- | |
11420 | ||
11421 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
11422 | ||
11423 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
11424 | -- Determines whether the left-most prefix of a selected component is a | |
11425 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
11426 | ||
11427 | -------------------------------- | |
11428 | -- Prefix_Is_Formal_Parameter -- | |
11429 | -------------------------------- | |
11430 | ||
11431 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
83bb90af | 11432 | Sel_Comp : Node_Id; |
5d09245e AC |
11433 | |
11434 | begin | |
11435 | -- Move to the left-most prefix by climbing up the tree | |
11436 | ||
83bb90af | 11437 | Sel_Comp := N; |
5d09245e AC |
11438 | while Present (Parent (Sel_Comp)) |
11439 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
11440 | loop | |
11441 | Sel_Comp := Parent (Sel_Comp); | |
11442 | end loop; | |
11443 | ||
11444 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
11445 | end Prefix_Is_Formal_Parameter; | |
11446 | ||
11447 | -- Start of processing for Has_Inferable_Discriminants | |
11448 | ||
11449 | begin | |
5d09245e AC |
11450 | -- For selected components, the subtype of the selector must be a |
11451 | -- constrained Unchecked_Union. If the component is subject to a | |
11452 | -- per-object constraint, then the enclosing object must have inferable | |
11453 | -- discriminants. | |
11454 | ||
83bb90af | 11455 | if Nkind (N) = N_Selected_Component then |
5d09245e AC |
11456 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then |
11457 | ||
11458 | -- A small hack. If we have a per-object constrained selected | |
11459 | -- component of a formal parameter, return True since we do not | |
11460 | -- know the actual parameter association yet. | |
11461 | ||
11462 | if Prefix_Is_Formal_Parameter (N) then | |
11463 | return True; | |
5d09245e AC |
11464 | |
11465 | -- Otherwise, check the enclosing object and the selector | |
11466 | ||
83bb90af TQ |
11467 | else |
11468 | return Has_Inferable_Discriminants (Prefix (N)) | |
11469 | and then Has_Inferable_Discriminants (Selector_Name (N)); | |
11470 | end if; | |
5d09245e AC |
11471 | |
11472 | -- The call to Has_Inferable_Discriminants will determine whether | |
11473 | -- the selector has a constrained Unchecked_Union nominal type. | |
11474 | ||
83bb90af TQ |
11475 | else |
11476 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
11477 | end if; | |
5d09245e AC |
11478 | |
11479 | -- A qualified expression has inferable discriminants if its subtype | |
11480 | -- mark is a constrained Unchecked_Union subtype. | |
11481 | ||
11482 | elsif Nkind (N) = N_Qualified_Expression then | |
053cf994 | 11483 | return Is_Unchecked_Union (Etype (Subtype_Mark (N))) |
5b5b27ad | 11484 | and then Is_Constrained (Etype (Subtype_Mark (N))); |
5d09245e | 11485 | |
83bb90af TQ |
11486 | -- For all other names, it is sufficient to have a constrained |
11487 | -- Unchecked_Union nominal subtype. | |
11488 | ||
11489 | else | |
11490 | return Is_Unchecked_Union (Base_Type (Etype (N))) | |
11491 | and then Is_Constrained (Etype (N)); | |
11492 | end if; | |
5d09245e AC |
11493 | end Has_Inferable_Discriminants; |
11494 | ||
70482933 RK |
11495 | ------------------------------- |
11496 | -- Insert_Dereference_Action -- | |
11497 | ------------------------------- | |
11498 | ||
11499 | procedure Insert_Dereference_Action (N : Node_Id) is | |
8777c5a6 | 11500 | |
70482933 | 11501 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; |
2e071734 AC |
11502 | -- Return true if type of P is derived from Checked_Pool; |
11503 | ||
11504 | ----------------------------- | |
11505 | -- Is_Checked_Storage_Pool -- | |
11506 | ----------------------------- | |
70482933 RK |
11507 | |
11508 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
11509 | T : Entity_Id; | |
761f7dcb | 11510 | |
70482933 RK |
11511 | begin |
11512 | if No (P) then | |
11513 | return False; | |
11514 | end if; | |
11515 | ||
11516 | T := Etype (P); | |
11517 | while T /= Etype (T) loop | |
11518 | if Is_RTE (T, RE_Checked_Pool) then | |
11519 | return True; | |
11520 | else | |
11521 | T := Etype (T); | |
11522 | end if; | |
11523 | end loop; | |
11524 | ||
11525 | return False; | |
11526 | end Is_Checked_Storage_Pool; | |
11527 | ||
b0d71355 HK |
11528 | -- Local variables |
11529 | ||
11530 | Typ : constant Entity_Id := Etype (N); | |
11531 | Desig : constant Entity_Id := Available_View (Designated_Type (Typ)); | |
11532 | Loc : constant Source_Ptr := Sloc (N); | |
11533 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
11534 | Pnod : constant Node_Id := Parent (N); | |
11535 | ||
11536 | Addr : Entity_Id; | |
11537 | Alig : Entity_Id; | |
11538 | Deref : Node_Id; | |
11539 | Size : Entity_Id; | |
11540 | Stmt : Node_Id; | |
11541 | ||
70482933 RK |
11542 | -- Start of processing for Insert_Dereference_Action |
11543 | ||
11544 | begin | |
e6f69614 AC |
11545 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
11546 | ||
b0d71355 HK |
11547 | -- Do not re-expand a dereference which has already been processed by |
11548 | -- this routine. | |
11549 | ||
11550 | if Has_Dereference_Action (Pnod) then | |
70482933 | 11551 | return; |
70482933 | 11552 | |
b0d71355 HK |
11553 | -- Do not perform this type of expansion for internally-generated |
11554 | -- dereferences. | |
70482933 | 11555 | |
b0d71355 HK |
11556 | elsif not Comes_From_Source (Original_Node (Pnod)) then |
11557 | return; | |
70482933 | 11558 | |
b0d71355 HK |
11559 | -- A dereference action is only applicable to objects which have been |
11560 | -- allocated on a checked pool. | |
70482933 | 11561 | |
b0d71355 HK |
11562 | elsif not Is_Checked_Storage_Pool (Pool) then |
11563 | return; | |
11564 | end if; | |
70482933 | 11565 | |
b0d71355 | 11566 | -- Extract the address of the dereferenced object. Generate: |
8777c5a6 | 11567 | |
b0d71355 | 11568 | -- Addr : System.Address := <N>'Pool_Address; |
70482933 | 11569 | |
b0d71355 | 11570 | Addr := Make_Temporary (Loc, 'P'); |
70482933 | 11571 | |
b0d71355 HK |
11572 | Insert_Action (N, |
11573 | Make_Object_Declaration (Loc, | |
11574 | Defining_Identifier => Addr, | |
11575 | Object_Definition => | |
e4494292 | 11576 | New_Occurrence_Of (RTE (RE_Address), Loc), |
b0d71355 HK |
11577 | Expression => |
11578 | Make_Attribute_Reference (Loc, | |
11579 | Prefix => Duplicate_Subexpr_Move_Checks (N), | |
11580 | Attribute_Name => Name_Pool_Address))); | |
11581 | ||
11582 | -- Calculate the size of the dereferenced object. Generate: | |
8777c5a6 | 11583 | |
b0d71355 HK |
11584 | -- Size : Storage_Count := <N>.all'Size / Storage_Unit; |
11585 | ||
11586 | Deref := | |
11587 | Make_Explicit_Dereference (Loc, | |
11588 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11589 | Set_Has_Dereference_Action (Deref); | |
70482933 | 11590 | |
b0d71355 HK |
11591 | Size := Make_Temporary (Loc, 'S'); |
11592 | ||
11593 | Insert_Action (N, | |
11594 | Make_Object_Declaration (Loc, | |
11595 | Defining_Identifier => Size, | |
8777c5a6 | 11596 | |
b0d71355 | 11597 | Object_Definition => |
e4494292 | 11598 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
8777c5a6 | 11599 | |
b0d71355 HK |
11600 | Expression => |
11601 | Make_Op_Divide (Loc, | |
11602 | Left_Opnd => | |
70482933 | 11603 | Make_Attribute_Reference (Loc, |
b0d71355 | 11604 | Prefix => Deref, |
70482933 RK |
11605 | Attribute_Name => Name_Size), |
11606 | Right_Opnd => | |
b0d71355 | 11607 | Make_Integer_Literal (Loc, System_Storage_Unit)))); |
70482933 | 11608 | |
b0d71355 HK |
11609 | -- Calculate the alignment of the dereferenced object. Generate: |
11610 | -- Alig : constant Storage_Count := <N>.all'Alignment; | |
70482933 | 11611 | |
b0d71355 HK |
11612 | Deref := |
11613 | Make_Explicit_Dereference (Loc, | |
11614 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11615 | Set_Has_Dereference_Action (Deref); | |
11616 | ||
11617 | Alig := Make_Temporary (Loc, 'A'); | |
11618 | ||
11619 | Insert_Action (N, | |
11620 | Make_Object_Declaration (Loc, | |
11621 | Defining_Identifier => Alig, | |
11622 | Object_Definition => | |
e4494292 | 11623 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
b0d71355 HK |
11624 | Expression => |
11625 | Make_Attribute_Reference (Loc, | |
11626 | Prefix => Deref, | |
11627 | Attribute_Name => Name_Alignment))); | |
11628 | ||
11629 | -- A dereference of a controlled object requires special processing. The | |
11630 | -- finalization machinery requests additional space from the underlying | |
11631 | -- pool to allocate and hide two pointers. As a result, a checked pool | |
11632 | -- may mark the wrong memory as valid. Since checked pools do not have | |
11633 | -- knowledge of hidden pointers, we have to bring the two pointers back | |
11634 | -- in view in order to restore the original state of the object. | |
11635 | ||
11636 | if Needs_Finalization (Desig) then | |
11637 | ||
11638 | -- Adjust the address and size of the dereferenced object. Generate: | |
11639 | -- Adjust_Controlled_Dereference (Addr, Size, Alig); | |
11640 | ||
11641 | Stmt := | |
11642 | Make_Procedure_Call_Statement (Loc, | |
11643 | Name => | |
e4494292 | 11644 | New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc), |
b0d71355 | 11645 | Parameter_Associations => New_List ( |
e4494292 RD |
11646 | New_Occurrence_Of (Addr, Loc), |
11647 | New_Occurrence_Of (Size, Loc), | |
11648 | New_Occurrence_Of (Alig, Loc))); | |
b0d71355 HK |
11649 | |
11650 | -- Class-wide types complicate things because we cannot determine | |
11651 | -- statically whether the actual object is truly controlled. We must | |
11652 | -- generate a runtime check to detect this property. Generate: | |
11653 | -- | |
11654 | -- if Needs_Finalization (<N>.all'Tag) then | |
11655 | -- <Stmt>; | |
11656 | -- end if; | |
11657 | ||
11658 | if Is_Class_Wide_Type (Desig) then | |
11659 | Deref := | |
11660 | Make_Explicit_Dereference (Loc, | |
11661 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11662 | Set_Has_Dereference_Action (Deref); | |
11663 | ||
11664 | Stmt := | |
8b1011c0 | 11665 | Make_Implicit_If_Statement (N, |
b0d71355 HK |
11666 | Condition => |
11667 | Make_Function_Call (Loc, | |
11668 | Name => | |
e4494292 | 11669 | New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc), |
b0d71355 HK |
11670 | Parameter_Associations => New_List ( |
11671 | Make_Attribute_Reference (Loc, | |
11672 | Prefix => Deref, | |
11673 | Attribute_Name => Name_Tag))), | |
11674 | Then_Statements => New_List (Stmt)); | |
11675 | end if; | |
11676 | ||
11677 | Insert_Action (N, Stmt); | |
11678 | end if; | |
11679 | ||
11680 | -- Generate: | |
11681 | -- Dereference (Pool, Addr, Size, Alig); | |
11682 | ||
11683 | Insert_Action (N, | |
11684 | Make_Procedure_Call_Statement (Loc, | |
11685 | Name => | |
e4494292 | 11686 | New_Occurrence_Of |
b0d71355 HK |
11687 | (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), |
11688 | Parameter_Associations => New_List ( | |
e4494292 RD |
11689 | New_Occurrence_Of (Pool, Loc), |
11690 | New_Occurrence_Of (Addr, Loc), | |
11691 | New_Occurrence_Of (Size, Loc), | |
11692 | New_Occurrence_Of (Alig, Loc)))); | |
b0d71355 HK |
11693 | |
11694 | -- Mark the explicit dereference as processed to avoid potential | |
11695 | -- infinite expansion. | |
11696 | ||
11697 | Set_Has_Dereference_Action (Pnod); | |
70482933 | 11698 | |
fbf5a39b AC |
11699 | exception |
11700 | when RE_Not_Available => | |
11701 | return; | |
70482933 RK |
11702 | end Insert_Dereference_Action; |
11703 | ||
fdfcc663 AC |
11704 | -------------------------------- |
11705 | -- Integer_Promotion_Possible -- | |
11706 | -------------------------------- | |
11707 | ||
11708 | function Integer_Promotion_Possible (N : Node_Id) return Boolean is | |
11709 | Operand : constant Node_Id := Expression (N); | |
11710 | Operand_Type : constant Entity_Id := Etype (Operand); | |
11711 | Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type); | |
11712 | ||
11713 | begin | |
11714 | pragma Assert (Nkind (N) = N_Type_Conversion); | |
11715 | ||
11716 | return | |
11717 | ||
11718 | -- We only do the transformation for source constructs. We assume | |
11719 | -- that the expander knows what it is doing when it generates code. | |
11720 | ||
11721 | Comes_From_Source (N) | |
11722 | ||
11723 | -- If the operand type is Short_Integer or Short_Short_Integer, | |
11724 | -- then we will promote to Integer, which is available on all | |
11725 | -- targets, and is sufficient to ensure no intermediate overflow. | |
11726 | -- Furthermore it is likely to be as efficient or more efficient | |
11727 | -- than using the smaller type for the computation so we do this | |
11728 | -- unconditionally. | |
11729 | ||
11730 | and then | |
11731 | (Root_Operand_Type = Base_Type (Standard_Short_Integer) | |
761f7dcb | 11732 | or else |
fdfcc663 AC |
11733 | Root_Operand_Type = Base_Type (Standard_Short_Short_Integer)) |
11734 | ||
11735 | -- Test for interesting operation, which includes addition, | |
5f3f175d AC |
11736 | -- division, exponentiation, multiplication, subtraction, absolute |
11737 | -- value and unary negation. Unary "+" is omitted since it is a | |
11738 | -- no-op and thus can't overflow. | |
fdfcc663 | 11739 | |
5f3f175d AC |
11740 | and then Nkind_In (Operand, N_Op_Abs, |
11741 | N_Op_Add, | |
fdfcc663 AC |
11742 | N_Op_Divide, |
11743 | N_Op_Expon, | |
11744 | N_Op_Minus, | |
11745 | N_Op_Multiply, | |
11746 | N_Op_Subtract); | |
11747 | end Integer_Promotion_Possible; | |
11748 | ||
70482933 RK |
11749 | ------------------------------ |
11750 | -- Make_Array_Comparison_Op -- | |
11751 | ------------------------------ | |
11752 | ||
11753 | -- This is a hand-coded expansion of the following generic function: | |
11754 | ||
11755 | -- generic | |
11756 | -- type elem is (<>); | |
11757 | -- type index is (<>); | |
11758 | -- type a is array (index range <>) of elem; | |
20b5d666 | 11759 | |
70482933 RK |
11760 | -- function Gnnn (X : a; Y: a) return boolean is |
11761 | -- J : index := Y'first; | |
20b5d666 | 11762 | |
70482933 RK |
11763 | -- begin |
11764 | -- if X'length = 0 then | |
11765 | -- return false; | |
20b5d666 | 11766 | |
70482933 RK |
11767 | -- elsif Y'length = 0 then |
11768 | -- return true; | |
20b5d666 | 11769 | |
70482933 RK |
11770 | -- else |
11771 | -- for I in X'range loop | |
11772 | -- if X (I) = Y (J) then | |
11773 | -- if J = Y'last then | |
11774 | -- exit; | |
11775 | -- else | |
11776 | -- J := index'succ (J); | |
11777 | -- end if; | |
20b5d666 | 11778 | |
70482933 RK |
11779 | -- else |
11780 | -- return X (I) > Y (J); | |
11781 | -- end if; | |
11782 | -- end loop; | |
20b5d666 | 11783 | |
70482933 RK |
11784 | -- return X'length > Y'length; |
11785 | -- end if; | |
11786 | -- end Gnnn; | |
11787 | ||
11788 | -- Note that since we are essentially doing this expansion by hand, we | |
11789 | -- do not need to generate an actual or formal generic part, just the | |
11790 | -- instantiated function itself. | |
11791 | ||
bb012790 AC |
11792 | -- Perhaps we could have the actual generic available in the run-time, |
11793 | -- obtained by rtsfind, and actually expand a real instantiation ??? | |
11794 | ||
70482933 | 11795 | function Make_Array_Comparison_Op |
2e071734 AC |
11796 | (Typ : Entity_Id; |
11797 | Nod : Node_Id) return Node_Id | |
70482933 RK |
11798 | is |
11799 | Loc : constant Source_Ptr := Sloc (Nod); | |
11800 | ||
11801 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
11802 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
11803 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
11804 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
11805 | ||
11806 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
11807 | ||
11808 | Loop_Statement : Node_Id; | |
11809 | Loop_Body : Node_Id; | |
11810 | If_Stat : Node_Id; | |
11811 | Inner_If : Node_Id; | |
11812 | Final_Expr : Node_Id; | |
11813 | Func_Body : Node_Id; | |
11814 | Func_Name : Entity_Id; | |
11815 | Formals : List_Id; | |
11816 | Length1 : Node_Id; | |
11817 | Length2 : Node_Id; | |
11818 | ||
11819 | begin | |
11820 | -- if J = Y'last then | |
11821 | -- exit; | |
11822 | -- else | |
11823 | -- J := index'succ (J); | |
11824 | -- end if; | |
11825 | ||
11826 | Inner_If := | |
11827 | Make_Implicit_If_Statement (Nod, | |
11828 | Condition => | |
11829 | Make_Op_Eq (Loc, | |
e4494292 | 11830 | Left_Opnd => New_Occurrence_Of (J, Loc), |
70482933 RK |
11831 | Right_Opnd => |
11832 | Make_Attribute_Reference (Loc, | |
e4494292 | 11833 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
11834 | Attribute_Name => Name_Last)), |
11835 | ||
11836 | Then_Statements => New_List ( | |
11837 | Make_Exit_Statement (Loc)), | |
11838 | ||
11839 | Else_Statements => | |
11840 | New_List ( | |
11841 | Make_Assignment_Statement (Loc, | |
e4494292 | 11842 | Name => New_Occurrence_Of (J, Loc), |
70482933 RK |
11843 | Expression => |
11844 | Make_Attribute_Reference (Loc, | |
e4494292 | 11845 | Prefix => New_Occurrence_Of (Index, Loc), |
70482933 | 11846 | Attribute_Name => Name_Succ, |
e4494292 | 11847 | Expressions => New_List (New_Occurrence_Of (J, Loc)))))); |
70482933 RK |
11848 | |
11849 | -- if X (I) = Y (J) then | |
11850 | -- if ... end if; | |
11851 | -- else | |
11852 | -- return X (I) > Y (J); | |
11853 | -- end if; | |
11854 | ||
11855 | Loop_Body := | |
11856 | Make_Implicit_If_Statement (Nod, | |
11857 | Condition => | |
11858 | Make_Op_Eq (Loc, | |
11859 | Left_Opnd => | |
11860 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11861 | Prefix => New_Occurrence_Of (X, Loc), |
11862 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
11863 | |
11864 | Right_Opnd => | |
11865 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11866 | Prefix => New_Occurrence_Of (Y, Loc), |
11867 | Expressions => New_List (New_Occurrence_Of (J, Loc)))), | |
70482933 RK |
11868 | |
11869 | Then_Statements => New_List (Inner_If), | |
11870 | ||
11871 | Else_Statements => New_List ( | |
d766cee3 | 11872 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
11873 | Expression => |
11874 | Make_Op_Gt (Loc, | |
11875 | Left_Opnd => | |
11876 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11877 | Prefix => New_Occurrence_Of (X, Loc), |
11878 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
11879 | |
11880 | Right_Opnd => | |
11881 | Make_Indexed_Component (Loc, | |
e4494292 | 11882 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 | 11883 | Expressions => New_List ( |
e4494292 | 11884 | New_Occurrence_Of (J, Loc))))))); |
70482933 RK |
11885 | |
11886 | -- for I in X'range loop | |
11887 | -- if ... end if; | |
11888 | -- end loop; | |
11889 | ||
11890 | Loop_Statement := | |
11891 | Make_Implicit_Loop_Statement (Nod, | |
11892 | Identifier => Empty, | |
11893 | ||
11894 | Iteration_Scheme => | |
11895 | Make_Iteration_Scheme (Loc, | |
11896 | Loop_Parameter_Specification => | |
11897 | Make_Loop_Parameter_Specification (Loc, | |
11898 | Defining_Identifier => I, | |
11899 | Discrete_Subtype_Definition => | |
11900 | Make_Attribute_Reference (Loc, | |
e4494292 | 11901 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
11902 | Attribute_Name => Name_Range))), |
11903 | ||
11904 | Statements => New_List (Loop_Body)); | |
11905 | ||
11906 | -- if X'length = 0 then | |
11907 | -- return false; | |
11908 | -- elsif Y'length = 0 then | |
11909 | -- return true; | |
11910 | -- else | |
11911 | -- for ... loop ... end loop; | |
11912 | -- return X'length > Y'length; | |
11913 | -- end if; | |
11914 | ||
11915 | Length1 := | |
11916 | Make_Attribute_Reference (Loc, | |
e4494292 | 11917 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
11918 | Attribute_Name => Name_Length); |
11919 | ||
11920 | Length2 := | |
11921 | Make_Attribute_Reference (Loc, | |
e4494292 | 11922 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
11923 | Attribute_Name => Name_Length); |
11924 | ||
11925 | Final_Expr := | |
11926 | Make_Op_Gt (Loc, | |
11927 | Left_Opnd => Length1, | |
11928 | Right_Opnd => Length2); | |
11929 | ||
11930 | If_Stat := | |
11931 | Make_Implicit_If_Statement (Nod, | |
11932 | Condition => | |
11933 | Make_Op_Eq (Loc, | |
11934 | Left_Opnd => | |
11935 | Make_Attribute_Reference (Loc, | |
e4494292 | 11936 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
11937 | Attribute_Name => Name_Length), |
11938 | Right_Opnd => | |
11939 | Make_Integer_Literal (Loc, 0)), | |
11940 | ||
11941 | Then_Statements => | |
11942 | New_List ( | |
d766cee3 | 11943 | Make_Simple_Return_Statement (Loc, |
e4494292 | 11944 | Expression => New_Occurrence_Of (Standard_False, Loc))), |
70482933 RK |
11945 | |
11946 | Elsif_Parts => New_List ( | |
11947 | Make_Elsif_Part (Loc, | |
11948 | Condition => | |
11949 | Make_Op_Eq (Loc, | |
11950 | Left_Opnd => | |
11951 | Make_Attribute_Reference (Loc, | |
e4494292 | 11952 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
11953 | Attribute_Name => Name_Length), |
11954 | Right_Opnd => | |
11955 | Make_Integer_Literal (Loc, 0)), | |
11956 | ||
11957 | Then_Statements => | |
11958 | New_List ( | |
d766cee3 | 11959 | Make_Simple_Return_Statement (Loc, |
e4494292 | 11960 | Expression => New_Occurrence_Of (Standard_True, Loc))))), |
70482933 RK |
11961 | |
11962 | Else_Statements => New_List ( | |
11963 | Loop_Statement, | |
d766cee3 | 11964 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
11965 | Expression => Final_Expr))); |
11966 | ||
11967 | -- (X : a; Y: a) | |
11968 | ||
11969 | Formals := New_List ( | |
11970 | Make_Parameter_Specification (Loc, | |
11971 | Defining_Identifier => X, | |
e4494292 | 11972 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
11973 | |
11974 | Make_Parameter_Specification (Loc, | |
11975 | Defining_Identifier => Y, | |
e4494292 | 11976 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 RK |
11977 | |
11978 | -- function Gnnn (...) return boolean is | |
11979 | -- J : index := Y'first; | |
11980 | -- begin | |
11981 | -- if ... end if; | |
11982 | -- end Gnnn; | |
11983 | ||
191fcb3a | 11984 | Func_Name := Make_Temporary (Loc, 'G'); |
70482933 RK |
11985 | |
11986 | Func_Body := | |
11987 | Make_Subprogram_Body (Loc, | |
11988 | Specification => | |
11989 | Make_Function_Specification (Loc, | |
11990 | Defining_Unit_Name => Func_Name, | |
11991 | Parameter_Specifications => Formals, | |
e4494292 | 11992 | Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)), |
70482933 RK |
11993 | |
11994 | Declarations => New_List ( | |
11995 | Make_Object_Declaration (Loc, | |
11996 | Defining_Identifier => J, | |
e4494292 | 11997 | Object_Definition => New_Occurrence_Of (Index, Loc), |
70482933 RK |
11998 | Expression => |
11999 | Make_Attribute_Reference (Loc, | |
e4494292 | 12000 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
12001 | Attribute_Name => Name_First))), |
12002 | ||
12003 | Handled_Statement_Sequence => | |
12004 | Make_Handled_Sequence_Of_Statements (Loc, | |
12005 | Statements => New_List (If_Stat))); | |
12006 | ||
12007 | return Func_Body; | |
70482933 RK |
12008 | end Make_Array_Comparison_Op; |
12009 | ||
12010 | --------------------------- | |
12011 | -- Make_Boolean_Array_Op -- | |
12012 | --------------------------- | |
12013 | ||
685094bf RD |
12014 | -- For logical operations on boolean arrays, expand in line the following, |
12015 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
12016 | |
12017 | -- function Annn (A : typ; B: typ) return typ is | |
12018 | -- C : typ; | |
12019 | -- begin | |
12020 | -- for J in A'range loop | |
12021 | -- C (J) := A (J) op B (J); | |
12022 | -- end loop; | |
12023 | -- return C; | |
12024 | -- end Annn; | |
12025 | ||
12026 | -- Here typ is the boolean array type | |
12027 | ||
12028 | function Make_Boolean_Array_Op | |
2e071734 AC |
12029 | (Typ : Entity_Id; |
12030 | N : Node_Id) return Node_Id | |
70482933 RK |
12031 | is |
12032 | Loc : constant Source_Ptr := Sloc (N); | |
12033 | ||
12034 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
12035 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
12036 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
12037 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
12038 | ||
12039 | A_J : Node_Id; | |
12040 | B_J : Node_Id; | |
12041 | C_J : Node_Id; | |
12042 | Op : Node_Id; | |
12043 | ||
12044 | Formals : List_Id; | |
12045 | Func_Name : Entity_Id; | |
12046 | Func_Body : Node_Id; | |
12047 | Loop_Statement : Node_Id; | |
12048 | ||
12049 | begin | |
12050 | A_J := | |
12051 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12052 | Prefix => New_Occurrence_Of (A, Loc), |
12053 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12054 | |
12055 | B_J := | |
12056 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12057 | Prefix => New_Occurrence_Of (B, Loc), |
12058 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12059 | |
12060 | C_J := | |
12061 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12062 | Prefix => New_Occurrence_Of (C, Loc), |
12063 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12064 | |
12065 | if Nkind (N) = N_Op_And then | |
12066 | Op := | |
12067 | Make_Op_And (Loc, | |
12068 | Left_Opnd => A_J, | |
12069 | Right_Opnd => B_J); | |
12070 | ||
12071 | elsif Nkind (N) = N_Op_Or then | |
12072 | Op := | |
12073 | Make_Op_Or (Loc, | |
12074 | Left_Opnd => A_J, | |
12075 | Right_Opnd => B_J); | |
12076 | ||
12077 | else | |
12078 | Op := | |
12079 | Make_Op_Xor (Loc, | |
12080 | Left_Opnd => A_J, | |
12081 | Right_Opnd => B_J); | |
12082 | end if; | |
12083 | ||
12084 | Loop_Statement := | |
12085 | Make_Implicit_Loop_Statement (N, | |
12086 | Identifier => Empty, | |
12087 | ||
12088 | Iteration_Scheme => | |
12089 | Make_Iteration_Scheme (Loc, | |
12090 | Loop_Parameter_Specification => | |
12091 | Make_Loop_Parameter_Specification (Loc, | |
12092 | Defining_Identifier => J, | |
12093 | Discrete_Subtype_Definition => | |
12094 | Make_Attribute_Reference (Loc, | |
e4494292 | 12095 | Prefix => New_Occurrence_Of (A, Loc), |
70482933 RK |
12096 | Attribute_Name => Name_Range))), |
12097 | ||
12098 | Statements => New_List ( | |
12099 | Make_Assignment_Statement (Loc, | |
12100 | Name => C_J, | |
12101 | Expression => Op))); | |
12102 | ||
12103 | Formals := New_List ( | |
12104 | Make_Parameter_Specification (Loc, | |
12105 | Defining_Identifier => A, | |
e4494292 | 12106 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
12107 | |
12108 | Make_Parameter_Specification (Loc, | |
12109 | Defining_Identifier => B, | |
e4494292 | 12110 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 | 12111 | |
191fcb3a | 12112 | Func_Name := Make_Temporary (Loc, 'A'); |
70482933 RK |
12113 | Set_Is_Inlined (Func_Name); |
12114 | ||
12115 | Func_Body := | |
12116 | Make_Subprogram_Body (Loc, | |
12117 | Specification => | |
12118 | Make_Function_Specification (Loc, | |
12119 | Defining_Unit_Name => Func_Name, | |
12120 | Parameter_Specifications => Formals, | |
e4494292 | 12121 | Result_Definition => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
12122 | |
12123 | Declarations => New_List ( | |
12124 | Make_Object_Declaration (Loc, | |
12125 | Defining_Identifier => C, | |
e4494292 | 12126 | Object_Definition => New_Occurrence_Of (Typ, Loc))), |
70482933 RK |
12127 | |
12128 | Handled_Statement_Sequence => | |
12129 | Make_Handled_Sequence_Of_Statements (Loc, | |
12130 | Statements => New_List ( | |
12131 | Loop_Statement, | |
d766cee3 | 12132 | Make_Simple_Return_Statement (Loc, |
e4494292 | 12133 | Expression => New_Occurrence_Of (C, Loc))))); |
70482933 RK |
12134 | |
12135 | return Func_Body; | |
12136 | end Make_Boolean_Array_Op; | |
12137 | ||
b6b5cca8 AC |
12138 | ----------------------------------------- |
12139 | -- Minimized_Eliminated_Overflow_Check -- | |
12140 | ----------------------------------------- | |
12141 | ||
12142 | function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is | |
12143 | begin | |
12144 | return | |
12145 | Is_Signed_Integer_Type (Etype (N)) | |
a7f1b24f | 12146 | and then Overflow_Check_Mode in Minimized_Or_Eliminated; |
b6b5cca8 AC |
12147 | end Minimized_Eliminated_Overflow_Check; |
12148 | ||
0580d807 AC |
12149 | -------------------------------- |
12150 | -- Optimize_Length_Comparison -- | |
12151 | -------------------------------- | |
12152 | ||
12153 | procedure Optimize_Length_Comparison (N : Node_Id) is | |
12154 | Loc : constant Source_Ptr := Sloc (N); | |
12155 | Typ : constant Entity_Id := Etype (N); | |
12156 | Result : Node_Id; | |
12157 | ||
12158 | Left : Node_Id; | |
12159 | Right : Node_Id; | |
12160 | -- First and Last attribute reference nodes, which end up as left and | |
12161 | -- right operands of the optimized result. | |
12162 | ||
12163 | Is_Zero : Boolean; | |
12164 | -- True for comparison operand of zero | |
12165 | ||
12166 | Comp : Node_Id; | |
12167 | -- Comparison operand, set only if Is_Zero is false | |
12168 | ||
12169 | Ent : Entity_Id; | |
12170 | -- Entity whose length is being compared | |
12171 | ||
12172 | Index : Node_Id; | |
12173 | -- Integer_Literal node for length attribute expression, or Empty | |
12174 | -- if there is no such expression present. | |
12175 | ||
12176 | Ityp : Entity_Id; | |
12177 | -- Type of array index to which 'Length is applied | |
12178 | ||
12179 | Op : Node_Kind := Nkind (N); | |
12180 | -- Kind of comparison operator, gets flipped if operands backwards | |
12181 | ||
12182 | function Is_Optimizable (N : Node_Id) return Boolean; | |
abcd9db2 AC |
12183 | -- Tests N to see if it is an optimizable comparison value (defined as |
12184 | -- constant zero or one, or something else where the value is known to | |
12185 | -- be positive and in the range of 32-bits, and where the corresponding | |
12186 | -- Length value is also known to be 32-bits. If result is true, sets | |
12187 | -- Is_Zero, Ityp, and Comp accordingly. | |
0580d807 AC |
12188 | |
12189 | function Is_Entity_Length (N : Node_Id) return Boolean; | |
12190 | -- Tests if N is a length attribute applied to a simple entity. If so, | |
12191 | -- returns True, and sets Ent to the entity, and Index to the integer | |
12192 | -- literal provided as an attribute expression, or to Empty if none. | |
12193 | -- Also returns True if the expression is a generated type conversion | |
12194 | -- whose expression is of the desired form. This latter case arises | |
12195 | -- when Apply_Universal_Integer_Attribute_Check installs a conversion | |
12196 | -- to check for being in range, which is not needed in this context. | |
12197 | -- Returns False if neither condition holds. | |
12198 | ||
12199 | function Prepare_64 (N : Node_Id) return Node_Id; | |
12200 | -- Given a discrete expression, returns a Long_Long_Integer typed | |
12201 | -- expression representing the underlying value of the expression. | |
12202 | -- This is done with an unchecked conversion to the result type. We | |
12203 | -- use unchecked conversion to handle the enumeration type case. | |
12204 | ||
12205 | ---------------------- | |
12206 | -- Is_Entity_Length -- | |
12207 | ---------------------- | |
12208 | ||
12209 | function Is_Entity_Length (N : Node_Id) return Boolean is | |
12210 | begin | |
12211 | if Nkind (N) = N_Attribute_Reference | |
12212 | and then Attribute_Name (N) = Name_Length | |
12213 | and then Is_Entity_Name (Prefix (N)) | |
12214 | then | |
12215 | Ent := Entity (Prefix (N)); | |
12216 | ||
12217 | if Present (Expressions (N)) then | |
12218 | Index := First (Expressions (N)); | |
12219 | else | |
12220 | Index := Empty; | |
12221 | end if; | |
12222 | ||
12223 | return True; | |
12224 | ||
12225 | elsif Nkind (N) = N_Type_Conversion | |
12226 | and then not Comes_From_Source (N) | |
12227 | then | |
12228 | return Is_Entity_Length (Expression (N)); | |
12229 | ||
12230 | else | |
12231 | return False; | |
12232 | end if; | |
12233 | end Is_Entity_Length; | |
12234 | ||
12235 | -------------------- | |
12236 | -- Is_Optimizable -- | |
12237 | -------------------- | |
12238 | ||
12239 | function Is_Optimizable (N : Node_Id) return Boolean is | |
12240 | Val : Uint; | |
12241 | OK : Boolean; | |
12242 | Lo : Uint; | |
12243 | Hi : Uint; | |
12244 | Indx : Node_Id; | |
12245 | ||
12246 | begin | |
12247 | if Compile_Time_Known_Value (N) then | |
12248 | Val := Expr_Value (N); | |
12249 | ||
12250 | if Val = Uint_0 then | |
12251 | Is_Zero := True; | |
12252 | Comp := Empty; | |
12253 | return True; | |
12254 | ||
12255 | elsif Val = Uint_1 then | |
12256 | Is_Zero := False; | |
12257 | Comp := Empty; | |
12258 | return True; | |
12259 | end if; | |
12260 | end if; | |
12261 | ||
12262 | -- Here we have to make sure of being within 32-bits | |
12263 | ||
12264 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); | |
12265 | ||
12266 | if not OK | |
abcd9db2 | 12267 | or else Lo < Uint_1 |
0580d807 AC |
12268 | or else Hi > UI_From_Int (Int'Last) |
12269 | then | |
12270 | return False; | |
12271 | end if; | |
12272 | ||
abcd9db2 AC |
12273 | -- Comparison value was within range, so now we must check the index |
12274 | -- value to make sure it is also within 32-bits. | |
0580d807 AC |
12275 | |
12276 | Indx := First_Index (Etype (Ent)); | |
12277 | ||
12278 | if Present (Index) then | |
12279 | for J in 2 .. UI_To_Int (Intval (Index)) loop | |
12280 | Next_Index (Indx); | |
12281 | end loop; | |
12282 | end if; | |
12283 | ||
12284 | Ityp := Etype (Indx); | |
12285 | ||
12286 | if Esize (Ityp) > 32 then | |
12287 | return False; | |
12288 | end if; | |
12289 | ||
12290 | Is_Zero := False; | |
12291 | Comp := N; | |
12292 | return True; | |
12293 | end Is_Optimizable; | |
12294 | ||
12295 | ---------------- | |
12296 | -- Prepare_64 -- | |
12297 | ---------------- | |
12298 | ||
12299 | function Prepare_64 (N : Node_Id) return Node_Id is | |
12300 | begin | |
12301 | return Unchecked_Convert_To (Standard_Long_Long_Integer, N); | |
12302 | end Prepare_64; | |
12303 | ||
12304 | -- Start of processing for Optimize_Length_Comparison | |
12305 | ||
12306 | begin | |
12307 | -- Nothing to do if not a comparison | |
12308 | ||
12309 | if Op not in N_Op_Compare then | |
12310 | return; | |
12311 | end if; | |
12312 | ||
12313 | -- Nothing to do if special -gnatd.P debug flag set | |
12314 | ||
12315 | if Debug_Flag_Dot_PP then | |
12316 | return; | |
12317 | end if; | |
12318 | ||
12319 | -- Ent'Length op 0/1 | |
12320 | ||
12321 | if Is_Entity_Length (Left_Opnd (N)) | |
12322 | and then Is_Optimizable (Right_Opnd (N)) | |
12323 | then | |
12324 | null; | |
12325 | ||
12326 | -- 0/1 op Ent'Length | |
12327 | ||
12328 | elsif Is_Entity_Length (Right_Opnd (N)) | |
12329 | and then Is_Optimizable (Left_Opnd (N)) | |
12330 | then | |
12331 | -- Flip comparison to opposite sense | |
12332 | ||
12333 | case Op is | |
12334 | when N_Op_Lt => Op := N_Op_Gt; | |
12335 | when N_Op_Le => Op := N_Op_Ge; | |
12336 | when N_Op_Gt => Op := N_Op_Lt; | |
12337 | when N_Op_Ge => Op := N_Op_Le; | |
12338 | when others => null; | |
12339 | end case; | |
12340 | ||
12341 | -- Else optimization not possible | |
12342 | ||
12343 | else | |
12344 | return; | |
12345 | end if; | |
12346 | ||
12347 | -- Fall through if we will do the optimization | |
12348 | ||
12349 | -- Cases to handle: | |
12350 | ||
12351 | -- X'Length = 0 => X'First > X'Last | |
12352 | -- X'Length = 1 => X'First = X'Last | |
12353 | -- X'Length = n => X'First + (n - 1) = X'Last | |
12354 | ||
12355 | -- X'Length /= 0 => X'First <= X'Last | |
12356 | -- X'Length /= 1 => X'First /= X'Last | |
12357 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
12358 | ||
12359 | -- X'Length >= 0 => always true, warn | |
12360 | -- X'Length >= 1 => X'First <= X'Last | |
12361 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
12362 | ||
12363 | -- X'Length > 0 => X'First <= X'Last | |
12364 | -- X'Length > 1 => X'First < X'Last | |
12365 | -- X'Length > n => X'First + (n - 1) < X'Last | |
12366 | ||
12367 | -- X'Length <= 0 => X'First > X'Last (warn, could be =) | |
12368 | -- X'Length <= 1 => X'First >= X'Last | |
12369 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
12370 | ||
12371 | -- X'Length < 0 => always false (warn) | |
12372 | -- X'Length < 1 => X'First > X'Last | |
12373 | -- X'Length < n => X'First + (n - 1) > X'Last | |
12374 | ||
12375 | -- Note: for the cases of n (not constant 0,1), we require that the | |
12376 | -- corresponding index type be integer or shorter (i.e. not 64-bit), | |
12377 | -- and the same for the comparison value. Then we do the comparison | |
12378 | -- using 64-bit arithmetic (actually long long integer), so that we | |
12379 | -- cannot have overflow intefering with the result. | |
12380 | ||
12381 | -- First deal with warning cases | |
12382 | ||
12383 | if Is_Zero then | |
12384 | case Op is | |
12385 | ||
12386 | -- X'Length >= 0 | |
12387 | ||
12388 | when N_Op_Ge => | |
12389 | Rewrite (N, | |
12390 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc))); | |
12391 | Analyze_And_Resolve (N, Typ); | |
12392 | Warn_On_Known_Condition (N); | |
12393 | return; | |
12394 | ||
12395 | -- X'Length < 0 | |
12396 | ||
12397 | when N_Op_Lt => | |
12398 | Rewrite (N, | |
12399 | Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc))); | |
12400 | Analyze_And_Resolve (N, Typ); | |
12401 | Warn_On_Known_Condition (N); | |
12402 | return; | |
12403 | ||
12404 | when N_Op_Le => | |
12405 | if Constant_Condition_Warnings | |
12406 | and then Comes_From_Source (Original_Node (N)) | |
12407 | then | |
324ac540 | 12408 | Error_Msg_N ("could replace by ""'=""?c?", N); |
0580d807 AC |
12409 | end if; |
12410 | ||
12411 | Op := N_Op_Eq; | |
12412 | ||
12413 | when others => | |
12414 | null; | |
12415 | end case; | |
12416 | end if; | |
12417 | ||
12418 | -- Build the First reference we will use | |
12419 | ||
12420 | Left := | |
12421 | Make_Attribute_Reference (Loc, | |
12422 | Prefix => New_Occurrence_Of (Ent, Loc), | |
12423 | Attribute_Name => Name_First); | |
12424 | ||
12425 | if Present (Index) then | |
12426 | Set_Expressions (Left, New_List (New_Copy (Index))); | |
12427 | end if; | |
12428 | ||
12429 | -- If general value case, then do the addition of (n - 1), and | |
12430 | -- also add the needed conversions to type Long_Long_Integer. | |
12431 | ||
12432 | if Present (Comp) then | |
12433 | Left := | |
12434 | Make_Op_Add (Loc, | |
12435 | Left_Opnd => Prepare_64 (Left), | |
12436 | Right_Opnd => | |
12437 | Make_Op_Subtract (Loc, | |
12438 | Left_Opnd => Prepare_64 (Comp), | |
12439 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
12440 | end if; | |
12441 | ||
12442 | -- Build the Last reference we will use | |
12443 | ||
12444 | Right := | |
12445 | Make_Attribute_Reference (Loc, | |
12446 | Prefix => New_Occurrence_Of (Ent, Loc), | |
12447 | Attribute_Name => Name_Last); | |
12448 | ||
12449 | if Present (Index) then | |
12450 | Set_Expressions (Right, New_List (New_Copy (Index))); | |
12451 | end if; | |
12452 | ||
12453 | -- If general operand, convert Last reference to Long_Long_Integer | |
12454 | ||
12455 | if Present (Comp) then | |
12456 | Right := Prepare_64 (Right); | |
12457 | end if; | |
12458 | ||
12459 | -- Check for cases to optimize | |
12460 | ||
12461 | -- X'Length = 0 => X'First > X'Last | |
12462 | -- X'Length < 1 => X'First > X'Last | |
12463 | -- X'Length < n => X'First + (n - 1) > X'Last | |
12464 | ||
12465 | if (Is_Zero and then Op = N_Op_Eq) | |
12466 | or else (not Is_Zero and then Op = N_Op_Lt) | |
12467 | then | |
12468 | Result := | |
12469 | Make_Op_Gt (Loc, | |
12470 | Left_Opnd => Left, | |
12471 | Right_Opnd => Right); | |
12472 | ||
12473 | -- X'Length = 1 => X'First = X'Last | |
12474 | -- X'Length = n => X'First + (n - 1) = X'Last | |
12475 | ||
12476 | elsif not Is_Zero and then Op = N_Op_Eq then | |
12477 | Result := | |
12478 | Make_Op_Eq (Loc, | |
12479 | Left_Opnd => Left, | |
12480 | Right_Opnd => Right); | |
12481 | ||
12482 | -- X'Length /= 0 => X'First <= X'Last | |
12483 | -- X'Length > 0 => X'First <= X'Last | |
12484 | ||
12485 | elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then | |
12486 | Result := | |
12487 | Make_Op_Le (Loc, | |
12488 | Left_Opnd => Left, | |
12489 | Right_Opnd => Right); | |
12490 | ||
12491 | -- X'Length /= 1 => X'First /= X'Last | |
12492 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
12493 | ||
12494 | elsif not Is_Zero and then Op = N_Op_Ne then | |
12495 | Result := | |
12496 | Make_Op_Ne (Loc, | |
12497 | Left_Opnd => Left, | |
12498 | Right_Opnd => Right); | |
12499 | ||
12500 | -- X'Length >= 1 => X'First <= X'Last | |
12501 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
12502 | ||
12503 | elsif not Is_Zero and then Op = N_Op_Ge then | |
12504 | Result := | |
12505 | Make_Op_Le (Loc, | |
12506 | Left_Opnd => Left, | |
12507 | Right_Opnd => Right); | |
12508 | ||
12509 | -- X'Length > 1 => X'First < X'Last | |
12510 | -- X'Length > n => X'First + (n = 1) < X'Last | |
12511 | ||
12512 | elsif not Is_Zero and then Op = N_Op_Gt then | |
12513 | Result := | |
12514 | Make_Op_Lt (Loc, | |
12515 | Left_Opnd => Left, | |
12516 | Right_Opnd => Right); | |
12517 | ||
12518 | -- X'Length <= 1 => X'First >= X'Last | |
12519 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
12520 | ||
12521 | elsif not Is_Zero and then Op = N_Op_Le then | |
12522 | Result := | |
12523 | Make_Op_Ge (Loc, | |
12524 | Left_Opnd => Left, | |
12525 | Right_Opnd => Right); | |
12526 | ||
12527 | -- Should not happen at this stage | |
12528 | ||
12529 | else | |
12530 | raise Program_Error; | |
12531 | end if; | |
12532 | ||
12533 | -- Rewrite and finish up | |
12534 | ||
12535 | Rewrite (N, Result); | |
12536 | Analyze_And_Resolve (N, Typ); | |
12537 | return; | |
12538 | end Optimize_Length_Comparison; | |
12539 | ||
b2c28399 AC |
12540 | ------------------------------ |
12541 | -- Process_Transient_Object -- | |
12542 | ------------------------------ | |
12543 | ||
12544 | procedure Process_Transient_Object | |
12545 | (Decl : Node_Id; | |
12546 | Rel_Node : Node_Id) | |
12547 | is | |
8942b30c AC |
12548 | Loc : constant Source_Ptr := Sloc (Decl); |
12549 | Obj_Id : constant Entity_Id := Defining_Identifier (Decl); | |
12550 | Obj_Typ : constant Node_Id := Etype (Obj_Id); | |
12551 | Desig_Typ : Entity_Id; | |
12552 | Expr : Node_Id; | |
12553 | Fin_Stmts : List_Id; | |
12554 | Ptr_Id : Entity_Id; | |
12555 | Temp_Id : Entity_Id; | |
12556 | Temp_Ins : Node_Id; | |
12557 | ||
9ab5d86b | 12558 | Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node); |
8942b30c AC |
12559 | -- Node on which to insert the hook pointer (as an action): the |
12560 | -- innermost enclosing non-transient scope. | |
b2c28399 | 12561 | |
064f4527 TQ |
12562 | Finalization_Context : Node_Id; |
12563 | -- Node after which to insert finalization actions | |
12564 | ||
12565 | Finalize_Always : Boolean; | |
9ab5d86b RD |
12566 | -- If False, call to finalizer includes a test of whether the hook |
12567 | -- pointer is null. | |
b2c28399 | 12568 | |
8942b30c AC |
12569 | begin |
12570 | -- Step 0: determine where to attach finalization actions in the tree | |
064f4527 | 12571 | |
8942b30c AC |
12572 | -- Special case for Boolean EWAs: capture expression in a temporary, |
12573 | -- whose declaration will serve as the context around which to insert | |
12574 | -- finalization code. The finalization thus remains local to the | |
12575 | -- specific condition being evaluated. | |
064f4527 | 12576 | |
8942b30c | 12577 | if Is_Boolean_Type (Etype (Rel_Node)) then |
064f4527 | 12578 | |
9ab5d86b RD |
12579 | -- In this case, the finalization context is chosen so that we know |
12580 | -- at finalization point that the hook pointer is never null, so no | |
12581 | -- need for a test, we can call the finalizer unconditionally, except | |
12582 | -- in the case where the object is created in a specific branch of a | |
12583 | -- conditional expression. | |
064f4527 | 12584 | |
8942b30c | 12585 | Finalize_Always := |
c5c780e6 HK |
12586 | not Within_Case_Or_If_Expression (Rel_Node) |
12587 | and then not Nkind_In | |
12588 | (Original_Node (Rel_Node), N_Case_Expression, | |
12589 | N_If_Expression); | |
064f4527 | 12590 | |
8942b30c AC |
12591 | declare |
12592 | Loc : constant Source_Ptr := Sloc (Rel_Node); | |
12593 | Temp : constant Entity_Id := Make_Temporary (Loc, 'E', Rel_Node); | |
b2c28399 | 12594 | |
8942b30c AC |
12595 | begin |
12596 | Append_To (Actions (Rel_Node), | |
12597 | Make_Object_Declaration (Loc, | |
12598 | Defining_Identifier => Temp, | |
12599 | Constant_Present => True, | |
12600 | Object_Definition => | |
12601 | New_Occurrence_Of (Etype (Rel_Node), Loc), | |
12602 | Expression => Expression (Rel_Node))); | |
12603 | Finalization_Context := Last (Actions (Rel_Node)); | |
b2c28399 | 12604 | |
8942b30c | 12605 | Analyze (Last (Actions (Rel_Node))); |
b2c28399 | 12606 | |
8942b30c AC |
12607 | Set_Expression (Rel_Node, New_Occurrence_Of (Temp, Loc)); |
12608 | Analyze (Expression (Rel_Node)); | |
12609 | end; | |
b2c28399 | 12610 | |
8942b30c AC |
12611 | else |
12612 | Finalize_Always := False; | |
12613 | Finalization_Context := Hook_Context; | |
12614 | end if; | |
064f4527 | 12615 | |
b2c28399 AC |
12616 | -- Step 1: Create the access type which provides a reference to the |
12617 | -- transient controlled object. | |
12618 | ||
12619 | if Is_Access_Type (Obj_Typ) then | |
12620 | Desig_Typ := Directly_Designated_Type (Obj_Typ); | |
12621 | else | |
12622 | Desig_Typ := Obj_Typ; | |
12623 | end if; | |
12624 | ||
12625 | Desig_Typ := Base_Type (Desig_Typ); | |
12626 | ||
12627 | -- Generate: | |
12628 | -- Ann : access [all] <Desig_Typ>; | |
12629 | ||
12630 | Ptr_Id := Make_Temporary (Loc, 'A'); | |
12631 | ||
064f4527 | 12632 | Insert_Action (Hook_Context, |
b2c28399 AC |
12633 | Make_Full_Type_Declaration (Loc, |
12634 | Defining_Identifier => Ptr_Id, | |
12635 | Type_Definition => | |
12636 | Make_Access_To_Object_Definition (Loc, | |
12637 | All_Present => Ekind (Obj_Typ) = E_General_Access_Type, | |
e4494292 | 12638 | Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)))); |
b2c28399 AC |
12639 | |
12640 | -- Step 2: Create a temporary which acts as a hook to the transient | |
12641 | -- controlled object. Generate: | |
12642 | ||
12643 | -- Temp : Ptr_Id := null; | |
12644 | ||
12645 | Temp_Id := Make_Temporary (Loc, 'T'); | |
12646 | ||
064f4527 | 12647 | Insert_Action (Hook_Context, |
b2c28399 AC |
12648 | Make_Object_Declaration (Loc, |
12649 | Defining_Identifier => Temp_Id, | |
e4494292 | 12650 | Object_Definition => New_Occurrence_Of (Ptr_Id, Loc))); |
b2c28399 AC |
12651 | |
12652 | -- Mark the temporary as created for the purposes of exporting the | |
12653 | -- transient controlled object out of the expression_with_action or if | |
12654 | -- expression. This signals the machinery in Build_Finalizer to treat | |
12655 | -- this case specially. | |
12656 | ||
12657 | Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl); | |
12658 | ||
12659 | -- Step 3: Hook the transient object to the temporary | |
12660 | ||
a7d08a38 AC |
12661 | -- This must be inserted right after the object declaration, so that |
12662 | -- the assignment is executed if, and only if, the object is actually | |
12663 | -- created (whereas the declaration of the hook pointer, and the | |
12664 | -- finalization call, may be inserted at an outer level, and may | |
12665 | -- remain unused for some executions, if the actual creation of | |
12666 | -- the object is conditional). | |
12667 | ||
b2c28399 AC |
12668 | -- The use of unchecked conversion / unrestricted access is needed to |
12669 | -- avoid an accessibility violation. Note that the finalization code is | |
12670 | -- structured in such a way that the "hook" is processed only when it | |
12671 | -- points to an existing object. | |
12672 | ||
12673 | if Is_Access_Type (Obj_Typ) then | |
e4494292 RD |
12674 | Expr := |
12675 | Unchecked_Convert_To (Ptr_Id, New_Occurrence_Of (Obj_Id, Loc)); | |
b2c28399 AC |
12676 | else |
12677 | Expr := | |
12678 | Make_Attribute_Reference (Loc, | |
e4494292 | 12679 | Prefix => New_Occurrence_Of (Obj_Id, Loc), |
b2c28399 AC |
12680 | Attribute_Name => Name_Unrestricted_Access); |
12681 | end if; | |
12682 | ||
12683 | -- Generate: | |
12684 | -- Temp := Ptr_Id (Obj_Id); | |
12685 | -- <or> | |
12686 | -- Temp := Obj_Id'Unrestricted_Access; | |
12687 | ||
97779c34 AC |
12688 | -- When the transient object is initialized by an aggregate, the hook |
12689 | -- must capture the object after the last component assignment takes | |
12690 | -- place. Only then is the object fully initialized. | |
12691 | ||
12692 | if Ekind (Obj_Id) = E_Variable | |
12693 | and then Present (Last_Aggregate_Assignment (Obj_Id)) | |
12694 | then | |
12695 | Temp_Ins := Last_Aggregate_Assignment (Obj_Id); | |
12696 | ||
12697 | -- Otherwise the hook seizes the related object immediately | |
12698 | ||
12699 | else | |
12700 | Temp_Ins := Decl; | |
12701 | end if; | |
12702 | ||
12703 | Insert_After_And_Analyze (Temp_Ins, | |
a7d08a38 | 12704 | Make_Assignment_Statement (Loc, |
e4494292 | 12705 | Name => New_Occurrence_Of (Temp_Id, Loc), |
a7d08a38 | 12706 | Expression => Expr)); |
b2c28399 AC |
12707 | |
12708 | -- Step 4: Finalize the transient controlled object after the context | |
12709 | -- has been evaluated/elaborated. Generate: | |
12710 | ||
12711 | -- if Temp /= null then | |
12712 | -- [Deep_]Finalize (Temp.all); | |
12713 | -- Temp := null; | |
12714 | -- end if; | |
12715 | ||
12716 | -- When the node is part of a return statement, there is no need to | |
12717 | -- insert a finalization call, as the general finalization mechanism | |
12718 | -- (see Build_Finalizer) would take care of the transient controlled | |
12719 | -- object on subprogram exit. Note that it would also be impossible to | |
12720 | -- insert the finalization code after the return statement as this will | |
12721 | -- render it unreachable. | |
12722 | ||
064f4527 TQ |
12723 | if Nkind (Finalization_Context) /= N_Simple_Return_Statement then |
12724 | Fin_Stmts := New_List ( | |
12725 | Make_Final_Call | |
12726 | (Obj_Ref => | |
12727 | Make_Explicit_Dereference (Loc, | |
e4494292 | 12728 | Prefix => New_Occurrence_Of (Temp_Id, Loc)), |
064f4527 | 12729 | Typ => Desig_Typ), |
b2c28399 | 12730 | |
064f4527 | 12731 | Make_Assignment_Statement (Loc, |
e4494292 | 12732 | Name => New_Occurrence_Of (Temp_Id, Loc), |
064f4527 | 12733 | Expression => Make_Null (Loc))); |
b2c28399 | 12734 | |
064f4527 TQ |
12735 | if not Finalize_Always then |
12736 | Fin_Stmts := New_List ( | |
12737 | Make_Implicit_If_Statement (Decl, | |
12738 | Condition => | |
12739 | Make_Op_Ne (Loc, | |
e4494292 | 12740 | Left_Opnd => New_Occurrence_Of (Temp_Id, Loc), |
064f4527 TQ |
12741 | Right_Opnd => Make_Null (Loc)), |
12742 | Then_Statements => Fin_Stmts)); | |
12743 | end if; | |
b2c28399 | 12744 | |
064f4527 | 12745 | Insert_Actions_After (Finalization_Context, Fin_Stmts); |
b2c28399 AC |
12746 | end if; |
12747 | end Process_Transient_Object; | |
12748 | ||
70482933 RK |
12749 | ------------------------ |
12750 | -- Rewrite_Comparison -- | |
12751 | ------------------------ | |
12752 | ||
12753 | procedure Rewrite_Comparison (N : Node_Id) is | |
c800f862 RD |
12754 | Warning_Generated : Boolean := False; |
12755 | -- Set to True if first pass with Assume_Valid generates a warning in | |
12756 | -- which case we skip the second pass to avoid warning overloaded. | |
12757 | ||
12758 | Result : Node_Id; | |
12759 | -- Set to Standard_True or Standard_False | |
12760 | ||
d26dc4b5 AC |
12761 | begin |
12762 | if Nkind (N) = N_Type_Conversion then | |
12763 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 12764 | return; |
70482933 | 12765 | |
d26dc4b5 | 12766 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
12767 | return; |
12768 | end if; | |
70482933 | 12769 | |
c800f862 RD |
12770 | -- Now start looking at the comparison in detail. We potentially go |
12771 | -- through this loop twice. The first time, Assume_Valid is set False | |
12772 | -- in the call to Compile_Time_Compare. If this call results in a | |
12773 | -- clear result of always True or Always False, that's decisive and | |
12774 | -- we are done. Otherwise we repeat the processing with Assume_Valid | |
e7e4d230 | 12775 | -- set to True to generate additional warnings. We can skip that step |
c800f862 RD |
12776 | -- if Constant_Condition_Warnings is False. |
12777 | ||
12778 | for AV in False .. True loop | |
12779 | declare | |
12780 | Typ : constant Entity_Id := Etype (N); | |
12781 | Op1 : constant Node_Id := Left_Opnd (N); | |
12782 | Op2 : constant Node_Id := Right_Opnd (N); | |
70482933 | 12783 | |
c800f862 RD |
12784 | Res : constant Compare_Result := |
12785 | Compile_Time_Compare (Op1, Op2, Assume_Valid => AV); | |
12786 | -- Res indicates if compare outcome can be compile time determined | |
f02b8bb8 | 12787 | |
c800f862 RD |
12788 | True_Result : Boolean; |
12789 | False_Result : Boolean; | |
f02b8bb8 | 12790 | |
c800f862 RD |
12791 | begin |
12792 | case N_Op_Compare (Nkind (N)) is | |
d26dc4b5 AC |
12793 | when N_Op_Eq => |
12794 | True_Result := Res = EQ; | |
12795 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
12796 | ||
12797 | when N_Op_Ge => | |
12798 | True_Result := Res in Compare_GE; | |
12799 | False_Result := Res = LT; | |
12800 | ||
12801 | if Res = LE | |
12802 | and then Constant_Condition_Warnings | |
12803 | and then Comes_From_Source (Original_Node (N)) | |
12804 | and then Nkind (Original_Node (N)) = N_Op_Ge | |
12805 | and then not In_Instance | |
d26dc4b5 | 12806 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 12807 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 12808 | then |
ed2233dc | 12809 | Error_Msg_N |
324ac540 AC |
12810 | ("can never be greater than, could replace by ""'=""?c?", |
12811 | N); | |
c800f862 | 12812 | Warning_Generated := True; |
d26dc4b5 | 12813 | end if; |
70482933 | 12814 | |
d26dc4b5 AC |
12815 | when N_Op_Gt => |
12816 | True_Result := Res = GT; | |
12817 | False_Result := Res in Compare_LE; | |
12818 | ||
12819 | when N_Op_Lt => | |
12820 | True_Result := Res = LT; | |
12821 | False_Result := Res in Compare_GE; | |
12822 | ||
12823 | when N_Op_Le => | |
12824 | True_Result := Res in Compare_LE; | |
12825 | False_Result := Res = GT; | |
12826 | ||
12827 | if Res = GE | |
12828 | and then Constant_Condition_Warnings | |
12829 | and then Comes_From_Source (Original_Node (N)) | |
12830 | and then Nkind (Original_Node (N)) = N_Op_Le | |
12831 | and then not In_Instance | |
d26dc4b5 | 12832 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 12833 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 12834 | then |
ed2233dc | 12835 | Error_Msg_N |
324ac540 | 12836 | ("can never be less than, could replace by ""'=""?c?", N); |
c800f862 | 12837 | Warning_Generated := True; |
d26dc4b5 | 12838 | end if; |
70482933 | 12839 | |
d26dc4b5 AC |
12840 | when N_Op_Ne => |
12841 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
12842 | False_Result := Res = EQ; | |
c800f862 | 12843 | end case; |
d26dc4b5 | 12844 | |
c800f862 RD |
12845 | -- If this is the first iteration, then we actually convert the |
12846 | -- comparison into True or False, if the result is certain. | |
d26dc4b5 | 12847 | |
c800f862 RD |
12848 | if AV = False then |
12849 | if True_Result or False_Result then | |
21791d97 | 12850 | Result := Boolean_Literals (True_Result); |
c800f862 RD |
12851 | Rewrite (N, |
12852 | Convert_To (Typ, | |
12853 | New_Occurrence_Of (Result, Sloc (N)))); | |
12854 | Analyze_And_Resolve (N, Typ); | |
12855 | Warn_On_Known_Condition (N); | |
12856 | return; | |
12857 | end if; | |
12858 | ||
12859 | -- If this is the second iteration (AV = True), and the original | |
e7e4d230 AC |
12860 | -- node comes from source and we are not in an instance, then give |
12861 | -- a warning if we know result would be True or False. Note: we | |
12862 | -- know Constant_Condition_Warnings is set if we get here. | |
c800f862 RD |
12863 | |
12864 | elsif Comes_From_Source (Original_Node (N)) | |
12865 | and then not In_Instance | |
12866 | then | |
12867 | if True_Result then | |
ed2233dc | 12868 | Error_Msg_N |
324ac540 | 12869 | ("condition can only be False if invalid values present??", |
c800f862 RD |
12870 | N); |
12871 | elsif False_Result then | |
ed2233dc | 12872 | Error_Msg_N |
324ac540 | 12873 | ("condition can only be True if invalid values present??", |
c800f862 RD |
12874 | N); |
12875 | end if; | |
12876 | end if; | |
12877 | end; | |
12878 | ||
12879 | -- Skip second iteration if not warning on constant conditions or | |
e7e4d230 AC |
12880 | -- if the first iteration already generated a warning of some kind or |
12881 | -- if we are in any case assuming all values are valid (so that the | |
12882 | -- first iteration took care of the valid case). | |
c800f862 RD |
12883 | |
12884 | exit when not Constant_Condition_Warnings; | |
12885 | exit when Warning_Generated; | |
12886 | exit when Assume_No_Invalid_Values; | |
12887 | end loop; | |
70482933 RK |
12888 | end Rewrite_Comparison; |
12889 | ||
fbf5a39b AC |
12890 | ---------------------------- |
12891 | -- Safe_In_Place_Array_Op -- | |
12892 | ---------------------------- | |
12893 | ||
12894 | function Safe_In_Place_Array_Op | |
2e071734 AC |
12895 | (Lhs : Node_Id; |
12896 | Op1 : Node_Id; | |
12897 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
12898 | is |
12899 | Target : Entity_Id; | |
12900 | ||
12901 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
12902 | -- Operand is safe if it cannot overlap part of the target of the | |
12903 | -- operation. If the operand and the target are identical, the operand | |
12904 | -- is safe. The operand can be empty in the case of negation. | |
12905 | ||
12906 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 12907 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
12908 | |
12909 | ------------------ | |
12910 | -- Is_Unaliased -- | |
12911 | ------------------ | |
12912 | ||
12913 | function Is_Unaliased (N : Node_Id) return Boolean is | |
12914 | begin | |
12915 | return | |
12916 | Is_Entity_Name (N) | |
12917 | and then No (Address_Clause (Entity (N))) | |
12918 | and then No (Renamed_Object (Entity (N))); | |
12919 | end Is_Unaliased; | |
12920 | ||
12921 | --------------------- | |
12922 | -- Is_Safe_Operand -- | |
12923 | --------------------- | |
12924 | ||
12925 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
12926 | begin | |
12927 | if No (Op) then | |
12928 | return True; | |
12929 | ||
12930 | elsif Is_Entity_Name (Op) then | |
12931 | return Is_Unaliased (Op); | |
12932 | ||
303b4d58 | 12933 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
12934 | return Is_Unaliased (Prefix (Op)); |
12935 | ||
12936 | elsif Nkind (Op) = N_Slice then | |
12937 | return | |
12938 | Is_Unaliased (Prefix (Op)) | |
12939 | and then Entity (Prefix (Op)) /= Target; | |
12940 | ||
12941 | elsif Nkind (Op) = N_Op_Not then | |
12942 | return Is_Safe_Operand (Right_Opnd (Op)); | |
12943 | ||
12944 | else | |
12945 | return False; | |
12946 | end if; | |
12947 | end Is_Safe_Operand; | |
12948 | ||
b6b5cca8 | 12949 | -- Start of processing for Safe_In_Place_Array_Op |
fbf5a39b AC |
12950 | |
12951 | begin | |
685094bf RD |
12952 | -- Skip this processing if the component size is different from system |
12953 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b | 12954 | |
eaa826f8 | 12955 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then |
fbf5a39b AC |
12956 | return False; |
12957 | ||
26bff3d9 | 12958 | -- Cannot do in place stuff on VM_Target since cannot pass addresses |
fbf5a39b | 12959 | |
26bff3d9 | 12960 | elsif VM_Target /= No_VM then |
fbf5a39b AC |
12961 | return False; |
12962 | ||
12963 | -- Cannot do in place stuff if non-standard Boolean representation | |
12964 | ||
eaa826f8 | 12965 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then |
fbf5a39b AC |
12966 | return False; |
12967 | ||
12968 | elsif not Is_Unaliased (Lhs) then | |
12969 | return False; | |
e7e4d230 | 12970 | |
fbf5a39b AC |
12971 | else |
12972 | Target := Entity (Lhs); | |
e7e4d230 | 12973 | return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2); |
fbf5a39b AC |
12974 | end if; |
12975 | end Safe_In_Place_Array_Op; | |
12976 | ||
70482933 RK |
12977 | ----------------------- |
12978 | -- Tagged_Membership -- | |
12979 | ----------------------- | |
12980 | ||
685094bf RD |
12981 | -- There are two different cases to consider depending on whether the right |
12982 | -- operand is a class-wide type or not. If not we just compare the actual | |
12983 | -- tag of the left expr to the target type tag: | |
70482933 RK |
12984 | -- |
12985 | -- Left_Expr.Tag = Right_Type'Tag; | |
12986 | -- | |
685094bf RD |
12987 | -- If it is a class-wide type we use the RT function CW_Membership which is |
12988 | -- usually implemented by looking in the ancestor tables contained in the | |
12989 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 12990 | |
0669bebe GB |
12991 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
12992 | -- function IW_Membership which is usually implemented by looking in the | |
12993 | -- table of abstract interface types plus the ancestor table contained in | |
12994 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
12995 | ||
82878151 AC |
12996 | procedure Tagged_Membership |
12997 | (N : Node_Id; | |
12998 | SCIL_Node : out Node_Id; | |
12999 | Result : out Node_Id) | |
13000 | is | |
70482933 RK |
13001 | Left : constant Node_Id := Left_Opnd (N); |
13002 | Right : constant Node_Id := Right_Opnd (N); | |
13003 | Loc : constant Source_Ptr := Sloc (N); | |
13004 | ||
38171f43 | 13005 | Full_R_Typ : Entity_Id; |
70482933 | 13006 | Left_Type : Entity_Id; |
82878151 | 13007 | New_Node : Node_Id; |
70482933 RK |
13008 | Right_Type : Entity_Id; |
13009 | Obj_Tag : Node_Id; | |
13010 | ||
13011 | begin | |
82878151 AC |
13012 | SCIL_Node := Empty; |
13013 | ||
852dba80 AC |
13014 | -- Handle entities from the limited view |
13015 | ||
13016 | Left_Type := Available_View (Etype (Left)); | |
13017 | Right_Type := Available_View (Etype (Right)); | |
70482933 | 13018 | |
6cce2156 GD |
13019 | -- In the case where the type is an access type, the test is applied |
13020 | -- using the designated types (needed in Ada 2012 for implicit anonymous | |
13021 | -- access conversions, for AI05-0149). | |
13022 | ||
13023 | if Is_Access_Type (Right_Type) then | |
13024 | Left_Type := Designated_Type (Left_Type); | |
13025 | Right_Type := Designated_Type (Right_Type); | |
13026 | end if; | |
13027 | ||
70482933 RK |
13028 | if Is_Class_Wide_Type (Left_Type) then |
13029 | Left_Type := Root_Type (Left_Type); | |
13030 | end if; | |
13031 | ||
38171f43 AC |
13032 | if Is_Class_Wide_Type (Right_Type) then |
13033 | Full_R_Typ := Underlying_Type (Root_Type (Right_Type)); | |
13034 | else | |
13035 | Full_R_Typ := Underlying_Type (Right_Type); | |
13036 | end if; | |
13037 | ||
70482933 RK |
13038 | Obj_Tag := |
13039 | Make_Selected_Component (Loc, | |
13040 | Prefix => Relocate_Node (Left), | |
a9d8907c | 13041 | Selector_Name => |
e4494292 | 13042 | New_Occurrence_Of (First_Tag_Component (Left_Type), Loc)); |
70482933 RK |
13043 | |
13044 | if Is_Class_Wide_Type (Right_Type) then | |
758c442c | 13045 | |
0669bebe GB |
13046 | -- No need to issue a run-time check if we statically know that the |
13047 | -- result of this membership test is always true. For example, | |
13048 | -- considering the following declarations: | |
13049 | ||
13050 | -- type Iface is interface; | |
13051 | -- type T is tagged null record; | |
13052 | -- type DT is new T and Iface with null record; | |
13053 | ||
13054 | -- Obj1 : T; | |
13055 | -- Obj2 : DT; | |
13056 | ||
13057 | -- These membership tests are always true: | |
13058 | ||
13059 | -- Obj1 in T'Class | |
13060 | -- Obj2 in T'Class; | |
13061 | -- Obj2 in Iface'Class; | |
13062 | ||
13063 | -- We do not need to handle cases where the membership is illegal. | |
13064 | -- For example: | |
13065 | ||
13066 | -- Obj1 in DT'Class; -- Compile time error | |
13067 | -- Obj1 in Iface'Class; -- Compile time error | |
13068 | ||
13069 | if not Is_Class_Wide_Type (Left_Type) | |
4ac2477e JM |
13070 | and then (Is_Ancestor (Etype (Right_Type), Left_Type, |
13071 | Use_Full_View => True) | |
533369aa AC |
13072 | or else (Is_Interface (Etype (Right_Type)) |
13073 | and then Interface_Present_In_Ancestor | |
761f7dcb AC |
13074 | (Typ => Left_Type, |
13075 | Iface => Etype (Right_Type)))) | |
0669bebe | 13076 | then |
e4494292 | 13077 | Result := New_Occurrence_Of (Standard_True, Loc); |
82878151 | 13078 | return; |
0669bebe GB |
13079 | end if; |
13080 | ||
758c442c GD |
13081 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
13082 | ||
630d30e9 RD |
13083 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
13084 | ||
0669bebe | 13085 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
13086 | |
13087 | or else Is_Interface (Left_Type) | |
13088 | then | |
dfd99a80 TQ |
13089 | -- Issue error if IW_Membership operation not available in a |
13090 | -- configurable run time setting. | |
13091 | ||
13092 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
13093 | Error_Msg_CRT |
13094 | ("dynamic membership test on interface types", N); | |
82878151 AC |
13095 | Result := Empty; |
13096 | return; | |
dfd99a80 TQ |
13097 | end if; |
13098 | ||
82878151 | 13099 | Result := |
758c442c GD |
13100 | Make_Function_Call (Loc, |
13101 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
13102 | Parameter_Associations => New_List ( | |
13103 | Make_Attribute_Reference (Loc, | |
13104 | Prefix => Obj_Tag, | |
13105 | Attribute_Name => Name_Address), | |
e4494292 | 13106 | New_Occurrence_Of ( |
38171f43 | 13107 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), |
758c442c GD |
13108 | Loc))); |
13109 | ||
13110 | -- Ada 95: Normal case | |
13111 | ||
13112 | else | |
82878151 AC |
13113 | Build_CW_Membership (Loc, |
13114 | Obj_Tag_Node => Obj_Tag, | |
13115 | Typ_Tag_Node => | |
e4494292 | 13116 | New_Occurrence_Of ( |
38171f43 | 13117 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc), |
82878151 AC |
13118 | Related_Nod => N, |
13119 | New_Node => New_Node); | |
13120 | ||
13121 | -- Generate the SCIL node for this class-wide membership test. | |
13122 | -- Done here because the previous call to Build_CW_Membership | |
13123 | -- relocates Obj_Tag. | |
13124 | ||
13125 | if Generate_SCIL then | |
13126 | SCIL_Node := Make_SCIL_Membership_Test (Sloc (N)); | |
13127 | Set_SCIL_Entity (SCIL_Node, Etype (Right_Type)); | |
13128 | Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag); | |
13129 | end if; | |
13130 | ||
13131 | Result := New_Node; | |
758c442c GD |
13132 | end if; |
13133 | ||
0669bebe GB |
13134 | -- Right_Type is not a class-wide type |
13135 | ||
70482933 | 13136 | else |
0669bebe GB |
13137 | -- No need to check the tag of the object if Right_Typ is abstract |
13138 | ||
13139 | if Is_Abstract_Type (Right_Type) then | |
e4494292 | 13140 | Result := New_Occurrence_Of (Standard_False, Loc); |
0669bebe GB |
13141 | |
13142 | else | |
82878151 | 13143 | Result := |
0669bebe GB |
13144 | Make_Op_Eq (Loc, |
13145 | Left_Opnd => Obj_Tag, | |
13146 | Right_Opnd => | |
e4494292 | 13147 | New_Occurrence_Of |
38171f43 | 13148 | (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc)); |
0669bebe | 13149 | end if; |
70482933 | 13150 | end if; |
70482933 RK |
13151 | end Tagged_Membership; |
13152 | ||
13153 | ------------------------------ | |
13154 | -- Unary_Op_Validity_Checks -- | |
13155 | ------------------------------ | |
13156 | ||
13157 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
13158 | begin | |
13159 | if Validity_Checks_On and Validity_Check_Operands then | |
13160 | Ensure_Valid (Right_Opnd (N)); | |
13161 | end if; | |
13162 | end Unary_Op_Validity_Checks; | |
13163 | ||
13164 | end Exp_Ch4; |