]>
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; | |
45 | with Exp_VFpt; use Exp_VFpt; | |
f02b8bb8 | 46 | with Freeze; use Freeze; |
70482933 | 47 | with Inline; use Inline; |
df3e68b1 | 48 | with Lib; use Lib; |
26bff3d9 | 49 | with Namet; use Namet; |
70482933 RK |
50 | with Nlists; use Nlists; |
51 | with Nmake; use Nmake; | |
52 | with Opt; use Opt; | |
25adc5fb | 53 | with Par_SCO; use Par_SCO; |
0669bebe GB |
54 | with Restrict; use Restrict; |
55 | with Rident; use Rident; | |
70482933 RK |
56 | with Rtsfind; use Rtsfind; |
57 | with Sem; use Sem; | |
a4100e55 | 58 | with Sem_Aux; use Sem_Aux; |
70482933 | 59 | with Sem_Cat; use Sem_Cat; |
5d09245e | 60 | with Sem_Ch3; use Sem_Ch3; |
11fa950b | 61 | with Sem_Ch8; use Sem_Ch8; |
70482933 RK |
62 | with Sem_Ch13; use Sem_Ch13; |
63 | with Sem_Eval; use Sem_Eval; | |
64 | with Sem_Res; use Sem_Res; | |
65 | with Sem_Type; use Sem_Type; | |
66 | with Sem_Util; use Sem_Util; | |
07fc65c4 | 67 | with Sem_Warn; use Sem_Warn; |
70482933 | 68 | with Sinfo; use Sinfo; |
70482933 RK |
69 | with Snames; use Snames; |
70 | with Stand; use Stand; | |
7665e4bd | 71 | with SCIL_LL; use SCIL_LL; |
07fc65c4 | 72 | with Targparm; use Targparm; |
70482933 RK |
73 | with Tbuild; use Tbuild; |
74 | with Ttypes; use Ttypes; | |
75 | with Uintp; use Uintp; | |
76 | with Urealp; use Urealp; | |
77 | with Validsw; use Validsw; | |
78 | ||
79 | package body Exp_Ch4 is | |
80 | ||
15ce9ca2 AC |
81 | ----------------------- |
82 | -- Local Subprograms -- | |
83 | ----------------------- | |
70482933 RK |
84 | |
85 | procedure Binary_Op_Validity_Checks (N : Node_Id); | |
86 | pragma Inline (Binary_Op_Validity_Checks); | |
87 | -- Performs validity checks for a binary operator | |
88 | ||
fbf5a39b AC |
89 | procedure Build_Boolean_Array_Proc_Call |
90 | (N : Node_Id; | |
91 | Op1 : Node_Id; | |
92 | Op2 : Node_Id); | |
303b4d58 | 93 | -- If a boolean array assignment can be done in place, build call to |
fbf5a39b AC |
94 | -- corresponding library procedure. |
95 | ||
11fa950b AC |
96 | function Current_Anonymous_Master return Entity_Id; |
97 | -- Return the entity of the heterogeneous finalization master belonging to | |
98 | -- the current unit (either function, package or procedure). This master | |
99 | -- services all anonymous access-to-controlled types. If the current unit | |
100 | -- does not have such master, create one. | |
df3e68b1 | 101 | |
26bff3d9 JM |
102 | procedure Displace_Allocator_Pointer (N : Node_Id); |
103 | -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and | |
104 | -- Expand_Allocator_Expression. Allocating class-wide interface objects | |
105 | -- this routine displaces the pointer to the allocated object to reference | |
106 | -- the component referencing the corresponding secondary dispatch table. | |
107 | ||
fbf5a39b AC |
108 | procedure Expand_Allocator_Expression (N : Node_Id); |
109 | -- Subsidiary to Expand_N_Allocator, for the case when the expression | |
110 | -- is a qualified expression or an aggregate. | |
111 | ||
70482933 RK |
112 | procedure Expand_Array_Comparison (N : Node_Id); |
113 | -- This routine handles expansion of the comparison operators (N_Op_Lt, | |
114 | -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic | |
115 | -- code for these operators is similar, differing only in the details of | |
fbf5a39b AC |
116 | -- the actual comparison call that is made. Special processing (call a |
117 | -- run-time routine) | |
70482933 RK |
118 | |
119 | function Expand_Array_Equality | |
120 | (Nod : Node_Id; | |
70482933 RK |
121 | Lhs : Node_Id; |
122 | Rhs : Node_Id; | |
0da2c8ac AC |
123 | Bodies : List_Id; |
124 | Typ : Entity_Id) return Node_Id; | |
70482933 | 125 | -- Expand an array equality into a call to a function implementing this |
685094bf RD |
126 | -- equality, and a call to it. Loc is the location for the generated nodes. |
127 | -- Lhs and Rhs are the array expressions to be compared. Bodies is a list | |
128 | -- on which to attach bodies of local functions that are created in the | |
129 | -- process. It is the responsibility of the caller to insert those bodies | |
130 | -- at the right place. Nod provides the Sloc value for the generated code. | |
131 | -- Normally the types used for the generated equality routine are taken | |
132 | -- from Lhs and Rhs. However, in some situations of generated code, the | |
133 | -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies | |
134 | -- the type to be used for the formal parameters. | |
70482933 RK |
135 | |
136 | procedure Expand_Boolean_Operator (N : Node_Id); | |
685094bf RD |
137 | -- Common expansion processing for Boolean operators (And, Or, Xor) for the |
138 | -- case of array type arguments. | |
70482933 | 139 | |
5875f8d6 AC |
140 | procedure Expand_Short_Circuit_Operator (N : Node_Id); |
141 | -- Common expansion processing for short-circuit boolean operators | |
142 | ||
456cbfa5 | 143 | procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id); |
5707e389 AC |
144 | -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is |
145 | -- where we allow comparison of "out of range" values. | |
456cbfa5 | 146 | |
70482933 RK |
147 | function Expand_Composite_Equality |
148 | (Nod : Node_Id; | |
149 | Typ : Entity_Id; | |
150 | Lhs : Node_Id; | |
151 | Rhs : Node_Id; | |
2e071734 | 152 | Bodies : List_Id) return Node_Id; |
685094bf RD |
153 | -- Local recursive function used to expand equality for nested composite |
154 | -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which | |
155 | -- to attach bodies of local functions that are created in the process. | |
3058f181 | 156 | -- It is the responsibility of the caller to insert those bodies at the |
685094bf RD |
157 | -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs |
158 | -- are the left and right sides for the comparison, and Typ is the type of | |
3058f181 | 159 | -- the objects to compare. |
70482933 | 160 | |
fdac1f80 AC |
161 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id); |
162 | -- Routine to expand concatenation of a sequence of two or more operands | |
163 | -- (in the list Operands) and replace node Cnode with the result of the | |
164 | -- concatenation. The operands can be of any appropriate type, and can | |
165 | -- include both arrays and singleton elements. | |
70482933 | 166 | |
f6194278 | 167 | procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id); |
5707e389 AC |
168 | -- N is an N_In membership test mode, with the overflow check mode set to |
169 | -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed | |
170 | -- integer type. This is a case where top level processing is required to | |
171 | -- handle overflow checks in subtrees. | |
f6194278 | 172 | |
70482933 | 173 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); |
685094bf RD |
174 | -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal |
175 | -- fixed. We do not have such a type at runtime, so the purpose of this | |
176 | -- routine is to find the real type by looking up the tree. We also | |
177 | -- determine if the operation must be rounded. | |
70482933 | 178 | |
5d09245e AC |
179 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean; |
180 | -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable | |
181 | -- discriminants if it has a constrained nominal type, unless the object | |
182 | -- is a component of an enclosing Unchecked_Union object that is subject | |
183 | -- to a per-object constraint and the enclosing object lacks inferable | |
184 | -- discriminants. | |
185 | -- | |
186 | -- An expression of an Unchecked_Union type has inferable discriminants | |
187 | -- if it is either a name of an object with inferable discriminants or a | |
188 | -- qualified expression whose subtype mark denotes a constrained subtype. | |
189 | ||
70482933 | 190 | procedure Insert_Dereference_Action (N : Node_Id); |
e6f69614 AC |
191 | -- N is an expression whose type is an access. When the type of the |
192 | -- associated storage pool is derived from Checked_Pool, generate a | |
193 | -- call to the 'Dereference' primitive operation. | |
70482933 RK |
194 | |
195 | function Make_Array_Comparison_Op | |
2e071734 AC |
196 | (Typ : Entity_Id; |
197 | Nod : Node_Id) return Node_Id; | |
685094bf RD |
198 | -- Comparisons between arrays are expanded in line. This function produces |
199 | -- the body of the implementation of (a > b), where a and b are one- | |
200 | -- dimensional arrays of some discrete type. The original node is then | |
201 | -- expanded into the appropriate call to this function. Nod provides the | |
202 | -- Sloc value for the generated code. | |
70482933 RK |
203 | |
204 | function Make_Boolean_Array_Op | |
2e071734 AC |
205 | (Typ : Entity_Id; |
206 | N : Node_Id) return Node_Id; | |
685094bf RD |
207 | -- Boolean operations on boolean arrays are expanded in line. This function |
208 | -- produce the body for the node N, which is (a and b), (a or b), or (a xor | |
209 | -- b). It is used only the normal case and not the packed case. The type | |
210 | -- involved, Typ, is the Boolean array type, and the logical operations in | |
211 | -- the body are simple boolean operations. Note that Typ is always a | |
212 | -- constrained type (the caller has ensured this by using | |
213 | -- Convert_To_Actual_Subtype if necessary). | |
70482933 | 214 | |
b6b5cca8 | 215 | function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean; |
a7f1b24f RD |
216 | -- For signed arithmetic operations when the current overflow mode is |
217 | -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks | |
218 | -- as the first thing we do. We then return. We count on the recursive | |
219 | -- apparatus for overflow checks to call us back with an equivalent | |
220 | -- operation that is in CHECKED mode, avoiding a recursive entry into this | |
221 | -- routine, and that is when we will proceed with the expansion of the | |
222 | -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do | |
223 | -- these optimizations without first making this check, since there may be | |
224 | -- operands further down the tree that are relying on the recursive calls | |
225 | -- triggered by the top level nodes to properly process overflow checking | |
226 | -- and remaining expansion on these nodes. Note that this call back may be | |
227 | -- skipped if the operation is done in Bignum mode but that's fine, since | |
228 | -- the Bignum call takes care of everything. | |
b6b5cca8 | 229 | |
0580d807 AC |
230 | procedure Optimize_Length_Comparison (N : Node_Id); |
231 | -- Given an expression, if it is of the form X'Length op N (or the other | |
232 | -- way round), where N is known at compile time to be 0 or 1, and X is a | |
233 | -- simple entity, and op is a comparison operator, optimizes it into a | |
234 | -- comparison of First and Last. | |
235 | ||
b2c28399 AC |
236 | procedure Process_Transient_Object |
237 | (Decl : Node_Id; | |
238 | Rel_Node : Node_Id); | |
239 | -- Subsidiary routine to the expansion of expression_with_actions and if | |
240 | -- expressions. Generate all the necessary code to finalize a transient | |
241 | -- controlled object when the enclosing context is elaborated or evaluated. | |
242 | -- Decl denotes the declaration of the transient controlled object which is | |
243 | -- usually the result of a controlled function call. Rel_Node denotes the | |
244 | -- context, either an expression_with_actions or an if expression. | |
245 | ||
70482933 | 246 | procedure Rewrite_Comparison (N : Node_Id); |
20b5d666 | 247 | -- If N is the node for a comparison whose outcome can be determined at |
d26dc4b5 AC |
248 | -- compile time, then the node N can be rewritten with True or False. If |
249 | -- the outcome cannot be determined at compile time, the call has no | |
250 | -- effect. If N is a type conversion, then this processing is applied to | |
251 | -- its expression. If N is neither comparison nor a type conversion, the | |
252 | -- call has no effect. | |
70482933 | 253 | |
82878151 AC |
254 | procedure Tagged_Membership |
255 | (N : Node_Id; | |
256 | SCIL_Node : out Node_Id; | |
257 | Result : out Node_Id); | |
70482933 RK |
258 | -- Construct the expression corresponding to the tagged membership test. |
259 | -- Deals with a second operand being (or not) a class-wide type. | |
260 | ||
fbf5a39b | 261 | function Safe_In_Place_Array_Op |
2e071734 AC |
262 | (Lhs : Node_Id; |
263 | Op1 : Node_Id; | |
264 | Op2 : Node_Id) return Boolean; | |
685094bf RD |
265 | -- In the context of an assignment, where the right-hand side is a boolean |
266 | -- operation on arrays, check whether operation can be performed in place. | |
fbf5a39b | 267 | |
70482933 RK |
268 | procedure Unary_Op_Validity_Checks (N : Node_Id); |
269 | pragma Inline (Unary_Op_Validity_Checks); | |
270 | -- Performs validity checks for a unary operator | |
271 | ||
272 | ------------------------------- | |
273 | -- Binary_Op_Validity_Checks -- | |
274 | ------------------------------- | |
275 | ||
276 | procedure Binary_Op_Validity_Checks (N : Node_Id) is | |
277 | begin | |
278 | if Validity_Checks_On and Validity_Check_Operands then | |
279 | Ensure_Valid (Left_Opnd (N)); | |
280 | Ensure_Valid (Right_Opnd (N)); | |
281 | end if; | |
282 | end Binary_Op_Validity_Checks; | |
283 | ||
fbf5a39b AC |
284 | ------------------------------------ |
285 | -- Build_Boolean_Array_Proc_Call -- | |
286 | ------------------------------------ | |
287 | ||
288 | procedure Build_Boolean_Array_Proc_Call | |
289 | (N : Node_Id; | |
290 | Op1 : Node_Id; | |
291 | Op2 : Node_Id) | |
292 | is | |
293 | Loc : constant Source_Ptr := Sloc (N); | |
294 | Kind : constant Node_Kind := Nkind (Expression (N)); | |
295 | Target : constant Node_Id := | |
296 | Make_Attribute_Reference (Loc, | |
297 | Prefix => Name (N), | |
298 | Attribute_Name => Name_Address); | |
299 | ||
bed8af19 | 300 | Arg1 : Node_Id := Op1; |
fbf5a39b AC |
301 | Arg2 : Node_Id := Op2; |
302 | Call_Node : Node_Id; | |
303 | Proc_Name : Entity_Id; | |
304 | ||
305 | begin | |
306 | if Kind = N_Op_Not then | |
307 | if Nkind (Op1) in N_Binary_Op then | |
308 | ||
5e1c00fa | 309 | -- Use negated version of the binary operators |
fbf5a39b AC |
310 | |
311 | if Nkind (Op1) = N_Op_And then | |
312 | Proc_Name := RTE (RE_Vector_Nand); | |
313 | ||
314 | elsif Nkind (Op1) = N_Op_Or then | |
315 | Proc_Name := RTE (RE_Vector_Nor); | |
316 | ||
317 | else pragma Assert (Nkind (Op1) = N_Op_Xor); | |
318 | Proc_Name := RTE (RE_Vector_Xor); | |
319 | end if; | |
320 | ||
321 | Call_Node := | |
322 | Make_Procedure_Call_Statement (Loc, | |
323 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
324 | ||
325 | Parameter_Associations => New_List ( | |
326 | Target, | |
327 | Make_Attribute_Reference (Loc, | |
328 | Prefix => Left_Opnd (Op1), | |
329 | Attribute_Name => Name_Address), | |
330 | ||
331 | Make_Attribute_Reference (Loc, | |
332 | Prefix => Right_Opnd (Op1), | |
333 | Attribute_Name => Name_Address), | |
334 | ||
335 | Make_Attribute_Reference (Loc, | |
336 | Prefix => Left_Opnd (Op1), | |
337 | Attribute_Name => Name_Length))); | |
338 | ||
339 | else | |
340 | Proc_Name := RTE (RE_Vector_Not); | |
341 | ||
342 | Call_Node := | |
343 | Make_Procedure_Call_Statement (Loc, | |
344 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
345 | Parameter_Associations => New_List ( | |
346 | Target, | |
347 | ||
348 | Make_Attribute_Reference (Loc, | |
349 | Prefix => Op1, | |
350 | Attribute_Name => Name_Address), | |
351 | ||
352 | Make_Attribute_Reference (Loc, | |
353 | Prefix => Op1, | |
354 | Attribute_Name => Name_Length))); | |
355 | end if; | |
356 | ||
357 | else | |
358 | -- We use the following equivalences: | |
359 | ||
360 | -- (not X) or (not Y) = not (X and Y) = Nand (X, Y) | |
361 | -- (not X) and (not Y) = not (X or Y) = Nor (X, Y) | |
362 | -- (not X) xor (not Y) = X xor Y | |
363 | -- X xor (not Y) = not (X xor Y) = Nxor (X, Y) | |
364 | ||
365 | if Nkind (Op1) = N_Op_Not then | |
bed8af19 AC |
366 | Arg1 := Right_Opnd (Op1); |
367 | Arg2 := Right_Opnd (Op2); | |
533369aa | 368 | |
fbf5a39b AC |
369 | if Kind = N_Op_And then |
370 | Proc_Name := RTE (RE_Vector_Nor); | |
fbf5a39b AC |
371 | elsif Kind = N_Op_Or then |
372 | Proc_Name := RTE (RE_Vector_Nand); | |
fbf5a39b AC |
373 | else |
374 | Proc_Name := RTE (RE_Vector_Xor); | |
375 | end if; | |
376 | ||
377 | else | |
378 | if Kind = N_Op_And then | |
379 | Proc_Name := RTE (RE_Vector_And); | |
fbf5a39b AC |
380 | elsif Kind = N_Op_Or then |
381 | Proc_Name := RTE (RE_Vector_Or); | |
fbf5a39b AC |
382 | elsif Nkind (Op2) = N_Op_Not then |
383 | Proc_Name := RTE (RE_Vector_Nxor); | |
384 | Arg2 := Right_Opnd (Op2); | |
fbf5a39b AC |
385 | else |
386 | Proc_Name := RTE (RE_Vector_Xor); | |
387 | end if; | |
388 | end if; | |
389 | ||
390 | Call_Node := | |
391 | Make_Procedure_Call_Statement (Loc, | |
392 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
393 | Parameter_Associations => New_List ( | |
394 | Target, | |
955871d3 AC |
395 | Make_Attribute_Reference (Loc, |
396 | Prefix => Arg1, | |
397 | Attribute_Name => Name_Address), | |
398 | Make_Attribute_Reference (Loc, | |
399 | Prefix => Arg2, | |
400 | Attribute_Name => Name_Address), | |
401 | Make_Attribute_Reference (Loc, | |
a8ef12e5 | 402 | Prefix => Arg1, |
955871d3 | 403 | Attribute_Name => Name_Length))); |
fbf5a39b AC |
404 | end if; |
405 | ||
406 | Rewrite (N, Call_Node); | |
407 | Analyze (N); | |
408 | ||
409 | exception | |
410 | when RE_Not_Available => | |
411 | return; | |
412 | end Build_Boolean_Array_Proc_Call; | |
413 | ||
11fa950b AC |
414 | ------------------------------ |
415 | -- Current_Anonymous_Master -- | |
416 | ------------------------------ | |
df3e68b1 | 417 | |
11fa950b | 418 | function Current_Anonymous_Master return Entity_Id is |
2c17ca0a AC |
419 | Decls : List_Id; |
420 | Loc : Source_Ptr; | |
421 | Subp_Body : Node_Id; | |
422 | Unit_Decl : Node_Id; | |
423 | Unit_Id : Entity_Id; | |
df3e68b1 | 424 | |
ca5af305 | 425 | begin |
11fa950b AC |
426 | Unit_Id := Cunit_Entity (Current_Sem_Unit); |
427 | ||
428 | -- Find the entity of the current unit | |
429 | ||
430 | if Ekind (Unit_Id) = E_Subprogram_Body then | |
431 | ||
432 | -- When processing subprogram bodies, the proper scope is always that | |
433 | -- of the spec. | |
434 | ||
435 | Subp_Body := Unit_Id; | |
436 | while Present (Subp_Body) | |
437 | and then Nkind (Subp_Body) /= N_Subprogram_Body | |
438 | loop | |
439 | Subp_Body := Parent (Subp_Body); | |
440 | end loop; | |
441 | ||
442 | Unit_Id := Corresponding_Spec (Subp_Body); | |
443 | end if; | |
444 | ||
445 | Loc := Sloc (Unit_Id); | |
446 | Unit_Decl := Unit (Cunit (Current_Sem_Unit)); | |
447 | ||
448 | -- Find the declarations list of the current unit | |
449 | ||
450 | if Nkind (Unit_Decl) = N_Package_Declaration then | |
451 | Unit_Decl := Specification (Unit_Decl); | |
452 | Decls := Visible_Declarations (Unit_Decl); | |
df3e68b1 | 453 | |
ca5af305 | 454 | if No (Decls) then |
11fa950b AC |
455 | Decls := New_List (Make_Null_Statement (Loc)); |
456 | Set_Visible_Declarations (Unit_Decl, Decls); | |
df3e68b1 | 457 | |
ca5af305 | 458 | elsif Is_Empty_List (Decls) then |
11fa950b | 459 | Append_To (Decls, Make_Null_Statement (Loc)); |
df3e68b1 HK |
460 | end if; |
461 | ||
ca5af305 | 462 | else |
11fa950b | 463 | Decls := Declarations (Unit_Decl); |
f553e7bc | 464 | |
ca5af305 | 465 | if No (Decls) then |
11fa950b AC |
466 | Decls := New_List (Make_Null_Statement (Loc)); |
467 | Set_Declarations (Unit_Decl, Decls); | |
df3e68b1 | 468 | |
ca5af305 | 469 | elsif Is_Empty_List (Decls) then |
11fa950b | 470 | Append_To (Decls, Make_Null_Statement (Loc)); |
ca5af305 | 471 | end if; |
df3e68b1 HK |
472 | end if; |
473 | ||
11fa950b AC |
474 | -- The current unit has an existing anonymous master, traverse its |
475 | -- declarations and locate the entity. | |
df3e68b1 | 476 | |
11fa950b | 477 | if Has_Anonymous_Master (Unit_Id) then |
2c17ca0a AC |
478 | declare |
479 | Decl : Node_Id; | |
480 | Fin_Mas_Id : Entity_Id; | |
df3e68b1 | 481 | |
2c17ca0a AC |
482 | begin |
483 | Decl := First (Decls); | |
484 | while Present (Decl) loop | |
df3e68b1 | 485 | |
2c17ca0a AC |
486 | -- Look for the first variable in the declarations whole type |
487 | -- is Finalization_Master. | |
df3e68b1 | 488 | |
2c17ca0a AC |
489 | if Nkind (Decl) = N_Object_Declaration then |
490 | Fin_Mas_Id := Defining_Identifier (Decl); | |
491 | ||
492 | if Ekind (Fin_Mas_Id) = E_Variable | |
493 | and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master) | |
494 | then | |
495 | return Fin_Mas_Id; | |
496 | end if; | |
497 | end if; | |
498 | ||
499 | Next (Decl); | |
500 | end loop; | |
501 | ||
502 | -- The master was not found even though the unit was labeled as | |
503 | -- having one. | |
df3e68b1 | 504 | |
2c17ca0a AC |
505 | raise Program_Error; |
506 | end; | |
11fa950b AC |
507 | |
508 | -- Create a new anonymous master | |
509 | ||
510 | else | |
511 | declare | |
512 | First_Decl : constant Node_Id := First (Decls); | |
513 | Action : Node_Id; | |
2c17ca0a | 514 | Fin_Mas_Id : Entity_Id; |
df3e68b1 | 515 | |
11fa950b AC |
516 | begin |
517 | -- Since the master and its associated initialization is inserted | |
518 | -- at top level, use the scope of the unit when analyzing. | |
519 | ||
520 | Push_Scope (Unit_Id); | |
521 | ||
522 | -- Create the finalization master | |
523 | ||
524 | Fin_Mas_Id := | |
525 | Make_Defining_Identifier (Loc, | |
526 | Chars => New_External_Name (Chars (Unit_Id), "AM")); | |
527 | ||
528 | -- Generate: | |
529 | -- <Fin_Mas_Id> : Finalization_Master; | |
530 | ||
531 | Action := | |
532 | Make_Object_Declaration (Loc, | |
533 | Defining_Identifier => Fin_Mas_Id, | |
534 | Object_Definition => | |
e4494292 | 535 | New_Occurrence_Of (RTE (RE_Finalization_Master), Loc)); |
11fa950b AC |
536 | |
537 | Insert_Before_And_Analyze (First_Decl, Action); | |
538 | ||
539 | -- Mark the unit to prevent the generation of multiple masters | |
540 | ||
541 | Set_Has_Anonymous_Master (Unit_Id); | |
542 | ||
543 | -- Do not set the base pool and mode of operation on .NET/JVM | |
544 | -- since those targets do not support pools and all VM masters | |
545 | -- are heterogeneous by default. | |
546 | ||
547 | if VM_Target = No_VM then | |
548 | ||
549 | -- Generate: | |
550 | -- Set_Base_Pool | |
551 | -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access); | |
552 | ||
553 | Action := | |
554 | Make_Procedure_Call_Statement (Loc, | |
555 | Name => | |
e4494292 | 556 | New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc), |
11fa950b AC |
557 | |
558 | Parameter_Associations => New_List ( | |
e4494292 | 559 | New_Occurrence_Of (Fin_Mas_Id, Loc), |
11fa950b AC |
560 | Make_Attribute_Reference (Loc, |
561 | Prefix => | |
e4494292 | 562 | New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc), |
11fa950b AC |
563 | Attribute_Name => Name_Unrestricted_Access))); |
564 | ||
565 | Insert_Before_And_Analyze (First_Decl, Action); | |
566 | ||
567 | -- Generate: | |
568 | -- Set_Is_Heterogeneous (<Fin_Mas_Id>); | |
569 | ||
570 | Action := | |
571 | Make_Procedure_Call_Statement (Loc, | |
572 | Name => | |
e4494292 | 573 | New_Occurrence_Of (RTE (RE_Set_Is_Heterogeneous), Loc), |
11fa950b | 574 | Parameter_Associations => New_List ( |
e4494292 | 575 | New_Occurrence_Of (Fin_Mas_Id, Loc))); |
11fa950b AC |
576 | |
577 | Insert_Before_And_Analyze (First_Decl, Action); | |
578 | end if; | |
579 | ||
580 | -- Restore the original state of the scope stack | |
581 | ||
582 | Pop_Scope; | |
583 | ||
584 | return Fin_Mas_Id; | |
585 | end; | |
586 | end if; | |
587 | end Current_Anonymous_Master; | |
df3e68b1 | 588 | |
26bff3d9 JM |
589 | -------------------------------- |
590 | -- Displace_Allocator_Pointer -- | |
591 | -------------------------------- | |
592 | ||
593 | procedure Displace_Allocator_Pointer (N : Node_Id) is | |
594 | Loc : constant Source_Ptr := Sloc (N); | |
595 | Orig_Node : constant Node_Id := Original_Node (N); | |
596 | Dtyp : Entity_Id; | |
597 | Etyp : Entity_Id; | |
598 | PtrT : Entity_Id; | |
599 | ||
600 | begin | |
303b4d58 AC |
601 | -- Do nothing in case of VM targets: the virtual machine will handle |
602 | -- interfaces directly. | |
603 | ||
1f110335 | 604 | if not Tagged_Type_Expansion then |
303b4d58 AC |
605 | return; |
606 | end if; | |
607 | ||
26bff3d9 JM |
608 | pragma Assert (Nkind (N) = N_Identifier |
609 | and then Nkind (Orig_Node) = N_Allocator); | |
610 | ||
611 | PtrT := Etype (Orig_Node); | |
d6a24cdb | 612 | Dtyp := Available_View (Designated_Type (PtrT)); |
26bff3d9 JM |
613 | Etyp := Etype (Expression (Orig_Node)); |
614 | ||
533369aa AC |
615 | if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then |
616 | ||
26bff3d9 JM |
617 | -- If the type of the allocator expression is not an interface type |
618 | -- we can generate code to reference the record component containing | |
619 | -- the pointer to the secondary dispatch table. | |
620 | ||
621 | if not Is_Interface (Etyp) then | |
622 | declare | |
623 | Saved_Typ : constant Entity_Id := Etype (Orig_Node); | |
624 | ||
625 | begin | |
626 | -- 1) Get access to the allocated object | |
627 | ||
628 | Rewrite (N, | |
5972791c | 629 | Make_Explicit_Dereference (Loc, Relocate_Node (N))); |
26bff3d9 JM |
630 | Set_Etype (N, Etyp); |
631 | Set_Analyzed (N); | |
632 | ||
633 | -- 2) Add the conversion to displace the pointer to reference | |
634 | -- the secondary dispatch table. | |
635 | ||
636 | Rewrite (N, Convert_To (Dtyp, Relocate_Node (N))); | |
637 | Analyze_And_Resolve (N, Dtyp); | |
638 | ||
639 | -- 3) The 'access to the secondary dispatch table will be used | |
640 | -- as the value returned by the allocator. | |
641 | ||
642 | Rewrite (N, | |
643 | Make_Attribute_Reference (Loc, | |
644 | Prefix => Relocate_Node (N), | |
645 | Attribute_Name => Name_Access)); | |
646 | Set_Etype (N, Saved_Typ); | |
647 | Set_Analyzed (N); | |
648 | end; | |
649 | ||
650 | -- If the type of the allocator expression is an interface type we | |
651 | -- generate a run-time call to displace "this" to reference the | |
652 | -- component containing the pointer to the secondary dispatch table | |
653 | -- or else raise Constraint_Error if the actual object does not | |
533369aa | 654 | -- implement the target interface. This case corresponds to the |
26bff3d9 JM |
655 | -- following example: |
656 | ||
8fc789c8 | 657 | -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is |
26bff3d9 JM |
658 | -- begin |
659 | -- return new Iface_2'Class'(Obj); | |
660 | -- end Op; | |
661 | ||
662 | else | |
663 | Rewrite (N, | |
664 | Unchecked_Convert_To (PtrT, | |
665 | Make_Function_Call (Loc, | |
e4494292 | 666 | Name => New_Occurrence_Of (RTE (RE_Displace), Loc), |
26bff3d9 JM |
667 | Parameter_Associations => New_List ( |
668 | Unchecked_Convert_To (RTE (RE_Address), | |
669 | Relocate_Node (N)), | |
670 | ||
671 | New_Occurrence_Of | |
672 | (Elists.Node | |
673 | (First_Elmt | |
674 | (Access_Disp_Table (Etype (Base_Type (Dtyp))))), | |
675 | Loc))))); | |
676 | Analyze_And_Resolve (N, PtrT); | |
677 | end if; | |
678 | end if; | |
679 | end Displace_Allocator_Pointer; | |
680 | ||
fbf5a39b AC |
681 | --------------------------------- |
682 | -- Expand_Allocator_Expression -- | |
683 | --------------------------------- | |
684 | ||
685 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
f02b8bb8 RD |
686 | Loc : constant Source_Ptr := Sloc (N); |
687 | Exp : constant Node_Id := Expression (Expression (N)); | |
f02b8bb8 RD |
688 | PtrT : constant Entity_Id := Etype (N); |
689 | DesigT : constant Entity_Id := Designated_Type (PtrT); | |
26bff3d9 JM |
690 | |
691 | procedure Apply_Accessibility_Check | |
692 | (Ref : Node_Id; | |
693 | Built_In_Place : Boolean := False); | |
694 | -- Ada 2005 (AI-344): For an allocator with a class-wide designated | |
685094bf RD |
695 | -- type, generate an accessibility check to verify that the level of the |
696 | -- type of the created object is not deeper than the level of the access | |
50878404 | 697 | -- type. If the type of the qualified expression is class-wide, then |
685094bf RD |
698 | -- always generate the check (except in the case where it is known to be |
699 | -- unnecessary, see comment below). Otherwise, only generate the check | |
700 | -- if the level of the qualified expression type is statically deeper | |
701 | -- than the access type. | |
702 | -- | |
703 | -- Although the static accessibility will generally have been performed | |
704 | -- as a legality check, it won't have been done in cases where the | |
705 | -- allocator appears in generic body, so a run-time check is needed in | |
706 | -- general. One special case is when the access type is declared in the | |
707 | -- same scope as the class-wide allocator, in which case the check can | |
708 | -- never fail, so it need not be generated. | |
709 | -- | |
710 | -- As an open issue, there seem to be cases where the static level | |
711 | -- associated with the class-wide object's underlying type is not | |
712 | -- sufficient to perform the proper accessibility check, such as for | |
713 | -- allocators in nested subprograms or accept statements initialized by | |
714 | -- class-wide formals when the actual originates outside at a deeper | |
715 | -- static level. The nested subprogram case might require passing | |
716 | -- accessibility levels along with class-wide parameters, and the task | |
717 | -- case seems to be an actual gap in the language rules that needs to | |
718 | -- be fixed by the ARG. ??? | |
26bff3d9 JM |
719 | |
720 | ------------------------------- | |
721 | -- Apply_Accessibility_Check -- | |
722 | ------------------------------- | |
723 | ||
724 | procedure Apply_Accessibility_Check | |
725 | (Ref : Node_Id; | |
726 | Built_In_Place : Boolean := False) | |
727 | is | |
a98838ff HK |
728 | Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT); |
729 | Cond : Node_Id; | |
730 | Fin_Call : Node_Id; | |
731 | Free_Stmt : Node_Id; | |
732 | Obj_Ref : Node_Id; | |
733 | Stmts : List_Id; | |
26bff3d9 JM |
734 | |
735 | begin | |
0791fbe9 | 736 | if Ada_Version >= Ada_2005 |
26bff3d9 | 737 | and then Is_Class_Wide_Type (DesigT) |
a98838ff | 738 | and then (Tagged_Type_Expansion or else VM_Target /= No_VM) |
3217f71e | 739 | and then not Scope_Suppress.Suppress (Accessibility_Check) |
26bff3d9 JM |
740 | and then |
741 | (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT) | |
742 | or else | |
743 | (Is_Class_Wide_Type (Etype (Exp)) | |
744 | and then Scope (PtrT) /= Current_Scope)) | |
745 | then | |
e761d11c | 746 | -- If the allocator was built in place, Ref is already a reference |
26bff3d9 | 747 | -- to the access object initialized to the result of the allocator |
e761d11c AC |
748 | -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call |
749 | -- Remove_Side_Effects for cases where the build-in-place call may | |
750 | -- still be the prefix of the reference (to avoid generating | |
751 | -- duplicate calls). Otherwise, it is the entity associated with | |
752 | -- the object containing the address of the allocated object. | |
26bff3d9 JM |
753 | |
754 | if Built_In_Place then | |
e761d11c | 755 | Remove_Side_Effects (Ref); |
a98838ff | 756 | Obj_Ref := New_Copy_Tree (Ref); |
26bff3d9 | 757 | else |
e4494292 | 758 | Obj_Ref := New_Occurrence_Of (Ref, Loc); |
50878404 AC |
759 | end if; |
760 | ||
761 | -- Step 1: Create the object clean up code | |
762 | ||
763 | Stmts := New_List; | |
764 | ||
a98838ff HK |
765 | -- Deallocate the object if the accessibility check fails. This |
766 | -- is done only on targets or profiles that support deallocation. | |
767 | ||
768 | -- Free (Obj_Ref); | |
769 | ||
770 | if RTE_Available (RE_Free) then | |
771 | Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref)); | |
772 | Set_Storage_Pool (Free_Stmt, Pool_Id); | |
773 | ||
774 | Append_To (Stmts, Free_Stmt); | |
775 | ||
776 | -- The target or profile cannot deallocate objects | |
777 | ||
778 | else | |
779 | Free_Stmt := Empty; | |
780 | end if; | |
781 | ||
782 | -- Finalize the object if applicable. Generate: | |
a530b8bb AC |
783 | |
784 | -- [Deep_]Finalize (Obj_Ref.all); | |
785 | ||
2cbac6c6 | 786 | if Needs_Finalization (DesigT) then |
a98838ff | 787 | Fin_Call := |
2cbac6c6 AC |
788 | Make_Final_Call ( |
789 | Obj_Ref => | |
790 | Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)), | |
a98838ff HK |
791 | Typ => DesigT); |
792 | ||
793 | -- When the target or profile supports deallocation, wrap the | |
794 | -- finalization call in a block to ensure proper deallocation | |
795 | -- even if finalization fails. Generate: | |
796 | ||
797 | -- begin | |
798 | -- <Fin_Call> | |
799 | -- exception | |
800 | -- when others => | |
801 | -- <Free_Stmt> | |
802 | -- raise; | |
803 | -- end; | |
804 | ||
805 | if Present (Free_Stmt) then | |
806 | Fin_Call := | |
807 | Make_Block_Statement (Loc, | |
808 | Handled_Statement_Sequence => | |
809 | Make_Handled_Sequence_Of_Statements (Loc, | |
810 | Statements => New_List (Fin_Call), | |
811 | ||
812 | Exception_Handlers => New_List ( | |
813 | Make_Exception_Handler (Loc, | |
814 | Exception_Choices => New_List ( | |
815 | Make_Others_Choice (Loc)), | |
816 | ||
817 | Statements => New_List ( | |
818 | New_Copy_Tree (Free_Stmt), | |
819 | Make_Raise_Statement (Loc)))))); | |
820 | end if; | |
821 | ||
822 | Prepend_To (Stmts, Fin_Call); | |
f46faa08 AC |
823 | end if; |
824 | ||
50878404 AC |
825 | -- Signal the accessibility failure through a Program_Error |
826 | ||
827 | Append_To (Stmts, | |
828 | Make_Raise_Program_Error (Loc, | |
e4494292 | 829 | Condition => New_Occurrence_Of (Standard_True, Loc), |
50878404 AC |
830 | Reason => PE_Accessibility_Check_Failed)); |
831 | ||
832 | -- Step 2: Create the accessibility comparison | |
833 | ||
da574a86 AC |
834 | -- Reference the tag: for a renaming of an access to an interface |
835 | -- object Obj_Ref already references the tag of the secondary | |
836 | -- dispatch table. | |
837 | ||
601bb6b3 AC |
838 | if Nkind (Obj_Ref) in N_Has_Entity |
839 | and then Present (Entity (Obj_Ref)) | |
da574a86 AC |
840 | and then Present (Renamed_Object (Entity (Obj_Ref))) |
841 | and then Is_Interface (DesigT) | |
842 | then | |
843 | null; | |
844 | ||
50878404 AC |
845 | -- Generate: |
846 | -- Ref'Tag | |
847 | ||
da574a86 AC |
848 | else |
849 | Obj_Ref := | |
850 | Make_Attribute_Reference (Loc, | |
851 | Prefix => Obj_Ref, | |
852 | Attribute_Name => Name_Tag); | |
853 | end if; | |
f46faa08 | 854 | |
50878404 AC |
855 | -- For tagged types, determine the accessibility level by looking |
856 | -- at the type specific data of the dispatch table. Generate: | |
857 | ||
858 | -- Type_Specific_Data (Address (Ref'Tag)).Access_Level | |
859 | ||
f46faa08 | 860 | if Tagged_Type_Expansion then |
50878404 | 861 | Cond := Build_Get_Access_Level (Loc, Obj_Ref); |
f46faa08 | 862 | |
50878404 AC |
863 | -- Use a runtime call to determine the accessibility level when |
864 | -- compiling on virtual machine targets. Generate: | |
f46faa08 | 865 | |
50878404 | 866 | -- Get_Access_Level (Ref'Tag) |
f46faa08 AC |
867 | |
868 | else | |
50878404 AC |
869 | Cond := |
870 | Make_Function_Call (Loc, | |
871 | Name => | |
e4494292 | 872 | New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc), |
50878404 | 873 | Parameter_Associations => New_List (Obj_Ref)); |
26bff3d9 JM |
874 | end if; |
875 | ||
50878404 AC |
876 | Cond := |
877 | Make_Op_Gt (Loc, | |
878 | Left_Opnd => Cond, | |
879 | Right_Opnd => | |
880 | Make_Integer_Literal (Loc, Type_Access_Level (PtrT))); | |
881 | ||
882 | -- Due to the complexity and side effects of the check, utilize an | |
883 | -- if statement instead of the regular Program_Error circuitry. | |
884 | ||
26bff3d9 | 885 | Insert_Action (N, |
8b1011c0 | 886 | Make_Implicit_If_Statement (N, |
50878404 AC |
887 | Condition => Cond, |
888 | Then_Statements => Stmts)); | |
26bff3d9 JM |
889 | end if; |
890 | end Apply_Accessibility_Check; | |
891 | ||
892 | -- Local variables | |
893 | ||
df3e68b1 HK |
894 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); |
895 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
896 | T : constant Entity_Id := Entity (Indic); | |
897 | Node : Node_Id; | |
898 | Tag_Assign : Node_Id; | |
899 | Temp : Entity_Id; | |
900 | Temp_Decl : Node_Id; | |
fbf5a39b | 901 | |
d26dc4b5 AC |
902 | TagT : Entity_Id := Empty; |
903 | -- Type used as source for tag assignment | |
904 | ||
905 | TagR : Node_Id := Empty; | |
906 | -- Target reference for tag assignment | |
907 | ||
26bff3d9 JM |
908 | -- Start of processing for Expand_Allocator_Expression |
909 | ||
fbf5a39b | 910 | begin |
3bfb3c03 JM |
911 | -- Handle call to C++ constructor |
912 | ||
913 | if Is_CPP_Constructor_Call (Exp) then | |
914 | Make_CPP_Constructor_Call_In_Allocator | |
915 | (Allocator => N, | |
916 | Function_Call => Exp); | |
917 | return; | |
918 | end if; | |
919 | ||
885c4871 | 920 | -- In the case of an Ada 2012 allocator whose initial value comes from a |
63585f75 SB |
921 | -- function call, pass "the accessibility level determined by the point |
922 | -- of call" (AI05-0234) to the function. Conceptually, this belongs in | |
923 | -- Expand_Call but it couldn't be done there (because the Etype of the | |
924 | -- allocator wasn't set then) so we generate the parameter here. See | |
925 | -- the Boolean variable Defer in (a block within) Expand_Call. | |
926 | ||
927 | if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then | |
928 | declare | |
929 | Subp : Entity_Id; | |
930 | ||
931 | begin | |
932 | if Nkind (Name (Exp)) = N_Explicit_Dereference then | |
933 | Subp := Designated_Type (Etype (Prefix (Name (Exp)))); | |
934 | else | |
935 | Subp := Entity (Name (Exp)); | |
936 | end if; | |
937 | ||
57a3fca9 AC |
938 | Subp := Ultimate_Alias (Subp); |
939 | ||
63585f75 SB |
940 | if Present (Extra_Accessibility_Of_Result (Subp)) then |
941 | Add_Extra_Actual_To_Call | |
942 | (Subprogram_Call => Exp, | |
943 | Extra_Formal => Extra_Accessibility_Of_Result (Subp), | |
944 | Extra_Actual => Dynamic_Accessibility_Level (PtrT)); | |
945 | end if; | |
946 | end; | |
947 | end if; | |
948 | ||
f6194278 | 949 | -- Case of tagged type or type requiring finalization |
63585f75 SB |
950 | |
951 | if Is_Tagged_Type (T) or else Needs_Finalization (T) then | |
fadcf313 | 952 | |
685094bf RD |
953 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
954 | -- to a build-in-place function, then access to the allocated object | |
955 | -- must be passed to the function. Currently we limit such functions | |
956 | -- to those with constrained limited result subtypes, but eventually | |
957 | -- we plan to expand the allowed forms of functions that are treated | |
958 | -- as build-in-place. | |
20b5d666 | 959 | |
0791fbe9 | 960 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
961 | and then Is_Build_In_Place_Function_Call (Exp) |
962 | then | |
963 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
26bff3d9 JM |
964 | Apply_Accessibility_Check (N, Built_In_Place => True); |
965 | return; | |
20b5d666 JM |
966 | end if; |
967 | ||
ca5af305 AC |
968 | -- Actions inserted before: |
969 | -- Temp : constant ptr_T := new T'(Expression); | |
970 | -- Temp._tag = T'tag; -- when not class-wide | |
971 | -- [Deep_]Adjust (Temp.all); | |
fbf5a39b | 972 | |
ca5af305 | 973 | -- We analyze by hand the new internal allocator to avoid any |
6b6041ec | 974 | -- recursion and inappropriate call to Initialize. |
7324bf49 | 975 | |
20b5d666 JM |
976 | -- We don't want to remove side effects when the expression must be |
977 | -- built in place. In the case of a build-in-place function call, | |
978 | -- that could lead to a duplication of the call, which was already | |
979 | -- substituted for the allocator. | |
980 | ||
26bff3d9 | 981 | if not Aggr_In_Place then |
fbf5a39b AC |
982 | Remove_Side_Effects (Exp); |
983 | end if; | |
984 | ||
e86a3a7e | 985 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
986 | |
987 | -- For a class wide allocation generate the following code: | |
988 | ||
989 | -- type Equiv_Record is record ... end record; | |
990 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
991 | -- temp : PtrT := new CW'(CW!(expr)); | |
992 | ||
993 | if Is_Class_Wide_Type (T) then | |
994 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
995 | ||
26bff3d9 JM |
996 | -- Ada 2005 (AI-251): If the expression is a class-wide interface |
997 | -- object we generate code to move up "this" to reference the | |
998 | -- base of the object before allocating the new object. | |
999 | ||
1000 | -- Note that Exp'Address is recursively expanded into a call | |
1001 | -- to Base_Address (Exp.Tag) | |
1002 | ||
1003 | if Is_Class_Wide_Type (Etype (Exp)) | |
1004 | and then Is_Interface (Etype (Exp)) | |
1f110335 | 1005 | and then Tagged_Type_Expansion |
26bff3d9 JM |
1006 | then |
1007 | Set_Expression | |
1008 | (Expression (N), | |
1009 | Unchecked_Convert_To (Entity (Indic), | |
1010 | Make_Explicit_Dereference (Loc, | |
1011 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
1012 | Make_Attribute_Reference (Loc, | |
1013 | Prefix => Exp, | |
1014 | Attribute_Name => Name_Address))))); | |
26bff3d9 JM |
1015 | else |
1016 | Set_Expression | |
1017 | (Expression (N), | |
1018 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
1019 | end if; | |
fbf5a39b AC |
1020 | |
1021 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
1022 | end if; | |
1023 | ||
df3e68b1 | 1024 | -- Processing for allocators returning non-interface types |
fbf5a39b | 1025 | |
26bff3d9 JM |
1026 | if not Is_Interface (Directly_Designated_Type (PtrT)) then |
1027 | if Aggr_In_Place then | |
df3e68b1 | 1028 | Temp_Decl := |
26bff3d9 JM |
1029 | Make_Object_Declaration (Loc, |
1030 | Defining_Identifier => Temp, | |
e4494292 | 1031 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
26bff3d9 JM |
1032 | Expression => |
1033 | Make_Allocator (Loc, | |
df3e68b1 | 1034 | Expression => |
e4494292 | 1035 | New_Occurrence_Of (Etype (Exp), Loc))); |
fbf5a39b | 1036 | |
fad0600d AC |
1037 | -- Copy the Comes_From_Source flag for the allocator we just |
1038 | -- built, since logically this allocator is a replacement of | |
1039 | -- the original allocator node. This is for proper handling of | |
1040 | -- restriction No_Implicit_Heap_Allocations. | |
1041 | ||
26bff3d9 | 1042 | Set_Comes_From_Source |
df3e68b1 | 1043 | (Expression (Temp_Decl), Comes_From_Source (N)); |
fbf5a39b | 1044 | |
df3e68b1 HK |
1045 | Set_No_Initialization (Expression (Temp_Decl)); |
1046 | Insert_Action (N, Temp_Decl); | |
fbf5a39b | 1047 | |
ca5af305 | 1048 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1049 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fad0600d | 1050 | |
d3f70b35 | 1051 | -- Attach the object to the associated finalization master. |
deb8dacc HK |
1052 | -- This is done manually on .NET/JVM since those compilers do |
1053 | -- no support pools and can't benefit from internally generated | |
1054 | -- Allocate / Deallocate procedures. | |
1055 | ||
1056 | if VM_Target /= No_VM | |
1057 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1058 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1059 | then |
1060 | Insert_Action (N, | |
1061 | Make_Attach_Call ( | |
e4494292 | 1062 | Obj_Ref => New_Occurrence_Of (Temp, Loc), |
deb8dacc HK |
1063 | Ptr_Typ => PtrT)); |
1064 | end if; | |
1065 | ||
26bff3d9 JM |
1066 | else |
1067 | Node := Relocate_Node (N); | |
1068 | Set_Analyzed (Node); | |
df3e68b1 HK |
1069 | |
1070 | Temp_Decl := | |
26bff3d9 JM |
1071 | Make_Object_Declaration (Loc, |
1072 | Defining_Identifier => Temp, | |
1073 | Constant_Present => True, | |
e4494292 | 1074 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
df3e68b1 HK |
1075 | Expression => Node); |
1076 | ||
1077 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 1078 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
deb8dacc | 1079 | |
d3f70b35 | 1080 | -- Attach the object to the associated finalization master. |
deb8dacc HK |
1081 | -- This is done manually on .NET/JVM since those compilers do |
1082 | -- no support pools and can't benefit from internally generated | |
1083 | -- Allocate / Deallocate procedures. | |
1084 | ||
1085 | if VM_Target /= No_VM | |
1086 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1087 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1088 | then |
1089 | Insert_Action (N, | |
1090 | Make_Attach_Call ( | |
1091 | Obj_Ref => | |
e4494292 | 1092 | New_Occurrence_Of (Temp, Loc), |
deb8dacc HK |
1093 | Ptr_Typ => PtrT)); |
1094 | end if; | |
fbf5a39b AC |
1095 | end if; |
1096 | ||
26bff3d9 JM |
1097 | -- Ada 2005 (AI-251): Handle allocators whose designated type is an |
1098 | -- interface type. In this case we use the type of the qualified | |
1099 | -- expression to allocate the object. | |
1100 | ||
fbf5a39b | 1101 | else |
26bff3d9 | 1102 | declare |
191fcb3a | 1103 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); |
26bff3d9 | 1104 | New_Decl : Node_Id; |
fbf5a39b | 1105 | |
26bff3d9 JM |
1106 | begin |
1107 | New_Decl := | |
1108 | Make_Full_Type_Declaration (Loc, | |
1109 | Defining_Identifier => Def_Id, | |
1110 | Type_Definition => | |
1111 | Make_Access_To_Object_Definition (Loc, | |
1112 | All_Present => True, | |
1113 | Null_Exclusion_Present => False, | |
0929eaeb AC |
1114 | Constant_Present => |
1115 | Is_Access_Constant (Etype (N)), | |
26bff3d9 | 1116 | Subtype_Indication => |
e4494292 | 1117 | New_Occurrence_Of (Etype (Exp), Loc))); |
26bff3d9 JM |
1118 | |
1119 | Insert_Action (N, New_Decl); | |
1120 | ||
df3e68b1 HK |
1121 | -- Inherit the allocation-related attributes from the original |
1122 | -- access type. | |
26bff3d9 | 1123 | |
d3f70b35 | 1124 | Set_Finalization_Master (Def_Id, Finalization_Master (PtrT)); |
df3e68b1 HK |
1125 | |
1126 | Set_Associated_Storage_Pool (Def_Id, | |
1127 | Associated_Storage_Pool (PtrT)); | |
758c442c | 1128 | |
26bff3d9 JM |
1129 | -- Declare the object using the previous type declaration |
1130 | ||
1131 | if Aggr_In_Place then | |
df3e68b1 | 1132 | Temp_Decl := |
26bff3d9 JM |
1133 | Make_Object_Declaration (Loc, |
1134 | Defining_Identifier => Temp, | |
e4494292 | 1135 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
26bff3d9 JM |
1136 | Expression => |
1137 | Make_Allocator (Loc, | |
e4494292 | 1138 | New_Occurrence_Of (Etype (Exp), Loc))); |
26bff3d9 | 1139 | |
fad0600d AC |
1140 | -- Copy the Comes_From_Source flag for the allocator we just |
1141 | -- built, since logically this allocator is a replacement of | |
1142 | -- the original allocator node. This is for proper handling | |
1143 | -- of restriction No_Implicit_Heap_Allocations. | |
1144 | ||
26bff3d9 | 1145 | Set_Comes_From_Source |
df3e68b1 | 1146 | (Expression (Temp_Decl), Comes_From_Source (N)); |
26bff3d9 | 1147 | |
df3e68b1 HK |
1148 | Set_No_Initialization (Expression (Temp_Decl)); |
1149 | Insert_Action (N, Temp_Decl); | |
26bff3d9 | 1150 | |
ca5af305 | 1151 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1152 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
26bff3d9 | 1153 | |
26bff3d9 JM |
1154 | else |
1155 | Node := Relocate_Node (N); | |
1156 | Set_Analyzed (Node); | |
df3e68b1 HK |
1157 | |
1158 | Temp_Decl := | |
26bff3d9 JM |
1159 | Make_Object_Declaration (Loc, |
1160 | Defining_Identifier => Temp, | |
1161 | Constant_Present => True, | |
e4494292 | 1162 | Object_Definition => New_Occurrence_Of (Def_Id, Loc), |
df3e68b1 HK |
1163 | Expression => Node); |
1164 | ||
1165 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 1166 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
26bff3d9 JM |
1167 | end if; |
1168 | ||
1169 | -- Generate an additional object containing the address of the | |
1170 | -- returned object. The type of this second object declaration | |
685094bf RD |
1171 | -- is the correct type required for the common processing that |
1172 | -- is still performed by this subprogram. The displacement of | |
1173 | -- this pointer to reference the component associated with the | |
1174 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
1175 | |
1176 | New_Decl := | |
1177 | Make_Object_Declaration (Loc, | |
243cae0a | 1178 | Defining_Identifier => Make_Temporary (Loc, 'P'), |
e4494292 | 1179 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
243cae0a | 1180 | Expression => |
df3e68b1 | 1181 | Unchecked_Convert_To (PtrT, |
e4494292 | 1182 | New_Occurrence_Of (Temp, Loc))); |
26bff3d9 JM |
1183 | |
1184 | Insert_Action (N, New_Decl); | |
1185 | ||
df3e68b1 HK |
1186 | Temp_Decl := New_Decl; |
1187 | Temp := Defining_Identifier (New_Decl); | |
26bff3d9 | 1188 | end; |
758c442c GD |
1189 | end if; |
1190 | ||
26bff3d9 JM |
1191 | Apply_Accessibility_Check (Temp); |
1192 | ||
1193 | -- Generate the tag assignment | |
1194 | ||
1195 | -- Suppress the tag assignment when VM_Target because VM tags are | |
1196 | -- represented implicitly in objects. | |
1197 | ||
1f110335 | 1198 | if not Tagged_Type_Expansion then |
26bff3d9 | 1199 | null; |
fbf5a39b | 1200 | |
26bff3d9 JM |
1201 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
1202 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 1203 | |
26bff3d9 JM |
1204 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
1205 | pragma Assert (Is_Class_Wide_Type | |
1206 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
1207 | null; |
1208 | ||
1209 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
1210 | TagT := T; | |
e4494292 | 1211 | TagR := New_Occurrence_Of (Temp, Loc); |
d26dc4b5 AC |
1212 | |
1213 | elsif Is_Private_Type (T) | |
1214 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 1215 | then |
d26dc4b5 | 1216 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
1217 | TagR := |
1218 | Unchecked_Convert_To (Underlying_Type (T), | |
1219 | Make_Explicit_Dereference (Loc, | |
e4494292 | 1220 | Prefix => New_Occurrence_Of (Temp, Loc))); |
d26dc4b5 AC |
1221 | end if; |
1222 | ||
1223 | if Present (TagT) then | |
38171f43 AC |
1224 | declare |
1225 | Full_T : constant Entity_Id := Underlying_Type (TagT); | |
e4494292 | 1226 | |
38171f43 AC |
1227 | begin |
1228 | Tag_Assign := | |
1229 | Make_Assignment_Statement (Loc, | |
1230 | Name => | |
1231 | Make_Selected_Component (Loc, | |
1232 | Prefix => TagR, | |
1233 | Selector_Name => | |
e4494292 RD |
1234 | New_Occurrence_Of |
1235 | (First_Tag_Component (Full_T), Loc)), | |
1236 | ||
38171f43 AC |
1237 | Expression => |
1238 | Unchecked_Convert_To (RTE (RE_Tag), | |
e4494292 | 1239 | New_Occurrence_Of |
38171f43 AC |
1240 | (Elists.Node |
1241 | (First_Elmt (Access_Disp_Table (Full_T))), Loc))); | |
1242 | end; | |
fbf5a39b AC |
1243 | |
1244 | -- The previous assignment has to be done in any case | |
1245 | ||
1246 | Set_Assignment_OK (Name (Tag_Assign)); | |
1247 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
1248 | end if; |
1249 | ||
533369aa AC |
1250 | if Needs_Finalization (DesigT) and then Needs_Finalization (T) then |
1251 | ||
df3e68b1 HK |
1252 | -- Generate an Adjust call if the object will be moved. In Ada |
1253 | -- 2005, the object may be inherently limited, in which case | |
1254 | -- there is no Adjust procedure, and the object is built in | |
1255 | -- place. In Ada 95, the object can be limited but not | |
1256 | -- inherently limited if this allocator came from a return | |
1257 | -- statement (we're allocating the result on the secondary | |
1258 | -- stack). In that case, the object will be moved, so we _do_ | |
1259 | -- want to Adjust. | |
1260 | ||
1261 | if not Aggr_In_Place | |
51245e2d | 1262 | and then not Is_Limited_View (T) |
df3e68b1 HK |
1263 | then |
1264 | Insert_Action (N, | |
fbf5a39b | 1265 | |
533369aa AC |
1266 | -- An unchecked conversion is needed in the classwide case |
1267 | -- because the designated type can be an ancestor of the | |
1268 | -- subtype mark of the allocator. | |
fbf5a39b | 1269 | |
533369aa AC |
1270 | Make_Adjust_Call |
1271 | (Obj_Ref => | |
1272 | Unchecked_Convert_To (T, | |
1273 | Make_Explicit_Dereference (Loc, | |
e4494292 | 1274 | Prefix => New_Occurrence_Of (Temp, Loc))), |
533369aa | 1275 | Typ => T)); |
df3e68b1 | 1276 | end if; |
b254da66 AC |
1277 | |
1278 | -- Generate: | |
1279 | -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access); | |
1280 | ||
2bfa5484 | 1281 | -- Do not generate this call in the following cases: |
c5f5123f | 1282 | |
2bfa5484 HK |
1283 | -- * .NET/JVM - these targets do not support address arithmetic |
1284 | -- and unchecked conversion, key elements of Finalize_Address. | |
c5f5123f | 1285 | |
2bfa5484 HK |
1286 | -- * CodePeer mode - TSS primitive Finalize_Address is not |
1287 | -- created in this mode. | |
b254da66 AC |
1288 | |
1289 | if VM_Target = No_VM | |
1290 | and then not CodePeer_Mode | |
1291 | and then Present (Finalization_Master (PtrT)) | |
f7bb41af AC |
1292 | and then Present (Temp_Decl) |
1293 | and then Nkind (Expression (Temp_Decl)) = N_Allocator | |
b254da66 AC |
1294 | then |
1295 | Insert_Action (N, | |
1296 | Make_Set_Finalize_Address_Call | |
1297 | (Loc => Loc, | |
1298 | Typ => T, | |
1299 | Ptr_Typ => PtrT)); | |
1300 | end if; | |
fbf5a39b AC |
1301 | end if; |
1302 | ||
e4494292 | 1303 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1304 | Analyze_And_Resolve (N, PtrT); |
1305 | ||
685094bf RD |
1306 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
1307 | -- component containing the secondary dispatch table of the interface | |
1308 | -- type. | |
26bff3d9 JM |
1309 | |
1310 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
1311 | Displace_Allocator_Pointer (N); | |
1312 | end if; | |
1313 | ||
fbf5a39b | 1314 | elsif Aggr_In_Place then |
e86a3a7e | 1315 | Temp := Make_Temporary (Loc, 'P', N); |
df3e68b1 | 1316 | Temp_Decl := |
fbf5a39b AC |
1317 | Make_Object_Declaration (Loc, |
1318 | Defining_Identifier => Temp, | |
e4494292 | 1319 | Object_Definition => New_Occurrence_Of (PtrT, Loc), |
df3e68b1 HK |
1320 | Expression => |
1321 | Make_Allocator (Loc, | |
e4494292 | 1322 | Expression => New_Occurrence_Of (Etype (Exp), Loc))); |
fbf5a39b | 1323 | |
fad0600d AC |
1324 | -- Copy the Comes_From_Source flag for the allocator we just built, |
1325 | -- since logically this allocator is a replacement of the original | |
1326 | -- allocator node. This is for proper handling of restriction | |
1327 | -- No_Implicit_Heap_Allocations. | |
1328 | ||
fbf5a39b | 1329 | Set_Comes_From_Source |
df3e68b1 HK |
1330 | (Expression (Temp_Decl), Comes_From_Source (N)); |
1331 | ||
1332 | Set_No_Initialization (Expression (Temp_Decl)); | |
1333 | Insert_Action (N, Temp_Decl); | |
1334 | ||
ca5af305 | 1335 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1336 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fbf5a39b | 1337 | |
d3f70b35 AC |
1338 | -- Attach the object to the associated finalization master. Thisis |
1339 | -- done manually on .NET/JVM since those compilers do no support | |
deb8dacc HK |
1340 | -- pools and cannot benefit from internally generated Allocate and |
1341 | -- Deallocate procedures. | |
1342 | ||
1343 | if VM_Target /= No_VM | |
1344 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1345 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1346 | then |
1347 | Insert_Action (N, | |
243cae0a | 1348 | Make_Attach_Call |
e4494292 | 1349 | (Obj_Ref => New_Occurrence_Of (Temp, Loc), |
243cae0a | 1350 | Ptr_Typ => PtrT)); |
deb8dacc HK |
1351 | end if; |
1352 | ||
e4494292 | 1353 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1354 | Analyze_And_Resolve (N, PtrT); |
1355 | ||
533369aa | 1356 | elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then |
51e4c4b9 AC |
1357 | Install_Null_Excluding_Check (Exp); |
1358 | ||
f02b8bb8 | 1359 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
1360 | and then Nkind (Exp) = N_Allocator |
1361 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
1362 | then | |
0da2c8ac | 1363 | -- Apply constraint to designated subtype indication |
fbf5a39b AC |
1364 | |
1365 | Apply_Constraint_Check (Expression (Exp), | |
f02b8bb8 | 1366 | Designated_Type (DesigT), |
fbf5a39b AC |
1367 | No_Sliding => True); |
1368 | ||
1369 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
1370 | ||
1371 | -- Propagate constraint_error to enclosing allocator | |
1372 | ||
1373 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
1374 | end if; | |
1df4f514 | 1375 | |
fbf5a39b | 1376 | else |
14f0f659 AC |
1377 | Build_Allocate_Deallocate_Proc (N, True); |
1378 | ||
36c73552 AC |
1379 | -- If we have: |
1380 | -- type A is access T1; | |
1381 | -- X : A := new T2'(...); | |
1382 | -- T1 and T2 can be different subtypes, and we might need to check | |
1383 | -- both constraints. First check against the type of the qualified | |
1384 | -- expression. | |
1385 | ||
1386 | Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
fbf5a39b | 1387 | |
d79e621a GD |
1388 | if Do_Range_Check (Exp) then |
1389 | Set_Do_Range_Check (Exp, False); | |
1390 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); | |
1391 | end if; | |
1392 | ||
685094bf RD |
1393 | -- A check is also needed in cases where the designated subtype is |
1394 | -- constrained and differs from the subtype given in the qualified | |
1395 | -- expression. Note that the check on the qualified expression does | |
1396 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 1397 | |
f02b8bb8 | 1398 | if Is_Constrained (DesigT) |
9450205a | 1399 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
1400 | then |
1401 | Apply_Constraint_Check | |
f02b8bb8 | 1402 | (Exp, DesigT, No_Sliding => False); |
d79e621a GD |
1403 | |
1404 | if Do_Range_Check (Exp) then | |
1405 | Set_Do_Range_Check (Exp, False); | |
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, | |
1839 | Attribute_Name => Nam, | |
e4494292 | 1840 | Prefix => New_Occurrence_Of (Arr, Loc), |
fbf5a39b AC |
1841 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); |
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, | |
1883 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), | |
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, | |
e4494292 | 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, | |
2163 | Condition => Test_Empty_Arrays, | |
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, | |
2170 | Condition => Test_Lengths_Correspond, | |
2171 | Then_Statements => New_List ( | |
d766cee3 | 2172 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
2173 | Expression => |
2174 | New_Occurrence_Of (Standard_False, Loc)))), | |
2175 | ||
0da2c8ac | 2176 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 2177 | |
d766cee3 | 2178 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
2179 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
2180 | ||
2181 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 2182 | Set_Is_Inlined (Func_Name); |
70482933 | 2183 | |
685094bf RD |
2184 | -- If the array type is distinct from the type of the arguments, it |
2185 | -- is the full view of a private type. Apply an unchecked conversion | |
2186 | -- to insure that analysis of the call succeeds. | |
70482933 | 2187 | |
0da2c8ac AC |
2188 | declare |
2189 | L, R : Node_Id; | |
2190 | ||
2191 | begin | |
2192 | L := Lhs; | |
2193 | R := Rhs; | |
2194 | ||
2195 | if No (Etype (Lhs)) | |
2196 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
2197 | then | |
2198 | L := OK_Convert_To (Ltyp, Lhs); | |
2199 | end if; | |
2200 | ||
2201 | if No (Etype (Rhs)) | |
2202 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
2203 | then | |
2204 | R := OK_Convert_To (Rtyp, Rhs); | |
2205 | end if; | |
2206 | ||
2207 | Actuals := New_List (L, R); | |
2208 | end; | |
70482933 RK |
2209 | |
2210 | Append_To (Bodies, Func_Body); | |
2211 | ||
2212 | return | |
2213 | Make_Function_Call (Loc, | |
e4494292 | 2214 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
2215 | Parameter_Associations => Actuals); |
2216 | end Expand_Array_Equality; | |
2217 | ||
2218 | ----------------------------- | |
2219 | -- Expand_Boolean_Operator -- | |
2220 | ----------------------------- | |
2221 | ||
685094bf RD |
2222 | -- Note that we first get the actual subtypes of the operands, since we |
2223 | -- always want to deal with types that have bounds. | |
70482933 RK |
2224 | |
2225 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 2226 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
2227 | |
2228 | begin | |
685094bf RD |
2229 | -- Special case of bit packed array where both operands are known to be |
2230 | -- properly aligned. In this case we use an efficient run time routine | |
2231 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
2232 | |
2233 | if Is_Bit_Packed_Array (Typ) | |
2234 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
2235 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
2236 | then | |
70482933 | 2237 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
2238 | return; |
2239 | end if; | |
70482933 | 2240 | |
a9d8907c JM |
2241 | -- For the normal non-packed case, the general expansion is to build |
2242 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
2243 | -- and then inserting it into the tree. The original operator node is | |
2244 | -- then rewritten as a call to this function. We also use this in the | |
2245 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 2246 | |
a9d8907c JM |
2247 | declare |
2248 | Loc : constant Source_Ptr := Sloc (N); | |
2249 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
2250 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
2251 | Func_Body : Node_Id; | |
2252 | Func_Name : Entity_Id; | |
fbf5a39b | 2253 | |
a9d8907c JM |
2254 | begin |
2255 | Convert_To_Actual_Subtype (L); | |
2256 | Convert_To_Actual_Subtype (R); | |
2257 | Ensure_Defined (Etype (L), N); | |
2258 | Ensure_Defined (Etype (R), N); | |
2259 | Apply_Length_Check (R, Etype (L)); | |
2260 | ||
b4592168 GD |
2261 | if Nkind (N) = N_Op_Xor then |
2262 | Silly_Boolean_Array_Xor_Test (N, Etype (L)); | |
2263 | end if; | |
2264 | ||
a9d8907c JM |
2265 | if Nkind (Parent (N)) = N_Assignment_Statement |
2266 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
2267 | then | |
2268 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 2269 | |
a9d8907c JM |
2270 | elsif Nkind (Parent (N)) = N_Op_Not |
2271 | and then Nkind (N) = N_Op_And | |
2272 | and then | |
b4592168 | 2273 | Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
2274 | then |
2275 | return; | |
2276 | else | |
fbf5a39b | 2277 | |
a9d8907c JM |
2278 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
2279 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
2280 | Insert_Action (N, Func_Body); | |
70482933 | 2281 | |
a9d8907c | 2282 | -- Now rewrite the expression with a call |
70482933 | 2283 | |
a9d8907c JM |
2284 | Rewrite (N, |
2285 | Make_Function_Call (Loc, | |
e4494292 | 2286 | Name => New_Occurrence_Of (Func_Name, Loc), |
a9d8907c JM |
2287 | Parameter_Associations => |
2288 | New_List ( | |
2289 | L, | |
2290 | Make_Type_Conversion | |
e4494292 | 2291 | (Loc, New_Occurrence_Of (Etype (L), Loc), R)))); |
70482933 | 2292 | |
a9d8907c JM |
2293 | Analyze_And_Resolve (N, Typ); |
2294 | end if; | |
2295 | end; | |
70482933 RK |
2296 | end Expand_Boolean_Operator; |
2297 | ||
456cbfa5 AC |
2298 | ------------------------------------------------ |
2299 | -- Expand_Compare_Minimize_Eliminate_Overflow -- | |
2300 | ------------------------------------------------ | |
2301 | ||
2302 | procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is | |
2303 | Loc : constant Source_Ptr := Sloc (N); | |
2304 | ||
71fb4dc8 AC |
2305 | Result_Type : constant Entity_Id := Etype (N); |
2306 | -- Capture result type (could be a derived boolean type) | |
2307 | ||
456cbfa5 AC |
2308 | Llo, Lhi : Uint; |
2309 | Rlo, Rhi : Uint; | |
2310 | ||
2311 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
2312 | -- Entity for Long_Long_Integer'Base | |
2313 | ||
15c94a55 | 2314 | Check : constant Overflow_Mode_Type := Overflow_Check_Mode; |
a7f1b24f | 2315 | -- Current overflow checking mode |
456cbfa5 AC |
2316 | |
2317 | procedure Set_True; | |
2318 | procedure Set_False; | |
2319 | -- These procedures rewrite N with an occurrence of Standard_True or | |
2320 | -- Standard_False, and then makes a call to Warn_On_Known_Condition. | |
2321 | ||
2322 | --------------- | |
2323 | -- Set_False -- | |
2324 | --------------- | |
2325 | ||
2326 | procedure Set_False is | |
2327 | begin | |
2328 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
2329 | Warn_On_Known_Condition (N); | |
2330 | end Set_False; | |
2331 | ||
2332 | -------------- | |
2333 | -- Set_True -- | |
2334 | -------------- | |
2335 | ||
2336 | procedure Set_True is | |
2337 | begin | |
2338 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
2339 | Warn_On_Known_Condition (N); | |
2340 | end Set_True; | |
2341 | ||
2342 | -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow | |
2343 | ||
2344 | begin | |
2345 | -- Nothing to do unless we have a comparison operator with operands | |
2346 | -- that are signed integer types, and we are operating in either | |
2347 | -- MINIMIZED or ELIMINATED overflow checking mode. | |
2348 | ||
2349 | if Nkind (N) not in N_Op_Compare | |
2350 | or else Check not in Minimized_Or_Eliminated | |
2351 | or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N))) | |
2352 | then | |
2353 | return; | |
2354 | end if; | |
2355 | ||
2356 | -- OK, this is the case we are interested in. First step is to process | |
2357 | -- our operands using the Minimize_Eliminate circuitry which applies | |
2358 | -- this processing to the two operand subtrees. | |
2359 | ||
a7f1b24f | 2360 | Minimize_Eliminate_Overflows |
c7e152b5 | 2361 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
a7f1b24f | 2362 | Minimize_Eliminate_Overflows |
c7e152b5 | 2363 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
456cbfa5 | 2364 | |
65f7ed64 AC |
2365 | -- See if the range information decides the result of the comparison. |
2366 | -- We can only do this if we in fact have full range information (which | |
2367 | -- won't be the case if either operand is bignum at this stage). | |
456cbfa5 | 2368 | |
65f7ed64 AC |
2369 | if Llo /= No_Uint and then Rlo /= No_Uint then |
2370 | case N_Op_Compare (Nkind (N)) is | |
456cbfa5 AC |
2371 | when N_Op_Eq => |
2372 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
2373 | Set_True; | |
a40ada7e | 2374 | elsif Llo > Rhi or else Lhi < Rlo then |
456cbfa5 AC |
2375 | Set_False; |
2376 | end if; | |
2377 | ||
2378 | when N_Op_Ge => | |
2379 | if Llo >= Rhi then | |
2380 | Set_True; | |
2381 | elsif Lhi < Rlo then | |
2382 | Set_False; | |
2383 | end if; | |
2384 | ||
2385 | when N_Op_Gt => | |
2386 | if Llo > Rhi then | |
2387 | Set_True; | |
2388 | elsif Lhi <= Rlo then | |
2389 | Set_False; | |
2390 | end if; | |
2391 | ||
2392 | when N_Op_Le => | |
2393 | if Llo > Rhi then | |
2394 | Set_False; | |
2395 | elsif Lhi <= Rlo then | |
2396 | Set_True; | |
2397 | end if; | |
2398 | ||
2399 | when N_Op_Lt => | |
2400 | if Llo >= Rhi then | |
456cbfa5 | 2401 | Set_False; |
b6b5cca8 AC |
2402 | elsif Lhi < Rlo then |
2403 | Set_True; | |
456cbfa5 AC |
2404 | end if; |
2405 | ||
2406 | when N_Op_Ne => | |
2407 | if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then | |
456cbfa5 | 2408 | Set_False; |
a40ada7e RD |
2409 | elsif Llo > Rhi or else Lhi < Rlo then |
2410 | Set_True; | |
456cbfa5 | 2411 | end if; |
65f7ed64 | 2412 | end case; |
456cbfa5 | 2413 | |
65f7ed64 | 2414 | -- All done if we did the rewrite |
456cbfa5 | 2415 | |
65f7ed64 AC |
2416 | if Nkind (N) not in N_Op_Compare then |
2417 | return; | |
2418 | end if; | |
456cbfa5 AC |
2419 | end if; |
2420 | ||
2421 | -- Otherwise, time to do the comparison | |
2422 | ||
2423 | declare | |
2424 | Ltype : constant Entity_Id := Etype (Left_Opnd (N)); | |
2425 | Rtype : constant Entity_Id := Etype (Right_Opnd (N)); | |
2426 | ||
2427 | begin | |
2428 | -- If the two operands have the same signed integer type we are | |
2429 | -- all set, nothing more to do. This is the case where either | |
2430 | -- both operands were unchanged, or we rewrote both of them to | |
2431 | -- be Long_Long_Integer. | |
2432 | ||
2433 | -- Note: Entity for the comparison may be wrong, but it's not worth | |
2434 | -- the effort to change it, since the back end does not use it. | |
2435 | ||
2436 | if Is_Signed_Integer_Type (Ltype) | |
2437 | and then Base_Type (Ltype) = Base_Type (Rtype) | |
2438 | then | |
2439 | return; | |
2440 | ||
2441 | -- Here if bignums are involved (can only happen in ELIMINATED mode) | |
2442 | ||
2443 | elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then | |
2444 | declare | |
2445 | Left : Node_Id := Left_Opnd (N); | |
2446 | Right : Node_Id := Right_Opnd (N); | |
2447 | -- Bignum references for left and right operands | |
2448 | ||
2449 | begin | |
2450 | if not Is_RTE (Ltype, RE_Bignum) then | |
2451 | Left := Convert_To_Bignum (Left); | |
2452 | elsif not Is_RTE (Rtype, RE_Bignum) then | |
2453 | Right := Convert_To_Bignum (Right); | |
2454 | end if; | |
2455 | ||
71fb4dc8 | 2456 | -- We rewrite our node with: |
456cbfa5 | 2457 | |
71fb4dc8 AC |
2458 | -- do |
2459 | -- Bnn : Result_Type; | |
2460 | -- declare | |
2461 | -- M : Mark_Id := SS_Mark; | |
2462 | -- begin | |
2463 | -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc) | |
2464 | -- SS_Release (M); | |
2465 | -- end; | |
2466 | -- in | |
2467 | -- Bnn | |
2468 | -- end | |
456cbfa5 AC |
2469 | |
2470 | declare | |
71fb4dc8 | 2471 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
456cbfa5 AC |
2472 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); |
2473 | Ent : RE_Id; | |
2474 | ||
2475 | begin | |
2476 | case N_Op_Compare (Nkind (N)) is | |
2477 | when N_Op_Eq => Ent := RE_Big_EQ; | |
2478 | when N_Op_Ge => Ent := RE_Big_GE; | |
2479 | when N_Op_Gt => Ent := RE_Big_GT; | |
2480 | when N_Op_Le => Ent := RE_Big_LE; | |
2481 | when N_Op_Lt => Ent := RE_Big_LT; | |
2482 | when N_Op_Ne => Ent := RE_Big_NE; | |
2483 | end case; | |
2484 | ||
71fb4dc8 | 2485 | -- Insert assignment to Bnn into the bignum block |
456cbfa5 AC |
2486 | |
2487 | Insert_Before | |
2488 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
2489 | Make_Assignment_Statement (Loc, | |
2490 | Name => New_Occurrence_Of (Bnn, Loc), | |
2491 | Expression => | |
2492 | Make_Function_Call (Loc, | |
2493 | Name => | |
2494 | New_Occurrence_Of (RTE (Ent), Loc), | |
2495 | Parameter_Associations => New_List (Left, Right)))); | |
2496 | ||
71fb4dc8 AC |
2497 | -- Now do the rewrite with expression actions |
2498 | ||
2499 | Rewrite (N, | |
2500 | Make_Expression_With_Actions (Loc, | |
2501 | Actions => New_List ( | |
2502 | Make_Object_Declaration (Loc, | |
2503 | Defining_Identifier => Bnn, | |
2504 | Object_Definition => | |
2505 | New_Occurrence_Of (Result_Type, Loc)), | |
2506 | Blk), | |
2507 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
2508 | Analyze_And_Resolve (N, Result_Type); | |
456cbfa5 AC |
2509 | end; |
2510 | end; | |
2511 | ||
2512 | -- No bignums involved, but types are different, so we must have | |
2513 | -- rewritten one of the operands as a Long_Long_Integer but not | |
2514 | -- the other one. | |
2515 | ||
2516 | -- If left operand is Long_Long_Integer, convert right operand | |
2517 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2518 | ||
2519 | elsif Ltype = LLIB then | |
2520 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
2521 | Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks); | |
2522 | return; | |
2523 | ||
2524 | -- If right operand is Long_Long_Integer, convert left operand | |
2525 | -- and we are done (with a comparison of two Long_Long_Integers). | |
2526 | ||
2527 | -- This is the only remaining possibility | |
2528 | ||
2529 | else pragma Assert (Rtype = LLIB); | |
2530 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
2531 | Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks); | |
2532 | return; | |
2533 | end if; | |
2534 | end; | |
2535 | end Expand_Compare_Minimize_Eliminate_Overflow; | |
2536 | ||
70482933 RK |
2537 | ------------------------------- |
2538 | -- Expand_Composite_Equality -- | |
2539 | ------------------------------- | |
2540 | ||
2541 | -- This function is only called for comparing internal fields of composite | |
2542 | -- types when these fields are themselves composites. This is a special | |
2543 | -- case because it is not possible to respect normal Ada visibility rules. | |
2544 | ||
2545 | function Expand_Composite_Equality | |
2546 | (Nod : Node_Id; | |
2547 | Typ : Entity_Id; | |
2548 | Lhs : Node_Id; | |
2549 | Rhs : Node_Id; | |
2e071734 | 2550 | Bodies : List_Id) return Node_Id |
70482933 RK |
2551 | is |
2552 | Loc : constant Source_Ptr := Sloc (Nod); | |
2553 | Full_Type : Entity_Id; | |
2554 | Prim : Elmt_Id; | |
2555 | Eq_Op : Entity_Id; | |
2556 | ||
7efc3f2d AC |
2557 | function Find_Primitive_Eq return Node_Id; |
2558 | -- AI05-0123: Locate primitive equality for type if it exists, and | |
2559 | -- build the corresponding call. If operation is abstract, replace | |
2560 | -- call with an explicit raise. Return Empty if there is no primitive. | |
2561 | ||
2562 | ----------------------- | |
2563 | -- Find_Primitive_Eq -- | |
2564 | ----------------------- | |
2565 | ||
2566 | function Find_Primitive_Eq return Node_Id is | |
2567 | Prim_E : Elmt_Id; | |
2568 | Prim : Node_Id; | |
2569 | ||
2570 | begin | |
2571 | Prim_E := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2572 | while Present (Prim_E) loop | |
2573 | Prim := Node (Prim_E); | |
2574 | ||
2575 | -- Locate primitive equality with the right signature | |
2576 | ||
2577 | if Chars (Prim) = Name_Op_Eq | |
2578 | and then Etype (First_Formal (Prim)) = | |
39ade2f9 | 2579 | Etype (Next_Formal (First_Formal (Prim))) |
7efc3f2d AC |
2580 | and then Etype (Prim) = Standard_Boolean |
2581 | then | |
2582 | if Is_Abstract_Subprogram (Prim) then | |
2583 | return | |
2584 | Make_Raise_Program_Error (Loc, | |
2585 | Reason => PE_Explicit_Raise); | |
2586 | ||
2587 | else | |
2588 | return | |
2589 | Make_Function_Call (Loc, | |
e4494292 | 2590 | Name => New_Occurrence_Of (Prim, Loc), |
7efc3f2d AC |
2591 | Parameter_Associations => New_List (Lhs, Rhs)); |
2592 | end if; | |
2593 | end if; | |
2594 | ||
2595 | Next_Elmt (Prim_E); | |
2596 | end loop; | |
2597 | ||
2598 | -- If not found, predefined operation will be used | |
2599 | ||
2600 | return Empty; | |
2601 | end Find_Primitive_Eq; | |
2602 | ||
2603 | -- Start of processing for Expand_Composite_Equality | |
2604 | ||
70482933 RK |
2605 | begin |
2606 | if Is_Private_Type (Typ) then | |
2607 | Full_Type := Underlying_Type (Typ); | |
2608 | else | |
2609 | Full_Type := Typ; | |
2610 | end if; | |
2611 | ||
ced8450b ES |
2612 | -- If the private type has no completion the context may be the |
2613 | -- expansion of a composite equality for a composite type with some | |
2614 | -- still incomplete components. The expression will not be analyzed | |
2615 | -- until the enclosing type is completed, at which point this will be | |
2616 | -- properly expanded, unless there is a bona fide completion error. | |
70482933 RK |
2617 | |
2618 | if No (Full_Type) then | |
ced8450b | 2619 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 RK |
2620 | end if; |
2621 | ||
2622 | Full_Type := Base_Type (Full_Type); | |
2623 | ||
da1b76c1 HK |
2624 | -- When the base type itself is private, use the full view to expand |
2625 | -- the composite equality. | |
2626 | ||
2627 | if Is_Private_Type (Full_Type) then | |
2628 | Full_Type := Underlying_Type (Full_Type); | |
2629 | end if; | |
2630 | ||
16788d44 RD |
2631 | -- Case of array types |
2632 | ||
70482933 RK |
2633 | if Is_Array_Type (Full_Type) then |
2634 | ||
2635 | -- If the operand is an elementary type other than a floating-point | |
2636 | -- type, then we can simply use the built-in block bitwise equality, | |
2637 | -- since the predefined equality operators always apply and bitwise | |
2638 | -- equality is fine for all these cases. | |
2639 | ||
2640 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
2641 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
2642 | then | |
39ade2f9 | 2643 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 | 2644 | |
685094bf RD |
2645 | -- For composite component types, and floating-point types, use the |
2646 | -- expansion. This deals with tagged component types (where we use | |
2647 | -- the applicable equality routine) and floating-point, (where we | |
2648 | -- need to worry about negative zeroes), and also the case of any | |
2649 | -- composite type recursively containing such fields. | |
70482933 RK |
2650 | |
2651 | else | |
0da2c8ac | 2652 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
2653 | end if; |
2654 | ||
16788d44 RD |
2655 | -- Case of tagged record types |
2656 | ||
70482933 RK |
2657 | elsif Is_Tagged_Type (Full_Type) then |
2658 | ||
2659 | -- Call the primitive operation "=" of this type | |
2660 | ||
2661 | if Is_Class_Wide_Type (Full_Type) then | |
2662 | Full_Type := Root_Type (Full_Type); | |
2663 | end if; | |
2664 | ||
685094bf RD |
2665 | -- If this is derived from an untagged private type completed with a |
2666 | -- tagged type, it does not have a full view, so we use the primitive | |
2667 | -- operations of the private type. This check should no longer be | |
2668 | -- necessary when these types receive their full views ??? | |
70482933 RK |
2669 | |
2670 | if Is_Private_Type (Typ) | |
2671 | and then not Is_Tagged_Type (Typ) | |
2672 | and then not Is_Controlled (Typ) | |
2673 | and then Is_Derived_Type (Typ) | |
2674 | and then No (Full_View (Typ)) | |
2675 | then | |
2676 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2677 | else | |
2678 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
2679 | end if; | |
2680 | ||
2681 | loop | |
2682 | Eq_Op := Node (Prim); | |
2683 | exit when Chars (Eq_Op) = Name_Op_Eq | |
2684 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
2685 | Etype (Next_Formal (First_Formal (Eq_Op))) |
2686 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
2687 | Next_Elmt (Prim); |
2688 | pragma Assert (Present (Prim)); | |
2689 | end loop; | |
2690 | ||
2691 | Eq_Op := Node (Prim); | |
2692 | ||
2693 | return | |
2694 | Make_Function_Call (Loc, | |
e4494292 | 2695 | Name => New_Occurrence_Of (Eq_Op, Loc), |
70482933 RK |
2696 | Parameter_Associations => |
2697 | New_List | |
2698 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2699 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2700 | ||
16788d44 RD |
2701 | -- Case of untagged record types |
2702 | ||
70482933 | 2703 | elsif Is_Record_Type (Full_Type) then |
fbf5a39b | 2704 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2705 | |
2706 | if Present (Eq_Op) then | |
2707 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2708 | ||
685094bf RD |
2709 | -- Inherited equality from parent type. Convert the actuals to |
2710 | -- match signature of operation. | |
70482933 RK |
2711 | |
2712 | declare | |
fbf5a39b | 2713 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2714 | |
2715 | begin | |
2716 | return | |
2717 | Make_Function_Call (Loc, | |
e4494292 | 2718 | Name => New_Occurrence_Of (Eq_Op, Loc), |
39ade2f9 AC |
2719 | Parameter_Associations => New_List ( |
2720 | OK_Convert_To (T, Lhs), | |
2721 | OK_Convert_To (T, Rhs))); | |
70482933 RK |
2722 | end; |
2723 | ||
2724 | else | |
5d09245e AC |
2725 | -- Comparison between Unchecked_Union components |
2726 | ||
2727 | if Is_Unchecked_Union (Full_Type) then | |
2728 | declare | |
2729 | Lhs_Type : Node_Id := Full_Type; | |
2730 | Rhs_Type : Node_Id := Full_Type; | |
2731 | Lhs_Discr_Val : Node_Id; | |
2732 | Rhs_Discr_Val : Node_Id; | |
2733 | ||
2734 | begin | |
2735 | -- Lhs subtype | |
2736 | ||
2737 | if Nkind (Lhs) = N_Selected_Component then | |
2738 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2739 | end if; | |
2740 | ||
2741 | -- Rhs subtype | |
2742 | ||
2743 | if Nkind (Rhs) = N_Selected_Component then | |
2744 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2745 | end if; | |
2746 | ||
2747 | -- Lhs of the composite equality | |
2748 | ||
2749 | if Is_Constrained (Lhs_Type) then | |
2750 | ||
685094bf | 2751 | -- Since the enclosing record type can never be an |
5d09245e AC |
2752 | -- Unchecked_Union (this code is executed for records |
2753 | -- that do not have variants), we may reference its | |
2754 | -- discriminant(s). | |
2755 | ||
2756 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
2757 | and then Has_Per_Object_Constraint |
2758 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
2759 | then |
2760 | Lhs_Discr_Val := | |
2761 | Make_Selected_Component (Loc, | |
39ade2f9 | 2762 | Prefix => Prefix (Lhs), |
5d09245e | 2763 | Selector_Name => |
39ade2f9 AC |
2764 | New_Copy |
2765 | (Get_Discriminant_Value | |
2766 | (First_Discriminant (Lhs_Type), | |
2767 | Lhs_Type, | |
2768 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
2769 | |
2770 | else | |
39ade2f9 AC |
2771 | Lhs_Discr_Val := |
2772 | New_Copy | |
2773 | (Get_Discriminant_Value | |
2774 | (First_Discriminant (Lhs_Type), | |
2775 | Lhs_Type, | |
2776 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
2777 | |
2778 | end if; | |
2779 | else | |
2780 | -- It is not possible to infer the discriminant since | |
2781 | -- the subtype is not constrained. | |
2782 | ||
8aceda64 | 2783 | return |
5d09245e | 2784 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2785 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2786 | end if; |
2787 | ||
2788 | -- Rhs of the composite equality | |
2789 | ||
2790 | if Is_Constrained (Rhs_Type) then | |
2791 | if Nkind (Rhs) = N_Selected_Component | |
39ade2f9 AC |
2792 | and then Has_Per_Object_Constraint |
2793 | (Entity (Selector_Name (Rhs))) | |
5d09245e AC |
2794 | then |
2795 | Rhs_Discr_Val := | |
2796 | Make_Selected_Component (Loc, | |
39ade2f9 | 2797 | Prefix => Prefix (Rhs), |
5d09245e | 2798 | Selector_Name => |
39ade2f9 AC |
2799 | New_Copy |
2800 | (Get_Discriminant_Value | |
2801 | (First_Discriminant (Rhs_Type), | |
2802 | Rhs_Type, | |
2803 | Stored_Constraint (Rhs_Type)))); | |
5d09245e AC |
2804 | |
2805 | else | |
39ade2f9 AC |
2806 | Rhs_Discr_Val := |
2807 | New_Copy | |
2808 | (Get_Discriminant_Value | |
2809 | (First_Discriminant (Rhs_Type), | |
2810 | Rhs_Type, | |
2811 | Stored_Constraint (Rhs_Type))); | |
5d09245e AC |
2812 | |
2813 | end if; | |
2814 | else | |
8aceda64 | 2815 | return |
5d09245e | 2816 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2817 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2818 | end if; |
2819 | ||
2820 | -- Call the TSS equality function with the inferred | |
2821 | -- discriminant values. | |
2822 | ||
2823 | return | |
2824 | Make_Function_Call (Loc, | |
e4494292 | 2825 | Name => New_Occurrence_Of (Eq_Op, Loc), |
5d09245e AC |
2826 | Parameter_Associations => New_List ( |
2827 | Lhs, | |
2828 | Rhs, | |
2829 | Lhs_Discr_Val, | |
2830 | Rhs_Discr_Val)); | |
2831 | end; | |
d151d6a3 AC |
2832 | |
2833 | else | |
2834 | return | |
2835 | Make_Function_Call (Loc, | |
e4494292 | 2836 | Name => New_Occurrence_Of (Eq_Op, Loc), |
d151d6a3 | 2837 | Parameter_Associations => New_List (Lhs, Rhs)); |
5d09245e | 2838 | end if; |
d151d6a3 | 2839 | end if; |
5d09245e | 2840 | |
3058f181 BD |
2841 | -- Equality composes in Ada 2012 for untagged record types. It also |
2842 | -- composes for bounded strings, because they are part of the | |
2843 | -- predefined environment. We could make it compose for bounded | |
2844 | -- strings by making them tagged, or by making sure all subcomponents | |
2845 | -- are set to the same value, even when not used. Instead, we have | |
2846 | -- this special case in the compiler, because it's more efficient. | |
2847 | ||
2848 | elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then | |
5d09245e | 2849 | |
08daa782 | 2850 | -- If no TSS has been created for the type, check whether there is |
7efc3f2d | 2851 | -- a primitive equality declared for it. |
d151d6a3 AC |
2852 | |
2853 | declare | |
3058f181 | 2854 | Op : constant Node_Id := Find_Primitive_Eq; |
d151d6a3 AC |
2855 | |
2856 | begin | |
a1fc903a AC |
2857 | -- Use user-defined primitive if it exists, otherwise use |
2858 | -- predefined equality. | |
2859 | ||
3058f181 BD |
2860 | if Present (Op) then |
2861 | return Op; | |
7efc3f2d | 2862 | else |
7efc3f2d AC |
2863 | return Make_Op_Eq (Loc, Lhs, Rhs); |
2864 | end if; | |
d151d6a3 AC |
2865 | end; |
2866 | ||
70482933 RK |
2867 | else |
2868 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2869 | end if; | |
2870 | ||
16788d44 | 2871 | -- Non-composite types (always use predefined equality) |
70482933 | 2872 | |
16788d44 | 2873 | else |
70482933 RK |
2874 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
2875 | end if; | |
2876 | end Expand_Composite_Equality; | |
2877 | ||
fdac1f80 AC |
2878 | ------------------------ |
2879 | -- Expand_Concatenate -- | |
2880 | ------------------------ | |
70482933 | 2881 | |
fdac1f80 AC |
2882 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2883 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2884 | |
fdac1f80 AC |
2885 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2886 | -- Result type of concatenation | |
70482933 | 2887 | |
fdac1f80 AC |
2888 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2889 | -- Component type. Elements of this component type can appear as one | |
2890 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2891 | |
ecc4ddde AC |
2892 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2893 | -- Index subtype | |
2894 | ||
2895 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2896 | -- Index type. This is the base type of the index subtype, and is used | |
2897 | -- for all computed bounds (which may be out of range of Istyp in the | |
2898 | -- case of null ranges). | |
70482933 | 2899 | |
46ff89f3 | 2900 | Artyp : Entity_Id; |
fdac1f80 AC |
2901 | -- This is the type we use to do arithmetic to compute the bounds and |
2902 | -- lengths of operands. The choice of this type is a little subtle and | |
2903 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2904 | |
fdac1f80 AC |
2905 | Concatenation_Error : exception; |
2906 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2907 | |
0ac73189 AC |
2908 | Result_May_Be_Null : Boolean := True; |
2909 | -- Reset to False if at least one operand is encountered which is known | |
2910 | -- at compile time to be non-null. Used for handling the special case | |
2911 | -- of setting the high bound to the last operand high bound for a null | |
2912 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2913 | ||
df46b832 | 2914 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2915 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2916 | |
2917 | NN : Nat := 0; | |
a29262fd AC |
2918 | -- Number of operands excluding any known to be null, except that the |
2919 | -- last operand is always retained, in case it provides the bounds for | |
2920 | -- a null result. | |
2921 | ||
2922 | Opnd : Node_Id; | |
2923 | -- Current operand being processed in the loop through operands. After | |
2924 | -- this loop is complete, always contains the last operand (which is not | |
2925 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2926 | |
2927 | -- Arrays describing the operands, only the first NN entries of each | |
2928 | -- array are set (NN < N when we exclude known null operands). | |
2929 | ||
2930 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2931 | -- True if length of corresponding operand known at compile time | |
2932 | ||
2933 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2934 | -- Set to the corresponding entry in the Opnds list (but note that null |
2935 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2936 | |
2937 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2938 | -- Set to length of operand. Entries in this array are set only if the |
2939 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2940 | |
0ac73189 AC |
2941 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2942 | -- Set to lower bound of operand. Either an integer literal in the case | |
2943 | -- where the bound is known at compile time, else actual lower bound. | |
2944 | -- The operand low bound is of type Ityp. | |
2945 | ||
df46b832 AC |
2946 | Var_Length : array (1 .. N) of Entity_Id; |
2947 | -- Set to an entity of type Natural that contains the length of an | |
2948 | -- operand whose length is not known at compile time. Entries in this | |
2949 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2950 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2951 | |
2952 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2953 | -- The J'th entry in an expression node that represents the total length |
2954 | -- of operands 1 through J. It is either an integer literal node, or a | |
2955 | -- reference to a constant entity with the right value, so it is fine | |
2956 | -- to just do a Copy_Node to get an appropriate copy. The extra zero'th | |
46ff89f3 | 2957 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 AC |
2958 | |
2959 | Low_Bound : Node_Id; | |
0ac73189 AC |
2960 | -- A tree node representing the low bound of the result (of type Ityp). |
2961 | -- This is either an integer literal node, or an identifier reference to | |
2962 | -- a constant entity initialized to the appropriate value. | |
2963 | ||
88a27b18 AC |
2964 | Last_Opnd_Low_Bound : Node_Id; |
2965 | -- A tree node representing the low bound of the last operand. This | |
2966 | -- need only be set if the result could be null. It is used for the | |
2967 | -- special case of setting the right low bound for a null result. | |
2968 | -- This is of type Ityp. | |
2969 | ||
a29262fd AC |
2970 | Last_Opnd_High_Bound : Node_Id; |
2971 | -- A tree node representing the high bound of the last operand. This | |
2972 | -- need only be set if the result could be null. It is used for the | |
2973 | -- special case of setting the right high bound for a null result. | |
2974 | -- This is of type Ityp. | |
2975 | ||
0ac73189 AC |
2976 | High_Bound : Node_Id; |
2977 | -- A tree node representing the high bound of the result (of type Ityp) | |
df46b832 AC |
2978 | |
2979 | Result : Node_Id; | |
0ac73189 | 2980 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2981 | |
d0f8d157 | 2982 | Actions : constant List_Id := New_List; |
4c9fe6c7 | 2983 | -- Collect actions to be inserted |
d0f8d157 | 2984 | |
fa969310 | 2985 | Known_Non_Null_Operand_Seen : Boolean; |
308e6f3a | 2986 | -- Set True during generation of the assignments of operands into |
fa969310 AC |
2987 | -- result once an operand known to be non-null has been seen. |
2988 | ||
2989 | function Make_Artyp_Literal (Val : Nat) return Node_Id; | |
2990 | -- This function makes an N_Integer_Literal node that is returned in | |
2991 | -- analyzed form with the type set to Artyp. Importantly this literal | |
2992 | -- is not flagged as static, so that if we do computations with it that | |
2993 | -- result in statically detected out of range conditions, we will not | |
2994 | -- generate error messages but instead warning messages. | |
2995 | ||
46ff89f3 | 2996 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 2997 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
2998 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
2999 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
3000 | |
3001 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 3002 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 3003 | |
fa969310 AC |
3004 | ------------------------ |
3005 | -- Make_Artyp_Literal -- | |
3006 | ------------------------ | |
3007 | ||
3008 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
3009 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
3010 | begin | |
3011 | Set_Etype (Result, Artyp); | |
3012 | Set_Analyzed (Result, True); | |
3013 | Set_Is_Static_Expression (Result, False); | |
3014 | return Result; | |
3015 | end Make_Artyp_Literal; | |
76c597a1 | 3016 | |
fdac1f80 | 3017 | -------------- |
46ff89f3 | 3018 | -- To_Artyp -- |
fdac1f80 AC |
3019 | -------------- |
3020 | ||
46ff89f3 | 3021 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 3022 | begin |
46ff89f3 | 3023 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
3024 | return X; |
3025 | ||
3026 | elsif Is_Enumeration_Type (Ityp) then | |
3027 | return | |
3028 | Make_Attribute_Reference (Loc, | |
3029 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
3030 | Attribute_Name => Name_Pos, | |
3031 | Expressions => New_List (X)); | |
3032 | ||
3033 | else | |
46ff89f3 | 3034 | return Convert_To (Artyp, X); |
fdac1f80 | 3035 | end if; |
46ff89f3 | 3036 | end To_Artyp; |
fdac1f80 AC |
3037 | |
3038 | ------------- | |
3039 | -- To_Ityp -- | |
3040 | ------------- | |
3041 | ||
3042 | function To_Ityp (X : Node_Id) return Node_Id is | |
3043 | begin | |
2fc05e3d | 3044 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
3045 | return |
3046 | Make_Attribute_Reference (Loc, | |
3047 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
3048 | Attribute_Name => Name_Val, | |
3049 | Expressions => New_List (X)); | |
3050 | ||
3051 | -- Case where we will do a type conversion | |
3052 | ||
3053 | else | |
76c597a1 AC |
3054 | if Ityp = Base_Type (Artyp) then |
3055 | return X; | |
fdac1f80 | 3056 | else |
76c597a1 | 3057 | return Convert_To (Ityp, X); |
fdac1f80 AC |
3058 | end if; |
3059 | end if; | |
3060 | end To_Ityp; | |
3061 | ||
3062 | -- Local Declarations | |
3063 | ||
00ba7be8 AC |
3064 | Lib_Level_Target : constant Boolean := |
3065 | Nkind (Parent (Cnode)) = N_Object_Declaration | |
3066 | and then | |
3067 | Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode))); | |
3068 | ||
3069 | -- If the concatenation declares a library level entity, we call the | |
3070 | -- built-in concatenation routines to prevent code bloat, regardless | |
3071 | -- of optimization level. This is space-efficient, and prevent linking | |
3072 | -- problems when units are compiled with different optimizations. | |
3073 | ||
0ac73189 AC |
3074 | Opnd_Typ : Entity_Id; |
3075 | Ent : Entity_Id; | |
3076 | Len : Uint; | |
3077 | J : Nat; | |
3078 | Clen : Node_Id; | |
3079 | Set : Boolean; | |
70482933 | 3080 | |
f46faa08 AC |
3081 | -- Start of processing for Expand_Concatenate |
3082 | ||
70482933 | 3083 | begin |
fdac1f80 AC |
3084 | -- Choose an appropriate computational type |
3085 | ||
3086 | -- We will be doing calculations of lengths and bounds in this routine | |
3087 | -- and computing one from the other in some cases, e.g. getting the high | |
3088 | -- bound by adding the length-1 to the low bound. | |
3089 | ||
3090 | -- We can't just use the index type, or even its base type for this | |
3091 | -- purpose for two reasons. First it might be an enumeration type which | |
308e6f3a RW |
3092 | -- is not suitable for computations of any kind, and second it may |
3093 | -- simply not have enough range. For example if the index type is | |
3094 | -- -128..+127 then lengths can be up to 256, which is out of range of | |
3095 | -- the type. | |
fdac1f80 AC |
3096 | |
3097 | -- For enumeration types, we can simply use Standard_Integer, this is | |
3098 | -- sufficient since the actual number of enumeration literals cannot | |
3099 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 3100 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
3101 | |
3102 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 3103 | Artyp := Standard_Integer; |
fdac1f80 | 3104 | |
59262ebb AC |
3105 | -- If index type is Positive, we use the standard unsigned type, to give |
3106 | -- more room on the top of the range, obviating the need for an overflow | |
3107 | -- check when creating the upper bound. This is needed to avoid junk | |
3108 | -- overflow checks in the common case of String types. | |
3109 | ||
3110 | -- ??? Disabled for now | |
3111 | ||
3112 | -- elsif Istyp = Standard_Positive then | |
3113 | -- Artyp := Standard_Unsigned; | |
3114 | ||
2fc05e3d AC |
3115 | -- For modular types, we use a 32-bit modular type for types whose size |
3116 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
3117 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 3118 | |
2fc05e3d | 3119 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 3120 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 3121 | Artyp := Standard_Unsigned; |
ecc4ddde | 3122 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 3123 | Artyp := Ityp; |
fdac1f80 | 3124 | else |
46ff89f3 | 3125 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
3126 | end if; |
3127 | ||
2fc05e3d | 3128 | -- Similar treatment for signed types |
fdac1f80 AC |
3129 | |
3130 | else | |
ecc4ddde | 3131 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 3132 | Artyp := Standard_Integer; |
ecc4ddde | 3133 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 3134 | Artyp := Ityp; |
fdac1f80 | 3135 | else |
46ff89f3 | 3136 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
3137 | end if; |
3138 | end if; | |
3139 | ||
fa969310 AC |
3140 | -- Supply dummy entry at start of length array |
3141 | ||
3142 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
3143 | ||
fdac1f80 | 3144 | -- Go through operands setting up the above arrays |
70482933 | 3145 | |
df46b832 AC |
3146 | J := 1; |
3147 | while J <= N loop | |
3148 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 3149 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
3150 | |
3151 | -- The parent got messed up when we put the operands in a list, | |
d347f572 AC |
3152 | -- so now put back the proper parent for the saved operand, that |
3153 | -- is to say the concatenation node, to make sure that each operand | |
3154 | -- is seen as a subexpression, e.g. if actions must be inserted. | |
fdac1f80 | 3155 | |
d347f572 | 3156 | Set_Parent (Opnd, Cnode); |
fdac1f80 AC |
3157 | |
3158 | -- Set will be True when we have setup one entry in the array | |
3159 | ||
df46b832 AC |
3160 | Set := False; |
3161 | ||
fdac1f80 | 3162 | -- Singleton element (or character literal) case |
df46b832 | 3163 | |
0ac73189 | 3164 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
3165 | NN := NN + 1; |
3166 | Operands (NN) := Opnd; | |
3167 | Is_Fixed_Length (NN) := True; | |
3168 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 3169 | Result_May_Be_Null := False; |
fdac1f80 | 3170 | |
a29262fd AC |
3171 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
3172 | -- since we know that the result cannot be null). | |
fdac1f80 | 3173 | |
0ac73189 AC |
3174 | Opnd_Low_Bound (NN) := |
3175 | Make_Attribute_Reference (Loc, | |
e4494292 | 3176 | Prefix => New_Occurrence_Of (Istyp, Loc), |
0ac73189 AC |
3177 | Attribute_Name => Name_First); |
3178 | ||
df46b832 AC |
3179 | Set := True; |
3180 | ||
fdac1f80 | 3181 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
3182 | |
3183 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 3184 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 3185 | |
a29262fd AC |
3186 | if Len /= 0 then |
3187 | Result_May_Be_Null := False; | |
3188 | end if; | |
3189 | ||
88a27b18 | 3190 | -- Capture last operand low and high bound if result could be null |
a29262fd AC |
3191 | |
3192 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3193 | Last_Opnd_Low_Bound := |
3194 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3195 | ||
a29262fd | 3196 | Last_Opnd_High_Bound := |
88a27b18 | 3197 | Make_Op_Subtract (Loc, |
a29262fd AC |
3198 | Left_Opnd => |
3199 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 3200 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
3201 | end if; |
3202 | ||
3203 | -- Skip null string literal | |
fdac1f80 | 3204 | |
0ac73189 | 3205 | if J < N and then Len = 0 then |
df46b832 AC |
3206 | goto Continue; |
3207 | end if; | |
3208 | ||
3209 | NN := NN + 1; | |
3210 | Operands (NN) := Opnd; | |
3211 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
3212 | |
3213 | -- Set length and bounds | |
3214 | ||
df46b832 | 3215 | Fixed_Length (NN) := Len; |
0ac73189 AC |
3216 | |
3217 | Opnd_Low_Bound (NN) := | |
3218 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
3219 | ||
df46b832 AC |
3220 | Set := True; |
3221 | ||
3222 | -- All other cases | |
3223 | ||
3224 | else | |
3225 | -- Check constrained case with known bounds | |
3226 | ||
0ac73189 | 3227 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 3228 | declare |
df46b832 AC |
3229 | Index : constant Node_Id := First_Index (Opnd_Typ); |
3230 | Indx_Typ : constant Entity_Id := Etype (Index); | |
3231 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
3232 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
3233 | ||
3234 | begin | |
fdac1f80 AC |
3235 | -- Fixed length constrained array type with known at compile |
3236 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
3237 | |
3238 | if Compile_Time_Known_Value (Lo) | |
3239 | and then | |
3240 | Compile_Time_Known_Value (Hi) | |
3241 | then | |
3242 | declare | |
3243 | Loval : constant Uint := Expr_Value (Lo); | |
3244 | Hival : constant Uint := Expr_Value (Hi); | |
3245 | Len : constant Uint := | |
3246 | UI_Max (Hival - Loval + 1, Uint_0); | |
3247 | ||
3248 | begin | |
0ac73189 AC |
3249 | if Len > 0 then |
3250 | Result_May_Be_Null := False; | |
df46b832 | 3251 | end if; |
0ac73189 | 3252 | |
88a27b18 | 3253 | -- Capture last operand bounds if result could be null |
a29262fd AC |
3254 | |
3255 | if J = N and then Result_May_Be_Null then | |
88a27b18 AC |
3256 | Last_Opnd_Low_Bound := |
3257 | Convert_To (Ityp, | |
3258 | Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
3259 | ||
a29262fd AC |
3260 | Last_Opnd_High_Bound := |
3261 | Convert_To (Ityp, | |
39ade2f9 | 3262 | Make_Integer_Literal (Loc, Expr_Value (Hi))); |
a29262fd AC |
3263 | end if; |
3264 | ||
3265 | -- Exclude null length case unless last operand | |
0ac73189 | 3266 | |
a29262fd | 3267 | if J < N and then Len = 0 then |
0ac73189 AC |
3268 | goto Continue; |
3269 | end if; | |
3270 | ||
3271 | NN := NN + 1; | |
3272 | Operands (NN) := Opnd; | |
3273 | Is_Fixed_Length (NN) := True; | |
3274 | Fixed_Length (NN) := Len; | |
3275 | ||
39ade2f9 AC |
3276 | Opnd_Low_Bound (NN) := |
3277 | To_Ityp | |
3278 | (Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
0ac73189 | 3279 | Set := True; |
df46b832 AC |
3280 | end; |
3281 | end if; | |
3282 | end; | |
3283 | end if; | |
3284 | ||
0ac73189 AC |
3285 | -- All cases where the length is not known at compile time, or the |
3286 | -- special case of an operand which is known to be null but has a | |
3287 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
3288 | |
3289 | if not Set then | |
3290 | NN := NN + 1; | |
0ac73189 AC |
3291 | |
3292 | -- Capture operand bounds | |
3293 | ||
3294 | Opnd_Low_Bound (NN) := | |
3295 | Make_Attribute_Reference (Loc, | |
3296 | Prefix => | |
3297 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3298 | Attribute_Name => Name_First); | |
3299 | ||
88a27b18 AC |
3300 | -- Capture last operand bounds if result could be null |
3301 | ||
a29262fd | 3302 | if J = N and Result_May_Be_Null then |
88a27b18 AC |
3303 | Last_Opnd_Low_Bound := |
3304 | Convert_To (Ityp, | |
3305 | Make_Attribute_Reference (Loc, | |
3306 | Prefix => | |
3307 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3308 | Attribute_Name => Name_First)); | |
3309 | ||
a29262fd AC |
3310 | Last_Opnd_High_Bound := |
3311 | Convert_To (Ityp, | |
3312 | Make_Attribute_Reference (Loc, | |
3313 | Prefix => | |
3314 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
3315 | Attribute_Name => Name_Last)); | |
3316 | end if; | |
0ac73189 AC |
3317 | |
3318 | -- Capture length of operand in entity | |
3319 | ||
df46b832 AC |
3320 | Operands (NN) := Opnd; |
3321 | Is_Fixed_Length (NN) := False; | |
3322 | ||
191fcb3a | 3323 | Var_Length (NN) := Make_Temporary (Loc, 'L'); |
df46b832 | 3324 | |
d0f8d157 | 3325 | Append_To (Actions, |
df46b832 AC |
3326 | Make_Object_Declaration (Loc, |
3327 | Defining_Identifier => Var_Length (NN), | |
3328 | Constant_Present => True, | |
39ade2f9 | 3329 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3330 | Expression => |
3331 | Make_Attribute_Reference (Loc, | |
3332 | Prefix => | |
3333 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
d0f8d157 | 3334 | Attribute_Name => Name_Length))); |
df46b832 AC |
3335 | end if; |
3336 | end if; | |
3337 | ||
3338 | -- Set next entry in aggregate length array | |
3339 | ||
3340 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 3341 | -- or a reference to the saved length for variable length. |
df46b832 AC |
3342 | |
3343 | if NN = 1 then | |
3344 | if Is_Fixed_Length (1) then | |
39ade2f9 | 3345 | Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1)); |
df46b832 | 3346 | else |
e4494292 | 3347 | Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc); |
df46b832 AC |
3348 | end if; |
3349 | ||
3350 | -- If entry is fixed length and only fixed lengths so far, make | |
3351 | -- appropriate new integer literal adding new length. | |
3352 | ||
3353 | elsif Is_Fixed_Length (NN) | |
3354 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
3355 | then | |
3356 | Aggr_Length (NN) := | |
3357 | Make_Integer_Literal (Loc, | |
3358 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
3359 | ||
d0f8d157 AC |
3360 | -- All other cases, construct an addition node for the length and |
3361 | -- create an entity initialized to this length. | |
df46b832 AC |
3362 | |
3363 | else | |
191fcb3a | 3364 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 AC |
3365 | |
3366 | if Is_Fixed_Length (NN) then | |
3367 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
3368 | else | |
e4494292 | 3369 | Clen := New_Occurrence_Of (Var_Length (NN), Loc); |
df46b832 AC |
3370 | end if; |
3371 | ||
d0f8d157 | 3372 | Append_To (Actions, |
df46b832 AC |
3373 | Make_Object_Declaration (Loc, |
3374 | Defining_Identifier => Ent, | |
3375 | Constant_Present => True, | |
39ade2f9 | 3376 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
3377 | Expression => |
3378 | Make_Op_Add (Loc, | |
3379 | Left_Opnd => New_Copy (Aggr_Length (NN - 1)), | |
d0f8d157 | 3380 | Right_Opnd => Clen))); |
df46b832 | 3381 | |
76c597a1 | 3382 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
3383 | end if; |
3384 | ||
3385 | <<Continue>> | |
3386 | J := J + 1; | |
3387 | end loop; | |
3388 | ||
a29262fd | 3389 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
3390 | |
3391 | if NN = 0 then | |
a29262fd | 3392 | Result := Opnd; |
df46b832 AC |
3393 | goto Done; |
3394 | end if; | |
3395 | ||
3396 | -- If we have only one non-null operand, return it and we are done. | |
3397 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
3398 | -- the sole operand is of the element type, in which case it must be |
3399 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
3400 | -- through the normal general circuit. |
3401 | ||
533369aa | 3402 | if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then |
df46b832 AC |
3403 | Result := Operands (1); |
3404 | goto Done; | |
3405 | end if; | |
3406 | ||
3407 | -- Cases where we have a real concatenation | |
3408 | ||
fdac1f80 AC |
3409 | -- Next step is to find the low bound for the result array that we |
3410 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
3411 | ||
3412 | -- If the ultimate ancestor of the index subtype is a constrained array | |
3413 | -- definition, then the lower bound is that of the index subtype as | |
3414 | -- specified by (RM 4.5.3(6)). | |
3415 | ||
3416 | -- The right test here is to go to the root type, and then the ultimate | |
3417 | -- ancestor is the first subtype of this root type. | |
3418 | ||
3419 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 3420 | Low_Bound := |
fdac1f80 AC |
3421 | Make_Attribute_Reference (Loc, |
3422 | Prefix => | |
3423 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 3424 | Attribute_Name => Name_First); |
df46b832 AC |
3425 | |
3426 | -- If the first operand in the list has known length we know that | |
3427 | -- the lower bound of the result is the lower bound of this operand. | |
3428 | ||
fdac1f80 | 3429 | elsif Is_Fixed_Length (1) then |
0ac73189 | 3430 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
3431 | |
3432 | -- OK, we don't know the lower bound, we have to build a horrible | |
9b16cb57 | 3433 | -- if expression node of the form |
df46b832 AC |
3434 | |
3435 | -- if Cond1'Length /= 0 then | |
0ac73189 | 3436 | -- Opnd1 low bound |
df46b832 AC |
3437 | -- else |
3438 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 3439 | -- Opnd2 low bound |
df46b832 AC |
3440 | -- else |
3441 | -- ... | |
3442 | ||
3443 | -- The nesting ends either when we hit an operand whose length is known | |
3444 | -- at compile time, or on reaching the last operand, whose low bound we | |
3445 | -- take unconditionally whether or not it is null. It's easiest to do | |
3446 | -- this with a recursive procedure: | |
3447 | ||
3448 | else | |
3449 | declare | |
3450 | function Get_Known_Bound (J : Nat) return Node_Id; | |
3451 | -- Returns the lower bound determined by operands J .. NN | |
3452 | ||
3453 | --------------------- | |
3454 | -- Get_Known_Bound -- | |
3455 | --------------------- | |
3456 | ||
3457 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 3458 | begin |
0ac73189 AC |
3459 | if Is_Fixed_Length (J) or else J = NN then |
3460 | return New_Copy (Opnd_Low_Bound (J)); | |
70482933 RK |
3461 | |
3462 | else | |
df46b832 | 3463 | return |
9b16cb57 | 3464 | Make_If_Expression (Loc, |
df46b832 AC |
3465 | Expressions => New_List ( |
3466 | ||
3467 | Make_Op_Ne (Loc, | |
e4494292 RD |
3468 | Left_Opnd => |
3469 | New_Occurrence_Of (Var_Length (J), Loc), | |
3470 | Right_Opnd => | |
3471 | Make_Integer_Literal (Loc, 0)), | |
df46b832 | 3472 | |
0ac73189 | 3473 | New_Copy (Opnd_Low_Bound (J)), |
df46b832 | 3474 | Get_Known_Bound (J + 1))); |
70482933 | 3475 | end if; |
df46b832 | 3476 | end Get_Known_Bound; |
70482933 | 3477 | |
df46b832 | 3478 | begin |
191fcb3a | 3479 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 | 3480 | |
d0f8d157 | 3481 | Append_To (Actions, |
df46b832 AC |
3482 | Make_Object_Declaration (Loc, |
3483 | Defining_Identifier => Ent, | |
3484 | Constant_Present => True, | |
0ac73189 | 3485 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
d0f8d157 | 3486 | Expression => Get_Known_Bound (1))); |
df46b832 | 3487 | |
e4494292 | 3488 | Low_Bound := New_Occurrence_Of (Ent, Loc); |
df46b832 AC |
3489 | end; |
3490 | end if; | |
70482933 | 3491 | |
76c597a1 AC |
3492 | -- Now we can safely compute the upper bound, normally |
3493 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
3494 | |
3495 | High_Bound := | |
3496 | To_Ityp ( | |
3497 | Make_Op_Add (Loc, | |
46ff89f3 | 3498 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
0ac73189 AC |
3499 | Right_Opnd => |
3500 | Make_Op_Subtract (Loc, | |
3501 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 3502 | Right_Opnd => Make_Artyp_Literal (1)))); |
0ac73189 | 3503 | |
59262ebb | 3504 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
3505 | -- very weird cases, so in the general case we need an overflow check on |
3506 | -- the high bound. We can avoid this for the common case of string types | |
3507 | -- and other types whose index is Positive, since we chose a wider range | |
3508 | -- for the arithmetic type. | |
76c597a1 | 3509 | |
59262ebb AC |
3510 | if Istyp /= Standard_Positive then |
3511 | Activate_Overflow_Check (High_Bound); | |
3512 | end if; | |
76c597a1 AC |
3513 | |
3514 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
3515 | -- case the bounds come from the last operand (so that we get the proper |
3516 | -- bounds if the last operand is super-flat). | |
3517 | ||
0ac73189 | 3518 | if Result_May_Be_Null then |
88a27b18 | 3519 | Low_Bound := |
9b16cb57 | 3520 | Make_If_Expression (Loc, |
88a27b18 AC |
3521 | Expressions => New_List ( |
3522 | Make_Op_Eq (Loc, | |
3523 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
3524 | Right_Opnd => Make_Artyp_Literal (0)), | |
3525 | Last_Opnd_Low_Bound, | |
3526 | Low_Bound)); | |
3527 | ||
0ac73189 | 3528 | High_Bound := |
9b16cb57 | 3529 | Make_If_Expression (Loc, |
0ac73189 AC |
3530 | Expressions => New_List ( |
3531 | Make_Op_Eq (Loc, | |
3532 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 3533 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 3534 | Last_Opnd_High_Bound, |
0ac73189 AC |
3535 | High_Bound)); |
3536 | end if; | |
3537 | ||
d0f8d157 AC |
3538 | -- Here is where we insert the saved up actions |
3539 | ||
3540 | Insert_Actions (Cnode, Actions, Suppress => All_Checks); | |
3541 | ||
602a7ec0 AC |
3542 | -- Now we construct an array object with appropriate bounds. We mark |
3543 | -- the target as internal to prevent useless initialization when | |
e526d0c7 AC |
3544 | -- Initialize_Scalars is enabled. Also since this is the actual result |
3545 | -- entity, we make sure we have debug information for the result. | |
70482933 | 3546 | |
191fcb3a | 3547 | Ent := Make_Temporary (Loc, 'S'); |
008f6fd3 | 3548 | Set_Is_Internal (Ent); |
e526d0c7 | 3549 | Set_Needs_Debug_Info (Ent); |
70482933 | 3550 | |
76c597a1 | 3551 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
3552 | -- to abort, we want a warning and a runtime constraint error. Note that |
3553 | -- we have arranged that the result will not be treated as a static | |
3554 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 3555 | |
df46b832 AC |
3556 | Insert_Action (Cnode, |
3557 | Make_Object_Declaration (Loc, | |
3558 | Defining_Identifier => Ent, | |
df46b832 AC |
3559 | Object_Definition => |
3560 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 3561 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
3562 | Constraint => |
3563 | Make_Index_Or_Discriminant_Constraint (Loc, | |
3564 | Constraints => New_List ( | |
3565 | Make_Range (Loc, | |
0ac73189 AC |
3566 | Low_Bound => Low_Bound, |
3567 | High_Bound => High_Bound))))), | |
df46b832 AC |
3568 | Suppress => All_Checks); |
3569 | ||
d1f453b7 RD |
3570 | -- If the result of the concatenation appears as the initializing |
3571 | -- expression of an object declaration, we can just rename the | |
3572 | -- result, rather than copying it. | |
3573 | ||
3574 | Set_OK_To_Rename (Ent); | |
3575 | ||
76c597a1 AC |
3576 | -- Catch the static out of range case now |
3577 | ||
3578 | if Raises_Constraint_Error (High_Bound) then | |
3579 | raise Concatenation_Error; | |
3580 | end if; | |
3581 | ||
df46b832 AC |
3582 | -- Now we will generate the assignments to do the actual concatenation |
3583 | ||
bded454f RD |
3584 | -- There is one case in which we will not do this, namely when all the |
3585 | -- following conditions are met: | |
3586 | ||
3587 | -- The result type is Standard.String | |
3588 | ||
3589 | -- There are nine or fewer retained (non-null) operands | |
3590 | ||
ffec8e81 | 3591 | -- The optimization level is -O0 |
bded454f RD |
3592 | |
3593 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
3594 | -- available in the run time. | |
3595 | ||
3596 | -- The debug flag gnatd.c is not set | |
3597 | ||
3598 | -- If all these conditions are met then we generate a call to the | |
3599 | -- relevant concatenation routine. The purpose of this is to avoid | |
3600 | -- undesirable code bloat at -O0. | |
3601 | ||
3602 | if Atyp = Standard_String | |
3603 | and then NN in 2 .. 9 | |
00ba7be8 AC |
3604 | and then (Lib_Level_Target |
3605 | or else | |
3606 | ((Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC) | |
3607 | and then not Debug_Flag_Dot_C)) | |
bded454f RD |
3608 | then |
3609 | declare | |
3610 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
3611 | (RE_Str_Concat_2, | |
3612 | RE_Str_Concat_3, | |
3613 | RE_Str_Concat_4, | |
3614 | RE_Str_Concat_5, | |
3615 | RE_Str_Concat_6, | |
3616 | RE_Str_Concat_7, | |
3617 | RE_Str_Concat_8, | |
3618 | RE_Str_Concat_9); | |
3619 | ||
3620 | begin | |
3621 | if RTE_Available (RR (NN)) then | |
3622 | declare | |
3623 | Opnds : constant List_Id := | |
3624 | New_List (New_Occurrence_Of (Ent, Loc)); | |
3625 | ||
3626 | begin | |
3627 | for J in 1 .. NN loop | |
3628 | if Is_List_Member (Operands (J)) then | |
3629 | Remove (Operands (J)); | |
3630 | end if; | |
3631 | ||
3632 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
3633 | Append_To (Opnds, | |
3634 | Make_Aggregate (Loc, | |
3635 | Component_Associations => New_List ( | |
3636 | Make_Component_Association (Loc, | |
3637 | Choices => New_List ( | |
3638 | Make_Integer_Literal (Loc, 1)), | |
3639 | Expression => Operands (J))))); | |
3640 | ||
3641 | else | |
3642 | Append_To (Opnds, Operands (J)); | |
3643 | end if; | |
3644 | end loop; | |
3645 | ||
3646 | Insert_Action (Cnode, | |
3647 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 3648 | Name => New_Occurrence_Of (RTE (RR (NN)), Loc), |
bded454f RD |
3649 | Parameter_Associations => Opnds)); |
3650 | ||
e4494292 | 3651 | Result := New_Occurrence_Of (Ent, Loc); |
bded454f RD |
3652 | goto Done; |
3653 | end; | |
3654 | end if; | |
3655 | end; | |
3656 | end if; | |
3657 | ||
3658 | -- Not special case so generate the assignments | |
3659 | ||
76c597a1 AC |
3660 | Known_Non_Null_Operand_Seen := False; |
3661 | ||
df46b832 AC |
3662 | for J in 1 .. NN loop |
3663 | declare | |
3664 | Lo : constant Node_Id := | |
3665 | Make_Op_Add (Loc, | |
46ff89f3 | 3666 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3667 | Right_Opnd => Aggr_Length (J - 1)); |
3668 | ||
3669 | Hi : constant Node_Id := | |
3670 | Make_Op_Add (Loc, | |
46ff89f3 | 3671 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3672 | Right_Opnd => |
3673 | Make_Op_Subtract (Loc, | |
3674 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 3675 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 3676 | |
df46b832 | 3677 | begin |
fdac1f80 AC |
3678 | -- Singleton case, simple assignment |
3679 | ||
3680 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 3681 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
3682 | Insert_Action (Cnode, |
3683 | Make_Assignment_Statement (Loc, | |
3684 | Name => | |
3685 | Make_Indexed_Component (Loc, | |
3686 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 3687 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
3688 | Expression => Operands (J)), |
3689 | Suppress => All_Checks); | |
70482933 | 3690 | |
76c597a1 AC |
3691 | -- Array case, slice assignment, skipped when argument is fixed |
3692 | -- length and known to be null. | |
fdac1f80 | 3693 | |
76c597a1 AC |
3694 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
3695 | declare | |
3696 | Assign : Node_Id := | |
3697 | Make_Assignment_Statement (Loc, | |
3698 | Name => | |
3699 | Make_Slice (Loc, | |
3700 | Prefix => | |
3701 | New_Occurrence_Of (Ent, Loc), | |
3702 | Discrete_Range => | |
3703 | Make_Range (Loc, | |
3704 | Low_Bound => To_Ityp (Lo), | |
3705 | High_Bound => To_Ityp (Hi))), | |
3706 | Expression => Operands (J)); | |
3707 | begin | |
3708 | if Is_Fixed_Length (J) then | |
3709 | Known_Non_Null_Operand_Seen := True; | |
3710 | ||
3711 | elsif not Known_Non_Null_Operand_Seen then | |
3712 | ||
3713 | -- Here if operand length is not statically known and no | |
3714 | -- operand known to be non-null has been processed yet. | |
3715 | -- If operand length is 0, we do not need to perform the | |
3716 | -- assignment, and we must avoid the evaluation of the | |
3717 | -- high bound of the slice, since it may underflow if the | |
3718 | -- low bound is Ityp'First. | |
3719 | ||
3720 | Assign := | |
3721 | Make_Implicit_If_Statement (Cnode, | |
39ade2f9 | 3722 | Condition => |
76c597a1 | 3723 | Make_Op_Ne (Loc, |
39ade2f9 | 3724 | Left_Opnd => |
76c597a1 AC |
3725 | New_Occurrence_Of (Var_Length (J), Loc), |
3726 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
39ade2f9 | 3727 | Then_Statements => New_List (Assign)); |
76c597a1 | 3728 | end if; |
fa969310 | 3729 | |
76c597a1 AC |
3730 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
3731 | end; | |
df46b832 AC |
3732 | end if; |
3733 | end; | |
3734 | end loop; | |
70482933 | 3735 | |
0ac73189 AC |
3736 | -- Finally we build the result, which is a reference to the array object |
3737 | ||
e4494292 | 3738 | Result := New_Occurrence_Of (Ent, Loc); |
70482933 | 3739 | |
df46b832 AC |
3740 | <<Done>> |
3741 | Rewrite (Cnode, Result); | |
fdac1f80 AC |
3742 | Analyze_And_Resolve (Cnode, Atyp); |
3743 | ||
3744 | exception | |
3745 | when Concatenation_Error => | |
76c597a1 AC |
3746 | |
3747 | -- Kill warning generated for the declaration of the static out of | |
3748 | -- range high bound, and instead generate a Constraint_Error with | |
3749 | -- an appropriate specific message. | |
3750 | ||
3751 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
3752 | Apply_Compile_Time_Constraint_Error | |
3753 | (N => Cnode, | |
324ac540 | 3754 | Msg => "concatenation result upper bound out of range??", |
76c597a1 | 3755 | Reason => CE_Range_Check_Failed); |
fdac1f80 | 3756 | end Expand_Concatenate; |
70482933 | 3757 | |
f6194278 RD |
3758 | --------------------------------------------------- |
3759 | -- Expand_Membership_Minimize_Eliminate_Overflow -- | |
3760 | --------------------------------------------------- | |
3761 | ||
3762 | procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is | |
3763 | pragma Assert (Nkind (N) = N_In); | |
3764 | -- Despite the name, this routine applies only to N_In, not to | |
3765 | -- N_Not_In. The latter is always rewritten as not (X in Y). | |
3766 | ||
71fb4dc8 AC |
3767 | Result_Type : constant Entity_Id := Etype (N); |
3768 | -- Capture result type, may be a derived boolean type | |
3769 | ||
b6b5cca8 AC |
3770 | Loc : constant Source_Ptr := Sloc (N); |
3771 | Lop : constant Node_Id := Left_Opnd (N); | |
3772 | Rop : constant Node_Id := Right_Opnd (N); | |
3773 | ||
3774 | -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It | |
3775 | -- is thus tempting to capture these values, but due to the rewrites | |
3776 | -- that occur as a result of overflow checking, these values change | |
3777 | -- as we go along, and it is safe just to always use Etype explicitly. | |
f6194278 RD |
3778 | |
3779 | Restype : constant Entity_Id := Etype (N); | |
3780 | -- Save result type | |
3781 | ||
3782 | Lo, Hi : Uint; | |
d8192289 | 3783 | -- Bounds in Minimize calls, not used currently |
f6194278 RD |
3784 | |
3785 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); | |
3786 | -- Entity for Long_Long_Integer'Base (Standard should export this???) | |
3787 | ||
3788 | begin | |
a7f1b24f | 3789 | Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False); |
f6194278 RD |
3790 | |
3791 | -- If right operand is a subtype name, and the subtype name has no | |
3792 | -- predicate, then we can just replace the right operand with an | |
3793 | -- explicit range T'First .. T'Last, and use the explicit range code. | |
3794 | ||
b6b5cca8 AC |
3795 | if Nkind (Rop) /= N_Range |
3796 | and then No (Predicate_Function (Etype (Rop))) | |
3797 | then | |
3798 | declare | |
3799 | Rtyp : constant Entity_Id := Etype (Rop); | |
3800 | begin | |
3801 | Rewrite (Rop, | |
3802 | Make_Range (Loc, | |
3803 | Low_Bound => | |
3804 | Make_Attribute_Reference (Loc, | |
3805 | Attribute_Name => Name_First, | |
e4494292 | 3806 | Prefix => New_Occurrence_Of (Rtyp, Loc)), |
b6b5cca8 AC |
3807 | High_Bound => |
3808 | Make_Attribute_Reference (Loc, | |
3809 | Attribute_Name => Name_Last, | |
e4494292 | 3810 | Prefix => New_Occurrence_Of (Rtyp, Loc)))); |
b6b5cca8 AC |
3811 | Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks); |
3812 | end; | |
f6194278 RD |
3813 | end if; |
3814 | ||
3815 | -- Here for the explicit range case. Note that the bounds of the range | |
3816 | -- have not been processed for minimized or eliminated checks. | |
3817 | ||
3818 | if Nkind (Rop) = N_Range then | |
a7f1b24f | 3819 | Minimize_Eliminate_Overflows |
b6b5cca8 | 3820 | (Low_Bound (Rop), Lo, Hi, Top_Level => False); |
a7f1b24f | 3821 | Minimize_Eliminate_Overflows |
c7e152b5 | 3822 | (High_Bound (Rop), Lo, Hi, Top_Level => False); |
f6194278 RD |
3823 | |
3824 | -- We have A in B .. C, treated as A >= B and then A <= C | |
3825 | ||
3826 | -- Bignum case | |
3827 | ||
b6b5cca8 | 3828 | if Is_RTE (Etype (Lop), RE_Bignum) |
f6194278 RD |
3829 | or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum) |
3830 | or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum) | |
3831 | then | |
3832 | declare | |
3833 | Blk : constant Node_Id := Make_Bignum_Block (Loc); | |
3834 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
71fb4dc8 AC |
3835 | L : constant Entity_Id := |
3836 | Make_Defining_Identifier (Loc, Name_uL); | |
f6194278 RD |
3837 | Lopnd : constant Node_Id := Convert_To_Bignum (Lop); |
3838 | Lbound : constant Node_Id := | |
3839 | Convert_To_Bignum (Low_Bound (Rop)); | |
3840 | Hbound : constant Node_Id := | |
3841 | Convert_To_Bignum (High_Bound (Rop)); | |
3842 | ||
71fb4dc8 AC |
3843 | -- Now we rewrite the membership test node to look like |
3844 | ||
3845 | -- do | |
3846 | -- Bnn : Result_Type; | |
3847 | -- declare | |
3848 | -- M : Mark_Id := SS_Mark; | |
3849 | -- L : Bignum := Lopnd; | |
3850 | -- begin | |
3851 | -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound) | |
3852 | -- SS_Release (M); | |
3853 | -- end; | |
3854 | -- in | |
3855 | -- Bnn | |
3856 | -- end | |
f6194278 RD |
3857 | |
3858 | begin | |
71fb4dc8 AC |
3859 | -- Insert declaration of L into declarations of bignum block |
3860 | ||
f6194278 RD |
3861 | Insert_After |
3862 | (Last (Declarations (Blk)), | |
3863 | Make_Object_Declaration (Loc, | |
71fb4dc8 | 3864 | Defining_Identifier => L, |
f6194278 RD |
3865 | Object_Definition => |
3866 | New_Occurrence_Of (RTE (RE_Bignum), Loc), | |
3867 | Expression => Lopnd)); | |
3868 | ||
71fb4dc8 AC |
3869 | -- Insert assignment to Bnn into expressions of bignum block |
3870 | ||
f6194278 RD |
3871 | Insert_Before |
3872 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
3873 | Make_Assignment_Statement (Loc, | |
3874 | Name => New_Occurrence_Of (Bnn, Loc), | |
3875 | Expression => | |
3876 | Make_And_Then (Loc, | |
3877 | Left_Opnd => | |
3878 | Make_Function_Call (Loc, | |
3879 | Name => | |
3880 | New_Occurrence_Of (RTE (RE_Big_GE), Loc), | |
71fb4dc8 AC |
3881 | Parameter_Associations => New_List ( |
3882 | New_Occurrence_Of (L, Loc), | |
3883 | Lbound)), | |
f6194278 RD |
3884 | Right_Opnd => |
3885 | Make_Function_Call (Loc, | |
3886 | Name => | |
71fb4dc8 AC |
3887 | New_Occurrence_Of (RTE (RE_Big_LE), Loc), |
3888 | Parameter_Associations => New_List ( | |
3889 | New_Occurrence_Of (L, Loc), | |
3890 | Hbound))))); | |
f6194278 | 3891 | |
71fb4dc8 | 3892 | -- Now rewrite the node |
f6194278 | 3893 | |
71fb4dc8 AC |
3894 | Rewrite (N, |
3895 | Make_Expression_With_Actions (Loc, | |
3896 | Actions => New_List ( | |
3897 | Make_Object_Declaration (Loc, | |
3898 | Defining_Identifier => Bnn, | |
3899 | Object_Definition => | |
3900 | New_Occurrence_Of (Result_Type, Loc)), | |
3901 | Blk), | |
3902 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
3903 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
3904 | return; |
3905 | end; | |
3906 | ||
3907 | -- Here if no bignums around | |
3908 | ||
3909 | else | |
3910 | -- Case where types are all the same | |
3911 | ||
b6b5cca8 | 3912 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop))) |
f6194278 | 3913 | and then |
b6b5cca8 | 3914 | Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop))) |
f6194278 RD |
3915 | then |
3916 | null; | |
3917 | ||
3918 | -- If types are not all the same, it means that we have rewritten | |
3919 | -- at least one of them to be of type Long_Long_Integer, and we | |
3920 | -- will convert the other operands to Long_Long_Integer. | |
3921 | ||
3922 | else | |
3923 | Convert_To_And_Rewrite (LLIB, Lop); | |
71fb4dc8 AC |
3924 | Set_Analyzed (Lop, False); |
3925 | Analyze_And_Resolve (Lop, LLIB); | |
3926 | ||
3927 | -- For the right operand, avoid unnecessary recursion into | |
3928 | -- this routine, we know that overflow is not possible. | |
f6194278 RD |
3929 | |
3930 | Convert_To_And_Rewrite (LLIB, Low_Bound (Rop)); | |
3931 | Convert_To_And_Rewrite (LLIB, High_Bound (Rop)); | |
3932 | Set_Analyzed (Rop, False); | |
71fb4dc8 | 3933 | Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check); |
f6194278 RD |
3934 | end if; |
3935 | ||
3936 | -- Now the three operands are of the same signed integer type, | |
b6b5cca8 AC |
3937 | -- so we can use the normal expansion routine for membership, |
3938 | -- setting the flag to prevent recursion into this procedure. | |
f6194278 RD |
3939 | |
3940 | Set_No_Minimize_Eliminate (N); | |
3941 | Expand_N_In (N); | |
3942 | end if; | |
3943 | ||
3944 | -- Right operand is a subtype name and the subtype has a predicate. We | |
f6636994 AC |
3945 | -- have to make sure the predicate is checked, and for that we need to |
3946 | -- use the standard N_In circuitry with appropriate types. | |
f6194278 RD |
3947 | |
3948 | else | |
b6b5cca8 | 3949 | pragma Assert (Present (Predicate_Function (Etype (Rop)))); |
f6194278 RD |
3950 | |
3951 | -- If types are "right", just call Expand_N_In preventing recursion | |
3952 | ||
b6b5cca8 | 3953 | if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then |
f6194278 RD |
3954 | Set_No_Minimize_Eliminate (N); |
3955 | Expand_N_In (N); | |
3956 | ||
3957 | -- Bignum case | |
3958 | ||
b6b5cca8 | 3959 | elsif Is_RTE (Etype (Lop), RE_Bignum) then |
f6194278 | 3960 | |
71fb4dc8 | 3961 | -- For X in T, we want to rewrite our node as |
f6194278 | 3962 | |
71fb4dc8 AC |
3963 | -- do |
3964 | -- Bnn : Result_Type; | |
f6194278 | 3965 | |
71fb4dc8 AC |
3966 | -- declare |
3967 | -- M : Mark_Id := SS_Mark; | |
3968 | -- Lnn : Long_Long_Integer'Base | |
3969 | -- Nnn : Bignum; | |
f6194278 | 3970 | |
71fb4dc8 AC |
3971 | -- begin |
3972 | -- Nnn := X; | |
3973 | ||
3974 | -- if not Bignum_In_LLI_Range (Nnn) then | |
3975 | -- Bnn := False; | |
3976 | -- else | |
3977 | -- Lnn := From_Bignum (Nnn); | |
3978 | -- Bnn := | |
3979 | -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last) | |
3980 | -- and then T'Base (Lnn) in T; | |
3981 | -- end if; | |
f6194278 | 3982 | -- |
71fb4dc8 AC |
3983 | -- SS_Release (M); |
3984 | -- end | |
3985 | -- in | |
3986 | -- Bnn | |
3987 | -- end | |
f6194278 | 3988 | |
f6636994 | 3989 | -- A bit gruesome, but there doesn't seem to be a simpler way |
f6194278 RD |
3990 | |
3991 | declare | |
b6b5cca8 AC |
3992 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3993 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
3994 | Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N); | |
3995 | Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N); | |
71fb4dc8 AC |
3996 | T : constant Entity_Id := Etype (Rop); |
3997 | TB : constant Entity_Id := Base_Type (T); | |
b6b5cca8 | 3998 | Nin : Node_Id; |
f6194278 RD |
3999 | |
4000 | begin | |
71fb4dc8 | 4001 | -- Mark the last membership operation to prevent recursion |
f6194278 RD |
4002 | |
4003 | Nin := | |
4004 | Make_In (Loc, | |
f6636994 AC |
4005 | Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)), |
4006 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
4007 | Set_No_Minimize_Eliminate (Nin); |
4008 | ||
4009 | -- Now decorate the block | |
4010 | ||
4011 | Insert_After | |
4012 | (Last (Declarations (Blk)), | |
4013 | Make_Object_Declaration (Loc, | |
4014 | Defining_Identifier => Lnn, | |
4015 | Object_Definition => New_Occurrence_Of (LLIB, Loc))); | |
4016 | ||
4017 | Insert_After | |
4018 | (Last (Declarations (Blk)), | |
4019 | Make_Object_Declaration (Loc, | |
4020 | Defining_Identifier => Nnn, | |
4021 | Object_Definition => | |
4022 | New_Occurrence_Of (RTE (RE_Bignum), Loc))); | |
4023 | ||
4024 | Insert_List_Before | |
4025 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
4026 | New_List ( | |
4027 | Make_Assignment_Statement (Loc, | |
4028 | Name => New_Occurrence_Of (Nnn, Loc), | |
4029 | Expression => Relocate_Node (Lop)), | |
4030 | ||
8b1011c0 | 4031 | Make_Implicit_If_Statement (N, |
f6194278 | 4032 | Condition => |
71fb4dc8 AC |
4033 | Make_Op_Not (Loc, |
4034 | Right_Opnd => | |
4035 | Make_Function_Call (Loc, | |
4036 | Name => | |
4037 | New_Occurrence_Of | |
4038 | (RTE (RE_Bignum_In_LLI_Range), Loc), | |
4039 | Parameter_Associations => New_List ( | |
4040 | New_Occurrence_Of (Nnn, Loc)))), | |
f6194278 RD |
4041 | |
4042 | Then_Statements => New_List ( | |
4043 | Make_Assignment_Statement (Loc, | |
4044 | Name => New_Occurrence_Of (Bnn, Loc), | |
4045 | Expression => | |
4046 | New_Occurrence_Of (Standard_False, Loc))), | |
4047 | ||
4048 | Else_Statements => New_List ( | |
4049 | Make_Assignment_Statement (Loc, | |
4050 | Name => New_Occurrence_Of (Lnn, Loc), | |
4051 | Expression => | |
4052 | Make_Function_Call (Loc, | |
4053 | Name => | |
4054 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4055 | Parameter_Associations => New_List ( | |
4056 | New_Occurrence_Of (Nnn, Loc)))), | |
4057 | ||
4058 | Make_Assignment_Statement (Loc, | |
71fb4dc8 | 4059 | Name => New_Occurrence_Of (Bnn, Loc), |
f6194278 RD |
4060 | Expression => |
4061 | Make_And_Then (Loc, | |
71fb4dc8 | 4062 | Left_Opnd => |
f6194278 | 4063 | Make_In (Loc, |
71fb4dc8 | 4064 | Left_Opnd => New_Occurrence_Of (Lnn, Loc), |
f6194278 | 4065 | Right_Opnd => |
71fb4dc8 AC |
4066 | Make_Range (Loc, |
4067 | Low_Bound => | |
4068 | Convert_To (LLIB, | |
4069 | Make_Attribute_Reference (Loc, | |
4070 | Attribute_Name => Name_First, | |
4071 | Prefix => | |
4072 | New_Occurrence_Of (TB, Loc))), | |
4073 | ||
4074 | High_Bound => | |
4075 | Convert_To (LLIB, | |
4076 | Make_Attribute_Reference (Loc, | |
4077 | Attribute_Name => Name_Last, | |
4078 | Prefix => | |
4079 | New_Occurrence_Of (TB, Loc))))), | |
4080 | ||
f6194278 RD |
4081 | Right_Opnd => Nin)))))); |
4082 | ||
71fb4dc8 | 4083 | -- Now we can do the rewrite |
f6194278 | 4084 | |
71fb4dc8 AC |
4085 | Rewrite (N, |
4086 | Make_Expression_With_Actions (Loc, | |
4087 | Actions => New_List ( | |
4088 | Make_Object_Declaration (Loc, | |
4089 | Defining_Identifier => Bnn, | |
4090 | Object_Definition => | |
4091 | New_Occurrence_Of (Result_Type, Loc)), | |
4092 | Blk), | |
4093 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
4094 | Analyze_And_Resolve (N, Result_Type); | |
f6194278 RD |
4095 | return; |
4096 | end; | |
4097 | ||
4098 | -- Not bignum case, but types don't match (this means we rewrote the | |
b6b5cca8 | 4099 | -- left operand to be Long_Long_Integer). |
f6194278 RD |
4100 | |
4101 | else | |
b6b5cca8 | 4102 | pragma Assert (Base_Type (Etype (Lop)) = LLIB); |
f6194278 | 4103 | |
71fb4dc8 AC |
4104 | -- We rewrite the membership test as (where T is the type with |
4105 | -- the predicate, i.e. the type of the right operand) | |
f6194278 | 4106 | |
71fb4dc8 AC |
4107 | -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last) |
4108 | -- and then T'Base (Lop) in T | |
f6194278 RD |
4109 | |
4110 | declare | |
71fb4dc8 AC |
4111 | T : constant Entity_Id := Etype (Rop); |
4112 | TB : constant Entity_Id := Base_Type (T); | |
f6194278 RD |
4113 | Nin : Node_Id; |
4114 | ||
4115 | begin | |
4116 | -- The last membership test is marked to prevent recursion | |
4117 | ||
4118 | Nin := | |
4119 | Make_In (Loc, | |
71fb4dc8 AC |
4120 | Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)), |
4121 | Right_Opnd => New_Occurrence_Of (T, Loc)); | |
f6194278 RD |
4122 | Set_No_Minimize_Eliminate (Nin); |
4123 | ||
4124 | -- Now do the rewrite | |
4125 | ||
4126 | Rewrite (N, | |
4127 | Make_And_Then (Loc, | |
71fb4dc8 | 4128 | Left_Opnd => |
f6194278 RD |
4129 | Make_In (Loc, |
4130 | Left_Opnd => Lop, | |
4131 | Right_Opnd => | |
71fb4dc8 AC |
4132 | Make_Range (Loc, |
4133 | Low_Bound => | |
4134 | Convert_To (LLIB, | |
4135 | Make_Attribute_Reference (Loc, | |
4136 | Attribute_Name => Name_First, | |
4137 | Prefix => New_Occurrence_Of (TB, Loc))), | |
4138 | High_Bound => | |
4139 | Convert_To (LLIB, | |
4140 | Make_Attribute_Reference (Loc, | |
4141 | Attribute_Name => Name_Last, | |
4142 | Prefix => New_Occurrence_Of (TB, Loc))))), | |
f6194278 | 4143 | Right_Opnd => Nin)); |
71fb4dc8 AC |
4144 | Set_Analyzed (N, False); |
4145 | Analyze_And_Resolve (N, Restype); | |
f6194278 RD |
4146 | end; |
4147 | end if; | |
4148 | end if; | |
4149 | end Expand_Membership_Minimize_Eliminate_Overflow; | |
4150 | ||
70482933 RK |
4151 | ------------------------ |
4152 | -- Expand_N_Allocator -- | |
4153 | ------------------------ | |
4154 | ||
4155 | procedure Expand_N_Allocator (N : Node_Id) is | |
8b1011c0 AC |
4156 | Etyp : constant Entity_Id := Etype (Expression (N)); |
4157 | Loc : constant Source_Ptr := Sloc (N); | |
4158 | PtrT : constant Entity_Id := Etype (N); | |
70482933 | 4159 | |
26bff3d9 JM |
4160 | procedure Rewrite_Coextension (N : Node_Id); |
4161 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 4162 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 4163 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 4164 | |
8aec446b | 4165 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd AC |
4166 | -- Given a constrained array type E, returns a node representing the |
4167 | -- code to compute the size in storage elements for the given type. | |
205c14b0 | 4168 | -- This is done without using the attribute (which malfunctions for |
507ed3fd | 4169 | -- large sizes ???) |
8aec446b | 4170 | |
26bff3d9 JM |
4171 | ------------------------- |
4172 | -- Rewrite_Coextension -- | |
4173 | ------------------------- | |
4174 | ||
4175 | procedure Rewrite_Coextension (N : Node_Id) is | |
e5a22243 AC |
4176 | Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C'); |
4177 | Temp_Decl : Node_Id; | |
26bff3d9 | 4178 | |
df3e68b1 | 4179 | begin |
26bff3d9 JM |
4180 | -- Generate: |
4181 | -- Cnn : aliased Etyp; | |
4182 | ||
df3e68b1 HK |
4183 | Temp_Decl := |
4184 | Make_Object_Declaration (Loc, | |
4185 | Defining_Identifier => Temp_Id, | |
243cae0a AC |
4186 | Aliased_Present => True, |
4187 | Object_Definition => New_Occurrence_Of (Etyp, Loc)); | |
26bff3d9 | 4188 | |
26bff3d9 | 4189 | if Nkind (Expression (N)) = N_Qualified_Expression then |
df3e68b1 | 4190 | Set_Expression (Temp_Decl, Expression (Expression (N))); |
0669bebe | 4191 | end if; |
26bff3d9 | 4192 | |
e5a22243 | 4193 | Insert_Action (N, Temp_Decl); |
26bff3d9 JM |
4194 | Rewrite (N, |
4195 | Make_Attribute_Reference (Loc, | |
243cae0a | 4196 | Prefix => New_Occurrence_Of (Temp_Id, Loc), |
26bff3d9 JM |
4197 | Attribute_Name => Name_Unrestricted_Access)); |
4198 | ||
4199 | Analyze_And_Resolve (N, PtrT); | |
4200 | end Rewrite_Coextension; | |
0669bebe | 4201 | |
8aec446b AC |
4202 | ------------------------------ |
4203 | -- Size_In_Storage_Elements -- | |
4204 | ------------------------------ | |
4205 | ||
4206 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
4207 | begin | |
4208 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
4209 | -- However, the reason for the existence of this function is | |
4210 | -- to construct a test for sizes too large, which means near the | |
4211 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
4212 | -- is that we get overflows when sizes are greater than 2**31. | |
4213 | ||
507ed3fd | 4214 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
4215 | |
4216 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
4217 | ||
46202729 | 4218 | -- which avoids this problem. All this is a bit bogus, but it does |
8aec446b AC |
4219 | -- mean we catch common cases of trying to allocate arrays that |
4220 | -- are too large, and which in the absence of a check results in | |
4221 | -- undetected chaos ??? | |
4222 | ||
ce532f42 AC |
4223 | -- Note in particular that this is a pessimistic estimate in the |
4224 | -- case of packed array types, where an array element might occupy | |
4225 | -- just a fraction of a storage element??? | |
4226 | ||
507ed3fd AC |
4227 | declare |
4228 | Len : Node_Id; | |
4229 | Res : Node_Id; | |
8aec446b | 4230 | |
507ed3fd AC |
4231 | begin |
4232 | for J in 1 .. Number_Dimensions (E) loop | |
4233 | Len := | |
4234 | Make_Attribute_Reference (Loc, | |
4235 | Prefix => New_Occurrence_Of (E, Loc), | |
4236 | Attribute_Name => Name_Length, | |
243cae0a | 4237 | Expressions => New_List (Make_Integer_Literal (Loc, J))); |
8aec446b | 4238 | |
507ed3fd AC |
4239 | if J = 1 then |
4240 | Res := Len; | |
8aec446b | 4241 | |
507ed3fd AC |
4242 | else |
4243 | Res := | |
4244 | Make_Op_Multiply (Loc, | |
4245 | Left_Opnd => Res, | |
4246 | Right_Opnd => Len); | |
4247 | end if; | |
4248 | end loop; | |
8aec446b | 4249 | |
8aec446b | 4250 | return |
507ed3fd AC |
4251 | Make_Op_Multiply (Loc, |
4252 | Left_Opnd => Len, | |
4253 | Right_Opnd => | |
4254 | Make_Attribute_Reference (Loc, | |
4255 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
4256 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
4257 | end; | |
8aec446b AC |
4258 | end Size_In_Storage_Elements; |
4259 | ||
8b1011c0 AC |
4260 | -- Local variables |
4261 | ||
70861157 | 4262 | Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT)); |
8b1011c0 AC |
4263 | Desig : Entity_Id; |
4264 | Nod : Node_Id; | |
4265 | Pool : Entity_Id; | |
4266 | Rel_Typ : Entity_Id; | |
4267 | Temp : Entity_Id; | |
4268 | ||
0669bebe GB |
4269 | -- Start of processing for Expand_N_Allocator |
4270 | ||
70482933 RK |
4271 | begin |
4272 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
4273 | -- shall not be a remote access-to-class-wide-limited-private type | |
4274 | ||
4275 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
4276 | ||
4277 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
4278 | ||
ca5af305 AC |
4279 | -- Processing for anonymous access-to-controlled types. These access |
4280 | -- types receive a special finalization master which appears in the | |
4281 | -- declarations of the enclosing semantic unit. This expansion is done | |
84f4072a JM |
4282 | -- now to ensure that any additional types generated by this routine or |
4283 | -- Expand_Allocator_Expression inherit the proper type attributes. | |
ca5af305 | 4284 | |
84f4072a | 4285 | if (Ekind (PtrT) = E_Anonymous_Access_Type |
533369aa | 4286 | or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT)))) |
ca5af305 AC |
4287 | and then Needs_Finalization (Dtyp) |
4288 | then | |
8b1011c0 AC |
4289 | -- Detect the allocation of an anonymous controlled object where the |
4290 | -- type of the context is named. For example: | |
4291 | ||
4292 | -- procedure Proc (Ptr : Named_Access_Typ); | |
4293 | -- Proc (new Designated_Typ); | |
4294 | ||
4295 | -- Regardless of the anonymous-to-named access type conversion, the | |
4296 | -- lifetime of the object must be associated with the named access | |
0088ba92 | 4297 | -- type. Use the finalization-related attributes of this type. |
8b1011c0 AC |
4298 | |
4299 | if Nkind_In (Parent (N), N_Type_Conversion, | |
4300 | N_Unchecked_Type_Conversion) | |
4301 | and then Ekind_In (Etype (Parent (N)), E_Access_Subtype, | |
4302 | E_Access_Type, | |
4303 | E_General_Access_Type) | |
4304 | then | |
4305 | Rel_Typ := Etype (Parent (N)); | |
4306 | else | |
4307 | Rel_Typ := Empty; | |
4308 | end if; | |
4309 | ||
b254da66 AC |
4310 | -- Anonymous access-to-controlled types allocate on the global pool. |
4311 | -- Do not set this attribute on .NET/JVM since those targets do not | |
4312 | -- support pools. | |
ca5af305 | 4313 | |
bde73c6b | 4314 | if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then |
8b1011c0 AC |
4315 | if Present (Rel_Typ) then |
4316 | Set_Associated_Storage_Pool (PtrT, | |
4317 | Associated_Storage_Pool (Rel_Typ)); | |
4318 | else | |
4319 | Set_Associated_Storage_Pool (PtrT, | |
4320 | Get_Global_Pool_For_Access_Type (PtrT)); | |
4321 | end if; | |
ca5af305 AC |
4322 | end if; |
4323 | ||
4324 | -- The finalization master must be inserted and analyzed as part of | |
5114f3ff AC |
4325 | -- the current semantic unit. Note that the master is updated when |
4326 | -- analysis changes current units. | |
ca5af305 | 4327 | |
5114f3ff AC |
4328 | if Present (Rel_Typ) then |
4329 | Set_Finalization_Master (PtrT, Finalization_Master (Rel_Typ)); | |
4330 | else | |
4331 | Set_Finalization_Master (PtrT, Current_Anonymous_Master); | |
ca5af305 AC |
4332 | end if; |
4333 | end if; | |
4334 | ||
4335 | -- Set the storage pool and find the appropriate version of Allocate to | |
8417f4b2 AC |
4336 | -- call. Do not overwrite the storage pool if it is already set, which |
4337 | -- can happen for build-in-place function returns (see | |
200b7162 | 4338 | -- Exp_Ch4.Expand_N_Extended_Return_Statement). |
70482933 | 4339 | |
200b7162 BD |
4340 | if No (Storage_Pool (N)) then |
4341 | Pool := Associated_Storage_Pool (Root_Type (PtrT)); | |
70482933 | 4342 | |
200b7162 BD |
4343 | if Present (Pool) then |
4344 | Set_Storage_Pool (N, Pool); | |
fbf5a39b | 4345 | |
200b7162 BD |
4346 | if Is_RTE (Pool, RE_SS_Pool) then |
4347 | if VM_Target = No_VM then | |
4348 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); | |
4349 | end if; | |
fbf5a39b | 4350 | |
a8551b5f AC |
4351 | -- In the case of an allocator for a simple storage pool, locate |
4352 | -- and save a reference to the pool type's Allocate routine. | |
4353 | ||
4354 | elsif Present (Get_Rep_Pragma | |
f6205414 | 4355 | (Etype (Pool), Name_Simple_Storage_Pool_Type)) |
a8551b5f AC |
4356 | then |
4357 | declare | |
a8551b5f | 4358 | Pool_Type : constant Entity_Id := Base_Type (Etype (Pool)); |
260359e3 | 4359 | Alloc_Op : Entity_Id; |
a8551b5f | 4360 | begin |
260359e3 | 4361 | Alloc_Op := Get_Name_Entity_Id (Name_Allocate); |
a8551b5f AC |
4362 | while Present (Alloc_Op) loop |
4363 | if Scope (Alloc_Op) = Scope (Pool_Type) | |
4364 | and then Present (First_Formal (Alloc_Op)) | |
4365 | and then Etype (First_Formal (Alloc_Op)) = Pool_Type | |
4366 | then | |
4367 | Set_Procedure_To_Call (N, Alloc_Op); | |
a8551b5f | 4368 | exit; |
260359e3 AC |
4369 | else |
4370 | Alloc_Op := Homonym (Alloc_Op); | |
a8551b5f | 4371 | end if; |
a8551b5f AC |
4372 | end loop; |
4373 | end; | |
4374 | ||
200b7162 BD |
4375 | elsif Is_Class_Wide_Type (Etype (Pool)) then |
4376 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
4377 | ||
4378 | else | |
4379 | Set_Procedure_To_Call (N, | |
4380 | Find_Prim_Op (Etype (Pool), Name_Allocate)); | |
4381 | end if; | |
70482933 RK |
4382 | end if; |
4383 | end if; | |
4384 | ||
685094bf RD |
4385 | -- Under certain circumstances we can replace an allocator by an access |
4386 | -- to statically allocated storage. The conditions, as noted in AARM | |
4387 | -- 3.10 (10c) are as follows: | |
70482933 RK |
4388 | |
4389 | -- Size and initial value is known at compile time | |
4390 | -- Access type is access-to-constant | |
4391 | ||
fbf5a39b AC |
4392 | -- The allocator is not part of a constraint on a record component, |
4393 | -- because in that case the inserted actions are delayed until the | |
4394 | -- record declaration is fully analyzed, which is too late for the | |
4395 | -- analysis of the rewritten allocator. | |
4396 | ||
70482933 RK |
4397 | if Is_Access_Constant (PtrT) |
4398 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
4399 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
243cae0a AC |
4400 | and then Size_Known_At_Compile_Time |
4401 | (Etype (Expression (Expression (N)))) | |
fbf5a39b | 4402 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
4403 | then |
4404 | -- Here we can do the optimization. For the allocator | |
4405 | ||
4406 | -- new x'(y) | |
4407 | ||
4408 | -- We insert an object declaration | |
4409 | ||
4410 | -- Tnn : aliased x := y; | |
4411 | ||
685094bf RD |
4412 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
4413 | -- marked as requiring static allocation. | |
70482933 | 4414 | |
df3e68b1 | 4415 | Temp := Make_Temporary (Loc, 'T', Expression (Expression (N))); |
70482933 RK |
4416 | Desig := Subtype_Mark (Expression (N)); |
4417 | ||
4418 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 4419 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
4420 | -- unconstrained subtype. |
4421 | ||
0da2c8ac AC |
4422 | if Entity (Desig) = Base_Type (Dtyp) then |
4423 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
4424 | end if; |
4425 | ||
4426 | Insert_Action (N, | |
4427 | Make_Object_Declaration (Loc, | |
4428 | Defining_Identifier => Temp, | |
4429 | Aliased_Present => True, | |
4430 | Constant_Present => Is_Access_Constant (PtrT), | |
4431 | Object_Definition => Desig, | |
4432 | Expression => Expression (Expression (N)))); | |
4433 | ||
4434 | Rewrite (N, | |
4435 | Make_Attribute_Reference (Loc, | |
243cae0a | 4436 | Prefix => New_Occurrence_Of (Temp, Loc), |
70482933 RK |
4437 | Attribute_Name => Name_Unrestricted_Access)); |
4438 | ||
4439 | Analyze_And_Resolve (N, PtrT); | |
4440 | ||
685094bf | 4441 | -- We set the variable as statically allocated, since we don't want |
a90bd866 | 4442 | -- it going on the stack of the current procedure. |
70482933 RK |
4443 | |
4444 | Set_Is_Statically_Allocated (Temp); | |
4445 | return; | |
4446 | end if; | |
4447 | ||
0669bebe GB |
4448 | -- Same if the allocator is an access discriminant for a local object: |
4449 | -- instead of an allocator we create a local value and constrain the | |
308e6f3a | 4450 | -- enclosing object with the corresponding access attribute. |
0669bebe | 4451 | |
26bff3d9 JM |
4452 | if Is_Static_Coextension (N) then |
4453 | Rewrite_Coextension (N); | |
0669bebe GB |
4454 | return; |
4455 | end if; | |
4456 | ||
8aec446b AC |
4457 | -- Check for size too large, we do this because the back end misses |
4458 | -- proper checks here and can generate rubbish allocation calls when | |
4459 | -- we are near the limit. We only do this for the 32-bit address case | |
4460 | -- since that is from a practical point of view where we see a problem. | |
4461 | ||
4462 | if System_Address_Size = 32 | |
4463 | and then not Storage_Checks_Suppressed (PtrT) | |
4464 | and then not Storage_Checks_Suppressed (Dtyp) | |
4465 | and then not Storage_Checks_Suppressed (Etyp) | |
4466 | then | |
4467 | -- The check we want to generate should look like | |
4468 | ||
4469 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
4470 | -- raise Storage_Error; | |
4471 | -- end if; | |
4472 | ||
308e6f3a | 4473 | -- where 3.5 gigabytes is a constant large enough to accommodate any |
507ed3fd AC |
4474 | -- reasonable request for. But we can't do it this way because at |
4475 | -- least at the moment we don't compute this attribute right, and | |
4476 | -- can silently give wrong results when the result gets large. Since | |
4477 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 4478 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 4479 | -- value of the attribute ??? |
8aec446b | 4480 | |
507ed3fd AC |
4481 | if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then |
4482 | Insert_Action (N, | |
4483 | Make_Raise_Storage_Error (Loc, | |
4484 | Condition => | |
4485 | Make_Op_Gt (Loc, | |
4486 | Left_Opnd => Size_In_Storage_Elements (Etyp), | |
4487 | Right_Opnd => | |
243cae0a | 4488 | Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))), |
507ed3fd AC |
4489 | Reason => SE_Object_Too_Large)); |
4490 | end if; | |
8aec446b AC |
4491 | end if; |
4492 | ||
0da2c8ac | 4493 | -- Handle case of qualified expression (other than optimization above) |
cac5a801 AC |
4494 | -- First apply constraint checks, because the bounds or discriminants |
4495 | -- in the aggregate might not match the subtype mark in the allocator. | |
0da2c8ac | 4496 | |
70482933 | 4497 | if Nkind (Expression (N)) = N_Qualified_Expression then |
cac5a801 AC |
4498 | Apply_Constraint_Check |
4499 | (Expression (Expression (N)), Etype (Expression (N))); | |
4500 | ||
fbf5a39b | 4501 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
4502 | return; |
4503 | end if; | |
fbf5a39b | 4504 | |
26bff3d9 JM |
4505 | -- If the allocator is for a type which requires initialization, and |
4506 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
4507 | -- rather than a qualified expression), then we must generate a call to |
4508 | -- the initialization routine using an expressions action node: | |
70482933 | 4509 | |
26bff3d9 | 4510 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 4511 | |
26bff3d9 JM |
4512 | -- Here ptr_T is the pointer type for the allocator, and T is the |
4513 | -- subtype of the allocator. A special case arises if the designated | |
4514 | -- type of the access type is a task or contains tasks. In this case | |
4515 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
4516 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
4517 | -- for details). In addition, if the type T is a task T, then the | |
4518 | -- first argument to Init must be converted to the task record type. | |
70482933 | 4519 | |
26bff3d9 | 4520 | declare |
df3e68b1 HK |
4521 | T : constant Entity_Id := Entity (Expression (N)); |
4522 | Args : List_Id; | |
4523 | Decls : List_Id; | |
4524 | Decl : Node_Id; | |
4525 | Discr : Elmt_Id; | |
4526 | Init : Entity_Id; | |
4527 | Init_Arg1 : Node_Id; | |
4528 | Temp_Decl : Node_Id; | |
4529 | Temp_Type : Entity_Id; | |
70482933 | 4530 | |
26bff3d9 JM |
4531 | begin |
4532 | if No_Initialization (N) then | |
df3e68b1 HK |
4533 | |
4534 | -- Even though this might be a simple allocation, create a custom | |
deb8dacc HK |
4535 | -- Allocate if the context requires it. Since .NET/JVM compilers |
4536 | -- do not support pools, this step is skipped. | |
df3e68b1 | 4537 | |
deb8dacc | 4538 | if VM_Target = No_VM |
d3f70b35 | 4539 | and then Present (Finalization_Master (PtrT)) |
deb8dacc | 4540 | then |
df3e68b1 | 4541 | Build_Allocate_Deallocate_Proc |
ca5af305 | 4542 | (N => N, |
df3e68b1 HK |
4543 | Is_Allocate => True); |
4544 | end if; | |
70482933 | 4545 | |
26bff3d9 | 4546 | -- Case of no initialization procedure present |
70482933 | 4547 | |
26bff3d9 | 4548 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 4549 | |
26bff3d9 | 4550 | -- Case of simple initialization required |
70482933 | 4551 | |
26bff3d9 | 4552 | if Needs_Simple_Initialization (T) then |
b4592168 | 4553 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
4554 | Rewrite (Expression (N), |
4555 | Make_Qualified_Expression (Loc, | |
4556 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 4557 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 4558 | |
26bff3d9 JM |
4559 | Analyze_And_Resolve (Expression (Expression (N)), T); |
4560 | Analyze_And_Resolve (Expression (N), T); | |
4561 | Set_Paren_Count (Expression (Expression (N)), 1); | |
4562 | Expand_N_Allocator (N); | |
70482933 | 4563 | |
26bff3d9 | 4564 | -- No initialization required |
70482933 RK |
4565 | |
4566 | else | |
26bff3d9 JM |
4567 | null; |
4568 | end if; | |
70482933 | 4569 | |
26bff3d9 | 4570 | -- Case of initialization procedure present, must be called |
70482933 | 4571 | |
26bff3d9 | 4572 | else |
b4592168 | 4573 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 4574 | |
b4592168 GD |
4575 | if not Restriction_Active (No_Default_Initialization) then |
4576 | Init := Base_Init_Proc (T); | |
4577 | Nod := N; | |
191fcb3a | 4578 | Temp := Make_Temporary (Loc, 'P'); |
70482933 | 4579 | |
b4592168 | 4580 | -- Construct argument list for the initialization routine call |
70482933 | 4581 | |
df3e68b1 | 4582 | Init_Arg1 := |
b4592168 | 4583 | Make_Explicit_Dereference (Loc, |
df3e68b1 | 4584 | Prefix => |
e4494292 | 4585 | New_Occurrence_Of (Temp, Loc)); |
df3e68b1 HK |
4586 | |
4587 | Set_Assignment_OK (Init_Arg1); | |
b4592168 | 4588 | Temp_Type := PtrT; |
26bff3d9 | 4589 | |
b4592168 GD |
4590 | -- The initialization procedure expects a specific type. if the |
4591 | -- context is access to class wide, indicate that the object | |
4592 | -- being allocated has the right specific type. | |
70482933 | 4593 | |
b4592168 | 4594 | if Is_Class_Wide_Type (Dtyp) then |
df3e68b1 | 4595 | Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1); |
b4592168 | 4596 | end if; |
70482933 | 4597 | |
b4592168 GD |
4598 | -- If designated type is a concurrent type or if it is private |
4599 | -- type whose definition is a concurrent type, the first | |
4600 | -- argument in the Init routine has to be unchecked conversion | |
4601 | -- to the corresponding record type. If the designated type is | |
243cae0a | 4602 | -- a derived type, also convert the argument to its root type. |
20b5d666 | 4603 | |
b4592168 | 4604 | if Is_Concurrent_Type (T) then |
df3e68b1 HK |
4605 | Init_Arg1 := |
4606 | Unchecked_Convert_To ( | |
4607 | Corresponding_Record_Type (T), Init_Arg1); | |
70482933 | 4608 | |
b4592168 GD |
4609 | elsif Is_Private_Type (T) |
4610 | and then Present (Full_View (T)) | |
4611 | and then Is_Concurrent_Type (Full_View (T)) | |
4612 | then | |
df3e68b1 | 4613 | Init_Arg1 := |
b4592168 | 4614 | Unchecked_Convert_To |
df3e68b1 | 4615 | (Corresponding_Record_Type (Full_View (T)), Init_Arg1); |
70482933 | 4616 | |
b4592168 GD |
4617 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
4618 | declare | |
4619 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
df3e68b1 | 4620 | |
b4592168 | 4621 | begin |
df3e68b1 HK |
4622 | Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1); |
4623 | Set_Etype (Init_Arg1, Ftyp); | |
b4592168 GD |
4624 | end; |
4625 | end if; | |
70482933 | 4626 | |
df3e68b1 | 4627 | Args := New_List (Init_Arg1); |
70482933 | 4628 | |
b4592168 GD |
4629 | -- For the task case, pass the Master_Id of the access type as |
4630 | -- the value of the _Master parameter, and _Chain as the value | |
4631 | -- of the _Chain parameter (_Chain will be defined as part of | |
4632 | -- the generated code for the allocator). | |
70482933 | 4633 | |
b4592168 GD |
4634 | -- In Ada 2005, the context may be a function that returns an |
4635 | -- anonymous access type. In that case the Master_Id has been | |
4636 | -- created when expanding the function declaration. | |
70482933 | 4637 | |
b4592168 GD |
4638 | if Has_Task (T) then |
4639 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 4640 | |
b4592168 GD |
4641 | -- The designated type was an incomplete type, and the |
4642 | -- access type did not get expanded. Salvage it now. | |
70482933 | 4643 | |
b941ae65 | 4644 | if not Restriction_Active (No_Task_Hierarchy) then |
3d67b239 AC |
4645 | if Present (Parent (Base_Type (PtrT))) then |
4646 | Expand_N_Full_Type_Declaration | |
4647 | (Parent (Base_Type (PtrT))); | |
4648 | ||
0d5fbf52 AC |
4649 | -- The only other possibility is an itype. For this |
4650 | -- case, the master must exist in the context. This is | |
4651 | -- the case when the allocator initializes an access | |
4652 | -- component in an init-proc. | |
3d67b239 | 4653 | |
0d5fbf52 | 4654 | else |
3d67b239 AC |
4655 | pragma Assert (Is_Itype (PtrT)); |
4656 | Build_Master_Renaming (PtrT, N); | |
4657 | end if; | |
b941ae65 | 4658 | end if; |
b4592168 | 4659 | end if; |
70482933 | 4660 | |
b4592168 GD |
4661 | -- If the context of the allocator is a declaration or an |
4662 | -- assignment, we can generate a meaningful image for it, | |
4663 | -- even though subsequent assignments might remove the | |
4664 | -- connection between task and entity. We build this image | |
4665 | -- when the left-hand side is a simple variable, a simple | |
4666 | -- indexed assignment or a simple selected component. | |
4667 | ||
4668 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
4669 | declare | |
4670 | Nam : constant Node_Id := Name (Parent (N)); | |
4671 | ||
4672 | begin | |
4673 | if Is_Entity_Name (Nam) then | |
4674 | Decls := | |
4675 | Build_Task_Image_Decls | |
4676 | (Loc, | |
4677 | New_Occurrence_Of | |
4678 | (Entity (Nam), Sloc (Nam)), T); | |
4679 | ||
243cae0a AC |
4680 | elsif Nkind_In (Nam, N_Indexed_Component, |
4681 | N_Selected_Component) | |
b4592168 GD |
4682 | and then Is_Entity_Name (Prefix (Nam)) |
4683 | then | |
4684 | Decls := | |
4685 | Build_Task_Image_Decls | |
4686 | (Loc, Nam, Etype (Prefix (Nam))); | |
4687 | else | |
4688 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
4689 | end if; | |
4690 | end; | |
70482933 | 4691 | |
b4592168 GD |
4692 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
4693 | Decls := | |
4694 | Build_Task_Image_Decls | |
4695 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 4696 | |
b4592168 GD |
4697 | else |
4698 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
4699 | end if; | |
26bff3d9 | 4700 | |
87dc09cb | 4701 | if Restriction_Active (No_Task_Hierarchy) then |
3c1ecd7e AC |
4702 | Append_To (Args, |
4703 | New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc)); | |
87dc09cb AC |
4704 | else |
4705 | Append_To (Args, | |
e4494292 | 4706 | New_Occurrence_Of |
87dc09cb AC |
4707 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); |
4708 | end if; | |
4709 | ||
b4592168 | 4710 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); |
26bff3d9 | 4711 | |
b4592168 GD |
4712 | Decl := Last (Decls); |
4713 | Append_To (Args, | |
4714 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 4715 | |
87dc09cb | 4716 | -- Has_Task is false, Decls not used |
26bff3d9 | 4717 | |
b4592168 GD |
4718 | else |
4719 | Decls := No_List; | |
26bff3d9 JM |
4720 | end if; |
4721 | ||
b4592168 GD |
4722 | -- Add discriminants if discriminated type |
4723 | ||
4724 | declare | |
4725 | Dis : Boolean := False; | |
4726 | Typ : Entity_Id; | |
4727 | ||
4728 | begin | |
4729 | if Has_Discriminants (T) then | |
4730 | Dis := True; | |
4731 | Typ := T; | |
4732 | ||
4733 | elsif Is_Private_Type (T) | |
4734 | and then Present (Full_View (T)) | |
4735 | and then Has_Discriminants (Full_View (T)) | |
20b5d666 | 4736 | then |
b4592168 GD |
4737 | Dis := True; |
4738 | Typ := Full_View (T); | |
20b5d666 | 4739 | end if; |
70482933 | 4740 | |
b4592168 | 4741 | if Dis then |
26bff3d9 | 4742 | |
b4592168 | 4743 | -- If the allocated object will be constrained by the |
685094bf RD |
4744 | -- default values for discriminants, then build a subtype |
4745 | -- with those defaults, and change the allocated subtype | |
4746 | -- to that. Note that this happens in fewer cases in Ada | |
4747 | -- 2005 (AI-363). | |
26bff3d9 | 4748 | |
b4592168 GD |
4749 | if not Is_Constrained (Typ) |
4750 | and then Present (Discriminant_Default_Value | |
df3e68b1 | 4751 | (First_Discriminant (Typ))) |
0791fbe9 | 4752 | and then (Ada_Version < Ada_2005 |
cc96a1b8 | 4753 | or else not |
0fbcb11c ES |
4754 | Object_Type_Has_Constrained_Partial_View |
4755 | (Typ, Current_Scope)) | |
20b5d666 | 4756 | then |
b4592168 | 4757 | Typ := Build_Default_Subtype (Typ, N); |
e4494292 | 4758 | Set_Expression (N, New_Occurrence_Of (Typ, Loc)); |
20b5d666 JM |
4759 | end if; |
4760 | ||
b4592168 GD |
4761 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
4762 | while Present (Discr) loop | |
4763 | Nod := Node (Discr); | |
4764 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 4765 | |
b4592168 GD |
4766 | -- AI-416: when the discriminant constraint is an |
4767 | -- anonymous access type make sure an accessibility | |
4768 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 4769 | |
0791fbe9 | 4770 | if Ada_Version >= Ada_2005 |
b4592168 GD |
4771 | and then |
4772 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
4773 | then | |
e84e11ba GD |
4774 | Apply_Accessibility_Check |
4775 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 4776 | end if; |
20b5d666 | 4777 | |
b4592168 GD |
4778 | Next_Elmt (Discr); |
4779 | end loop; | |
4780 | end if; | |
4781 | end; | |
70482933 | 4782 | |
4b985e20 | 4783 | -- We set the allocator as analyzed so that when we analyze |
9b16cb57 RD |
4784 | -- the if expression node, we do not get an unwanted recursive |
4785 | -- expansion of the allocator expression. | |
70482933 | 4786 | |
b4592168 GD |
4787 | Set_Analyzed (N, True); |
4788 | Nod := Relocate_Node (N); | |
70482933 | 4789 | |
b4592168 | 4790 | -- Here is the transformation: |
ca5af305 AC |
4791 | -- input: new Ctrl_Typ |
4792 | -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ; | |
4793 | -- Ctrl_TypIP (Temp.all, ...); | |
4794 | -- [Deep_]Initialize (Temp.all); | |
70482933 | 4795 | |
ca5af305 AC |
4796 | -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and |
4797 | -- is the subtype of the allocator. | |
70482933 | 4798 | |
b4592168 GD |
4799 | Temp_Decl := |
4800 | Make_Object_Declaration (Loc, | |
4801 | Defining_Identifier => Temp, | |
4802 | Constant_Present => True, | |
e4494292 | 4803 | Object_Definition => New_Occurrence_Of (Temp_Type, Loc), |
b4592168 | 4804 | Expression => Nod); |
70482933 | 4805 | |
b4592168 GD |
4806 | Set_Assignment_OK (Temp_Decl); |
4807 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 4808 | |
ca5af305 | 4809 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 4810 | |
b4592168 GD |
4811 | -- If the designated type is a task type or contains tasks, |
4812 | -- create block to activate created tasks, and insert | |
4813 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 4814 | |
b4592168 GD |
4815 | if Has_Task (T) then |
4816 | declare | |
4817 | L : constant List_Id := New_List; | |
4818 | Blk : Node_Id; | |
4819 | begin | |
4820 | Build_Task_Allocate_Block (L, Nod, Args); | |
4821 | Blk := Last (L); | |
4822 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
4823 | Insert_Actions (N, L); | |
4824 | end; | |
70482933 | 4825 | |
b4592168 GD |
4826 | else |
4827 | Insert_Action (N, | |
4828 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 4829 | Name => New_Occurrence_Of (Init, Loc), |
b4592168 GD |
4830 | Parameter_Associations => Args)); |
4831 | end if; | |
70482933 | 4832 | |
048e5cef | 4833 | if Needs_Finalization (T) then |
70482933 | 4834 | |
df3e68b1 HK |
4835 | -- Generate: |
4836 | -- [Deep_]Initialize (Init_Arg1); | |
70482933 | 4837 | |
df3e68b1 | 4838 | Insert_Action (N, |
243cae0a AC |
4839 | Make_Init_Call |
4840 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
4841 | Typ => T)); | |
b4592168 | 4842 | |
b254da66 | 4843 | if Present (Finalization_Master (PtrT)) then |
deb8dacc | 4844 | |
b254da66 AC |
4845 | -- Special processing for .NET/JVM, the allocated object |
4846 | -- is attached to the finalization master. Generate: | |
deb8dacc | 4847 | |
b254da66 | 4848 | -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1)); |
deb8dacc | 4849 | |
b254da66 AC |
4850 | -- Types derived from [Limited_]Controlled are the only |
4851 | -- ones considered since they have fields Prev and Next. | |
4852 | ||
e0c32166 AC |
4853 | if VM_Target /= No_VM then |
4854 | if Is_Controlled (T) then | |
4855 | Insert_Action (N, | |
4856 | Make_Attach_Call | |
4857 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
4858 | Ptr_Typ => PtrT)); | |
4859 | end if; | |
b254da66 AC |
4860 | |
4861 | -- Default case, generate: | |
4862 | ||
4863 | -- Set_Finalize_Address | |
4864 | -- (<PtrT>FM, <T>FD'Unrestricted_Access); | |
4865 | ||
5114f3ff AC |
4866 | -- Do not generate this call in CodePeer mode, as TSS |
4867 | -- primitive Finalize_Address is not created in this | |
4868 | -- mode. | |
b254da66 | 4869 | |
5114f3ff | 4870 | elsif not CodePeer_Mode then |
b254da66 AC |
4871 | Insert_Action (N, |
4872 | Make_Set_Finalize_Address_Call | |
4873 | (Loc => Loc, | |
4874 | Typ => T, | |
4875 | Ptr_Typ => PtrT)); | |
4876 | end if; | |
b4592168 | 4877 | end if; |
70482933 RK |
4878 | end if; |
4879 | ||
e4494292 | 4880 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
b4592168 GD |
4881 | Analyze_And_Resolve (N, PtrT); |
4882 | end if; | |
26bff3d9 JM |
4883 | end if; |
4884 | end; | |
f82944b7 | 4885 | |
26bff3d9 JM |
4886 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
4887 | -- object that has been rewritten as a reference, we displace "this" | |
4888 | -- to reference properly its secondary dispatch table. | |
4889 | ||
533369aa | 4890 | if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then |
26bff3d9 | 4891 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
4892 | end if; |
4893 | ||
fbf5a39b AC |
4894 | exception |
4895 | when RE_Not_Available => | |
4896 | return; | |
70482933 RK |
4897 | end Expand_N_Allocator; |
4898 | ||
4899 | ----------------------- | |
4900 | -- Expand_N_And_Then -- | |
4901 | ----------------------- | |
4902 | ||
5875f8d6 AC |
4903 | procedure Expand_N_And_Then (N : Node_Id) |
4904 | renames Expand_Short_Circuit_Operator; | |
70482933 | 4905 | |
19d846a0 RD |
4906 | ------------------------------ |
4907 | -- Expand_N_Case_Expression -- | |
4908 | ------------------------------ | |
4909 | ||
4910 | procedure Expand_N_Case_Expression (N : Node_Id) is | |
4911 | Loc : constant Source_Ptr := Sloc (N); | |
4912 | Typ : constant Entity_Id := Etype (N); | |
4913 | Cstmt : Node_Id; | |
27a8f150 | 4914 | Decl : Node_Id; |
19d846a0 RD |
4915 | Tnn : Entity_Id; |
4916 | Pnn : Entity_Id; | |
4917 | Actions : List_Id; | |
4918 | Ttyp : Entity_Id; | |
4919 | Alt : Node_Id; | |
4920 | Fexp : Node_Id; | |
4921 | ||
4922 | begin | |
b6b5cca8 AC |
4923 | -- Check for MINIMIZED/ELIMINATED overflow mode |
4924 | ||
4925 | if Minimized_Eliminated_Overflow_Check (N) then | |
4b1c4f20 RD |
4926 | Apply_Arithmetic_Overflow_Check (N); |
4927 | return; | |
4928 | end if; | |
4929 | ||
19d846a0 RD |
4930 | -- We expand |
4931 | ||
4932 | -- case X is when A => AX, when B => BX ... | |
4933 | ||
4934 | -- to | |
4935 | ||
4936 | -- do | |
4937 | -- Tnn : typ; | |
4938 | -- case X is | |
4939 | -- when A => | |
4940 | -- Tnn := AX; | |
4941 | -- when B => | |
4942 | -- Tnn := BX; | |
4943 | -- ... | |
4944 | -- end case; | |
4945 | -- in Tnn end; | |
4946 | ||
4947 | -- However, this expansion is wrong for limited types, and also | |
4948 | -- wrong for unconstrained types (since the bounds may not be the | |
4949 | -- same in all branches). Furthermore it involves an extra copy | |
4950 | -- for large objects. So we take care of this by using the following | |
2492305b | 4951 | -- modified expansion for non-elementary types: |
19d846a0 RD |
4952 | |
4953 | -- do | |
4954 | -- type Pnn is access all typ; | |
4955 | -- Tnn : Pnn; | |
4956 | -- case X is | |
4957 | -- when A => | |
4958 | -- T := AX'Unrestricted_Access; | |
4959 | -- when B => | |
4960 | -- T := BX'Unrestricted_Access; | |
4961 | -- ... | |
4962 | -- end case; | |
4963 | -- in Tnn.all end; | |
4964 | ||
4965 | Cstmt := | |
4966 | Make_Case_Statement (Loc, | |
4967 | Expression => Expression (N), | |
4968 | Alternatives => New_List); | |
4969 | ||
4970 | Actions := New_List; | |
4971 | ||
4972 | -- Scalar case | |
4973 | ||
2492305b | 4974 | if Is_Elementary_Type (Typ) then |
19d846a0 RD |
4975 | Ttyp := Typ; |
4976 | ||
4977 | else | |
4978 | Pnn := Make_Temporary (Loc, 'P'); | |
4979 | Append_To (Actions, | |
4980 | Make_Full_Type_Declaration (Loc, | |
4981 | Defining_Identifier => Pnn, | |
11d59a86 | 4982 | Type_Definition => |
19d846a0 | 4983 | Make_Access_To_Object_Definition (Loc, |
11d59a86 | 4984 | All_Present => True, |
e4494292 | 4985 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); |
19d846a0 RD |
4986 | Ttyp := Pnn; |
4987 | end if; | |
4988 | ||
4989 | Tnn := Make_Temporary (Loc, 'T'); | |
27a8f150 AC |
4990 | |
4991 | -- Create declaration for target of expression, and indicate that it | |
4992 | -- does not require initialization. | |
4993 | ||
11d59a86 AC |
4994 | Decl := |
4995 | Make_Object_Declaration (Loc, | |
19d846a0 | 4996 | Defining_Identifier => Tnn, |
27a8f150 AC |
4997 | Object_Definition => New_Occurrence_Of (Ttyp, Loc)); |
4998 | Set_No_Initialization (Decl); | |
4999 | Append_To (Actions, Decl); | |
19d846a0 RD |
5000 | |
5001 | -- Now process the alternatives | |
5002 | ||
5003 | Alt := First (Alternatives (N)); | |
5004 | while Present (Alt) loop | |
5005 | declare | |
eaed0c37 AC |
5006 | Aexp : Node_Id := Expression (Alt); |
5007 | Aloc : constant Source_Ptr := Sloc (Aexp); | |
5008 | Stats : List_Id; | |
19d846a0 RD |
5009 | |
5010 | begin | |
eaed0c37 AC |
5011 | -- As described above, take Unrestricted_Access for case of non- |
5012 | -- scalar types, to avoid big copies, and special cases. | |
05dbd302 | 5013 | |
2492305b | 5014 | if not Is_Elementary_Type (Typ) then |
19d846a0 RD |
5015 | Aexp := |
5016 | Make_Attribute_Reference (Aloc, | |
5017 | Prefix => Relocate_Node (Aexp), | |
5018 | Attribute_Name => Name_Unrestricted_Access); | |
5019 | end if; | |
5020 | ||
eaed0c37 AC |
5021 | Stats := New_List ( |
5022 | Make_Assignment_Statement (Aloc, | |
5023 | Name => New_Occurrence_Of (Tnn, Loc), | |
5024 | Expression => Aexp)); | |
5025 | ||
5026 | -- Propagate declarations inserted in the node by Insert_Actions | |
5027 | -- (for example, temporaries generated to remove side effects). | |
5028 | -- These actions must remain attached to the alternative, given | |
5029 | -- that they are generated by the corresponding expression. | |
5030 | ||
5031 | if Present (Sinfo.Actions (Alt)) then | |
5032 | Prepend_List (Sinfo.Actions (Alt), Stats); | |
5033 | end if; | |
5034 | ||
19d846a0 RD |
5035 | Append_To |
5036 | (Alternatives (Cstmt), | |
5037 | Make_Case_Statement_Alternative (Sloc (Alt), | |
5038 | Discrete_Choices => Discrete_Choices (Alt), | |
eaed0c37 | 5039 | Statements => Stats)); |
19d846a0 RD |
5040 | end; |
5041 | ||
5042 | Next (Alt); | |
5043 | end loop; | |
5044 | ||
5045 | Append_To (Actions, Cstmt); | |
5046 | ||
5047 | -- Construct and return final expression with actions | |
5048 | ||
2492305b | 5049 | if Is_Elementary_Type (Typ) then |
19d846a0 RD |
5050 | Fexp := New_Occurrence_Of (Tnn, Loc); |
5051 | else | |
5052 | Fexp := | |
5053 | Make_Explicit_Dereference (Loc, | |
5054 | Prefix => New_Occurrence_Of (Tnn, Loc)); | |
5055 | end if; | |
5056 | ||
5057 | Rewrite (N, | |
5058 | Make_Expression_With_Actions (Loc, | |
5059 | Expression => Fexp, | |
5060 | Actions => Actions)); | |
5061 | ||
5062 | Analyze_And_Resolve (N, Typ); | |
5063 | end Expand_N_Case_Expression; | |
5064 | ||
9b16cb57 RD |
5065 | ----------------------------------- |
5066 | -- Expand_N_Explicit_Dereference -- | |
5067 | ----------------------------------- | |
5068 | ||
5069 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
5070 | begin | |
5071 | -- Insert explicit dereference call for the checked storage pool case | |
5072 | ||
5073 | Insert_Dereference_Action (Prefix (N)); | |
5074 | ||
5075 | -- If the type is an Atomic type for which Atomic_Sync is enabled, then | |
5076 | -- we set the atomic sync flag. | |
5077 | ||
5078 | if Is_Atomic (Etype (N)) | |
5079 | and then not Atomic_Synchronization_Disabled (Etype (N)) | |
5080 | then | |
5081 | Activate_Atomic_Synchronization (N); | |
5082 | end if; | |
5083 | end Expand_N_Explicit_Dereference; | |
5084 | ||
5085 | -------------------------------------- | |
5086 | -- Expand_N_Expression_With_Actions -- | |
5087 | -------------------------------------- | |
5088 | ||
5089 | procedure Expand_N_Expression_With_Actions (N : Node_Id) is | |
3a845e07 | 5090 | |
4c7e0990 | 5091 | function Process_Action (Act : Node_Id) return Traverse_Result; |
b2c28399 AC |
5092 | -- Inspect and process a single action of an expression_with_actions for |
5093 | -- transient controlled objects. If such objects are found, the routine | |
5094 | -- generates code to clean them up when the context of the expression is | |
5095 | -- evaluated or elaborated. | |
9b16cb57 | 5096 | |
4c7e0990 AC |
5097 | -------------------- |
5098 | -- Process_Action -- | |
5099 | -------------------- | |
5100 | ||
5101 | function Process_Action (Act : Node_Id) return Traverse_Result is | |
4c7e0990 AC |
5102 | begin |
5103 | if Nkind (Act) = N_Object_Declaration | |
5104 | and then Is_Finalizable_Transient (Act, N) | |
5105 | then | |
b2c28399 AC |
5106 | Process_Transient_Object (Act, N); |
5107 | return Abandon; | |
9b16cb57 | 5108 | |
4c7e0990 AC |
5109 | -- Avoid processing temporary function results multiple times when |
5110 | -- dealing with nested expression_with_actions. | |
9b16cb57 | 5111 | |
4c7e0990 AC |
5112 | elsif Nkind (Act) = N_Expression_With_Actions then |
5113 | return Abandon; | |
5114 | ||
b2c28399 AC |
5115 | -- Do not process temporary function results in loops. This is done |
5116 | -- by Expand_N_Loop_Statement and Build_Finalizer. | |
4c7e0990 AC |
5117 | |
5118 | elsif Nkind (Act) = N_Loop_Statement then | |
5119 | return Abandon; | |
9b16cb57 RD |
5120 | end if; |
5121 | ||
4c7e0990 AC |
5122 | return OK; |
5123 | end Process_Action; | |
9b16cb57 | 5124 | |
4c7e0990 | 5125 | procedure Process_Single_Action is new Traverse_Proc (Process_Action); |
9b16cb57 RD |
5126 | |
5127 | -- Local variables | |
5128 | ||
4c7e0990 | 5129 | Act : Node_Id; |
9b16cb57 RD |
5130 | |
5131 | -- Start of processing for Expand_N_Expression_With_Actions | |
5132 | ||
5133 | begin | |
e0f63680 AC |
5134 | -- Process the actions as described above |
5135 | ||
4c7e0990 | 5136 | Act := First (Actions (N)); |
e0f63680 AC |
5137 | while Present (Act) loop |
5138 | Process_Single_Action (Act); | |
5139 | Next (Act); | |
5140 | end loop; | |
5141 | ||
ebdaa81b | 5142 | -- Deal with case where there are no actions. In this case we simply |
5a521b8a | 5143 | -- rewrite the node with its expression since we don't need the actions |
ebdaa81b AC |
5144 | -- and the specification of this node does not allow a null action list. |
5145 | ||
5a521b8a AC |
5146 | -- Note: we use Rewrite instead of Replace, because Codepeer is using |
5147 | -- the expanded tree and relying on being able to retrieve the original | |
5148 | -- tree in cases like this. This raises a whole lot of issues of whether | |
5149 | -- we have problems elsewhere, which will be addressed in the future??? | |
5150 | ||
e0f63680 | 5151 | if Is_Empty_List (Actions (N)) then |
5a521b8a | 5152 | Rewrite (N, Relocate_Node (Expression (N))); |
ebdaa81b | 5153 | end if; |
9b16cb57 RD |
5154 | end Expand_N_Expression_With_Actions; |
5155 | ||
5156 | ---------------------------- | |
5157 | -- Expand_N_If_Expression -- | |
5158 | ---------------------------- | |
70482933 | 5159 | |
4b985e20 | 5160 | -- Deal with limited types and condition actions |
70482933 | 5161 | |
9b16cb57 | 5162 | procedure Expand_N_If_Expression (N : Node_Id) is |
b2c28399 AC |
5163 | procedure Process_Actions (Actions : List_Id); |
5164 | -- Inspect and process a single action list of an if expression for | |
5165 | -- transient controlled objects. If such objects are found, the routine | |
5166 | -- generates code to clean them up when the context of the expression is | |
5167 | -- evaluated or elaborated. | |
3cebd1c0 | 5168 | |
b2c28399 AC |
5169 | --------------------- |
5170 | -- Process_Actions -- | |
5171 | --------------------- | |
3cebd1c0 | 5172 | |
b2c28399 AC |
5173 | procedure Process_Actions (Actions : List_Id) is |
5174 | Act : Node_Id; | |
3cebd1c0 AC |
5175 | |
5176 | begin | |
b2c28399 AC |
5177 | Act := First (Actions); |
5178 | while Present (Act) loop | |
5179 | if Nkind (Act) = N_Object_Declaration | |
5180 | and then Is_Finalizable_Transient (Act, N) | |
5181 | then | |
5182 | Process_Transient_Object (Act, N); | |
5183 | end if; | |
3cebd1c0 | 5184 | |
b2c28399 AC |
5185 | Next (Act); |
5186 | end loop; | |
5187 | end Process_Actions; | |
3cebd1c0 AC |
5188 | |
5189 | -- Local variables | |
5190 | ||
70482933 RK |
5191 | Loc : constant Source_Ptr := Sloc (N); |
5192 | Cond : constant Node_Id := First (Expressions (N)); | |
5193 | Thenx : constant Node_Id := Next (Cond); | |
5194 | Elsex : constant Node_Id := Next (Thenx); | |
5195 | Typ : constant Entity_Id := Etype (N); | |
c471e2da | 5196 | |
3cebd1c0 | 5197 | Actions : List_Id; |
602a7ec0 AC |
5198 | Cnn : Entity_Id; |
5199 | Decl : Node_Id; | |
3cebd1c0 | 5200 | Expr : Node_Id; |
602a7ec0 AC |
5201 | New_If : Node_Id; |
5202 | New_N : Node_Id; | |
b2c28399 | 5203 | Ptr_Typ : Entity_Id; |
70482933 | 5204 | |
a53c5613 AC |
5205 | -- Start of processing for Expand_N_If_Expression |
5206 | ||
70482933 | 5207 | begin |
b6b5cca8 AC |
5208 | -- Check for MINIMIZED/ELIMINATED overflow mode |
5209 | ||
5210 | if Minimized_Eliminated_Overflow_Check (N) then | |
5211 | Apply_Arithmetic_Overflow_Check (N); | |
5212 | return; | |
5213 | end if; | |
5214 | ||
602a7ec0 | 5215 | -- Fold at compile time if condition known. We have already folded |
9b16cb57 RD |
5216 | -- static if expressions, but it is possible to fold any case in which |
5217 | -- the condition is known at compile time, even though the result is | |
5218 | -- non-static. | |
602a7ec0 AC |
5219 | |
5220 | -- Note that we don't do the fold of such cases in Sem_Elab because | |
5221 | -- it can cause infinite loops with the expander adding a conditional | |
5222 | -- expression, and Sem_Elab circuitry removing it repeatedly. | |
5223 | ||
5224 | if Compile_Time_Known_Value (Cond) then | |
5225 | if Is_True (Expr_Value (Cond)) then | |
5226 | Expr := Thenx; | |
5227 | Actions := Then_Actions (N); | |
5228 | else | |
5229 | Expr := Elsex; | |
5230 | Actions := Else_Actions (N); | |
5231 | end if; | |
5232 | ||
5233 | Remove (Expr); | |
ae77c68b AC |
5234 | |
5235 | if Present (Actions) then | |
ae77c68b AC |
5236 | Rewrite (N, |
5237 | Make_Expression_With_Actions (Loc, | |
5238 | Expression => Relocate_Node (Expr), | |
5239 | Actions => Actions)); | |
5240 | Analyze_And_Resolve (N, Typ); | |
ae77c68b AC |
5241 | else |
5242 | Rewrite (N, Relocate_Node (Expr)); | |
5243 | end if; | |
602a7ec0 AC |
5244 | |
5245 | -- Note that the result is never static (legitimate cases of static | |
9b16cb57 | 5246 | -- if expressions were folded in Sem_Eval). |
602a7ec0 AC |
5247 | |
5248 | Set_Is_Static_Expression (N, False); | |
5249 | return; | |
5250 | end if; | |
5251 | ||
305caf42 AC |
5252 | -- If the type is limited or unconstrained, we expand as follows to |
5253 | -- avoid any possibility of improper copies. | |
70482933 | 5254 | |
305caf42 AC |
5255 | -- Note: it may be possible to avoid this special processing if the |
5256 | -- back end uses its own mechanisms for handling by-reference types ??? | |
ac7120ce | 5257 | |
c471e2da AC |
5258 | -- type Ptr is access all Typ; |
5259 | -- Cnn : Ptr; | |
ac7120ce RD |
5260 | -- if cond then |
5261 | -- <<then actions>> | |
5262 | -- Cnn := then-expr'Unrestricted_Access; | |
5263 | -- else | |
5264 | -- <<else actions>> | |
5265 | -- Cnn := else-expr'Unrestricted_Access; | |
5266 | -- end if; | |
5267 | ||
9b16cb57 | 5268 | -- and replace the if expression by a reference to Cnn.all. |
ac7120ce | 5269 | |
305caf42 AC |
5270 | -- This special case can be skipped if the back end handles limited |
5271 | -- types properly and ensures that no incorrect copies are made. | |
5272 | ||
5273 | if Is_By_Reference_Type (Typ) | |
5274 | and then not Back_End_Handles_Limited_Types | |
5275 | then | |
b2c28399 AC |
5276 | -- When the "then" or "else" expressions involve controlled function |
5277 | -- calls, generated temporaries are chained on the corresponding list | |
5278 | -- of actions. These temporaries need to be finalized after the if | |
5279 | -- expression is evaluated. | |
3cebd1c0 | 5280 | |
b2c28399 AC |
5281 | Process_Actions (Then_Actions (N)); |
5282 | Process_Actions (Else_Actions (N)); | |
3cebd1c0 | 5283 | |
b2c28399 AC |
5284 | -- Generate: |
5285 | -- type Ann is access all Typ; | |
3cebd1c0 | 5286 | |
b2c28399 | 5287 | Ptr_Typ := Make_Temporary (Loc, 'A'); |
3cebd1c0 | 5288 | |
b2c28399 AC |
5289 | Insert_Action (N, |
5290 | Make_Full_Type_Declaration (Loc, | |
5291 | Defining_Identifier => Ptr_Typ, | |
5292 | Type_Definition => | |
5293 | Make_Access_To_Object_Definition (Loc, | |
5294 | All_Present => True, | |
e4494292 | 5295 | Subtype_Indication => New_Occurrence_Of (Typ, Loc)))); |
3cebd1c0 | 5296 | |
b2c28399 AC |
5297 | -- Generate: |
5298 | -- Cnn : Ann; | |
3cebd1c0 | 5299 | |
b2c28399 | 5300 | Cnn := Make_Temporary (Loc, 'C', N); |
3cebd1c0 | 5301 | |
b2c28399 AC |
5302 | Decl := |
5303 | Make_Object_Declaration (Loc, | |
5304 | Defining_Identifier => Cnn, | |
5305 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc)); | |
3cebd1c0 | 5306 | |
b2c28399 AC |
5307 | -- Generate: |
5308 | -- if Cond then | |
5309 | -- Cnn := <Thenx>'Unrestricted_Access; | |
5310 | -- else | |
5311 | -- Cnn := <Elsex>'Unrestricted_Access; | |
5312 | -- end if; | |
3cebd1c0 | 5313 | |
b2c28399 AC |
5314 | New_If := |
5315 | Make_Implicit_If_Statement (N, | |
5316 | Condition => Relocate_Node (Cond), | |
5317 | Then_Statements => New_List ( | |
5318 | Make_Assignment_Statement (Sloc (Thenx), | |
e4494292 | 5319 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), |
b2c28399 AC |
5320 | Expression => |
5321 | Make_Attribute_Reference (Loc, | |
5322 | Prefix => Relocate_Node (Thenx), | |
5323 | Attribute_Name => Name_Unrestricted_Access))), | |
3cebd1c0 | 5324 | |
b2c28399 AC |
5325 | Else_Statements => New_List ( |
5326 | Make_Assignment_Statement (Sloc (Elsex), | |
e4494292 | 5327 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), |
b2c28399 AC |
5328 | Expression => |
5329 | Make_Attribute_Reference (Loc, | |
5330 | Prefix => Relocate_Node (Elsex), | |
5331 | Attribute_Name => Name_Unrestricted_Access)))); | |
3cebd1c0 AC |
5332 | |
5333 | New_N := | |
5334 | Make_Explicit_Dereference (Loc, | |
5335 | Prefix => New_Occurrence_Of (Cnn, Loc)); | |
fb1949a0 | 5336 | |
c471e2da AC |
5337 | -- For other types, we only need to expand if there are other actions |
5338 | -- associated with either branch. | |
5339 | ||
5340 | elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
c471e2da | 5341 | |
0812b84e | 5342 | -- We now wrap the actions into the appropriate expression |
fb1949a0 | 5343 | |
0812b84e AC |
5344 | if Present (Then_Actions (N)) then |
5345 | Rewrite (Thenx, | |
b2c28399 AC |
5346 | Make_Expression_With_Actions (Sloc (Thenx), |
5347 | Actions => Then_Actions (N), | |
5348 | Expression => Relocate_Node (Thenx))); | |
5349 | ||
0812b84e AC |
5350 | Set_Then_Actions (N, No_List); |
5351 | Analyze_And_Resolve (Thenx, Typ); | |
5352 | end if; | |
305caf42 | 5353 | |
0812b84e AC |
5354 | if Present (Else_Actions (N)) then |
5355 | Rewrite (Elsex, | |
b2c28399 AC |
5356 | Make_Expression_With_Actions (Sloc (Elsex), |
5357 | Actions => Else_Actions (N), | |
5358 | Expression => Relocate_Node (Elsex))); | |
5359 | ||
0812b84e AC |
5360 | Set_Else_Actions (N, No_List); |
5361 | Analyze_And_Resolve (Elsex, Typ); | |
305caf42 AC |
5362 | end if; |
5363 | ||
0812b84e AC |
5364 | return; |
5365 | ||
b2c28399 AC |
5366 | -- If no actions then no expansion needed, gigi will handle it using the |
5367 | -- same approach as a C conditional expression. | |
305caf42 AC |
5368 | |
5369 | else | |
c471e2da AC |
5370 | return; |
5371 | end if; | |
5372 | ||
305caf42 AC |
5373 | -- Fall through here for either the limited expansion, or the case of |
5374 | -- inserting actions for non-limited types. In both these cases, we must | |
5375 | -- move the SLOC of the parent If statement to the newly created one and | |
3fc5d116 RD |
5376 | -- change it to the SLOC of the expression which, after expansion, will |
5377 | -- correspond to what is being evaluated. | |
c471e2da | 5378 | |
533369aa | 5379 | if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then |
c471e2da AC |
5380 | Set_Sloc (New_If, Sloc (Parent (N))); |
5381 | Set_Sloc (Parent (N), Loc); | |
5382 | end if; | |
70482933 | 5383 | |
3fc5d116 RD |
5384 | -- Make sure Then_Actions and Else_Actions are appropriately moved |
5385 | -- to the new if statement. | |
5386 | ||
c471e2da AC |
5387 | if Present (Then_Actions (N)) then |
5388 | Insert_List_Before | |
5389 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
70482933 | 5390 | end if; |
c471e2da AC |
5391 | |
5392 | if Present (Else_Actions (N)) then | |
5393 | Insert_List_Before | |
5394 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
5395 | end if; | |
5396 | ||
5397 | Insert_Action (N, Decl); | |
5398 | Insert_Action (N, New_If); | |
5399 | Rewrite (N, New_N); | |
5400 | Analyze_And_Resolve (N, Typ); | |
9b16cb57 | 5401 | end Expand_N_If_Expression; |
35a1c212 | 5402 | |
70482933 RK |
5403 | ----------------- |
5404 | -- Expand_N_In -- | |
5405 | ----------------- | |
5406 | ||
5407 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 | 5408 | Loc : constant Source_Ptr := Sloc (N); |
4818e7b9 | 5409 | Restyp : constant Entity_Id := Etype (N); |
7324bf49 AC |
5410 | Lop : constant Node_Id := Left_Opnd (N); |
5411 | Rop : constant Node_Id := Right_Opnd (N); | |
5412 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 5413 | |
4818e7b9 RD |
5414 | Ltyp : Entity_Id; |
5415 | Rtyp : Entity_Id; | |
5416 | ||
630d30e9 RD |
5417 | procedure Substitute_Valid_Check; |
5418 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
5419 | -- test for the left operand being in range of its subtype. | |
5420 | ||
5421 | ---------------------------- | |
5422 | -- Substitute_Valid_Check -- | |
5423 | ---------------------------- | |
5424 | ||
5425 | procedure Substitute_Valid_Check is | |
5426 | begin | |
c7532b2d AC |
5427 | Rewrite (N, |
5428 | Make_Attribute_Reference (Loc, | |
5429 | Prefix => Relocate_Node (Lop), | |
5430 | Attribute_Name => Name_Valid)); | |
630d30e9 | 5431 | |
c7532b2d | 5432 | Analyze_And_Resolve (N, Restyp); |
630d30e9 | 5433 | |
acad3c0a AC |
5434 | -- Give warning unless overflow checking is MINIMIZED or ELIMINATED, |
5435 | -- in which case, this usage makes sense, and in any case, we have | |
5436 | -- actually eliminated the danger of optimization above. | |
5437 | ||
a7f1b24f | 5438 | if Overflow_Check_Mode not in Minimized_Or_Eliminated then |
324ac540 AC |
5439 | Error_Msg_N |
5440 | ("??explicit membership test may be optimized away", N); | |
acad3c0a | 5441 | Error_Msg_N -- CODEFIX |
324ac540 | 5442 | ("\??use ''Valid attribute instead", N); |
acad3c0a AC |
5443 | end if; |
5444 | ||
c7532b2d | 5445 | return; |
630d30e9 RD |
5446 | end Substitute_Valid_Check; |
5447 | ||
5448 | -- Start of processing for Expand_N_In | |
5449 | ||
70482933 | 5450 | begin |
308e6f3a | 5451 | -- If set membership case, expand with separate procedure |
4818e7b9 | 5452 | |
197e4514 | 5453 | if Present (Alternatives (N)) then |
a3068ca6 | 5454 | Expand_Set_Membership (N); |
197e4514 AC |
5455 | return; |
5456 | end if; | |
5457 | ||
4818e7b9 RD |
5458 | -- Not set membership, proceed with expansion |
5459 | ||
5460 | Ltyp := Etype (Left_Opnd (N)); | |
5461 | Rtyp := Etype (Right_Opnd (N)); | |
5462 | ||
5707e389 | 5463 | -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer |
f6194278 RD |
5464 | -- type, then expand with a separate procedure. Note the use of the |
5465 | -- flag No_Minimize_Eliminate to prevent infinite recursion. | |
5466 | ||
a7f1b24f | 5467 | if Overflow_Check_Mode in Minimized_Or_Eliminated |
f6194278 RD |
5468 | and then Is_Signed_Integer_Type (Ltyp) |
5469 | and then not No_Minimize_Eliminate (N) | |
5470 | then | |
5471 | Expand_Membership_Minimize_Eliminate_Overflow (N); | |
5472 | return; | |
5473 | end if; | |
5474 | ||
630d30e9 RD |
5475 | -- Check case of explicit test for an expression in range of its |
5476 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
b6b5cca8 | 5477 | -- test and give a warning for scalar types. |
630d30e9 | 5478 | |
4818e7b9 | 5479 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
5480 | |
5481 | -- Only relevant for source comparisons | |
5482 | ||
5483 | and then Comes_From_Source (N) | |
5484 | ||
5485 | -- In floating-point this is a standard way to check for finite values | |
5486 | -- and using 'Valid would typically be a pessimization. | |
5487 | ||
4818e7b9 | 5488 | and then not Is_Floating_Point_Type (Ltyp) |
b6b5cca8 AC |
5489 | |
5490 | -- Don't give the message unless right operand is a type entity and | |
5491 | -- the type of the left operand matches this type. Note that this | |
5492 | -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow | |
5493 | -- checks have changed the type of the left operand. | |
5494 | ||
630d30e9 | 5495 | and then Nkind (Rop) in N_Has_Entity |
4818e7b9 | 5496 | and then Ltyp = Entity (Rop) |
b6b5cca8 AC |
5497 | |
5498 | -- Skip in VM mode, where we have no sense of invalid values. The | |
5499 | -- warning still seems relevant, but not important enough to worry. | |
5500 | ||
26bff3d9 | 5501 | and then VM_Target = No_VM |
b6b5cca8 AC |
5502 | |
5503 | -- Skip this for predicated types, where such expressions are a | |
5504 | -- reasonable way of testing if something meets the predicate. | |
5505 | ||
3d6db7f8 | 5506 | and then not Present (Predicate_Function (Ltyp)) |
630d30e9 RD |
5507 | then |
5508 | Substitute_Valid_Check; | |
5509 | return; | |
5510 | end if; | |
5511 | ||
20b5d666 JM |
5512 | -- Do validity check on operands |
5513 | ||
5514 | if Validity_Checks_On and Validity_Check_Operands then | |
5515 | Ensure_Valid (Left_Opnd (N)); | |
5516 | Validity_Check_Range (Right_Opnd (N)); | |
5517 | end if; | |
5518 | ||
630d30e9 | 5519 | -- Case of explicit range |
fbf5a39b AC |
5520 | |
5521 | if Nkind (Rop) = N_Range then | |
5522 | declare | |
630d30e9 RD |
5523 | Lo : constant Node_Id := Low_Bound (Rop); |
5524 | Hi : constant Node_Id := High_Bound (Rop); | |
5525 | ||
5526 | Lo_Orig : constant Node_Id := Original_Node (Lo); | |
5527 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
5528 | ||
c800f862 RD |
5529 | Lcheck : Compare_Result; |
5530 | Ucheck : Compare_Result; | |
fbf5a39b | 5531 | |
d766cee3 RD |
5532 | Warn1 : constant Boolean := |
5533 | Constant_Condition_Warnings | |
c800f862 RD |
5534 | and then Comes_From_Source (N) |
5535 | and then not In_Instance; | |
d766cee3 | 5536 | -- This must be true for any of the optimization warnings, we |
9a0ddeee AC |
5537 | -- clearly want to give them only for source with the flag on. We |
5538 | -- also skip these warnings in an instance since it may be the | |
5539 | -- case that different instantiations have different ranges. | |
d766cee3 RD |
5540 | |
5541 | Warn2 : constant Boolean := | |
5542 | Warn1 | |
5543 | and then Nkind (Original_Node (Rop)) = N_Range | |
5544 | and then Is_Integer_Type (Etype (Lo)); | |
5545 | -- For the case where only one bound warning is elided, we also | |
5546 | -- insist on an explicit range and an integer type. The reason is | |
5547 | -- that the use of enumeration ranges including an end point is | |
9a0ddeee AC |
5548 | -- common, as is the use of a subtype name, one of whose bounds is |
5549 | -- the same as the type of the expression. | |
d766cee3 | 5550 | |
fbf5a39b | 5551 | begin |
c95e0edc | 5552 | -- If test is explicit x'First .. x'Last, replace by valid check |
630d30e9 | 5553 | |
e606088a AC |
5554 | -- Could use some individual comments for this complex test ??? |
5555 | ||
d766cee3 | 5556 | if Is_Scalar_Type (Ltyp) |
b6b5cca8 AC |
5557 | |
5558 | -- And left operand is X'First where X matches left operand | |
5559 | -- type (this eliminates cases of type mismatch, including | |
5560 | -- the cases where ELIMINATED/MINIMIZED mode has changed the | |
5561 | -- type of the left operand. | |
5562 | ||
630d30e9 RD |
5563 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
5564 | and then Attribute_Name (Lo_Orig) = Name_First | |
5565 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 5566 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
b6b5cca8 AC |
5567 | |
5568 | -- Same tests for right operand | |
5569 | ||
630d30e9 RD |
5570 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
5571 | and then Attribute_Name (Hi_Orig) = Name_Last | |
5572 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 5573 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
b6b5cca8 AC |
5574 | |
5575 | -- Relevant only for source cases | |
5576 | ||
630d30e9 | 5577 | and then Comes_From_Source (N) |
b6b5cca8 AC |
5578 | |
5579 | -- Omit for VM cases, where we don't have invalid values | |
5580 | ||
26bff3d9 | 5581 | and then VM_Target = No_VM |
630d30e9 RD |
5582 | then |
5583 | Substitute_Valid_Check; | |
4818e7b9 | 5584 | goto Leave; |
630d30e9 RD |
5585 | end if; |
5586 | ||
d766cee3 RD |
5587 | -- If bounds of type are known at compile time, and the end points |
5588 | -- are known at compile time and identical, this is another case | |
5589 | -- for substituting a valid test. We only do this for discrete | |
5590 | -- types, since it won't arise in practice for float types. | |
5591 | ||
5592 | if Comes_From_Source (N) | |
5593 | and then Is_Discrete_Type (Ltyp) | |
5594 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
5595 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
5596 | and then Compile_Time_Known_Value (Lo) | |
5597 | and then Compile_Time_Known_Value (Hi) | |
5598 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
5599 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e | 5600 | |
f6194278 RD |
5601 | -- Kill warnings in instances, since they may be cases where we |
5602 | -- have a test in the generic that makes sense with some types | |
5603 | -- and not with other types. | |
94eefd2e RD |
5604 | |
5605 | and then not In_Instance | |
d766cee3 RD |
5606 | then |
5607 | Substitute_Valid_Check; | |
4818e7b9 | 5608 | goto Leave; |
d766cee3 RD |
5609 | end if; |
5610 | ||
9a0ddeee AC |
5611 | -- If we have an explicit range, do a bit of optimization based on |
5612 | -- range analysis (we may be able to kill one or both checks). | |
630d30e9 | 5613 | |
c800f862 RD |
5614 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
5615 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
5616 | ||
630d30e9 RD |
5617 | -- If either check is known to fail, replace result by False since |
5618 | -- the other check does not matter. Preserve the static flag for | |
5619 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
5620 | |
5621 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 5622 | if Warn1 then |
685bc70f AC |
5623 | Error_Msg_N ("?c?range test optimized away", N); |
5624 | Error_Msg_N ("\?c?value is known to be out of range", N); | |
d766cee3 RD |
5625 | end if; |
5626 | ||
e4494292 | 5627 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 5628 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 5629 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 5630 | goto Leave; |
fbf5a39b | 5631 | |
685094bf RD |
5632 | -- If both checks are known to succeed, replace result by True, |
5633 | -- since we know we are in range. | |
fbf5a39b AC |
5634 | |
5635 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 5636 | if Warn1 then |
685bc70f AC |
5637 | Error_Msg_N ("?c?range test optimized away", N); |
5638 | Error_Msg_N ("\?c?value is known to be in range", N); | |
d766cee3 RD |
5639 | end if; |
5640 | ||
e4494292 | 5641 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 5642 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 5643 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 5644 | goto Leave; |
fbf5a39b | 5645 | |
d766cee3 RD |
5646 | -- If lower bound check succeeds and upper bound check is not |
5647 | -- known to succeed or fail, then replace the range check with | |
5648 | -- a comparison against the upper bound. | |
fbf5a39b AC |
5649 | |
5650 | elsif Lcheck in Compare_GE then | |
94eefd2e | 5651 | if Warn2 and then not In_Instance then |
324ac540 AC |
5652 | Error_Msg_N ("??lower bound test optimized away", Lo); |
5653 | Error_Msg_N ("\??value is known to be in range", Lo); | |
d766cee3 RD |
5654 | end if; |
5655 | ||
fbf5a39b AC |
5656 | Rewrite (N, |
5657 | Make_Op_Le (Loc, | |
5658 | Left_Opnd => Lop, | |
5659 | Right_Opnd => High_Bound (Rop))); | |
4818e7b9 RD |
5660 | Analyze_And_Resolve (N, Restyp); |
5661 | goto Leave; | |
fbf5a39b | 5662 | |
d766cee3 RD |
5663 | -- If upper bound check succeeds and lower bound check is not |
5664 | -- known to succeed or fail, then replace the range check with | |
5665 | -- a comparison against the lower bound. | |
fbf5a39b AC |
5666 | |
5667 | elsif Ucheck in Compare_LE then | |
94eefd2e | 5668 | if Warn2 and then not In_Instance then |
324ac540 AC |
5669 | Error_Msg_N ("??upper bound test optimized away", Hi); |
5670 | Error_Msg_N ("\??value is known to be in range", Hi); | |
d766cee3 RD |
5671 | end if; |
5672 | ||
fbf5a39b AC |
5673 | Rewrite (N, |
5674 | Make_Op_Ge (Loc, | |
5675 | Left_Opnd => Lop, | |
5676 | Right_Opnd => Low_Bound (Rop))); | |
4818e7b9 RD |
5677 | Analyze_And_Resolve (N, Restyp); |
5678 | goto Leave; | |
fbf5a39b | 5679 | end if; |
c800f862 RD |
5680 | |
5681 | -- We couldn't optimize away the range check, but there is one | |
5682 | -- more issue. If we are checking constant conditionals, then we | |
5683 | -- see if we can determine the outcome assuming everything is | |
5684 | -- valid, and if so give an appropriate warning. | |
5685 | ||
5686 | if Warn1 and then not Assume_No_Invalid_Values then | |
5687 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
5688 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
5689 | ||
5690 | -- Result is out of range for valid value | |
5691 | ||
5692 | if Lcheck = LT or else Ucheck = GT then | |
ed2233dc | 5693 | Error_Msg_N |
685bc70f | 5694 | ("?c?value can only be in range if it is invalid", N); |
c800f862 RD |
5695 | |
5696 | -- Result is in range for valid value | |
5697 | ||
5698 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
ed2233dc | 5699 | Error_Msg_N |
685bc70f | 5700 | ("?c?value can only be out of range if it is invalid", N); |
c800f862 RD |
5701 | |
5702 | -- Lower bound check succeeds if value is valid | |
5703 | ||
5704 | elsif Warn2 and then Lcheck in Compare_GE then | |
ed2233dc | 5705 | Error_Msg_N |
685bc70f | 5706 | ("?c?lower bound check only fails if it is invalid", Lo); |
c800f862 RD |
5707 | |
5708 | -- Upper bound check succeeds if value is valid | |
5709 | ||
5710 | elsif Warn2 and then Ucheck in Compare_LE then | |
ed2233dc | 5711 | Error_Msg_N |
685bc70f | 5712 | ("?c?upper bound check only fails for invalid values", Hi); |
c800f862 RD |
5713 | end if; |
5714 | end if; | |
fbf5a39b AC |
5715 | end; |
5716 | ||
5717 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 5718 | |
4818e7b9 | 5719 | goto Leave; |
70482933 RK |
5720 | |
5721 | -- Here right operand is a subtype mark | |
5722 | ||
5723 | else | |
5724 | declare | |
82878151 AC |
5725 | Typ : Entity_Id := Etype (Rop); |
5726 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
5727 | Cond : Node_Id := Empty; | |
5728 | New_N : Node_Id; | |
5729 | Obj : Node_Id := Lop; | |
5730 | SCIL_Node : Node_Id; | |
70482933 RK |
5731 | |
5732 | begin | |
5733 | Remove_Side_Effects (Obj); | |
5734 | ||
5735 | -- For tagged type, do tagged membership operation | |
5736 | ||
5737 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 5738 | |
26bff3d9 JM |
5739 | -- No expansion will be performed when VM_Target, as the VM |
5740 | -- back-ends will handle the membership tests directly (tags | |
5741 | -- are not explicitly represented in Java objects, so the | |
5742 | -- normal tagged membership expansion is not what we want). | |
70482933 | 5743 | |
1f110335 | 5744 | if Tagged_Type_Expansion then |
82878151 AC |
5745 | Tagged_Membership (N, SCIL_Node, New_N); |
5746 | Rewrite (N, New_N); | |
4818e7b9 | 5747 | Analyze_And_Resolve (N, Restyp); |
82878151 AC |
5748 | |
5749 | -- Update decoration of relocated node referenced by the | |
5750 | -- SCIL node. | |
5751 | ||
9a0ddeee | 5752 | if Generate_SCIL and then Present (SCIL_Node) then |
7665e4bd | 5753 | Set_SCIL_Node (N, SCIL_Node); |
82878151 | 5754 | end if; |
70482933 RK |
5755 | end if; |
5756 | ||
4818e7b9 | 5757 | goto Leave; |
70482933 | 5758 | |
c95e0edc | 5759 | -- If type is scalar type, rewrite as x in t'First .. t'Last. |
70482933 | 5760 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
5761 | -- type if they come from the original type definition. Also this |
5762 | -- way we get all the processing above for an explicit range. | |
70482933 | 5763 | |
f6194278 | 5764 | -- Don't do this for predicated types, since in this case we |
a90bd866 | 5765 | -- want to check the predicate. |
c0f136cd | 5766 | |
c7532b2d AC |
5767 | elsif Is_Scalar_Type (Typ) then |
5768 | if No (Predicate_Function (Typ)) then | |
5769 | Rewrite (Rop, | |
5770 | Make_Range (Loc, | |
5771 | Low_Bound => | |
5772 | Make_Attribute_Reference (Loc, | |
5773 | Attribute_Name => Name_First, | |
e4494292 | 5774 | Prefix => New_Occurrence_Of (Typ, Loc)), |
c7532b2d AC |
5775 | |
5776 | High_Bound => | |
5777 | Make_Attribute_Reference (Loc, | |
5778 | Attribute_Name => Name_Last, | |
e4494292 | 5779 | Prefix => New_Occurrence_Of (Typ, Loc)))); |
c7532b2d AC |
5780 | Analyze_And_Resolve (N, Restyp); |
5781 | end if; | |
70482933 | 5782 | |
4818e7b9 | 5783 | goto Leave; |
5d09245e AC |
5784 | |
5785 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
5786 | -- a membership test if the subtype mark denotes a constrained | |
5787 | -- Unchecked_Union subtype and the expression lacks inferable | |
5788 | -- discriminants. | |
5789 | ||
5790 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
5791 | and then Is_Constrained (Typ) | |
5792 | and then not Has_Inferable_Discriminants (Lop) | |
5793 | then | |
5794 | Insert_Action (N, | |
5795 | Make_Raise_Program_Error (Loc, | |
5796 | Reason => PE_Unchecked_Union_Restriction)); | |
5797 | ||
9a0ddeee | 5798 | -- Prevent Gigi from generating incorrect code by rewriting the |
f6194278 | 5799 | -- test as False. What is this undocumented thing about ??? |
5d09245e | 5800 | |
9a0ddeee | 5801 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 5802 | goto Leave; |
70482933 RK |
5803 | end if; |
5804 | ||
fbf5a39b AC |
5805 | -- Here we have a non-scalar type |
5806 | ||
70482933 RK |
5807 | if Is_Acc then |
5808 | Typ := Designated_Type (Typ); | |
5809 | end if; | |
5810 | ||
5811 | if not Is_Constrained (Typ) then | |
e4494292 | 5812 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); |
4818e7b9 | 5813 | Analyze_And_Resolve (N, Restyp); |
70482933 | 5814 | |
685094bf RD |
5815 | -- For the constrained array case, we have to check the subscripts |
5816 | -- for an exact match if the lengths are non-zero (the lengths | |
5817 | -- must match in any case). | |
70482933 RK |
5818 | |
5819 | elsif Is_Array_Type (Typ) then | |
fbf5a39b | 5820 | Check_Subscripts : declare |
9a0ddeee | 5821 | function Build_Attribute_Reference |
2e071734 AC |
5822 | (E : Node_Id; |
5823 | Nam : Name_Id; | |
5824 | Dim : Nat) return Node_Id; | |
9a0ddeee | 5825 | -- Build attribute reference E'Nam (Dim) |
70482933 | 5826 | |
9a0ddeee AC |
5827 | ------------------------------- |
5828 | -- Build_Attribute_Reference -- | |
5829 | ------------------------------- | |
fbf5a39b | 5830 | |
9a0ddeee | 5831 | function Build_Attribute_Reference |
2e071734 AC |
5832 | (E : Node_Id; |
5833 | Nam : Name_Id; | |
5834 | Dim : Nat) return Node_Id | |
70482933 RK |
5835 | is |
5836 | begin | |
5837 | return | |
5838 | Make_Attribute_Reference (Loc, | |
9a0ddeee | 5839 | Prefix => E, |
70482933 | 5840 | Attribute_Name => Nam, |
9a0ddeee | 5841 | Expressions => New_List ( |
70482933 | 5842 | Make_Integer_Literal (Loc, Dim))); |
9a0ddeee | 5843 | end Build_Attribute_Reference; |
70482933 | 5844 | |
fad0600d | 5845 | -- Start of processing for Check_Subscripts |
fbf5a39b | 5846 | |
70482933 RK |
5847 | begin |
5848 | for J in 1 .. Number_Dimensions (Typ) loop | |
5849 | Evolve_And_Then (Cond, | |
5850 | Make_Op_Eq (Loc, | |
5851 | Left_Opnd => | |
9a0ddeee | 5852 | Build_Attribute_Reference |
fbf5a39b AC |
5853 | (Duplicate_Subexpr_No_Checks (Obj), |
5854 | Name_First, J), | |
70482933 | 5855 | Right_Opnd => |
9a0ddeee | 5856 | Build_Attribute_Reference |
70482933 RK |
5857 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); |
5858 | ||
5859 | Evolve_And_Then (Cond, | |
5860 | Make_Op_Eq (Loc, | |
5861 | Left_Opnd => | |
9a0ddeee | 5862 | Build_Attribute_Reference |
fbf5a39b AC |
5863 | (Duplicate_Subexpr_No_Checks (Obj), |
5864 | Name_Last, J), | |
70482933 | 5865 | Right_Opnd => |
9a0ddeee | 5866 | Build_Attribute_Reference |
70482933 RK |
5867 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); |
5868 | end loop; | |
5869 | ||
5870 | if Is_Acc then | |
fbf5a39b AC |
5871 | Cond := |
5872 | Make_Or_Else (Loc, | |
5873 | Left_Opnd => | |
5874 | Make_Op_Eq (Loc, | |
5875 | Left_Opnd => Obj, | |
5876 | Right_Opnd => Make_Null (Loc)), | |
5877 | Right_Opnd => Cond); | |
70482933 RK |
5878 | end if; |
5879 | ||
5880 | Rewrite (N, Cond); | |
4818e7b9 | 5881 | Analyze_And_Resolve (N, Restyp); |
fbf5a39b | 5882 | end Check_Subscripts; |
70482933 | 5883 | |
685094bf RD |
5884 | -- These are the cases where constraint checks may be required, |
5885 | -- e.g. records with possible discriminants | |
70482933 RK |
5886 | |
5887 | else | |
5888 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
5889 | -- The expression that is built is the negation of the one that |
5890 | -- is used for checking discriminant constraints. | |
70482933 RK |
5891 | |
5892 | Obj := Relocate_Node (Left_Opnd (N)); | |
5893 | ||
5894 | if Has_Discriminants (Typ) then | |
5895 | Cond := Make_Op_Not (Loc, | |
5896 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
5897 | ||
5898 | if Is_Acc then | |
5899 | Cond := Make_Or_Else (Loc, | |
5900 | Left_Opnd => | |
5901 | Make_Op_Eq (Loc, | |
5902 | Left_Opnd => Obj, | |
5903 | Right_Opnd => Make_Null (Loc)), | |
5904 | Right_Opnd => Cond); | |
5905 | end if; | |
5906 | ||
5907 | else | |
5908 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
5909 | end if; | |
5910 | ||
5911 | Rewrite (N, Cond); | |
4818e7b9 | 5912 | Analyze_And_Resolve (N, Restyp); |
70482933 | 5913 | end if; |
6cce2156 GD |
5914 | |
5915 | -- Ada 2012 (AI05-0149): Handle membership tests applied to an | |
5916 | -- expression of an anonymous access type. This can involve an | |
5917 | -- accessibility test and a tagged type membership test in the | |
5918 | -- case of tagged designated types. | |
5919 | ||
5920 | if Ada_Version >= Ada_2012 | |
5921 | and then Is_Acc | |
5922 | and then Ekind (Ltyp) = E_Anonymous_Access_Type | |
5923 | then | |
5924 | declare | |
5925 | Expr_Entity : Entity_Id := Empty; | |
5926 | New_N : Node_Id; | |
5927 | Param_Level : Node_Id; | |
5928 | Type_Level : Node_Id; | |
996c8821 | 5929 | |
6cce2156 GD |
5930 | begin |
5931 | if Is_Entity_Name (Lop) then | |
5932 | Expr_Entity := Param_Entity (Lop); | |
996c8821 | 5933 | |
6cce2156 GD |
5934 | if not Present (Expr_Entity) then |
5935 | Expr_Entity := Entity (Lop); | |
5936 | end if; | |
5937 | end if; | |
5938 | ||
5939 | -- If a conversion of the anonymous access value to the | |
5940 | -- tested type would be illegal, then the result is False. | |
5941 | ||
5942 | if not Valid_Conversion | |
5943 | (Lop, Rtyp, Lop, Report_Errs => False) | |
5944 | then | |
5945 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
5946 | Analyze_And_Resolve (N, Restyp); | |
5947 | ||
5948 | -- Apply an accessibility check if the access object has an | |
5949 | -- associated access level and when the level of the type is | |
5950 | -- less deep than the level of the access parameter. This | |
5951 | -- only occur for access parameters and stand-alone objects | |
5952 | -- of an anonymous access type. | |
5953 | ||
5954 | else | |
5955 | if Present (Expr_Entity) | |
996c8821 RD |
5956 | and then |
5957 | Present | |
5958 | (Effective_Extra_Accessibility (Expr_Entity)) | |
5959 | and then UI_Gt (Object_Access_Level (Lop), | |
5960 | Type_Access_Level (Rtyp)) | |
6cce2156 GD |
5961 | then |
5962 | Param_Level := | |
5963 | New_Occurrence_Of | |
d15f9422 | 5964 | (Effective_Extra_Accessibility (Expr_Entity), Loc); |
6cce2156 GD |
5965 | |
5966 | Type_Level := | |
5967 | Make_Integer_Literal (Loc, Type_Access_Level (Rtyp)); | |
5968 | ||
5969 | -- Return True only if the accessibility level of the | |
5970 | -- expression entity is not deeper than the level of | |
5971 | -- the tested access type. | |
5972 | ||
5973 | Rewrite (N, | |
5974 | Make_And_Then (Loc, | |
5975 | Left_Opnd => Relocate_Node (N), | |
5976 | Right_Opnd => Make_Op_Le (Loc, | |
5977 | Left_Opnd => Param_Level, | |
5978 | Right_Opnd => Type_Level))); | |
5979 | ||
5980 | Analyze_And_Resolve (N); | |
5981 | end if; | |
5982 | ||
5983 | -- If the designated type is tagged, do tagged membership | |
5984 | -- operation. | |
5985 | ||
5986 | -- *** NOTE: we have to check not null before doing the | |
5987 | -- tagged membership test (but maybe that can be done | |
5988 | -- inside Tagged_Membership?). | |
5989 | ||
5990 | if Is_Tagged_Type (Typ) then | |
5991 | Rewrite (N, | |
5992 | Make_And_Then (Loc, | |
5993 | Left_Opnd => Relocate_Node (N), | |
5994 | Right_Opnd => | |
5995 | Make_Op_Ne (Loc, | |
5996 | Left_Opnd => Obj, | |
5997 | Right_Opnd => Make_Null (Loc)))); | |
5998 | ||
5999 | -- No expansion will be performed when VM_Target, as | |
6000 | -- the VM back-ends will handle the membership tests | |
6001 | -- directly (tags are not explicitly represented in | |
6002 | -- Java objects, so the normal tagged membership | |
6003 | -- expansion is not what we want). | |
6004 | ||
6005 | if Tagged_Type_Expansion then | |
6006 | ||
6007 | -- Note that we have to pass Original_Node, because | |
6008 | -- the membership test might already have been | |
6009 | -- rewritten by earlier parts of membership test. | |
6010 | ||
6011 | Tagged_Membership | |
6012 | (Original_Node (N), SCIL_Node, New_N); | |
6013 | ||
6014 | -- Update decoration of relocated node referenced | |
6015 | -- by the SCIL node. | |
6016 | ||
6017 | if Generate_SCIL and then Present (SCIL_Node) then | |
6018 | Set_SCIL_Node (New_N, SCIL_Node); | |
6019 | end if; | |
6020 | ||
6021 | Rewrite (N, | |
6022 | Make_And_Then (Loc, | |
6023 | Left_Opnd => Relocate_Node (N), | |
6024 | Right_Opnd => New_N)); | |
6025 | ||
6026 | Analyze_And_Resolve (N, Restyp); | |
6027 | end if; | |
6028 | end if; | |
6029 | end if; | |
6030 | end; | |
6031 | end if; | |
70482933 RK |
6032 | end; |
6033 | end if; | |
4818e7b9 RD |
6034 | |
6035 | -- At this point, we have done the processing required for the basic | |
6036 | -- membership test, but not yet dealt with the predicate. | |
6037 | ||
6038 | <<Leave>> | |
6039 | ||
c7532b2d AC |
6040 | -- If a predicate is present, then we do the predicate test, but we |
6041 | -- most certainly want to omit this if we are within the predicate | |
a90bd866 | 6042 | -- function itself, since otherwise we have an infinite recursion. |
3d6db7f8 GD |
6043 | -- The check should also not be emitted when testing against a range |
6044 | -- (the check is only done when the right operand is a subtype; see | |
6045 | -- RM12-4.5.2 (28.1/3-30/3)). | |
4818e7b9 | 6046 | |
c7532b2d AC |
6047 | declare |
6048 | PFunc : constant Entity_Id := Predicate_Function (Rtyp); | |
4818e7b9 | 6049 | |
c7532b2d AC |
6050 | begin |
6051 | if Present (PFunc) | |
6052 | and then Current_Scope /= PFunc | |
3d6db7f8 | 6053 | and then Nkind (Rop) /= N_Range |
c7532b2d AC |
6054 | then |
6055 | Rewrite (N, | |
6056 | Make_And_Then (Loc, | |
6057 | Left_Opnd => Relocate_Node (N), | |
fc142f63 | 6058 | Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True))); |
4818e7b9 | 6059 | |
c7532b2d | 6060 | -- Analyze new expression, mark left operand as analyzed to |
b2009d46 AC |
6061 | -- avoid infinite recursion adding predicate calls. Similarly, |
6062 | -- suppress further range checks on the call. | |
4818e7b9 | 6063 | |
c7532b2d | 6064 | Set_Analyzed (Left_Opnd (N)); |
b2009d46 | 6065 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); |
4818e7b9 | 6066 | |
c7532b2d AC |
6067 | -- All done, skip attempt at compile time determination of result |
6068 | ||
6069 | return; | |
6070 | end if; | |
6071 | end; | |
70482933 RK |
6072 | end Expand_N_In; |
6073 | ||
6074 | -------------------------------- | |
6075 | -- Expand_N_Indexed_Component -- | |
6076 | -------------------------------- | |
6077 | ||
6078 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
6079 | Loc : constant Source_Ptr := Sloc (N); | |
6080 | Typ : constant Entity_Id := Etype (N); | |
6081 | P : constant Node_Id := Prefix (N); | |
6082 | T : constant Entity_Id := Etype (P); | |
5972791c | 6083 | Atp : Entity_Id; |
70482933 RK |
6084 | |
6085 | begin | |
685094bf RD |
6086 | -- A special optimization, if we have an indexed component that is |
6087 | -- selecting from a slice, then we can eliminate the slice, since, for | |
6088 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
6089 | -- the range check required by the slice. The range check for the slice | |
6090 | -- itself has already been generated. The range check for the | |
6091 | -- subscripting operation is ensured by converting the subject to | |
6092 | -- the subtype of the slice. | |
6093 | ||
6094 | -- This optimization not only generates better code, avoiding slice | |
6095 | -- messing especially in the packed case, but more importantly bypasses | |
6096 | -- some problems in handling this peculiar case, for example, the issue | |
6097 | -- of dealing specially with object renamings. | |
70482933 RK |
6098 | |
6099 | if Nkind (P) = N_Slice then | |
6100 | Rewrite (N, | |
6101 | Make_Indexed_Component (Loc, | |
6102 | Prefix => Prefix (P), | |
6103 | Expressions => New_List ( | |
6104 | Convert_To | |
6105 | (Etype (First_Index (Etype (P))), | |
6106 | First (Expressions (N)))))); | |
6107 | Analyze_And_Resolve (N, Typ); | |
6108 | return; | |
6109 | end if; | |
6110 | ||
b4592168 GD |
6111 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
6112 | -- function, then additional actuals must be passed. | |
6113 | ||
0791fbe9 | 6114 | if Ada_Version >= Ada_2005 |
b4592168 GD |
6115 | and then Is_Build_In_Place_Function_Call (P) |
6116 | then | |
6117 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
6118 | end if; | |
6119 | ||
685094bf | 6120 | -- If the prefix is an access type, then we unconditionally rewrite if |
09494c32 | 6121 | -- as an explicit dereference. This simplifies processing for several |
685094bf RD |
6122 | -- cases, including packed array cases and certain cases in which checks |
6123 | -- must be generated. We used to try to do this only when it was | |
6124 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
6125 | |
6126 | if Is_Access_Type (T) then | |
2717634d | 6127 | Insert_Explicit_Dereference (P); |
70482933 | 6128 | Analyze_And_Resolve (P, Designated_Type (T)); |
5972791c AC |
6129 | Atp := Designated_Type (T); |
6130 | else | |
6131 | Atp := T; | |
70482933 RK |
6132 | end if; |
6133 | ||
fbf5a39b AC |
6134 | -- Generate index and validity checks |
6135 | ||
6136 | Generate_Index_Checks (N); | |
6137 | ||
70482933 RK |
6138 | if Validity_Checks_On and then Validity_Check_Subscripts then |
6139 | Apply_Subscript_Validity_Checks (N); | |
6140 | end if; | |
6141 | ||
5972791c AC |
6142 | -- If selecting from an array with atomic components, and atomic sync |
6143 | -- is not suppressed for this array type, set atomic sync flag. | |
6144 | ||
6145 | if (Has_Atomic_Components (Atp) | |
6146 | and then not Atomic_Synchronization_Disabled (Atp)) | |
6147 | or else (Is_Atomic (Typ) | |
6148 | and then not Atomic_Synchronization_Disabled (Typ)) | |
6149 | then | |
4c318253 | 6150 | Activate_Atomic_Synchronization (N); |
5972791c AC |
6151 | end if; |
6152 | ||
70482933 RK |
6153 | -- All done for the non-packed case |
6154 | ||
6155 | if not Is_Packed (Etype (Prefix (N))) then | |
6156 | return; | |
6157 | end if; | |
6158 | ||
6159 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 6160 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
6161 | -- we can always use the normal packed element get circuit. |
6162 | ||
6163 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
6164 | Expand_Packed_Element_Reference (N); | |
6165 | return; | |
6166 | end if; | |
6167 | ||
6168 | -- For a reference to a component of a bit packed array, we have to | |
6169 | -- convert it to a reference to the corresponding Packed_Array_Type. | |
6170 | -- We only want to do this for simple references, and not for: | |
6171 | ||
685094bf RD |
6172 | -- Left side of assignment, or prefix of left side of assignment, or |
6173 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
6174 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
6175 | ||
6176 | -- Renaming objects in renaming associations | |
6177 | -- This case is handled when a use of the renamed variable occurs | |
6178 | ||
6179 | -- Actual parameters for a procedure call | |
6180 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
6181 | ||
6182 | -- The second expression in a 'Read attribute reference | |
6183 | ||
47d3b920 | 6184 | -- The prefix of an address or bit or size attribute reference |
70482933 RK |
6185 | |
6186 | -- The following circuit detects these exceptions | |
6187 | ||
6188 | declare | |
6189 | Child : Node_Id := N; | |
6190 | Parnt : Node_Id := Parent (N); | |
6191 | ||
6192 | begin | |
6193 | loop | |
6194 | if Nkind (Parnt) = N_Unchecked_Expression then | |
6195 | null; | |
6196 | ||
303b4d58 AC |
6197 | elsif Nkind_In (Parnt, N_Object_Renaming_Declaration, |
6198 | N_Procedure_Call_Statement) | |
70482933 RK |
6199 | or else (Nkind (Parnt) = N_Parameter_Association |
6200 | and then | |
6201 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
6202 | then | |
6203 | return; | |
6204 | ||
6205 | elsif Nkind (Parnt) = N_Attribute_Reference | |
b69cd36a AC |
6206 | and then Nam_In (Attribute_Name (Parnt), Name_Address, |
6207 | Name_Bit, | |
6208 | Name_Size) | |
70482933 RK |
6209 | and then Prefix (Parnt) = Child |
6210 | then | |
6211 | return; | |
6212 | ||
6213 | elsif Nkind (Parnt) = N_Assignment_Statement | |
6214 | and then Name (Parnt) = Child | |
6215 | then | |
6216 | return; | |
6217 | ||
685094bf RD |
6218 | -- If the expression is an index of an indexed component, it must |
6219 | -- be expanded regardless of context. | |
fbf5a39b AC |
6220 | |
6221 | elsif Nkind (Parnt) = N_Indexed_Component | |
6222 | and then Child /= Prefix (Parnt) | |
6223 | then | |
6224 | Expand_Packed_Element_Reference (N); | |
6225 | return; | |
6226 | ||
6227 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
6228 | and then Name (Parent (Parnt)) = Parnt | |
6229 | then | |
6230 | return; | |
6231 | ||
70482933 RK |
6232 | elsif Nkind (Parnt) = N_Attribute_Reference |
6233 | and then Attribute_Name (Parnt) = Name_Read | |
6234 | and then Next (First (Expressions (Parnt))) = Child | |
6235 | then | |
6236 | return; | |
6237 | ||
303b4d58 | 6238 | elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component) |
533369aa | 6239 | and then Prefix (Parnt) = Child |
70482933 RK |
6240 | then |
6241 | null; | |
6242 | ||
6243 | else | |
6244 | Expand_Packed_Element_Reference (N); | |
6245 | return; | |
6246 | end if; | |
6247 | ||
685094bf RD |
6248 | -- Keep looking up tree for unchecked expression, or if we are the |
6249 | -- prefix of a possible assignment left side. | |
70482933 RK |
6250 | |
6251 | Child := Parnt; | |
6252 | Parnt := Parent (Child); | |
6253 | end loop; | |
6254 | end; | |
70482933 RK |
6255 | end Expand_N_Indexed_Component; |
6256 | ||
6257 | --------------------- | |
6258 | -- Expand_N_Not_In -- | |
6259 | --------------------- | |
6260 | ||
6261 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
6262 | -- can be done. This avoids needing to duplicate this expansion code. | |
6263 | ||
6264 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
6265 | Loc : constant Source_Ptr := Sloc (N); |
6266 | Typ : constant Entity_Id := Etype (N); | |
6267 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
6268 | |
6269 | begin | |
6270 | Rewrite (N, | |
6271 | Make_Op_Not (Loc, | |
6272 | Right_Opnd => | |
6273 | Make_In (Loc, | |
6274 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 6275 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 6276 | |
197e4514 AC |
6277 | -- If this is a set membership, preserve list of alternatives |
6278 | ||
6279 | Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N))); | |
6280 | ||
d766cee3 | 6281 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 6282 | -- transformations in Expand_N_In). |
630d30e9 RD |
6283 | |
6284 | Set_Comes_From_Source (N, Cfs); | |
6285 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
6286 | ||
8fc789c8 | 6287 | -- Now analyze transformed node |
630d30e9 | 6288 | |
70482933 RK |
6289 | Analyze_And_Resolve (N, Typ); |
6290 | end Expand_N_Not_In; | |
6291 | ||
6292 | ------------------- | |
6293 | -- Expand_N_Null -- | |
6294 | ------------------- | |
6295 | ||
a3f2babd AC |
6296 | -- The only replacement required is for the case of a null of a type that |
6297 | -- is an access to protected subprogram, or a subtype thereof. We represent | |
6298 | -- such access values as a record, and so we must replace the occurrence of | |
6299 | -- null by the equivalent record (with a null address and a null pointer in | |
6300 | -- it), so that the backend creates the proper value. | |
70482933 RK |
6301 | |
6302 | procedure Expand_N_Null (N : Node_Id) is | |
6303 | Loc : constant Source_Ptr := Sloc (N); | |
a3f2babd | 6304 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
70482933 RK |
6305 | Agg : Node_Id; |
6306 | ||
6307 | begin | |
26bff3d9 | 6308 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
6309 | Agg := |
6310 | Make_Aggregate (Loc, | |
6311 | Expressions => New_List ( | |
6312 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
6313 | Make_Null (Loc))); | |
6314 | ||
6315 | Rewrite (N, Agg); | |
6316 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
6317 | ||
685094bf RD |
6318 | -- For subsequent semantic analysis, the node must retain its type. |
6319 | -- Gigi in any case replaces this type by the corresponding record | |
6320 | -- type before processing the node. | |
70482933 RK |
6321 | |
6322 | Set_Etype (N, Typ); | |
6323 | end if; | |
fbf5a39b AC |
6324 | |
6325 | exception | |
6326 | when RE_Not_Available => | |
6327 | return; | |
70482933 RK |
6328 | end Expand_N_Null; |
6329 | ||
6330 | --------------------- | |
6331 | -- Expand_N_Op_Abs -- | |
6332 | --------------------- | |
6333 | ||
6334 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
6335 | Loc : constant Source_Ptr := Sloc (N); | |
6336 | Expr : constant Node_Id := Right_Opnd (N); | |
6337 | ||
6338 | begin | |
6339 | Unary_Op_Validity_Checks (N); | |
6340 | ||
b6b5cca8 AC |
6341 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6342 | ||
6343 | if Minimized_Eliminated_Overflow_Check (N) then | |
6344 | Apply_Arithmetic_Overflow_Check (N); | |
6345 | return; | |
6346 | end if; | |
6347 | ||
70482933 RK |
6348 | -- Deal with software overflow checking |
6349 | ||
07fc65c4 | 6350 | if not Backend_Overflow_Checks_On_Target |
533369aa AC |
6351 | and then Is_Signed_Integer_Type (Etype (N)) |
6352 | and then Do_Overflow_Check (N) | |
70482933 | 6353 | then |
685094bf RD |
6354 | -- The only case to worry about is when the argument is equal to the |
6355 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 6356 | |
fbf5a39b | 6357 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
6358 | |
6359 | -- with the usual Duplicate_Subexpr use coding for expr | |
6360 | ||
fbf5a39b AC |
6361 | Insert_Action (N, |
6362 | Make_Raise_Constraint_Error (Loc, | |
6363 | Condition => | |
6364 | Make_Op_Eq (Loc, | |
70482933 | 6365 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
6366 | Right_Opnd => |
6367 | Make_Attribute_Reference (Loc, | |
6368 | Prefix => | |
6369 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), | |
6370 | Attribute_Name => Name_First)), | |
6371 | Reason => CE_Overflow_Check_Failed)); | |
6372 | end if; | |
70482933 RK |
6373 | |
6374 | -- Vax floating-point types case | |
6375 | ||
fbf5a39b | 6376 | if Vax_Float (Etype (N)) then |
70482933 RK |
6377 | Expand_Vax_Arith (N); |
6378 | end if; | |
6379 | end Expand_N_Op_Abs; | |
6380 | ||
6381 | --------------------- | |
6382 | -- Expand_N_Op_Add -- | |
6383 | --------------------- | |
6384 | ||
6385 | procedure Expand_N_Op_Add (N : Node_Id) is | |
6386 | Typ : constant Entity_Id := Etype (N); | |
6387 | ||
6388 | begin | |
6389 | Binary_Op_Validity_Checks (N); | |
6390 | ||
b6b5cca8 AC |
6391 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6392 | ||
6393 | if Minimized_Eliminated_Overflow_Check (N) then | |
6394 | Apply_Arithmetic_Overflow_Check (N); | |
6395 | return; | |
6396 | end if; | |
6397 | ||
70482933 RK |
6398 | -- N + 0 = 0 + N = N for integer types |
6399 | ||
6400 | if Is_Integer_Type (Typ) then | |
6401 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
6402 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
6403 | then | |
6404 | Rewrite (N, Left_Opnd (N)); | |
6405 | return; | |
6406 | ||
6407 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
6408 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
6409 | then | |
6410 | Rewrite (N, Right_Opnd (N)); | |
6411 | return; | |
6412 | end if; | |
6413 | end if; | |
6414 | ||
fbf5a39b | 6415 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 6416 | |
761f7dcb | 6417 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 RK |
6418 | Apply_Arithmetic_Overflow_Check (N); |
6419 | return; | |
6420 | ||
6421 | -- Vax floating-point types case | |
6422 | ||
6423 | elsif Vax_Float (Typ) then | |
6424 | Expand_Vax_Arith (N); | |
6425 | end if; | |
6426 | end Expand_N_Op_Add; | |
6427 | ||
6428 | --------------------- | |
6429 | -- Expand_N_Op_And -- | |
6430 | --------------------- | |
6431 | ||
6432 | procedure Expand_N_Op_And (N : Node_Id) is | |
6433 | Typ : constant Entity_Id := Etype (N); | |
6434 | ||
6435 | begin | |
6436 | Binary_Op_Validity_Checks (N); | |
6437 | ||
6438 | if Is_Array_Type (Etype (N)) then | |
6439 | Expand_Boolean_Operator (N); | |
6440 | ||
6441 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
6442 | Adjust_Condition (Left_Opnd (N)); |
6443 | Adjust_Condition (Right_Opnd (N)); | |
6444 | Set_Etype (N, Standard_Boolean); | |
6445 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
6446 | |
6447 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
6448 | Expand_Intrinsic_Call (N, Entity (N)); | |
6449 | ||
70482933 RK |
6450 | end if; |
6451 | end Expand_N_Op_And; | |
6452 | ||
6453 | ------------------------ | |
6454 | -- Expand_N_Op_Concat -- | |
6455 | ------------------------ | |
6456 | ||
6457 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
6458 | Opnds : List_Id; |
6459 | -- List of operands to be concatenated | |
6460 | ||
70482933 | 6461 | Cnode : Node_Id; |
685094bf RD |
6462 | -- Node which is to be replaced by the result of concatenating the nodes |
6463 | -- in the list Opnds. | |
70482933 | 6464 | |
70482933 | 6465 | begin |
fbf5a39b AC |
6466 | -- Ensure validity of both operands |
6467 | ||
70482933 RK |
6468 | Binary_Op_Validity_Checks (N); |
6469 | ||
685094bf RD |
6470 | -- If we are the left operand of a concatenation higher up the tree, |
6471 | -- then do nothing for now, since we want to deal with a series of | |
6472 | -- concatenations as a unit. | |
70482933 RK |
6473 | |
6474 | if Nkind (Parent (N)) = N_Op_Concat | |
6475 | and then N = Left_Opnd (Parent (N)) | |
6476 | then | |
6477 | return; | |
6478 | end if; | |
6479 | ||
6480 | -- We get here with a concatenation whose left operand may be a | |
6481 | -- concatenation itself with a consistent type. We need to process | |
6482 | -- these concatenation operands from left to right, which means | |
6483 | -- from the deepest node in the tree to the highest node. | |
6484 | ||
6485 | Cnode := N; | |
6486 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
6487 | Cnode := Left_Opnd (Cnode); | |
6488 | end loop; | |
6489 | ||
64425dff BD |
6490 | -- Now Cnode is the deepest concatenation, and its parents are the |
6491 | -- concatenation nodes above, so now we process bottom up, doing the | |
64425dff | 6492 | -- operands. |
70482933 | 6493 | |
df46b832 AC |
6494 | -- The outer loop runs more than once if more than one concatenation |
6495 | -- type is involved. | |
70482933 RK |
6496 | |
6497 | Outer : loop | |
6498 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
6499 | Set_Parent (Opnds, N); | |
6500 | ||
df46b832 | 6501 | -- The inner loop gathers concatenation operands |
70482933 RK |
6502 | |
6503 | Inner : while Cnode /= N | |
70482933 RK |
6504 | and then Base_Type (Etype (Cnode)) = |
6505 | Base_Type (Etype (Parent (Cnode))) | |
6506 | loop | |
6507 | Cnode := Parent (Cnode); | |
6508 | Append (Right_Opnd (Cnode), Opnds); | |
6509 | end loop Inner; | |
6510 | ||
68bab0fd | 6511 | Expand_Concatenate (Cnode, Opnds); |
70482933 RK |
6512 | |
6513 | exit Outer when Cnode = N; | |
6514 | Cnode := Parent (Cnode); | |
6515 | end loop Outer; | |
6516 | end Expand_N_Op_Concat; | |
6517 | ||
6518 | ------------------------ | |
6519 | -- Expand_N_Op_Divide -- | |
6520 | ------------------------ | |
6521 | ||
6522 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
6523 | Loc : constant Source_Ptr := Sloc (N); |
6524 | Lopnd : constant Node_Id := Left_Opnd (N); | |
6525 | Ropnd : constant Node_Id := Right_Opnd (N); | |
6526 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
6527 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
6528 | Typ : Entity_Id := Etype (N); | |
6529 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
6530 | and then | |
6531 | Compile_Time_Known_Value (Ropnd); | |
6532 | Rval : Uint; | |
70482933 RK |
6533 | |
6534 | begin | |
6535 | Binary_Op_Validity_Checks (N); | |
6536 | ||
b6b5cca8 AC |
6537 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6538 | ||
6539 | if Minimized_Eliminated_Overflow_Check (N) then | |
6540 | Apply_Arithmetic_Overflow_Check (N); | |
6541 | return; | |
6542 | end if; | |
6543 | ||
6544 | -- Otherwise proceed with expansion of division | |
6545 | ||
f82944b7 JM |
6546 | if Rknow then |
6547 | Rval := Expr_Value (Ropnd); | |
6548 | end if; | |
6549 | ||
70482933 RK |
6550 | -- N / 1 = N for integer types |
6551 | ||
f82944b7 JM |
6552 | if Rknow and then Rval = Uint_1 then |
6553 | Rewrite (N, Lopnd); | |
70482933 RK |
6554 | return; |
6555 | end if; | |
6556 | ||
6557 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
6558 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
6559 | -- operand is an unsigned integer, as required for this to work. | |
6560 | ||
f82944b7 JM |
6561 | if Nkind (Ropnd) = N_Op_Expon |
6562 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
6563 | |
6564 | -- We cannot do this transformation in configurable run time mode if we | |
51bf9bdf | 6565 | -- have 64-bit integers and long shifts are not available. |
fbf5a39b | 6566 | |
761f7dcb | 6567 | and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target) |
70482933 RK |
6568 | then |
6569 | Rewrite (N, | |
6570 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 6571 | Left_Opnd => Lopnd, |
70482933 | 6572 | Right_Opnd => |
f82944b7 | 6573 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
6574 | Analyze_And_Resolve (N, Typ); |
6575 | return; | |
6576 | end if; | |
6577 | ||
6578 | -- Do required fixup of universal fixed operation | |
6579 | ||
6580 | if Typ = Universal_Fixed then | |
6581 | Fixup_Universal_Fixed_Operation (N); | |
6582 | Typ := Etype (N); | |
6583 | end if; | |
6584 | ||
6585 | -- Divisions with fixed-point results | |
6586 | ||
6587 | if Is_Fixed_Point_Type (Typ) then | |
6588 | ||
685094bf RD |
6589 | -- No special processing if Treat_Fixed_As_Integer is set, since |
6590 | -- from a semantic point of view such operations are simply integer | |
6591 | -- operations and will be treated that way. | |
70482933 RK |
6592 | |
6593 | if not Treat_Fixed_As_Integer (N) then | |
6594 | if Is_Integer_Type (Rtyp) then | |
6595 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
6596 | else | |
6597 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
6598 | end if; | |
6599 | end if; | |
6600 | ||
685094bf RD |
6601 | -- Other cases of division of fixed-point operands. Again we exclude the |
6602 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 | 6603 | |
761f7dcb | 6604 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) |
70482933 RK |
6605 | and then not Treat_Fixed_As_Integer (N) |
6606 | then | |
6607 | if Is_Integer_Type (Typ) then | |
6608 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
6609 | else | |
6610 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
6611 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
6612 | end if; | |
6613 | ||
685094bf RD |
6614 | -- Mixed-mode operations can appear in a non-static universal context, |
6615 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 6616 | |
533369aa | 6617 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
f82944b7 JM |
6618 | Rewrite (Ropnd, |
6619 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 6620 | |
f82944b7 | 6621 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 | 6622 | |
533369aa | 6623 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
f82944b7 JM |
6624 | Rewrite (Lopnd, |
6625 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 6626 | |
f82944b7 | 6627 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 6628 | |
f02b8bb8 | 6629 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
6630 | |
6631 | elsif Is_Integer_Type (Typ) then | |
a91e9ac7 | 6632 | Apply_Divide_Checks (N); |
fbf5a39b | 6633 | |
f02b8bb8 RD |
6634 | -- Deal with Vax_Float |
6635 | ||
6636 | elsif Vax_Float (Typ) then | |
6637 | Expand_Vax_Arith (N); | |
6638 | return; | |
70482933 RK |
6639 | end if; |
6640 | end Expand_N_Op_Divide; | |
6641 | ||
6642 | -------------------- | |
6643 | -- Expand_N_Op_Eq -- | |
6644 | -------------------- | |
6645 | ||
6646 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
6647 | Loc : constant Source_Ptr := Sloc (N); |
6648 | Typ : constant Entity_Id := Etype (N); | |
6649 | Lhs : constant Node_Id := Left_Opnd (N); | |
6650 | Rhs : constant Node_Id := Right_Opnd (N); | |
6651 | Bodies : constant List_Id := New_List; | |
6652 | A_Typ : constant Entity_Id := Etype (Lhs); | |
6653 | ||
70482933 RK |
6654 | Typl : Entity_Id := A_Typ; |
6655 | Op_Name : Entity_Id; | |
6656 | Prim : Elmt_Id; | |
70482933 RK |
6657 | |
6658 | procedure Build_Equality_Call (Eq : Entity_Id); | |
6659 | -- If a constructed equality exists for the type or for its parent, | |
6660 | -- build and analyze call, adding conversions if the operation is | |
6661 | -- inherited. | |
6662 | ||
5d09245e | 6663 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
8fc789c8 | 6664 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
6665 | -- Unchecked_Union subtype. Typ is a record type. |
6666 | ||
70482933 RK |
6667 | ------------------------- |
6668 | -- Build_Equality_Call -- | |
6669 | ------------------------- | |
6670 | ||
6671 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
6672 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
6673 | L_Exp : Node_Id := Relocate_Node (Lhs); | |
6674 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
6675 | ||
6676 | begin | |
dda38714 AC |
6677 | -- Adjust operands if necessary to comparison type |
6678 | ||
70482933 RK |
6679 | if Base_Type (Op_Type) /= Base_Type (A_Typ) |
6680 | and then not Is_Class_Wide_Type (A_Typ) | |
6681 | then | |
6682 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
6683 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
6684 | end if; | |
6685 | ||
5d09245e AC |
6686 | -- If we have an Unchecked_Union, we need to add the inferred |
6687 | -- discriminant values as actuals in the function call. At this | |
6688 | -- point, the expansion has determined that both operands have | |
6689 | -- inferable discriminants. | |
6690 | ||
6691 | if Is_Unchecked_Union (Op_Type) then | |
6692 | declare | |
fa1608c2 ES |
6693 | Lhs_Type : constant Node_Id := Etype (L_Exp); |
6694 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
6695 | ||
6696 | Lhs_Discr_Vals : Elist_Id; | |
6697 | -- List of inferred discriminant values for left operand. | |
6698 | ||
6699 | Rhs_Discr_Vals : Elist_Id; | |
6700 | -- List of inferred discriminant values for right operand. | |
6701 | ||
6702 | Discr : Entity_Id; | |
5d09245e AC |
6703 | |
6704 | begin | |
fa1608c2 ES |
6705 | Lhs_Discr_Vals := New_Elmt_List; |
6706 | Rhs_Discr_Vals := New_Elmt_List; | |
6707 | ||
5d09245e AC |
6708 | -- Per-object constrained selected components require special |
6709 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 6710 | -- Unchecked_Union, we cannot reference its discriminants |
fa1608c2 ES |
6711 | -- directly. This is why we use the extra parameters of the |
6712 | -- equality function of the enclosing Unchecked_Union. | |
5d09245e AC |
6713 | |
6714 | -- type UU_Type (Discr : Integer := 0) is | |
6715 | -- . . . | |
6716 | -- end record; | |
6717 | -- pragma Unchecked_Union (UU_Type); | |
6718 | ||
6719 | -- 1. Unchecked_Union enclosing record: | |
6720 | ||
6721 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
6722 | -- . . . | |
6723 | -- Comp : UU_Type (Discr); | |
6724 | -- . . . | |
6725 | -- end Enclosing_UU_Type; | |
6726 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
6727 | ||
6728 | -- Obj1 : Enclosing_UU_Type; | |
6729 | -- Obj2 : Enclosing_UU_Type (1); | |
6730 | ||
2717634d | 6731 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
6732 | |
6733 | -- Generated code: | |
6734 | ||
6735 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
6736 | ||
6737 | -- A and B are the formal parameters of the equality function | |
6738 | -- of Enclosing_UU_Type. The function always has two extra | |
fa1608c2 ES |
6739 | -- formals to capture the inferred discriminant values for |
6740 | -- each discriminant of the type. | |
5d09245e AC |
6741 | |
6742 | -- 2. Non-Unchecked_Union enclosing record: | |
6743 | ||
6744 | -- type | |
6745 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
6746 | -- is record | |
6747 | -- . . . | |
6748 | -- Comp : UU_Type (Discr); | |
6749 | -- . . . | |
6750 | -- end Enclosing_Non_UU_Type; | |
6751 | ||
6752 | -- Obj1 : Enclosing_Non_UU_Type; | |
6753 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
6754 | ||
630d30e9 | 6755 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
6756 | |
6757 | -- Generated code: | |
6758 | ||
6759 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
6760 | -- obj1.discr, obj2.discr)) then | |
6761 | ||
6762 | -- In this case we can directly reference the discriminants of | |
6763 | -- the enclosing record. | |
6764 | ||
fa1608c2 | 6765 | -- Process left operand of equality |
5d09245e AC |
6766 | |
6767 | if Nkind (Lhs) = N_Selected_Component | |
533369aa AC |
6768 | and then |
6769 | Has_Per_Object_Constraint (Entity (Selector_Name (Lhs))) | |
5d09245e | 6770 | then |
fa1608c2 ES |
6771 | -- If enclosing record is an Unchecked_Union, use formals |
6772 | -- corresponding to each discriminant. The name of the | |
6773 | -- formal is that of the discriminant, with added suffix, | |
6774 | -- see Exp_Ch3.Build_Record_Equality for details. | |
5d09245e | 6775 | |
dda38714 | 6776 | if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs)))) |
5d09245e | 6777 | then |
fa1608c2 ES |
6778 | Discr := |
6779 | First_Discriminant | |
6780 | (Scope (Entity (Selector_Name (Lhs)))); | |
6781 | while Present (Discr) loop | |
6782 | Append_Elmt ( | |
6783 | Make_Identifier (Loc, | |
6784 | Chars => New_External_Name (Chars (Discr), 'A')), | |
6785 | To => Lhs_Discr_Vals); | |
6786 | Next_Discriminant (Discr); | |
6787 | end loop; | |
5d09245e | 6788 | |
fa1608c2 ES |
6789 | -- If enclosing record is of a non-Unchecked_Union type, it |
6790 | -- is possible to reference its discriminants directly. | |
5d09245e AC |
6791 | |
6792 | else | |
fa1608c2 ES |
6793 | Discr := First_Discriminant (Lhs_Type); |
6794 | while Present (Discr) loop | |
6795 | Append_Elmt ( | |
6796 | Make_Selected_Component (Loc, | |
6797 | Prefix => Prefix (Lhs), | |
6798 | Selector_Name => | |
6799 | New_Copy | |
6800 | (Get_Discriminant_Value (Discr, | |
6801 | Lhs_Type, | |
6802 | Stored_Constraint (Lhs_Type)))), | |
6803 | To => Lhs_Discr_Vals); | |
6804 | Next_Discriminant (Discr); | |
6805 | end loop; | |
5d09245e AC |
6806 | end if; |
6807 | ||
fa1608c2 ES |
6808 | -- Otherwise operand is on object with a constrained type. |
6809 | -- Infer the discriminant values from the constraint. | |
5d09245e AC |
6810 | |
6811 | else | |
fa1608c2 ES |
6812 | |
6813 | Discr := First_Discriminant (Lhs_Type); | |
6814 | while Present (Discr) loop | |
6815 | Append_Elmt ( | |
6816 | New_Copy | |
6817 | (Get_Discriminant_Value (Discr, | |
6818 | Lhs_Type, | |
6819 | Stored_Constraint (Lhs_Type))), | |
6820 | To => Lhs_Discr_Vals); | |
6821 | Next_Discriminant (Discr); | |
6822 | end loop; | |
5d09245e AC |
6823 | end if; |
6824 | ||
fa1608c2 | 6825 | -- Similar processing for right operand of equality |
5d09245e AC |
6826 | |
6827 | if Nkind (Rhs) = N_Selected_Component | |
533369aa AC |
6828 | and then |
6829 | Has_Per_Object_Constraint (Entity (Selector_Name (Rhs))) | |
5d09245e | 6830 | then |
5e1c00fa | 6831 | if Is_Unchecked_Union |
fa1608c2 | 6832 | (Scope (Entity (Selector_Name (Rhs)))) |
5d09245e | 6833 | then |
fa1608c2 ES |
6834 | Discr := |
6835 | First_Discriminant | |
6836 | (Scope (Entity (Selector_Name (Rhs)))); | |
6837 | while Present (Discr) loop | |
6838 | Append_Elmt ( | |
6839 | Make_Identifier (Loc, | |
6840 | Chars => New_External_Name (Chars (Discr), 'B')), | |
6841 | To => Rhs_Discr_Vals); | |
6842 | Next_Discriminant (Discr); | |
6843 | end loop; | |
5d09245e AC |
6844 | |
6845 | else | |
fa1608c2 ES |
6846 | Discr := First_Discriminant (Rhs_Type); |
6847 | while Present (Discr) loop | |
6848 | Append_Elmt ( | |
6849 | Make_Selected_Component (Loc, | |
6850 | Prefix => Prefix (Rhs), | |
6851 | Selector_Name => | |
6852 | New_Copy (Get_Discriminant_Value | |
6853 | (Discr, | |
6854 | Rhs_Type, | |
6855 | Stored_Constraint (Rhs_Type)))), | |
6856 | To => Rhs_Discr_Vals); | |
6857 | Next_Discriminant (Discr); | |
6858 | end loop; | |
5d09245e | 6859 | end if; |
5d09245e | 6860 | |
fa1608c2 ES |
6861 | else |
6862 | Discr := First_Discriminant (Rhs_Type); | |
6863 | while Present (Discr) loop | |
6864 | Append_Elmt ( | |
6865 | New_Copy (Get_Discriminant_Value | |
6866 | (Discr, | |
6867 | Rhs_Type, | |
6868 | Stored_Constraint (Rhs_Type))), | |
6869 | To => Rhs_Discr_Vals); | |
6870 | Next_Discriminant (Discr); | |
6871 | end loop; | |
5d09245e AC |
6872 | end if; |
6873 | ||
fa1608c2 ES |
6874 | -- Now merge the list of discriminant values so that values |
6875 | -- of corresponding discriminants are adjacent. | |
6876 | ||
6877 | declare | |
6878 | Params : List_Id; | |
6879 | L_Elmt : Elmt_Id; | |
6880 | R_Elmt : Elmt_Id; | |
6881 | ||
6882 | begin | |
6883 | Params := New_List (L_Exp, R_Exp); | |
6884 | L_Elmt := First_Elmt (Lhs_Discr_Vals); | |
6885 | R_Elmt := First_Elmt (Rhs_Discr_Vals); | |
6886 | while Present (L_Elmt) loop | |
6887 | Append_To (Params, Node (L_Elmt)); | |
6888 | Append_To (Params, Node (R_Elmt)); | |
6889 | Next_Elmt (L_Elmt); | |
6890 | Next_Elmt (R_Elmt); | |
6891 | end loop; | |
6892 | ||
6893 | Rewrite (N, | |
6894 | Make_Function_Call (Loc, | |
e4494292 | 6895 | Name => New_Occurrence_Of (Eq, Loc), |
fa1608c2 ES |
6896 | Parameter_Associations => Params)); |
6897 | end; | |
5d09245e AC |
6898 | end; |
6899 | ||
6900 | -- Normal case, not an unchecked union | |
6901 | ||
6902 | else | |
6903 | Rewrite (N, | |
6904 | Make_Function_Call (Loc, | |
e4494292 | 6905 | Name => New_Occurrence_Of (Eq, Loc), |
5d09245e AC |
6906 | Parameter_Associations => New_List (L_Exp, R_Exp))); |
6907 | end if; | |
70482933 RK |
6908 | |
6909 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
6910 | end Build_Equality_Call; | |
6911 | ||
5d09245e AC |
6912 | ------------------------------------ |
6913 | -- Has_Unconstrained_UU_Component -- | |
6914 | ------------------------------------ | |
6915 | ||
6916 | function Has_Unconstrained_UU_Component | |
6917 | (Typ : Node_Id) return Boolean | |
6918 | is | |
6919 | Tdef : constant Node_Id := | |
57848bf7 | 6920 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
6921 | Clist : Node_Id; |
6922 | Vpart : Node_Id; | |
6923 | ||
6924 | function Component_Is_Unconstrained_UU | |
6925 | (Comp : Node_Id) return Boolean; | |
6926 | -- Determines whether the subtype of the component is an | |
6927 | -- unconstrained Unchecked_Union. | |
6928 | ||
6929 | function Variant_Is_Unconstrained_UU | |
6930 | (Variant : Node_Id) return Boolean; | |
6931 | -- Determines whether a component of the variant has an unconstrained | |
6932 | -- Unchecked_Union subtype. | |
6933 | ||
6934 | ----------------------------------- | |
6935 | -- Component_Is_Unconstrained_UU -- | |
6936 | ----------------------------------- | |
6937 | ||
6938 | function Component_Is_Unconstrained_UU | |
6939 | (Comp : Node_Id) return Boolean | |
6940 | is | |
6941 | begin | |
6942 | if Nkind (Comp) /= N_Component_Declaration then | |
6943 | return False; | |
6944 | end if; | |
6945 | ||
6946 | declare | |
6947 | Sindic : constant Node_Id := | |
6948 | Subtype_Indication (Component_Definition (Comp)); | |
6949 | ||
6950 | begin | |
6951 | -- Unconstrained nominal type. In the case of a constraint | |
6952 | -- present, the node kind would have been N_Subtype_Indication. | |
6953 | ||
6954 | if Nkind (Sindic) = N_Identifier then | |
6955 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
6956 | end if; | |
6957 | ||
6958 | return False; | |
6959 | end; | |
6960 | end Component_Is_Unconstrained_UU; | |
6961 | ||
6962 | --------------------------------- | |
6963 | -- Variant_Is_Unconstrained_UU -- | |
6964 | --------------------------------- | |
6965 | ||
6966 | function Variant_Is_Unconstrained_UU | |
6967 | (Variant : Node_Id) return Boolean | |
6968 | is | |
6969 | Clist : constant Node_Id := Component_List (Variant); | |
6970 | ||
6971 | begin | |
6972 | if Is_Empty_List (Component_Items (Clist)) then | |
6973 | return False; | |
6974 | end if; | |
6975 | ||
f02b8bb8 RD |
6976 | -- We only need to test one component |
6977 | ||
5d09245e AC |
6978 | declare |
6979 | Comp : Node_Id := First (Component_Items (Clist)); | |
6980 | ||
6981 | begin | |
6982 | while Present (Comp) loop | |
5d09245e AC |
6983 | if Component_Is_Unconstrained_UU (Comp) then |
6984 | return True; | |
6985 | end if; | |
6986 | ||
6987 | Next (Comp); | |
6988 | end loop; | |
6989 | end; | |
6990 | ||
6991 | -- None of the components withing the variant were of | |
6992 | -- unconstrained Unchecked_Union type. | |
6993 | ||
6994 | return False; | |
6995 | end Variant_Is_Unconstrained_UU; | |
6996 | ||
6997 | -- Start of processing for Has_Unconstrained_UU_Component | |
6998 | ||
6999 | begin | |
7000 | if Null_Present (Tdef) then | |
7001 | return False; | |
7002 | end if; | |
7003 | ||
7004 | Clist := Component_List (Tdef); | |
7005 | Vpart := Variant_Part (Clist); | |
7006 | ||
7007 | -- Inspect available components | |
7008 | ||
7009 | if Present (Component_Items (Clist)) then | |
7010 | declare | |
7011 | Comp : Node_Id := First (Component_Items (Clist)); | |
7012 | ||
7013 | begin | |
7014 | while Present (Comp) loop | |
7015 | ||
8fc789c8 | 7016 | -- One component is sufficient |
5d09245e AC |
7017 | |
7018 | if Component_Is_Unconstrained_UU (Comp) then | |
7019 | return True; | |
7020 | end if; | |
7021 | ||
7022 | Next (Comp); | |
7023 | end loop; | |
7024 | end; | |
7025 | end if; | |
7026 | ||
7027 | -- Inspect available components withing variants | |
7028 | ||
7029 | if Present (Vpart) then | |
7030 | declare | |
7031 | Variant : Node_Id := First (Variants (Vpart)); | |
7032 | ||
7033 | begin | |
7034 | while Present (Variant) loop | |
7035 | ||
8fc789c8 | 7036 | -- One component within a variant is sufficient |
5d09245e AC |
7037 | |
7038 | if Variant_Is_Unconstrained_UU (Variant) then | |
7039 | return True; | |
7040 | end if; | |
7041 | ||
7042 | Next (Variant); | |
7043 | end loop; | |
7044 | end; | |
7045 | end if; | |
7046 | ||
7047 | -- Neither the available components, nor the components inside the | |
7048 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
7049 | ||
7050 | return False; | |
7051 | end Has_Unconstrained_UU_Component; | |
7052 | ||
70482933 RK |
7053 | -- Start of processing for Expand_N_Op_Eq |
7054 | ||
7055 | begin | |
7056 | Binary_Op_Validity_Checks (N); | |
7057 | ||
456cbfa5 AC |
7058 | -- Deal with private types |
7059 | ||
70482933 RK |
7060 | if Ekind (Typl) = E_Private_Type then |
7061 | Typl := Underlying_Type (Typl); | |
70482933 RK |
7062 | elsif Ekind (Typl) = E_Private_Subtype then |
7063 | Typl := Underlying_Type (Base_Type (Typl)); | |
f02b8bb8 RD |
7064 | else |
7065 | null; | |
70482933 RK |
7066 | end if; |
7067 | ||
7068 | -- It may happen in error situations that the underlying type is not | |
7069 | -- set. The error will be detected later, here we just defend the | |
7070 | -- expander code. | |
7071 | ||
7072 | if No (Typl) then | |
7073 | return; | |
7074 | end if; | |
7075 | ||
7076 | Typl := Base_Type (Typl); | |
7077 | ||
dda38714 AC |
7078 | -- Equality between variant records results in a call to a routine |
7079 | -- that has conditional tests of the discriminant value(s), and hence | |
7080 | -- violates the No_Implicit_Conditionals restriction. | |
7081 | ||
7082 | if Has_Variant_Part (Typl) then | |
7083 | declare | |
7084 | Msg : Boolean; | |
7085 | ||
7086 | begin | |
7087 | Check_Restriction (Msg, No_Implicit_Conditionals, N); | |
7088 | ||
7089 | if Msg then | |
7090 | Error_Msg_N | |
7091 | ("\comparison of variant records tests discriminants", N); | |
7092 | return; | |
7093 | end if; | |
7094 | end; | |
7095 | end if; | |
7096 | ||
456cbfa5 | 7097 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7098 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7099 | |
7100 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7101 | ||
7102 | if Nkind (N) /= N_Op_Eq then | |
7103 | return; | |
7104 | end if; | |
7105 | ||
70482933 RK |
7106 | -- Boolean types (requiring handling of non-standard case) |
7107 | ||
f02b8bb8 | 7108 | if Is_Boolean_Type (Typl) then |
70482933 RK |
7109 | Adjust_Condition (Left_Opnd (N)); |
7110 | Adjust_Condition (Right_Opnd (N)); | |
7111 | Set_Etype (N, Standard_Boolean); | |
7112 | Adjust_Result_Type (N, Typ); | |
7113 | ||
7114 | -- Array types | |
7115 | ||
7116 | elsif Is_Array_Type (Typl) then | |
7117 | ||
1033834f RD |
7118 | -- If we are doing full validity checking, and it is possible for the |
7119 | -- array elements to be invalid then expand out array comparisons to | |
7120 | -- make sure that we check the array elements. | |
fbf5a39b | 7121 | |
1033834f RD |
7122 | if Validity_Check_Operands |
7123 | and then not Is_Known_Valid (Component_Type (Typl)) | |
7124 | then | |
fbf5a39b AC |
7125 | declare |
7126 | Save_Force_Validity_Checks : constant Boolean := | |
7127 | Force_Validity_Checks; | |
7128 | begin | |
7129 | Force_Validity_Checks := True; | |
7130 | Rewrite (N, | |
0da2c8ac AC |
7131 | Expand_Array_Equality |
7132 | (N, | |
7133 | Relocate_Node (Lhs), | |
7134 | Relocate_Node (Rhs), | |
7135 | Bodies, | |
7136 | Typl)); | |
7137 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
7138 | Analyze_And_Resolve (N, Standard_Boolean); |
7139 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
7140 | end; | |
7141 | ||
a9d8907c | 7142 | -- Packed case where both operands are known aligned |
70482933 | 7143 | |
a9d8907c JM |
7144 | elsif Is_Bit_Packed_Array (Typl) |
7145 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
7146 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
7147 | then | |
70482933 RK |
7148 | Expand_Packed_Eq (N); |
7149 | ||
5e1c00fa RD |
7150 | -- Where the component type is elementary we can use a block bit |
7151 | -- comparison (if supported on the target) exception in the case | |
7152 | -- of floating-point (negative zero issues require element by | |
7153 | -- element comparison), and atomic types (where we must be sure | |
a9d8907c | 7154 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 7155 | |
70482933 RK |
7156 | elsif Is_Elementary_Type (Component_Type (Typl)) |
7157 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 7158 | and then not Is_Atomic (Component_Type (Typl)) |
a9d8907c JM |
7159 | and then not Is_Possibly_Unaligned_Object (Lhs) |
7160 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
fbf5a39b | 7161 | and then Support_Composite_Compare_On_Target |
70482933 RK |
7162 | then |
7163 | null; | |
7164 | ||
685094bf RD |
7165 | -- For composite and floating-point cases, expand equality loop to |
7166 | -- make sure of using proper comparisons for tagged types, and | |
7167 | -- correctly handling the floating-point case. | |
70482933 RK |
7168 | |
7169 | else | |
7170 | Rewrite (N, | |
0da2c8ac AC |
7171 | Expand_Array_Equality |
7172 | (N, | |
7173 | Relocate_Node (Lhs), | |
7174 | Relocate_Node (Rhs), | |
7175 | Bodies, | |
7176 | Typl)); | |
70482933 RK |
7177 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
7178 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7179 | end if; | |
7180 | ||
7181 | -- Record Types | |
7182 | ||
7183 | elsif Is_Record_Type (Typl) then | |
7184 | ||
7185 | -- For tagged types, use the primitive "=" | |
7186 | ||
7187 | if Is_Tagged_Type (Typl) then | |
7188 | ||
0669bebe GB |
7189 | -- No need to do anything else compiling under restriction |
7190 | -- No_Dispatching_Calls. During the semantic analysis we | |
7191 | -- already notified such violation. | |
7192 | ||
7193 | if Restriction_Active (No_Dispatching_Calls) then | |
7194 | return; | |
7195 | end if; | |
7196 | ||
685094bf RD |
7197 | -- If this is derived from an untagged private type completed with |
7198 | -- a tagged type, it does not have a full view, so we use the | |
7199 | -- primitive operations of the private type. This check should no | |
7200 | -- longer be necessary when these types get their full views??? | |
70482933 RK |
7201 | |
7202 | if Is_Private_Type (A_Typ) | |
7203 | and then not Is_Tagged_Type (A_Typ) | |
7204 | and then Is_Derived_Type (A_Typ) | |
7205 | and then No (Full_View (A_Typ)) | |
7206 | then | |
685094bf RD |
7207 | -- Search for equality operation, checking that the operands |
7208 | -- have the same type. Note that we must find a matching entry, | |
a90bd866 | 7209 | -- or something is very wrong. |
2e071734 | 7210 | |
70482933 RK |
7211 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
7212 | ||
2e071734 AC |
7213 | while Present (Prim) loop |
7214 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7215 | and then Etype (First_Formal (Node (Prim))) = | |
7216 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
7217 | and then | |
7218 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
7219 | ||
70482933 | 7220 | Next_Elmt (Prim); |
70482933 RK |
7221 | end loop; |
7222 | ||
2e071734 | 7223 | pragma Assert (Present (Prim)); |
70482933 | 7224 | Op_Name := Node (Prim); |
fbf5a39b AC |
7225 | |
7226 | -- Find the type's predefined equality or an overriding | |
685094bf | 7227 | -- user- defined equality. The reason for not simply calling |
fbf5a39b | 7228 | -- Find_Prim_Op here is that there may be a user-defined |
685094bf RD |
7229 | -- overloaded equality op that precedes the equality that we want, |
7230 | -- so we have to explicitly search (e.g., there could be an | |
7231 | -- equality with two different parameter types). | |
fbf5a39b | 7232 | |
70482933 | 7233 | else |
fbf5a39b AC |
7234 | if Is_Class_Wide_Type (Typl) then |
7235 | Typl := Root_Type (Typl); | |
7236 | end if; | |
7237 | ||
7238 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
7239 | while Present (Prim) loop |
7240 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7241 | and then Etype (First_Formal (Node (Prim))) = | |
7242 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
7243 | and then |
7244 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
7245 | |
7246 | Next_Elmt (Prim); | |
fbf5a39b AC |
7247 | end loop; |
7248 | ||
2e071734 | 7249 | pragma Assert (Present (Prim)); |
fbf5a39b | 7250 | Op_Name := Node (Prim); |
70482933 RK |
7251 | end if; |
7252 | ||
7253 | Build_Equality_Call (Op_Name); | |
7254 | ||
5d09245e AC |
7255 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
7256 | -- predefined equality operator for a type which has a subcomponent | |
7257 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
7258 | ||
7259 | elsif Has_Unconstrained_UU_Component (Typl) then | |
7260 | Insert_Action (N, | |
7261 | Make_Raise_Program_Error (Loc, | |
7262 | Reason => PE_Unchecked_Union_Restriction)); | |
7263 | ||
7264 | -- Prevent Gigi from generating incorrect code by rewriting the | |
6cb3037c | 7265 | -- equality as a standard False. (is this documented somewhere???) |
5d09245e AC |
7266 | |
7267 | Rewrite (N, | |
7268 | New_Occurrence_Of (Standard_False, Loc)); | |
7269 | ||
7270 | elsif Is_Unchecked_Union (Typl) then | |
7271 | ||
7272 | -- If we can infer the discriminants of the operands, we make a | |
7273 | -- call to the TSS equality function. | |
7274 | ||
7275 | if Has_Inferable_Discriminants (Lhs) | |
7276 | and then | |
7277 | Has_Inferable_Discriminants (Rhs) | |
7278 | then | |
7279 | Build_Equality_Call | |
7280 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
7281 | ||
7282 | else | |
7283 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
7284 | -- the predefined equality operator for an Unchecked_Union type | |
7285 | -- if either of the operands lack inferable discriminants. | |
7286 | ||
7287 | Insert_Action (N, | |
7288 | Make_Raise_Program_Error (Loc, | |
7289 | Reason => PE_Unchecked_Union_Restriction)); | |
7290 | ||
7291 | -- Prevent Gigi from generating incorrect code by rewriting | |
6cb3037c | 7292 | -- the equality as a standard False (documented where???). |
5d09245e AC |
7293 | |
7294 | Rewrite (N, | |
7295 | New_Occurrence_Of (Standard_False, Loc)); | |
7296 | ||
7297 | end if; | |
7298 | ||
70482933 RK |
7299 | -- If a type support function is present (for complex cases), use it |
7300 | ||
fbf5a39b AC |
7301 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
7302 | Build_Equality_Call | |
7303 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 | 7304 | |
8d80ff64 AC |
7305 | -- When comparing two Bounded_Strings, use the primitive equality of |
7306 | -- the root Super_String type. | |
7307 | ||
7308 | elsif Is_Bounded_String (Typl) then | |
7309 | Prim := | |
7310 | First_Elmt (Collect_Primitive_Operations (Root_Type (Typl))); | |
7311 | ||
7312 | while Present (Prim) loop | |
7313 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
7314 | and then Etype (First_Formal (Node (Prim))) = | |
7315 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
7316 | and then Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
7317 | ||
7318 | Next_Elmt (Prim); | |
7319 | end loop; | |
7320 | ||
7321 | -- A Super_String type should always have a primitive equality | |
7322 | ||
7323 | pragma Assert (Present (Prim)); | |
7324 | Build_Equality_Call (Node (Prim)); | |
7325 | ||
70482933 | 7326 | -- Otherwise expand the component by component equality. Note that |
8fc789c8 | 7327 | -- we never use block-bit comparisons for records, because of the |
70482933 RK |
7328 | -- problems with gaps. The backend will often be able to recombine |
7329 | -- the separate comparisons that we generate here. | |
7330 | ||
7331 | else | |
7332 | Remove_Side_Effects (Lhs); | |
7333 | Remove_Side_Effects (Rhs); | |
7334 | Rewrite (N, | |
7335 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
7336 | ||
7337 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
7338 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
7339 | end if; | |
7340 | end if; | |
7341 | ||
d26dc4b5 | 7342 | -- Test if result is known at compile time |
70482933 | 7343 | |
d26dc4b5 | 7344 | Rewrite_Comparison (N); |
f02b8bb8 RD |
7345 | |
7346 | -- If we still have comparison for Vax_Float, process it | |
7347 | ||
7348 | if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then | |
7349 | Expand_Vax_Comparison (N); | |
7350 | return; | |
7351 | end if; | |
0580d807 AC |
7352 | |
7353 | Optimize_Length_Comparison (N); | |
70482933 RK |
7354 | end Expand_N_Op_Eq; |
7355 | ||
7356 | ----------------------- | |
7357 | -- Expand_N_Op_Expon -- | |
7358 | ----------------------- | |
7359 | ||
7360 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
7361 | Loc : constant Source_Ptr := Sloc (N); | |
7362 | Typ : constant Entity_Id := Etype (N); | |
7363 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
7364 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 7365 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
7366 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
7367 | Exptyp : constant Entity_Id := Etype (Exp); | |
7368 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
7369 | Expv : Uint; | |
70482933 RK |
7370 | Temp : Node_Id; |
7371 | Rent : RE_Id; | |
7372 | Ent : Entity_Id; | |
fbf5a39b | 7373 | Etyp : Entity_Id; |
cb42ba5d | 7374 | Xnode : Node_Id; |
70482933 RK |
7375 | |
7376 | begin | |
7377 | Binary_Op_Validity_Checks (N); | |
7378 | ||
5114f3ff | 7379 | -- CodePeer wants to see the unexpanded N_Op_Expon node |
8f66cda7 | 7380 | |
5114f3ff | 7381 | if CodePeer_Mode then |
8f66cda7 AC |
7382 | return; |
7383 | end if; | |
7384 | ||
685094bf RD |
7385 | -- If either operand is of a private type, then we have the use of an |
7386 | -- intrinsic operator, and we get rid of the privateness, by using root | |
7387 | -- types of underlying types for the actual operation. Otherwise the | |
7388 | -- private types will cause trouble if we expand multiplications or | |
7389 | -- shifts etc. We also do this transformation if the result type is | |
7390 | -- different from the base type. | |
07fc65c4 GB |
7391 | |
7392 | if Is_Private_Type (Etype (Base)) | |
8f66cda7 AC |
7393 | or else Is_Private_Type (Typ) |
7394 | or else Is_Private_Type (Exptyp) | |
7395 | or else Rtyp /= Root_Type (Bastyp) | |
07fc65c4 GB |
7396 | then |
7397 | declare | |
7398 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
7399 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
07fc65c4 GB |
7400 | begin |
7401 | Rewrite (N, | |
7402 | Unchecked_Convert_To (Typ, | |
7403 | Make_Op_Expon (Loc, | |
7404 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
7405 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
7406 | Analyze_And_Resolve (N, Typ); | |
7407 | return; | |
7408 | end; | |
7409 | end if; | |
7410 | ||
b6b5cca8 | 7411 | -- Check for MINIMIZED/ELIMINATED overflow mode |
6cb3037c | 7412 | |
b6b5cca8 | 7413 | if Minimized_Eliminated_Overflow_Check (N) then |
6cb3037c AC |
7414 | Apply_Arithmetic_Overflow_Check (N); |
7415 | return; | |
7416 | end if; | |
7417 | ||
cb42ba5d AC |
7418 | -- Test for case of known right argument where we can replace the |
7419 | -- exponentiation by an equivalent expression using multiplication. | |
70482933 | 7420 | |
6c3c671e AC |
7421 | -- Note: use CRT_Safe version of Compile_Time_Known_Value because in |
7422 | -- configurable run-time mode, we may not have the exponentiation | |
7423 | -- routine available, and we don't want the legality of the program | |
7424 | -- to depend on how clever the compiler is in knowing values. | |
7425 | ||
7426 | if CRT_Safe_Compile_Time_Known_Value (Exp) then | |
70482933 RK |
7427 | Expv := Expr_Value (Exp); |
7428 | ||
7429 | -- We only fold small non-negative exponents. You might think we | |
7430 | -- could fold small negative exponents for the real case, but we | |
7431 | -- can't because we are required to raise Constraint_Error for | |
7432 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
7433 | -- See ACVC test C4A012B. | |
7434 | ||
7435 | if Expv >= 0 and then Expv <= 4 then | |
7436 | ||
7437 | -- X ** 0 = 1 (or 1.0) | |
7438 | ||
7439 | if Expv = 0 then | |
abcbd24c ST |
7440 | |
7441 | -- Call Remove_Side_Effects to ensure that any side effects | |
7442 | -- in the ignored left operand (in particular function calls | |
7443 | -- to user defined functions) are properly executed. | |
7444 | ||
7445 | Remove_Side_Effects (Base); | |
7446 | ||
70482933 RK |
7447 | if Ekind (Typ) in Integer_Kind then |
7448 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
7449 | else | |
7450 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
7451 | end if; | |
7452 | ||
7453 | -- X ** 1 = X | |
7454 | ||
7455 | elsif Expv = 1 then | |
7456 | Xnode := Base; | |
7457 | ||
7458 | -- X ** 2 = X * X | |
7459 | ||
7460 | elsif Expv = 2 then | |
7461 | Xnode := | |
7462 | Make_Op_Multiply (Loc, | |
7463 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 7464 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
7465 | |
7466 | -- X ** 3 = X * X * X | |
7467 | ||
7468 | elsif Expv = 3 then | |
7469 | Xnode := | |
7470 | Make_Op_Multiply (Loc, | |
7471 | Left_Opnd => | |
7472 | Make_Op_Multiply (Loc, | |
7473 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
7474 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
7475 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
7476 | |
7477 | -- X ** 4 -> | |
cb42ba5d AC |
7478 | |
7479 | -- do | |
70482933 | 7480 | -- En : constant base'type := base * base; |
cb42ba5d | 7481 | -- in |
70482933 RK |
7482 | -- En * En |
7483 | ||
cb42ba5d AC |
7484 | else |
7485 | pragma Assert (Expv = 4); | |
191fcb3a | 7486 | Temp := Make_Temporary (Loc, 'E', Base); |
70482933 | 7487 | |
cb42ba5d AC |
7488 | Xnode := |
7489 | Make_Expression_With_Actions (Loc, | |
7490 | Actions => New_List ( | |
7491 | Make_Object_Declaration (Loc, | |
7492 | Defining_Identifier => Temp, | |
7493 | Constant_Present => True, | |
e4494292 | 7494 | Object_Definition => New_Occurrence_Of (Typ, Loc), |
cb42ba5d AC |
7495 | Expression => |
7496 | Make_Op_Multiply (Loc, | |
7497 | Left_Opnd => | |
7498 | Duplicate_Subexpr (Base), | |
7499 | Right_Opnd => | |
7500 | Duplicate_Subexpr_No_Checks (Base)))), | |
7501 | ||
70482933 RK |
7502 | Expression => |
7503 | Make_Op_Multiply (Loc, | |
e4494292 RD |
7504 | Left_Opnd => New_Occurrence_Of (Temp, Loc), |
7505 | Right_Opnd => New_Occurrence_Of (Temp, Loc))); | |
70482933 RK |
7506 | end if; |
7507 | ||
7508 | Rewrite (N, Xnode); | |
7509 | Analyze_And_Resolve (N, Typ); | |
7510 | return; | |
7511 | end if; | |
7512 | end if; | |
7513 | ||
7514 | -- Case of (2 ** expression) appearing as an argument of an integer | |
7515 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 7516 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
7517 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
7518 | -- of the higher level node converts it into a shift. | |
7519 | ||
51bf9bdf AC |
7520 | -- Another case is 2 ** N in any other context. We simply convert |
7521 | -- this to 1 * 2 ** N, and then the above transformation applies. | |
7522 | ||
685094bf RD |
7523 | -- Note: this transformation is not applicable for a modular type with |
7524 | -- a non-binary modulus in the multiplication case, since we get a wrong | |
7525 | -- result if the shift causes an overflow before the modular reduction. | |
7526 | ||
8b4230c8 AC |
7527 | -- Note: we used to check that Exptyp was an unsigned type. But that is |
7528 | -- an unnecessary check, since if Exp is negative, we have a run-time | |
7529 | -- error that is either caught (so we get the right result) or we have | |
7530 | -- suppressed the check, in which case the code is erroneous anyway. | |
7531 | ||
70482933 | 7532 | if Nkind (Base) = N_Integer_Literal |
6c3c671e AC |
7533 | and then CRT_Safe_Compile_Time_Known_Value (Base) |
7534 | and then Expr_Value (Base) = Uint_2 | |
70482933 RK |
7535 | and then Is_Integer_Type (Root_Type (Exptyp)) |
7536 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
70482933 | 7537 | and then not Ovflo |
70482933 | 7538 | then |
51bf9bdf | 7539 | -- First the multiply and divide cases |
70482933 | 7540 | |
51bf9bdf AC |
7541 | if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then |
7542 | declare | |
7543 | P : constant Node_Id := Parent (N); | |
7544 | L : constant Node_Id := Left_Opnd (P); | |
7545 | R : constant Node_Id := Right_Opnd (P); | |
7546 | ||
7547 | begin | |
7548 | if (Nkind (P) = N_Op_Multiply | |
7549 | and then not Non_Binary_Modulus (Typ) | |
7550 | and then | |
7551 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
7552 | or else | |
7553 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
7554 | and then not Do_Overflow_Check (P)) | |
7555 | or else | |
7556 | (Nkind (P) = N_Op_Divide | |
533369aa AC |
7557 | and then Is_Integer_Type (Etype (L)) |
7558 | and then Is_Unsigned_Type (Etype (L)) | |
7559 | and then R = N | |
7560 | and then not Do_Overflow_Check (P)) | |
51bf9bdf AC |
7561 | then |
7562 | Set_Is_Power_Of_2_For_Shift (N); | |
7563 | return; | |
7564 | end if; | |
7565 | end; | |
7566 | ||
7567 | -- Now the other cases | |
7568 | ||
7569 | elsif not Non_Binary_Modulus (Typ) then | |
7570 | Rewrite (N, | |
7571 | Make_Op_Multiply (Loc, | |
7572 | Left_Opnd => Make_Integer_Literal (Loc, 1), | |
7573 | Right_Opnd => Relocate_Node (N))); | |
7574 | Analyze_And_Resolve (N, Typ); | |
7575 | return; | |
7576 | end if; | |
70482933 RK |
7577 | end if; |
7578 | ||
07fc65c4 GB |
7579 | -- Fall through if exponentiation must be done using a runtime routine |
7580 | ||
07fc65c4 | 7581 | -- First deal with modular case |
70482933 RK |
7582 | |
7583 | if Is_Modular_Integer_Type (Rtyp) then | |
7584 | ||
7585 | -- Non-binary case, we call the special exponentiation routine for | |
7586 | -- the non-binary case, converting the argument to Long_Long_Integer | |
7587 | -- and passing the modulus value. Then the result is converted back | |
7588 | -- to the base type. | |
7589 | ||
7590 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
7591 | Rewrite (N, |
7592 | Convert_To (Typ, | |
7593 | Make_Function_Call (Loc, | |
e4494292 | 7594 | Name => New_Occurrence_Of (RTE (RE_Exp_Modular), Loc), |
70482933 | 7595 | Parameter_Associations => New_List ( |
e9daba51 | 7596 | Convert_To (RTE (RE_Unsigned), Base), |
70482933 RK |
7597 | Make_Integer_Literal (Loc, Modulus (Rtyp)), |
7598 | Exp)))); | |
7599 | ||
685094bf RD |
7600 | -- Binary case, in this case, we call one of two routines, either the |
7601 | -- unsigned integer case, or the unsigned long long integer case, | |
7602 | -- with a final "and" operation to do the required mod. | |
70482933 RK |
7603 | |
7604 | else | |
7605 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
7606 | Ent := RTE (RE_Exp_Unsigned); | |
7607 | else | |
7608 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
7609 | end if; | |
7610 | ||
7611 | Rewrite (N, | |
7612 | Convert_To (Typ, | |
7613 | Make_Op_And (Loc, | |
7614 | Left_Opnd => | |
7615 | Make_Function_Call (Loc, | |
e4494292 | 7616 | Name => New_Occurrence_Of (Ent, Loc), |
70482933 RK |
7617 | Parameter_Associations => New_List ( |
7618 | Convert_To (Etype (First_Formal (Ent)), Base), | |
7619 | Exp)), | |
7620 | Right_Opnd => | |
7621 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
7622 | ||
7623 | end if; | |
7624 | ||
7625 | -- Common exit point for modular type case | |
7626 | ||
7627 | Analyze_And_Resolve (N, Typ); | |
7628 | return; | |
7629 | ||
fbf5a39b AC |
7630 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
7631 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
7632 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 7633 | -- might need certification when a certified run time is required. |
70482933 | 7634 | |
fbf5a39b | 7635 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
7636 | -- checks are required, and one when they are not required, since there |
7637 | -- is a real gain in omitting checks on many machines. | |
70482933 | 7638 | |
fbf5a39b AC |
7639 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
7640 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
761f7dcb AC |
7641 | and then |
7642 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
7643 | or else Rtyp = Universal_Integer | |
70482933 | 7644 | then |
fbf5a39b AC |
7645 | Etyp := Standard_Long_Long_Integer; |
7646 | ||
ebb6b0bd AC |
7647 | -- Overflow checking is the only choice on the AAMP target, where |
7648 | -- arithmetic instructions check overflow automatically, so only | |
7649 | -- one version of the exponentiation unit is needed. | |
7650 | ||
1037b0f4 | 7651 | if Ovflo or AAMP_On_Target then |
70482933 RK |
7652 | Rent := RE_Exp_Long_Long_Integer; |
7653 | else | |
7654 | Rent := RE_Exn_Long_Long_Integer; | |
7655 | end if; | |
7656 | ||
fbf5a39b AC |
7657 | elsif Is_Signed_Integer_Type (Rtyp) then |
7658 | Etyp := Standard_Integer; | |
70482933 | 7659 | |
ebb6b0bd AC |
7660 | -- Overflow checking is the only choice on the AAMP target, where |
7661 | -- arithmetic instructions check overflow automatically, so only | |
7662 | -- one version of the exponentiation unit is needed. | |
7663 | ||
1037b0f4 | 7664 | if Ovflo or AAMP_On_Target then |
fbf5a39b | 7665 | Rent := RE_Exp_Integer; |
70482933 | 7666 | else |
fbf5a39b | 7667 | Rent := RE_Exn_Integer; |
70482933 | 7668 | end if; |
fbf5a39b AC |
7669 | |
7670 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
7671 | -- need separate routines for the overflow case here, since in the case | |
7672 | -- of floating-point, we generate infinities anyway as a rule (either | |
7673 | -- that or we automatically trap overflow), and if there is an infinity | |
7674 | -- generated and a range check is required, the check will fail anyway. | |
7675 | ||
7676 | else | |
7677 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
7678 | Etyp := Standard_Long_Long_Float; | |
7679 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
7680 | end if; |
7681 | ||
7682 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 7683 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 7684 | |
fbf5a39b | 7685 | if Typ = Etyp |
70482933 RK |
7686 | and then Rtyp /= Universal_Integer |
7687 | and then Rtyp /= Universal_Real | |
7688 | then | |
7689 | Rewrite (N, | |
7690 | Make_Function_Call (Loc, | |
e4494292 | 7691 | Name => New_Occurrence_Of (RTE (Rent), Loc), |
70482933 RK |
7692 | Parameter_Associations => New_List (Base, Exp))); |
7693 | ||
7694 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 7695 | -- required in the universal cases, since the runtime routine is |
1147c704 | 7696 | -- typed using one of the standard types). |
70482933 RK |
7697 | |
7698 | else | |
7699 | Rewrite (N, | |
7700 | Convert_To (Typ, | |
7701 | Make_Function_Call (Loc, | |
e4494292 | 7702 | Name => New_Occurrence_Of (RTE (Rent), Loc), |
70482933 | 7703 | Parameter_Associations => New_List ( |
fbf5a39b | 7704 | Convert_To (Etyp, Base), |
70482933 RK |
7705 | Exp)))); |
7706 | end if; | |
7707 | ||
7708 | Analyze_And_Resolve (N, Typ); | |
7709 | return; | |
7710 | ||
fbf5a39b AC |
7711 | exception |
7712 | when RE_Not_Available => | |
7713 | return; | |
70482933 RK |
7714 | end Expand_N_Op_Expon; |
7715 | ||
7716 | -------------------- | |
7717 | -- Expand_N_Op_Ge -- | |
7718 | -------------------- | |
7719 | ||
7720 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
7721 | Typ : constant Entity_Id := Etype (N); | |
7722 | Op1 : constant Node_Id := Left_Opnd (N); | |
7723 | Op2 : constant Node_Id := Right_Opnd (N); | |
7724 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7725 | ||
7726 | begin | |
7727 | Binary_Op_Validity_Checks (N); | |
7728 | ||
456cbfa5 | 7729 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7730 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7731 | |
7732 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7733 | ||
7734 | if Nkind (N) /= N_Op_Ge then | |
7735 | return; | |
7736 | end if; | |
7737 | ||
7738 | -- Array type case | |
7739 | ||
f02b8bb8 | 7740 | if Is_Array_Type (Typ1) then |
70482933 RK |
7741 | Expand_Array_Comparison (N); |
7742 | return; | |
7743 | end if; | |
7744 | ||
456cbfa5 AC |
7745 | -- Deal with boolean operands |
7746 | ||
70482933 RK |
7747 | if Is_Boolean_Type (Typ1) then |
7748 | Adjust_Condition (Op1); | |
7749 | Adjust_Condition (Op2); | |
7750 | Set_Etype (N, Standard_Boolean); | |
7751 | Adjust_Result_Type (N, Typ); | |
7752 | end if; | |
7753 | ||
7754 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
7755 | |
7756 | -- If we still have comparison, and Vax_Float type, process it | |
7757 | ||
7758 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
7759 | Expand_Vax_Comparison (N); | |
7760 | return; | |
7761 | end if; | |
0580d807 AC |
7762 | |
7763 | Optimize_Length_Comparison (N); | |
70482933 RK |
7764 | end Expand_N_Op_Ge; |
7765 | ||
7766 | -------------------- | |
7767 | -- Expand_N_Op_Gt -- | |
7768 | -------------------- | |
7769 | ||
7770 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
7771 | Typ : constant Entity_Id := Etype (N); | |
7772 | Op1 : constant Node_Id := Left_Opnd (N); | |
7773 | Op2 : constant Node_Id := Right_Opnd (N); | |
7774 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7775 | ||
7776 | begin | |
7777 | Binary_Op_Validity_Checks (N); | |
7778 | ||
456cbfa5 | 7779 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7780 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7781 | |
7782 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7783 | ||
7784 | if Nkind (N) /= N_Op_Gt then | |
7785 | return; | |
7786 | end if; | |
7787 | ||
7788 | -- Deal with array type operands | |
7789 | ||
f02b8bb8 | 7790 | if Is_Array_Type (Typ1) then |
70482933 RK |
7791 | Expand_Array_Comparison (N); |
7792 | return; | |
7793 | end if; | |
7794 | ||
456cbfa5 AC |
7795 | -- Deal with boolean type operands |
7796 | ||
70482933 RK |
7797 | if Is_Boolean_Type (Typ1) then |
7798 | Adjust_Condition (Op1); | |
7799 | Adjust_Condition (Op2); | |
7800 | Set_Etype (N, Standard_Boolean); | |
7801 | Adjust_Result_Type (N, Typ); | |
7802 | end if; | |
7803 | ||
7804 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
7805 | |
7806 | -- If we still have comparison, and Vax_Float type, process it | |
7807 | ||
7808 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
7809 | Expand_Vax_Comparison (N); | |
7810 | return; | |
7811 | end if; | |
0580d807 AC |
7812 | |
7813 | Optimize_Length_Comparison (N); | |
70482933 RK |
7814 | end Expand_N_Op_Gt; |
7815 | ||
7816 | -------------------- | |
7817 | -- Expand_N_Op_Le -- | |
7818 | -------------------- | |
7819 | ||
7820 | procedure Expand_N_Op_Le (N : Node_Id) is | |
7821 | Typ : constant Entity_Id := Etype (N); | |
7822 | Op1 : constant Node_Id := Left_Opnd (N); | |
7823 | Op2 : constant Node_Id := Right_Opnd (N); | |
7824 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7825 | ||
7826 | begin | |
7827 | Binary_Op_Validity_Checks (N); | |
7828 | ||
456cbfa5 | 7829 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7830 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7831 | |
7832 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7833 | ||
7834 | if Nkind (N) /= N_Op_Le then | |
7835 | return; | |
7836 | end if; | |
7837 | ||
7838 | -- Deal with array type operands | |
7839 | ||
f02b8bb8 | 7840 | if Is_Array_Type (Typ1) then |
70482933 RK |
7841 | Expand_Array_Comparison (N); |
7842 | return; | |
7843 | end if; | |
7844 | ||
456cbfa5 AC |
7845 | -- Deal with Boolean type operands |
7846 | ||
70482933 RK |
7847 | if Is_Boolean_Type (Typ1) then |
7848 | Adjust_Condition (Op1); | |
7849 | Adjust_Condition (Op2); | |
7850 | Set_Etype (N, Standard_Boolean); | |
7851 | Adjust_Result_Type (N, Typ); | |
7852 | end if; | |
7853 | ||
7854 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
7855 | |
7856 | -- If we still have comparison, and Vax_Float type, process it | |
7857 | ||
7858 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
7859 | Expand_Vax_Comparison (N); | |
7860 | return; | |
7861 | end if; | |
0580d807 AC |
7862 | |
7863 | Optimize_Length_Comparison (N); | |
70482933 RK |
7864 | end Expand_N_Op_Le; |
7865 | ||
7866 | -------------------- | |
7867 | -- Expand_N_Op_Lt -- | |
7868 | -------------------- | |
7869 | ||
7870 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
7871 | Typ : constant Entity_Id := Etype (N); | |
7872 | Op1 : constant Node_Id := Left_Opnd (N); | |
7873 | Op2 : constant Node_Id := Right_Opnd (N); | |
7874 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
7875 | ||
7876 | begin | |
7877 | Binary_Op_Validity_Checks (N); | |
7878 | ||
456cbfa5 | 7879 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that |
60b68e56 | 7880 | -- means we no longer have a comparison operation, we are all done. |
456cbfa5 AC |
7881 | |
7882 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
7883 | ||
7884 | if Nkind (N) /= N_Op_Lt then | |
7885 | return; | |
7886 | end if; | |
7887 | ||
7888 | -- Deal with array type operands | |
7889 | ||
f02b8bb8 | 7890 | if Is_Array_Type (Typ1) then |
70482933 RK |
7891 | Expand_Array_Comparison (N); |
7892 | return; | |
7893 | end if; | |
7894 | ||
456cbfa5 AC |
7895 | -- Deal with Boolean type operands |
7896 | ||
70482933 RK |
7897 | if Is_Boolean_Type (Typ1) then |
7898 | Adjust_Condition (Op1); | |
7899 | Adjust_Condition (Op2); | |
7900 | Set_Etype (N, Standard_Boolean); | |
7901 | Adjust_Result_Type (N, Typ); | |
7902 | end if; | |
7903 | ||
7904 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
7905 | |
7906 | -- If we still have comparison, and Vax_Float type, process it | |
7907 | ||
7908 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
7909 | Expand_Vax_Comparison (N); | |
7910 | return; | |
7911 | end if; | |
0580d807 AC |
7912 | |
7913 | Optimize_Length_Comparison (N); | |
70482933 RK |
7914 | end Expand_N_Op_Lt; |
7915 | ||
7916 | ----------------------- | |
7917 | -- Expand_N_Op_Minus -- | |
7918 | ----------------------- | |
7919 | ||
7920 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
7921 | Loc : constant Source_Ptr := Sloc (N); | |
7922 | Typ : constant Entity_Id := Etype (N); | |
7923 | ||
7924 | begin | |
7925 | Unary_Op_Validity_Checks (N); | |
7926 | ||
b6b5cca8 AC |
7927 | -- Check for MINIMIZED/ELIMINATED overflow mode |
7928 | ||
7929 | if Minimized_Eliminated_Overflow_Check (N) then | |
7930 | Apply_Arithmetic_Overflow_Check (N); | |
7931 | return; | |
7932 | end if; | |
7933 | ||
07fc65c4 | 7934 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
7935 | and then Is_Signed_Integer_Type (Etype (N)) |
7936 | and then Do_Overflow_Check (N) | |
7937 | then | |
7938 | -- Software overflow checking expands -expr into (0 - expr) | |
7939 | ||
7940 | Rewrite (N, | |
7941 | Make_Op_Subtract (Loc, | |
7942 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
7943 | Right_Opnd => Right_Opnd (N))); | |
7944 | ||
7945 | Analyze_And_Resolve (N, Typ); | |
7946 | ||
7947 | -- Vax floating-point types case | |
7948 | ||
7949 | elsif Vax_Float (Etype (N)) then | |
7950 | Expand_Vax_Arith (N); | |
7951 | end if; | |
7952 | end Expand_N_Op_Minus; | |
7953 | ||
7954 | --------------------- | |
7955 | -- Expand_N_Op_Mod -- | |
7956 | --------------------- | |
7957 | ||
7958 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
7959 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 7960 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
7961 | DDC : constant Boolean := Do_Division_Check (N); |
7962 | ||
b6b5cca8 AC |
7963 | Left : Node_Id; |
7964 | Right : Node_Id; | |
7965 | ||
70482933 RK |
7966 | LLB : Uint; |
7967 | Llo : Uint; | |
7968 | Lhi : Uint; | |
7969 | LOK : Boolean; | |
7970 | Rlo : Uint; | |
7971 | Rhi : Uint; | |
7972 | ROK : Boolean; | |
7973 | ||
1033834f RD |
7974 | pragma Warnings (Off, Lhi); |
7975 | ||
70482933 RK |
7976 | begin |
7977 | Binary_Op_Validity_Checks (N); | |
7978 | ||
b6b5cca8 AC |
7979 | -- Check for MINIMIZED/ELIMINATED overflow mode |
7980 | ||
7981 | if Minimized_Eliminated_Overflow_Check (N) then | |
7982 | Apply_Arithmetic_Overflow_Check (N); | |
7983 | return; | |
7984 | end if; | |
7985 | ||
9a6dc470 RD |
7986 | if Is_Integer_Type (Etype (N)) then |
7987 | Apply_Divide_Checks (N); | |
b6b5cca8 AC |
7988 | |
7989 | -- All done if we don't have a MOD any more, which can happen as a | |
7990 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
7991 | ||
7992 | if Nkind (N) /= N_Op_Mod then | |
7993 | return; | |
7994 | end if; | |
9a6dc470 RD |
7995 | end if; |
7996 | ||
b6b5cca8 AC |
7997 | -- Proceed with expansion of mod operator |
7998 | ||
7999 | Left := Left_Opnd (N); | |
8000 | Right := Right_Opnd (N); | |
8001 | ||
5d5e9775 AC |
8002 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
8003 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); | |
70482933 | 8004 | |
2c9f8c0a AC |
8005 | -- Convert mod to rem if operands are both known to be non-negative, or |
8006 | -- both known to be non-positive (these are the cases in which rem and | |
8007 | -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite | |
8008 | -- likely that this will improve the quality of code, (the operation now | |
8009 | -- corresponds to the hardware remainder), and it does not seem likely | |
8010 | -- that it could be harmful. It also avoids some cases of the elaborate | |
8011 | -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %). | |
8012 | ||
8013 | if (LOK and ROK) | |
8014 | and then ((Llo >= 0 and then Rlo >= 0) | |
8015 | or else | |
8016 | (Lhi <= 0 and then Rhi <= 0)) | |
8017 | then | |
70482933 RK |
8018 | Rewrite (N, |
8019 | Make_Op_Rem (Sloc (N), | |
8020 | Left_Opnd => Left_Opnd (N), | |
8021 | Right_Opnd => Right_Opnd (N))); | |
8022 | ||
685094bf RD |
8023 | -- Instead of reanalyzing the node we do the analysis manually. This |
8024 | -- avoids anomalies when the replacement is done in an instance and | |
8025 | -- is epsilon more efficient. | |
70482933 RK |
8026 | |
8027 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 8028 | Set_Etype (N, Typ); |
70482933 RK |
8029 | Set_Do_Division_Check (N, DDC); |
8030 | Expand_N_Op_Rem (N); | |
8031 | Set_Analyzed (N); | |
2c9f8c0a | 8032 | return; |
70482933 RK |
8033 | |
8034 | -- Otherwise, normal mod processing | |
8035 | ||
8036 | else | |
fbf5a39b AC |
8037 | -- Apply optimization x mod 1 = 0. We don't really need that with |
8038 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
8039 | -- certainly harmless. | |
8040 | ||
8041 | if Is_Integer_Type (Etype (N)) | |
8042 | and then Compile_Time_Known_Value (Right) | |
8043 | and then Expr_Value (Right) = Uint_1 | |
8044 | then | |
abcbd24c ST |
8045 | -- Call Remove_Side_Effects to ensure that any side effects in |
8046 | -- the ignored left operand (in particular function calls to | |
8047 | -- user defined functions) are properly executed. | |
8048 | ||
8049 | Remove_Side_Effects (Left); | |
8050 | ||
fbf5a39b AC |
8051 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
8052 | Analyze_And_Resolve (N, Typ); | |
8053 | return; | |
8054 | end if; | |
8055 | ||
2c9f8c0a AC |
8056 | -- If we still have a mod operator and we are in Modify_Tree_For_C |
8057 | -- mode, and we have a signed integer type, then here is where we do | |
8058 | -- the rewrite in terms of Rem. Note this rewrite bypasses the need | |
8059 | -- for the special handling of the annoying case of largest negative | |
8060 | -- number mod minus one. | |
8061 | ||
8062 | if Nkind (N) = N_Op_Mod | |
8063 | and then Is_Signed_Integer_Type (Typ) | |
8064 | and then Modify_Tree_For_C | |
8065 | then | |
8066 | -- In the general case, we expand A mod B as | |
8067 | ||
8068 | -- Tnn : constant typ := A rem B; | |
8069 | -- .. | |
8070 | -- (if (A >= 0) = (B >= 0) then Tnn | |
8071 | -- elsif Tnn = 0 then 0 | |
8072 | -- else Tnn + B) | |
8073 | ||
8074 | -- The comparison can be written simply as A >= 0 if we know that | |
8075 | -- B >= 0 which is a very common case. | |
8076 | ||
8077 | -- An important optimization is when B is known at compile time | |
8078 | -- to be 2**K for some constant. In this case we can simply AND | |
8079 | -- the left operand with the bit string 2**K-1 (i.e. K 1-bits) | |
8080 | -- and that works for both the positive and negative cases. | |
8081 | ||
8082 | declare | |
8083 | P2 : constant Nat := Power_Of_Two (Right); | |
8084 | ||
8085 | begin | |
8086 | if P2 /= 0 then | |
8087 | Rewrite (N, | |
8088 | Unchecked_Convert_To (Typ, | |
8089 | Make_Op_And (Loc, | |
8090 | Left_Opnd => | |
8091 | Unchecked_Convert_To | |
8092 | (Corresponding_Unsigned_Type (Typ), Left), | |
8093 | Right_Opnd => | |
8094 | Make_Integer_Literal (Loc, 2 ** P2 - 1)))); | |
8095 | Analyze_And_Resolve (N, Typ); | |
8096 | return; | |
8097 | end if; | |
8098 | end; | |
8099 | ||
8100 | -- Here for the full rewrite | |
8101 | ||
8102 | declare | |
8103 | Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N); | |
8104 | Cmp : Node_Id; | |
8105 | ||
8106 | begin | |
8107 | Cmp := | |
8108 | Make_Op_Ge (Loc, | |
8109 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
8110 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
8111 | ||
8112 | if not LOK or else Rlo < 0 then | |
8113 | Cmp := | |
8114 | Make_Op_Eq (Loc, | |
8115 | Left_Opnd => Cmp, | |
8116 | Right_Opnd => | |
8117 | Make_Op_Ge (Loc, | |
8118 | Left_Opnd => Duplicate_Subexpr_No_Checks (Right), | |
8119 | Right_Opnd => Make_Integer_Literal (Loc, 0))); | |
8120 | end if; | |
8121 | ||
8122 | Insert_Action (N, | |
8123 | Make_Object_Declaration (Loc, | |
8124 | Defining_Identifier => Tnn, | |
8125 | Constant_Present => True, | |
8126 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
8127 | Expression => | |
8128 | Make_Op_Rem (Loc, | |
8129 | Left_Opnd => Left, | |
8130 | Right_Opnd => Right))); | |
8131 | ||
8132 | Rewrite (N, | |
8133 | Make_If_Expression (Loc, | |
8134 | Expressions => New_List ( | |
8135 | Cmp, | |
8136 | New_Occurrence_Of (Tnn, Loc), | |
8137 | Make_If_Expression (Loc, | |
8138 | Is_Elsif => True, | |
8139 | Expressions => New_List ( | |
8140 | Make_Op_Eq (Loc, | |
8141 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8142 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
8143 | Make_Integer_Literal (Loc, 0), | |
8144 | Make_Op_Add (Loc, | |
8145 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8146 | Right_Opnd => | |
8147 | Duplicate_Subexpr_No_Checks (Right))))))); | |
8148 | ||
8149 | Analyze_And_Resolve (N, Typ); | |
8150 | return; | |
8151 | end; | |
8152 | end if; | |
8153 | ||
8154 | -- Deal with annoying case of largest negative number mod minus one. | |
8155 | -- Gigi may not handle this case correctly, because on some targets, | |
8156 | -- the mod value is computed using a divide instruction which gives | |
8157 | -- an overflow trap for this case. | |
b9daa96e AC |
8158 | |
8159 | -- It would be a bit more efficient to figure out which targets | |
8160 | -- this is really needed for, but in practice it is reasonable | |
8161 | -- to do the following special check in all cases, since it means | |
8162 | -- we get a clearer message, and also the overhead is minimal given | |
8163 | -- that division is expensive in any case. | |
70482933 | 8164 | |
685094bf RD |
8165 | -- In fact the check is quite easy, if the right operand is -1, then |
8166 | -- the mod value is always 0, and we can just ignore the left operand | |
8167 | -- completely in this case. | |
70482933 | 8168 | |
9a6dc470 RD |
8169 | -- This only applies if we still have a mod operator. Skip if we |
8170 | -- have already rewritten this (e.g. in the case of eliminated | |
8171 | -- overflow checks which have driven us into bignum mode). | |
fbf5a39b | 8172 | |
9a6dc470 | 8173 | if Nkind (N) = N_Op_Mod then |
70482933 | 8174 | |
9a6dc470 RD |
8175 | -- The operand type may be private (e.g. in the expansion of an |
8176 | -- intrinsic operation) so we must use the underlying type to get | |
8177 | -- the bounds, and convert the literals explicitly. | |
70482933 | 8178 | |
9a6dc470 RD |
8179 | LLB := |
8180 | Expr_Value | |
8181 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
8182 | ||
8183 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
761f7dcb | 8184 | and then ((not LOK) or else (Llo = LLB)) |
9a6dc470 RD |
8185 | then |
8186 | Rewrite (N, | |
9b16cb57 | 8187 | Make_If_Expression (Loc, |
9a6dc470 RD |
8188 | Expressions => New_List ( |
8189 | Make_Op_Eq (Loc, | |
8190 | Left_Opnd => Duplicate_Subexpr (Right), | |
8191 | Right_Opnd => | |
8192 | Unchecked_Convert_To (Typ, | |
8193 | Make_Integer_Literal (Loc, -1))), | |
8194 | Unchecked_Convert_To (Typ, | |
8195 | Make_Integer_Literal (Loc, Uint_0)), | |
8196 | Relocate_Node (N)))); | |
8197 | ||
8198 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
8199 | Analyze_And_Resolve (N, Typ); | |
8200 | end if; | |
70482933 RK |
8201 | end if; |
8202 | end if; | |
8203 | end Expand_N_Op_Mod; | |
8204 | ||
8205 | -------------------------- | |
8206 | -- Expand_N_Op_Multiply -- | |
8207 | -------------------------- | |
8208 | ||
8209 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
8210 | Loc : constant Source_Ptr := Sloc (N); |
8211 | Lop : constant Node_Id := Left_Opnd (N); | |
8212 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 8213 | |
abcbd24c | 8214 | Lp2 : constant Boolean := |
533369aa | 8215 | Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop); |
abcbd24c | 8216 | Rp2 : constant Boolean := |
533369aa | 8217 | Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop); |
fbf5a39b | 8218 | |
70482933 RK |
8219 | Ltyp : constant Entity_Id := Etype (Lop); |
8220 | Rtyp : constant Entity_Id := Etype (Rop); | |
8221 | Typ : Entity_Id := Etype (N); | |
8222 | ||
8223 | begin | |
8224 | Binary_Op_Validity_Checks (N); | |
8225 | ||
b6b5cca8 AC |
8226 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8227 | ||
8228 | if Minimized_Eliminated_Overflow_Check (N) then | |
8229 | Apply_Arithmetic_Overflow_Check (N); | |
8230 | return; | |
8231 | end if; | |
8232 | ||
70482933 RK |
8233 | -- Special optimizations for integer types |
8234 | ||
8235 | if Is_Integer_Type (Typ) then | |
8236 | ||
abcbd24c | 8237 | -- N * 0 = 0 for integer types |
70482933 | 8238 | |
abcbd24c ST |
8239 | if Compile_Time_Known_Value (Rop) |
8240 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 8241 | then |
abcbd24c ST |
8242 | -- Call Remove_Side_Effects to ensure that any side effects in |
8243 | -- the ignored left operand (in particular function calls to | |
8244 | -- user defined functions) are properly executed. | |
8245 | ||
8246 | Remove_Side_Effects (Lop); | |
8247 | ||
8248 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
8249 | Analyze_And_Resolve (N, Typ); | |
8250 | return; | |
8251 | end if; | |
8252 | ||
8253 | -- Similar handling for 0 * N = 0 | |
8254 | ||
8255 | if Compile_Time_Known_Value (Lop) | |
8256 | and then Expr_Value (Lop) = Uint_0 | |
8257 | then | |
8258 | Remove_Side_Effects (Rop); | |
70482933 RK |
8259 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
8260 | Analyze_And_Resolve (N, Typ); | |
8261 | return; | |
8262 | end if; | |
8263 | ||
8264 | -- N * 1 = 1 * N = N for integer types | |
8265 | ||
fbf5a39b AC |
8266 | -- This optimisation is not done if we are going to |
8267 | -- rewrite the product 1 * 2 ** N to a shift. | |
8268 | ||
8269 | if Compile_Time_Known_Value (Rop) | |
8270 | and then Expr_Value (Rop) = Uint_1 | |
8271 | and then not Lp2 | |
70482933 | 8272 | then |
fbf5a39b | 8273 | Rewrite (N, Lop); |
70482933 RK |
8274 | return; |
8275 | ||
fbf5a39b AC |
8276 | elsif Compile_Time_Known_Value (Lop) |
8277 | and then Expr_Value (Lop) = Uint_1 | |
8278 | and then not Rp2 | |
70482933 | 8279 | then |
fbf5a39b | 8280 | Rewrite (N, Rop); |
70482933 RK |
8281 | return; |
8282 | end if; | |
8283 | end if; | |
8284 | ||
70482933 RK |
8285 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
8286 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
8287 | -- operand is an integer, as required for this to work. | |
8288 | ||
fbf5a39b AC |
8289 | if Rp2 then |
8290 | if Lp2 then | |
70482933 | 8291 | |
fbf5a39b | 8292 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
8293 | |
8294 | Rewrite (N, | |
8295 | Make_Op_Expon (Loc, | |
8296 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
8297 | Right_Opnd => | |
8298 | Make_Op_Add (Loc, | |
8299 | Left_Opnd => Right_Opnd (Lop), | |
8300 | Right_Opnd => Right_Opnd (Rop)))); | |
8301 | Analyze_And_Resolve (N, Typ); | |
8302 | return; | |
8303 | ||
8304 | else | |
eefe3761 AC |
8305 | -- If the result is modular, perform the reduction of the result |
8306 | -- appropriately. | |
8307 | ||
8308 | if Is_Modular_Integer_Type (Typ) | |
8309 | and then not Non_Binary_Modulus (Typ) | |
8310 | then | |
8311 | Rewrite (N, | |
573e5dd6 RD |
8312 | Make_Op_And (Loc, |
8313 | Left_Opnd => | |
8314 | Make_Op_Shift_Left (Loc, | |
8315 | Left_Opnd => Lop, | |
8316 | Right_Opnd => | |
8317 | Convert_To (Standard_Natural, Right_Opnd (Rop))), | |
8318 | Right_Opnd => | |
eefe3761 | 8319 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); |
573e5dd6 | 8320 | |
eefe3761 AC |
8321 | else |
8322 | Rewrite (N, | |
8323 | Make_Op_Shift_Left (Loc, | |
8324 | Left_Opnd => Lop, | |
8325 | Right_Opnd => | |
8326 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
8327 | end if; | |
8328 | ||
70482933 RK |
8329 | Analyze_And_Resolve (N, Typ); |
8330 | return; | |
8331 | end if; | |
8332 | ||
8333 | -- Same processing for the operands the other way round | |
8334 | ||
fbf5a39b | 8335 | elsif Lp2 then |
eefe3761 AC |
8336 | if Is_Modular_Integer_Type (Typ) |
8337 | and then not Non_Binary_Modulus (Typ) | |
8338 | then | |
8339 | Rewrite (N, | |
573e5dd6 RD |
8340 | Make_Op_And (Loc, |
8341 | Left_Opnd => | |
8342 | Make_Op_Shift_Left (Loc, | |
8343 | Left_Opnd => Rop, | |
8344 | Right_Opnd => | |
8345 | Convert_To (Standard_Natural, Right_Opnd (Lop))), | |
8346 | Right_Opnd => | |
8347 | Make_Integer_Literal (Loc, Modulus (Typ) - 1))); | |
8348 | ||
eefe3761 AC |
8349 | else |
8350 | Rewrite (N, | |
8351 | Make_Op_Shift_Left (Loc, | |
8352 | Left_Opnd => Rop, | |
8353 | Right_Opnd => | |
8354 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
8355 | end if; | |
8356 | ||
70482933 RK |
8357 | Analyze_And_Resolve (N, Typ); |
8358 | return; | |
8359 | end if; | |
8360 | ||
8361 | -- Do required fixup of universal fixed operation | |
8362 | ||
8363 | if Typ = Universal_Fixed then | |
8364 | Fixup_Universal_Fixed_Operation (N); | |
8365 | Typ := Etype (N); | |
8366 | end if; | |
8367 | ||
8368 | -- Multiplications with fixed-point results | |
8369 | ||
8370 | if Is_Fixed_Point_Type (Typ) then | |
8371 | ||
685094bf RD |
8372 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
8373 | -- a semantic point of view such operations are simply integer | |
8374 | -- operations and will be treated that way. | |
70482933 RK |
8375 | |
8376 | if not Treat_Fixed_As_Integer (N) then | |
8377 | ||
8378 | -- Case of fixed * integer => fixed | |
8379 | ||
8380 | if Is_Integer_Type (Rtyp) then | |
8381 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
8382 | ||
8383 | -- Case of integer * fixed => fixed | |
8384 | ||
8385 | elsif Is_Integer_Type (Ltyp) then | |
8386 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
8387 | ||
8388 | -- Case of fixed * fixed => fixed | |
8389 | ||
8390 | else | |
8391 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
8392 | end if; | |
8393 | end if; | |
8394 | ||
685094bf RD |
8395 | -- Other cases of multiplication of fixed-point operands. Again we |
8396 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
8397 | |
8398 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
8399 | and then not Treat_Fixed_As_Integer (N) | |
8400 | then | |
8401 | if Is_Integer_Type (Typ) then | |
8402 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
8403 | else | |
8404 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
8405 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
8406 | end if; | |
8407 | ||
685094bf RD |
8408 | -- Mixed-mode operations can appear in a non-static universal context, |
8409 | -- in which case the integer argument must be converted explicitly. | |
70482933 | 8410 | |
533369aa | 8411 | elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then |
70482933 | 8412 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); |
70482933 RK |
8413 | Analyze_And_Resolve (Rop, Universal_Real); |
8414 | ||
533369aa | 8415 | elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then |
70482933 | 8416 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); |
70482933 RK |
8417 | Analyze_And_Resolve (Lop, Universal_Real); |
8418 | ||
8419 | -- Non-fixed point cases, check software overflow checking required | |
8420 | ||
8421 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
8422 | Apply_Arithmetic_Overflow_Check (N); | |
f02b8bb8 RD |
8423 | |
8424 | -- Deal with VAX float case | |
8425 | ||
8426 | elsif Vax_Float (Typ) then | |
8427 | Expand_Vax_Arith (N); | |
8428 | return; | |
70482933 RK |
8429 | end if; |
8430 | end Expand_N_Op_Multiply; | |
8431 | ||
8432 | -------------------- | |
8433 | -- Expand_N_Op_Ne -- | |
8434 | -------------------- | |
8435 | ||
70482933 | 8436 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 8437 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
8438 | |
8439 | begin | |
f02b8bb8 | 8440 | -- Case of elementary type with standard operator |
70482933 | 8441 | |
f02b8bb8 RD |
8442 | if Is_Elementary_Type (Typ) |
8443 | and then Sloc (Entity (N)) = Standard_Location | |
8444 | then | |
8445 | Binary_Op_Validity_Checks (N); | |
70482933 | 8446 | |
456cbfa5 | 8447 | -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if |
60b68e56 | 8448 | -- means we no longer have a /= operation, we are all done. |
456cbfa5 AC |
8449 | |
8450 | Expand_Compare_Minimize_Eliminate_Overflow (N); | |
8451 | ||
8452 | if Nkind (N) /= N_Op_Ne then | |
8453 | return; | |
8454 | end if; | |
8455 | ||
f02b8bb8 | 8456 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 8457 | |
f02b8bb8 RD |
8458 | if Is_Boolean_Type (Typ) then |
8459 | Adjust_Condition (Left_Opnd (N)); | |
8460 | Adjust_Condition (Right_Opnd (N)); | |
8461 | Set_Etype (N, Standard_Boolean); | |
8462 | Adjust_Result_Type (N, Typ); | |
8463 | end if; | |
fbf5a39b | 8464 | |
f02b8bb8 RD |
8465 | Rewrite_Comparison (N); |
8466 | ||
8467 | -- If we still have comparison for Vax_Float, process it | |
8468 | ||
8469 | if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then | |
8470 | Expand_Vax_Comparison (N); | |
8471 | return; | |
8472 | end if; | |
8473 | ||
8474 | -- For all cases other than elementary types, we rewrite node as the | |
8475 | -- negation of an equality operation, and reanalyze. The equality to be | |
8476 | -- used is defined in the same scope and has the same signature. This | |
8477 | -- signature must be set explicitly since in an instance it may not have | |
8478 | -- the same visibility as in the generic unit. This avoids duplicating | |
8479 | -- or factoring the complex code for record/array equality tests etc. | |
8480 | ||
8481 | else | |
8482 | declare | |
8483 | Loc : constant Source_Ptr := Sloc (N); | |
8484 | Neg : Node_Id; | |
8485 | Ne : constant Entity_Id := Entity (N); | |
8486 | ||
8487 | begin | |
8488 | Binary_Op_Validity_Checks (N); | |
8489 | ||
8490 | Neg := | |
8491 | Make_Op_Not (Loc, | |
8492 | Right_Opnd => | |
8493 | Make_Op_Eq (Loc, | |
8494 | Left_Opnd => Left_Opnd (N), | |
8495 | Right_Opnd => Right_Opnd (N))); | |
8496 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
8497 | ||
8498 | if Scope (Ne) /= Standard_Standard then | |
8499 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
8500 | end if; | |
8501 | ||
4637729f | 8502 | -- For navigation purposes, we want to treat the inequality as an |
f02b8bb8 | 8503 | -- implicit reference to the corresponding equality. Preserve the |
4637729f | 8504 | -- Comes_From_ source flag to generate proper Xref entries. |
f02b8bb8 RD |
8505 | |
8506 | Preserve_Comes_From_Source (Neg, N); | |
8507 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
8508 | Rewrite (N, Neg); | |
8509 | Analyze_And_Resolve (N, Standard_Boolean); | |
8510 | end; | |
8511 | end if; | |
0580d807 AC |
8512 | |
8513 | Optimize_Length_Comparison (N); | |
70482933 RK |
8514 | end Expand_N_Op_Ne; |
8515 | ||
8516 | --------------------- | |
8517 | -- Expand_N_Op_Not -- | |
8518 | --------------------- | |
8519 | ||
685094bf | 8520 | -- If the argument is other than a Boolean array type, there is no special |
c77599d5 | 8521 | -- expansion required, except for VMS operations on signed integers. |
70482933 RK |
8522 | |
8523 | -- For the packed case, we call the special routine in Exp_Pakd, except | |
8524 | -- that if the component size is greater than one, we use the standard | |
8525 | -- routine generating a gruesome loop (it is so peculiar to have packed | |
685094bf RD |
8526 | -- arrays with non-standard Boolean representations anyway, so it does not |
8527 | -- matter that we do not handle this case efficiently). | |
70482933 | 8528 | |
685094bf RD |
8529 | -- For the unpacked case (and for the special packed case where we have non |
8530 | -- standard Booleans, as discussed above), we generate and insert into the | |
8531 | -- tree the following function definition: | |
70482933 RK |
8532 | |
8533 | -- function Nnnn (A : arr) is | |
8534 | -- B : arr; | |
8535 | -- begin | |
8536 | -- for J in a'range loop | |
8537 | -- B (J) := not A (J); | |
8538 | -- end loop; | |
8539 | -- return B; | |
8540 | -- end Nnnn; | |
8541 | ||
8542 | -- Here arr is the actual subtype of the parameter (and hence always | |
8543 | -- constrained). Then we replace the not with a call to this function. | |
8544 | ||
8545 | procedure Expand_N_Op_Not (N : Node_Id) is | |
8546 | Loc : constant Source_Ptr := Sloc (N); | |
8547 | Typ : constant Entity_Id := Etype (N); | |
8548 | Opnd : Node_Id; | |
8549 | Arr : Entity_Id; | |
8550 | A : Entity_Id; | |
8551 | B : Entity_Id; | |
8552 | J : Entity_Id; | |
8553 | A_J : Node_Id; | |
8554 | B_J : Node_Id; | |
8555 | ||
8556 | Func_Name : Entity_Id; | |
8557 | Loop_Statement : Node_Id; | |
8558 | ||
8559 | begin | |
8560 | Unary_Op_Validity_Checks (N); | |
8561 | ||
8562 | -- For boolean operand, deal with non-standard booleans | |
8563 | ||
8564 | if Is_Boolean_Type (Typ) then | |
8565 | Adjust_Condition (Right_Opnd (N)); | |
8566 | Set_Etype (N, Standard_Boolean); | |
8567 | Adjust_Result_Type (N, Typ); | |
8568 | return; | |
8569 | end if; | |
8570 | ||
880dabb5 AC |
8571 | -- For the VMS "not" on signed integer types, use conversion to and from |
8572 | -- a predefined modular type. | |
c77599d5 AC |
8573 | |
8574 | if Is_VMS_Operator (Entity (N)) then | |
8575 | declare | |
9bebf0e9 AC |
8576 | Rtyp : Entity_Id; |
8577 | Utyp : Entity_Id; | |
8578 | ||
c77599d5 | 8579 | begin |
9bebf0e9 AC |
8580 | -- If this is a derived type, retrieve original VMS type so that |
8581 | -- the proper sized type is used for intermediate values. | |
8582 | ||
8583 | if Is_Derived_Type (Typ) then | |
8584 | Rtyp := First_Subtype (Etype (Typ)); | |
8585 | else | |
8586 | Rtyp := Typ; | |
8587 | end if; | |
8588 | ||
0d901290 AC |
8589 | -- The proper unsigned type must have a size compatible with the |
8590 | -- operand, to prevent misalignment. | |
9bebf0e9 AC |
8591 | |
8592 | if RM_Size (Rtyp) <= 8 then | |
8593 | Utyp := RTE (RE_Unsigned_8); | |
8594 | ||
8595 | elsif RM_Size (Rtyp) <= 16 then | |
8596 | Utyp := RTE (RE_Unsigned_16); | |
8597 | ||
8598 | elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then | |
bc20523f | 8599 | Utyp := RTE (RE_Unsigned_32); |
9bebf0e9 AC |
8600 | |
8601 | else | |
8602 | Utyp := RTE (RE_Long_Long_Unsigned); | |
8603 | end if; | |
8604 | ||
c77599d5 AC |
8605 | Rewrite (N, |
8606 | Unchecked_Convert_To (Typ, | |
9bebf0e9 AC |
8607 | Make_Op_Not (Loc, |
8608 | Unchecked_Convert_To (Utyp, Right_Opnd (N))))); | |
c77599d5 AC |
8609 | Analyze_And_Resolve (N, Typ); |
8610 | return; | |
8611 | end; | |
8612 | end if; | |
8613 | ||
da94696d | 8614 | -- Only array types need any other processing |
70482933 | 8615 | |
da94696d | 8616 | if not Is_Array_Type (Typ) then |
70482933 RK |
8617 | return; |
8618 | end if; | |
8619 | ||
a9d8907c JM |
8620 | -- Case of array operand. If bit packed with a component size of 1, |
8621 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 8622 | |
a9d8907c JM |
8623 | if Is_Bit_Packed_Array (Typ) |
8624 | and then Component_Size (Typ) = 1 | |
8625 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
8626 | then | |
70482933 RK |
8627 | Expand_Packed_Not (N); |
8628 | return; | |
8629 | end if; | |
8630 | ||
fbf5a39b AC |
8631 | -- Case of array operand which is not bit-packed. If the context is |
8632 | -- a safe assignment, call in-place operation, If context is a larger | |
8633 | -- boolean expression in the context of a safe assignment, expansion is | |
8634 | -- done by enclosing operation. | |
70482933 RK |
8635 | |
8636 | Opnd := Relocate_Node (Right_Opnd (N)); | |
8637 | Convert_To_Actual_Subtype (Opnd); | |
8638 | Arr := Etype (Opnd); | |
8639 | Ensure_Defined (Arr, N); | |
b4592168 | 8640 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 8641 | |
fbf5a39b AC |
8642 | if Nkind (Parent (N)) = N_Assignment_Statement then |
8643 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
8644 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
8645 | return; | |
8646 | ||
5e1c00fa | 8647 | -- Special case the negation of a binary operation |
fbf5a39b | 8648 | |
303b4d58 | 8649 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 8650 | and then Safe_In_Place_Array_Op |
303b4d58 | 8651 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
8652 | then |
8653 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
8654 | return; | |
8655 | end if; | |
8656 | ||
8657 | elsif Nkind (Parent (N)) in N_Binary_Op | |
8658 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
8659 | then | |
8660 | declare | |
8661 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
8662 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
8663 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
8664 | ||
8665 | begin | |
8666 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
fbf5a39b | 8667 | |
aa9a7dd7 AC |
8668 | -- (not A) op (not B) can be reduced to a single call |
8669 | ||
8670 | if N = Op1 and then Nkind (Op2) = N_Op_Not then | |
fbf5a39b AC |
8671 | return; |
8672 | ||
bed8af19 AC |
8673 | elsif N = Op2 and then Nkind (Op1) = N_Op_Not then |
8674 | return; | |
8675 | ||
aa9a7dd7 | 8676 | -- A xor (not B) can also be special-cased |
fbf5a39b | 8677 | |
aa9a7dd7 | 8678 | elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then |
fbf5a39b AC |
8679 | return; |
8680 | end if; | |
8681 | end if; | |
8682 | end; | |
8683 | end if; | |
8684 | ||
70482933 RK |
8685 | A := Make_Defining_Identifier (Loc, Name_uA); |
8686 | B := Make_Defining_Identifier (Loc, Name_uB); | |
8687 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
8688 | ||
8689 | A_J := | |
8690 | Make_Indexed_Component (Loc, | |
e4494292 RD |
8691 | Prefix => New_Occurrence_Of (A, Loc), |
8692 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
8693 | |
8694 | B_J := | |
8695 | Make_Indexed_Component (Loc, | |
e4494292 RD |
8696 | Prefix => New_Occurrence_Of (B, Loc), |
8697 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
8698 | |
8699 | Loop_Statement := | |
8700 | Make_Implicit_Loop_Statement (N, | |
8701 | Identifier => Empty, | |
8702 | ||
8703 | Iteration_Scheme => | |
8704 | Make_Iteration_Scheme (Loc, | |
8705 | Loop_Parameter_Specification => | |
8706 | Make_Loop_Parameter_Specification (Loc, | |
0d901290 | 8707 | Defining_Identifier => J, |
70482933 RK |
8708 | Discrete_Subtype_Definition => |
8709 | Make_Attribute_Reference (Loc, | |
0d901290 | 8710 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
8711 | Attribute_Name => Name_Range))), |
8712 | ||
8713 | Statements => New_List ( | |
8714 | Make_Assignment_Statement (Loc, | |
8715 | Name => B_J, | |
8716 | Expression => Make_Op_Not (Loc, A_J)))); | |
8717 | ||
191fcb3a | 8718 | Func_Name := Make_Temporary (Loc, 'N'); |
70482933 RK |
8719 | Set_Is_Inlined (Func_Name); |
8720 | ||
8721 | Insert_Action (N, | |
8722 | Make_Subprogram_Body (Loc, | |
8723 | Specification => | |
8724 | Make_Function_Specification (Loc, | |
8725 | Defining_Unit_Name => Func_Name, | |
8726 | Parameter_Specifications => New_List ( | |
8727 | Make_Parameter_Specification (Loc, | |
8728 | Defining_Identifier => A, | |
e4494292 RD |
8729 | Parameter_Type => New_Occurrence_Of (Typ, Loc))), |
8730 | Result_Definition => New_Occurrence_Of (Typ, Loc)), | |
70482933 RK |
8731 | |
8732 | Declarations => New_List ( | |
8733 | Make_Object_Declaration (Loc, | |
8734 | Defining_Identifier => B, | |
e4494292 | 8735 | Object_Definition => New_Occurrence_Of (Arr, Loc))), |
70482933 RK |
8736 | |
8737 | Handled_Statement_Sequence => | |
8738 | Make_Handled_Sequence_Of_Statements (Loc, | |
8739 | Statements => New_List ( | |
8740 | Loop_Statement, | |
d766cee3 | 8741 | Make_Simple_Return_Statement (Loc, |
0d901290 | 8742 | Expression => Make_Identifier (Loc, Chars (B))))))); |
70482933 RK |
8743 | |
8744 | Rewrite (N, | |
8745 | Make_Function_Call (Loc, | |
e4494292 | 8746 | Name => New_Occurrence_Of (Func_Name, Loc), |
70482933 RK |
8747 | Parameter_Associations => New_List (Opnd))); |
8748 | ||
8749 | Analyze_And_Resolve (N, Typ); | |
8750 | end Expand_N_Op_Not; | |
8751 | ||
8752 | -------------------- | |
8753 | -- Expand_N_Op_Or -- | |
8754 | -------------------- | |
8755 | ||
8756 | procedure Expand_N_Op_Or (N : Node_Id) is | |
8757 | Typ : constant Entity_Id := Etype (N); | |
8758 | ||
8759 | begin | |
8760 | Binary_Op_Validity_Checks (N); | |
8761 | ||
8762 | if Is_Array_Type (Etype (N)) then | |
8763 | Expand_Boolean_Operator (N); | |
8764 | ||
8765 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
8766 | Adjust_Condition (Left_Opnd (N)); |
8767 | Adjust_Condition (Right_Opnd (N)); | |
8768 | Set_Etype (N, Standard_Boolean); | |
8769 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
8770 | |
8771 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
8772 | Expand_Intrinsic_Call (N, Entity (N)); | |
8773 | ||
70482933 RK |
8774 | end if; |
8775 | end Expand_N_Op_Or; | |
8776 | ||
8777 | ---------------------- | |
8778 | -- Expand_N_Op_Plus -- | |
8779 | ---------------------- | |
8780 | ||
8781 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
8782 | begin | |
8783 | Unary_Op_Validity_Checks (N); | |
b6b5cca8 AC |
8784 | |
8785 | -- Check for MINIMIZED/ELIMINATED overflow mode | |
8786 | ||
8787 | if Minimized_Eliminated_Overflow_Check (N) then | |
8788 | Apply_Arithmetic_Overflow_Check (N); | |
8789 | return; | |
8790 | end if; | |
70482933 RK |
8791 | end Expand_N_Op_Plus; |
8792 | ||
8793 | --------------------- | |
8794 | -- Expand_N_Op_Rem -- | |
8795 | --------------------- | |
8796 | ||
8797 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
8798 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 8799 | Typ : constant Entity_Id := Etype (N); |
70482933 | 8800 | |
b6b5cca8 AC |
8801 | Left : Node_Id; |
8802 | Right : Node_Id; | |
70482933 | 8803 | |
5d5e9775 AC |
8804 | Lo : Uint; |
8805 | Hi : Uint; | |
8806 | OK : Boolean; | |
70482933 | 8807 | |
5d5e9775 AC |
8808 | Lneg : Boolean; |
8809 | Rneg : Boolean; | |
8810 | -- Set if corresponding operand can be negative | |
8811 | ||
8812 | pragma Unreferenced (Hi); | |
1033834f | 8813 | |
70482933 RK |
8814 | begin |
8815 | Binary_Op_Validity_Checks (N); | |
8816 | ||
b6b5cca8 AC |
8817 | -- Check for MINIMIZED/ELIMINATED overflow mode |
8818 | ||
8819 | if Minimized_Eliminated_Overflow_Check (N) then | |
8820 | Apply_Arithmetic_Overflow_Check (N); | |
8821 | return; | |
8822 | end if; | |
8823 | ||
70482933 | 8824 | if Is_Integer_Type (Etype (N)) then |
a91e9ac7 | 8825 | Apply_Divide_Checks (N); |
b6b5cca8 AC |
8826 | |
8827 | -- All done if we don't have a REM any more, which can happen as a | |
8828 | -- result of overflow expansion in MINIMIZED or ELIMINATED modes. | |
8829 | ||
8830 | if Nkind (N) /= N_Op_Rem then | |
8831 | return; | |
8832 | end if; | |
70482933 RK |
8833 | end if; |
8834 | ||
b6b5cca8 AC |
8835 | -- Proceed with expansion of REM |
8836 | ||
8837 | Left := Left_Opnd (N); | |
8838 | Right := Right_Opnd (N); | |
8839 | ||
685094bf RD |
8840 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
8841 | -- but it is useful with other back ends (e.g. AAMP), and is certainly | |
8842 | -- harmless. | |
fbf5a39b AC |
8843 | |
8844 | if Is_Integer_Type (Etype (N)) | |
8845 | and then Compile_Time_Known_Value (Right) | |
8846 | and then Expr_Value (Right) = Uint_1 | |
8847 | then | |
abcbd24c ST |
8848 | -- Call Remove_Side_Effects to ensure that any side effects in the |
8849 | -- ignored left operand (in particular function calls to user defined | |
8850 | -- functions) are properly executed. | |
8851 | ||
8852 | Remove_Side_Effects (Left); | |
8853 | ||
fbf5a39b AC |
8854 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
8855 | Analyze_And_Resolve (N, Typ); | |
8856 | return; | |
8857 | end if; | |
8858 | ||
685094bf | 8859 | -- Deal with annoying case of largest negative number remainder minus |
b9daa96e AC |
8860 | -- one. Gigi may not handle this case correctly, because on some |
8861 | -- targets, the mod value is computed using a divide instruction | |
8862 | -- which gives an overflow trap for this case. | |
8863 | ||
8864 | -- It would be a bit more efficient to figure out which targets this | |
8865 | -- is really needed for, but in practice it is reasonable to do the | |
8866 | -- following special check in all cases, since it means we get a clearer | |
8867 | -- message, and also the overhead is minimal given that division is | |
8868 | -- expensive in any case. | |
70482933 | 8869 | |
685094bf RD |
8870 | -- In fact the check is quite easy, if the right operand is -1, then |
8871 | -- the remainder is always 0, and we can just ignore the left operand | |
8872 | -- completely in this case. | |
70482933 | 8873 | |
5d5e9775 AC |
8874 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); |
8875 | Lneg := (not OK) or else Lo < 0; | |
fbf5a39b | 8876 | |
5d5e9775 AC |
8877 | Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True); |
8878 | Rneg := (not OK) or else Lo < 0; | |
fbf5a39b | 8879 | |
5d5e9775 AC |
8880 | -- We won't mess with trying to find out if the left operand can really |
8881 | -- be the largest negative number (that's a pain in the case of private | |
8882 | -- types and this is really marginal). We will just assume that we need | |
8883 | -- the test if the left operand can be negative at all. | |
fbf5a39b | 8884 | |
5d5e9775 | 8885 | if Lneg and Rneg then |
70482933 | 8886 | Rewrite (N, |
9b16cb57 | 8887 | Make_If_Expression (Loc, |
70482933 RK |
8888 | Expressions => New_List ( |
8889 | Make_Op_Eq (Loc, | |
0d901290 | 8890 | Left_Opnd => Duplicate_Subexpr (Right), |
70482933 | 8891 | Right_Opnd => |
0d901290 | 8892 | Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))), |
70482933 | 8893 | |
fbf5a39b AC |
8894 | Unchecked_Convert_To (Typ, |
8895 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
8896 | |
8897 | Relocate_Node (N)))); | |
8898 | ||
8899 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
8900 | Analyze_And_Resolve (N, Typ); | |
8901 | end if; | |
8902 | end Expand_N_Op_Rem; | |
8903 | ||
8904 | ----------------------------- | |
8905 | -- Expand_N_Op_Rotate_Left -- | |
8906 | ----------------------------- | |
8907 | ||
8908 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
8909 | begin | |
8910 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
8911 | |
8912 | -- If we are in Modify_Tree_For_C mode, there is no rotate left in C, | |
8913 | -- so we rewrite in terms of logical shifts | |
8914 | ||
8915 | -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits) | |
8916 | ||
8917 | -- where Bits is the shift count mod Esize (the mod operation here | |
8918 | -- deals with ludicrous large shift counts, which are apparently OK). | |
8919 | ||
8920 | -- What about non-binary modulus ??? | |
8921 | ||
8922 | declare | |
8923 | Loc : constant Source_Ptr := Sloc (N); | |
8924 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
8925 | Typ : constant Entity_Id := Etype (N); | |
8926 | ||
8927 | begin | |
8928 | if Modify_Tree_For_C then | |
8929 | Rewrite (Right_Opnd (N), | |
8930 | Make_Op_Rem (Loc, | |
8931 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
8932 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
8933 | ||
8934 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
8935 | ||
8936 | Rewrite (N, | |
8937 | Make_Op_Or (Loc, | |
8938 | Left_Opnd => | |
8939 | Make_Op_Shift_Left (Loc, | |
8940 | Left_Opnd => Left_Opnd (N), | |
8941 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 8942 | |
5216b599 AC |
8943 | Right_Opnd => |
8944 | Make_Op_Shift_Right (Loc, | |
8945 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
8946 | Right_Opnd => | |
8947 | Make_Op_Subtract (Loc, | |
8948 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
8949 | Right_Opnd => | |
8950 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
8951 | ||
8952 | Analyze_And_Resolve (N, Typ); | |
8953 | end if; | |
8954 | end; | |
70482933 RK |
8955 | end Expand_N_Op_Rotate_Left; |
8956 | ||
8957 | ------------------------------ | |
8958 | -- Expand_N_Op_Rotate_Right -- | |
8959 | ------------------------------ | |
8960 | ||
8961 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
8962 | begin | |
8963 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
8964 | |
8965 | -- If we are in Modify_Tree_For_C mode, there is no rotate right in C, | |
8966 | -- so we rewrite in terms of logical shifts | |
8967 | ||
8968 | -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits) | |
8969 | ||
8970 | -- where Bits is the shift count mod Esize (the mod operation here | |
8971 | -- deals with ludicrous large shift counts, which are apparently OK). | |
8972 | ||
8973 | -- What about non-binary modulus ??? | |
8974 | ||
8975 | declare | |
8976 | Loc : constant Source_Ptr := Sloc (N); | |
8977 | Rtp : constant Entity_Id := Etype (Right_Opnd (N)); | |
8978 | Typ : constant Entity_Id := Etype (N); | |
8979 | ||
8980 | begin | |
8981 | Rewrite (Right_Opnd (N), | |
8982 | Make_Op_Rem (Loc, | |
8983 | Left_Opnd => Relocate_Node (Right_Opnd (N)), | |
8984 | Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ)))); | |
8985 | ||
8986 | Analyze_And_Resolve (Right_Opnd (N), Rtp); | |
8987 | ||
8988 | if Modify_Tree_For_C then | |
8989 | Rewrite (N, | |
8990 | Make_Op_Or (Loc, | |
8991 | Left_Opnd => | |
8992 | Make_Op_Shift_Right (Loc, | |
8993 | Left_Opnd => Left_Opnd (N), | |
8994 | Right_Opnd => Right_Opnd (N)), | |
e09a5598 | 8995 | |
5216b599 AC |
8996 | Right_Opnd => |
8997 | Make_Op_Shift_Left (Loc, | |
8998 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)), | |
8999 | Right_Opnd => | |
9000 | Make_Op_Subtract (Loc, | |
9001 | Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)), | |
9002 | Right_Opnd => | |
9003 | Duplicate_Subexpr_No_Checks (Right_Opnd (N)))))); | |
9004 | ||
9005 | Analyze_And_Resolve (N, Typ); | |
9006 | end if; | |
9007 | end; | |
70482933 RK |
9008 | end Expand_N_Op_Rotate_Right; |
9009 | ||
9010 | ---------------------------- | |
9011 | -- Expand_N_Op_Shift_Left -- | |
9012 | ---------------------------- | |
9013 | ||
e09a5598 AC |
9014 | -- Note: nothing in this routine depends on left as opposed to right shifts |
9015 | -- so we share the routine for expanding shift right operations. | |
9016 | ||
70482933 RK |
9017 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is |
9018 | begin | |
9019 | Binary_Op_Validity_Checks (N); | |
e09a5598 AC |
9020 | |
9021 | -- If we are in Modify_Tree_For_C mode, then ensure that the right | |
9022 | -- operand is not greater than the word size (since that would not | |
9023 | -- be defined properly by the corresponding C shift operator). | |
9024 | ||
9025 | if Modify_Tree_For_C then | |
9026 | declare | |
9027 | Right : constant Node_Id := Right_Opnd (N); | |
9028 | Loc : constant Source_Ptr := Sloc (Right); | |
9029 | Typ : constant Entity_Id := Etype (N); | |
9030 | Siz : constant Uint := Esize (Typ); | |
9031 | Orig : Node_Id; | |
9032 | OK : Boolean; | |
9033 | Lo : Uint; | |
9034 | Hi : Uint; | |
9035 | ||
9036 | begin | |
9037 | if Compile_Time_Known_Value (Right) then | |
9038 | if Expr_Value (Right) >= Siz then | |
9039 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
9040 | Analyze_And_Resolve (N, Typ); | |
9041 | end if; | |
9042 | ||
9043 | -- Not compile time known, find range | |
9044 | ||
9045 | else | |
9046 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); | |
9047 | ||
9048 | -- Nothing to do if known to be OK range, otherwise expand | |
9049 | ||
9050 | if not OK or else Hi >= Siz then | |
9051 | ||
9052 | -- Prevent recursion on copy of shift node | |
9053 | ||
9054 | Orig := Relocate_Node (N); | |
9055 | Set_Analyzed (Orig); | |
9056 | ||
9057 | -- Now do the rewrite | |
9058 | ||
9059 | Rewrite (N, | |
9060 | Make_If_Expression (Loc, | |
9061 | Expressions => New_List ( | |
9062 | Make_Op_Ge (Loc, | |
9063 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
9064 | Right_Opnd => Make_Integer_Literal (Loc, Siz)), | |
9065 | Make_Integer_Literal (Loc, 0), | |
9066 | Orig))); | |
9067 | Analyze_And_Resolve (N, Typ); | |
9068 | end if; | |
9069 | end if; | |
9070 | end; | |
9071 | end if; | |
70482933 RK |
9072 | end Expand_N_Op_Shift_Left; |
9073 | ||
9074 | ----------------------------- | |
9075 | -- Expand_N_Op_Shift_Right -- | |
9076 | ----------------------------- | |
9077 | ||
9078 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
9079 | begin | |
e09a5598 AC |
9080 | -- Share shift left circuit |
9081 | ||
9082 | Expand_N_Op_Shift_Left (N); | |
70482933 RK |
9083 | end Expand_N_Op_Shift_Right; |
9084 | ||
9085 | ---------------------------------------- | |
9086 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
9087 | ---------------------------------------- | |
9088 | ||
9089 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
9090 | begin | |
9091 | Binary_Op_Validity_Checks (N); | |
5216b599 AC |
9092 | |
9093 | -- If we are in Modify_Tree_For_C mode, there is no shift right | |
9094 | -- arithmetic in C, so we rewrite in terms of logical shifts. | |
9095 | ||
9096 | -- Shift_Right (Num, Bits) or | |
9097 | -- (if Num >= Sign | |
9098 | -- then not (Shift_Right (Mask, bits)) | |
9099 | -- else 0) | |
9100 | ||
9101 | -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1) | |
9102 | ||
9103 | -- Note: in almost all C compilers it would work to just shift a | |
9104 | -- signed integer right, but it's undefined and we cannot rely on it. | |
9105 | ||
e09a5598 AC |
9106 | -- Note: the above works fine for shift counts greater than or equal |
9107 | -- to the word size, since in this case (not (Shift_Right (Mask, bits))) | |
9108 | -- generates all 1'bits. | |
9109 | ||
5216b599 AC |
9110 | -- What about non-binary modulus ??? |
9111 | ||
9112 | declare | |
9113 | Loc : constant Source_Ptr := Sloc (N); | |
9114 | Typ : constant Entity_Id := Etype (N); | |
9115 | Sign : constant Uint := 2 ** (Esize (Typ) - 1); | |
9116 | Mask : constant Uint := (2 ** Esize (Typ)) - 1; | |
9117 | Left : constant Node_Id := Left_Opnd (N); | |
9118 | Right : constant Node_Id := Right_Opnd (N); | |
9119 | Maskx : Node_Id; | |
9120 | ||
9121 | begin | |
9122 | if Modify_Tree_For_C then | |
9123 | ||
9124 | -- Here if not (Shift_Right (Mask, bits)) can be computed at | |
9125 | -- compile time as a single constant. | |
9126 | ||
9127 | if Compile_Time_Known_Value (Right) then | |
9128 | declare | |
9129 | Val : constant Uint := Expr_Value (Right); | |
9130 | ||
9131 | begin | |
9132 | if Val >= Esize (Typ) then | |
9133 | Maskx := Make_Integer_Literal (Loc, Mask); | |
9134 | ||
9135 | else | |
9136 | Maskx := | |
9137 | Make_Integer_Literal (Loc, | |
9138 | Intval => Mask - (Mask / (2 ** Expr_Value (Right)))); | |
9139 | end if; | |
9140 | end; | |
9141 | ||
9142 | else | |
9143 | Maskx := | |
9144 | Make_Op_Not (Loc, | |
9145 | Right_Opnd => | |
9146 | Make_Op_Shift_Right (Loc, | |
9147 | Left_Opnd => Make_Integer_Literal (Loc, Mask), | |
9148 | Right_Opnd => Duplicate_Subexpr_No_Checks (Right))); | |
9149 | end if; | |
9150 | ||
9151 | -- Now do the rewrite | |
9152 | ||
9153 | Rewrite (N, | |
9154 | Make_Op_Or (Loc, | |
9155 | Left_Opnd => | |
9156 | Make_Op_Shift_Right (Loc, | |
9157 | Left_Opnd => Left, | |
9158 | Right_Opnd => Right), | |
9159 | Right_Opnd => | |
9160 | Make_If_Expression (Loc, | |
9161 | Expressions => New_List ( | |
9162 | Make_Op_Ge (Loc, | |
9163 | Left_Opnd => Duplicate_Subexpr_No_Checks (Left), | |
9164 | Right_Opnd => Make_Integer_Literal (Loc, Sign)), | |
9165 | Maskx, | |
9166 | Make_Integer_Literal (Loc, 0))))); | |
9167 | Analyze_And_Resolve (N, Typ); | |
9168 | end if; | |
9169 | end; | |
70482933 RK |
9170 | end Expand_N_Op_Shift_Right_Arithmetic; |
9171 | ||
9172 | -------------------------- | |
9173 | -- Expand_N_Op_Subtract -- | |
9174 | -------------------------- | |
9175 | ||
9176 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
9177 | Typ : constant Entity_Id := Etype (N); | |
9178 | ||
9179 | begin | |
9180 | Binary_Op_Validity_Checks (N); | |
9181 | ||
b6b5cca8 AC |
9182 | -- Check for MINIMIZED/ELIMINATED overflow mode |
9183 | ||
9184 | if Minimized_Eliminated_Overflow_Check (N) then | |
9185 | Apply_Arithmetic_Overflow_Check (N); | |
9186 | return; | |
9187 | end if; | |
9188 | ||
70482933 RK |
9189 | -- N - 0 = N for integer types |
9190 | ||
9191 | if Is_Integer_Type (Typ) | |
9192 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
9193 | and then Expr_Value (Right_Opnd (N)) = 0 | |
9194 | then | |
9195 | Rewrite (N, Left_Opnd (N)); | |
9196 | return; | |
9197 | end if; | |
9198 | ||
8fc789c8 | 9199 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 9200 | |
761f7dcb | 9201 | if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then |
70482933 RK |
9202 | Apply_Arithmetic_Overflow_Check (N); |
9203 | ||
0d901290 | 9204 | -- VAX floating-point types case |
70482933 RK |
9205 | |
9206 | elsif Vax_Float (Typ) then | |
9207 | Expand_Vax_Arith (N); | |
9208 | end if; | |
9209 | end Expand_N_Op_Subtract; | |
9210 | ||
9211 | --------------------- | |
9212 | -- Expand_N_Op_Xor -- | |
9213 | --------------------- | |
9214 | ||
9215 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
9216 | Typ : constant Entity_Id := Etype (N); | |
9217 | ||
9218 | begin | |
9219 | Binary_Op_Validity_Checks (N); | |
9220 | ||
9221 | if Is_Array_Type (Etype (N)) then | |
9222 | Expand_Boolean_Operator (N); | |
9223 | ||
9224 | elsif Is_Boolean_Type (Etype (N)) then | |
9225 | Adjust_Condition (Left_Opnd (N)); | |
9226 | Adjust_Condition (Right_Opnd (N)); | |
9227 | Set_Etype (N, Standard_Boolean); | |
9228 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
9229 | |
9230 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
9231 | Expand_Intrinsic_Call (N, Entity (N)); | |
9232 | ||
70482933 RK |
9233 | end if; |
9234 | end Expand_N_Op_Xor; | |
9235 | ||
9236 | ---------------------- | |
9237 | -- Expand_N_Or_Else -- | |
9238 | ---------------------- | |
9239 | ||
5875f8d6 AC |
9240 | procedure Expand_N_Or_Else (N : Node_Id) |
9241 | renames Expand_Short_Circuit_Operator; | |
70482933 RK |
9242 | |
9243 | ----------------------------------- | |
9244 | -- Expand_N_Qualified_Expression -- | |
9245 | ----------------------------------- | |
9246 | ||
9247 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
9248 | Operand : constant Node_Id := Expression (N); | |
9249 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
9250 | ||
9251 | begin | |
f82944b7 JM |
9252 | -- Do validity check if validity checking operands |
9253 | ||
533369aa | 9254 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
9255 | Ensure_Valid (Operand); |
9256 | end if; | |
9257 | ||
9258 | -- Apply possible constraint check | |
9259 | ||
70482933 | 9260 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
d79e621a GD |
9261 | |
9262 | if Do_Range_Check (Operand) then | |
9263 | Set_Do_Range_Check (Operand, False); | |
9264 | Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed); | |
9265 | end if; | |
70482933 RK |
9266 | end Expand_N_Qualified_Expression; |
9267 | ||
a961aa79 AC |
9268 | ------------------------------------ |
9269 | -- Expand_N_Quantified_Expression -- | |
9270 | ------------------------------------ | |
9271 | ||
c0f136cd AC |
9272 | -- We expand: |
9273 | ||
9274 | -- for all X in range => Cond | |
a961aa79 | 9275 | |
c0f136cd | 9276 | -- into: |
a961aa79 | 9277 | |
c0f136cd AC |
9278 | -- T := True; |
9279 | -- for X in range loop | |
9280 | -- if not Cond then | |
9281 | -- T := False; | |
9282 | -- exit; | |
9283 | -- end if; | |
9284 | -- end loop; | |
90c63b09 | 9285 | |
36504e5f | 9286 | -- Similarly, an existentially quantified expression: |
90c63b09 | 9287 | |
c0f136cd | 9288 | -- for some X in range => Cond |
90c63b09 | 9289 | |
c0f136cd | 9290 | -- becomes: |
90c63b09 | 9291 | |
c0f136cd AC |
9292 | -- T := False; |
9293 | -- for X in range loop | |
9294 | -- if Cond then | |
9295 | -- T := True; | |
9296 | -- exit; | |
9297 | -- end if; | |
9298 | -- end loop; | |
90c63b09 | 9299 | |
c0f136cd AC |
9300 | -- In both cases, the iteration may be over a container in which case it is |
9301 | -- given by an iterator specification, not a loop parameter specification. | |
a961aa79 | 9302 | |
c0f136cd | 9303 | procedure Expand_N_Quantified_Expression (N : Node_Id) is |
804670f1 AC |
9304 | Actions : constant List_Id := New_List; |
9305 | For_All : constant Boolean := All_Present (N); | |
9306 | Iter_Spec : constant Node_Id := Iterator_Specification (N); | |
9307 | Loc : constant Source_Ptr := Sloc (N); | |
9308 | Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N); | |
9309 | Cond : Node_Id; | |
9310 | Flag : Entity_Id; | |
9311 | Scheme : Node_Id; | |
9312 | Stmts : List_Id; | |
c56a9ba4 | 9313 | |
a961aa79 | 9314 | begin |
804670f1 AC |
9315 | -- Create the declaration of the flag which tracks the status of the |
9316 | -- quantified expression. Generate: | |
011f9d5d | 9317 | |
804670f1 | 9318 | -- Flag : Boolean := (True | False); |
011f9d5d | 9319 | |
804670f1 | 9320 | Flag := Make_Temporary (Loc, 'T', N); |
011f9d5d | 9321 | |
804670f1 | 9322 | Append_To (Actions, |
90c63b09 | 9323 | Make_Object_Declaration (Loc, |
804670f1 | 9324 | Defining_Identifier => Flag, |
c0f136cd AC |
9325 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
9326 | Expression => | |
804670f1 AC |
9327 | New_Occurrence_Of (Boolean_Literals (For_All), Loc))); |
9328 | ||
9329 | -- Construct the circuitry which tracks the status of the quantified | |
9330 | -- expression. Generate: | |
9331 | ||
9332 | -- if [not] Cond then | |
9333 | -- Flag := (False | True); | |
9334 | -- exit; | |
9335 | -- end if; | |
a961aa79 | 9336 | |
c0f136cd | 9337 | Cond := Relocate_Node (Condition (N)); |
a961aa79 | 9338 | |
804670f1 | 9339 | if For_All then |
c0f136cd | 9340 | Cond := Make_Op_Not (Loc, Cond); |
a961aa79 AC |
9341 | end if; |
9342 | ||
804670f1 | 9343 | Stmts := New_List ( |
c0f136cd AC |
9344 | Make_Implicit_If_Statement (N, |
9345 | Condition => Cond, | |
9346 | Then_Statements => New_List ( | |
9347 | Make_Assignment_Statement (Loc, | |
804670f1 | 9348 | Name => New_Occurrence_Of (Flag, Loc), |
c0f136cd | 9349 | Expression => |
804670f1 AC |
9350 | New_Occurrence_Of (Boolean_Literals (not For_All), Loc)), |
9351 | Make_Exit_Statement (Loc)))); | |
9352 | ||
9353 | -- Build the loop equivalent of the quantified expression | |
c0f136cd | 9354 | |
804670f1 AC |
9355 | if Present (Iter_Spec) then |
9356 | Scheme := | |
011f9d5d | 9357 | Make_Iteration_Scheme (Loc, |
804670f1 | 9358 | Iterator_Specification => Iter_Spec); |
c56a9ba4 | 9359 | else |
804670f1 | 9360 | Scheme := |
011f9d5d | 9361 | Make_Iteration_Scheme (Loc, |
804670f1 | 9362 | Loop_Parameter_Specification => Loop_Spec); |
c56a9ba4 AC |
9363 | end if; |
9364 | ||
a961aa79 AC |
9365 | Append_To (Actions, |
9366 | Make_Loop_Statement (Loc, | |
804670f1 AC |
9367 | Iteration_Scheme => Scheme, |
9368 | Statements => Stmts, | |
c0f136cd | 9369 | End_Label => Empty)); |
a961aa79 | 9370 | |
804670f1 AC |
9371 | -- Transform the quantified expression |
9372 | ||
a961aa79 AC |
9373 | Rewrite (N, |
9374 | Make_Expression_With_Actions (Loc, | |
804670f1 | 9375 | Expression => New_Occurrence_Of (Flag, Loc), |
a961aa79 | 9376 | Actions => Actions)); |
a961aa79 AC |
9377 | Analyze_And_Resolve (N, Standard_Boolean); |
9378 | end Expand_N_Quantified_Expression; | |
9379 | ||
70482933 RK |
9380 | --------------------------------- |
9381 | -- Expand_N_Selected_Component -- | |
9382 | --------------------------------- | |
9383 | ||
70482933 RK |
9384 | procedure Expand_N_Selected_Component (N : Node_Id) is |
9385 | Loc : constant Source_Ptr := Sloc (N); | |
9386 | Par : constant Node_Id := Parent (N); | |
9387 | P : constant Node_Id := Prefix (N); | |
03eb6036 | 9388 | S : constant Node_Id := Selector_Name (N); |
fbf5a39b | 9389 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 9390 | Disc : Entity_Id; |
70482933 | 9391 | New_N : Node_Id; |
fbf5a39b | 9392 | Dcon : Elmt_Id; |
d606f1df | 9393 | Dval : Node_Id; |
70482933 RK |
9394 | |
9395 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
9396 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
9397 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
9398 | -- Don't we have a general routine that does this??? |
9399 | ||
53f29d4f AC |
9400 | function Is_Subtype_Declaration return Boolean; |
9401 | -- The replacement of a discriminant reference by its value is required | |
4317e442 AC |
9402 | -- if this is part of the initialization of an temporary generated by a |
9403 | -- change of representation. This shows up as the construction of a | |
53f29d4f | 9404 | -- discriminant constraint for a subtype declared at the same point as |
4317e442 AC |
9405 | -- the entity in the prefix of the selected component. We recognize this |
9406 | -- case when the context of the reference is: | |
9407 | -- subtype ST is T(Obj.D); | |
9408 | -- where the entity for Obj comes from source, and ST has the same sloc. | |
53f29d4f | 9409 | |
fbf5a39b AC |
9410 | ----------------------- |
9411 | -- In_Left_Hand_Side -- | |
9412 | ----------------------- | |
70482933 RK |
9413 | |
9414 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
9415 | begin | |
fbf5a39b | 9416 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
90c63b09 | 9417 | and then Comp = Name (Parent (Comp))) |
fbf5a39b | 9418 | or else (Present (Parent (Comp)) |
90c63b09 AC |
9419 | and then Nkind (Parent (Comp)) in N_Subexpr |
9420 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
9421 | end In_Left_Hand_Side; |
9422 | ||
53f29d4f AC |
9423 | ----------------------------- |
9424 | -- Is_Subtype_Declaration -- | |
9425 | ----------------------------- | |
9426 | ||
9427 | function Is_Subtype_Declaration return Boolean is | |
9428 | Par : constant Node_Id := Parent (N); | |
53f29d4f AC |
9429 | begin |
9430 | return | |
9431 | Nkind (Par) = N_Index_Or_Discriminant_Constraint | |
9432 | and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration | |
9433 | and then Comes_From_Source (Entity (Prefix (N))) | |
9434 | and then Sloc (Par) = Sloc (Entity (Prefix (N))); | |
9435 | end Is_Subtype_Declaration; | |
9436 | ||
fbf5a39b AC |
9437 | -- Start of processing for Expand_N_Selected_Component |
9438 | ||
70482933 | 9439 | begin |
fbf5a39b AC |
9440 | -- Insert explicit dereference if required |
9441 | ||
9442 | if Is_Access_Type (Ptyp) then | |
702d2020 AC |
9443 | |
9444 | -- First set prefix type to proper access type, in case it currently | |
9445 | -- has a private (non-access) view of this type. | |
9446 | ||
9447 | Set_Etype (P, Ptyp); | |
9448 | ||
fbf5a39b | 9449 | Insert_Explicit_Dereference (P); |
e6f69614 | 9450 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
9451 | |
9452 | if Ekind (Etype (P)) = E_Private_Subtype | |
9453 | and then Is_For_Access_Subtype (Etype (P)) | |
9454 | then | |
9455 | Set_Etype (P, Base_Type (Etype (P))); | |
9456 | end if; | |
9457 | ||
9458 | Ptyp := Etype (P); | |
9459 | end if; | |
9460 | ||
9461 | -- Deal with discriminant check required | |
9462 | ||
70482933 | 9463 | if Do_Discriminant_Check (N) then |
03eb6036 AC |
9464 | if Present (Discriminant_Checking_Func |
9465 | (Original_Record_Component (Entity (S)))) | |
9466 | then | |
9467 | -- Present the discriminant checking function to the backend, so | |
9468 | -- that it can inline the call to the function. | |
9469 | ||
9470 | Add_Inlined_Body | |
9471 | (Discriminant_Checking_Func | |
9472 | (Original_Record_Component (Entity (S)))); | |
70482933 | 9473 | |
03eb6036 | 9474 | -- Now reset the flag and generate the call |
70482933 | 9475 | |
03eb6036 AC |
9476 | Set_Do_Discriminant_Check (N, False); |
9477 | Generate_Discriminant_Check (N); | |
70482933 | 9478 | |
03eb6036 AC |
9479 | -- In the case of Unchecked_Union, no discriminant checking is |
9480 | -- actually performed. | |
70482933 | 9481 | |
03eb6036 AC |
9482 | else |
9483 | Set_Do_Discriminant_Check (N, False); | |
9484 | end if; | |
70482933 RK |
9485 | end if; |
9486 | ||
b4592168 GD |
9487 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
9488 | -- function, then additional actuals must be passed. | |
9489 | ||
0791fbe9 | 9490 | if Ada_Version >= Ada_2005 |
b4592168 GD |
9491 | and then Is_Build_In_Place_Function_Call (P) |
9492 | then | |
9493 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
9494 | end if; | |
9495 | ||
fbf5a39b AC |
9496 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
9497 | -- selected component with discriminants. This must be checked during | |
9498 | -- expansion, because during analysis the type of the selector is not | |
9499 | -- known at the point the prefix is analyzed. If the conversion is the | |
9500 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
9501 | |
9502 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
9503 | and then Has_Discriminants (Etype (N)) | |
9504 | and then not In_Left_Hand_Side (N) | |
9505 | then | |
9506 | Force_Evaluation (Prefix (N)); | |
9507 | end if; | |
9508 | ||
9509 | -- Remaining processing applies only if selector is a discriminant | |
9510 | ||
9511 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
9512 | ||
9513 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
9514 | -- we may be able to rewrite the expression with the actual value |
9515 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
9516 | |
9517 | if Is_Record_Type (Ptyp) | |
9518 | and then Has_Discriminants (Ptyp) | |
9519 | and then Is_Constrained (Ptyp) | |
70482933 | 9520 | then |
fbf5a39b | 9521 | -- Do this optimization for discrete types only, and not for |
a90bd866 | 9522 | -- access types (access discriminants get us into trouble). |
70482933 | 9523 | |
fbf5a39b AC |
9524 | if not Is_Discrete_Type (Etype (N)) then |
9525 | null; | |
9526 | ||
9527 | -- Don't do this on the left hand of an assignment statement. | |
0d901290 AC |
9528 | -- Normally one would think that references like this would not |
9529 | -- occur, but they do in generated code, and mean that we really | |
a90bd866 | 9530 | -- do want to assign the discriminant. |
fbf5a39b AC |
9531 | |
9532 | elsif Nkind (Par) = N_Assignment_Statement | |
9533 | and then Name (Par) = N | |
9534 | then | |
9535 | null; | |
9536 | ||
685094bf | 9537 | -- Don't do this optimization for the prefix of an attribute or |
e2534738 | 9538 | -- the name of an object renaming declaration since these are |
685094bf | 9539 | -- contexts where we do not want the value anyway. |
fbf5a39b AC |
9540 | |
9541 | elsif (Nkind (Par) = N_Attribute_Reference | |
533369aa | 9542 | and then Prefix (Par) = N) |
fbf5a39b AC |
9543 | or else Is_Renamed_Object (N) |
9544 | then | |
9545 | null; | |
9546 | ||
9547 | -- Don't do this optimization if we are within the code for a | |
9548 | -- discriminant check, since the whole point of such a check may | |
a90bd866 | 9549 | -- be to verify the condition on which the code below depends. |
fbf5a39b AC |
9550 | |
9551 | elsif Is_In_Discriminant_Check (N) then | |
9552 | null; | |
9553 | ||
9554 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
9555 | -- still one condition that inhibits the optimization below but |
9556 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
9557 | |
9558 | else | |
685094bf RD |
9559 | -- Loop through discriminants to find the matching discriminant |
9560 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
9561 | |
9562 | Disc := First_Discriminant (Ptyp); | |
9563 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
9564 | Discr_Loop : while Present (Dcon) loop | |
d606f1df | 9565 | Dval := Node (Dcon); |
fbf5a39b | 9566 | |
bd949ee2 RD |
9567 | -- Check if this is the matching discriminant and if the |
9568 | -- discriminant value is simple enough to make sense to | |
9569 | -- copy. We don't want to copy complex expressions, and | |
9570 | -- indeed to do so can cause trouble (before we put in | |
9571 | -- this guard, a discriminant expression containing an | |
e7d897b8 | 9572 | -- AND THEN was copied, causing problems for coverage |
c228a069 | 9573 | -- analysis tools). |
bd949ee2 | 9574 | |
53f29d4f AC |
9575 | -- However, if the reference is part of the initialization |
9576 | -- code generated for an object declaration, we must use | |
9577 | -- the discriminant value from the subtype constraint, | |
9578 | -- because the selected component may be a reference to the | |
9579 | -- object being initialized, whose discriminant is not yet | |
9580 | -- set. This only happens in complex cases involving changes | |
9581 | -- or representation. | |
9582 | ||
bd949ee2 RD |
9583 | if Disc = Entity (Selector_Name (N)) |
9584 | and then (Is_Entity_Name (Dval) | |
170b2989 AC |
9585 | or else Compile_Time_Known_Value (Dval) |
9586 | or else Is_Subtype_Declaration) | |
bd949ee2 | 9587 | then |
fbf5a39b AC |
9588 | -- Here we have the matching discriminant. Check for |
9589 | -- the case of a discriminant of a component that is | |
9590 | -- constrained by an outer discriminant, which cannot | |
9591 | -- be optimized away. | |
9592 | ||
d606f1df AC |
9593 | if Denotes_Discriminant |
9594 | (Dval, Check_Concurrent => True) | |
9595 | then | |
9596 | exit Discr_Loop; | |
9597 | ||
9598 | elsif Nkind (Original_Node (Dval)) = N_Selected_Component | |
9599 | and then | |
9600 | Denotes_Discriminant | |
9601 | (Selector_Name (Original_Node (Dval)), True) | |
9602 | then | |
9603 | exit Discr_Loop; | |
9604 | ||
9605 | -- Do not retrieve value if constraint is not static. It | |
9606 | -- is generally not useful, and the constraint may be a | |
9607 | -- rewritten outer discriminant in which case it is in | |
9608 | -- fact incorrect. | |
9609 | ||
9610 | elsif Is_Entity_Name (Dval) | |
d606f1df | 9611 | and then |
533369aa AC |
9612 | Nkind (Parent (Entity (Dval))) = N_Object_Declaration |
9613 | and then Present (Expression (Parent (Entity (Dval)))) | |
9614 | and then not | |
9615 | Is_Static_Expression | |
d606f1df | 9616 | (Expression (Parent (Entity (Dval)))) |
fbf5a39b AC |
9617 | then |
9618 | exit Discr_Loop; | |
70482933 | 9619 | |
685094bf RD |
9620 | -- In the context of a case statement, the expression may |
9621 | -- have the base type of the discriminant, and we need to | |
9622 | -- preserve the constraint to avoid spurious errors on | |
9623 | -- missing cases. | |
70482933 | 9624 | |
fbf5a39b | 9625 | elsif Nkind (Parent (N)) = N_Case_Statement |
d606f1df | 9626 | and then Etype (Dval) /= Etype (Disc) |
70482933 RK |
9627 | then |
9628 | Rewrite (N, | |
9629 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
9630 | Subtype_Mark => |
9631 | New_Occurrence_Of (Etype (Disc), Loc), | |
9632 | Expression => | |
d606f1df | 9633 | New_Copy_Tree (Dval))); |
ffe9aba8 | 9634 | Analyze_And_Resolve (N, Etype (Disc)); |
fbf5a39b AC |
9635 | |
9636 | -- In case that comes out as a static expression, | |
9637 | -- reset it (a selected component is never static). | |
9638 | ||
9639 | Set_Is_Static_Expression (N, False); | |
9640 | return; | |
9641 | ||
9642 | -- Otherwise we can just copy the constraint, but the | |
a90bd866 | 9643 | -- result is certainly not static. In some cases the |
ffe9aba8 AC |
9644 | -- discriminant constraint has been analyzed in the |
9645 | -- context of the original subtype indication, but for | |
9646 | -- itypes the constraint might not have been analyzed | |
9647 | -- yet, and this must be done now. | |
fbf5a39b | 9648 | |
70482933 | 9649 | else |
d606f1df | 9650 | Rewrite (N, New_Copy_Tree (Dval)); |
ffe9aba8 | 9651 | Analyze_And_Resolve (N); |
fbf5a39b AC |
9652 | Set_Is_Static_Expression (N, False); |
9653 | return; | |
70482933 | 9654 | end if; |
70482933 RK |
9655 | end if; |
9656 | ||
fbf5a39b AC |
9657 | Next_Elmt (Dcon); |
9658 | Next_Discriminant (Disc); | |
9659 | end loop Discr_Loop; | |
70482933 | 9660 | |
fbf5a39b AC |
9661 | -- Note: the above loop should always find a matching |
9662 | -- discriminant, but if it does not, we just missed an | |
c228a069 AC |
9663 | -- optimization due to some glitch (perhaps a previous |
9664 | -- error), so ignore. | |
fbf5a39b AC |
9665 | |
9666 | end if; | |
70482933 RK |
9667 | end if; |
9668 | ||
9669 | -- The only remaining processing is in the case of a discriminant of | |
9670 | -- a concurrent object, where we rewrite the prefix to denote the | |
9671 | -- corresponding record type. If the type is derived and has renamed | |
9672 | -- discriminants, use corresponding discriminant, which is the one | |
9673 | -- that appears in the corresponding record. | |
9674 | ||
9675 | if not Is_Concurrent_Type (Ptyp) then | |
9676 | return; | |
9677 | end if; | |
9678 | ||
9679 | Disc := Entity (Selector_Name (N)); | |
9680 | ||
9681 | if Is_Derived_Type (Ptyp) | |
9682 | and then Present (Corresponding_Discriminant (Disc)) | |
9683 | then | |
9684 | Disc := Corresponding_Discriminant (Disc); | |
9685 | end if; | |
9686 | ||
9687 | New_N := | |
9688 | Make_Selected_Component (Loc, | |
9689 | Prefix => | |
9690 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
9691 | New_Copy_Tree (P)), | |
9692 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
9693 | ||
9694 | Rewrite (N, New_N); | |
9695 | Analyze (N); | |
9696 | end if; | |
5972791c | 9697 | |
73fe1679 | 9698 | -- Set Atomic_Sync_Required if necessary for atomic component |
5972791c | 9699 | |
73fe1679 AC |
9700 | if Nkind (N) = N_Selected_Component then |
9701 | declare | |
9702 | E : constant Entity_Id := Entity (Selector_Name (N)); | |
9703 | Set : Boolean; | |
9704 | ||
9705 | begin | |
9706 | -- If component is atomic, but type is not, setting depends on | |
9707 | -- disable/enable state for the component. | |
9708 | ||
9709 | if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then | |
9710 | Set := not Atomic_Synchronization_Disabled (E); | |
9711 | ||
9712 | -- If component is not atomic, but its type is atomic, setting | |
9713 | -- depends on disable/enable state for the type. | |
9714 | ||
9715 | elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
9716 | Set := not Atomic_Synchronization_Disabled (Etype (E)); | |
9717 | ||
9718 | -- If both component and type are atomic, we disable if either | |
9719 | -- component or its type have sync disabled. | |
9720 | ||
9721 | elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
9722 | Set := (not Atomic_Synchronization_Disabled (E)) | |
9723 | and then | |
9724 | (not Atomic_Synchronization_Disabled (Etype (E))); | |
9725 | ||
9726 | else | |
9727 | Set := False; | |
9728 | end if; | |
9729 | ||
9730 | -- Set flag if required | |
9731 | ||
9732 | if Set then | |
9733 | Activate_Atomic_Synchronization (N); | |
9734 | end if; | |
9735 | end; | |
5972791c | 9736 | end if; |
70482933 RK |
9737 | end Expand_N_Selected_Component; |
9738 | ||
9739 | -------------------- | |
9740 | -- Expand_N_Slice -- | |
9741 | -------------------- | |
9742 | ||
9743 | procedure Expand_N_Slice (N : Node_Id) is | |
5ff90f08 AC |
9744 | Loc : constant Source_Ptr := Sloc (N); |
9745 | Typ : constant Entity_Id := Etype (N); | |
fbf5a39b | 9746 | |
81a5b587 | 9747 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
9748 | -- Check whether the argument is an actual for a procedure call, in |
9749 | -- which case the expansion of a bit-packed slice is deferred until the | |
9750 | -- call itself is expanded. The reason this is required is that we might | |
9751 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
9752 | -- that copy out would be missed if we created a temporary here in | |
9753 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
9754 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
9755 | -- is harmless to defer expansion in the IN case, since the call | |
9756 | -- processing will still generate the appropriate copy in operation, | |
9757 | -- which will take care of the slice. | |
81a5b587 | 9758 | |
b01bf852 | 9759 | procedure Make_Temporary_For_Slice; |
685094bf RD |
9760 | -- Create a named variable for the value of the slice, in cases where |
9761 | -- the back-end cannot handle it properly, e.g. when packed types or | |
9762 | -- unaligned slices are involved. | |
fbf5a39b | 9763 | |
81a5b587 AC |
9764 | ------------------------- |
9765 | -- Is_Procedure_Actual -- | |
9766 | ------------------------- | |
9767 | ||
9768 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
9769 | Par : Node_Id := Parent (N); | |
08aa9a4a | 9770 | |
81a5b587 | 9771 | begin |
81a5b587 | 9772 | loop |
c6a60aa1 RD |
9773 | -- If our parent is a procedure call we can return |
9774 | ||
81a5b587 AC |
9775 | if Nkind (Par) = N_Procedure_Call_Statement then |
9776 | return True; | |
6b6fcd3e | 9777 | |
685094bf RD |
9778 | -- If our parent is a type conversion, keep climbing the tree, |
9779 | -- since a type conversion can be a procedure actual. Also keep | |
9780 | -- climbing if parameter association or a qualified expression, | |
9781 | -- since these are additional cases that do can appear on | |
9782 | -- procedure actuals. | |
6b6fcd3e | 9783 | |
303b4d58 AC |
9784 | elsif Nkind_In (Par, N_Type_Conversion, |
9785 | N_Parameter_Association, | |
9786 | N_Qualified_Expression) | |
c6a60aa1 | 9787 | then |
81a5b587 | 9788 | Par := Parent (Par); |
c6a60aa1 RD |
9789 | |
9790 | -- Any other case is not what we are looking for | |
9791 | ||
9792 | else | |
9793 | return False; | |
81a5b587 AC |
9794 | end if; |
9795 | end loop; | |
81a5b587 AC |
9796 | end Is_Procedure_Actual; |
9797 | ||
b01bf852 AC |
9798 | ------------------------------ |
9799 | -- Make_Temporary_For_Slice -- | |
9800 | ------------------------------ | |
fbf5a39b | 9801 | |
b01bf852 | 9802 | procedure Make_Temporary_For_Slice is |
b01bf852 | 9803 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
5ff90f08 | 9804 | Decl : Node_Id; |
13d923cc | 9805 | |
fbf5a39b AC |
9806 | begin |
9807 | Decl := | |
9808 | Make_Object_Declaration (Loc, | |
9809 | Defining_Identifier => Ent, | |
9810 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
9811 | ||
9812 | Set_No_Initialization (Decl); | |
9813 | ||
9814 | Insert_Actions (N, New_List ( | |
9815 | Decl, | |
9816 | Make_Assignment_Statement (Loc, | |
5ff90f08 | 9817 | Name => New_Occurrence_Of (Ent, Loc), |
fbf5a39b AC |
9818 | Expression => Relocate_Node (N)))); |
9819 | ||
9820 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
9821 | Analyze_And_Resolve (N, Typ); | |
b01bf852 | 9822 | end Make_Temporary_For_Slice; |
fbf5a39b | 9823 | |
5ff90f08 AC |
9824 | -- Local variables |
9825 | ||
800da977 AC |
9826 | Pref : constant Node_Id := Prefix (N); |
9827 | Pref_Typ : Entity_Id := Etype (Pref); | |
5ff90f08 | 9828 | |
fbf5a39b | 9829 | -- Start of processing for Expand_N_Slice |
70482933 RK |
9830 | |
9831 | begin | |
9832 | -- Special handling for access types | |
9833 | ||
5ff90f08 AC |
9834 | if Is_Access_Type (Pref_Typ) then |
9835 | Pref_Typ := Designated_Type (Pref_Typ); | |
70482933 | 9836 | |
5ff90f08 | 9837 | Rewrite (Pref, |
e6f69614 | 9838 | Make_Explicit_Dereference (Sloc (N), |
5ff90f08 | 9839 | Prefix => Relocate_Node (Pref))); |
70482933 | 9840 | |
5ff90f08 | 9841 | Analyze_And_Resolve (Pref, Pref_Typ); |
70482933 RK |
9842 | end if; |
9843 | ||
b4592168 GD |
9844 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
9845 | -- function, then additional actuals must be passed. | |
9846 | ||
0791fbe9 | 9847 | if Ada_Version >= Ada_2005 |
5ff90f08 | 9848 | and then Is_Build_In_Place_Function_Call (Pref) |
b4592168 | 9849 | then |
5ff90f08 | 9850 | Make_Build_In_Place_Call_In_Anonymous_Context (Pref); |
b4592168 GD |
9851 | end if; |
9852 | ||
70482933 RK |
9853 | -- The remaining case to be handled is packed slices. We can leave |
9854 | -- packed slices as they are in the following situations: | |
9855 | ||
9856 | -- 1. Right or left side of an assignment (we can handle this | |
9857 | -- situation correctly in the assignment statement expansion). | |
9858 | ||
685094bf RD |
9859 | -- 2. Prefix of indexed component (the slide is optimized away in this |
9860 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 9861 | |
685094bf RD |
9862 | -- 3. Object renaming declaration, since we want the name of the |
9863 | -- slice, not the value. | |
70482933 | 9864 | |
685094bf RD |
9865 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
9866 | -- be required, and this is handled in the expansion of call | |
9867 | -- itself. | |
70482933 | 9868 | |
685094bf RD |
9869 | -- 5. Prefix of an address attribute (this is an error which is caught |
9870 | -- elsewhere, and the expansion would interfere with generating the | |
9871 | -- error message). | |
70482933 | 9872 | |
81a5b587 | 9873 | if not Is_Packed (Typ) then |
08aa9a4a | 9874 | |
685094bf RD |
9875 | -- Apply transformation for actuals of a function call, where |
9876 | -- Expand_Actuals is not used. | |
81a5b587 AC |
9877 | |
9878 | if Nkind (Parent (N)) = N_Function_Call | |
9879 | and then Is_Possibly_Unaligned_Slice (N) | |
9880 | then | |
b01bf852 | 9881 | Make_Temporary_For_Slice; |
81a5b587 AC |
9882 | end if; |
9883 | ||
9884 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
9885 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
533369aa | 9886 | and then Parent (N) = Name (Parent (Parent (N)))) |
70482933 | 9887 | then |
81a5b587 | 9888 | return; |
70482933 | 9889 | |
81a5b587 AC |
9890 | elsif Nkind (Parent (N)) = N_Indexed_Component |
9891 | or else Is_Renamed_Object (N) | |
9892 | or else Is_Procedure_Actual (N) | |
9893 | then | |
9894 | return; | |
70482933 | 9895 | |
91b1417d AC |
9896 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
9897 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 9898 | then |
81a5b587 AC |
9899 | return; |
9900 | ||
9901 | else | |
b01bf852 | 9902 | Make_Temporary_For_Slice; |
70482933 RK |
9903 | end if; |
9904 | end Expand_N_Slice; | |
9905 | ||
9906 | ------------------------------ | |
9907 | -- Expand_N_Type_Conversion -- | |
9908 | ------------------------------ | |
9909 | ||
9910 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
9911 | Loc : constant Source_Ptr := Sloc (N); | |
9912 | Operand : constant Node_Id := Expression (N); | |
9913 | Target_Type : constant Entity_Id := Etype (N); | |
9914 | Operand_Type : Entity_Id := Etype (Operand); | |
9915 | ||
9916 | procedure Handle_Changed_Representation; | |
685094bf RD |
9917 | -- This is called in the case of record and array type conversions to |
9918 | -- see if there is a change of representation to be handled. Change of | |
9919 | -- representation is actually handled at the assignment statement level, | |
9920 | -- and what this procedure does is rewrite node N conversion as an | |
9921 | -- assignment to temporary. If there is no change of representation, | |
9922 | -- then the conversion node is unchanged. | |
70482933 | 9923 | |
426908f8 RD |
9924 | procedure Raise_Accessibility_Error; |
9925 | -- Called when we know that an accessibility check will fail. Rewrites | |
9926 | -- node N to an appropriate raise statement and outputs warning msgs. | |
9927 | -- The Etype of the raise node is set to Target_Type. | |
9928 | ||
70482933 RK |
9929 | procedure Real_Range_Check; |
9930 | -- Handles generation of range check for real target value | |
9931 | ||
d15f9422 AC |
9932 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean; |
9933 | -- True iff Present (Effective_Extra_Accessibility (Id)) successfully | |
9934 | -- evaluates to True. | |
9935 | ||
70482933 RK |
9936 | ----------------------------------- |
9937 | -- Handle_Changed_Representation -- | |
9938 | ----------------------------------- | |
9939 | ||
9940 | procedure Handle_Changed_Representation is | |
9941 | Temp : Entity_Id; | |
9942 | Decl : Node_Id; | |
9943 | Odef : Node_Id; | |
9944 | Disc : Node_Id; | |
9945 | N_Ix : Node_Id; | |
9946 | Cons : List_Id; | |
9947 | ||
9948 | begin | |
f82944b7 | 9949 | -- Nothing else to do if no change of representation |
70482933 RK |
9950 | |
9951 | if Same_Representation (Operand_Type, Target_Type) then | |
9952 | return; | |
9953 | ||
9954 | -- The real change of representation work is done by the assignment | |
9955 | -- statement processing. So if this type conversion is appearing as | |
9956 | -- the expression of an assignment statement, nothing needs to be | |
9957 | -- done to the conversion. | |
9958 | ||
9959 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
9960 | return; | |
9961 | ||
9962 | -- Otherwise we need to generate a temporary variable, and do the | |
9963 | -- change of representation assignment into that temporary variable. | |
9964 | -- The conversion is then replaced by a reference to this variable. | |
9965 | ||
9966 | else | |
9967 | Cons := No_List; | |
9968 | ||
685094bf RD |
9969 | -- If type is unconstrained we have to add a constraint, copied |
9970 | -- from the actual value of the left hand side. | |
70482933 RK |
9971 | |
9972 | if not Is_Constrained (Target_Type) then | |
9973 | if Has_Discriminants (Operand_Type) then | |
9974 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
9975 | |
9976 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
9977 | Disc := First_Stored_Discriminant (Operand_Type); | |
9978 | end if; | |
9979 | ||
70482933 RK |
9980 | Cons := New_List; |
9981 | while Present (Disc) loop | |
9982 | Append_To (Cons, | |
9983 | Make_Selected_Component (Loc, | |
7675ad4f AC |
9984 | Prefix => |
9985 | Duplicate_Subexpr_Move_Checks (Operand), | |
70482933 RK |
9986 | Selector_Name => |
9987 | Make_Identifier (Loc, Chars (Disc)))); | |
9988 | Next_Discriminant (Disc); | |
9989 | end loop; | |
9990 | ||
9991 | elsif Is_Array_Type (Operand_Type) then | |
9992 | N_Ix := First_Index (Target_Type); | |
9993 | Cons := New_List; | |
9994 | ||
9995 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
9996 | ||
9997 | -- We convert the bounds explicitly. We use an unchecked | |
9998 | -- conversion because bounds checks are done elsewhere. | |
9999 | ||
10000 | Append_To (Cons, | |
10001 | Make_Range (Loc, | |
10002 | Low_Bound => | |
10003 | Unchecked_Convert_To (Etype (N_Ix), | |
10004 | Make_Attribute_Reference (Loc, | |
10005 | Prefix => | |
fbf5a39b | 10006 | Duplicate_Subexpr_No_Checks |
70482933 RK |
10007 | (Operand, Name_Req => True), |
10008 | Attribute_Name => Name_First, | |
10009 | Expressions => New_List ( | |
10010 | Make_Integer_Literal (Loc, J)))), | |
10011 | ||
10012 | High_Bound => | |
10013 | Unchecked_Convert_To (Etype (N_Ix), | |
10014 | Make_Attribute_Reference (Loc, | |
10015 | Prefix => | |
fbf5a39b | 10016 | Duplicate_Subexpr_No_Checks |
70482933 RK |
10017 | (Operand, Name_Req => True), |
10018 | Attribute_Name => Name_Last, | |
10019 | Expressions => New_List ( | |
10020 | Make_Integer_Literal (Loc, J)))))); | |
10021 | ||
10022 | Next_Index (N_Ix); | |
10023 | end loop; | |
10024 | end if; | |
10025 | end if; | |
10026 | ||
10027 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
10028 | ||
10029 | if Present (Cons) then | |
10030 | Odef := | |
10031 | Make_Subtype_Indication (Loc, | |
10032 | Subtype_Mark => Odef, | |
10033 | Constraint => | |
10034 | Make_Index_Or_Discriminant_Constraint (Loc, | |
10035 | Constraints => Cons)); | |
10036 | end if; | |
10037 | ||
191fcb3a | 10038 | Temp := Make_Temporary (Loc, 'C'); |
70482933 RK |
10039 | Decl := |
10040 | Make_Object_Declaration (Loc, | |
10041 | Defining_Identifier => Temp, | |
10042 | Object_Definition => Odef); | |
10043 | ||
10044 | Set_No_Initialization (Decl, True); | |
10045 | ||
10046 | -- Insert required actions. It is essential to suppress checks | |
10047 | -- since we have suppressed default initialization, which means | |
10048 | -- that the variable we create may have no discriminants. | |
10049 | ||
10050 | Insert_Actions (N, | |
10051 | New_List ( | |
10052 | Decl, | |
10053 | Make_Assignment_Statement (Loc, | |
10054 | Name => New_Occurrence_Of (Temp, Loc), | |
10055 | Expression => Relocate_Node (N))), | |
10056 | Suppress => All_Checks); | |
10057 | ||
10058 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
10059 | return; | |
10060 | end if; | |
10061 | end Handle_Changed_Representation; | |
10062 | ||
426908f8 RD |
10063 | ------------------------------- |
10064 | -- Raise_Accessibility_Error -- | |
10065 | ------------------------------- | |
10066 | ||
10067 | procedure Raise_Accessibility_Error is | |
10068 | begin | |
43417b90 | 10069 | Error_Msg_Warn := SPARK_Mode /= On; |
426908f8 RD |
10070 | Rewrite (N, |
10071 | Make_Raise_Program_Error (Sloc (N), | |
10072 | Reason => PE_Accessibility_Check_Failed)); | |
10073 | Set_Etype (N, Target_Type); | |
10074 | ||
4a28b181 AC |
10075 | Error_Msg_N ("<<accessibility check failure", N); |
10076 | Error_Msg_NE ("\<<& [", N, Standard_Program_Error); | |
426908f8 RD |
10077 | end Raise_Accessibility_Error; |
10078 | ||
70482933 RK |
10079 | ---------------------- |
10080 | -- Real_Range_Check -- | |
10081 | ---------------------- | |
10082 | ||
685094bf RD |
10083 | -- Case of conversions to floating-point or fixed-point. If range checks |
10084 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
10085 | |
10086 | -- typ (x) | |
10087 | ||
10088 | -- to | |
10089 | ||
10090 | -- Tnn : typ'Base := typ'Base (x); | |
10091 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
10092 | -- Tnn | |
10093 | ||
685094bf RD |
10094 | -- This is necessary when there is a conversion of integer to float or |
10095 | -- to fixed-point to ensure that the correct checks are made. It is not | |
10096 | -- necessary for float to float where it is enough to simply set the | |
10097 | -- Do_Range_Check flag. | |
fbf5a39b | 10098 | |
70482933 RK |
10099 | procedure Real_Range_Check is |
10100 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
10101 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
10102 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 10103 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
10104 | Conv : Node_Id; |
10105 | Tnn : Entity_Id; | |
10106 | ||
10107 | begin | |
10108 | -- Nothing to do if conversion was rewritten | |
10109 | ||
10110 | if Nkind (N) /= N_Type_Conversion then | |
10111 | return; | |
10112 | end if; | |
10113 | ||
685094bf RD |
10114 | -- Nothing to do if range checks suppressed, or target has the same |
10115 | -- range as the base type (or is the base type). | |
70482933 RK |
10116 | |
10117 | if Range_Checks_Suppressed (Target_Type) | |
533369aa | 10118 | or else (Lo = Type_Low_Bound (Btyp) |
70482933 RK |
10119 | and then |
10120 | Hi = Type_High_Bound (Btyp)) | |
10121 | then | |
10122 | return; | |
10123 | end if; | |
10124 | ||
685094bf RD |
10125 | -- Nothing to do if expression is an entity on which checks have been |
10126 | -- suppressed. | |
70482933 | 10127 | |
fbf5a39b AC |
10128 | if Is_Entity_Name (Operand) |
10129 | and then Range_Checks_Suppressed (Entity (Operand)) | |
10130 | then | |
10131 | return; | |
10132 | end if; | |
10133 | ||
685094bf RD |
10134 | -- Nothing to do if bounds are all static and we can tell that the |
10135 | -- expression is within the bounds of the target. Note that if the | |
10136 | -- operand is of an unconstrained floating-point type, then we do | |
10137 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
10138 | |
10139 | declare | |
f02b8bb8 RD |
10140 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); |
10141 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
fbf5a39b AC |
10142 | |
10143 | begin | |
10144 | if (not Is_Floating_Point_Type (Xtyp) | |
10145 | or else Is_Constrained (Xtyp)) | |
10146 | and then Compile_Time_Known_Value (S_Lo) | |
10147 | and then Compile_Time_Known_Value (S_Hi) | |
10148 | and then Compile_Time_Known_Value (Hi) | |
10149 | and then Compile_Time_Known_Value (Lo) | |
10150 | then | |
10151 | declare | |
10152 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
10153 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
10154 | S_Lov : Ureal; | |
10155 | S_Hiv : Ureal; | |
10156 | ||
10157 | begin | |
10158 | if Is_Real_Type (Xtyp) then | |
10159 | S_Lov := Expr_Value_R (S_Lo); | |
10160 | S_Hiv := Expr_Value_R (S_Hi); | |
10161 | else | |
10162 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
10163 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
10164 | end if; | |
10165 | ||
10166 | if D_Hiv > D_Lov | |
10167 | and then S_Lov >= D_Lov | |
10168 | and then S_Hiv <= D_Hiv | |
10169 | then | |
10170 | Set_Do_Range_Check (Operand, False); | |
10171 | return; | |
10172 | end if; | |
10173 | end; | |
10174 | end if; | |
10175 | end; | |
10176 | ||
10177 | -- For float to float conversions, we are done | |
10178 | ||
10179 | if Is_Floating_Point_Type (Xtyp) | |
10180 | and then | |
10181 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
10182 | then |
10183 | return; | |
10184 | end if; | |
10185 | ||
fbf5a39b | 10186 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
10187 | |
10188 | Conv := Relocate_Node (N); | |
eaa826f8 | 10189 | Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); |
70482933 RK |
10190 | Set_Etype (Conv, Btyp); |
10191 | ||
f02b8bb8 RD |
10192 | -- Enable overflow except for case of integer to float conversions, |
10193 | -- where it is never required, since we can never have overflow in | |
10194 | -- this case. | |
70482933 | 10195 | |
fbf5a39b AC |
10196 | if not Is_Integer_Type (Etype (Operand)) then |
10197 | Enable_Overflow_Check (Conv); | |
70482933 RK |
10198 | end if; |
10199 | ||
191fcb3a | 10200 | Tnn := Make_Temporary (Loc, 'T', Conv); |
70482933 RK |
10201 | |
10202 | Insert_Actions (N, New_List ( | |
10203 | Make_Object_Declaration (Loc, | |
10204 | Defining_Identifier => Tnn, | |
10205 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
0ac2a660 AC |
10206 | Constant_Present => True, |
10207 | Expression => Conv), | |
70482933 RK |
10208 | |
10209 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
10210 | Condition => |
10211 | Make_Or_Else (Loc, | |
10212 | Left_Opnd => | |
10213 | Make_Op_Lt (Loc, | |
10214 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
10215 | Right_Opnd => | |
10216 | Make_Attribute_Reference (Loc, | |
10217 | Attribute_Name => Name_First, | |
10218 | Prefix => | |
10219 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 10220 | |
07fc65c4 GB |
10221 | Right_Opnd => |
10222 | Make_Op_Gt (Loc, | |
10223 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
10224 | Right_Opnd => | |
10225 | Make_Attribute_Reference (Loc, | |
10226 | Attribute_Name => Name_Last, | |
10227 | Prefix => | |
10228 | New_Occurrence_Of (Target_Type, Loc)))), | |
10229 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
10230 | |
10231 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
10232 | Analyze_And_Resolve (N, Btyp); | |
10233 | end Real_Range_Check; | |
10234 | ||
d15f9422 AC |
10235 | ----------------------------- |
10236 | -- Has_Extra_Accessibility -- | |
10237 | ----------------------------- | |
10238 | ||
10239 | -- Returns true for a formal of an anonymous access type or for | |
10240 | -- an Ada 2012-style stand-alone object of an anonymous access type. | |
10241 | ||
10242 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is | |
10243 | begin | |
10244 | if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then | |
10245 | return Present (Effective_Extra_Accessibility (Id)); | |
10246 | else | |
10247 | return False; | |
10248 | end if; | |
10249 | end Has_Extra_Accessibility; | |
10250 | ||
70482933 RK |
10251 | -- Start of processing for Expand_N_Type_Conversion |
10252 | ||
10253 | begin | |
83851b23 | 10254 | -- First remove check marks put by the semantic analysis on the type |
b2502161 AC |
10255 | -- conversion between array types. We need these checks, and they will |
10256 | -- be generated by this expansion routine, but we do not depend on these | |
10257 | -- flags being set, and since we do intend to expand the checks in the | |
10258 | -- front end, we don't want them on the tree passed to the back end. | |
83851b23 AC |
10259 | |
10260 | if Is_Array_Type (Target_Type) then | |
10261 | if Is_Constrained (Target_Type) then | |
10262 | Set_Do_Length_Check (N, False); | |
10263 | else | |
10264 | Set_Do_Range_Check (Operand, False); | |
10265 | end if; | |
10266 | end if; | |
10267 | ||
685094bf | 10268 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 AC |
10269 | -- the conversion completely, it is useless, except that it may carry |
10270 | -- an Assignment_OK attribute, which must be propagated to the operand. | |
70482933 RK |
10271 | |
10272 | if Operand_Type = Target_Type then | |
7b00e31d AC |
10273 | if Assignment_OK (N) then |
10274 | Set_Assignment_OK (Operand); | |
10275 | end if; | |
10276 | ||
fbf5a39b | 10277 | Rewrite (N, Relocate_Node (Operand)); |
e606088a | 10278 | goto Done; |
70482933 RK |
10279 | end if; |
10280 | ||
685094bf RD |
10281 | -- Nothing to do if this is the second argument of read. This is a |
10282 | -- "backwards" conversion that will be handled by the specialized code | |
10283 | -- in attribute processing. | |
70482933 RK |
10284 | |
10285 | if Nkind (Parent (N)) = N_Attribute_Reference | |
10286 | and then Attribute_Name (Parent (N)) = Name_Read | |
10287 | and then Next (First (Expressions (Parent (N)))) = N | |
10288 | then | |
e606088a AC |
10289 | goto Done; |
10290 | end if; | |
10291 | ||
10292 | -- Check for case of converting to a type that has an invariant | |
10293 | -- associated with it. This required an invariant check. We convert | |
10294 | ||
10295 | -- typ (expr) | |
10296 | ||
10297 | -- into | |
10298 | ||
10299 | -- do invariant_check (typ (expr)) in typ (expr); | |
10300 | ||
10301 | -- using Duplicate_Subexpr to avoid multiple side effects | |
10302 | ||
10303 | -- Note: the Comes_From_Source check, and then the resetting of this | |
10304 | -- flag prevents what would otherwise be an infinite recursion. | |
10305 | ||
fd0ff1cf RD |
10306 | if Has_Invariants (Target_Type) |
10307 | and then Present (Invariant_Procedure (Target_Type)) | |
e606088a AC |
10308 | and then Comes_From_Source (N) |
10309 | then | |
10310 | Set_Comes_From_Source (N, False); | |
10311 | Rewrite (N, | |
10312 | Make_Expression_With_Actions (Loc, | |
10313 | Actions => New_List ( | |
10314 | Make_Invariant_Call (Duplicate_Subexpr (N))), | |
10315 | Expression => Duplicate_Subexpr_No_Checks (N))); | |
10316 | Analyze_And_Resolve (N, Target_Type); | |
10317 | goto Done; | |
70482933 RK |
10318 | end if; |
10319 | ||
10320 | -- Here if we may need to expand conversion | |
10321 | ||
eaa826f8 RD |
10322 | -- If the operand of the type conversion is an arithmetic operation on |
10323 | -- signed integers, and the based type of the signed integer type in | |
10324 | -- question is smaller than Standard.Integer, we promote both of the | |
10325 | -- operands to type Integer. | |
10326 | ||
10327 | -- For example, if we have | |
10328 | ||
10329 | -- target-type (opnd1 + opnd2) | |
10330 | ||
10331 | -- and opnd1 and opnd2 are of type short integer, then we rewrite | |
10332 | -- this as: | |
10333 | ||
10334 | -- target-type (integer(opnd1) + integer(opnd2)) | |
10335 | ||
10336 | -- We do this because we are always allowed to compute in a larger type | |
10337 | -- if we do the right thing with the result, and in this case we are | |
10338 | -- going to do a conversion which will do an appropriate check to make | |
10339 | -- sure that things are in range of the target type in any case. This | |
10340 | -- avoids some unnecessary intermediate overflows. | |
10341 | ||
dfcfdc0a AC |
10342 | -- We might consider a similar transformation in the case where the |
10343 | -- target is a real type or a 64-bit integer type, and the operand | |
10344 | -- is an arithmetic operation using a 32-bit integer type. However, | |
10345 | -- we do not bother with this case, because it could cause significant | |
308e6f3a | 10346 | -- inefficiencies on 32-bit machines. On a 64-bit machine it would be |
dfcfdc0a AC |
10347 | -- much cheaper, but we don't want different behavior on 32-bit and |
10348 | -- 64-bit machines. Note that the exclusion of the 64-bit case also | |
10349 | -- handles the configurable run-time cases where 64-bit arithmetic | |
10350 | -- may simply be unavailable. | |
eaa826f8 RD |
10351 | |
10352 | -- Note: this circuit is partially redundant with respect to the circuit | |
10353 | -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in | |
10354 | -- the processing here. Also we still need the Checks circuit, since we | |
10355 | -- have to be sure not to generate junk overflow checks in the first | |
a90bd866 | 10356 | -- place, since it would be trick to remove them here. |
eaa826f8 | 10357 | |
fdfcc663 | 10358 | if Integer_Promotion_Possible (N) then |
eaa826f8 | 10359 | |
fdfcc663 | 10360 | -- All conditions met, go ahead with transformation |
eaa826f8 | 10361 | |
fdfcc663 AC |
10362 | declare |
10363 | Opnd : Node_Id; | |
10364 | L, R : Node_Id; | |
dfcfdc0a | 10365 | |
fdfcc663 AC |
10366 | begin |
10367 | R := | |
10368 | Make_Type_Conversion (Loc, | |
e4494292 | 10369 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 | 10370 | Expression => Relocate_Node (Right_Opnd (Operand))); |
eaa826f8 | 10371 | |
5f3f175d AC |
10372 | Opnd := New_Op_Node (Nkind (Operand), Loc); |
10373 | Set_Right_Opnd (Opnd, R); | |
eaa826f8 | 10374 | |
5f3f175d | 10375 | if Nkind (Operand) in N_Binary_Op then |
fdfcc663 | 10376 | L := |
eaa826f8 | 10377 | Make_Type_Conversion (Loc, |
e4494292 | 10378 | Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc), |
fdfcc663 AC |
10379 | Expression => Relocate_Node (Left_Opnd (Operand))); |
10380 | ||
5f3f175d AC |
10381 | Set_Left_Opnd (Opnd, L); |
10382 | end if; | |
eaa826f8 | 10383 | |
5f3f175d AC |
10384 | Rewrite (N, |
10385 | Make_Type_Conversion (Loc, | |
10386 | Subtype_Mark => Relocate_Node (Subtype_Mark (N)), | |
10387 | Expression => Opnd)); | |
dfcfdc0a | 10388 | |
5f3f175d | 10389 | Analyze_And_Resolve (N, Target_Type); |
e606088a | 10390 | goto Done; |
fdfcc663 AC |
10391 | end; |
10392 | end if; | |
eaa826f8 | 10393 | |
f82944b7 JM |
10394 | -- Do validity check if validity checking operands |
10395 | ||
533369aa | 10396 | if Validity_Checks_On and Validity_Check_Operands then |
f82944b7 JM |
10397 | Ensure_Valid (Operand); |
10398 | end if; | |
10399 | ||
70482933 RK |
10400 | -- Special case of converting from non-standard boolean type |
10401 | ||
10402 | if Is_Boolean_Type (Operand_Type) | |
10403 | and then (Nonzero_Is_True (Operand_Type)) | |
10404 | then | |
10405 | Adjust_Condition (Operand); | |
10406 | Set_Etype (Operand, Standard_Boolean); | |
10407 | Operand_Type := Standard_Boolean; | |
10408 | end if; | |
10409 | ||
10410 | -- Case of converting to an access type | |
10411 | ||
10412 | if Is_Access_Type (Target_Type) then | |
10413 | ||
d766cee3 RD |
10414 | -- Apply an accessibility check when the conversion operand is an |
10415 | -- access parameter (or a renaming thereof), unless conversion was | |
e84e11ba GD |
10416 | -- expanded from an Unchecked_ or Unrestricted_Access attribute. |
10417 | -- Note that other checks may still need to be applied below (such | |
10418 | -- as tagged type checks). | |
70482933 RK |
10419 | |
10420 | if Is_Entity_Name (Operand) | |
d15f9422 | 10421 | and then Has_Extra_Accessibility (Entity (Operand)) |
70482933 | 10422 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type |
d766cee3 RD |
10423 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
10424 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 10425 | then |
e84e11ba GD |
10426 | Apply_Accessibility_Check |
10427 | (Operand, Target_Type, Insert_Node => Operand); | |
70482933 | 10428 | |
e84e11ba | 10429 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
10430 | -- level of the target type, then force Program_Error. Note that this |
10431 | -- can only occur for cases where the attribute is within the body of | |
10432 | -- an instantiation (otherwise the conversion will already have been | |
10433 | -- rejected as illegal). Note: warnings are issued by the analyzer | |
10434 | -- for the instance cases. | |
70482933 RK |
10435 | |
10436 | elsif In_Instance_Body | |
07fc65c4 GB |
10437 | and then Type_Access_Level (Operand_Type) > |
10438 | Type_Access_Level (Target_Type) | |
70482933 | 10439 | then |
426908f8 | 10440 | Raise_Accessibility_Error; |
70482933 | 10441 | |
685094bf RD |
10442 | -- When the operand is a selected access discriminant the check needs |
10443 | -- to be made against the level of the object denoted by the prefix | |
10444 | -- of the selected name. Force Program_Error for this case as well | |
10445 | -- (this accessibility violation can only happen if within the body | |
10446 | -- of an instantiation). | |
70482933 RK |
10447 | |
10448 | elsif In_Instance_Body | |
10449 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
10450 | and then Nkind (Operand) = N_Selected_Component | |
10451 | and then Object_Access_Level (Operand) > | |
10452 | Type_Access_Level (Target_Type) | |
10453 | then | |
426908f8 | 10454 | Raise_Accessibility_Error; |
e606088a | 10455 | goto Done; |
70482933 RK |
10456 | end if; |
10457 | end if; | |
10458 | ||
10459 | -- Case of conversions of tagged types and access to tagged types | |
10460 | ||
685094bf RD |
10461 | -- When needed, that is to say when the expression is class-wide, Add |
10462 | -- runtime a tag check for (strict) downward conversion by using the | |
10463 | -- membership test, generating: | |
70482933 RK |
10464 | |
10465 | -- [constraint_error when Operand not in Target_Type'Class] | |
10466 | ||
10467 | -- or in the access type case | |
10468 | ||
10469 | -- [constraint_error | |
10470 | -- when Operand /= null | |
10471 | -- and then Operand.all not in | |
10472 | -- Designated_Type (Target_Type)'Class] | |
10473 | ||
10474 | if (Is_Access_Type (Target_Type) | |
10475 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
10476 | or else Is_Tagged_Type (Target_Type) | |
10477 | then | |
685094bf RD |
10478 | -- Do not do any expansion in the access type case if the parent is a |
10479 | -- renaming, since this is an error situation which will be caught by | |
10480 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 | 10481 | |
e7e4d230 | 10482 | if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then |
e606088a | 10483 | goto Done; |
70482933 RK |
10484 | end if; |
10485 | ||
0669bebe | 10486 | -- Otherwise, proceed with processing tagged conversion |
70482933 | 10487 | |
e7e4d230 | 10488 | Tagged_Conversion : declare |
8cea7b64 HK |
10489 | Actual_Op_Typ : Entity_Id; |
10490 | Actual_Targ_Typ : Entity_Id; | |
10491 | Make_Conversion : Boolean := False; | |
10492 | Root_Op_Typ : Entity_Id; | |
70482933 | 10493 | |
8cea7b64 HK |
10494 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
10495 | -- Create a membership check to test whether Operand is a member | |
10496 | -- of Targ_Typ. If the original Target_Type is an access, include | |
10497 | -- a test for null value. The check is inserted at N. | |
10498 | ||
10499 | -------------------- | |
10500 | -- Make_Tag_Check -- | |
10501 | -------------------- | |
10502 | ||
10503 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
10504 | Cond : Node_Id; | |
10505 | ||
10506 | begin | |
10507 | -- Generate: | |
10508 | -- [Constraint_Error | |
10509 | -- when Operand /= null | |
10510 | -- and then Operand.all not in Targ_Typ] | |
10511 | ||
10512 | if Is_Access_Type (Target_Type) then | |
10513 | Cond := | |
10514 | Make_And_Then (Loc, | |
10515 | Left_Opnd => | |
10516 | Make_Op_Ne (Loc, | |
10517 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
10518 | Right_Opnd => Make_Null (Loc)), | |
10519 | ||
10520 | Right_Opnd => | |
10521 | Make_Not_In (Loc, | |
10522 | Left_Opnd => | |
10523 | Make_Explicit_Dereference (Loc, | |
10524 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
e4494292 | 10525 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc))); |
8cea7b64 HK |
10526 | |
10527 | -- Generate: | |
10528 | -- [Constraint_Error when Operand not in Targ_Typ] | |
10529 | ||
10530 | else | |
10531 | Cond := | |
10532 | Make_Not_In (Loc, | |
10533 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
e4494292 | 10534 | Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)); |
8cea7b64 HK |
10535 | end if; |
10536 | ||
10537 | Insert_Action (N, | |
10538 | Make_Raise_Constraint_Error (Loc, | |
10539 | Condition => Cond, | |
10540 | Reason => CE_Tag_Check_Failed)); | |
10541 | end Make_Tag_Check; | |
10542 | ||
e7e4d230 | 10543 | -- Start of processing for Tagged_Conversion |
70482933 RK |
10544 | |
10545 | begin | |
9732e886 | 10546 | -- Handle entities from the limited view |
852dba80 | 10547 | |
9732e886 | 10548 | if Is_Access_Type (Operand_Type) then |
852dba80 AC |
10549 | Actual_Op_Typ := |
10550 | Available_View (Designated_Type (Operand_Type)); | |
9732e886 JM |
10551 | else |
10552 | Actual_Op_Typ := Operand_Type; | |
10553 | end if; | |
10554 | ||
10555 | if Is_Access_Type (Target_Type) then | |
852dba80 AC |
10556 | Actual_Targ_Typ := |
10557 | Available_View (Designated_Type (Target_Type)); | |
70482933 | 10558 | else |
8cea7b64 | 10559 | Actual_Targ_Typ := Target_Type; |
70482933 RK |
10560 | end if; |
10561 | ||
8cea7b64 HK |
10562 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
10563 | ||
20b5d666 JM |
10564 | -- Ada 2005 (AI-251): Handle interface type conversion |
10565 | ||
8cea7b64 | 10566 | if Is_Interface (Actual_Op_Typ) then |
f6f4d8d4 | 10567 | Expand_Interface_Conversion (N); |
e606088a | 10568 | goto Done; |
20b5d666 JM |
10569 | end if; |
10570 | ||
8cea7b64 | 10571 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 10572 | |
8cea7b64 HK |
10573 | -- Create a runtime tag check for a downward class-wide type |
10574 | -- conversion. | |
70482933 | 10575 | |
8cea7b64 | 10576 | if Is_Class_Wide_Type (Actual_Op_Typ) |
852dba80 | 10577 | and then Actual_Op_Typ /= Actual_Targ_Typ |
8cea7b64 | 10578 | and then Root_Op_Typ /= Actual_Targ_Typ |
4ac2477e JM |
10579 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ, |
10580 | Use_Full_View => True) | |
8cea7b64 HK |
10581 | then |
10582 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
10583 | Make_Conversion := True; | |
10584 | end if; | |
70482933 | 10585 | |
8cea7b64 HK |
10586 | -- AI05-0073: If the result subtype of the function is defined |
10587 | -- by an access_definition designating a specific tagged type | |
10588 | -- T, a check is made that the result value is null or the tag | |
10589 | -- of the object designated by the result value identifies T. | |
10590 | -- Constraint_Error is raised if this check fails. | |
70482933 | 10591 | |
92a7cd46 | 10592 | if Nkind (Parent (N)) = N_Simple_Return_Statement then |
8cea7b64 | 10593 | declare |
e886436a | 10594 | Func : Entity_Id; |
8cea7b64 HK |
10595 | Func_Typ : Entity_Id; |
10596 | ||
10597 | begin | |
e886436a | 10598 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 10599 | |
e886436a | 10600 | Func := Current_Scope; |
8cea7b64 HK |
10601 | while Present (Func) |
10602 | and then Ekind (Func) /= E_Function | |
10603 | loop | |
10604 | Func := Scope (Func); | |
10605 | end loop; | |
10606 | ||
10607 | -- The function's return subtype must be defined using | |
10608 | -- an access definition. | |
10609 | ||
10610 | if Nkind (Result_Definition (Parent (Func))) = | |
10611 | N_Access_Definition | |
10612 | then | |
10613 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
10614 | ||
10615 | -- The return subtype denotes a specific tagged type, | |
10616 | -- in other words, a non class-wide type. | |
10617 | ||
10618 | if Is_Tagged_Type (Func_Typ) | |
10619 | and then not Is_Class_Wide_Type (Func_Typ) | |
10620 | then | |
10621 | Make_Tag_Check (Actual_Targ_Typ); | |
10622 | Make_Conversion := True; | |
10623 | end if; | |
10624 | end if; | |
10625 | end; | |
70482933 RK |
10626 | end if; |
10627 | ||
8cea7b64 HK |
10628 | -- We have generated a tag check for either a class-wide type |
10629 | -- conversion or for AI05-0073. | |
70482933 | 10630 | |
8cea7b64 HK |
10631 | if Make_Conversion then |
10632 | declare | |
10633 | Conv : Node_Id; | |
10634 | begin | |
10635 | Conv := | |
10636 | Make_Unchecked_Type_Conversion (Loc, | |
10637 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
10638 | Expression => Relocate_Node (Expression (N))); | |
10639 | Rewrite (N, Conv); | |
10640 | Analyze_And_Resolve (N, Target_Type); | |
10641 | end; | |
10642 | end if; | |
70482933 | 10643 | end if; |
e7e4d230 | 10644 | end Tagged_Conversion; |
70482933 RK |
10645 | |
10646 | -- Case of other access type conversions | |
10647 | ||
10648 | elsif Is_Access_Type (Target_Type) then | |
10649 | Apply_Constraint_Check (Operand, Target_Type); | |
10650 | ||
10651 | -- Case of conversions from a fixed-point type | |
10652 | ||
685094bf RD |
10653 | -- These conversions require special expansion and processing, found in |
10654 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
10655 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
10656 | -- conversions, which do not need further processing. |
10657 | ||
10658 | elsif Is_Fixed_Point_Type (Operand_Type) | |
10659 | and then not Conversion_OK (N) | |
10660 | then | |
10661 | -- We should never see universal fixed at this case, since the | |
10662 | -- expansion of the constituent divide or multiply should have | |
10663 | -- eliminated the explicit mention of universal fixed. | |
10664 | ||
10665 | pragma Assert (Operand_Type /= Universal_Fixed); | |
10666 | ||
685094bf RD |
10667 | -- Check for special case of the conversion to universal real that |
10668 | -- occurs as a result of the use of a round attribute. In this case, | |
10669 | -- the real type for the conversion is taken from the target type of | |
10670 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
10671 | |
10672 | if Target_Type = Universal_Real | |
10673 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
10674 | and then Attribute_Name (Parent (N)) = Name_Round | |
10675 | then | |
10676 | Set_Rounded_Result (N); | |
10677 | Set_Etype (N, Etype (Parent (N))); | |
10678 | end if; | |
10679 | ||
10680 | -- Otherwise do correct fixed-conversion, but skip these if the | |
e7e4d230 AC |
10681 | -- Conversion_OK flag is set, because from a semantic point of view |
10682 | -- these are simple integer conversions needing no further processing | |
10683 | -- (the backend will simply treat them as integers). | |
70482933 RK |
10684 | |
10685 | if not Conversion_OK (N) then | |
10686 | if Is_Fixed_Point_Type (Etype (N)) then | |
10687 | Expand_Convert_Fixed_To_Fixed (N); | |
10688 | Real_Range_Check; | |
10689 | ||
10690 | elsif Is_Integer_Type (Etype (N)) then | |
10691 | Expand_Convert_Fixed_To_Integer (N); | |
10692 | ||
10693 | else | |
10694 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
10695 | Expand_Convert_Fixed_To_Float (N); | |
10696 | Real_Range_Check; | |
10697 | end if; | |
10698 | end if; | |
10699 | ||
10700 | -- Case of conversions to a fixed-point type | |
10701 | ||
685094bf RD |
10702 | -- These conversions require special expansion and processing, found in |
10703 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
10704 | -- since from a semantic point of view, these are simple integer | |
10705 | -- conversions, which do not need further processing. | |
70482933 RK |
10706 | |
10707 | elsif Is_Fixed_Point_Type (Target_Type) | |
10708 | and then not Conversion_OK (N) | |
10709 | then | |
10710 | if Is_Integer_Type (Operand_Type) then | |
10711 | Expand_Convert_Integer_To_Fixed (N); | |
10712 | Real_Range_Check; | |
10713 | else | |
10714 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
10715 | Expand_Convert_Float_To_Fixed (N); | |
10716 | Real_Range_Check; | |
10717 | end if; | |
10718 | ||
10719 | -- Case of float-to-integer conversions | |
10720 | ||
10721 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
10722 | -- since semantically the fixed-point target is treated as though it | |
10723 | -- were an integer in such cases. | |
10724 | ||
10725 | elsif Is_Floating_Point_Type (Operand_Type) | |
10726 | and then | |
10727 | (Is_Integer_Type (Target_Type) | |
10728 | or else | |
10729 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
10730 | then | |
70482933 RK |
10731 | -- One more check here, gcc is still not able to do conversions of |
10732 | -- this type with proper overflow checking, and so gigi is doing an | |
10733 | -- approximation of what is required by doing floating-point compares | |
10734 | -- with the end-point. But that can lose precision in some cases, and | |
f02b8bb8 | 10735 | -- give a wrong result. Converting the operand to Universal_Real is |
70482933 | 10736 | -- helpful, but still does not catch all cases with 64-bit integers |
e7e4d230 | 10737 | -- on targets with only 64-bit floats. |
0669bebe GB |
10738 | |
10739 | -- The above comment seems obsoleted by Apply_Float_Conversion_Check | |
10740 | -- Can this code be removed ??? | |
70482933 | 10741 | |
fbf5a39b AC |
10742 | if Do_Range_Check (Operand) then |
10743 | Rewrite (Operand, | |
70482933 RK |
10744 | Make_Type_Conversion (Loc, |
10745 | Subtype_Mark => | |
f02b8bb8 | 10746 | New_Occurrence_Of (Universal_Real, Loc), |
70482933 | 10747 | Expression => |
fbf5a39b | 10748 | Relocate_Node (Operand))); |
70482933 | 10749 | |
f02b8bb8 | 10750 | Set_Etype (Operand, Universal_Real); |
fbf5a39b AC |
10751 | Enable_Range_Check (Operand); |
10752 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
10753 | end if; |
10754 | ||
10755 | -- Case of array conversions | |
10756 | ||
685094bf RD |
10757 | -- Expansion of array conversions, add required length/range checks but |
10758 | -- only do this if there is no change of representation. For handling of | |
10759 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
10760 | |
10761 | elsif Is_Array_Type (Target_Type) then | |
70482933 RK |
10762 | if Is_Constrained (Target_Type) then |
10763 | Apply_Length_Check (Operand, Target_Type); | |
10764 | else | |
10765 | Apply_Range_Check (Operand, Target_Type); | |
10766 | end if; | |
10767 | ||
10768 | Handle_Changed_Representation; | |
10769 | ||
10770 | -- Case of conversions of discriminated types | |
10771 | ||
685094bf RD |
10772 | -- Add required discriminant checks if target is constrained. Again this |
10773 | -- change is skipped if we have a change of representation. | |
70482933 RK |
10774 | |
10775 | elsif Has_Discriminants (Target_Type) | |
10776 | and then Is_Constrained (Target_Type) | |
10777 | then | |
10778 | Apply_Discriminant_Check (Operand, Target_Type); | |
10779 | Handle_Changed_Representation; | |
10780 | ||
10781 | -- Case of all other record conversions. The only processing required | |
10782 | -- is to check for a change of representation requiring the special | |
10783 | -- assignment processing. | |
10784 | ||
10785 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
10786 | |
10787 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
10788 | -- a derived Unchecked_Union type to an unconstrained type that is |
10789 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
10790 | |
10791 | if Is_Derived_Type (Operand_Type) | |
10792 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
10793 | and then not Is_Constrained (Target_Type) | |
10794 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
10795 | and then not Has_Inferable_Discriminants (Operand) | |
10796 | then | |
685094bf | 10797 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e | 10798 | -- Program_Error node, but we give it the target type of the |
6cb3037c | 10799 | -- conversion (is this requirement documented somewhere ???) |
5d09245e AC |
10800 | |
10801 | declare | |
10802 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
10803 | Reason => PE_Unchecked_Union_Restriction); | |
10804 | ||
10805 | begin | |
10806 | Set_Etype (PE, Target_Type); | |
10807 | Rewrite (N, PE); | |
10808 | ||
10809 | end; | |
10810 | else | |
10811 | Handle_Changed_Representation; | |
10812 | end if; | |
70482933 RK |
10813 | |
10814 | -- Case of conversions of enumeration types | |
10815 | ||
10816 | elsif Is_Enumeration_Type (Target_Type) then | |
10817 | ||
10818 | -- Special processing is required if there is a change of | |
e7e4d230 | 10819 | -- representation (from enumeration representation clauses). |
70482933 RK |
10820 | |
10821 | if not Same_Representation (Target_Type, Operand_Type) then | |
10822 | ||
10823 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
10824 | ||
10825 | Rewrite (N, | |
1c66c4f5 AC |
10826 | Make_Attribute_Reference (Loc, |
10827 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
10828 | Attribute_Name => Name_Val, | |
10829 | Expressions => New_List ( | |
10830 | Make_Attribute_Reference (Loc, | |
10831 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
10832 | Attribute_Name => Name_Pos, | |
10833 | Expressions => New_List (Operand))))); | |
70482933 RK |
10834 | |
10835 | Analyze_And_Resolve (N, Target_Type); | |
10836 | end if; | |
10837 | ||
10838 | -- Case of conversions to floating-point | |
10839 | ||
10840 | elsif Is_Floating_Point_Type (Target_Type) then | |
10841 | Real_Range_Check; | |
70482933 RK |
10842 | end if; |
10843 | ||
685094bf | 10844 | -- At this stage, either the conversion node has been transformed into |
e7e4d230 AC |
10845 | -- some other equivalent expression, or left as a conversion that can be |
10846 | -- handled by Gigi, in the following cases: | |
70482933 RK |
10847 | |
10848 | -- Conversions with no change of representation or type | |
10849 | ||
685094bf RD |
10850 | -- Numeric conversions involving integer, floating- and fixed-point |
10851 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
10852 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 10853 | |
5e1c00fa RD |
10854 | -- No other conversions should be passed to Gigi |
10855 | ||
10856 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 10857 | |
685094bf RD |
10858 | -- The only remaining step is to generate a range check if we still have |
10859 | -- a type conversion at this stage and Do_Range_Check is set. For now we | |
10860 | -- do this only for conversions of discrete types. | |
fbf5a39b AC |
10861 | |
10862 | if Nkind (N) = N_Type_Conversion | |
10863 | and then Is_Discrete_Type (Etype (N)) | |
10864 | then | |
10865 | declare | |
10866 | Expr : constant Node_Id := Expression (N); | |
10867 | Ftyp : Entity_Id; | |
10868 | Ityp : Entity_Id; | |
10869 | ||
10870 | begin | |
10871 | if Do_Range_Check (Expr) | |
10872 | and then Is_Discrete_Type (Etype (Expr)) | |
10873 | then | |
10874 | Set_Do_Range_Check (Expr, False); | |
10875 | ||
685094bf RD |
10876 | -- Before we do a range check, we have to deal with treating a |
10877 | -- fixed-point operand as an integer. The way we do this is | |
10878 | -- simply to do an unchecked conversion to an appropriate | |
fbf5a39b AC |
10879 | -- integer type large enough to hold the result. |
10880 | ||
10881 | -- This code is not active yet, because we are only dealing | |
10882 | -- with discrete types so far ??? | |
10883 | ||
10884 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer | |
10885 | and then Treat_Fixed_As_Integer (Expr) | |
10886 | then | |
10887 | Ftyp := Base_Type (Etype (Expr)); | |
10888 | ||
10889 | if Esize (Ftyp) >= Esize (Standard_Integer) then | |
10890 | Ityp := Standard_Long_Long_Integer; | |
10891 | else | |
10892 | Ityp := Standard_Integer; | |
10893 | end if; | |
10894 | ||
10895 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); | |
10896 | end if; | |
10897 | ||
10898 | -- Reset overflow flag, since the range check will include | |
e7e4d230 | 10899 | -- dealing with possible overflow, and generate the check. If |
685094bf | 10900 | -- Address is either a source type or target type, suppress |
8a36a0cc AC |
10901 | -- range check to avoid typing anomalies when it is a visible |
10902 | -- integer type. | |
fbf5a39b AC |
10903 | |
10904 | Set_Do_Overflow_Check (N, False); | |
8a36a0cc AC |
10905 | if not Is_Descendent_Of_Address (Etype (Expr)) |
10906 | and then not Is_Descendent_Of_Address (Target_Type) | |
10907 | then | |
10908 | Generate_Range_Check | |
10909 | (Expr, Target_Type, CE_Range_Check_Failed); | |
10910 | end if; | |
fbf5a39b AC |
10911 | end if; |
10912 | end; | |
10913 | end if; | |
f02b8bb8 RD |
10914 | |
10915 | -- Final step, if the result is a type conversion involving Vax_Float | |
10916 | -- types, then it is subject for further special processing. | |
10917 | ||
10918 | if Nkind (N) = N_Type_Conversion | |
10919 | and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type)) | |
10920 | then | |
10921 | Expand_Vax_Conversion (N); | |
e606088a | 10922 | goto Done; |
f02b8bb8 | 10923 | end if; |
e606088a AC |
10924 | |
10925 | -- Here at end of processing | |
10926 | ||
48f91b44 RD |
10927 | <<Done>> |
10928 | -- Apply predicate check if required. Note that we can't just call | |
10929 | -- Apply_Predicate_Check here, because the type looks right after | |
10930 | -- the conversion and it would omit the check. The Comes_From_Source | |
10931 | -- guard is necessary to prevent infinite recursions when we generate | |
10932 | -- internal conversions for the purpose of checking predicates. | |
10933 | ||
10934 | if Present (Predicate_Function (Target_Type)) | |
10935 | and then Target_Type /= Operand_Type | |
10936 | and then Comes_From_Source (N) | |
10937 | then | |
00332244 AC |
10938 | declare |
10939 | New_Expr : constant Node_Id := Duplicate_Subexpr (N); | |
10940 | ||
10941 | begin | |
10942 | -- Avoid infinite recursion on the subsequent expansion of | |
10943 | -- of the copy of the original type conversion. | |
10944 | ||
10945 | Set_Comes_From_Source (New_Expr, False); | |
10946 | Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr)); | |
10947 | end; | |
48f91b44 | 10948 | end if; |
70482933 RK |
10949 | end Expand_N_Type_Conversion; |
10950 | ||
10951 | ----------------------------------- | |
10952 | -- Expand_N_Unchecked_Expression -- | |
10953 | ----------------------------------- | |
10954 | ||
e7e4d230 | 10955 | -- Remove the unchecked expression node from the tree. Its job was simply |
70482933 RK |
10956 | -- to make sure that its constituent expression was handled with checks |
10957 | -- off, and now that that is done, we can remove it from the tree, and | |
e7e4d230 | 10958 | -- indeed must, since Gigi does not expect to see these nodes. |
70482933 RK |
10959 | |
10960 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
10961 | Exp : constant Node_Id := Expression (N); | |
70482933 | 10962 | begin |
e7e4d230 | 10963 | Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp)); |
70482933 RK |
10964 | Rewrite (N, Exp); |
10965 | end Expand_N_Unchecked_Expression; | |
10966 | ||
10967 | ---------------------------------------- | |
10968 | -- Expand_N_Unchecked_Type_Conversion -- | |
10969 | ---------------------------------------- | |
10970 | ||
685094bf RD |
10971 | -- If this cannot be handled by Gigi and we haven't already made a |
10972 | -- temporary for it, do it now. | |
70482933 RK |
10973 | |
10974 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
10975 | Target_Type : constant Entity_Id := Etype (N); | |
10976 | Operand : constant Node_Id := Expression (N); | |
10977 | Operand_Type : constant Entity_Id := Etype (Operand); | |
10978 | ||
10979 | begin | |
7b00e31d | 10980 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 | 10981 | -- the conversion completely, it is useless, except that it may carry |
e7e4d230 | 10982 | -- an Assignment_OK indication which must be propagated to the operand. |
7b00e31d AC |
10983 | |
10984 | if Operand_Type = Target_Type then | |
13d923cc | 10985 | |
e7e4d230 AC |
10986 | -- Code duplicates Expand_N_Unchecked_Expression above, factor??? |
10987 | ||
7b00e31d AC |
10988 | if Assignment_OK (N) then |
10989 | Set_Assignment_OK (Operand); | |
10990 | end if; | |
10991 | ||
10992 | Rewrite (N, Relocate_Node (Operand)); | |
10993 | return; | |
10994 | end if; | |
10995 | ||
70482933 RK |
10996 | -- If we have a conversion of a compile time known value to a target |
10997 | -- type and the value is in range of the target type, then we can simply | |
10998 | -- replace the construct by an integer literal of the correct type. We | |
10999 | -- only apply this to integer types being converted. Possibly it may | |
11000 | -- apply in other cases, but it is too much trouble to worry about. | |
11001 | ||
11002 | -- Note that we do not do this transformation if the Kill_Range_Check | |
11003 | -- flag is set, since then the value may be outside the expected range. | |
11004 | -- This happens in the Normalize_Scalars case. | |
11005 | ||
20b5d666 JM |
11006 | -- We also skip this if either the target or operand type is biased |
11007 | -- because in this case, the unchecked conversion is supposed to | |
11008 | -- preserve the bit pattern, not the integer value. | |
11009 | ||
70482933 | 11010 | if Is_Integer_Type (Target_Type) |
20b5d666 | 11011 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 11012 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 11013 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
11014 | and then Compile_Time_Known_Value (Operand) |
11015 | and then not Kill_Range_Check (N) | |
11016 | then | |
11017 | declare | |
11018 | Val : constant Uint := Expr_Value (Operand); | |
11019 | ||
11020 | begin | |
11021 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
11022 | and then | |
11023 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
11024 | and then | |
11025 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
11026 | and then | |
11027 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
11028 | then | |
11029 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 11030 | |
685094bf RD |
11031 | -- If Address is the target type, just set the type to avoid a |
11032 | -- spurious type error on the literal when Address is a visible | |
11033 | -- integer type. | |
8a36a0cc AC |
11034 | |
11035 | if Is_Descendent_Of_Address (Target_Type) then | |
11036 | Set_Etype (N, Target_Type); | |
11037 | else | |
11038 | Analyze_And_Resolve (N, Target_Type); | |
11039 | end if; | |
11040 | ||
70482933 RK |
11041 | return; |
11042 | end if; | |
11043 | end; | |
11044 | end if; | |
11045 | ||
11046 | -- Nothing to do if conversion is safe | |
11047 | ||
11048 | if Safe_Unchecked_Type_Conversion (N) then | |
11049 | return; | |
11050 | end if; | |
11051 | ||
11052 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
324ac540 | 11053 | -- flag indicates ??? More comments needed here) |
70482933 RK |
11054 | |
11055 | if Assignment_OK (N) then | |
11056 | null; | |
11057 | else | |
11058 | Force_Evaluation (N); | |
11059 | end if; | |
11060 | end Expand_N_Unchecked_Type_Conversion; | |
11061 | ||
11062 | ---------------------------- | |
11063 | -- Expand_Record_Equality -- | |
11064 | ---------------------------- | |
11065 | ||
11066 | -- For non-variant records, Equality is expanded when needed into: | |
11067 | ||
11068 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
11069 | -- and then ... | |
11070 | -- and then Lhs.Discrn = Rhs.Discrn | |
11071 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
11072 | -- and then ... | |
11073 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
11074 | ||
11075 | -- The expression is folded by the back-end for adjacent fields. This | |
11076 | -- function is called for tagged record in only one occasion: for imple- | |
11077 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
11078 | -- otherwise the primitive "=" is used directly. | |
11079 | ||
11080 | function Expand_Record_Equality | |
11081 | (Nod : Node_Id; | |
11082 | Typ : Entity_Id; | |
11083 | Lhs : Node_Id; | |
11084 | Rhs : Node_Id; | |
2e071734 | 11085 | Bodies : List_Id) return Node_Id |
70482933 RK |
11086 | is |
11087 | Loc : constant Source_Ptr := Sloc (Nod); | |
11088 | ||
0ab80019 AC |
11089 | Result : Node_Id; |
11090 | C : Entity_Id; | |
11091 | ||
11092 | First_Time : Boolean := True; | |
11093 | ||
6b670dcf AC |
11094 | function Element_To_Compare (C : Entity_Id) return Entity_Id; |
11095 | -- Return the next discriminant or component to compare, starting with | |
11096 | -- C, skipping inherited components. | |
0ab80019 | 11097 | |
6b670dcf AC |
11098 | ------------------------ |
11099 | -- Element_To_Compare -- | |
11100 | ------------------------ | |
70482933 | 11101 | |
6b670dcf AC |
11102 | function Element_To_Compare (C : Entity_Id) return Entity_Id is |
11103 | Comp : Entity_Id; | |
28270211 | 11104 | |
70482933 | 11105 | begin |
6b670dcf | 11106 | Comp := C; |
6b670dcf AC |
11107 | loop |
11108 | -- Exit loop when the next element to be compared is found, or | |
11109 | -- there is no more such element. | |
70482933 | 11110 | |
6b670dcf | 11111 | exit when No (Comp); |
8190087e | 11112 | |
6b670dcf AC |
11113 | exit when Ekind_In (Comp, E_Discriminant, E_Component) |
11114 | and then not ( | |
70482933 | 11115 | |
6b670dcf | 11116 | -- Skip inherited components |
70482933 | 11117 | |
6b670dcf AC |
11118 | -- Note: for a tagged type, we always generate the "=" primitive |
11119 | -- for the base type (not on the first subtype), so the test for | |
11120 | -- Comp /= Original_Record_Component (Comp) is True for | |
11121 | -- inherited components only. | |
24558db8 | 11122 | |
6b670dcf | 11123 | (Is_Tagged_Type (Typ) |
28270211 | 11124 | and then Comp /= Original_Record_Component (Comp)) |
24558db8 | 11125 | |
6b670dcf | 11126 | -- Skip _Tag |
26bff3d9 | 11127 | |
6b670dcf AC |
11128 | or else Chars (Comp) = Name_uTag |
11129 | ||
11130 | -- The .NET/JVM version of type Root_Controlled contains two | |
11131 | -- fields which should not be considered part of the object. To | |
11132 | -- achieve proper equiality between two controlled objects on | |
11133 | -- .NET/JVM, skip _Parent whenever it has type Root_Controlled. | |
11134 | ||
11135 | or else (Chars (Comp) = Name_uParent | |
28270211 AC |
11136 | and then VM_Target /= No_VM |
11137 | and then Etype (Comp) = RTE (RE_Root_Controlled)) | |
6b670dcf AC |
11138 | |
11139 | -- Skip interface elements (secondary tags???) | |
11140 | ||
11141 | or else Is_Interface (Etype (Comp))); | |
11142 | ||
11143 | Next_Entity (Comp); | |
11144 | end loop; | |
11145 | ||
11146 | return Comp; | |
11147 | end Element_To_Compare; | |
70482933 | 11148 | |
70482933 RK |
11149 | -- Start of processing for Expand_Record_Equality |
11150 | ||
11151 | begin | |
70482933 RK |
11152 | -- Generates the following code: (assuming that Typ has one Discr and |
11153 | -- component C2 is also a record) | |
11154 | ||
11155 | -- True | |
11156 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
11157 | -- and then Lhs.C1 = Rhs.C1 | |
11158 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
11159 | -- and then ... | |
11160 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
11161 | ||
e4494292 | 11162 | Result := New_Occurrence_Of (Standard_True, Loc); |
6b670dcf | 11163 | C := Element_To_Compare (First_Entity (Typ)); |
70482933 | 11164 | while Present (C) loop |
70482933 RK |
11165 | declare |
11166 | New_Lhs : Node_Id; | |
11167 | New_Rhs : Node_Id; | |
8aceda64 | 11168 | Check : Node_Id; |
70482933 RK |
11169 | |
11170 | begin | |
11171 | if First_Time then | |
11172 | First_Time := False; | |
11173 | New_Lhs := Lhs; | |
11174 | New_Rhs := Rhs; | |
70482933 RK |
11175 | else |
11176 | New_Lhs := New_Copy_Tree (Lhs); | |
11177 | New_Rhs := New_Copy_Tree (Rhs); | |
11178 | end if; | |
11179 | ||
8aceda64 AC |
11180 | Check := |
11181 | Expand_Composite_Equality (Nod, Etype (C), | |
11182 | Lhs => | |
11183 | Make_Selected_Component (Loc, | |
8d80ff64 | 11184 | Prefix => New_Lhs, |
e4494292 | 11185 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
11186 | Rhs => |
11187 | Make_Selected_Component (Loc, | |
8d80ff64 | 11188 | Prefix => New_Rhs, |
e4494292 | 11189 | Selector_Name => New_Occurrence_Of (C, Loc)), |
8aceda64 AC |
11190 | Bodies => Bodies); |
11191 | ||
11192 | -- If some (sub)component is an unchecked_union, the whole | |
11193 | -- operation will raise program error. | |
11194 | ||
11195 | if Nkind (Check) = N_Raise_Program_Error then | |
11196 | Result := Check; | |
11197 | Set_Etype (Result, Standard_Boolean); | |
11198 | exit; | |
11199 | else | |
11200 | Result := | |
11201 | Make_And_Then (Loc, | |
11202 | Left_Opnd => Result, | |
11203 | Right_Opnd => Check); | |
11204 | end if; | |
70482933 RK |
11205 | end; |
11206 | ||
6b670dcf | 11207 | C := Element_To_Compare (Next_Entity (C)); |
70482933 RK |
11208 | end loop; |
11209 | ||
11210 | return Result; | |
11211 | end Expand_Record_Equality; | |
11212 | ||
a3068ca6 AC |
11213 | --------------------------- |
11214 | -- Expand_Set_Membership -- | |
11215 | --------------------------- | |
11216 | ||
11217 | procedure Expand_Set_Membership (N : Node_Id) is | |
11218 | Lop : constant Node_Id := Left_Opnd (N); | |
11219 | Alt : Node_Id; | |
11220 | Res : Node_Id; | |
11221 | ||
11222 | function Make_Cond (Alt : Node_Id) return Node_Id; | |
11223 | -- If the alternative is a subtype mark, create a simple membership | |
11224 | -- test. Otherwise create an equality test for it. | |
11225 | ||
11226 | --------------- | |
11227 | -- Make_Cond -- | |
11228 | --------------- | |
11229 | ||
11230 | function Make_Cond (Alt : Node_Id) return Node_Id is | |
11231 | Cond : Node_Id; | |
11232 | L : constant Node_Id := New_Copy (Lop); | |
11233 | R : constant Node_Id := Relocate_Node (Alt); | |
11234 | ||
11235 | begin | |
11236 | if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt))) | |
11237 | or else Nkind (Alt) = N_Range | |
11238 | then | |
11239 | Cond := | |
11240 | Make_In (Sloc (Alt), | |
11241 | Left_Opnd => L, | |
11242 | Right_Opnd => R); | |
11243 | else | |
11244 | Cond := | |
11245 | Make_Op_Eq (Sloc (Alt), | |
11246 | Left_Opnd => L, | |
11247 | Right_Opnd => R); | |
11248 | end if; | |
11249 | ||
11250 | return Cond; | |
11251 | end Make_Cond; | |
11252 | ||
11253 | -- Start of processing for Expand_Set_Membership | |
11254 | ||
11255 | begin | |
11256 | Remove_Side_Effects (Lop); | |
11257 | ||
11258 | Alt := Last (Alternatives (N)); | |
11259 | Res := Make_Cond (Alt); | |
11260 | ||
11261 | Prev (Alt); | |
11262 | while Present (Alt) loop | |
11263 | Res := | |
11264 | Make_Or_Else (Sloc (Alt), | |
11265 | Left_Opnd => Make_Cond (Alt), | |
11266 | Right_Opnd => Res); | |
11267 | Prev (Alt); | |
11268 | end loop; | |
11269 | ||
11270 | Rewrite (N, Res); | |
11271 | Analyze_And_Resolve (N, Standard_Boolean); | |
11272 | end Expand_Set_Membership; | |
11273 | ||
5875f8d6 AC |
11274 | ----------------------------------- |
11275 | -- Expand_Short_Circuit_Operator -- | |
11276 | ----------------------------------- | |
11277 | ||
955871d3 AC |
11278 | -- Deal with special expansion if actions are present for the right operand |
11279 | -- and deal with optimizing case of arguments being True or False. We also | |
11280 | -- deal with the special case of non-standard boolean values. | |
5875f8d6 AC |
11281 | |
11282 | procedure Expand_Short_Circuit_Operator (N : Node_Id) is | |
11283 | Loc : constant Source_Ptr := Sloc (N); | |
11284 | Typ : constant Entity_Id := Etype (N); | |
5875f8d6 AC |
11285 | Left : constant Node_Id := Left_Opnd (N); |
11286 | Right : constant Node_Id := Right_Opnd (N); | |
955871d3 | 11287 | LocR : constant Source_Ptr := Sloc (Right); |
5875f8d6 AC |
11288 | Actlist : List_Id; |
11289 | ||
11290 | Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else; | |
11291 | Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value); | |
11292 | -- If Left = Shortcut_Value then Right need not be evaluated | |
11293 | ||
5875f8d6 AC |
11294 | begin |
11295 | -- Deal with non-standard booleans | |
11296 | ||
11297 | if Is_Boolean_Type (Typ) then | |
11298 | Adjust_Condition (Left); | |
11299 | Adjust_Condition (Right); | |
11300 | Set_Etype (N, Standard_Boolean); | |
11301 | end if; | |
11302 | ||
11303 | -- Check for cases where left argument is known to be True or False | |
11304 | ||
11305 | if Compile_Time_Known_Value (Left) then | |
25adc5fb AC |
11306 | |
11307 | -- Mark SCO for left condition as compile time known | |
11308 | ||
11309 | if Generate_SCO and then Comes_From_Source (Left) then | |
11310 | Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True); | |
11311 | end if; | |
11312 | ||
5875f8d6 AC |
11313 | -- Rewrite True AND THEN Right / False OR ELSE Right to Right. |
11314 | -- Any actions associated with Right will be executed unconditionally | |
11315 | -- and can thus be inserted into the tree unconditionally. | |
11316 | ||
11317 | if Expr_Value_E (Left) /= Shortcut_Ent then | |
11318 | if Present (Actions (N)) then | |
11319 | Insert_Actions (N, Actions (N)); | |
11320 | end if; | |
11321 | ||
11322 | Rewrite (N, Right); | |
11323 | ||
11324 | -- Rewrite False AND THEN Right / True OR ELSE Right to Left. | |
11325 | -- In this case we can forget the actions associated with Right, | |
11326 | -- since they will never be executed. | |
11327 | ||
11328 | else | |
11329 | Kill_Dead_Code (Right); | |
11330 | Kill_Dead_Code (Actions (N)); | |
11331 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
11332 | end if; | |
11333 | ||
11334 | Adjust_Result_Type (N, Typ); | |
11335 | return; | |
11336 | end if; | |
11337 | ||
955871d3 AC |
11338 | -- If Actions are present for the right operand, we have to do some |
11339 | -- special processing. We can't just let these actions filter back into | |
11340 | -- code preceding the short circuit (which is what would have happened | |
11341 | -- if we had not trapped them in the short-circuit form), since they | |
11342 | -- must only be executed if the right operand of the short circuit is | |
11343 | -- executed and not otherwise. | |
5875f8d6 | 11344 | |
955871d3 AC |
11345 | if Present (Actions (N)) then |
11346 | Actlist := Actions (N); | |
5875f8d6 | 11347 | |
0812b84e AC |
11348 | -- We now use an Expression_With_Actions node for the right operand |
11349 | -- of the short-circuit form. Note that this solves the traceability | |
11350 | -- problems for coverage analysis. | |
5875f8d6 | 11351 | |
0812b84e AC |
11352 | Rewrite (Right, |
11353 | Make_Expression_With_Actions (LocR, | |
11354 | Expression => Relocate_Node (Right), | |
11355 | Actions => Actlist)); | |
11356 | Set_Actions (N, No_List); | |
11357 | Analyze_And_Resolve (Right, Standard_Boolean); | |
955871d3 | 11358 | |
5875f8d6 AC |
11359 | Adjust_Result_Type (N, Typ); |
11360 | return; | |
11361 | end if; | |
11362 | ||
11363 | -- No actions present, check for cases of right argument True/False | |
11364 | ||
11365 | if Compile_Time_Known_Value (Right) then | |
25adc5fb AC |
11366 | |
11367 | -- Mark SCO for left condition as compile time known | |
11368 | ||
11369 | if Generate_SCO and then Comes_From_Source (Right) then | |
11370 | Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True); | |
11371 | end if; | |
11372 | ||
5875f8d6 AC |
11373 | -- Change (Left and then True), (Left or else False) to Left. |
11374 | -- Note that we know there are no actions associated with the right | |
11375 | -- operand, since we just checked for this case above. | |
11376 | ||
11377 | if Expr_Value_E (Right) /= Shortcut_Ent then | |
11378 | Rewrite (N, Left); | |
11379 | ||
11380 | -- Change (Left and then False), (Left or else True) to Right, | |
11381 | -- making sure to preserve any side effects associated with the Left | |
11382 | -- operand. | |
11383 | ||
11384 | else | |
11385 | Remove_Side_Effects (Left); | |
11386 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
11387 | end if; | |
11388 | end if; | |
11389 | ||
11390 | Adjust_Result_Type (N, Typ); | |
11391 | end Expand_Short_Circuit_Operator; | |
11392 | ||
70482933 RK |
11393 | ------------------------------------- |
11394 | -- Fixup_Universal_Fixed_Operation -- | |
11395 | ------------------------------------- | |
11396 | ||
11397 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
11398 | Conv : constant Node_Id := Parent (N); | |
11399 | ||
11400 | begin | |
11401 | -- We must have a type conversion immediately above us | |
11402 | ||
11403 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
11404 | ||
11405 | -- Normally the type conversion gives our target type. The exception | |
11406 | -- occurs in the case of the Round attribute, where the conversion | |
11407 | -- will be to universal real, and our real type comes from the Round | |
11408 | -- attribute (as well as an indication that we must round the result) | |
11409 | ||
11410 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
11411 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
11412 | then | |
11413 | Set_Etype (N, Etype (Parent (Conv))); | |
11414 | Set_Rounded_Result (N); | |
11415 | ||
11416 | -- Normal case where type comes from conversion above us | |
11417 | ||
11418 | else | |
11419 | Set_Etype (N, Etype (Conv)); | |
11420 | end if; | |
11421 | end Fixup_Universal_Fixed_Operation; | |
11422 | ||
5d09245e AC |
11423 | --------------------------------- |
11424 | -- Has_Inferable_Discriminants -- | |
11425 | --------------------------------- | |
11426 | ||
11427 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
11428 | ||
11429 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
11430 | -- Determines whether the left-most prefix of a selected component is a | |
11431 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
11432 | ||
11433 | -------------------------------- | |
11434 | -- Prefix_Is_Formal_Parameter -- | |
11435 | -------------------------------- | |
11436 | ||
11437 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
83bb90af | 11438 | Sel_Comp : Node_Id; |
5d09245e AC |
11439 | |
11440 | begin | |
11441 | -- Move to the left-most prefix by climbing up the tree | |
11442 | ||
83bb90af | 11443 | Sel_Comp := N; |
5d09245e AC |
11444 | while Present (Parent (Sel_Comp)) |
11445 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
11446 | loop | |
11447 | Sel_Comp := Parent (Sel_Comp); | |
11448 | end loop; | |
11449 | ||
11450 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
11451 | end Prefix_Is_Formal_Parameter; | |
11452 | ||
11453 | -- Start of processing for Has_Inferable_Discriminants | |
11454 | ||
11455 | begin | |
5d09245e AC |
11456 | -- For selected components, the subtype of the selector must be a |
11457 | -- constrained Unchecked_Union. If the component is subject to a | |
11458 | -- per-object constraint, then the enclosing object must have inferable | |
11459 | -- discriminants. | |
11460 | ||
83bb90af | 11461 | if Nkind (N) = N_Selected_Component then |
5d09245e AC |
11462 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then |
11463 | ||
11464 | -- A small hack. If we have a per-object constrained selected | |
11465 | -- component of a formal parameter, return True since we do not | |
11466 | -- know the actual parameter association yet. | |
11467 | ||
11468 | if Prefix_Is_Formal_Parameter (N) then | |
11469 | return True; | |
5d09245e AC |
11470 | |
11471 | -- Otherwise, check the enclosing object and the selector | |
11472 | ||
83bb90af TQ |
11473 | else |
11474 | return Has_Inferable_Discriminants (Prefix (N)) | |
11475 | and then Has_Inferable_Discriminants (Selector_Name (N)); | |
11476 | end if; | |
5d09245e AC |
11477 | |
11478 | -- The call to Has_Inferable_Discriminants will determine whether | |
11479 | -- the selector has a constrained Unchecked_Union nominal type. | |
11480 | ||
83bb90af TQ |
11481 | else |
11482 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
11483 | end if; | |
5d09245e AC |
11484 | |
11485 | -- A qualified expression has inferable discriminants if its subtype | |
11486 | -- mark is a constrained Unchecked_Union subtype. | |
11487 | ||
11488 | elsif Nkind (N) = N_Qualified_Expression then | |
053cf994 | 11489 | return Is_Unchecked_Union (Etype (Subtype_Mark (N))) |
5b5b27ad | 11490 | and then Is_Constrained (Etype (Subtype_Mark (N))); |
5d09245e | 11491 | |
83bb90af TQ |
11492 | -- For all other names, it is sufficient to have a constrained |
11493 | -- Unchecked_Union nominal subtype. | |
11494 | ||
11495 | else | |
11496 | return Is_Unchecked_Union (Base_Type (Etype (N))) | |
11497 | and then Is_Constrained (Etype (N)); | |
11498 | end if; | |
5d09245e AC |
11499 | end Has_Inferable_Discriminants; |
11500 | ||
70482933 RK |
11501 | ------------------------------- |
11502 | -- Insert_Dereference_Action -- | |
11503 | ------------------------------- | |
11504 | ||
11505 | procedure Insert_Dereference_Action (N : Node_Id) is | |
8777c5a6 | 11506 | |
70482933 | 11507 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; |
2e071734 AC |
11508 | -- Return true if type of P is derived from Checked_Pool; |
11509 | ||
11510 | ----------------------------- | |
11511 | -- Is_Checked_Storage_Pool -- | |
11512 | ----------------------------- | |
70482933 RK |
11513 | |
11514 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
11515 | T : Entity_Id; | |
761f7dcb | 11516 | |
70482933 RK |
11517 | begin |
11518 | if No (P) then | |
11519 | return False; | |
11520 | end if; | |
11521 | ||
11522 | T := Etype (P); | |
11523 | while T /= Etype (T) loop | |
11524 | if Is_RTE (T, RE_Checked_Pool) then | |
11525 | return True; | |
11526 | else | |
11527 | T := Etype (T); | |
11528 | end if; | |
11529 | end loop; | |
11530 | ||
11531 | return False; | |
11532 | end Is_Checked_Storage_Pool; | |
11533 | ||
b0d71355 HK |
11534 | -- Local variables |
11535 | ||
11536 | Typ : constant Entity_Id := Etype (N); | |
11537 | Desig : constant Entity_Id := Available_View (Designated_Type (Typ)); | |
11538 | Loc : constant Source_Ptr := Sloc (N); | |
11539 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
11540 | Pnod : constant Node_Id := Parent (N); | |
11541 | ||
11542 | Addr : Entity_Id; | |
11543 | Alig : Entity_Id; | |
11544 | Deref : Node_Id; | |
11545 | Size : Entity_Id; | |
11546 | Stmt : Node_Id; | |
11547 | ||
70482933 RK |
11548 | -- Start of processing for Insert_Dereference_Action |
11549 | ||
11550 | begin | |
e6f69614 AC |
11551 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
11552 | ||
b0d71355 HK |
11553 | -- Do not re-expand a dereference which has already been processed by |
11554 | -- this routine. | |
11555 | ||
11556 | if Has_Dereference_Action (Pnod) then | |
70482933 | 11557 | return; |
70482933 | 11558 | |
b0d71355 HK |
11559 | -- Do not perform this type of expansion for internally-generated |
11560 | -- dereferences. | |
70482933 | 11561 | |
b0d71355 HK |
11562 | elsif not Comes_From_Source (Original_Node (Pnod)) then |
11563 | return; | |
70482933 | 11564 | |
b0d71355 HK |
11565 | -- A dereference action is only applicable to objects which have been |
11566 | -- allocated on a checked pool. | |
70482933 | 11567 | |
b0d71355 HK |
11568 | elsif not Is_Checked_Storage_Pool (Pool) then |
11569 | return; | |
11570 | end if; | |
70482933 | 11571 | |
b0d71355 | 11572 | -- Extract the address of the dereferenced object. Generate: |
8777c5a6 | 11573 | |
b0d71355 | 11574 | -- Addr : System.Address := <N>'Pool_Address; |
70482933 | 11575 | |
b0d71355 | 11576 | Addr := Make_Temporary (Loc, 'P'); |
70482933 | 11577 | |
b0d71355 HK |
11578 | Insert_Action (N, |
11579 | Make_Object_Declaration (Loc, | |
11580 | Defining_Identifier => Addr, | |
11581 | Object_Definition => | |
e4494292 | 11582 | New_Occurrence_Of (RTE (RE_Address), Loc), |
b0d71355 HK |
11583 | Expression => |
11584 | Make_Attribute_Reference (Loc, | |
11585 | Prefix => Duplicate_Subexpr_Move_Checks (N), | |
11586 | Attribute_Name => Name_Pool_Address))); | |
11587 | ||
11588 | -- Calculate the size of the dereferenced object. Generate: | |
8777c5a6 | 11589 | |
b0d71355 HK |
11590 | -- Size : Storage_Count := <N>.all'Size / Storage_Unit; |
11591 | ||
11592 | Deref := | |
11593 | Make_Explicit_Dereference (Loc, | |
11594 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11595 | Set_Has_Dereference_Action (Deref); | |
70482933 | 11596 | |
b0d71355 HK |
11597 | Size := Make_Temporary (Loc, 'S'); |
11598 | ||
11599 | Insert_Action (N, | |
11600 | Make_Object_Declaration (Loc, | |
11601 | Defining_Identifier => Size, | |
8777c5a6 | 11602 | |
b0d71355 | 11603 | Object_Definition => |
e4494292 | 11604 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
8777c5a6 | 11605 | |
b0d71355 HK |
11606 | Expression => |
11607 | Make_Op_Divide (Loc, | |
11608 | Left_Opnd => | |
70482933 | 11609 | Make_Attribute_Reference (Loc, |
b0d71355 | 11610 | Prefix => Deref, |
70482933 RK |
11611 | Attribute_Name => Name_Size), |
11612 | Right_Opnd => | |
b0d71355 | 11613 | Make_Integer_Literal (Loc, System_Storage_Unit)))); |
70482933 | 11614 | |
b0d71355 HK |
11615 | -- Calculate the alignment of the dereferenced object. Generate: |
11616 | -- Alig : constant Storage_Count := <N>.all'Alignment; | |
70482933 | 11617 | |
b0d71355 HK |
11618 | Deref := |
11619 | Make_Explicit_Dereference (Loc, | |
11620 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11621 | Set_Has_Dereference_Action (Deref); | |
11622 | ||
11623 | Alig := Make_Temporary (Loc, 'A'); | |
11624 | ||
11625 | Insert_Action (N, | |
11626 | Make_Object_Declaration (Loc, | |
11627 | Defining_Identifier => Alig, | |
11628 | Object_Definition => | |
e4494292 | 11629 | New_Occurrence_Of (RTE (RE_Storage_Count), Loc), |
b0d71355 HK |
11630 | Expression => |
11631 | Make_Attribute_Reference (Loc, | |
11632 | Prefix => Deref, | |
11633 | Attribute_Name => Name_Alignment))); | |
11634 | ||
11635 | -- A dereference of a controlled object requires special processing. The | |
11636 | -- finalization machinery requests additional space from the underlying | |
11637 | -- pool to allocate and hide two pointers. As a result, a checked pool | |
11638 | -- may mark the wrong memory as valid. Since checked pools do not have | |
11639 | -- knowledge of hidden pointers, we have to bring the two pointers back | |
11640 | -- in view in order to restore the original state of the object. | |
11641 | ||
11642 | if Needs_Finalization (Desig) then | |
11643 | ||
11644 | -- Adjust the address and size of the dereferenced object. Generate: | |
11645 | -- Adjust_Controlled_Dereference (Addr, Size, Alig); | |
11646 | ||
11647 | Stmt := | |
11648 | Make_Procedure_Call_Statement (Loc, | |
11649 | Name => | |
e4494292 | 11650 | New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc), |
b0d71355 | 11651 | Parameter_Associations => New_List ( |
e4494292 RD |
11652 | New_Occurrence_Of (Addr, Loc), |
11653 | New_Occurrence_Of (Size, Loc), | |
11654 | New_Occurrence_Of (Alig, Loc))); | |
b0d71355 HK |
11655 | |
11656 | -- Class-wide types complicate things because we cannot determine | |
11657 | -- statically whether the actual object is truly controlled. We must | |
11658 | -- generate a runtime check to detect this property. Generate: | |
11659 | -- | |
11660 | -- if Needs_Finalization (<N>.all'Tag) then | |
11661 | -- <Stmt>; | |
11662 | -- end if; | |
11663 | ||
11664 | if Is_Class_Wide_Type (Desig) then | |
11665 | Deref := | |
11666 | Make_Explicit_Dereference (Loc, | |
11667 | Prefix => Duplicate_Subexpr_Move_Checks (N)); | |
11668 | Set_Has_Dereference_Action (Deref); | |
11669 | ||
11670 | Stmt := | |
8b1011c0 | 11671 | Make_Implicit_If_Statement (N, |
b0d71355 HK |
11672 | Condition => |
11673 | Make_Function_Call (Loc, | |
11674 | Name => | |
e4494292 | 11675 | New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc), |
b0d71355 HK |
11676 | Parameter_Associations => New_List ( |
11677 | Make_Attribute_Reference (Loc, | |
11678 | Prefix => Deref, | |
11679 | Attribute_Name => Name_Tag))), | |
11680 | Then_Statements => New_List (Stmt)); | |
11681 | end if; | |
11682 | ||
11683 | Insert_Action (N, Stmt); | |
11684 | end if; | |
11685 | ||
11686 | -- Generate: | |
11687 | -- Dereference (Pool, Addr, Size, Alig); | |
11688 | ||
11689 | Insert_Action (N, | |
11690 | Make_Procedure_Call_Statement (Loc, | |
11691 | Name => | |
e4494292 | 11692 | New_Occurrence_Of |
b0d71355 HK |
11693 | (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), |
11694 | Parameter_Associations => New_List ( | |
e4494292 RD |
11695 | New_Occurrence_Of (Pool, Loc), |
11696 | New_Occurrence_Of (Addr, Loc), | |
11697 | New_Occurrence_Of (Size, Loc), | |
11698 | New_Occurrence_Of (Alig, Loc)))); | |
b0d71355 HK |
11699 | |
11700 | -- Mark the explicit dereference as processed to avoid potential | |
11701 | -- infinite expansion. | |
11702 | ||
11703 | Set_Has_Dereference_Action (Pnod); | |
70482933 | 11704 | |
fbf5a39b AC |
11705 | exception |
11706 | when RE_Not_Available => | |
11707 | return; | |
70482933 RK |
11708 | end Insert_Dereference_Action; |
11709 | ||
fdfcc663 AC |
11710 | -------------------------------- |
11711 | -- Integer_Promotion_Possible -- | |
11712 | -------------------------------- | |
11713 | ||
11714 | function Integer_Promotion_Possible (N : Node_Id) return Boolean is | |
11715 | Operand : constant Node_Id := Expression (N); | |
11716 | Operand_Type : constant Entity_Id := Etype (Operand); | |
11717 | Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type); | |
11718 | ||
11719 | begin | |
11720 | pragma Assert (Nkind (N) = N_Type_Conversion); | |
11721 | ||
11722 | return | |
11723 | ||
11724 | -- We only do the transformation for source constructs. We assume | |
11725 | -- that the expander knows what it is doing when it generates code. | |
11726 | ||
11727 | Comes_From_Source (N) | |
11728 | ||
11729 | -- If the operand type is Short_Integer or Short_Short_Integer, | |
11730 | -- then we will promote to Integer, which is available on all | |
11731 | -- targets, and is sufficient to ensure no intermediate overflow. | |
11732 | -- Furthermore it is likely to be as efficient or more efficient | |
11733 | -- than using the smaller type for the computation so we do this | |
11734 | -- unconditionally. | |
11735 | ||
11736 | and then | |
11737 | (Root_Operand_Type = Base_Type (Standard_Short_Integer) | |
761f7dcb | 11738 | or else |
fdfcc663 AC |
11739 | Root_Operand_Type = Base_Type (Standard_Short_Short_Integer)) |
11740 | ||
11741 | -- Test for interesting operation, which includes addition, | |
5f3f175d AC |
11742 | -- division, exponentiation, multiplication, subtraction, absolute |
11743 | -- value and unary negation. Unary "+" is omitted since it is a | |
11744 | -- no-op and thus can't overflow. | |
fdfcc663 | 11745 | |
5f3f175d AC |
11746 | and then Nkind_In (Operand, N_Op_Abs, |
11747 | N_Op_Add, | |
fdfcc663 AC |
11748 | N_Op_Divide, |
11749 | N_Op_Expon, | |
11750 | N_Op_Minus, | |
11751 | N_Op_Multiply, | |
11752 | N_Op_Subtract); | |
11753 | end Integer_Promotion_Possible; | |
11754 | ||
70482933 RK |
11755 | ------------------------------ |
11756 | -- Make_Array_Comparison_Op -- | |
11757 | ------------------------------ | |
11758 | ||
11759 | -- This is a hand-coded expansion of the following generic function: | |
11760 | ||
11761 | -- generic | |
11762 | -- type elem is (<>); | |
11763 | -- type index is (<>); | |
11764 | -- type a is array (index range <>) of elem; | |
20b5d666 | 11765 | |
70482933 RK |
11766 | -- function Gnnn (X : a; Y: a) return boolean is |
11767 | -- J : index := Y'first; | |
20b5d666 | 11768 | |
70482933 RK |
11769 | -- begin |
11770 | -- if X'length = 0 then | |
11771 | -- return false; | |
20b5d666 | 11772 | |
70482933 RK |
11773 | -- elsif Y'length = 0 then |
11774 | -- return true; | |
20b5d666 | 11775 | |
70482933 RK |
11776 | -- else |
11777 | -- for I in X'range loop | |
11778 | -- if X (I) = Y (J) then | |
11779 | -- if J = Y'last then | |
11780 | -- exit; | |
11781 | -- else | |
11782 | -- J := index'succ (J); | |
11783 | -- end if; | |
20b5d666 | 11784 | |
70482933 RK |
11785 | -- else |
11786 | -- return X (I) > Y (J); | |
11787 | -- end if; | |
11788 | -- end loop; | |
20b5d666 | 11789 | |
70482933 RK |
11790 | -- return X'length > Y'length; |
11791 | -- end if; | |
11792 | -- end Gnnn; | |
11793 | ||
11794 | -- Note that since we are essentially doing this expansion by hand, we | |
11795 | -- do not need to generate an actual or formal generic part, just the | |
11796 | -- instantiated function itself. | |
11797 | ||
11798 | function Make_Array_Comparison_Op | |
2e071734 AC |
11799 | (Typ : Entity_Id; |
11800 | Nod : Node_Id) return Node_Id | |
70482933 RK |
11801 | is |
11802 | Loc : constant Source_Ptr := Sloc (Nod); | |
11803 | ||
11804 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
11805 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
11806 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
11807 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
11808 | ||
11809 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
11810 | ||
11811 | Loop_Statement : Node_Id; | |
11812 | Loop_Body : Node_Id; | |
11813 | If_Stat : Node_Id; | |
11814 | Inner_If : Node_Id; | |
11815 | Final_Expr : Node_Id; | |
11816 | Func_Body : Node_Id; | |
11817 | Func_Name : Entity_Id; | |
11818 | Formals : List_Id; | |
11819 | Length1 : Node_Id; | |
11820 | Length2 : Node_Id; | |
11821 | ||
11822 | begin | |
11823 | -- if J = Y'last then | |
11824 | -- exit; | |
11825 | -- else | |
11826 | -- J := index'succ (J); | |
11827 | -- end if; | |
11828 | ||
11829 | Inner_If := | |
11830 | Make_Implicit_If_Statement (Nod, | |
11831 | Condition => | |
11832 | Make_Op_Eq (Loc, | |
e4494292 | 11833 | Left_Opnd => New_Occurrence_Of (J, Loc), |
70482933 RK |
11834 | Right_Opnd => |
11835 | Make_Attribute_Reference (Loc, | |
e4494292 | 11836 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
11837 | Attribute_Name => Name_Last)), |
11838 | ||
11839 | Then_Statements => New_List ( | |
11840 | Make_Exit_Statement (Loc)), | |
11841 | ||
11842 | Else_Statements => | |
11843 | New_List ( | |
11844 | Make_Assignment_Statement (Loc, | |
e4494292 | 11845 | Name => New_Occurrence_Of (J, Loc), |
70482933 RK |
11846 | Expression => |
11847 | Make_Attribute_Reference (Loc, | |
e4494292 | 11848 | Prefix => New_Occurrence_Of (Index, Loc), |
70482933 | 11849 | Attribute_Name => Name_Succ, |
e4494292 | 11850 | Expressions => New_List (New_Occurrence_Of (J, Loc)))))); |
70482933 RK |
11851 | |
11852 | -- if X (I) = Y (J) then | |
11853 | -- if ... end if; | |
11854 | -- else | |
11855 | -- return X (I) > Y (J); | |
11856 | -- end if; | |
11857 | ||
11858 | Loop_Body := | |
11859 | Make_Implicit_If_Statement (Nod, | |
11860 | Condition => | |
11861 | Make_Op_Eq (Loc, | |
11862 | Left_Opnd => | |
11863 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11864 | Prefix => New_Occurrence_Of (X, Loc), |
11865 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
11866 | |
11867 | Right_Opnd => | |
11868 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11869 | Prefix => New_Occurrence_Of (Y, Loc), |
11870 | Expressions => New_List (New_Occurrence_Of (J, Loc)))), | |
70482933 RK |
11871 | |
11872 | Then_Statements => New_List (Inner_If), | |
11873 | ||
11874 | Else_Statements => New_List ( | |
d766cee3 | 11875 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
11876 | Expression => |
11877 | Make_Op_Gt (Loc, | |
11878 | Left_Opnd => | |
11879 | Make_Indexed_Component (Loc, | |
e4494292 RD |
11880 | Prefix => New_Occurrence_Of (X, Loc), |
11881 | Expressions => New_List (New_Occurrence_Of (I, Loc))), | |
70482933 RK |
11882 | |
11883 | Right_Opnd => | |
11884 | Make_Indexed_Component (Loc, | |
e4494292 | 11885 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 | 11886 | Expressions => New_List ( |
e4494292 | 11887 | New_Occurrence_Of (J, Loc))))))); |
70482933 RK |
11888 | |
11889 | -- for I in X'range loop | |
11890 | -- if ... end if; | |
11891 | -- end loop; | |
11892 | ||
11893 | Loop_Statement := | |
11894 | Make_Implicit_Loop_Statement (Nod, | |
11895 | Identifier => Empty, | |
11896 | ||
11897 | Iteration_Scheme => | |
11898 | Make_Iteration_Scheme (Loc, | |
11899 | Loop_Parameter_Specification => | |
11900 | Make_Loop_Parameter_Specification (Loc, | |
11901 | Defining_Identifier => I, | |
11902 | Discrete_Subtype_Definition => | |
11903 | Make_Attribute_Reference (Loc, | |
e4494292 | 11904 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
11905 | Attribute_Name => Name_Range))), |
11906 | ||
11907 | Statements => New_List (Loop_Body)); | |
11908 | ||
11909 | -- if X'length = 0 then | |
11910 | -- return false; | |
11911 | -- elsif Y'length = 0 then | |
11912 | -- return true; | |
11913 | -- else | |
11914 | -- for ... loop ... end loop; | |
11915 | -- return X'length > Y'length; | |
11916 | -- end if; | |
11917 | ||
11918 | Length1 := | |
11919 | Make_Attribute_Reference (Loc, | |
e4494292 | 11920 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
11921 | Attribute_Name => Name_Length); |
11922 | ||
11923 | Length2 := | |
11924 | Make_Attribute_Reference (Loc, | |
e4494292 | 11925 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
11926 | Attribute_Name => Name_Length); |
11927 | ||
11928 | Final_Expr := | |
11929 | Make_Op_Gt (Loc, | |
11930 | Left_Opnd => Length1, | |
11931 | Right_Opnd => Length2); | |
11932 | ||
11933 | If_Stat := | |
11934 | Make_Implicit_If_Statement (Nod, | |
11935 | Condition => | |
11936 | Make_Op_Eq (Loc, | |
11937 | Left_Opnd => | |
11938 | Make_Attribute_Reference (Loc, | |
e4494292 | 11939 | Prefix => New_Occurrence_Of (X, Loc), |
70482933 RK |
11940 | Attribute_Name => Name_Length), |
11941 | Right_Opnd => | |
11942 | Make_Integer_Literal (Loc, 0)), | |
11943 | ||
11944 | Then_Statements => | |
11945 | New_List ( | |
d766cee3 | 11946 | Make_Simple_Return_Statement (Loc, |
e4494292 | 11947 | Expression => New_Occurrence_Of (Standard_False, Loc))), |
70482933 RK |
11948 | |
11949 | Elsif_Parts => New_List ( | |
11950 | Make_Elsif_Part (Loc, | |
11951 | Condition => | |
11952 | Make_Op_Eq (Loc, | |
11953 | Left_Opnd => | |
11954 | Make_Attribute_Reference (Loc, | |
e4494292 | 11955 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
11956 | Attribute_Name => Name_Length), |
11957 | Right_Opnd => | |
11958 | Make_Integer_Literal (Loc, 0)), | |
11959 | ||
11960 | Then_Statements => | |
11961 | New_List ( | |
d766cee3 | 11962 | Make_Simple_Return_Statement (Loc, |
e4494292 | 11963 | Expression => New_Occurrence_Of (Standard_True, Loc))))), |
70482933 RK |
11964 | |
11965 | Else_Statements => New_List ( | |
11966 | Loop_Statement, | |
d766cee3 | 11967 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
11968 | Expression => Final_Expr))); |
11969 | ||
11970 | -- (X : a; Y: a) | |
11971 | ||
11972 | Formals := New_List ( | |
11973 | Make_Parameter_Specification (Loc, | |
11974 | Defining_Identifier => X, | |
e4494292 | 11975 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
11976 | |
11977 | Make_Parameter_Specification (Loc, | |
11978 | Defining_Identifier => Y, | |
e4494292 | 11979 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 RK |
11980 | |
11981 | -- function Gnnn (...) return boolean is | |
11982 | -- J : index := Y'first; | |
11983 | -- begin | |
11984 | -- if ... end if; | |
11985 | -- end Gnnn; | |
11986 | ||
191fcb3a | 11987 | Func_Name := Make_Temporary (Loc, 'G'); |
70482933 RK |
11988 | |
11989 | Func_Body := | |
11990 | Make_Subprogram_Body (Loc, | |
11991 | Specification => | |
11992 | Make_Function_Specification (Loc, | |
11993 | Defining_Unit_Name => Func_Name, | |
11994 | Parameter_Specifications => Formals, | |
e4494292 | 11995 | Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)), |
70482933 RK |
11996 | |
11997 | Declarations => New_List ( | |
11998 | Make_Object_Declaration (Loc, | |
11999 | Defining_Identifier => J, | |
e4494292 | 12000 | Object_Definition => New_Occurrence_Of (Index, Loc), |
70482933 RK |
12001 | Expression => |
12002 | Make_Attribute_Reference (Loc, | |
e4494292 | 12003 | Prefix => New_Occurrence_Of (Y, Loc), |
70482933 RK |
12004 | Attribute_Name => Name_First))), |
12005 | ||
12006 | Handled_Statement_Sequence => | |
12007 | Make_Handled_Sequence_Of_Statements (Loc, | |
12008 | Statements => New_List (If_Stat))); | |
12009 | ||
12010 | return Func_Body; | |
70482933 RK |
12011 | end Make_Array_Comparison_Op; |
12012 | ||
12013 | --------------------------- | |
12014 | -- Make_Boolean_Array_Op -- | |
12015 | --------------------------- | |
12016 | ||
685094bf RD |
12017 | -- For logical operations on boolean arrays, expand in line the following, |
12018 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
12019 | |
12020 | -- function Annn (A : typ; B: typ) return typ is | |
12021 | -- C : typ; | |
12022 | -- begin | |
12023 | -- for J in A'range loop | |
12024 | -- C (J) := A (J) op B (J); | |
12025 | -- end loop; | |
12026 | -- return C; | |
12027 | -- end Annn; | |
12028 | ||
12029 | -- Here typ is the boolean array type | |
12030 | ||
12031 | function Make_Boolean_Array_Op | |
2e071734 AC |
12032 | (Typ : Entity_Id; |
12033 | N : Node_Id) return Node_Id | |
70482933 RK |
12034 | is |
12035 | Loc : constant Source_Ptr := Sloc (N); | |
12036 | ||
12037 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
12038 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
12039 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
12040 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
12041 | ||
12042 | A_J : Node_Id; | |
12043 | B_J : Node_Id; | |
12044 | C_J : Node_Id; | |
12045 | Op : Node_Id; | |
12046 | ||
12047 | Formals : List_Id; | |
12048 | Func_Name : Entity_Id; | |
12049 | Func_Body : Node_Id; | |
12050 | Loop_Statement : Node_Id; | |
12051 | ||
12052 | begin | |
12053 | A_J := | |
12054 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12055 | Prefix => New_Occurrence_Of (A, Loc), |
12056 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12057 | |
12058 | B_J := | |
12059 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12060 | Prefix => New_Occurrence_Of (B, Loc), |
12061 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12062 | |
12063 | C_J := | |
12064 | Make_Indexed_Component (Loc, | |
e4494292 RD |
12065 | Prefix => New_Occurrence_Of (C, Loc), |
12066 | Expressions => New_List (New_Occurrence_Of (J, Loc))); | |
70482933 RK |
12067 | |
12068 | if Nkind (N) = N_Op_And then | |
12069 | Op := | |
12070 | Make_Op_And (Loc, | |
12071 | Left_Opnd => A_J, | |
12072 | Right_Opnd => B_J); | |
12073 | ||
12074 | elsif Nkind (N) = N_Op_Or then | |
12075 | Op := | |
12076 | Make_Op_Or (Loc, | |
12077 | Left_Opnd => A_J, | |
12078 | Right_Opnd => B_J); | |
12079 | ||
12080 | else | |
12081 | Op := | |
12082 | Make_Op_Xor (Loc, | |
12083 | Left_Opnd => A_J, | |
12084 | Right_Opnd => B_J); | |
12085 | end if; | |
12086 | ||
12087 | Loop_Statement := | |
12088 | Make_Implicit_Loop_Statement (N, | |
12089 | Identifier => Empty, | |
12090 | ||
12091 | Iteration_Scheme => | |
12092 | Make_Iteration_Scheme (Loc, | |
12093 | Loop_Parameter_Specification => | |
12094 | Make_Loop_Parameter_Specification (Loc, | |
12095 | Defining_Identifier => J, | |
12096 | Discrete_Subtype_Definition => | |
12097 | Make_Attribute_Reference (Loc, | |
e4494292 | 12098 | Prefix => New_Occurrence_Of (A, Loc), |
70482933 RK |
12099 | Attribute_Name => Name_Range))), |
12100 | ||
12101 | Statements => New_List ( | |
12102 | Make_Assignment_Statement (Loc, | |
12103 | Name => C_J, | |
12104 | Expression => Op))); | |
12105 | ||
12106 | Formals := New_List ( | |
12107 | Make_Parameter_Specification (Loc, | |
12108 | Defining_Identifier => A, | |
e4494292 | 12109 | Parameter_Type => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
12110 | |
12111 | Make_Parameter_Specification (Loc, | |
12112 | Defining_Identifier => B, | |
e4494292 | 12113 | Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
70482933 | 12114 | |
191fcb3a | 12115 | Func_Name := Make_Temporary (Loc, 'A'); |
70482933 RK |
12116 | Set_Is_Inlined (Func_Name); |
12117 | ||
12118 | Func_Body := | |
12119 | Make_Subprogram_Body (Loc, | |
12120 | Specification => | |
12121 | Make_Function_Specification (Loc, | |
12122 | Defining_Unit_Name => Func_Name, | |
12123 | Parameter_Specifications => Formals, | |
e4494292 | 12124 | Result_Definition => New_Occurrence_Of (Typ, Loc)), |
70482933 RK |
12125 | |
12126 | Declarations => New_List ( | |
12127 | Make_Object_Declaration (Loc, | |
12128 | Defining_Identifier => C, | |
e4494292 | 12129 | Object_Definition => New_Occurrence_Of (Typ, Loc))), |
70482933 RK |
12130 | |
12131 | Handled_Statement_Sequence => | |
12132 | Make_Handled_Sequence_Of_Statements (Loc, | |
12133 | Statements => New_List ( | |
12134 | Loop_Statement, | |
d766cee3 | 12135 | Make_Simple_Return_Statement (Loc, |
e4494292 | 12136 | Expression => New_Occurrence_Of (C, Loc))))); |
70482933 RK |
12137 | |
12138 | return Func_Body; | |
12139 | end Make_Boolean_Array_Op; | |
12140 | ||
b6b5cca8 AC |
12141 | ----------------------------------------- |
12142 | -- Minimized_Eliminated_Overflow_Check -- | |
12143 | ----------------------------------------- | |
12144 | ||
12145 | function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is | |
12146 | begin | |
12147 | return | |
12148 | Is_Signed_Integer_Type (Etype (N)) | |
a7f1b24f | 12149 | and then Overflow_Check_Mode in Minimized_Or_Eliminated; |
b6b5cca8 AC |
12150 | end Minimized_Eliminated_Overflow_Check; |
12151 | ||
0580d807 AC |
12152 | -------------------------------- |
12153 | -- Optimize_Length_Comparison -- | |
12154 | -------------------------------- | |
12155 | ||
12156 | procedure Optimize_Length_Comparison (N : Node_Id) is | |
12157 | Loc : constant Source_Ptr := Sloc (N); | |
12158 | Typ : constant Entity_Id := Etype (N); | |
12159 | Result : Node_Id; | |
12160 | ||
12161 | Left : Node_Id; | |
12162 | Right : Node_Id; | |
12163 | -- First and Last attribute reference nodes, which end up as left and | |
12164 | -- right operands of the optimized result. | |
12165 | ||
12166 | Is_Zero : Boolean; | |
12167 | -- True for comparison operand of zero | |
12168 | ||
12169 | Comp : Node_Id; | |
12170 | -- Comparison operand, set only if Is_Zero is false | |
12171 | ||
12172 | Ent : Entity_Id; | |
12173 | -- Entity whose length is being compared | |
12174 | ||
12175 | Index : Node_Id; | |
12176 | -- Integer_Literal node for length attribute expression, or Empty | |
12177 | -- if there is no such expression present. | |
12178 | ||
12179 | Ityp : Entity_Id; | |
12180 | -- Type of array index to which 'Length is applied | |
12181 | ||
12182 | Op : Node_Kind := Nkind (N); | |
12183 | -- Kind of comparison operator, gets flipped if operands backwards | |
12184 | ||
12185 | function Is_Optimizable (N : Node_Id) return Boolean; | |
abcd9db2 AC |
12186 | -- Tests N to see if it is an optimizable comparison value (defined as |
12187 | -- constant zero or one, or something else where the value is known to | |
12188 | -- be positive and in the range of 32-bits, and where the corresponding | |
12189 | -- Length value is also known to be 32-bits. If result is true, sets | |
12190 | -- Is_Zero, Ityp, and Comp accordingly. | |
0580d807 AC |
12191 | |
12192 | function Is_Entity_Length (N : Node_Id) return Boolean; | |
12193 | -- Tests if N is a length attribute applied to a simple entity. If so, | |
12194 | -- returns True, and sets Ent to the entity, and Index to the integer | |
12195 | -- literal provided as an attribute expression, or to Empty if none. | |
12196 | -- Also returns True if the expression is a generated type conversion | |
12197 | -- whose expression is of the desired form. This latter case arises | |
12198 | -- when Apply_Universal_Integer_Attribute_Check installs a conversion | |
12199 | -- to check for being in range, which is not needed in this context. | |
12200 | -- Returns False if neither condition holds. | |
12201 | ||
12202 | function Prepare_64 (N : Node_Id) return Node_Id; | |
12203 | -- Given a discrete expression, returns a Long_Long_Integer typed | |
12204 | -- expression representing the underlying value of the expression. | |
12205 | -- This is done with an unchecked conversion to the result type. We | |
12206 | -- use unchecked conversion to handle the enumeration type case. | |
12207 | ||
12208 | ---------------------- | |
12209 | -- Is_Entity_Length -- | |
12210 | ---------------------- | |
12211 | ||
12212 | function Is_Entity_Length (N : Node_Id) return Boolean is | |
12213 | begin | |
12214 | if Nkind (N) = N_Attribute_Reference | |
12215 | and then Attribute_Name (N) = Name_Length | |
12216 | and then Is_Entity_Name (Prefix (N)) | |
12217 | then | |
12218 | Ent := Entity (Prefix (N)); | |
12219 | ||
12220 | if Present (Expressions (N)) then | |
12221 | Index := First (Expressions (N)); | |
12222 | else | |
12223 | Index := Empty; | |
12224 | end if; | |
12225 | ||
12226 | return True; | |
12227 | ||
12228 | elsif Nkind (N) = N_Type_Conversion | |
12229 | and then not Comes_From_Source (N) | |
12230 | then | |
12231 | return Is_Entity_Length (Expression (N)); | |
12232 | ||
12233 | else | |
12234 | return False; | |
12235 | end if; | |
12236 | end Is_Entity_Length; | |
12237 | ||
12238 | -------------------- | |
12239 | -- Is_Optimizable -- | |
12240 | -------------------- | |
12241 | ||
12242 | function Is_Optimizable (N : Node_Id) return Boolean is | |
12243 | Val : Uint; | |
12244 | OK : Boolean; | |
12245 | Lo : Uint; | |
12246 | Hi : Uint; | |
12247 | Indx : Node_Id; | |
12248 | ||
12249 | begin | |
12250 | if Compile_Time_Known_Value (N) then | |
12251 | Val := Expr_Value (N); | |
12252 | ||
12253 | if Val = Uint_0 then | |
12254 | Is_Zero := True; | |
12255 | Comp := Empty; | |
12256 | return True; | |
12257 | ||
12258 | elsif Val = Uint_1 then | |
12259 | Is_Zero := False; | |
12260 | Comp := Empty; | |
12261 | return True; | |
12262 | end if; | |
12263 | end if; | |
12264 | ||
12265 | -- Here we have to make sure of being within 32-bits | |
12266 | ||
12267 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); | |
12268 | ||
12269 | if not OK | |
abcd9db2 | 12270 | or else Lo < Uint_1 |
0580d807 AC |
12271 | or else Hi > UI_From_Int (Int'Last) |
12272 | then | |
12273 | return False; | |
12274 | end if; | |
12275 | ||
abcd9db2 AC |
12276 | -- Comparison value was within range, so now we must check the index |
12277 | -- value to make sure it is also within 32-bits. | |
0580d807 AC |
12278 | |
12279 | Indx := First_Index (Etype (Ent)); | |
12280 | ||
12281 | if Present (Index) then | |
12282 | for J in 2 .. UI_To_Int (Intval (Index)) loop | |
12283 | Next_Index (Indx); | |
12284 | end loop; | |
12285 | end if; | |
12286 | ||
12287 | Ityp := Etype (Indx); | |
12288 | ||
12289 | if Esize (Ityp) > 32 then | |
12290 | return False; | |
12291 | end if; | |
12292 | ||
12293 | Is_Zero := False; | |
12294 | Comp := N; | |
12295 | return True; | |
12296 | end Is_Optimizable; | |
12297 | ||
12298 | ---------------- | |
12299 | -- Prepare_64 -- | |
12300 | ---------------- | |
12301 | ||
12302 | function Prepare_64 (N : Node_Id) return Node_Id is | |
12303 | begin | |
12304 | return Unchecked_Convert_To (Standard_Long_Long_Integer, N); | |
12305 | end Prepare_64; | |
12306 | ||
12307 | -- Start of processing for Optimize_Length_Comparison | |
12308 | ||
12309 | begin | |
12310 | -- Nothing to do if not a comparison | |
12311 | ||
12312 | if Op not in N_Op_Compare then | |
12313 | return; | |
12314 | end if; | |
12315 | ||
12316 | -- Nothing to do if special -gnatd.P debug flag set | |
12317 | ||
12318 | if Debug_Flag_Dot_PP then | |
12319 | return; | |
12320 | end if; | |
12321 | ||
12322 | -- Ent'Length op 0/1 | |
12323 | ||
12324 | if Is_Entity_Length (Left_Opnd (N)) | |
12325 | and then Is_Optimizable (Right_Opnd (N)) | |
12326 | then | |
12327 | null; | |
12328 | ||
12329 | -- 0/1 op Ent'Length | |
12330 | ||
12331 | elsif Is_Entity_Length (Right_Opnd (N)) | |
12332 | and then Is_Optimizable (Left_Opnd (N)) | |
12333 | then | |
12334 | -- Flip comparison to opposite sense | |
12335 | ||
12336 | case Op is | |
12337 | when N_Op_Lt => Op := N_Op_Gt; | |
12338 | when N_Op_Le => Op := N_Op_Ge; | |
12339 | when N_Op_Gt => Op := N_Op_Lt; | |
12340 | when N_Op_Ge => Op := N_Op_Le; | |
12341 | when others => null; | |
12342 | end case; | |
12343 | ||
12344 | -- Else optimization not possible | |
12345 | ||
12346 | else | |
12347 | return; | |
12348 | end if; | |
12349 | ||
12350 | -- Fall through if we will do the optimization | |
12351 | ||
12352 | -- Cases to handle: | |
12353 | ||
12354 | -- X'Length = 0 => X'First > X'Last | |
12355 | -- X'Length = 1 => X'First = X'Last | |
12356 | -- X'Length = n => X'First + (n - 1) = X'Last | |
12357 | ||
12358 | -- X'Length /= 0 => X'First <= X'Last | |
12359 | -- X'Length /= 1 => X'First /= X'Last | |
12360 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
12361 | ||
12362 | -- X'Length >= 0 => always true, warn | |
12363 | -- X'Length >= 1 => X'First <= X'Last | |
12364 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
12365 | ||
12366 | -- X'Length > 0 => X'First <= X'Last | |
12367 | -- X'Length > 1 => X'First < X'Last | |
12368 | -- X'Length > n => X'First + (n - 1) < X'Last | |
12369 | ||
12370 | -- X'Length <= 0 => X'First > X'Last (warn, could be =) | |
12371 | -- X'Length <= 1 => X'First >= X'Last | |
12372 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
12373 | ||
12374 | -- X'Length < 0 => always false (warn) | |
12375 | -- X'Length < 1 => X'First > X'Last | |
12376 | -- X'Length < n => X'First + (n - 1) > X'Last | |
12377 | ||
12378 | -- Note: for the cases of n (not constant 0,1), we require that the | |
12379 | -- corresponding index type be integer or shorter (i.e. not 64-bit), | |
12380 | -- and the same for the comparison value. Then we do the comparison | |
12381 | -- using 64-bit arithmetic (actually long long integer), so that we | |
12382 | -- cannot have overflow intefering with the result. | |
12383 | ||
12384 | -- First deal with warning cases | |
12385 | ||
12386 | if Is_Zero then | |
12387 | case Op is | |
12388 | ||
12389 | -- X'Length >= 0 | |
12390 | ||
12391 | when N_Op_Ge => | |
12392 | Rewrite (N, | |
12393 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc))); | |
12394 | Analyze_And_Resolve (N, Typ); | |
12395 | Warn_On_Known_Condition (N); | |
12396 | return; | |
12397 | ||
12398 | -- X'Length < 0 | |
12399 | ||
12400 | when N_Op_Lt => | |
12401 | Rewrite (N, | |
12402 | Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc))); | |
12403 | Analyze_And_Resolve (N, Typ); | |
12404 | Warn_On_Known_Condition (N); | |
12405 | return; | |
12406 | ||
12407 | when N_Op_Le => | |
12408 | if Constant_Condition_Warnings | |
12409 | and then Comes_From_Source (Original_Node (N)) | |
12410 | then | |
324ac540 | 12411 | Error_Msg_N ("could replace by ""'=""?c?", N); |
0580d807 AC |
12412 | end if; |
12413 | ||
12414 | Op := N_Op_Eq; | |
12415 | ||
12416 | when others => | |
12417 | null; | |
12418 | end case; | |
12419 | end if; | |
12420 | ||
12421 | -- Build the First reference we will use | |
12422 | ||
12423 | Left := | |
12424 | Make_Attribute_Reference (Loc, | |
12425 | Prefix => New_Occurrence_Of (Ent, Loc), | |
12426 | Attribute_Name => Name_First); | |
12427 | ||
12428 | if Present (Index) then | |
12429 | Set_Expressions (Left, New_List (New_Copy (Index))); | |
12430 | end if; | |
12431 | ||
12432 | -- If general value case, then do the addition of (n - 1), and | |
12433 | -- also add the needed conversions to type Long_Long_Integer. | |
12434 | ||
12435 | if Present (Comp) then | |
12436 | Left := | |
12437 | Make_Op_Add (Loc, | |
12438 | Left_Opnd => Prepare_64 (Left), | |
12439 | Right_Opnd => | |
12440 | Make_Op_Subtract (Loc, | |
12441 | Left_Opnd => Prepare_64 (Comp), | |
12442 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
12443 | end if; | |
12444 | ||
12445 | -- Build the Last reference we will use | |
12446 | ||
12447 | Right := | |
12448 | Make_Attribute_Reference (Loc, | |
12449 | Prefix => New_Occurrence_Of (Ent, Loc), | |
12450 | Attribute_Name => Name_Last); | |
12451 | ||
12452 | if Present (Index) then | |
12453 | Set_Expressions (Right, New_List (New_Copy (Index))); | |
12454 | end if; | |
12455 | ||
12456 | -- If general operand, convert Last reference to Long_Long_Integer | |
12457 | ||
12458 | if Present (Comp) then | |
12459 | Right := Prepare_64 (Right); | |
12460 | end if; | |
12461 | ||
12462 | -- Check for cases to optimize | |
12463 | ||
12464 | -- X'Length = 0 => X'First > X'Last | |
12465 | -- X'Length < 1 => X'First > X'Last | |
12466 | -- X'Length < n => X'First + (n - 1) > X'Last | |
12467 | ||
12468 | if (Is_Zero and then Op = N_Op_Eq) | |
12469 | or else (not Is_Zero and then Op = N_Op_Lt) | |
12470 | then | |
12471 | Result := | |
12472 | Make_Op_Gt (Loc, | |
12473 | Left_Opnd => Left, | |
12474 | Right_Opnd => Right); | |
12475 | ||
12476 | -- X'Length = 1 => X'First = X'Last | |
12477 | -- X'Length = n => X'First + (n - 1) = X'Last | |
12478 | ||
12479 | elsif not Is_Zero and then Op = N_Op_Eq then | |
12480 | Result := | |
12481 | Make_Op_Eq (Loc, | |
12482 | Left_Opnd => Left, | |
12483 | Right_Opnd => Right); | |
12484 | ||
12485 | -- X'Length /= 0 => X'First <= X'Last | |
12486 | -- X'Length > 0 => X'First <= X'Last | |
12487 | ||
12488 | elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then | |
12489 | Result := | |
12490 | Make_Op_Le (Loc, | |
12491 | Left_Opnd => Left, | |
12492 | Right_Opnd => Right); | |
12493 | ||
12494 | -- X'Length /= 1 => X'First /= X'Last | |
12495 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
12496 | ||
12497 | elsif not Is_Zero and then Op = N_Op_Ne then | |
12498 | Result := | |
12499 | Make_Op_Ne (Loc, | |
12500 | Left_Opnd => Left, | |
12501 | Right_Opnd => Right); | |
12502 | ||
12503 | -- X'Length >= 1 => X'First <= X'Last | |
12504 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
12505 | ||
12506 | elsif not Is_Zero and then Op = N_Op_Ge then | |
12507 | Result := | |
12508 | Make_Op_Le (Loc, | |
12509 | Left_Opnd => Left, | |
12510 | Right_Opnd => Right); | |
12511 | ||
12512 | -- X'Length > 1 => X'First < X'Last | |
12513 | -- X'Length > n => X'First + (n = 1) < X'Last | |
12514 | ||
12515 | elsif not Is_Zero and then Op = N_Op_Gt then | |
12516 | Result := | |
12517 | Make_Op_Lt (Loc, | |
12518 | Left_Opnd => Left, | |
12519 | Right_Opnd => Right); | |
12520 | ||
12521 | -- X'Length <= 1 => X'First >= X'Last | |
12522 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
12523 | ||
12524 | elsif not Is_Zero and then Op = N_Op_Le then | |
12525 | Result := | |
12526 | Make_Op_Ge (Loc, | |
12527 | Left_Opnd => Left, | |
12528 | Right_Opnd => Right); | |
12529 | ||
12530 | -- Should not happen at this stage | |
12531 | ||
12532 | else | |
12533 | raise Program_Error; | |
12534 | end if; | |
12535 | ||
12536 | -- Rewrite and finish up | |
12537 | ||
12538 | Rewrite (N, Result); | |
12539 | Analyze_And_Resolve (N, Typ); | |
12540 | return; | |
12541 | end Optimize_Length_Comparison; | |
12542 | ||
b2c28399 AC |
12543 | ------------------------------ |
12544 | -- Process_Transient_Object -- | |
12545 | ------------------------------ | |
12546 | ||
12547 | procedure Process_Transient_Object | |
12548 | (Decl : Node_Id; | |
12549 | Rel_Node : Node_Id) | |
12550 | is | |
064f4527 TQ |
12551 | Hook_Context : Node_Id; |
12552 | -- Node on which to insert the hook pointer (as an action) | |
b2c28399 | 12553 | |
064f4527 TQ |
12554 | Finalization_Context : Node_Id; |
12555 | -- Node after which to insert finalization actions | |
12556 | ||
12557 | Finalize_Always : Boolean; | |
12558 | -- If False, call to finalizer includes a test of whether the | |
12559 | -- hook pointer is null. | |
b2c28399 | 12560 | |
064f4527 | 12561 | procedure Find_Enclosing_Contexts (N : Node_Id); |
0083dd66 | 12562 | -- Find the logical context where N appears, and initialize |
064f4527 TQ |
12563 | -- Hook_Context and Finalization_Context accordingly. Also |
12564 | -- sets Finalize_Always. | |
12565 | ||
12566 | ----------------------------- | |
12567 | -- Find_Enclosing_Contexts -- | |
12568 | ----------------------------- | |
12569 | ||
12570 | procedure Find_Enclosing_Contexts (N : Node_Id) is | |
b2c28399 AC |
12571 | Par : Node_Id; |
12572 | Top : Node_Id; | |
12573 | ||
a7d08a38 AC |
12574 | Wrapped_Node : Node_Id; |
12575 | -- Note: if we are in a transient scope, we want to reuse it as | |
12576 | -- the context for actions insertion, if possible. But if N is itself | |
12577 | -- part of the stored actions for the current transient scope, | |
12578 | -- then we need to insert at the appropriate (inner) location in | |
12579 | -- the not as an action on Node_To_Be_Wrapped. | |
0247964d | 12580 | |
a7d08a38 | 12581 | In_Cond_Expr : constant Boolean := Within_Case_Or_If_Expression (N); |
0247964d | 12582 | |
a7d08a38 | 12583 | begin |
b2c28399 AC |
12584 | -- When the node is inside a case/if expression, the lifetime of any |
12585 | -- temporary controlled object is extended. Find a suitable insertion | |
12586 | -- node by locating the topmost case or if expressions. | |
12587 | ||
a7d08a38 | 12588 | if In_Cond_Expr then |
b2c28399 AC |
12589 | Par := N; |
12590 | Top := N; | |
12591 | while Present (Par) loop | |
12592 | if Nkind_In (Original_Node (Par), N_Case_Expression, | |
12593 | N_If_Expression) | |
12594 | then | |
12595 | Top := Par; | |
12596 | ||
12597 | -- Prevent the search from going too far | |
12598 | ||
12599 | elsif Is_Body_Or_Package_Declaration (Par) then | |
12600 | exit; | |
12601 | end if; | |
12602 | ||
12603 | Par := Parent (Par); | |
12604 | end loop; | |
12605 | ||
12606 | -- The topmost case or if expression is now recovered, but it may | |
12607 | -- still not be the correct place to add generated code. Climb to | |
c2e54001 AC |
12608 | -- find a parent that is part of a declarative or statement list, |
12609 | -- and is not a list of actuals in a call. | |
b2c28399 AC |
12610 | |
12611 | Par := Top; | |
12612 | while Present (Par) loop | |
12613 | if Is_List_Member (Par) | |
12614 | and then not Nkind_In (Par, N_Component_Association, | |
12615 | N_Discriminant_Association, | |
12616 | N_Parameter_Association, | |
12617 | N_Pragma_Argument_Association) | |
c2e54001 AC |
12618 | and then not Nkind_In |
12619 | (Parent (Par), N_Function_Call, | |
12620 | N_Procedure_Call_Statement, | |
12621 | N_Entry_Call_Statement) | |
12622 | ||
b2c28399 | 12623 | then |
064f4527 TQ |
12624 | Hook_Context := Par; |
12625 | goto Hook_Context_Found; | |
b2c28399 AC |
12626 | |
12627 | -- Prevent the search from going too far | |
12628 | ||
12629 | elsif Is_Body_Or_Package_Declaration (Par) then | |
12630 | exit; | |
12631 | end if; | |
12632 | ||
12633 | Par := Parent (Par); | |
12634 | end loop; | |
12635 | ||
064f4527 TQ |
12636 | Hook_Context := Par; |
12637 | goto Hook_Context_Found; | |
b2c28399 AC |
12638 | |
12639 | else | |
b2c28399 AC |
12640 | Par := N; |
12641 | while Present (Par) loop | |
12642 | ||
12643 | -- Keep climbing past various operators | |
12644 | ||
12645 | if Nkind (Parent (Par)) in N_Op | |
12646 | or else Nkind_In (Parent (Par), N_And_Then, N_Or_Else) | |
12647 | then | |
12648 | Par := Parent (Par); | |
12649 | else | |
12650 | exit; | |
12651 | end if; | |
12652 | end loop; | |
12653 | ||
12654 | Top := Par; | |
12655 | ||
12656 | -- The node may be located in a pragma in which case return the | |
12657 | -- pragma itself: | |
12658 | ||
12659 | -- pragma Precondition (... and then Ctrl_Func_Call ...); | |
12660 | ||
12661 | -- Similar case occurs when the node is related to an object | |
12662 | -- declaration or assignment: | |
12663 | ||
12664 | -- Obj [: Some_Typ] := ... and then Ctrl_Func_Call ...; | |
12665 | ||
12666 | -- Another case to consider is when the node is part of a return | |
12667 | -- statement: | |
12668 | ||
12669 | -- return ... and then Ctrl_Func_Call ...; | |
12670 | ||
12671 | -- Another case is when the node acts as a formal in a procedure | |
12672 | -- call statement: | |
12673 | ||
12674 | -- Proc (... and then Ctrl_Func_Call ...); | |
12675 | ||
a7d08a38 AC |
12676 | if Scope_Is_Transient then |
12677 | Wrapped_Node := Node_To_Be_Wrapped; | |
12678 | else | |
12679 | Wrapped_Node := Empty; | |
12680 | end if; | |
12681 | ||
b2c28399 | 12682 | while Present (Par) loop |
a7d08a38 | 12683 | if Par = Wrapped_Node |
1f0b1e48 RD |
12684 | or else Nkind_In (Par, N_Assignment_Statement, |
12685 | N_Object_Declaration, | |
12686 | N_Pragma, | |
12687 | N_Procedure_Call_Statement, | |
12688 | N_Simple_Return_Statement) | |
b2c28399 | 12689 | then |
064f4527 TQ |
12690 | Hook_Context := Par; |
12691 | goto Hook_Context_Found; | |
b2c28399 AC |
12692 | |
12693 | -- Prevent the search from going too far | |
12694 | ||
12695 | elsif Is_Body_Or_Package_Declaration (Par) then | |
12696 | exit; | |
12697 | end if; | |
12698 | ||
12699 | Par := Parent (Par); | |
12700 | end loop; | |
12701 | ||
12702 | -- Return the topmost short circuit operator | |
12703 | ||
064f4527 | 12704 | Hook_Context := Top; |
b2c28399 | 12705 | end if; |
064f4527 TQ |
12706 | |
12707 | <<Hook_Context_Found>> | |
12708 | ||
12709 | -- Special case for Boolean EWAs: capture expression in a temporary, | |
12710 | -- whose declaration will serve as the context around which to insert | |
12711 | -- finalization code. The finalization thus remains local to the | |
12712 | -- specific condition being evaluated. | |
12713 | ||
12714 | if Is_Boolean_Type (Etype (N)) then | |
12715 | ||
12716 | -- In this case, the finalization context is chosen so that | |
12717 | -- we know at finalization point that the hook pointer is | |
12718 | -- never null, so no need for a test, we can call the finalizer | |
a7d08a38 AC |
12719 | -- unconditionally, except in the case where the object is |
12720 | -- created in a specific branch of a conditional expression. | |
064f4527 | 12721 | |
a7d08a38 AC |
12722 | Finalize_Always := |
12723 | not (In_Cond_Expr | |
1f0b1e48 RD |
12724 | or else |
12725 | Nkind_In (Original_Node (N), N_Case_Expression, | |
12726 | N_If_Expression)); | |
064f4527 TQ |
12727 | |
12728 | declare | |
12729 | Loc : constant Source_Ptr := Sloc (N); | |
12730 | Temp : constant Entity_Id := Make_Temporary (Loc, 'E', N); | |
1f0b1e48 | 12731 | |
064f4527 TQ |
12732 | begin |
12733 | Append_To (Actions (N), | |
12734 | Make_Object_Declaration (Loc, | |
12735 | Defining_Identifier => Temp, | |
12736 | Constant_Present => True, | |
12737 | Object_Definition => | |
12738 | New_Occurrence_Of (Etype (N), Loc), | |
12739 | Expression => Expression (N))); | |
12740 | Finalization_Context := Last (Actions (N)); | |
12741 | ||
12742 | Analyze (Last (Actions (N))); | |
12743 | ||
12744 | Set_Expression (N, New_Occurrence_Of (Temp, Loc)); | |
12745 | Analyze (Expression (N)); | |
12746 | end; | |
12747 | ||
12748 | else | |
12749 | Finalize_Always := False; | |
12750 | Finalization_Context := Hook_Context; | |
12751 | end if; | |
12752 | end Find_Enclosing_Contexts; | |
b2c28399 AC |
12753 | |
12754 | -- Local variables | |
12755 | ||
b2c28399 AC |
12756 | Loc : constant Source_Ptr := Sloc (Decl); |
12757 | Obj_Id : constant Entity_Id := Defining_Identifier (Decl); | |
12758 | Obj_Typ : constant Node_Id := Etype (Obj_Id); | |
12759 | Desig_Typ : Entity_Id; | |
12760 | Expr : Node_Id; | |
064f4527 | 12761 | Fin_Stmts : List_Id; |
b2c28399 AC |
12762 | Ptr_Id : Entity_Id; |
12763 | Temp_Id : Entity_Id; | |
97779c34 | 12764 | Temp_Ins : Node_Id; |
b2c28399 AC |
12765 | |
12766 | -- Start of processing for Process_Transient_Object | |
12767 | ||
12768 | begin | |
064f4527 TQ |
12769 | Find_Enclosing_Contexts (Rel_Node); |
12770 | ||
b2c28399 AC |
12771 | -- Step 1: Create the access type which provides a reference to the |
12772 | -- transient controlled object. | |
12773 | ||
12774 | if Is_Access_Type (Obj_Typ) then | |
12775 | Desig_Typ := Directly_Designated_Type (Obj_Typ); | |
12776 | else | |
12777 | Desig_Typ := Obj_Typ; | |
12778 | end if; | |
12779 | ||
12780 | Desig_Typ := Base_Type (Desig_Typ); | |
12781 | ||
12782 | -- Generate: | |
12783 | -- Ann : access [all] <Desig_Typ>; | |
12784 | ||
12785 | Ptr_Id := Make_Temporary (Loc, 'A'); | |
12786 | ||
064f4527 | 12787 | Insert_Action (Hook_Context, |
b2c28399 AC |
12788 | Make_Full_Type_Declaration (Loc, |
12789 | Defining_Identifier => Ptr_Id, | |
12790 | Type_Definition => | |
12791 | Make_Access_To_Object_Definition (Loc, | |
12792 | All_Present => Ekind (Obj_Typ) = E_General_Access_Type, | |
e4494292 | 12793 | Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc)))); |
b2c28399 AC |
12794 | |
12795 | -- Step 2: Create a temporary which acts as a hook to the transient | |
12796 | -- controlled object. Generate: | |
12797 | ||
12798 | -- Temp : Ptr_Id := null; | |
12799 | ||
12800 | Temp_Id := Make_Temporary (Loc, 'T'); | |
12801 | ||
064f4527 | 12802 | Insert_Action (Hook_Context, |
b2c28399 AC |
12803 | Make_Object_Declaration (Loc, |
12804 | Defining_Identifier => Temp_Id, | |
e4494292 | 12805 | Object_Definition => New_Occurrence_Of (Ptr_Id, Loc))); |
b2c28399 AC |
12806 | |
12807 | -- Mark the temporary as created for the purposes of exporting the | |
12808 | -- transient controlled object out of the expression_with_action or if | |
12809 | -- expression. This signals the machinery in Build_Finalizer to treat | |
12810 | -- this case specially. | |
12811 | ||
12812 | Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl); | |
12813 | ||
12814 | -- Step 3: Hook the transient object to the temporary | |
12815 | ||
a7d08a38 AC |
12816 | -- This must be inserted right after the object declaration, so that |
12817 | -- the assignment is executed if, and only if, the object is actually | |
12818 | -- created (whereas the declaration of the hook pointer, and the | |
12819 | -- finalization call, may be inserted at an outer level, and may | |
12820 | -- remain unused for some executions, if the actual creation of | |
12821 | -- the object is conditional). | |
12822 | ||
b2c28399 AC |
12823 | -- The use of unchecked conversion / unrestricted access is needed to |
12824 | -- avoid an accessibility violation. Note that the finalization code is | |
12825 | -- structured in such a way that the "hook" is processed only when it | |
12826 | -- points to an existing object. | |
12827 | ||
12828 | if Is_Access_Type (Obj_Typ) then | |
e4494292 RD |
12829 | Expr := |
12830 | Unchecked_Convert_To (Ptr_Id, New_Occurrence_Of (Obj_Id, Loc)); | |
b2c28399 AC |
12831 | else |
12832 | Expr := | |
12833 | Make_Attribute_Reference (Loc, | |
e4494292 | 12834 | Prefix => New_Occurrence_Of (Obj_Id, Loc), |
b2c28399 AC |
12835 | Attribute_Name => Name_Unrestricted_Access); |
12836 | end if; | |
12837 | ||
12838 | -- Generate: | |
12839 | -- Temp := Ptr_Id (Obj_Id); | |
12840 | -- <or> | |
12841 | -- Temp := Obj_Id'Unrestricted_Access; | |
12842 | ||
97779c34 AC |
12843 | -- When the transient object is initialized by an aggregate, the hook |
12844 | -- must capture the object after the last component assignment takes | |
12845 | -- place. Only then is the object fully initialized. | |
12846 | ||
12847 | if Ekind (Obj_Id) = E_Variable | |
12848 | and then Present (Last_Aggregate_Assignment (Obj_Id)) | |
12849 | then | |
12850 | Temp_Ins := Last_Aggregate_Assignment (Obj_Id); | |
12851 | ||
12852 | -- Otherwise the hook seizes the related object immediately | |
12853 | ||
12854 | else | |
12855 | Temp_Ins := Decl; | |
12856 | end if; | |
12857 | ||
12858 | Insert_After_And_Analyze (Temp_Ins, | |
a7d08a38 | 12859 | Make_Assignment_Statement (Loc, |
e4494292 | 12860 | Name => New_Occurrence_Of (Temp_Id, Loc), |
a7d08a38 | 12861 | Expression => Expr)); |
b2c28399 AC |
12862 | |
12863 | -- Step 4: Finalize the transient controlled object after the context | |
12864 | -- has been evaluated/elaborated. Generate: | |
12865 | ||
12866 | -- if Temp /= null then | |
12867 | -- [Deep_]Finalize (Temp.all); | |
12868 | -- Temp := null; | |
12869 | -- end if; | |
12870 | ||
12871 | -- When the node is part of a return statement, there is no need to | |
12872 | -- insert a finalization call, as the general finalization mechanism | |
12873 | -- (see Build_Finalizer) would take care of the transient controlled | |
12874 | -- object on subprogram exit. Note that it would also be impossible to | |
12875 | -- insert the finalization code after the return statement as this will | |
12876 | -- render it unreachable. | |
12877 | ||
064f4527 TQ |
12878 | if Nkind (Finalization_Context) /= N_Simple_Return_Statement then |
12879 | Fin_Stmts := New_List ( | |
12880 | Make_Final_Call | |
12881 | (Obj_Ref => | |
12882 | Make_Explicit_Dereference (Loc, | |
e4494292 | 12883 | Prefix => New_Occurrence_Of (Temp_Id, Loc)), |
064f4527 | 12884 | Typ => Desig_Typ), |
b2c28399 | 12885 | |
064f4527 | 12886 | Make_Assignment_Statement (Loc, |
e4494292 | 12887 | Name => New_Occurrence_Of (Temp_Id, Loc), |
064f4527 | 12888 | Expression => Make_Null (Loc))); |
b2c28399 | 12889 | |
064f4527 TQ |
12890 | if not Finalize_Always then |
12891 | Fin_Stmts := New_List ( | |
12892 | Make_Implicit_If_Statement (Decl, | |
12893 | Condition => | |
12894 | Make_Op_Ne (Loc, | |
e4494292 | 12895 | Left_Opnd => New_Occurrence_Of (Temp_Id, Loc), |
064f4527 TQ |
12896 | Right_Opnd => Make_Null (Loc)), |
12897 | Then_Statements => Fin_Stmts)); | |
12898 | end if; | |
b2c28399 | 12899 | |
064f4527 | 12900 | Insert_Actions_After (Finalization_Context, Fin_Stmts); |
b2c28399 AC |
12901 | end if; |
12902 | end Process_Transient_Object; | |
12903 | ||
70482933 RK |
12904 | ------------------------ |
12905 | -- Rewrite_Comparison -- | |
12906 | ------------------------ | |
12907 | ||
12908 | procedure Rewrite_Comparison (N : Node_Id) is | |
c800f862 RD |
12909 | Warning_Generated : Boolean := False; |
12910 | -- Set to True if first pass with Assume_Valid generates a warning in | |
12911 | -- which case we skip the second pass to avoid warning overloaded. | |
12912 | ||
12913 | Result : Node_Id; | |
12914 | -- Set to Standard_True or Standard_False | |
12915 | ||
d26dc4b5 AC |
12916 | begin |
12917 | if Nkind (N) = N_Type_Conversion then | |
12918 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 12919 | return; |
70482933 | 12920 | |
d26dc4b5 | 12921 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
12922 | return; |
12923 | end if; | |
70482933 | 12924 | |
c800f862 RD |
12925 | -- Now start looking at the comparison in detail. We potentially go |
12926 | -- through this loop twice. The first time, Assume_Valid is set False | |
12927 | -- in the call to Compile_Time_Compare. If this call results in a | |
12928 | -- clear result of always True or Always False, that's decisive and | |
12929 | -- we are done. Otherwise we repeat the processing with Assume_Valid | |
e7e4d230 | 12930 | -- set to True to generate additional warnings. We can skip that step |
c800f862 RD |
12931 | -- if Constant_Condition_Warnings is False. |
12932 | ||
12933 | for AV in False .. True loop | |
12934 | declare | |
12935 | Typ : constant Entity_Id := Etype (N); | |
12936 | Op1 : constant Node_Id := Left_Opnd (N); | |
12937 | Op2 : constant Node_Id := Right_Opnd (N); | |
70482933 | 12938 | |
c800f862 RD |
12939 | Res : constant Compare_Result := |
12940 | Compile_Time_Compare (Op1, Op2, Assume_Valid => AV); | |
12941 | -- Res indicates if compare outcome can be compile time determined | |
f02b8bb8 | 12942 | |
c800f862 RD |
12943 | True_Result : Boolean; |
12944 | False_Result : Boolean; | |
f02b8bb8 | 12945 | |
c800f862 RD |
12946 | begin |
12947 | case N_Op_Compare (Nkind (N)) is | |
d26dc4b5 AC |
12948 | when N_Op_Eq => |
12949 | True_Result := Res = EQ; | |
12950 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
12951 | ||
12952 | when N_Op_Ge => | |
12953 | True_Result := Res in Compare_GE; | |
12954 | False_Result := Res = LT; | |
12955 | ||
12956 | if Res = LE | |
12957 | and then Constant_Condition_Warnings | |
12958 | and then Comes_From_Source (Original_Node (N)) | |
12959 | and then Nkind (Original_Node (N)) = N_Op_Ge | |
12960 | and then not In_Instance | |
d26dc4b5 | 12961 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 12962 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 12963 | then |
ed2233dc | 12964 | Error_Msg_N |
324ac540 AC |
12965 | ("can never be greater than, could replace by ""'=""?c?", |
12966 | N); | |
c800f862 | 12967 | Warning_Generated := True; |
d26dc4b5 | 12968 | end if; |
70482933 | 12969 | |
d26dc4b5 AC |
12970 | when N_Op_Gt => |
12971 | True_Result := Res = GT; | |
12972 | False_Result := Res in Compare_LE; | |
12973 | ||
12974 | when N_Op_Lt => | |
12975 | True_Result := Res = LT; | |
12976 | False_Result := Res in Compare_GE; | |
12977 | ||
12978 | when N_Op_Le => | |
12979 | True_Result := Res in Compare_LE; | |
12980 | False_Result := Res = GT; | |
12981 | ||
12982 | if Res = GE | |
12983 | and then Constant_Condition_Warnings | |
12984 | and then Comes_From_Source (Original_Node (N)) | |
12985 | and then Nkind (Original_Node (N)) = N_Op_Le | |
12986 | and then not In_Instance | |
d26dc4b5 | 12987 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 12988 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 12989 | then |
ed2233dc | 12990 | Error_Msg_N |
324ac540 | 12991 | ("can never be less than, could replace by ""'=""?c?", N); |
c800f862 | 12992 | Warning_Generated := True; |
d26dc4b5 | 12993 | end if; |
70482933 | 12994 | |
d26dc4b5 AC |
12995 | when N_Op_Ne => |
12996 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
12997 | False_Result := Res = EQ; | |
c800f862 | 12998 | end case; |
d26dc4b5 | 12999 | |
c800f862 RD |
13000 | -- If this is the first iteration, then we actually convert the |
13001 | -- comparison into True or False, if the result is certain. | |
d26dc4b5 | 13002 | |
c800f862 RD |
13003 | if AV = False then |
13004 | if True_Result or False_Result then | |
21791d97 | 13005 | Result := Boolean_Literals (True_Result); |
c800f862 RD |
13006 | Rewrite (N, |
13007 | Convert_To (Typ, | |
13008 | New_Occurrence_Of (Result, Sloc (N)))); | |
13009 | Analyze_And_Resolve (N, Typ); | |
13010 | Warn_On_Known_Condition (N); | |
13011 | return; | |
13012 | end if; | |
13013 | ||
13014 | -- If this is the second iteration (AV = True), and the original | |
e7e4d230 AC |
13015 | -- node comes from source and we are not in an instance, then give |
13016 | -- a warning if we know result would be True or False. Note: we | |
13017 | -- know Constant_Condition_Warnings is set if we get here. | |
c800f862 RD |
13018 | |
13019 | elsif Comes_From_Source (Original_Node (N)) | |
13020 | and then not In_Instance | |
13021 | then | |
13022 | if True_Result then | |
ed2233dc | 13023 | Error_Msg_N |
324ac540 | 13024 | ("condition can only be False if invalid values present??", |
c800f862 RD |
13025 | N); |
13026 | elsif False_Result then | |
ed2233dc | 13027 | Error_Msg_N |
324ac540 | 13028 | ("condition can only be True if invalid values present??", |
c800f862 RD |
13029 | N); |
13030 | end if; | |
13031 | end if; | |
13032 | end; | |
13033 | ||
13034 | -- Skip second iteration if not warning on constant conditions or | |
e7e4d230 AC |
13035 | -- if the first iteration already generated a warning of some kind or |
13036 | -- if we are in any case assuming all values are valid (so that the | |
13037 | -- first iteration took care of the valid case). | |
c800f862 RD |
13038 | |
13039 | exit when not Constant_Condition_Warnings; | |
13040 | exit when Warning_Generated; | |
13041 | exit when Assume_No_Invalid_Values; | |
13042 | end loop; | |
70482933 RK |
13043 | end Rewrite_Comparison; |
13044 | ||
fbf5a39b AC |
13045 | ---------------------------- |
13046 | -- Safe_In_Place_Array_Op -- | |
13047 | ---------------------------- | |
13048 | ||
13049 | function Safe_In_Place_Array_Op | |
2e071734 AC |
13050 | (Lhs : Node_Id; |
13051 | Op1 : Node_Id; | |
13052 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
13053 | is |
13054 | Target : Entity_Id; | |
13055 | ||
13056 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
13057 | -- Operand is safe if it cannot overlap part of the target of the | |
13058 | -- operation. If the operand and the target are identical, the operand | |
13059 | -- is safe. The operand can be empty in the case of negation. | |
13060 | ||
13061 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 13062 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
13063 | |
13064 | ------------------ | |
13065 | -- Is_Unaliased -- | |
13066 | ------------------ | |
13067 | ||
13068 | function Is_Unaliased (N : Node_Id) return Boolean is | |
13069 | begin | |
13070 | return | |
13071 | Is_Entity_Name (N) | |
13072 | and then No (Address_Clause (Entity (N))) | |
13073 | and then No (Renamed_Object (Entity (N))); | |
13074 | end Is_Unaliased; | |
13075 | ||
13076 | --------------------- | |
13077 | -- Is_Safe_Operand -- | |
13078 | --------------------- | |
13079 | ||
13080 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
13081 | begin | |
13082 | if No (Op) then | |
13083 | return True; | |
13084 | ||
13085 | elsif Is_Entity_Name (Op) then | |
13086 | return Is_Unaliased (Op); | |
13087 | ||
303b4d58 | 13088 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
13089 | return Is_Unaliased (Prefix (Op)); |
13090 | ||
13091 | elsif Nkind (Op) = N_Slice then | |
13092 | return | |
13093 | Is_Unaliased (Prefix (Op)) | |
13094 | and then Entity (Prefix (Op)) /= Target; | |
13095 | ||
13096 | elsif Nkind (Op) = N_Op_Not then | |
13097 | return Is_Safe_Operand (Right_Opnd (Op)); | |
13098 | ||
13099 | else | |
13100 | return False; | |
13101 | end if; | |
13102 | end Is_Safe_Operand; | |
13103 | ||
b6b5cca8 | 13104 | -- Start of processing for Safe_In_Place_Array_Op |
fbf5a39b AC |
13105 | |
13106 | begin | |
685094bf RD |
13107 | -- Skip this processing if the component size is different from system |
13108 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b | 13109 | |
eaa826f8 | 13110 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then |
fbf5a39b AC |
13111 | return False; |
13112 | ||
26bff3d9 | 13113 | -- Cannot do in place stuff on VM_Target since cannot pass addresses |
fbf5a39b | 13114 | |
26bff3d9 | 13115 | elsif VM_Target /= No_VM then |
fbf5a39b AC |
13116 | return False; |
13117 | ||
13118 | -- Cannot do in place stuff if non-standard Boolean representation | |
13119 | ||
eaa826f8 | 13120 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then |
fbf5a39b AC |
13121 | return False; |
13122 | ||
13123 | elsif not Is_Unaliased (Lhs) then | |
13124 | return False; | |
e7e4d230 | 13125 | |
fbf5a39b AC |
13126 | else |
13127 | Target := Entity (Lhs); | |
e7e4d230 | 13128 | return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2); |
fbf5a39b AC |
13129 | end if; |
13130 | end Safe_In_Place_Array_Op; | |
13131 | ||
70482933 RK |
13132 | ----------------------- |
13133 | -- Tagged_Membership -- | |
13134 | ----------------------- | |
13135 | ||
685094bf RD |
13136 | -- There are two different cases to consider depending on whether the right |
13137 | -- operand is a class-wide type or not. If not we just compare the actual | |
13138 | -- tag of the left expr to the target type tag: | |
70482933 RK |
13139 | -- |
13140 | -- Left_Expr.Tag = Right_Type'Tag; | |
13141 | -- | |
685094bf RD |
13142 | -- If it is a class-wide type we use the RT function CW_Membership which is |
13143 | -- usually implemented by looking in the ancestor tables contained in the | |
13144 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 13145 | |
0669bebe GB |
13146 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
13147 | -- function IW_Membership which is usually implemented by looking in the | |
13148 | -- table of abstract interface types plus the ancestor table contained in | |
13149 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
13150 | ||
82878151 AC |
13151 | procedure Tagged_Membership |
13152 | (N : Node_Id; | |
13153 | SCIL_Node : out Node_Id; | |
13154 | Result : out Node_Id) | |
13155 | is | |
70482933 RK |
13156 | Left : constant Node_Id := Left_Opnd (N); |
13157 | Right : constant Node_Id := Right_Opnd (N); | |
13158 | Loc : constant Source_Ptr := Sloc (N); | |
13159 | ||
38171f43 | 13160 | Full_R_Typ : Entity_Id; |
70482933 | 13161 | Left_Type : Entity_Id; |
82878151 | 13162 | New_Node : Node_Id; |
70482933 RK |
13163 | Right_Type : Entity_Id; |
13164 | Obj_Tag : Node_Id; | |
13165 | ||
13166 | begin | |
82878151 AC |
13167 | SCIL_Node := Empty; |
13168 | ||
852dba80 AC |
13169 | -- Handle entities from the limited view |
13170 | ||
13171 | Left_Type := Available_View (Etype (Left)); | |
13172 | Right_Type := Available_View (Etype (Right)); | |
70482933 | 13173 | |
6cce2156 GD |
13174 | -- In the case where the type is an access type, the test is applied |
13175 | -- using the designated types (needed in Ada 2012 for implicit anonymous | |
13176 | -- access conversions, for AI05-0149). | |
13177 | ||
13178 | if Is_Access_Type (Right_Type) then | |
13179 | Left_Type := Designated_Type (Left_Type); | |
13180 | Right_Type := Designated_Type (Right_Type); | |
13181 | end if; | |
13182 | ||
70482933 RK |
13183 | if Is_Class_Wide_Type (Left_Type) then |
13184 | Left_Type := Root_Type (Left_Type); | |
13185 | end if; | |
13186 | ||
38171f43 AC |
13187 | if Is_Class_Wide_Type (Right_Type) then |
13188 | Full_R_Typ := Underlying_Type (Root_Type (Right_Type)); | |
13189 | else | |
13190 | Full_R_Typ := Underlying_Type (Right_Type); | |
13191 | end if; | |
13192 | ||
70482933 RK |
13193 | Obj_Tag := |
13194 | Make_Selected_Component (Loc, | |
13195 | Prefix => Relocate_Node (Left), | |
a9d8907c | 13196 | Selector_Name => |
e4494292 | 13197 | New_Occurrence_Of (First_Tag_Component (Left_Type), Loc)); |
70482933 RK |
13198 | |
13199 | if Is_Class_Wide_Type (Right_Type) then | |
758c442c | 13200 | |
0669bebe GB |
13201 | -- No need to issue a run-time check if we statically know that the |
13202 | -- result of this membership test is always true. For example, | |
13203 | -- considering the following declarations: | |
13204 | ||
13205 | -- type Iface is interface; | |
13206 | -- type T is tagged null record; | |
13207 | -- type DT is new T and Iface with null record; | |
13208 | ||
13209 | -- Obj1 : T; | |
13210 | -- Obj2 : DT; | |
13211 | ||
13212 | -- These membership tests are always true: | |
13213 | ||
13214 | -- Obj1 in T'Class | |
13215 | -- Obj2 in T'Class; | |
13216 | -- Obj2 in Iface'Class; | |
13217 | ||
13218 | -- We do not need to handle cases where the membership is illegal. | |
13219 | -- For example: | |
13220 | ||
13221 | -- Obj1 in DT'Class; -- Compile time error | |
13222 | -- Obj1 in Iface'Class; -- Compile time error | |
13223 | ||
13224 | if not Is_Class_Wide_Type (Left_Type) | |
4ac2477e JM |
13225 | and then (Is_Ancestor (Etype (Right_Type), Left_Type, |
13226 | Use_Full_View => True) | |
533369aa AC |
13227 | or else (Is_Interface (Etype (Right_Type)) |
13228 | and then Interface_Present_In_Ancestor | |
761f7dcb AC |
13229 | (Typ => Left_Type, |
13230 | Iface => Etype (Right_Type)))) | |
0669bebe | 13231 | then |
e4494292 | 13232 | Result := New_Occurrence_Of (Standard_True, Loc); |
82878151 | 13233 | return; |
0669bebe GB |
13234 | end if; |
13235 | ||
758c442c GD |
13236 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
13237 | ||
630d30e9 RD |
13238 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
13239 | ||
0669bebe | 13240 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
13241 | |
13242 | or else Is_Interface (Left_Type) | |
13243 | then | |
dfd99a80 TQ |
13244 | -- Issue error if IW_Membership operation not available in a |
13245 | -- configurable run time setting. | |
13246 | ||
13247 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
13248 | Error_Msg_CRT |
13249 | ("dynamic membership test on interface types", N); | |
82878151 AC |
13250 | Result := Empty; |
13251 | return; | |
dfd99a80 TQ |
13252 | end if; |
13253 | ||
82878151 | 13254 | Result := |
758c442c GD |
13255 | Make_Function_Call (Loc, |
13256 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
13257 | Parameter_Associations => New_List ( | |
13258 | Make_Attribute_Reference (Loc, | |
13259 | Prefix => Obj_Tag, | |
13260 | Attribute_Name => Name_Address), | |
e4494292 | 13261 | New_Occurrence_Of ( |
38171f43 | 13262 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), |
758c442c GD |
13263 | Loc))); |
13264 | ||
13265 | -- Ada 95: Normal case | |
13266 | ||
13267 | else | |
82878151 AC |
13268 | Build_CW_Membership (Loc, |
13269 | Obj_Tag_Node => Obj_Tag, | |
13270 | Typ_Tag_Node => | |
e4494292 | 13271 | New_Occurrence_Of ( |
38171f43 | 13272 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc), |
82878151 AC |
13273 | Related_Nod => N, |
13274 | New_Node => New_Node); | |
13275 | ||
13276 | -- Generate the SCIL node for this class-wide membership test. | |
13277 | -- Done here because the previous call to Build_CW_Membership | |
13278 | -- relocates Obj_Tag. | |
13279 | ||
13280 | if Generate_SCIL then | |
13281 | SCIL_Node := Make_SCIL_Membership_Test (Sloc (N)); | |
13282 | Set_SCIL_Entity (SCIL_Node, Etype (Right_Type)); | |
13283 | Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag); | |
13284 | end if; | |
13285 | ||
13286 | Result := New_Node; | |
758c442c GD |
13287 | end if; |
13288 | ||
0669bebe GB |
13289 | -- Right_Type is not a class-wide type |
13290 | ||
70482933 | 13291 | else |
0669bebe GB |
13292 | -- No need to check the tag of the object if Right_Typ is abstract |
13293 | ||
13294 | if Is_Abstract_Type (Right_Type) then | |
e4494292 | 13295 | Result := New_Occurrence_Of (Standard_False, Loc); |
0669bebe GB |
13296 | |
13297 | else | |
82878151 | 13298 | Result := |
0669bebe GB |
13299 | Make_Op_Eq (Loc, |
13300 | Left_Opnd => Obj_Tag, | |
13301 | Right_Opnd => | |
e4494292 | 13302 | New_Occurrence_Of |
38171f43 | 13303 | (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc)); |
0669bebe | 13304 | end if; |
70482933 | 13305 | end if; |
70482933 RK |
13306 | end Tagged_Membership; |
13307 | ||
13308 | ------------------------------ | |
13309 | -- Unary_Op_Validity_Checks -- | |
13310 | ------------------------------ | |
13311 | ||
13312 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
13313 | begin | |
13314 | if Validity_Checks_On and Validity_Check_Operands then | |
13315 | Ensure_Valid (Right_Opnd (N)); | |
13316 | end if; | |
13317 | end Unary_Op_Validity_Checks; | |
13318 | ||
13319 | end Exp_Ch4; |