]>
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 | -- -- | |
0580d807 | 9 | -- Copyright (C) 1992-2011, 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 | ||
70482933 RK |
143 | function Expand_Composite_Equality |
144 | (Nod : Node_Id; | |
145 | Typ : Entity_Id; | |
146 | Lhs : Node_Id; | |
147 | Rhs : Node_Id; | |
2e071734 | 148 | Bodies : List_Id) return Node_Id; |
685094bf RD |
149 | -- Local recursive function used to expand equality for nested composite |
150 | -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which | |
151 | -- to attach bodies of local functions that are created in the process. | |
152 | -- This is the responsibility of the caller to insert those bodies at the | |
153 | -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs | |
154 | -- are the left and right sides for the comparison, and Typ is the type of | |
155 | -- the arrays to compare. | |
70482933 | 156 | |
fdac1f80 AC |
157 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id); |
158 | -- Routine to expand concatenation of a sequence of two or more operands | |
159 | -- (in the list Operands) and replace node Cnode with the result of the | |
160 | -- concatenation. The operands can be of any appropriate type, and can | |
161 | -- include both arrays and singleton elements. | |
70482933 RK |
162 | |
163 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); | |
685094bf RD |
164 | -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal |
165 | -- fixed. We do not have such a type at runtime, so the purpose of this | |
166 | -- routine is to find the real type by looking up the tree. We also | |
167 | -- determine if the operation must be rounded. | |
70482933 | 168 | |
5d09245e AC |
169 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean; |
170 | -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable | |
171 | -- discriminants if it has a constrained nominal type, unless the object | |
172 | -- is a component of an enclosing Unchecked_Union object that is subject | |
173 | -- to a per-object constraint and the enclosing object lacks inferable | |
174 | -- discriminants. | |
175 | -- | |
176 | -- An expression of an Unchecked_Union type has inferable discriminants | |
177 | -- if it is either a name of an object with inferable discriminants or a | |
178 | -- qualified expression whose subtype mark denotes a constrained subtype. | |
179 | ||
70482933 | 180 | procedure Insert_Dereference_Action (N : Node_Id); |
e6f69614 AC |
181 | -- N is an expression whose type is an access. When the type of the |
182 | -- associated storage pool is derived from Checked_Pool, generate a | |
183 | -- call to the 'Dereference' primitive operation. | |
70482933 RK |
184 | |
185 | function Make_Array_Comparison_Op | |
2e071734 AC |
186 | (Typ : Entity_Id; |
187 | Nod : Node_Id) return Node_Id; | |
685094bf RD |
188 | -- Comparisons between arrays are expanded in line. This function produces |
189 | -- the body of the implementation of (a > b), where a and b are one- | |
190 | -- dimensional arrays of some discrete type. The original node is then | |
191 | -- expanded into the appropriate call to this function. Nod provides the | |
192 | -- Sloc value for the generated code. | |
70482933 RK |
193 | |
194 | function Make_Boolean_Array_Op | |
2e071734 AC |
195 | (Typ : Entity_Id; |
196 | N : Node_Id) return Node_Id; | |
685094bf RD |
197 | -- Boolean operations on boolean arrays are expanded in line. This function |
198 | -- produce the body for the node N, which is (a and b), (a or b), or (a xor | |
199 | -- b). It is used only the normal case and not the packed case. The type | |
200 | -- involved, Typ, is the Boolean array type, and the logical operations in | |
201 | -- the body are simple boolean operations. Note that Typ is always a | |
202 | -- constrained type (the caller has ensured this by using | |
203 | -- Convert_To_Actual_Subtype if necessary). | |
70482933 | 204 | |
0580d807 AC |
205 | procedure Optimize_Length_Comparison (N : Node_Id); |
206 | -- Given an expression, if it is of the form X'Length op N (or the other | |
207 | -- way round), where N is known at compile time to be 0 or 1, and X is a | |
208 | -- simple entity, and op is a comparison operator, optimizes it into a | |
209 | -- comparison of First and Last. | |
210 | ||
70482933 | 211 | procedure Rewrite_Comparison (N : Node_Id); |
20b5d666 | 212 | -- If N is the node for a comparison whose outcome can be determined at |
d26dc4b5 AC |
213 | -- compile time, then the node N can be rewritten with True or False. If |
214 | -- the outcome cannot be determined at compile time, the call has no | |
215 | -- effect. If N is a type conversion, then this processing is applied to | |
216 | -- its expression. If N is neither comparison nor a type conversion, the | |
217 | -- call has no effect. | |
70482933 | 218 | |
82878151 AC |
219 | procedure Tagged_Membership |
220 | (N : Node_Id; | |
221 | SCIL_Node : out Node_Id; | |
222 | Result : out Node_Id); | |
70482933 RK |
223 | -- Construct the expression corresponding to the tagged membership test. |
224 | -- Deals with a second operand being (or not) a class-wide type. | |
225 | ||
fbf5a39b | 226 | function Safe_In_Place_Array_Op |
2e071734 AC |
227 | (Lhs : Node_Id; |
228 | Op1 : Node_Id; | |
229 | Op2 : Node_Id) return Boolean; | |
685094bf RD |
230 | -- In the context of an assignment, where the right-hand side is a boolean |
231 | -- operation on arrays, check whether operation can be performed in place. | |
fbf5a39b | 232 | |
70482933 RK |
233 | procedure Unary_Op_Validity_Checks (N : Node_Id); |
234 | pragma Inline (Unary_Op_Validity_Checks); | |
235 | -- Performs validity checks for a unary operator | |
236 | ||
237 | ------------------------------- | |
238 | -- Binary_Op_Validity_Checks -- | |
239 | ------------------------------- | |
240 | ||
241 | procedure Binary_Op_Validity_Checks (N : Node_Id) is | |
242 | begin | |
243 | if Validity_Checks_On and Validity_Check_Operands then | |
244 | Ensure_Valid (Left_Opnd (N)); | |
245 | Ensure_Valid (Right_Opnd (N)); | |
246 | end if; | |
247 | end Binary_Op_Validity_Checks; | |
248 | ||
fbf5a39b AC |
249 | ------------------------------------ |
250 | -- Build_Boolean_Array_Proc_Call -- | |
251 | ------------------------------------ | |
252 | ||
253 | procedure Build_Boolean_Array_Proc_Call | |
254 | (N : Node_Id; | |
255 | Op1 : Node_Id; | |
256 | Op2 : Node_Id) | |
257 | is | |
258 | Loc : constant Source_Ptr := Sloc (N); | |
259 | Kind : constant Node_Kind := Nkind (Expression (N)); | |
260 | Target : constant Node_Id := | |
261 | Make_Attribute_Reference (Loc, | |
262 | Prefix => Name (N), | |
263 | Attribute_Name => Name_Address); | |
264 | ||
bed8af19 | 265 | Arg1 : Node_Id := Op1; |
fbf5a39b AC |
266 | Arg2 : Node_Id := Op2; |
267 | Call_Node : Node_Id; | |
268 | Proc_Name : Entity_Id; | |
269 | ||
270 | begin | |
271 | if Kind = N_Op_Not then | |
272 | if Nkind (Op1) in N_Binary_Op then | |
273 | ||
5e1c00fa | 274 | -- Use negated version of the binary operators |
fbf5a39b AC |
275 | |
276 | if Nkind (Op1) = N_Op_And then | |
277 | Proc_Name := RTE (RE_Vector_Nand); | |
278 | ||
279 | elsif Nkind (Op1) = N_Op_Or then | |
280 | Proc_Name := RTE (RE_Vector_Nor); | |
281 | ||
282 | else pragma Assert (Nkind (Op1) = N_Op_Xor); | |
283 | Proc_Name := RTE (RE_Vector_Xor); | |
284 | end if; | |
285 | ||
286 | Call_Node := | |
287 | Make_Procedure_Call_Statement (Loc, | |
288 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
289 | ||
290 | Parameter_Associations => New_List ( | |
291 | Target, | |
292 | Make_Attribute_Reference (Loc, | |
293 | Prefix => Left_Opnd (Op1), | |
294 | Attribute_Name => Name_Address), | |
295 | ||
296 | Make_Attribute_Reference (Loc, | |
297 | Prefix => Right_Opnd (Op1), | |
298 | Attribute_Name => Name_Address), | |
299 | ||
300 | Make_Attribute_Reference (Loc, | |
301 | Prefix => Left_Opnd (Op1), | |
302 | Attribute_Name => Name_Length))); | |
303 | ||
304 | else | |
305 | Proc_Name := RTE (RE_Vector_Not); | |
306 | ||
307 | Call_Node := | |
308 | Make_Procedure_Call_Statement (Loc, | |
309 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
310 | Parameter_Associations => New_List ( | |
311 | Target, | |
312 | ||
313 | Make_Attribute_Reference (Loc, | |
314 | Prefix => Op1, | |
315 | Attribute_Name => Name_Address), | |
316 | ||
317 | Make_Attribute_Reference (Loc, | |
318 | Prefix => Op1, | |
319 | Attribute_Name => Name_Length))); | |
320 | end if; | |
321 | ||
322 | else | |
323 | -- We use the following equivalences: | |
324 | ||
325 | -- (not X) or (not Y) = not (X and Y) = Nand (X, Y) | |
326 | -- (not X) and (not Y) = not (X or Y) = Nor (X, Y) | |
327 | -- (not X) xor (not Y) = X xor Y | |
328 | -- X xor (not Y) = not (X xor Y) = Nxor (X, Y) | |
329 | ||
330 | if Nkind (Op1) = N_Op_Not then | |
bed8af19 AC |
331 | Arg1 := Right_Opnd (Op1); |
332 | Arg2 := Right_Opnd (Op2); | |
fbf5a39b AC |
333 | if Kind = N_Op_And then |
334 | Proc_Name := RTE (RE_Vector_Nor); | |
fbf5a39b AC |
335 | elsif Kind = N_Op_Or then |
336 | Proc_Name := RTE (RE_Vector_Nand); | |
fbf5a39b AC |
337 | else |
338 | Proc_Name := RTE (RE_Vector_Xor); | |
339 | end if; | |
340 | ||
341 | else | |
342 | if Kind = N_Op_And then | |
343 | Proc_Name := RTE (RE_Vector_And); | |
fbf5a39b AC |
344 | elsif Kind = N_Op_Or then |
345 | Proc_Name := RTE (RE_Vector_Or); | |
fbf5a39b AC |
346 | elsif Nkind (Op2) = N_Op_Not then |
347 | Proc_Name := RTE (RE_Vector_Nxor); | |
348 | Arg2 := Right_Opnd (Op2); | |
fbf5a39b AC |
349 | else |
350 | Proc_Name := RTE (RE_Vector_Xor); | |
351 | end if; | |
352 | end if; | |
353 | ||
354 | Call_Node := | |
355 | Make_Procedure_Call_Statement (Loc, | |
356 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
357 | Parameter_Associations => New_List ( | |
358 | Target, | |
955871d3 AC |
359 | Make_Attribute_Reference (Loc, |
360 | Prefix => Arg1, | |
361 | Attribute_Name => Name_Address), | |
362 | Make_Attribute_Reference (Loc, | |
363 | Prefix => Arg2, | |
364 | Attribute_Name => Name_Address), | |
365 | Make_Attribute_Reference (Loc, | |
a8ef12e5 | 366 | Prefix => Arg1, |
955871d3 | 367 | Attribute_Name => Name_Length))); |
fbf5a39b AC |
368 | end if; |
369 | ||
370 | Rewrite (N, Call_Node); | |
371 | Analyze (N); | |
372 | ||
373 | exception | |
374 | when RE_Not_Available => | |
375 | return; | |
376 | end Build_Boolean_Array_Proc_Call; | |
377 | ||
11fa950b AC |
378 | ------------------------------ |
379 | -- Current_Anonymous_Master -- | |
380 | ------------------------------ | |
df3e68b1 | 381 | |
11fa950b | 382 | function Current_Anonymous_Master return Entity_Id is |
2c17ca0a AC |
383 | Decls : List_Id; |
384 | Loc : Source_Ptr; | |
385 | Subp_Body : Node_Id; | |
386 | Unit_Decl : Node_Id; | |
387 | Unit_Id : Entity_Id; | |
df3e68b1 | 388 | |
ca5af305 | 389 | begin |
11fa950b AC |
390 | Unit_Id := Cunit_Entity (Current_Sem_Unit); |
391 | ||
392 | -- Find the entity of the current unit | |
393 | ||
394 | if Ekind (Unit_Id) = E_Subprogram_Body then | |
395 | ||
396 | -- When processing subprogram bodies, the proper scope is always that | |
397 | -- of the spec. | |
398 | ||
399 | Subp_Body := Unit_Id; | |
400 | while Present (Subp_Body) | |
401 | and then Nkind (Subp_Body) /= N_Subprogram_Body | |
402 | loop | |
403 | Subp_Body := Parent (Subp_Body); | |
404 | end loop; | |
405 | ||
406 | Unit_Id := Corresponding_Spec (Subp_Body); | |
407 | end if; | |
408 | ||
409 | Loc := Sloc (Unit_Id); | |
410 | Unit_Decl := Unit (Cunit (Current_Sem_Unit)); | |
411 | ||
412 | -- Find the declarations list of the current unit | |
413 | ||
414 | if Nkind (Unit_Decl) = N_Package_Declaration then | |
415 | Unit_Decl := Specification (Unit_Decl); | |
416 | Decls := Visible_Declarations (Unit_Decl); | |
df3e68b1 | 417 | |
ca5af305 | 418 | if No (Decls) then |
11fa950b AC |
419 | Decls := New_List (Make_Null_Statement (Loc)); |
420 | Set_Visible_Declarations (Unit_Decl, Decls); | |
df3e68b1 | 421 | |
ca5af305 | 422 | elsif Is_Empty_List (Decls) then |
11fa950b | 423 | Append_To (Decls, Make_Null_Statement (Loc)); |
df3e68b1 HK |
424 | end if; |
425 | ||
ca5af305 | 426 | else |
11fa950b | 427 | Decls := Declarations (Unit_Decl); |
f553e7bc | 428 | |
ca5af305 | 429 | if No (Decls) then |
11fa950b AC |
430 | Decls := New_List (Make_Null_Statement (Loc)); |
431 | Set_Declarations (Unit_Decl, Decls); | |
df3e68b1 | 432 | |
ca5af305 | 433 | elsif Is_Empty_List (Decls) then |
11fa950b | 434 | Append_To (Decls, Make_Null_Statement (Loc)); |
ca5af305 | 435 | end if; |
df3e68b1 HK |
436 | end if; |
437 | ||
11fa950b AC |
438 | -- The current unit has an existing anonymous master, traverse its |
439 | -- declarations and locate the entity. | |
df3e68b1 | 440 | |
11fa950b | 441 | if Has_Anonymous_Master (Unit_Id) then |
2c17ca0a AC |
442 | declare |
443 | Decl : Node_Id; | |
444 | Fin_Mas_Id : Entity_Id; | |
df3e68b1 | 445 | |
2c17ca0a AC |
446 | begin |
447 | Decl := First (Decls); | |
448 | while Present (Decl) loop | |
df3e68b1 | 449 | |
2c17ca0a AC |
450 | -- Look for the first variable in the declarations whole type |
451 | -- is Finalization_Master. | |
df3e68b1 | 452 | |
2c17ca0a AC |
453 | if Nkind (Decl) = N_Object_Declaration then |
454 | Fin_Mas_Id := Defining_Identifier (Decl); | |
455 | ||
456 | if Ekind (Fin_Mas_Id) = E_Variable | |
457 | and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master) | |
458 | then | |
459 | return Fin_Mas_Id; | |
460 | end if; | |
461 | end if; | |
462 | ||
463 | Next (Decl); | |
464 | end loop; | |
465 | ||
466 | -- The master was not found even though the unit was labeled as | |
467 | -- having one. | |
df3e68b1 | 468 | |
2c17ca0a AC |
469 | raise Program_Error; |
470 | end; | |
11fa950b AC |
471 | |
472 | -- Create a new anonymous master | |
473 | ||
474 | else | |
475 | declare | |
476 | First_Decl : constant Node_Id := First (Decls); | |
477 | Action : Node_Id; | |
2c17ca0a | 478 | Fin_Mas_Id : Entity_Id; |
df3e68b1 | 479 | |
11fa950b AC |
480 | begin |
481 | -- Since the master and its associated initialization is inserted | |
482 | -- at top level, use the scope of the unit when analyzing. | |
483 | ||
484 | Push_Scope (Unit_Id); | |
485 | ||
486 | -- Create the finalization master | |
487 | ||
488 | Fin_Mas_Id := | |
489 | Make_Defining_Identifier (Loc, | |
490 | Chars => New_External_Name (Chars (Unit_Id), "AM")); | |
491 | ||
492 | -- Generate: | |
493 | -- <Fin_Mas_Id> : Finalization_Master; | |
494 | ||
495 | Action := | |
496 | Make_Object_Declaration (Loc, | |
497 | Defining_Identifier => Fin_Mas_Id, | |
498 | Object_Definition => | |
499 | New_Reference_To (RTE (RE_Finalization_Master), Loc)); | |
500 | ||
501 | Insert_Before_And_Analyze (First_Decl, Action); | |
502 | ||
503 | -- Mark the unit to prevent the generation of multiple masters | |
504 | ||
505 | Set_Has_Anonymous_Master (Unit_Id); | |
506 | ||
507 | -- Do not set the base pool and mode of operation on .NET/JVM | |
508 | -- since those targets do not support pools and all VM masters | |
509 | -- are heterogeneous by default. | |
510 | ||
511 | if VM_Target = No_VM then | |
512 | ||
513 | -- Generate: | |
514 | -- Set_Base_Pool | |
515 | -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access); | |
516 | ||
517 | Action := | |
518 | Make_Procedure_Call_Statement (Loc, | |
519 | Name => | |
520 | New_Reference_To (RTE (RE_Set_Base_Pool), Loc), | |
521 | ||
522 | Parameter_Associations => New_List ( | |
523 | New_Reference_To (Fin_Mas_Id, Loc), | |
524 | Make_Attribute_Reference (Loc, | |
525 | Prefix => | |
526 | New_Reference_To (RTE (RE_Global_Pool_Object), Loc), | |
527 | Attribute_Name => Name_Unrestricted_Access))); | |
528 | ||
529 | Insert_Before_And_Analyze (First_Decl, Action); | |
530 | ||
531 | -- Generate: | |
532 | -- Set_Is_Heterogeneous (<Fin_Mas_Id>); | |
533 | ||
534 | Action := | |
535 | Make_Procedure_Call_Statement (Loc, | |
536 | Name => | |
537 | New_Reference_To (RTE (RE_Set_Is_Heterogeneous), Loc), | |
538 | Parameter_Associations => New_List ( | |
539 | New_Reference_To (Fin_Mas_Id, Loc))); | |
540 | ||
541 | Insert_Before_And_Analyze (First_Decl, Action); | |
542 | end if; | |
543 | ||
544 | -- Restore the original state of the scope stack | |
545 | ||
546 | Pop_Scope; | |
547 | ||
548 | return Fin_Mas_Id; | |
549 | end; | |
550 | end if; | |
551 | end Current_Anonymous_Master; | |
df3e68b1 | 552 | |
26bff3d9 JM |
553 | -------------------------------- |
554 | -- Displace_Allocator_Pointer -- | |
555 | -------------------------------- | |
556 | ||
557 | procedure Displace_Allocator_Pointer (N : Node_Id) is | |
558 | Loc : constant Source_Ptr := Sloc (N); | |
559 | Orig_Node : constant Node_Id := Original_Node (N); | |
560 | Dtyp : Entity_Id; | |
561 | Etyp : Entity_Id; | |
562 | PtrT : Entity_Id; | |
563 | ||
564 | begin | |
303b4d58 AC |
565 | -- Do nothing in case of VM targets: the virtual machine will handle |
566 | -- interfaces directly. | |
567 | ||
1f110335 | 568 | if not Tagged_Type_Expansion then |
303b4d58 AC |
569 | return; |
570 | end if; | |
571 | ||
26bff3d9 JM |
572 | pragma Assert (Nkind (N) = N_Identifier |
573 | and then Nkind (Orig_Node) = N_Allocator); | |
574 | ||
575 | PtrT := Etype (Orig_Node); | |
d6a24cdb | 576 | Dtyp := Available_View (Designated_Type (PtrT)); |
26bff3d9 JM |
577 | Etyp := Etype (Expression (Orig_Node)); |
578 | ||
579 | if Is_Class_Wide_Type (Dtyp) | |
580 | and then Is_Interface (Dtyp) | |
581 | then | |
582 | -- If the type of the allocator expression is not an interface type | |
583 | -- we can generate code to reference the record component containing | |
584 | -- the pointer to the secondary dispatch table. | |
585 | ||
586 | if not Is_Interface (Etyp) then | |
587 | declare | |
588 | Saved_Typ : constant Entity_Id := Etype (Orig_Node); | |
589 | ||
590 | begin | |
591 | -- 1) Get access to the allocated object | |
592 | ||
593 | Rewrite (N, | |
5972791c | 594 | Make_Explicit_Dereference (Loc, Relocate_Node (N))); |
26bff3d9 JM |
595 | Set_Etype (N, Etyp); |
596 | Set_Analyzed (N); | |
597 | ||
598 | -- 2) Add the conversion to displace the pointer to reference | |
599 | -- the secondary dispatch table. | |
600 | ||
601 | Rewrite (N, Convert_To (Dtyp, Relocate_Node (N))); | |
602 | Analyze_And_Resolve (N, Dtyp); | |
603 | ||
604 | -- 3) The 'access to the secondary dispatch table will be used | |
605 | -- as the value returned by the allocator. | |
606 | ||
607 | Rewrite (N, | |
608 | Make_Attribute_Reference (Loc, | |
609 | Prefix => Relocate_Node (N), | |
610 | Attribute_Name => Name_Access)); | |
611 | Set_Etype (N, Saved_Typ); | |
612 | Set_Analyzed (N); | |
613 | end; | |
614 | ||
615 | -- If the type of the allocator expression is an interface type we | |
616 | -- generate a run-time call to displace "this" to reference the | |
617 | -- component containing the pointer to the secondary dispatch table | |
618 | -- or else raise Constraint_Error if the actual object does not | |
619 | -- implement the target interface. This case corresponds with the | |
620 | -- following example: | |
621 | ||
8fc789c8 | 622 | -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is |
26bff3d9 JM |
623 | -- begin |
624 | -- return new Iface_2'Class'(Obj); | |
625 | -- end Op; | |
626 | ||
627 | else | |
628 | Rewrite (N, | |
629 | Unchecked_Convert_To (PtrT, | |
630 | Make_Function_Call (Loc, | |
631 | Name => New_Reference_To (RTE (RE_Displace), Loc), | |
632 | Parameter_Associations => New_List ( | |
633 | Unchecked_Convert_To (RTE (RE_Address), | |
634 | Relocate_Node (N)), | |
635 | ||
636 | New_Occurrence_Of | |
637 | (Elists.Node | |
638 | (First_Elmt | |
639 | (Access_Disp_Table (Etype (Base_Type (Dtyp))))), | |
640 | Loc))))); | |
641 | Analyze_And_Resolve (N, PtrT); | |
642 | end if; | |
643 | end if; | |
644 | end Displace_Allocator_Pointer; | |
645 | ||
fbf5a39b AC |
646 | --------------------------------- |
647 | -- Expand_Allocator_Expression -- | |
648 | --------------------------------- | |
649 | ||
650 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
f02b8bb8 RD |
651 | Loc : constant Source_Ptr := Sloc (N); |
652 | Exp : constant Node_Id := Expression (Expression (N)); | |
f02b8bb8 RD |
653 | PtrT : constant Entity_Id := Etype (N); |
654 | DesigT : constant Entity_Id := Designated_Type (PtrT); | |
26bff3d9 JM |
655 | |
656 | procedure Apply_Accessibility_Check | |
657 | (Ref : Node_Id; | |
658 | Built_In_Place : Boolean := False); | |
659 | -- Ada 2005 (AI-344): For an allocator with a class-wide designated | |
685094bf RD |
660 | -- type, generate an accessibility check to verify that the level of the |
661 | -- type of the created object is not deeper than the level of the access | |
662 | -- type. If the type of the qualified expression is class- wide, then | |
663 | -- always generate the check (except in the case where it is known to be | |
664 | -- unnecessary, see comment below). Otherwise, only generate the check | |
665 | -- if the level of the qualified expression type is statically deeper | |
666 | -- than the access type. | |
667 | -- | |
668 | -- Although the static accessibility will generally have been performed | |
669 | -- as a legality check, it won't have been done in cases where the | |
670 | -- allocator appears in generic body, so a run-time check is needed in | |
671 | -- general. One special case is when the access type is declared in the | |
672 | -- same scope as the class-wide allocator, in which case the check can | |
673 | -- never fail, so it need not be generated. | |
674 | -- | |
675 | -- As an open issue, there seem to be cases where the static level | |
676 | -- associated with the class-wide object's underlying type is not | |
677 | -- sufficient to perform the proper accessibility check, such as for | |
678 | -- allocators in nested subprograms or accept statements initialized by | |
679 | -- class-wide formals when the actual originates outside at a deeper | |
680 | -- static level. The nested subprogram case might require passing | |
681 | -- accessibility levels along with class-wide parameters, and the task | |
682 | -- case seems to be an actual gap in the language rules that needs to | |
683 | -- be fixed by the ARG. ??? | |
26bff3d9 JM |
684 | |
685 | ------------------------------- | |
686 | -- Apply_Accessibility_Check -- | |
687 | ------------------------------- | |
688 | ||
689 | procedure Apply_Accessibility_Check | |
690 | (Ref : Node_Id; | |
691 | Built_In_Place : Boolean := False) | |
692 | is | |
f46faa08 | 693 | New_Node : Node_Id; |
26bff3d9 JM |
694 | |
695 | begin | |
0791fbe9 | 696 | if Ada_Version >= Ada_2005 |
26bff3d9 JM |
697 | and then Is_Class_Wide_Type (DesigT) |
698 | and then not Scope_Suppress (Accessibility_Check) | |
699 | and then | |
700 | (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT) | |
701 | or else | |
702 | (Is_Class_Wide_Type (Etype (Exp)) | |
703 | and then Scope (PtrT) /= Current_Scope)) | |
704 | then | |
705 | -- If the allocator was built in place Ref is already a reference | |
706 | -- to the access object initialized to the result of the allocator | |
707 | -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise | |
708 | -- it is the entity associated with the object containing the | |
709 | -- address of the allocated object. | |
710 | ||
711 | if Built_In_Place then | |
f46faa08 | 712 | New_Node := New_Copy (Ref); |
26bff3d9 | 713 | else |
f46faa08 AC |
714 | New_Node := New_Reference_To (Ref, Loc); |
715 | end if; | |
716 | ||
717 | New_Node := | |
718 | Make_Attribute_Reference (Loc, | |
719 | Prefix => New_Node, | |
720 | Attribute_Name => Name_Tag); | |
721 | ||
722 | if Tagged_Type_Expansion then | |
15d8a51d | 723 | New_Node := Build_Get_Access_Level (Loc, New_Node); |
f46faa08 AC |
724 | |
725 | elsif VM_Target /= No_VM then | |
726 | New_Node := | |
727 | Make_Function_Call (Loc, | |
728 | Name => New_Reference_To (RTE (RE_Get_Access_Level), Loc), | |
729 | Parameter_Associations => New_List (New_Node)); | |
730 | ||
731 | -- Cannot generate the runtime check | |
732 | ||
733 | else | |
734 | return; | |
26bff3d9 JM |
735 | end if; |
736 | ||
737 | Insert_Action (N, | |
df3e68b1 HK |
738 | Make_Raise_Program_Error (Loc, |
739 | Condition => | |
740 | Make_Op_Gt (Loc, | |
f46faa08 | 741 | Left_Opnd => New_Node, |
df3e68b1 | 742 | Right_Opnd => |
243cae0a | 743 | Make_Integer_Literal (Loc, Type_Access_Level (PtrT))), |
df3e68b1 | 744 | Reason => PE_Accessibility_Check_Failed)); |
26bff3d9 JM |
745 | end if; |
746 | end Apply_Accessibility_Check; | |
747 | ||
748 | -- Local variables | |
749 | ||
df3e68b1 HK |
750 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); |
751 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
752 | T : constant Entity_Id := Entity (Indic); | |
753 | Node : Node_Id; | |
754 | Tag_Assign : Node_Id; | |
755 | Temp : Entity_Id; | |
756 | Temp_Decl : Node_Id; | |
fbf5a39b | 757 | |
d26dc4b5 AC |
758 | TagT : Entity_Id := Empty; |
759 | -- Type used as source for tag assignment | |
760 | ||
761 | TagR : Node_Id := Empty; | |
762 | -- Target reference for tag assignment | |
763 | ||
26bff3d9 JM |
764 | -- Start of processing for Expand_Allocator_Expression |
765 | ||
fbf5a39b | 766 | begin |
885c4871 | 767 | -- In the case of an Ada 2012 allocator whose initial value comes from a |
63585f75 SB |
768 | -- function call, pass "the accessibility level determined by the point |
769 | -- of call" (AI05-0234) to the function. Conceptually, this belongs in | |
770 | -- Expand_Call but it couldn't be done there (because the Etype of the | |
771 | -- allocator wasn't set then) so we generate the parameter here. See | |
772 | -- the Boolean variable Defer in (a block within) Expand_Call. | |
773 | ||
774 | if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then | |
775 | declare | |
776 | Subp : Entity_Id; | |
777 | ||
778 | begin | |
779 | if Nkind (Name (Exp)) = N_Explicit_Dereference then | |
780 | Subp := Designated_Type (Etype (Prefix (Name (Exp)))); | |
781 | else | |
782 | Subp := Entity (Name (Exp)); | |
783 | end if; | |
784 | ||
57a3fca9 AC |
785 | Subp := Ultimate_Alias (Subp); |
786 | ||
63585f75 SB |
787 | if Present (Extra_Accessibility_Of_Result (Subp)) then |
788 | Add_Extra_Actual_To_Call | |
789 | (Subprogram_Call => Exp, | |
790 | Extra_Formal => Extra_Accessibility_Of_Result (Subp), | |
791 | Extra_Actual => Dynamic_Accessibility_Level (PtrT)); | |
792 | end if; | |
793 | end; | |
794 | end if; | |
795 | ||
796 | -- Would be nice to comment the branches of this very long if ??? | |
797 | ||
798 | if Is_Tagged_Type (T) or else Needs_Finalization (T) then | |
fadcf313 AC |
799 | if Is_CPP_Constructor_Call (Exp) then |
800 | ||
801 | -- Generate: | |
df3e68b1 HK |
802 | -- Pnnn : constant ptr_T := new (T); |
803 | -- Init (Pnnn.all,...); | |
fadcf313 | 804 | |
df3e68b1 | 805 | -- Allocate the object without an expression |
fadcf313 AC |
806 | |
807 | Node := Relocate_Node (N); | |
7b4db06c | 808 | Set_Expression (Node, New_Reference_To (Etype (Exp), Loc)); |
fadcf313 AC |
809 | |
810 | -- Avoid its expansion to avoid generating a call to the default | |
df3e68b1 | 811 | -- C++ constructor. |
fadcf313 AC |
812 | |
813 | Set_Analyzed (Node); | |
814 | ||
e86a3a7e | 815 | Temp := Make_Temporary (Loc, 'P', N); |
fadcf313 | 816 | |
df3e68b1 | 817 | Temp_Decl := |
fadcf313 AC |
818 | Make_Object_Declaration (Loc, |
819 | Defining_Identifier => Temp, | |
820 | Constant_Present => True, | |
821 | Object_Definition => New_Reference_To (PtrT, Loc), | |
df3e68b1 HK |
822 | Expression => Node); |
823 | Insert_Action (N, Temp_Decl); | |
fadcf313 AC |
824 | |
825 | Apply_Accessibility_Check (Temp); | |
826 | ||
ffa5876f | 827 | -- Locate the enclosing list and insert the C++ constructor call |
fadcf313 AC |
828 | |
829 | declare | |
ffa5876f | 830 | P : Node_Id; |
fadcf313 AC |
831 | |
832 | begin | |
ffa5876f | 833 | P := Parent (Node); |
fadcf313 AC |
834 | while not Is_List_Member (P) loop |
835 | P := Parent (P); | |
836 | end loop; | |
837 | ||
838 | Insert_List_After_And_Analyze (P, | |
839 | Build_Initialization_Call (Loc, | |
63585f75 | 840 | Id_Ref => |
ffa5876f AC |
841 | Make_Explicit_Dereference (Loc, |
842 | Prefix => New_Reference_To (Temp, Loc)), | |
63585f75 | 843 | Typ => Etype (Exp), |
fadcf313 AC |
844 | Constructor_Ref => Exp)); |
845 | end; | |
846 | ||
847 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
848 | Analyze_And_Resolve (N, PtrT); | |
fadcf313 AC |
849 | return; |
850 | end if; | |
851 | ||
685094bf RD |
852 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
853 | -- to a build-in-place function, then access to the allocated object | |
854 | -- must be passed to the function. Currently we limit such functions | |
855 | -- to those with constrained limited result subtypes, but eventually | |
856 | -- we plan to expand the allowed forms of functions that are treated | |
857 | -- as build-in-place. | |
20b5d666 | 858 | |
0791fbe9 | 859 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
860 | and then Is_Build_In_Place_Function_Call (Exp) |
861 | then | |
862 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
26bff3d9 JM |
863 | Apply_Accessibility_Check (N, Built_In_Place => True); |
864 | return; | |
20b5d666 JM |
865 | end if; |
866 | ||
ca5af305 AC |
867 | -- Actions inserted before: |
868 | -- Temp : constant ptr_T := new T'(Expression); | |
869 | -- Temp._tag = T'tag; -- when not class-wide | |
870 | -- [Deep_]Adjust (Temp.all); | |
fbf5a39b | 871 | |
ca5af305 AC |
872 | -- We analyze by hand the new internal allocator to avoid any |
873 | -- recursion and inappropriate call to Initialize | |
7324bf49 | 874 | |
20b5d666 JM |
875 | -- We don't want to remove side effects when the expression must be |
876 | -- built in place. In the case of a build-in-place function call, | |
877 | -- that could lead to a duplication of the call, which was already | |
878 | -- substituted for the allocator. | |
879 | ||
26bff3d9 | 880 | if not Aggr_In_Place then |
fbf5a39b AC |
881 | Remove_Side_Effects (Exp); |
882 | end if; | |
883 | ||
e86a3a7e | 884 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
885 | |
886 | -- For a class wide allocation generate the following code: | |
887 | ||
888 | -- type Equiv_Record is record ... end record; | |
889 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
890 | -- temp : PtrT := new CW'(CW!(expr)); | |
891 | ||
892 | if Is_Class_Wide_Type (T) then | |
893 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
894 | ||
26bff3d9 JM |
895 | -- Ada 2005 (AI-251): If the expression is a class-wide interface |
896 | -- object we generate code to move up "this" to reference the | |
897 | -- base of the object before allocating the new object. | |
898 | ||
899 | -- Note that Exp'Address is recursively expanded into a call | |
900 | -- to Base_Address (Exp.Tag) | |
901 | ||
902 | if Is_Class_Wide_Type (Etype (Exp)) | |
903 | and then Is_Interface (Etype (Exp)) | |
1f110335 | 904 | and then Tagged_Type_Expansion |
26bff3d9 JM |
905 | then |
906 | Set_Expression | |
907 | (Expression (N), | |
908 | Unchecked_Convert_To (Entity (Indic), | |
909 | Make_Explicit_Dereference (Loc, | |
910 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
911 | Make_Attribute_Reference (Loc, | |
912 | Prefix => Exp, | |
913 | Attribute_Name => Name_Address))))); | |
26bff3d9 JM |
914 | else |
915 | Set_Expression | |
916 | (Expression (N), | |
917 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
918 | end if; | |
fbf5a39b AC |
919 | |
920 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
921 | end if; | |
922 | ||
df3e68b1 | 923 | -- Processing for allocators returning non-interface types |
fbf5a39b | 924 | |
26bff3d9 JM |
925 | if not Is_Interface (Directly_Designated_Type (PtrT)) then |
926 | if Aggr_In_Place then | |
df3e68b1 | 927 | Temp_Decl := |
26bff3d9 JM |
928 | Make_Object_Declaration (Loc, |
929 | Defining_Identifier => Temp, | |
930 | Object_Definition => New_Reference_To (PtrT, Loc), | |
931 | Expression => | |
932 | Make_Allocator (Loc, | |
df3e68b1 HK |
933 | Expression => |
934 | New_Reference_To (Etype (Exp), Loc))); | |
fbf5a39b | 935 | |
fad0600d AC |
936 | -- Copy the Comes_From_Source flag for the allocator we just |
937 | -- built, since logically this allocator is a replacement of | |
938 | -- the original allocator node. This is for proper handling of | |
939 | -- restriction No_Implicit_Heap_Allocations. | |
940 | ||
26bff3d9 | 941 | Set_Comes_From_Source |
df3e68b1 | 942 | (Expression (Temp_Decl), Comes_From_Source (N)); |
fbf5a39b | 943 | |
df3e68b1 HK |
944 | Set_No_Initialization (Expression (Temp_Decl)); |
945 | Insert_Action (N, Temp_Decl); | |
fbf5a39b | 946 | |
ca5af305 | 947 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 948 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fad0600d | 949 | |
d3f70b35 | 950 | -- Attach the object to the associated finalization master. |
deb8dacc HK |
951 | -- This is done manually on .NET/JVM since those compilers do |
952 | -- no support pools and can't benefit from internally generated | |
953 | -- Allocate / Deallocate procedures. | |
954 | ||
955 | if VM_Target /= No_VM | |
956 | and then Is_Controlled (DesigT) | |
d3f70b35 | 957 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
958 | then |
959 | Insert_Action (N, | |
960 | Make_Attach_Call ( | |
961 | Obj_Ref => | |
962 | New_Reference_To (Temp, Loc), | |
963 | Ptr_Typ => PtrT)); | |
964 | end if; | |
965 | ||
26bff3d9 JM |
966 | else |
967 | Node := Relocate_Node (N); | |
968 | Set_Analyzed (Node); | |
df3e68b1 HK |
969 | |
970 | Temp_Decl := | |
26bff3d9 JM |
971 | Make_Object_Declaration (Loc, |
972 | Defining_Identifier => Temp, | |
973 | Constant_Present => True, | |
974 | Object_Definition => New_Reference_To (PtrT, Loc), | |
df3e68b1 HK |
975 | Expression => Node); |
976 | ||
977 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 978 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
deb8dacc | 979 | |
d3f70b35 | 980 | -- Attach the object to the associated finalization master. |
deb8dacc HK |
981 | -- This is done manually on .NET/JVM since those compilers do |
982 | -- no support pools and can't benefit from internally generated | |
983 | -- Allocate / Deallocate procedures. | |
984 | ||
985 | if VM_Target /= No_VM | |
986 | and then Is_Controlled (DesigT) | |
d3f70b35 | 987 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
988 | then |
989 | Insert_Action (N, | |
990 | Make_Attach_Call ( | |
991 | Obj_Ref => | |
992 | New_Reference_To (Temp, Loc), | |
993 | Ptr_Typ => PtrT)); | |
994 | end if; | |
fbf5a39b AC |
995 | end if; |
996 | ||
26bff3d9 JM |
997 | -- Ada 2005 (AI-251): Handle allocators whose designated type is an |
998 | -- interface type. In this case we use the type of the qualified | |
999 | -- expression to allocate the object. | |
1000 | ||
fbf5a39b | 1001 | else |
26bff3d9 | 1002 | declare |
191fcb3a | 1003 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); |
26bff3d9 | 1004 | New_Decl : Node_Id; |
fbf5a39b | 1005 | |
26bff3d9 JM |
1006 | begin |
1007 | New_Decl := | |
1008 | Make_Full_Type_Declaration (Loc, | |
1009 | Defining_Identifier => Def_Id, | |
1010 | Type_Definition => | |
1011 | Make_Access_To_Object_Definition (Loc, | |
1012 | All_Present => True, | |
1013 | Null_Exclusion_Present => False, | |
1014 | Constant_Present => False, | |
1015 | Subtype_Indication => | |
1016 | New_Reference_To (Etype (Exp), Loc))); | |
1017 | ||
1018 | Insert_Action (N, New_Decl); | |
1019 | ||
df3e68b1 HK |
1020 | -- Inherit the allocation-related attributes from the original |
1021 | -- access type. | |
26bff3d9 | 1022 | |
d3f70b35 | 1023 | Set_Finalization_Master (Def_Id, Finalization_Master (PtrT)); |
df3e68b1 HK |
1024 | |
1025 | Set_Associated_Storage_Pool (Def_Id, | |
1026 | Associated_Storage_Pool (PtrT)); | |
758c442c | 1027 | |
26bff3d9 JM |
1028 | -- Declare the object using the previous type declaration |
1029 | ||
1030 | if Aggr_In_Place then | |
df3e68b1 | 1031 | Temp_Decl := |
26bff3d9 JM |
1032 | Make_Object_Declaration (Loc, |
1033 | Defining_Identifier => Temp, | |
1034 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
1035 | Expression => | |
1036 | Make_Allocator (Loc, | |
1037 | New_Reference_To (Etype (Exp), Loc))); | |
1038 | ||
fad0600d AC |
1039 | -- Copy the Comes_From_Source flag for the allocator we just |
1040 | -- built, since logically this allocator is a replacement of | |
1041 | -- the original allocator node. This is for proper handling | |
1042 | -- of restriction No_Implicit_Heap_Allocations. | |
1043 | ||
26bff3d9 | 1044 | Set_Comes_From_Source |
df3e68b1 | 1045 | (Expression (Temp_Decl), Comes_From_Source (N)); |
26bff3d9 | 1046 | |
df3e68b1 HK |
1047 | Set_No_Initialization (Expression (Temp_Decl)); |
1048 | Insert_Action (N, Temp_Decl); | |
26bff3d9 | 1049 | |
ca5af305 | 1050 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1051 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
26bff3d9 | 1052 | |
26bff3d9 JM |
1053 | else |
1054 | Node := Relocate_Node (N); | |
1055 | Set_Analyzed (Node); | |
df3e68b1 HK |
1056 | |
1057 | Temp_Decl := | |
26bff3d9 JM |
1058 | Make_Object_Declaration (Loc, |
1059 | Defining_Identifier => Temp, | |
1060 | Constant_Present => True, | |
1061 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
df3e68b1 HK |
1062 | Expression => Node); |
1063 | ||
1064 | Insert_Action (N, Temp_Decl); | |
ca5af305 | 1065 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
26bff3d9 JM |
1066 | end if; |
1067 | ||
1068 | -- Generate an additional object containing the address of the | |
1069 | -- returned object. The type of this second object declaration | |
685094bf RD |
1070 | -- is the correct type required for the common processing that |
1071 | -- is still performed by this subprogram. The displacement of | |
1072 | -- this pointer to reference the component associated with the | |
1073 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
1074 | |
1075 | New_Decl := | |
1076 | Make_Object_Declaration (Loc, | |
243cae0a AC |
1077 | Defining_Identifier => Make_Temporary (Loc, 'P'), |
1078 | Object_Definition => New_Reference_To (PtrT, Loc), | |
1079 | Expression => | |
df3e68b1 HK |
1080 | Unchecked_Convert_To (PtrT, |
1081 | New_Reference_To (Temp, Loc))); | |
26bff3d9 JM |
1082 | |
1083 | Insert_Action (N, New_Decl); | |
1084 | ||
df3e68b1 HK |
1085 | Temp_Decl := New_Decl; |
1086 | Temp := Defining_Identifier (New_Decl); | |
26bff3d9 | 1087 | end; |
758c442c GD |
1088 | end if; |
1089 | ||
26bff3d9 JM |
1090 | Apply_Accessibility_Check (Temp); |
1091 | ||
1092 | -- Generate the tag assignment | |
1093 | ||
1094 | -- Suppress the tag assignment when VM_Target because VM tags are | |
1095 | -- represented implicitly in objects. | |
1096 | ||
1f110335 | 1097 | if not Tagged_Type_Expansion then |
26bff3d9 | 1098 | null; |
fbf5a39b | 1099 | |
26bff3d9 JM |
1100 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
1101 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 1102 | |
26bff3d9 JM |
1103 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
1104 | pragma Assert (Is_Class_Wide_Type | |
1105 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
1106 | null; |
1107 | ||
1108 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
1109 | TagT := T; | |
1110 | TagR := New_Reference_To (Temp, Loc); | |
1111 | ||
1112 | elsif Is_Private_Type (T) | |
1113 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 1114 | then |
d26dc4b5 | 1115 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
1116 | TagR := |
1117 | Unchecked_Convert_To (Underlying_Type (T), | |
1118 | Make_Explicit_Dereference (Loc, | |
1119 | Prefix => New_Reference_To (Temp, Loc))); | |
d26dc4b5 AC |
1120 | end if; |
1121 | ||
1122 | if Present (TagT) then | |
38171f43 AC |
1123 | declare |
1124 | Full_T : constant Entity_Id := Underlying_Type (TagT); | |
38171f43 AC |
1125 | begin |
1126 | Tag_Assign := | |
1127 | Make_Assignment_Statement (Loc, | |
1128 | Name => | |
1129 | Make_Selected_Component (Loc, | |
1130 | Prefix => TagR, | |
1131 | Selector_Name => | |
1132 | New_Reference_To (First_Tag_Component (Full_T), Loc)), | |
1133 | Expression => | |
1134 | Unchecked_Convert_To (RTE (RE_Tag), | |
1135 | New_Reference_To | |
1136 | (Elists.Node | |
1137 | (First_Elmt (Access_Disp_Table (Full_T))), Loc))); | |
1138 | end; | |
fbf5a39b AC |
1139 | |
1140 | -- The previous assignment has to be done in any case | |
1141 | ||
1142 | Set_Assignment_OK (Name (Tag_Assign)); | |
1143 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
1144 | end if; |
1145 | ||
048e5cef BD |
1146 | if Needs_Finalization (DesigT) |
1147 | and then Needs_Finalization (T) | |
fbf5a39b | 1148 | then |
df3e68b1 HK |
1149 | -- Generate an Adjust call if the object will be moved. In Ada |
1150 | -- 2005, the object may be inherently limited, in which case | |
1151 | -- there is no Adjust procedure, and the object is built in | |
1152 | -- place. In Ada 95, the object can be limited but not | |
1153 | -- inherently limited if this allocator came from a return | |
1154 | -- statement (we're allocating the result on the secondary | |
1155 | -- stack). In that case, the object will be moved, so we _do_ | |
1156 | -- want to Adjust. | |
1157 | ||
1158 | if not Aggr_In_Place | |
1159 | and then not Is_Immutably_Limited_Type (T) | |
1160 | then | |
1161 | Insert_Action (N, | |
1162 | Make_Adjust_Call ( | |
1163 | Obj_Ref => | |
fbf5a39b | 1164 | |
685094bf | 1165 | -- An unchecked conversion is needed in the classwide |
df3e68b1 HK |
1166 | -- case because the designated type can be an ancestor |
1167 | -- of the subtype mark of the allocator. | |
fbf5a39b | 1168 | |
df3e68b1 HK |
1169 | Unchecked_Convert_To (T, |
1170 | Make_Explicit_Dereference (Loc, | |
1171 | Prefix => New_Reference_To (Temp, Loc))), | |
1172 | Typ => T)); | |
1173 | end if; | |
b254da66 AC |
1174 | |
1175 | -- Generate: | |
1176 | -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access); | |
1177 | ||
2bfa5484 | 1178 | -- Do not generate this call in the following cases: |
c5f5123f | 1179 | |
2bfa5484 HK |
1180 | -- * .NET/JVM - these targets do not support address arithmetic |
1181 | -- and unchecked conversion, key elements of Finalize_Address. | |
c5f5123f | 1182 | |
2bfa5484 HK |
1183 | -- * Alfa mode - the call is useless and results in unwanted |
1184 | -- expansion. | |
c5f5123f | 1185 | |
2bfa5484 HK |
1186 | -- * CodePeer mode - TSS primitive Finalize_Address is not |
1187 | -- created in this mode. | |
b254da66 AC |
1188 | |
1189 | if VM_Target = No_VM | |
2bfa5484 | 1190 | and then not Alfa_Mode |
b254da66 AC |
1191 | and then not CodePeer_Mode |
1192 | and then Present (Finalization_Master (PtrT)) | |
f7bb41af AC |
1193 | and then Present (Temp_Decl) |
1194 | and then Nkind (Expression (Temp_Decl)) = N_Allocator | |
b254da66 AC |
1195 | then |
1196 | Insert_Action (N, | |
1197 | Make_Set_Finalize_Address_Call | |
1198 | (Loc => Loc, | |
1199 | Typ => T, | |
1200 | Ptr_Typ => PtrT)); | |
1201 | end if; | |
fbf5a39b AC |
1202 | end if; |
1203 | ||
1204 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
1205 | Analyze_And_Resolve (N, PtrT); | |
1206 | ||
685094bf RD |
1207 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
1208 | -- component containing the secondary dispatch table of the interface | |
1209 | -- type. | |
26bff3d9 JM |
1210 | |
1211 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
1212 | Displace_Allocator_Pointer (N); | |
1213 | end if; | |
1214 | ||
fbf5a39b | 1215 | elsif Aggr_In_Place then |
e86a3a7e | 1216 | Temp := Make_Temporary (Loc, 'P', N); |
df3e68b1 | 1217 | Temp_Decl := |
fbf5a39b AC |
1218 | Make_Object_Declaration (Loc, |
1219 | Defining_Identifier => Temp, | |
1220 | Object_Definition => New_Reference_To (PtrT, Loc), | |
df3e68b1 HK |
1221 | Expression => |
1222 | Make_Allocator (Loc, | |
243cae0a | 1223 | Expression => New_Reference_To (Etype (Exp), Loc))); |
fbf5a39b | 1224 | |
fad0600d AC |
1225 | -- Copy the Comes_From_Source flag for the allocator we just built, |
1226 | -- since logically this allocator is a replacement of the original | |
1227 | -- allocator node. This is for proper handling of restriction | |
1228 | -- No_Implicit_Heap_Allocations. | |
1229 | ||
fbf5a39b | 1230 | Set_Comes_From_Source |
df3e68b1 HK |
1231 | (Expression (Temp_Decl), Comes_From_Source (N)); |
1232 | ||
1233 | Set_No_Initialization (Expression (Temp_Decl)); | |
1234 | Insert_Action (N, Temp_Decl); | |
1235 | ||
ca5af305 | 1236 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 1237 | Convert_Aggr_In_Allocator (N, Temp_Decl, Exp); |
fbf5a39b | 1238 | |
d3f70b35 AC |
1239 | -- Attach the object to the associated finalization master. Thisis |
1240 | -- done manually on .NET/JVM since those compilers do no support | |
deb8dacc HK |
1241 | -- pools and cannot benefit from internally generated Allocate and |
1242 | -- Deallocate procedures. | |
1243 | ||
1244 | if VM_Target /= No_VM | |
1245 | and then Is_Controlled (DesigT) | |
d3f70b35 | 1246 | and then Present (Finalization_Master (PtrT)) |
deb8dacc HK |
1247 | then |
1248 | Insert_Action (N, | |
243cae0a AC |
1249 | Make_Attach_Call |
1250 | (Obj_Ref => New_Reference_To (Temp, Loc), | |
1251 | Ptr_Typ => PtrT)); | |
deb8dacc HK |
1252 | end if; |
1253 | ||
fbf5a39b AC |
1254 | Rewrite (N, New_Reference_To (Temp, Loc)); |
1255 | Analyze_And_Resolve (N, PtrT); | |
1256 | ||
51e4c4b9 AC |
1257 | elsif Is_Access_Type (T) |
1258 | and then Can_Never_Be_Null (T) | |
1259 | then | |
1260 | Install_Null_Excluding_Check (Exp); | |
1261 | ||
f02b8bb8 | 1262 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
1263 | and then Nkind (Exp) = N_Allocator |
1264 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
1265 | then | |
0da2c8ac | 1266 | -- Apply constraint to designated subtype indication |
fbf5a39b AC |
1267 | |
1268 | Apply_Constraint_Check (Expression (Exp), | |
f02b8bb8 | 1269 | Designated_Type (DesigT), |
fbf5a39b AC |
1270 | No_Sliding => True); |
1271 | ||
1272 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
1273 | ||
1274 | -- Propagate constraint_error to enclosing allocator | |
1275 | ||
1276 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
1277 | end if; | |
1df4f514 | 1278 | |
fbf5a39b | 1279 | else |
14f0f659 AC |
1280 | Build_Allocate_Deallocate_Proc (N, True); |
1281 | ||
36c73552 AC |
1282 | -- If we have: |
1283 | -- type A is access T1; | |
1284 | -- X : A := new T2'(...); | |
1285 | -- T1 and T2 can be different subtypes, and we might need to check | |
1286 | -- both constraints. First check against the type of the qualified | |
1287 | -- expression. | |
1288 | ||
1289 | Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
fbf5a39b | 1290 | |
d79e621a GD |
1291 | if Do_Range_Check (Exp) then |
1292 | Set_Do_Range_Check (Exp, False); | |
1293 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); | |
1294 | end if; | |
1295 | ||
685094bf RD |
1296 | -- A check is also needed in cases where the designated subtype is |
1297 | -- constrained and differs from the subtype given in the qualified | |
1298 | -- expression. Note that the check on the qualified expression does | |
1299 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 1300 | |
f02b8bb8 | 1301 | if Is_Constrained (DesigT) |
9450205a | 1302 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
1303 | then |
1304 | Apply_Constraint_Check | |
f02b8bb8 | 1305 | (Exp, DesigT, No_Sliding => False); |
d79e621a GD |
1306 | |
1307 | if Do_Range_Check (Exp) then | |
1308 | Set_Do_Range_Check (Exp, False); | |
1309 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); | |
1310 | end if; | |
f02b8bb8 RD |
1311 | end if; |
1312 | ||
685094bf RD |
1313 | -- For an access to unconstrained packed array, GIGI needs to see an |
1314 | -- expression with a constrained subtype in order to compute the | |
1315 | -- proper size for the allocator. | |
f02b8bb8 RD |
1316 | |
1317 | if Is_Array_Type (T) | |
1318 | and then not Is_Constrained (T) | |
1319 | and then Is_Packed (T) | |
1320 | then | |
1321 | declare | |
191fcb3a | 1322 | ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A'); |
f02b8bb8 RD |
1323 | Internal_Exp : constant Node_Id := Relocate_Node (Exp); |
1324 | begin | |
1325 | Insert_Action (Exp, | |
1326 | Make_Subtype_Declaration (Loc, | |
1327 | Defining_Identifier => ConstrT, | |
25ebc085 AC |
1328 | Subtype_Indication => |
1329 | Make_Subtype_From_Expr (Internal_Exp, T))); | |
f02b8bb8 RD |
1330 | Freeze_Itype (ConstrT, Exp); |
1331 | Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp)); | |
1332 | end; | |
fbf5a39b | 1333 | end if; |
f02b8bb8 | 1334 | |
685094bf RD |
1335 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
1336 | -- to a build-in-place function, then access to the allocated object | |
1337 | -- must be passed to the function. Currently we limit such functions | |
1338 | -- to those with constrained limited result subtypes, but eventually | |
1339 | -- we plan to expand the allowed forms of functions that are treated | |
1340 | -- as build-in-place. | |
20b5d666 | 1341 | |
0791fbe9 | 1342 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
1343 | and then Is_Build_In_Place_Function_Call (Exp) |
1344 | then | |
1345 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
1346 | end if; | |
fbf5a39b AC |
1347 | end if; |
1348 | ||
1349 | exception | |
1350 | when RE_Not_Available => | |
1351 | return; | |
1352 | end Expand_Allocator_Expression; | |
1353 | ||
70482933 RK |
1354 | ----------------------------- |
1355 | -- Expand_Array_Comparison -- | |
1356 | ----------------------------- | |
1357 | ||
685094bf RD |
1358 | -- Expansion is only required in the case of array types. For the unpacked |
1359 | -- case, an appropriate runtime routine is called. For packed cases, and | |
1360 | -- also in some other cases where a runtime routine cannot be called, the | |
1361 | -- form of the expansion is: | |
70482933 RK |
1362 | |
1363 | -- [body for greater_nn; boolean_expression] | |
1364 | ||
1365 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
1366 | -- Boolean expression depends on the operator involved. | |
1367 | ||
1368 | procedure Expand_Array_Comparison (N : Node_Id) is | |
1369 | Loc : constant Source_Ptr := Sloc (N); | |
1370 | Op1 : Node_Id := Left_Opnd (N); | |
1371 | Op2 : Node_Id := Right_Opnd (N); | |
1372 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 1373 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
1374 | |
1375 | Expr : Node_Id; | |
1376 | Func_Body : Node_Id; | |
1377 | Func_Name : Entity_Id; | |
1378 | ||
fbf5a39b AC |
1379 | Comp : RE_Id; |
1380 | ||
9bc43c53 AC |
1381 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
1382 | -- True for byte addressable target | |
91b1417d | 1383 | |
fbf5a39b | 1384 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
685094bf RD |
1385 | -- Returns True if the length of the given operand is known to be less |
1386 | -- than 4. Returns False if this length is known to be four or greater | |
1387 | -- or is not known at compile time. | |
fbf5a39b AC |
1388 | |
1389 | ------------------------ | |
1390 | -- Length_Less_Than_4 -- | |
1391 | ------------------------ | |
1392 | ||
1393 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
1394 | Otyp : constant Entity_Id := Etype (Opnd); | |
1395 | ||
1396 | begin | |
1397 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
1398 | return String_Literal_Length (Otyp) < 4; | |
1399 | ||
1400 | else | |
1401 | declare | |
1402 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
1403 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
1404 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
1405 | Lov : Uint; | |
1406 | Hiv : Uint; | |
1407 | ||
1408 | begin | |
1409 | if Compile_Time_Known_Value (Lo) then | |
1410 | Lov := Expr_Value (Lo); | |
1411 | else | |
1412 | return False; | |
1413 | end if; | |
1414 | ||
1415 | if Compile_Time_Known_Value (Hi) then | |
1416 | Hiv := Expr_Value (Hi); | |
1417 | else | |
1418 | return False; | |
1419 | end if; | |
1420 | ||
1421 | return Hiv < Lov + 3; | |
1422 | end; | |
1423 | end if; | |
1424 | end Length_Less_Than_4; | |
1425 | ||
1426 | -- Start of processing for Expand_Array_Comparison | |
1427 | ||
70482933 | 1428 | begin |
fbf5a39b AC |
1429 | -- Deal first with unpacked case, where we can call a runtime routine |
1430 | -- except that we avoid this for targets for which are not addressable | |
26bff3d9 | 1431 | -- by bytes, and for the JVM/CIL, since they do not support direct |
fbf5a39b AC |
1432 | -- addressing of array components. |
1433 | ||
1434 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 1435 | and then Byte_Addressable |
26bff3d9 | 1436 | and then VM_Target = No_VM |
fbf5a39b AC |
1437 | then |
1438 | -- The call we generate is: | |
1439 | ||
1440 | -- Compare_Array_xn[_Unaligned] | |
1441 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
1442 | ||
1443 | -- x = U for unsigned, S for signed | |
1444 | -- n = 8,16,32,64 for component size | |
1445 | -- Add _Unaligned if length < 4 and component size is 8. | |
1446 | -- <op> is the standard comparison operator | |
1447 | ||
1448 | if Component_Size (Typ1) = 8 then | |
1449 | if Length_Less_Than_4 (Op1) | |
1450 | or else | |
1451 | Length_Less_Than_4 (Op2) | |
1452 | then | |
1453 | if Is_Unsigned_Type (Ctyp) then | |
1454 | Comp := RE_Compare_Array_U8_Unaligned; | |
1455 | else | |
1456 | Comp := RE_Compare_Array_S8_Unaligned; | |
1457 | end if; | |
1458 | ||
1459 | else | |
1460 | if Is_Unsigned_Type (Ctyp) then | |
1461 | Comp := RE_Compare_Array_U8; | |
1462 | else | |
1463 | Comp := RE_Compare_Array_S8; | |
1464 | end if; | |
1465 | end if; | |
1466 | ||
1467 | elsif Component_Size (Typ1) = 16 then | |
1468 | if Is_Unsigned_Type (Ctyp) then | |
1469 | Comp := RE_Compare_Array_U16; | |
1470 | else | |
1471 | Comp := RE_Compare_Array_S16; | |
1472 | end if; | |
1473 | ||
1474 | elsif Component_Size (Typ1) = 32 then | |
1475 | if Is_Unsigned_Type (Ctyp) then | |
1476 | Comp := RE_Compare_Array_U32; | |
1477 | else | |
1478 | Comp := RE_Compare_Array_S32; | |
1479 | end if; | |
1480 | ||
1481 | else pragma Assert (Component_Size (Typ1) = 64); | |
1482 | if Is_Unsigned_Type (Ctyp) then | |
1483 | Comp := RE_Compare_Array_U64; | |
1484 | else | |
1485 | Comp := RE_Compare_Array_S64; | |
1486 | end if; | |
1487 | end if; | |
1488 | ||
1489 | Remove_Side_Effects (Op1, Name_Req => True); | |
1490 | Remove_Side_Effects (Op2, Name_Req => True); | |
1491 | ||
1492 | Rewrite (Op1, | |
1493 | Make_Function_Call (Sloc (Op1), | |
1494 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
1495 | ||
1496 | Parameter_Associations => New_List ( | |
1497 | Make_Attribute_Reference (Loc, | |
1498 | Prefix => Relocate_Node (Op1), | |
1499 | Attribute_Name => Name_Address), | |
1500 | ||
1501 | Make_Attribute_Reference (Loc, | |
1502 | Prefix => Relocate_Node (Op2), | |
1503 | Attribute_Name => Name_Address), | |
1504 | ||
1505 | Make_Attribute_Reference (Loc, | |
1506 | Prefix => Relocate_Node (Op1), | |
1507 | Attribute_Name => Name_Length), | |
1508 | ||
1509 | Make_Attribute_Reference (Loc, | |
1510 | Prefix => Relocate_Node (Op2), | |
1511 | Attribute_Name => Name_Length)))); | |
1512 | ||
1513 | Rewrite (Op2, | |
1514 | Make_Integer_Literal (Sloc (Op2), | |
1515 | Intval => Uint_0)); | |
1516 | ||
1517 | Analyze_And_Resolve (Op1, Standard_Integer); | |
1518 | Analyze_And_Resolve (Op2, Standard_Integer); | |
1519 | return; | |
1520 | end if; | |
1521 | ||
1522 | -- Cases where we cannot make runtime call | |
1523 | ||
70482933 RK |
1524 | -- For (a <= b) we convert to not (a > b) |
1525 | ||
1526 | if Chars (N) = Name_Op_Le then | |
1527 | Rewrite (N, | |
1528 | Make_Op_Not (Loc, | |
1529 | Right_Opnd => | |
1530 | Make_Op_Gt (Loc, | |
1531 | Left_Opnd => Op1, | |
1532 | Right_Opnd => Op2))); | |
1533 | Analyze_And_Resolve (N, Standard_Boolean); | |
1534 | return; | |
1535 | ||
1536 | -- For < the Boolean expression is | |
1537 | -- greater__nn (op2, op1) | |
1538 | ||
1539 | elsif Chars (N) = Name_Op_Lt then | |
1540 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1541 | ||
1542 | -- Switch operands | |
1543 | ||
1544 | Op1 := Right_Opnd (N); | |
1545 | Op2 := Left_Opnd (N); | |
1546 | ||
1547 | -- For (a >= b) we convert to not (a < b) | |
1548 | ||
1549 | elsif Chars (N) = Name_Op_Ge then | |
1550 | Rewrite (N, | |
1551 | Make_Op_Not (Loc, | |
1552 | Right_Opnd => | |
1553 | Make_Op_Lt (Loc, | |
1554 | Left_Opnd => Op1, | |
1555 | Right_Opnd => Op2))); | |
1556 | Analyze_And_Resolve (N, Standard_Boolean); | |
1557 | return; | |
1558 | ||
1559 | -- For > the Boolean expression is | |
1560 | -- greater__nn (op1, op2) | |
1561 | ||
1562 | else | |
1563 | pragma Assert (Chars (N) = Name_Op_Gt); | |
1564 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1565 | end if; | |
1566 | ||
1567 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1568 | Expr := | |
1569 | Make_Function_Call (Loc, | |
1570 | Name => New_Reference_To (Func_Name, Loc), | |
1571 | Parameter_Associations => New_List (Op1, Op2)); | |
1572 | ||
1573 | Insert_Action (N, Func_Body); | |
1574 | Rewrite (N, Expr); | |
1575 | Analyze_And_Resolve (N, Standard_Boolean); | |
1576 | ||
fbf5a39b AC |
1577 | exception |
1578 | when RE_Not_Available => | |
1579 | return; | |
70482933 RK |
1580 | end Expand_Array_Comparison; |
1581 | ||
1582 | --------------------------- | |
1583 | -- Expand_Array_Equality -- | |
1584 | --------------------------- | |
1585 | ||
685094bf RD |
1586 | -- Expand an equality function for multi-dimensional arrays. Here is an |
1587 | -- example of such a function for Nb_Dimension = 2 | |
70482933 | 1588 | |
0da2c8ac | 1589 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 1590 | -- begin |
fbf5a39b AC |
1591 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
1592 | -- and then | |
1593 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
1594 | -- then | |
1595 | -- return True; -- RM 4.5.2(22) | |
1596 | -- end if; | |
0da2c8ac | 1597 | |
fbf5a39b AC |
1598 | -- if A'length (1) /= B'length (1) |
1599 | -- or else | |
1600 | -- A'length (2) /= B'length (2) | |
1601 | -- then | |
1602 | -- return False; -- RM 4.5.2(23) | |
1603 | -- end if; | |
0da2c8ac | 1604 | |
fbf5a39b | 1605 | -- declare |
523456db AC |
1606 | -- A1 : Index_T1 := A'first (1); |
1607 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 1608 | -- begin |
523456db | 1609 | -- loop |
fbf5a39b | 1610 | -- declare |
523456db AC |
1611 | -- A2 : Index_T2 := A'first (2); |
1612 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 1613 | -- begin |
523456db | 1614 | -- loop |
fbf5a39b AC |
1615 | -- if A (A1, A2) /= B (B1, B2) then |
1616 | -- return False; | |
70482933 | 1617 | -- end if; |
0da2c8ac | 1618 | |
523456db AC |
1619 | -- exit when A2 = A'last (2); |
1620 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 1621 | -- B2 := Index_T2'succ (B2); |
70482933 | 1622 | -- end loop; |
fbf5a39b | 1623 | -- end; |
0da2c8ac | 1624 | |
523456db AC |
1625 | -- exit when A1 = A'last (1); |
1626 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 1627 | -- B1 := Index_T1'succ (B1); |
70482933 | 1628 | -- end loop; |
fbf5a39b | 1629 | -- end; |
0da2c8ac | 1630 | |
70482933 RK |
1631 | -- return true; |
1632 | -- end Enn; | |
1633 | ||
685094bf RD |
1634 | -- Note on the formal types used (atyp and btyp). If either of the arrays |
1635 | -- is of a private type, we use the underlying type, and do an unchecked | |
1636 | -- conversion of the actual. If either of the arrays has a bound depending | |
1637 | -- on a discriminant, then we use the base type since otherwise we have an | |
1638 | -- escaped discriminant in the function. | |
0da2c8ac | 1639 | |
685094bf RD |
1640 | -- If both arrays are constrained and have the same bounds, we can generate |
1641 | -- a loop with an explicit iteration scheme using a 'Range attribute over | |
1642 | -- the first array. | |
523456db | 1643 | |
70482933 RK |
1644 | function Expand_Array_Equality |
1645 | (Nod : Node_Id; | |
70482933 RK |
1646 | Lhs : Node_Id; |
1647 | Rhs : Node_Id; | |
0da2c8ac AC |
1648 | Bodies : List_Id; |
1649 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
1650 | is |
1651 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
1652 | Decls : constant List_Id := New_List; |
1653 | Index_List1 : constant List_Id := New_List; | |
1654 | Index_List2 : constant List_Id := New_List; | |
1655 | ||
1656 | Actuals : List_Id; | |
1657 | Formals : List_Id; | |
1658 | Func_Name : Entity_Id; | |
1659 | Func_Body : Node_Id; | |
70482933 RK |
1660 | |
1661 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
1662 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
1663 | ||
0da2c8ac AC |
1664 | Ltyp : Entity_Id; |
1665 | Rtyp : Entity_Id; | |
1666 | -- The parameter types to be used for the formals | |
1667 | ||
fbf5a39b AC |
1668 | function Arr_Attr |
1669 | (Arr : Entity_Id; | |
1670 | Nam : Name_Id; | |
2e071734 | 1671 | Num : Int) return Node_Id; |
5e1c00fa | 1672 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 1673 | |
70482933 | 1674 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
685094bf | 1675 | -- Create one statement to compare corresponding components, designated |
3b42c566 | 1676 | -- by a full set of indexes. |
70482933 | 1677 | |
0da2c8ac | 1678 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
685094bf RD |
1679 | -- Given one of the arguments, computes the appropriate type to be used |
1680 | -- for that argument in the corresponding function formal | |
0da2c8ac | 1681 | |
fbf5a39b | 1682 | function Handle_One_Dimension |
70482933 | 1683 | (N : Int; |
2e071734 | 1684 | Index : Node_Id) return Node_Id; |
0da2c8ac | 1685 | -- This procedure returns the following code |
fbf5a39b AC |
1686 | -- |
1687 | -- declare | |
523456db | 1688 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 1689 | -- begin |
523456db | 1690 | -- loop |
fbf5a39b | 1691 | -- xxx |
523456db AC |
1692 | -- exit when An = A'Last (N); |
1693 | -- An := Index_T'Succ (An) | |
0da2c8ac | 1694 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
1695 | -- end loop; |
1696 | -- end; | |
1697 | -- | |
3b42c566 | 1698 | -- If both indexes are constrained and identical, the procedure |
523456db AC |
1699 | -- returns a simpler loop: |
1700 | -- | |
1701 | -- for An in A'Range (N) loop | |
1702 | -- xxx | |
1703 | -- end loop | |
0da2c8ac | 1704 | -- |
523456db | 1705 | -- N is the dimension for which we are generating a loop. Index is the |
685094bf RD |
1706 | -- N'th index node, whose Etype is Index_Type_n in the above code. The |
1707 | -- xxx statement is either the loop or declare for the next dimension | |
1708 | -- or if this is the last dimension the comparison of corresponding | |
1709 | -- components of the arrays. | |
fbf5a39b | 1710 | -- |
685094bf RD |
1711 | -- The actual way the code works is to return the comparison of |
1712 | -- corresponding components for the N+1 call. That's neater! | |
fbf5a39b AC |
1713 | |
1714 | function Test_Empty_Arrays return Node_Id; | |
1715 | -- This function constructs the test for both arrays being empty | |
1716 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1717 | -- and then | |
1718 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1719 | ||
1720 | function Test_Lengths_Correspond return Node_Id; | |
685094bf RD |
1721 | -- This function constructs the test for arrays having different lengths |
1722 | -- in at least one index position, in which case the resulting code is: | |
fbf5a39b AC |
1723 | |
1724 | -- A'length (1) /= B'length (1) | |
1725 | -- or else | |
1726 | -- A'length (2) /= B'length (2) | |
1727 | -- or else | |
1728 | -- ... | |
1729 | ||
1730 | -------------- | |
1731 | -- Arr_Attr -- | |
1732 | -------------- | |
1733 | ||
1734 | function Arr_Attr | |
1735 | (Arr : Entity_Id; | |
1736 | Nam : Name_Id; | |
2e071734 | 1737 | Num : Int) return Node_Id |
fbf5a39b AC |
1738 | is |
1739 | begin | |
1740 | return | |
1741 | Make_Attribute_Reference (Loc, | |
1742 | Attribute_Name => Nam, | |
1743 | Prefix => New_Reference_To (Arr, Loc), | |
1744 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
1745 | end Arr_Attr; | |
70482933 RK |
1746 | |
1747 | ------------------------ | |
1748 | -- Component_Equality -- | |
1749 | ------------------------ | |
1750 | ||
1751 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1752 | Test : Node_Id; | |
1753 | L, R : Node_Id; | |
1754 | ||
1755 | begin | |
1756 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1757 | ||
1758 | L := | |
1759 | Make_Indexed_Component (Loc, | |
7675ad4f | 1760 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
1761 | Expressions => Index_List1); |
1762 | ||
1763 | R := | |
1764 | Make_Indexed_Component (Loc, | |
7675ad4f | 1765 | Prefix => Make_Identifier (Loc, Chars (B)), |
70482933 RK |
1766 | Expressions => Index_List2); |
1767 | ||
1768 | Test := Expand_Composite_Equality | |
1769 | (Nod, Component_Type (Typ), L, R, Decls); | |
1770 | ||
a9d8907c JM |
1771 | -- If some (sub)component is an unchecked_union, the whole operation |
1772 | -- will raise program error. | |
8aceda64 AC |
1773 | |
1774 | if Nkind (Test) = N_Raise_Program_Error then | |
a9d8907c JM |
1775 | |
1776 | -- This node is going to be inserted at a location where a | |
685094bf RD |
1777 | -- statement is expected: clear its Etype so analysis will set |
1778 | -- it to the expected Standard_Void_Type. | |
a9d8907c JM |
1779 | |
1780 | Set_Etype (Test, Empty); | |
8aceda64 AC |
1781 | return Test; |
1782 | ||
1783 | else | |
1784 | return | |
1785 | Make_Implicit_If_Statement (Nod, | |
1786 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), | |
1787 | Then_Statements => New_List ( | |
d766cee3 | 1788 | Make_Simple_Return_Statement (Loc, |
8aceda64 AC |
1789 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
1790 | end if; | |
70482933 RK |
1791 | end Component_Equality; |
1792 | ||
0da2c8ac AC |
1793 | ------------------ |
1794 | -- Get_Arg_Type -- | |
1795 | ------------------ | |
1796 | ||
1797 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1798 | T : Entity_Id; | |
1799 | X : Node_Id; | |
1800 | ||
1801 | begin | |
1802 | T := Etype (N); | |
1803 | ||
1804 | if No (T) then | |
1805 | return Typ; | |
1806 | ||
1807 | else | |
1808 | T := Underlying_Type (T); | |
1809 | ||
1810 | X := First_Index (T); | |
1811 | while Present (X) loop | |
1812 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) | |
1813 | or else | |
1814 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
1815 | then | |
1816 | T := Base_Type (T); | |
1817 | exit; | |
1818 | end if; | |
1819 | ||
1820 | Next_Index (X); | |
1821 | end loop; | |
1822 | ||
1823 | return T; | |
1824 | end if; | |
1825 | end Get_Arg_Type; | |
1826 | ||
fbf5a39b AC |
1827 | -------------------------- |
1828 | -- Handle_One_Dimension -- | |
1829 | --------------------------- | |
70482933 | 1830 | |
fbf5a39b | 1831 | function Handle_One_Dimension |
70482933 | 1832 | (N : Int; |
2e071734 | 1833 | Index : Node_Id) return Node_Id |
70482933 | 1834 | is |
0da2c8ac AC |
1835 | Need_Separate_Indexes : constant Boolean := |
1836 | Ltyp /= Rtyp | |
1837 | or else not Is_Constrained (Ltyp); | |
1838 | -- If the index types are identical, and we are working with | |
685094bf RD |
1839 | -- constrained types, then we can use the same index for both |
1840 | -- of the arrays. | |
0da2c8ac | 1841 | |
191fcb3a | 1842 | An : constant Entity_Id := Make_Temporary (Loc, 'A'); |
0da2c8ac AC |
1843 | |
1844 | Bn : Entity_Id; | |
1845 | Index_T : Entity_Id; | |
1846 | Stm_List : List_Id; | |
1847 | Loop_Stm : Node_Id; | |
70482933 RK |
1848 | |
1849 | begin | |
0da2c8ac AC |
1850 | if N > Number_Dimensions (Ltyp) then |
1851 | return Component_Equality (Ltyp); | |
fbf5a39b | 1852 | end if; |
70482933 | 1853 | |
0da2c8ac AC |
1854 | -- Case where we generate a loop |
1855 | ||
1856 | Index_T := Base_Type (Etype (Index)); | |
1857 | ||
1858 | if Need_Separate_Indexes then | |
191fcb3a | 1859 | Bn := Make_Temporary (Loc, 'B'); |
0da2c8ac AC |
1860 | else |
1861 | Bn := An; | |
1862 | end if; | |
70482933 | 1863 | |
fbf5a39b AC |
1864 | Append (New_Reference_To (An, Loc), Index_List1); |
1865 | Append (New_Reference_To (Bn, Loc), Index_List2); | |
70482933 | 1866 | |
0da2c8ac AC |
1867 | Stm_List := New_List ( |
1868 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1869 | |
0da2c8ac | 1870 | if Need_Separate_Indexes then |
a9d8907c | 1871 | |
3b42c566 | 1872 | -- Generate guard for loop, followed by increments of indexes |
523456db AC |
1873 | |
1874 | Append_To (Stm_List, | |
1875 | Make_Exit_Statement (Loc, | |
1876 | Condition => | |
1877 | Make_Op_Eq (Loc, | |
1878 | Left_Opnd => New_Reference_To (An, Loc), | |
1879 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); | |
1880 | ||
1881 | Append_To (Stm_List, | |
1882 | Make_Assignment_Statement (Loc, | |
1883 | Name => New_Reference_To (An, Loc), | |
1884 | Expression => | |
1885 | Make_Attribute_Reference (Loc, | |
1886 | Prefix => New_Reference_To (Index_T, Loc), | |
1887 | Attribute_Name => Name_Succ, | |
1888 | Expressions => New_List (New_Reference_To (An, Loc))))); | |
1889 | ||
0da2c8ac AC |
1890 | Append_To (Stm_List, |
1891 | Make_Assignment_Statement (Loc, | |
1892 | Name => New_Reference_To (Bn, Loc), | |
1893 | Expression => | |
1894 | Make_Attribute_Reference (Loc, | |
1895 | Prefix => New_Reference_To (Index_T, Loc), | |
1896 | Attribute_Name => Name_Succ, | |
1897 | Expressions => New_List (New_Reference_To (Bn, Loc))))); | |
1898 | end if; | |
1899 | ||
a9d8907c JM |
1900 | -- If separate indexes, we need a declare block for An and Bn, and a |
1901 | -- loop without an iteration scheme. | |
0da2c8ac AC |
1902 | |
1903 | if Need_Separate_Indexes then | |
523456db AC |
1904 | Loop_Stm := |
1905 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
1906 | ||
0da2c8ac AC |
1907 | return |
1908 | Make_Block_Statement (Loc, | |
1909 | Declarations => New_List ( | |
523456db AC |
1910 | Make_Object_Declaration (Loc, |
1911 | Defining_Identifier => An, | |
1912 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1913 | Expression => Arr_Attr (A, Name_First, N)), | |
1914 | ||
0da2c8ac AC |
1915 | Make_Object_Declaration (Loc, |
1916 | Defining_Identifier => Bn, | |
1917 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1918 | Expression => Arr_Attr (B, Name_First, N))), | |
523456db | 1919 | |
0da2c8ac AC |
1920 | Handled_Statement_Sequence => |
1921 | Make_Handled_Sequence_Of_Statements (Loc, | |
1922 | Statements => New_List (Loop_Stm))); | |
1923 | ||
523456db AC |
1924 | -- If no separate indexes, return loop statement with explicit |
1925 | -- iteration scheme on its own | |
0da2c8ac AC |
1926 | |
1927 | else | |
523456db AC |
1928 | Loop_Stm := |
1929 | Make_Implicit_Loop_Statement (Nod, | |
1930 | Statements => Stm_List, | |
1931 | Iteration_Scheme => | |
1932 | Make_Iteration_Scheme (Loc, | |
1933 | Loop_Parameter_Specification => | |
1934 | Make_Loop_Parameter_Specification (Loc, | |
1935 | Defining_Identifier => An, | |
1936 | Discrete_Subtype_Definition => | |
1937 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
1938 | return Loop_Stm; |
1939 | end if; | |
fbf5a39b AC |
1940 | end Handle_One_Dimension; |
1941 | ||
1942 | ----------------------- | |
1943 | -- Test_Empty_Arrays -- | |
1944 | ----------------------- | |
1945 | ||
1946 | function Test_Empty_Arrays return Node_Id is | |
1947 | Alist : Node_Id; | |
1948 | Blist : Node_Id; | |
1949 | ||
1950 | Atest : Node_Id; | |
1951 | Btest : Node_Id; | |
70482933 | 1952 | |
fbf5a39b AC |
1953 | begin |
1954 | Alist := Empty; | |
1955 | Blist := Empty; | |
0da2c8ac | 1956 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1957 | Atest := |
1958 | Make_Op_Eq (Loc, | |
1959 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1960 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1961 | ||
1962 | Btest := | |
1963 | Make_Op_Eq (Loc, | |
1964 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
1965 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1966 | ||
1967 | if No (Alist) then | |
1968 | Alist := Atest; | |
1969 | Blist := Btest; | |
70482933 | 1970 | |
fbf5a39b AC |
1971 | else |
1972 | Alist := | |
1973 | Make_Or_Else (Loc, | |
1974 | Left_Opnd => Relocate_Node (Alist), | |
1975 | Right_Opnd => Atest); | |
1976 | ||
1977 | Blist := | |
1978 | Make_Or_Else (Loc, | |
1979 | Left_Opnd => Relocate_Node (Blist), | |
1980 | Right_Opnd => Btest); | |
1981 | end if; | |
1982 | end loop; | |
70482933 | 1983 | |
fbf5a39b AC |
1984 | return |
1985 | Make_And_Then (Loc, | |
1986 | Left_Opnd => Alist, | |
1987 | Right_Opnd => Blist); | |
1988 | end Test_Empty_Arrays; | |
70482933 | 1989 | |
fbf5a39b AC |
1990 | ----------------------------- |
1991 | -- Test_Lengths_Correspond -- | |
1992 | ----------------------------- | |
70482933 | 1993 | |
fbf5a39b AC |
1994 | function Test_Lengths_Correspond return Node_Id is |
1995 | Result : Node_Id; | |
1996 | Rtest : Node_Id; | |
1997 | ||
1998 | begin | |
1999 | Result := Empty; | |
0da2c8ac | 2000 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
2001 | Rtest := |
2002 | Make_Op_Ne (Loc, | |
2003 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
2004 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
2005 | ||
2006 | if No (Result) then | |
2007 | Result := Rtest; | |
2008 | else | |
2009 | Result := | |
2010 | Make_Or_Else (Loc, | |
2011 | Left_Opnd => Relocate_Node (Result), | |
2012 | Right_Opnd => Rtest); | |
2013 | end if; | |
2014 | end loop; | |
2015 | ||
2016 | return Result; | |
2017 | end Test_Lengths_Correspond; | |
70482933 RK |
2018 | |
2019 | -- Start of processing for Expand_Array_Equality | |
2020 | ||
2021 | begin | |
0da2c8ac AC |
2022 | Ltyp := Get_Arg_Type (Lhs); |
2023 | Rtyp := Get_Arg_Type (Rhs); | |
2024 | ||
685094bf RD |
2025 | -- For now, if the argument types are not the same, go to the base type, |
2026 | -- since the code assumes that the formals have the same type. This is | |
2027 | -- fixable in future ??? | |
0da2c8ac AC |
2028 | |
2029 | if Ltyp /= Rtyp then | |
2030 | Ltyp := Base_Type (Ltyp); | |
2031 | Rtyp := Base_Type (Rtyp); | |
2032 | pragma Assert (Ltyp = Rtyp); | |
2033 | end if; | |
2034 | ||
2035 | -- Build list of formals for function | |
2036 | ||
70482933 RK |
2037 | Formals := New_List ( |
2038 | Make_Parameter_Specification (Loc, | |
2039 | Defining_Identifier => A, | |
0da2c8ac | 2040 | Parameter_Type => New_Reference_To (Ltyp, Loc)), |
70482933 RK |
2041 | |
2042 | Make_Parameter_Specification (Loc, | |
2043 | Defining_Identifier => B, | |
0da2c8ac | 2044 | Parameter_Type => New_Reference_To (Rtyp, Loc))); |
70482933 | 2045 | |
191fcb3a | 2046 | Func_Name := Make_Temporary (Loc, 'E'); |
70482933 | 2047 | |
fbf5a39b | 2048 | -- Build statement sequence for function |
70482933 RK |
2049 | |
2050 | Func_Body := | |
2051 | Make_Subprogram_Body (Loc, | |
2052 | Specification => | |
2053 | Make_Function_Specification (Loc, | |
2054 | Defining_Unit_Name => Func_Name, | |
2055 | Parameter_Specifications => Formals, | |
630d30e9 | 2056 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
fbf5a39b AC |
2057 | |
2058 | Declarations => Decls, | |
2059 | ||
70482933 RK |
2060 | Handled_Statement_Sequence => |
2061 | Make_Handled_Sequence_Of_Statements (Loc, | |
2062 | Statements => New_List ( | |
fbf5a39b AC |
2063 | |
2064 | Make_Implicit_If_Statement (Nod, | |
2065 | Condition => Test_Empty_Arrays, | |
2066 | Then_Statements => New_List ( | |
d766cee3 | 2067 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
2068 | Expression => |
2069 | New_Occurrence_Of (Standard_True, Loc)))), | |
2070 | ||
2071 | Make_Implicit_If_Statement (Nod, | |
2072 | Condition => Test_Lengths_Correspond, | |
2073 | Then_Statements => New_List ( | |
d766cee3 | 2074 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
2075 | Expression => |
2076 | New_Occurrence_Of (Standard_False, Loc)))), | |
2077 | ||
0da2c8ac | 2078 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 2079 | |
d766cee3 | 2080 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
2081 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
2082 | ||
2083 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 2084 | Set_Is_Inlined (Func_Name); |
70482933 | 2085 | |
685094bf RD |
2086 | -- If the array type is distinct from the type of the arguments, it |
2087 | -- is the full view of a private type. Apply an unchecked conversion | |
2088 | -- to insure that analysis of the call succeeds. | |
70482933 | 2089 | |
0da2c8ac AC |
2090 | declare |
2091 | L, R : Node_Id; | |
2092 | ||
2093 | begin | |
2094 | L := Lhs; | |
2095 | R := Rhs; | |
2096 | ||
2097 | if No (Etype (Lhs)) | |
2098 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
2099 | then | |
2100 | L := OK_Convert_To (Ltyp, Lhs); | |
2101 | end if; | |
2102 | ||
2103 | if No (Etype (Rhs)) | |
2104 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
2105 | then | |
2106 | R := OK_Convert_To (Rtyp, Rhs); | |
2107 | end if; | |
2108 | ||
2109 | Actuals := New_List (L, R); | |
2110 | end; | |
70482933 RK |
2111 | |
2112 | Append_To (Bodies, Func_Body); | |
2113 | ||
2114 | return | |
2115 | Make_Function_Call (Loc, | |
0da2c8ac | 2116 | Name => New_Reference_To (Func_Name, Loc), |
70482933 RK |
2117 | Parameter_Associations => Actuals); |
2118 | end Expand_Array_Equality; | |
2119 | ||
2120 | ----------------------------- | |
2121 | -- Expand_Boolean_Operator -- | |
2122 | ----------------------------- | |
2123 | ||
685094bf RD |
2124 | -- Note that we first get the actual subtypes of the operands, since we |
2125 | -- always want to deal with types that have bounds. | |
70482933 RK |
2126 | |
2127 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 2128 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
2129 | |
2130 | begin | |
685094bf RD |
2131 | -- Special case of bit packed array where both operands are known to be |
2132 | -- properly aligned. In this case we use an efficient run time routine | |
2133 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
2134 | |
2135 | if Is_Bit_Packed_Array (Typ) | |
2136 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
2137 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
2138 | then | |
70482933 | 2139 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
2140 | return; |
2141 | end if; | |
70482933 | 2142 | |
a9d8907c JM |
2143 | -- For the normal non-packed case, the general expansion is to build |
2144 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
2145 | -- and then inserting it into the tree. The original operator node is | |
2146 | -- then rewritten as a call to this function. We also use this in the | |
2147 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 2148 | |
a9d8907c JM |
2149 | declare |
2150 | Loc : constant Source_Ptr := Sloc (N); | |
2151 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
2152 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
2153 | Func_Body : Node_Id; | |
2154 | Func_Name : Entity_Id; | |
fbf5a39b | 2155 | |
a9d8907c JM |
2156 | begin |
2157 | Convert_To_Actual_Subtype (L); | |
2158 | Convert_To_Actual_Subtype (R); | |
2159 | Ensure_Defined (Etype (L), N); | |
2160 | Ensure_Defined (Etype (R), N); | |
2161 | Apply_Length_Check (R, Etype (L)); | |
2162 | ||
b4592168 GD |
2163 | if Nkind (N) = N_Op_Xor then |
2164 | Silly_Boolean_Array_Xor_Test (N, Etype (L)); | |
2165 | end if; | |
2166 | ||
a9d8907c JM |
2167 | if Nkind (Parent (N)) = N_Assignment_Statement |
2168 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
2169 | then | |
2170 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 2171 | |
a9d8907c JM |
2172 | elsif Nkind (Parent (N)) = N_Op_Not |
2173 | and then Nkind (N) = N_Op_And | |
2174 | and then | |
b4592168 | 2175 | Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
2176 | then |
2177 | return; | |
2178 | else | |
fbf5a39b | 2179 | |
a9d8907c JM |
2180 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
2181 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
2182 | Insert_Action (N, Func_Body); | |
70482933 | 2183 | |
a9d8907c | 2184 | -- Now rewrite the expression with a call |
70482933 | 2185 | |
a9d8907c JM |
2186 | Rewrite (N, |
2187 | Make_Function_Call (Loc, | |
2188 | Name => New_Reference_To (Func_Name, Loc), | |
2189 | Parameter_Associations => | |
2190 | New_List ( | |
2191 | L, | |
2192 | Make_Type_Conversion | |
2193 | (Loc, New_Reference_To (Etype (L), Loc), R)))); | |
70482933 | 2194 | |
a9d8907c JM |
2195 | Analyze_And_Resolve (N, Typ); |
2196 | end if; | |
2197 | end; | |
70482933 RK |
2198 | end Expand_Boolean_Operator; |
2199 | ||
2200 | ------------------------------- | |
2201 | -- Expand_Composite_Equality -- | |
2202 | ------------------------------- | |
2203 | ||
2204 | -- This function is only called for comparing internal fields of composite | |
2205 | -- types when these fields are themselves composites. This is a special | |
2206 | -- case because it is not possible to respect normal Ada visibility rules. | |
2207 | ||
2208 | function Expand_Composite_Equality | |
2209 | (Nod : Node_Id; | |
2210 | Typ : Entity_Id; | |
2211 | Lhs : Node_Id; | |
2212 | Rhs : Node_Id; | |
2e071734 | 2213 | Bodies : List_Id) return Node_Id |
70482933 RK |
2214 | is |
2215 | Loc : constant Source_Ptr := Sloc (Nod); | |
2216 | Full_Type : Entity_Id; | |
2217 | Prim : Elmt_Id; | |
2218 | Eq_Op : Entity_Id; | |
2219 | ||
7efc3f2d AC |
2220 | function Find_Primitive_Eq return Node_Id; |
2221 | -- AI05-0123: Locate primitive equality for type if it exists, and | |
2222 | -- build the corresponding call. If operation is abstract, replace | |
2223 | -- call with an explicit raise. Return Empty if there is no primitive. | |
2224 | ||
2225 | ----------------------- | |
2226 | -- Find_Primitive_Eq -- | |
2227 | ----------------------- | |
2228 | ||
2229 | function Find_Primitive_Eq return Node_Id is | |
2230 | Prim_E : Elmt_Id; | |
2231 | Prim : Node_Id; | |
2232 | ||
2233 | begin | |
2234 | Prim_E := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2235 | while Present (Prim_E) loop | |
2236 | Prim := Node (Prim_E); | |
2237 | ||
2238 | -- Locate primitive equality with the right signature | |
2239 | ||
2240 | if Chars (Prim) = Name_Op_Eq | |
2241 | and then Etype (First_Formal (Prim)) = | |
39ade2f9 | 2242 | Etype (Next_Formal (First_Formal (Prim))) |
7efc3f2d AC |
2243 | and then Etype (Prim) = Standard_Boolean |
2244 | then | |
2245 | if Is_Abstract_Subprogram (Prim) then | |
2246 | return | |
2247 | Make_Raise_Program_Error (Loc, | |
2248 | Reason => PE_Explicit_Raise); | |
2249 | ||
2250 | else | |
2251 | return | |
2252 | Make_Function_Call (Loc, | |
39ade2f9 | 2253 | Name => New_Reference_To (Prim, Loc), |
7efc3f2d AC |
2254 | Parameter_Associations => New_List (Lhs, Rhs)); |
2255 | end if; | |
2256 | end if; | |
2257 | ||
2258 | Next_Elmt (Prim_E); | |
2259 | end loop; | |
2260 | ||
2261 | -- If not found, predefined operation will be used | |
2262 | ||
2263 | return Empty; | |
2264 | end Find_Primitive_Eq; | |
2265 | ||
2266 | -- Start of processing for Expand_Composite_Equality | |
2267 | ||
70482933 RK |
2268 | begin |
2269 | if Is_Private_Type (Typ) then | |
2270 | Full_Type := Underlying_Type (Typ); | |
2271 | else | |
2272 | Full_Type := Typ; | |
2273 | end if; | |
2274 | ||
685094bf RD |
2275 | -- Defense against malformed private types with no completion the error |
2276 | -- will be diagnosed later by check_completion | |
70482933 RK |
2277 | |
2278 | if No (Full_Type) then | |
2279 | return New_Reference_To (Standard_False, Loc); | |
2280 | end if; | |
2281 | ||
2282 | Full_Type := Base_Type (Full_Type); | |
2283 | ||
2284 | if Is_Array_Type (Full_Type) then | |
2285 | ||
2286 | -- If the operand is an elementary type other than a floating-point | |
2287 | -- type, then we can simply use the built-in block bitwise equality, | |
2288 | -- since the predefined equality operators always apply and bitwise | |
2289 | -- equality is fine for all these cases. | |
2290 | ||
2291 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
2292 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
2293 | then | |
39ade2f9 | 2294 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); |
70482933 | 2295 | |
685094bf RD |
2296 | -- For composite component types, and floating-point types, use the |
2297 | -- expansion. This deals with tagged component types (where we use | |
2298 | -- the applicable equality routine) and floating-point, (where we | |
2299 | -- need to worry about negative zeroes), and also the case of any | |
2300 | -- composite type recursively containing such fields. | |
70482933 RK |
2301 | |
2302 | else | |
0da2c8ac | 2303 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
2304 | end if; |
2305 | ||
2306 | elsif Is_Tagged_Type (Full_Type) then | |
2307 | ||
2308 | -- Call the primitive operation "=" of this type | |
2309 | ||
2310 | if Is_Class_Wide_Type (Full_Type) then | |
2311 | Full_Type := Root_Type (Full_Type); | |
2312 | end if; | |
2313 | ||
685094bf RD |
2314 | -- If this is derived from an untagged private type completed with a |
2315 | -- tagged type, it does not have a full view, so we use the primitive | |
2316 | -- operations of the private type. This check should no longer be | |
2317 | -- necessary when these types receive their full views ??? | |
70482933 RK |
2318 | |
2319 | if Is_Private_Type (Typ) | |
2320 | and then not Is_Tagged_Type (Typ) | |
2321 | and then not Is_Controlled (Typ) | |
2322 | and then Is_Derived_Type (Typ) | |
2323 | and then No (Full_View (Typ)) | |
2324 | then | |
2325 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2326 | else | |
2327 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
2328 | end if; | |
2329 | ||
2330 | loop | |
2331 | Eq_Op := Node (Prim); | |
2332 | exit when Chars (Eq_Op) = Name_Op_Eq | |
2333 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
2334 | Etype (Next_Formal (First_Formal (Eq_Op))) |
2335 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
2336 | Next_Elmt (Prim); |
2337 | pragma Assert (Present (Prim)); | |
2338 | end loop; | |
2339 | ||
2340 | Eq_Op := Node (Prim); | |
2341 | ||
2342 | return | |
2343 | Make_Function_Call (Loc, | |
2344 | Name => New_Reference_To (Eq_Op, Loc), | |
2345 | Parameter_Associations => | |
2346 | New_List | |
2347 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2348 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2349 | ||
2350 | elsif Is_Record_Type (Full_Type) then | |
fbf5a39b | 2351 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2352 | |
2353 | if Present (Eq_Op) then | |
2354 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2355 | ||
685094bf RD |
2356 | -- Inherited equality from parent type. Convert the actuals to |
2357 | -- match signature of operation. | |
70482933 RK |
2358 | |
2359 | declare | |
fbf5a39b | 2360 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2361 | |
2362 | begin | |
2363 | return | |
2364 | Make_Function_Call (Loc, | |
39ade2f9 AC |
2365 | Name => New_Reference_To (Eq_Op, Loc), |
2366 | Parameter_Associations => New_List ( | |
2367 | OK_Convert_To (T, Lhs), | |
2368 | OK_Convert_To (T, Rhs))); | |
70482933 RK |
2369 | end; |
2370 | ||
2371 | else | |
5d09245e AC |
2372 | -- Comparison between Unchecked_Union components |
2373 | ||
2374 | if Is_Unchecked_Union (Full_Type) then | |
2375 | declare | |
2376 | Lhs_Type : Node_Id := Full_Type; | |
2377 | Rhs_Type : Node_Id := Full_Type; | |
2378 | Lhs_Discr_Val : Node_Id; | |
2379 | Rhs_Discr_Val : Node_Id; | |
2380 | ||
2381 | begin | |
2382 | -- Lhs subtype | |
2383 | ||
2384 | if Nkind (Lhs) = N_Selected_Component then | |
2385 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2386 | end if; | |
2387 | ||
2388 | -- Rhs subtype | |
2389 | ||
2390 | if Nkind (Rhs) = N_Selected_Component then | |
2391 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2392 | end if; | |
2393 | ||
2394 | -- Lhs of the composite equality | |
2395 | ||
2396 | if Is_Constrained (Lhs_Type) then | |
2397 | ||
685094bf | 2398 | -- Since the enclosing record type can never be an |
5d09245e AC |
2399 | -- Unchecked_Union (this code is executed for records |
2400 | -- that do not have variants), we may reference its | |
2401 | -- discriminant(s). | |
2402 | ||
2403 | if Nkind (Lhs) = N_Selected_Component | |
2404 | and then Has_Per_Object_Constraint ( | |
2405 | Entity (Selector_Name (Lhs))) | |
2406 | then | |
2407 | Lhs_Discr_Val := | |
2408 | Make_Selected_Component (Loc, | |
39ade2f9 | 2409 | Prefix => Prefix (Lhs), |
5d09245e | 2410 | Selector_Name => |
39ade2f9 AC |
2411 | New_Copy |
2412 | (Get_Discriminant_Value | |
2413 | (First_Discriminant (Lhs_Type), | |
2414 | Lhs_Type, | |
2415 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
2416 | |
2417 | else | |
39ade2f9 AC |
2418 | Lhs_Discr_Val := |
2419 | New_Copy | |
2420 | (Get_Discriminant_Value | |
2421 | (First_Discriminant (Lhs_Type), | |
2422 | Lhs_Type, | |
2423 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
2424 | |
2425 | end if; | |
2426 | else | |
2427 | -- It is not possible to infer the discriminant since | |
2428 | -- the subtype is not constrained. | |
2429 | ||
8aceda64 | 2430 | return |
5d09245e | 2431 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2432 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2433 | end if; |
2434 | ||
2435 | -- Rhs of the composite equality | |
2436 | ||
2437 | if Is_Constrained (Rhs_Type) then | |
2438 | if Nkind (Rhs) = N_Selected_Component | |
39ade2f9 AC |
2439 | and then Has_Per_Object_Constraint |
2440 | (Entity (Selector_Name (Rhs))) | |
5d09245e AC |
2441 | then |
2442 | Rhs_Discr_Val := | |
2443 | Make_Selected_Component (Loc, | |
39ade2f9 | 2444 | Prefix => Prefix (Rhs), |
5d09245e | 2445 | Selector_Name => |
39ade2f9 AC |
2446 | New_Copy |
2447 | (Get_Discriminant_Value | |
2448 | (First_Discriminant (Rhs_Type), | |
2449 | Rhs_Type, | |
2450 | Stored_Constraint (Rhs_Type)))); | |
5d09245e AC |
2451 | |
2452 | else | |
39ade2f9 AC |
2453 | Rhs_Discr_Val := |
2454 | New_Copy | |
2455 | (Get_Discriminant_Value | |
2456 | (First_Discriminant (Rhs_Type), | |
2457 | Rhs_Type, | |
2458 | Stored_Constraint (Rhs_Type))); | |
5d09245e AC |
2459 | |
2460 | end if; | |
2461 | else | |
8aceda64 | 2462 | return |
5d09245e | 2463 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2464 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2465 | end if; |
2466 | ||
2467 | -- Call the TSS equality function with the inferred | |
2468 | -- discriminant values. | |
2469 | ||
2470 | return | |
2471 | Make_Function_Call (Loc, | |
2472 | Name => New_Reference_To (Eq_Op, Loc), | |
2473 | Parameter_Associations => New_List ( | |
2474 | Lhs, | |
2475 | Rhs, | |
2476 | Lhs_Discr_Val, | |
2477 | Rhs_Discr_Val)); | |
2478 | end; | |
d151d6a3 AC |
2479 | |
2480 | else | |
2481 | return | |
2482 | Make_Function_Call (Loc, | |
2483 | Name => New_Reference_To (Eq_Op, Loc), | |
2484 | Parameter_Associations => New_List (Lhs, Rhs)); | |
5d09245e | 2485 | end if; |
d151d6a3 | 2486 | end if; |
5d09245e | 2487 | |
dbe945f1 | 2488 | elsif Ada_Version >= Ada_2012 then |
5d09245e | 2489 | |
d151d6a3 | 2490 | -- if no TSS has been created for the type, check whether there is |
7efc3f2d | 2491 | -- a primitive equality declared for it. |
d151d6a3 AC |
2492 | |
2493 | declare | |
7efc3f2d | 2494 | Ada_2012_Op : constant Node_Id := Find_Primitive_Eq; |
d151d6a3 AC |
2495 | |
2496 | begin | |
7efc3f2d AC |
2497 | if Present (Ada_2012_Op) then |
2498 | return Ada_2012_Op; | |
2499 | else | |
72e9f2b9 | 2500 | |
7efc3f2d | 2501 | -- Use predefined equality if no user-defined primitive exists |
72e9f2b9 | 2502 | |
7efc3f2d AC |
2503 | return Make_Op_Eq (Loc, Lhs, Rhs); |
2504 | end if; | |
d151d6a3 AC |
2505 | end; |
2506 | ||
70482933 RK |
2507 | else |
2508 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2509 | end if; | |
2510 | ||
2511 | else | |
a3f2babd | 2512 | -- If not array or record type, it is predefined equality. |
70482933 RK |
2513 | |
2514 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
2515 | end if; | |
2516 | end Expand_Composite_Equality; | |
2517 | ||
fdac1f80 AC |
2518 | ------------------------ |
2519 | -- Expand_Concatenate -- | |
2520 | ------------------------ | |
70482933 | 2521 | |
fdac1f80 AC |
2522 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2523 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2524 | |
fdac1f80 AC |
2525 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2526 | -- Result type of concatenation | |
70482933 | 2527 | |
fdac1f80 AC |
2528 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2529 | -- Component type. Elements of this component type can appear as one | |
2530 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2531 | |
ecc4ddde AC |
2532 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2533 | -- Index subtype | |
2534 | ||
2535 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2536 | -- Index type. This is the base type of the index subtype, and is used | |
2537 | -- for all computed bounds (which may be out of range of Istyp in the | |
2538 | -- case of null ranges). | |
70482933 | 2539 | |
46ff89f3 | 2540 | Artyp : Entity_Id; |
fdac1f80 AC |
2541 | -- This is the type we use to do arithmetic to compute the bounds and |
2542 | -- lengths of operands. The choice of this type is a little subtle and | |
2543 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2544 | |
fdac1f80 AC |
2545 | Concatenation_Error : exception; |
2546 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2547 | |
0ac73189 AC |
2548 | Result_May_Be_Null : Boolean := True; |
2549 | -- Reset to False if at least one operand is encountered which is known | |
2550 | -- at compile time to be non-null. Used for handling the special case | |
2551 | -- of setting the high bound to the last operand high bound for a null | |
2552 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2553 | ||
df46b832 | 2554 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2555 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2556 | |
2557 | NN : Nat := 0; | |
a29262fd AC |
2558 | -- Number of operands excluding any known to be null, except that the |
2559 | -- last operand is always retained, in case it provides the bounds for | |
2560 | -- a null result. | |
2561 | ||
2562 | Opnd : Node_Id; | |
2563 | -- Current operand being processed in the loop through operands. After | |
2564 | -- this loop is complete, always contains the last operand (which is not | |
2565 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2566 | |
2567 | -- Arrays describing the operands, only the first NN entries of each | |
2568 | -- array are set (NN < N when we exclude known null operands). | |
2569 | ||
2570 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2571 | -- True if length of corresponding operand known at compile time | |
2572 | ||
2573 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2574 | -- Set to the corresponding entry in the Opnds list (but note that null |
2575 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2576 | |
2577 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2578 | -- Set to length of operand. Entries in this array are set only if the |
2579 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2580 | |
0ac73189 AC |
2581 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2582 | -- Set to lower bound of operand. Either an integer literal in the case | |
2583 | -- where the bound is known at compile time, else actual lower bound. | |
2584 | -- The operand low bound is of type Ityp. | |
2585 | ||
df46b832 AC |
2586 | Var_Length : array (1 .. N) of Entity_Id; |
2587 | -- Set to an entity of type Natural that contains the length of an | |
2588 | -- operand whose length is not known at compile time. Entries in this | |
2589 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2590 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2591 | |
2592 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2593 | -- The J'th entry in an expression node that represents the total length |
2594 | -- of operands 1 through J. It is either an integer literal node, or a | |
2595 | -- reference to a constant entity with the right value, so it is fine | |
2596 | -- to just do a Copy_Node to get an appropriate copy. The extra zero'th | |
46ff89f3 | 2597 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 AC |
2598 | |
2599 | Low_Bound : Node_Id; | |
0ac73189 AC |
2600 | -- A tree node representing the low bound of the result (of type Ityp). |
2601 | -- This is either an integer literal node, or an identifier reference to | |
2602 | -- a constant entity initialized to the appropriate value. | |
2603 | ||
a29262fd AC |
2604 | Last_Opnd_High_Bound : Node_Id; |
2605 | -- A tree node representing the high bound of the last operand. This | |
2606 | -- need only be set if the result could be null. It is used for the | |
2607 | -- special case of setting the right high bound for a null result. | |
2608 | -- This is of type Ityp. | |
2609 | ||
0ac73189 AC |
2610 | High_Bound : Node_Id; |
2611 | -- A tree node representing the high bound of the result (of type Ityp) | |
df46b832 AC |
2612 | |
2613 | Result : Node_Id; | |
0ac73189 | 2614 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2615 | |
d0f8d157 | 2616 | Actions : constant List_Id := New_List; |
4c9fe6c7 | 2617 | -- Collect actions to be inserted |
d0f8d157 | 2618 | |
fa969310 | 2619 | Known_Non_Null_Operand_Seen : Boolean; |
308e6f3a | 2620 | -- Set True during generation of the assignments of operands into |
fa969310 AC |
2621 | -- result once an operand known to be non-null has been seen. |
2622 | ||
2623 | function Make_Artyp_Literal (Val : Nat) return Node_Id; | |
2624 | -- This function makes an N_Integer_Literal node that is returned in | |
2625 | -- analyzed form with the type set to Artyp. Importantly this literal | |
2626 | -- is not flagged as static, so that if we do computations with it that | |
2627 | -- result in statically detected out of range conditions, we will not | |
2628 | -- generate error messages but instead warning messages. | |
2629 | ||
46ff89f3 | 2630 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 2631 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
2632 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
2633 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
2634 | |
2635 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 2636 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 2637 | |
fa969310 AC |
2638 | ------------------------ |
2639 | -- Make_Artyp_Literal -- | |
2640 | ------------------------ | |
2641 | ||
2642 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
2643 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
2644 | begin | |
2645 | Set_Etype (Result, Artyp); | |
2646 | Set_Analyzed (Result, True); | |
2647 | Set_Is_Static_Expression (Result, False); | |
2648 | return Result; | |
2649 | end Make_Artyp_Literal; | |
76c597a1 | 2650 | |
fdac1f80 | 2651 | -------------- |
46ff89f3 | 2652 | -- To_Artyp -- |
fdac1f80 AC |
2653 | -------------- |
2654 | ||
46ff89f3 | 2655 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 2656 | begin |
46ff89f3 | 2657 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
2658 | return X; |
2659 | ||
2660 | elsif Is_Enumeration_Type (Ityp) then | |
2661 | return | |
2662 | Make_Attribute_Reference (Loc, | |
2663 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2664 | Attribute_Name => Name_Pos, | |
2665 | Expressions => New_List (X)); | |
2666 | ||
2667 | else | |
46ff89f3 | 2668 | return Convert_To (Artyp, X); |
fdac1f80 | 2669 | end if; |
46ff89f3 | 2670 | end To_Artyp; |
fdac1f80 AC |
2671 | |
2672 | ------------- | |
2673 | -- To_Ityp -- | |
2674 | ------------- | |
2675 | ||
2676 | function To_Ityp (X : Node_Id) return Node_Id is | |
2677 | begin | |
2fc05e3d | 2678 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
2679 | return |
2680 | Make_Attribute_Reference (Loc, | |
2681 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2682 | Attribute_Name => Name_Val, | |
2683 | Expressions => New_List (X)); | |
2684 | ||
2685 | -- Case where we will do a type conversion | |
2686 | ||
2687 | else | |
76c597a1 AC |
2688 | if Ityp = Base_Type (Artyp) then |
2689 | return X; | |
fdac1f80 | 2690 | else |
76c597a1 | 2691 | return Convert_To (Ityp, X); |
fdac1f80 AC |
2692 | end if; |
2693 | end if; | |
2694 | end To_Ityp; | |
2695 | ||
2696 | -- Local Declarations | |
2697 | ||
0ac73189 AC |
2698 | Opnd_Typ : Entity_Id; |
2699 | Ent : Entity_Id; | |
2700 | Len : Uint; | |
2701 | J : Nat; | |
2702 | Clen : Node_Id; | |
2703 | Set : Boolean; | |
70482933 | 2704 | |
f46faa08 AC |
2705 | -- Start of processing for Expand_Concatenate |
2706 | ||
70482933 | 2707 | begin |
fdac1f80 AC |
2708 | -- Choose an appropriate computational type |
2709 | ||
2710 | -- We will be doing calculations of lengths and bounds in this routine | |
2711 | -- and computing one from the other in some cases, e.g. getting the high | |
2712 | -- bound by adding the length-1 to the low bound. | |
2713 | ||
2714 | -- We can't just use the index type, or even its base type for this | |
2715 | -- purpose for two reasons. First it might be an enumeration type which | |
308e6f3a RW |
2716 | -- is not suitable for computations of any kind, and second it may |
2717 | -- simply not have enough range. For example if the index type is | |
2718 | -- -128..+127 then lengths can be up to 256, which is out of range of | |
2719 | -- the type. | |
fdac1f80 AC |
2720 | |
2721 | -- For enumeration types, we can simply use Standard_Integer, this is | |
2722 | -- sufficient since the actual number of enumeration literals cannot | |
2723 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 2724 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
2725 | |
2726 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 2727 | Artyp := Standard_Integer; |
fdac1f80 | 2728 | |
59262ebb AC |
2729 | -- If index type is Positive, we use the standard unsigned type, to give |
2730 | -- more room on the top of the range, obviating the need for an overflow | |
2731 | -- check when creating the upper bound. This is needed to avoid junk | |
2732 | -- overflow checks in the common case of String types. | |
2733 | ||
2734 | -- ??? Disabled for now | |
2735 | ||
2736 | -- elsif Istyp = Standard_Positive then | |
2737 | -- Artyp := Standard_Unsigned; | |
2738 | ||
2fc05e3d AC |
2739 | -- For modular types, we use a 32-bit modular type for types whose size |
2740 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
2741 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 2742 | |
2fc05e3d | 2743 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 2744 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 2745 | Artyp := Standard_Unsigned; |
ecc4ddde | 2746 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 2747 | Artyp := Ityp; |
fdac1f80 | 2748 | else |
46ff89f3 | 2749 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
2750 | end if; |
2751 | ||
2fc05e3d | 2752 | -- Similar treatment for signed types |
fdac1f80 AC |
2753 | |
2754 | else | |
ecc4ddde | 2755 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 2756 | Artyp := Standard_Integer; |
ecc4ddde | 2757 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 2758 | Artyp := Ityp; |
fdac1f80 | 2759 | else |
46ff89f3 | 2760 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
2761 | end if; |
2762 | end if; | |
2763 | ||
fa969310 AC |
2764 | -- Supply dummy entry at start of length array |
2765 | ||
2766 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
2767 | ||
fdac1f80 | 2768 | -- Go through operands setting up the above arrays |
70482933 | 2769 | |
df46b832 AC |
2770 | J := 1; |
2771 | while J <= N loop | |
2772 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 2773 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
2774 | |
2775 | -- The parent got messed up when we put the operands in a list, | |
d347f572 AC |
2776 | -- so now put back the proper parent for the saved operand, that |
2777 | -- is to say the concatenation node, to make sure that each operand | |
2778 | -- is seen as a subexpression, e.g. if actions must be inserted. | |
fdac1f80 | 2779 | |
d347f572 | 2780 | Set_Parent (Opnd, Cnode); |
fdac1f80 AC |
2781 | |
2782 | -- Set will be True when we have setup one entry in the array | |
2783 | ||
df46b832 AC |
2784 | Set := False; |
2785 | ||
fdac1f80 | 2786 | -- Singleton element (or character literal) case |
df46b832 | 2787 | |
0ac73189 | 2788 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
2789 | NN := NN + 1; |
2790 | Operands (NN) := Opnd; | |
2791 | Is_Fixed_Length (NN) := True; | |
2792 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 2793 | Result_May_Be_Null := False; |
fdac1f80 | 2794 | |
a29262fd AC |
2795 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
2796 | -- since we know that the result cannot be null). | |
fdac1f80 | 2797 | |
0ac73189 AC |
2798 | Opnd_Low_Bound (NN) := |
2799 | Make_Attribute_Reference (Loc, | |
ecc4ddde | 2800 | Prefix => New_Reference_To (Istyp, Loc), |
0ac73189 AC |
2801 | Attribute_Name => Name_First); |
2802 | ||
df46b832 AC |
2803 | Set := True; |
2804 | ||
fdac1f80 | 2805 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
2806 | |
2807 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 2808 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 2809 | |
a29262fd AC |
2810 | if Len /= 0 then |
2811 | Result_May_Be_Null := False; | |
2812 | end if; | |
2813 | ||
2814 | -- Capture last operand high bound if result could be null | |
2815 | ||
2816 | if J = N and then Result_May_Be_Null then | |
2817 | Last_Opnd_High_Bound := | |
2818 | Make_Op_Add (Loc, | |
2819 | Left_Opnd => | |
2820 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 2821 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
2822 | end if; |
2823 | ||
2824 | -- Skip null string literal | |
fdac1f80 | 2825 | |
0ac73189 | 2826 | if J < N and then Len = 0 then |
df46b832 AC |
2827 | goto Continue; |
2828 | end if; | |
2829 | ||
2830 | NN := NN + 1; | |
2831 | Operands (NN) := Opnd; | |
2832 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
2833 | |
2834 | -- Set length and bounds | |
2835 | ||
df46b832 | 2836 | Fixed_Length (NN) := Len; |
0ac73189 AC |
2837 | |
2838 | Opnd_Low_Bound (NN) := | |
2839 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
2840 | ||
df46b832 AC |
2841 | Set := True; |
2842 | ||
2843 | -- All other cases | |
2844 | ||
2845 | else | |
2846 | -- Check constrained case with known bounds | |
2847 | ||
0ac73189 | 2848 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 2849 | declare |
df46b832 AC |
2850 | Index : constant Node_Id := First_Index (Opnd_Typ); |
2851 | Indx_Typ : constant Entity_Id := Etype (Index); | |
2852 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
2853 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
2854 | ||
2855 | begin | |
fdac1f80 AC |
2856 | -- Fixed length constrained array type with known at compile |
2857 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
2858 | |
2859 | if Compile_Time_Known_Value (Lo) | |
2860 | and then | |
2861 | Compile_Time_Known_Value (Hi) | |
2862 | then | |
2863 | declare | |
2864 | Loval : constant Uint := Expr_Value (Lo); | |
2865 | Hival : constant Uint := Expr_Value (Hi); | |
2866 | Len : constant Uint := | |
2867 | UI_Max (Hival - Loval + 1, Uint_0); | |
2868 | ||
2869 | begin | |
0ac73189 AC |
2870 | if Len > 0 then |
2871 | Result_May_Be_Null := False; | |
df46b832 | 2872 | end if; |
0ac73189 | 2873 | |
a29262fd AC |
2874 | -- Capture last operand bound if result could be null |
2875 | ||
2876 | if J = N and then Result_May_Be_Null then | |
2877 | Last_Opnd_High_Bound := | |
2878 | Convert_To (Ityp, | |
39ade2f9 | 2879 | Make_Integer_Literal (Loc, Expr_Value (Hi))); |
a29262fd AC |
2880 | end if; |
2881 | ||
2882 | -- Exclude null length case unless last operand | |
0ac73189 | 2883 | |
a29262fd | 2884 | if J < N and then Len = 0 then |
0ac73189 AC |
2885 | goto Continue; |
2886 | end if; | |
2887 | ||
2888 | NN := NN + 1; | |
2889 | Operands (NN) := Opnd; | |
2890 | Is_Fixed_Length (NN) := True; | |
2891 | Fixed_Length (NN) := Len; | |
2892 | ||
39ade2f9 AC |
2893 | Opnd_Low_Bound (NN) := |
2894 | To_Ityp | |
2895 | (Make_Integer_Literal (Loc, Expr_Value (Lo))); | |
0ac73189 | 2896 | Set := True; |
df46b832 AC |
2897 | end; |
2898 | end if; | |
2899 | end; | |
2900 | end if; | |
2901 | ||
0ac73189 AC |
2902 | -- All cases where the length is not known at compile time, or the |
2903 | -- special case of an operand which is known to be null but has a | |
2904 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
2905 | |
2906 | if not Set then | |
2907 | NN := NN + 1; | |
0ac73189 AC |
2908 | |
2909 | -- Capture operand bounds | |
2910 | ||
2911 | Opnd_Low_Bound (NN) := | |
2912 | Make_Attribute_Reference (Loc, | |
2913 | Prefix => | |
2914 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2915 | Attribute_Name => Name_First); | |
2916 | ||
a29262fd AC |
2917 | if J = N and Result_May_Be_Null then |
2918 | Last_Opnd_High_Bound := | |
2919 | Convert_To (Ityp, | |
2920 | Make_Attribute_Reference (Loc, | |
2921 | Prefix => | |
2922 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2923 | Attribute_Name => Name_Last)); | |
2924 | end if; | |
0ac73189 AC |
2925 | |
2926 | -- Capture length of operand in entity | |
2927 | ||
df46b832 AC |
2928 | Operands (NN) := Opnd; |
2929 | Is_Fixed_Length (NN) := False; | |
2930 | ||
191fcb3a | 2931 | Var_Length (NN) := Make_Temporary (Loc, 'L'); |
df46b832 | 2932 | |
d0f8d157 | 2933 | Append_To (Actions, |
df46b832 AC |
2934 | Make_Object_Declaration (Loc, |
2935 | Defining_Identifier => Var_Length (NN), | |
2936 | Constant_Present => True, | |
39ade2f9 | 2937 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2938 | Expression => |
2939 | Make_Attribute_Reference (Loc, | |
2940 | Prefix => | |
2941 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
d0f8d157 | 2942 | Attribute_Name => Name_Length))); |
df46b832 AC |
2943 | end if; |
2944 | end if; | |
2945 | ||
2946 | -- Set next entry in aggregate length array | |
2947 | ||
2948 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 2949 | -- or a reference to the saved length for variable length. |
df46b832 AC |
2950 | |
2951 | if NN = 1 then | |
2952 | if Is_Fixed_Length (1) then | |
39ade2f9 | 2953 | Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1)); |
df46b832 | 2954 | else |
39ade2f9 | 2955 | Aggr_Length (1) := New_Reference_To (Var_Length (1), Loc); |
df46b832 AC |
2956 | end if; |
2957 | ||
2958 | -- If entry is fixed length and only fixed lengths so far, make | |
2959 | -- appropriate new integer literal adding new length. | |
2960 | ||
2961 | elsif Is_Fixed_Length (NN) | |
2962 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
2963 | then | |
2964 | Aggr_Length (NN) := | |
2965 | Make_Integer_Literal (Loc, | |
2966 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
2967 | ||
d0f8d157 AC |
2968 | -- All other cases, construct an addition node for the length and |
2969 | -- create an entity initialized to this length. | |
df46b832 AC |
2970 | |
2971 | else | |
191fcb3a | 2972 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 AC |
2973 | |
2974 | if Is_Fixed_Length (NN) then | |
2975 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
2976 | else | |
2977 | Clen := New_Reference_To (Var_Length (NN), Loc); | |
2978 | end if; | |
2979 | ||
d0f8d157 | 2980 | Append_To (Actions, |
df46b832 AC |
2981 | Make_Object_Declaration (Loc, |
2982 | Defining_Identifier => Ent, | |
2983 | Constant_Present => True, | |
39ade2f9 | 2984 | Object_Definition => New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2985 | Expression => |
2986 | Make_Op_Add (Loc, | |
2987 | Left_Opnd => New_Copy (Aggr_Length (NN - 1)), | |
d0f8d157 | 2988 | Right_Opnd => Clen))); |
df46b832 | 2989 | |
76c597a1 | 2990 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
2991 | end if; |
2992 | ||
2993 | <<Continue>> | |
2994 | J := J + 1; | |
2995 | end loop; | |
2996 | ||
a29262fd | 2997 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
2998 | |
2999 | if NN = 0 then | |
a29262fd | 3000 | Result := Opnd; |
df46b832 AC |
3001 | goto Done; |
3002 | end if; | |
3003 | ||
3004 | -- If we have only one non-null operand, return it and we are done. | |
3005 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
3006 | -- the sole operand is of the element type, in which case it must be |
3007 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
3008 | -- through the normal general circuit. |
3009 | ||
3010 | if NN = 1 | |
fdac1f80 | 3011 | and then Base_Type (Etype (Operands (1))) /= Ctyp |
df46b832 AC |
3012 | then |
3013 | Result := Operands (1); | |
3014 | goto Done; | |
3015 | end if; | |
3016 | ||
3017 | -- Cases where we have a real concatenation | |
3018 | ||
fdac1f80 AC |
3019 | -- Next step is to find the low bound for the result array that we |
3020 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
3021 | ||
3022 | -- If the ultimate ancestor of the index subtype is a constrained array | |
3023 | -- definition, then the lower bound is that of the index subtype as | |
3024 | -- specified by (RM 4.5.3(6)). | |
3025 | ||
3026 | -- The right test here is to go to the root type, and then the ultimate | |
3027 | -- ancestor is the first subtype of this root type. | |
3028 | ||
3029 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 3030 | Low_Bound := |
fdac1f80 AC |
3031 | Make_Attribute_Reference (Loc, |
3032 | Prefix => | |
3033 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 3034 | Attribute_Name => Name_First); |
df46b832 AC |
3035 | |
3036 | -- If the first operand in the list has known length we know that | |
3037 | -- the lower bound of the result is the lower bound of this operand. | |
3038 | ||
fdac1f80 | 3039 | elsif Is_Fixed_Length (1) then |
0ac73189 | 3040 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
3041 | |
3042 | -- OK, we don't know the lower bound, we have to build a horrible | |
3043 | -- expression actions node of the form | |
3044 | ||
3045 | -- if Cond1'Length /= 0 then | |
0ac73189 | 3046 | -- Opnd1 low bound |
df46b832 AC |
3047 | -- else |
3048 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 3049 | -- Opnd2 low bound |
df46b832 AC |
3050 | -- else |
3051 | -- ... | |
3052 | ||
3053 | -- The nesting ends either when we hit an operand whose length is known | |
3054 | -- at compile time, or on reaching the last operand, whose low bound we | |
3055 | -- take unconditionally whether or not it is null. It's easiest to do | |
3056 | -- this with a recursive procedure: | |
3057 | ||
3058 | else | |
3059 | declare | |
3060 | function Get_Known_Bound (J : Nat) return Node_Id; | |
3061 | -- Returns the lower bound determined by operands J .. NN | |
3062 | ||
3063 | --------------------- | |
3064 | -- Get_Known_Bound -- | |
3065 | --------------------- | |
3066 | ||
3067 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 3068 | begin |
0ac73189 AC |
3069 | if Is_Fixed_Length (J) or else J = NN then |
3070 | return New_Copy (Opnd_Low_Bound (J)); | |
70482933 RK |
3071 | |
3072 | else | |
df46b832 AC |
3073 | return |
3074 | Make_Conditional_Expression (Loc, | |
3075 | Expressions => New_List ( | |
3076 | ||
3077 | Make_Op_Ne (Loc, | |
3078 | Left_Opnd => New_Reference_To (Var_Length (J), Loc), | |
3079 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
3080 | ||
0ac73189 | 3081 | New_Copy (Opnd_Low_Bound (J)), |
df46b832 | 3082 | Get_Known_Bound (J + 1))); |
70482933 | 3083 | end if; |
df46b832 | 3084 | end Get_Known_Bound; |
70482933 | 3085 | |
df46b832 | 3086 | begin |
191fcb3a | 3087 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 | 3088 | |
d0f8d157 | 3089 | Append_To (Actions, |
df46b832 AC |
3090 | Make_Object_Declaration (Loc, |
3091 | Defining_Identifier => Ent, | |
3092 | Constant_Present => True, | |
0ac73189 | 3093 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
d0f8d157 | 3094 | Expression => Get_Known_Bound (1))); |
df46b832 AC |
3095 | |
3096 | Low_Bound := New_Reference_To (Ent, Loc); | |
3097 | end; | |
3098 | end if; | |
70482933 | 3099 | |
76c597a1 AC |
3100 | -- Now we can safely compute the upper bound, normally |
3101 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
3102 | |
3103 | High_Bound := | |
3104 | To_Ityp ( | |
3105 | Make_Op_Add (Loc, | |
46ff89f3 | 3106 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
0ac73189 AC |
3107 | Right_Opnd => |
3108 | Make_Op_Subtract (Loc, | |
3109 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 3110 | Right_Opnd => Make_Artyp_Literal (1)))); |
0ac73189 | 3111 | |
59262ebb | 3112 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
3113 | -- very weird cases, so in the general case we need an overflow check on |
3114 | -- the high bound. We can avoid this for the common case of string types | |
3115 | -- and other types whose index is Positive, since we chose a wider range | |
3116 | -- for the arithmetic type. | |
76c597a1 | 3117 | |
59262ebb AC |
3118 | if Istyp /= Standard_Positive then |
3119 | Activate_Overflow_Check (High_Bound); | |
3120 | end if; | |
76c597a1 AC |
3121 | |
3122 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
3123 | -- case the bounds come from the last operand (so that we get the proper |
3124 | -- bounds if the last operand is super-flat). | |
3125 | ||
0ac73189 AC |
3126 | if Result_May_Be_Null then |
3127 | High_Bound := | |
3128 | Make_Conditional_Expression (Loc, | |
3129 | Expressions => New_List ( | |
3130 | Make_Op_Eq (Loc, | |
3131 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 3132 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 3133 | Last_Opnd_High_Bound, |
0ac73189 AC |
3134 | High_Bound)); |
3135 | end if; | |
3136 | ||
d0f8d157 AC |
3137 | -- Here is where we insert the saved up actions |
3138 | ||
3139 | Insert_Actions (Cnode, Actions, Suppress => All_Checks); | |
3140 | ||
602a7ec0 AC |
3141 | -- Now we construct an array object with appropriate bounds. We mark |
3142 | -- the target as internal to prevent useless initialization when | |
e526d0c7 AC |
3143 | -- Initialize_Scalars is enabled. Also since this is the actual result |
3144 | -- entity, we make sure we have debug information for the result. | |
70482933 | 3145 | |
191fcb3a | 3146 | Ent := Make_Temporary (Loc, 'S'); |
008f6fd3 | 3147 | Set_Is_Internal (Ent); |
e526d0c7 | 3148 | Set_Needs_Debug_Info (Ent); |
70482933 | 3149 | |
76c597a1 | 3150 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
3151 | -- to abort, we want a warning and a runtime constraint error. Note that |
3152 | -- we have arranged that the result will not be treated as a static | |
3153 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 3154 | |
df46b832 AC |
3155 | Insert_Action (Cnode, |
3156 | Make_Object_Declaration (Loc, | |
3157 | Defining_Identifier => Ent, | |
df46b832 AC |
3158 | Object_Definition => |
3159 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 3160 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
3161 | Constraint => |
3162 | Make_Index_Or_Discriminant_Constraint (Loc, | |
3163 | Constraints => New_List ( | |
3164 | Make_Range (Loc, | |
0ac73189 AC |
3165 | Low_Bound => Low_Bound, |
3166 | High_Bound => High_Bound))))), | |
df46b832 AC |
3167 | Suppress => All_Checks); |
3168 | ||
d1f453b7 RD |
3169 | -- If the result of the concatenation appears as the initializing |
3170 | -- expression of an object declaration, we can just rename the | |
3171 | -- result, rather than copying it. | |
3172 | ||
3173 | Set_OK_To_Rename (Ent); | |
3174 | ||
76c597a1 AC |
3175 | -- Catch the static out of range case now |
3176 | ||
3177 | if Raises_Constraint_Error (High_Bound) then | |
3178 | raise Concatenation_Error; | |
3179 | end if; | |
3180 | ||
df46b832 AC |
3181 | -- Now we will generate the assignments to do the actual concatenation |
3182 | ||
bded454f RD |
3183 | -- There is one case in which we will not do this, namely when all the |
3184 | -- following conditions are met: | |
3185 | ||
3186 | -- The result type is Standard.String | |
3187 | ||
3188 | -- There are nine or fewer retained (non-null) operands | |
3189 | ||
ffec8e81 | 3190 | -- The optimization level is -O0 |
bded454f RD |
3191 | |
3192 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
3193 | -- available in the run time. | |
3194 | ||
3195 | -- The debug flag gnatd.c is not set | |
3196 | ||
3197 | -- If all these conditions are met then we generate a call to the | |
3198 | -- relevant concatenation routine. The purpose of this is to avoid | |
3199 | -- undesirable code bloat at -O0. | |
3200 | ||
3201 | if Atyp = Standard_String | |
3202 | and then NN in 2 .. 9 | |
ffec8e81 | 3203 | and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC) |
bded454f RD |
3204 | and then not Debug_Flag_Dot_C |
3205 | then | |
3206 | declare | |
3207 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
3208 | (RE_Str_Concat_2, | |
3209 | RE_Str_Concat_3, | |
3210 | RE_Str_Concat_4, | |
3211 | RE_Str_Concat_5, | |
3212 | RE_Str_Concat_6, | |
3213 | RE_Str_Concat_7, | |
3214 | RE_Str_Concat_8, | |
3215 | RE_Str_Concat_9); | |
3216 | ||
3217 | begin | |
3218 | if RTE_Available (RR (NN)) then | |
3219 | declare | |
3220 | Opnds : constant List_Id := | |
3221 | New_List (New_Occurrence_Of (Ent, Loc)); | |
3222 | ||
3223 | begin | |
3224 | for J in 1 .. NN loop | |
3225 | if Is_List_Member (Operands (J)) then | |
3226 | Remove (Operands (J)); | |
3227 | end if; | |
3228 | ||
3229 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
3230 | Append_To (Opnds, | |
3231 | Make_Aggregate (Loc, | |
3232 | Component_Associations => New_List ( | |
3233 | Make_Component_Association (Loc, | |
3234 | Choices => New_List ( | |
3235 | Make_Integer_Literal (Loc, 1)), | |
3236 | Expression => Operands (J))))); | |
3237 | ||
3238 | else | |
3239 | Append_To (Opnds, Operands (J)); | |
3240 | end if; | |
3241 | end loop; | |
3242 | ||
3243 | Insert_Action (Cnode, | |
3244 | Make_Procedure_Call_Statement (Loc, | |
3245 | Name => New_Reference_To (RTE (RR (NN)), Loc), | |
3246 | Parameter_Associations => Opnds)); | |
3247 | ||
3248 | Result := New_Reference_To (Ent, Loc); | |
3249 | goto Done; | |
3250 | end; | |
3251 | end if; | |
3252 | end; | |
3253 | end if; | |
3254 | ||
3255 | -- Not special case so generate the assignments | |
3256 | ||
76c597a1 AC |
3257 | Known_Non_Null_Operand_Seen := False; |
3258 | ||
df46b832 AC |
3259 | for J in 1 .. NN loop |
3260 | declare | |
3261 | Lo : constant Node_Id := | |
3262 | Make_Op_Add (Loc, | |
46ff89f3 | 3263 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3264 | Right_Opnd => Aggr_Length (J - 1)); |
3265 | ||
3266 | Hi : constant Node_Id := | |
3267 | Make_Op_Add (Loc, | |
46ff89f3 | 3268 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3269 | Right_Opnd => |
3270 | Make_Op_Subtract (Loc, | |
3271 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 3272 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 3273 | |
df46b832 | 3274 | begin |
fdac1f80 AC |
3275 | -- Singleton case, simple assignment |
3276 | ||
3277 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 3278 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
3279 | Insert_Action (Cnode, |
3280 | Make_Assignment_Statement (Loc, | |
3281 | Name => | |
3282 | Make_Indexed_Component (Loc, | |
3283 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 3284 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
3285 | Expression => Operands (J)), |
3286 | Suppress => All_Checks); | |
70482933 | 3287 | |
76c597a1 AC |
3288 | -- Array case, slice assignment, skipped when argument is fixed |
3289 | -- length and known to be null. | |
fdac1f80 | 3290 | |
76c597a1 AC |
3291 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
3292 | declare | |
3293 | Assign : Node_Id := | |
3294 | Make_Assignment_Statement (Loc, | |
3295 | Name => | |
3296 | Make_Slice (Loc, | |
3297 | Prefix => | |
3298 | New_Occurrence_Of (Ent, Loc), | |
3299 | Discrete_Range => | |
3300 | Make_Range (Loc, | |
3301 | Low_Bound => To_Ityp (Lo), | |
3302 | High_Bound => To_Ityp (Hi))), | |
3303 | Expression => Operands (J)); | |
3304 | begin | |
3305 | if Is_Fixed_Length (J) then | |
3306 | Known_Non_Null_Operand_Seen := True; | |
3307 | ||
3308 | elsif not Known_Non_Null_Operand_Seen then | |
3309 | ||
3310 | -- Here if operand length is not statically known and no | |
3311 | -- operand known to be non-null has been processed yet. | |
3312 | -- If operand length is 0, we do not need to perform the | |
3313 | -- assignment, and we must avoid the evaluation of the | |
3314 | -- high bound of the slice, since it may underflow if the | |
3315 | -- low bound is Ityp'First. | |
3316 | ||
3317 | Assign := | |
3318 | Make_Implicit_If_Statement (Cnode, | |
39ade2f9 | 3319 | Condition => |
76c597a1 | 3320 | Make_Op_Ne (Loc, |
39ade2f9 | 3321 | Left_Opnd => |
76c597a1 AC |
3322 | New_Occurrence_Of (Var_Length (J), Loc), |
3323 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
39ade2f9 | 3324 | Then_Statements => New_List (Assign)); |
76c597a1 | 3325 | end if; |
fa969310 | 3326 | |
76c597a1 AC |
3327 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
3328 | end; | |
df46b832 AC |
3329 | end if; |
3330 | end; | |
3331 | end loop; | |
70482933 | 3332 | |
0ac73189 AC |
3333 | -- Finally we build the result, which is a reference to the array object |
3334 | ||
df46b832 | 3335 | Result := New_Reference_To (Ent, Loc); |
70482933 | 3336 | |
df46b832 AC |
3337 | <<Done>> |
3338 | Rewrite (Cnode, Result); | |
fdac1f80 AC |
3339 | Analyze_And_Resolve (Cnode, Atyp); |
3340 | ||
3341 | exception | |
3342 | when Concatenation_Error => | |
76c597a1 AC |
3343 | |
3344 | -- Kill warning generated for the declaration of the static out of | |
3345 | -- range high bound, and instead generate a Constraint_Error with | |
3346 | -- an appropriate specific message. | |
3347 | ||
3348 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
3349 | Apply_Compile_Time_Constraint_Error | |
3350 | (N => Cnode, | |
3351 | Msg => "concatenation result upper bound out of range?", | |
3352 | Reason => CE_Range_Check_Failed); | |
3353 | -- Set_Etype (Cnode, Atyp); | |
fdac1f80 | 3354 | end Expand_Concatenate; |
70482933 RK |
3355 | |
3356 | ------------------------ | |
3357 | -- Expand_N_Allocator -- | |
3358 | ------------------------ | |
3359 | ||
3360 | procedure Expand_N_Allocator (N : Node_Id) is | |
3361 | PtrT : constant Entity_Id := Etype (N); | |
d6a24cdb | 3362 | Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT)); |
f82944b7 | 3363 | Etyp : constant Entity_Id := Etype (Expression (N)); |
70482933 | 3364 | Loc : constant Source_Ptr := Sloc (N); |
f82944b7 | 3365 | Desig : Entity_Id; |
26bff3d9 | 3366 | Nod : Node_Id; |
ca5af305 AC |
3367 | Pool : Entity_Id; |
3368 | Temp : Entity_Id; | |
70482933 | 3369 | |
26bff3d9 JM |
3370 | procedure Rewrite_Coextension (N : Node_Id); |
3371 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 3372 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 3373 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 3374 | |
8aec446b | 3375 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd AC |
3376 | -- Given a constrained array type E, returns a node representing the |
3377 | -- code to compute the size in storage elements for the given type. | |
205c14b0 | 3378 | -- This is done without using the attribute (which malfunctions for |
507ed3fd | 3379 | -- large sizes ???) |
8aec446b | 3380 | |
26bff3d9 JM |
3381 | ------------------------- |
3382 | -- Rewrite_Coextension -- | |
3383 | ------------------------- | |
3384 | ||
3385 | procedure Rewrite_Coextension (N : Node_Id) is | |
df3e68b1 HK |
3386 | Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C'); |
3387 | Temp_Decl : Node_Id; | |
3388 | Insert_Nod : Node_Id; | |
26bff3d9 | 3389 | |
df3e68b1 | 3390 | begin |
26bff3d9 JM |
3391 | -- Generate: |
3392 | -- Cnn : aliased Etyp; | |
3393 | ||
df3e68b1 HK |
3394 | Temp_Decl := |
3395 | Make_Object_Declaration (Loc, | |
3396 | Defining_Identifier => Temp_Id, | |
243cae0a AC |
3397 | Aliased_Present => True, |
3398 | Object_Definition => New_Occurrence_Of (Etyp, Loc)); | |
26bff3d9 | 3399 | |
26bff3d9 | 3400 | if Nkind (Expression (N)) = N_Qualified_Expression then |
df3e68b1 | 3401 | Set_Expression (Temp_Decl, Expression (Expression (N))); |
0669bebe | 3402 | end if; |
26bff3d9 JM |
3403 | |
3404 | -- Find the proper insertion node for the declaration | |
3405 | ||
df3e68b1 HK |
3406 | Insert_Nod := Parent (N); |
3407 | while Present (Insert_Nod) loop | |
3408 | exit when | |
3409 | Nkind (Insert_Nod) in N_Statement_Other_Than_Procedure_Call | |
3410 | or else Nkind (Insert_Nod) = N_Procedure_Call_Statement | |
3411 | or else Nkind (Insert_Nod) in N_Declaration; | |
3412 | ||
3413 | Insert_Nod := Parent (Insert_Nod); | |
26bff3d9 JM |
3414 | end loop; |
3415 | ||
df3e68b1 HK |
3416 | Insert_Before (Insert_Nod, Temp_Decl); |
3417 | Analyze (Temp_Decl); | |
26bff3d9 JM |
3418 | |
3419 | Rewrite (N, | |
3420 | Make_Attribute_Reference (Loc, | |
243cae0a | 3421 | Prefix => New_Occurrence_Of (Temp_Id, Loc), |
26bff3d9 JM |
3422 | Attribute_Name => Name_Unrestricted_Access)); |
3423 | ||
3424 | Analyze_And_Resolve (N, PtrT); | |
3425 | end Rewrite_Coextension; | |
0669bebe | 3426 | |
8aec446b AC |
3427 | ------------------------------ |
3428 | -- Size_In_Storage_Elements -- | |
3429 | ------------------------------ | |
3430 | ||
3431 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
3432 | begin | |
3433 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
3434 | -- However, the reason for the existence of this function is | |
3435 | -- to construct a test for sizes too large, which means near the | |
3436 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
3437 | -- is that we get overflows when sizes are greater than 2**31. | |
3438 | ||
507ed3fd | 3439 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
3440 | |
3441 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
3442 | ||
46202729 | 3443 | -- which avoids this problem. All this is a bit bogus, but it does |
8aec446b AC |
3444 | -- mean we catch common cases of trying to allocate arrays that |
3445 | -- are too large, and which in the absence of a check results in | |
3446 | -- undetected chaos ??? | |
3447 | ||
507ed3fd AC |
3448 | declare |
3449 | Len : Node_Id; | |
3450 | Res : Node_Id; | |
8aec446b | 3451 | |
507ed3fd AC |
3452 | begin |
3453 | for J in 1 .. Number_Dimensions (E) loop | |
3454 | Len := | |
3455 | Make_Attribute_Reference (Loc, | |
3456 | Prefix => New_Occurrence_Of (E, Loc), | |
3457 | Attribute_Name => Name_Length, | |
243cae0a | 3458 | Expressions => New_List (Make_Integer_Literal (Loc, J))); |
8aec446b | 3459 | |
507ed3fd AC |
3460 | if J = 1 then |
3461 | Res := Len; | |
8aec446b | 3462 | |
507ed3fd AC |
3463 | else |
3464 | Res := | |
3465 | Make_Op_Multiply (Loc, | |
3466 | Left_Opnd => Res, | |
3467 | Right_Opnd => Len); | |
3468 | end if; | |
3469 | end loop; | |
8aec446b | 3470 | |
8aec446b | 3471 | return |
507ed3fd AC |
3472 | Make_Op_Multiply (Loc, |
3473 | Left_Opnd => Len, | |
3474 | Right_Opnd => | |
3475 | Make_Attribute_Reference (Loc, | |
3476 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
3477 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
3478 | end; | |
8aec446b AC |
3479 | end Size_In_Storage_Elements; |
3480 | ||
0669bebe GB |
3481 | -- Start of processing for Expand_N_Allocator |
3482 | ||
70482933 RK |
3483 | begin |
3484 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
3485 | -- shall not be a remote access-to-class-wide-limited-private type | |
3486 | ||
3487 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
3488 | ||
3489 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
3490 | ||
ca5af305 AC |
3491 | -- Processing for anonymous access-to-controlled types. These access |
3492 | -- types receive a special finalization master which appears in the | |
3493 | -- declarations of the enclosing semantic unit. This expansion is done | |
3494 | -- now to ensure that any additional types generated by this routine | |
3495 | -- or Expand_Allocator_Expression inherit the proper type attributes. | |
3496 | ||
3497 | if Ekind (PtrT) = E_Anonymous_Access_Type | |
3498 | and then Needs_Finalization (Dtyp) | |
3499 | then | |
b254da66 AC |
3500 | -- Anonymous access-to-controlled types allocate on the global pool. |
3501 | -- Do not set this attribute on .NET/JVM since those targets do not | |
3502 | -- support pools. | |
ca5af305 | 3503 | |
b254da66 AC |
3504 | if No (Associated_Storage_Pool (PtrT)) |
3505 | and then VM_Target = No_VM | |
3506 | then | |
11fa950b AC |
3507 | Set_Associated_Storage_Pool |
3508 | (PtrT, Get_Global_Pool_For_Access_Type (PtrT)); | |
ca5af305 AC |
3509 | end if; |
3510 | ||
3511 | -- The finalization master must be inserted and analyzed as part of | |
2bfa5484 | 3512 | -- the current semantic unit. This form of expansion is not carried |
ad5a445d HK |
3513 | -- out in Alfa mode because it is useless. Note that the master is |
3514 | -- updated when analysis changes current units. | |
ca5af305 | 3515 | |
ad5a445d | 3516 | if not Alfa_Mode then |
11fa950b | 3517 | Set_Finalization_Master (PtrT, Current_Anonymous_Master); |
ca5af305 AC |
3518 | end if; |
3519 | end if; | |
3520 | ||
3521 | -- Set the storage pool and find the appropriate version of Allocate to | |
8417f4b2 AC |
3522 | -- call. Do not overwrite the storage pool if it is already set, which |
3523 | -- can happen for build-in-place function returns (see | |
200b7162 | 3524 | -- Exp_Ch4.Expand_N_Extended_Return_Statement). |
70482933 | 3525 | |
200b7162 BD |
3526 | if No (Storage_Pool (N)) then |
3527 | Pool := Associated_Storage_Pool (Root_Type (PtrT)); | |
70482933 | 3528 | |
200b7162 BD |
3529 | if Present (Pool) then |
3530 | Set_Storage_Pool (N, Pool); | |
fbf5a39b | 3531 | |
200b7162 BD |
3532 | if Is_RTE (Pool, RE_SS_Pool) then |
3533 | if VM_Target = No_VM then | |
3534 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); | |
3535 | end if; | |
fbf5a39b | 3536 | |
200b7162 BD |
3537 | elsif Is_Class_Wide_Type (Etype (Pool)) then |
3538 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
3539 | ||
3540 | else | |
3541 | Set_Procedure_To_Call (N, | |
3542 | Find_Prim_Op (Etype (Pool), Name_Allocate)); | |
3543 | end if; | |
70482933 RK |
3544 | end if; |
3545 | end if; | |
3546 | ||
685094bf RD |
3547 | -- Under certain circumstances we can replace an allocator by an access |
3548 | -- to statically allocated storage. The conditions, as noted in AARM | |
3549 | -- 3.10 (10c) are as follows: | |
70482933 RK |
3550 | |
3551 | -- Size and initial value is known at compile time | |
3552 | -- Access type is access-to-constant | |
3553 | ||
fbf5a39b AC |
3554 | -- The allocator is not part of a constraint on a record component, |
3555 | -- because in that case the inserted actions are delayed until the | |
3556 | -- record declaration is fully analyzed, which is too late for the | |
3557 | -- analysis of the rewritten allocator. | |
3558 | ||
70482933 RK |
3559 | if Is_Access_Constant (PtrT) |
3560 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
3561 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
243cae0a AC |
3562 | and then Size_Known_At_Compile_Time |
3563 | (Etype (Expression (Expression (N)))) | |
fbf5a39b | 3564 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
3565 | then |
3566 | -- Here we can do the optimization. For the allocator | |
3567 | ||
3568 | -- new x'(y) | |
3569 | ||
3570 | -- We insert an object declaration | |
3571 | ||
3572 | -- Tnn : aliased x := y; | |
3573 | ||
685094bf RD |
3574 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
3575 | -- marked as requiring static allocation. | |
70482933 | 3576 | |
df3e68b1 | 3577 | Temp := Make_Temporary (Loc, 'T', Expression (Expression (N))); |
70482933 RK |
3578 | Desig := Subtype_Mark (Expression (N)); |
3579 | ||
3580 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 3581 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
3582 | -- unconstrained subtype. |
3583 | ||
0da2c8ac AC |
3584 | if Entity (Desig) = Base_Type (Dtyp) then |
3585 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
3586 | end if; |
3587 | ||
3588 | Insert_Action (N, | |
3589 | Make_Object_Declaration (Loc, | |
3590 | Defining_Identifier => Temp, | |
3591 | Aliased_Present => True, | |
3592 | Constant_Present => Is_Access_Constant (PtrT), | |
3593 | Object_Definition => Desig, | |
3594 | Expression => Expression (Expression (N)))); | |
3595 | ||
3596 | Rewrite (N, | |
3597 | Make_Attribute_Reference (Loc, | |
243cae0a | 3598 | Prefix => New_Occurrence_Of (Temp, Loc), |
70482933 RK |
3599 | Attribute_Name => Name_Unrestricted_Access)); |
3600 | ||
3601 | Analyze_And_Resolve (N, PtrT); | |
3602 | ||
685094bf RD |
3603 | -- We set the variable as statically allocated, since we don't want |
3604 | -- it going on the stack of the current procedure! | |
70482933 RK |
3605 | |
3606 | Set_Is_Statically_Allocated (Temp); | |
3607 | return; | |
3608 | end if; | |
3609 | ||
0669bebe GB |
3610 | -- Same if the allocator is an access discriminant for a local object: |
3611 | -- instead of an allocator we create a local value and constrain the | |
308e6f3a | 3612 | -- enclosing object with the corresponding access attribute. |
0669bebe | 3613 | |
26bff3d9 JM |
3614 | if Is_Static_Coextension (N) then |
3615 | Rewrite_Coextension (N); | |
0669bebe GB |
3616 | return; |
3617 | end if; | |
3618 | ||
8aec446b AC |
3619 | -- Check for size too large, we do this because the back end misses |
3620 | -- proper checks here and can generate rubbish allocation calls when | |
3621 | -- we are near the limit. We only do this for the 32-bit address case | |
3622 | -- since that is from a practical point of view where we see a problem. | |
3623 | ||
3624 | if System_Address_Size = 32 | |
3625 | and then not Storage_Checks_Suppressed (PtrT) | |
3626 | and then not Storage_Checks_Suppressed (Dtyp) | |
3627 | and then not Storage_Checks_Suppressed (Etyp) | |
3628 | then | |
3629 | -- The check we want to generate should look like | |
3630 | ||
3631 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
3632 | -- raise Storage_Error; | |
3633 | -- end if; | |
3634 | ||
308e6f3a | 3635 | -- where 3.5 gigabytes is a constant large enough to accommodate any |
507ed3fd AC |
3636 | -- reasonable request for. But we can't do it this way because at |
3637 | -- least at the moment we don't compute this attribute right, and | |
3638 | -- can silently give wrong results when the result gets large. Since | |
3639 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 3640 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 3641 | -- value of the attribute ??? |
8aec446b | 3642 | |
507ed3fd AC |
3643 | if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then |
3644 | Insert_Action (N, | |
3645 | Make_Raise_Storage_Error (Loc, | |
3646 | Condition => | |
3647 | Make_Op_Gt (Loc, | |
3648 | Left_Opnd => Size_In_Storage_Elements (Etyp), | |
3649 | Right_Opnd => | |
243cae0a | 3650 | Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))), |
507ed3fd AC |
3651 | Reason => SE_Object_Too_Large)); |
3652 | end if; | |
8aec446b AC |
3653 | end if; |
3654 | ||
0da2c8ac | 3655 | -- Handle case of qualified expression (other than optimization above) |
cac5a801 AC |
3656 | -- First apply constraint checks, because the bounds or discriminants |
3657 | -- in the aggregate might not match the subtype mark in the allocator. | |
0da2c8ac | 3658 | |
70482933 | 3659 | if Nkind (Expression (N)) = N_Qualified_Expression then |
cac5a801 AC |
3660 | Apply_Constraint_Check |
3661 | (Expression (Expression (N)), Etype (Expression (N))); | |
3662 | ||
fbf5a39b | 3663 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
3664 | return; |
3665 | end if; | |
fbf5a39b | 3666 | |
26bff3d9 JM |
3667 | -- If the allocator is for a type which requires initialization, and |
3668 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
3669 | -- rather than a qualified expression), then we must generate a call to |
3670 | -- the initialization routine using an expressions action node: | |
70482933 | 3671 | |
26bff3d9 | 3672 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 3673 | |
26bff3d9 JM |
3674 | -- Here ptr_T is the pointer type for the allocator, and T is the |
3675 | -- subtype of the allocator. A special case arises if the designated | |
3676 | -- type of the access type is a task or contains tasks. In this case | |
3677 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
3678 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
3679 | -- for details). In addition, if the type T is a task T, then the | |
3680 | -- first argument to Init must be converted to the task record type. | |
70482933 | 3681 | |
26bff3d9 | 3682 | declare |
df3e68b1 HK |
3683 | T : constant Entity_Id := Entity (Expression (N)); |
3684 | Args : List_Id; | |
3685 | Decls : List_Id; | |
3686 | Decl : Node_Id; | |
3687 | Discr : Elmt_Id; | |
3688 | Init : Entity_Id; | |
3689 | Init_Arg1 : Node_Id; | |
3690 | Temp_Decl : Node_Id; | |
3691 | Temp_Type : Entity_Id; | |
70482933 | 3692 | |
26bff3d9 JM |
3693 | begin |
3694 | if No_Initialization (N) then | |
df3e68b1 HK |
3695 | |
3696 | -- Even though this might be a simple allocation, create a custom | |
deb8dacc HK |
3697 | -- Allocate if the context requires it. Since .NET/JVM compilers |
3698 | -- do not support pools, this step is skipped. | |
df3e68b1 | 3699 | |
deb8dacc | 3700 | if VM_Target = No_VM |
d3f70b35 | 3701 | and then Present (Finalization_Master (PtrT)) |
deb8dacc | 3702 | then |
df3e68b1 | 3703 | Build_Allocate_Deallocate_Proc |
ca5af305 | 3704 | (N => N, |
df3e68b1 HK |
3705 | Is_Allocate => True); |
3706 | end if; | |
70482933 | 3707 | |
26bff3d9 | 3708 | -- Case of no initialization procedure present |
70482933 | 3709 | |
26bff3d9 | 3710 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 3711 | |
26bff3d9 | 3712 | -- Case of simple initialization required |
70482933 | 3713 | |
26bff3d9 | 3714 | if Needs_Simple_Initialization (T) then |
b4592168 | 3715 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
3716 | Rewrite (Expression (N), |
3717 | Make_Qualified_Expression (Loc, | |
3718 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 3719 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 3720 | |
26bff3d9 JM |
3721 | Analyze_And_Resolve (Expression (Expression (N)), T); |
3722 | Analyze_And_Resolve (Expression (N), T); | |
3723 | Set_Paren_Count (Expression (Expression (N)), 1); | |
3724 | Expand_N_Allocator (N); | |
70482933 | 3725 | |
26bff3d9 | 3726 | -- No initialization required |
70482933 RK |
3727 | |
3728 | else | |
26bff3d9 JM |
3729 | null; |
3730 | end if; | |
70482933 | 3731 | |
26bff3d9 | 3732 | -- Case of initialization procedure present, must be called |
70482933 | 3733 | |
26bff3d9 | 3734 | else |
b4592168 | 3735 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 3736 | |
b4592168 GD |
3737 | if not Restriction_Active (No_Default_Initialization) then |
3738 | Init := Base_Init_Proc (T); | |
3739 | Nod := N; | |
191fcb3a | 3740 | Temp := Make_Temporary (Loc, 'P'); |
70482933 | 3741 | |
b4592168 | 3742 | -- Construct argument list for the initialization routine call |
70482933 | 3743 | |
df3e68b1 | 3744 | Init_Arg1 := |
b4592168 | 3745 | Make_Explicit_Dereference (Loc, |
df3e68b1 HK |
3746 | Prefix => |
3747 | New_Reference_To (Temp, Loc)); | |
3748 | ||
3749 | Set_Assignment_OK (Init_Arg1); | |
b4592168 | 3750 | Temp_Type := PtrT; |
26bff3d9 | 3751 | |
b4592168 GD |
3752 | -- The initialization procedure expects a specific type. if the |
3753 | -- context is access to class wide, indicate that the object | |
3754 | -- being allocated has the right specific type. | |
70482933 | 3755 | |
b4592168 | 3756 | if Is_Class_Wide_Type (Dtyp) then |
df3e68b1 | 3757 | Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1); |
b4592168 | 3758 | end if; |
70482933 | 3759 | |
b4592168 GD |
3760 | -- If designated type is a concurrent type or if it is private |
3761 | -- type whose definition is a concurrent type, the first | |
3762 | -- argument in the Init routine has to be unchecked conversion | |
3763 | -- to the corresponding record type. If the designated type is | |
243cae0a | 3764 | -- a derived type, also convert the argument to its root type. |
20b5d666 | 3765 | |
b4592168 | 3766 | if Is_Concurrent_Type (T) then |
df3e68b1 HK |
3767 | Init_Arg1 := |
3768 | Unchecked_Convert_To ( | |
3769 | Corresponding_Record_Type (T), Init_Arg1); | |
70482933 | 3770 | |
b4592168 GD |
3771 | elsif Is_Private_Type (T) |
3772 | and then Present (Full_View (T)) | |
3773 | and then Is_Concurrent_Type (Full_View (T)) | |
3774 | then | |
df3e68b1 | 3775 | Init_Arg1 := |
b4592168 | 3776 | Unchecked_Convert_To |
df3e68b1 | 3777 | (Corresponding_Record_Type (Full_View (T)), Init_Arg1); |
70482933 | 3778 | |
b4592168 GD |
3779 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
3780 | declare | |
3781 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
df3e68b1 | 3782 | |
b4592168 | 3783 | begin |
df3e68b1 HK |
3784 | Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1); |
3785 | Set_Etype (Init_Arg1, Ftyp); | |
b4592168 GD |
3786 | end; |
3787 | end if; | |
70482933 | 3788 | |
df3e68b1 | 3789 | Args := New_List (Init_Arg1); |
70482933 | 3790 | |
b4592168 GD |
3791 | -- For the task case, pass the Master_Id of the access type as |
3792 | -- the value of the _Master parameter, and _Chain as the value | |
3793 | -- of the _Chain parameter (_Chain will be defined as part of | |
3794 | -- the generated code for the allocator). | |
70482933 | 3795 | |
b4592168 GD |
3796 | -- In Ada 2005, the context may be a function that returns an |
3797 | -- anonymous access type. In that case the Master_Id has been | |
3798 | -- created when expanding the function declaration. | |
70482933 | 3799 | |
b4592168 GD |
3800 | if Has_Task (T) then |
3801 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 3802 | |
b4592168 GD |
3803 | -- The designated type was an incomplete type, and the |
3804 | -- access type did not get expanded. Salvage it now. | |
70482933 | 3805 | |
b941ae65 AC |
3806 | if not Restriction_Active (No_Task_Hierarchy) then |
3807 | pragma Assert (Present (Parent (Base_Type (PtrT)))); | |
3808 | Expand_N_Full_Type_Declaration | |
3809 | (Parent (Base_Type (PtrT))); | |
3810 | end if; | |
b4592168 | 3811 | end if; |
70482933 | 3812 | |
b4592168 GD |
3813 | -- If the context of the allocator is a declaration or an |
3814 | -- assignment, we can generate a meaningful image for it, | |
3815 | -- even though subsequent assignments might remove the | |
3816 | -- connection between task and entity. We build this image | |
3817 | -- when the left-hand side is a simple variable, a simple | |
3818 | -- indexed assignment or a simple selected component. | |
3819 | ||
3820 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
3821 | declare | |
3822 | Nam : constant Node_Id := Name (Parent (N)); | |
3823 | ||
3824 | begin | |
3825 | if Is_Entity_Name (Nam) then | |
3826 | Decls := | |
3827 | Build_Task_Image_Decls | |
3828 | (Loc, | |
3829 | New_Occurrence_Of | |
3830 | (Entity (Nam), Sloc (Nam)), T); | |
3831 | ||
243cae0a AC |
3832 | elsif Nkind_In (Nam, N_Indexed_Component, |
3833 | N_Selected_Component) | |
b4592168 GD |
3834 | and then Is_Entity_Name (Prefix (Nam)) |
3835 | then | |
3836 | Decls := | |
3837 | Build_Task_Image_Decls | |
3838 | (Loc, Nam, Etype (Prefix (Nam))); | |
3839 | else | |
3840 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3841 | end if; | |
3842 | end; | |
70482933 | 3843 | |
b4592168 GD |
3844 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
3845 | Decls := | |
3846 | Build_Task_Image_Decls | |
3847 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 3848 | |
b4592168 GD |
3849 | else |
3850 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3851 | end if; | |
26bff3d9 | 3852 | |
87dc09cb | 3853 | if Restriction_Active (No_Task_Hierarchy) then |
3c1ecd7e AC |
3854 | Append_To (Args, |
3855 | New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc)); | |
87dc09cb AC |
3856 | else |
3857 | Append_To (Args, | |
3858 | New_Reference_To | |
3859 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); | |
3860 | end if; | |
3861 | ||
b4592168 | 3862 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); |
26bff3d9 | 3863 | |
b4592168 GD |
3864 | Decl := Last (Decls); |
3865 | Append_To (Args, | |
3866 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 3867 | |
87dc09cb | 3868 | -- Has_Task is false, Decls not used |
26bff3d9 | 3869 | |
b4592168 GD |
3870 | else |
3871 | Decls := No_List; | |
26bff3d9 JM |
3872 | end if; |
3873 | ||
b4592168 GD |
3874 | -- Add discriminants if discriminated type |
3875 | ||
3876 | declare | |
3877 | Dis : Boolean := False; | |
3878 | Typ : Entity_Id; | |
3879 | ||
3880 | begin | |
3881 | if Has_Discriminants (T) then | |
3882 | Dis := True; | |
3883 | Typ := T; | |
3884 | ||
3885 | elsif Is_Private_Type (T) | |
3886 | and then Present (Full_View (T)) | |
3887 | and then Has_Discriminants (Full_View (T)) | |
20b5d666 | 3888 | then |
b4592168 GD |
3889 | Dis := True; |
3890 | Typ := Full_View (T); | |
20b5d666 | 3891 | end if; |
70482933 | 3892 | |
b4592168 | 3893 | if Dis then |
26bff3d9 | 3894 | |
b4592168 | 3895 | -- If the allocated object will be constrained by the |
685094bf RD |
3896 | -- default values for discriminants, then build a subtype |
3897 | -- with those defaults, and change the allocated subtype | |
3898 | -- to that. Note that this happens in fewer cases in Ada | |
3899 | -- 2005 (AI-363). | |
26bff3d9 | 3900 | |
b4592168 GD |
3901 | if not Is_Constrained (Typ) |
3902 | and then Present (Discriminant_Default_Value | |
df3e68b1 | 3903 | (First_Discriminant (Typ))) |
0791fbe9 | 3904 | and then (Ada_Version < Ada_2005 |
cc96a1b8 AC |
3905 | or else not |
3906 | Effectively_Has_Constrained_Partial_View | |
414b312e AC |
3907 | (Typ => Typ, |
3908 | Scop => Current_Scope)) | |
20b5d666 | 3909 | then |
b4592168 GD |
3910 | Typ := Build_Default_Subtype (Typ, N); |
3911 | Set_Expression (N, New_Reference_To (Typ, Loc)); | |
20b5d666 JM |
3912 | end if; |
3913 | ||
b4592168 GD |
3914 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
3915 | while Present (Discr) loop | |
3916 | Nod := Node (Discr); | |
3917 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 3918 | |
b4592168 GD |
3919 | -- AI-416: when the discriminant constraint is an |
3920 | -- anonymous access type make sure an accessibility | |
3921 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 3922 | |
0791fbe9 | 3923 | if Ada_Version >= Ada_2005 |
b4592168 GD |
3924 | and then |
3925 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
3926 | then | |
e84e11ba GD |
3927 | Apply_Accessibility_Check |
3928 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 3929 | end if; |
20b5d666 | 3930 | |
b4592168 GD |
3931 | Next_Elmt (Discr); |
3932 | end loop; | |
3933 | end if; | |
3934 | end; | |
70482933 | 3935 | |
b4592168 GD |
3936 | -- We set the allocator as analyzed so that when we analyze the |
3937 | -- expression actions node, we do not get an unwanted recursive | |
3938 | -- expansion of the allocator expression. | |
70482933 | 3939 | |
b4592168 GD |
3940 | Set_Analyzed (N, True); |
3941 | Nod := Relocate_Node (N); | |
70482933 | 3942 | |
b4592168 | 3943 | -- Here is the transformation: |
ca5af305 AC |
3944 | -- input: new Ctrl_Typ |
3945 | -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ; | |
3946 | -- Ctrl_TypIP (Temp.all, ...); | |
3947 | -- [Deep_]Initialize (Temp.all); | |
70482933 | 3948 | |
ca5af305 AC |
3949 | -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and |
3950 | -- is the subtype of the allocator. | |
70482933 | 3951 | |
b4592168 GD |
3952 | Temp_Decl := |
3953 | Make_Object_Declaration (Loc, | |
3954 | Defining_Identifier => Temp, | |
3955 | Constant_Present => True, | |
3956 | Object_Definition => New_Reference_To (Temp_Type, Loc), | |
3957 | Expression => Nod); | |
70482933 | 3958 | |
b4592168 GD |
3959 | Set_Assignment_OK (Temp_Decl); |
3960 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 3961 | |
ca5af305 | 3962 | Build_Allocate_Deallocate_Proc (Temp_Decl, True); |
df3e68b1 | 3963 | |
b4592168 GD |
3964 | -- If the designated type is a task type or contains tasks, |
3965 | -- create block to activate created tasks, and insert | |
3966 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 3967 | |
b4592168 GD |
3968 | if Has_Task (T) then |
3969 | declare | |
3970 | L : constant List_Id := New_List; | |
3971 | Blk : Node_Id; | |
3972 | begin | |
3973 | Build_Task_Allocate_Block (L, Nod, Args); | |
3974 | Blk := Last (L); | |
3975 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
3976 | Insert_Actions (N, L); | |
3977 | end; | |
70482933 | 3978 | |
b4592168 GD |
3979 | else |
3980 | Insert_Action (N, | |
3981 | Make_Procedure_Call_Statement (Loc, | |
243cae0a | 3982 | Name => New_Reference_To (Init, Loc), |
b4592168 GD |
3983 | Parameter_Associations => Args)); |
3984 | end if; | |
70482933 | 3985 | |
048e5cef | 3986 | if Needs_Finalization (T) then |
70482933 | 3987 | |
df3e68b1 HK |
3988 | -- Generate: |
3989 | -- [Deep_]Initialize (Init_Arg1); | |
70482933 | 3990 | |
df3e68b1 | 3991 | Insert_Action (N, |
243cae0a AC |
3992 | Make_Init_Call |
3993 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
3994 | Typ => T)); | |
b4592168 | 3995 | |
b254da66 | 3996 | if Present (Finalization_Master (PtrT)) then |
deb8dacc | 3997 | |
b254da66 AC |
3998 | -- Special processing for .NET/JVM, the allocated object |
3999 | -- is attached to the finalization master. Generate: | |
deb8dacc | 4000 | |
b254da66 | 4001 | -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1)); |
deb8dacc | 4002 | |
b254da66 AC |
4003 | -- Types derived from [Limited_]Controlled are the only |
4004 | -- ones considered since they have fields Prev and Next. | |
4005 | ||
e0c32166 AC |
4006 | if VM_Target /= No_VM then |
4007 | if Is_Controlled (T) then | |
4008 | Insert_Action (N, | |
4009 | Make_Attach_Call | |
4010 | (Obj_Ref => New_Copy_Tree (Init_Arg1), | |
4011 | Ptr_Typ => PtrT)); | |
4012 | end if; | |
b254da66 AC |
4013 | |
4014 | -- Default case, generate: | |
4015 | ||
4016 | -- Set_Finalize_Address | |
4017 | -- (<PtrT>FM, <T>FD'Unrestricted_Access); | |
4018 | ||
2bfa5484 HK |
4019 | -- Do not generate this call in the following cases: |
4020 | -- | |
4021 | -- * Alfa mode - the call is useless and results in | |
4022 | -- unwanted expansion. | |
4023 | -- | |
4024 | -- * CodePeer mode - TSS primitive Finalize_Address is | |
4025 | -- not created in this mode. | |
b254da66 | 4026 | |
2bfa5484 HK |
4027 | elsif not Alfa_Mode |
4028 | and then not CodePeer_Mode | |
4029 | then | |
b254da66 AC |
4030 | Insert_Action (N, |
4031 | Make_Set_Finalize_Address_Call | |
4032 | (Loc => Loc, | |
4033 | Typ => T, | |
4034 | Ptr_Typ => PtrT)); | |
4035 | end if; | |
b4592168 | 4036 | end if; |
70482933 RK |
4037 | end if; |
4038 | ||
b4592168 GD |
4039 | Rewrite (N, New_Reference_To (Temp, Loc)); |
4040 | Analyze_And_Resolve (N, PtrT); | |
4041 | end if; | |
26bff3d9 JM |
4042 | end if; |
4043 | end; | |
f82944b7 | 4044 | |
26bff3d9 JM |
4045 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
4046 | -- object that has been rewritten as a reference, we displace "this" | |
4047 | -- to reference properly its secondary dispatch table. | |
4048 | ||
4049 | if Nkind (N) = N_Identifier | |
f82944b7 JM |
4050 | and then Is_Interface (Dtyp) |
4051 | then | |
26bff3d9 | 4052 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
4053 | end if; |
4054 | ||
fbf5a39b AC |
4055 | exception |
4056 | when RE_Not_Available => | |
4057 | return; | |
70482933 RK |
4058 | end Expand_N_Allocator; |
4059 | ||
4060 | ----------------------- | |
4061 | -- Expand_N_And_Then -- | |
4062 | ----------------------- | |
4063 | ||
5875f8d6 AC |
4064 | procedure Expand_N_And_Then (N : Node_Id) |
4065 | renames Expand_Short_Circuit_Operator; | |
70482933 | 4066 | |
19d846a0 RD |
4067 | ------------------------------ |
4068 | -- Expand_N_Case_Expression -- | |
4069 | ------------------------------ | |
4070 | ||
4071 | procedure Expand_N_Case_Expression (N : Node_Id) is | |
4072 | Loc : constant Source_Ptr := Sloc (N); | |
4073 | Typ : constant Entity_Id := Etype (N); | |
4074 | Cstmt : Node_Id; | |
4075 | Tnn : Entity_Id; | |
4076 | Pnn : Entity_Id; | |
4077 | Actions : List_Id; | |
4078 | Ttyp : Entity_Id; | |
4079 | Alt : Node_Id; | |
4080 | Fexp : Node_Id; | |
4081 | ||
4082 | begin | |
4083 | -- We expand | |
4084 | ||
4085 | -- case X is when A => AX, when B => BX ... | |
4086 | ||
4087 | -- to | |
4088 | ||
4089 | -- do | |
4090 | -- Tnn : typ; | |
4091 | -- case X is | |
4092 | -- when A => | |
4093 | -- Tnn := AX; | |
4094 | -- when B => | |
4095 | -- Tnn := BX; | |
4096 | -- ... | |
4097 | -- end case; | |
4098 | -- in Tnn end; | |
4099 | ||
4100 | -- However, this expansion is wrong for limited types, and also | |
4101 | -- wrong for unconstrained types (since the bounds may not be the | |
4102 | -- same in all branches). Furthermore it involves an extra copy | |
4103 | -- for large objects. So we take care of this by using the following | |
4104 | -- modified expansion for non-scalar types: | |
4105 | ||
4106 | -- do | |
4107 | -- type Pnn is access all typ; | |
4108 | -- Tnn : Pnn; | |
4109 | -- case X is | |
4110 | -- when A => | |
4111 | -- T := AX'Unrestricted_Access; | |
4112 | -- when B => | |
4113 | -- T := BX'Unrestricted_Access; | |
4114 | -- ... | |
4115 | -- end case; | |
4116 | -- in Tnn.all end; | |
4117 | ||
4118 | Cstmt := | |
4119 | Make_Case_Statement (Loc, | |
4120 | Expression => Expression (N), | |
4121 | Alternatives => New_List); | |
4122 | ||
4123 | Actions := New_List; | |
4124 | ||
4125 | -- Scalar case | |
4126 | ||
4127 | if Is_Scalar_Type (Typ) then | |
4128 | Ttyp := Typ; | |
4129 | ||
4130 | else | |
4131 | Pnn := Make_Temporary (Loc, 'P'); | |
4132 | Append_To (Actions, | |
4133 | Make_Full_Type_Declaration (Loc, | |
4134 | Defining_Identifier => Pnn, | |
4135 | Type_Definition => | |
4136 | Make_Access_To_Object_Definition (Loc, | |
4137 | All_Present => True, | |
4138 | Subtype_Indication => | |
4139 | New_Reference_To (Typ, Loc)))); | |
4140 | Ttyp := Pnn; | |
4141 | end if; | |
4142 | ||
4143 | Tnn := Make_Temporary (Loc, 'T'); | |
4144 | Append_To (Actions, | |
4145 | Make_Object_Declaration (Loc, | |
4146 | Defining_Identifier => Tnn, | |
4147 | Object_Definition => New_Occurrence_Of (Ttyp, Loc))); | |
4148 | ||
4149 | -- Now process the alternatives | |
4150 | ||
4151 | Alt := First (Alternatives (N)); | |
4152 | while Present (Alt) loop | |
4153 | declare | |
eaed0c37 AC |
4154 | Aexp : Node_Id := Expression (Alt); |
4155 | Aloc : constant Source_Ptr := Sloc (Aexp); | |
4156 | Stats : List_Id; | |
19d846a0 RD |
4157 | |
4158 | begin | |
eaed0c37 AC |
4159 | -- As described above, take Unrestricted_Access for case of non- |
4160 | -- scalar types, to avoid big copies, and special cases. | |
05dbd302 | 4161 | |
19d846a0 RD |
4162 | if not Is_Scalar_Type (Typ) then |
4163 | Aexp := | |
4164 | Make_Attribute_Reference (Aloc, | |
4165 | Prefix => Relocate_Node (Aexp), | |
4166 | Attribute_Name => Name_Unrestricted_Access); | |
4167 | end if; | |
4168 | ||
eaed0c37 AC |
4169 | Stats := New_List ( |
4170 | Make_Assignment_Statement (Aloc, | |
4171 | Name => New_Occurrence_Of (Tnn, Loc), | |
4172 | Expression => Aexp)); | |
4173 | ||
4174 | -- Propagate declarations inserted in the node by Insert_Actions | |
4175 | -- (for example, temporaries generated to remove side effects). | |
4176 | -- These actions must remain attached to the alternative, given | |
4177 | -- that they are generated by the corresponding expression. | |
4178 | ||
4179 | if Present (Sinfo.Actions (Alt)) then | |
4180 | Prepend_List (Sinfo.Actions (Alt), Stats); | |
4181 | end if; | |
4182 | ||
19d846a0 RD |
4183 | Append_To |
4184 | (Alternatives (Cstmt), | |
4185 | Make_Case_Statement_Alternative (Sloc (Alt), | |
4186 | Discrete_Choices => Discrete_Choices (Alt), | |
eaed0c37 | 4187 | Statements => Stats)); |
19d846a0 RD |
4188 | end; |
4189 | ||
4190 | Next (Alt); | |
4191 | end loop; | |
4192 | ||
4193 | Append_To (Actions, Cstmt); | |
4194 | ||
4195 | -- Construct and return final expression with actions | |
4196 | ||
4197 | if Is_Scalar_Type (Typ) then | |
4198 | Fexp := New_Occurrence_Of (Tnn, Loc); | |
4199 | else | |
4200 | Fexp := | |
4201 | Make_Explicit_Dereference (Loc, | |
4202 | Prefix => New_Occurrence_Of (Tnn, Loc)); | |
4203 | end if; | |
4204 | ||
4205 | Rewrite (N, | |
4206 | Make_Expression_With_Actions (Loc, | |
4207 | Expression => Fexp, | |
4208 | Actions => Actions)); | |
4209 | ||
4210 | Analyze_And_Resolve (N, Typ); | |
4211 | end Expand_N_Case_Expression; | |
4212 | ||
70482933 RK |
4213 | ------------------------------------- |
4214 | -- Expand_N_Conditional_Expression -- | |
4215 | ------------------------------------- | |
4216 | ||
305caf42 | 4217 | -- Deal with limited types and expression actions |
70482933 RK |
4218 | |
4219 | procedure Expand_N_Conditional_Expression (N : Node_Id) is | |
4220 | Loc : constant Source_Ptr := Sloc (N); | |
4221 | Cond : constant Node_Id := First (Expressions (N)); | |
4222 | Thenx : constant Node_Id := Next (Cond); | |
4223 | Elsex : constant Node_Id := Next (Thenx); | |
4224 | Typ : constant Entity_Id := Etype (N); | |
c471e2da | 4225 | |
602a7ec0 AC |
4226 | Cnn : Entity_Id; |
4227 | Decl : Node_Id; | |
4228 | New_If : Node_Id; | |
4229 | New_N : Node_Id; | |
4230 | P_Decl : Node_Id; | |
4231 | Expr : Node_Id; | |
4232 | Actions : List_Id; | |
70482933 RK |
4233 | |
4234 | begin | |
602a7ec0 AC |
4235 | -- Fold at compile time if condition known. We have already folded |
4236 | -- static conditional expressions, but it is possible to fold any | |
4237 | -- case in which the condition is known at compile time, even though | |
4238 | -- the result is non-static. | |
4239 | ||
4240 | -- Note that we don't do the fold of such cases in Sem_Elab because | |
4241 | -- it can cause infinite loops with the expander adding a conditional | |
4242 | -- expression, and Sem_Elab circuitry removing it repeatedly. | |
4243 | ||
4244 | if Compile_Time_Known_Value (Cond) then | |
4245 | if Is_True (Expr_Value (Cond)) then | |
4246 | Expr := Thenx; | |
4247 | Actions := Then_Actions (N); | |
4248 | else | |
4249 | Expr := Elsex; | |
4250 | Actions := Else_Actions (N); | |
4251 | end if; | |
4252 | ||
4253 | Remove (Expr); | |
ae77c68b AC |
4254 | |
4255 | if Present (Actions) then | |
4256 | ||
9d641fc0 TQ |
4257 | -- If we are not allowed to use Expression_With_Actions, just skip |
4258 | -- the optimization, it is not critical for correctness. | |
ae77c68b AC |
4259 | |
4260 | if not Use_Expression_With_Actions then | |
4261 | goto Skip_Optimization; | |
4262 | end if; | |
4263 | ||
4264 | Rewrite (N, | |
4265 | Make_Expression_With_Actions (Loc, | |
4266 | Expression => Relocate_Node (Expr), | |
4267 | Actions => Actions)); | |
4268 | Analyze_And_Resolve (N, Typ); | |
4269 | ||
4270 | else | |
4271 | Rewrite (N, Relocate_Node (Expr)); | |
4272 | end if; | |
602a7ec0 AC |
4273 | |
4274 | -- Note that the result is never static (legitimate cases of static | |
4275 | -- conditional expressions were folded in Sem_Eval). | |
4276 | ||
4277 | Set_Is_Static_Expression (N, False); | |
4278 | return; | |
4279 | end if; | |
4280 | ||
ae77c68b AC |
4281 | <<Skip_Optimization>> |
4282 | ||
305caf42 AC |
4283 | -- If the type is limited or unconstrained, we expand as follows to |
4284 | -- avoid any possibility of improper copies. | |
70482933 | 4285 | |
305caf42 AC |
4286 | -- Note: it may be possible to avoid this special processing if the |
4287 | -- back end uses its own mechanisms for handling by-reference types ??? | |
ac7120ce | 4288 | |
c471e2da AC |
4289 | -- type Ptr is access all Typ; |
4290 | -- Cnn : Ptr; | |
ac7120ce RD |
4291 | -- if cond then |
4292 | -- <<then actions>> | |
4293 | -- Cnn := then-expr'Unrestricted_Access; | |
4294 | -- else | |
4295 | -- <<else actions>> | |
4296 | -- Cnn := else-expr'Unrestricted_Access; | |
4297 | -- end if; | |
4298 | ||
308e6f3a | 4299 | -- and replace the conditional expression by a reference to Cnn.all. |
ac7120ce | 4300 | |
305caf42 AC |
4301 | -- This special case can be skipped if the back end handles limited |
4302 | -- types properly and ensures that no incorrect copies are made. | |
4303 | ||
4304 | if Is_By_Reference_Type (Typ) | |
4305 | and then not Back_End_Handles_Limited_Types | |
4306 | then | |
faf387e1 | 4307 | Cnn := Make_Temporary (Loc, 'C', N); |
70482933 | 4308 | |
c471e2da AC |
4309 | P_Decl := |
4310 | Make_Full_Type_Declaration (Loc, | |
df3e68b1 HK |
4311 | Defining_Identifier => |
4312 | Make_Temporary (Loc, 'A'), | |
c471e2da AC |
4313 | Type_Definition => |
4314 | Make_Access_To_Object_Definition (Loc, | |
243cae0a AC |
4315 | All_Present => True, |
4316 | Subtype_Indication => New_Reference_To (Typ, Loc))); | |
c471e2da AC |
4317 | |
4318 | Insert_Action (N, P_Decl); | |
4319 | ||
4320 | Decl := | |
4321 | Make_Object_Declaration (Loc, | |
4322 | Defining_Identifier => Cnn, | |
4323 | Object_Definition => | |
4324 | New_Occurrence_Of (Defining_Identifier (P_Decl), Loc)); | |
4325 | ||
70482933 RK |
4326 | New_If := |
4327 | Make_Implicit_If_Statement (N, | |
4328 | Condition => Relocate_Node (Cond), | |
4329 | ||
4330 | Then_Statements => New_List ( | |
4331 | Make_Assignment_Statement (Sloc (Thenx), | |
243cae0a | 4332 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), |
c471e2da AC |
4333 | Expression => |
4334 | Make_Attribute_Reference (Loc, | |
4335 | Attribute_Name => Name_Unrestricted_Access, | |
243cae0a | 4336 | Prefix => Relocate_Node (Thenx)))), |
70482933 RK |
4337 | |
4338 | Else_Statements => New_List ( | |
4339 | Make_Assignment_Statement (Sloc (Elsex), | |
243cae0a | 4340 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), |
c471e2da AC |
4341 | Expression => |
4342 | Make_Attribute_Reference (Loc, | |
4343 | Attribute_Name => Name_Unrestricted_Access, | |
243cae0a | 4344 | Prefix => Relocate_Node (Elsex))))); |
70482933 | 4345 | |
c471e2da AC |
4346 | New_N := |
4347 | Make_Explicit_Dereference (Loc, | |
4348 | Prefix => New_Occurrence_Of (Cnn, Loc)); | |
fb1949a0 | 4349 | |
c471e2da AC |
4350 | -- For other types, we only need to expand if there are other actions |
4351 | -- associated with either branch. | |
4352 | ||
4353 | elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
c471e2da | 4354 | |
305caf42 AC |
4355 | -- We have two approaches to handling this. If we are allowed to use |
4356 | -- N_Expression_With_Actions, then we can just wrap the actions into | |
4357 | -- the appropriate expression. | |
4358 | ||
4359 | if Use_Expression_With_Actions then | |
4360 | if Present (Then_Actions (N)) then | |
4361 | Rewrite (Thenx, | |
4362 | Make_Expression_With_Actions (Sloc (Thenx), | |
4363 | Actions => Then_Actions (N), | |
4364 | Expression => Relocate_Node (Thenx))); | |
48b351d9 | 4365 | Set_Then_Actions (N, No_List); |
305caf42 AC |
4366 | Analyze_And_Resolve (Thenx, Typ); |
4367 | end if; | |
c471e2da | 4368 | |
305caf42 AC |
4369 | if Present (Else_Actions (N)) then |
4370 | Rewrite (Elsex, | |
4371 | Make_Expression_With_Actions (Sloc (Elsex), | |
4372 | Actions => Else_Actions (N), | |
4373 | Expression => Relocate_Node (Elsex))); | |
48b351d9 | 4374 | Set_Else_Actions (N, No_List); |
305caf42 AC |
4375 | Analyze_And_Resolve (Elsex, Typ); |
4376 | end if; | |
c471e2da | 4377 | |
305caf42 | 4378 | return; |
c471e2da | 4379 | |
305caf42 AC |
4380 | -- if we can't use N_Expression_With_Actions nodes, then we insert |
4381 | -- the following sequence of actions (using Insert_Actions): | |
fb1949a0 | 4382 | |
305caf42 AC |
4383 | -- Cnn : typ; |
4384 | -- if cond then | |
4385 | -- <<then actions>> | |
4386 | -- Cnn := then-expr; | |
4387 | -- else | |
4388 | -- <<else actions>> | |
4389 | -- Cnn := else-expr | |
4390 | -- end if; | |
fbf5a39b | 4391 | |
305caf42 | 4392 | -- and replace the conditional expression by a reference to Cnn |
70482933 | 4393 | |
305caf42 AC |
4394 | else |
4395 | Cnn := Make_Temporary (Loc, 'C', N); | |
4396 | ||
4397 | Decl := | |
4398 | Make_Object_Declaration (Loc, | |
4399 | Defining_Identifier => Cnn, | |
4400 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
4401 | ||
4402 | New_If := | |
4403 | Make_Implicit_If_Statement (N, | |
4404 | Condition => Relocate_Node (Cond), | |
4405 | ||
4406 | Then_Statements => New_List ( | |
4407 | Make_Assignment_Statement (Sloc (Thenx), | |
4408 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
4409 | Expression => Relocate_Node (Thenx))), | |
4410 | ||
4411 | Else_Statements => New_List ( | |
4412 | Make_Assignment_Statement (Sloc (Elsex), | |
4413 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
4414 | Expression => Relocate_Node (Elsex)))); | |
70482933 | 4415 | |
305caf42 AC |
4416 | Set_Assignment_OK (Name (First (Then_Statements (New_If)))); |
4417 | Set_Assignment_OK (Name (First (Else_Statements (New_If)))); | |
4418 | ||
4419 | New_N := New_Occurrence_Of (Cnn, Loc); | |
4420 | end if; | |
4421 | ||
4422 | -- If no actions then no expansion needed, gigi will handle it using | |
4423 | -- the same approach as a C conditional expression. | |
4424 | ||
4425 | else | |
c471e2da AC |
4426 | return; |
4427 | end if; | |
4428 | ||
305caf42 AC |
4429 | -- Fall through here for either the limited expansion, or the case of |
4430 | -- inserting actions for non-limited types. In both these cases, we must | |
4431 | -- move the SLOC of the parent If statement to the newly created one and | |
3fc5d116 RD |
4432 | -- change it to the SLOC of the expression which, after expansion, will |
4433 | -- correspond to what is being evaluated. | |
c471e2da AC |
4434 | |
4435 | if Present (Parent (N)) | |
4436 | and then Nkind (Parent (N)) = N_If_Statement | |
4437 | then | |
4438 | Set_Sloc (New_If, Sloc (Parent (N))); | |
4439 | Set_Sloc (Parent (N), Loc); | |
4440 | end if; | |
70482933 | 4441 | |
3fc5d116 RD |
4442 | -- Make sure Then_Actions and Else_Actions are appropriately moved |
4443 | -- to the new if statement. | |
4444 | ||
c471e2da AC |
4445 | if Present (Then_Actions (N)) then |
4446 | Insert_List_Before | |
4447 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
70482933 | 4448 | end if; |
c471e2da AC |
4449 | |
4450 | if Present (Else_Actions (N)) then | |
4451 | Insert_List_Before | |
4452 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
4453 | end if; | |
4454 | ||
4455 | Insert_Action (N, Decl); | |
4456 | Insert_Action (N, New_If); | |
4457 | Rewrite (N, New_N); | |
4458 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
4459 | end Expand_N_Conditional_Expression; |
4460 | ||
4461 | ----------------------------------- | |
4462 | -- Expand_N_Explicit_Dereference -- | |
4463 | ----------------------------------- | |
4464 | ||
4465 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
4466 | begin | |
dfd99a80 | 4467 | -- Insert explicit dereference call for the checked storage pool case |
70482933 RK |
4468 | |
4469 | Insert_Dereference_Action (Prefix (N)); | |
5972791c AC |
4470 | |
4471 | -- If the type is an Atomic type for which Atomic_Sync is enabled, then | |
4472 | -- we set the atomic sync flag. | |
4473 | ||
4474 | if Is_Atomic (Etype (N)) | |
4475 | and then not Atomic_Synchronization_Disabled (Etype (N)) | |
4476 | then | |
4c318253 | 4477 | Activate_Atomic_Synchronization (N); |
5972791c | 4478 | end if; |
70482933 RK |
4479 | end Expand_N_Explicit_Dereference; |
4480 | ||
35a1c212 AC |
4481 | -------------------------------------- |
4482 | -- Expand_N_Expression_With_Actions -- | |
4483 | -------------------------------------- | |
4484 | ||
4485 | procedure Expand_N_Expression_With_Actions (N : Node_Id) is | |
4486 | ||
4487 | procedure Process_Transient_Object (Decl : Node_Id); | |
4488 | -- Given the declaration of a controlled transient declared inside the | |
4489 | -- Actions list of an Expression_With_Actions, generate all necessary | |
4490 | -- types and hooks in order to properly finalize the transient. This | |
4491 | -- mechanism works in conjunction with Build_Finalizer. | |
4492 | ||
4493 | ------------------------------ | |
4494 | -- Process_Transient_Object -- | |
4495 | ------------------------------ | |
4496 | ||
4497 | procedure Process_Transient_Object (Decl : Node_Id) is | |
35a1c212 | 4498 | |
fecbd779 | 4499 | function Find_Insertion_Node return Node_Id; |
db15225a AC |
4500 | -- Complex conditions in if statements may be converted into nested |
4501 | -- EWAs. In this case, any generated code must be inserted before the | |
4502 | -- if statement to ensure proper visibility of the hook objects. This | |
4503 | -- routine returns the top most short circuit operator or the parent | |
4504 | -- of the EWA if no nesting was detected. | |
fecbd779 AC |
4505 | |
4506 | ------------------------- | |
4507 | -- Find_Insertion_Node -- | |
4508 | ------------------------- | |
4509 | ||
4510 | function Find_Insertion_Node return Node_Id is | |
3040dbd4 | 4511 | Par : Node_Id; |
fecbd779 AC |
4512 | |
4513 | begin | |
db15225a | 4514 | -- Climb up the branches of a complex condition |
fecbd779 | 4515 | |
3040dbd4 | 4516 | Par := N; |
fecbd779 AC |
4517 | while Nkind_In (Parent (Par), N_And_Then, N_Op_Not, N_Or_Else) loop |
4518 | Par := Parent (Par); | |
4519 | end loop; | |
4520 | ||
4521 | return Par; | |
4522 | end Find_Insertion_Node; | |
4523 | ||
3040dbd4 RD |
4524 | -- Local variables |
4525 | ||
db15225a | 4526 | Ins_Node : constant Node_Id := Find_Insertion_Node; |
35a1c212 AC |
4527 | Loc : constant Source_Ptr := Sloc (Decl); |
4528 | Obj_Id : constant Entity_Id := Defining_Identifier (Decl); | |
4529 | Obj_Typ : constant Entity_Id := Etype (Obj_Id); | |
4530 | Desig_Typ : Entity_Id; | |
4531 | Expr : Node_Id; | |
4532 | Ptr_Decl : Node_Id; | |
4533 | Ptr_Id : Entity_Id; | |
4534 | Temp_Decl : Node_Id; | |
4535 | Temp_Id : Node_Id; | |
4536 | ||
9d641fc0 TQ |
4537 | -- Start of processing for Process_Transient_Object |
4538 | ||
35a1c212 | 4539 | begin |
3040dbd4 RD |
4540 | -- Step 1: Create the access type which provides a reference to the |
4541 | -- transient object. | |
35a1c212 AC |
4542 | |
4543 | if Is_Access_Type (Obj_Typ) then | |
4544 | Desig_Typ := Directly_Designated_Type (Obj_Typ); | |
4545 | else | |
4546 | Desig_Typ := Obj_Typ; | |
4547 | end if; | |
4548 | ||
4549 | -- Generate: | |
4550 | -- Ann : access [all] <Desig_Typ>; | |
4551 | ||
4552 | Ptr_Id := Make_Temporary (Loc, 'A'); | |
4553 | ||
4554 | Ptr_Decl := | |
4555 | Make_Full_Type_Declaration (Loc, | |
4556 | Defining_Identifier => Ptr_Id, | |
3040dbd4 RD |
4557 | Type_Definition => |
4558 | Make_Access_To_Object_Definition (Loc, | |
4559 | All_Present => | |
4560 | Ekind (Obj_Typ) = E_General_Access_Type, | |
4561 | Subtype_Indication => New_Reference_To (Desig_Typ, Loc))); | |
35a1c212 | 4562 | |
db15225a | 4563 | Insert_Action (Ins_Node, Ptr_Decl); |
35a1c212 AC |
4564 | Analyze (Ptr_Decl); |
4565 | ||
4566 | -- Step 2: Create a temporary which acts as a hook to the transient | |
4567 | -- object. Generate: | |
4568 | ||
4569 | -- Temp : Ptr_Id := null; | |
4570 | ||
4571 | Temp_Id := Make_Temporary (Loc, 'T'); | |
4572 | ||
4573 | Temp_Decl := | |
4574 | Make_Object_Declaration (Loc, | |
4575 | Defining_Identifier => Temp_Id, | |
4576 | Object_Definition => New_Reference_To (Ptr_Id, Loc)); | |
4577 | ||
db15225a | 4578 | Insert_Action (Ins_Node, Temp_Decl); |
35a1c212 AC |
4579 | Analyze (Temp_Decl); |
4580 | ||
db15225a | 4581 | -- Mark this temporary as created for the purposes of exporting the |
35a1c212 AC |
4582 | -- transient declaration out of the Actions list. This signals the |
4583 | -- machinery in Build_Finalizer to recognize this special case. | |
4584 | ||
4585 | Set_Return_Flag_Or_Transient_Decl (Temp_Id, Decl); | |
4586 | ||
db15225a | 4587 | -- Step 3: Hook the transient object to the temporary |
35a1c212 AC |
4588 | |
4589 | if Is_Access_Type (Obj_Typ) then | |
4590 | Expr := Convert_To (Ptr_Id, New_Reference_To (Obj_Id, Loc)); | |
4591 | else | |
4592 | Expr := | |
4593 | Make_Attribute_Reference (Loc, | |
4fdebd93 | 4594 | Prefix => New_Reference_To (Obj_Id, Loc), |
35a1c212 AC |
4595 | Attribute_Name => Name_Unrestricted_Access); |
4596 | end if; | |
4597 | ||
4598 | -- Generate: | |
4599 | -- Temp := Ptr_Id (Obj_Id); | |
4600 | -- <or> | |
4601 | -- Temp := Obj_Id'Unrestricted_Access; | |
4602 | ||
4603 | Insert_After_And_Analyze (Decl, | |
4604 | Make_Assignment_Statement (Loc, | |
4605 | Name => New_Reference_To (Temp_Id, Loc), | |
4606 | Expression => Expr)); | |
4607 | end Process_Transient_Object; | |
4608 | ||
db15225a AC |
4609 | -- Local variables |
4610 | ||
35a1c212 AC |
4611 | Decl : Node_Id; |
4612 | ||
4613 | -- Start of processing for Expand_N_Expression_With_Actions | |
4614 | ||
4615 | begin | |
4616 | Decl := First (Actions (N)); | |
4617 | while Present (Decl) loop | |
4618 | if Nkind (Decl) = N_Object_Declaration | |
4619 | and then Is_Finalizable_Transient (Decl, N) | |
4620 | then | |
4621 | Process_Transient_Object (Decl); | |
4622 | end if; | |
4623 | ||
4624 | Next (Decl); | |
4625 | end loop; | |
4626 | end Expand_N_Expression_With_Actions; | |
4627 | ||
70482933 RK |
4628 | ----------------- |
4629 | -- Expand_N_In -- | |
4630 | ----------------- | |
4631 | ||
4632 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 | 4633 | Loc : constant Source_Ptr := Sloc (N); |
4818e7b9 | 4634 | Restyp : constant Entity_Id := Etype (N); |
7324bf49 AC |
4635 | Lop : constant Node_Id := Left_Opnd (N); |
4636 | Rop : constant Node_Id := Right_Opnd (N); | |
4637 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 4638 | |
4818e7b9 RD |
4639 | Ltyp : Entity_Id; |
4640 | Rtyp : Entity_Id; | |
4641 | ||
630d30e9 RD |
4642 | procedure Substitute_Valid_Check; |
4643 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
4644 | -- test for the left operand being in range of its subtype. | |
4645 | ||
4646 | ---------------------------- | |
4647 | -- Substitute_Valid_Check -- | |
4648 | ---------------------------- | |
4649 | ||
4650 | procedure Substitute_Valid_Check is | |
4651 | begin | |
c7532b2d AC |
4652 | Rewrite (N, |
4653 | Make_Attribute_Reference (Loc, | |
4654 | Prefix => Relocate_Node (Lop), | |
4655 | Attribute_Name => Name_Valid)); | |
630d30e9 | 4656 | |
c7532b2d | 4657 | Analyze_And_Resolve (N, Restyp); |
630d30e9 | 4658 | |
c7532b2d AC |
4659 | Error_Msg_N ("?explicit membership test may be optimized away", N); |
4660 | Error_Msg_N -- CODEFIX | |
4661 | ("\?use ''Valid attribute instead", N); | |
4662 | return; | |
630d30e9 RD |
4663 | end Substitute_Valid_Check; |
4664 | ||
4665 | -- Start of processing for Expand_N_In | |
4666 | ||
70482933 | 4667 | begin |
308e6f3a | 4668 | -- If set membership case, expand with separate procedure |
4818e7b9 | 4669 | |
197e4514 | 4670 | if Present (Alternatives (N)) then |
a3068ca6 | 4671 | Expand_Set_Membership (N); |
197e4514 AC |
4672 | return; |
4673 | end if; | |
4674 | ||
4818e7b9 RD |
4675 | -- Not set membership, proceed with expansion |
4676 | ||
4677 | Ltyp := Etype (Left_Opnd (N)); | |
4678 | Rtyp := Etype (Right_Opnd (N)); | |
4679 | ||
630d30e9 RD |
4680 | -- Check case of explicit test for an expression in range of its |
4681 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
9a0ddeee | 4682 | -- test and give a warning. For floating point types however, this is a |
c95e0edc | 4683 | -- standard way to check for finite numbers, and using 'Valid would |
c7532b2d AC |
4684 | -- typically be a pessimization. Also skip this test for predicated |
4685 | -- types, since it is perfectly reasonable to check if a value meets | |
4686 | -- its predicate. | |
630d30e9 | 4687 | |
4818e7b9 RD |
4688 | if Is_Scalar_Type (Ltyp) |
4689 | and then not Is_Floating_Point_Type (Ltyp) | |
630d30e9 | 4690 | and then Nkind (Rop) in N_Has_Entity |
4818e7b9 | 4691 | and then Ltyp = Entity (Rop) |
630d30e9 | 4692 | and then Comes_From_Source (N) |
26bff3d9 | 4693 | and then VM_Target = No_VM |
c7532b2d AC |
4694 | and then not (Is_Discrete_Type (Ltyp) |
4695 | and then Present (Predicate_Function (Ltyp))) | |
630d30e9 RD |
4696 | then |
4697 | Substitute_Valid_Check; | |
4698 | return; | |
4699 | end if; | |
4700 | ||
20b5d666 JM |
4701 | -- Do validity check on operands |
4702 | ||
4703 | if Validity_Checks_On and Validity_Check_Operands then | |
4704 | Ensure_Valid (Left_Opnd (N)); | |
4705 | Validity_Check_Range (Right_Opnd (N)); | |
4706 | end if; | |
4707 | ||
630d30e9 | 4708 | -- Case of explicit range |
fbf5a39b AC |
4709 | |
4710 | if Nkind (Rop) = N_Range then | |
4711 | declare | |
630d30e9 RD |
4712 | Lo : constant Node_Id := Low_Bound (Rop); |
4713 | Hi : constant Node_Id := High_Bound (Rop); | |
4714 | ||
4715 | Lo_Orig : constant Node_Id := Original_Node (Lo); | |
4716 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
4717 | ||
c800f862 RD |
4718 | Lcheck : Compare_Result; |
4719 | Ucheck : Compare_Result; | |
fbf5a39b | 4720 | |
d766cee3 RD |
4721 | Warn1 : constant Boolean := |
4722 | Constant_Condition_Warnings | |
c800f862 RD |
4723 | and then Comes_From_Source (N) |
4724 | and then not In_Instance; | |
d766cee3 | 4725 | -- This must be true for any of the optimization warnings, we |
9a0ddeee AC |
4726 | -- clearly want to give them only for source with the flag on. We |
4727 | -- also skip these warnings in an instance since it may be the | |
4728 | -- case that different instantiations have different ranges. | |
d766cee3 RD |
4729 | |
4730 | Warn2 : constant Boolean := | |
4731 | Warn1 | |
4732 | and then Nkind (Original_Node (Rop)) = N_Range | |
4733 | and then Is_Integer_Type (Etype (Lo)); | |
4734 | -- For the case where only one bound warning is elided, we also | |
4735 | -- insist on an explicit range and an integer type. The reason is | |
4736 | -- that the use of enumeration ranges including an end point is | |
9a0ddeee AC |
4737 | -- common, as is the use of a subtype name, one of whose bounds is |
4738 | -- the same as the type of the expression. | |
d766cee3 | 4739 | |
fbf5a39b | 4740 | begin |
c95e0edc | 4741 | -- If test is explicit x'First .. x'Last, replace by valid check |
630d30e9 | 4742 | |
e606088a AC |
4743 | -- Could use some individual comments for this complex test ??? |
4744 | ||
d766cee3 | 4745 | if Is_Scalar_Type (Ltyp) |
630d30e9 RD |
4746 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
4747 | and then Attribute_Name (Lo_Orig) = Name_First | |
4748 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 4749 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
630d30e9 RD |
4750 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
4751 | and then Attribute_Name (Hi_Orig) = Name_Last | |
4752 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 4753 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
630d30e9 | 4754 | and then Comes_From_Source (N) |
26bff3d9 | 4755 | and then VM_Target = No_VM |
630d30e9 RD |
4756 | then |
4757 | Substitute_Valid_Check; | |
4818e7b9 | 4758 | goto Leave; |
630d30e9 RD |
4759 | end if; |
4760 | ||
d766cee3 RD |
4761 | -- If bounds of type are known at compile time, and the end points |
4762 | -- are known at compile time and identical, this is another case | |
4763 | -- for substituting a valid test. We only do this for discrete | |
4764 | -- types, since it won't arise in practice for float types. | |
4765 | ||
4766 | if Comes_From_Source (N) | |
4767 | and then Is_Discrete_Type (Ltyp) | |
4768 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
4769 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
4770 | and then Compile_Time_Known_Value (Lo) | |
4771 | and then Compile_Time_Known_Value (Hi) | |
4772 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
4773 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e RD |
4774 | |
4775 | -- Kill warnings in instances, since they may be cases where we | |
4776 | -- have a test in the generic that makes sense with some types | |
4777 | -- and not with other types. | |
4778 | ||
4779 | and then not In_Instance | |
d766cee3 RD |
4780 | then |
4781 | Substitute_Valid_Check; | |
4818e7b9 | 4782 | goto Leave; |
d766cee3 RD |
4783 | end if; |
4784 | ||
9a0ddeee AC |
4785 | -- If we have an explicit range, do a bit of optimization based on |
4786 | -- range analysis (we may be able to kill one or both checks). | |
630d30e9 | 4787 | |
c800f862 RD |
4788 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
4789 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
4790 | ||
630d30e9 RD |
4791 | -- If either check is known to fail, replace result by False since |
4792 | -- the other check does not matter. Preserve the static flag for | |
4793 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
4794 | |
4795 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 4796 | if Warn1 then |
ed2233dc AC |
4797 | Error_Msg_N ("?range test optimized away", N); |
4798 | Error_Msg_N ("\?value is known to be out of range", N); | |
d766cee3 RD |
4799 | end if; |
4800 | ||
9a0ddeee | 4801 | Rewrite (N, New_Reference_To (Standard_False, Loc)); |
4818e7b9 | 4802 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 4803 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 4804 | goto Leave; |
fbf5a39b | 4805 | |
685094bf RD |
4806 | -- If both checks are known to succeed, replace result by True, |
4807 | -- since we know we are in range. | |
fbf5a39b AC |
4808 | |
4809 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 4810 | if Warn1 then |
ed2233dc AC |
4811 | Error_Msg_N ("?range test optimized away", N); |
4812 | Error_Msg_N ("\?value is known to be in range", N); | |
d766cee3 RD |
4813 | end if; |
4814 | ||
9a0ddeee | 4815 | Rewrite (N, New_Reference_To (Standard_True, Loc)); |
4818e7b9 | 4816 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 4817 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 4818 | goto Leave; |
fbf5a39b | 4819 | |
d766cee3 RD |
4820 | -- If lower bound check succeeds and upper bound check is not |
4821 | -- known to succeed or fail, then replace the range check with | |
4822 | -- a comparison against the upper bound. | |
fbf5a39b AC |
4823 | |
4824 | elsif Lcheck in Compare_GE then | |
94eefd2e | 4825 | if Warn2 and then not In_Instance then |
ed2233dc AC |
4826 | Error_Msg_N ("?lower bound test optimized away", Lo); |
4827 | Error_Msg_N ("\?value is known to be in range", Lo); | |
d766cee3 RD |
4828 | end if; |
4829 | ||
fbf5a39b AC |
4830 | Rewrite (N, |
4831 | Make_Op_Le (Loc, | |
4832 | Left_Opnd => Lop, | |
4833 | Right_Opnd => High_Bound (Rop))); | |
4818e7b9 RD |
4834 | Analyze_And_Resolve (N, Restyp); |
4835 | goto Leave; | |
fbf5a39b | 4836 | |
d766cee3 RD |
4837 | -- If upper bound check succeeds and lower bound check is not |
4838 | -- known to succeed or fail, then replace the range check with | |
4839 | -- a comparison against the lower bound. | |
fbf5a39b AC |
4840 | |
4841 | elsif Ucheck in Compare_LE then | |
94eefd2e | 4842 | if Warn2 and then not In_Instance then |
ed2233dc AC |
4843 | Error_Msg_N ("?upper bound test optimized away", Hi); |
4844 | Error_Msg_N ("\?value is known to be in range", Hi); | |
d766cee3 RD |
4845 | end if; |
4846 | ||
fbf5a39b AC |
4847 | Rewrite (N, |
4848 | Make_Op_Ge (Loc, | |
4849 | Left_Opnd => Lop, | |
4850 | Right_Opnd => Low_Bound (Rop))); | |
4818e7b9 RD |
4851 | Analyze_And_Resolve (N, Restyp); |
4852 | goto Leave; | |
fbf5a39b | 4853 | end if; |
c800f862 RD |
4854 | |
4855 | -- We couldn't optimize away the range check, but there is one | |
4856 | -- more issue. If we are checking constant conditionals, then we | |
4857 | -- see if we can determine the outcome assuming everything is | |
4858 | -- valid, and if so give an appropriate warning. | |
4859 | ||
4860 | if Warn1 and then not Assume_No_Invalid_Values then | |
4861 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
4862 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
4863 | ||
4864 | -- Result is out of range for valid value | |
4865 | ||
4866 | if Lcheck = LT or else Ucheck = GT then | |
ed2233dc | 4867 | Error_Msg_N |
c800f862 RD |
4868 | ("?value can only be in range if it is invalid", N); |
4869 | ||
4870 | -- Result is in range for valid value | |
4871 | ||
4872 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
ed2233dc | 4873 | Error_Msg_N |
c800f862 RD |
4874 | ("?value can only be out of range if it is invalid", N); |
4875 | ||
4876 | -- Lower bound check succeeds if value is valid | |
4877 | ||
4878 | elsif Warn2 and then Lcheck in Compare_GE then | |
ed2233dc | 4879 | Error_Msg_N |
c800f862 RD |
4880 | ("?lower bound check only fails if it is invalid", Lo); |
4881 | ||
4882 | -- Upper bound check succeeds if value is valid | |
4883 | ||
4884 | elsif Warn2 and then Ucheck in Compare_LE then | |
ed2233dc | 4885 | Error_Msg_N |
c800f862 RD |
4886 | ("?upper bound check only fails for invalid values", Hi); |
4887 | end if; | |
4888 | end if; | |
fbf5a39b AC |
4889 | end; |
4890 | ||
4891 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 4892 | |
4818e7b9 | 4893 | goto Leave; |
70482933 RK |
4894 | |
4895 | -- Here right operand is a subtype mark | |
4896 | ||
4897 | else | |
4898 | declare | |
82878151 AC |
4899 | Typ : Entity_Id := Etype (Rop); |
4900 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
4901 | Cond : Node_Id := Empty; | |
4902 | New_N : Node_Id; | |
4903 | Obj : Node_Id := Lop; | |
4904 | SCIL_Node : Node_Id; | |
70482933 RK |
4905 | |
4906 | begin | |
4907 | Remove_Side_Effects (Obj); | |
4908 | ||
4909 | -- For tagged type, do tagged membership operation | |
4910 | ||
4911 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 4912 | |
26bff3d9 JM |
4913 | -- No expansion will be performed when VM_Target, as the VM |
4914 | -- back-ends will handle the membership tests directly (tags | |
4915 | -- are not explicitly represented in Java objects, so the | |
4916 | -- normal tagged membership expansion is not what we want). | |
70482933 | 4917 | |
1f110335 | 4918 | if Tagged_Type_Expansion then |
82878151 AC |
4919 | Tagged_Membership (N, SCIL_Node, New_N); |
4920 | Rewrite (N, New_N); | |
4818e7b9 | 4921 | Analyze_And_Resolve (N, Restyp); |
82878151 AC |
4922 | |
4923 | -- Update decoration of relocated node referenced by the | |
4924 | -- SCIL node. | |
4925 | ||
9a0ddeee | 4926 | if Generate_SCIL and then Present (SCIL_Node) then |
7665e4bd | 4927 | Set_SCIL_Node (N, SCIL_Node); |
82878151 | 4928 | end if; |
70482933 RK |
4929 | end if; |
4930 | ||
4818e7b9 | 4931 | goto Leave; |
70482933 | 4932 | |
c95e0edc | 4933 | -- If type is scalar type, rewrite as x in t'First .. t'Last. |
70482933 | 4934 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
4935 | -- type if they come from the original type definition. Also this |
4936 | -- way we get all the processing above for an explicit range. | |
70482933 | 4937 | |
c7532b2d AC |
4938 | -- Don't do this for predicated types, since in this case we |
4939 | -- want to check the predicate! | |
c0f136cd | 4940 | |
c7532b2d AC |
4941 | elsif Is_Scalar_Type (Typ) then |
4942 | if No (Predicate_Function (Typ)) then | |
4943 | Rewrite (Rop, | |
4944 | Make_Range (Loc, | |
4945 | Low_Bound => | |
4946 | Make_Attribute_Reference (Loc, | |
4947 | Attribute_Name => Name_First, | |
4948 | Prefix => New_Reference_To (Typ, Loc)), | |
4949 | ||
4950 | High_Bound => | |
4951 | Make_Attribute_Reference (Loc, | |
4952 | Attribute_Name => Name_Last, | |
4953 | Prefix => New_Reference_To (Typ, Loc)))); | |
4954 | Analyze_And_Resolve (N, Restyp); | |
4955 | end if; | |
70482933 | 4956 | |
4818e7b9 | 4957 | goto Leave; |
5d09245e AC |
4958 | |
4959 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
4960 | -- a membership test if the subtype mark denotes a constrained | |
4961 | -- Unchecked_Union subtype and the expression lacks inferable | |
4962 | -- discriminants. | |
4963 | ||
4964 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
4965 | and then Is_Constrained (Typ) | |
4966 | and then not Has_Inferable_Discriminants (Lop) | |
4967 | then | |
4968 | Insert_Action (N, | |
4969 | Make_Raise_Program_Error (Loc, | |
4970 | Reason => PE_Unchecked_Union_Restriction)); | |
4971 | ||
9a0ddeee AC |
4972 | -- Prevent Gigi from generating incorrect code by rewriting the |
4973 | -- test as False. | |
5d09245e | 4974 | |
9a0ddeee | 4975 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 4976 | goto Leave; |
70482933 RK |
4977 | end if; |
4978 | ||
fbf5a39b AC |
4979 | -- Here we have a non-scalar type |
4980 | ||
70482933 RK |
4981 | if Is_Acc then |
4982 | Typ := Designated_Type (Typ); | |
4983 | end if; | |
4984 | ||
4985 | if not Is_Constrained (Typ) then | |
9a0ddeee | 4986 | Rewrite (N, New_Reference_To (Standard_True, Loc)); |
4818e7b9 | 4987 | Analyze_And_Resolve (N, Restyp); |
70482933 | 4988 | |
685094bf RD |
4989 | -- For the constrained array case, we have to check the subscripts |
4990 | -- for an exact match if the lengths are non-zero (the lengths | |
4991 | -- must match in any case). | |
70482933 RK |
4992 | |
4993 | elsif Is_Array_Type (Typ) then | |
fbf5a39b | 4994 | Check_Subscripts : declare |
9a0ddeee | 4995 | function Build_Attribute_Reference |
2e071734 AC |
4996 | (E : Node_Id; |
4997 | Nam : Name_Id; | |
4998 | Dim : Nat) return Node_Id; | |
9a0ddeee | 4999 | -- Build attribute reference E'Nam (Dim) |
70482933 | 5000 | |
9a0ddeee AC |
5001 | ------------------------------- |
5002 | -- Build_Attribute_Reference -- | |
5003 | ------------------------------- | |
fbf5a39b | 5004 | |
9a0ddeee | 5005 | function Build_Attribute_Reference |
2e071734 AC |
5006 | (E : Node_Id; |
5007 | Nam : Name_Id; | |
5008 | Dim : Nat) return Node_Id | |
70482933 RK |
5009 | is |
5010 | begin | |
5011 | return | |
5012 | Make_Attribute_Reference (Loc, | |
9a0ddeee | 5013 | Prefix => E, |
70482933 | 5014 | Attribute_Name => Nam, |
9a0ddeee | 5015 | Expressions => New_List ( |
70482933 | 5016 | Make_Integer_Literal (Loc, Dim))); |
9a0ddeee | 5017 | end Build_Attribute_Reference; |
70482933 | 5018 | |
fad0600d | 5019 | -- Start of processing for Check_Subscripts |
fbf5a39b | 5020 | |
70482933 RK |
5021 | begin |
5022 | for J in 1 .. Number_Dimensions (Typ) loop | |
5023 | Evolve_And_Then (Cond, | |
5024 | Make_Op_Eq (Loc, | |
5025 | Left_Opnd => | |
9a0ddeee | 5026 | Build_Attribute_Reference |
fbf5a39b AC |
5027 | (Duplicate_Subexpr_No_Checks (Obj), |
5028 | Name_First, J), | |
70482933 | 5029 | Right_Opnd => |
9a0ddeee | 5030 | Build_Attribute_Reference |
70482933 RK |
5031 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); |
5032 | ||
5033 | Evolve_And_Then (Cond, | |
5034 | Make_Op_Eq (Loc, | |
5035 | Left_Opnd => | |
9a0ddeee | 5036 | Build_Attribute_Reference |
fbf5a39b AC |
5037 | (Duplicate_Subexpr_No_Checks (Obj), |
5038 | Name_Last, J), | |
70482933 | 5039 | Right_Opnd => |
9a0ddeee | 5040 | Build_Attribute_Reference |
70482933 RK |
5041 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); |
5042 | end loop; | |
5043 | ||
5044 | if Is_Acc then | |
fbf5a39b AC |
5045 | Cond := |
5046 | Make_Or_Else (Loc, | |
5047 | Left_Opnd => | |
5048 | Make_Op_Eq (Loc, | |
5049 | Left_Opnd => Obj, | |
5050 | Right_Opnd => Make_Null (Loc)), | |
5051 | Right_Opnd => Cond); | |
70482933 RK |
5052 | end if; |
5053 | ||
5054 | Rewrite (N, Cond); | |
4818e7b9 | 5055 | Analyze_And_Resolve (N, Restyp); |
fbf5a39b | 5056 | end Check_Subscripts; |
70482933 | 5057 | |
685094bf RD |
5058 | -- These are the cases where constraint checks may be required, |
5059 | -- e.g. records with possible discriminants | |
70482933 RK |
5060 | |
5061 | else | |
5062 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
5063 | -- The expression that is built is the negation of the one that |
5064 | -- is used for checking discriminant constraints. | |
70482933 RK |
5065 | |
5066 | Obj := Relocate_Node (Left_Opnd (N)); | |
5067 | ||
5068 | if Has_Discriminants (Typ) then | |
5069 | Cond := Make_Op_Not (Loc, | |
5070 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
5071 | ||
5072 | if Is_Acc then | |
5073 | Cond := Make_Or_Else (Loc, | |
5074 | Left_Opnd => | |
5075 | Make_Op_Eq (Loc, | |
5076 | Left_Opnd => Obj, | |
5077 | Right_Opnd => Make_Null (Loc)), | |
5078 | Right_Opnd => Cond); | |
5079 | end if; | |
5080 | ||
5081 | else | |
5082 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
5083 | end if; | |
5084 | ||
5085 | Rewrite (N, Cond); | |
4818e7b9 | 5086 | Analyze_And_Resolve (N, Restyp); |
70482933 | 5087 | end if; |
6cce2156 GD |
5088 | |
5089 | -- Ada 2012 (AI05-0149): Handle membership tests applied to an | |
5090 | -- expression of an anonymous access type. This can involve an | |
5091 | -- accessibility test and a tagged type membership test in the | |
5092 | -- case of tagged designated types. | |
5093 | ||
5094 | if Ada_Version >= Ada_2012 | |
5095 | and then Is_Acc | |
5096 | and then Ekind (Ltyp) = E_Anonymous_Access_Type | |
5097 | then | |
5098 | declare | |
5099 | Expr_Entity : Entity_Id := Empty; | |
5100 | New_N : Node_Id; | |
5101 | Param_Level : Node_Id; | |
5102 | Type_Level : Node_Id; | |
996c8821 | 5103 | |
6cce2156 GD |
5104 | begin |
5105 | if Is_Entity_Name (Lop) then | |
5106 | Expr_Entity := Param_Entity (Lop); | |
996c8821 | 5107 | |
6cce2156 GD |
5108 | if not Present (Expr_Entity) then |
5109 | Expr_Entity := Entity (Lop); | |
5110 | end if; | |
5111 | end if; | |
5112 | ||
5113 | -- If a conversion of the anonymous access value to the | |
5114 | -- tested type would be illegal, then the result is False. | |
5115 | ||
5116 | if not Valid_Conversion | |
5117 | (Lop, Rtyp, Lop, Report_Errs => False) | |
5118 | then | |
5119 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
5120 | Analyze_And_Resolve (N, Restyp); | |
5121 | ||
5122 | -- Apply an accessibility check if the access object has an | |
5123 | -- associated access level and when the level of the type is | |
5124 | -- less deep than the level of the access parameter. This | |
5125 | -- only occur for access parameters and stand-alone objects | |
5126 | -- of an anonymous access type. | |
5127 | ||
5128 | else | |
5129 | if Present (Expr_Entity) | |
996c8821 RD |
5130 | and then |
5131 | Present | |
5132 | (Effective_Extra_Accessibility (Expr_Entity)) | |
5133 | and then UI_Gt (Object_Access_Level (Lop), | |
5134 | Type_Access_Level (Rtyp)) | |
6cce2156 GD |
5135 | then |
5136 | Param_Level := | |
5137 | New_Occurrence_Of | |
d15f9422 | 5138 | (Effective_Extra_Accessibility (Expr_Entity), Loc); |
6cce2156 GD |
5139 | |
5140 | Type_Level := | |
5141 | Make_Integer_Literal (Loc, Type_Access_Level (Rtyp)); | |
5142 | ||
5143 | -- Return True only if the accessibility level of the | |
5144 | -- expression entity is not deeper than the level of | |
5145 | -- the tested access type. | |
5146 | ||
5147 | Rewrite (N, | |
5148 | Make_And_Then (Loc, | |
5149 | Left_Opnd => Relocate_Node (N), | |
5150 | Right_Opnd => Make_Op_Le (Loc, | |
5151 | Left_Opnd => Param_Level, | |
5152 | Right_Opnd => Type_Level))); | |
5153 | ||
5154 | Analyze_And_Resolve (N); | |
5155 | end if; | |
5156 | ||
5157 | -- If the designated type is tagged, do tagged membership | |
5158 | -- operation. | |
5159 | ||
5160 | -- *** NOTE: we have to check not null before doing the | |
5161 | -- tagged membership test (but maybe that can be done | |
5162 | -- inside Tagged_Membership?). | |
5163 | ||
5164 | if Is_Tagged_Type (Typ) then | |
5165 | Rewrite (N, | |
5166 | Make_And_Then (Loc, | |
5167 | Left_Opnd => Relocate_Node (N), | |
5168 | Right_Opnd => | |
5169 | Make_Op_Ne (Loc, | |
5170 | Left_Opnd => Obj, | |
5171 | Right_Opnd => Make_Null (Loc)))); | |
5172 | ||
5173 | -- No expansion will be performed when VM_Target, as | |
5174 | -- the VM back-ends will handle the membership tests | |
5175 | -- directly (tags are not explicitly represented in | |
5176 | -- Java objects, so the normal tagged membership | |
5177 | -- expansion is not what we want). | |
5178 | ||
5179 | if Tagged_Type_Expansion then | |
5180 | ||
5181 | -- Note that we have to pass Original_Node, because | |
5182 | -- the membership test might already have been | |
5183 | -- rewritten by earlier parts of membership test. | |
5184 | ||
5185 | Tagged_Membership | |
5186 | (Original_Node (N), SCIL_Node, New_N); | |
5187 | ||
5188 | -- Update decoration of relocated node referenced | |
5189 | -- by the SCIL node. | |
5190 | ||
5191 | if Generate_SCIL and then Present (SCIL_Node) then | |
5192 | Set_SCIL_Node (New_N, SCIL_Node); | |
5193 | end if; | |
5194 | ||
5195 | Rewrite (N, | |
5196 | Make_And_Then (Loc, | |
5197 | Left_Opnd => Relocate_Node (N), | |
5198 | Right_Opnd => New_N)); | |
5199 | ||
5200 | Analyze_And_Resolve (N, Restyp); | |
5201 | end if; | |
5202 | end if; | |
5203 | end if; | |
5204 | end; | |
5205 | end if; | |
70482933 RK |
5206 | end; |
5207 | end if; | |
4818e7b9 RD |
5208 | |
5209 | -- At this point, we have done the processing required for the basic | |
5210 | -- membership test, but not yet dealt with the predicate. | |
5211 | ||
5212 | <<Leave>> | |
5213 | ||
c7532b2d AC |
5214 | -- If a predicate is present, then we do the predicate test, but we |
5215 | -- most certainly want to omit this if we are within the predicate | |
5216 | -- function itself, since otherwise we have an infinite recursion! | |
4818e7b9 | 5217 | |
c7532b2d AC |
5218 | declare |
5219 | PFunc : constant Entity_Id := Predicate_Function (Rtyp); | |
4818e7b9 | 5220 | |
c7532b2d AC |
5221 | begin |
5222 | if Present (PFunc) | |
5223 | and then Current_Scope /= PFunc | |
5224 | then | |
5225 | Rewrite (N, | |
5226 | Make_And_Then (Loc, | |
5227 | Left_Opnd => Relocate_Node (N), | |
5228 | Right_Opnd => Make_Predicate_Call (Rtyp, Lop))); | |
4818e7b9 | 5229 | |
c7532b2d AC |
5230 | -- Analyze new expression, mark left operand as analyzed to |
5231 | -- avoid infinite recursion adding predicate calls. | |
4818e7b9 | 5232 | |
c7532b2d AC |
5233 | Set_Analyzed (Left_Opnd (N)); |
5234 | Analyze_And_Resolve (N, Standard_Boolean); | |
4818e7b9 | 5235 | |
c7532b2d AC |
5236 | -- All done, skip attempt at compile time determination of result |
5237 | ||
5238 | return; | |
5239 | end if; | |
5240 | end; | |
70482933 RK |
5241 | end Expand_N_In; |
5242 | ||
5243 | -------------------------------- | |
5244 | -- Expand_N_Indexed_Component -- | |
5245 | -------------------------------- | |
5246 | ||
5247 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
5248 | Loc : constant Source_Ptr := Sloc (N); | |
5249 | Typ : constant Entity_Id := Etype (N); | |
5250 | P : constant Node_Id := Prefix (N); | |
5251 | T : constant Entity_Id := Etype (P); | |
5972791c | 5252 | Atp : Entity_Id; |
70482933 RK |
5253 | |
5254 | begin | |
685094bf RD |
5255 | -- A special optimization, if we have an indexed component that is |
5256 | -- selecting from a slice, then we can eliminate the slice, since, for | |
5257 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
5258 | -- the range check required by the slice. The range check for the slice | |
5259 | -- itself has already been generated. The range check for the | |
5260 | -- subscripting operation is ensured by converting the subject to | |
5261 | -- the subtype of the slice. | |
5262 | ||
5263 | -- This optimization not only generates better code, avoiding slice | |
5264 | -- messing especially in the packed case, but more importantly bypasses | |
5265 | -- some problems in handling this peculiar case, for example, the issue | |
5266 | -- of dealing specially with object renamings. | |
70482933 RK |
5267 | |
5268 | if Nkind (P) = N_Slice then | |
5269 | Rewrite (N, | |
5270 | Make_Indexed_Component (Loc, | |
5271 | Prefix => Prefix (P), | |
5272 | Expressions => New_List ( | |
5273 | Convert_To | |
5274 | (Etype (First_Index (Etype (P))), | |
5275 | First (Expressions (N)))))); | |
5276 | Analyze_And_Resolve (N, Typ); | |
5277 | return; | |
5278 | end if; | |
5279 | ||
b4592168 GD |
5280 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
5281 | -- function, then additional actuals must be passed. | |
5282 | ||
0791fbe9 | 5283 | if Ada_Version >= Ada_2005 |
b4592168 GD |
5284 | and then Is_Build_In_Place_Function_Call (P) |
5285 | then | |
5286 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
5287 | end if; | |
5288 | ||
685094bf | 5289 | -- If the prefix is an access type, then we unconditionally rewrite if |
09494c32 | 5290 | -- as an explicit dereference. This simplifies processing for several |
685094bf RD |
5291 | -- cases, including packed array cases and certain cases in which checks |
5292 | -- must be generated. We used to try to do this only when it was | |
5293 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
5294 | |
5295 | if Is_Access_Type (T) then | |
2717634d | 5296 | Insert_Explicit_Dereference (P); |
70482933 | 5297 | Analyze_And_Resolve (P, Designated_Type (T)); |
5972791c AC |
5298 | Atp := Designated_Type (T); |
5299 | else | |
5300 | Atp := T; | |
70482933 RK |
5301 | end if; |
5302 | ||
fbf5a39b AC |
5303 | -- Generate index and validity checks |
5304 | ||
5305 | Generate_Index_Checks (N); | |
5306 | ||
70482933 RK |
5307 | if Validity_Checks_On and then Validity_Check_Subscripts then |
5308 | Apply_Subscript_Validity_Checks (N); | |
5309 | end if; | |
5310 | ||
5972791c AC |
5311 | -- If selecting from an array with atomic components, and atomic sync |
5312 | -- is not suppressed for this array type, set atomic sync flag. | |
5313 | ||
5314 | if (Has_Atomic_Components (Atp) | |
5315 | and then not Atomic_Synchronization_Disabled (Atp)) | |
5316 | or else (Is_Atomic (Typ) | |
5317 | and then not Atomic_Synchronization_Disabled (Typ)) | |
5318 | then | |
4c318253 | 5319 | Activate_Atomic_Synchronization (N); |
5972791c AC |
5320 | end if; |
5321 | ||
70482933 RK |
5322 | -- All done for the non-packed case |
5323 | ||
5324 | if not Is_Packed (Etype (Prefix (N))) then | |
5325 | return; | |
5326 | end if; | |
5327 | ||
5328 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 5329 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
5330 | -- we can always use the normal packed element get circuit. |
5331 | ||
5332 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
5333 | Expand_Packed_Element_Reference (N); | |
5334 | return; | |
5335 | end if; | |
5336 | ||
5337 | -- For a reference to a component of a bit packed array, we have to | |
5338 | -- convert it to a reference to the corresponding Packed_Array_Type. | |
5339 | -- We only want to do this for simple references, and not for: | |
5340 | ||
685094bf RD |
5341 | -- Left side of assignment, or prefix of left side of assignment, or |
5342 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
5343 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
5344 | ||
5345 | -- Renaming objects in renaming associations | |
5346 | -- This case is handled when a use of the renamed variable occurs | |
5347 | ||
5348 | -- Actual parameters for a procedure call | |
5349 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
5350 | ||
5351 | -- The second expression in a 'Read attribute reference | |
5352 | ||
47d3b920 | 5353 | -- The prefix of an address or bit or size attribute reference |
70482933 RK |
5354 | |
5355 | -- The following circuit detects these exceptions | |
5356 | ||
5357 | declare | |
5358 | Child : Node_Id := N; | |
5359 | Parnt : Node_Id := Parent (N); | |
5360 | ||
5361 | begin | |
5362 | loop | |
5363 | if Nkind (Parnt) = N_Unchecked_Expression then | |
5364 | null; | |
5365 | ||
303b4d58 AC |
5366 | elsif Nkind_In (Parnt, N_Object_Renaming_Declaration, |
5367 | N_Procedure_Call_Statement) | |
70482933 RK |
5368 | or else (Nkind (Parnt) = N_Parameter_Association |
5369 | and then | |
5370 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
5371 | then | |
5372 | return; | |
5373 | ||
5374 | elsif Nkind (Parnt) = N_Attribute_Reference | |
5375 | and then (Attribute_Name (Parnt) = Name_Address | |
5376 | or else | |
47d3b920 AC |
5377 | Attribute_Name (Parnt) = Name_Bit |
5378 | or else | |
70482933 RK |
5379 | Attribute_Name (Parnt) = Name_Size) |
5380 | and then Prefix (Parnt) = Child | |
5381 | then | |
5382 | return; | |
5383 | ||
5384 | elsif Nkind (Parnt) = N_Assignment_Statement | |
5385 | and then Name (Parnt) = Child | |
5386 | then | |
5387 | return; | |
5388 | ||
685094bf RD |
5389 | -- If the expression is an index of an indexed component, it must |
5390 | -- be expanded regardless of context. | |
fbf5a39b AC |
5391 | |
5392 | elsif Nkind (Parnt) = N_Indexed_Component | |
5393 | and then Child /= Prefix (Parnt) | |
5394 | then | |
5395 | Expand_Packed_Element_Reference (N); | |
5396 | return; | |
5397 | ||
5398 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
5399 | and then Name (Parent (Parnt)) = Parnt | |
5400 | then | |
5401 | return; | |
5402 | ||
70482933 RK |
5403 | elsif Nkind (Parnt) = N_Attribute_Reference |
5404 | and then Attribute_Name (Parnt) = Name_Read | |
5405 | and then Next (First (Expressions (Parnt))) = Child | |
5406 | then | |
5407 | return; | |
5408 | ||
303b4d58 | 5409 | elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component) |
70482933 RK |
5410 | and then Prefix (Parnt) = Child |
5411 | then | |
5412 | null; | |
5413 | ||
5414 | else | |
5415 | Expand_Packed_Element_Reference (N); | |
5416 | return; | |
5417 | end if; | |
5418 | ||
685094bf RD |
5419 | -- Keep looking up tree for unchecked expression, or if we are the |
5420 | -- prefix of a possible assignment left side. | |
70482933 RK |
5421 | |
5422 | Child := Parnt; | |
5423 | Parnt := Parent (Child); | |
5424 | end loop; | |
5425 | end; | |
70482933 RK |
5426 | end Expand_N_Indexed_Component; |
5427 | ||
5428 | --------------------- | |
5429 | -- Expand_N_Not_In -- | |
5430 | --------------------- | |
5431 | ||
5432 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
5433 | -- can be done. This avoids needing to duplicate this expansion code. | |
5434 | ||
5435 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
5436 | Loc : constant Source_Ptr := Sloc (N); |
5437 | Typ : constant Entity_Id := Etype (N); | |
5438 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
5439 | |
5440 | begin | |
5441 | Rewrite (N, | |
5442 | Make_Op_Not (Loc, | |
5443 | Right_Opnd => | |
5444 | Make_In (Loc, | |
5445 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 5446 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 5447 | |
197e4514 AC |
5448 | -- If this is a set membership, preserve list of alternatives |
5449 | ||
5450 | Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N))); | |
5451 | ||
d766cee3 | 5452 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 5453 | -- transformations in Expand_N_In). |
630d30e9 RD |
5454 | |
5455 | Set_Comes_From_Source (N, Cfs); | |
5456 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
5457 | ||
8fc789c8 | 5458 | -- Now analyze transformed node |
630d30e9 | 5459 | |
70482933 RK |
5460 | Analyze_And_Resolve (N, Typ); |
5461 | end Expand_N_Not_In; | |
5462 | ||
5463 | ------------------- | |
5464 | -- Expand_N_Null -- | |
5465 | ------------------- | |
5466 | ||
a3f2babd AC |
5467 | -- The only replacement required is for the case of a null of a type that |
5468 | -- is an access to protected subprogram, or a subtype thereof. We represent | |
5469 | -- such access values as a record, and so we must replace the occurrence of | |
5470 | -- null by the equivalent record (with a null address and a null pointer in | |
5471 | -- it), so that the backend creates the proper value. | |
70482933 RK |
5472 | |
5473 | procedure Expand_N_Null (N : Node_Id) is | |
5474 | Loc : constant Source_Ptr := Sloc (N); | |
a3f2babd | 5475 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
70482933 RK |
5476 | Agg : Node_Id; |
5477 | ||
5478 | begin | |
26bff3d9 | 5479 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
5480 | Agg := |
5481 | Make_Aggregate (Loc, | |
5482 | Expressions => New_List ( | |
5483 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
5484 | Make_Null (Loc))); | |
5485 | ||
5486 | Rewrite (N, Agg); | |
5487 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
5488 | ||
685094bf RD |
5489 | -- For subsequent semantic analysis, the node must retain its type. |
5490 | -- Gigi in any case replaces this type by the corresponding record | |
5491 | -- type before processing the node. | |
70482933 RK |
5492 | |
5493 | Set_Etype (N, Typ); | |
5494 | end if; | |
fbf5a39b AC |
5495 | |
5496 | exception | |
5497 | when RE_Not_Available => | |
5498 | return; | |
70482933 RK |
5499 | end Expand_N_Null; |
5500 | ||
5501 | --------------------- | |
5502 | -- Expand_N_Op_Abs -- | |
5503 | --------------------- | |
5504 | ||
5505 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
5506 | Loc : constant Source_Ptr := Sloc (N); | |
5507 | Expr : constant Node_Id := Right_Opnd (N); | |
5508 | ||
5509 | begin | |
5510 | Unary_Op_Validity_Checks (N); | |
5511 | ||
5512 | -- Deal with software overflow checking | |
5513 | ||
07fc65c4 | 5514 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
5515 | and then Is_Signed_Integer_Type (Etype (N)) |
5516 | and then Do_Overflow_Check (N) | |
5517 | then | |
685094bf RD |
5518 | -- The only case to worry about is when the argument is equal to the |
5519 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 5520 | |
fbf5a39b | 5521 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
5522 | |
5523 | -- with the usual Duplicate_Subexpr use coding for expr | |
5524 | ||
fbf5a39b AC |
5525 | Insert_Action (N, |
5526 | Make_Raise_Constraint_Error (Loc, | |
5527 | Condition => | |
5528 | Make_Op_Eq (Loc, | |
70482933 | 5529 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
5530 | Right_Opnd => |
5531 | Make_Attribute_Reference (Loc, | |
5532 | Prefix => | |
5533 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), | |
5534 | Attribute_Name => Name_First)), | |
5535 | Reason => CE_Overflow_Check_Failed)); | |
5536 | end if; | |
70482933 RK |
5537 | |
5538 | -- Vax floating-point types case | |
5539 | ||
fbf5a39b | 5540 | if Vax_Float (Etype (N)) then |
70482933 RK |
5541 | Expand_Vax_Arith (N); |
5542 | end if; | |
5543 | end Expand_N_Op_Abs; | |
5544 | ||
5545 | --------------------- | |
5546 | -- Expand_N_Op_Add -- | |
5547 | --------------------- | |
5548 | ||
5549 | procedure Expand_N_Op_Add (N : Node_Id) is | |
5550 | Typ : constant Entity_Id := Etype (N); | |
5551 | ||
5552 | begin | |
5553 | Binary_Op_Validity_Checks (N); | |
5554 | ||
5555 | -- N + 0 = 0 + N = N for integer types | |
5556 | ||
5557 | if Is_Integer_Type (Typ) then | |
5558 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
5559 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
5560 | then | |
5561 | Rewrite (N, Left_Opnd (N)); | |
5562 | return; | |
5563 | ||
5564 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
5565 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
5566 | then | |
5567 | Rewrite (N, Right_Opnd (N)); | |
5568 | return; | |
5569 | end if; | |
5570 | end if; | |
5571 | ||
fbf5a39b | 5572 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 RK |
5573 | |
5574 | if Is_Signed_Integer_Type (Typ) | |
5575 | or else Is_Fixed_Point_Type (Typ) | |
5576 | then | |
5577 | Apply_Arithmetic_Overflow_Check (N); | |
5578 | return; | |
5579 | ||
5580 | -- Vax floating-point types case | |
5581 | ||
5582 | elsif Vax_Float (Typ) then | |
5583 | Expand_Vax_Arith (N); | |
5584 | end if; | |
5585 | end Expand_N_Op_Add; | |
5586 | ||
5587 | --------------------- | |
5588 | -- Expand_N_Op_And -- | |
5589 | --------------------- | |
5590 | ||
5591 | procedure Expand_N_Op_And (N : Node_Id) is | |
5592 | Typ : constant Entity_Id := Etype (N); | |
5593 | ||
5594 | begin | |
5595 | Binary_Op_Validity_Checks (N); | |
5596 | ||
5597 | if Is_Array_Type (Etype (N)) then | |
5598 | Expand_Boolean_Operator (N); | |
5599 | ||
5600 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
5601 | Adjust_Condition (Left_Opnd (N)); |
5602 | Adjust_Condition (Right_Opnd (N)); | |
5603 | Set_Etype (N, Standard_Boolean); | |
5604 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
5605 | |
5606 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
5607 | Expand_Intrinsic_Call (N, Entity (N)); | |
5608 | ||
70482933 RK |
5609 | end if; |
5610 | end Expand_N_Op_And; | |
5611 | ||
5612 | ------------------------ | |
5613 | -- Expand_N_Op_Concat -- | |
5614 | ------------------------ | |
5615 | ||
5616 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
5617 | Opnds : List_Id; |
5618 | -- List of operands to be concatenated | |
5619 | ||
70482933 | 5620 | Cnode : Node_Id; |
685094bf RD |
5621 | -- Node which is to be replaced by the result of concatenating the nodes |
5622 | -- in the list Opnds. | |
70482933 | 5623 | |
70482933 | 5624 | begin |
fbf5a39b AC |
5625 | -- Ensure validity of both operands |
5626 | ||
70482933 RK |
5627 | Binary_Op_Validity_Checks (N); |
5628 | ||
685094bf RD |
5629 | -- If we are the left operand of a concatenation higher up the tree, |
5630 | -- then do nothing for now, since we want to deal with a series of | |
5631 | -- concatenations as a unit. | |
70482933 RK |
5632 | |
5633 | if Nkind (Parent (N)) = N_Op_Concat | |
5634 | and then N = Left_Opnd (Parent (N)) | |
5635 | then | |
5636 | return; | |
5637 | end if; | |
5638 | ||
5639 | -- We get here with a concatenation whose left operand may be a | |
5640 | -- concatenation itself with a consistent type. We need to process | |
5641 | -- these concatenation operands from left to right, which means | |
5642 | -- from the deepest node in the tree to the highest node. | |
5643 | ||
5644 | Cnode := N; | |
5645 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
5646 | Cnode := Left_Opnd (Cnode); | |
5647 | end loop; | |
5648 | ||
64425dff BD |
5649 | -- Now Cnode is the deepest concatenation, and its parents are the |
5650 | -- concatenation nodes above, so now we process bottom up, doing the | |
5651 | -- operations. We gather a string that is as long as possible up to five | |
5652 | -- operands. | |
70482933 | 5653 | |
df46b832 AC |
5654 | -- The outer loop runs more than once if more than one concatenation |
5655 | -- type is involved. | |
70482933 RK |
5656 | |
5657 | Outer : loop | |
5658 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
5659 | Set_Parent (Opnds, N); | |
5660 | ||
df46b832 | 5661 | -- The inner loop gathers concatenation operands |
70482933 RK |
5662 | |
5663 | Inner : while Cnode /= N | |
70482933 RK |
5664 | and then Base_Type (Etype (Cnode)) = |
5665 | Base_Type (Etype (Parent (Cnode))) | |
5666 | loop | |
5667 | Cnode := Parent (Cnode); | |
5668 | Append (Right_Opnd (Cnode), Opnds); | |
5669 | end loop Inner; | |
5670 | ||
fdac1f80 | 5671 | Expand_Concatenate (Cnode, Opnds); |
70482933 RK |
5672 | |
5673 | exit Outer when Cnode = N; | |
5674 | Cnode := Parent (Cnode); | |
5675 | end loop Outer; | |
5676 | end Expand_N_Op_Concat; | |
5677 | ||
5678 | ------------------------ | |
5679 | -- Expand_N_Op_Divide -- | |
5680 | ------------------------ | |
5681 | ||
5682 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
5683 | Loc : constant Source_Ptr := Sloc (N); |
5684 | Lopnd : constant Node_Id := Left_Opnd (N); | |
5685 | Ropnd : constant Node_Id := Right_Opnd (N); | |
5686 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
5687 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
5688 | Typ : Entity_Id := Etype (N); | |
5689 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
5690 | and then | |
5691 | Compile_Time_Known_Value (Ropnd); | |
5692 | Rval : Uint; | |
70482933 RK |
5693 | |
5694 | begin | |
5695 | Binary_Op_Validity_Checks (N); | |
5696 | ||
f82944b7 JM |
5697 | if Rknow then |
5698 | Rval := Expr_Value (Ropnd); | |
5699 | end if; | |
5700 | ||
70482933 RK |
5701 | -- N / 1 = N for integer types |
5702 | ||
f82944b7 JM |
5703 | if Rknow and then Rval = Uint_1 then |
5704 | Rewrite (N, Lopnd); | |
70482933 RK |
5705 | return; |
5706 | end if; | |
5707 | ||
5708 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
5709 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
5710 | -- operand is an unsigned integer, as required for this to work. | |
5711 | ||
f82944b7 JM |
5712 | if Nkind (Ropnd) = N_Op_Expon |
5713 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
5714 | |
5715 | -- We cannot do this transformation in configurable run time mode if we | |
51bf9bdf | 5716 | -- have 64-bit integers and long shifts are not available. |
fbf5a39b AC |
5717 | |
5718 | and then | |
5719 | (Esize (Ltyp) <= 32 | |
5720 | or else Support_Long_Shifts_On_Target) | |
70482933 RK |
5721 | then |
5722 | Rewrite (N, | |
5723 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 5724 | Left_Opnd => Lopnd, |
70482933 | 5725 | Right_Opnd => |
f82944b7 | 5726 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
5727 | Analyze_And_Resolve (N, Typ); |
5728 | return; | |
5729 | end if; | |
5730 | ||
5731 | -- Do required fixup of universal fixed operation | |
5732 | ||
5733 | if Typ = Universal_Fixed then | |
5734 | Fixup_Universal_Fixed_Operation (N); | |
5735 | Typ := Etype (N); | |
5736 | end if; | |
5737 | ||
5738 | -- Divisions with fixed-point results | |
5739 | ||
5740 | if Is_Fixed_Point_Type (Typ) then | |
5741 | ||
685094bf RD |
5742 | -- No special processing if Treat_Fixed_As_Integer is set, since |
5743 | -- from a semantic point of view such operations are simply integer | |
5744 | -- operations and will be treated that way. | |
70482933 RK |
5745 | |
5746 | if not Treat_Fixed_As_Integer (N) then | |
5747 | if Is_Integer_Type (Rtyp) then | |
5748 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
5749 | else | |
5750 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
5751 | end if; | |
5752 | end if; | |
5753 | ||
685094bf RD |
5754 | -- Other cases of division of fixed-point operands. Again we exclude the |
5755 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 RK |
5756 | |
5757 | elsif (Is_Fixed_Point_Type (Ltyp) or else | |
5758 | Is_Fixed_Point_Type (Rtyp)) | |
5759 | and then not Treat_Fixed_As_Integer (N) | |
5760 | then | |
5761 | if Is_Integer_Type (Typ) then | |
5762 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
5763 | else | |
5764 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
5765 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
5766 | end if; | |
5767 | ||
685094bf RD |
5768 | -- Mixed-mode operations can appear in a non-static universal context, |
5769 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
5770 | |
5771 | elsif Typ = Universal_Real | |
5772 | and then Is_Integer_Type (Rtyp) | |
5773 | then | |
f82944b7 JM |
5774 | Rewrite (Ropnd, |
5775 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 5776 | |
f82944b7 | 5777 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 RK |
5778 | |
5779 | elsif Typ = Universal_Real | |
5780 | and then Is_Integer_Type (Ltyp) | |
5781 | then | |
f82944b7 JM |
5782 | Rewrite (Lopnd, |
5783 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 5784 | |
f82944b7 | 5785 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 5786 | |
f02b8bb8 | 5787 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
5788 | |
5789 | elsif Is_Integer_Type (Typ) then | |
5790 | Apply_Divide_Check (N); | |
fbf5a39b | 5791 | |
f02b8bb8 RD |
5792 | -- Deal with Vax_Float |
5793 | ||
5794 | elsif Vax_Float (Typ) then | |
5795 | Expand_Vax_Arith (N); | |
5796 | return; | |
70482933 RK |
5797 | end if; |
5798 | end Expand_N_Op_Divide; | |
5799 | ||
5800 | -------------------- | |
5801 | -- Expand_N_Op_Eq -- | |
5802 | -------------------- | |
5803 | ||
5804 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
5805 | Loc : constant Source_Ptr := Sloc (N); |
5806 | Typ : constant Entity_Id := Etype (N); | |
5807 | Lhs : constant Node_Id := Left_Opnd (N); | |
5808 | Rhs : constant Node_Id := Right_Opnd (N); | |
5809 | Bodies : constant List_Id := New_List; | |
5810 | A_Typ : constant Entity_Id := Etype (Lhs); | |
5811 | ||
70482933 RK |
5812 | Typl : Entity_Id := A_Typ; |
5813 | Op_Name : Entity_Id; | |
5814 | Prim : Elmt_Id; | |
70482933 RK |
5815 | |
5816 | procedure Build_Equality_Call (Eq : Entity_Id); | |
5817 | -- If a constructed equality exists for the type or for its parent, | |
5818 | -- build and analyze call, adding conversions if the operation is | |
5819 | -- inherited. | |
5820 | ||
5d09245e | 5821 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
8fc789c8 | 5822 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
5823 | -- Unchecked_Union subtype. Typ is a record type. |
5824 | ||
70482933 RK |
5825 | ------------------------- |
5826 | -- Build_Equality_Call -- | |
5827 | ------------------------- | |
5828 | ||
5829 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
5830 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
5831 | L_Exp : Node_Id := Relocate_Node (Lhs); | |
5832 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
5833 | ||
5834 | begin | |
5835 | if Base_Type (Op_Type) /= Base_Type (A_Typ) | |
5836 | and then not Is_Class_Wide_Type (A_Typ) | |
5837 | then | |
5838 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
5839 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
5840 | end if; | |
5841 | ||
5d09245e AC |
5842 | -- If we have an Unchecked_Union, we need to add the inferred |
5843 | -- discriminant values as actuals in the function call. At this | |
5844 | -- point, the expansion has determined that both operands have | |
5845 | -- inferable discriminants. | |
5846 | ||
5847 | if Is_Unchecked_Union (Op_Type) then | |
5848 | declare | |
5849 | Lhs_Type : constant Node_Id := Etype (L_Exp); | |
5850 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
5851 | Lhs_Discr_Val : Node_Id; | |
5852 | Rhs_Discr_Val : Node_Id; | |
5853 | ||
5854 | begin | |
5855 | -- Per-object constrained selected components require special | |
5856 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 5857 | -- Unchecked_Union, we cannot reference its discriminants |
5d09245e AC |
5858 | -- directly. This is why we use the two extra parameters of |
5859 | -- the equality function of the enclosing Unchecked_Union. | |
5860 | ||
5861 | -- type UU_Type (Discr : Integer := 0) is | |
5862 | -- . . . | |
5863 | -- end record; | |
5864 | -- pragma Unchecked_Union (UU_Type); | |
5865 | ||
5866 | -- 1. Unchecked_Union enclosing record: | |
5867 | ||
5868 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
5869 | -- . . . | |
5870 | -- Comp : UU_Type (Discr); | |
5871 | -- . . . | |
5872 | -- end Enclosing_UU_Type; | |
5873 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
5874 | ||
5875 | -- Obj1 : Enclosing_UU_Type; | |
5876 | -- Obj2 : Enclosing_UU_Type (1); | |
5877 | ||
2717634d | 5878 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
5879 | |
5880 | -- Generated code: | |
5881 | ||
5882 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
5883 | ||
5884 | -- A and B are the formal parameters of the equality function | |
5885 | -- of Enclosing_UU_Type. The function always has two extra | |
5886 | -- formals to capture the inferred discriminant values. | |
5887 | ||
5888 | -- 2. Non-Unchecked_Union enclosing record: | |
5889 | ||
5890 | -- type | |
5891 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
5892 | -- is record | |
5893 | -- . . . | |
5894 | -- Comp : UU_Type (Discr); | |
5895 | -- . . . | |
5896 | -- end Enclosing_Non_UU_Type; | |
5897 | ||
5898 | -- Obj1 : Enclosing_Non_UU_Type; | |
5899 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
5900 | ||
630d30e9 | 5901 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
5902 | |
5903 | -- Generated code: | |
5904 | ||
5905 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
5906 | -- obj1.discr, obj2.discr)) then | |
5907 | ||
5908 | -- In this case we can directly reference the discriminants of | |
5909 | -- the enclosing record. | |
5910 | ||
5911 | -- Lhs of equality | |
5912 | ||
5913 | if Nkind (Lhs) = N_Selected_Component | |
5e1c00fa RD |
5914 | and then Has_Per_Object_Constraint |
5915 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
5916 | then |
5917 | -- Enclosing record is an Unchecked_Union, use formal A | |
5918 | ||
7675ad4f AC |
5919 | if Is_Unchecked_Union |
5920 | (Scope (Entity (Selector_Name (Lhs)))) | |
5d09245e | 5921 | then |
7675ad4f | 5922 | Lhs_Discr_Val := Make_Identifier (Loc, Name_A); |
5d09245e AC |
5923 | |
5924 | -- Enclosing record is of a non-Unchecked_Union type, it is | |
5925 | -- possible to reference the discriminant. | |
5926 | ||
5927 | else | |
5928 | Lhs_Discr_Val := | |
5929 | Make_Selected_Component (Loc, | |
5930 | Prefix => Prefix (Lhs), | |
5931 | Selector_Name => | |
5e1c00fa RD |
5932 | New_Copy |
5933 | (Get_Discriminant_Value | |
5934 | (First_Discriminant (Lhs_Type), | |
5935 | Lhs_Type, | |
5936 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
5937 | end if; |
5938 | ||
5939 | -- Comment needed here ??? | |
5940 | ||
5941 | else | |
5942 | -- Infer the discriminant value | |
5943 | ||
5944 | Lhs_Discr_Val := | |
5e1c00fa RD |
5945 | New_Copy |
5946 | (Get_Discriminant_Value | |
5947 | (First_Discriminant (Lhs_Type), | |
5948 | Lhs_Type, | |
5949 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
5950 | end if; |
5951 | ||
5952 | -- Rhs of equality | |
5953 | ||
5954 | if Nkind (Rhs) = N_Selected_Component | |
5e1c00fa RD |
5955 | and then Has_Per_Object_Constraint |
5956 | (Entity (Selector_Name (Rhs))) | |
5d09245e | 5957 | then |
5e1c00fa RD |
5958 | if Is_Unchecked_Union |
5959 | (Scope (Entity (Selector_Name (Rhs)))) | |
5d09245e | 5960 | then |
7675ad4f | 5961 | Rhs_Discr_Val := Make_Identifier (Loc, Name_B); |
5d09245e AC |
5962 | |
5963 | else | |
5964 | Rhs_Discr_Val := | |
5965 | Make_Selected_Component (Loc, | |
5966 | Prefix => Prefix (Rhs), | |
5967 | Selector_Name => | |
5968 | New_Copy (Get_Discriminant_Value ( | |
5969 | First_Discriminant (Rhs_Type), | |
5970 | Rhs_Type, | |
5971 | Stored_Constraint (Rhs_Type)))); | |
5972 | ||
5973 | end if; | |
5974 | else | |
5975 | Rhs_Discr_Val := | |
5976 | New_Copy (Get_Discriminant_Value ( | |
5977 | First_Discriminant (Rhs_Type), | |
5978 | Rhs_Type, | |
5979 | Stored_Constraint (Rhs_Type))); | |
5980 | ||
5981 | end if; | |
5982 | ||
5983 | Rewrite (N, | |
5984 | Make_Function_Call (Loc, | |
5985 | Name => New_Reference_To (Eq, Loc), | |
5986 | Parameter_Associations => New_List ( | |
5987 | L_Exp, | |
5988 | R_Exp, | |
5989 | Lhs_Discr_Val, | |
5990 | Rhs_Discr_Val))); | |
5991 | end; | |
5992 | ||
5993 | -- Normal case, not an unchecked union | |
5994 | ||
5995 | else | |
5996 | Rewrite (N, | |
5997 | Make_Function_Call (Loc, | |
5998 | Name => New_Reference_To (Eq, Loc), | |
5999 | Parameter_Associations => New_List (L_Exp, R_Exp))); | |
6000 | end if; | |
70482933 RK |
6001 | |
6002 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
6003 | end Build_Equality_Call; | |
6004 | ||
5d09245e AC |
6005 | ------------------------------------ |
6006 | -- Has_Unconstrained_UU_Component -- | |
6007 | ------------------------------------ | |
6008 | ||
6009 | function Has_Unconstrained_UU_Component | |
6010 | (Typ : Node_Id) return Boolean | |
6011 | is | |
6012 | Tdef : constant Node_Id := | |
57848bf7 | 6013 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
6014 | Clist : Node_Id; |
6015 | Vpart : Node_Id; | |
6016 | ||
6017 | function Component_Is_Unconstrained_UU | |
6018 | (Comp : Node_Id) return Boolean; | |
6019 | -- Determines whether the subtype of the component is an | |
6020 | -- unconstrained Unchecked_Union. | |
6021 | ||
6022 | function Variant_Is_Unconstrained_UU | |
6023 | (Variant : Node_Id) return Boolean; | |
6024 | -- Determines whether a component of the variant has an unconstrained | |
6025 | -- Unchecked_Union subtype. | |
6026 | ||
6027 | ----------------------------------- | |
6028 | -- Component_Is_Unconstrained_UU -- | |
6029 | ----------------------------------- | |
6030 | ||
6031 | function Component_Is_Unconstrained_UU | |
6032 | (Comp : Node_Id) return Boolean | |
6033 | is | |
6034 | begin | |
6035 | if Nkind (Comp) /= N_Component_Declaration then | |
6036 | return False; | |
6037 | end if; | |
6038 | ||
6039 | declare | |
6040 | Sindic : constant Node_Id := | |
6041 | Subtype_Indication (Component_Definition (Comp)); | |
6042 | ||
6043 | begin | |
6044 | -- Unconstrained nominal type. In the case of a constraint | |
6045 | -- present, the node kind would have been N_Subtype_Indication. | |
6046 | ||
6047 | if Nkind (Sindic) = N_Identifier then | |
6048 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
6049 | end if; | |
6050 | ||
6051 | return False; | |
6052 | end; | |
6053 | end Component_Is_Unconstrained_UU; | |
6054 | ||
6055 | --------------------------------- | |
6056 | -- Variant_Is_Unconstrained_UU -- | |
6057 | --------------------------------- | |
6058 | ||
6059 | function Variant_Is_Unconstrained_UU | |
6060 | (Variant : Node_Id) return Boolean | |
6061 | is | |
6062 | Clist : constant Node_Id := Component_List (Variant); | |
6063 | ||
6064 | begin | |
6065 | if Is_Empty_List (Component_Items (Clist)) then | |
6066 | return False; | |
6067 | end if; | |
6068 | ||
f02b8bb8 RD |
6069 | -- We only need to test one component |
6070 | ||
5d09245e AC |
6071 | declare |
6072 | Comp : Node_Id := First (Component_Items (Clist)); | |
6073 | ||
6074 | begin | |
6075 | while Present (Comp) loop | |
5d09245e AC |
6076 | if Component_Is_Unconstrained_UU (Comp) then |
6077 | return True; | |
6078 | end if; | |
6079 | ||
6080 | Next (Comp); | |
6081 | end loop; | |
6082 | end; | |
6083 | ||
6084 | -- None of the components withing the variant were of | |
6085 | -- unconstrained Unchecked_Union type. | |
6086 | ||
6087 | return False; | |
6088 | end Variant_Is_Unconstrained_UU; | |
6089 | ||
6090 | -- Start of processing for Has_Unconstrained_UU_Component | |
6091 | ||
6092 | begin | |
6093 | if Null_Present (Tdef) then | |
6094 | return False; | |
6095 | end if; | |
6096 | ||
6097 | Clist := Component_List (Tdef); | |
6098 | Vpart := Variant_Part (Clist); | |
6099 | ||
6100 | -- Inspect available components | |
6101 | ||
6102 | if Present (Component_Items (Clist)) then | |
6103 | declare | |
6104 | Comp : Node_Id := First (Component_Items (Clist)); | |
6105 | ||
6106 | begin | |
6107 | while Present (Comp) loop | |
6108 | ||
8fc789c8 | 6109 | -- One component is sufficient |
5d09245e AC |
6110 | |
6111 | if Component_Is_Unconstrained_UU (Comp) then | |
6112 | return True; | |
6113 | end if; | |
6114 | ||
6115 | Next (Comp); | |
6116 | end loop; | |
6117 | end; | |
6118 | end if; | |
6119 | ||
6120 | -- Inspect available components withing variants | |
6121 | ||
6122 | if Present (Vpart) then | |
6123 | declare | |
6124 | Variant : Node_Id := First (Variants (Vpart)); | |
6125 | ||
6126 | begin | |
6127 | while Present (Variant) loop | |
6128 | ||
8fc789c8 | 6129 | -- One component within a variant is sufficient |
5d09245e AC |
6130 | |
6131 | if Variant_Is_Unconstrained_UU (Variant) then | |
6132 | return True; | |
6133 | end if; | |
6134 | ||
6135 | Next (Variant); | |
6136 | end loop; | |
6137 | end; | |
6138 | end if; | |
6139 | ||
6140 | -- Neither the available components, nor the components inside the | |
6141 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
6142 | ||
6143 | return False; | |
6144 | end Has_Unconstrained_UU_Component; | |
6145 | ||
70482933 RK |
6146 | -- Start of processing for Expand_N_Op_Eq |
6147 | ||
6148 | begin | |
6149 | Binary_Op_Validity_Checks (N); | |
6150 | ||
6151 | if Ekind (Typl) = E_Private_Type then | |
6152 | Typl := Underlying_Type (Typl); | |
70482933 RK |
6153 | elsif Ekind (Typl) = E_Private_Subtype then |
6154 | Typl := Underlying_Type (Base_Type (Typl)); | |
f02b8bb8 RD |
6155 | else |
6156 | null; | |
70482933 RK |
6157 | end if; |
6158 | ||
6159 | -- It may happen in error situations that the underlying type is not | |
6160 | -- set. The error will be detected later, here we just defend the | |
6161 | -- expander code. | |
6162 | ||
6163 | if No (Typl) then | |
6164 | return; | |
6165 | end if; | |
6166 | ||
6167 | Typl := Base_Type (Typl); | |
6168 | ||
70482933 RK |
6169 | -- Boolean types (requiring handling of non-standard case) |
6170 | ||
f02b8bb8 | 6171 | if Is_Boolean_Type (Typl) then |
70482933 RK |
6172 | Adjust_Condition (Left_Opnd (N)); |
6173 | Adjust_Condition (Right_Opnd (N)); | |
6174 | Set_Etype (N, Standard_Boolean); | |
6175 | Adjust_Result_Type (N, Typ); | |
6176 | ||
6177 | -- Array types | |
6178 | ||
6179 | elsif Is_Array_Type (Typl) then | |
6180 | ||
1033834f RD |
6181 | -- If we are doing full validity checking, and it is possible for the |
6182 | -- array elements to be invalid then expand out array comparisons to | |
6183 | -- make sure that we check the array elements. | |
fbf5a39b | 6184 | |
1033834f RD |
6185 | if Validity_Check_Operands |
6186 | and then not Is_Known_Valid (Component_Type (Typl)) | |
6187 | then | |
fbf5a39b AC |
6188 | declare |
6189 | Save_Force_Validity_Checks : constant Boolean := | |
6190 | Force_Validity_Checks; | |
6191 | begin | |
6192 | Force_Validity_Checks := True; | |
6193 | Rewrite (N, | |
0da2c8ac AC |
6194 | Expand_Array_Equality |
6195 | (N, | |
6196 | Relocate_Node (Lhs), | |
6197 | Relocate_Node (Rhs), | |
6198 | Bodies, | |
6199 | Typl)); | |
6200 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
6201 | Analyze_And_Resolve (N, Standard_Boolean); |
6202 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
6203 | end; | |
6204 | ||
a9d8907c | 6205 | -- Packed case where both operands are known aligned |
70482933 | 6206 | |
a9d8907c JM |
6207 | elsif Is_Bit_Packed_Array (Typl) |
6208 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
6209 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
6210 | then | |
70482933 RK |
6211 | Expand_Packed_Eq (N); |
6212 | ||
5e1c00fa RD |
6213 | -- Where the component type is elementary we can use a block bit |
6214 | -- comparison (if supported on the target) exception in the case | |
6215 | -- of floating-point (negative zero issues require element by | |
6216 | -- element comparison), and atomic types (where we must be sure | |
a9d8907c | 6217 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 6218 | |
70482933 RK |
6219 | elsif Is_Elementary_Type (Component_Type (Typl)) |
6220 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 6221 | and then not Is_Atomic (Component_Type (Typl)) |
a9d8907c JM |
6222 | and then not Is_Possibly_Unaligned_Object (Lhs) |
6223 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
fbf5a39b | 6224 | and then Support_Composite_Compare_On_Target |
70482933 RK |
6225 | then |
6226 | null; | |
6227 | ||
685094bf RD |
6228 | -- For composite and floating-point cases, expand equality loop to |
6229 | -- make sure of using proper comparisons for tagged types, and | |
6230 | -- correctly handling the floating-point case. | |
70482933 RK |
6231 | |
6232 | else | |
6233 | Rewrite (N, | |
0da2c8ac AC |
6234 | Expand_Array_Equality |
6235 | (N, | |
6236 | Relocate_Node (Lhs), | |
6237 | Relocate_Node (Rhs), | |
6238 | Bodies, | |
6239 | Typl)); | |
70482933 RK |
6240 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
6241 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
6242 | end if; | |
6243 | ||
6244 | -- Record Types | |
6245 | ||
6246 | elsif Is_Record_Type (Typl) then | |
6247 | ||
6248 | -- For tagged types, use the primitive "=" | |
6249 | ||
6250 | if Is_Tagged_Type (Typl) then | |
6251 | ||
0669bebe GB |
6252 | -- No need to do anything else compiling under restriction |
6253 | -- No_Dispatching_Calls. During the semantic analysis we | |
6254 | -- already notified such violation. | |
6255 | ||
6256 | if Restriction_Active (No_Dispatching_Calls) then | |
6257 | return; | |
6258 | end if; | |
6259 | ||
685094bf RD |
6260 | -- If this is derived from an untagged private type completed with |
6261 | -- a tagged type, it does not have a full view, so we use the | |
6262 | -- primitive operations of the private type. This check should no | |
6263 | -- longer be necessary when these types get their full views??? | |
70482933 RK |
6264 | |
6265 | if Is_Private_Type (A_Typ) | |
6266 | and then not Is_Tagged_Type (A_Typ) | |
6267 | and then Is_Derived_Type (A_Typ) | |
6268 | and then No (Full_View (A_Typ)) | |
6269 | then | |
685094bf RD |
6270 | -- Search for equality operation, checking that the operands |
6271 | -- have the same type. Note that we must find a matching entry, | |
6272 | -- or something is very wrong! | |
2e071734 | 6273 | |
70482933 RK |
6274 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
6275 | ||
2e071734 AC |
6276 | while Present (Prim) loop |
6277 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
6278 | and then Etype (First_Formal (Node (Prim))) = | |
6279 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
6280 | and then | |
6281 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
6282 | ||
70482933 | 6283 | Next_Elmt (Prim); |
70482933 RK |
6284 | end loop; |
6285 | ||
2e071734 | 6286 | pragma Assert (Present (Prim)); |
70482933 | 6287 | Op_Name := Node (Prim); |
fbf5a39b AC |
6288 | |
6289 | -- Find the type's predefined equality or an overriding | |
685094bf | 6290 | -- user- defined equality. The reason for not simply calling |
fbf5a39b | 6291 | -- Find_Prim_Op here is that there may be a user-defined |
685094bf RD |
6292 | -- overloaded equality op that precedes the equality that we want, |
6293 | -- so we have to explicitly search (e.g., there could be an | |
6294 | -- equality with two different parameter types). | |
fbf5a39b | 6295 | |
70482933 | 6296 | else |
fbf5a39b AC |
6297 | if Is_Class_Wide_Type (Typl) then |
6298 | Typl := Root_Type (Typl); | |
6299 | end if; | |
6300 | ||
6301 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
6302 | while Present (Prim) loop |
6303 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
6304 | and then Etype (First_Formal (Node (Prim))) = | |
6305 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
6306 | and then |
6307 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
6308 | |
6309 | Next_Elmt (Prim); | |
fbf5a39b AC |
6310 | end loop; |
6311 | ||
2e071734 | 6312 | pragma Assert (Present (Prim)); |
fbf5a39b | 6313 | Op_Name := Node (Prim); |
70482933 RK |
6314 | end if; |
6315 | ||
6316 | Build_Equality_Call (Op_Name); | |
6317 | ||
5d09245e AC |
6318 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
6319 | -- predefined equality operator for a type which has a subcomponent | |
6320 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
6321 | ||
6322 | elsif Has_Unconstrained_UU_Component (Typl) then | |
6323 | Insert_Action (N, | |
6324 | Make_Raise_Program_Error (Loc, | |
6325 | Reason => PE_Unchecked_Union_Restriction)); | |
6326 | ||
6327 | -- Prevent Gigi from generating incorrect code by rewriting the | |
6328 | -- equality as a standard False. | |
6329 | ||
6330 | Rewrite (N, | |
6331 | New_Occurrence_Of (Standard_False, Loc)); | |
6332 | ||
6333 | elsif Is_Unchecked_Union (Typl) then | |
6334 | ||
6335 | -- If we can infer the discriminants of the operands, we make a | |
6336 | -- call to the TSS equality function. | |
6337 | ||
6338 | if Has_Inferable_Discriminants (Lhs) | |
6339 | and then | |
6340 | Has_Inferable_Discriminants (Rhs) | |
6341 | then | |
6342 | Build_Equality_Call | |
6343 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
6344 | ||
6345 | else | |
6346 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
6347 | -- the predefined equality operator for an Unchecked_Union type | |
6348 | -- if either of the operands lack inferable discriminants. | |
6349 | ||
6350 | Insert_Action (N, | |
6351 | Make_Raise_Program_Error (Loc, | |
6352 | Reason => PE_Unchecked_Union_Restriction)); | |
6353 | ||
6354 | -- Prevent Gigi from generating incorrect code by rewriting | |
6355 | -- the equality as a standard False. | |
6356 | ||
6357 | Rewrite (N, | |
6358 | New_Occurrence_Of (Standard_False, Loc)); | |
6359 | ||
6360 | end if; | |
6361 | ||
70482933 RK |
6362 | -- If a type support function is present (for complex cases), use it |
6363 | ||
fbf5a39b AC |
6364 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
6365 | Build_Equality_Call | |
6366 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 RK |
6367 | |
6368 | -- Otherwise expand the component by component equality. Note that | |
8fc789c8 | 6369 | -- we never use block-bit comparisons for records, because of the |
70482933 RK |
6370 | -- problems with gaps. The backend will often be able to recombine |
6371 | -- the separate comparisons that we generate here. | |
6372 | ||
6373 | else | |
6374 | Remove_Side_Effects (Lhs); | |
6375 | Remove_Side_Effects (Rhs); | |
6376 | Rewrite (N, | |
6377 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
6378 | ||
6379 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
6380 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
6381 | end if; | |
6382 | end if; | |
6383 | ||
d26dc4b5 | 6384 | -- Test if result is known at compile time |
70482933 | 6385 | |
d26dc4b5 | 6386 | Rewrite_Comparison (N); |
f02b8bb8 RD |
6387 | |
6388 | -- If we still have comparison for Vax_Float, process it | |
6389 | ||
6390 | if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then | |
6391 | Expand_Vax_Comparison (N); | |
6392 | return; | |
6393 | end if; | |
0580d807 AC |
6394 | |
6395 | Optimize_Length_Comparison (N); | |
70482933 RK |
6396 | end Expand_N_Op_Eq; |
6397 | ||
6398 | ----------------------- | |
6399 | -- Expand_N_Op_Expon -- | |
6400 | ----------------------- | |
6401 | ||
6402 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
6403 | Loc : constant Source_Ptr := Sloc (N); | |
6404 | Typ : constant Entity_Id := Etype (N); | |
6405 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
6406 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 6407 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
6408 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
6409 | Exptyp : constant Entity_Id := Etype (Exp); | |
6410 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
6411 | Expv : Uint; | |
6412 | Xnode : Node_Id; | |
6413 | Temp : Node_Id; | |
6414 | Rent : RE_Id; | |
6415 | Ent : Entity_Id; | |
fbf5a39b | 6416 | Etyp : Entity_Id; |
70482933 RK |
6417 | |
6418 | begin | |
6419 | Binary_Op_Validity_Checks (N); | |
6420 | ||
8f66cda7 AC |
6421 | -- CodePeer and GNATprove want to see the unexpanded N_Op_Expon node |
6422 | ||
56812278 | 6423 | if CodePeer_Mode or Alfa_Mode then |
8f66cda7 AC |
6424 | return; |
6425 | end if; | |
6426 | ||
685094bf RD |
6427 | -- If either operand is of a private type, then we have the use of an |
6428 | -- intrinsic operator, and we get rid of the privateness, by using root | |
6429 | -- types of underlying types for the actual operation. Otherwise the | |
6430 | -- private types will cause trouble if we expand multiplications or | |
6431 | -- shifts etc. We also do this transformation if the result type is | |
6432 | -- different from the base type. | |
07fc65c4 GB |
6433 | |
6434 | if Is_Private_Type (Etype (Base)) | |
8f66cda7 AC |
6435 | or else Is_Private_Type (Typ) |
6436 | or else Is_Private_Type (Exptyp) | |
6437 | or else Rtyp /= Root_Type (Bastyp) | |
07fc65c4 GB |
6438 | then |
6439 | declare | |
6440 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
6441 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
6442 | ||
6443 | begin | |
6444 | Rewrite (N, | |
6445 | Unchecked_Convert_To (Typ, | |
6446 | Make_Op_Expon (Loc, | |
6447 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
6448 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
6449 | Analyze_And_Resolve (N, Typ); | |
6450 | return; | |
6451 | end; | |
6452 | end if; | |
6453 | ||
fbf5a39b | 6454 | -- Test for case of known right argument |
70482933 RK |
6455 | |
6456 | if Compile_Time_Known_Value (Exp) then | |
6457 | Expv := Expr_Value (Exp); | |
6458 | ||
6459 | -- We only fold small non-negative exponents. You might think we | |
6460 | -- could fold small negative exponents for the real case, but we | |
6461 | -- can't because we are required to raise Constraint_Error for | |
6462 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
6463 | -- See ACVC test C4A012B. | |
6464 | ||
6465 | if Expv >= 0 and then Expv <= 4 then | |
6466 | ||
6467 | -- X ** 0 = 1 (or 1.0) | |
6468 | ||
6469 | if Expv = 0 then | |
abcbd24c ST |
6470 | |
6471 | -- Call Remove_Side_Effects to ensure that any side effects | |
6472 | -- in the ignored left operand (in particular function calls | |
6473 | -- to user defined functions) are properly executed. | |
6474 | ||
6475 | Remove_Side_Effects (Base); | |
6476 | ||
70482933 RK |
6477 | if Ekind (Typ) in Integer_Kind then |
6478 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
6479 | else | |
6480 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
6481 | end if; | |
6482 | ||
6483 | -- X ** 1 = X | |
6484 | ||
6485 | elsif Expv = 1 then | |
6486 | Xnode := Base; | |
6487 | ||
6488 | -- X ** 2 = X * X | |
6489 | ||
6490 | elsif Expv = 2 then | |
6491 | Xnode := | |
6492 | Make_Op_Multiply (Loc, | |
6493 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 6494 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
6495 | |
6496 | -- X ** 3 = X * X * X | |
6497 | ||
6498 | elsif Expv = 3 then | |
6499 | Xnode := | |
6500 | Make_Op_Multiply (Loc, | |
6501 | Left_Opnd => | |
6502 | Make_Op_Multiply (Loc, | |
6503 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
6504 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
6505 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
6506 | |
6507 | -- X ** 4 -> | |
6508 | -- En : constant base'type := base * base; | |
6509 | -- ... | |
6510 | -- En * En | |
6511 | ||
6512 | else -- Expv = 4 | |
191fcb3a | 6513 | Temp := Make_Temporary (Loc, 'E', Base); |
70482933 RK |
6514 | |
6515 | Insert_Actions (N, New_List ( | |
6516 | Make_Object_Declaration (Loc, | |
6517 | Defining_Identifier => Temp, | |
6518 | Constant_Present => True, | |
6519 | Object_Definition => New_Reference_To (Typ, Loc), | |
6520 | Expression => | |
6521 | Make_Op_Multiply (Loc, | |
6522 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 6523 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base))))); |
70482933 RK |
6524 | |
6525 | Xnode := | |
6526 | Make_Op_Multiply (Loc, | |
6527 | Left_Opnd => New_Reference_To (Temp, Loc), | |
6528 | Right_Opnd => New_Reference_To (Temp, Loc)); | |
6529 | end if; | |
6530 | ||
6531 | Rewrite (N, Xnode); | |
6532 | Analyze_And_Resolve (N, Typ); | |
6533 | return; | |
6534 | end if; | |
6535 | end if; | |
6536 | ||
6537 | -- Case of (2 ** expression) appearing as an argument of an integer | |
6538 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 6539 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
6540 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
6541 | -- of the higher level node converts it into a shift. | |
6542 | ||
51bf9bdf AC |
6543 | -- Another case is 2 ** N in any other context. We simply convert |
6544 | -- this to 1 * 2 ** N, and then the above transformation applies. | |
6545 | ||
685094bf RD |
6546 | -- Note: this transformation is not applicable for a modular type with |
6547 | -- a non-binary modulus in the multiplication case, since we get a wrong | |
6548 | -- result if the shift causes an overflow before the modular reduction. | |
6549 | ||
70482933 RK |
6550 | if Nkind (Base) = N_Integer_Literal |
6551 | and then Intval (Base) = 2 | |
6552 | and then Is_Integer_Type (Root_Type (Exptyp)) | |
6553 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
6554 | and then Is_Unsigned_Type (Exptyp) | |
6555 | and then not Ovflo | |
70482933 | 6556 | then |
51bf9bdf | 6557 | -- First the multiply and divide cases |
70482933 | 6558 | |
51bf9bdf AC |
6559 | if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then |
6560 | declare | |
6561 | P : constant Node_Id := Parent (N); | |
6562 | L : constant Node_Id := Left_Opnd (P); | |
6563 | R : constant Node_Id := Right_Opnd (P); | |
6564 | ||
6565 | begin | |
6566 | if (Nkind (P) = N_Op_Multiply | |
6567 | and then not Non_Binary_Modulus (Typ) | |
6568 | and then | |
6569 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
6570 | or else | |
6571 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
6572 | and then not Do_Overflow_Check (P)) | |
6573 | or else | |
6574 | (Nkind (P) = N_Op_Divide | |
6575 | and then Is_Integer_Type (Etype (L)) | |
6576 | and then Is_Unsigned_Type (Etype (L)) | |
6577 | and then R = N | |
6578 | and then not Do_Overflow_Check (P)) | |
6579 | then | |
6580 | Set_Is_Power_Of_2_For_Shift (N); | |
6581 | return; | |
6582 | end if; | |
6583 | end; | |
6584 | ||
6585 | -- Now the other cases | |
6586 | ||
6587 | elsif not Non_Binary_Modulus (Typ) then | |
6588 | Rewrite (N, | |
6589 | Make_Op_Multiply (Loc, | |
6590 | Left_Opnd => Make_Integer_Literal (Loc, 1), | |
6591 | Right_Opnd => Relocate_Node (N))); | |
6592 | Analyze_And_Resolve (N, Typ); | |
6593 | return; | |
6594 | end if; | |
70482933 RK |
6595 | end if; |
6596 | ||
07fc65c4 GB |
6597 | -- Fall through if exponentiation must be done using a runtime routine |
6598 | ||
07fc65c4 | 6599 | -- First deal with modular case |
70482933 RK |
6600 | |
6601 | if Is_Modular_Integer_Type (Rtyp) then | |
6602 | ||
6603 | -- Non-binary case, we call the special exponentiation routine for | |
6604 | -- the non-binary case, converting the argument to Long_Long_Integer | |
6605 | -- and passing the modulus value. Then the result is converted back | |
6606 | -- to the base type. | |
6607 | ||
6608 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
6609 | Rewrite (N, |
6610 | Convert_To (Typ, | |
6611 | Make_Function_Call (Loc, | |
6612 | Name => New_Reference_To (RTE (RE_Exp_Modular), Loc), | |
6613 | Parameter_Associations => New_List ( | |
6614 | Convert_To (Standard_Integer, Base), | |
6615 | Make_Integer_Literal (Loc, Modulus (Rtyp)), | |
6616 | Exp)))); | |
6617 | ||
685094bf RD |
6618 | -- Binary case, in this case, we call one of two routines, either the |
6619 | -- unsigned integer case, or the unsigned long long integer case, | |
6620 | -- with a final "and" operation to do the required mod. | |
70482933 RK |
6621 | |
6622 | else | |
6623 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
6624 | Ent := RTE (RE_Exp_Unsigned); | |
6625 | else | |
6626 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
6627 | end if; | |
6628 | ||
6629 | Rewrite (N, | |
6630 | Convert_To (Typ, | |
6631 | Make_Op_And (Loc, | |
6632 | Left_Opnd => | |
6633 | Make_Function_Call (Loc, | |
6634 | Name => New_Reference_To (Ent, Loc), | |
6635 | Parameter_Associations => New_List ( | |
6636 | Convert_To (Etype (First_Formal (Ent)), Base), | |
6637 | Exp)), | |
6638 | Right_Opnd => | |
6639 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
6640 | ||
6641 | end if; | |
6642 | ||
6643 | -- Common exit point for modular type case | |
6644 | ||
6645 | Analyze_And_Resolve (N, Typ); | |
6646 | return; | |
6647 | ||
fbf5a39b AC |
6648 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
6649 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
6650 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 6651 | -- might need certification when a certified run time is required. |
70482933 | 6652 | |
fbf5a39b | 6653 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
6654 | -- checks are required, and one when they are not required, since there |
6655 | -- is a real gain in omitting checks on many machines. | |
70482933 | 6656 | |
fbf5a39b AC |
6657 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
6658 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
6659 | and then | |
6660 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
6661 | or else (Rtyp = Universal_Integer) | |
70482933 | 6662 | then |
fbf5a39b AC |
6663 | Etyp := Standard_Long_Long_Integer; |
6664 | ||
70482933 RK |
6665 | if Ovflo then |
6666 | Rent := RE_Exp_Long_Long_Integer; | |
6667 | else | |
6668 | Rent := RE_Exn_Long_Long_Integer; | |
6669 | end if; | |
6670 | ||
fbf5a39b AC |
6671 | elsif Is_Signed_Integer_Type (Rtyp) then |
6672 | Etyp := Standard_Integer; | |
70482933 RK |
6673 | |
6674 | if Ovflo then | |
fbf5a39b | 6675 | Rent := RE_Exp_Integer; |
70482933 | 6676 | else |
fbf5a39b | 6677 | Rent := RE_Exn_Integer; |
70482933 | 6678 | end if; |
fbf5a39b AC |
6679 | |
6680 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
6681 | -- need separate routines for the overflow case here, since in the case | |
6682 | -- of floating-point, we generate infinities anyway as a rule (either | |
6683 | -- that or we automatically trap overflow), and if there is an infinity | |
6684 | -- generated and a range check is required, the check will fail anyway. | |
6685 | ||
6686 | else | |
6687 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
6688 | Etyp := Standard_Long_Long_Float; | |
6689 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
6690 | end if; |
6691 | ||
6692 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 6693 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 6694 | |
fbf5a39b | 6695 | if Typ = Etyp |
70482933 RK |
6696 | and then Rtyp /= Universal_Integer |
6697 | and then Rtyp /= Universal_Real | |
6698 | then | |
6699 | Rewrite (N, | |
6700 | Make_Function_Call (Loc, | |
6701 | Name => New_Reference_To (RTE (Rent), Loc), | |
6702 | Parameter_Associations => New_List (Base, Exp))); | |
6703 | ||
6704 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 6705 | -- required in the universal cases, since the runtime routine is |
1147c704 | 6706 | -- typed using one of the standard types). |
70482933 RK |
6707 | |
6708 | else | |
6709 | Rewrite (N, | |
6710 | Convert_To (Typ, | |
6711 | Make_Function_Call (Loc, | |
6712 | Name => New_Reference_To (RTE (Rent), Loc), | |
6713 | Parameter_Associations => New_List ( | |
fbf5a39b | 6714 | Convert_To (Etyp, Base), |
70482933 RK |
6715 | Exp)))); |
6716 | end if; | |
6717 | ||
6718 | Analyze_And_Resolve (N, Typ); | |
6719 | return; | |
6720 | ||
fbf5a39b AC |
6721 | exception |
6722 | when RE_Not_Available => | |
6723 | return; | |
70482933 RK |
6724 | end Expand_N_Op_Expon; |
6725 | ||
6726 | -------------------- | |
6727 | -- Expand_N_Op_Ge -- | |
6728 | -------------------- | |
6729 | ||
6730 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
6731 | Typ : constant Entity_Id := Etype (N); | |
6732 | Op1 : constant Node_Id := Left_Opnd (N); | |
6733 | Op2 : constant Node_Id := Right_Opnd (N); | |
6734 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6735 | ||
6736 | begin | |
6737 | Binary_Op_Validity_Checks (N); | |
6738 | ||
f02b8bb8 | 6739 | if Is_Array_Type (Typ1) then |
70482933 RK |
6740 | Expand_Array_Comparison (N); |
6741 | return; | |
6742 | end if; | |
6743 | ||
6744 | if Is_Boolean_Type (Typ1) then | |
6745 | Adjust_Condition (Op1); | |
6746 | Adjust_Condition (Op2); | |
6747 | Set_Etype (N, Standard_Boolean); | |
6748 | Adjust_Result_Type (N, Typ); | |
6749 | end if; | |
6750 | ||
6751 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6752 | |
6753 | -- If we still have comparison, and Vax_Float type, process it | |
6754 | ||
6755 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6756 | Expand_Vax_Comparison (N); | |
6757 | return; | |
6758 | end if; | |
0580d807 AC |
6759 | |
6760 | Optimize_Length_Comparison (N); | |
70482933 RK |
6761 | end Expand_N_Op_Ge; |
6762 | ||
6763 | -------------------- | |
6764 | -- Expand_N_Op_Gt -- | |
6765 | -------------------- | |
6766 | ||
6767 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
6768 | Typ : constant Entity_Id := Etype (N); | |
6769 | Op1 : constant Node_Id := Left_Opnd (N); | |
6770 | Op2 : constant Node_Id := Right_Opnd (N); | |
6771 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6772 | ||
6773 | begin | |
6774 | Binary_Op_Validity_Checks (N); | |
6775 | ||
f02b8bb8 | 6776 | if Is_Array_Type (Typ1) then |
70482933 RK |
6777 | Expand_Array_Comparison (N); |
6778 | return; | |
6779 | end if; | |
6780 | ||
6781 | if Is_Boolean_Type (Typ1) then | |
6782 | Adjust_Condition (Op1); | |
6783 | Adjust_Condition (Op2); | |
6784 | Set_Etype (N, Standard_Boolean); | |
6785 | Adjust_Result_Type (N, Typ); | |
6786 | end if; | |
6787 | ||
6788 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6789 | |
6790 | -- If we still have comparison, and Vax_Float type, process it | |
6791 | ||
6792 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6793 | Expand_Vax_Comparison (N); | |
6794 | return; | |
6795 | end if; | |
0580d807 AC |
6796 | |
6797 | Optimize_Length_Comparison (N); | |
70482933 RK |
6798 | end Expand_N_Op_Gt; |
6799 | ||
6800 | -------------------- | |
6801 | -- Expand_N_Op_Le -- | |
6802 | -------------------- | |
6803 | ||
6804 | procedure Expand_N_Op_Le (N : Node_Id) is | |
6805 | Typ : constant Entity_Id := Etype (N); | |
6806 | Op1 : constant Node_Id := Left_Opnd (N); | |
6807 | Op2 : constant Node_Id := Right_Opnd (N); | |
6808 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6809 | ||
6810 | begin | |
6811 | Binary_Op_Validity_Checks (N); | |
6812 | ||
f02b8bb8 | 6813 | if Is_Array_Type (Typ1) then |
70482933 RK |
6814 | Expand_Array_Comparison (N); |
6815 | return; | |
6816 | end if; | |
6817 | ||
6818 | if Is_Boolean_Type (Typ1) then | |
6819 | Adjust_Condition (Op1); | |
6820 | Adjust_Condition (Op2); | |
6821 | Set_Etype (N, Standard_Boolean); | |
6822 | Adjust_Result_Type (N, Typ); | |
6823 | end if; | |
6824 | ||
6825 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6826 | |
6827 | -- If we still have comparison, and Vax_Float type, process it | |
6828 | ||
6829 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6830 | Expand_Vax_Comparison (N); | |
6831 | return; | |
6832 | end if; | |
0580d807 AC |
6833 | |
6834 | Optimize_Length_Comparison (N); | |
70482933 RK |
6835 | end Expand_N_Op_Le; |
6836 | ||
6837 | -------------------- | |
6838 | -- Expand_N_Op_Lt -- | |
6839 | -------------------- | |
6840 | ||
6841 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
6842 | Typ : constant Entity_Id := Etype (N); | |
6843 | Op1 : constant Node_Id := Left_Opnd (N); | |
6844 | Op2 : constant Node_Id := Right_Opnd (N); | |
6845 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6846 | ||
6847 | begin | |
6848 | Binary_Op_Validity_Checks (N); | |
6849 | ||
f02b8bb8 | 6850 | if Is_Array_Type (Typ1) then |
70482933 RK |
6851 | Expand_Array_Comparison (N); |
6852 | return; | |
6853 | end if; | |
6854 | ||
6855 | if Is_Boolean_Type (Typ1) then | |
6856 | Adjust_Condition (Op1); | |
6857 | Adjust_Condition (Op2); | |
6858 | Set_Etype (N, Standard_Boolean); | |
6859 | Adjust_Result_Type (N, Typ); | |
6860 | end if; | |
6861 | ||
6862 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6863 | |
6864 | -- If we still have comparison, and Vax_Float type, process it | |
6865 | ||
6866 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6867 | Expand_Vax_Comparison (N); | |
6868 | return; | |
6869 | end if; | |
0580d807 AC |
6870 | |
6871 | Optimize_Length_Comparison (N); | |
70482933 RK |
6872 | end Expand_N_Op_Lt; |
6873 | ||
6874 | ----------------------- | |
6875 | -- Expand_N_Op_Minus -- | |
6876 | ----------------------- | |
6877 | ||
6878 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
6879 | Loc : constant Source_Ptr := Sloc (N); | |
6880 | Typ : constant Entity_Id := Etype (N); | |
6881 | ||
6882 | begin | |
6883 | Unary_Op_Validity_Checks (N); | |
6884 | ||
07fc65c4 | 6885 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
6886 | and then Is_Signed_Integer_Type (Etype (N)) |
6887 | and then Do_Overflow_Check (N) | |
6888 | then | |
6889 | -- Software overflow checking expands -expr into (0 - expr) | |
6890 | ||
6891 | Rewrite (N, | |
6892 | Make_Op_Subtract (Loc, | |
6893 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
6894 | Right_Opnd => Right_Opnd (N))); | |
6895 | ||
6896 | Analyze_And_Resolve (N, Typ); | |
6897 | ||
6898 | -- Vax floating-point types case | |
6899 | ||
6900 | elsif Vax_Float (Etype (N)) then | |
6901 | Expand_Vax_Arith (N); | |
6902 | end if; | |
6903 | end Expand_N_Op_Minus; | |
6904 | ||
6905 | --------------------- | |
6906 | -- Expand_N_Op_Mod -- | |
6907 | --------------------- | |
6908 | ||
6909 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
6910 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 6911 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
6912 | Left : constant Node_Id := Left_Opnd (N); |
6913 | Right : constant Node_Id := Right_Opnd (N); | |
6914 | DOC : constant Boolean := Do_Overflow_Check (N); | |
6915 | DDC : constant Boolean := Do_Division_Check (N); | |
6916 | ||
6917 | LLB : Uint; | |
6918 | Llo : Uint; | |
6919 | Lhi : Uint; | |
6920 | LOK : Boolean; | |
6921 | Rlo : Uint; | |
6922 | Rhi : Uint; | |
6923 | ROK : Boolean; | |
6924 | ||
1033834f RD |
6925 | pragma Warnings (Off, Lhi); |
6926 | ||
70482933 RK |
6927 | begin |
6928 | Binary_Op_Validity_Checks (N); | |
6929 | ||
5d5e9775 AC |
6930 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
6931 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); | |
70482933 RK |
6932 | |
6933 | -- Convert mod to rem if operands are known non-negative. We do this | |
6934 | -- since it is quite likely that this will improve the quality of code, | |
6935 | -- (the operation now corresponds to the hardware remainder), and it | |
6936 | -- does not seem likely that it could be harmful. | |
6937 | ||
6938 | if LOK and then Llo >= 0 | |
6939 | and then | |
6940 | ROK and then Rlo >= 0 | |
6941 | then | |
6942 | Rewrite (N, | |
6943 | Make_Op_Rem (Sloc (N), | |
6944 | Left_Opnd => Left_Opnd (N), | |
6945 | Right_Opnd => Right_Opnd (N))); | |
6946 | ||
685094bf RD |
6947 | -- Instead of reanalyzing the node we do the analysis manually. This |
6948 | -- avoids anomalies when the replacement is done in an instance and | |
6949 | -- is epsilon more efficient. | |
70482933 RK |
6950 | |
6951 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 6952 | Set_Etype (N, Typ); |
70482933 RK |
6953 | Set_Do_Overflow_Check (N, DOC); |
6954 | Set_Do_Division_Check (N, DDC); | |
6955 | Expand_N_Op_Rem (N); | |
6956 | Set_Analyzed (N); | |
6957 | ||
6958 | -- Otherwise, normal mod processing | |
6959 | ||
6960 | else | |
6961 | if Is_Integer_Type (Etype (N)) then | |
6962 | Apply_Divide_Check (N); | |
6963 | end if; | |
6964 | ||
fbf5a39b AC |
6965 | -- Apply optimization x mod 1 = 0. We don't really need that with |
6966 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
6967 | -- certainly harmless. | |
6968 | ||
6969 | if Is_Integer_Type (Etype (N)) | |
6970 | and then Compile_Time_Known_Value (Right) | |
6971 | and then Expr_Value (Right) = Uint_1 | |
6972 | then | |
abcbd24c ST |
6973 | -- Call Remove_Side_Effects to ensure that any side effects in |
6974 | -- the ignored left operand (in particular function calls to | |
6975 | -- user defined functions) are properly executed. | |
6976 | ||
6977 | Remove_Side_Effects (Left); | |
6978 | ||
fbf5a39b AC |
6979 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
6980 | Analyze_And_Resolve (N, Typ); | |
6981 | return; | |
6982 | end if; | |
6983 | ||
70482933 RK |
6984 | -- Deal with annoying case of largest negative number remainder |
6985 | -- minus one. Gigi does not handle this case correctly, because | |
6986 | -- it generates a divide instruction which may trap in this case. | |
6987 | ||
685094bf RD |
6988 | -- In fact the check is quite easy, if the right operand is -1, then |
6989 | -- the mod value is always 0, and we can just ignore the left operand | |
6990 | -- completely in this case. | |
70482933 | 6991 | |
30783513 | 6992 | -- The operand type may be private (e.g. in the expansion of an |
685094bf RD |
6993 | -- intrinsic operation) so we must use the underlying type to get the |
6994 | -- bounds, and convert the literals explicitly. | |
fbf5a39b AC |
6995 | |
6996 | LLB := | |
6997 | Expr_Value | |
6998 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
70482933 RK |
6999 | |
7000 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
7001 | and then | |
7002 | ((not LOK) or else (Llo = LLB)) | |
7003 | then | |
7004 | Rewrite (N, | |
7005 | Make_Conditional_Expression (Loc, | |
7006 | Expressions => New_List ( | |
7007 | Make_Op_Eq (Loc, | |
7008 | Left_Opnd => Duplicate_Subexpr (Right), | |
7009 | Right_Opnd => | |
fbf5a39b AC |
7010 | Unchecked_Convert_To (Typ, |
7011 | Make_Integer_Literal (Loc, -1))), | |
7012 | Unchecked_Convert_To (Typ, | |
7013 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
7014 | Relocate_Node (N)))); |
7015 | ||
7016 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
fbf5a39b | 7017 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
7018 | end if; |
7019 | end if; | |
7020 | end Expand_N_Op_Mod; | |
7021 | ||
7022 | -------------------------- | |
7023 | -- Expand_N_Op_Multiply -- | |
7024 | -------------------------- | |
7025 | ||
7026 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
7027 | Loc : constant Source_Ptr := Sloc (N); |
7028 | Lop : constant Node_Id := Left_Opnd (N); | |
7029 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 7030 | |
abcbd24c ST |
7031 | Lp2 : constant Boolean := |
7032 | Nkind (Lop) = N_Op_Expon | |
7033 | and then Is_Power_Of_2_For_Shift (Lop); | |
fbf5a39b | 7034 | |
abcbd24c ST |
7035 | Rp2 : constant Boolean := |
7036 | Nkind (Rop) = N_Op_Expon | |
7037 | and then Is_Power_Of_2_For_Shift (Rop); | |
fbf5a39b | 7038 | |
70482933 RK |
7039 | Ltyp : constant Entity_Id := Etype (Lop); |
7040 | Rtyp : constant Entity_Id := Etype (Rop); | |
7041 | Typ : Entity_Id := Etype (N); | |
7042 | ||
7043 | begin | |
7044 | Binary_Op_Validity_Checks (N); | |
7045 | ||
7046 | -- Special optimizations for integer types | |
7047 | ||
7048 | if Is_Integer_Type (Typ) then | |
7049 | ||
abcbd24c | 7050 | -- N * 0 = 0 for integer types |
70482933 | 7051 | |
abcbd24c ST |
7052 | if Compile_Time_Known_Value (Rop) |
7053 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 7054 | then |
abcbd24c ST |
7055 | -- Call Remove_Side_Effects to ensure that any side effects in |
7056 | -- the ignored left operand (in particular function calls to | |
7057 | -- user defined functions) are properly executed. | |
7058 | ||
7059 | Remove_Side_Effects (Lop); | |
7060 | ||
7061 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
7062 | Analyze_And_Resolve (N, Typ); | |
7063 | return; | |
7064 | end if; | |
7065 | ||
7066 | -- Similar handling for 0 * N = 0 | |
7067 | ||
7068 | if Compile_Time_Known_Value (Lop) | |
7069 | and then Expr_Value (Lop) = Uint_0 | |
7070 | then | |
7071 | Remove_Side_Effects (Rop); | |
70482933 RK |
7072 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
7073 | Analyze_And_Resolve (N, Typ); | |
7074 | return; | |
7075 | end if; | |
7076 | ||
7077 | -- N * 1 = 1 * N = N for integer types | |
7078 | ||
fbf5a39b AC |
7079 | -- This optimisation is not done if we are going to |
7080 | -- rewrite the product 1 * 2 ** N to a shift. | |
7081 | ||
7082 | if Compile_Time_Known_Value (Rop) | |
7083 | and then Expr_Value (Rop) = Uint_1 | |
7084 | and then not Lp2 | |
70482933 | 7085 | then |
fbf5a39b | 7086 | Rewrite (N, Lop); |
70482933 RK |
7087 | return; |
7088 | ||
fbf5a39b AC |
7089 | elsif Compile_Time_Known_Value (Lop) |
7090 | and then Expr_Value (Lop) = Uint_1 | |
7091 | and then not Rp2 | |
70482933 | 7092 | then |
fbf5a39b | 7093 | Rewrite (N, Rop); |
70482933 RK |
7094 | return; |
7095 | end if; | |
7096 | end if; | |
7097 | ||
70482933 RK |
7098 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
7099 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
7100 | -- operand is an integer, as required for this to work. | |
7101 | ||
fbf5a39b AC |
7102 | if Rp2 then |
7103 | if Lp2 then | |
70482933 | 7104 | |
fbf5a39b | 7105 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
7106 | |
7107 | Rewrite (N, | |
7108 | Make_Op_Expon (Loc, | |
7109 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
7110 | Right_Opnd => | |
7111 | Make_Op_Add (Loc, | |
7112 | Left_Opnd => Right_Opnd (Lop), | |
7113 | Right_Opnd => Right_Opnd (Rop)))); | |
7114 | Analyze_And_Resolve (N, Typ); | |
7115 | return; | |
7116 | ||
7117 | else | |
7118 | Rewrite (N, | |
7119 | Make_Op_Shift_Left (Loc, | |
7120 | Left_Opnd => Lop, | |
7121 | Right_Opnd => | |
7122 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
7123 | Analyze_And_Resolve (N, Typ); | |
7124 | return; | |
7125 | end if; | |
7126 | ||
7127 | -- Same processing for the operands the other way round | |
7128 | ||
fbf5a39b | 7129 | elsif Lp2 then |
70482933 RK |
7130 | Rewrite (N, |
7131 | Make_Op_Shift_Left (Loc, | |
7132 | Left_Opnd => Rop, | |
7133 | Right_Opnd => | |
7134 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
7135 | Analyze_And_Resolve (N, Typ); | |
7136 | return; | |
7137 | end if; | |
7138 | ||
7139 | -- Do required fixup of universal fixed operation | |
7140 | ||
7141 | if Typ = Universal_Fixed then | |
7142 | Fixup_Universal_Fixed_Operation (N); | |
7143 | Typ := Etype (N); | |
7144 | end if; | |
7145 | ||
7146 | -- Multiplications with fixed-point results | |
7147 | ||
7148 | if Is_Fixed_Point_Type (Typ) then | |
7149 | ||
685094bf RD |
7150 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
7151 | -- a semantic point of view such operations are simply integer | |
7152 | -- operations and will be treated that way. | |
70482933 RK |
7153 | |
7154 | if not Treat_Fixed_As_Integer (N) then | |
7155 | ||
7156 | -- Case of fixed * integer => fixed | |
7157 | ||
7158 | if Is_Integer_Type (Rtyp) then | |
7159 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
7160 | ||
7161 | -- Case of integer * fixed => fixed | |
7162 | ||
7163 | elsif Is_Integer_Type (Ltyp) then | |
7164 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
7165 | ||
7166 | -- Case of fixed * fixed => fixed | |
7167 | ||
7168 | else | |
7169 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
7170 | end if; | |
7171 | end if; | |
7172 | ||
685094bf RD |
7173 | -- Other cases of multiplication of fixed-point operands. Again we |
7174 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
7175 | |
7176 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
7177 | and then not Treat_Fixed_As_Integer (N) | |
7178 | then | |
7179 | if Is_Integer_Type (Typ) then | |
7180 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
7181 | else | |
7182 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
7183 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
7184 | end if; | |
7185 | ||
685094bf RD |
7186 | -- Mixed-mode operations can appear in a non-static universal context, |
7187 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
7188 | |
7189 | elsif Typ = Universal_Real | |
7190 | and then Is_Integer_Type (Rtyp) | |
7191 | then | |
7192 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); | |
7193 | ||
7194 | Analyze_And_Resolve (Rop, Universal_Real); | |
7195 | ||
7196 | elsif Typ = Universal_Real | |
7197 | and then Is_Integer_Type (Ltyp) | |
7198 | then | |
7199 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); | |
7200 | ||
7201 | Analyze_And_Resolve (Lop, Universal_Real); | |
7202 | ||
7203 | -- Non-fixed point cases, check software overflow checking required | |
7204 | ||
7205 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
7206 | Apply_Arithmetic_Overflow_Check (N); | |
f02b8bb8 RD |
7207 | |
7208 | -- Deal with VAX float case | |
7209 | ||
7210 | elsif Vax_Float (Typ) then | |
7211 | Expand_Vax_Arith (N); | |
7212 | return; | |
70482933 RK |
7213 | end if; |
7214 | end Expand_N_Op_Multiply; | |
7215 | ||
7216 | -------------------- | |
7217 | -- Expand_N_Op_Ne -- | |
7218 | -------------------- | |
7219 | ||
70482933 | 7220 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 7221 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
7222 | |
7223 | begin | |
f02b8bb8 | 7224 | -- Case of elementary type with standard operator |
70482933 | 7225 | |
f02b8bb8 RD |
7226 | if Is_Elementary_Type (Typ) |
7227 | and then Sloc (Entity (N)) = Standard_Location | |
7228 | then | |
7229 | Binary_Op_Validity_Checks (N); | |
70482933 | 7230 | |
f02b8bb8 | 7231 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 7232 | |
f02b8bb8 RD |
7233 | if Is_Boolean_Type (Typ) then |
7234 | Adjust_Condition (Left_Opnd (N)); | |
7235 | Adjust_Condition (Right_Opnd (N)); | |
7236 | Set_Etype (N, Standard_Boolean); | |
7237 | Adjust_Result_Type (N, Typ); | |
7238 | end if; | |
fbf5a39b | 7239 | |
f02b8bb8 RD |
7240 | Rewrite_Comparison (N); |
7241 | ||
7242 | -- If we still have comparison for Vax_Float, process it | |
7243 | ||
7244 | if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then | |
7245 | Expand_Vax_Comparison (N); | |
7246 | return; | |
7247 | end if; | |
7248 | ||
7249 | -- For all cases other than elementary types, we rewrite node as the | |
7250 | -- negation of an equality operation, and reanalyze. The equality to be | |
7251 | -- used is defined in the same scope and has the same signature. This | |
7252 | -- signature must be set explicitly since in an instance it may not have | |
7253 | -- the same visibility as in the generic unit. This avoids duplicating | |
7254 | -- or factoring the complex code for record/array equality tests etc. | |
7255 | ||
7256 | else | |
7257 | declare | |
7258 | Loc : constant Source_Ptr := Sloc (N); | |
7259 | Neg : Node_Id; | |
7260 | Ne : constant Entity_Id := Entity (N); | |
7261 | ||
7262 | begin | |
7263 | Binary_Op_Validity_Checks (N); | |
7264 | ||
7265 | Neg := | |
7266 | Make_Op_Not (Loc, | |
7267 | Right_Opnd => | |
7268 | Make_Op_Eq (Loc, | |
7269 | Left_Opnd => Left_Opnd (N), | |
7270 | Right_Opnd => Right_Opnd (N))); | |
7271 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
7272 | ||
7273 | if Scope (Ne) /= Standard_Standard then | |
7274 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
7275 | end if; | |
7276 | ||
4637729f | 7277 | -- For navigation purposes, we want to treat the inequality as an |
f02b8bb8 | 7278 | -- implicit reference to the corresponding equality. Preserve the |
4637729f | 7279 | -- Comes_From_ source flag to generate proper Xref entries. |
f02b8bb8 RD |
7280 | |
7281 | Preserve_Comes_From_Source (Neg, N); | |
7282 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
7283 | Rewrite (N, Neg); | |
7284 | Analyze_And_Resolve (N, Standard_Boolean); | |
7285 | end; | |
7286 | end if; | |
0580d807 AC |
7287 | |
7288 | Optimize_Length_Comparison (N); | |
70482933 RK |
7289 | end Expand_N_Op_Ne; |
7290 | ||
7291 | --------------------- | |
7292 | -- Expand_N_Op_Not -- | |
7293 | --------------------- | |
7294 | ||
685094bf | 7295 | -- If the argument is other than a Boolean array type, there is no special |
c77599d5 | 7296 | -- expansion required, except for VMS operations on signed integers. |
70482933 RK |
7297 | |
7298 | -- For the packed case, we call the special routine in Exp_Pakd, except | |
7299 | -- that if the component size is greater than one, we use the standard | |
7300 | -- routine generating a gruesome loop (it is so peculiar to have packed | |
685094bf RD |
7301 | -- arrays with non-standard Boolean representations anyway, so it does not |
7302 | -- matter that we do not handle this case efficiently). | |
70482933 | 7303 | |
685094bf RD |
7304 | -- For the unpacked case (and for the special packed case where we have non |
7305 | -- standard Booleans, as discussed above), we generate and insert into the | |
7306 | -- tree the following function definition: | |
70482933 RK |
7307 | |
7308 | -- function Nnnn (A : arr) is | |
7309 | -- B : arr; | |
7310 | -- begin | |
7311 | -- for J in a'range loop | |
7312 | -- B (J) := not A (J); | |
7313 | -- end loop; | |
7314 | -- return B; | |
7315 | -- end Nnnn; | |
7316 | ||
7317 | -- Here arr is the actual subtype of the parameter (and hence always | |
7318 | -- constrained). Then we replace the not with a call to this function. | |
7319 | ||
7320 | procedure Expand_N_Op_Not (N : Node_Id) is | |
7321 | Loc : constant Source_Ptr := Sloc (N); | |
7322 | Typ : constant Entity_Id := Etype (N); | |
7323 | Opnd : Node_Id; | |
7324 | Arr : Entity_Id; | |
7325 | A : Entity_Id; | |
7326 | B : Entity_Id; | |
7327 | J : Entity_Id; | |
7328 | A_J : Node_Id; | |
7329 | B_J : Node_Id; | |
7330 | ||
7331 | Func_Name : Entity_Id; | |
7332 | Loop_Statement : Node_Id; | |
7333 | ||
7334 | begin | |
7335 | Unary_Op_Validity_Checks (N); | |
7336 | ||
7337 | -- For boolean operand, deal with non-standard booleans | |
7338 | ||
7339 | if Is_Boolean_Type (Typ) then | |
7340 | Adjust_Condition (Right_Opnd (N)); | |
7341 | Set_Etype (N, Standard_Boolean); | |
7342 | Adjust_Result_Type (N, Typ); | |
7343 | return; | |
7344 | end if; | |
7345 | ||
880dabb5 AC |
7346 | -- For the VMS "not" on signed integer types, use conversion to and from |
7347 | -- a predefined modular type. | |
c77599d5 AC |
7348 | |
7349 | if Is_VMS_Operator (Entity (N)) then | |
7350 | declare | |
9bebf0e9 AC |
7351 | Rtyp : Entity_Id; |
7352 | Utyp : Entity_Id; | |
7353 | ||
c77599d5 | 7354 | begin |
9bebf0e9 AC |
7355 | -- If this is a derived type, retrieve original VMS type so that |
7356 | -- the proper sized type is used for intermediate values. | |
7357 | ||
7358 | if Is_Derived_Type (Typ) then | |
7359 | Rtyp := First_Subtype (Etype (Typ)); | |
7360 | else | |
7361 | Rtyp := Typ; | |
7362 | end if; | |
7363 | ||
0d901290 AC |
7364 | -- The proper unsigned type must have a size compatible with the |
7365 | -- operand, to prevent misalignment. | |
9bebf0e9 AC |
7366 | |
7367 | if RM_Size (Rtyp) <= 8 then | |
7368 | Utyp := RTE (RE_Unsigned_8); | |
7369 | ||
7370 | elsif RM_Size (Rtyp) <= 16 then | |
7371 | Utyp := RTE (RE_Unsigned_16); | |
7372 | ||
7373 | elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then | |
bc20523f | 7374 | Utyp := RTE (RE_Unsigned_32); |
9bebf0e9 AC |
7375 | |
7376 | else | |
7377 | Utyp := RTE (RE_Long_Long_Unsigned); | |
7378 | end if; | |
7379 | ||
c77599d5 AC |
7380 | Rewrite (N, |
7381 | Unchecked_Convert_To (Typ, | |
9bebf0e9 AC |
7382 | Make_Op_Not (Loc, |
7383 | Unchecked_Convert_To (Utyp, Right_Opnd (N))))); | |
c77599d5 AC |
7384 | Analyze_And_Resolve (N, Typ); |
7385 | return; | |
7386 | end; | |
7387 | end if; | |
7388 | ||
da94696d | 7389 | -- Only array types need any other processing |
70482933 | 7390 | |
da94696d | 7391 | if not Is_Array_Type (Typ) then |
70482933 RK |
7392 | return; |
7393 | end if; | |
7394 | ||
a9d8907c JM |
7395 | -- Case of array operand. If bit packed with a component size of 1, |
7396 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 7397 | |
a9d8907c JM |
7398 | if Is_Bit_Packed_Array (Typ) |
7399 | and then Component_Size (Typ) = 1 | |
7400 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
7401 | then | |
70482933 RK |
7402 | Expand_Packed_Not (N); |
7403 | return; | |
7404 | end if; | |
7405 | ||
fbf5a39b AC |
7406 | -- Case of array operand which is not bit-packed. If the context is |
7407 | -- a safe assignment, call in-place operation, If context is a larger | |
7408 | -- boolean expression in the context of a safe assignment, expansion is | |
7409 | -- done by enclosing operation. | |
70482933 RK |
7410 | |
7411 | Opnd := Relocate_Node (Right_Opnd (N)); | |
7412 | Convert_To_Actual_Subtype (Opnd); | |
7413 | Arr := Etype (Opnd); | |
7414 | Ensure_Defined (Arr, N); | |
b4592168 | 7415 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 7416 | |
fbf5a39b AC |
7417 | if Nkind (Parent (N)) = N_Assignment_Statement then |
7418 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
7419 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
7420 | return; | |
7421 | ||
5e1c00fa | 7422 | -- Special case the negation of a binary operation |
fbf5a39b | 7423 | |
303b4d58 | 7424 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 7425 | and then Safe_In_Place_Array_Op |
303b4d58 | 7426 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
7427 | then |
7428 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
7429 | return; | |
7430 | end if; | |
7431 | ||
7432 | elsif Nkind (Parent (N)) in N_Binary_Op | |
7433 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
7434 | then | |
7435 | declare | |
7436 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
7437 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
7438 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
7439 | ||
7440 | begin | |
7441 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
fbf5a39b | 7442 | |
aa9a7dd7 AC |
7443 | -- (not A) op (not B) can be reduced to a single call |
7444 | ||
7445 | if N = Op1 and then Nkind (Op2) = N_Op_Not then | |
fbf5a39b AC |
7446 | return; |
7447 | ||
bed8af19 AC |
7448 | elsif N = Op2 and then Nkind (Op1) = N_Op_Not then |
7449 | return; | |
7450 | ||
aa9a7dd7 | 7451 | -- A xor (not B) can also be special-cased |
fbf5a39b | 7452 | |
aa9a7dd7 | 7453 | elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then |
fbf5a39b AC |
7454 | return; |
7455 | end if; | |
7456 | end if; | |
7457 | end; | |
7458 | end if; | |
7459 | ||
70482933 RK |
7460 | A := Make_Defining_Identifier (Loc, Name_uA); |
7461 | B := Make_Defining_Identifier (Loc, Name_uB); | |
7462 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
7463 | ||
7464 | A_J := | |
7465 | Make_Indexed_Component (Loc, | |
7466 | Prefix => New_Reference_To (A, Loc), | |
7467 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7468 | ||
7469 | B_J := | |
7470 | Make_Indexed_Component (Loc, | |
7471 | Prefix => New_Reference_To (B, Loc), | |
7472 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7473 | ||
7474 | Loop_Statement := | |
7475 | Make_Implicit_Loop_Statement (N, | |
7476 | Identifier => Empty, | |
7477 | ||
7478 | Iteration_Scheme => | |
7479 | Make_Iteration_Scheme (Loc, | |
7480 | Loop_Parameter_Specification => | |
7481 | Make_Loop_Parameter_Specification (Loc, | |
0d901290 | 7482 | Defining_Identifier => J, |
70482933 RK |
7483 | Discrete_Subtype_Definition => |
7484 | Make_Attribute_Reference (Loc, | |
0d901290 | 7485 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
7486 | Attribute_Name => Name_Range))), |
7487 | ||
7488 | Statements => New_List ( | |
7489 | Make_Assignment_Statement (Loc, | |
7490 | Name => B_J, | |
7491 | Expression => Make_Op_Not (Loc, A_J)))); | |
7492 | ||
191fcb3a | 7493 | Func_Name := Make_Temporary (Loc, 'N'); |
70482933 RK |
7494 | Set_Is_Inlined (Func_Name); |
7495 | ||
7496 | Insert_Action (N, | |
7497 | Make_Subprogram_Body (Loc, | |
7498 | Specification => | |
7499 | Make_Function_Specification (Loc, | |
7500 | Defining_Unit_Name => Func_Name, | |
7501 | Parameter_Specifications => New_List ( | |
7502 | Make_Parameter_Specification (Loc, | |
7503 | Defining_Identifier => A, | |
7504 | Parameter_Type => New_Reference_To (Typ, Loc))), | |
630d30e9 | 7505 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
7506 | |
7507 | Declarations => New_List ( | |
7508 | Make_Object_Declaration (Loc, | |
7509 | Defining_Identifier => B, | |
7510 | Object_Definition => New_Reference_To (Arr, Loc))), | |
7511 | ||
7512 | Handled_Statement_Sequence => | |
7513 | Make_Handled_Sequence_Of_Statements (Loc, | |
7514 | Statements => New_List ( | |
7515 | Loop_Statement, | |
d766cee3 | 7516 | Make_Simple_Return_Statement (Loc, |
0d901290 | 7517 | Expression => Make_Identifier (Loc, Chars (B))))))); |
70482933 RK |
7518 | |
7519 | Rewrite (N, | |
7520 | Make_Function_Call (Loc, | |
0d901290 | 7521 | Name => New_Reference_To (Func_Name, Loc), |
70482933 RK |
7522 | Parameter_Associations => New_List (Opnd))); |
7523 | ||
7524 | Analyze_And_Resolve (N, Typ); | |
7525 | end Expand_N_Op_Not; | |
7526 | ||
7527 | -------------------- | |
7528 | -- Expand_N_Op_Or -- | |
7529 | -------------------- | |
7530 | ||
7531 | procedure Expand_N_Op_Or (N : Node_Id) is | |
7532 | Typ : constant Entity_Id := Etype (N); | |
7533 | ||
7534 | begin | |
7535 | Binary_Op_Validity_Checks (N); | |
7536 | ||
7537 | if Is_Array_Type (Etype (N)) then | |
7538 | Expand_Boolean_Operator (N); | |
7539 | ||
7540 | elsif Is_Boolean_Type (Etype (N)) then | |
f2d10a02 AC |
7541 | Adjust_Condition (Left_Opnd (N)); |
7542 | Adjust_Condition (Right_Opnd (N)); | |
7543 | Set_Etype (N, Standard_Boolean); | |
7544 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
7545 | |
7546 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
7547 | Expand_Intrinsic_Call (N, Entity (N)); | |
7548 | ||
70482933 RK |
7549 | end if; |
7550 | end Expand_N_Op_Or; | |
7551 | ||
7552 | ---------------------- | |
7553 | -- Expand_N_Op_Plus -- | |
7554 | ---------------------- | |
7555 | ||
7556 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
7557 | begin | |
7558 | Unary_Op_Validity_Checks (N); | |
7559 | end Expand_N_Op_Plus; | |
7560 | ||
7561 | --------------------- | |
7562 | -- Expand_N_Op_Rem -- | |
7563 | --------------------- | |
7564 | ||
7565 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
7566 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 7567 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
7568 | |
7569 | Left : constant Node_Id := Left_Opnd (N); | |
7570 | Right : constant Node_Id := Right_Opnd (N); | |
7571 | ||
5d5e9775 AC |
7572 | Lo : Uint; |
7573 | Hi : Uint; | |
7574 | OK : Boolean; | |
70482933 | 7575 | |
5d5e9775 AC |
7576 | Lneg : Boolean; |
7577 | Rneg : Boolean; | |
7578 | -- Set if corresponding operand can be negative | |
7579 | ||
7580 | pragma Unreferenced (Hi); | |
1033834f | 7581 | |
70482933 RK |
7582 | begin |
7583 | Binary_Op_Validity_Checks (N); | |
7584 | ||
7585 | if Is_Integer_Type (Etype (N)) then | |
7586 | Apply_Divide_Check (N); | |
7587 | end if; | |
7588 | ||
685094bf RD |
7589 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
7590 | -- but it is useful with other back ends (e.g. AAMP), and is certainly | |
7591 | -- harmless. | |
fbf5a39b AC |
7592 | |
7593 | if Is_Integer_Type (Etype (N)) | |
7594 | and then Compile_Time_Known_Value (Right) | |
7595 | and then Expr_Value (Right) = Uint_1 | |
7596 | then | |
abcbd24c ST |
7597 | -- Call Remove_Side_Effects to ensure that any side effects in the |
7598 | -- ignored left operand (in particular function calls to user defined | |
7599 | -- functions) are properly executed. | |
7600 | ||
7601 | Remove_Side_Effects (Left); | |
7602 | ||
fbf5a39b AC |
7603 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
7604 | Analyze_And_Resolve (N, Typ); | |
7605 | return; | |
7606 | end if; | |
7607 | ||
685094bf RD |
7608 | -- Deal with annoying case of largest negative number remainder minus |
7609 | -- one. Gigi does not handle this case correctly, because it generates | |
7610 | -- a divide instruction which may trap in this case. | |
70482933 | 7611 | |
685094bf RD |
7612 | -- In fact the check is quite easy, if the right operand is -1, then |
7613 | -- the remainder is always 0, and we can just ignore the left operand | |
7614 | -- completely in this case. | |
70482933 | 7615 | |
5d5e9775 AC |
7616 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); |
7617 | Lneg := (not OK) or else Lo < 0; | |
fbf5a39b | 7618 | |
5d5e9775 AC |
7619 | Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True); |
7620 | Rneg := (not OK) or else Lo < 0; | |
fbf5a39b | 7621 | |
5d5e9775 AC |
7622 | -- We won't mess with trying to find out if the left operand can really |
7623 | -- be the largest negative number (that's a pain in the case of private | |
7624 | -- types and this is really marginal). We will just assume that we need | |
7625 | -- the test if the left operand can be negative at all. | |
fbf5a39b | 7626 | |
5d5e9775 | 7627 | if Lneg and Rneg then |
70482933 RK |
7628 | Rewrite (N, |
7629 | Make_Conditional_Expression (Loc, | |
7630 | Expressions => New_List ( | |
7631 | Make_Op_Eq (Loc, | |
0d901290 | 7632 | Left_Opnd => Duplicate_Subexpr (Right), |
70482933 | 7633 | Right_Opnd => |
0d901290 | 7634 | Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))), |
70482933 | 7635 | |
fbf5a39b AC |
7636 | Unchecked_Convert_To (Typ, |
7637 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
7638 | |
7639 | Relocate_Node (N)))); | |
7640 | ||
7641 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
7642 | Analyze_And_Resolve (N, Typ); | |
7643 | end if; | |
7644 | end Expand_N_Op_Rem; | |
7645 | ||
7646 | ----------------------------- | |
7647 | -- Expand_N_Op_Rotate_Left -- | |
7648 | ----------------------------- | |
7649 | ||
7650 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
7651 | begin | |
7652 | Binary_Op_Validity_Checks (N); | |
7653 | end Expand_N_Op_Rotate_Left; | |
7654 | ||
7655 | ------------------------------ | |
7656 | -- Expand_N_Op_Rotate_Right -- | |
7657 | ------------------------------ | |
7658 | ||
7659 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
7660 | begin | |
7661 | Binary_Op_Validity_Checks (N); | |
7662 | end Expand_N_Op_Rotate_Right; | |
7663 | ||
7664 | ---------------------------- | |
7665 | -- Expand_N_Op_Shift_Left -- | |
7666 | ---------------------------- | |
7667 | ||
7668 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is | |
7669 | begin | |
7670 | Binary_Op_Validity_Checks (N); | |
7671 | end Expand_N_Op_Shift_Left; | |
7672 | ||
7673 | ----------------------------- | |
7674 | -- Expand_N_Op_Shift_Right -- | |
7675 | ----------------------------- | |
7676 | ||
7677 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
7678 | begin | |
7679 | Binary_Op_Validity_Checks (N); | |
7680 | end Expand_N_Op_Shift_Right; | |
7681 | ||
7682 | ---------------------------------------- | |
7683 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
7684 | ---------------------------------------- | |
7685 | ||
7686 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
7687 | begin | |
7688 | Binary_Op_Validity_Checks (N); | |
7689 | end Expand_N_Op_Shift_Right_Arithmetic; | |
7690 | ||
7691 | -------------------------- | |
7692 | -- Expand_N_Op_Subtract -- | |
7693 | -------------------------- | |
7694 | ||
7695 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
7696 | Typ : constant Entity_Id := Etype (N); | |
7697 | ||
7698 | begin | |
7699 | Binary_Op_Validity_Checks (N); | |
7700 | ||
7701 | -- N - 0 = N for integer types | |
7702 | ||
7703 | if Is_Integer_Type (Typ) | |
7704 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
7705 | and then Expr_Value (Right_Opnd (N)) = 0 | |
7706 | then | |
7707 | Rewrite (N, Left_Opnd (N)); | |
7708 | return; | |
7709 | end if; | |
7710 | ||
8fc789c8 | 7711 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 7712 | |
aa9a7dd7 AC |
7713 | if Is_Signed_Integer_Type (Typ) |
7714 | or else | |
7715 | Is_Fixed_Point_Type (Typ) | |
7716 | then | |
70482933 RK |
7717 | Apply_Arithmetic_Overflow_Check (N); |
7718 | ||
0d901290 | 7719 | -- VAX floating-point types case |
70482933 RK |
7720 | |
7721 | elsif Vax_Float (Typ) then | |
7722 | Expand_Vax_Arith (N); | |
7723 | end if; | |
7724 | end Expand_N_Op_Subtract; | |
7725 | ||
7726 | --------------------- | |
7727 | -- Expand_N_Op_Xor -- | |
7728 | --------------------- | |
7729 | ||
7730 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
7731 | Typ : constant Entity_Id := Etype (N); | |
7732 | ||
7733 | begin | |
7734 | Binary_Op_Validity_Checks (N); | |
7735 | ||
7736 | if Is_Array_Type (Etype (N)) then | |
7737 | Expand_Boolean_Operator (N); | |
7738 | ||
7739 | elsif Is_Boolean_Type (Etype (N)) then | |
7740 | Adjust_Condition (Left_Opnd (N)); | |
7741 | Adjust_Condition (Right_Opnd (N)); | |
7742 | Set_Etype (N, Standard_Boolean); | |
7743 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
7744 | |
7745 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
7746 | Expand_Intrinsic_Call (N, Entity (N)); | |
7747 | ||
70482933 RK |
7748 | end if; |
7749 | end Expand_N_Op_Xor; | |
7750 | ||
7751 | ---------------------- | |
7752 | -- Expand_N_Or_Else -- | |
7753 | ---------------------- | |
7754 | ||
5875f8d6 AC |
7755 | procedure Expand_N_Or_Else (N : Node_Id) |
7756 | renames Expand_Short_Circuit_Operator; | |
70482933 RK |
7757 | |
7758 | ----------------------------------- | |
7759 | -- Expand_N_Qualified_Expression -- | |
7760 | ----------------------------------- | |
7761 | ||
7762 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
7763 | Operand : constant Node_Id := Expression (N); | |
7764 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
7765 | ||
7766 | begin | |
f82944b7 JM |
7767 | -- Do validity check if validity checking operands |
7768 | ||
7769 | if Validity_Checks_On | |
7770 | and then Validity_Check_Operands | |
7771 | then | |
7772 | Ensure_Valid (Operand); | |
7773 | end if; | |
7774 | ||
7775 | -- Apply possible constraint check | |
7776 | ||
70482933 | 7777 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
d79e621a GD |
7778 | |
7779 | if Do_Range_Check (Operand) then | |
7780 | Set_Do_Range_Check (Operand, False); | |
7781 | Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed); | |
7782 | end if; | |
70482933 RK |
7783 | end Expand_N_Qualified_Expression; |
7784 | ||
a961aa79 AC |
7785 | ------------------------------------ |
7786 | -- Expand_N_Quantified_Expression -- | |
7787 | ------------------------------------ | |
7788 | ||
c0f136cd AC |
7789 | -- We expand: |
7790 | ||
7791 | -- for all X in range => Cond | |
a961aa79 | 7792 | |
c0f136cd | 7793 | -- into: |
a961aa79 | 7794 | |
c0f136cd AC |
7795 | -- T := True; |
7796 | -- for X in range loop | |
7797 | -- if not Cond then | |
7798 | -- T := False; | |
7799 | -- exit; | |
7800 | -- end if; | |
7801 | -- end loop; | |
90c63b09 | 7802 | |
c0f136cd | 7803 | -- Conversely, an existentially quantified expression: |
90c63b09 | 7804 | |
c0f136cd | 7805 | -- for some X in range => Cond |
90c63b09 | 7806 | |
c0f136cd | 7807 | -- becomes: |
90c63b09 | 7808 | |
c0f136cd AC |
7809 | -- T := False; |
7810 | -- for X in range loop | |
7811 | -- if Cond then | |
7812 | -- T := True; | |
7813 | -- exit; | |
7814 | -- end if; | |
7815 | -- end loop; | |
90c63b09 | 7816 | |
c0f136cd AC |
7817 | -- In both cases, the iteration may be over a container in which case it is |
7818 | -- given by an iterator specification, not a loop parameter specification. | |
a961aa79 | 7819 | |
c0f136cd AC |
7820 | procedure Expand_N_Quantified_Expression (N : Node_Id) is |
7821 | Loc : constant Source_Ptr := Sloc (N); | |
7822 | Is_Universal : constant Boolean := All_Present (N); | |
7823 | Actions : constant List_Id := New_List; | |
7824 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); | |
7825 | Cond : Node_Id; | |
7826 | Decl : Node_Id; | |
7827 | I_Scheme : Node_Id; | |
7828 | Test : Node_Id; | |
c56a9ba4 | 7829 | |
a961aa79 | 7830 | begin |
90c63b09 AC |
7831 | Decl := |
7832 | Make_Object_Declaration (Loc, | |
7833 | Defining_Identifier => Tnn, | |
c0f136cd AC |
7834 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
7835 | Expression => | |
7836 | New_Occurrence_Of (Boolean_Literals (Is_Universal), Loc)); | |
a961aa79 AC |
7837 | Append_To (Actions, Decl); |
7838 | ||
c0f136cd | 7839 | Cond := Relocate_Node (Condition (N)); |
a961aa79 | 7840 | |
62be5d0a JM |
7841 | -- Reset flag analyzed in the condition to force its analysis. Required |
7842 | -- since the previous analysis was done with expansion disabled (see | |
7843 | -- Resolve_Quantified_Expression) and hence checks were not inserted | |
7844 | -- and record comparisons have not been expanded. | |
7845 | ||
7846 | Reset_Analyzed_Flags (Cond); | |
7847 | ||
c0f136cd AC |
7848 | if Is_Universal then |
7849 | Cond := Make_Op_Not (Loc, Cond); | |
a961aa79 AC |
7850 | end if; |
7851 | ||
c0f136cd AC |
7852 | Test := |
7853 | Make_Implicit_If_Statement (N, | |
7854 | Condition => Cond, | |
7855 | Then_Statements => New_List ( | |
7856 | Make_Assignment_Statement (Loc, | |
7857 | Name => New_Occurrence_Of (Tnn, Loc), | |
7858 | Expression => | |
7859 | New_Occurrence_Of (Boolean_Literals (not Is_Universal), Loc)), | |
7860 | Make_Exit_Statement (Loc))); | |
7861 | ||
c56a9ba4 AC |
7862 | if Present (Loop_Parameter_Specification (N)) then |
7863 | I_Scheme := | |
7864 | Make_Iteration_Scheme (Loc, | |
7865 | Loop_Parameter_Specification => | |
7866 | Loop_Parameter_Specification (N)); | |
7867 | else | |
7868 | I_Scheme := | |
7869 | Make_Iteration_Scheme (Loc, | |
7870 | Iterator_Specification => Iterator_Specification (N)); | |
7871 | end if; | |
7872 | ||
a961aa79 AC |
7873 | Append_To (Actions, |
7874 | Make_Loop_Statement (Loc, | |
c56a9ba4 | 7875 | Iteration_Scheme => I_Scheme, |
c0f136cd AC |
7876 | Statements => New_List (Test), |
7877 | End_Label => Empty)); | |
a961aa79 AC |
7878 | |
7879 | Rewrite (N, | |
7880 | Make_Expression_With_Actions (Loc, | |
7881 | Expression => New_Occurrence_Of (Tnn, Loc), | |
7882 | Actions => Actions)); | |
7883 | ||
7884 | Analyze_And_Resolve (N, Standard_Boolean); | |
7885 | end Expand_N_Quantified_Expression; | |
7886 | ||
70482933 RK |
7887 | --------------------------------- |
7888 | -- Expand_N_Selected_Component -- | |
7889 | --------------------------------- | |
7890 | ||
70482933 RK |
7891 | procedure Expand_N_Selected_Component (N : Node_Id) is |
7892 | Loc : constant Source_Ptr := Sloc (N); | |
7893 | Par : constant Node_Id := Parent (N); | |
7894 | P : constant Node_Id := Prefix (N); | |
fbf5a39b | 7895 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 7896 | Disc : Entity_Id; |
70482933 | 7897 | New_N : Node_Id; |
fbf5a39b | 7898 | Dcon : Elmt_Id; |
d606f1df | 7899 | Dval : Node_Id; |
70482933 RK |
7900 | |
7901 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
7902 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
7903 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
7904 | -- Don't we have a general routine that does this??? |
7905 | ||
53f29d4f AC |
7906 | function Is_Subtype_Declaration return Boolean; |
7907 | -- The replacement of a discriminant reference by its value is required | |
4317e442 AC |
7908 | -- if this is part of the initialization of an temporary generated by a |
7909 | -- change of representation. This shows up as the construction of a | |
53f29d4f | 7910 | -- discriminant constraint for a subtype declared at the same point as |
4317e442 AC |
7911 | -- the entity in the prefix of the selected component. We recognize this |
7912 | -- case when the context of the reference is: | |
7913 | -- subtype ST is T(Obj.D); | |
7914 | -- where the entity for Obj comes from source, and ST has the same sloc. | |
53f29d4f | 7915 | |
fbf5a39b AC |
7916 | ----------------------- |
7917 | -- In_Left_Hand_Side -- | |
7918 | ----------------------- | |
70482933 RK |
7919 | |
7920 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
7921 | begin | |
fbf5a39b | 7922 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
90c63b09 | 7923 | and then Comp = Name (Parent (Comp))) |
fbf5a39b | 7924 | or else (Present (Parent (Comp)) |
90c63b09 AC |
7925 | and then Nkind (Parent (Comp)) in N_Subexpr |
7926 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
7927 | end In_Left_Hand_Side; |
7928 | ||
53f29d4f AC |
7929 | ----------------------------- |
7930 | -- Is_Subtype_Declaration -- | |
7931 | ----------------------------- | |
7932 | ||
7933 | function Is_Subtype_Declaration return Boolean is | |
7934 | Par : constant Node_Id := Parent (N); | |
53f29d4f AC |
7935 | begin |
7936 | return | |
7937 | Nkind (Par) = N_Index_Or_Discriminant_Constraint | |
7938 | and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration | |
7939 | and then Comes_From_Source (Entity (Prefix (N))) | |
7940 | and then Sloc (Par) = Sloc (Entity (Prefix (N))); | |
7941 | end Is_Subtype_Declaration; | |
7942 | ||
fbf5a39b AC |
7943 | -- Start of processing for Expand_N_Selected_Component |
7944 | ||
70482933 | 7945 | begin |
fbf5a39b AC |
7946 | -- Insert explicit dereference if required |
7947 | ||
7948 | if Is_Access_Type (Ptyp) then | |
702d2020 AC |
7949 | |
7950 | -- First set prefix type to proper access type, in case it currently | |
7951 | -- has a private (non-access) view of this type. | |
7952 | ||
7953 | Set_Etype (P, Ptyp); | |
7954 | ||
fbf5a39b | 7955 | Insert_Explicit_Dereference (P); |
e6f69614 | 7956 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
7957 | |
7958 | if Ekind (Etype (P)) = E_Private_Subtype | |
7959 | and then Is_For_Access_Subtype (Etype (P)) | |
7960 | then | |
7961 | Set_Etype (P, Base_Type (Etype (P))); | |
7962 | end if; | |
7963 | ||
7964 | Ptyp := Etype (P); | |
7965 | end if; | |
7966 | ||
7967 | -- Deal with discriminant check required | |
7968 | ||
70482933 RK |
7969 | if Do_Discriminant_Check (N) then |
7970 | ||
685094bf RD |
7971 | -- Present the discriminant checking function to the backend, so that |
7972 | -- it can inline the call to the function. | |
70482933 RK |
7973 | |
7974 | Add_Inlined_Body | |
7975 | (Discriminant_Checking_Func | |
7976 | (Original_Record_Component (Entity (Selector_Name (N))))); | |
70482933 | 7977 | |
fbf5a39b | 7978 | -- Now reset the flag and generate the call |
70482933 | 7979 | |
fbf5a39b AC |
7980 | Set_Do_Discriminant_Check (N, False); |
7981 | Generate_Discriminant_Check (N); | |
70482933 RK |
7982 | end if; |
7983 | ||
b4592168 GD |
7984 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
7985 | -- function, then additional actuals must be passed. | |
7986 | ||
0791fbe9 | 7987 | if Ada_Version >= Ada_2005 |
b4592168 GD |
7988 | and then Is_Build_In_Place_Function_Call (P) |
7989 | then | |
7990 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
7991 | end if; | |
7992 | ||
fbf5a39b AC |
7993 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
7994 | -- selected component with discriminants. This must be checked during | |
7995 | -- expansion, because during analysis the type of the selector is not | |
7996 | -- known at the point the prefix is analyzed. If the conversion is the | |
7997 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
7998 | |
7999 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
8000 | and then Has_Discriminants (Etype (N)) | |
8001 | and then not In_Left_Hand_Side (N) | |
8002 | then | |
8003 | Force_Evaluation (Prefix (N)); | |
8004 | end if; | |
8005 | ||
8006 | -- Remaining processing applies only if selector is a discriminant | |
8007 | ||
8008 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
8009 | ||
8010 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
8011 | -- we may be able to rewrite the expression with the actual value |
8012 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
8013 | |
8014 | if Is_Record_Type (Ptyp) | |
8015 | and then Has_Discriminants (Ptyp) | |
8016 | and then Is_Constrained (Ptyp) | |
70482933 | 8017 | then |
fbf5a39b AC |
8018 | -- Do this optimization for discrete types only, and not for |
8019 | -- access types (access discriminants get us into trouble!) | |
70482933 | 8020 | |
fbf5a39b AC |
8021 | if not Is_Discrete_Type (Etype (N)) then |
8022 | null; | |
8023 | ||
8024 | -- Don't do this on the left hand of an assignment statement. | |
0d901290 AC |
8025 | -- Normally one would think that references like this would not |
8026 | -- occur, but they do in generated code, and mean that we really | |
8027 | -- do want to assign the discriminant! | |
fbf5a39b AC |
8028 | |
8029 | elsif Nkind (Par) = N_Assignment_Statement | |
8030 | and then Name (Par) = N | |
8031 | then | |
8032 | null; | |
8033 | ||
685094bf | 8034 | -- Don't do this optimization for the prefix of an attribute or |
e2534738 | 8035 | -- the name of an object renaming declaration since these are |
685094bf | 8036 | -- contexts where we do not want the value anyway. |
fbf5a39b AC |
8037 | |
8038 | elsif (Nkind (Par) = N_Attribute_Reference | |
8039 | and then Prefix (Par) = N) | |
8040 | or else Is_Renamed_Object (N) | |
8041 | then | |
8042 | null; | |
8043 | ||
8044 | -- Don't do this optimization if we are within the code for a | |
8045 | -- discriminant check, since the whole point of such a check may | |
8046 | -- be to verify the condition on which the code below depends! | |
8047 | ||
8048 | elsif Is_In_Discriminant_Check (N) then | |
8049 | null; | |
8050 | ||
8051 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
8052 | -- still one condition that inhibits the optimization below but |
8053 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
8054 | |
8055 | else | |
685094bf RD |
8056 | -- Loop through discriminants to find the matching discriminant |
8057 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
8058 | |
8059 | Disc := First_Discriminant (Ptyp); | |
8060 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
8061 | Discr_Loop : while Present (Dcon) loop | |
d606f1df | 8062 | Dval := Node (Dcon); |
fbf5a39b | 8063 | |
bd949ee2 RD |
8064 | -- Check if this is the matching discriminant and if the |
8065 | -- discriminant value is simple enough to make sense to | |
8066 | -- copy. We don't want to copy complex expressions, and | |
8067 | -- indeed to do so can cause trouble (before we put in | |
8068 | -- this guard, a discriminant expression containing an | |
e7d897b8 | 8069 | -- AND THEN was copied, causing problems for coverage |
c228a069 | 8070 | -- analysis tools). |
bd949ee2 | 8071 | |
53f29d4f AC |
8072 | -- However, if the reference is part of the initialization |
8073 | -- code generated for an object declaration, we must use | |
8074 | -- the discriminant value from the subtype constraint, | |
8075 | -- because the selected component may be a reference to the | |
8076 | -- object being initialized, whose discriminant is not yet | |
8077 | -- set. This only happens in complex cases involving changes | |
8078 | -- or representation. | |
8079 | ||
bd949ee2 RD |
8080 | if Disc = Entity (Selector_Name (N)) |
8081 | and then (Is_Entity_Name (Dval) | |
170b2989 AC |
8082 | or else Compile_Time_Known_Value (Dval) |
8083 | or else Is_Subtype_Declaration) | |
bd949ee2 | 8084 | then |
fbf5a39b AC |
8085 | -- Here we have the matching discriminant. Check for |
8086 | -- the case of a discriminant of a component that is | |
8087 | -- constrained by an outer discriminant, which cannot | |
8088 | -- be optimized away. | |
8089 | ||
d606f1df AC |
8090 | if Denotes_Discriminant |
8091 | (Dval, Check_Concurrent => True) | |
8092 | then | |
8093 | exit Discr_Loop; | |
8094 | ||
8095 | elsif Nkind (Original_Node (Dval)) = N_Selected_Component | |
8096 | and then | |
8097 | Denotes_Discriminant | |
8098 | (Selector_Name (Original_Node (Dval)), True) | |
8099 | then | |
8100 | exit Discr_Loop; | |
8101 | ||
8102 | -- Do not retrieve value if constraint is not static. It | |
8103 | -- is generally not useful, and the constraint may be a | |
8104 | -- rewritten outer discriminant in which case it is in | |
8105 | -- fact incorrect. | |
8106 | ||
8107 | elsif Is_Entity_Name (Dval) | |
e7d897b8 AC |
8108 | and then Nkind (Parent (Entity (Dval))) = |
8109 | N_Object_Declaration | |
d606f1df AC |
8110 | and then Present (Expression (Parent (Entity (Dval)))) |
8111 | and then | |
8112 | not Is_Static_Expression | |
8113 | (Expression (Parent (Entity (Dval)))) | |
fbf5a39b AC |
8114 | then |
8115 | exit Discr_Loop; | |
70482933 | 8116 | |
685094bf RD |
8117 | -- In the context of a case statement, the expression may |
8118 | -- have the base type of the discriminant, and we need to | |
8119 | -- preserve the constraint to avoid spurious errors on | |
8120 | -- missing cases. | |
70482933 | 8121 | |
fbf5a39b | 8122 | elsif Nkind (Parent (N)) = N_Case_Statement |
d606f1df | 8123 | and then Etype (Dval) /= Etype (Disc) |
70482933 RK |
8124 | then |
8125 | Rewrite (N, | |
8126 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
8127 | Subtype_Mark => |
8128 | New_Occurrence_Of (Etype (Disc), Loc), | |
8129 | Expression => | |
d606f1df | 8130 | New_Copy_Tree (Dval))); |
ffe9aba8 | 8131 | Analyze_And_Resolve (N, Etype (Disc)); |
fbf5a39b AC |
8132 | |
8133 | -- In case that comes out as a static expression, | |
8134 | -- reset it (a selected component is never static). | |
8135 | ||
8136 | Set_Is_Static_Expression (N, False); | |
8137 | return; | |
8138 | ||
8139 | -- Otherwise we can just copy the constraint, but the | |
ffe9aba8 AC |
8140 | -- result is certainly not static! In some cases the |
8141 | -- discriminant constraint has been analyzed in the | |
8142 | -- context of the original subtype indication, but for | |
8143 | -- itypes the constraint might not have been analyzed | |
8144 | -- yet, and this must be done now. | |
fbf5a39b | 8145 | |
70482933 | 8146 | else |
d606f1df | 8147 | Rewrite (N, New_Copy_Tree (Dval)); |
ffe9aba8 | 8148 | Analyze_And_Resolve (N); |
fbf5a39b AC |
8149 | Set_Is_Static_Expression (N, False); |
8150 | return; | |
70482933 | 8151 | end if; |
70482933 RK |
8152 | end if; |
8153 | ||
fbf5a39b AC |
8154 | Next_Elmt (Dcon); |
8155 | Next_Discriminant (Disc); | |
8156 | end loop Discr_Loop; | |
70482933 | 8157 | |
fbf5a39b AC |
8158 | -- Note: the above loop should always find a matching |
8159 | -- discriminant, but if it does not, we just missed an | |
c228a069 AC |
8160 | -- optimization due to some glitch (perhaps a previous |
8161 | -- error), so ignore. | |
fbf5a39b AC |
8162 | |
8163 | end if; | |
70482933 RK |
8164 | end if; |
8165 | ||
8166 | -- The only remaining processing is in the case of a discriminant of | |
8167 | -- a concurrent object, where we rewrite the prefix to denote the | |
8168 | -- corresponding record type. If the type is derived and has renamed | |
8169 | -- discriminants, use corresponding discriminant, which is the one | |
8170 | -- that appears in the corresponding record. | |
8171 | ||
8172 | if not Is_Concurrent_Type (Ptyp) then | |
8173 | return; | |
8174 | end if; | |
8175 | ||
8176 | Disc := Entity (Selector_Name (N)); | |
8177 | ||
8178 | if Is_Derived_Type (Ptyp) | |
8179 | and then Present (Corresponding_Discriminant (Disc)) | |
8180 | then | |
8181 | Disc := Corresponding_Discriminant (Disc); | |
8182 | end if; | |
8183 | ||
8184 | New_N := | |
8185 | Make_Selected_Component (Loc, | |
8186 | Prefix => | |
8187 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
8188 | New_Copy_Tree (P)), | |
8189 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
8190 | ||
8191 | Rewrite (N, New_N); | |
8192 | Analyze (N); | |
8193 | end if; | |
5972791c | 8194 | |
73fe1679 | 8195 | -- Set Atomic_Sync_Required if necessary for atomic component |
5972791c | 8196 | |
73fe1679 AC |
8197 | if Nkind (N) = N_Selected_Component then |
8198 | declare | |
8199 | E : constant Entity_Id := Entity (Selector_Name (N)); | |
8200 | Set : Boolean; | |
8201 | ||
8202 | begin | |
8203 | -- If component is atomic, but type is not, setting depends on | |
8204 | -- disable/enable state for the component. | |
8205 | ||
8206 | if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then | |
8207 | Set := not Atomic_Synchronization_Disabled (E); | |
8208 | ||
8209 | -- If component is not atomic, but its type is atomic, setting | |
8210 | -- depends on disable/enable state for the type. | |
8211 | ||
8212 | elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
8213 | Set := not Atomic_Synchronization_Disabled (Etype (E)); | |
8214 | ||
8215 | -- If both component and type are atomic, we disable if either | |
8216 | -- component or its type have sync disabled. | |
8217 | ||
8218 | elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then | |
8219 | Set := (not Atomic_Synchronization_Disabled (E)) | |
8220 | and then | |
8221 | (not Atomic_Synchronization_Disabled (Etype (E))); | |
8222 | ||
8223 | else | |
8224 | Set := False; | |
8225 | end if; | |
8226 | ||
8227 | -- Set flag if required | |
8228 | ||
8229 | if Set then | |
8230 | Activate_Atomic_Synchronization (N); | |
8231 | end if; | |
8232 | end; | |
5972791c | 8233 | end if; |
70482933 RK |
8234 | end Expand_N_Selected_Component; |
8235 | ||
8236 | -------------------- | |
8237 | -- Expand_N_Slice -- | |
8238 | -------------------- | |
8239 | ||
8240 | procedure Expand_N_Slice (N : Node_Id) is | |
8241 | Loc : constant Source_Ptr := Sloc (N); | |
8242 | Typ : constant Entity_Id := Etype (N); | |
8243 | Pfx : constant Node_Id := Prefix (N); | |
8244 | Ptp : Entity_Id := Etype (Pfx); | |
fbf5a39b | 8245 | |
81a5b587 | 8246 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
8247 | -- Check whether the argument is an actual for a procedure call, in |
8248 | -- which case the expansion of a bit-packed slice is deferred until the | |
8249 | -- call itself is expanded. The reason this is required is that we might | |
8250 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
8251 | -- that copy out would be missed if we created a temporary here in | |
8252 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
8253 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
8254 | -- is harmless to defer expansion in the IN case, since the call | |
8255 | -- processing will still generate the appropriate copy in operation, | |
8256 | -- which will take care of the slice. | |
81a5b587 | 8257 | |
b01bf852 | 8258 | procedure Make_Temporary_For_Slice; |
685094bf RD |
8259 | -- Create a named variable for the value of the slice, in cases where |
8260 | -- the back-end cannot handle it properly, e.g. when packed types or | |
8261 | -- unaligned slices are involved. | |
fbf5a39b | 8262 | |
81a5b587 AC |
8263 | ------------------------- |
8264 | -- Is_Procedure_Actual -- | |
8265 | ------------------------- | |
8266 | ||
8267 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
8268 | Par : Node_Id := Parent (N); | |
08aa9a4a | 8269 | |
81a5b587 | 8270 | begin |
81a5b587 | 8271 | loop |
c6a60aa1 RD |
8272 | -- If our parent is a procedure call we can return |
8273 | ||
81a5b587 AC |
8274 | if Nkind (Par) = N_Procedure_Call_Statement then |
8275 | return True; | |
6b6fcd3e | 8276 | |
685094bf RD |
8277 | -- If our parent is a type conversion, keep climbing the tree, |
8278 | -- since a type conversion can be a procedure actual. Also keep | |
8279 | -- climbing if parameter association or a qualified expression, | |
8280 | -- since these are additional cases that do can appear on | |
8281 | -- procedure actuals. | |
6b6fcd3e | 8282 | |
303b4d58 AC |
8283 | elsif Nkind_In (Par, N_Type_Conversion, |
8284 | N_Parameter_Association, | |
8285 | N_Qualified_Expression) | |
c6a60aa1 | 8286 | then |
81a5b587 | 8287 | Par := Parent (Par); |
c6a60aa1 RD |
8288 | |
8289 | -- Any other case is not what we are looking for | |
8290 | ||
8291 | else | |
8292 | return False; | |
81a5b587 AC |
8293 | end if; |
8294 | end loop; | |
81a5b587 AC |
8295 | end Is_Procedure_Actual; |
8296 | ||
b01bf852 AC |
8297 | ------------------------------ |
8298 | -- Make_Temporary_For_Slice -- | |
8299 | ------------------------------ | |
fbf5a39b | 8300 | |
b01bf852 | 8301 | procedure Make_Temporary_For_Slice is |
fbf5a39b | 8302 | Decl : Node_Id; |
b01bf852 | 8303 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
13d923cc | 8304 | |
fbf5a39b AC |
8305 | begin |
8306 | Decl := | |
8307 | Make_Object_Declaration (Loc, | |
8308 | Defining_Identifier => Ent, | |
8309 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
8310 | ||
8311 | Set_No_Initialization (Decl); | |
8312 | ||
8313 | Insert_Actions (N, New_List ( | |
8314 | Decl, | |
8315 | Make_Assignment_Statement (Loc, | |
8316 | Name => New_Occurrence_Of (Ent, Loc), | |
8317 | Expression => Relocate_Node (N)))); | |
8318 | ||
8319 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
8320 | Analyze_And_Resolve (N, Typ); | |
b01bf852 | 8321 | end Make_Temporary_For_Slice; |
fbf5a39b AC |
8322 | |
8323 | -- Start of processing for Expand_N_Slice | |
70482933 RK |
8324 | |
8325 | begin | |
8326 | -- Special handling for access types | |
8327 | ||
8328 | if Is_Access_Type (Ptp) then | |
8329 | ||
70482933 RK |
8330 | Ptp := Designated_Type (Ptp); |
8331 | ||
e6f69614 AC |
8332 | Rewrite (Pfx, |
8333 | Make_Explicit_Dereference (Sloc (N), | |
8334 | Prefix => Relocate_Node (Pfx))); | |
70482933 | 8335 | |
e6f69614 | 8336 | Analyze_And_Resolve (Pfx, Ptp); |
70482933 RK |
8337 | end if; |
8338 | ||
b4592168 GD |
8339 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
8340 | -- function, then additional actuals must be passed. | |
8341 | ||
0791fbe9 | 8342 | if Ada_Version >= Ada_2005 |
b4592168 GD |
8343 | and then Is_Build_In_Place_Function_Call (Pfx) |
8344 | then | |
8345 | Make_Build_In_Place_Call_In_Anonymous_Context (Pfx); | |
8346 | end if; | |
8347 | ||
70482933 RK |
8348 | -- The remaining case to be handled is packed slices. We can leave |
8349 | -- packed slices as they are in the following situations: | |
8350 | ||
8351 | -- 1. Right or left side of an assignment (we can handle this | |
8352 | -- situation correctly in the assignment statement expansion). | |
8353 | ||
685094bf RD |
8354 | -- 2. Prefix of indexed component (the slide is optimized away in this |
8355 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 8356 | |
685094bf RD |
8357 | -- 3. Object renaming declaration, since we want the name of the |
8358 | -- slice, not the value. | |
70482933 | 8359 | |
685094bf RD |
8360 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
8361 | -- be required, and this is handled in the expansion of call | |
8362 | -- itself. | |
70482933 | 8363 | |
685094bf RD |
8364 | -- 5. Prefix of an address attribute (this is an error which is caught |
8365 | -- elsewhere, and the expansion would interfere with generating the | |
8366 | -- error message). | |
70482933 | 8367 | |
81a5b587 | 8368 | if not Is_Packed (Typ) then |
08aa9a4a | 8369 | |
685094bf RD |
8370 | -- Apply transformation for actuals of a function call, where |
8371 | -- Expand_Actuals is not used. | |
81a5b587 AC |
8372 | |
8373 | if Nkind (Parent (N)) = N_Function_Call | |
8374 | and then Is_Possibly_Unaligned_Slice (N) | |
8375 | then | |
b01bf852 | 8376 | Make_Temporary_For_Slice; |
81a5b587 AC |
8377 | end if; |
8378 | ||
8379 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
8380 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
8381 | and then Parent (N) = Name (Parent (Parent (N)))) | |
70482933 | 8382 | then |
81a5b587 | 8383 | return; |
70482933 | 8384 | |
81a5b587 AC |
8385 | elsif Nkind (Parent (N)) = N_Indexed_Component |
8386 | or else Is_Renamed_Object (N) | |
8387 | or else Is_Procedure_Actual (N) | |
8388 | then | |
8389 | return; | |
70482933 | 8390 | |
91b1417d AC |
8391 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
8392 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 8393 | then |
81a5b587 AC |
8394 | return; |
8395 | ||
8396 | else | |
b01bf852 | 8397 | Make_Temporary_For_Slice; |
70482933 RK |
8398 | end if; |
8399 | end Expand_N_Slice; | |
8400 | ||
8401 | ------------------------------ | |
8402 | -- Expand_N_Type_Conversion -- | |
8403 | ------------------------------ | |
8404 | ||
8405 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
8406 | Loc : constant Source_Ptr := Sloc (N); | |
8407 | Operand : constant Node_Id := Expression (N); | |
8408 | Target_Type : constant Entity_Id := Etype (N); | |
8409 | Operand_Type : Entity_Id := Etype (Operand); | |
8410 | ||
8411 | procedure Handle_Changed_Representation; | |
685094bf RD |
8412 | -- This is called in the case of record and array type conversions to |
8413 | -- see if there is a change of representation to be handled. Change of | |
8414 | -- representation is actually handled at the assignment statement level, | |
8415 | -- and what this procedure does is rewrite node N conversion as an | |
8416 | -- assignment to temporary. If there is no change of representation, | |
8417 | -- then the conversion node is unchanged. | |
70482933 | 8418 | |
426908f8 RD |
8419 | procedure Raise_Accessibility_Error; |
8420 | -- Called when we know that an accessibility check will fail. Rewrites | |
8421 | -- node N to an appropriate raise statement and outputs warning msgs. | |
8422 | -- The Etype of the raise node is set to Target_Type. | |
8423 | ||
70482933 RK |
8424 | procedure Real_Range_Check; |
8425 | -- Handles generation of range check for real target value | |
8426 | ||
d15f9422 AC |
8427 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean; |
8428 | -- True iff Present (Effective_Extra_Accessibility (Id)) successfully | |
8429 | -- evaluates to True. | |
8430 | ||
70482933 RK |
8431 | ----------------------------------- |
8432 | -- Handle_Changed_Representation -- | |
8433 | ----------------------------------- | |
8434 | ||
8435 | procedure Handle_Changed_Representation is | |
8436 | Temp : Entity_Id; | |
8437 | Decl : Node_Id; | |
8438 | Odef : Node_Id; | |
8439 | Disc : Node_Id; | |
8440 | N_Ix : Node_Id; | |
8441 | Cons : List_Id; | |
8442 | ||
8443 | begin | |
f82944b7 | 8444 | -- Nothing else to do if no change of representation |
70482933 RK |
8445 | |
8446 | if Same_Representation (Operand_Type, Target_Type) then | |
8447 | return; | |
8448 | ||
8449 | -- The real change of representation work is done by the assignment | |
8450 | -- statement processing. So if this type conversion is appearing as | |
8451 | -- the expression of an assignment statement, nothing needs to be | |
8452 | -- done to the conversion. | |
8453 | ||
8454 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
8455 | return; | |
8456 | ||
8457 | -- Otherwise we need to generate a temporary variable, and do the | |
8458 | -- change of representation assignment into that temporary variable. | |
8459 | -- The conversion is then replaced by a reference to this variable. | |
8460 | ||
8461 | else | |
8462 | Cons := No_List; | |
8463 | ||
685094bf RD |
8464 | -- If type is unconstrained we have to add a constraint, copied |
8465 | -- from the actual value of the left hand side. | |
70482933 RK |
8466 | |
8467 | if not Is_Constrained (Target_Type) then | |
8468 | if Has_Discriminants (Operand_Type) then | |
8469 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
8470 | |
8471 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
8472 | Disc := First_Stored_Discriminant (Operand_Type); | |
8473 | end if; | |
8474 | ||
70482933 RK |
8475 | Cons := New_List; |
8476 | while Present (Disc) loop | |
8477 | Append_To (Cons, | |
8478 | Make_Selected_Component (Loc, | |
7675ad4f AC |
8479 | Prefix => |
8480 | Duplicate_Subexpr_Move_Checks (Operand), | |
70482933 RK |
8481 | Selector_Name => |
8482 | Make_Identifier (Loc, Chars (Disc)))); | |
8483 | Next_Discriminant (Disc); | |
8484 | end loop; | |
8485 | ||
8486 | elsif Is_Array_Type (Operand_Type) then | |
8487 | N_Ix := First_Index (Target_Type); | |
8488 | Cons := New_List; | |
8489 | ||
8490 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
8491 | ||
8492 | -- We convert the bounds explicitly. We use an unchecked | |
8493 | -- conversion because bounds checks are done elsewhere. | |
8494 | ||
8495 | Append_To (Cons, | |
8496 | Make_Range (Loc, | |
8497 | Low_Bound => | |
8498 | Unchecked_Convert_To (Etype (N_Ix), | |
8499 | Make_Attribute_Reference (Loc, | |
8500 | Prefix => | |
fbf5a39b | 8501 | Duplicate_Subexpr_No_Checks |
70482933 RK |
8502 | (Operand, Name_Req => True), |
8503 | Attribute_Name => Name_First, | |
8504 | Expressions => New_List ( | |
8505 | Make_Integer_Literal (Loc, J)))), | |
8506 | ||
8507 | High_Bound => | |
8508 | Unchecked_Convert_To (Etype (N_Ix), | |
8509 | Make_Attribute_Reference (Loc, | |
8510 | Prefix => | |
fbf5a39b | 8511 | Duplicate_Subexpr_No_Checks |
70482933 RK |
8512 | (Operand, Name_Req => True), |
8513 | Attribute_Name => Name_Last, | |
8514 | Expressions => New_List ( | |
8515 | Make_Integer_Literal (Loc, J)))))); | |
8516 | ||
8517 | Next_Index (N_Ix); | |
8518 | end loop; | |
8519 | end if; | |
8520 | end if; | |
8521 | ||
8522 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
8523 | ||
8524 | if Present (Cons) then | |
8525 | Odef := | |
8526 | Make_Subtype_Indication (Loc, | |
8527 | Subtype_Mark => Odef, | |
8528 | Constraint => | |
8529 | Make_Index_Or_Discriminant_Constraint (Loc, | |
8530 | Constraints => Cons)); | |
8531 | end if; | |
8532 | ||
191fcb3a | 8533 | Temp := Make_Temporary (Loc, 'C'); |
70482933 RK |
8534 | Decl := |
8535 | Make_Object_Declaration (Loc, | |
8536 | Defining_Identifier => Temp, | |
8537 | Object_Definition => Odef); | |
8538 | ||
8539 | Set_No_Initialization (Decl, True); | |
8540 | ||
8541 | -- Insert required actions. It is essential to suppress checks | |
8542 | -- since we have suppressed default initialization, which means | |
8543 | -- that the variable we create may have no discriminants. | |
8544 | ||
8545 | Insert_Actions (N, | |
8546 | New_List ( | |
8547 | Decl, | |
8548 | Make_Assignment_Statement (Loc, | |
8549 | Name => New_Occurrence_Of (Temp, Loc), | |
8550 | Expression => Relocate_Node (N))), | |
8551 | Suppress => All_Checks); | |
8552 | ||
8553 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
8554 | return; | |
8555 | end if; | |
8556 | end Handle_Changed_Representation; | |
8557 | ||
426908f8 RD |
8558 | ------------------------------- |
8559 | -- Raise_Accessibility_Error -- | |
8560 | ------------------------------- | |
8561 | ||
8562 | procedure Raise_Accessibility_Error is | |
8563 | begin | |
8564 | Rewrite (N, | |
8565 | Make_Raise_Program_Error (Sloc (N), | |
8566 | Reason => PE_Accessibility_Check_Failed)); | |
8567 | Set_Etype (N, Target_Type); | |
8568 | ||
8569 | Error_Msg_N ("?accessibility check failure", N); | |
8570 | Error_Msg_NE | |
8571 | ("\?& will be raised at run time", N, Standard_Program_Error); | |
8572 | end Raise_Accessibility_Error; | |
8573 | ||
70482933 RK |
8574 | ---------------------- |
8575 | -- Real_Range_Check -- | |
8576 | ---------------------- | |
8577 | ||
685094bf RD |
8578 | -- Case of conversions to floating-point or fixed-point. If range checks |
8579 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
8580 | |
8581 | -- typ (x) | |
8582 | ||
8583 | -- to | |
8584 | ||
8585 | -- Tnn : typ'Base := typ'Base (x); | |
8586 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
8587 | -- Tnn | |
8588 | ||
685094bf RD |
8589 | -- This is necessary when there is a conversion of integer to float or |
8590 | -- to fixed-point to ensure that the correct checks are made. It is not | |
8591 | -- necessary for float to float where it is enough to simply set the | |
8592 | -- Do_Range_Check flag. | |
fbf5a39b | 8593 | |
70482933 RK |
8594 | procedure Real_Range_Check is |
8595 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
8596 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
8597 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 8598 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
8599 | Conv : Node_Id; |
8600 | Tnn : Entity_Id; | |
8601 | ||
8602 | begin | |
8603 | -- Nothing to do if conversion was rewritten | |
8604 | ||
8605 | if Nkind (N) /= N_Type_Conversion then | |
8606 | return; | |
8607 | end if; | |
8608 | ||
685094bf RD |
8609 | -- Nothing to do if range checks suppressed, or target has the same |
8610 | -- range as the base type (or is the base type). | |
70482933 RK |
8611 | |
8612 | if Range_Checks_Suppressed (Target_Type) | |
8613 | or else (Lo = Type_Low_Bound (Btyp) | |
8614 | and then | |
8615 | Hi = Type_High_Bound (Btyp)) | |
8616 | then | |
8617 | return; | |
8618 | end if; | |
8619 | ||
685094bf RD |
8620 | -- Nothing to do if expression is an entity on which checks have been |
8621 | -- suppressed. | |
70482933 | 8622 | |
fbf5a39b AC |
8623 | if Is_Entity_Name (Operand) |
8624 | and then Range_Checks_Suppressed (Entity (Operand)) | |
8625 | then | |
8626 | return; | |
8627 | end if; | |
8628 | ||
685094bf RD |
8629 | -- Nothing to do if bounds are all static and we can tell that the |
8630 | -- expression is within the bounds of the target. Note that if the | |
8631 | -- operand is of an unconstrained floating-point type, then we do | |
8632 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
8633 | |
8634 | declare | |
f02b8bb8 RD |
8635 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); |
8636 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
fbf5a39b AC |
8637 | |
8638 | begin | |
8639 | if (not Is_Floating_Point_Type (Xtyp) | |
8640 | or else Is_Constrained (Xtyp)) | |
8641 | and then Compile_Time_Known_Value (S_Lo) | |
8642 | and then Compile_Time_Known_Value (S_Hi) | |
8643 | and then Compile_Time_Known_Value (Hi) | |
8644 | and then Compile_Time_Known_Value (Lo) | |
8645 | then | |
8646 | declare | |
8647 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
8648 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
8649 | S_Lov : Ureal; | |
8650 | S_Hiv : Ureal; | |
8651 | ||
8652 | begin | |
8653 | if Is_Real_Type (Xtyp) then | |
8654 | S_Lov := Expr_Value_R (S_Lo); | |
8655 | S_Hiv := Expr_Value_R (S_Hi); | |
8656 | else | |
8657 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
8658 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
8659 | end if; | |
8660 | ||
8661 | if D_Hiv > D_Lov | |
8662 | and then S_Lov >= D_Lov | |
8663 | and then S_Hiv <= D_Hiv | |
8664 | then | |
8665 | Set_Do_Range_Check (Operand, False); | |
8666 | return; | |
8667 | end if; | |
8668 | end; | |
8669 | end if; | |
8670 | end; | |
8671 | ||
8672 | -- For float to float conversions, we are done | |
8673 | ||
8674 | if Is_Floating_Point_Type (Xtyp) | |
8675 | and then | |
8676 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
8677 | then |
8678 | return; | |
8679 | end if; | |
8680 | ||
fbf5a39b | 8681 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
8682 | |
8683 | Conv := Relocate_Node (N); | |
eaa826f8 | 8684 | Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); |
70482933 RK |
8685 | Set_Etype (Conv, Btyp); |
8686 | ||
f02b8bb8 RD |
8687 | -- Enable overflow except for case of integer to float conversions, |
8688 | -- where it is never required, since we can never have overflow in | |
8689 | -- this case. | |
70482933 | 8690 | |
fbf5a39b AC |
8691 | if not Is_Integer_Type (Etype (Operand)) then |
8692 | Enable_Overflow_Check (Conv); | |
70482933 RK |
8693 | end if; |
8694 | ||
191fcb3a | 8695 | Tnn := Make_Temporary (Loc, 'T', Conv); |
70482933 RK |
8696 | |
8697 | Insert_Actions (N, New_List ( | |
8698 | Make_Object_Declaration (Loc, | |
8699 | Defining_Identifier => Tnn, | |
8700 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
0ac2a660 AC |
8701 | Constant_Present => True, |
8702 | Expression => Conv), | |
70482933 RK |
8703 | |
8704 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
8705 | Condition => |
8706 | Make_Or_Else (Loc, | |
8707 | Left_Opnd => | |
8708 | Make_Op_Lt (Loc, | |
8709 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8710 | Right_Opnd => | |
8711 | Make_Attribute_Reference (Loc, | |
8712 | Attribute_Name => Name_First, | |
8713 | Prefix => | |
8714 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 8715 | |
07fc65c4 GB |
8716 | Right_Opnd => |
8717 | Make_Op_Gt (Loc, | |
8718 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8719 | Right_Opnd => | |
8720 | Make_Attribute_Reference (Loc, | |
8721 | Attribute_Name => Name_Last, | |
8722 | Prefix => | |
8723 | New_Occurrence_Of (Target_Type, Loc)))), | |
8724 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
8725 | |
8726 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
8727 | Analyze_And_Resolve (N, Btyp); | |
8728 | end Real_Range_Check; | |
8729 | ||
d15f9422 AC |
8730 | ----------------------------- |
8731 | -- Has_Extra_Accessibility -- | |
8732 | ----------------------------- | |
8733 | ||
8734 | -- Returns true for a formal of an anonymous access type or for | |
8735 | -- an Ada 2012-style stand-alone object of an anonymous access type. | |
8736 | ||
8737 | function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is | |
8738 | begin | |
8739 | if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then | |
8740 | return Present (Effective_Extra_Accessibility (Id)); | |
8741 | else | |
8742 | return False; | |
8743 | end if; | |
8744 | end Has_Extra_Accessibility; | |
8745 | ||
70482933 RK |
8746 | -- Start of processing for Expand_N_Type_Conversion |
8747 | ||
8748 | begin | |
685094bf | 8749 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 AC |
8750 | -- the conversion completely, it is useless, except that it may carry |
8751 | -- an Assignment_OK attribute, which must be propagated to the operand. | |
70482933 RK |
8752 | |
8753 | if Operand_Type = Target_Type then | |
7b00e31d AC |
8754 | if Assignment_OK (N) then |
8755 | Set_Assignment_OK (Operand); | |
8756 | end if; | |
8757 | ||
fbf5a39b | 8758 | Rewrite (N, Relocate_Node (Operand)); |
e606088a | 8759 | goto Done; |
70482933 RK |
8760 | end if; |
8761 | ||
685094bf RD |
8762 | -- Nothing to do if this is the second argument of read. This is a |
8763 | -- "backwards" conversion that will be handled by the specialized code | |
8764 | -- in attribute processing. | |
70482933 RK |
8765 | |
8766 | if Nkind (Parent (N)) = N_Attribute_Reference | |
8767 | and then Attribute_Name (Parent (N)) = Name_Read | |
8768 | and then Next (First (Expressions (Parent (N)))) = N | |
8769 | then | |
e606088a AC |
8770 | goto Done; |
8771 | end if; | |
8772 | ||
8773 | -- Check for case of converting to a type that has an invariant | |
8774 | -- associated with it. This required an invariant check. We convert | |
8775 | ||
8776 | -- typ (expr) | |
8777 | ||
8778 | -- into | |
8779 | ||
8780 | -- do invariant_check (typ (expr)) in typ (expr); | |
8781 | ||
8782 | -- using Duplicate_Subexpr to avoid multiple side effects | |
8783 | ||
8784 | -- Note: the Comes_From_Source check, and then the resetting of this | |
8785 | -- flag prevents what would otherwise be an infinite recursion. | |
8786 | ||
fd0ff1cf RD |
8787 | if Has_Invariants (Target_Type) |
8788 | and then Present (Invariant_Procedure (Target_Type)) | |
e606088a AC |
8789 | and then Comes_From_Source (N) |
8790 | then | |
8791 | Set_Comes_From_Source (N, False); | |
8792 | Rewrite (N, | |
8793 | Make_Expression_With_Actions (Loc, | |
8794 | Actions => New_List ( | |
8795 | Make_Invariant_Call (Duplicate_Subexpr (N))), | |
8796 | Expression => Duplicate_Subexpr_No_Checks (N))); | |
8797 | Analyze_And_Resolve (N, Target_Type); | |
8798 | goto Done; | |
70482933 RK |
8799 | end if; |
8800 | ||
8801 | -- Here if we may need to expand conversion | |
8802 | ||
eaa826f8 RD |
8803 | -- If the operand of the type conversion is an arithmetic operation on |
8804 | -- signed integers, and the based type of the signed integer type in | |
8805 | -- question is smaller than Standard.Integer, we promote both of the | |
8806 | -- operands to type Integer. | |
8807 | ||
8808 | -- For example, if we have | |
8809 | ||
8810 | -- target-type (opnd1 + opnd2) | |
8811 | ||
8812 | -- and opnd1 and opnd2 are of type short integer, then we rewrite | |
8813 | -- this as: | |
8814 | ||
8815 | -- target-type (integer(opnd1) + integer(opnd2)) | |
8816 | ||
8817 | -- We do this because we are always allowed to compute in a larger type | |
8818 | -- if we do the right thing with the result, and in this case we are | |
8819 | -- going to do a conversion which will do an appropriate check to make | |
8820 | -- sure that things are in range of the target type in any case. This | |
8821 | -- avoids some unnecessary intermediate overflows. | |
8822 | ||
dfcfdc0a AC |
8823 | -- We might consider a similar transformation in the case where the |
8824 | -- target is a real type or a 64-bit integer type, and the operand | |
8825 | -- is an arithmetic operation using a 32-bit integer type. However, | |
8826 | -- we do not bother with this case, because it could cause significant | |
308e6f3a | 8827 | -- inefficiencies on 32-bit machines. On a 64-bit machine it would be |
dfcfdc0a AC |
8828 | -- much cheaper, but we don't want different behavior on 32-bit and |
8829 | -- 64-bit machines. Note that the exclusion of the 64-bit case also | |
8830 | -- handles the configurable run-time cases where 64-bit arithmetic | |
8831 | -- may simply be unavailable. | |
eaa826f8 RD |
8832 | |
8833 | -- Note: this circuit is partially redundant with respect to the circuit | |
8834 | -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in | |
8835 | -- the processing here. Also we still need the Checks circuit, since we | |
8836 | -- have to be sure not to generate junk overflow checks in the first | |
8837 | -- place, since it would be trick to remove them here! | |
8838 | ||
fdfcc663 | 8839 | if Integer_Promotion_Possible (N) then |
eaa826f8 | 8840 | |
fdfcc663 | 8841 | -- All conditions met, go ahead with transformation |
eaa826f8 | 8842 | |
fdfcc663 AC |
8843 | declare |
8844 | Opnd : Node_Id; | |
8845 | L, R : Node_Id; | |
dfcfdc0a | 8846 | |
fdfcc663 AC |
8847 | begin |
8848 | R := | |
8849 | Make_Type_Conversion (Loc, | |
8850 | Subtype_Mark => New_Reference_To (Standard_Integer, Loc), | |
8851 | Expression => Relocate_Node (Right_Opnd (Operand))); | |
eaa826f8 | 8852 | |
5f3f175d AC |
8853 | Opnd := New_Op_Node (Nkind (Operand), Loc); |
8854 | Set_Right_Opnd (Opnd, R); | |
eaa826f8 | 8855 | |
5f3f175d | 8856 | if Nkind (Operand) in N_Binary_Op then |
fdfcc663 | 8857 | L := |
eaa826f8 | 8858 | Make_Type_Conversion (Loc, |
dfcfdc0a | 8859 | Subtype_Mark => New_Reference_To (Standard_Integer, Loc), |
fdfcc663 AC |
8860 | Expression => Relocate_Node (Left_Opnd (Operand))); |
8861 | ||
5f3f175d AC |
8862 | Set_Left_Opnd (Opnd, L); |
8863 | end if; | |
eaa826f8 | 8864 | |
5f3f175d AC |
8865 | Rewrite (N, |
8866 | Make_Type_Conversion (Loc, | |
8867 | Subtype_Mark => Relocate_Node (Subtype_Mark (N)), | |
8868 | Expression => Opnd)); | |
dfcfdc0a | 8869 | |
5f3f175d | 8870 | Analyze_And_Resolve (N, Target_Type); |
e606088a | 8871 | goto Done; |
fdfcc663 AC |
8872 | end; |
8873 | end if; | |
eaa826f8 | 8874 | |
f82944b7 JM |
8875 | -- Do validity check if validity checking operands |
8876 | ||
8877 | if Validity_Checks_On | |
8878 | and then Validity_Check_Operands | |
8879 | then | |
8880 | Ensure_Valid (Operand); | |
8881 | end if; | |
8882 | ||
70482933 RK |
8883 | -- Special case of converting from non-standard boolean type |
8884 | ||
8885 | if Is_Boolean_Type (Operand_Type) | |
8886 | and then (Nonzero_Is_True (Operand_Type)) | |
8887 | then | |
8888 | Adjust_Condition (Operand); | |
8889 | Set_Etype (Operand, Standard_Boolean); | |
8890 | Operand_Type := Standard_Boolean; | |
8891 | end if; | |
8892 | ||
8893 | -- Case of converting to an access type | |
8894 | ||
8895 | if Is_Access_Type (Target_Type) then | |
8896 | ||
d766cee3 RD |
8897 | -- Apply an accessibility check when the conversion operand is an |
8898 | -- access parameter (or a renaming thereof), unless conversion was | |
e84e11ba GD |
8899 | -- expanded from an Unchecked_ or Unrestricted_Access attribute. |
8900 | -- Note that other checks may still need to be applied below (such | |
8901 | -- as tagged type checks). | |
70482933 RK |
8902 | |
8903 | if Is_Entity_Name (Operand) | |
d15f9422 | 8904 | and then Has_Extra_Accessibility (Entity (Operand)) |
70482933 | 8905 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type |
d766cee3 RD |
8906 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
8907 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 8908 | then |
e84e11ba GD |
8909 | Apply_Accessibility_Check |
8910 | (Operand, Target_Type, Insert_Node => Operand); | |
70482933 | 8911 | |
e84e11ba | 8912 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
8913 | -- level of the target type, then force Program_Error. Note that this |
8914 | -- can only occur for cases where the attribute is within the body of | |
8915 | -- an instantiation (otherwise the conversion will already have been | |
8916 | -- rejected as illegal). Note: warnings are issued by the analyzer | |
8917 | -- for the instance cases. | |
70482933 RK |
8918 | |
8919 | elsif In_Instance_Body | |
07fc65c4 GB |
8920 | and then Type_Access_Level (Operand_Type) > |
8921 | Type_Access_Level (Target_Type) | |
70482933 | 8922 | then |
426908f8 | 8923 | Raise_Accessibility_Error; |
70482933 | 8924 | |
685094bf RD |
8925 | -- When the operand is a selected access discriminant the check needs |
8926 | -- to be made against the level of the object denoted by the prefix | |
8927 | -- of the selected name. Force Program_Error for this case as well | |
8928 | -- (this accessibility violation can only happen if within the body | |
8929 | -- of an instantiation). | |
70482933 RK |
8930 | |
8931 | elsif In_Instance_Body | |
8932 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
8933 | and then Nkind (Operand) = N_Selected_Component | |
8934 | and then Object_Access_Level (Operand) > | |
8935 | Type_Access_Level (Target_Type) | |
8936 | then | |
426908f8 | 8937 | Raise_Accessibility_Error; |
e606088a | 8938 | goto Done; |
70482933 RK |
8939 | end if; |
8940 | end if; | |
8941 | ||
8942 | -- Case of conversions of tagged types and access to tagged types | |
8943 | ||
685094bf RD |
8944 | -- When needed, that is to say when the expression is class-wide, Add |
8945 | -- runtime a tag check for (strict) downward conversion by using the | |
8946 | -- membership test, generating: | |
70482933 RK |
8947 | |
8948 | -- [constraint_error when Operand not in Target_Type'Class] | |
8949 | ||
8950 | -- or in the access type case | |
8951 | ||
8952 | -- [constraint_error | |
8953 | -- when Operand /= null | |
8954 | -- and then Operand.all not in | |
8955 | -- Designated_Type (Target_Type)'Class] | |
8956 | ||
8957 | if (Is_Access_Type (Target_Type) | |
8958 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
8959 | or else Is_Tagged_Type (Target_Type) | |
8960 | then | |
685094bf RD |
8961 | -- Do not do any expansion in the access type case if the parent is a |
8962 | -- renaming, since this is an error situation which will be caught by | |
8963 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 | 8964 | |
e7e4d230 | 8965 | if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then |
e606088a | 8966 | goto Done; |
70482933 RK |
8967 | end if; |
8968 | ||
0669bebe | 8969 | -- Otherwise, proceed with processing tagged conversion |
70482933 | 8970 | |
e7e4d230 | 8971 | Tagged_Conversion : declare |
8cea7b64 HK |
8972 | Actual_Op_Typ : Entity_Id; |
8973 | Actual_Targ_Typ : Entity_Id; | |
8974 | Make_Conversion : Boolean := False; | |
8975 | Root_Op_Typ : Entity_Id; | |
70482933 | 8976 | |
8cea7b64 HK |
8977 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
8978 | -- Create a membership check to test whether Operand is a member | |
8979 | -- of Targ_Typ. If the original Target_Type is an access, include | |
8980 | -- a test for null value. The check is inserted at N. | |
8981 | ||
8982 | -------------------- | |
8983 | -- Make_Tag_Check -- | |
8984 | -------------------- | |
8985 | ||
8986 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
8987 | Cond : Node_Id; | |
8988 | ||
8989 | begin | |
8990 | -- Generate: | |
8991 | -- [Constraint_Error | |
8992 | -- when Operand /= null | |
8993 | -- and then Operand.all not in Targ_Typ] | |
8994 | ||
8995 | if Is_Access_Type (Target_Type) then | |
8996 | Cond := | |
8997 | Make_And_Then (Loc, | |
8998 | Left_Opnd => | |
8999 | Make_Op_Ne (Loc, | |
9000 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
9001 | Right_Opnd => Make_Null (Loc)), | |
9002 | ||
9003 | Right_Opnd => | |
9004 | Make_Not_In (Loc, | |
9005 | Left_Opnd => | |
9006 | Make_Explicit_Dereference (Loc, | |
9007 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
9008 | Right_Opnd => New_Reference_To (Targ_Typ, Loc))); | |
9009 | ||
9010 | -- Generate: | |
9011 | -- [Constraint_Error when Operand not in Targ_Typ] | |
9012 | ||
9013 | else | |
9014 | Cond := | |
9015 | Make_Not_In (Loc, | |
9016 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
9017 | Right_Opnd => New_Reference_To (Targ_Typ, Loc)); | |
9018 | end if; | |
9019 | ||
9020 | Insert_Action (N, | |
9021 | Make_Raise_Constraint_Error (Loc, | |
9022 | Condition => Cond, | |
9023 | Reason => CE_Tag_Check_Failed)); | |
9024 | end Make_Tag_Check; | |
9025 | ||
e7e4d230 | 9026 | -- Start of processing for Tagged_Conversion |
70482933 RK |
9027 | |
9028 | begin | |
9732e886 | 9029 | -- Handle entities from the limited view |
852dba80 | 9030 | |
9732e886 | 9031 | if Is_Access_Type (Operand_Type) then |
852dba80 AC |
9032 | Actual_Op_Typ := |
9033 | Available_View (Designated_Type (Operand_Type)); | |
9732e886 JM |
9034 | else |
9035 | Actual_Op_Typ := Operand_Type; | |
9036 | end if; | |
9037 | ||
9038 | if Is_Access_Type (Target_Type) then | |
852dba80 AC |
9039 | Actual_Targ_Typ := |
9040 | Available_View (Designated_Type (Target_Type)); | |
70482933 | 9041 | else |
8cea7b64 | 9042 | Actual_Targ_Typ := Target_Type; |
70482933 RK |
9043 | end if; |
9044 | ||
8cea7b64 HK |
9045 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
9046 | ||
20b5d666 JM |
9047 | -- Ada 2005 (AI-251): Handle interface type conversion |
9048 | ||
8cea7b64 | 9049 | if Is_Interface (Actual_Op_Typ) then |
20b5d666 | 9050 | Expand_Interface_Conversion (N, Is_Static => False); |
e606088a | 9051 | goto Done; |
20b5d666 JM |
9052 | end if; |
9053 | ||
8cea7b64 | 9054 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 9055 | |
8cea7b64 HK |
9056 | -- Create a runtime tag check for a downward class-wide type |
9057 | -- conversion. | |
70482933 | 9058 | |
8cea7b64 | 9059 | if Is_Class_Wide_Type (Actual_Op_Typ) |
852dba80 | 9060 | and then Actual_Op_Typ /= Actual_Targ_Typ |
8cea7b64 | 9061 | and then Root_Op_Typ /= Actual_Targ_Typ |
4ac2477e JM |
9062 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ, |
9063 | Use_Full_View => True) | |
8cea7b64 HK |
9064 | then |
9065 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
9066 | Make_Conversion := True; | |
9067 | end if; | |
70482933 | 9068 | |
8cea7b64 HK |
9069 | -- AI05-0073: If the result subtype of the function is defined |
9070 | -- by an access_definition designating a specific tagged type | |
9071 | -- T, a check is made that the result value is null or the tag | |
9072 | -- of the object designated by the result value identifies T. | |
9073 | -- Constraint_Error is raised if this check fails. | |
70482933 | 9074 | |
8cea7b64 HK |
9075 | if Nkind (Parent (N)) = Sinfo.N_Return_Statement then |
9076 | declare | |
e886436a | 9077 | Func : Entity_Id; |
8cea7b64 HK |
9078 | Func_Typ : Entity_Id; |
9079 | ||
9080 | begin | |
e886436a | 9081 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 9082 | |
e886436a | 9083 | Func := Current_Scope; |
8cea7b64 HK |
9084 | while Present (Func) |
9085 | and then Ekind (Func) /= E_Function | |
9086 | loop | |
9087 | Func := Scope (Func); | |
9088 | end loop; | |
9089 | ||
9090 | -- The function's return subtype must be defined using | |
9091 | -- an access definition. | |
9092 | ||
9093 | if Nkind (Result_Definition (Parent (Func))) = | |
9094 | N_Access_Definition | |
9095 | then | |
9096 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
9097 | ||
9098 | -- The return subtype denotes a specific tagged type, | |
9099 | -- in other words, a non class-wide type. | |
9100 | ||
9101 | if Is_Tagged_Type (Func_Typ) | |
9102 | and then not Is_Class_Wide_Type (Func_Typ) | |
9103 | then | |
9104 | Make_Tag_Check (Actual_Targ_Typ); | |
9105 | Make_Conversion := True; | |
9106 | end if; | |
9107 | end if; | |
9108 | end; | |
70482933 RK |
9109 | end if; |
9110 | ||
8cea7b64 HK |
9111 | -- We have generated a tag check for either a class-wide type |
9112 | -- conversion or for AI05-0073. | |
70482933 | 9113 | |
8cea7b64 HK |
9114 | if Make_Conversion then |
9115 | declare | |
9116 | Conv : Node_Id; | |
9117 | begin | |
9118 | Conv := | |
9119 | Make_Unchecked_Type_Conversion (Loc, | |
9120 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
9121 | Expression => Relocate_Node (Expression (N))); | |
9122 | Rewrite (N, Conv); | |
9123 | Analyze_And_Resolve (N, Target_Type); | |
9124 | end; | |
9125 | end if; | |
70482933 | 9126 | end if; |
e7e4d230 | 9127 | end Tagged_Conversion; |
70482933 RK |
9128 | |
9129 | -- Case of other access type conversions | |
9130 | ||
9131 | elsif Is_Access_Type (Target_Type) then | |
9132 | Apply_Constraint_Check (Operand, Target_Type); | |
9133 | ||
9134 | -- Case of conversions from a fixed-point type | |
9135 | ||
685094bf RD |
9136 | -- These conversions require special expansion and processing, found in |
9137 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
9138 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
9139 | -- conversions, which do not need further processing. |
9140 | ||
9141 | elsif Is_Fixed_Point_Type (Operand_Type) | |
9142 | and then not Conversion_OK (N) | |
9143 | then | |
9144 | -- We should never see universal fixed at this case, since the | |
9145 | -- expansion of the constituent divide or multiply should have | |
9146 | -- eliminated the explicit mention of universal fixed. | |
9147 | ||
9148 | pragma Assert (Operand_Type /= Universal_Fixed); | |
9149 | ||
685094bf RD |
9150 | -- Check for special case of the conversion to universal real that |
9151 | -- occurs as a result of the use of a round attribute. In this case, | |
9152 | -- the real type for the conversion is taken from the target type of | |
9153 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
9154 | |
9155 | if Target_Type = Universal_Real | |
9156 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
9157 | and then Attribute_Name (Parent (N)) = Name_Round | |
9158 | then | |
9159 | Set_Rounded_Result (N); | |
9160 | Set_Etype (N, Etype (Parent (N))); | |
9161 | end if; | |
9162 | ||
9163 | -- Otherwise do correct fixed-conversion, but skip these if the | |
e7e4d230 AC |
9164 | -- Conversion_OK flag is set, because from a semantic point of view |
9165 | -- these are simple integer conversions needing no further processing | |
9166 | -- (the backend will simply treat them as integers). | |
70482933 RK |
9167 | |
9168 | if not Conversion_OK (N) then | |
9169 | if Is_Fixed_Point_Type (Etype (N)) then | |
9170 | Expand_Convert_Fixed_To_Fixed (N); | |
9171 | Real_Range_Check; | |
9172 | ||
9173 | elsif Is_Integer_Type (Etype (N)) then | |
9174 | Expand_Convert_Fixed_To_Integer (N); | |
9175 | ||
9176 | else | |
9177 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
9178 | Expand_Convert_Fixed_To_Float (N); | |
9179 | Real_Range_Check; | |
9180 | end if; | |
9181 | end if; | |
9182 | ||
9183 | -- Case of conversions to a fixed-point type | |
9184 | ||
685094bf RD |
9185 | -- These conversions require special expansion and processing, found in |
9186 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
9187 | -- since from a semantic point of view, these are simple integer | |
9188 | -- conversions, which do not need further processing. | |
70482933 RK |
9189 | |
9190 | elsif Is_Fixed_Point_Type (Target_Type) | |
9191 | and then not Conversion_OK (N) | |
9192 | then | |
9193 | if Is_Integer_Type (Operand_Type) then | |
9194 | Expand_Convert_Integer_To_Fixed (N); | |
9195 | Real_Range_Check; | |
9196 | else | |
9197 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
9198 | Expand_Convert_Float_To_Fixed (N); | |
9199 | Real_Range_Check; | |
9200 | end if; | |
9201 | ||
9202 | -- Case of float-to-integer conversions | |
9203 | ||
9204 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
9205 | -- since semantically the fixed-point target is treated as though it | |
9206 | -- were an integer in such cases. | |
9207 | ||
9208 | elsif Is_Floating_Point_Type (Operand_Type) | |
9209 | and then | |
9210 | (Is_Integer_Type (Target_Type) | |
9211 | or else | |
9212 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
9213 | then | |
70482933 RK |
9214 | -- One more check here, gcc is still not able to do conversions of |
9215 | -- this type with proper overflow checking, and so gigi is doing an | |
9216 | -- approximation of what is required by doing floating-point compares | |
9217 | -- with the end-point. But that can lose precision in some cases, and | |
f02b8bb8 | 9218 | -- give a wrong result. Converting the operand to Universal_Real is |
70482933 | 9219 | -- helpful, but still does not catch all cases with 64-bit integers |
e7e4d230 | 9220 | -- on targets with only 64-bit floats. |
0669bebe GB |
9221 | |
9222 | -- The above comment seems obsoleted by Apply_Float_Conversion_Check | |
9223 | -- Can this code be removed ??? | |
70482933 | 9224 | |
fbf5a39b AC |
9225 | if Do_Range_Check (Operand) then |
9226 | Rewrite (Operand, | |
70482933 RK |
9227 | Make_Type_Conversion (Loc, |
9228 | Subtype_Mark => | |
f02b8bb8 | 9229 | New_Occurrence_Of (Universal_Real, Loc), |
70482933 | 9230 | Expression => |
fbf5a39b | 9231 | Relocate_Node (Operand))); |
70482933 | 9232 | |
f02b8bb8 | 9233 | Set_Etype (Operand, Universal_Real); |
fbf5a39b AC |
9234 | Enable_Range_Check (Operand); |
9235 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
9236 | end if; |
9237 | ||
9238 | -- Case of array conversions | |
9239 | ||
685094bf RD |
9240 | -- Expansion of array conversions, add required length/range checks but |
9241 | -- only do this if there is no change of representation. For handling of | |
9242 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
9243 | |
9244 | elsif Is_Array_Type (Target_Type) then | |
70482933 RK |
9245 | if Is_Constrained (Target_Type) then |
9246 | Apply_Length_Check (Operand, Target_Type); | |
9247 | else | |
9248 | Apply_Range_Check (Operand, Target_Type); | |
9249 | end if; | |
9250 | ||
9251 | Handle_Changed_Representation; | |
9252 | ||
9253 | -- Case of conversions of discriminated types | |
9254 | ||
685094bf RD |
9255 | -- Add required discriminant checks if target is constrained. Again this |
9256 | -- change is skipped if we have a change of representation. | |
70482933 RK |
9257 | |
9258 | elsif Has_Discriminants (Target_Type) | |
9259 | and then Is_Constrained (Target_Type) | |
9260 | then | |
9261 | Apply_Discriminant_Check (Operand, Target_Type); | |
9262 | Handle_Changed_Representation; | |
9263 | ||
9264 | -- Case of all other record conversions. The only processing required | |
9265 | -- is to check for a change of representation requiring the special | |
9266 | -- assignment processing. | |
9267 | ||
9268 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
9269 | |
9270 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
9271 | -- a derived Unchecked_Union type to an unconstrained type that is |
9272 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
9273 | |
9274 | if Is_Derived_Type (Operand_Type) | |
9275 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
9276 | and then not Is_Constrained (Target_Type) | |
9277 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
9278 | and then not Has_Inferable_Discriminants (Operand) | |
9279 | then | |
685094bf | 9280 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e AC |
9281 | -- Program_Error node, but we give it the target type of the |
9282 | -- conversion. | |
9283 | ||
9284 | declare | |
9285 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
9286 | Reason => PE_Unchecked_Union_Restriction); | |
9287 | ||
9288 | begin | |
9289 | Set_Etype (PE, Target_Type); | |
9290 | Rewrite (N, PE); | |
9291 | ||
9292 | end; | |
9293 | else | |
9294 | Handle_Changed_Representation; | |
9295 | end if; | |
70482933 RK |
9296 | |
9297 | -- Case of conversions of enumeration types | |
9298 | ||
9299 | elsif Is_Enumeration_Type (Target_Type) then | |
9300 | ||
9301 | -- Special processing is required if there is a change of | |
e7e4d230 | 9302 | -- representation (from enumeration representation clauses). |
70482933 RK |
9303 | |
9304 | if not Same_Representation (Target_Type, Operand_Type) then | |
9305 | ||
9306 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
9307 | ||
9308 | Rewrite (N, | |
9309 | Make_Attribute_Reference (Loc, | |
9310 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
9311 | Attribute_Name => Name_Val, | |
9312 | Expressions => New_List ( | |
9313 | Make_Attribute_Reference (Loc, | |
9314 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
9315 | Attribute_Name => Name_Pos, | |
9316 | Expressions => New_List (Operand))))); | |
9317 | ||
9318 | Analyze_And_Resolve (N, Target_Type); | |
9319 | end if; | |
9320 | ||
9321 | -- Case of conversions to floating-point | |
9322 | ||
9323 | elsif Is_Floating_Point_Type (Target_Type) then | |
9324 | Real_Range_Check; | |
70482933 RK |
9325 | end if; |
9326 | ||
685094bf | 9327 | -- At this stage, either the conversion node has been transformed into |
e7e4d230 AC |
9328 | -- some other equivalent expression, or left as a conversion that can be |
9329 | -- handled by Gigi, in the following cases: | |
70482933 RK |
9330 | |
9331 | -- Conversions with no change of representation or type | |
9332 | ||
685094bf RD |
9333 | -- Numeric conversions involving integer, floating- and fixed-point |
9334 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
9335 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 9336 | |
5e1c00fa RD |
9337 | -- No other conversions should be passed to Gigi |
9338 | ||
9339 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 9340 | |
685094bf RD |
9341 | -- The only remaining step is to generate a range check if we still have |
9342 | -- a type conversion at this stage and Do_Range_Check is set. For now we | |
9343 | -- do this only for conversions of discrete types. | |
fbf5a39b AC |
9344 | |
9345 | if Nkind (N) = N_Type_Conversion | |
9346 | and then Is_Discrete_Type (Etype (N)) | |
9347 | then | |
9348 | declare | |
9349 | Expr : constant Node_Id := Expression (N); | |
9350 | Ftyp : Entity_Id; | |
9351 | Ityp : Entity_Id; | |
9352 | ||
9353 | begin | |
9354 | if Do_Range_Check (Expr) | |
9355 | and then Is_Discrete_Type (Etype (Expr)) | |
9356 | then | |
9357 | Set_Do_Range_Check (Expr, False); | |
9358 | ||
685094bf RD |
9359 | -- Before we do a range check, we have to deal with treating a |
9360 | -- fixed-point operand as an integer. The way we do this is | |
9361 | -- simply to do an unchecked conversion to an appropriate | |
fbf5a39b AC |
9362 | -- integer type large enough to hold the result. |
9363 | ||
9364 | -- This code is not active yet, because we are only dealing | |
9365 | -- with discrete types so far ??? | |
9366 | ||
9367 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer | |
9368 | and then Treat_Fixed_As_Integer (Expr) | |
9369 | then | |
9370 | Ftyp := Base_Type (Etype (Expr)); | |
9371 | ||
9372 | if Esize (Ftyp) >= Esize (Standard_Integer) then | |
9373 | Ityp := Standard_Long_Long_Integer; | |
9374 | else | |
9375 | Ityp := Standard_Integer; | |
9376 | end if; | |
9377 | ||
9378 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); | |
9379 | end if; | |
9380 | ||
9381 | -- Reset overflow flag, since the range check will include | |
e7e4d230 | 9382 | -- dealing with possible overflow, and generate the check. If |
685094bf | 9383 | -- Address is either a source type or target type, suppress |
8a36a0cc AC |
9384 | -- range check to avoid typing anomalies when it is a visible |
9385 | -- integer type. | |
fbf5a39b AC |
9386 | |
9387 | Set_Do_Overflow_Check (N, False); | |
8a36a0cc AC |
9388 | if not Is_Descendent_Of_Address (Etype (Expr)) |
9389 | and then not Is_Descendent_Of_Address (Target_Type) | |
9390 | then | |
9391 | Generate_Range_Check | |
9392 | (Expr, Target_Type, CE_Range_Check_Failed); | |
9393 | end if; | |
fbf5a39b AC |
9394 | end if; |
9395 | end; | |
9396 | end if; | |
f02b8bb8 RD |
9397 | |
9398 | -- Final step, if the result is a type conversion involving Vax_Float | |
9399 | -- types, then it is subject for further special processing. | |
9400 | ||
9401 | if Nkind (N) = N_Type_Conversion | |
9402 | and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type)) | |
9403 | then | |
9404 | Expand_Vax_Conversion (N); | |
e606088a | 9405 | goto Done; |
f02b8bb8 | 9406 | end if; |
e606088a AC |
9407 | |
9408 | -- Here at end of processing | |
9409 | ||
48f91b44 RD |
9410 | <<Done>> |
9411 | -- Apply predicate check if required. Note that we can't just call | |
9412 | -- Apply_Predicate_Check here, because the type looks right after | |
9413 | -- the conversion and it would omit the check. The Comes_From_Source | |
9414 | -- guard is necessary to prevent infinite recursions when we generate | |
9415 | -- internal conversions for the purpose of checking predicates. | |
9416 | ||
9417 | if Present (Predicate_Function (Target_Type)) | |
9418 | and then Target_Type /= Operand_Type | |
9419 | and then Comes_From_Source (N) | |
9420 | then | |
00332244 AC |
9421 | declare |
9422 | New_Expr : constant Node_Id := Duplicate_Subexpr (N); | |
9423 | ||
9424 | begin | |
9425 | -- Avoid infinite recursion on the subsequent expansion of | |
9426 | -- of the copy of the original type conversion. | |
9427 | ||
9428 | Set_Comes_From_Source (New_Expr, False); | |
9429 | Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr)); | |
9430 | end; | |
48f91b44 | 9431 | end if; |
70482933 RK |
9432 | end Expand_N_Type_Conversion; |
9433 | ||
9434 | ----------------------------------- | |
9435 | -- Expand_N_Unchecked_Expression -- | |
9436 | ----------------------------------- | |
9437 | ||
e7e4d230 | 9438 | -- Remove the unchecked expression node from the tree. Its job was simply |
70482933 RK |
9439 | -- to make sure that its constituent expression was handled with checks |
9440 | -- off, and now that that is done, we can remove it from the tree, and | |
e7e4d230 | 9441 | -- indeed must, since Gigi does not expect to see these nodes. |
70482933 RK |
9442 | |
9443 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
9444 | Exp : constant Node_Id := Expression (N); | |
70482933 | 9445 | begin |
e7e4d230 | 9446 | Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp)); |
70482933 RK |
9447 | Rewrite (N, Exp); |
9448 | end Expand_N_Unchecked_Expression; | |
9449 | ||
9450 | ---------------------------------------- | |
9451 | -- Expand_N_Unchecked_Type_Conversion -- | |
9452 | ---------------------------------------- | |
9453 | ||
685094bf RD |
9454 | -- If this cannot be handled by Gigi and we haven't already made a |
9455 | -- temporary for it, do it now. | |
70482933 RK |
9456 | |
9457 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
9458 | Target_Type : constant Entity_Id := Etype (N); | |
9459 | Operand : constant Node_Id := Expression (N); | |
9460 | Operand_Type : constant Entity_Id := Etype (Operand); | |
9461 | ||
9462 | begin | |
7b00e31d | 9463 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 | 9464 | -- the conversion completely, it is useless, except that it may carry |
e7e4d230 | 9465 | -- an Assignment_OK indication which must be propagated to the operand. |
7b00e31d AC |
9466 | |
9467 | if Operand_Type = Target_Type then | |
13d923cc | 9468 | |
e7e4d230 AC |
9469 | -- Code duplicates Expand_N_Unchecked_Expression above, factor??? |
9470 | ||
7b00e31d AC |
9471 | if Assignment_OK (N) then |
9472 | Set_Assignment_OK (Operand); | |
9473 | end if; | |
9474 | ||
9475 | Rewrite (N, Relocate_Node (Operand)); | |
9476 | return; | |
9477 | end if; | |
9478 | ||
70482933 RK |
9479 | -- If we have a conversion of a compile time known value to a target |
9480 | -- type and the value is in range of the target type, then we can simply | |
9481 | -- replace the construct by an integer literal of the correct type. We | |
9482 | -- only apply this to integer types being converted. Possibly it may | |
9483 | -- apply in other cases, but it is too much trouble to worry about. | |
9484 | ||
9485 | -- Note that we do not do this transformation if the Kill_Range_Check | |
9486 | -- flag is set, since then the value may be outside the expected range. | |
9487 | -- This happens in the Normalize_Scalars case. | |
9488 | ||
20b5d666 JM |
9489 | -- We also skip this if either the target or operand type is biased |
9490 | -- because in this case, the unchecked conversion is supposed to | |
9491 | -- preserve the bit pattern, not the integer value. | |
9492 | ||
70482933 | 9493 | if Is_Integer_Type (Target_Type) |
20b5d666 | 9494 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 9495 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 9496 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
9497 | and then Compile_Time_Known_Value (Operand) |
9498 | and then not Kill_Range_Check (N) | |
9499 | then | |
9500 | declare | |
9501 | Val : constant Uint := Expr_Value (Operand); | |
9502 | ||
9503 | begin | |
9504 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
9505 | and then | |
9506 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
9507 | and then | |
9508 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
9509 | and then | |
9510 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
9511 | then | |
9512 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 9513 | |
685094bf RD |
9514 | -- If Address is the target type, just set the type to avoid a |
9515 | -- spurious type error on the literal when Address is a visible | |
9516 | -- integer type. | |
8a36a0cc AC |
9517 | |
9518 | if Is_Descendent_Of_Address (Target_Type) then | |
9519 | Set_Etype (N, Target_Type); | |
9520 | else | |
9521 | Analyze_And_Resolve (N, Target_Type); | |
9522 | end if; | |
9523 | ||
70482933 RK |
9524 | return; |
9525 | end if; | |
9526 | end; | |
9527 | end if; | |
9528 | ||
9529 | -- Nothing to do if conversion is safe | |
9530 | ||
9531 | if Safe_Unchecked_Type_Conversion (N) then | |
9532 | return; | |
9533 | end if; | |
9534 | ||
9535 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
9536 | -- flag indicates ??? -- more comments needed here) | |
9537 | ||
9538 | if Assignment_OK (N) then | |
9539 | null; | |
9540 | else | |
9541 | Force_Evaluation (N); | |
9542 | end if; | |
9543 | end Expand_N_Unchecked_Type_Conversion; | |
9544 | ||
9545 | ---------------------------- | |
9546 | -- Expand_Record_Equality -- | |
9547 | ---------------------------- | |
9548 | ||
9549 | -- For non-variant records, Equality is expanded when needed into: | |
9550 | ||
9551 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
9552 | -- and then ... | |
9553 | -- and then Lhs.Discrn = Rhs.Discrn | |
9554 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
9555 | -- and then ... | |
9556 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
9557 | ||
9558 | -- The expression is folded by the back-end for adjacent fields. This | |
9559 | -- function is called for tagged record in only one occasion: for imple- | |
9560 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
9561 | -- otherwise the primitive "=" is used directly. | |
9562 | ||
9563 | function Expand_Record_Equality | |
9564 | (Nod : Node_Id; | |
9565 | Typ : Entity_Id; | |
9566 | Lhs : Node_Id; | |
9567 | Rhs : Node_Id; | |
2e071734 | 9568 | Bodies : List_Id) return Node_Id |
70482933 RK |
9569 | is |
9570 | Loc : constant Source_Ptr := Sloc (Nod); | |
9571 | ||
0ab80019 AC |
9572 | Result : Node_Id; |
9573 | C : Entity_Id; | |
9574 | ||
9575 | First_Time : Boolean := True; | |
9576 | ||
70482933 RK |
9577 | function Suitable_Element (C : Entity_Id) return Entity_Id; |
9578 | -- Return the first field to compare beginning with C, skipping the | |
0ab80019 AC |
9579 | -- inherited components. |
9580 | ||
9581 | ---------------------- | |
9582 | -- Suitable_Element -- | |
9583 | ---------------------- | |
70482933 RK |
9584 | |
9585 | function Suitable_Element (C : Entity_Id) return Entity_Id is | |
9586 | begin | |
9587 | if No (C) then | |
9588 | return Empty; | |
9589 | ||
9590 | elsif Ekind (C) /= E_Discriminant | |
9591 | and then Ekind (C) /= E_Component | |
9592 | then | |
9593 | return Suitable_Element (Next_Entity (C)); | |
9594 | ||
9595 | elsif Is_Tagged_Type (Typ) | |
9596 | and then C /= Original_Record_Component (C) | |
9597 | then | |
9598 | return Suitable_Element (Next_Entity (C)); | |
9599 | ||
df3e68b1 | 9600 | elsif Chars (C) = Name_uTag then |
70482933 RK |
9601 | return Suitable_Element (Next_Entity (C)); |
9602 | ||
24558db8 AC |
9603 | -- The .NET/JVM version of type Root_Controlled contains two fields |
9604 | -- which should not be considered part of the object. To achieve | |
9605 | -- proper equiality between two controlled objects on .NET/JVM, skip | |
9606 | -- field _parent whenever it is of type Root_Controlled. | |
9607 | ||
9608 | elsif Chars (C) = Name_uParent | |
9609 | and then VM_Target /= No_VM | |
9610 | and then Etype (C) = RTE (RE_Root_Controlled) | |
9611 | then | |
9612 | return Suitable_Element (Next_Entity (C)); | |
9613 | ||
26bff3d9 JM |
9614 | elsif Is_Interface (Etype (C)) then |
9615 | return Suitable_Element (Next_Entity (C)); | |
9616 | ||
70482933 RK |
9617 | else |
9618 | return C; | |
9619 | end if; | |
9620 | end Suitable_Element; | |
9621 | ||
70482933 RK |
9622 | -- Start of processing for Expand_Record_Equality |
9623 | ||
9624 | begin | |
70482933 RK |
9625 | -- Generates the following code: (assuming that Typ has one Discr and |
9626 | -- component C2 is also a record) | |
9627 | ||
9628 | -- True | |
9629 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
9630 | -- and then Lhs.C1 = Rhs.C1 | |
9631 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
9632 | -- and then ... | |
9633 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
9634 | ||
9635 | Result := New_Reference_To (Standard_True, Loc); | |
9636 | C := Suitable_Element (First_Entity (Typ)); | |
70482933 | 9637 | while Present (C) loop |
70482933 RK |
9638 | declare |
9639 | New_Lhs : Node_Id; | |
9640 | New_Rhs : Node_Id; | |
8aceda64 | 9641 | Check : Node_Id; |
70482933 RK |
9642 | |
9643 | begin | |
9644 | if First_Time then | |
9645 | First_Time := False; | |
9646 | New_Lhs := Lhs; | |
9647 | New_Rhs := Rhs; | |
70482933 RK |
9648 | else |
9649 | New_Lhs := New_Copy_Tree (Lhs); | |
9650 | New_Rhs := New_Copy_Tree (Rhs); | |
9651 | end if; | |
9652 | ||
8aceda64 AC |
9653 | Check := |
9654 | Expand_Composite_Equality (Nod, Etype (C), | |
9655 | Lhs => | |
9656 | Make_Selected_Component (Loc, | |
9657 | Prefix => New_Lhs, | |
9658 | Selector_Name => New_Reference_To (C, Loc)), | |
9659 | Rhs => | |
9660 | Make_Selected_Component (Loc, | |
9661 | Prefix => New_Rhs, | |
9662 | Selector_Name => New_Reference_To (C, Loc)), | |
9663 | Bodies => Bodies); | |
9664 | ||
9665 | -- If some (sub)component is an unchecked_union, the whole | |
9666 | -- operation will raise program error. | |
9667 | ||
9668 | if Nkind (Check) = N_Raise_Program_Error then | |
9669 | Result := Check; | |
9670 | Set_Etype (Result, Standard_Boolean); | |
9671 | exit; | |
9672 | else | |
9673 | Result := | |
9674 | Make_And_Then (Loc, | |
9675 | Left_Opnd => Result, | |
9676 | Right_Opnd => Check); | |
9677 | end if; | |
70482933 RK |
9678 | end; |
9679 | ||
9680 | C := Suitable_Element (Next_Entity (C)); | |
9681 | end loop; | |
9682 | ||
9683 | return Result; | |
9684 | end Expand_Record_Equality; | |
9685 | ||
a3068ca6 AC |
9686 | --------------------------- |
9687 | -- Expand_Set_Membership -- | |
9688 | --------------------------- | |
9689 | ||
9690 | procedure Expand_Set_Membership (N : Node_Id) is | |
9691 | Lop : constant Node_Id := Left_Opnd (N); | |
9692 | Alt : Node_Id; | |
9693 | Res : Node_Id; | |
9694 | ||
9695 | function Make_Cond (Alt : Node_Id) return Node_Id; | |
9696 | -- If the alternative is a subtype mark, create a simple membership | |
9697 | -- test. Otherwise create an equality test for it. | |
9698 | ||
9699 | --------------- | |
9700 | -- Make_Cond -- | |
9701 | --------------- | |
9702 | ||
9703 | function Make_Cond (Alt : Node_Id) return Node_Id is | |
9704 | Cond : Node_Id; | |
9705 | L : constant Node_Id := New_Copy (Lop); | |
9706 | R : constant Node_Id := Relocate_Node (Alt); | |
9707 | ||
9708 | begin | |
9709 | if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt))) | |
9710 | or else Nkind (Alt) = N_Range | |
9711 | then | |
9712 | Cond := | |
9713 | Make_In (Sloc (Alt), | |
9714 | Left_Opnd => L, | |
9715 | Right_Opnd => R); | |
9716 | else | |
9717 | Cond := | |
9718 | Make_Op_Eq (Sloc (Alt), | |
9719 | Left_Opnd => L, | |
9720 | Right_Opnd => R); | |
9721 | end if; | |
9722 | ||
9723 | return Cond; | |
9724 | end Make_Cond; | |
9725 | ||
9726 | -- Start of processing for Expand_Set_Membership | |
9727 | ||
9728 | begin | |
9729 | Remove_Side_Effects (Lop); | |
9730 | ||
9731 | Alt := Last (Alternatives (N)); | |
9732 | Res := Make_Cond (Alt); | |
9733 | ||
9734 | Prev (Alt); | |
9735 | while Present (Alt) loop | |
9736 | Res := | |
9737 | Make_Or_Else (Sloc (Alt), | |
9738 | Left_Opnd => Make_Cond (Alt), | |
9739 | Right_Opnd => Res); | |
9740 | Prev (Alt); | |
9741 | end loop; | |
9742 | ||
9743 | Rewrite (N, Res); | |
9744 | Analyze_And_Resolve (N, Standard_Boolean); | |
9745 | end Expand_Set_Membership; | |
9746 | ||
5875f8d6 AC |
9747 | ----------------------------------- |
9748 | -- Expand_Short_Circuit_Operator -- | |
9749 | ----------------------------------- | |
9750 | ||
955871d3 AC |
9751 | -- Deal with special expansion if actions are present for the right operand |
9752 | -- and deal with optimizing case of arguments being True or False. We also | |
9753 | -- deal with the special case of non-standard boolean values. | |
5875f8d6 AC |
9754 | |
9755 | procedure Expand_Short_Circuit_Operator (N : Node_Id) is | |
9756 | Loc : constant Source_Ptr := Sloc (N); | |
9757 | Typ : constant Entity_Id := Etype (N); | |
5875f8d6 AC |
9758 | Left : constant Node_Id := Left_Opnd (N); |
9759 | Right : constant Node_Id := Right_Opnd (N); | |
955871d3 | 9760 | LocR : constant Source_Ptr := Sloc (Right); |
5875f8d6 AC |
9761 | Actlist : List_Id; |
9762 | ||
9763 | Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else; | |
9764 | Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value); | |
9765 | -- If Left = Shortcut_Value then Right need not be evaluated | |
9766 | ||
25adc5fb AC |
9767 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id; |
9768 | -- For Opnd a boolean expression, return a Boolean expression equivalent | |
9769 | -- to Opnd /= Shortcut_Value. | |
9770 | ||
9771 | -------------------- | |
9772 | -- Make_Test_Expr -- | |
9773 | -------------------- | |
9774 | ||
9775 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id is | |
9776 | begin | |
9777 | if Shortcut_Value then | |
9778 | return Make_Op_Not (Sloc (Opnd), Opnd); | |
9779 | else | |
9780 | return Opnd; | |
9781 | end if; | |
9782 | end Make_Test_Expr; | |
9783 | ||
9784 | Op_Var : Entity_Id; | |
9785 | -- Entity for a temporary variable holding the value of the operator, | |
9786 | -- used for expansion in the case where actions are present. | |
9787 | ||
9788 | -- Start of processing for Expand_Short_Circuit_Operator | |
5875f8d6 AC |
9789 | |
9790 | begin | |
9791 | -- Deal with non-standard booleans | |
9792 | ||
9793 | if Is_Boolean_Type (Typ) then | |
9794 | Adjust_Condition (Left); | |
9795 | Adjust_Condition (Right); | |
9796 | Set_Etype (N, Standard_Boolean); | |
9797 | end if; | |
9798 | ||
9799 | -- Check for cases where left argument is known to be True or False | |
9800 | ||
9801 | if Compile_Time_Known_Value (Left) then | |
25adc5fb AC |
9802 | |
9803 | -- Mark SCO for left condition as compile time known | |
9804 | ||
9805 | if Generate_SCO and then Comes_From_Source (Left) then | |
9806 | Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True); | |
9807 | end if; | |
9808 | ||
5875f8d6 AC |
9809 | -- Rewrite True AND THEN Right / False OR ELSE Right to Right. |
9810 | -- Any actions associated with Right will be executed unconditionally | |
9811 | -- and can thus be inserted into the tree unconditionally. | |
9812 | ||
9813 | if Expr_Value_E (Left) /= Shortcut_Ent then | |
9814 | if Present (Actions (N)) then | |
9815 | Insert_Actions (N, Actions (N)); | |
9816 | end if; | |
9817 | ||
9818 | Rewrite (N, Right); | |
9819 | ||
9820 | -- Rewrite False AND THEN Right / True OR ELSE Right to Left. | |
9821 | -- In this case we can forget the actions associated with Right, | |
9822 | -- since they will never be executed. | |
9823 | ||
9824 | else | |
9825 | Kill_Dead_Code (Right); | |
9826 | Kill_Dead_Code (Actions (N)); | |
9827 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
9828 | end if; | |
9829 | ||
9830 | Adjust_Result_Type (N, Typ); | |
9831 | return; | |
9832 | end if; | |
9833 | ||
955871d3 AC |
9834 | -- If Actions are present for the right operand, we have to do some |
9835 | -- special processing. We can't just let these actions filter back into | |
9836 | -- code preceding the short circuit (which is what would have happened | |
9837 | -- if we had not trapped them in the short-circuit form), since they | |
9838 | -- must only be executed if the right operand of the short circuit is | |
9839 | -- executed and not otherwise. | |
5875f8d6 | 9840 | |
955871d3 | 9841 | -- the temporary variable C. |
5875f8d6 | 9842 | |
955871d3 AC |
9843 | if Present (Actions (N)) then |
9844 | Actlist := Actions (N); | |
5875f8d6 | 9845 | |
955871d3 | 9846 | -- The old approach is to expand: |
5875f8d6 | 9847 | |
955871d3 | 9848 | -- left AND THEN right |
25adc5fb | 9849 | |
955871d3 | 9850 | -- into |
25adc5fb | 9851 | |
955871d3 AC |
9852 | -- C : Boolean := False; |
9853 | -- IF left THEN | |
9854 | -- Actions; | |
9855 | -- IF right THEN | |
9856 | -- C := True; | |
9857 | -- END IF; | |
9858 | -- END IF; | |
5875f8d6 | 9859 | |
955871d3 AC |
9860 | -- and finally rewrite the operator into a reference to C. Similarly |
9861 | -- for left OR ELSE right, with negated values. Note that this | |
9862 | -- rewrite causes some difficulties for coverage analysis because | |
9863 | -- of the introduction of the new variable C, which obscures the | |
9864 | -- structure of the test. | |
5875f8d6 | 9865 | |
9cbfc269 AC |
9866 | -- We use this "old approach" if use of N_Expression_With_Actions |
9867 | -- is False (see description in Opt of when this is or is not set). | |
5875f8d6 | 9868 | |
9cbfc269 | 9869 | if not Use_Expression_With_Actions then |
955871d3 | 9870 | Op_Var := Make_Temporary (Loc, 'C', Related_Node => N); |
5875f8d6 | 9871 | |
955871d3 AC |
9872 | Insert_Action (N, |
9873 | Make_Object_Declaration (Loc, | |
9874 | Defining_Identifier => | |
9875 | Op_Var, | |
9876 | Object_Definition => | |
9877 | New_Occurrence_Of (Standard_Boolean, Loc), | |
9878 | Expression => | |
9879 | New_Occurrence_Of (Shortcut_Ent, Loc))); | |
9880 | ||
9881 | Append_To (Actlist, | |
9882 | Make_Implicit_If_Statement (Right, | |
9883 | Condition => Make_Test_Expr (Right), | |
9884 | Then_Statements => New_List ( | |
9885 | Make_Assignment_Statement (LocR, | |
9886 | Name => New_Occurrence_Of (Op_Var, LocR), | |
9887 | Expression => | |
9888 | New_Occurrence_Of | |
9889 | (Boolean_Literals (not Shortcut_Value), LocR))))); | |
5875f8d6 | 9890 | |
955871d3 AC |
9891 | Insert_Action (N, |
9892 | Make_Implicit_If_Statement (Left, | |
9893 | Condition => Make_Test_Expr (Left), | |
9894 | Then_Statements => Actlist)); | |
9895 | ||
9896 | Rewrite (N, New_Occurrence_Of (Op_Var, Loc)); | |
9897 | Analyze_And_Resolve (N, Standard_Boolean); | |
9898 | ||
9899 | -- The new approach, activated for now by the use of debug flag | |
9900 | -- -gnatd.X is to use the new Expression_With_Actions node for the | |
9901 | -- right operand of the short-circuit form. This should solve the | |
9902 | -- traceability problems for coverage analysis. | |
9903 | ||
9904 | else | |
9905 | Rewrite (Right, | |
9906 | Make_Expression_With_Actions (LocR, | |
9907 | Expression => Relocate_Node (Right), | |
9908 | Actions => Actlist)); | |
48b351d9 | 9909 | Set_Actions (N, No_List); |
955871d3 AC |
9910 | Analyze_And_Resolve (Right, Standard_Boolean); |
9911 | end if; | |
9912 | ||
5875f8d6 AC |
9913 | Adjust_Result_Type (N, Typ); |
9914 | return; | |
9915 | end if; | |
9916 | ||
9917 | -- No actions present, check for cases of right argument True/False | |
9918 | ||
9919 | if Compile_Time_Known_Value (Right) then | |
25adc5fb AC |
9920 | |
9921 | -- Mark SCO for left condition as compile time known | |
9922 | ||
9923 | if Generate_SCO and then Comes_From_Source (Right) then | |
9924 | Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True); | |
9925 | end if; | |
9926 | ||
5875f8d6 AC |
9927 | -- Change (Left and then True), (Left or else False) to Left. |
9928 | -- Note that we know there are no actions associated with the right | |
9929 | -- operand, since we just checked for this case above. | |
9930 | ||
9931 | if Expr_Value_E (Right) /= Shortcut_Ent then | |
9932 | Rewrite (N, Left); | |
9933 | ||
9934 | -- Change (Left and then False), (Left or else True) to Right, | |
9935 | -- making sure to preserve any side effects associated with the Left | |
9936 | -- operand. | |
9937 | ||
9938 | else | |
9939 | Remove_Side_Effects (Left); | |
9940 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
9941 | end if; | |
9942 | end if; | |
9943 | ||
9944 | Adjust_Result_Type (N, Typ); | |
9945 | end Expand_Short_Circuit_Operator; | |
9946 | ||
70482933 RK |
9947 | ------------------------------------- |
9948 | -- Fixup_Universal_Fixed_Operation -- | |
9949 | ------------------------------------- | |
9950 | ||
9951 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
9952 | Conv : constant Node_Id := Parent (N); | |
9953 | ||
9954 | begin | |
9955 | -- We must have a type conversion immediately above us | |
9956 | ||
9957 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
9958 | ||
9959 | -- Normally the type conversion gives our target type. The exception | |
9960 | -- occurs in the case of the Round attribute, where the conversion | |
9961 | -- will be to universal real, and our real type comes from the Round | |
9962 | -- attribute (as well as an indication that we must round the result) | |
9963 | ||
9964 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
9965 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
9966 | then | |
9967 | Set_Etype (N, Etype (Parent (Conv))); | |
9968 | Set_Rounded_Result (N); | |
9969 | ||
9970 | -- Normal case where type comes from conversion above us | |
9971 | ||
9972 | else | |
9973 | Set_Etype (N, Etype (Conv)); | |
9974 | end if; | |
9975 | end Fixup_Universal_Fixed_Operation; | |
9976 | ||
5d09245e AC |
9977 | --------------------------------- |
9978 | -- Has_Inferable_Discriminants -- | |
9979 | --------------------------------- | |
9980 | ||
9981 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
9982 | ||
9983 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
9984 | -- Determines whether the left-most prefix of a selected component is a | |
9985 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
9986 | ||
9987 | -------------------------------- | |
9988 | -- Prefix_Is_Formal_Parameter -- | |
9989 | -------------------------------- | |
9990 | ||
9991 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
9992 | Sel_Comp : Node_Id := N; | |
9993 | ||
9994 | begin | |
9995 | -- Move to the left-most prefix by climbing up the tree | |
9996 | ||
9997 | while Present (Parent (Sel_Comp)) | |
9998 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
9999 | loop | |
10000 | Sel_Comp := Parent (Sel_Comp); | |
10001 | end loop; | |
10002 | ||
10003 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
10004 | end Prefix_Is_Formal_Parameter; | |
10005 | ||
10006 | -- Start of processing for Has_Inferable_Discriminants | |
10007 | ||
10008 | begin | |
8fc789c8 | 10009 | -- For identifiers and indexed components, it is sufficient to have a |
5d09245e AC |
10010 | -- constrained Unchecked_Union nominal subtype. |
10011 | ||
303b4d58 | 10012 | if Nkind_In (N, N_Identifier, N_Indexed_Component) then |
5d09245e AC |
10013 | return Is_Unchecked_Union (Base_Type (Etype (N))) |
10014 | and then | |
10015 | Is_Constrained (Etype (N)); | |
10016 | ||
10017 | -- For selected components, the subtype of the selector must be a | |
10018 | -- constrained Unchecked_Union. If the component is subject to a | |
10019 | -- per-object constraint, then the enclosing object must have inferable | |
10020 | -- discriminants. | |
10021 | ||
10022 | elsif Nkind (N) = N_Selected_Component then | |
10023 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then | |
10024 | ||
10025 | -- A small hack. If we have a per-object constrained selected | |
10026 | -- component of a formal parameter, return True since we do not | |
10027 | -- know the actual parameter association yet. | |
10028 | ||
10029 | if Prefix_Is_Formal_Parameter (N) then | |
10030 | return True; | |
10031 | end if; | |
10032 | ||
10033 | -- Otherwise, check the enclosing object and the selector | |
10034 | ||
10035 | return Has_Inferable_Discriminants (Prefix (N)) | |
10036 | and then | |
10037 | Has_Inferable_Discriminants (Selector_Name (N)); | |
10038 | end if; | |
10039 | ||
10040 | -- The call to Has_Inferable_Discriminants will determine whether | |
10041 | -- the selector has a constrained Unchecked_Union nominal type. | |
10042 | ||
10043 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
10044 | ||
10045 | -- A qualified expression has inferable discriminants if its subtype | |
10046 | -- mark is a constrained Unchecked_Union subtype. | |
10047 | ||
10048 | elsif Nkind (N) = N_Qualified_Expression then | |
10049 | return Is_Unchecked_Union (Subtype_Mark (N)) | |
10050 | and then | |
10051 | Is_Constrained (Subtype_Mark (N)); | |
10052 | ||
10053 | end if; | |
10054 | ||
10055 | return False; | |
10056 | end Has_Inferable_Discriminants; | |
10057 | ||
70482933 RK |
10058 | ------------------------------- |
10059 | -- Insert_Dereference_Action -- | |
10060 | ------------------------------- | |
10061 | ||
10062 | procedure Insert_Dereference_Action (N : Node_Id) is | |
10063 | Loc : constant Source_Ptr := Sloc (N); | |
10064 | Typ : constant Entity_Id := Etype (N); | |
10065 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
0ab80019 | 10066 | Pnod : constant Node_Id := Parent (N); |
70482933 RK |
10067 | |
10068 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; | |
2e071734 AC |
10069 | -- Return true if type of P is derived from Checked_Pool; |
10070 | ||
10071 | ----------------------------- | |
10072 | -- Is_Checked_Storage_Pool -- | |
10073 | ----------------------------- | |
70482933 RK |
10074 | |
10075 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
10076 | T : Entity_Id; | |
10077 | ||
10078 | begin | |
10079 | if No (P) then | |
10080 | return False; | |
10081 | end if; | |
10082 | ||
10083 | T := Etype (P); | |
10084 | while T /= Etype (T) loop | |
10085 | if Is_RTE (T, RE_Checked_Pool) then | |
10086 | return True; | |
10087 | else | |
10088 | T := Etype (T); | |
10089 | end if; | |
10090 | end loop; | |
10091 | ||
10092 | return False; | |
10093 | end Is_Checked_Storage_Pool; | |
10094 | ||
10095 | -- Start of processing for Insert_Dereference_Action | |
10096 | ||
10097 | begin | |
e6f69614 AC |
10098 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
10099 | ||
0ab80019 AC |
10100 | if not (Is_Checked_Storage_Pool (Pool) |
10101 | and then Comes_From_Source (Original_Node (Pnod))) | |
e6f69614 | 10102 | then |
70482933 | 10103 | return; |
70482933 RK |
10104 | end if; |
10105 | ||
10106 | Insert_Action (N, | |
10107 | Make_Procedure_Call_Statement (Loc, | |
10108 | Name => New_Reference_To ( | |
10109 | Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), | |
10110 | ||
10111 | Parameter_Associations => New_List ( | |
10112 | ||
10113 | -- Pool | |
10114 | ||
10115 | New_Reference_To (Pool, Loc), | |
10116 | ||
685094bf RD |
10117 | -- Storage_Address. We use the attribute Pool_Address, which uses |
10118 | -- the pointer itself to find the address of the object, and which | |
10119 | -- handles unconstrained arrays properly by computing the address | |
10120 | -- of the template. i.e. the correct address of the corresponding | |
10121 | -- allocation. | |
70482933 RK |
10122 | |
10123 | Make_Attribute_Reference (Loc, | |
fbf5a39b AC |
10124 | Prefix => Duplicate_Subexpr_Move_Checks (N), |
10125 | Attribute_Name => Name_Pool_Address), | |
70482933 RK |
10126 | |
10127 | -- Size_In_Storage_Elements | |
10128 | ||
10129 | Make_Op_Divide (Loc, | |
10130 | Left_Opnd => | |
10131 | Make_Attribute_Reference (Loc, | |
10132 | Prefix => | |
fbf5a39b AC |
10133 | Make_Explicit_Dereference (Loc, |
10134 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
10135 | Attribute_Name => Name_Size), |
10136 | Right_Opnd => | |
10137 | Make_Integer_Literal (Loc, System_Storage_Unit)), | |
10138 | ||
10139 | -- Alignment | |
10140 | ||
10141 | Make_Attribute_Reference (Loc, | |
10142 | Prefix => | |
fbf5a39b AC |
10143 | Make_Explicit_Dereference (Loc, |
10144 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
10145 | Attribute_Name => Name_Alignment)))); |
10146 | ||
fbf5a39b AC |
10147 | exception |
10148 | when RE_Not_Available => | |
10149 | return; | |
70482933 RK |
10150 | end Insert_Dereference_Action; |
10151 | ||
fdfcc663 AC |
10152 | -------------------------------- |
10153 | -- Integer_Promotion_Possible -- | |
10154 | -------------------------------- | |
10155 | ||
10156 | function Integer_Promotion_Possible (N : Node_Id) return Boolean is | |
10157 | Operand : constant Node_Id := Expression (N); | |
10158 | Operand_Type : constant Entity_Id := Etype (Operand); | |
10159 | Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type); | |
10160 | ||
10161 | begin | |
10162 | pragma Assert (Nkind (N) = N_Type_Conversion); | |
10163 | ||
10164 | return | |
10165 | ||
10166 | -- We only do the transformation for source constructs. We assume | |
10167 | -- that the expander knows what it is doing when it generates code. | |
10168 | ||
10169 | Comes_From_Source (N) | |
10170 | ||
10171 | -- If the operand type is Short_Integer or Short_Short_Integer, | |
10172 | -- then we will promote to Integer, which is available on all | |
10173 | -- targets, and is sufficient to ensure no intermediate overflow. | |
10174 | -- Furthermore it is likely to be as efficient or more efficient | |
10175 | -- than using the smaller type for the computation so we do this | |
10176 | -- unconditionally. | |
10177 | ||
10178 | and then | |
10179 | (Root_Operand_Type = Base_Type (Standard_Short_Integer) | |
10180 | or else | |
10181 | Root_Operand_Type = Base_Type (Standard_Short_Short_Integer)) | |
10182 | ||
10183 | -- Test for interesting operation, which includes addition, | |
5f3f175d AC |
10184 | -- division, exponentiation, multiplication, subtraction, absolute |
10185 | -- value and unary negation. Unary "+" is omitted since it is a | |
10186 | -- no-op and thus can't overflow. | |
fdfcc663 | 10187 | |
5f3f175d AC |
10188 | and then Nkind_In (Operand, N_Op_Abs, |
10189 | N_Op_Add, | |
fdfcc663 AC |
10190 | N_Op_Divide, |
10191 | N_Op_Expon, | |
10192 | N_Op_Minus, | |
10193 | N_Op_Multiply, | |
10194 | N_Op_Subtract); | |
10195 | end Integer_Promotion_Possible; | |
10196 | ||
70482933 RK |
10197 | ------------------------------ |
10198 | -- Make_Array_Comparison_Op -- | |
10199 | ------------------------------ | |
10200 | ||
10201 | -- This is a hand-coded expansion of the following generic function: | |
10202 | ||
10203 | -- generic | |
10204 | -- type elem is (<>); | |
10205 | -- type index is (<>); | |
10206 | -- type a is array (index range <>) of elem; | |
20b5d666 | 10207 | |
70482933 RK |
10208 | -- function Gnnn (X : a; Y: a) return boolean is |
10209 | -- J : index := Y'first; | |
20b5d666 | 10210 | |
70482933 RK |
10211 | -- begin |
10212 | -- if X'length = 0 then | |
10213 | -- return false; | |
20b5d666 | 10214 | |
70482933 RK |
10215 | -- elsif Y'length = 0 then |
10216 | -- return true; | |
20b5d666 | 10217 | |
70482933 RK |
10218 | -- else |
10219 | -- for I in X'range loop | |
10220 | -- if X (I) = Y (J) then | |
10221 | -- if J = Y'last then | |
10222 | -- exit; | |
10223 | -- else | |
10224 | -- J := index'succ (J); | |
10225 | -- end if; | |
20b5d666 | 10226 | |
70482933 RK |
10227 | -- else |
10228 | -- return X (I) > Y (J); | |
10229 | -- end if; | |
10230 | -- end loop; | |
20b5d666 | 10231 | |
70482933 RK |
10232 | -- return X'length > Y'length; |
10233 | -- end if; | |
10234 | -- end Gnnn; | |
10235 | ||
10236 | -- Note that since we are essentially doing this expansion by hand, we | |
10237 | -- do not need to generate an actual or formal generic part, just the | |
10238 | -- instantiated function itself. | |
10239 | ||
10240 | function Make_Array_Comparison_Op | |
2e071734 AC |
10241 | (Typ : Entity_Id; |
10242 | Nod : Node_Id) return Node_Id | |
70482933 RK |
10243 | is |
10244 | Loc : constant Source_Ptr := Sloc (Nod); | |
10245 | ||
10246 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
10247 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
10248 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
10249 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
10250 | ||
10251 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
10252 | ||
10253 | Loop_Statement : Node_Id; | |
10254 | Loop_Body : Node_Id; | |
10255 | If_Stat : Node_Id; | |
10256 | Inner_If : Node_Id; | |
10257 | Final_Expr : Node_Id; | |
10258 | Func_Body : Node_Id; | |
10259 | Func_Name : Entity_Id; | |
10260 | Formals : List_Id; | |
10261 | Length1 : Node_Id; | |
10262 | Length2 : Node_Id; | |
10263 | ||
10264 | begin | |
10265 | -- if J = Y'last then | |
10266 | -- exit; | |
10267 | -- else | |
10268 | -- J := index'succ (J); | |
10269 | -- end if; | |
10270 | ||
10271 | Inner_If := | |
10272 | Make_Implicit_If_Statement (Nod, | |
10273 | Condition => | |
10274 | Make_Op_Eq (Loc, | |
10275 | Left_Opnd => New_Reference_To (J, Loc), | |
10276 | Right_Opnd => | |
10277 | Make_Attribute_Reference (Loc, | |
10278 | Prefix => New_Reference_To (Y, Loc), | |
10279 | Attribute_Name => Name_Last)), | |
10280 | ||
10281 | Then_Statements => New_List ( | |
10282 | Make_Exit_Statement (Loc)), | |
10283 | ||
10284 | Else_Statements => | |
10285 | New_List ( | |
10286 | Make_Assignment_Statement (Loc, | |
10287 | Name => New_Reference_To (J, Loc), | |
10288 | Expression => | |
10289 | Make_Attribute_Reference (Loc, | |
10290 | Prefix => New_Reference_To (Index, Loc), | |
10291 | Attribute_Name => Name_Succ, | |
10292 | Expressions => New_List (New_Reference_To (J, Loc)))))); | |
10293 | ||
10294 | -- if X (I) = Y (J) then | |
10295 | -- if ... end if; | |
10296 | -- else | |
10297 | -- return X (I) > Y (J); | |
10298 | -- end if; | |
10299 | ||
10300 | Loop_Body := | |
10301 | Make_Implicit_If_Statement (Nod, | |
10302 | Condition => | |
10303 | Make_Op_Eq (Loc, | |
10304 | Left_Opnd => | |
10305 | Make_Indexed_Component (Loc, | |
10306 | Prefix => New_Reference_To (X, Loc), | |
10307 | Expressions => New_List (New_Reference_To (I, Loc))), | |
10308 | ||
10309 | Right_Opnd => | |
10310 | Make_Indexed_Component (Loc, | |
10311 | Prefix => New_Reference_To (Y, Loc), | |
10312 | Expressions => New_List (New_Reference_To (J, Loc)))), | |
10313 | ||
10314 | Then_Statements => New_List (Inner_If), | |
10315 | ||
10316 | Else_Statements => New_List ( | |
d766cee3 | 10317 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10318 | Expression => |
10319 | Make_Op_Gt (Loc, | |
10320 | Left_Opnd => | |
10321 | Make_Indexed_Component (Loc, | |
10322 | Prefix => New_Reference_To (X, Loc), | |
10323 | Expressions => New_List (New_Reference_To (I, Loc))), | |
10324 | ||
10325 | Right_Opnd => | |
10326 | Make_Indexed_Component (Loc, | |
10327 | Prefix => New_Reference_To (Y, Loc), | |
10328 | Expressions => New_List ( | |
10329 | New_Reference_To (J, Loc))))))); | |
10330 | ||
10331 | -- for I in X'range loop | |
10332 | -- if ... end if; | |
10333 | -- end loop; | |
10334 | ||
10335 | Loop_Statement := | |
10336 | Make_Implicit_Loop_Statement (Nod, | |
10337 | Identifier => Empty, | |
10338 | ||
10339 | Iteration_Scheme => | |
10340 | Make_Iteration_Scheme (Loc, | |
10341 | Loop_Parameter_Specification => | |
10342 | Make_Loop_Parameter_Specification (Loc, | |
10343 | Defining_Identifier => I, | |
10344 | Discrete_Subtype_Definition => | |
10345 | Make_Attribute_Reference (Loc, | |
10346 | Prefix => New_Reference_To (X, Loc), | |
10347 | Attribute_Name => Name_Range))), | |
10348 | ||
10349 | Statements => New_List (Loop_Body)); | |
10350 | ||
10351 | -- if X'length = 0 then | |
10352 | -- return false; | |
10353 | -- elsif Y'length = 0 then | |
10354 | -- return true; | |
10355 | -- else | |
10356 | -- for ... loop ... end loop; | |
10357 | -- return X'length > Y'length; | |
10358 | -- end if; | |
10359 | ||
10360 | Length1 := | |
10361 | Make_Attribute_Reference (Loc, | |
10362 | Prefix => New_Reference_To (X, Loc), | |
10363 | Attribute_Name => Name_Length); | |
10364 | ||
10365 | Length2 := | |
10366 | Make_Attribute_Reference (Loc, | |
10367 | Prefix => New_Reference_To (Y, Loc), | |
10368 | Attribute_Name => Name_Length); | |
10369 | ||
10370 | Final_Expr := | |
10371 | Make_Op_Gt (Loc, | |
10372 | Left_Opnd => Length1, | |
10373 | Right_Opnd => Length2); | |
10374 | ||
10375 | If_Stat := | |
10376 | Make_Implicit_If_Statement (Nod, | |
10377 | Condition => | |
10378 | Make_Op_Eq (Loc, | |
10379 | Left_Opnd => | |
10380 | Make_Attribute_Reference (Loc, | |
10381 | Prefix => New_Reference_To (X, Loc), | |
10382 | Attribute_Name => Name_Length), | |
10383 | Right_Opnd => | |
10384 | Make_Integer_Literal (Loc, 0)), | |
10385 | ||
10386 | Then_Statements => | |
10387 | New_List ( | |
d766cee3 | 10388 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10389 | Expression => New_Reference_To (Standard_False, Loc))), |
10390 | ||
10391 | Elsif_Parts => New_List ( | |
10392 | Make_Elsif_Part (Loc, | |
10393 | Condition => | |
10394 | Make_Op_Eq (Loc, | |
10395 | Left_Opnd => | |
10396 | Make_Attribute_Reference (Loc, | |
10397 | Prefix => New_Reference_To (Y, Loc), | |
10398 | Attribute_Name => Name_Length), | |
10399 | Right_Opnd => | |
10400 | Make_Integer_Literal (Loc, 0)), | |
10401 | ||
10402 | Then_Statements => | |
10403 | New_List ( | |
d766cee3 | 10404 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10405 | Expression => New_Reference_To (Standard_True, Loc))))), |
10406 | ||
10407 | Else_Statements => New_List ( | |
10408 | Loop_Statement, | |
d766cee3 | 10409 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10410 | Expression => Final_Expr))); |
10411 | ||
10412 | -- (X : a; Y: a) | |
10413 | ||
10414 | Formals := New_List ( | |
10415 | Make_Parameter_Specification (Loc, | |
10416 | Defining_Identifier => X, | |
10417 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
10418 | ||
10419 | Make_Parameter_Specification (Loc, | |
10420 | Defining_Identifier => Y, | |
10421 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
10422 | ||
10423 | -- function Gnnn (...) return boolean is | |
10424 | -- J : index := Y'first; | |
10425 | -- begin | |
10426 | -- if ... end if; | |
10427 | -- end Gnnn; | |
10428 | ||
191fcb3a | 10429 | Func_Name := Make_Temporary (Loc, 'G'); |
70482933 RK |
10430 | |
10431 | Func_Body := | |
10432 | Make_Subprogram_Body (Loc, | |
10433 | Specification => | |
10434 | Make_Function_Specification (Loc, | |
10435 | Defining_Unit_Name => Func_Name, | |
10436 | Parameter_Specifications => Formals, | |
630d30e9 | 10437 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
70482933 RK |
10438 | |
10439 | Declarations => New_List ( | |
10440 | Make_Object_Declaration (Loc, | |
10441 | Defining_Identifier => J, | |
10442 | Object_Definition => New_Reference_To (Index, Loc), | |
10443 | Expression => | |
10444 | Make_Attribute_Reference (Loc, | |
10445 | Prefix => New_Reference_To (Y, Loc), | |
10446 | Attribute_Name => Name_First))), | |
10447 | ||
10448 | Handled_Statement_Sequence => | |
10449 | Make_Handled_Sequence_Of_Statements (Loc, | |
10450 | Statements => New_List (If_Stat))); | |
10451 | ||
10452 | return Func_Body; | |
70482933 RK |
10453 | end Make_Array_Comparison_Op; |
10454 | ||
10455 | --------------------------- | |
10456 | -- Make_Boolean_Array_Op -- | |
10457 | --------------------------- | |
10458 | ||
685094bf RD |
10459 | -- For logical operations on boolean arrays, expand in line the following, |
10460 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
10461 | |
10462 | -- function Annn (A : typ; B: typ) return typ is | |
10463 | -- C : typ; | |
10464 | -- begin | |
10465 | -- for J in A'range loop | |
10466 | -- C (J) := A (J) op B (J); | |
10467 | -- end loop; | |
10468 | -- return C; | |
10469 | -- end Annn; | |
10470 | ||
10471 | -- Here typ is the boolean array type | |
10472 | ||
10473 | function Make_Boolean_Array_Op | |
2e071734 AC |
10474 | (Typ : Entity_Id; |
10475 | N : Node_Id) return Node_Id | |
70482933 RK |
10476 | is |
10477 | Loc : constant Source_Ptr := Sloc (N); | |
10478 | ||
10479 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
10480 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
10481 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
10482 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
10483 | ||
10484 | A_J : Node_Id; | |
10485 | B_J : Node_Id; | |
10486 | C_J : Node_Id; | |
10487 | Op : Node_Id; | |
10488 | ||
10489 | Formals : List_Id; | |
10490 | Func_Name : Entity_Id; | |
10491 | Func_Body : Node_Id; | |
10492 | Loop_Statement : Node_Id; | |
10493 | ||
10494 | begin | |
10495 | A_J := | |
10496 | Make_Indexed_Component (Loc, | |
10497 | Prefix => New_Reference_To (A, Loc), | |
10498 | Expressions => New_List (New_Reference_To (J, Loc))); | |
10499 | ||
10500 | B_J := | |
10501 | Make_Indexed_Component (Loc, | |
10502 | Prefix => New_Reference_To (B, Loc), | |
10503 | Expressions => New_List (New_Reference_To (J, Loc))); | |
10504 | ||
10505 | C_J := | |
10506 | Make_Indexed_Component (Loc, | |
10507 | Prefix => New_Reference_To (C, Loc), | |
10508 | Expressions => New_List (New_Reference_To (J, Loc))); | |
10509 | ||
10510 | if Nkind (N) = N_Op_And then | |
10511 | Op := | |
10512 | Make_Op_And (Loc, | |
10513 | Left_Opnd => A_J, | |
10514 | Right_Opnd => B_J); | |
10515 | ||
10516 | elsif Nkind (N) = N_Op_Or then | |
10517 | Op := | |
10518 | Make_Op_Or (Loc, | |
10519 | Left_Opnd => A_J, | |
10520 | Right_Opnd => B_J); | |
10521 | ||
10522 | else | |
10523 | Op := | |
10524 | Make_Op_Xor (Loc, | |
10525 | Left_Opnd => A_J, | |
10526 | Right_Opnd => B_J); | |
10527 | end if; | |
10528 | ||
10529 | Loop_Statement := | |
10530 | Make_Implicit_Loop_Statement (N, | |
10531 | Identifier => Empty, | |
10532 | ||
10533 | Iteration_Scheme => | |
10534 | Make_Iteration_Scheme (Loc, | |
10535 | Loop_Parameter_Specification => | |
10536 | Make_Loop_Parameter_Specification (Loc, | |
10537 | Defining_Identifier => J, | |
10538 | Discrete_Subtype_Definition => | |
10539 | Make_Attribute_Reference (Loc, | |
10540 | Prefix => New_Reference_To (A, Loc), | |
10541 | Attribute_Name => Name_Range))), | |
10542 | ||
10543 | Statements => New_List ( | |
10544 | Make_Assignment_Statement (Loc, | |
10545 | Name => C_J, | |
10546 | Expression => Op))); | |
10547 | ||
10548 | Formals := New_List ( | |
10549 | Make_Parameter_Specification (Loc, | |
10550 | Defining_Identifier => A, | |
10551 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
10552 | ||
10553 | Make_Parameter_Specification (Loc, | |
10554 | Defining_Identifier => B, | |
10555 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
10556 | ||
191fcb3a | 10557 | Func_Name := Make_Temporary (Loc, 'A'); |
70482933 RK |
10558 | Set_Is_Inlined (Func_Name); |
10559 | ||
10560 | Func_Body := | |
10561 | Make_Subprogram_Body (Loc, | |
10562 | Specification => | |
10563 | Make_Function_Specification (Loc, | |
10564 | Defining_Unit_Name => Func_Name, | |
10565 | Parameter_Specifications => Formals, | |
630d30e9 | 10566 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
10567 | |
10568 | Declarations => New_List ( | |
10569 | Make_Object_Declaration (Loc, | |
10570 | Defining_Identifier => C, | |
10571 | Object_Definition => New_Reference_To (Typ, Loc))), | |
10572 | ||
10573 | Handled_Statement_Sequence => | |
10574 | Make_Handled_Sequence_Of_Statements (Loc, | |
10575 | Statements => New_List ( | |
10576 | Loop_Statement, | |
d766cee3 | 10577 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10578 | Expression => New_Reference_To (C, Loc))))); |
10579 | ||
10580 | return Func_Body; | |
10581 | end Make_Boolean_Array_Op; | |
10582 | ||
0580d807 AC |
10583 | -------------------------------- |
10584 | -- Optimize_Length_Comparison -- | |
10585 | -------------------------------- | |
10586 | ||
10587 | procedure Optimize_Length_Comparison (N : Node_Id) is | |
10588 | Loc : constant Source_Ptr := Sloc (N); | |
10589 | Typ : constant Entity_Id := Etype (N); | |
10590 | Result : Node_Id; | |
10591 | ||
10592 | Left : Node_Id; | |
10593 | Right : Node_Id; | |
10594 | -- First and Last attribute reference nodes, which end up as left and | |
10595 | -- right operands of the optimized result. | |
10596 | ||
10597 | Is_Zero : Boolean; | |
10598 | -- True for comparison operand of zero | |
10599 | ||
10600 | Comp : Node_Id; | |
10601 | -- Comparison operand, set only if Is_Zero is false | |
10602 | ||
10603 | Ent : Entity_Id; | |
10604 | -- Entity whose length is being compared | |
10605 | ||
10606 | Index : Node_Id; | |
10607 | -- Integer_Literal node for length attribute expression, or Empty | |
10608 | -- if there is no such expression present. | |
10609 | ||
10610 | Ityp : Entity_Id; | |
10611 | -- Type of array index to which 'Length is applied | |
10612 | ||
10613 | Op : Node_Kind := Nkind (N); | |
10614 | -- Kind of comparison operator, gets flipped if operands backwards | |
10615 | ||
10616 | function Is_Optimizable (N : Node_Id) return Boolean; | |
abcd9db2 AC |
10617 | -- Tests N to see if it is an optimizable comparison value (defined as |
10618 | -- constant zero or one, or something else where the value is known to | |
10619 | -- be positive and in the range of 32-bits, and where the corresponding | |
10620 | -- Length value is also known to be 32-bits. If result is true, sets | |
10621 | -- Is_Zero, Ityp, and Comp accordingly. | |
0580d807 AC |
10622 | |
10623 | function Is_Entity_Length (N : Node_Id) return Boolean; | |
10624 | -- Tests if N is a length attribute applied to a simple entity. If so, | |
10625 | -- returns True, and sets Ent to the entity, and Index to the integer | |
10626 | -- literal provided as an attribute expression, or to Empty if none. | |
10627 | -- Also returns True if the expression is a generated type conversion | |
10628 | -- whose expression is of the desired form. This latter case arises | |
10629 | -- when Apply_Universal_Integer_Attribute_Check installs a conversion | |
10630 | -- to check for being in range, which is not needed in this context. | |
10631 | -- Returns False if neither condition holds. | |
10632 | ||
10633 | function Prepare_64 (N : Node_Id) return Node_Id; | |
10634 | -- Given a discrete expression, returns a Long_Long_Integer typed | |
10635 | -- expression representing the underlying value of the expression. | |
10636 | -- This is done with an unchecked conversion to the result type. We | |
10637 | -- use unchecked conversion to handle the enumeration type case. | |
10638 | ||
10639 | ---------------------- | |
10640 | -- Is_Entity_Length -- | |
10641 | ---------------------- | |
10642 | ||
10643 | function Is_Entity_Length (N : Node_Id) return Boolean is | |
10644 | begin | |
10645 | if Nkind (N) = N_Attribute_Reference | |
10646 | and then Attribute_Name (N) = Name_Length | |
10647 | and then Is_Entity_Name (Prefix (N)) | |
10648 | then | |
10649 | Ent := Entity (Prefix (N)); | |
10650 | ||
10651 | if Present (Expressions (N)) then | |
10652 | Index := First (Expressions (N)); | |
10653 | else | |
10654 | Index := Empty; | |
10655 | end if; | |
10656 | ||
10657 | return True; | |
10658 | ||
10659 | elsif Nkind (N) = N_Type_Conversion | |
10660 | and then not Comes_From_Source (N) | |
10661 | then | |
10662 | return Is_Entity_Length (Expression (N)); | |
10663 | ||
10664 | else | |
10665 | return False; | |
10666 | end if; | |
10667 | end Is_Entity_Length; | |
10668 | ||
10669 | -------------------- | |
10670 | -- Is_Optimizable -- | |
10671 | -------------------- | |
10672 | ||
10673 | function Is_Optimizable (N : Node_Id) return Boolean is | |
10674 | Val : Uint; | |
10675 | OK : Boolean; | |
10676 | Lo : Uint; | |
10677 | Hi : Uint; | |
10678 | Indx : Node_Id; | |
10679 | ||
10680 | begin | |
10681 | if Compile_Time_Known_Value (N) then | |
10682 | Val := Expr_Value (N); | |
10683 | ||
10684 | if Val = Uint_0 then | |
10685 | Is_Zero := True; | |
10686 | Comp := Empty; | |
10687 | return True; | |
10688 | ||
10689 | elsif Val = Uint_1 then | |
10690 | Is_Zero := False; | |
10691 | Comp := Empty; | |
10692 | return True; | |
10693 | end if; | |
10694 | end if; | |
10695 | ||
10696 | -- Here we have to make sure of being within 32-bits | |
10697 | ||
10698 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); | |
10699 | ||
10700 | if not OK | |
abcd9db2 | 10701 | or else Lo < Uint_1 |
0580d807 AC |
10702 | or else Hi > UI_From_Int (Int'Last) |
10703 | then | |
10704 | return False; | |
10705 | end if; | |
10706 | ||
abcd9db2 AC |
10707 | -- Comparison value was within range, so now we must check the index |
10708 | -- value to make sure it is also within 32-bits. | |
0580d807 AC |
10709 | |
10710 | Indx := First_Index (Etype (Ent)); | |
10711 | ||
10712 | if Present (Index) then | |
10713 | for J in 2 .. UI_To_Int (Intval (Index)) loop | |
10714 | Next_Index (Indx); | |
10715 | end loop; | |
10716 | end if; | |
10717 | ||
10718 | Ityp := Etype (Indx); | |
10719 | ||
10720 | if Esize (Ityp) > 32 then | |
10721 | return False; | |
10722 | end if; | |
10723 | ||
10724 | Is_Zero := False; | |
10725 | Comp := N; | |
10726 | return True; | |
10727 | end Is_Optimizable; | |
10728 | ||
10729 | ---------------- | |
10730 | -- Prepare_64 -- | |
10731 | ---------------- | |
10732 | ||
10733 | function Prepare_64 (N : Node_Id) return Node_Id is | |
10734 | begin | |
10735 | return Unchecked_Convert_To (Standard_Long_Long_Integer, N); | |
10736 | end Prepare_64; | |
10737 | ||
10738 | -- Start of processing for Optimize_Length_Comparison | |
10739 | ||
10740 | begin | |
10741 | -- Nothing to do if not a comparison | |
10742 | ||
10743 | if Op not in N_Op_Compare then | |
10744 | return; | |
10745 | end if; | |
10746 | ||
10747 | -- Nothing to do if special -gnatd.P debug flag set | |
10748 | ||
10749 | if Debug_Flag_Dot_PP then | |
10750 | return; | |
10751 | end if; | |
10752 | ||
10753 | -- Ent'Length op 0/1 | |
10754 | ||
10755 | if Is_Entity_Length (Left_Opnd (N)) | |
10756 | and then Is_Optimizable (Right_Opnd (N)) | |
10757 | then | |
10758 | null; | |
10759 | ||
10760 | -- 0/1 op Ent'Length | |
10761 | ||
10762 | elsif Is_Entity_Length (Right_Opnd (N)) | |
10763 | and then Is_Optimizable (Left_Opnd (N)) | |
10764 | then | |
10765 | -- Flip comparison to opposite sense | |
10766 | ||
10767 | case Op is | |
10768 | when N_Op_Lt => Op := N_Op_Gt; | |
10769 | when N_Op_Le => Op := N_Op_Ge; | |
10770 | when N_Op_Gt => Op := N_Op_Lt; | |
10771 | when N_Op_Ge => Op := N_Op_Le; | |
10772 | when others => null; | |
10773 | end case; | |
10774 | ||
10775 | -- Else optimization not possible | |
10776 | ||
10777 | else | |
10778 | return; | |
10779 | end if; | |
10780 | ||
10781 | -- Fall through if we will do the optimization | |
10782 | ||
10783 | -- Cases to handle: | |
10784 | ||
10785 | -- X'Length = 0 => X'First > X'Last | |
10786 | -- X'Length = 1 => X'First = X'Last | |
10787 | -- X'Length = n => X'First + (n - 1) = X'Last | |
10788 | ||
10789 | -- X'Length /= 0 => X'First <= X'Last | |
10790 | -- X'Length /= 1 => X'First /= X'Last | |
10791 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
10792 | ||
10793 | -- X'Length >= 0 => always true, warn | |
10794 | -- X'Length >= 1 => X'First <= X'Last | |
10795 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
10796 | ||
10797 | -- X'Length > 0 => X'First <= X'Last | |
10798 | -- X'Length > 1 => X'First < X'Last | |
10799 | -- X'Length > n => X'First + (n - 1) < X'Last | |
10800 | ||
10801 | -- X'Length <= 0 => X'First > X'Last (warn, could be =) | |
10802 | -- X'Length <= 1 => X'First >= X'Last | |
10803 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
10804 | ||
10805 | -- X'Length < 0 => always false (warn) | |
10806 | -- X'Length < 1 => X'First > X'Last | |
10807 | -- X'Length < n => X'First + (n - 1) > X'Last | |
10808 | ||
10809 | -- Note: for the cases of n (not constant 0,1), we require that the | |
10810 | -- corresponding index type be integer or shorter (i.e. not 64-bit), | |
10811 | -- and the same for the comparison value. Then we do the comparison | |
10812 | -- using 64-bit arithmetic (actually long long integer), so that we | |
10813 | -- cannot have overflow intefering with the result. | |
10814 | ||
10815 | -- First deal with warning cases | |
10816 | ||
10817 | if Is_Zero then | |
10818 | case Op is | |
10819 | ||
10820 | -- X'Length >= 0 | |
10821 | ||
10822 | when N_Op_Ge => | |
10823 | Rewrite (N, | |
10824 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc))); | |
10825 | Analyze_And_Resolve (N, Typ); | |
10826 | Warn_On_Known_Condition (N); | |
10827 | return; | |
10828 | ||
10829 | -- X'Length < 0 | |
10830 | ||
10831 | when N_Op_Lt => | |
10832 | Rewrite (N, | |
10833 | Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc))); | |
10834 | Analyze_And_Resolve (N, Typ); | |
10835 | Warn_On_Known_Condition (N); | |
10836 | return; | |
10837 | ||
10838 | when N_Op_Le => | |
10839 | if Constant_Condition_Warnings | |
10840 | and then Comes_From_Source (Original_Node (N)) | |
10841 | then | |
10842 | Error_Msg_N ("could replace by ""'=""?", N); | |
10843 | end if; | |
10844 | ||
10845 | Op := N_Op_Eq; | |
10846 | ||
10847 | when others => | |
10848 | null; | |
10849 | end case; | |
10850 | end if; | |
10851 | ||
10852 | -- Build the First reference we will use | |
10853 | ||
10854 | Left := | |
10855 | Make_Attribute_Reference (Loc, | |
10856 | Prefix => New_Occurrence_Of (Ent, Loc), | |
10857 | Attribute_Name => Name_First); | |
10858 | ||
10859 | if Present (Index) then | |
10860 | Set_Expressions (Left, New_List (New_Copy (Index))); | |
10861 | end if; | |
10862 | ||
10863 | -- If general value case, then do the addition of (n - 1), and | |
10864 | -- also add the needed conversions to type Long_Long_Integer. | |
10865 | ||
10866 | if Present (Comp) then | |
10867 | Left := | |
10868 | Make_Op_Add (Loc, | |
10869 | Left_Opnd => Prepare_64 (Left), | |
10870 | Right_Opnd => | |
10871 | Make_Op_Subtract (Loc, | |
10872 | Left_Opnd => Prepare_64 (Comp), | |
10873 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
10874 | end if; | |
10875 | ||
10876 | -- Build the Last reference we will use | |
10877 | ||
10878 | Right := | |
10879 | Make_Attribute_Reference (Loc, | |
10880 | Prefix => New_Occurrence_Of (Ent, Loc), | |
10881 | Attribute_Name => Name_Last); | |
10882 | ||
10883 | if Present (Index) then | |
10884 | Set_Expressions (Right, New_List (New_Copy (Index))); | |
10885 | end if; | |
10886 | ||
10887 | -- If general operand, convert Last reference to Long_Long_Integer | |
10888 | ||
10889 | if Present (Comp) then | |
10890 | Right := Prepare_64 (Right); | |
10891 | end if; | |
10892 | ||
10893 | -- Check for cases to optimize | |
10894 | ||
10895 | -- X'Length = 0 => X'First > X'Last | |
10896 | -- X'Length < 1 => X'First > X'Last | |
10897 | -- X'Length < n => X'First + (n - 1) > X'Last | |
10898 | ||
10899 | if (Is_Zero and then Op = N_Op_Eq) | |
10900 | or else (not Is_Zero and then Op = N_Op_Lt) | |
10901 | then | |
10902 | Result := | |
10903 | Make_Op_Gt (Loc, | |
10904 | Left_Opnd => Left, | |
10905 | Right_Opnd => Right); | |
10906 | ||
10907 | -- X'Length = 1 => X'First = X'Last | |
10908 | -- X'Length = n => X'First + (n - 1) = X'Last | |
10909 | ||
10910 | elsif not Is_Zero and then Op = N_Op_Eq then | |
10911 | Result := | |
10912 | Make_Op_Eq (Loc, | |
10913 | Left_Opnd => Left, | |
10914 | Right_Opnd => Right); | |
10915 | ||
10916 | -- X'Length /= 0 => X'First <= X'Last | |
10917 | -- X'Length > 0 => X'First <= X'Last | |
10918 | ||
10919 | elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then | |
10920 | Result := | |
10921 | Make_Op_Le (Loc, | |
10922 | Left_Opnd => Left, | |
10923 | Right_Opnd => Right); | |
10924 | ||
10925 | -- X'Length /= 1 => X'First /= X'Last | |
10926 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
10927 | ||
10928 | elsif not Is_Zero and then Op = N_Op_Ne then | |
10929 | Result := | |
10930 | Make_Op_Ne (Loc, | |
10931 | Left_Opnd => Left, | |
10932 | Right_Opnd => Right); | |
10933 | ||
10934 | -- X'Length >= 1 => X'First <= X'Last | |
10935 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
10936 | ||
10937 | elsif not Is_Zero and then Op = N_Op_Ge then | |
10938 | Result := | |
10939 | Make_Op_Le (Loc, | |
10940 | Left_Opnd => Left, | |
10941 | Right_Opnd => Right); | |
10942 | ||
10943 | -- X'Length > 1 => X'First < X'Last | |
10944 | -- X'Length > n => X'First + (n = 1) < X'Last | |
10945 | ||
10946 | elsif not Is_Zero and then Op = N_Op_Gt then | |
10947 | Result := | |
10948 | Make_Op_Lt (Loc, | |
10949 | Left_Opnd => Left, | |
10950 | Right_Opnd => Right); | |
10951 | ||
10952 | -- X'Length <= 1 => X'First >= X'Last | |
10953 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
10954 | ||
10955 | elsif not Is_Zero and then Op = N_Op_Le then | |
10956 | Result := | |
10957 | Make_Op_Ge (Loc, | |
10958 | Left_Opnd => Left, | |
10959 | Right_Opnd => Right); | |
10960 | ||
10961 | -- Should not happen at this stage | |
10962 | ||
10963 | else | |
10964 | raise Program_Error; | |
10965 | end if; | |
10966 | ||
10967 | -- Rewrite and finish up | |
10968 | ||
10969 | Rewrite (N, Result); | |
10970 | Analyze_And_Resolve (N, Typ); | |
10971 | return; | |
10972 | end Optimize_Length_Comparison; | |
10973 | ||
70482933 RK |
10974 | ------------------------ |
10975 | -- Rewrite_Comparison -- | |
10976 | ------------------------ | |
10977 | ||
10978 | procedure Rewrite_Comparison (N : Node_Id) is | |
c800f862 RD |
10979 | Warning_Generated : Boolean := False; |
10980 | -- Set to True if first pass with Assume_Valid generates a warning in | |
10981 | -- which case we skip the second pass to avoid warning overloaded. | |
10982 | ||
10983 | Result : Node_Id; | |
10984 | -- Set to Standard_True or Standard_False | |
10985 | ||
d26dc4b5 AC |
10986 | begin |
10987 | if Nkind (N) = N_Type_Conversion then | |
10988 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 10989 | return; |
70482933 | 10990 | |
d26dc4b5 | 10991 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
10992 | return; |
10993 | end if; | |
70482933 | 10994 | |
c800f862 RD |
10995 | -- Now start looking at the comparison in detail. We potentially go |
10996 | -- through this loop twice. The first time, Assume_Valid is set False | |
10997 | -- in the call to Compile_Time_Compare. If this call results in a | |
10998 | -- clear result of always True or Always False, that's decisive and | |
10999 | -- we are done. Otherwise we repeat the processing with Assume_Valid | |
e7e4d230 | 11000 | -- set to True to generate additional warnings. We can skip that step |
c800f862 RD |
11001 | -- if Constant_Condition_Warnings is False. |
11002 | ||
11003 | for AV in False .. True loop | |
11004 | declare | |
11005 | Typ : constant Entity_Id := Etype (N); | |
11006 | Op1 : constant Node_Id := Left_Opnd (N); | |
11007 | Op2 : constant Node_Id := Right_Opnd (N); | |
70482933 | 11008 | |
c800f862 RD |
11009 | Res : constant Compare_Result := |
11010 | Compile_Time_Compare (Op1, Op2, Assume_Valid => AV); | |
11011 | -- Res indicates if compare outcome can be compile time determined | |
f02b8bb8 | 11012 | |
c800f862 RD |
11013 | True_Result : Boolean; |
11014 | False_Result : Boolean; | |
f02b8bb8 | 11015 | |
c800f862 RD |
11016 | begin |
11017 | case N_Op_Compare (Nkind (N)) is | |
d26dc4b5 AC |
11018 | when N_Op_Eq => |
11019 | True_Result := Res = EQ; | |
11020 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
11021 | ||
11022 | when N_Op_Ge => | |
11023 | True_Result := Res in Compare_GE; | |
11024 | False_Result := Res = LT; | |
11025 | ||
11026 | if Res = LE | |
11027 | and then Constant_Condition_Warnings | |
11028 | and then Comes_From_Source (Original_Node (N)) | |
11029 | and then Nkind (Original_Node (N)) = N_Op_Ge | |
11030 | and then not In_Instance | |
d26dc4b5 | 11031 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 11032 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 11033 | then |
ed2233dc | 11034 | Error_Msg_N |
d26dc4b5 | 11035 | ("can never be greater than, could replace by ""'=""?", N); |
c800f862 | 11036 | Warning_Generated := True; |
d26dc4b5 | 11037 | end if; |
70482933 | 11038 | |
d26dc4b5 AC |
11039 | when N_Op_Gt => |
11040 | True_Result := Res = GT; | |
11041 | False_Result := Res in Compare_LE; | |
11042 | ||
11043 | when N_Op_Lt => | |
11044 | True_Result := Res = LT; | |
11045 | False_Result := Res in Compare_GE; | |
11046 | ||
11047 | when N_Op_Le => | |
11048 | True_Result := Res in Compare_LE; | |
11049 | False_Result := Res = GT; | |
11050 | ||
11051 | if Res = GE | |
11052 | and then Constant_Condition_Warnings | |
11053 | and then Comes_From_Source (Original_Node (N)) | |
11054 | and then Nkind (Original_Node (N)) = N_Op_Le | |
11055 | and then not In_Instance | |
d26dc4b5 | 11056 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 11057 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 11058 | then |
ed2233dc | 11059 | Error_Msg_N |
d26dc4b5 | 11060 | ("can never be less than, could replace by ""'=""?", N); |
c800f862 | 11061 | Warning_Generated := True; |
d26dc4b5 | 11062 | end if; |
70482933 | 11063 | |
d26dc4b5 AC |
11064 | when N_Op_Ne => |
11065 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
11066 | False_Result := Res = EQ; | |
c800f862 | 11067 | end case; |
d26dc4b5 | 11068 | |
c800f862 RD |
11069 | -- If this is the first iteration, then we actually convert the |
11070 | -- comparison into True or False, if the result is certain. | |
d26dc4b5 | 11071 | |
c800f862 RD |
11072 | if AV = False then |
11073 | if True_Result or False_Result then | |
11074 | if True_Result then | |
11075 | Result := Standard_True; | |
11076 | else | |
11077 | Result := Standard_False; | |
11078 | end if; | |
11079 | ||
11080 | Rewrite (N, | |
11081 | Convert_To (Typ, | |
11082 | New_Occurrence_Of (Result, Sloc (N)))); | |
11083 | Analyze_And_Resolve (N, Typ); | |
11084 | Warn_On_Known_Condition (N); | |
11085 | return; | |
11086 | end if; | |
11087 | ||
11088 | -- If this is the second iteration (AV = True), and the original | |
e7e4d230 AC |
11089 | -- node comes from source and we are not in an instance, then give |
11090 | -- a warning if we know result would be True or False. Note: we | |
11091 | -- know Constant_Condition_Warnings is set if we get here. | |
c800f862 RD |
11092 | |
11093 | elsif Comes_From_Source (Original_Node (N)) | |
11094 | and then not In_Instance | |
11095 | then | |
11096 | if True_Result then | |
ed2233dc | 11097 | Error_Msg_N |
c800f862 RD |
11098 | ("condition can only be False if invalid values present?", |
11099 | N); | |
11100 | elsif False_Result then | |
ed2233dc | 11101 | Error_Msg_N |
c800f862 RD |
11102 | ("condition can only be True if invalid values present?", |
11103 | N); | |
11104 | end if; | |
11105 | end if; | |
11106 | end; | |
11107 | ||
11108 | -- Skip second iteration if not warning on constant conditions or | |
e7e4d230 AC |
11109 | -- if the first iteration already generated a warning of some kind or |
11110 | -- if we are in any case assuming all values are valid (so that the | |
11111 | -- first iteration took care of the valid case). | |
c800f862 RD |
11112 | |
11113 | exit when not Constant_Condition_Warnings; | |
11114 | exit when Warning_Generated; | |
11115 | exit when Assume_No_Invalid_Values; | |
11116 | end loop; | |
70482933 RK |
11117 | end Rewrite_Comparison; |
11118 | ||
fbf5a39b AC |
11119 | ---------------------------- |
11120 | -- Safe_In_Place_Array_Op -- | |
11121 | ---------------------------- | |
11122 | ||
11123 | function Safe_In_Place_Array_Op | |
2e071734 AC |
11124 | (Lhs : Node_Id; |
11125 | Op1 : Node_Id; | |
11126 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
11127 | is |
11128 | Target : Entity_Id; | |
11129 | ||
11130 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
11131 | -- Operand is safe if it cannot overlap part of the target of the | |
11132 | -- operation. If the operand and the target are identical, the operand | |
11133 | -- is safe. The operand can be empty in the case of negation. | |
11134 | ||
11135 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 11136 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
11137 | |
11138 | ------------------ | |
11139 | -- Is_Unaliased -- | |
11140 | ------------------ | |
11141 | ||
11142 | function Is_Unaliased (N : Node_Id) return Boolean is | |
11143 | begin | |
11144 | return | |
11145 | Is_Entity_Name (N) | |
11146 | and then No (Address_Clause (Entity (N))) | |
11147 | and then No (Renamed_Object (Entity (N))); | |
11148 | end Is_Unaliased; | |
11149 | ||
11150 | --------------------- | |
11151 | -- Is_Safe_Operand -- | |
11152 | --------------------- | |
11153 | ||
11154 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
11155 | begin | |
11156 | if No (Op) then | |
11157 | return True; | |
11158 | ||
11159 | elsif Is_Entity_Name (Op) then | |
11160 | return Is_Unaliased (Op); | |
11161 | ||
303b4d58 | 11162 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
11163 | return Is_Unaliased (Prefix (Op)); |
11164 | ||
11165 | elsif Nkind (Op) = N_Slice then | |
11166 | return | |
11167 | Is_Unaliased (Prefix (Op)) | |
11168 | and then Entity (Prefix (Op)) /= Target; | |
11169 | ||
11170 | elsif Nkind (Op) = N_Op_Not then | |
11171 | return Is_Safe_Operand (Right_Opnd (Op)); | |
11172 | ||
11173 | else | |
11174 | return False; | |
11175 | end if; | |
11176 | end Is_Safe_Operand; | |
11177 | ||
e7e4d230 | 11178 | -- Start of processing for Is_Safe_In_Place_Array_Op |
fbf5a39b AC |
11179 | |
11180 | begin | |
685094bf RD |
11181 | -- Skip this processing if the component size is different from system |
11182 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b | 11183 | |
eaa826f8 | 11184 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then |
fbf5a39b AC |
11185 | return False; |
11186 | ||
26bff3d9 | 11187 | -- Cannot do in place stuff on VM_Target since cannot pass addresses |
fbf5a39b | 11188 | |
26bff3d9 | 11189 | elsif VM_Target /= No_VM then |
fbf5a39b AC |
11190 | return False; |
11191 | ||
11192 | -- Cannot do in place stuff if non-standard Boolean representation | |
11193 | ||
eaa826f8 | 11194 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then |
fbf5a39b AC |
11195 | return False; |
11196 | ||
11197 | elsif not Is_Unaliased (Lhs) then | |
11198 | return False; | |
e7e4d230 | 11199 | |
fbf5a39b AC |
11200 | else |
11201 | Target := Entity (Lhs); | |
e7e4d230 | 11202 | return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2); |
fbf5a39b AC |
11203 | end if; |
11204 | end Safe_In_Place_Array_Op; | |
11205 | ||
70482933 RK |
11206 | ----------------------- |
11207 | -- Tagged_Membership -- | |
11208 | ----------------------- | |
11209 | ||
685094bf RD |
11210 | -- There are two different cases to consider depending on whether the right |
11211 | -- operand is a class-wide type or not. If not we just compare the actual | |
11212 | -- tag of the left expr to the target type tag: | |
70482933 RK |
11213 | -- |
11214 | -- Left_Expr.Tag = Right_Type'Tag; | |
11215 | -- | |
685094bf RD |
11216 | -- If it is a class-wide type we use the RT function CW_Membership which is |
11217 | -- usually implemented by looking in the ancestor tables contained in the | |
11218 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 11219 | |
0669bebe GB |
11220 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
11221 | -- function IW_Membership which is usually implemented by looking in the | |
11222 | -- table of abstract interface types plus the ancestor table contained in | |
11223 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
11224 | ||
82878151 AC |
11225 | procedure Tagged_Membership |
11226 | (N : Node_Id; | |
11227 | SCIL_Node : out Node_Id; | |
11228 | Result : out Node_Id) | |
11229 | is | |
70482933 RK |
11230 | Left : constant Node_Id := Left_Opnd (N); |
11231 | Right : constant Node_Id := Right_Opnd (N); | |
11232 | Loc : constant Source_Ptr := Sloc (N); | |
11233 | ||
38171f43 | 11234 | Full_R_Typ : Entity_Id; |
70482933 | 11235 | Left_Type : Entity_Id; |
82878151 | 11236 | New_Node : Node_Id; |
70482933 RK |
11237 | Right_Type : Entity_Id; |
11238 | Obj_Tag : Node_Id; | |
11239 | ||
11240 | begin | |
82878151 AC |
11241 | SCIL_Node := Empty; |
11242 | ||
852dba80 AC |
11243 | -- Handle entities from the limited view |
11244 | ||
11245 | Left_Type := Available_View (Etype (Left)); | |
11246 | Right_Type := Available_View (Etype (Right)); | |
70482933 | 11247 | |
6cce2156 GD |
11248 | -- In the case where the type is an access type, the test is applied |
11249 | -- using the designated types (needed in Ada 2012 for implicit anonymous | |
11250 | -- access conversions, for AI05-0149). | |
11251 | ||
11252 | if Is_Access_Type (Right_Type) then | |
11253 | Left_Type := Designated_Type (Left_Type); | |
11254 | Right_Type := Designated_Type (Right_Type); | |
11255 | end if; | |
11256 | ||
70482933 RK |
11257 | if Is_Class_Wide_Type (Left_Type) then |
11258 | Left_Type := Root_Type (Left_Type); | |
11259 | end if; | |
11260 | ||
38171f43 AC |
11261 | if Is_Class_Wide_Type (Right_Type) then |
11262 | Full_R_Typ := Underlying_Type (Root_Type (Right_Type)); | |
11263 | else | |
11264 | Full_R_Typ := Underlying_Type (Right_Type); | |
11265 | end if; | |
11266 | ||
70482933 RK |
11267 | Obj_Tag := |
11268 | Make_Selected_Component (Loc, | |
11269 | Prefix => Relocate_Node (Left), | |
a9d8907c JM |
11270 | Selector_Name => |
11271 | New_Reference_To (First_Tag_Component (Left_Type), Loc)); | |
70482933 RK |
11272 | |
11273 | if Is_Class_Wide_Type (Right_Type) then | |
758c442c | 11274 | |
0669bebe GB |
11275 | -- No need to issue a run-time check if we statically know that the |
11276 | -- result of this membership test is always true. For example, | |
11277 | -- considering the following declarations: | |
11278 | ||
11279 | -- type Iface is interface; | |
11280 | -- type T is tagged null record; | |
11281 | -- type DT is new T and Iface with null record; | |
11282 | ||
11283 | -- Obj1 : T; | |
11284 | -- Obj2 : DT; | |
11285 | ||
11286 | -- These membership tests are always true: | |
11287 | ||
11288 | -- Obj1 in T'Class | |
11289 | -- Obj2 in T'Class; | |
11290 | -- Obj2 in Iface'Class; | |
11291 | ||
11292 | -- We do not need to handle cases where the membership is illegal. | |
11293 | -- For example: | |
11294 | ||
11295 | -- Obj1 in DT'Class; -- Compile time error | |
11296 | -- Obj1 in Iface'Class; -- Compile time error | |
11297 | ||
11298 | if not Is_Class_Wide_Type (Left_Type) | |
4ac2477e JM |
11299 | and then (Is_Ancestor (Etype (Right_Type), Left_Type, |
11300 | Use_Full_View => True) | |
0669bebe GB |
11301 | or else (Is_Interface (Etype (Right_Type)) |
11302 | and then Interface_Present_In_Ancestor | |
11303 | (Typ => Left_Type, | |
11304 | Iface => Etype (Right_Type)))) | |
11305 | then | |
82878151 AC |
11306 | Result := New_Reference_To (Standard_True, Loc); |
11307 | return; | |
0669bebe GB |
11308 | end if; |
11309 | ||
758c442c GD |
11310 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
11311 | ||
630d30e9 RD |
11312 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
11313 | ||
0669bebe | 11314 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
11315 | |
11316 | or else Is_Interface (Left_Type) | |
11317 | then | |
dfd99a80 TQ |
11318 | -- Issue error if IW_Membership operation not available in a |
11319 | -- configurable run time setting. | |
11320 | ||
11321 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
11322 | Error_Msg_CRT |
11323 | ("dynamic membership test on interface types", N); | |
82878151 AC |
11324 | Result := Empty; |
11325 | return; | |
dfd99a80 TQ |
11326 | end if; |
11327 | ||
82878151 | 11328 | Result := |
758c442c GD |
11329 | Make_Function_Call (Loc, |
11330 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
11331 | Parameter_Associations => New_List ( | |
11332 | Make_Attribute_Reference (Loc, | |
11333 | Prefix => Obj_Tag, | |
11334 | Attribute_Name => Name_Address), | |
11335 | New_Reference_To ( | |
38171f43 | 11336 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), |
758c442c GD |
11337 | Loc))); |
11338 | ||
11339 | -- Ada 95: Normal case | |
11340 | ||
11341 | else | |
82878151 AC |
11342 | Build_CW_Membership (Loc, |
11343 | Obj_Tag_Node => Obj_Tag, | |
11344 | Typ_Tag_Node => | |
11345 | New_Reference_To ( | |
38171f43 | 11346 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc), |
82878151 AC |
11347 | Related_Nod => N, |
11348 | New_Node => New_Node); | |
11349 | ||
11350 | -- Generate the SCIL node for this class-wide membership test. | |
11351 | -- Done here because the previous call to Build_CW_Membership | |
11352 | -- relocates Obj_Tag. | |
11353 | ||
11354 | if Generate_SCIL then | |
11355 | SCIL_Node := Make_SCIL_Membership_Test (Sloc (N)); | |
11356 | Set_SCIL_Entity (SCIL_Node, Etype (Right_Type)); | |
11357 | Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag); | |
11358 | end if; | |
11359 | ||
11360 | Result := New_Node; | |
758c442c GD |
11361 | end if; |
11362 | ||
0669bebe GB |
11363 | -- Right_Type is not a class-wide type |
11364 | ||
70482933 | 11365 | else |
0669bebe GB |
11366 | -- No need to check the tag of the object if Right_Typ is abstract |
11367 | ||
11368 | if Is_Abstract_Type (Right_Type) then | |
82878151 | 11369 | Result := New_Reference_To (Standard_False, Loc); |
0669bebe GB |
11370 | |
11371 | else | |
82878151 | 11372 | Result := |
0669bebe GB |
11373 | Make_Op_Eq (Loc, |
11374 | Left_Opnd => Obj_Tag, | |
11375 | Right_Opnd => | |
11376 | New_Reference_To | |
38171f43 | 11377 | (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc)); |
0669bebe | 11378 | end if; |
70482933 | 11379 | end if; |
70482933 RK |
11380 | end Tagged_Membership; |
11381 | ||
11382 | ------------------------------ | |
11383 | -- Unary_Op_Validity_Checks -- | |
11384 | ------------------------------ | |
11385 | ||
11386 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
11387 | begin | |
11388 | if Validity_Checks_On and Validity_Check_Operands then | |
11389 | Ensure_Valid (Right_Opnd (N)); | |
11390 | end if; | |
11391 | end Unary_Op_Validity_Checks; | |
11392 | ||
11393 | end Exp_Ch4; |