]>
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; |
70482933 | 34 | with Exp_Ch3; use Exp_Ch3; |
20b5d666 | 35 | with Exp_Ch6; use Exp_Ch6; |
70482933 RK |
36 | with Exp_Ch7; use Exp_Ch7; |
37 | with Exp_Ch9; use Exp_Ch9; | |
20b5d666 | 38 | with Exp_Disp; use Exp_Disp; |
70482933 | 39 | with Exp_Fixd; use Exp_Fixd; |
437f8c1e | 40 | with Exp_Intr; use Exp_Intr; |
70482933 RK |
41 | with Exp_Pakd; use Exp_Pakd; |
42 | with Exp_Tss; use Exp_Tss; | |
43 | with Exp_Util; use Exp_Util; | |
44 | with Exp_VFpt; use Exp_VFpt; | |
f02b8bb8 | 45 | with Freeze; use Freeze; |
70482933 | 46 | with Inline; use Inline; |
26bff3d9 | 47 | with Namet; use Namet; |
70482933 RK |
48 | with Nlists; use Nlists; |
49 | with Nmake; use Nmake; | |
50 | with Opt; use Opt; | |
25adc5fb | 51 | with Par_SCO; use Par_SCO; |
0669bebe GB |
52 | with Restrict; use Restrict; |
53 | with Rident; use Rident; | |
70482933 RK |
54 | with Rtsfind; use Rtsfind; |
55 | with Sem; use Sem; | |
a4100e55 | 56 | with Sem_Aux; use Sem_Aux; |
70482933 | 57 | with Sem_Cat; use Sem_Cat; |
5d09245e | 58 | with Sem_Ch3; use Sem_Ch3; |
26bff3d9 | 59 | with Sem_Ch8; use Sem_Ch8; |
70482933 RK |
60 | with Sem_Ch13; use Sem_Ch13; |
61 | with Sem_Eval; use Sem_Eval; | |
62 | with Sem_Res; use Sem_Res; | |
63 | with Sem_Type; use Sem_Type; | |
64 | with Sem_Util; use Sem_Util; | |
07fc65c4 | 65 | with Sem_Warn; use Sem_Warn; |
70482933 | 66 | with Sinfo; use Sinfo; |
70482933 RK |
67 | with Snames; use Snames; |
68 | with Stand; use Stand; | |
7665e4bd | 69 | with SCIL_LL; use SCIL_LL; |
07fc65c4 | 70 | with Targparm; use Targparm; |
70482933 RK |
71 | with Tbuild; use Tbuild; |
72 | with Ttypes; use Ttypes; | |
73 | with Uintp; use Uintp; | |
74 | with Urealp; use Urealp; | |
75 | with Validsw; use Validsw; | |
76 | ||
77 | package body Exp_Ch4 is | |
78 | ||
15ce9ca2 AC |
79 | ----------------------- |
80 | -- Local Subprograms -- | |
81 | ----------------------- | |
70482933 RK |
82 | |
83 | procedure Binary_Op_Validity_Checks (N : Node_Id); | |
84 | pragma Inline (Binary_Op_Validity_Checks); | |
85 | -- Performs validity checks for a binary operator | |
86 | ||
fbf5a39b AC |
87 | procedure Build_Boolean_Array_Proc_Call |
88 | (N : Node_Id; | |
89 | Op1 : Node_Id; | |
90 | Op2 : Node_Id); | |
303b4d58 | 91 | -- If a boolean array assignment can be done in place, build call to |
fbf5a39b AC |
92 | -- corresponding library procedure. |
93 | ||
26bff3d9 JM |
94 | procedure Displace_Allocator_Pointer (N : Node_Id); |
95 | -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and | |
96 | -- Expand_Allocator_Expression. Allocating class-wide interface objects | |
97 | -- this routine displaces the pointer to the allocated object to reference | |
98 | -- the component referencing the corresponding secondary dispatch table. | |
99 | ||
fbf5a39b AC |
100 | procedure Expand_Allocator_Expression (N : Node_Id); |
101 | -- Subsidiary to Expand_N_Allocator, for the case when the expression | |
102 | -- is a qualified expression or an aggregate. | |
103 | ||
70482933 RK |
104 | procedure Expand_Array_Comparison (N : Node_Id); |
105 | -- This routine handles expansion of the comparison operators (N_Op_Lt, | |
106 | -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic | |
107 | -- code for these operators is similar, differing only in the details of | |
fbf5a39b AC |
108 | -- the actual comparison call that is made. Special processing (call a |
109 | -- run-time routine) | |
70482933 RK |
110 | |
111 | function Expand_Array_Equality | |
112 | (Nod : Node_Id; | |
70482933 RK |
113 | Lhs : Node_Id; |
114 | Rhs : Node_Id; | |
0da2c8ac AC |
115 | Bodies : List_Id; |
116 | Typ : Entity_Id) return Node_Id; | |
70482933 | 117 | -- Expand an array equality into a call to a function implementing this |
685094bf RD |
118 | -- equality, and a call to it. Loc is the location for the generated nodes. |
119 | -- Lhs and Rhs are the array expressions to be compared. Bodies is a list | |
120 | -- on which to attach bodies of local functions that are created in the | |
121 | -- process. It is the responsibility of the caller to insert those bodies | |
122 | -- at the right place. Nod provides the Sloc value for the generated code. | |
123 | -- Normally the types used for the generated equality routine are taken | |
124 | -- from Lhs and Rhs. However, in some situations of generated code, the | |
125 | -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies | |
126 | -- the type to be used for the formal parameters. | |
70482933 RK |
127 | |
128 | procedure Expand_Boolean_Operator (N : Node_Id); | |
685094bf RD |
129 | -- Common expansion processing for Boolean operators (And, Or, Xor) for the |
130 | -- case of array type arguments. | |
70482933 | 131 | |
5875f8d6 AC |
132 | procedure Expand_Short_Circuit_Operator (N : Node_Id); |
133 | -- Common expansion processing for short-circuit boolean operators | |
134 | ||
70482933 RK |
135 | function Expand_Composite_Equality |
136 | (Nod : Node_Id; | |
137 | Typ : Entity_Id; | |
138 | Lhs : Node_Id; | |
139 | Rhs : Node_Id; | |
2e071734 | 140 | Bodies : List_Id) return Node_Id; |
685094bf RD |
141 | -- Local recursive function used to expand equality for nested composite |
142 | -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which | |
143 | -- to attach bodies of local functions that are created in the process. | |
144 | -- This is the responsibility of the caller to insert those bodies at the | |
145 | -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs | |
146 | -- are the left and right sides for the comparison, and Typ is the type of | |
147 | -- the arrays to compare. | |
70482933 | 148 | |
fdac1f80 AC |
149 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id); |
150 | -- Routine to expand concatenation of a sequence of two or more operands | |
151 | -- (in the list Operands) and replace node Cnode with the result of the | |
152 | -- concatenation. The operands can be of any appropriate type, and can | |
153 | -- include both arrays and singleton elements. | |
70482933 RK |
154 | |
155 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); | |
685094bf RD |
156 | -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal |
157 | -- fixed. We do not have such a type at runtime, so the purpose of this | |
158 | -- routine is to find the real type by looking up the tree. We also | |
159 | -- determine if the operation must be rounded. | |
70482933 | 160 | |
fbf5a39b AC |
161 | function Get_Allocator_Final_List |
162 | (N : Node_Id; | |
163 | T : Entity_Id; | |
2e071734 | 164 | PtrT : Entity_Id) return Entity_Id; |
685094bf RD |
165 | -- If the designated type is controlled, build final_list expression for |
166 | -- created object. If context is an access parameter, create a local access | |
167 | -- type to have a usable finalization list. | |
fbf5a39b | 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 | ||
26bff3d9 JM |
378 | -------------------------------- |
379 | -- Displace_Allocator_Pointer -- | |
380 | -------------------------------- | |
381 | ||
382 | procedure Displace_Allocator_Pointer (N : Node_Id) is | |
383 | Loc : constant Source_Ptr := Sloc (N); | |
384 | Orig_Node : constant Node_Id := Original_Node (N); | |
385 | Dtyp : Entity_Id; | |
386 | Etyp : Entity_Id; | |
387 | PtrT : Entity_Id; | |
388 | ||
389 | begin | |
303b4d58 AC |
390 | -- Do nothing in case of VM targets: the virtual machine will handle |
391 | -- interfaces directly. | |
392 | ||
1f110335 | 393 | if not Tagged_Type_Expansion then |
303b4d58 AC |
394 | return; |
395 | end if; | |
396 | ||
26bff3d9 JM |
397 | pragma Assert (Nkind (N) = N_Identifier |
398 | and then Nkind (Orig_Node) = N_Allocator); | |
399 | ||
400 | PtrT := Etype (Orig_Node); | |
d6a24cdb | 401 | Dtyp := Available_View (Designated_Type (PtrT)); |
26bff3d9 JM |
402 | Etyp := Etype (Expression (Orig_Node)); |
403 | ||
404 | if Is_Class_Wide_Type (Dtyp) | |
405 | and then Is_Interface (Dtyp) | |
406 | then | |
407 | -- If the type of the allocator expression is not an interface type | |
408 | -- we can generate code to reference the record component containing | |
409 | -- the pointer to the secondary dispatch table. | |
410 | ||
411 | if not Is_Interface (Etyp) then | |
412 | declare | |
413 | Saved_Typ : constant Entity_Id := Etype (Orig_Node); | |
414 | ||
415 | begin | |
416 | -- 1) Get access to the allocated object | |
417 | ||
418 | Rewrite (N, | |
419 | Make_Explicit_Dereference (Loc, | |
420 | Relocate_Node (N))); | |
421 | Set_Etype (N, Etyp); | |
422 | Set_Analyzed (N); | |
423 | ||
424 | -- 2) Add the conversion to displace the pointer to reference | |
425 | -- the secondary dispatch table. | |
426 | ||
427 | Rewrite (N, Convert_To (Dtyp, Relocate_Node (N))); | |
428 | Analyze_And_Resolve (N, Dtyp); | |
429 | ||
430 | -- 3) The 'access to the secondary dispatch table will be used | |
431 | -- as the value returned by the allocator. | |
432 | ||
433 | Rewrite (N, | |
434 | Make_Attribute_Reference (Loc, | |
435 | Prefix => Relocate_Node (N), | |
436 | Attribute_Name => Name_Access)); | |
437 | Set_Etype (N, Saved_Typ); | |
438 | Set_Analyzed (N); | |
439 | end; | |
440 | ||
441 | -- If the type of the allocator expression is an interface type we | |
442 | -- generate a run-time call to displace "this" to reference the | |
443 | -- component containing the pointer to the secondary dispatch table | |
444 | -- or else raise Constraint_Error if the actual object does not | |
445 | -- implement the target interface. This case corresponds with the | |
446 | -- following example: | |
447 | ||
8fc789c8 | 448 | -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is |
26bff3d9 JM |
449 | -- begin |
450 | -- return new Iface_2'Class'(Obj); | |
451 | -- end Op; | |
452 | ||
453 | else | |
454 | Rewrite (N, | |
455 | Unchecked_Convert_To (PtrT, | |
456 | Make_Function_Call (Loc, | |
457 | Name => New_Reference_To (RTE (RE_Displace), Loc), | |
458 | Parameter_Associations => New_List ( | |
459 | Unchecked_Convert_To (RTE (RE_Address), | |
460 | Relocate_Node (N)), | |
461 | ||
462 | New_Occurrence_Of | |
463 | (Elists.Node | |
464 | (First_Elmt | |
465 | (Access_Disp_Table (Etype (Base_Type (Dtyp))))), | |
466 | Loc))))); | |
467 | Analyze_And_Resolve (N, PtrT); | |
468 | end if; | |
469 | end if; | |
470 | end Displace_Allocator_Pointer; | |
471 | ||
fbf5a39b AC |
472 | --------------------------------- |
473 | -- Expand_Allocator_Expression -- | |
474 | --------------------------------- | |
475 | ||
476 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
f02b8bb8 RD |
477 | Loc : constant Source_Ptr := Sloc (N); |
478 | Exp : constant Node_Id := Expression (Expression (N)); | |
f02b8bb8 RD |
479 | PtrT : constant Entity_Id := Etype (N); |
480 | DesigT : constant Entity_Id := Designated_Type (PtrT); | |
26bff3d9 JM |
481 | |
482 | procedure Apply_Accessibility_Check | |
483 | (Ref : Node_Id; | |
484 | Built_In_Place : Boolean := False); | |
485 | -- Ada 2005 (AI-344): For an allocator with a class-wide designated | |
685094bf RD |
486 | -- type, generate an accessibility check to verify that the level of the |
487 | -- type of the created object is not deeper than the level of the access | |
488 | -- type. If the type of the qualified expression is class- wide, then | |
489 | -- always generate the check (except in the case where it is known to be | |
490 | -- unnecessary, see comment below). Otherwise, only generate the check | |
491 | -- if the level of the qualified expression type is statically deeper | |
492 | -- than the access type. | |
493 | -- | |
494 | -- Although the static accessibility will generally have been performed | |
495 | -- as a legality check, it won't have been done in cases where the | |
496 | -- allocator appears in generic body, so a run-time check is needed in | |
497 | -- general. One special case is when the access type is declared in the | |
498 | -- same scope as the class-wide allocator, in which case the check can | |
499 | -- never fail, so it need not be generated. | |
500 | -- | |
501 | -- As an open issue, there seem to be cases where the static level | |
502 | -- associated with the class-wide object's underlying type is not | |
503 | -- sufficient to perform the proper accessibility check, such as for | |
504 | -- allocators in nested subprograms or accept statements initialized by | |
505 | -- class-wide formals when the actual originates outside at a deeper | |
506 | -- static level. The nested subprogram case might require passing | |
507 | -- accessibility levels along with class-wide parameters, and the task | |
508 | -- case seems to be an actual gap in the language rules that needs to | |
509 | -- be fixed by the ARG. ??? | |
26bff3d9 JM |
510 | |
511 | ------------------------------- | |
512 | -- Apply_Accessibility_Check -- | |
513 | ------------------------------- | |
514 | ||
515 | procedure Apply_Accessibility_Check | |
516 | (Ref : Node_Id; | |
517 | Built_In_Place : Boolean := False) | |
518 | is | |
519 | Ref_Node : Node_Id; | |
520 | ||
521 | begin | |
522 | -- Note: we skip the accessibility check for the VM case, since | |
523 | -- there does not seem to be any practical way of implementing it. | |
524 | ||
0791fbe9 | 525 | if Ada_Version >= Ada_2005 |
1f110335 | 526 | and then Tagged_Type_Expansion |
26bff3d9 JM |
527 | and then Is_Class_Wide_Type (DesigT) |
528 | and then not Scope_Suppress (Accessibility_Check) | |
529 | and then | |
530 | (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT) | |
531 | or else | |
532 | (Is_Class_Wide_Type (Etype (Exp)) | |
533 | and then Scope (PtrT) /= Current_Scope)) | |
534 | then | |
535 | -- If the allocator was built in place Ref is already a reference | |
536 | -- to the access object initialized to the result of the allocator | |
537 | -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise | |
538 | -- it is the entity associated with the object containing the | |
539 | -- address of the allocated object. | |
540 | ||
541 | if Built_In_Place then | |
542 | Ref_Node := New_Copy (Ref); | |
543 | else | |
544 | Ref_Node := New_Reference_To (Ref, Loc); | |
545 | end if; | |
546 | ||
547 | Insert_Action (N, | |
548 | Make_Raise_Program_Error (Loc, | |
549 | Condition => | |
550 | Make_Op_Gt (Loc, | |
551 | Left_Opnd => | |
552 | Build_Get_Access_Level (Loc, | |
553 | Make_Attribute_Reference (Loc, | |
554 | Prefix => Ref_Node, | |
555 | Attribute_Name => Name_Tag)), | |
556 | Right_Opnd => | |
557 | Make_Integer_Literal (Loc, | |
558 | Type_Access_Level (PtrT))), | |
559 | Reason => PE_Accessibility_Check_Failed)); | |
560 | end if; | |
561 | end Apply_Accessibility_Check; | |
562 | ||
563 | -- Local variables | |
564 | ||
565 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
566 | T : constant Entity_Id := Entity (Indic); | |
567 | Flist : Node_Id; | |
568 | Node : Node_Id; | |
569 | Temp : Entity_Id; | |
fbf5a39b | 570 | |
d26dc4b5 AC |
571 | TagT : Entity_Id := Empty; |
572 | -- Type used as source for tag assignment | |
573 | ||
574 | TagR : Node_Id := Empty; | |
575 | -- Target reference for tag assignment | |
576 | ||
fbf5a39b AC |
577 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); |
578 | ||
579 | Tag_Assign : Node_Id; | |
580 | Tmp_Node : Node_Id; | |
581 | ||
26bff3d9 JM |
582 | -- Start of processing for Expand_Allocator_Expression |
583 | ||
fbf5a39b | 584 | begin |
048e5cef | 585 | if Is_Tagged_Type (T) or else Needs_Finalization (T) then |
fbf5a39b | 586 | |
fadcf313 AC |
587 | if Is_CPP_Constructor_Call (Exp) then |
588 | ||
589 | -- Generate: | |
590 | -- Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn | |
591 | ||
592 | -- Allocate the object with no expression | |
593 | ||
594 | Node := Relocate_Node (N); | |
7b4db06c | 595 | Set_Expression (Node, New_Reference_To (Etype (Exp), Loc)); |
fadcf313 AC |
596 | |
597 | -- Avoid its expansion to avoid generating a call to the default | |
598 | -- C++ constructor | |
599 | ||
600 | Set_Analyzed (Node); | |
601 | ||
e86a3a7e | 602 | Temp := Make_Temporary (Loc, 'P', N); |
fadcf313 AC |
603 | |
604 | Insert_Action (N, | |
605 | Make_Object_Declaration (Loc, | |
606 | Defining_Identifier => Temp, | |
607 | Constant_Present => True, | |
608 | Object_Definition => New_Reference_To (PtrT, Loc), | |
609 | Expression => Node)); | |
610 | ||
611 | Apply_Accessibility_Check (Temp); | |
612 | ||
ffa5876f | 613 | -- Locate the enclosing list and insert the C++ constructor call |
fadcf313 AC |
614 | |
615 | declare | |
ffa5876f | 616 | P : Node_Id; |
fadcf313 AC |
617 | |
618 | begin | |
ffa5876f | 619 | P := Parent (Node); |
fadcf313 AC |
620 | while not Is_List_Member (P) loop |
621 | P := Parent (P); | |
622 | end loop; | |
623 | ||
624 | Insert_List_After_And_Analyze (P, | |
625 | Build_Initialization_Call (Loc, | |
ffa5876f AC |
626 | Id_Ref => |
627 | Make_Explicit_Dereference (Loc, | |
628 | Prefix => New_Reference_To (Temp, Loc)), | |
7b4db06c | 629 | Typ => Etype (Exp), |
fadcf313 AC |
630 | Constructor_Ref => Exp)); |
631 | end; | |
632 | ||
633 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
634 | Analyze_And_Resolve (N, PtrT); | |
fadcf313 AC |
635 | return; |
636 | end if; | |
637 | ||
685094bf RD |
638 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
639 | -- to a build-in-place function, then access to the allocated object | |
640 | -- must be passed to the function. Currently we limit such functions | |
641 | -- to those with constrained limited result subtypes, but eventually | |
642 | -- we plan to expand the allowed forms of functions that are treated | |
643 | -- as build-in-place. | |
20b5d666 | 644 | |
0791fbe9 | 645 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
646 | and then Is_Build_In_Place_Function_Call (Exp) |
647 | then | |
648 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
26bff3d9 JM |
649 | Apply_Accessibility_Check (N, Built_In_Place => True); |
650 | return; | |
20b5d666 JM |
651 | end if; |
652 | ||
fbf5a39b AC |
653 | -- Actions inserted before: |
654 | -- Temp : constant ptr_T := new T'(Expression); | |
655 | -- <no CW> Temp._tag := T'tag; | |
656 | -- <CTRL> Adjust (Finalizable (Temp.all)); | |
657 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
658 | ||
659 | -- We analyze by hand the new internal allocator to avoid | |
660 | -- any recursion and inappropriate call to Initialize | |
7324bf49 | 661 | |
20b5d666 JM |
662 | -- We don't want to remove side effects when the expression must be |
663 | -- built in place. In the case of a build-in-place function call, | |
664 | -- that could lead to a duplication of the call, which was already | |
665 | -- substituted for the allocator. | |
666 | ||
26bff3d9 | 667 | if not Aggr_In_Place then |
fbf5a39b AC |
668 | Remove_Side_Effects (Exp); |
669 | end if; | |
670 | ||
e86a3a7e | 671 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
672 | |
673 | -- For a class wide allocation generate the following code: | |
674 | ||
675 | -- type Equiv_Record is record ... end record; | |
676 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
677 | -- temp : PtrT := new CW'(CW!(expr)); | |
678 | ||
679 | if Is_Class_Wide_Type (T) then | |
680 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
681 | ||
26bff3d9 JM |
682 | -- Ada 2005 (AI-251): If the expression is a class-wide interface |
683 | -- object we generate code to move up "this" to reference the | |
684 | -- base of the object before allocating the new object. | |
685 | ||
686 | -- Note that Exp'Address is recursively expanded into a call | |
687 | -- to Base_Address (Exp.Tag) | |
688 | ||
689 | if Is_Class_Wide_Type (Etype (Exp)) | |
690 | and then Is_Interface (Etype (Exp)) | |
1f110335 | 691 | and then Tagged_Type_Expansion |
26bff3d9 JM |
692 | then |
693 | Set_Expression | |
694 | (Expression (N), | |
695 | Unchecked_Convert_To (Entity (Indic), | |
696 | Make_Explicit_Dereference (Loc, | |
697 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
698 | Make_Attribute_Reference (Loc, | |
699 | Prefix => Exp, | |
700 | Attribute_Name => Name_Address))))); | |
701 | ||
702 | else | |
703 | Set_Expression | |
704 | (Expression (N), | |
705 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
706 | end if; | |
fbf5a39b AC |
707 | |
708 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
709 | end if; | |
710 | ||
26bff3d9 | 711 | -- Keep separate the management of allocators returning interfaces |
fbf5a39b | 712 | |
26bff3d9 JM |
713 | if not Is_Interface (Directly_Designated_Type (PtrT)) then |
714 | if Aggr_In_Place then | |
715 | Tmp_Node := | |
716 | Make_Object_Declaration (Loc, | |
717 | Defining_Identifier => Temp, | |
718 | Object_Definition => New_Reference_To (PtrT, Loc), | |
719 | Expression => | |
720 | Make_Allocator (Loc, | |
721 | New_Reference_To (Etype (Exp), Loc))); | |
fbf5a39b | 722 | |
fad0600d AC |
723 | -- Copy the Comes_From_Source flag for the allocator we just |
724 | -- built, since logically this allocator is a replacement of | |
725 | -- the original allocator node. This is for proper handling of | |
726 | -- restriction No_Implicit_Heap_Allocations. | |
727 | ||
26bff3d9 JM |
728 | Set_Comes_From_Source |
729 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
fbf5a39b | 730 | |
26bff3d9 JM |
731 | Set_No_Initialization (Expression (Tmp_Node)); |
732 | Insert_Action (N, Tmp_Node); | |
fbf5a39b | 733 | |
048e5cef | 734 | if Needs_Finalization (T) |
26bff3d9 JM |
735 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
736 | then | |
737 | -- Create local finalization list for access parameter | |
738 | ||
739 | Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
740 | end if; | |
741 | ||
d766cee3 | 742 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
fad0600d | 743 | |
26bff3d9 JM |
744 | else |
745 | Node := Relocate_Node (N); | |
746 | Set_Analyzed (Node); | |
747 | Insert_Action (N, | |
748 | Make_Object_Declaration (Loc, | |
749 | Defining_Identifier => Temp, | |
750 | Constant_Present => True, | |
751 | Object_Definition => New_Reference_To (PtrT, Loc), | |
752 | Expression => Node)); | |
fbf5a39b AC |
753 | end if; |
754 | ||
26bff3d9 JM |
755 | -- Ada 2005 (AI-251): Handle allocators whose designated type is an |
756 | -- interface type. In this case we use the type of the qualified | |
757 | -- expression to allocate the object. | |
758 | ||
fbf5a39b | 759 | else |
26bff3d9 | 760 | declare |
191fcb3a | 761 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); |
26bff3d9 | 762 | New_Decl : Node_Id; |
fbf5a39b | 763 | |
26bff3d9 JM |
764 | begin |
765 | New_Decl := | |
766 | Make_Full_Type_Declaration (Loc, | |
767 | Defining_Identifier => Def_Id, | |
768 | Type_Definition => | |
769 | Make_Access_To_Object_Definition (Loc, | |
770 | All_Present => True, | |
771 | Null_Exclusion_Present => False, | |
772 | Constant_Present => False, | |
773 | Subtype_Indication => | |
774 | New_Reference_To (Etype (Exp), Loc))); | |
775 | ||
776 | Insert_Action (N, New_Decl); | |
777 | ||
778 | -- Inherit the final chain to ensure that the expansion of the | |
779 | -- aggregate is correct in case of controlled types | |
780 | ||
048e5cef | 781 | if Needs_Finalization (Directly_Designated_Type (PtrT)) then |
26bff3d9 JM |
782 | Set_Associated_Final_Chain (Def_Id, |
783 | Associated_Final_Chain (PtrT)); | |
784 | end if; | |
758c442c | 785 | |
26bff3d9 JM |
786 | -- Declare the object using the previous type declaration |
787 | ||
788 | if Aggr_In_Place then | |
789 | Tmp_Node := | |
790 | Make_Object_Declaration (Loc, | |
791 | Defining_Identifier => Temp, | |
792 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
793 | Expression => | |
794 | Make_Allocator (Loc, | |
795 | New_Reference_To (Etype (Exp), Loc))); | |
796 | ||
fad0600d AC |
797 | -- Copy the Comes_From_Source flag for the allocator we just |
798 | -- built, since logically this allocator is a replacement of | |
799 | -- the original allocator node. This is for proper handling | |
800 | -- of restriction No_Implicit_Heap_Allocations. | |
801 | ||
26bff3d9 JM |
802 | Set_Comes_From_Source |
803 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
804 | ||
805 | Set_No_Initialization (Expression (Tmp_Node)); | |
806 | Insert_Action (N, Tmp_Node); | |
807 | ||
048e5cef | 808 | if Needs_Finalization (T) |
26bff3d9 JM |
809 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
810 | then | |
811 | -- Create local finalization list for access parameter | |
812 | ||
813 | Flist := | |
814 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
815 | end if; | |
816 | ||
d766cee3 | 817 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
26bff3d9 JM |
818 | else |
819 | Node := Relocate_Node (N); | |
820 | Set_Analyzed (Node); | |
821 | Insert_Action (N, | |
822 | Make_Object_Declaration (Loc, | |
823 | Defining_Identifier => Temp, | |
824 | Constant_Present => True, | |
825 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
826 | Expression => Node)); | |
827 | end if; | |
828 | ||
829 | -- Generate an additional object containing the address of the | |
830 | -- returned object. The type of this second object declaration | |
685094bf RD |
831 | -- is the correct type required for the common processing that |
832 | -- is still performed by this subprogram. The displacement of | |
833 | -- this pointer to reference the component associated with the | |
834 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
835 | |
836 | New_Decl := | |
837 | Make_Object_Declaration (Loc, | |
191fcb3a | 838 | Defining_Identifier => Make_Temporary (Loc, 'P'), |
26bff3d9 JM |
839 | Object_Definition => New_Reference_To (PtrT, Loc), |
840 | Expression => Unchecked_Convert_To (PtrT, | |
841 | New_Reference_To (Temp, Loc))); | |
842 | ||
843 | Insert_Action (N, New_Decl); | |
844 | ||
845 | Tmp_Node := New_Decl; | |
846 | Temp := Defining_Identifier (New_Decl); | |
847 | end; | |
758c442c GD |
848 | end if; |
849 | ||
26bff3d9 JM |
850 | Apply_Accessibility_Check (Temp); |
851 | ||
852 | -- Generate the tag assignment | |
853 | ||
854 | -- Suppress the tag assignment when VM_Target because VM tags are | |
855 | -- represented implicitly in objects. | |
856 | ||
1f110335 | 857 | if not Tagged_Type_Expansion then |
26bff3d9 | 858 | null; |
fbf5a39b | 859 | |
26bff3d9 JM |
860 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
861 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 862 | |
26bff3d9 JM |
863 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
864 | pragma Assert (Is_Class_Wide_Type | |
865 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
866 | null; |
867 | ||
868 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
869 | TagT := T; | |
870 | TagR := New_Reference_To (Temp, Loc); | |
871 | ||
872 | elsif Is_Private_Type (T) | |
873 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 874 | then |
d26dc4b5 | 875 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
876 | TagR := |
877 | Unchecked_Convert_To (Underlying_Type (T), | |
878 | Make_Explicit_Dereference (Loc, | |
879 | Prefix => New_Reference_To (Temp, Loc))); | |
d26dc4b5 AC |
880 | end if; |
881 | ||
882 | if Present (TagT) then | |
38171f43 AC |
883 | declare |
884 | Full_T : constant Entity_Id := Underlying_Type (TagT); | |
38171f43 AC |
885 | begin |
886 | Tag_Assign := | |
887 | Make_Assignment_Statement (Loc, | |
888 | Name => | |
889 | Make_Selected_Component (Loc, | |
890 | Prefix => TagR, | |
891 | Selector_Name => | |
892 | New_Reference_To (First_Tag_Component (Full_T), Loc)), | |
893 | Expression => | |
894 | Unchecked_Convert_To (RTE (RE_Tag), | |
895 | New_Reference_To | |
896 | (Elists.Node | |
897 | (First_Elmt (Access_Disp_Table (Full_T))), Loc))); | |
898 | end; | |
fbf5a39b AC |
899 | |
900 | -- The previous assignment has to be done in any case | |
901 | ||
902 | Set_Assignment_OK (Name (Tag_Assign)); | |
903 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
904 | end if; |
905 | ||
048e5cef BD |
906 | if Needs_Finalization (DesigT) |
907 | and then Needs_Finalization (T) | |
fbf5a39b AC |
908 | then |
909 | declare | |
910 | Attach : Node_Id; | |
911 | Apool : constant Entity_Id := | |
912 | Associated_Storage_Pool (PtrT); | |
913 | ||
914 | begin | |
685094bf RD |
915 | -- If it is an allocation on the secondary stack (i.e. a value |
916 | -- returned from a function), the object is attached on the | |
917 | -- caller side as soon as the call is completed (see | |
918 | -- Expand_Ctrl_Function_Call) | |
fbf5a39b AC |
919 | |
920 | if Is_RTE (Apool, RE_SS_Pool) then | |
921 | declare | |
191fcb3a | 922 | F : constant Entity_Id := Make_Temporary (Loc, 'F'); |
fbf5a39b AC |
923 | begin |
924 | Insert_Action (N, | |
925 | Make_Object_Declaration (Loc, | |
926 | Defining_Identifier => F, | |
191fcb3a RD |
927 | Object_Definition => |
928 | New_Reference_To (RTE (RE_Finalizable_Ptr), Loc))); | |
fbf5a39b AC |
929 | Flist := New_Reference_To (F, Loc); |
930 | Attach := Make_Integer_Literal (Loc, 1); | |
931 | end; | |
932 | ||
933 | -- Normal case, not a secondary stack allocation | |
934 | ||
935 | else | |
048e5cef | 936 | if Needs_Finalization (T) |
615cbd95 AC |
937 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
938 | then | |
5e1c00fa | 939 | -- Create local finalization list for access parameter |
615cbd95 AC |
940 | |
941 | Flist := | |
942 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
943 | else | |
944 | Flist := Find_Final_List (PtrT); | |
945 | end if; | |
946 | ||
fbf5a39b AC |
947 | Attach := Make_Integer_Literal (Loc, 2); |
948 | end if; | |
949 | ||
26bff3d9 JM |
950 | -- Generate an Adjust call if the object will be moved. In Ada |
951 | -- 2005, the object may be inherently limited, in which case | |
952 | -- there is no Adjust procedure, and the object is built in | |
953 | -- place. In Ada 95, the object can be limited but not | |
954 | -- inherently limited if this allocator came from a return | |
955 | -- statement (we're allocating the result on the secondary | |
956 | -- stack). In that case, the object will be moved, so we _do_ | |
957 | -- want to Adjust. | |
958 | ||
959 | if not Aggr_In_Place | |
40f07b4b | 960 | and then not Is_Immutably_Limited_Type (T) |
26bff3d9 | 961 | then |
fbf5a39b AC |
962 | Insert_Actions (N, |
963 | Make_Adjust_Call ( | |
964 | Ref => | |
965 | ||
685094bf RD |
966 | -- An unchecked conversion is needed in the classwide |
967 | -- case because the designated type can be an ancestor of | |
968 | -- the subtype mark of the allocator. | |
fbf5a39b AC |
969 | |
970 | Unchecked_Convert_To (T, | |
971 | Make_Explicit_Dereference (Loc, | |
dfd99a80 | 972 | Prefix => New_Reference_To (Temp, Loc))), |
fbf5a39b AC |
973 | |
974 | Typ => T, | |
975 | Flist_Ref => Flist, | |
dfd99a80 TQ |
976 | With_Attach => Attach, |
977 | Allocator => True)); | |
fbf5a39b AC |
978 | end if; |
979 | end; | |
980 | end if; | |
981 | ||
982 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
983 | Analyze_And_Resolve (N, PtrT); | |
984 | ||
685094bf RD |
985 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
986 | -- component containing the secondary dispatch table of the interface | |
987 | -- type. | |
26bff3d9 JM |
988 | |
989 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
990 | Displace_Allocator_Pointer (N); | |
991 | end if; | |
992 | ||
fbf5a39b | 993 | elsif Aggr_In_Place then |
e86a3a7e | 994 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
995 | Tmp_Node := |
996 | Make_Object_Declaration (Loc, | |
997 | Defining_Identifier => Temp, | |
998 | Object_Definition => New_Reference_To (PtrT, Loc), | |
999 | Expression => Make_Allocator (Loc, | |
1000 | New_Reference_To (Etype (Exp), Loc))); | |
1001 | ||
fad0600d AC |
1002 | -- Copy the Comes_From_Source flag for the allocator we just built, |
1003 | -- since logically this allocator is a replacement of the original | |
1004 | -- allocator node. This is for proper handling of restriction | |
1005 | -- No_Implicit_Heap_Allocations. | |
1006 | ||
fbf5a39b AC |
1007 | Set_Comes_From_Source |
1008 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
1009 | ||
1010 | Set_No_Initialization (Expression (Tmp_Node)); | |
1011 | Insert_Action (N, Tmp_Node); | |
d766cee3 | 1012 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
fbf5a39b AC |
1013 | Rewrite (N, New_Reference_To (Temp, Loc)); |
1014 | Analyze_And_Resolve (N, PtrT); | |
1015 | ||
51e4c4b9 AC |
1016 | elsif Is_Access_Type (T) |
1017 | and then Can_Never_Be_Null (T) | |
1018 | then | |
1019 | Install_Null_Excluding_Check (Exp); | |
1020 | ||
f02b8bb8 | 1021 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
1022 | and then Nkind (Exp) = N_Allocator |
1023 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
1024 | then | |
0da2c8ac | 1025 | -- Apply constraint to designated subtype indication |
fbf5a39b AC |
1026 | |
1027 | Apply_Constraint_Check (Expression (Exp), | |
f02b8bb8 | 1028 | Designated_Type (DesigT), |
fbf5a39b AC |
1029 | No_Sliding => True); |
1030 | ||
1031 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
1032 | ||
1033 | -- Propagate constraint_error to enclosing allocator | |
1034 | ||
1035 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
1036 | end if; | |
1037 | else | |
36c73552 AC |
1038 | -- If we have: |
1039 | -- type A is access T1; | |
1040 | -- X : A := new T2'(...); | |
1041 | -- T1 and T2 can be different subtypes, and we might need to check | |
1042 | -- both constraints. First check against the type of the qualified | |
1043 | -- expression. | |
1044 | ||
1045 | Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
fbf5a39b | 1046 | |
d79e621a GD |
1047 | if Do_Range_Check (Exp) then |
1048 | Set_Do_Range_Check (Exp, False); | |
1049 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); | |
1050 | end if; | |
1051 | ||
685094bf RD |
1052 | -- A check is also needed in cases where the designated subtype is |
1053 | -- constrained and differs from the subtype given in the qualified | |
1054 | -- expression. Note that the check on the qualified expression does | |
1055 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 1056 | |
f02b8bb8 | 1057 | if Is_Constrained (DesigT) |
9450205a | 1058 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
1059 | then |
1060 | Apply_Constraint_Check | |
f02b8bb8 | 1061 | (Exp, DesigT, No_Sliding => False); |
d79e621a GD |
1062 | |
1063 | if Do_Range_Check (Exp) then | |
1064 | Set_Do_Range_Check (Exp, False); | |
1065 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); | |
1066 | end if; | |
f02b8bb8 RD |
1067 | end if; |
1068 | ||
685094bf RD |
1069 | -- For an access to unconstrained packed array, GIGI needs to see an |
1070 | -- expression with a constrained subtype in order to compute the | |
1071 | -- proper size for the allocator. | |
f02b8bb8 RD |
1072 | |
1073 | if Is_Array_Type (T) | |
1074 | and then not Is_Constrained (T) | |
1075 | and then Is_Packed (T) | |
1076 | then | |
1077 | declare | |
191fcb3a | 1078 | ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A'); |
f02b8bb8 RD |
1079 | Internal_Exp : constant Node_Id := Relocate_Node (Exp); |
1080 | begin | |
1081 | Insert_Action (Exp, | |
1082 | Make_Subtype_Declaration (Loc, | |
1083 | Defining_Identifier => ConstrT, | |
1084 | Subtype_Indication => | |
1085 | Make_Subtype_From_Expr (Exp, T))); | |
1086 | Freeze_Itype (ConstrT, Exp); | |
1087 | Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp)); | |
1088 | end; | |
fbf5a39b | 1089 | end if; |
f02b8bb8 | 1090 | |
685094bf RD |
1091 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
1092 | -- to a build-in-place function, then access to the allocated object | |
1093 | -- must be passed to the function. Currently we limit such functions | |
1094 | -- to those with constrained limited result subtypes, but eventually | |
1095 | -- we plan to expand the allowed forms of functions that are treated | |
1096 | -- as build-in-place. | |
20b5d666 | 1097 | |
0791fbe9 | 1098 | if Ada_Version >= Ada_2005 |
20b5d666 JM |
1099 | and then Is_Build_In_Place_Function_Call (Exp) |
1100 | then | |
1101 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
1102 | end if; | |
fbf5a39b AC |
1103 | end if; |
1104 | ||
1105 | exception | |
1106 | when RE_Not_Available => | |
1107 | return; | |
1108 | end Expand_Allocator_Expression; | |
1109 | ||
70482933 RK |
1110 | ----------------------------- |
1111 | -- Expand_Array_Comparison -- | |
1112 | ----------------------------- | |
1113 | ||
685094bf RD |
1114 | -- Expansion is only required in the case of array types. For the unpacked |
1115 | -- case, an appropriate runtime routine is called. For packed cases, and | |
1116 | -- also in some other cases where a runtime routine cannot be called, the | |
1117 | -- form of the expansion is: | |
70482933 RK |
1118 | |
1119 | -- [body for greater_nn; boolean_expression] | |
1120 | ||
1121 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
1122 | -- Boolean expression depends on the operator involved. | |
1123 | ||
1124 | procedure Expand_Array_Comparison (N : Node_Id) is | |
1125 | Loc : constant Source_Ptr := Sloc (N); | |
1126 | Op1 : Node_Id := Left_Opnd (N); | |
1127 | Op2 : Node_Id := Right_Opnd (N); | |
1128 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 1129 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
1130 | |
1131 | Expr : Node_Id; | |
1132 | Func_Body : Node_Id; | |
1133 | Func_Name : Entity_Id; | |
1134 | ||
fbf5a39b AC |
1135 | Comp : RE_Id; |
1136 | ||
9bc43c53 AC |
1137 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
1138 | -- True for byte addressable target | |
91b1417d | 1139 | |
fbf5a39b | 1140 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
685094bf RD |
1141 | -- Returns True if the length of the given operand is known to be less |
1142 | -- than 4. Returns False if this length is known to be four or greater | |
1143 | -- or is not known at compile time. | |
fbf5a39b AC |
1144 | |
1145 | ------------------------ | |
1146 | -- Length_Less_Than_4 -- | |
1147 | ------------------------ | |
1148 | ||
1149 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
1150 | Otyp : constant Entity_Id := Etype (Opnd); | |
1151 | ||
1152 | begin | |
1153 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
1154 | return String_Literal_Length (Otyp) < 4; | |
1155 | ||
1156 | else | |
1157 | declare | |
1158 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
1159 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
1160 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
1161 | Lov : Uint; | |
1162 | Hiv : Uint; | |
1163 | ||
1164 | begin | |
1165 | if Compile_Time_Known_Value (Lo) then | |
1166 | Lov := Expr_Value (Lo); | |
1167 | else | |
1168 | return False; | |
1169 | end if; | |
1170 | ||
1171 | if Compile_Time_Known_Value (Hi) then | |
1172 | Hiv := Expr_Value (Hi); | |
1173 | else | |
1174 | return False; | |
1175 | end if; | |
1176 | ||
1177 | return Hiv < Lov + 3; | |
1178 | end; | |
1179 | end if; | |
1180 | end Length_Less_Than_4; | |
1181 | ||
1182 | -- Start of processing for Expand_Array_Comparison | |
1183 | ||
70482933 | 1184 | begin |
fbf5a39b AC |
1185 | -- Deal first with unpacked case, where we can call a runtime routine |
1186 | -- except that we avoid this for targets for which are not addressable | |
26bff3d9 | 1187 | -- by bytes, and for the JVM/CIL, since they do not support direct |
fbf5a39b AC |
1188 | -- addressing of array components. |
1189 | ||
1190 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 1191 | and then Byte_Addressable |
26bff3d9 | 1192 | and then VM_Target = No_VM |
fbf5a39b AC |
1193 | then |
1194 | -- The call we generate is: | |
1195 | ||
1196 | -- Compare_Array_xn[_Unaligned] | |
1197 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
1198 | ||
1199 | -- x = U for unsigned, S for signed | |
1200 | -- n = 8,16,32,64 for component size | |
1201 | -- Add _Unaligned if length < 4 and component size is 8. | |
1202 | -- <op> is the standard comparison operator | |
1203 | ||
1204 | if Component_Size (Typ1) = 8 then | |
1205 | if Length_Less_Than_4 (Op1) | |
1206 | or else | |
1207 | Length_Less_Than_4 (Op2) | |
1208 | then | |
1209 | if Is_Unsigned_Type (Ctyp) then | |
1210 | Comp := RE_Compare_Array_U8_Unaligned; | |
1211 | else | |
1212 | Comp := RE_Compare_Array_S8_Unaligned; | |
1213 | end if; | |
1214 | ||
1215 | else | |
1216 | if Is_Unsigned_Type (Ctyp) then | |
1217 | Comp := RE_Compare_Array_U8; | |
1218 | else | |
1219 | Comp := RE_Compare_Array_S8; | |
1220 | end if; | |
1221 | end if; | |
1222 | ||
1223 | elsif Component_Size (Typ1) = 16 then | |
1224 | if Is_Unsigned_Type (Ctyp) then | |
1225 | Comp := RE_Compare_Array_U16; | |
1226 | else | |
1227 | Comp := RE_Compare_Array_S16; | |
1228 | end if; | |
1229 | ||
1230 | elsif Component_Size (Typ1) = 32 then | |
1231 | if Is_Unsigned_Type (Ctyp) then | |
1232 | Comp := RE_Compare_Array_U32; | |
1233 | else | |
1234 | Comp := RE_Compare_Array_S32; | |
1235 | end if; | |
1236 | ||
1237 | else pragma Assert (Component_Size (Typ1) = 64); | |
1238 | if Is_Unsigned_Type (Ctyp) then | |
1239 | Comp := RE_Compare_Array_U64; | |
1240 | else | |
1241 | Comp := RE_Compare_Array_S64; | |
1242 | end if; | |
1243 | end if; | |
1244 | ||
1245 | Remove_Side_Effects (Op1, Name_Req => True); | |
1246 | Remove_Side_Effects (Op2, Name_Req => True); | |
1247 | ||
1248 | Rewrite (Op1, | |
1249 | Make_Function_Call (Sloc (Op1), | |
1250 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
1251 | ||
1252 | Parameter_Associations => New_List ( | |
1253 | Make_Attribute_Reference (Loc, | |
1254 | Prefix => Relocate_Node (Op1), | |
1255 | Attribute_Name => Name_Address), | |
1256 | ||
1257 | Make_Attribute_Reference (Loc, | |
1258 | Prefix => Relocate_Node (Op2), | |
1259 | Attribute_Name => Name_Address), | |
1260 | ||
1261 | Make_Attribute_Reference (Loc, | |
1262 | Prefix => Relocate_Node (Op1), | |
1263 | Attribute_Name => Name_Length), | |
1264 | ||
1265 | Make_Attribute_Reference (Loc, | |
1266 | Prefix => Relocate_Node (Op2), | |
1267 | Attribute_Name => Name_Length)))); | |
1268 | ||
1269 | Rewrite (Op2, | |
1270 | Make_Integer_Literal (Sloc (Op2), | |
1271 | Intval => Uint_0)); | |
1272 | ||
1273 | Analyze_And_Resolve (Op1, Standard_Integer); | |
1274 | Analyze_And_Resolve (Op2, Standard_Integer); | |
1275 | return; | |
1276 | end if; | |
1277 | ||
1278 | -- Cases where we cannot make runtime call | |
1279 | ||
70482933 RK |
1280 | -- For (a <= b) we convert to not (a > b) |
1281 | ||
1282 | if Chars (N) = Name_Op_Le then | |
1283 | Rewrite (N, | |
1284 | Make_Op_Not (Loc, | |
1285 | Right_Opnd => | |
1286 | Make_Op_Gt (Loc, | |
1287 | Left_Opnd => Op1, | |
1288 | Right_Opnd => Op2))); | |
1289 | Analyze_And_Resolve (N, Standard_Boolean); | |
1290 | return; | |
1291 | ||
1292 | -- For < the Boolean expression is | |
1293 | -- greater__nn (op2, op1) | |
1294 | ||
1295 | elsif Chars (N) = Name_Op_Lt then | |
1296 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1297 | ||
1298 | -- Switch operands | |
1299 | ||
1300 | Op1 := Right_Opnd (N); | |
1301 | Op2 := Left_Opnd (N); | |
1302 | ||
1303 | -- For (a >= b) we convert to not (a < b) | |
1304 | ||
1305 | elsif Chars (N) = Name_Op_Ge then | |
1306 | Rewrite (N, | |
1307 | Make_Op_Not (Loc, | |
1308 | Right_Opnd => | |
1309 | Make_Op_Lt (Loc, | |
1310 | Left_Opnd => Op1, | |
1311 | Right_Opnd => Op2))); | |
1312 | Analyze_And_Resolve (N, Standard_Boolean); | |
1313 | return; | |
1314 | ||
1315 | -- For > the Boolean expression is | |
1316 | -- greater__nn (op1, op2) | |
1317 | ||
1318 | else | |
1319 | pragma Assert (Chars (N) = Name_Op_Gt); | |
1320 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1321 | end if; | |
1322 | ||
1323 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1324 | Expr := | |
1325 | Make_Function_Call (Loc, | |
1326 | Name => New_Reference_To (Func_Name, Loc), | |
1327 | Parameter_Associations => New_List (Op1, Op2)); | |
1328 | ||
1329 | Insert_Action (N, Func_Body); | |
1330 | Rewrite (N, Expr); | |
1331 | Analyze_And_Resolve (N, Standard_Boolean); | |
1332 | ||
fbf5a39b AC |
1333 | exception |
1334 | when RE_Not_Available => | |
1335 | return; | |
70482933 RK |
1336 | end Expand_Array_Comparison; |
1337 | ||
1338 | --------------------------- | |
1339 | -- Expand_Array_Equality -- | |
1340 | --------------------------- | |
1341 | ||
685094bf RD |
1342 | -- Expand an equality function for multi-dimensional arrays. Here is an |
1343 | -- example of such a function for Nb_Dimension = 2 | |
70482933 | 1344 | |
0da2c8ac | 1345 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 1346 | -- begin |
fbf5a39b AC |
1347 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
1348 | -- and then | |
1349 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
1350 | -- then | |
1351 | -- return True; -- RM 4.5.2(22) | |
1352 | -- end if; | |
0da2c8ac | 1353 | |
fbf5a39b AC |
1354 | -- if A'length (1) /= B'length (1) |
1355 | -- or else | |
1356 | -- A'length (2) /= B'length (2) | |
1357 | -- then | |
1358 | -- return False; -- RM 4.5.2(23) | |
1359 | -- end if; | |
0da2c8ac | 1360 | |
fbf5a39b | 1361 | -- declare |
523456db AC |
1362 | -- A1 : Index_T1 := A'first (1); |
1363 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 1364 | -- begin |
523456db | 1365 | -- loop |
fbf5a39b | 1366 | -- declare |
523456db AC |
1367 | -- A2 : Index_T2 := A'first (2); |
1368 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 1369 | -- begin |
523456db | 1370 | -- loop |
fbf5a39b AC |
1371 | -- if A (A1, A2) /= B (B1, B2) then |
1372 | -- return False; | |
70482933 | 1373 | -- end if; |
0da2c8ac | 1374 | |
523456db AC |
1375 | -- exit when A2 = A'last (2); |
1376 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 1377 | -- B2 := Index_T2'succ (B2); |
70482933 | 1378 | -- end loop; |
fbf5a39b | 1379 | -- end; |
0da2c8ac | 1380 | |
523456db AC |
1381 | -- exit when A1 = A'last (1); |
1382 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 1383 | -- B1 := Index_T1'succ (B1); |
70482933 | 1384 | -- end loop; |
fbf5a39b | 1385 | -- end; |
0da2c8ac | 1386 | |
70482933 RK |
1387 | -- return true; |
1388 | -- end Enn; | |
1389 | ||
685094bf RD |
1390 | -- Note on the formal types used (atyp and btyp). If either of the arrays |
1391 | -- is of a private type, we use the underlying type, and do an unchecked | |
1392 | -- conversion of the actual. If either of the arrays has a bound depending | |
1393 | -- on a discriminant, then we use the base type since otherwise we have an | |
1394 | -- escaped discriminant in the function. | |
0da2c8ac | 1395 | |
685094bf RD |
1396 | -- If both arrays are constrained and have the same bounds, we can generate |
1397 | -- a loop with an explicit iteration scheme using a 'Range attribute over | |
1398 | -- the first array. | |
523456db | 1399 | |
70482933 RK |
1400 | function Expand_Array_Equality |
1401 | (Nod : Node_Id; | |
70482933 RK |
1402 | Lhs : Node_Id; |
1403 | Rhs : Node_Id; | |
0da2c8ac AC |
1404 | Bodies : List_Id; |
1405 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
1406 | is |
1407 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
1408 | Decls : constant List_Id := New_List; |
1409 | Index_List1 : constant List_Id := New_List; | |
1410 | Index_List2 : constant List_Id := New_List; | |
1411 | ||
1412 | Actuals : List_Id; | |
1413 | Formals : List_Id; | |
1414 | Func_Name : Entity_Id; | |
1415 | Func_Body : Node_Id; | |
70482933 RK |
1416 | |
1417 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
1418 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
1419 | ||
0da2c8ac AC |
1420 | Ltyp : Entity_Id; |
1421 | Rtyp : Entity_Id; | |
1422 | -- The parameter types to be used for the formals | |
1423 | ||
fbf5a39b AC |
1424 | function Arr_Attr |
1425 | (Arr : Entity_Id; | |
1426 | Nam : Name_Id; | |
2e071734 | 1427 | Num : Int) return Node_Id; |
5e1c00fa | 1428 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 1429 | |
70482933 | 1430 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
685094bf | 1431 | -- Create one statement to compare corresponding components, designated |
3b42c566 | 1432 | -- by a full set of indexes. |
70482933 | 1433 | |
0da2c8ac | 1434 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
685094bf RD |
1435 | -- Given one of the arguments, computes the appropriate type to be used |
1436 | -- for that argument in the corresponding function formal | |
0da2c8ac | 1437 | |
fbf5a39b | 1438 | function Handle_One_Dimension |
70482933 | 1439 | (N : Int; |
2e071734 | 1440 | Index : Node_Id) return Node_Id; |
0da2c8ac | 1441 | -- This procedure returns the following code |
fbf5a39b AC |
1442 | -- |
1443 | -- declare | |
523456db | 1444 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 1445 | -- begin |
523456db | 1446 | -- loop |
fbf5a39b | 1447 | -- xxx |
523456db AC |
1448 | -- exit when An = A'Last (N); |
1449 | -- An := Index_T'Succ (An) | |
0da2c8ac | 1450 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
1451 | -- end loop; |
1452 | -- end; | |
1453 | -- | |
3b42c566 | 1454 | -- If both indexes are constrained and identical, the procedure |
523456db AC |
1455 | -- returns a simpler loop: |
1456 | -- | |
1457 | -- for An in A'Range (N) loop | |
1458 | -- xxx | |
1459 | -- end loop | |
0da2c8ac | 1460 | -- |
523456db | 1461 | -- N is the dimension for which we are generating a loop. Index is the |
685094bf RD |
1462 | -- N'th index node, whose Etype is Index_Type_n in the above code. The |
1463 | -- xxx statement is either the loop or declare for the next dimension | |
1464 | -- or if this is the last dimension the comparison of corresponding | |
1465 | -- components of the arrays. | |
fbf5a39b | 1466 | -- |
685094bf RD |
1467 | -- The actual way the code works is to return the comparison of |
1468 | -- corresponding components for the N+1 call. That's neater! | |
fbf5a39b AC |
1469 | |
1470 | function Test_Empty_Arrays return Node_Id; | |
1471 | -- This function constructs the test for both arrays being empty | |
1472 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1473 | -- and then | |
1474 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1475 | ||
1476 | function Test_Lengths_Correspond return Node_Id; | |
685094bf RD |
1477 | -- This function constructs the test for arrays having different lengths |
1478 | -- in at least one index position, in which case the resulting code is: | |
fbf5a39b AC |
1479 | |
1480 | -- A'length (1) /= B'length (1) | |
1481 | -- or else | |
1482 | -- A'length (2) /= B'length (2) | |
1483 | -- or else | |
1484 | -- ... | |
1485 | ||
1486 | -------------- | |
1487 | -- Arr_Attr -- | |
1488 | -------------- | |
1489 | ||
1490 | function Arr_Attr | |
1491 | (Arr : Entity_Id; | |
1492 | Nam : Name_Id; | |
2e071734 | 1493 | Num : Int) return Node_Id |
fbf5a39b AC |
1494 | is |
1495 | begin | |
1496 | return | |
1497 | Make_Attribute_Reference (Loc, | |
1498 | Attribute_Name => Nam, | |
1499 | Prefix => New_Reference_To (Arr, Loc), | |
1500 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
1501 | end Arr_Attr; | |
70482933 RK |
1502 | |
1503 | ------------------------ | |
1504 | -- Component_Equality -- | |
1505 | ------------------------ | |
1506 | ||
1507 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1508 | Test : Node_Id; | |
1509 | L, R : Node_Id; | |
1510 | ||
1511 | begin | |
1512 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1513 | ||
1514 | L := | |
1515 | Make_Indexed_Component (Loc, | |
7675ad4f | 1516 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
1517 | Expressions => Index_List1); |
1518 | ||
1519 | R := | |
1520 | Make_Indexed_Component (Loc, | |
7675ad4f | 1521 | Prefix => Make_Identifier (Loc, Chars (B)), |
70482933 RK |
1522 | Expressions => Index_List2); |
1523 | ||
1524 | Test := Expand_Composite_Equality | |
1525 | (Nod, Component_Type (Typ), L, R, Decls); | |
1526 | ||
a9d8907c JM |
1527 | -- If some (sub)component is an unchecked_union, the whole operation |
1528 | -- will raise program error. | |
8aceda64 AC |
1529 | |
1530 | if Nkind (Test) = N_Raise_Program_Error then | |
a9d8907c JM |
1531 | |
1532 | -- This node is going to be inserted at a location where a | |
685094bf RD |
1533 | -- statement is expected: clear its Etype so analysis will set |
1534 | -- it to the expected Standard_Void_Type. | |
a9d8907c JM |
1535 | |
1536 | Set_Etype (Test, Empty); | |
8aceda64 AC |
1537 | return Test; |
1538 | ||
1539 | else | |
1540 | return | |
1541 | Make_Implicit_If_Statement (Nod, | |
1542 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), | |
1543 | Then_Statements => New_List ( | |
d766cee3 | 1544 | Make_Simple_Return_Statement (Loc, |
8aceda64 AC |
1545 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
1546 | end if; | |
70482933 RK |
1547 | end Component_Equality; |
1548 | ||
0da2c8ac AC |
1549 | ------------------ |
1550 | -- Get_Arg_Type -- | |
1551 | ------------------ | |
1552 | ||
1553 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1554 | T : Entity_Id; | |
1555 | X : Node_Id; | |
1556 | ||
1557 | begin | |
1558 | T := Etype (N); | |
1559 | ||
1560 | if No (T) then | |
1561 | return Typ; | |
1562 | ||
1563 | else | |
1564 | T := Underlying_Type (T); | |
1565 | ||
1566 | X := First_Index (T); | |
1567 | while Present (X) loop | |
1568 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) | |
1569 | or else | |
1570 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
1571 | then | |
1572 | T := Base_Type (T); | |
1573 | exit; | |
1574 | end if; | |
1575 | ||
1576 | Next_Index (X); | |
1577 | end loop; | |
1578 | ||
1579 | return T; | |
1580 | end if; | |
1581 | end Get_Arg_Type; | |
1582 | ||
fbf5a39b AC |
1583 | -------------------------- |
1584 | -- Handle_One_Dimension -- | |
1585 | --------------------------- | |
70482933 | 1586 | |
fbf5a39b | 1587 | function Handle_One_Dimension |
70482933 | 1588 | (N : Int; |
2e071734 | 1589 | Index : Node_Id) return Node_Id |
70482933 | 1590 | is |
0da2c8ac AC |
1591 | Need_Separate_Indexes : constant Boolean := |
1592 | Ltyp /= Rtyp | |
1593 | or else not Is_Constrained (Ltyp); | |
1594 | -- If the index types are identical, and we are working with | |
685094bf RD |
1595 | -- constrained types, then we can use the same index for both |
1596 | -- of the arrays. | |
0da2c8ac | 1597 | |
191fcb3a | 1598 | An : constant Entity_Id := Make_Temporary (Loc, 'A'); |
0da2c8ac AC |
1599 | |
1600 | Bn : Entity_Id; | |
1601 | Index_T : Entity_Id; | |
1602 | Stm_List : List_Id; | |
1603 | Loop_Stm : Node_Id; | |
70482933 RK |
1604 | |
1605 | begin | |
0da2c8ac AC |
1606 | if N > Number_Dimensions (Ltyp) then |
1607 | return Component_Equality (Ltyp); | |
fbf5a39b | 1608 | end if; |
70482933 | 1609 | |
0da2c8ac AC |
1610 | -- Case where we generate a loop |
1611 | ||
1612 | Index_T := Base_Type (Etype (Index)); | |
1613 | ||
1614 | if Need_Separate_Indexes then | |
191fcb3a | 1615 | Bn := Make_Temporary (Loc, 'B'); |
0da2c8ac AC |
1616 | else |
1617 | Bn := An; | |
1618 | end if; | |
70482933 | 1619 | |
fbf5a39b AC |
1620 | Append (New_Reference_To (An, Loc), Index_List1); |
1621 | Append (New_Reference_To (Bn, Loc), Index_List2); | |
70482933 | 1622 | |
0da2c8ac AC |
1623 | Stm_List := New_List ( |
1624 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1625 | |
0da2c8ac | 1626 | if Need_Separate_Indexes then |
a9d8907c | 1627 | |
3b42c566 | 1628 | -- Generate guard for loop, followed by increments of indexes |
523456db AC |
1629 | |
1630 | Append_To (Stm_List, | |
1631 | Make_Exit_Statement (Loc, | |
1632 | Condition => | |
1633 | Make_Op_Eq (Loc, | |
1634 | Left_Opnd => New_Reference_To (An, Loc), | |
1635 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); | |
1636 | ||
1637 | Append_To (Stm_List, | |
1638 | Make_Assignment_Statement (Loc, | |
1639 | Name => New_Reference_To (An, Loc), | |
1640 | Expression => | |
1641 | Make_Attribute_Reference (Loc, | |
1642 | Prefix => New_Reference_To (Index_T, Loc), | |
1643 | Attribute_Name => Name_Succ, | |
1644 | Expressions => New_List (New_Reference_To (An, Loc))))); | |
1645 | ||
0da2c8ac AC |
1646 | Append_To (Stm_List, |
1647 | Make_Assignment_Statement (Loc, | |
1648 | Name => New_Reference_To (Bn, Loc), | |
1649 | Expression => | |
1650 | Make_Attribute_Reference (Loc, | |
1651 | Prefix => New_Reference_To (Index_T, Loc), | |
1652 | Attribute_Name => Name_Succ, | |
1653 | Expressions => New_List (New_Reference_To (Bn, Loc))))); | |
1654 | end if; | |
1655 | ||
a9d8907c JM |
1656 | -- If separate indexes, we need a declare block for An and Bn, and a |
1657 | -- loop without an iteration scheme. | |
0da2c8ac AC |
1658 | |
1659 | if Need_Separate_Indexes then | |
523456db AC |
1660 | Loop_Stm := |
1661 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
1662 | ||
0da2c8ac AC |
1663 | return |
1664 | Make_Block_Statement (Loc, | |
1665 | Declarations => New_List ( | |
523456db AC |
1666 | Make_Object_Declaration (Loc, |
1667 | Defining_Identifier => An, | |
1668 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1669 | Expression => Arr_Attr (A, Name_First, N)), | |
1670 | ||
0da2c8ac AC |
1671 | Make_Object_Declaration (Loc, |
1672 | Defining_Identifier => Bn, | |
1673 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1674 | Expression => Arr_Attr (B, Name_First, N))), | |
523456db | 1675 | |
0da2c8ac AC |
1676 | Handled_Statement_Sequence => |
1677 | Make_Handled_Sequence_Of_Statements (Loc, | |
1678 | Statements => New_List (Loop_Stm))); | |
1679 | ||
523456db AC |
1680 | -- If no separate indexes, return loop statement with explicit |
1681 | -- iteration scheme on its own | |
0da2c8ac AC |
1682 | |
1683 | else | |
523456db AC |
1684 | Loop_Stm := |
1685 | Make_Implicit_Loop_Statement (Nod, | |
1686 | Statements => Stm_List, | |
1687 | Iteration_Scheme => | |
1688 | Make_Iteration_Scheme (Loc, | |
1689 | Loop_Parameter_Specification => | |
1690 | Make_Loop_Parameter_Specification (Loc, | |
1691 | Defining_Identifier => An, | |
1692 | Discrete_Subtype_Definition => | |
1693 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
1694 | return Loop_Stm; |
1695 | end if; | |
fbf5a39b AC |
1696 | end Handle_One_Dimension; |
1697 | ||
1698 | ----------------------- | |
1699 | -- Test_Empty_Arrays -- | |
1700 | ----------------------- | |
1701 | ||
1702 | function Test_Empty_Arrays return Node_Id is | |
1703 | Alist : Node_Id; | |
1704 | Blist : Node_Id; | |
1705 | ||
1706 | Atest : Node_Id; | |
1707 | Btest : Node_Id; | |
70482933 | 1708 | |
fbf5a39b AC |
1709 | begin |
1710 | Alist := Empty; | |
1711 | Blist := Empty; | |
0da2c8ac | 1712 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1713 | Atest := |
1714 | Make_Op_Eq (Loc, | |
1715 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1716 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1717 | ||
1718 | Btest := | |
1719 | Make_Op_Eq (Loc, | |
1720 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
1721 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1722 | ||
1723 | if No (Alist) then | |
1724 | Alist := Atest; | |
1725 | Blist := Btest; | |
70482933 | 1726 | |
fbf5a39b AC |
1727 | else |
1728 | Alist := | |
1729 | Make_Or_Else (Loc, | |
1730 | Left_Opnd => Relocate_Node (Alist), | |
1731 | Right_Opnd => Atest); | |
1732 | ||
1733 | Blist := | |
1734 | Make_Or_Else (Loc, | |
1735 | Left_Opnd => Relocate_Node (Blist), | |
1736 | Right_Opnd => Btest); | |
1737 | end if; | |
1738 | end loop; | |
70482933 | 1739 | |
fbf5a39b AC |
1740 | return |
1741 | Make_And_Then (Loc, | |
1742 | Left_Opnd => Alist, | |
1743 | Right_Opnd => Blist); | |
1744 | end Test_Empty_Arrays; | |
70482933 | 1745 | |
fbf5a39b AC |
1746 | ----------------------------- |
1747 | -- Test_Lengths_Correspond -- | |
1748 | ----------------------------- | |
70482933 | 1749 | |
fbf5a39b AC |
1750 | function Test_Lengths_Correspond return Node_Id is |
1751 | Result : Node_Id; | |
1752 | Rtest : Node_Id; | |
1753 | ||
1754 | begin | |
1755 | Result := Empty; | |
0da2c8ac | 1756 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1757 | Rtest := |
1758 | Make_Op_Ne (Loc, | |
1759 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1760 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
1761 | ||
1762 | if No (Result) then | |
1763 | Result := Rtest; | |
1764 | else | |
1765 | Result := | |
1766 | Make_Or_Else (Loc, | |
1767 | Left_Opnd => Relocate_Node (Result), | |
1768 | Right_Opnd => Rtest); | |
1769 | end if; | |
1770 | end loop; | |
1771 | ||
1772 | return Result; | |
1773 | end Test_Lengths_Correspond; | |
70482933 RK |
1774 | |
1775 | -- Start of processing for Expand_Array_Equality | |
1776 | ||
1777 | begin | |
0da2c8ac AC |
1778 | Ltyp := Get_Arg_Type (Lhs); |
1779 | Rtyp := Get_Arg_Type (Rhs); | |
1780 | ||
685094bf RD |
1781 | -- For now, if the argument types are not the same, go to the base type, |
1782 | -- since the code assumes that the formals have the same type. This is | |
1783 | -- fixable in future ??? | |
0da2c8ac AC |
1784 | |
1785 | if Ltyp /= Rtyp then | |
1786 | Ltyp := Base_Type (Ltyp); | |
1787 | Rtyp := Base_Type (Rtyp); | |
1788 | pragma Assert (Ltyp = Rtyp); | |
1789 | end if; | |
1790 | ||
1791 | -- Build list of formals for function | |
1792 | ||
70482933 RK |
1793 | Formals := New_List ( |
1794 | Make_Parameter_Specification (Loc, | |
1795 | Defining_Identifier => A, | |
0da2c8ac | 1796 | Parameter_Type => New_Reference_To (Ltyp, Loc)), |
70482933 RK |
1797 | |
1798 | Make_Parameter_Specification (Loc, | |
1799 | Defining_Identifier => B, | |
0da2c8ac | 1800 | Parameter_Type => New_Reference_To (Rtyp, Loc))); |
70482933 | 1801 | |
191fcb3a | 1802 | Func_Name := Make_Temporary (Loc, 'E'); |
70482933 | 1803 | |
fbf5a39b | 1804 | -- Build statement sequence for function |
70482933 RK |
1805 | |
1806 | Func_Body := | |
1807 | Make_Subprogram_Body (Loc, | |
1808 | Specification => | |
1809 | Make_Function_Specification (Loc, | |
1810 | Defining_Unit_Name => Func_Name, | |
1811 | Parameter_Specifications => Formals, | |
630d30e9 | 1812 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
fbf5a39b AC |
1813 | |
1814 | Declarations => Decls, | |
1815 | ||
70482933 RK |
1816 | Handled_Statement_Sequence => |
1817 | Make_Handled_Sequence_Of_Statements (Loc, | |
1818 | Statements => New_List ( | |
fbf5a39b AC |
1819 | |
1820 | Make_Implicit_If_Statement (Nod, | |
1821 | Condition => Test_Empty_Arrays, | |
1822 | Then_Statements => New_List ( | |
d766cee3 | 1823 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
1824 | Expression => |
1825 | New_Occurrence_Of (Standard_True, Loc)))), | |
1826 | ||
1827 | Make_Implicit_If_Statement (Nod, | |
1828 | Condition => Test_Lengths_Correspond, | |
1829 | Then_Statements => New_List ( | |
d766cee3 | 1830 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
1831 | Expression => |
1832 | New_Occurrence_Of (Standard_False, Loc)))), | |
1833 | ||
0da2c8ac | 1834 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 1835 | |
d766cee3 | 1836 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
1837 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
1838 | ||
1839 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 1840 | Set_Is_Inlined (Func_Name); |
70482933 | 1841 | |
685094bf RD |
1842 | -- If the array type is distinct from the type of the arguments, it |
1843 | -- is the full view of a private type. Apply an unchecked conversion | |
1844 | -- to insure that analysis of the call succeeds. | |
70482933 | 1845 | |
0da2c8ac AC |
1846 | declare |
1847 | L, R : Node_Id; | |
1848 | ||
1849 | begin | |
1850 | L := Lhs; | |
1851 | R := Rhs; | |
1852 | ||
1853 | if No (Etype (Lhs)) | |
1854 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
1855 | then | |
1856 | L := OK_Convert_To (Ltyp, Lhs); | |
1857 | end if; | |
1858 | ||
1859 | if No (Etype (Rhs)) | |
1860 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
1861 | then | |
1862 | R := OK_Convert_To (Rtyp, Rhs); | |
1863 | end if; | |
1864 | ||
1865 | Actuals := New_List (L, R); | |
1866 | end; | |
70482933 RK |
1867 | |
1868 | Append_To (Bodies, Func_Body); | |
1869 | ||
1870 | return | |
1871 | Make_Function_Call (Loc, | |
0da2c8ac | 1872 | Name => New_Reference_To (Func_Name, Loc), |
70482933 RK |
1873 | Parameter_Associations => Actuals); |
1874 | end Expand_Array_Equality; | |
1875 | ||
1876 | ----------------------------- | |
1877 | -- Expand_Boolean_Operator -- | |
1878 | ----------------------------- | |
1879 | ||
685094bf RD |
1880 | -- Note that we first get the actual subtypes of the operands, since we |
1881 | -- always want to deal with types that have bounds. | |
70482933 RK |
1882 | |
1883 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 1884 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
1885 | |
1886 | begin | |
685094bf RD |
1887 | -- Special case of bit packed array where both operands are known to be |
1888 | -- properly aligned. In this case we use an efficient run time routine | |
1889 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
1890 | |
1891 | if Is_Bit_Packed_Array (Typ) | |
1892 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
1893 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
1894 | then | |
70482933 | 1895 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
1896 | return; |
1897 | end if; | |
70482933 | 1898 | |
a9d8907c JM |
1899 | -- For the normal non-packed case, the general expansion is to build |
1900 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
1901 | -- and then inserting it into the tree. The original operator node is | |
1902 | -- then rewritten as a call to this function. We also use this in the | |
1903 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 1904 | |
a9d8907c JM |
1905 | declare |
1906 | Loc : constant Source_Ptr := Sloc (N); | |
1907 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
1908 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
1909 | Func_Body : Node_Id; | |
1910 | Func_Name : Entity_Id; | |
fbf5a39b | 1911 | |
a9d8907c JM |
1912 | begin |
1913 | Convert_To_Actual_Subtype (L); | |
1914 | Convert_To_Actual_Subtype (R); | |
1915 | Ensure_Defined (Etype (L), N); | |
1916 | Ensure_Defined (Etype (R), N); | |
1917 | Apply_Length_Check (R, Etype (L)); | |
1918 | ||
b4592168 GD |
1919 | if Nkind (N) = N_Op_Xor then |
1920 | Silly_Boolean_Array_Xor_Test (N, Etype (L)); | |
1921 | end if; | |
1922 | ||
a9d8907c JM |
1923 | if Nkind (Parent (N)) = N_Assignment_Statement |
1924 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
1925 | then | |
1926 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 1927 | |
a9d8907c JM |
1928 | elsif Nkind (Parent (N)) = N_Op_Not |
1929 | and then Nkind (N) = N_Op_And | |
1930 | and then | |
b4592168 | 1931 | Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
1932 | then |
1933 | return; | |
1934 | else | |
fbf5a39b | 1935 | |
a9d8907c JM |
1936 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
1937 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1938 | Insert_Action (N, Func_Body); | |
70482933 | 1939 | |
a9d8907c | 1940 | -- Now rewrite the expression with a call |
70482933 | 1941 | |
a9d8907c JM |
1942 | Rewrite (N, |
1943 | Make_Function_Call (Loc, | |
1944 | Name => New_Reference_To (Func_Name, Loc), | |
1945 | Parameter_Associations => | |
1946 | New_List ( | |
1947 | L, | |
1948 | Make_Type_Conversion | |
1949 | (Loc, New_Reference_To (Etype (L), Loc), R)))); | |
70482933 | 1950 | |
a9d8907c JM |
1951 | Analyze_And_Resolve (N, Typ); |
1952 | end if; | |
1953 | end; | |
70482933 RK |
1954 | end Expand_Boolean_Operator; |
1955 | ||
1956 | ------------------------------- | |
1957 | -- Expand_Composite_Equality -- | |
1958 | ------------------------------- | |
1959 | ||
1960 | -- This function is only called for comparing internal fields of composite | |
1961 | -- types when these fields are themselves composites. This is a special | |
1962 | -- case because it is not possible to respect normal Ada visibility rules. | |
1963 | ||
1964 | function Expand_Composite_Equality | |
1965 | (Nod : Node_Id; | |
1966 | Typ : Entity_Id; | |
1967 | Lhs : Node_Id; | |
1968 | Rhs : Node_Id; | |
2e071734 | 1969 | Bodies : List_Id) return Node_Id |
70482933 RK |
1970 | is |
1971 | Loc : constant Source_Ptr := Sloc (Nod); | |
1972 | Full_Type : Entity_Id; | |
1973 | Prim : Elmt_Id; | |
1974 | Eq_Op : Entity_Id; | |
1975 | ||
1976 | begin | |
1977 | if Is_Private_Type (Typ) then | |
1978 | Full_Type := Underlying_Type (Typ); | |
1979 | else | |
1980 | Full_Type := Typ; | |
1981 | end if; | |
1982 | ||
685094bf RD |
1983 | -- Defense against malformed private types with no completion the error |
1984 | -- will be diagnosed later by check_completion | |
70482933 RK |
1985 | |
1986 | if No (Full_Type) then | |
1987 | return New_Reference_To (Standard_False, Loc); | |
1988 | end if; | |
1989 | ||
1990 | Full_Type := Base_Type (Full_Type); | |
1991 | ||
1992 | if Is_Array_Type (Full_Type) then | |
1993 | ||
1994 | -- If the operand is an elementary type other than a floating-point | |
1995 | -- type, then we can simply use the built-in block bitwise equality, | |
1996 | -- since the predefined equality operators always apply and bitwise | |
1997 | -- equality is fine for all these cases. | |
1998 | ||
1999 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
2000 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
2001 | then | |
2002 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
2003 | ||
685094bf RD |
2004 | -- For composite component types, and floating-point types, use the |
2005 | -- expansion. This deals with tagged component types (where we use | |
2006 | -- the applicable equality routine) and floating-point, (where we | |
2007 | -- need to worry about negative zeroes), and also the case of any | |
2008 | -- composite type recursively containing such fields. | |
70482933 RK |
2009 | |
2010 | else | |
0da2c8ac | 2011 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
2012 | end if; |
2013 | ||
2014 | elsif Is_Tagged_Type (Full_Type) then | |
2015 | ||
2016 | -- Call the primitive operation "=" of this type | |
2017 | ||
2018 | if Is_Class_Wide_Type (Full_Type) then | |
2019 | Full_Type := Root_Type (Full_Type); | |
2020 | end if; | |
2021 | ||
685094bf RD |
2022 | -- If this is derived from an untagged private type completed with a |
2023 | -- tagged type, it does not have a full view, so we use the primitive | |
2024 | -- operations of the private type. This check should no longer be | |
2025 | -- necessary when these types receive their full views ??? | |
70482933 RK |
2026 | |
2027 | if Is_Private_Type (Typ) | |
2028 | and then not Is_Tagged_Type (Typ) | |
2029 | and then not Is_Controlled (Typ) | |
2030 | and then Is_Derived_Type (Typ) | |
2031 | and then No (Full_View (Typ)) | |
2032 | then | |
2033 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2034 | else | |
2035 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
2036 | end if; | |
2037 | ||
2038 | loop | |
2039 | Eq_Op := Node (Prim); | |
2040 | exit when Chars (Eq_Op) = Name_Op_Eq | |
2041 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
2042 | Etype (Next_Formal (First_Formal (Eq_Op))) |
2043 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
2044 | Next_Elmt (Prim); |
2045 | pragma Assert (Present (Prim)); | |
2046 | end loop; | |
2047 | ||
2048 | Eq_Op := Node (Prim); | |
2049 | ||
2050 | return | |
2051 | Make_Function_Call (Loc, | |
2052 | Name => New_Reference_To (Eq_Op, Loc), | |
2053 | Parameter_Associations => | |
2054 | New_List | |
2055 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2056 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2057 | ||
2058 | elsif Is_Record_Type (Full_Type) then | |
fbf5a39b | 2059 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2060 | |
2061 | if Present (Eq_Op) then | |
2062 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2063 | ||
685094bf RD |
2064 | -- Inherited equality from parent type. Convert the actuals to |
2065 | -- match signature of operation. | |
70482933 RK |
2066 | |
2067 | declare | |
fbf5a39b | 2068 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2069 | |
2070 | begin | |
2071 | return | |
2072 | Make_Function_Call (Loc, | |
2073 | Name => New_Reference_To (Eq_Op, Loc), | |
2074 | Parameter_Associations => | |
2075 | New_List (OK_Convert_To (T, Lhs), | |
2076 | OK_Convert_To (T, Rhs))); | |
2077 | end; | |
2078 | ||
2079 | else | |
5d09245e AC |
2080 | -- Comparison between Unchecked_Union components |
2081 | ||
2082 | if Is_Unchecked_Union (Full_Type) then | |
2083 | declare | |
2084 | Lhs_Type : Node_Id := Full_Type; | |
2085 | Rhs_Type : Node_Id := Full_Type; | |
2086 | Lhs_Discr_Val : Node_Id; | |
2087 | Rhs_Discr_Val : Node_Id; | |
2088 | ||
2089 | begin | |
2090 | -- Lhs subtype | |
2091 | ||
2092 | if Nkind (Lhs) = N_Selected_Component then | |
2093 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2094 | end if; | |
2095 | ||
2096 | -- Rhs subtype | |
2097 | ||
2098 | if Nkind (Rhs) = N_Selected_Component then | |
2099 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2100 | end if; | |
2101 | ||
2102 | -- Lhs of the composite equality | |
2103 | ||
2104 | if Is_Constrained (Lhs_Type) then | |
2105 | ||
685094bf | 2106 | -- Since the enclosing record type can never be an |
5d09245e AC |
2107 | -- Unchecked_Union (this code is executed for records |
2108 | -- that do not have variants), we may reference its | |
2109 | -- discriminant(s). | |
2110 | ||
2111 | if Nkind (Lhs) = N_Selected_Component | |
2112 | and then Has_Per_Object_Constraint ( | |
2113 | Entity (Selector_Name (Lhs))) | |
2114 | then | |
2115 | Lhs_Discr_Val := | |
2116 | Make_Selected_Component (Loc, | |
2117 | Prefix => Prefix (Lhs), | |
2118 | Selector_Name => | |
2119 | New_Copy ( | |
2120 | Get_Discriminant_Value ( | |
2121 | First_Discriminant (Lhs_Type), | |
2122 | Lhs_Type, | |
2123 | Stored_Constraint (Lhs_Type)))); | |
2124 | ||
2125 | else | |
2126 | Lhs_Discr_Val := New_Copy ( | |
2127 | Get_Discriminant_Value ( | |
2128 | First_Discriminant (Lhs_Type), | |
2129 | Lhs_Type, | |
2130 | Stored_Constraint (Lhs_Type))); | |
2131 | ||
2132 | end if; | |
2133 | else | |
2134 | -- It is not possible to infer the discriminant since | |
2135 | -- the subtype is not constrained. | |
2136 | ||
8aceda64 | 2137 | return |
5d09245e | 2138 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2139 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2140 | end if; |
2141 | ||
2142 | -- Rhs of the composite equality | |
2143 | ||
2144 | if Is_Constrained (Rhs_Type) then | |
2145 | if Nkind (Rhs) = N_Selected_Component | |
2146 | and then Has_Per_Object_Constraint ( | |
2147 | Entity (Selector_Name (Rhs))) | |
2148 | then | |
2149 | Rhs_Discr_Val := | |
2150 | Make_Selected_Component (Loc, | |
2151 | Prefix => Prefix (Rhs), | |
2152 | Selector_Name => | |
2153 | New_Copy ( | |
2154 | Get_Discriminant_Value ( | |
2155 | First_Discriminant (Rhs_Type), | |
2156 | Rhs_Type, | |
2157 | Stored_Constraint (Rhs_Type)))); | |
2158 | ||
2159 | else | |
2160 | Rhs_Discr_Val := New_Copy ( | |
2161 | Get_Discriminant_Value ( | |
2162 | First_Discriminant (Rhs_Type), | |
2163 | Rhs_Type, | |
2164 | Stored_Constraint (Rhs_Type))); | |
2165 | ||
2166 | end if; | |
2167 | else | |
8aceda64 | 2168 | return |
5d09245e | 2169 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2170 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2171 | end if; |
2172 | ||
2173 | -- Call the TSS equality function with the inferred | |
2174 | -- discriminant values. | |
2175 | ||
2176 | return | |
2177 | Make_Function_Call (Loc, | |
2178 | Name => New_Reference_To (Eq_Op, Loc), | |
2179 | Parameter_Associations => New_List ( | |
2180 | Lhs, | |
2181 | Rhs, | |
2182 | Lhs_Discr_Val, | |
2183 | Rhs_Discr_Val)); | |
2184 | end; | |
d151d6a3 AC |
2185 | |
2186 | else | |
2187 | return | |
2188 | Make_Function_Call (Loc, | |
2189 | Name => New_Reference_To (Eq_Op, Loc), | |
2190 | Parameter_Associations => New_List (Lhs, Rhs)); | |
5d09245e | 2191 | end if; |
d151d6a3 | 2192 | end if; |
5d09245e | 2193 | |
dbe945f1 | 2194 | elsif Ada_Version >= Ada_2012 then |
5d09245e | 2195 | |
d151d6a3 AC |
2196 | -- if no TSS has been created for the type, check whether there is |
2197 | -- a primitive equality declared for it. If it is abstract replace | |
a3f2babd | 2198 | -- the call with an explicit raise (AI05-0123). |
d151d6a3 AC |
2199 | |
2200 | declare | |
2201 | Prim : Elmt_Id; | |
2202 | ||
2203 | begin | |
2204 | Prim := First_Elmt (Collect_Primitive_Operations (Full_Type)); | |
2205 | while Present (Prim) loop | |
72e9f2b9 AC |
2206 | |
2207 | -- Locate primitive equality with the right signature | |
2208 | ||
2209 | if Chars (Node (Prim)) = Name_Op_Eq | |
2210 | and then Etype (First_Formal (Node (Prim))) = | |
2211 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
2212 | and then Etype (Node (Prim)) = Standard_Boolean | |
2213 | then | |
d151d6a3 AC |
2214 | if Is_Abstract_Subprogram (Node (Prim)) then |
2215 | return | |
2216 | Make_Raise_Program_Error (Loc, | |
2217 | Reason => PE_Explicit_Raise); | |
2218 | else | |
2219 | return | |
2220 | Make_Function_Call (Loc, | |
2221 | Name => New_Reference_To (Node (Prim), Loc), | |
2222 | Parameter_Associations => New_List (Lhs, Rhs)); | |
2223 | end if; | |
2224 | end if; | |
2225 | ||
2226 | Next_Elmt (Prim); | |
2227 | end loop; | |
2228 | end; | |
2229 | ||
a3f2babd | 2230 | -- Use predefined equality iff no user-defined primitive exists |
d151d6a3 AC |
2231 | |
2232 | return Make_Op_Eq (Loc, Lhs, Rhs); | |
70482933 RK |
2233 | |
2234 | else | |
2235 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2236 | end if; | |
2237 | ||
2238 | else | |
a3f2babd | 2239 | -- If not array or record type, it is predefined equality. |
70482933 RK |
2240 | |
2241 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
2242 | end if; | |
2243 | end Expand_Composite_Equality; | |
2244 | ||
fdac1f80 AC |
2245 | ------------------------ |
2246 | -- Expand_Concatenate -- | |
2247 | ------------------------ | |
70482933 | 2248 | |
fdac1f80 AC |
2249 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2250 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2251 | |
fdac1f80 AC |
2252 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2253 | -- Result type of concatenation | |
70482933 | 2254 | |
fdac1f80 AC |
2255 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2256 | -- Component type. Elements of this component type can appear as one | |
2257 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2258 | |
ecc4ddde AC |
2259 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2260 | -- Index subtype | |
2261 | ||
2262 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2263 | -- Index type. This is the base type of the index subtype, and is used | |
2264 | -- for all computed bounds (which may be out of range of Istyp in the | |
2265 | -- case of null ranges). | |
70482933 | 2266 | |
46ff89f3 | 2267 | Artyp : Entity_Id; |
fdac1f80 AC |
2268 | -- This is the type we use to do arithmetic to compute the bounds and |
2269 | -- lengths of operands. The choice of this type is a little subtle and | |
2270 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2271 | |
fdac1f80 AC |
2272 | Concatenation_Error : exception; |
2273 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2274 | |
0ac73189 AC |
2275 | Result_May_Be_Null : Boolean := True; |
2276 | -- Reset to False if at least one operand is encountered which is known | |
2277 | -- at compile time to be non-null. Used for handling the special case | |
2278 | -- of setting the high bound to the last operand high bound for a null | |
2279 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2280 | ||
df46b832 | 2281 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2282 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2283 | |
2284 | NN : Nat := 0; | |
a29262fd AC |
2285 | -- Number of operands excluding any known to be null, except that the |
2286 | -- last operand is always retained, in case it provides the bounds for | |
2287 | -- a null result. | |
2288 | ||
2289 | Opnd : Node_Id; | |
2290 | -- Current operand being processed in the loop through operands. After | |
2291 | -- this loop is complete, always contains the last operand (which is not | |
2292 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2293 | |
2294 | -- Arrays describing the operands, only the first NN entries of each | |
2295 | -- array are set (NN < N when we exclude known null operands). | |
2296 | ||
2297 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2298 | -- True if length of corresponding operand known at compile time | |
2299 | ||
2300 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2301 | -- Set to the corresponding entry in the Opnds list (but note that null |
2302 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2303 | |
2304 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2305 | -- Set to length of operand. Entries in this array are set only if the |
2306 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2307 | |
0ac73189 AC |
2308 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2309 | -- Set to lower bound of operand. Either an integer literal in the case | |
2310 | -- where the bound is known at compile time, else actual lower bound. | |
2311 | -- The operand low bound is of type Ityp. | |
2312 | ||
df46b832 AC |
2313 | Var_Length : array (1 .. N) of Entity_Id; |
2314 | -- Set to an entity of type Natural that contains the length of an | |
2315 | -- operand whose length is not known at compile time. Entries in this | |
2316 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2317 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2318 | |
2319 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2320 | -- The J'th entry in an expression node that represents the total length |
2321 | -- of operands 1 through J. It is either an integer literal node, or a | |
2322 | -- reference to a constant entity with the right value, so it is fine | |
2323 | -- to just do a Copy_Node to get an appropriate copy. The extra zero'th | |
46ff89f3 | 2324 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 AC |
2325 | |
2326 | Low_Bound : Node_Id; | |
0ac73189 AC |
2327 | -- A tree node representing the low bound of the result (of type Ityp). |
2328 | -- This is either an integer literal node, or an identifier reference to | |
2329 | -- a constant entity initialized to the appropriate value. | |
2330 | ||
a29262fd AC |
2331 | Last_Opnd_High_Bound : Node_Id; |
2332 | -- A tree node representing the high bound of the last operand. This | |
2333 | -- need only be set if the result could be null. It is used for the | |
2334 | -- special case of setting the right high bound for a null result. | |
2335 | -- This is of type Ityp. | |
2336 | ||
0ac73189 AC |
2337 | High_Bound : Node_Id; |
2338 | -- A tree node representing the high bound of the result (of type Ityp) | |
df46b832 AC |
2339 | |
2340 | Result : Node_Id; | |
0ac73189 | 2341 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2342 | |
d0f8d157 AC |
2343 | Actions : constant List_Id := New_List; |
2344 | -- Collect actions to be inserted if Save_Space is False | |
2345 | ||
2346 | Save_Space : Boolean; | |
2347 | pragma Warnings (Off, Save_Space); | |
2348 | -- Set to True if we are saving generated code space by calling routines | |
2349 | -- in packages System.Concat_n. | |
2350 | ||
fa969310 | 2351 | Known_Non_Null_Operand_Seen : Boolean; |
308e6f3a | 2352 | -- Set True during generation of the assignments of operands into |
fa969310 AC |
2353 | -- result once an operand known to be non-null has been seen. |
2354 | ||
2355 | function Make_Artyp_Literal (Val : Nat) return Node_Id; | |
2356 | -- This function makes an N_Integer_Literal node that is returned in | |
2357 | -- analyzed form with the type set to Artyp. Importantly this literal | |
2358 | -- is not flagged as static, so that if we do computations with it that | |
2359 | -- result in statically detected out of range conditions, we will not | |
2360 | -- generate error messages but instead warning messages. | |
2361 | ||
46ff89f3 | 2362 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 2363 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
2364 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
2365 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
2366 | |
2367 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 2368 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 2369 | |
fa969310 AC |
2370 | ------------------------ |
2371 | -- Make_Artyp_Literal -- | |
2372 | ------------------------ | |
2373 | ||
2374 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
2375 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
2376 | begin | |
2377 | Set_Etype (Result, Artyp); | |
2378 | Set_Analyzed (Result, True); | |
2379 | Set_Is_Static_Expression (Result, False); | |
2380 | return Result; | |
2381 | end Make_Artyp_Literal; | |
76c597a1 | 2382 | |
fdac1f80 | 2383 | -------------- |
46ff89f3 | 2384 | -- To_Artyp -- |
fdac1f80 AC |
2385 | -------------- |
2386 | ||
46ff89f3 | 2387 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 2388 | begin |
46ff89f3 | 2389 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
2390 | return X; |
2391 | ||
2392 | elsif Is_Enumeration_Type (Ityp) then | |
2393 | return | |
2394 | Make_Attribute_Reference (Loc, | |
2395 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2396 | Attribute_Name => Name_Pos, | |
2397 | Expressions => New_List (X)); | |
2398 | ||
2399 | else | |
46ff89f3 | 2400 | return Convert_To (Artyp, X); |
fdac1f80 | 2401 | end if; |
46ff89f3 | 2402 | end To_Artyp; |
fdac1f80 AC |
2403 | |
2404 | ------------- | |
2405 | -- To_Ityp -- | |
2406 | ------------- | |
2407 | ||
2408 | function To_Ityp (X : Node_Id) return Node_Id is | |
2409 | begin | |
2fc05e3d | 2410 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
2411 | return |
2412 | Make_Attribute_Reference (Loc, | |
2413 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2414 | Attribute_Name => Name_Val, | |
2415 | Expressions => New_List (X)); | |
2416 | ||
2417 | -- Case where we will do a type conversion | |
2418 | ||
2419 | else | |
76c597a1 AC |
2420 | if Ityp = Base_Type (Artyp) then |
2421 | return X; | |
fdac1f80 | 2422 | else |
76c597a1 | 2423 | return Convert_To (Ityp, X); |
fdac1f80 AC |
2424 | end if; |
2425 | end if; | |
2426 | end To_Ityp; | |
2427 | ||
2428 | -- Local Declarations | |
2429 | ||
0ac73189 AC |
2430 | Opnd_Typ : Entity_Id; |
2431 | Ent : Entity_Id; | |
2432 | Len : Uint; | |
2433 | J : Nat; | |
2434 | Clen : Node_Id; | |
2435 | Set : Boolean; | |
70482933 RK |
2436 | |
2437 | begin | |
fdac1f80 AC |
2438 | -- Choose an appropriate computational type |
2439 | ||
2440 | -- We will be doing calculations of lengths and bounds in this routine | |
2441 | -- and computing one from the other in some cases, e.g. getting the high | |
2442 | -- bound by adding the length-1 to the low bound. | |
2443 | ||
2444 | -- We can't just use the index type, or even its base type for this | |
2445 | -- purpose for two reasons. First it might be an enumeration type which | |
308e6f3a RW |
2446 | -- is not suitable for computations of any kind, and second it may |
2447 | -- simply not have enough range. For example if the index type is | |
2448 | -- -128..+127 then lengths can be up to 256, which is out of range of | |
2449 | -- the type. | |
fdac1f80 AC |
2450 | |
2451 | -- For enumeration types, we can simply use Standard_Integer, this is | |
2452 | -- sufficient since the actual number of enumeration literals cannot | |
2453 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 2454 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
2455 | |
2456 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 2457 | Artyp := Standard_Integer; |
fdac1f80 | 2458 | |
59262ebb AC |
2459 | -- If index type is Positive, we use the standard unsigned type, to give |
2460 | -- more room on the top of the range, obviating the need for an overflow | |
2461 | -- check when creating the upper bound. This is needed to avoid junk | |
2462 | -- overflow checks in the common case of String types. | |
2463 | ||
2464 | -- ??? Disabled for now | |
2465 | ||
2466 | -- elsif Istyp = Standard_Positive then | |
2467 | -- Artyp := Standard_Unsigned; | |
2468 | ||
2fc05e3d AC |
2469 | -- For modular types, we use a 32-bit modular type for types whose size |
2470 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
2471 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 2472 | |
2fc05e3d | 2473 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 2474 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 2475 | Artyp := Standard_Unsigned; |
ecc4ddde | 2476 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 2477 | Artyp := Ityp; |
fdac1f80 | 2478 | else |
46ff89f3 | 2479 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
2480 | end if; |
2481 | ||
2fc05e3d | 2482 | -- Similar treatment for signed types |
fdac1f80 AC |
2483 | |
2484 | else | |
ecc4ddde | 2485 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 2486 | Artyp := Standard_Integer; |
ecc4ddde | 2487 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 2488 | Artyp := Ityp; |
fdac1f80 | 2489 | else |
46ff89f3 | 2490 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
2491 | end if; |
2492 | end if; | |
2493 | ||
fa969310 AC |
2494 | -- Supply dummy entry at start of length array |
2495 | ||
2496 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
2497 | ||
fdac1f80 | 2498 | -- Go through operands setting up the above arrays |
70482933 | 2499 | |
df46b832 AC |
2500 | J := 1; |
2501 | while J <= N loop | |
2502 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 2503 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
2504 | |
2505 | -- The parent got messed up when we put the operands in a list, | |
d347f572 AC |
2506 | -- so now put back the proper parent for the saved operand, that |
2507 | -- is to say the concatenation node, to make sure that each operand | |
2508 | -- is seen as a subexpression, e.g. if actions must be inserted. | |
fdac1f80 | 2509 | |
d347f572 | 2510 | Set_Parent (Opnd, Cnode); |
fdac1f80 AC |
2511 | |
2512 | -- Set will be True when we have setup one entry in the array | |
2513 | ||
df46b832 AC |
2514 | Set := False; |
2515 | ||
fdac1f80 | 2516 | -- Singleton element (or character literal) case |
df46b832 | 2517 | |
0ac73189 | 2518 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
2519 | NN := NN + 1; |
2520 | Operands (NN) := Opnd; | |
2521 | Is_Fixed_Length (NN) := True; | |
2522 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 2523 | Result_May_Be_Null := False; |
fdac1f80 | 2524 | |
a29262fd AC |
2525 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
2526 | -- since we know that the result cannot be null). | |
fdac1f80 | 2527 | |
0ac73189 AC |
2528 | Opnd_Low_Bound (NN) := |
2529 | Make_Attribute_Reference (Loc, | |
ecc4ddde | 2530 | Prefix => New_Reference_To (Istyp, Loc), |
0ac73189 AC |
2531 | Attribute_Name => Name_First); |
2532 | ||
df46b832 AC |
2533 | Set := True; |
2534 | ||
fdac1f80 | 2535 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
2536 | |
2537 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 2538 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 2539 | |
a29262fd AC |
2540 | if Len /= 0 then |
2541 | Result_May_Be_Null := False; | |
2542 | end if; | |
2543 | ||
2544 | -- Capture last operand high bound if result could be null | |
2545 | ||
2546 | if J = N and then Result_May_Be_Null then | |
2547 | Last_Opnd_High_Bound := | |
2548 | Make_Op_Add (Loc, | |
2549 | Left_Opnd => | |
2550 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 2551 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
2552 | end if; |
2553 | ||
2554 | -- Skip null string literal | |
fdac1f80 | 2555 | |
0ac73189 | 2556 | if J < N and then Len = 0 then |
df46b832 AC |
2557 | goto Continue; |
2558 | end if; | |
2559 | ||
2560 | NN := NN + 1; | |
2561 | Operands (NN) := Opnd; | |
2562 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
2563 | |
2564 | -- Set length and bounds | |
2565 | ||
df46b832 | 2566 | Fixed_Length (NN) := Len; |
0ac73189 AC |
2567 | |
2568 | Opnd_Low_Bound (NN) := | |
2569 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
2570 | ||
df46b832 AC |
2571 | Set := True; |
2572 | ||
2573 | -- All other cases | |
2574 | ||
2575 | else | |
2576 | -- Check constrained case with known bounds | |
2577 | ||
0ac73189 | 2578 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 2579 | declare |
df46b832 AC |
2580 | Index : constant Node_Id := First_Index (Opnd_Typ); |
2581 | Indx_Typ : constant Entity_Id := Etype (Index); | |
2582 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
2583 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
2584 | ||
2585 | begin | |
fdac1f80 AC |
2586 | -- Fixed length constrained array type with known at compile |
2587 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
2588 | |
2589 | if Compile_Time_Known_Value (Lo) | |
2590 | and then | |
2591 | Compile_Time_Known_Value (Hi) | |
2592 | then | |
2593 | declare | |
2594 | Loval : constant Uint := Expr_Value (Lo); | |
2595 | Hival : constant Uint := Expr_Value (Hi); | |
2596 | Len : constant Uint := | |
2597 | UI_Max (Hival - Loval + 1, Uint_0); | |
2598 | ||
2599 | begin | |
0ac73189 AC |
2600 | if Len > 0 then |
2601 | Result_May_Be_Null := False; | |
df46b832 | 2602 | end if; |
0ac73189 | 2603 | |
a29262fd AC |
2604 | -- Capture last operand bound if result could be null |
2605 | ||
2606 | if J = N and then Result_May_Be_Null then | |
2607 | Last_Opnd_High_Bound := | |
2608 | Convert_To (Ityp, | |
2609 | Make_Integer_Literal (Loc, | |
2610 | Intval => Expr_Value (Hi))); | |
2611 | end if; | |
2612 | ||
2613 | -- Exclude null length case unless last operand | |
0ac73189 | 2614 | |
a29262fd | 2615 | if J < N and then Len = 0 then |
0ac73189 AC |
2616 | goto Continue; |
2617 | end if; | |
2618 | ||
2619 | NN := NN + 1; | |
2620 | Operands (NN) := Opnd; | |
2621 | Is_Fixed_Length (NN) := True; | |
2622 | Fixed_Length (NN) := Len; | |
2623 | ||
a2dc5812 | 2624 | Opnd_Low_Bound (NN) := To_Ityp ( |
0ac73189 | 2625 | Make_Integer_Literal (Loc, |
a2dc5812 | 2626 | Intval => Expr_Value (Lo))); |
0ac73189 | 2627 | |
0ac73189 | 2628 | Set := True; |
df46b832 AC |
2629 | end; |
2630 | end if; | |
2631 | end; | |
2632 | end if; | |
2633 | ||
0ac73189 AC |
2634 | -- All cases where the length is not known at compile time, or the |
2635 | -- special case of an operand which is known to be null but has a | |
2636 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
2637 | |
2638 | if not Set then | |
2639 | NN := NN + 1; | |
0ac73189 AC |
2640 | |
2641 | -- Capture operand bounds | |
2642 | ||
2643 | Opnd_Low_Bound (NN) := | |
2644 | Make_Attribute_Reference (Loc, | |
2645 | Prefix => | |
2646 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2647 | Attribute_Name => Name_First); | |
2648 | ||
a29262fd AC |
2649 | if J = N and Result_May_Be_Null then |
2650 | Last_Opnd_High_Bound := | |
2651 | Convert_To (Ityp, | |
2652 | Make_Attribute_Reference (Loc, | |
2653 | Prefix => | |
2654 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2655 | Attribute_Name => Name_Last)); | |
2656 | end if; | |
0ac73189 AC |
2657 | |
2658 | -- Capture length of operand in entity | |
2659 | ||
df46b832 AC |
2660 | Operands (NN) := Opnd; |
2661 | Is_Fixed_Length (NN) := False; | |
2662 | ||
191fcb3a | 2663 | Var_Length (NN) := Make_Temporary (Loc, 'L'); |
df46b832 | 2664 | |
d0f8d157 | 2665 | Append_To (Actions, |
df46b832 AC |
2666 | Make_Object_Declaration (Loc, |
2667 | Defining_Identifier => Var_Length (NN), | |
2668 | Constant_Present => True, | |
2669 | ||
2670 | Object_Definition => | |
46ff89f3 | 2671 | New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2672 | |
2673 | Expression => | |
2674 | Make_Attribute_Reference (Loc, | |
2675 | Prefix => | |
2676 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
d0f8d157 | 2677 | Attribute_Name => Name_Length))); |
df46b832 AC |
2678 | end if; |
2679 | end if; | |
2680 | ||
2681 | -- Set next entry in aggregate length array | |
2682 | ||
2683 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 2684 | -- or a reference to the saved length for variable length. |
df46b832 AC |
2685 | |
2686 | if NN = 1 then | |
2687 | if Is_Fixed_Length (1) then | |
2688 | Aggr_Length (1) := | |
2689 | Make_Integer_Literal (Loc, | |
2690 | Intval => Fixed_Length (1)); | |
2691 | else | |
2692 | Aggr_Length (1) := | |
2693 | New_Reference_To (Var_Length (1), Loc); | |
2694 | end if; | |
2695 | ||
2696 | -- If entry is fixed length and only fixed lengths so far, make | |
2697 | -- appropriate new integer literal adding new length. | |
2698 | ||
2699 | elsif Is_Fixed_Length (NN) | |
2700 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
2701 | then | |
2702 | Aggr_Length (NN) := | |
2703 | Make_Integer_Literal (Loc, | |
2704 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
2705 | ||
d0f8d157 AC |
2706 | -- All other cases, construct an addition node for the length and |
2707 | -- create an entity initialized to this length. | |
df46b832 AC |
2708 | |
2709 | else | |
191fcb3a | 2710 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 AC |
2711 | |
2712 | if Is_Fixed_Length (NN) then | |
2713 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
2714 | else | |
2715 | Clen := New_Reference_To (Var_Length (NN), Loc); | |
2716 | end if; | |
2717 | ||
d0f8d157 | 2718 | Append_To (Actions, |
df46b832 AC |
2719 | Make_Object_Declaration (Loc, |
2720 | Defining_Identifier => Ent, | |
2721 | Constant_Present => True, | |
2722 | ||
2723 | Object_Definition => | |
46ff89f3 | 2724 | New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2725 | |
2726 | Expression => | |
2727 | Make_Op_Add (Loc, | |
2728 | Left_Opnd => New_Copy (Aggr_Length (NN - 1)), | |
d0f8d157 | 2729 | Right_Opnd => Clen))); |
df46b832 | 2730 | |
76c597a1 | 2731 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
2732 | end if; |
2733 | ||
2734 | <<Continue>> | |
2735 | J := J + 1; | |
2736 | end loop; | |
2737 | ||
a29262fd | 2738 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
2739 | |
2740 | if NN = 0 then | |
a29262fd | 2741 | Result := Opnd; |
df46b832 AC |
2742 | goto Done; |
2743 | end if; | |
2744 | ||
2745 | -- If we have only one non-null operand, return it and we are done. | |
2746 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
2747 | -- the sole operand is of the element type, in which case it must be |
2748 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
2749 | -- through the normal general circuit. |
2750 | ||
2751 | if NN = 1 | |
fdac1f80 | 2752 | and then Base_Type (Etype (Operands (1))) /= Ctyp |
df46b832 AC |
2753 | then |
2754 | Result := Operands (1); | |
2755 | goto Done; | |
2756 | end if; | |
2757 | ||
2758 | -- Cases where we have a real concatenation | |
2759 | ||
fdac1f80 AC |
2760 | -- Next step is to find the low bound for the result array that we |
2761 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
2762 | ||
2763 | -- If the ultimate ancestor of the index subtype is a constrained array | |
2764 | -- definition, then the lower bound is that of the index subtype as | |
2765 | -- specified by (RM 4.5.3(6)). | |
2766 | ||
2767 | -- The right test here is to go to the root type, and then the ultimate | |
2768 | -- ancestor is the first subtype of this root type. | |
2769 | ||
2770 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 2771 | Low_Bound := |
fdac1f80 AC |
2772 | Make_Attribute_Reference (Loc, |
2773 | Prefix => | |
2774 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 2775 | Attribute_Name => Name_First); |
df46b832 AC |
2776 | |
2777 | -- If the first operand in the list has known length we know that | |
2778 | -- the lower bound of the result is the lower bound of this operand. | |
2779 | ||
fdac1f80 | 2780 | elsif Is_Fixed_Length (1) then |
0ac73189 | 2781 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
2782 | |
2783 | -- OK, we don't know the lower bound, we have to build a horrible | |
2784 | -- expression actions node of the form | |
2785 | ||
2786 | -- if Cond1'Length /= 0 then | |
0ac73189 | 2787 | -- Opnd1 low bound |
df46b832 AC |
2788 | -- else |
2789 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 2790 | -- Opnd2 low bound |
df46b832 AC |
2791 | -- else |
2792 | -- ... | |
2793 | ||
2794 | -- The nesting ends either when we hit an operand whose length is known | |
2795 | -- at compile time, or on reaching the last operand, whose low bound we | |
2796 | -- take unconditionally whether or not it is null. It's easiest to do | |
2797 | -- this with a recursive procedure: | |
2798 | ||
2799 | else | |
2800 | declare | |
2801 | function Get_Known_Bound (J : Nat) return Node_Id; | |
2802 | -- Returns the lower bound determined by operands J .. NN | |
2803 | ||
2804 | --------------------- | |
2805 | -- Get_Known_Bound -- | |
2806 | --------------------- | |
2807 | ||
2808 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 2809 | begin |
0ac73189 AC |
2810 | if Is_Fixed_Length (J) or else J = NN then |
2811 | return New_Copy (Opnd_Low_Bound (J)); | |
70482933 RK |
2812 | |
2813 | else | |
df46b832 AC |
2814 | return |
2815 | Make_Conditional_Expression (Loc, | |
2816 | Expressions => New_List ( | |
2817 | ||
2818 | Make_Op_Ne (Loc, | |
2819 | Left_Opnd => New_Reference_To (Var_Length (J), Loc), | |
2820 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
2821 | ||
0ac73189 | 2822 | New_Copy (Opnd_Low_Bound (J)), |
df46b832 | 2823 | Get_Known_Bound (J + 1))); |
70482933 | 2824 | end if; |
df46b832 | 2825 | end Get_Known_Bound; |
70482933 | 2826 | |
df46b832 | 2827 | begin |
191fcb3a | 2828 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 | 2829 | |
d0f8d157 | 2830 | Append_To (Actions, |
df46b832 AC |
2831 | Make_Object_Declaration (Loc, |
2832 | Defining_Identifier => Ent, | |
2833 | Constant_Present => True, | |
0ac73189 | 2834 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
d0f8d157 | 2835 | Expression => Get_Known_Bound (1))); |
df46b832 AC |
2836 | |
2837 | Low_Bound := New_Reference_To (Ent, Loc); | |
2838 | end; | |
2839 | end if; | |
70482933 | 2840 | |
76c597a1 AC |
2841 | -- Now we can safely compute the upper bound, normally |
2842 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
2843 | |
2844 | High_Bound := | |
2845 | To_Ityp ( | |
2846 | Make_Op_Add (Loc, | |
46ff89f3 | 2847 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
0ac73189 AC |
2848 | Right_Opnd => |
2849 | Make_Op_Subtract (Loc, | |
2850 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 2851 | Right_Opnd => Make_Artyp_Literal (1)))); |
0ac73189 | 2852 | |
59262ebb | 2853 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
2854 | -- very weird cases, so in the general case we need an overflow check on |
2855 | -- the high bound. We can avoid this for the common case of string types | |
2856 | -- and other types whose index is Positive, since we chose a wider range | |
2857 | -- for the arithmetic type. | |
76c597a1 | 2858 | |
59262ebb AC |
2859 | if Istyp /= Standard_Positive then |
2860 | Activate_Overflow_Check (High_Bound); | |
2861 | end if; | |
76c597a1 AC |
2862 | |
2863 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
2864 | -- case the bounds come from the last operand (so that we get the proper |
2865 | -- bounds if the last operand is super-flat). | |
2866 | ||
0ac73189 AC |
2867 | if Result_May_Be_Null then |
2868 | High_Bound := | |
2869 | Make_Conditional_Expression (Loc, | |
2870 | Expressions => New_List ( | |
2871 | Make_Op_Eq (Loc, | |
2872 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 2873 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 2874 | Last_Opnd_High_Bound, |
0ac73189 AC |
2875 | High_Bound)); |
2876 | end if; | |
2877 | ||
d0f8d157 AC |
2878 | -- Here is where we insert the saved up actions |
2879 | ||
2880 | Insert_Actions (Cnode, Actions, Suppress => All_Checks); | |
2881 | ||
602a7ec0 AC |
2882 | -- Now we construct an array object with appropriate bounds. We mark |
2883 | -- the target as internal to prevent useless initialization when | |
e526d0c7 AC |
2884 | -- Initialize_Scalars is enabled. Also since this is the actual result |
2885 | -- entity, we make sure we have debug information for the result. | |
70482933 | 2886 | |
191fcb3a | 2887 | Ent := Make_Temporary (Loc, 'S'); |
008f6fd3 | 2888 | Set_Is_Internal (Ent); |
e526d0c7 | 2889 | Set_Needs_Debug_Info (Ent); |
70482933 | 2890 | |
76c597a1 | 2891 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
2892 | -- to abort, we want a warning and a runtime constraint error. Note that |
2893 | -- we have arranged that the result will not be treated as a static | |
2894 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 2895 | |
df46b832 AC |
2896 | Insert_Action (Cnode, |
2897 | Make_Object_Declaration (Loc, | |
2898 | Defining_Identifier => Ent, | |
df46b832 AC |
2899 | Object_Definition => |
2900 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 2901 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
2902 | Constraint => |
2903 | Make_Index_Or_Discriminant_Constraint (Loc, | |
2904 | Constraints => New_List ( | |
2905 | Make_Range (Loc, | |
0ac73189 AC |
2906 | Low_Bound => Low_Bound, |
2907 | High_Bound => High_Bound))))), | |
df46b832 AC |
2908 | Suppress => All_Checks); |
2909 | ||
d1f453b7 RD |
2910 | -- If the result of the concatenation appears as the initializing |
2911 | -- expression of an object declaration, we can just rename the | |
2912 | -- result, rather than copying it. | |
2913 | ||
2914 | Set_OK_To_Rename (Ent); | |
2915 | ||
76c597a1 AC |
2916 | -- Catch the static out of range case now |
2917 | ||
2918 | if Raises_Constraint_Error (High_Bound) then | |
2919 | raise Concatenation_Error; | |
2920 | end if; | |
2921 | ||
df46b832 AC |
2922 | -- Now we will generate the assignments to do the actual concatenation |
2923 | ||
bded454f RD |
2924 | -- There is one case in which we will not do this, namely when all the |
2925 | -- following conditions are met: | |
2926 | ||
2927 | -- The result type is Standard.String | |
2928 | ||
2929 | -- There are nine or fewer retained (non-null) operands | |
2930 | ||
ffec8e81 | 2931 | -- The optimization level is -O0 |
bded454f RD |
2932 | |
2933 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
2934 | -- available in the run time. | |
2935 | ||
2936 | -- The debug flag gnatd.c is not set | |
2937 | ||
2938 | -- If all these conditions are met then we generate a call to the | |
2939 | -- relevant concatenation routine. The purpose of this is to avoid | |
2940 | -- undesirable code bloat at -O0. | |
2941 | ||
2942 | if Atyp = Standard_String | |
2943 | and then NN in 2 .. 9 | |
ffec8e81 | 2944 | and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC) |
bded454f RD |
2945 | and then not Debug_Flag_Dot_C |
2946 | then | |
2947 | declare | |
2948 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
2949 | (RE_Str_Concat_2, | |
2950 | RE_Str_Concat_3, | |
2951 | RE_Str_Concat_4, | |
2952 | RE_Str_Concat_5, | |
2953 | RE_Str_Concat_6, | |
2954 | RE_Str_Concat_7, | |
2955 | RE_Str_Concat_8, | |
2956 | RE_Str_Concat_9); | |
2957 | ||
2958 | begin | |
2959 | if RTE_Available (RR (NN)) then | |
2960 | declare | |
2961 | Opnds : constant List_Id := | |
2962 | New_List (New_Occurrence_Of (Ent, Loc)); | |
2963 | ||
2964 | begin | |
2965 | for J in 1 .. NN loop | |
2966 | if Is_List_Member (Operands (J)) then | |
2967 | Remove (Operands (J)); | |
2968 | end if; | |
2969 | ||
2970 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
2971 | Append_To (Opnds, | |
2972 | Make_Aggregate (Loc, | |
2973 | Component_Associations => New_List ( | |
2974 | Make_Component_Association (Loc, | |
2975 | Choices => New_List ( | |
2976 | Make_Integer_Literal (Loc, 1)), | |
2977 | Expression => Operands (J))))); | |
2978 | ||
2979 | else | |
2980 | Append_To (Opnds, Operands (J)); | |
2981 | end if; | |
2982 | end loop; | |
2983 | ||
2984 | Insert_Action (Cnode, | |
2985 | Make_Procedure_Call_Statement (Loc, | |
2986 | Name => New_Reference_To (RTE (RR (NN)), Loc), | |
2987 | Parameter_Associations => Opnds)); | |
2988 | ||
2989 | Result := New_Reference_To (Ent, Loc); | |
2990 | goto Done; | |
2991 | end; | |
2992 | end if; | |
2993 | end; | |
2994 | end if; | |
2995 | ||
2996 | -- Not special case so generate the assignments | |
2997 | ||
76c597a1 AC |
2998 | Known_Non_Null_Operand_Seen := False; |
2999 | ||
df46b832 AC |
3000 | for J in 1 .. NN loop |
3001 | declare | |
3002 | Lo : constant Node_Id := | |
3003 | Make_Op_Add (Loc, | |
46ff89f3 | 3004 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3005 | Right_Opnd => Aggr_Length (J - 1)); |
3006 | ||
3007 | Hi : constant Node_Id := | |
3008 | Make_Op_Add (Loc, | |
46ff89f3 | 3009 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
3010 | Right_Opnd => |
3011 | Make_Op_Subtract (Loc, | |
3012 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 3013 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 3014 | |
df46b832 | 3015 | begin |
fdac1f80 AC |
3016 | -- Singleton case, simple assignment |
3017 | ||
3018 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 3019 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
3020 | Insert_Action (Cnode, |
3021 | Make_Assignment_Statement (Loc, | |
3022 | Name => | |
3023 | Make_Indexed_Component (Loc, | |
3024 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 3025 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
3026 | Expression => Operands (J)), |
3027 | Suppress => All_Checks); | |
70482933 | 3028 | |
76c597a1 AC |
3029 | -- Array case, slice assignment, skipped when argument is fixed |
3030 | -- length and known to be null. | |
fdac1f80 | 3031 | |
76c597a1 AC |
3032 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
3033 | declare | |
3034 | Assign : Node_Id := | |
3035 | Make_Assignment_Statement (Loc, | |
3036 | Name => | |
3037 | Make_Slice (Loc, | |
3038 | Prefix => | |
3039 | New_Occurrence_Of (Ent, Loc), | |
3040 | Discrete_Range => | |
3041 | Make_Range (Loc, | |
3042 | Low_Bound => To_Ityp (Lo), | |
3043 | High_Bound => To_Ityp (Hi))), | |
3044 | Expression => Operands (J)); | |
3045 | begin | |
3046 | if Is_Fixed_Length (J) then | |
3047 | Known_Non_Null_Operand_Seen := True; | |
3048 | ||
3049 | elsif not Known_Non_Null_Operand_Seen then | |
3050 | ||
3051 | -- Here if operand length is not statically known and no | |
3052 | -- operand known to be non-null has been processed yet. | |
3053 | -- If operand length is 0, we do not need to perform the | |
3054 | -- assignment, and we must avoid the evaluation of the | |
3055 | -- high bound of the slice, since it may underflow if the | |
3056 | -- low bound is Ityp'First. | |
3057 | ||
3058 | Assign := | |
3059 | Make_Implicit_If_Statement (Cnode, | |
3060 | Condition => | |
3061 | Make_Op_Ne (Loc, | |
3062 | Left_Opnd => | |
3063 | New_Occurrence_Of (Var_Length (J), Loc), | |
3064 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
3065 | Then_Statements => | |
3066 | New_List (Assign)); | |
3067 | end if; | |
fa969310 | 3068 | |
76c597a1 AC |
3069 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
3070 | end; | |
df46b832 AC |
3071 | end if; |
3072 | end; | |
3073 | end loop; | |
70482933 | 3074 | |
0ac73189 AC |
3075 | -- Finally we build the result, which is a reference to the array object |
3076 | ||
df46b832 | 3077 | Result := New_Reference_To (Ent, Loc); |
70482933 | 3078 | |
df46b832 AC |
3079 | <<Done>> |
3080 | Rewrite (Cnode, Result); | |
fdac1f80 AC |
3081 | Analyze_And_Resolve (Cnode, Atyp); |
3082 | ||
3083 | exception | |
3084 | when Concatenation_Error => | |
76c597a1 AC |
3085 | |
3086 | -- Kill warning generated for the declaration of the static out of | |
3087 | -- range high bound, and instead generate a Constraint_Error with | |
3088 | -- an appropriate specific message. | |
3089 | ||
3090 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
3091 | Apply_Compile_Time_Constraint_Error | |
3092 | (N => Cnode, | |
3093 | Msg => "concatenation result upper bound out of range?", | |
3094 | Reason => CE_Range_Check_Failed); | |
3095 | -- Set_Etype (Cnode, Atyp); | |
fdac1f80 | 3096 | end Expand_Concatenate; |
70482933 RK |
3097 | |
3098 | ------------------------ | |
3099 | -- Expand_N_Allocator -- | |
3100 | ------------------------ | |
3101 | ||
3102 | procedure Expand_N_Allocator (N : Node_Id) is | |
3103 | PtrT : constant Entity_Id := Etype (N); | |
d6a24cdb | 3104 | Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT)); |
f82944b7 | 3105 | Etyp : constant Entity_Id := Etype (Expression (N)); |
70482933 | 3106 | Loc : constant Source_Ptr := Sloc (N); |
f82944b7 | 3107 | Desig : Entity_Id; |
70482933 | 3108 | Temp : Entity_Id; |
26bff3d9 | 3109 | Nod : Node_Id; |
70482933 | 3110 | |
26bff3d9 JM |
3111 | procedure Complete_Coextension_Finalization; |
3112 | -- Generate finalization calls for all nested coextensions of N. This | |
3113 | -- routine may allocate list controllers if necessary. | |
0669bebe | 3114 | |
26bff3d9 JM |
3115 | procedure Rewrite_Coextension (N : Node_Id); |
3116 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 3117 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 3118 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 3119 | |
8aec446b | 3120 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd AC |
3121 | -- Given a constrained array type E, returns a node representing the |
3122 | -- code to compute the size in storage elements for the given type. | |
205c14b0 | 3123 | -- This is done without using the attribute (which malfunctions for |
507ed3fd | 3124 | -- large sizes ???) |
8aec446b | 3125 | |
26bff3d9 JM |
3126 | --------------------------------------- |
3127 | -- Complete_Coextension_Finalization -- | |
3128 | --------------------------------------- | |
0669bebe | 3129 | |
26bff3d9 JM |
3130 | procedure Complete_Coextension_Finalization is |
3131 | Coext : Node_Id; | |
3132 | Coext_Elmt : Elmt_Id; | |
3133 | Flist : Node_Id; | |
3134 | Ref : Node_Id; | |
0669bebe | 3135 | |
26bff3d9 JM |
3136 | function Inside_A_Return_Statement (N : Node_Id) return Boolean; |
3137 | -- Determine whether node N is part of a return statement | |
3138 | ||
3139 | function Needs_Initialization_Call (N : Node_Id) return Boolean; | |
3140 | -- Determine whether node N is a subtype indicator allocator which | |
b4592168 | 3141 | -- acts a coextension. Such coextensions need initialization. |
26bff3d9 JM |
3142 | |
3143 | ------------------------------- | |
3144 | -- Inside_A_Return_Statement -- | |
3145 | ------------------------------- | |
3146 | ||
3147 | function Inside_A_Return_Statement (N : Node_Id) return Boolean is | |
3148 | P : Node_Id; | |
3149 | ||
3150 | begin | |
3151 | P := Parent (N); | |
3152 | while Present (P) loop | |
303b4d58 AC |
3153 | if Nkind_In |
3154 | (P, N_Extended_Return_Statement, N_Simple_Return_Statement) | |
26bff3d9 JM |
3155 | then |
3156 | return True; | |
3157 | ||
3158 | -- Stop the traversal when we reach a subprogram body | |
3159 | ||
3160 | elsif Nkind (P) = N_Subprogram_Body then | |
3161 | return False; | |
3162 | end if; | |
3163 | ||
3164 | P := Parent (P); | |
3165 | end loop; | |
3166 | ||
3167 | return False; | |
3168 | end Inside_A_Return_Statement; | |
3169 | ||
3170 | ------------------------------- | |
3171 | -- Needs_Initialization_Call -- | |
3172 | ------------------------------- | |
3173 | ||
3174 | function Needs_Initialization_Call (N : Node_Id) return Boolean is | |
3175 | Obj_Decl : Node_Id; | |
3176 | ||
3177 | begin | |
3178 | if Nkind (N) = N_Explicit_Dereference | |
3179 | and then Nkind (Prefix (N)) = N_Identifier | |
3180 | and then Nkind (Parent (Entity (Prefix (N)))) = | |
3181 | N_Object_Declaration | |
3182 | then | |
3183 | Obj_Decl := Parent (Entity (Prefix (N))); | |
0669bebe | 3184 | |
26bff3d9 JM |
3185 | return |
3186 | Present (Expression (Obj_Decl)) | |
3187 | and then Nkind (Expression (Obj_Decl)) = N_Allocator | |
3188 | and then Nkind (Expression (Expression (Obj_Decl))) /= | |
3189 | N_Qualified_Expression; | |
0669bebe GB |
3190 | end if; |
3191 | ||
26bff3d9 JM |
3192 | return False; |
3193 | end Needs_Initialization_Call; | |
3194 | ||
3195 | -- Start of processing for Complete_Coextension_Finalization | |
3196 | ||
3197 | begin | |
3198 | -- When a coextension root is inside a return statement, we need to | |
3199 | -- use the finalization chain of the function's scope. This does not | |
3200 | -- apply for controlled named access types because in those cases we | |
3201 | -- can use the finalization chain of the type itself. | |
3202 | ||
3203 | if Inside_A_Return_Statement (N) | |
3204 | and then | |
3205 | (Ekind (PtrT) = E_Anonymous_Access_Type | |
0580d807 AC |
3206 | or else |
3207 | (Ekind (PtrT) = E_Access_Type | |
3208 | and then No (Associated_Final_Chain (PtrT)))) | |
26bff3d9 | 3209 | then |
0669bebe | 3210 | declare |
26bff3d9 JM |
3211 | Decl : Node_Id; |
3212 | Outer_S : Entity_Id; | |
13d923cc | 3213 | S : Entity_Id; |
0669bebe GB |
3214 | |
3215 | begin | |
13d923cc | 3216 | S := Current_Scope; |
26bff3d9 JM |
3217 | while Present (S) and then S /= Standard_Standard loop |
3218 | if Ekind (S) = E_Function then | |
3219 | Outer_S := Scope (S); | |
3220 | ||
3221 | -- Retrieve the declaration of the body | |
3222 | ||
8aec446b AC |
3223 | Decl := |
3224 | Parent | |
3225 | (Parent | |
3226 | (Corresponding_Body (Parent (Parent (S))))); | |
26bff3d9 JM |
3227 | exit; |
3228 | end if; | |
3229 | ||
3230 | S := Scope (S); | |
0669bebe GB |
3231 | end loop; |
3232 | ||
26bff3d9 JM |
3233 | -- Push the scope of the function body since we are inserting |
3234 | -- the list before the body, but we are currently in the body | |
3235 | -- itself. Override the finalization list of PtrT since the | |
3236 | -- finalization context is now different. | |
3237 | ||
3238 | Push_Scope (Outer_S); | |
3239 | Build_Final_List (Decl, PtrT); | |
3240 | Pop_Scope; | |
0669bebe GB |
3241 | end; |
3242 | ||
26bff3d9 JM |
3243 | -- The root allocator may not be controlled, but it still needs a |
3244 | -- finalization list for all nested coextensions. | |
0669bebe | 3245 | |
26bff3d9 JM |
3246 | elsif No (Associated_Final_Chain (PtrT)) then |
3247 | Build_Final_List (N, PtrT); | |
3248 | end if; | |
0669bebe | 3249 | |
26bff3d9 JM |
3250 | Flist := |
3251 | Make_Selected_Component (Loc, | |
7675ad4f | 3252 | Prefix => |
26bff3d9 | 3253 | New_Reference_To (Associated_Final_Chain (PtrT), Loc), |
7675ad4f | 3254 | Selector_Name => Make_Identifier (Loc, Name_F)); |
26bff3d9 JM |
3255 | |
3256 | Coext_Elmt := First_Elmt (Coextensions (N)); | |
3257 | while Present (Coext_Elmt) loop | |
3258 | Coext := Node (Coext_Elmt); | |
3259 | ||
3260 | -- Generate: | |
3261 | -- typ! (coext.all) | |
3262 | ||
3263 | if Nkind (Coext) = N_Identifier then | |
685094bf RD |
3264 | Ref := |
3265 | Make_Unchecked_Type_Conversion (Loc, | |
3266 | Subtype_Mark => New_Reference_To (Etype (Coext), Loc), | |
3267 | Expression => | |
3268 | Make_Explicit_Dereference (Loc, | |
3269 | Prefix => New_Copy_Tree (Coext))); | |
26bff3d9 JM |
3270 | else |
3271 | Ref := New_Copy_Tree (Coext); | |
3272 | end if; | |
0669bebe | 3273 | |
b4592168 | 3274 | -- No initialization call if not allowed |
26bff3d9 | 3275 | |
b4592168 | 3276 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 | 3277 | |
b4592168 | 3278 | if not Restriction_Active (No_Default_Initialization) then |
26bff3d9 | 3279 | |
b4592168 GD |
3280 | -- Generate: |
3281 | -- initialize (Ref) | |
3282 | -- attach_to_final_list (Ref, Flist, 2) | |
3283 | ||
3284 | if Needs_Initialization_Call (Coext) then | |
3285 | Insert_Actions (N, | |
3286 | Make_Init_Call ( | |
3287 | Ref => Ref, | |
3288 | Typ => Etype (Coext), | |
3289 | Flist_Ref => Flist, | |
3290 | With_Attach => Make_Integer_Literal (Loc, Uint_2))); | |
3291 | ||
3292 | -- Generate: | |
3293 | -- attach_to_final_list (Ref, Flist, 2) | |
3294 | ||
3295 | else | |
3296 | Insert_Action (N, | |
3297 | Make_Attach_Call ( | |
3298 | Obj_Ref => Ref, | |
3299 | Flist_Ref => New_Copy_Tree (Flist), | |
3300 | With_Attach => Make_Integer_Literal (Loc, Uint_2))); | |
3301 | end if; | |
26bff3d9 JM |
3302 | end if; |
3303 | ||
3304 | Next_Elmt (Coext_Elmt); | |
3305 | end loop; | |
3306 | end Complete_Coextension_Finalization; | |
3307 | ||
3308 | ------------------------- | |
3309 | -- Rewrite_Coextension -- | |
3310 | ------------------------- | |
3311 | ||
3312 | procedure Rewrite_Coextension (N : Node_Id) is | |
191fcb3a | 3313 | Temp : constant Node_Id := Make_Temporary (Loc, 'C'); |
26bff3d9 JM |
3314 | |
3315 | -- Generate: | |
3316 | -- Cnn : aliased Etyp; | |
3317 | ||
3318 | Decl : constant Node_Id := | |
3319 | Make_Object_Declaration (Loc, | |
3320 | Defining_Identifier => Temp, | |
3321 | Aliased_Present => True, | |
3322 | Object_Definition => | |
3323 | New_Occurrence_Of (Etyp, Loc)); | |
3324 | Nod : Node_Id; | |
3325 | ||
3326 | begin | |
3327 | if Nkind (Expression (N)) = N_Qualified_Expression then | |
3328 | Set_Expression (Decl, Expression (Expression (N))); | |
0669bebe | 3329 | end if; |
26bff3d9 JM |
3330 | |
3331 | -- Find the proper insertion node for the declaration | |
3332 | ||
3333 | Nod := Parent (N); | |
3334 | while Present (Nod) loop | |
3335 | exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call | |
3336 | or else Nkind (Nod) = N_Procedure_Call_Statement | |
3337 | or else Nkind (Nod) in N_Declaration; | |
3338 | Nod := Parent (Nod); | |
3339 | end loop; | |
3340 | ||
3341 | Insert_Before (Nod, Decl); | |
3342 | Analyze (Decl); | |
3343 | ||
3344 | Rewrite (N, | |
3345 | Make_Attribute_Reference (Loc, | |
3346 | Prefix => New_Occurrence_Of (Temp, Loc), | |
3347 | Attribute_Name => Name_Unrestricted_Access)); | |
3348 | ||
3349 | Analyze_And_Resolve (N, PtrT); | |
3350 | end Rewrite_Coextension; | |
0669bebe | 3351 | |
8aec446b AC |
3352 | ------------------------------ |
3353 | -- Size_In_Storage_Elements -- | |
3354 | ------------------------------ | |
3355 | ||
3356 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
3357 | begin | |
3358 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
3359 | -- However, the reason for the existence of this function is | |
3360 | -- to construct a test for sizes too large, which means near the | |
3361 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
3362 | -- is that we get overflows when sizes are greater than 2**31. | |
3363 | ||
507ed3fd | 3364 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
3365 | |
3366 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
3367 | ||
46202729 | 3368 | -- which avoids this problem. All this is a bit bogus, but it does |
8aec446b AC |
3369 | -- mean we catch common cases of trying to allocate arrays that |
3370 | -- are too large, and which in the absence of a check results in | |
3371 | -- undetected chaos ??? | |
3372 | ||
507ed3fd AC |
3373 | declare |
3374 | Len : Node_Id; | |
3375 | Res : Node_Id; | |
8aec446b | 3376 | |
507ed3fd AC |
3377 | begin |
3378 | for J in 1 .. Number_Dimensions (E) loop | |
3379 | Len := | |
3380 | Make_Attribute_Reference (Loc, | |
3381 | Prefix => New_Occurrence_Of (E, Loc), | |
3382 | Attribute_Name => Name_Length, | |
3383 | Expressions => New_List ( | |
3384 | Make_Integer_Literal (Loc, J))); | |
8aec446b | 3385 | |
507ed3fd AC |
3386 | if J = 1 then |
3387 | Res := Len; | |
8aec446b | 3388 | |
507ed3fd AC |
3389 | else |
3390 | Res := | |
3391 | Make_Op_Multiply (Loc, | |
3392 | Left_Opnd => Res, | |
3393 | Right_Opnd => Len); | |
3394 | end if; | |
3395 | end loop; | |
8aec446b | 3396 | |
8aec446b | 3397 | return |
507ed3fd AC |
3398 | Make_Op_Multiply (Loc, |
3399 | Left_Opnd => Len, | |
3400 | Right_Opnd => | |
3401 | Make_Attribute_Reference (Loc, | |
3402 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
3403 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
3404 | end; | |
8aec446b AC |
3405 | end Size_In_Storage_Elements; |
3406 | ||
0669bebe GB |
3407 | -- Start of processing for Expand_N_Allocator |
3408 | ||
70482933 RK |
3409 | begin |
3410 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
3411 | -- shall not be a remote access-to-class-wide-limited-private type | |
3412 | ||
3413 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
3414 | ||
3415 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
3416 | ||
3417 | -- Set the Storage Pool | |
3418 | ||
3419 | Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT))); | |
3420 | ||
3421 | if Present (Storage_Pool (N)) then | |
3422 | if Is_RTE (Storage_Pool (N), RE_SS_Pool) then | |
26bff3d9 | 3423 | if VM_Target = No_VM then |
70482933 RK |
3424 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); |
3425 | end if; | |
fbf5a39b AC |
3426 | |
3427 | elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then | |
3428 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
3429 | ||
70482933 RK |
3430 | else |
3431 | Set_Procedure_To_Call (N, | |
3432 | Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate)); | |
3433 | end if; | |
3434 | end if; | |
3435 | ||
685094bf RD |
3436 | -- Under certain circumstances we can replace an allocator by an access |
3437 | -- to statically allocated storage. The conditions, as noted in AARM | |
3438 | -- 3.10 (10c) are as follows: | |
70482933 RK |
3439 | |
3440 | -- Size and initial value is known at compile time | |
3441 | -- Access type is access-to-constant | |
3442 | ||
fbf5a39b AC |
3443 | -- The allocator is not part of a constraint on a record component, |
3444 | -- because in that case the inserted actions are delayed until the | |
3445 | -- record declaration is fully analyzed, which is too late for the | |
3446 | -- analysis of the rewritten allocator. | |
3447 | ||
70482933 RK |
3448 | if Is_Access_Constant (PtrT) |
3449 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
3450 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
3451 | and then Size_Known_At_Compile_Time (Etype (Expression | |
3452 | (Expression (N)))) | |
fbf5a39b | 3453 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
3454 | then |
3455 | -- Here we can do the optimization. For the allocator | |
3456 | ||
3457 | -- new x'(y) | |
3458 | ||
3459 | -- We insert an object declaration | |
3460 | ||
3461 | -- Tnn : aliased x := y; | |
3462 | ||
685094bf RD |
3463 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
3464 | -- marked as requiring static allocation. | |
70482933 | 3465 | |
191fcb3a | 3466 | Temp := Make_Temporary (Loc, 'T', Expression (Expression (N))); |
70482933 RK |
3467 | Desig := Subtype_Mark (Expression (N)); |
3468 | ||
3469 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 3470 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
3471 | -- unconstrained subtype. |
3472 | ||
0da2c8ac AC |
3473 | if Entity (Desig) = Base_Type (Dtyp) then |
3474 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
3475 | end if; |
3476 | ||
3477 | Insert_Action (N, | |
3478 | Make_Object_Declaration (Loc, | |
3479 | Defining_Identifier => Temp, | |
3480 | Aliased_Present => True, | |
3481 | Constant_Present => Is_Access_Constant (PtrT), | |
3482 | Object_Definition => Desig, | |
3483 | Expression => Expression (Expression (N)))); | |
3484 | ||
3485 | Rewrite (N, | |
3486 | Make_Attribute_Reference (Loc, | |
3487 | Prefix => New_Occurrence_Of (Temp, Loc), | |
3488 | Attribute_Name => Name_Unrestricted_Access)); | |
3489 | ||
3490 | Analyze_And_Resolve (N, PtrT); | |
3491 | ||
685094bf RD |
3492 | -- We set the variable as statically allocated, since we don't want |
3493 | -- it going on the stack of the current procedure! | |
70482933 RK |
3494 | |
3495 | Set_Is_Statically_Allocated (Temp); | |
3496 | return; | |
3497 | end if; | |
3498 | ||
0669bebe GB |
3499 | -- Same if the allocator is an access discriminant for a local object: |
3500 | -- instead of an allocator we create a local value and constrain the | |
308e6f3a | 3501 | -- enclosing object with the corresponding access attribute. |
0669bebe | 3502 | |
26bff3d9 JM |
3503 | if Is_Static_Coextension (N) then |
3504 | Rewrite_Coextension (N); | |
0669bebe GB |
3505 | return; |
3506 | end if; | |
3507 | ||
26bff3d9 JM |
3508 | -- The current allocator creates an object which may contain nested |
3509 | -- coextensions. Use the current allocator's finalization list to | |
3510 | -- generate finalization call for all nested coextensions. | |
3511 | ||
3512 | if Is_Coextension_Root (N) then | |
3513 | Complete_Coextension_Finalization; | |
3514 | end if; | |
3515 | ||
8aec446b AC |
3516 | -- Check for size too large, we do this because the back end misses |
3517 | -- proper checks here and can generate rubbish allocation calls when | |
3518 | -- we are near the limit. We only do this for the 32-bit address case | |
3519 | -- since that is from a practical point of view where we see a problem. | |
3520 | ||
3521 | if System_Address_Size = 32 | |
3522 | and then not Storage_Checks_Suppressed (PtrT) | |
3523 | and then not Storage_Checks_Suppressed (Dtyp) | |
3524 | and then not Storage_Checks_Suppressed (Etyp) | |
3525 | then | |
3526 | -- The check we want to generate should look like | |
3527 | ||
3528 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
3529 | -- raise Storage_Error; | |
3530 | -- end if; | |
3531 | ||
308e6f3a | 3532 | -- where 3.5 gigabytes is a constant large enough to accommodate any |
507ed3fd AC |
3533 | -- reasonable request for. But we can't do it this way because at |
3534 | -- least at the moment we don't compute this attribute right, and | |
3535 | -- can silently give wrong results when the result gets large. Since | |
3536 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 3537 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 3538 | -- value of the attribute ??? |
8aec446b | 3539 | |
507ed3fd AC |
3540 | if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then |
3541 | Insert_Action (N, | |
3542 | Make_Raise_Storage_Error (Loc, | |
3543 | Condition => | |
3544 | Make_Op_Gt (Loc, | |
3545 | Left_Opnd => Size_In_Storage_Elements (Etyp), | |
3546 | Right_Opnd => | |
3547 | Make_Integer_Literal (Loc, | |
3548 | Intval => Uint_7 * (Uint_2 ** 29))), | |
3549 | Reason => SE_Object_Too_Large)); | |
3550 | end if; | |
8aec446b AC |
3551 | end if; |
3552 | ||
0da2c8ac | 3553 | -- Handle case of qualified expression (other than optimization above) |
cac5a801 AC |
3554 | -- First apply constraint checks, because the bounds or discriminants |
3555 | -- in the aggregate might not match the subtype mark in the allocator. | |
0da2c8ac | 3556 | |
70482933 | 3557 | if Nkind (Expression (N)) = N_Qualified_Expression then |
cac5a801 AC |
3558 | Apply_Constraint_Check |
3559 | (Expression (Expression (N)), Etype (Expression (N))); | |
3560 | ||
fbf5a39b | 3561 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
3562 | return; |
3563 | end if; | |
fbf5a39b | 3564 | |
26bff3d9 JM |
3565 | -- If the allocator is for a type which requires initialization, and |
3566 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
3567 | -- rather than a qualified expression), then we must generate a call to |
3568 | -- the initialization routine using an expressions action node: | |
70482933 | 3569 | |
26bff3d9 | 3570 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 3571 | |
26bff3d9 JM |
3572 | -- Here ptr_T is the pointer type for the allocator, and T is the |
3573 | -- subtype of the allocator. A special case arises if the designated | |
3574 | -- type of the access type is a task or contains tasks. In this case | |
3575 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
3576 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
3577 | -- for details). In addition, if the type T is a task T, then the | |
3578 | -- first argument to Init must be converted to the task record type. | |
70482933 | 3579 | |
26bff3d9 JM |
3580 | declare |
3581 | T : constant Entity_Id := Entity (Expression (N)); | |
3582 | Init : Entity_Id; | |
3583 | Arg1 : Node_Id; | |
3584 | Args : List_Id; | |
3585 | Decls : List_Id; | |
3586 | Decl : Node_Id; | |
3587 | Discr : Elmt_Id; | |
3588 | Flist : Node_Id; | |
3589 | Temp_Decl : Node_Id; | |
3590 | Temp_Type : Entity_Id; | |
3591 | Attach_Level : Uint; | |
70482933 | 3592 | |
26bff3d9 JM |
3593 | begin |
3594 | if No_Initialization (N) then | |
3595 | null; | |
70482933 | 3596 | |
26bff3d9 | 3597 | -- Case of no initialization procedure present |
70482933 | 3598 | |
26bff3d9 | 3599 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 3600 | |
26bff3d9 | 3601 | -- Case of simple initialization required |
70482933 | 3602 | |
26bff3d9 | 3603 | if Needs_Simple_Initialization (T) then |
b4592168 | 3604 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
3605 | Rewrite (Expression (N), |
3606 | Make_Qualified_Expression (Loc, | |
3607 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 3608 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 3609 | |
26bff3d9 JM |
3610 | Analyze_And_Resolve (Expression (Expression (N)), T); |
3611 | Analyze_And_Resolve (Expression (N), T); | |
3612 | Set_Paren_Count (Expression (Expression (N)), 1); | |
3613 | Expand_N_Allocator (N); | |
70482933 | 3614 | |
26bff3d9 | 3615 | -- No initialization required |
70482933 RK |
3616 | |
3617 | else | |
26bff3d9 JM |
3618 | null; |
3619 | end if; | |
70482933 | 3620 | |
26bff3d9 | 3621 | -- Case of initialization procedure present, must be called |
70482933 | 3622 | |
26bff3d9 | 3623 | else |
b4592168 | 3624 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 3625 | |
b4592168 GD |
3626 | if not Restriction_Active (No_Default_Initialization) then |
3627 | Init := Base_Init_Proc (T); | |
3628 | Nod := N; | |
191fcb3a | 3629 | Temp := Make_Temporary (Loc, 'P'); |
70482933 | 3630 | |
b4592168 | 3631 | -- Construct argument list for the initialization routine call |
70482933 | 3632 | |
26bff3d9 | 3633 | Arg1 := |
b4592168 GD |
3634 | Make_Explicit_Dereference (Loc, |
3635 | Prefix => New_Reference_To (Temp, Loc)); | |
3636 | Set_Assignment_OK (Arg1); | |
3637 | Temp_Type := PtrT; | |
26bff3d9 | 3638 | |
b4592168 GD |
3639 | -- The initialization procedure expects a specific type. if the |
3640 | -- context is access to class wide, indicate that the object | |
3641 | -- being allocated has the right specific type. | |
70482933 | 3642 | |
b4592168 GD |
3643 | if Is_Class_Wide_Type (Dtyp) then |
3644 | Arg1 := Unchecked_Convert_To (T, Arg1); | |
3645 | end if; | |
70482933 | 3646 | |
b4592168 GD |
3647 | -- If designated type is a concurrent type or if it is private |
3648 | -- type whose definition is a concurrent type, the first | |
3649 | -- argument in the Init routine has to be unchecked conversion | |
3650 | -- to the corresponding record type. If the designated type is | |
3651 | -- a derived type, we also convert the argument to its root | |
3652 | -- type. | |
20b5d666 | 3653 | |
b4592168 GD |
3654 | if Is_Concurrent_Type (T) then |
3655 | Arg1 := | |
3656 | Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1); | |
70482933 | 3657 | |
b4592168 GD |
3658 | elsif Is_Private_Type (T) |
3659 | and then Present (Full_View (T)) | |
3660 | and then Is_Concurrent_Type (Full_View (T)) | |
3661 | then | |
3662 | Arg1 := | |
3663 | Unchecked_Convert_To | |
3664 | (Corresponding_Record_Type (Full_View (T)), Arg1); | |
70482933 | 3665 | |
b4592168 GD |
3666 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
3667 | declare | |
3668 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
3669 | begin | |
3670 | Arg1 := OK_Convert_To (Etype (Ftyp), Arg1); | |
3671 | Set_Etype (Arg1, Ftyp); | |
3672 | end; | |
3673 | end if; | |
70482933 | 3674 | |
b4592168 | 3675 | Args := New_List (Arg1); |
70482933 | 3676 | |
b4592168 GD |
3677 | -- For the task case, pass the Master_Id of the access type as |
3678 | -- the value of the _Master parameter, and _Chain as the value | |
3679 | -- of the _Chain parameter (_Chain will be defined as part of | |
3680 | -- the generated code for the allocator). | |
70482933 | 3681 | |
b4592168 GD |
3682 | -- In Ada 2005, the context may be a function that returns an |
3683 | -- anonymous access type. In that case the Master_Id has been | |
3684 | -- created when expanding the function declaration. | |
70482933 | 3685 | |
b4592168 GD |
3686 | if Has_Task (T) then |
3687 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 3688 | |
b4592168 GD |
3689 | -- The designated type was an incomplete type, and the |
3690 | -- access type did not get expanded. Salvage it now. | |
70482933 | 3691 | |
b941ae65 AC |
3692 | if not Restriction_Active (No_Task_Hierarchy) then |
3693 | pragma Assert (Present (Parent (Base_Type (PtrT)))); | |
3694 | Expand_N_Full_Type_Declaration | |
3695 | (Parent (Base_Type (PtrT))); | |
3696 | end if; | |
b4592168 | 3697 | end if; |
70482933 | 3698 | |
b4592168 GD |
3699 | -- If the context of the allocator is a declaration or an |
3700 | -- assignment, we can generate a meaningful image for it, | |
3701 | -- even though subsequent assignments might remove the | |
3702 | -- connection between task and entity. We build this image | |
3703 | -- when the left-hand side is a simple variable, a simple | |
3704 | -- indexed assignment or a simple selected component. | |
3705 | ||
3706 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
3707 | declare | |
3708 | Nam : constant Node_Id := Name (Parent (N)); | |
3709 | ||
3710 | begin | |
3711 | if Is_Entity_Name (Nam) then | |
3712 | Decls := | |
3713 | Build_Task_Image_Decls | |
3714 | (Loc, | |
3715 | New_Occurrence_Of | |
3716 | (Entity (Nam), Sloc (Nam)), T); | |
3717 | ||
3718 | elsif Nkind_In | |
3719 | (Nam, N_Indexed_Component, N_Selected_Component) | |
3720 | and then Is_Entity_Name (Prefix (Nam)) | |
3721 | then | |
3722 | Decls := | |
3723 | Build_Task_Image_Decls | |
3724 | (Loc, Nam, Etype (Prefix (Nam))); | |
3725 | else | |
3726 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3727 | end if; | |
3728 | end; | |
70482933 | 3729 | |
b4592168 GD |
3730 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
3731 | Decls := | |
3732 | Build_Task_Image_Decls | |
3733 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 3734 | |
b4592168 GD |
3735 | else |
3736 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3737 | end if; | |
26bff3d9 | 3738 | |
87dc09cb | 3739 | if Restriction_Active (No_Task_Hierarchy) then |
3c1ecd7e AC |
3740 | Append_To (Args, |
3741 | New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc)); | |
87dc09cb AC |
3742 | else |
3743 | Append_To (Args, | |
3744 | New_Reference_To | |
3745 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); | |
3746 | end if; | |
3747 | ||
b4592168 | 3748 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); |
26bff3d9 | 3749 | |
b4592168 GD |
3750 | Decl := Last (Decls); |
3751 | Append_To (Args, | |
3752 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 3753 | |
87dc09cb | 3754 | -- Has_Task is false, Decls not used |
26bff3d9 | 3755 | |
b4592168 GD |
3756 | else |
3757 | Decls := No_List; | |
26bff3d9 JM |
3758 | end if; |
3759 | ||
b4592168 GD |
3760 | -- Add discriminants if discriminated type |
3761 | ||
3762 | declare | |
3763 | Dis : Boolean := False; | |
3764 | Typ : Entity_Id; | |
3765 | ||
3766 | begin | |
3767 | if Has_Discriminants (T) then | |
3768 | Dis := True; | |
3769 | Typ := T; | |
3770 | ||
3771 | elsif Is_Private_Type (T) | |
3772 | and then Present (Full_View (T)) | |
3773 | and then Has_Discriminants (Full_View (T)) | |
20b5d666 | 3774 | then |
b4592168 GD |
3775 | Dis := True; |
3776 | Typ := Full_View (T); | |
20b5d666 | 3777 | end if; |
70482933 | 3778 | |
b4592168 | 3779 | if Dis then |
26bff3d9 | 3780 | |
b4592168 | 3781 | -- If the allocated object will be constrained by the |
685094bf RD |
3782 | -- default values for discriminants, then build a subtype |
3783 | -- with those defaults, and change the allocated subtype | |
3784 | -- to that. Note that this happens in fewer cases in Ada | |
3785 | -- 2005 (AI-363). | |
26bff3d9 | 3786 | |
b4592168 GD |
3787 | if not Is_Constrained (Typ) |
3788 | and then Present (Discriminant_Default_Value | |
3789 | (First_Discriminant (Typ))) | |
0791fbe9 | 3790 | and then (Ada_Version < Ada_2005 |
b4592168 GD |
3791 | or else |
3792 | not Has_Constrained_Partial_View (Typ)) | |
20b5d666 | 3793 | then |
b4592168 GD |
3794 | Typ := Build_Default_Subtype (Typ, N); |
3795 | Set_Expression (N, New_Reference_To (Typ, Loc)); | |
20b5d666 JM |
3796 | end if; |
3797 | ||
b4592168 GD |
3798 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
3799 | while Present (Discr) loop | |
3800 | Nod := Node (Discr); | |
3801 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 3802 | |
b4592168 GD |
3803 | -- AI-416: when the discriminant constraint is an |
3804 | -- anonymous access type make sure an accessibility | |
3805 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 3806 | |
0791fbe9 | 3807 | if Ada_Version >= Ada_2005 |
b4592168 GD |
3808 | and then |
3809 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
3810 | then | |
e84e11ba GD |
3811 | Apply_Accessibility_Check |
3812 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 3813 | end if; |
20b5d666 | 3814 | |
b4592168 GD |
3815 | Next_Elmt (Discr); |
3816 | end loop; | |
3817 | end if; | |
3818 | end; | |
70482933 | 3819 | |
b4592168 GD |
3820 | -- We set the allocator as analyzed so that when we analyze the |
3821 | -- expression actions node, we do not get an unwanted recursive | |
3822 | -- expansion of the allocator expression. | |
70482933 | 3823 | |
b4592168 GD |
3824 | Set_Analyzed (N, True); |
3825 | Nod := Relocate_Node (N); | |
70482933 | 3826 | |
b4592168 GD |
3827 | -- Here is the transformation: |
3828 | -- input: new T | |
3829 | -- output: Temp : constant ptr_T := new T; | |
3830 | -- Init (Temp.all, ...); | |
3831 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
3832 | -- <CTRL> Initialize (Finalizable (Temp.all)); | |
70482933 | 3833 | |
b4592168 GD |
3834 | -- Here ptr_T is the pointer type for the allocator, and is the |
3835 | -- subtype of the allocator. | |
70482933 | 3836 | |
b4592168 GD |
3837 | Temp_Decl := |
3838 | Make_Object_Declaration (Loc, | |
3839 | Defining_Identifier => Temp, | |
3840 | Constant_Present => True, | |
3841 | Object_Definition => New_Reference_To (Temp_Type, Loc), | |
3842 | Expression => Nod); | |
70482933 | 3843 | |
b4592168 GD |
3844 | Set_Assignment_OK (Temp_Decl); |
3845 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 3846 | |
b4592168 GD |
3847 | -- If the designated type is a task type or contains tasks, |
3848 | -- create block to activate created tasks, and insert | |
3849 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 3850 | |
b4592168 GD |
3851 | if Has_Task (T) then |
3852 | declare | |
3853 | L : constant List_Id := New_List; | |
3854 | Blk : Node_Id; | |
3855 | begin | |
3856 | Build_Task_Allocate_Block (L, Nod, Args); | |
3857 | Blk := Last (L); | |
3858 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
3859 | Insert_Actions (N, L); | |
3860 | end; | |
70482933 | 3861 | |
b4592168 GD |
3862 | else |
3863 | Insert_Action (N, | |
3864 | Make_Procedure_Call_Statement (Loc, | |
3865 | Name => New_Reference_To (Init, Loc), | |
3866 | Parameter_Associations => Args)); | |
3867 | end if; | |
70482933 | 3868 | |
048e5cef | 3869 | if Needs_Finalization (T) then |
70482933 | 3870 | |
b4592168 GD |
3871 | -- Postpone the generation of a finalization call for the |
3872 | -- current allocator if it acts as a coextension. | |
26bff3d9 | 3873 | |
b4592168 GD |
3874 | if Is_Dynamic_Coextension (N) then |
3875 | if No (Coextensions (N)) then | |
3876 | Set_Coextensions (N, New_Elmt_List); | |
3877 | end if; | |
70482933 | 3878 | |
b4592168 GD |
3879 | Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N)); |
3880 | ||
3881 | else | |
3882 | Flist := | |
3883 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
0669bebe | 3884 | |
b4592168 GD |
3885 | -- Anonymous access types created for access parameters |
3886 | -- are attached to an explicitly constructed controller, | |
3887 | -- which ensures that they can be finalized properly, | |
3888 | -- even if their deallocation might not happen. The list | |
3889 | -- associated with the controller is doubly-linked. For | |
3890 | -- other anonymous access types, the object may end up | |
3891 | -- on the global final list which is singly-linked. | |
3892 | -- Work needed for access discriminants in Ada 2005 ??? | |
0669bebe | 3893 | |
a523b302 | 3894 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
b4592168 GD |
3895 | Attach_Level := Uint_1; |
3896 | else | |
3897 | Attach_Level := Uint_2; | |
3898 | end if; | |
0669bebe | 3899 | |
b4592168 GD |
3900 | Insert_Actions (N, |
3901 | Make_Init_Call ( | |
3902 | Ref => New_Copy_Tree (Arg1), | |
3903 | Typ => T, | |
3904 | Flist_Ref => Flist, | |
3905 | With_Attach => Make_Integer_Literal (Loc, | |
3906 | Intval => Attach_Level))); | |
3907 | end if; | |
70482933 RK |
3908 | end if; |
3909 | ||
b4592168 GD |
3910 | Rewrite (N, New_Reference_To (Temp, Loc)); |
3911 | Analyze_And_Resolve (N, PtrT); | |
3912 | end if; | |
26bff3d9 JM |
3913 | end if; |
3914 | end; | |
f82944b7 | 3915 | |
26bff3d9 JM |
3916 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
3917 | -- object that has been rewritten as a reference, we displace "this" | |
3918 | -- to reference properly its secondary dispatch table. | |
3919 | ||
3920 | if Nkind (N) = N_Identifier | |
f82944b7 JM |
3921 | and then Is_Interface (Dtyp) |
3922 | then | |
26bff3d9 | 3923 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
3924 | end if; |
3925 | ||
fbf5a39b AC |
3926 | exception |
3927 | when RE_Not_Available => | |
3928 | return; | |
70482933 RK |
3929 | end Expand_N_Allocator; |
3930 | ||
3931 | ----------------------- | |
3932 | -- Expand_N_And_Then -- | |
3933 | ----------------------- | |
3934 | ||
5875f8d6 AC |
3935 | procedure Expand_N_And_Then (N : Node_Id) |
3936 | renames Expand_Short_Circuit_Operator; | |
70482933 | 3937 | |
19d846a0 RD |
3938 | ------------------------------ |
3939 | -- Expand_N_Case_Expression -- | |
3940 | ------------------------------ | |
3941 | ||
3942 | procedure Expand_N_Case_Expression (N : Node_Id) is | |
3943 | Loc : constant Source_Ptr := Sloc (N); | |
3944 | Typ : constant Entity_Id := Etype (N); | |
3945 | Cstmt : Node_Id; | |
3946 | Tnn : Entity_Id; | |
3947 | Pnn : Entity_Id; | |
3948 | Actions : List_Id; | |
3949 | Ttyp : Entity_Id; | |
3950 | Alt : Node_Id; | |
3951 | Fexp : Node_Id; | |
3952 | ||
3953 | begin | |
3954 | -- We expand | |
3955 | ||
3956 | -- case X is when A => AX, when B => BX ... | |
3957 | ||
3958 | -- to | |
3959 | ||
3960 | -- do | |
3961 | -- Tnn : typ; | |
3962 | -- case X is | |
3963 | -- when A => | |
3964 | -- Tnn := AX; | |
3965 | -- when B => | |
3966 | -- Tnn := BX; | |
3967 | -- ... | |
3968 | -- end case; | |
3969 | -- in Tnn end; | |
3970 | ||
3971 | -- However, this expansion is wrong for limited types, and also | |
3972 | -- wrong for unconstrained types (since the bounds may not be the | |
3973 | -- same in all branches). Furthermore it involves an extra copy | |
3974 | -- for large objects. So we take care of this by using the following | |
3975 | -- modified expansion for non-scalar types: | |
3976 | ||
3977 | -- do | |
3978 | -- type Pnn is access all typ; | |
3979 | -- Tnn : Pnn; | |
3980 | -- case X is | |
3981 | -- when A => | |
3982 | -- T := AX'Unrestricted_Access; | |
3983 | -- when B => | |
3984 | -- T := BX'Unrestricted_Access; | |
3985 | -- ... | |
3986 | -- end case; | |
3987 | -- in Tnn.all end; | |
3988 | ||
3989 | Cstmt := | |
3990 | Make_Case_Statement (Loc, | |
3991 | Expression => Expression (N), | |
3992 | Alternatives => New_List); | |
3993 | ||
3994 | Actions := New_List; | |
3995 | ||
3996 | -- Scalar case | |
3997 | ||
3998 | if Is_Scalar_Type (Typ) then | |
3999 | Ttyp := Typ; | |
4000 | ||
4001 | else | |
4002 | Pnn := Make_Temporary (Loc, 'P'); | |
4003 | Append_To (Actions, | |
4004 | Make_Full_Type_Declaration (Loc, | |
4005 | Defining_Identifier => Pnn, | |
4006 | Type_Definition => | |
4007 | Make_Access_To_Object_Definition (Loc, | |
4008 | All_Present => True, | |
4009 | Subtype_Indication => | |
4010 | New_Reference_To (Typ, Loc)))); | |
4011 | Ttyp := Pnn; | |
4012 | end if; | |
4013 | ||
4014 | Tnn := Make_Temporary (Loc, 'T'); | |
4015 | Append_To (Actions, | |
4016 | Make_Object_Declaration (Loc, | |
4017 | Defining_Identifier => Tnn, | |
4018 | Object_Definition => New_Occurrence_Of (Ttyp, Loc))); | |
4019 | ||
4020 | -- Now process the alternatives | |
4021 | ||
4022 | Alt := First (Alternatives (N)); | |
4023 | while Present (Alt) loop | |
4024 | declare | |
4025 | Aexp : Node_Id := Expression (Alt); | |
4026 | Aloc : constant Source_Ptr := Sloc (Aexp); | |
4027 | ||
4028 | begin | |
05dbd302 AC |
4029 | -- Propagate declarations inserted in the node by Insert_Actions |
4030 | -- (for example, temporaries generated to remove side effects). | |
4031 | ||
4032 | Append_List_To (Actions, Sinfo.Actions (Alt)); | |
4033 | ||
19d846a0 RD |
4034 | if not Is_Scalar_Type (Typ) then |
4035 | Aexp := | |
4036 | Make_Attribute_Reference (Aloc, | |
4037 | Prefix => Relocate_Node (Aexp), | |
4038 | Attribute_Name => Name_Unrestricted_Access); | |
4039 | end if; | |
4040 | ||
4041 | Append_To | |
4042 | (Alternatives (Cstmt), | |
4043 | Make_Case_Statement_Alternative (Sloc (Alt), | |
4044 | Discrete_Choices => Discrete_Choices (Alt), | |
4045 | Statements => New_List ( | |
4046 | Make_Assignment_Statement (Aloc, | |
4047 | Name => New_Occurrence_Of (Tnn, Loc), | |
4048 | Expression => Aexp)))); | |
4049 | end; | |
4050 | ||
4051 | Next (Alt); | |
4052 | end loop; | |
4053 | ||
4054 | Append_To (Actions, Cstmt); | |
4055 | ||
4056 | -- Construct and return final expression with actions | |
4057 | ||
4058 | if Is_Scalar_Type (Typ) then | |
4059 | Fexp := New_Occurrence_Of (Tnn, Loc); | |
4060 | else | |
4061 | Fexp := | |
4062 | Make_Explicit_Dereference (Loc, | |
4063 | Prefix => New_Occurrence_Of (Tnn, Loc)); | |
4064 | end if; | |
4065 | ||
4066 | Rewrite (N, | |
4067 | Make_Expression_With_Actions (Loc, | |
4068 | Expression => Fexp, | |
4069 | Actions => Actions)); | |
4070 | ||
4071 | Analyze_And_Resolve (N, Typ); | |
4072 | end Expand_N_Case_Expression; | |
4073 | ||
70482933 RK |
4074 | ------------------------------------- |
4075 | -- Expand_N_Conditional_Expression -- | |
4076 | ------------------------------------- | |
4077 | ||
305caf42 | 4078 | -- Deal with limited types and expression actions |
70482933 RK |
4079 | |
4080 | procedure Expand_N_Conditional_Expression (N : Node_Id) is | |
4081 | Loc : constant Source_Ptr := Sloc (N); | |
4082 | Cond : constant Node_Id := First (Expressions (N)); | |
4083 | Thenx : constant Node_Id := Next (Cond); | |
4084 | Elsex : constant Node_Id := Next (Thenx); | |
4085 | Typ : constant Entity_Id := Etype (N); | |
c471e2da | 4086 | |
602a7ec0 AC |
4087 | Cnn : Entity_Id; |
4088 | Decl : Node_Id; | |
4089 | New_If : Node_Id; | |
4090 | New_N : Node_Id; | |
4091 | P_Decl : Node_Id; | |
4092 | Expr : Node_Id; | |
4093 | Actions : List_Id; | |
70482933 RK |
4094 | |
4095 | begin | |
602a7ec0 AC |
4096 | -- Fold at compile time if condition known. We have already folded |
4097 | -- static conditional expressions, but it is possible to fold any | |
4098 | -- case in which the condition is known at compile time, even though | |
4099 | -- the result is non-static. | |
4100 | ||
4101 | -- Note that we don't do the fold of such cases in Sem_Elab because | |
4102 | -- it can cause infinite loops with the expander adding a conditional | |
4103 | -- expression, and Sem_Elab circuitry removing it repeatedly. | |
4104 | ||
4105 | if Compile_Time_Known_Value (Cond) then | |
4106 | if Is_True (Expr_Value (Cond)) then | |
4107 | Expr := Thenx; | |
4108 | Actions := Then_Actions (N); | |
4109 | else | |
4110 | Expr := Elsex; | |
4111 | Actions := Else_Actions (N); | |
4112 | end if; | |
4113 | ||
4114 | Remove (Expr); | |
ae77c68b AC |
4115 | |
4116 | if Present (Actions) then | |
4117 | ||
4118 | -- If we are not allowed to use Expression_With_Actions, just | |
4119 | -- skip the optimization, it is not critical for correctness. | |
4120 | ||
4121 | if not Use_Expression_With_Actions then | |
4122 | goto Skip_Optimization; | |
4123 | end if; | |
4124 | ||
4125 | Rewrite (N, | |
4126 | Make_Expression_With_Actions (Loc, | |
4127 | Expression => Relocate_Node (Expr), | |
4128 | Actions => Actions)); | |
4129 | Analyze_And_Resolve (N, Typ); | |
4130 | ||
4131 | else | |
4132 | Rewrite (N, Relocate_Node (Expr)); | |
4133 | end if; | |
602a7ec0 AC |
4134 | |
4135 | -- Note that the result is never static (legitimate cases of static | |
4136 | -- conditional expressions were folded in Sem_Eval). | |
4137 | ||
4138 | Set_Is_Static_Expression (N, False); | |
4139 | return; | |
4140 | end if; | |
4141 | ||
ae77c68b AC |
4142 | <<Skip_Optimization>> |
4143 | ||
305caf42 AC |
4144 | -- If the type is limited or unconstrained, we expand as follows to |
4145 | -- avoid any possibility of improper copies. | |
70482933 | 4146 | |
305caf42 AC |
4147 | -- Note: it may be possible to avoid this special processing if the |
4148 | -- back end uses its own mechanisms for handling by-reference types ??? | |
ac7120ce | 4149 | |
c471e2da AC |
4150 | -- type Ptr is access all Typ; |
4151 | -- Cnn : Ptr; | |
ac7120ce RD |
4152 | -- if cond then |
4153 | -- <<then actions>> | |
4154 | -- Cnn := then-expr'Unrestricted_Access; | |
4155 | -- else | |
4156 | -- <<else actions>> | |
4157 | -- Cnn := else-expr'Unrestricted_Access; | |
4158 | -- end if; | |
4159 | ||
308e6f3a | 4160 | -- and replace the conditional expression by a reference to Cnn.all. |
ac7120ce | 4161 | |
305caf42 AC |
4162 | -- This special case can be skipped if the back end handles limited |
4163 | -- types properly and ensures that no incorrect copies are made. | |
4164 | ||
4165 | if Is_By_Reference_Type (Typ) | |
4166 | and then not Back_End_Handles_Limited_Types | |
4167 | then | |
faf387e1 | 4168 | Cnn := Make_Temporary (Loc, 'C', N); |
70482933 | 4169 | |
c471e2da AC |
4170 | P_Decl := |
4171 | Make_Full_Type_Declaration (Loc, | |
191fcb3a | 4172 | Defining_Identifier => Make_Temporary (Loc, 'A'), |
c471e2da AC |
4173 | Type_Definition => |
4174 | Make_Access_To_Object_Definition (Loc, | |
4175 | All_Present => True, | |
4176 | Subtype_Indication => | |
4177 | New_Reference_To (Typ, Loc))); | |
4178 | ||
4179 | Insert_Action (N, P_Decl); | |
4180 | ||
4181 | Decl := | |
4182 | Make_Object_Declaration (Loc, | |
4183 | Defining_Identifier => Cnn, | |
4184 | Object_Definition => | |
4185 | New_Occurrence_Of (Defining_Identifier (P_Decl), Loc)); | |
4186 | ||
70482933 RK |
4187 | New_If := |
4188 | Make_Implicit_If_Statement (N, | |
4189 | Condition => Relocate_Node (Cond), | |
4190 | ||
4191 | Then_Statements => New_List ( | |
4192 | Make_Assignment_Statement (Sloc (Thenx), | |
4193 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
c471e2da AC |
4194 | Expression => |
4195 | Make_Attribute_Reference (Loc, | |
4196 | Attribute_Name => Name_Unrestricted_Access, | |
4197 | Prefix => Relocate_Node (Thenx)))), | |
70482933 RK |
4198 | |
4199 | Else_Statements => New_List ( | |
4200 | Make_Assignment_Statement (Sloc (Elsex), | |
4201 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
c471e2da AC |
4202 | Expression => |
4203 | Make_Attribute_Reference (Loc, | |
4204 | Attribute_Name => Name_Unrestricted_Access, | |
4205 | Prefix => Relocate_Node (Elsex))))); | |
70482933 | 4206 | |
c471e2da AC |
4207 | New_N := |
4208 | Make_Explicit_Dereference (Loc, | |
4209 | Prefix => New_Occurrence_Of (Cnn, Loc)); | |
fb1949a0 | 4210 | |
c471e2da AC |
4211 | -- For other types, we only need to expand if there are other actions |
4212 | -- associated with either branch. | |
4213 | ||
4214 | elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
c471e2da | 4215 | |
305caf42 AC |
4216 | -- We have two approaches to handling this. If we are allowed to use |
4217 | -- N_Expression_With_Actions, then we can just wrap the actions into | |
4218 | -- the appropriate expression. | |
4219 | ||
4220 | if Use_Expression_With_Actions then | |
4221 | if Present (Then_Actions (N)) then | |
4222 | Rewrite (Thenx, | |
4223 | Make_Expression_With_Actions (Sloc (Thenx), | |
4224 | Actions => Then_Actions (N), | |
4225 | Expression => Relocate_Node (Thenx))); | |
48b351d9 | 4226 | Set_Then_Actions (N, No_List); |
305caf42 AC |
4227 | Analyze_And_Resolve (Thenx, Typ); |
4228 | end if; | |
c471e2da | 4229 | |
305caf42 AC |
4230 | if Present (Else_Actions (N)) then |
4231 | Rewrite (Elsex, | |
4232 | Make_Expression_With_Actions (Sloc (Elsex), | |
4233 | Actions => Else_Actions (N), | |
4234 | Expression => Relocate_Node (Elsex))); | |
48b351d9 | 4235 | Set_Else_Actions (N, No_List); |
305caf42 AC |
4236 | Analyze_And_Resolve (Elsex, Typ); |
4237 | end if; | |
c471e2da | 4238 | |
305caf42 | 4239 | return; |
c471e2da | 4240 | |
305caf42 AC |
4241 | -- if we can't use N_Expression_With_Actions nodes, then we insert |
4242 | -- the following sequence of actions (using Insert_Actions): | |
fb1949a0 | 4243 | |
305caf42 AC |
4244 | -- Cnn : typ; |
4245 | -- if cond then | |
4246 | -- <<then actions>> | |
4247 | -- Cnn := then-expr; | |
4248 | -- else | |
4249 | -- <<else actions>> | |
4250 | -- Cnn := else-expr | |
4251 | -- end if; | |
fbf5a39b | 4252 | |
305caf42 | 4253 | -- and replace the conditional expression by a reference to Cnn |
70482933 | 4254 | |
305caf42 AC |
4255 | else |
4256 | Cnn := Make_Temporary (Loc, 'C', N); | |
4257 | ||
4258 | Decl := | |
4259 | Make_Object_Declaration (Loc, | |
4260 | Defining_Identifier => Cnn, | |
4261 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
4262 | ||
4263 | New_If := | |
4264 | Make_Implicit_If_Statement (N, | |
4265 | Condition => Relocate_Node (Cond), | |
4266 | ||
4267 | Then_Statements => New_List ( | |
4268 | Make_Assignment_Statement (Sloc (Thenx), | |
4269 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
4270 | Expression => Relocate_Node (Thenx))), | |
4271 | ||
4272 | Else_Statements => New_List ( | |
4273 | Make_Assignment_Statement (Sloc (Elsex), | |
4274 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
4275 | Expression => Relocate_Node (Elsex)))); | |
70482933 | 4276 | |
305caf42 AC |
4277 | Set_Assignment_OK (Name (First (Then_Statements (New_If)))); |
4278 | Set_Assignment_OK (Name (First (Else_Statements (New_If)))); | |
4279 | ||
4280 | New_N := New_Occurrence_Of (Cnn, Loc); | |
4281 | end if; | |
4282 | ||
4283 | -- If no actions then no expansion needed, gigi will handle it using | |
4284 | -- the same approach as a C conditional expression. | |
4285 | ||
4286 | else | |
c471e2da AC |
4287 | return; |
4288 | end if; | |
4289 | ||
305caf42 AC |
4290 | -- Fall through here for either the limited expansion, or the case of |
4291 | -- inserting actions for non-limited types. In both these cases, we must | |
4292 | -- move the SLOC of the parent If statement to the newly created one and | |
3fc5d116 RD |
4293 | -- change it to the SLOC of the expression which, after expansion, will |
4294 | -- correspond to what is being evaluated. | |
c471e2da AC |
4295 | |
4296 | if Present (Parent (N)) | |
4297 | and then Nkind (Parent (N)) = N_If_Statement | |
4298 | then | |
4299 | Set_Sloc (New_If, Sloc (Parent (N))); | |
4300 | Set_Sloc (Parent (N), Loc); | |
4301 | end if; | |
70482933 | 4302 | |
3fc5d116 RD |
4303 | -- Make sure Then_Actions and Else_Actions are appropriately moved |
4304 | -- to the new if statement. | |
4305 | ||
c471e2da AC |
4306 | if Present (Then_Actions (N)) then |
4307 | Insert_List_Before | |
4308 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
70482933 | 4309 | end if; |
c471e2da AC |
4310 | |
4311 | if Present (Else_Actions (N)) then | |
4312 | Insert_List_Before | |
4313 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
4314 | end if; | |
4315 | ||
4316 | Insert_Action (N, Decl); | |
4317 | Insert_Action (N, New_If); | |
4318 | Rewrite (N, New_N); | |
4319 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
4320 | end Expand_N_Conditional_Expression; |
4321 | ||
4322 | ----------------------------------- | |
4323 | -- Expand_N_Explicit_Dereference -- | |
4324 | ----------------------------------- | |
4325 | ||
4326 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
4327 | begin | |
dfd99a80 | 4328 | -- Insert explicit dereference call for the checked storage pool case |
70482933 RK |
4329 | |
4330 | Insert_Dereference_Action (Prefix (N)); | |
4331 | end Expand_N_Explicit_Dereference; | |
4332 | ||
4333 | ----------------- | |
4334 | -- Expand_N_In -- | |
4335 | ----------------- | |
4336 | ||
4337 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 | 4338 | Loc : constant Source_Ptr := Sloc (N); |
4818e7b9 | 4339 | Restyp : constant Entity_Id := Etype (N); |
7324bf49 AC |
4340 | Lop : constant Node_Id := Left_Opnd (N); |
4341 | Rop : constant Node_Id := Right_Opnd (N); | |
4342 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 4343 | |
4818e7b9 RD |
4344 | Ltyp : Entity_Id; |
4345 | Rtyp : Entity_Id; | |
4346 | ||
197e4514 | 4347 | procedure Expand_Set_Membership; |
4818e7b9 RD |
4348 | -- For each choice we create a simple equality or membership test. |
4349 | -- The whole membership is rewritten connecting these with OR ELSE. | |
197e4514 AC |
4350 | |
4351 | --------------------------- | |
4352 | -- Expand_Set_Membership -- | |
4353 | --------------------------- | |
4354 | ||
4355 | procedure Expand_Set_Membership is | |
4356 | Alt : Node_Id; | |
4357 | Res : Node_Id; | |
4358 | ||
4359 | function Make_Cond (Alt : Node_Id) return Node_Id; | |
4360 | -- If the alternative is a subtype mark, create a simple membership | |
4361 | -- test. Otherwise create an equality test for it. | |
4362 | ||
4363 | --------------- | |
4364 | -- Make_Cond -- | |
4365 | --------------- | |
4366 | ||
4367 | function Make_Cond (Alt : Node_Id) return Node_Id is | |
4368 | Cond : Node_Id; | |
4369 | L : constant Node_Id := New_Copy (Lop); | |
4370 | R : constant Node_Id := Relocate_Node (Alt); | |
4371 | ||
4372 | begin | |
c95e0edc | 4373 | if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt))) |
e606088a | 4374 | or else Nkind (Alt) = N_Range |
197e4514 | 4375 | then |
e606088a AC |
4376 | Cond := |
4377 | Make_In (Sloc (Alt), | |
4378 | Left_Opnd => L, | |
4379 | Right_Opnd => R); | |
197e4514 | 4380 | else |
c95e0edc | 4381 | Cond := |
e606088a AC |
4382 | Make_Op_Eq (Sloc (Alt), |
4383 | Left_Opnd => L, | |
4384 | Right_Opnd => R); | |
197e4514 AC |
4385 | end if; |
4386 | ||
4387 | return Cond; | |
4388 | end Make_Cond; | |
4389 | ||
66150d01 | 4390 | -- Start of processing for Expand_Set_Membership |
197e4514 AC |
4391 | |
4392 | begin | |
4393 | Alt := Last (Alternatives (N)); | |
4394 | Res := Make_Cond (Alt); | |
4395 | ||
4396 | Prev (Alt); | |
4397 | while Present (Alt) loop | |
4398 | Res := | |
4399 | Make_Or_Else (Sloc (Alt), | |
4400 | Left_Opnd => Make_Cond (Alt), | |
4401 | Right_Opnd => Res); | |
4402 | Prev (Alt); | |
4403 | end loop; | |
4404 | ||
4405 | Rewrite (N, Res); | |
4406 | Analyze_And_Resolve (N, Standard_Boolean); | |
4407 | end Expand_Set_Membership; | |
4408 | ||
630d30e9 RD |
4409 | procedure Substitute_Valid_Check; |
4410 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
4411 | -- test for the left operand being in range of its subtype. | |
4412 | ||
4413 | ---------------------------- | |
4414 | -- Substitute_Valid_Check -- | |
4415 | ---------------------------- | |
4416 | ||
4417 | procedure Substitute_Valid_Check is | |
4418 | begin | |
c7532b2d AC |
4419 | Rewrite (N, |
4420 | Make_Attribute_Reference (Loc, | |
4421 | Prefix => Relocate_Node (Lop), | |
4422 | Attribute_Name => Name_Valid)); | |
630d30e9 | 4423 | |
c7532b2d | 4424 | Analyze_And_Resolve (N, Restyp); |
630d30e9 | 4425 | |
c7532b2d AC |
4426 | Error_Msg_N ("?explicit membership test may be optimized away", N); |
4427 | Error_Msg_N -- CODEFIX | |
4428 | ("\?use ''Valid attribute instead", N); | |
4429 | return; | |
630d30e9 RD |
4430 | end Substitute_Valid_Check; |
4431 | ||
4432 | -- Start of processing for Expand_N_In | |
4433 | ||
70482933 | 4434 | begin |
308e6f3a | 4435 | -- If set membership case, expand with separate procedure |
4818e7b9 | 4436 | |
197e4514 AC |
4437 | if Present (Alternatives (N)) then |
4438 | Remove_Side_Effects (Lop); | |
4439 | Expand_Set_Membership; | |
4440 | return; | |
4441 | end if; | |
4442 | ||
4818e7b9 RD |
4443 | -- Not set membership, proceed with expansion |
4444 | ||
4445 | Ltyp := Etype (Left_Opnd (N)); | |
4446 | Rtyp := Etype (Right_Opnd (N)); | |
4447 | ||
630d30e9 RD |
4448 | -- Check case of explicit test for an expression in range of its |
4449 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
9a0ddeee | 4450 | -- test and give a warning. For floating point types however, this is a |
c95e0edc | 4451 | -- standard way to check for finite numbers, and using 'Valid would |
c7532b2d AC |
4452 | -- typically be a pessimization. Also skip this test for predicated |
4453 | -- types, since it is perfectly reasonable to check if a value meets | |
4454 | -- its predicate. | |
630d30e9 | 4455 | |
4818e7b9 RD |
4456 | if Is_Scalar_Type (Ltyp) |
4457 | and then not Is_Floating_Point_Type (Ltyp) | |
630d30e9 | 4458 | and then Nkind (Rop) in N_Has_Entity |
4818e7b9 | 4459 | and then Ltyp = Entity (Rop) |
630d30e9 | 4460 | and then Comes_From_Source (N) |
26bff3d9 | 4461 | and then VM_Target = No_VM |
c7532b2d AC |
4462 | and then not (Is_Discrete_Type (Ltyp) |
4463 | and then Present (Predicate_Function (Ltyp))) | |
630d30e9 RD |
4464 | then |
4465 | Substitute_Valid_Check; | |
4466 | return; | |
4467 | end if; | |
4468 | ||
20b5d666 JM |
4469 | -- Do validity check on operands |
4470 | ||
4471 | if Validity_Checks_On and Validity_Check_Operands then | |
4472 | Ensure_Valid (Left_Opnd (N)); | |
4473 | Validity_Check_Range (Right_Opnd (N)); | |
4474 | end if; | |
4475 | ||
630d30e9 | 4476 | -- Case of explicit range |
fbf5a39b AC |
4477 | |
4478 | if Nkind (Rop) = N_Range then | |
4479 | declare | |
630d30e9 RD |
4480 | Lo : constant Node_Id := Low_Bound (Rop); |
4481 | Hi : constant Node_Id := High_Bound (Rop); | |
4482 | ||
4483 | Lo_Orig : constant Node_Id := Original_Node (Lo); | |
4484 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
4485 | ||
c800f862 RD |
4486 | Lcheck : Compare_Result; |
4487 | Ucheck : Compare_Result; | |
fbf5a39b | 4488 | |
d766cee3 RD |
4489 | Warn1 : constant Boolean := |
4490 | Constant_Condition_Warnings | |
c800f862 RD |
4491 | and then Comes_From_Source (N) |
4492 | and then not In_Instance; | |
d766cee3 | 4493 | -- This must be true for any of the optimization warnings, we |
9a0ddeee AC |
4494 | -- clearly want to give them only for source with the flag on. We |
4495 | -- also skip these warnings in an instance since it may be the | |
4496 | -- case that different instantiations have different ranges. | |
d766cee3 RD |
4497 | |
4498 | Warn2 : constant Boolean := | |
4499 | Warn1 | |
4500 | and then Nkind (Original_Node (Rop)) = N_Range | |
4501 | and then Is_Integer_Type (Etype (Lo)); | |
4502 | -- For the case where only one bound warning is elided, we also | |
4503 | -- insist on an explicit range and an integer type. The reason is | |
4504 | -- that the use of enumeration ranges including an end point is | |
9a0ddeee AC |
4505 | -- common, as is the use of a subtype name, one of whose bounds is |
4506 | -- the same as the type of the expression. | |
d766cee3 | 4507 | |
fbf5a39b | 4508 | begin |
c95e0edc | 4509 | -- If test is explicit x'First .. x'Last, replace by valid check |
630d30e9 | 4510 | |
e606088a AC |
4511 | -- Could use some individual comments for this complex test ??? |
4512 | ||
d766cee3 | 4513 | if Is_Scalar_Type (Ltyp) |
630d30e9 RD |
4514 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
4515 | and then Attribute_Name (Lo_Orig) = Name_First | |
4516 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 4517 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
630d30e9 RD |
4518 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
4519 | and then Attribute_Name (Hi_Orig) = Name_Last | |
4520 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 4521 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
630d30e9 | 4522 | and then Comes_From_Source (N) |
26bff3d9 | 4523 | and then VM_Target = No_VM |
630d30e9 RD |
4524 | then |
4525 | Substitute_Valid_Check; | |
4818e7b9 | 4526 | goto Leave; |
630d30e9 RD |
4527 | end if; |
4528 | ||
d766cee3 RD |
4529 | -- If bounds of type are known at compile time, and the end points |
4530 | -- are known at compile time and identical, this is another case | |
4531 | -- for substituting a valid test. We only do this for discrete | |
4532 | -- types, since it won't arise in practice for float types. | |
4533 | ||
4534 | if Comes_From_Source (N) | |
4535 | and then Is_Discrete_Type (Ltyp) | |
4536 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
4537 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
4538 | and then Compile_Time_Known_Value (Lo) | |
4539 | and then Compile_Time_Known_Value (Hi) | |
4540 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
4541 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e RD |
4542 | |
4543 | -- Kill warnings in instances, since they may be cases where we | |
4544 | -- have a test in the generic that makes sense with some types | |
4545 | -- and not with other types. | |
4546 | ||
4547 | and then not In_Instance | |
d766cee3 RD |
4548 | then |
4549 | Substitute_Valid_Check; | |
4818e7b9 | 4550 | goto Leave; |
d766cee3 RD |
4551 | end if; |
4552 | ||
9a0ddeee AC |
4553 | -- If we have an explicit range, do a bit of optimization based on |
4554 | -- range analysis (we may be able to kill one or both checks). | |
630d30e9 | 4555 | |
c800f862 RD |
4556 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
4557 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
4558 | ||
630d30e9 RD |
4559 | -- If either check is known to fail, replace result by False since |
4560 | -- the other check does not matter. Preserve the static flag for | |
4561 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
4562 | |
4563 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 4564 | if Warn1 then |
ed2233dc AC |
4565 | Error_Msg_N ("?range test optimized away", N); |
4566 | Error_Msg_N ("\?value is known to be out of range", N); | |
d766cee3 RD |
4567 | end if; |
4568 | ||
9a0ddeee | 4569 | Rewrite (N, New_Reference_To (Standard_False, Loc)); |
4818e7b9 | 4570 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 4571 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 4572 | goto Leave; |
fbf5a39b | 4573 | |
685094bf RD |
4574 | -- If both checks are known to succeed, replace result by True, |
4575 | -- since we know we are in range. | |
fbf5a39b AC |
4576 | |
4577 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 4578 | if Warn1 then |
ed2233dc AC |
4579 | Error_Msg_N ("?range test optimized away", N); |
4580 | Error_Msg_N ("\?value is known to be in range", N); | |
d766cee3 RD |
4581 | end if; |
4582 | ||
9a0ddeee | 4583 | Rewrite (N, New_Reference_To (Standard_True, Loc)); |
4818e7b9 | 4584 | Analyze_And_Resolve (N, Restyp); |
7324bf49 | 4585 | Set_Is_Static_Expression (N, Static); |
4818e7b9 | 4586 | goto Leave; |
fbf5a39b | 4587 | |
d766cee3 RD |
4588 | -- If lower bound check succeeds and upper bound check is not |
4589 | -- known to succeed or fail, then replace the range check with | |
4590 | -- a comparison against the upper bound. | |
fbf5a39b AC |
4591 | |
4592 | elsif Lcheck in Compare_GE then | |
94eefd2e | 4593 | if Warn2 and then not In_Instance then |
ed2233dc AC |
4594 | Error_Msg_N ("?lower bound test optimized away", Lo); |
4595 | Error_Msg_N ("\?value is known to be in range", Lo); | |
d766cee3 RD |
4596 | end if; |
4597 | ||
fbf5a39b AC |
4598 | Rewrite (N, |
4599 | Make_Op_Le (Loc, | |
4600 | Left_Opnd => Lop, | |
4601 | Right_Opnd => High_Bound (Rop))); | |
4818e7b9 RD |
4602 | Analyze_And_Resolve (N, Restyp); |
4603 | goto Leave; | |
fbf5a39b | 4604 | |
d766cee3 RD |
4605 | -- If upper bound check succeeds and lower bound check is not |
4606 | -- known to succeed or fail, then replace the range check with | |
4607 | -- a comparison against the lower bound. | |
fbf5a39b AC |
4608 | |
4609 | elsif Ucheck in Compare_LE then | |
94eefd2e | 4610 | if Warn2 and then not In_Instance then |
ed2233dc AC |
4611 | Error_Msg_N ("?upper bound test optimized away", Hi); |
4612 | Error_Msg_N ("\?value is known to be in range", Hi); | |
d766cee3 RD |
4613 | end if; |
4614 | ||
fbf5a39b AC |
4615 | Rewrite (N, |
4616 | Make_Op_Ge (Loc, | |
4617 | Left_Opnd => Lop, | |
4618 | Right_Opnd => Low_Bound (Rop))); | |
4818e7b9 RD |
4619 | Analyze_And_Resolve (N, Restyp); |
4620 | goto Leave; | |
fbf5a39b | 4621 | end if; |
c800f862 RD |
4622 | |
4623 | -- We couldn't optimize away the range check, but there is one | |
4624 | -- more issue. If we are checking constant conditionals, then we | |
4625 | -- see if we can determine the outcome assuming everything is | |
4626 | -- valid, and if so give an appropriate warning. | |
4627 | ||
4628 | if Warn1 and then not Assume_No_Invalid_Values then | |
4629 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
4630 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
4631 | ||
4632 | -- Result is out of range for valid value | |
4633 | ||
4634 | if Lcheck = LT or else Ucheck = GT then | |
ed2233dc | 4635 | Error_Msg_N |
c800f862 RD |
4636 | ("?value can only be in range if it is invalid", N); |
4637 | ||
4638 | -- Result is in range for valid value | |
4639 | ||
4640 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
ed2233dc | 4641 | Error_Msg_N |
c800f862 RD |
4642 | ("?value can only be out of range if it is invalid", N); |
4643 | ||
4644 | -- Lower bound check succeeds if value is valid | |
4645 | ||
4646 | elsif Warn2 and then Lcheck in Compare_GE then | |
ed2233dc | 4647 | Error_Msg_N |
c800f862 RD |
4648 | ("?lower bound check only fails if it is invalid", Lo); |
4649 | ||
4650 | -- Upper bound check succeeds if value is valid | |
4651 | ||
4652 | elsif Warn2 and then Ucheck in Compare_LE then | |
ed2233dc | 4653 | Error_Msg_N |
c800f862 RD |
4654 | ("?upper bound check only fails for invalid values", Hi); |
4655 | end if; | |
4656 | end if; | |
fbf5a39b AC |
4657 | end; |
4658 | ||
4659 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 4660 | |
4818e7b9 | 4661 | goto Leave; |
70482933 RK |
4662 | |
4663 | -- Here right operand is a subtype mark | |
4664 | ||
4665 | else | |
4666 | declare | |
82878151 AC |
4667 | Typ : Entity_Id := Etype (Rop); |
4668 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
4669 | Cond : Node_Id := Empty; | |
4670 | New_N : Node_Id; | |
4671 | Obj : Node_Id := Lop; | |
4672 | SCIL_Node : Node_Id; | |
70482933 RK |
4673 | |
4674 | begin | |
4675 | Remove_Side_Effects (Obj); | |
4676 | ||
4677 | -- For tagged type, do tagged membership operation | |
4678 | ||
4679 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 4680 | |
26bff3d9 JM |
4681 | -- No expansion will be performed when VM_Target, as the VM |
4682 | -- back-ends will handle the membership tests directly (tags | |
4683 | -- are not explicitly represented in Java objects, so the | |
4684 | -- normal tagged membership expansion is not what we want). | |
70482933 | 4685 | |
1f110335 | 4686 | if Tagged_Type_Expansion then |
82878151 AC |
4687 | Tagged_Membership (N, SCIL_Node, New_N); |
4688 | Rewrite (N, New_N); | |
4818e7b9 | 4689 | Analyze_And_Resolve (N, Restyp); |
82878151 AC |
4690 | |
4691 | -- Update decoration of relocated node referenced by the | |
4692 | -- SCIL node. | |
4693 | ||
9a0ddeee | 4694 | if Generate_SCIL and then Present (SCIL_Node) then |
7665e4bd | 4695 | Set_SCIL_Node (N, SCIL_Node); |
82878151 | 4696 | end if; |
70482933 RK |
4697 | end if; |
4698 | ||
4818e7b9 | 4699 | goto Leave; |
70482933 | 4700 | |
c95e0edc | 4701 | -- If type is scalar type, rewrite as x in t'First .. t'Last. |
70482933 | 4702 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
4703 | -- type if they come from the original type definition. Also this |
4704 | -- way we get all the processing above for an explicit range. | |
70482933 | 4705 | |
c7532b2d AC |
4706 | -- Don't do this for predicated types, since in this case we |
4707 | -- want to check the predicate! | |
c0f136cd | 4708 | |
c7532b2d AC |
4709 | elsif Is_Scalar_Type (Typ) then |
4710 | if No (Predicate_Function (Typ)) then | |
4711 | Rewrite (Rop, | |
4712 | Make_Range (Loc, | |
4713 | Low_Bound => | |
4714 | Make_Attribute_Reference (Loc, | |
4715 | Attribute_Name => Name_First, | |
4716 | Prefix => New_Reference_To (Typ, Loc)), | |
4717 | ||
4718 | High_Bound => | |
4719 | Make_Attribute_Reference (Loc, | |
4720 | Attribute_Name => Name_Last, | |
4721 | Prefix => New_Reference_To (Typ, Loc)))); | |
4722 | Analyze_And_Resolve (N, Restyp); | |
4723 | end if; | |
70482933 | 4724 | |
4818e7b9 | 4725 | goto Leave; |
5d09245e AC |
4726 | |
4727 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
4728 | -- a membership test if the subtype mark denotes a constrained | |
4729 | -- Unchecked_Union subtype and the expression lacks inferable | |
4730 | -- discriminants. | |
4731 | ||
4732 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
4733 | and then Is_Constrained (Typ) | |
4734 | and then not Has_Inferable_Discriminants (Lop) | |
4735 | then | |
4736 | Insert_Action (N, | |
4737 | Make_Raise_Program_Error (Loc, | |
4738 | Reason => PE_Unchecked_Union_Restriction)); | |
4739 | ||
9a0ddeee AC |
4740 | -- Prevent Gigi from generating incorrect code by rewriting the |
4741 | -- test as False. | |
5d09245e | 4742 | |
9a0ddeee | 4743 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); |
4818e7b9 | 4744 | goto Leave; |
70482933 RK |
4745 | end if; |
4746 | ||
fbf5a39b AC |
4747 | -- Here we have a non-scalar type |
4748 | ||
70482933 RK |
4749 | if Is_Acc then |
4750 | Typ := Designated_Type (Typ); | |
4751 | end if; | |
4752 | ||
4753 | if not Is_Constrained (Typ) then | |
9a0ddeee | 4754 | Rewrite (N, New_Reference_To (Standard_True, Loc)); |
4818e7b9 | 4755 | Analyze_And_Resolve (N, Restyp); |
70482933 | 4756 | |
685094bf RD |
4757 | -- For the constrained array case, we have to check the subscripts |
4758 | -- for an exact match if the lengths are non-zero (the lengths | |
4759 | -- must match in any case). | |
70482933 RK |
4760 | |
4761 | elsif Is_Array_Type (Typ) then | |
fbf5a39b | 4762 | Check_Subscripts : declare |
9a0ddeee | 4763 | function Build_Attribute_Reference |
2e071734 AC |
4764 | (E : Node_Id; |
4765 | Nam : Name_Id; | |
4766 | Dim : Nat) return Node_Id; | |
9a0ddeee | 4767 | -- Build attribute reference E'Nam (Dim) |
70482933 | 4768 | |
9a0ddeee AC |
4769 | ------------------------------- |
4770 | -- Build_Attribute_Reference -- | |
4771 | ------------------------------- | |
fbf5a39b | 4772 | |
9a0ddeee | 4773 | function Build_Attribute_Reference |
2e071734 AC |
4774 | (E : Node_Id; |
4775 | Nam : Name_Id; | |
4776 | Dim : Nat) return Node_Id | |
70482933 RK |
4777 | is |
4778 | begin | |
4779 | return | |
4780 | Make_Attribute_Reference (Loc, | |
9a0ddeee | 4781 | Prefix => E, |
70482933 | 4782 | Attribute_Name => Nam, |
9a0ddeee | 4783 | Expressions => New_List ( |
70482933 | 4784 | Make_Integer_Literal (Loc, Dim))); |
9a0ddeee | 4785 | end Build_Attribute_Reference; |
70482933 | 4786 | |
fad0600d | 4787 | -- Start of processing for Check_Subscripts |
fbf5a39b | 4788 | |
70482933 RK |
4789 | begin |
4790 | for J in 1 .. Number_Dimensions (Typ) loop | |
4791 | Evolve_And_Then (Cond, | |
4792 | Make_Op_Eq (Loc, | |
4793 | Left_Opnd => | |
9a0ddeee | 4794 | Build_Attribute_Reference |
fbf5a39b AC |
4795 | (Duplicate_Subexpr_No_Checks (Obj), |
4796 | Name_First, J), | |
70482933 | 4797 | Right_Opnd => |
9a0ddeee | 4798 | Build_Attribute_Reference |
70482933 RK |
4799 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); |
4800 | ||
4801 | Evolve_And_Then (Cond, | |
4802 | Make_Op_Eq (Loc, | |
4803 | Left_Opnd => | |
9a0ddeee | 4804 | Build_Attribute_Reference |
fbf5a39b AC |
4805 | (Duplicate_Subexpr_No_Checks (Obj), |
4806 | Name_Last, J), | |
70482933 | 4807 | Right_Opnd => |
9a0ddeee | 4808 | Build_Attribute_Reference |
70482933 RK |
4809 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); |
4810 | end loop; | |
4811 | ||
4812 | if Is_Acc then | |
fbf5a39b AC |
4813 | Cond := |
4814 | Make_Or_Else (Loc, | |
4815 | Left_Opnd => | |
4816 | Make_Op_Eq (Loc, | |
4817 | Left_Opnd => Obj, | |
4818 | Right_Opnd => Make_Null (Loc)), | |
4819 | Right_Opnd => Cond); | |
70482933 RK |
4820 | end if; |
4821 | ||
4822 | Rewrite (N, Cond); | |
4818e7b9 | 4823 | Analyze_And_Resolve (N, Restyp); |
fbf5a39b | 4824 | end Check_Subscripts; |
70482933 | 4825 | |
685094bf RD |
4826 | -- These are the cases where constraint checks may be required, |
4827 | -- e.g. records with possible discriminants | |
70482933 RK |
4828 | |
4829 | else | |
4830 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
4831 | -- The expression that is built is the negation of the one that |
4832 | -- is used for checking discriminant constraints. | |
70482933 RK |
4833 | |
4834 | Obj := Relocate_Node (Left_Opnd (N)); | |
4835 | ||
4836 | if Has_Discriminants (Typ) then | |
4837 | Cond := Make_Op_Not (Loc, | |
4838 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
4839 | ||
4840 | if Is_Acc then | |
4841 | Cond := Make_Or_Else (Loc, | |
4842 | Left_Opnd => | |
4843 | Make_Op_Eq (Loc, | |
4844 | Left_Opnd => Obj, | |
4845 | Right_Opnd => Make_Null (Loc)), | |
4846 | Right_Opnd => Cond); | |
4847 | end if; | |
4848 | ||
4849 | else | |
4850 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
4851 | end if; | |
4852 | ||
4853 | Rewrite (N, Cond); | |
4818e7b9 | 4854 | Analyze_And_Resolve (N, Restyp); |
70482933 RK |
4855 | end if; |
4856 | end; | |
4857 | end if; | |
4818e7b9 RD |
4858 | |
4859 | -- At this point, we have done the processing required for the basic | |
4860 | -- membership test, but not yet dealt with the predicate. | |
4861 | ||
4862 | <<Leave>> | |
4863 | ||
c7532b2d AC |
4864 | -- If a predicate is present, then we do the predicate test, but we |
4865 | -- most certainly want to omit this if we are within the predicate | |
4866 | -- function itself, since otherwise we have an infinite recursion! | |
4818e7b9 | 4867 | |
c7532b2d AC |
4868 | declare |
4869 | PFunc : constant Entity_Id := Predicate_Function (Rtyp); | |
4818e7b9 | 4870 | |
c7532b2d AC |
4871 | begin |
4872 | if Present (PFunc) | |
4873 | and then Current_Scope /= PFunc | |
4874 | then | |
4875 | Rewrite (N, | |
4876 | Make_And_Then (Loc, | |
4877 | Left_Opnd => Relocate_Node (N), | |
4878 | Right_Opnd => Make_Predicate_Call (Rtyp, Lop))); | |
4818e7b9 | 4879 | |
c7532b2d AC |
4880 | -- Analyze new expression, mark left operand as analyzed to |
4881 | -- avoid infinite recursion adding predicate calls. | |
4818e7b9 | 4882 | |
c7532b2d AC |
4883 | Set_Analyzed (Left_Opnd (N)); |
4884 | Analyze_And_Resolve (N, Standard_Boolean); | |
4818e7b9 | 4885 | |
c7532b2d AC |
4886 | -- All done, skip attempt at compile time determination of result |
4887 | ||
4888 | return; | |
4889 | end if; | |
4890 | end; | |
70482933 RK |
4891 | end Expand_N_In; |
4892 | ||
4893 | -------------------------------- | |
4894 | -- Expand_N_Indexed_Component -- | |
4895 | -------------------------------- | |
4896 | ||
4897 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
4898 | Loc : constant Source_Ptr := Sloc (N); | |
4899 | Typ : constant Entity_Id := Etype (N); | |
4900 | P : constant Node_Id := Prefix (N); | |
4901 | T : constant Entity_Id := Etype (P); | |
4902 | ||
4903 | begin | |
685094bf RD |
4904 | -- A special optimization, if we have an indexed component that is |
4905 | -- selecting from a slice, then we can eliminate the slice, since, for | |
4906 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
4907 | -- the range check required by the slice. The range check for the slice | |
4908 | -- itself has already been generated. The range check for the | |
4909 | -- subscripting operation is ensured by converting the subject to | |
4910 | -- the subtype of the slice. | |
4911 | ||
4912 | -- This optimization not only generates better code, avoiding slice | |
4913 | -- messing especially in the packed case, but more importantly bypasses | |
4914 | -- some problems in handling this peculiar case, for example, the issue | |
4915 | -- of dealing specially with object renamings. | |
70482933 RK |
4916 | |
4917 | if Nkind (P) = N_Slice then | |
4918 | Rewrite (N, | |
4919 | Make_Indexed_Component (Loc, | |
4920 | Prefix => Prefix (P), | |
4921 | Expressions => New_List ( | |
4922 | Convert_To | |
4923 | (Etype (First_Index (Etype (P))), | |
4924 | First (Expressions (N)))))); | |
4925 | Analyze_And_Resolve (N, Typ); | |
4926 | return; | |
4927 | end if; | |
4928 | ||
b4592168 GD |
4929 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
4930 | -- function, then additional actuals must be passed. | |
4931 | ||
0791fbe9 | 4932 | if Ada_Version >= Ada_2005 |
b4592168 GD |
4933 | and then Is_Build_In_Place_Function_Call (P) |
4934 | then | |
4935 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
4936 | end if; | |
4937 | ||
685094bf | 4938 | -- If the prefix is an access type, then we unconditionally rewrite if |
09494c32 | 4939 | -- as an explicit dereference. This simplifies processing for several |
685094bf RD |
4940 | -- cases, including packed array cases and certain cases in which checks |
4941 | -- must be generated. We used to try to do this only when it was | |
4942 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
4943 | |
4944 | if Is_Access_Type (T) then | |
2717634d | 4945 | Insert_Explicit_Dereference (P); |
70482933 RK |
4946 | Analyze_And_Resolve (P, Designated_Type (T)); |
4947 | end if; | |
4948 | ||
fbf5a39b AC |
4949 | -- Generate index and validity checks |
4950 | ||
4951 | Generate_Index_Checks (N); | |
4952 | ||
70482933 RK |
4953 | if Validity_Checks_On and then Validity_Check_Subscripts then |
4954 | Apply_Subscript_Validity_Checks (N); | |
4955 | end if; | |
4956 | ||
4957 | -- All done for the non-packed case | |
4958 | ||
4959 | if not Is_Packed (Etype (Prefix (N))) then | |
4960 | return; | |
4961 | end if; | |
4962 | ||
4963 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 4964 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
4965 | -- we can always use the normal packed element get circuit. |
4966 | ||
4967 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
4968 | Expand_Packed_Element_Reference (N); | |
4969 | return; | |
4970 | end if; | |
4971 | ||
4972 | -- For a reference to a component of a bit packed array, we have to | |
4973 | -- convert it to a reference to the corresponding Packed_Array_Type. | |
4974 | -- We only want to do this for simple references, and not for: | |
4975 | ||
685094bf RD |
4976 | -- Left side of assignment, or prefix of left side of assignment, or |
4977 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
4978 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
4979 | ||
4980 | -- Renaming objects in renaming associations | |
4981 | -- This case is handled when a use of the renamed variable occurs | |
4982 | ||
4983 | -- Actual parameters for a procedure call | |
4984 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
4985 | ||
4986 | -- The second expression in a 'Read attribute reference | |
4987 | ||
47d3b920 | 4988 | -- The prefix of an address or bit or size attribute reference |
70482933 RK |
4989 | |
4990 | -- The following circuit detects these exceptions | |
4991 | ||
4992 | declare | |
4993 | Child : Node_Id := N; | |
4994 | Parnt : Node_Id := Parent (N); | |
4995 | ||
4996 | begin | |
4997 | loop | |
4998 | if Nkind (Parnt) = N_Unchecked_Expression then | |
4999 | null; | |
5000 | ||
303b4d58 AC |
5001 | elsif Nkind_In (Parnt, N_Object_Renaming_Declaration, |
5002 | N_Procedure_Call_Statement) | |
70482933 RK |
5003 | or else (Nkind (Parnt) = N_Parameter_Association |
5004 | and then | |
5005 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
5006 | then | |
5007 | return; | |
5008 | ||
5009 | elsif Nkind (Parnt) = N_Attribute_Reference | |
5010 | and then (Attribute_Name (Parnt) = Name_Address | |
5011 | or else | |
47d3b920 AC |
5012 | Attribute_Name (Parnt) = Name_Bit |
5013 | or else | |
70482933 RK |
5014 | Attribute_Name (Parnt) = Name_Size) |
5015 | and then Prefix (Parnt) = Child | |
5016 | then | |
5017 | return; | |
5018 | ||
5019 | elsif Nkind (Parnt) = N_Assignment_Statement | |
5020 | and then Name (Parnt) = Child | |
5021 | then | |
5022 | return; | |
5023 | ||
685094bf RD |
5024 | -- If the expression is an index of an indexed component, it must |
5025 | -- be expanded regardless of context. | |
fbf5a39b AC |
5026 | |
5027 | elsif Nkind (Parnt) = N_Indexed_Component | |
5028 | and then Child /= Prefix (Parnt) | |
5029 | then | |
5030 | Expand_Packed_Element_Reference (N); | |
5031 | return; | |
5032 | ||
5033 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
5034 | and then Name (Parent (Parnt)) = Parnt | |
5035 | then | |
5036 | return; | |
5037 | ||
70482933 RK |
5038 | elsif Nkind (Parnt) = N_Attribute_Reference |
5039 | and then Attribute_Name (Parnt) = Name_Read | |
5040 | and then Next (First (Expressions (Parnt))) = Child | |
5041 | then | |
5042 | return; | |
5043 | ||
303b4d58 | 5044 | elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component) |
70482933 RK |
5045 | and then Prefix (Parnt) = Child |
5046 | then | |
5047 | null; | |
5048 | ||
5049 | else | |
5050 | Expand_Packed_Element_Reference (N); | |
5051 | return; | |
5052 | end if; | |
5053 | ||
685094bf RD |
5054 | -- Keep looking up tree for unchecked expression, or if we are the |
5055 | -- prefix of a possible assignment left side. | |
70482933 RK |
5056 | |
5057 | Child := Parnt; | |
5058 | Parnt := Parent (Child); | |
5059 | end loop; | |
5060 | end; | |
70482933 RK |
5061 | end Expand_N_Indexed_Component; |
5062 | ||
5063 | --------------------- | |
5064 | -- Expand_N_Not_In -- | |
5065 | --------------------- | |
5066 | ||
5067 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
5068 | -- can be done. This avoids needing to duplicate this expansion code. | |
5069 | ||
5070 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
5071 | Loc : constant Source_Ptr := Sloc (N); |
5072 | Typ : constant Entity_Id := Etype (N); | |
5073 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
5074 | |
5075 | begin | |
5076 | Rewrite (N, | |
5077 | Make_Op_Not (Loc, | |
5078 | Right_Opnd => | |
5079 | Make_In (Loc, | |
5080 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 5081 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 5082 | |
197e4514 AC |
5083 | -- If this is a set membership, preserve list of alternatives |
5084 | ||
5085 | Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N))); | |
5086 | ||
d766cee3 | 5087 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 5088 | -- transformations in Expand_N_In). |
630d30e9 RD |
5089 | |
5090 | Set_Comes_From_Source (N, Cfs); | |
5091 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
5092 | ||
8fc789c8 | 5093 | -- Now analyze transformed node |
630d30e9 | 5094 | |
70482933 RK |
5095 | Analyze_And_Resolve (N, Typ); |
5096 | end Expand_N_Not_In; | |
5097 | ||
5098 | ------------------- | |
5099 | -- Expand_N_Null -- | |
5100 | ------------------- | |
5101 | ||
a3f2babd AC |
5102 | -- The only replacement required is for the case of a null of a type that |
5103 | -- is an access to protected subprogram, or a subtype thereof. We represent | |
5104 | -- such access values as a record, and so we must replace the occurrence of | |
5105 | -- null by the equivalent record (with a null address and a null pointer in | |
5106 | -- it), so that the backend creates the proper value. | |
70482933 RK |
5107 | |
5108 | procedure Expand_N_Null (N : Node_Id) is | |
5109 | Loc : constant Source_Ptr := Sloc (N); | |
a3f2babd | 5110 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
70482933 RK |
5111 | Agg : Node_Id; |
5112 | ||
5113 | begin | |
26bff3d9 | 5114 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
5115 | Agg := |
5116 | Make_Aggregate (Loc, | |
5117 | Expressions => New_List ( | |
5118 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
5119 | Make_Null (Loc))); | |
5120 | ||
5121 | Rewrite (N, Agg); | |
5122 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
5123 | ||
685094bf RD |
5124 | -- For subsequent semantic analysis, the node must retain its type. |
5125 | -- Gigi in any case replaces this type by the corresponding record | |
5126 | -- type before processing the node. | |
70482933 RK |
5127 | |
5128 | Set_Etype (N, Typ); | |
5129 | end if; | |
fbf5a39b AC |
5130 | |
5131 | exception | |
5132 | when RE_Not_Available => | |
5133 | return; | |
70482933 RK |
5134 | end Expand_N_Null; |
5135 | ||
5136 | --------------------- | |
5137 | -- Expand_N_Op_Abs -- | |
5138 | --------------------- | |
5139 | ||
5140 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
5141 | Loc : constant Source_Ptr := Sloc (N); | |
5142 | Expr : constant Node_Id := Right_Opnd (N); | |
5143 | ||
5144 | begin | |
5145 | Unary_Op_Validity_Checks (N); | |
5146 | ||
5147 | -- Deal with software overflow checking | |
5148 | ||
07fc65c4 | 5149 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
5150 | and then Is_Signed_Integer_Type (Etype (N)) |
5151 | and then Do_Overflow_Check (N) | |
5152 | then | |
685094bf RD |
5153 | -- The only case to worry about is when the argument is equal to the |
5154 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 5155 | |
fbf5a39b | 5156 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
5157 | |
5158 | -- with the usual Duplicate_Subexpr use coding for expr | |
5159 | ||
fbf5a39b AC |
5160 | Insert_Action (N, |
5161 | Make_Raise_Constraint_Error (Loc, | |
5162 | Condition => | |
5163 | Make_Op_Eq (Loc, | |
70482933 | 5164 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
5165 | Right_Opnd => |
5166 | Make_Attribute_Reference (Loc, | |
5167 | Prefix => | |
5168 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), | |
5169 | Attribute_Name => Name_First)), | |
5170 | Reason => CE_Overflow_Check_Failed)); | |
5171 | end if; | |
70482933 RK |
5172 | |
5173 | -- Vax floating-point types case | |
5174 | ||
fbf5a39b | 5175 | if Vax_Float (Etype (N)) then |
70482933 RK |
5176 | Expand_Vax_Arith (N); |
5177 | end if; | |
5178 | end Expand_N_Op_Abs; | |
5179 | ||
5180 | --------------------- | |
5181 | -- Expand_N_Op_Add -- | |
5182 | --------------------- | |
5183 | ||
5184 | procedure Expand_N_Op_Add (N : Node_Id) is | |
5185 | Typ : constant Entity_Id := Etype (N); | |
5186 | ||
5187 | begin | |
5188 | Binary_Op_Validity_Checks (N); | |
5189 | ||
5190 | -- N + 0 = 0 + N = N for integer types | |
5191 | ||
5192 | if Is_Integer_Type (Typ) then | |
5193 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
5194 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
5195 | then | |
5196 | Rewrite (N, Left_Opnd (N)); | |
5197 | return; | |
5198 | ||
5199 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
5200 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
5201 | then | |
5202 | Rewrite (N, Right_Opnd (N)); | |
5203 | return; | |
5204 | end if; | |
5205 | end if; | |
5206 | ||
fbf5a39b | 5207 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 RK |
5208 | |
5209 | if Is_Signed_Integer_Type (Typ) | |
5210 | or else Is_Fixed_Point_Type (Typ) | |
5211 | then | |
5212 | Apply_Arithmetic_Overflow_Check (N); | |
5213 | return; | |
5214 | ||
5215 | -- Vax floating-point types case | |
5216 | ||
5217 | elsif Vax_Float (Typ) then | |
5218 | Expand_Vax_Arith (N); | |
5219 | end if; | |
5220 | end Expand_N_Op_Add; | |
5221 | ||
5222 | --------------------- | |
5223 | -- Expand_N_Op_And -- | |
5224 | --------------------- | |
5225 | ||
5226 | procedure Expand_N_Op_And (N : Node_Id) is | |
5227 | Typ : constant Entity_Id := Etype (N); | |
5228 | ||
5229 | begin | |
5230 | Binary_Op_Validity_Checks (N); | |
5231 | ||
5232 | if Is_Array_Type (Etype (N)) then | |
5233 | Expand_Boolean_Operator (N); | |
5234 | ||
5235 | elsif Is_Boolean_Type (Etype (N)) then | |
6a2afd13 AC |
5236 | |
5237 | -- Replace AND by AND THEN if Short_Circuit_And_Or active and the | |
5238 | -- type is standard Boolean (do not mess with AND that uses a non- | |
5239 | -- standard Boolean type, because something strange is going on). | |
5240 | ||
5241 | if Short_Circuit_And_Or and then Typ = Standard_Boolean then | |
5242 | Rewrite (N, | |
5243 | Make_And_Then (Sloc (N), | |
5244 | Left_Opnd => Relocate_Node (Left_Opnd (N)), | |
5245 | Right_Opnd => Relocate_Node (Right_Opnd (N)))); | |
5246 | Analyze_And_Resolve (N, Typ); | |
5247 | ||
5248 | -- Otherwise, adjust conditions | |
5249 | ||
5250 | else | |
5251 | Adjust_Condition (Left_Opnd (N)); | |
5252 | Adjust_Condition (Right_Opnd (N)); | |
5253 | Set_Etype (N, Standard_Boolean); | |
5254 | Adjust_Result_Type (N, Typ); | |
5255 | end if; | |
437f8c1e AC |
5256 | |
5257 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
5258 | Expand_Intrinsic_Call (N, Entity (N)); | |
5259 | ||
70482933 RK |
5260 | end if; |
5261 | end Expand_N_Op_And; | |
5262 | ||
5263 | ------------------------ | |
5264 | -- Expand_N_Op_Concat -- | |
5265 | ------------------------ | |
5266 | ||
5267 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
5268 | Opnds : List_Id; |
5269 | -- List of operands to be concatenated | |
5270 | ||
70482933 | 5271 | Cnode : Node_Id; |
685094bf RD |
5272 | -- Node which is to be replaced by the result of concatenating the nodes |
5273 | -- in the list Opnds. | |
70482933 | 5274 | |
70482933 | 5275 | begin |
fbf5a39b AC |
5276 | -- Ensure validity of both operands |
5277 | ||
70482933 RK |
5278 | Binary_Op_Validity_Checks (N); |
5279 | ||
685094bf RD |
5280 | -- If we are the left operand of a concatenation higher up the tree, |
5281 | -- then do nothing for now, since we want to deal with a series of | |
5282 | -- concatenations as a unit. | |
70482933 RK |
5283 | |
5284 | if Nkind (Parent (N)) = N_Op_Concat | |
5285 | and then N = Left_Opnd (Parent (N)) | |
5286 | then | |
5287 | return; | |
5288 | end if; | |
5289 | ||
5290 | -- We get here with a concatenation whose left operand may be a | |
5291 | -- concatenation itself with a consistent type. We need to process | |
5292 | -- these concatenation operands from left to right, which means | |
5293 | -- from the deepest node in the tree to the highest node. | |
5294 | ||
5295 | Cnode := N; | |
5296 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
5297 | Cnode := Left_Opnd (Cnode); | |
5298 | end loop; | |
5299 | ||
64425dff BD |
5300 | -- Now Cnode is the deepest concatenation, and its parents are the |
5301 | -- concatenation nodes above, so now we process bottom up, doing the | |
5302 | -- operations. We gather a string that is as long as possible up to five | |
5303 | -- operands. | |
70482933 | 5304 | |
df46b832 AC |
5305 | -- The outer loop runs more than once if more than one concatenation |
5306 | -- type is involved. | |
70482933 RK |
5307 | |
5308 | Outer : loop | |
5309 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
5310 | Set_Parent (Opnds, N); | |
5311 | ||
df46b832 | 5312 | -- The inner loop gathers concatenation operands |
70482933 RK |
5313 | |
5314 | Inner : while Cnode /= N | |
70482933 RK |
5315 | and then Base_Type (Etype (Cnode)) = |
5316 | Base_Type (Etype (Parent (Cnode))) | |
5317 | loop | |
5318 | Cnode := Parent (Cnode); | |
5319 | Append (Right_Opnd (Cnode), Opnds); | |
5320 | end loop Inner; | |
5321 | ||
fdac1f80 | 5322 | Expand_Concatenate (Cnode, Opnds); |
70482933 RK |
5323 | |
5324 | exit Outer when Cnode = N; | |
5325 | Cnode := Parent (Cnode); | |
5326 | end loop Outer; | |
5327 | end Expand_N_Op_Concat; | |
5328 | ||
5329 | ------------------------ | |
5330 | -- Expand_N_Op_Divide -- | |
5331 | ------------------------ | |
5332 | ||
5333 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
5334 | Loc : constant Source_Ptr := Sloc (N); |
5335 | Lopnd : constant Node_Id := Left_Opnd (N); | |
5336 | Ropnd : constant Node_Id := Right_Opnd (N); | |
5337 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
5338 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
5339 | Typ : Entity_Id := Etype (N); | |
5340 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
5341 | and then | |
5342 | Compile_Time_Known_Value (Ropnd); | |
5343 | Rval : Uint; | |
70482933 RK |
5344 | |
5345 | begin | |
5346 | Binary_Op_Validity_Checks (N); | |
5347 | ||
f82944b7 JM |
5348 | if Rknow then |
5349 | Rval := Expr_Value (Ropnd); | |
5350 | end if; | |
5351 | ||
70482933 RK |
5352 | -- N / 1 = N for integer types |
5353 | ||
f82944b7 JM |
5354 | if Rknow and then Rval = Uint_1 then |
5355 | Rewrite (N, Lopnd); | |
70482933 RK |
5356 | return; |
5357 | end if; | |
5358 | ||
5359 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
5360 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
5361 | -- operand is an unsigned integer, as required for this to work. | |
5362 | ||
f82944b7 JM |
5363 | if Nkind (Ropnd) = N_Op_Expon |
5364 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
5365 | |
5366 | -- We cannot do this transformation in configurable run time mode if we | |
51bf9bdf | 5367 | -- have 64-bit integers and long shifts are not available. |
fbf5a39b AC |
5368 | |
5369 | and then | |
5370 | (Esize (Ltyp) <= 32 | |
5371 | or else Support_Long_Shifts_On_Target) | |
70482933 RK |
5372 | then |
5373 | Rewrite (N, | |
5374 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 5375 | Left_Opnd => Lopnd, |
70482933 | 5376 | Right_Opnd => |
f82944b7 | 5377 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
5378 | Analyze_And_Resolve (N, Typ); |
5379 | return; | |
5380 | end if; | |
5381 | ||
5382 | -- Do required fixup of universal fixed operation | |
5383 | ||
5384 | if Typ = Universal_Fixed then | |
5385 | Fixup_Universal_Fixed_Operation (N); | |
5386 | Typ := Etype (N); | |
5387 | end if; | |
5388 | ||
5389 | -- Divisions with fixed-point results | |
5390 | ||
5391 | if Is_Fixed_Point_Type (Typ) then | |
5392 | ||
685094bf RD |
5393 | -- No special processing if Treat_Fixed_As_Integer is set, since |
5394 | -- from a semantic point of view such operations are simply integer | |
5395 | -- operations and will be treated that way. | |
70482933 RK |
5396 | |
5397 | if not Treat_Fixed_As_Integer (N) then | |
5398 | if Is_Integer_Type (Rtyp) then | |
5399 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
5400 | else | |
5401 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
5402 | end if; | |
5403 | end if; | |
5404 | ||
685094bf RD |
5405 | -- Other cases of division of fixed-point operands. Again we exclude the |
5406 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 RK |
5407 | |
5408 | elsif (Is_Fixed_Point_Type (Ltyp) or else | |
5409 | Is_Fixed_Point_Type (Rtyp)) | |
5410 | and then not Treat_Fixed_As_Integer (N) | |
5411 | then | |
5412 | if Is_Integer_Type (Typ) then | |
5413 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
5414 | else | |
5415 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
5416 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
5417 | end if; | |
5418 | ||
685094bf RD |
5419 | -- Mixed-mode operations can appear in a non-static universal context, |
5420 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
5421 | |
5422 | elsif Typ = Universal_Real | |
5423 | and then Is_Integer_Type (Rtyp) | |
5424 | then | |
f82944b7 JM |
5425 | Rewrite (Ropnd, |
5426 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 5427 | |
f82944b7 | 5428 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 RK |
5429 | |
5430 | elsif Typ = Universal_Real | |
5431 | and then Is_Integer_Type (Ltyp) | |
5432 | then | |
f82944b7 JM |
5433 | Rewrite (Lopnd, |
5434 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 5435 | |
f82944b7 | 5436 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 5437 | |
f02b8bb8 | 5438 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
5439 | |
5440 | elsif Is_Integer_Type (Typ) then | |
5441 | Apply_Divide_Check (N); | |
fbf5a39b | 5442 | |
f82944b7 JM |
5443 | -- Check for 64-bit division available, or long shifts if the divisor |
5444 | -- is a small power of 2 (since such divides will be converted into | |
1147c704 | 5445 | -- long shifts). |
fbf5a39b AC |
5446 | |
5447 | if Esize (Ltyp) > 32 | |
5448 | and then not Support_64_Bit_Divides_On_Target | |
f82944b7 JM |
5449 | and then |
5450 | (not Rknow | |
5451 | or else not Support_Long_Shifts_On_Target | |
5452 | or else (Rval /= Uint_2 and then | |
5453 | Rval /= Uint_4 and then | |
5454 | Rval /= Uint_8 and then | |
5455 | Rval /= Uint_16 and then | |
5456 | Rval /= Uint_32 and then | |
5457 | Rval /= Uint_64)) | |
fbf5a39b AC |
5458 | then |
5459 | Error_Msg_CRT ("64-bit division", N); | |
5460 | end if; | |
f02b8bb8 RD |
5461 | |
5462 | -- Deal with Vax_Float | |
5463 | ||
5464 | elsif Vax_Float (Typ) then | |
5465 | Expand_Vax_Arith (N); | |
5466 | return; | |
70482933 RK |
5467 | end if; |
5468 | end Expand_N_Op_Divide; | |
5469 | ||
5470 | -------------------- | |
5471 | -- Expand_N_Op_Eq -- | |
5472 | -------------------- | |
5473 | ||
5474 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
5475 | Loc : constant Source_Ptr := Sloc (N); |
5476 | Typ : constant Entity_Id := Etype (N); | |
5477 | Lhs : constant Node_Id := Left_Opnd (N); | |
5478 | Rhs : constant Node_Id := Right_Opnd (N); | |
5479 | Bodies : constant List_Id := New_List; | |
5480 | A_Typ : constant Entity_Id := Etype (Lhs); | |
5481 | ||
70482933 RK |
5482 | Typl : Entity_Id := A_Typ; |
5483 | Op_Name : Entity_Id; | |
5484 | Prim : Elmt_Id; | |
70482933 RK |
5485 | |
5486 | procedure Build_Equality_Call (Eq : Entity_Id); | |
5487 | -- If a constructed equality exists for the type or for its parent, | |
5488 | -- build and analyze call, adding conversions if the operation is | |
5489 | -- inherited. | |
5490 | ||
5d09245e | 5491 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
8fc789c8 | 5492 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
5493 | -- Unchecked_Union subtype. Typ is a record type. |
5494 | ||
70482933 RK |
5495 | ------------------------- |
5496 | -- Build_Equality_Call -- | |
5497 | ------------------------- | |
5498 | ||
5499 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
5500 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
5501 | L_Exp : Node_Id := Relocate_Node (Lhs); | |
5502 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
5503 | ||
5504 | begin | |
5505 | if Base_Type (Op_Type) /= Base_Type (A_Typ) | |
5506 | and then not Is_Class_Wide_Type (A_Typ) | |
5507 | then | |
5508 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
5509 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
5510 | end if; | |
5511 | ||
5d09245e AC |
5512 | -- If we have an Unchecked_Union, we need to add the inferred |
5513 | -- discriminant values as actuals in the function call. At this | |
5514 | -- point, the expansion has determined that both operands have | |
5515 | -- inferable discriminants. | |
5516 | ||
5517 | if Is_Unchecked_Union (Op_Type) then | |
5518 | declare | |
5519 | Lhs_Type : constant Node_Id := Etype (L_Exp); | |
5520 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
5521 | Lhs_Discr_Val : Node_Id; | |
5522 | Rhs_Discr_Val : Node_Id; | |
5523 | ||
5524 | begin | |
5525 | -- Per-object constrained selected components require special | |
5526 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 5527 | -- Unchecked_Union, we cannot reference its discriminants |
5d09245e AC |
5528 | -- directly. This is why we use the two extra parameters of |
5529 | -- the equality function of the enclosing Unchecked_Union. | |
5530 | ||
5531 | -- type UU_Type (Discr : Integer := 0) is | |
5532 | -- . . . | |
5533 | -- end record; | |
5534 | -- pragma Unchecked_Union (UU_Type); | |
5535 | ||
5536 | -- 1. Unchecked_Union enclosing record: | |
5537 | ||
5538 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
5539 | -- . . . | |
5540 | -- Comp : UU_Type (Discr); | |
5541 | -- . . . | |
5542 | -- end Enclosing_UU_Type; | |
5543 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
5544 | ||
5545 | -- Obj1 : Enclosing_UU_Type; | |
5546 | -- Obj2 : Enclosing_UU_Type (1); | |
5547 | ||
2717634d | 5548 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
5549 | |
5550 | -- Generated code: | |
5551 | ||
5552 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
5553 | ||
5554 | -- A and B are the formal parameters of the equality function | |
5555 | -- of Enclosing_UU_Type. The function always has two extra | |
5556 | -- formals to capture the inferred discriminant values. | |
5557 | ||
5558 | -- 2. Non-Unchecked_Union enclosing record: | |
5559 | ||
5560 | -- type | |
5561 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
5562 | -- is record | |
5563 | -- . . . | |
5564 | -- Comp : UU_Type (Discr); | |
5565 | -- . . . | |
5566 | -- end Enclosing_Non_UU_Type; | |
5567 | ||
5568 | -- Obj1 : Enclosing_Non_UU_Type; | |
5569 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
5570 | ||
630d30e9 | 5571 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
5572 | |
5573 | -- Generated code: | |
5574 | ||
5575 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
5576 | -- obj1.discr, obj2.discr)) then | |
5577 | ||
5578 | -- In this case we can directly reference the discriminants of | |
5579 | -- the enclosing record. | |
5580 | ||
5581 | -- Lhs of equality | |
5582 | ||
5583 | if Nkind (Lhs) = N_Selected_Component | |
5e1c00fa RD |
5584 | and then Has_Per_Object_Constraint |
5585 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
5586 | then |
5587 | -- Enclosing record is an Unchecked_Union, use formal A | |
5588 | ||
7675ad4f AC |
5589 | if Is_Unchecked_Union |
5590 | (Scope (Entity (Selector_Name (Lhs)))) | |
5d09245e | 5591 | then |
7675ad4f | 5592 | Lhs_Discr_Val := Make_Identifier (Loc, Name_A); |
5d09245e AC |
5593 | |
5594 | -- Enclosing record is of a non-Unchecked_Union type, it is | |
5595 | -- possible to reference the discriminant. | |
5596 | ||
5597 | else | |
5598 | Lhs_Discr_Val := | |
5599 | Make_Selected_Component (Loc, | |
5600 | Prefix => Prefix (Lhs), | |
5601 | Selector_Name => | |
5e1c00fa RD |
5602 | New_Copy |
5603 | (Get_Discriminant_Value | |
5604 | (First_Discriminant (Lhs_Type), | |
5605 | Lhs_Type, | |
5606 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
5607 | end if; |
5608 | ||
5609 | -- Comment needed here ??? | |
5610 | ||
5611 | else | |
5612 | -- Infer the discriminant value | |
5613 | ||
5614 | Lhs_Discr_Val := | |
5e1c00fa RD |
5615 | New_Copy |
5616 | (Get_Discriminant_Value | |
5617 | (First_Discriminant (Lhs_Type), | |
5618 | Lhs_Type, | |
5619 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
5620 | end if; |
5621 | ||
5622 | -- Rhs of equality | |
5623 | ||
5624 | if Nkind (Rhs) = N_Selected_Component | |
5e1c00fa RD |
5625 | and then Has_Per_Object_Constraint |
5626 | (Entity (Selector_Name (Rhs))) | |
5d09245e | 5627 | then |
5e1c00fa RD |
5628 | if Is_Unchecked_Union |
5629 | (Scope (Entity (Selector_Name (Rhs)))) | |
5d09245e | 5630 | then |
7675ad4f | 5631 | Rhs_Discr_Val := Make_Identifier (Loc, Name_B); |
5d09245e AC |
5632 | |
5633 | else | |
5634 | Rhs_Discr_Val := | |
5635 | Make_Selected_Component (Loc, | |
5636 | Prefix => Prefix (Rhs), | |
5637 | Selector_Name => | |
5638 | New_Copy (Get_Discriminant_Value ( | |
5639 | First_Discriminant (Rhs_Type), | |
5640 | Rhs_Type, | |
5641 | Stored_Constraint (Rhs_Type)))); | |
5642 | ||
5643 | end if; | |
5644 | else | |
5645 | Rhs_Discr_Val := | |
5646 | New_Copy (Get_Discriminant_Value ( | |
5647 | First_Discriminant (Rhs_Type), | |
5648 | Rhs_Type, | |
5649 | Stored_Constraint (Rhs_Type))); | |
5650 | ||
5651 | end if; | |
5652 | ||
5653 | Rewrite (N, | |
5654 | Make_Function_Call (Loc, | |
5655 | Name => New_Reference_To (Eq, Loc), | |
5656 | Parameter_Associations => New_List ( | |
5657 | L_Exp, | |
5658 | R_Exp, | |
5659 | Lhs_Discr_Val, | |
5660 | Rhs_Discr_Val))); | |
5661 | end; | |
5662 | ||
5663 | -- Normal case, not an unchecked union | |
5664 | ||
5665 | else | |
5666 | Rewrite (N, | |
5667 | Make_Function_Call (Loc, | |
5668 | Name => New_Reference_To (Eq, Loc), | |
5669 | Parameter_Associations => New_List (L_Exp, R_Exp))); | |
5670 | end if; | |
70482933 RK |
5671 | |
5672 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5673 | end Build_Equality_Call; | |
5674 | ||
5d09245e AC |
5675 | ------------------------------------ |
5676 | -- Has_Unconstrained_UU_Component -- | |
5677 | ------------------------------------ | |
5678 | ||
5679 | function Has_Unconstrained_UU_Component | |
5680 | (Typ : Node_Id) return Boolean | |
5681 | is | |
5682 | Tdef : constant Node_Id := | |
57848bf7 | 5683 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
5684 | Clist : Node_Id; |
5685 | Vpart : Node_Id; | |
5686 | ||
5687 | function Component_Is_Unconstrained_UU | |
5688 | (Comp : Node_Id) return Boolean; | |
5689 | -- Determines whether the subtype of the component is an | |
5690 | -- unconstrained Unchecked_Union. | |
5691 | ||
5692 | function Variant_Is_Unconstrained_UU | |
5693 | (Variant : Node_Id) return Boolean; | |
5694 | -- Determines whether a component of the variant has an unconstrained | |
5695 | -- Unchecked_Union subtype. | |
5696 | ||
5697 | ----------------------------------- | |
5698 | -- Component_Is_Unconstrained_UU -- | |
5699 | ----------------------------------- | |
5700 | ||
5701 | function Component_Is_Unconstrained_UU | |
5702 | (Comp : Node_Id) return Boolean | |
5703 | is | |
5704 | begin | |
5705 | if Nkind (Comp) /= N_Component_Declaration then | |
5706 | return False; | |
5707 | end if; | |
5708 | ||
5709 | declare | |
5710 | Sindic : constant Node_Id := | |
5711 | Subtype_Indication (Component_Definition (Comp)); | |
5712 | ||
5713 | begin | |
5714 | -- Unconstrained nominal type. In the case of a constraint | |
5715 | -- present, the node kind would have been N_Subtype_Indication. | |
5716 | ||
5717 | if Nkind (Sindic) = N_Identifier then | |
5718 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
5719 | end if; | |
5720 | ||
5721 | return False; | |
5722 | end; | |
5723 | end Component_Is_Unconstrained_UU; | |
5724 | ||
5725 | --------------------------------- | |
5726 | -- Variant_Is_Unconstrained_UU -- | |
5727 | --------------------------------- | |
5728 | ||
5729 | function Variant_Is_Unconstrained_UU | |
5730 | (Variant : Node_Id) return Boolean | |
5731 | is | |
5732 | Clist : constant Node_Id := Component_List (Variant); | |
5733 | ||
5734 | begin | |
5735 | if Is_Empty_List (Component_Items (Clist)) then | |
5736 | return False; | |
5737 | end if; | |
5738 | ||
f02b8bb8 RD |
5739 | -- We only need to test one component |
5740 | ||
5d09245e AC |
5741 | declare |
5742 | Comp : Node_Id := First (Component_Items (Clist)); | |
5743 | ||
5744 | begin | |
5745 | while Present (Comp) loop | |
5d09245e AC |
5746 | if Component_Is_Unconstrained_UU (Comp) then |
5747 | return True; | |
5748 | end if; | |
5749 | ||
5750 | Next (Comp); | |
5751 | end loop; | |
5752 | end; | |
5753 | ||
5754 | -- None of the components withing the variant were of | |
5755 | -- unconstrained Unchecked_Union type. | |
5756 | ||
5757 | return False; | |
5758 | end Variant_Is_Unconstrained_UU; | |
5759 | ||
5760 | -- Start of processing for Has_Unconstrained_UU_Component | |
5761 | ||
5762 | begin | |
5763 | if Null_Present (Tdef) then | |
5764 | return False; | |
5765 | end if; | |
5766 | ||
5767 | Clist := Component_List (Tdef); | |
5768 | Vpart := Variant_Part (Clist); | |
5769 | ||
5770 | -- Inspect available components | |
5771 | ||
5772 | if Present (Component_Items (Clist)) then | |
5773 | declare | |
5774 | Comp : Node_Id := First (Component_Items (Clist)); | |
5775 | ||
5776 | begin | |
5777 | while Present (Comp) loop | |
5778 | ||
8fc789c8 | 5779 | -- One component is sufficient |
5d09245e AC |
5780 | |
5781 | if Component_Is_Unconstrained_UU (Comp) then | |
5782 | return True; | |
5783 | end if; | |
5784 | ||
5785 | Next (Comp); | |
5786 | end loop; | |
5787 | end; | |
5788 | end if; | |
5789 | ||
5790 | -- Inspect available components withing variants | |
5791 | ||
5792 | if Present (Vpart) then | |
5793 | declare | |
5794 | Variant : Node_Id := First (Variants (Vpart)); | |
5795 | ||
5796 | begin | |
5797 | while Present (Variant) loop | |
5798 | ||
8fc789c8 | 5799 | -- One component within a variant is sufficient |
5d09245e AC |
5800 | |
5801 | if Variant_Is_Unconstrained_UU (Variant) then | |
5802 | return True; | |
5803 | end if; | |
5804 | ||
5805 | Next (Variant); | |
5806 | end loop; | |
5807 | end; | |
5808 | end if; | |
5809 | ||
5810 | -- Neither the available components, nor the components inside the | |
5811 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
5812 | ||
5813 | return False; | |
5814 | end Has_Unconstrained_UU_Component; | |
5815 | ||
70482933 RK |
5816 | -- Start of processing for Expand_N_Op_Eq |
5817 | ||
5818 | begin | |
5819 | Binary_Op_Validity_Checks (N); | |
5820 | ||
5821 | if Ekind (Typl) = E_Private_Type then | |
5822 | Typl := Underlying_Type (Typl); | |
70482933 RK |
5823 | elsif Ekind (Typl) = E_Private_Subtype then |
5824 | Typl := Underlying_Type (Base_Type (Typl)); | |
f02b8bb8 RD |
5825 | else |
5826 | null; | |
70482933 RK |
5827 | end if; |
5828 | ||
5829 | -- It may happen in error situations that the underlying type is not | |
5830 | -- set. The error will be detected later, here we just defend the | |
5831 | -- expander code. | |
5832 | ||
5833 | if No (Typl) then | |
5834 | return; | |
5835 | end if; | |
5836 | ||
5837 | Typl := Base_Type (Typl); | |
5838 | ||
70482933 RK |
5839 | -- Boolean types (requiring handling of non-standard case) |
5840 | ||
f02b8bb8 | 5841 | if Is_Boolean_Type (Typl) then |
70482933 RK |
5842 | Adjust_Condition (Left_Opnd (N)); |
5843 | Adjust_Condition (Right_Opnd (N)); | |
5844 | Set_Etype (N, Standard_Boolean); | |
5845 | Adjust_Result_Type (N, Typ); | |
5846 | ||
5847 | -- Array types | |
5848 | ||
5849 | elsif Is_Array_Type (Typl) then | |
5850 | ||
1033834f RD |
5851 | -- If we are doing full validity checking, and it is possible for the |
5852 | -- array elements to be invalid then expand out array comparisons to | |
5853 | -- make sure that we check the array elements. | |
fbf5a39b | 5854 | |
1033834f RD |
5855 | if Validity_Check_Operands |
5856 | and then not Is_Known_Valid (Component_Type (Typl)) | |
5857 | then | |
fbf5a39b AC |
5858 | declare |
5859 | Save_Force_Validity_Checks : constant Boolean := | |
5860 | Force_Validity_Checks; | |
5861 | begin | |
5862 | Force_Validity_Checks := True; | |
5863 | Rewrite (N, | |
0da2c8ac AC |
5864 | Expand_Array_Equality |
5865 | (N, | |
5866 | Relocate_Node (Lhs), | |
5867 | Relocate_Node (Rhs), | |
5868 | Bodies, | |
5869 | Typl)); | |
5870 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
5871 | Analyze_And_Resolve (N, Standard_Boolean); |
5872 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
5873 | end; | |
5874 | ||
a9d8907c | 5875 | -- Packed case where both operands are known aligned |
70482933 | 5876 | |
a9d8907c JM |
5877 | elsif Is_Bit_Packed_Array (Typl) |
5878 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
5879 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
5880 | then | |
70482933 RK |
5881 | Expand_Packed_Eq (N); |
5882 | ||
5e1c00fa RD |
5883 | -- Where the component type is elementary we can use a block bit |
5884 | -- comparison (if supported on the target) exception in the case | |
5885 | -- of floating-point (negative zero issues require element by | |
5886 | -- element comparison), and atomic types (where we must be sure | |
a9d8907c | 5887 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 5888 | |
70482933 RK |
5889 | elsif Is_Elementary_Type (Component_Type (Typl)) |
5890 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 5891 | and then not Is_Atomic (Component_Type (Typl)) |
a9d8907c JM |
5892 | and then not Is_Possibly_Unaligned_Object (Lhs) |
5893 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
fbf5a39b | 5894 | and then Support_Composite_Compare_On_Target |
70482933 RK |
5895 | then |
5896 | null; | |
5897 | ||
685094bf RD |
5898 | -- For composite and floating-point cases, expand equality loop to |
5899 | -- make sure of using proper comparisons for tagged types, and | |
5900 | -- correctly handling the floating-point case. | |
70482933 RK |
5901 | |
5902 | else | |
5903 | Rewrite (N, | |
0da2c8ac AC |
5904 | Expand_Array_Equality |
5905 | (N, | |
5906 | Relocate_Node (Lhs), | |
5907 | Relocate_Node (Rhs), | |
5908 | Bodies, | |
5909 | Typl)); | |
70482933 RK |
5910 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
5911 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5912 | end if; | |
5913 | ||
5914 | -- Record Types | |
5915 | ||
5916 | elsif Is_Record_Type (Typl) then | |
5917 | ||
5918 | -- For tagged types, use the primitive "=" | |
5919 | ||
5920 | if Is_Tagged_Type (Typl) then | |
5921 | ||
0669bebe GB |
5922 | -- No need to do anything else compiling under restriction |
5923 | -- No_Dispatching_Calls. During the semantic analysis we | |
5924 | -- already notified such violation. | |
5925 | ||
5926 | if Restriction_Active (No_Dispatching_Calls) then | |
5927 | return; | |
5928 | end if; | |
5929 | ||
685094bf RD |
5930 | -- If this is derived from an untagged private type completed with |
5931 | -- a tagged type, it does not have a full view, so we use the | |
5932 | -- primitive operations of the private type. This check should no | |
5933 | -- longer be necessary when these types get their full views??? | |
70482933 RK |
5934 | |
5935 | if Is_Private_Type (A_Typ) | |
5936 | and then not Is_Tagged_Type (A_Typ) | |
5937 | and then Is_Derived_Type (A_Typ) | |
5938 | and then No (Full_View (A_Typ)) | |
5939 | then | |
685094bf RD |
5940 | -- Search for equality operation, checking that the operands |
5941 | -- have the same type. Note that we must find a matching entry, | |
5942 | -- or something is very wrong! | |
2e071734 | 5943 | |
70482933 RK |
5944 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
5945 | ||
2e071734 AC |
5946 | while Present (Prim) loop |
5947 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
5948 | and then Etype (First_Formal (Node (Prim))) = | |
5949 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
5950 | and then | |
5951 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
5952 | ||
70482933 | 5953 | Next_Elmt (Prim); |
70482933 RK |
5954 | end loop; |
5955 | ||
2e071734 | 5956 | pragma Assert (Present (Prim)); |
70482933 | 5957 | Op_Name := Node (Prim); |
fbf5a39b AC |
5958 | |
5959 | -- Find the type's predefined equality or an overriding | |
685094bf | 5960 | -- user- defined equality. The reason for not simply calling |
fbf5a39b | 5961 | -- Find_Prim_Op here is that there may be a user-defined |
685094bf RD |
5962 | -- overloaded equality op that precedes the equality that we want, |
5963 | -- so we have to explicitly search (e.g., there could be an | |
5964 | -- equality with two different parameter types). | |
fbf5a39b | 5965 | |
70482933 | 5966 | else |
fbf5a39b AC |
5967 | if Is_Class_Wide_Type (Typl) then |
5968 | Typl := Root_Type (Typl); | |
5969 | end if; | |
5970 | ||
5971 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
5972 | while Present (Prim) loop |
5973 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
5974 | and then Etype (First_Formal (Node (Prim))) = | |
5975 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
5976 | and then |
5977 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
5978 | |
5979 | Next_Elmt (Prim); | |
fbf5a39b AC |
5980 | end loop; |
5981 | ||
2e071734 | 5982 | pragma Assert (Present (Prim)); |
fbf5a39b | 5983 | Op_Name := Node (Prim); |
70482933 RK |
5984 | end if; |
5985 | ||
5986 | Build_Equality_Call (Op_Name); | |
5987 | ||
5d09245e AC |
5988 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
5989 | -- predefined equality operator for a type which has a subcomponent | |
5990 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
5991 | ||
5992 | elsif Has_Unconstrained_UU_Component (Typl) then | |
5993 | Insert_Action (N, | |
5994 | Make_Raise_Program_Error (Loc, | |
5995 | Reason => PE_Unchecked_Union_Restriction)); | |
5996 | ||
5997 | -- Prevent Gigi from generating incorrect code by rewriting the | |
5998 | -- equality as a standard False. | |
5999 | ||
6000 | Rewrite (N, | |
6001 | New_Occurrence_Of (Standard_False, Loc)); | |
6002 | ||
6003 | elsif Is_Unchecked_Union (Typl) then | |
6004 | ||
6005 | -- If we can infer the discriminants of the operands, we make a | |
6006 | -- call to the TSS equality function. | |
6007 | ||
6008 | if Has_Inferable_Discriminants (Lhs) | |
6009 | and then | |
6010 | Has_Inferable_Discriminants (Rhs) | |
6011 | then | |
6012 | Build_Equality_Call | |
6013 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
6014 | ||
6015 | else | |
6016 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
6017 | -- the predefined equality operator for an Unchecked_Union type | |
6018 | -- if either of the operands lack inferable discriminants. | |
6019 | ||
6020 | Insert_Action (N, | |
6021 | Make_Raise_Program_Error (Loc, | |
6022 | Reason => PE_Unchecked_Union_Restriction)); | |
6023 | ||
6024 | -- Prevent Gigi from generating incorrect code by rewriting | |
6025 | -- the equality as a standard False. | |
6026 | ||
6027 | Rewrite (N, | |
6028 | New_Occurrence_Of (Standard_False, Loc)); | |
6029 | ||
6030 | end if; | |
6031 | ||
70482933 RK |
6032 | -- If a type support function is present (for complex cases), use it |
6033 | ||
fbf5a39b AC |
6034 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
6035 | Build_Equality_Call | |
6036 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 RK |
6037 | |
6038 | -- Otherwise expand the component by component equality. Note that | |
8fc789c8 | 6039 | -- we never use block-bit comparisons for records, because of the |
70482933 RK |
6040 | -- problems with gaps. The backend will often be able to recombine |
6041 | -- the separate comparisons that we generate here. | |
6042 | ||
6043 | else | |
6044 | Remove_Side_Effects (Lhs); | |
6045 | Remove_Side_Effects (Rhs); | |
6046 | Rewrite (N, | |
6047 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
6048 | ||
6049 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
6050 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
6051 | end if; | |
6052 | end if; | |
6053 | ||
d26dc4b5 | 6054 | -- Test if result is known at compile time |
70482933 | 6055 | |
d26dc4b5 | 6056 | Rewrite_Comparison (N); |
f02b8bb8 RD |
6057 | |
6058 | -- If we still have comparison for Vax_Float, process it | |
6059 | ||
6060 | if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then | |
6061 | Expand_Vax_Comparison (N); | |
6062 | return; | |
6063 | end if; | |
0580d807 AC |
6064 | |
6065 | Optimize_Length_Comparison (N); | |
70482933 RK |
6066 | end Expand_N_Op_Eq; |
6067 | ||
6068 | ----------------------- | |
6069 | -- Expand_N_Op_Expon -- | |
6070 | ----------------------- | |
6071 | ||
6072 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
6073 | Loc : constant Source_Ptr := Sloc (N); | |
6074 | Typ : constant Entity_Id := Etype (N); | |
6075 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
6076 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 6077 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
6078 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
6079 | Exptyp : constant Entity_Id := Etype (Exp); | |
6080 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
6081 | Expv : Uint; | |
6082 | Xnode : Node_Id; | |
6083 | Temp : Node_Id; | |
6084 | Rent : RE_Id; | |
6085 | Ent : Entity_Id; | |
fbf5a39b | 6086 | Etyp : Entity_Id; |
70482933 RK |
6087 | |
6088 | begin | |
6089 | Binary_Op_Validity_Checks (N); | |
6090 | ||
685094bf RD |
6091 | -- If either operand is of a private type, then we have the use of an |
6092 | -- intrinsic operator, and we get rid of the privateness, by using root | |
6093 | -- types of underlying types for the actual operation. Otherwise the | |
6094 | -- private types will cause trouble if we expand multiplications or | |
6095 | -- shifts etc. We also do this transformation if the result type is | |
6096 | -- different from the base type. | |
07fc65c4 GB |
6097 | |
6098 | if Is_Private_Type (Etype (Base)) | |
6099 | or else | |
6100 | Is_Private_Type (Typ) | |
6101 | or else | |
6102 | Is_Private_Type (Exptyp) | |
6103 | or else | |
6104 | Rtyp /= Root_Type (Bastyp) | |
6105 | then | |
6106 | declare | |
6107 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
6108 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
6109 | ||
6110 | begin | |
6111 | Rewrite (N, | |
6112 | Unchecked_Convert_To (Typ, | |
6113 | Make_Op_Expon (Loc, | |
6114 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
6115 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
6116 | Analyze_And_Resolve (N, Typ); | |
6117 | return; | |
6118 | end; | |
6119 | end if; | |
6120 | ||
fbf5a39b | 6121 | -- Test for case of known right argument |
70482933 RK |
6122 | |
6123 | if Compile_Time_Known_Value (Exp) then | |
6124 | Expv := Expr_Value (Exp); | |
6125 | ||
6126 | -- We only fold small non-negative exponents. You might think we | |
6127 | -- could fold small negative exponents for the real case, but we | |
6128 | -- can't because we are required to raise Constraint_Error for | |
6129 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
6130 | -- See ACVC test C4A012B. | |
6131 | ||
6132 | if Expv >= 0 and then Expv <= 4 then | |
6133 | ||
6134 | -- X ** 0 = 1 (or 1.0) | |
6135 | ||
6136 | if Expv = 0 then | |
abcbd24c ST |
6137 | |
6138 | -- Call Remove_Side_Effects to ensure that any side effects | |
6139 | -- in the ignored left operand (in particular function calls | |
6140 | -- to user defined functions) are properly executed. | |
6141 | ||
6142 | Remove_Side_Effects (Base); | |
6143 | ||
70482933 RK |
6144 | if Ekind (Typ) in Integer_Kind then |
6145 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
6146 | else | |
6147 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
6148 | end if; | |
6149 | ||
6150 | -- X ** 1 = X | |
6151 | ||
6152 | elsif Expv = 1 then | |
6153 | Xnode := Base; | |
6154 | ||
6155 | -- X ** 2 = X * X | |
6156 | ||
6157 | elsif Expv = 2 then | |
6158 | Xnode := | |
6159 | Make_Op_Multiply (Loc, | |
6160 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 6161 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
6162 | |
6163 | -- X ** 3 = X * X * X | |
6164 | ||
6165 | elsif Expv = 3 then | |
6166 | Xnode := | |
6167 | Make_Op_Multiply (Loc, | |
6168 | Left_Opnd => | |
6169 | Make_Op_Multiply (Loc, | |
6170 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
6171 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
6172 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
6173 | |
6174 | -- X ** 4 -> | |
6175 | -- En : constant base'type := base * base; | |
6176 | -- ... | |
6177 | -- En * En | |
6178 | ||
6179 | else -- Expv = 4 | |
191fcb3a | 6180 | Temp := Make_Temporary (Loc, 'E', Base); |
70482933 RK |
6181 | |
6182 | Insert_Actions (N, New_List ( | |
6183 | Make_Object_Declaration (Loc, | |
6184 | Defining_Identifier => Temp, | |
6185 | Constant_Present => True, | |
6186 | Object_Definition => New_Reference_To (Typ, Loc), | |
6187 | Expression => | |
6188 | Make_Op_Multiply (Loc, | |
6189 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 6190 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base))))); |
70482933 RK |
6191 | |
6192 | Xnode := | |
6193 | Make_Op_Multiply (Loc, | |
6194 | Left_Opnd => New_Reference_To (Temp, Loc), | |
6195 | Right_Opnd => New_Reference_To (Temp, Loc)); | |
6196 | end if; | |
6197 | ||
6198 | Rewrite (N, Xnode); | |
6199 | Analyze_And_Resolve (N, Typ); | |
6200 | return; | |
6201 | end if; | |
6202 | end if; | |
6203 | ||
6204 | -- Case of (2 ** expression) appearing as an argument of an integer | |
6205 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 6206 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
6207 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
6208 | -- of the higher level node converts it into a shift. | |
6209 | ||
51bf9bdf AC |
6210 | -- Another case is 2 ** N in any other context. We simply convert |
6211 | -- this to 1 * 2 ** N, and then the above transformation applies. | |
6212 | ||
685094bf RD |
6213 | -- Note: this transformation is not applicable for a modular type with |
6214 | -- a non-binary modulus in the multiplication case, since we get a wrong | |
6215 | -- result if the shift causes an overflow before the modular reduction. | |
6216 | ||
70482933 RK |
6217 | if Nkind (Base) = N_Integer_Literal |
6218 | and then Intval (Base) = 2 | |
6219 | and then Is_Integer_Type (Root_Type (Exptyp)) | |
6220 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
6221 | and then Is_Unsigned_Type (Exptyp) | |
6222 | and then not Ovflo | |
70482933 | 6223 | then |
51bf9bdf | 6224 | -- First the multiply and divide cases |
70482933 | 6225 | |
51bf9bdf AC |
6226 | if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then |
6227 | declare | |
6228 | P : constant Node_Id := Parent (N); | |
6229 | L : constant Node_Id := Left_Opnd (P); | |
6230 | R : constant Node_Id := Right_Opnd (P); | |
6231 | ||
6232 | begin | |
6233 | if (Nkind (P) = N_Op_Multiply | |
6234 | and then not Non_Binary_Modulus (Typ) | |
6235 | and then | |
6236 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
6237 | or else | |
6238 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
6239 | and then not Do_Overflow_Check (P)) | |
6240 | or else | |
6241 | (Nkind (P) = N_Op_Divide | |
6242 | and then Is_Integer_Type (Etype (L)) | |
6243 | and then Is_Unsigned_Type (Etype (L)) | |
6244 | and then R = N | |
6245 | and then not Do_Overflow_Check (P)) | |
6246 | then | |
6247 | Set_Is_Power_Of_2_For_Shift (N); | |
6248 | return; | |
6249 | end if; | |
6250 | end; | |
6251 | ||
6252 | -- Now the other cases | |
6253 | ||
6254 | elsif not Non_Binary_Modulus (Typ) then | |
6255 | Rewrite (N, | |
6256 | Make_Op_Multiply (Loc, | |
6257 | Left_Opnd => Make_Integer_Literal (Loc, 1), | |
6258 | Right_Opnd => Relocate_Node (N))); | |
6259 | Analyze_And_Resolve (N, Typ); | |
6260 | return; | |
6261 | end if; | |
70482933 RK |
6262 | end if; |
6263 | ||
07fc65c4 GB |
6264 | -- Fall through if exponentiation must be done using a runtime routine |
6265 | ||
07fc65c4 | 6266 | -- First deal with modular case |
70482933 RK |
6267 | |
6268 | if Is_Modular_Integer_Type (Rtyp) then | |
6269 | ||
6270 | -- Non-binary case, we call the special exponentiation routine for | |
6271 | -- the non-binary case, converting the argument to Long_Long_Integer | |
6272 | -- and passing the modulus value. Then the result is converted back | |
6273 | -- to the base type. | |
6274 | ||
6275 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
6276 | Rewrite (N, |
6277 | Convert_To (Typ, | |
6278 | Make_Function_Call (Loc, | |
6279 | Name => New_Reference_To (RTE (RE_Exp_Modular), Loc), | |
6280 | Parameter_Associations => New_List ( | |
6281 | Convert_To (Standard_Integer, Base), | |
6282 | Make_Integer_Literal (Loc, Modulus (Rtyp)), | |
6283 | Exp)))); | |
6284 | ||
685094bf RD |
6285 | -- Binary case, in this case, we call one of two routines, either the |
6286 | -- unsigned integer case, or the unsigned long long integer case, | |
6287 | -- with a final "and" operation to do the required mod. | |
70482933 RK |
6288 | |
6289 | else | |
6290 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
6291 | Ent := RTE (RE_Exp_Unsigned); | |
6292 | else | |
6293 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
6294 | end if; | |
6295 | ||
6296 | Rewrite (N, | |
6297 | Convert_To (Typ, | |
6298 | Make_Op_And (Loc, | |
6299 | Left_Opnd => | |
6300 | Make_Function_Call (Loc, | |
6301 | Name => New_Reference_To (Ent, Loc), | |
6302 | Parameter_Associations => New_List ( | |
6303 | Convert_To (Etype (First_Formal (Ent)), Base), | |
6304 | Exp)), | |
6305 | Right_Opnd => | |
6306 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
6307 | ||
6308 | end if; | |
6309 | ||
6310 | -- Common exit point for modular type case | |
6311 | ||
6312 | Analyze_And_Resolve (N, Typ); | |
6313 | return; | |
6314 | ||
fbf5a39b AC |
6315 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
6316 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
6317 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 6318 | -- might need certification when a certified run time is required. |
70482933 | 6319 | |
fbf5a39b | 6320 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
6321 | -- checks are required, and one when they are not required, since there |
6322 | -- is a real gain in omitting checks on many machines. | |
70482933 | 6323 | |
fbf5a39b AC |
6324 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
6325 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
6326 | and then | |
6327 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
6328 | or else (Rtyp = Universal_Integer) | |
70482933 | 6329 | then |
fbf5a39b AC |
6330 | Etyp := Standard_Long_Long_Integer; |
6331 | ||
70482933 RK |
6332 | if Ovflo then |
6333 | Rent := RE_Exp_Long_Long_Integer; | |
6334 | else | |
6335 | Rent := RE_Exn_Long_Long_Integer; | |
6336 | end if; | |
6337 | ||
fbf5a39b AC |
6338 | elsif Is_Signed_Integer_Type (Rtyp) then |
6339 | Etyp := Standard_Integer; | |
70482933 RK |
6340 | |
6341 | if Ovflo then | |
fbf5a39b | 6342 | Rent := RE_Exp_Integer; |
70482933 | 6343 | else |
fbf5a39b | 6344 | Rent := RE_Exn_Integer; |
70482933 | 6345 | end if; |
fbf5a39b AC |
6346 | |
6347 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
6348 | -- need separate routines for the overflow case here, since in the case | |
6349 | -- of floating-point, we generate infinities anyway as a rule (either | |
6350 | -- that or we automatically trap overflow), and if there is an infinity | |
6351 | -- generated and a range check is required, the check will fail anyway. | |
6352 | ||
6353 | else | |
6354 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
6355 | Etyp := Standard_Long_Long_Float; | |
6356 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
6357 | end if; |
6358 | ||
6359 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 6360 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 6361 | |
fbf5a39b | 6362 | if Typ = Etyp |
70482933 RK |
6363 | and then Rtyp /= Universal_Integer |
6364 | and then Rtyp /= Universal_Real | |
6365 | then | |
6366 | Rewrite (N, | |
6367 | Make_Function_Call (Loc, | |
6368 | Name => New_Reference_To (RTE (Rent), Loc), | |
6369 | Parameter_Associations => New_List (Base, Exp))); | |
6370 | ||
6371 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 6372 | -- required in the universal cases, since the runtime routine is |
1147c704 | 6373 | -- typed using one of the standard types). |
70482933 RK |
6374 | |
6375 | else | |
6376 | Rewrite (N, | |
6377 | Convert_To (Typ, | |
6378 | Make_Function_Call (Loc, | |
6379 | Name => New_Reference_To (RTE (Rent), Loc), | |
6380 | Parameter_Associations => New_List ( | |
fbf5a39b | 6381 | Convert_To (Etyp, Base), |
70482933 RK |
6382 | Exp)))); |
6383 | end if; | |
6384 | ||
6385 | Analyze_And_Resolve (N, Typ); | |
6386 | return; | |
6387 | ||
fbf5a39b AC |
6388 | exception |
6389 | when RE_Not_Available => | |
6390 | return; | |
70482933 RK |
6391 | end Expand_N_Op_Expon; |
6392 | ||
6393 | -------------------- | |
6394 | -- Expand_N_Op_Ge -- | |
6395 | -------------------- | |
6396 | ||
6397 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
6398 | Typ : constant Entity_Id := Etype (N); | |
6399 | Op1 : constant Node_Id := Left_Opnd (N); | |
6400 | Op2 : constant Node_Id := Right_Opnd (N); | |
6401 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6402 | ||
6403 | begin | |
6404 | Binary_Op_Validity_Checks (N); | |
6405 | ||
f02b8bb8 | 6406 | if Is_Array_Type (Typ1) then |
70482933 RK |
6407 | Expand_Array_Comparison (N); |
6408 | return; | |
6409 | end if; | |
6410 | ||
6411 | if Is_Boolean_Type (Typ1) then | |
6412 | Adjust_Condition (Op1); | |
6413 | Adjust_Condition (Op2); | |
6414 | Set_Etype (N, Standard_Boolean); | |
6415 | Adjust_Result_Type (N, Typ); | |
6416 | end if; | |
6417 | ||
6418 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6419 | |
6420 | -- If we still have comparison, and Vax_Float type, process it | |
6421 | ||
6422 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6423 | Expand_Vax_Comparison (N); | |
6424 | return; | |
6425 | end if; | |
0580d807 AC |
6426 | |
6427 | Optimize_Length_Comparison (N); | |
70482933 RK |
6428 | end Expand_N_Op_Ge; |
6429 | ||
6430 | -------------------- | |
6431 | -- Expand_N_Op_Gt -- | |
6432 | -------------------- | |
6433 | ||
6434 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
6435 | Typ : constant Entity_Id := Etype (N); | |
6436 | Op1 : constant Node_Id := Left_Opnd (N); | |
6437 | Op2 : constant Node_Id := Right_Opnd (N); | |
6438 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6439 | ||
6440 | begin | |
6441 | Binary_Op_Validity_Checks (N); | |
6442 | ||
f02b8bb8 | 6443 | if Is_Array_Type (Typ1) then |
70482933 RK |
6444 | Expand_Array_Comparison (N); |
6445 | return; | |
6446 | end if; | |
6447 | ||
6448 | if Is_Boolean_Type (Typ1) then | |
6449 | Adjust_Condition (Op1); | |
6450 | Adjust_Condition (Op2); | |
6451 | Set_Etype (N, Standard_Boolean); | |
6452 | Adjust_Result_Type (N, Typ); | |
6453 | end if; | |
6454 | ||
6455 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6456 | |
6457 | -- If we still have comparison, and Vax_Float type, process it | |
6458 | ||
6459 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6460 | Expand_Vax_Comparison (N); | |
6461 | return; | |
6462 | end if; | |
0580d807 AC |
6463 | |
6464 | Optimize_Length_Comparison (N); | |
70482933 RK |
6465 | end Expand_N_Op_Gt; |
6466 | ||
6467 | -------------------- | |
6468 | -- Expand_N_Op_Le -- | |
6469 | -------------------- | |
6470 | ||
6471 | procedure Expand_N_Op_Le (N : Node_Id) is | |
6472 | Typ : constant Entity_Id := Etype (N); | |
6473 | Op1 : constant Node_Id := Left_Opnd (N); | |
6474 | Op2 : constant Node_Id := Right_Opnd (N); | |
6475 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6476 | ||
6477 | begin | |
6478 | Binary_Op_Validity_Checks (N); | |
6479 | ||
f02b8bb8 | 6480 | if Is_Array_Type (Typ1) then |
70482933 RK |
6481 | Expand_Array_Comparison (N); |
6482 | return; | |
6483 | end if; | |
6484 | ||
6485 | if Is_Boolean_Type (Typ1) then | |
6486 | Adjust_Condition (Op1); | |
6487 | Adjust_Condition (Op2); | |
6488 | Set_Etype (N, Standard_Boolean); | |
6489 | Adjust_Result_Type (N, Typ); | |
6490 | end if; | |
6491 | ||
6492 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6493 | |
6494 | -- If we still have comparison, and Vax_Float type, process it | |
6495 | ||
6496 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6497 | Expand_Vax_Comparison (N); | |
6498 | return; | |
6499 | end if; | |
0580d807 AC |
6500 | |
6501 | Optimize_Length_Comparison (N); | |
70482933 RK |
6502 | end Expand_N_Op_Le; |
6503 | ||
6504 | -------------------- | |
6505 | -- Expand_N_Op_Lt -- | |
6506 | -------------------- | |
6507 | ||
6508 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
6509 | Typ : constant Entity_Id := Etype (N); | |
6510 | Op1 : constant Node_Id := Left_Opnd (N); | |
6511 | Op2 : constant Node_Id := Right_Opnd (N); | |
6512 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6513 | ||
6514 | begin | |
6515 | Binary_Op_Validity_Checks (N); | |
6516 | ||
f02b8bb8 | 6517 | if Is_Array_Type (Typ1) then |
70482933 RK |
6518 | Expand_Array_Comparison (N); |
6519 | return; | |
6520 | end if; | |
6521 | ||
6522 | if Is_Boolean_Type (Typ1) then | |
6523 | Adjust_Condition (Op1); | |
6524 | Adjust_Condition (Op2); | |
6525 | Set_Etype (N, Standard_Boolean); | |
6526 | Adjust_Result_Type (N, Typ); | |
6527 | end if; | |
6528 | ||
6529 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6530 | |
6531 | -- If we still have comparison, and Vax_Float type, process it | |
6532 | ||
6533 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6534 | Expand_Vax_Comparison (N); | |
6535 | return; | |
6536 | end if; | |
0580d807 AC |
6537 | |
6538 | Optimize_Length_Comparison (N); | |
70482933 RK |
6539 | end Expand_N_Op_Lt; |
6540 | ||
6541 | ----------------------- | |
6542 | -- Expand_N_Op_Minus -- | |
6543 | ----------------------- | |
6544 | ||
6545 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
6546 | Loc : constant Source_Ptr := Sloc (N); | |
6547 | Typ : constant Entity_Id := Etype (N); | |
6548 | ||
6549 | begin | |
6550 | Unary_Op_Validity_Checks (N); | |
6551 | ||
07fc65c4 | 6552 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
6553 | and then Is_Signed_Integer_Type (Etype (N)) |
6554 | and then Do_Overflow_Check (N) | |
6555 | then | |
6556 | -- Software overflow checking expands -expr into (0 - expr) | |
6557 | ||
6558 | Rewrite (N, | |
6559 | Make_Op_Subtract (Loc, | |
6560 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
6561 | Right_Opnd => Right_Opnd (N))); | |
6562 | ||
6563 | Analyze_And_Resolve (N, Typ); | |
6564 | ||
6565 | -- Vax floating-point types case | |
6566 | ||
6567 | elsif Vax_Float (Etype (N)) then | |
6568 | Expand_Vax_Arith (N); | |
6569 | end if; | |
6570 | end Expand_N_Op_Minus; | |
6571 | ||
6572 | --------------------- | |
6573 | -- Expand_N_Op_Mod -- | |
6574 | --------------------- | |
6575 | ||
6576 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
6577 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 6578 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
6579 | Left : constant Node_Id := Left_Opnd (N); |
6580 | Right : constant Node_Id := Right_Opnd (N); | |
6581 | DOC : constant Boolean := Do_Overflow_Check (N); | |
6582 | DDC : constant Boolean := Do_Division_Check (N); | |
6583 | ||
6584 | LLB : Uint; | |
6585 | Llo : Uint; | |
6586 | Lhi : Uint; | |
6587 | LOK : Boolean; | |
6588 | Rlo : Uint; | |
6589 | Rhi : Uint; | |
6590 | ROK : Boolean; | |
6591 | ||
1033834f RD |
6592 | pragma Warnings (Off, Lhi); |
6593 | ||
70482933 RK |
6594 | begin |
6595 | Binary_Op_Validity_Checks (N); | |
6596 | ||
5d5e9775 AC |
6597 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
6598 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); | |
70482933 RK |
6599 | |
6600 | -- Convert mod to rem if operands are known non-negative. We do this | |
6601 | -- since it is quite likely that this will improve the quality of code, | |
6602 | -- (the operation now corresponds to the hardware remainder), and it | |
6603 | -- does not seem likely that it could be harmful. | |
6604 | ||
6605 | if LOK and then Llo >= 0 | |
6606 | and then | |
6607 | ROK and then Rlo >= 0 | |
6608 | then | |
6609 | Rewrite (N, | |
6610 | Make_Op_Rem (Sloc (N), | |
6611 | Left_Opnd => Left_Opnd (N), | |
6612 | Right_Opnd => Right_Opnd (N))); | |
6613 | ||
685094bf RD |
6614 | -- Instead of reanalyzing the node we do the analysis manually. This |
6615 | -- avoids anomalies when the replacement is done in an instance and | |
6616 | -- is epsilon more efficient. | |
70482933 RK |
6617 | |
6618 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 6619 | Set_Etype (N, Typ); |
70482933 RK |
6620 | Set_Do_Overflow_Check (N, DOC); |
6621 | Set_Do_Division_Check (N, DDC); | |
6622 | Expand_N_Op_Rem (N); | |
6623 | Set_Analyzed (N); | |
6624 | ||
6625 | -- Otherwise, normal mod processing | |
6626 | ||
6627 | else | |
6628 | if Is_Integer_Type (Etype (N)) then | |
6629 | Apply_Divide_Check (N); | |
6630 | end if; | |
6631 | ||
fbf5a39b AC |
6632 | -- Apply optimization x mod 1 = 0. We don't really need that with |
6633 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
6634 | -- certainly harmless. | |
6635 | ||
6636 | if Is_Integer_Type (Etype (N)) | |
6637 | and then Compile_Time_Known_Value (Right) | |
6638 | and then Expr_Value (Right) = Uint_1 | |
6639 | then | |
abcbd24c ST |
6640 | -- Call Remove_Side_Effects to ensure that any side effects in |
6641 | -- the ignored left operand (in particular function calls to | |
6642 | -- user defined functions) are properly executed. | |
6643 | ||
6644 | Remove_Side_Effects (Left); | |
6645 | ||
fbf5a39b AC |
6646 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
6647 | Analyze_And_Resolve (N, Typ); | |
6648 | return; | |
6649 | end if; | |
6650 | ||
70482933 RK |
6651 | -- Deal with annoying case of largest negative number remainder |
6652 | -- minus one. Gigi does not handle this case correctly, because | |
6653 | -- it generates a divide instruction which may trap in this case. | |
6654 | ||
685094bf RD |
6655 | -- In fact the check is quite easy, if the right operand is -1, then |
6656 | -- the mod value is always 0, and we can just ignore the left operand | |
6657 | -- completely in this case. | |
70482933 | 6658 | |
30783513 | 6659 | -- The operand type may be private (e.g. in the expansion of an |
685094bf RD |
6660 | -- intrinsic operation) so we must use the underlying type to get the |
6661 | -- bounds, and convert the literals explicitly. | |
fbf5a39b AC |
6662 | |
6663 | LLB := | |
6664 | Expr_Value | |
6665 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
70482933 RK |
6666 | |
6667 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
6668 | and then | |
6669 | ((not LOK) or else (Llo = LLB)) | |
6670 | then | |
6671 | Rewrite (N, | |
6672 | Make_Conditional_Expression (Loc, | |
6673 | Expressions => New_List ( | |
6674 | Make_Op_Eq (Loc, | |
6675 | Left_Opnd => Duplicate_Subexpr (Right), | |
6676 | Right_Opnd => | |
fbf5a39b AC |
6677 | Unchecked_Convert_To (Typ, |
6678 | Make_Integer_Literal (Loc, -1))), | |
6679 | Unchecked_Convert_To (Typ, | |
6680 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
6681 | Relocate_Node (N)))); |
6682 | ||
6683 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
fbf5a39b | 6684 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
6685 | end if; |
6686 | end if; | |
6687 | end Expand_N_Op_Mod; | |
6688 | ||
6689 | -------------------------- | |
6690 | -- Expand_N_Op_Multiply -- | |
6691 | -------------------------- | |
6692 | ||
6693 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
6694 | Loc : constant Source_Ptr := Sloc (N); |
6695 | Lop : constant Node_Id := Left_Opnd (N); | |
6696 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 6697 | |
abcbd24c ST |
6698 | Lp2 : constant Boolean := |
6699 | Nkind (Lop) = N_Op_Expon | |
6700 | and then Is_Power_Of_2_For_Shift (Lop); | |
fbf5a39b | 6701 | |
abcbd24c ST |
6702 | Rp2 : constant Boolean := |
6703 | Nkind (Rop) = N_Op_Expon | |
6704 | and then Is_Power_Of_2_For_Shift (Rop); | |
fbf5a39b | 6705 | |
70482933 RK |
6706 | Ltyp : constant Entity_Id := Etype (Lop); |
6707 | Rtyp : constant Entity_Id := Etype (Rop); | |
6708 | Typ : Entity_Id := Etype (N); | |
6709 | ||
6710 | begin | |
6711 | Binary_Op_Validity_Checks (N); | |
6712 | ||
6713 | -- Special optimizations for integer types | |
6714 | ||
6715 | if Is_Integer_Type (Typ) then | |
6716 | ||
abcbd24c | 6717 | -- N * 0 = 0 for integer types |
70482933 | 6718 | |
abcbd24c ST |
6719 | if Compile_Time_Known_Value (Rop) |
6720 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 6721 | then |
abcbd24c ST |
6722 | -- Call Remove_Side_Effects to ensure that any side effects in |
6723 | -- the ignored left operand (in particular function calls to | |
6724 | -- user defined functions) are properly executed. | |
6725 | ||
6726 | Remove_Side_Effects (Lop); | |
6727 | ||
6728 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
6729 | Analyze_And_Resolve (N, Typ); | |
6730 | return; | |
6731 | end if; | |
6732 | ||
6733 | -- Similar handling for 0 * N = 0 | |
6734 | ||
6735 | if Compile_Time_Known_Value (Lop) | |
6736 | and then Expr_Value (Lop) = Uint_0 | |
6737 | then | |
6738 | Remove_Side_Effects (Rop); | |
70482933 RK |
6739 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
6740 | Analyze_And_Resolve (N, Typ); | |
6741 | return; | |
6742 | end if; | |
6743 | ||
6744 | -- N * 1 = 1 * N = N for integer types | |
6745 | ||
fbf5a39b AC |
6746 | -- This optimisation is not done if we are going to |
6747 | -- rewrite the product 1 * 2 ** N to a shift. | |
6748 | ||
6749 | if Compile_Time_Known_Value (Rop) | |
6750 | and then Expr_Value (Rop) = Uint_1 | |
6751 | and then not Lp2 | |
70482933 | 6752 | then |
fbf5a39b | 6753 | Rewrite (N, Lop); |
70482933 RK |
6754 | return; |
6755 | ||
fbf5a39b AC |
6756 | elsif Compile_Time_Known_Value (Lop) |
6757 | and then Expr_Value (Lop) = Uint_1 | |
6758 | and then not Rp2 | |
70482933 | 6759 | then |
fbf5a39b | 6760 | Rewrite (N, Rop); |
70482933 RK |
6761 | return; |
6762 | end if; | |
6763 | end if; | |
6764 | ||
70482933 RK |
6765 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
6766 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
6767 | -- operand is an integer, as required for this to work. | |
6768 | ||
fbf5a39b AC |
6769 | if Rp2 then |
6770 | if Lp2 then | |
70482933 | 6771 | |
fbf5a39b | 6772 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
6773 | |
6774 | Rewrite (N, | |
6775 | Make_Op_Expon (Loc, | |
6776 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
6777 | Right_Opnd => | |
6778 | Make_Op_Add (Loc, | |
6779 | Left_Opnd => Right_Opnd (Lop), | |
6780 | Right_Opnd => Right_Opnd (Rop)))); | |
6781 | Analyze_And_Resolve (N, Typ); | |
6782 | return; | |
6783 | ||
6784 | else | |
6785 | Rewrite (N, | |
6786 | Make_Op_Shift_Left (Loc, | |
6787 | Left_Opnd => Lop, | |
6788 | Right_Opnd => | |
6789 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
6790 | Analyze_And_Resolve (N, Typ); | |
6791 | return; | |
6792 | end if; | |
6793 | ||
6794 | -- Same processing for the operands the other way round | |
6795 | ||
fbf5a39b | 6796 | elsif Lp2 then |
70482933 RK |
6797 | Rewrite (N, |
6798 | Make_Op_Shift_Left (Loc, | |
6799 | Left_Opnd => Rop, | |
6800 | Right_Opnd => | |
6801 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
6802 | Analyze_And_Resolve (N, Typ); | |
6803 | return; | |
6804 | end if; | |
6805 | ||
6806 | -- Do required fixup of universal fixed operation | |
6807 | ||
6808 | if Typ = Universal_Fixed then | |
6809 | Fixup_Universal_Fixed_Operation (N); | |
6810 | Typ := Etype (N); | |
6811 | end if; | |
6812 | ||
6813 | -- Multiplications with fixed-point results | |
6814 | ||
6815 | if Is_Fixed_Point_Type (Typ) then | |
6816 | ||
685094bf RD |
6817 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
6818 | -- a semantic point of view such operations are simply integer | |
6819 | -- operations and will be treated that way. | |
70482933 RK |
6820 | |
6821 | if not Treat_Fixed_As_Integer (N) then | |
6822 | ||
6823 | -- Case of fixed * integer => fixed | |
6824 | ||
6825 | if Is_Integer_Type (Rtyp) then | |
6826 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
6827 | ||
6828 | -- Case of integer * fixed => fixed | |
6829 | ||
6830 | elsif Is_Integer_Type (Ltyp) then | |
6831 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
6832 | ||
6833 | -- Case of fixed * fixed => fixed | |
6834 | ||
6835 | else | |
6836 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
6837 | end if; | |
6838 | end if; | |
6839 | ||
685094bf RD |
6840 | -- Other cases of multiplication of fixed-point operands. Again we |
6841 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
6842 | |
6843 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
6844 | and then not Treat_Fixed_As_Integer (N) | |
6845 | then | |
6846 | if Is_Integer_Type (Typ) then | |
6847 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
6848 | else | |
6849 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
6850 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
6851 | end if; | |
6852 | ||
685094bf RD |
6853 | -- Mixed-mode operations can appear in a non-static universal context, |
6854 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
6855 | |
6856 | elsif Typ = Universal_Real | |
6857 | and then Is_Integer_Type (Rtyp) | |
6858 | then | |
6859 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); | |
6860 | ||
6861 | Analyze_And_Resolve (Rop, Universal_Real); | |
6862 | ||
6863 | elsif Typ = Universal_Real | |
6864 | and then Is_Integer_Type (Ltyp) | |
6865 | then | |
6866 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); | |
6867 | ||
6868 | Analyze_And_Resolve (Lop, Universal_Real); | |
6869 | ||
6870 | -- Non-fixed point cases, check software overflow checking required | |
6871 | ||
6872 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
6873 | Apply_Arithmetic_Overflow_Check (N); | |
f02b8bb8 RD |
6874 | |
6875 | -- Deal with VAX float case | |
6876 | ||
6877 | elsif Vax_Float (Typ) then | |
6878 | Expand_Vax_Arith (N); | |
6879 | return; | |
70482933 RK |
6880 | end if; |
6881 | end Expand_N_Op_Multiply; | |
6882 | ||
6883 | -------------------- | |
6884 | -- Expand_N_Op_Ne -- | |
6885 | -------------------- | |
6886 | ||
70482933 | 6887 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 6888 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
6889 | |
6890 | begin | |
f02b8bb8 | 6891 | -- Case of elementary type with standard operator |
70482933 | 6892 | |
f02b8bb8 RD |
6893 | if Is_Elementary_Type (Typ) |
6894 | and then Sloc (Entity (N)) = Standard_Location | |
6895 | then | |
6896 | Binary_Op_Validity_Checks (N); | |
70482933 | 6897 | |
f02b8bb8 | 6898 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 6899 | |
f02b8bb8 RD |
6900 | if Is_Boolean_Type (Typ) then |
6901 | Adjust_Condition (Left_Opnd (N)); | |
6902 | Adjust_Condition (Right_Opnd (N)); | |
6903 | Set_Etype (N, Standard_Boolean); | |
6904 | Adjust_Result_Type (N, Typ); | |
6905 | end if; | |
fbf5a39b | 6906 | |
f02b8bb8 RD |
6907 | Rewrite_Comparison (N); |
6908 | ||
6909 | -- If we still have comparison for Vax_Float, process it | |
6910 | ||
6911 | if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then | |
6912 | Expand_Vax_Comparison (N); | |
6913 | return; | |
6914 | end if; | |
6915 | ||
6916 | -- For all cases other than elementary types, we rewrite node as the | |
6917 | -- negation of an equality operation, and reanalyze. The equality to be | |
6918 | -- used is defined in the same scope and has the same signature. This | |
6919 | -- signature must be set explicitly since in an instance it may not have | |
6920 | -- the same visibility as in the generic unit. This avoids duplicating | |
6921 | -- or factoring the complex code for record/array equality tests etc. | |
6922 | ||
6923 | else | |
6924 | declare | |
6925 | Loc : constant Source_Ptr := Sloc (N); | |
6926 | Neg : Node_Id; | |
6927 | Ne : constant Entity_Id := Entity (N); | |
6928 | ||
6929 | begin | |
6930 | Binary_Op_Validity_Checks (N); | |
6931 | ||
6932 | Neg := | |
6933 | Make_Op_Not (Loc, | |
6934 | Right_Opnd => | |
6935 | Make_Op_Eq (Loc, | |
6936 | Left_Opnd => Left_Opnd (N), | |
6937 | Right_Opnd => Right_Opnd (N))); | |
6938 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
6939 | ||
6940 | if Scope (Ne) /= Standard_Standard then | |
6941 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
6942 | end if; | |
6943 | ||
4637729f | 6944 | -- For navigation purposes, we want to treat the inequality as an |
f02b8bb8 | 6945 | -- implicit reference to the corresponding equality. Preserve the |
4637729f | 6946 | -- Comes_From_ source flag to generate proper Xref entries. |
f02b8bb8 RD |
6947 | |
6948 | Preserve_Comes_From_Source (Neg, N); | |
6949 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
6950 | Rewrite (N, Neg); | |
6951 | Analyze_And_Resolve (N, Standard_Boolean); | |
6952 | end; | |
6953 | end if; | |
0580d807 AC |
6954 | |
6955 | Optimize_Length_Comparison (N); | |
70482933 RK |
6956 | end Expand_N_Op_Ne; |
6957 | ||
6958 | --------------------- | |
6959 | -- Expand_N_Op_Not -- | |
6960 | --------------------- | |
6961 | ||
685094bf | 6962 | -- If the argument is other than a Boolean array type, there is no special |
c77599d5 | 6963 | -- expansion required, except for VMS operations on signed integers. |
70482933 RK |
6964 | |
6965 | -- For the packed case, we call the special routine in Exp_Pakd, except | |
6966 | -- that if the component size is greater than one, we use the standard | |
6967 | -- routine generating a gruesome loop (it is so peculiar to have packed | |
685094bf RD |
6968 | -- arrays with non-standard Boolean representations anyway, so it does not |
6969 | -- matter that we do not handle this case efficiently). | |
70482933 | 6970 | |
685094bf RD |
6971 | -- For the unpacked case (and for the special packed case where we have non |
6972 | -- standard Booleans, as discussed above), we generate and insert into the | |
6973 | -- tree the following function definition: | |
70482933 RK |
6974 | |
6975 | -- function Nnnn (A : arr) is | |
6976 | -- B : arr; | |
6977 | -- begin | |
6978 | -- for J in a'range loop | |
6979 | -- B (J) := not A (J); | |
6980 | -- end loop; | |
6981 | -- return B; | |
6982 | -- end Nnnn; | |
6983 | ||
6984 | -- Here arr is the actual subtype of the parameter (and hence always | |
6985 | -- constrained). Then we replace the not with a call to this function. | |
6986 | ||
6987 | procedure Expand_N_Op_Not (N : Node_Id) is | |
6988 | Loc : constant Source_Ptr := Sloc (N); | |
6989 | Typ : constant Entity_Id := Etype (N); | |
6990 | Opnd : Node_Id; | |
6991 | Arr : Entity_Id; | |
6992 | A : Entity_Id; | |
6993 | B : Entity_Id; | |
6994 | J : Entity_Id; | |
6995 | A_J : Node_Id; | |
6996 | B_J : Node_Id; | |
6997 | ||
6998 | Func_Name : Entity_Id; | |
6999 | Loop_Statement : Node_Id; | |
7000 | ||
7001 | begin | |
7002 | Unary_Op_Validity_Checks (N); | |
7003 | ||
7004 | -- For boolean operand, deal with non-standard booleans | |
7005 | ||
7006 | if Is_Boolean_Type (Typ) then | |
7007 | Adjust_Condition (Right_Opnd (N)); | |
7008 | Set_Etype (N, Standard_Boolean); | |
7009 | Adjust_Result_Type (N, Typ); | |
7010 | return; | |
7011 | end if; | |
7012 | ||
880dabb5 AC |
7013 | -- For the VMS "not" on signed integer types, use conversion to and from |
7014 | -- a predefined modular type. | |
c77599d5 AC |
7015 | |
7016 | if Is_VMS_Operator (Entity (N)) then | |
7017 | declare | |
9bebf0e9 AC |
7018 | Rtyp : Entity_Id; |
7019 | Utyp : Entity_Id; | |
7020 | ||
c77599d5 | 7021 | begin |
9bebf0e9 AC |
7022 | -- If this is a derived type, retrieve original VMS type so that |
7023 | -- the proper sized type is used for intermediate values. | |
7024 | ||
7025 | if Is_Derived_Type (Typ) then | |
7026 | Rtyp := First_Subtype (Etype (Typ)); | |
7027 | else | |
7028 | Rtyp := Typ; | |
7029 | end if; | |
7030 | ||
0d901290 AC |
7031 | -- The proper unsigned type must have a size compatible with the |
7032 | -- operand, to prevent misalignment. | |
9bebf0e9 AC |
7033 | |
7034 | if RM_Size (Rtyp) <= 8 then | |
7035 | Utyp := RTE (RE_Unsigned_8); | |
7036 | ||
7037 | elsif RM_Size (Rtyp) <= 16 then | |
7038 | Utyp := RTE (RE_Unsigned_16); | |
7039 | ||
7040 | elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then | |
bc20523f | 7041 | Utyp := RTE (RE_Unsigned_32); |
9bebf0e9 AC |
7042 | |
7043 | else | |
7044 | Utyp := RTE (RE_Long_Long_Unsigned); | |
7045 | end if; | |
7046 | ||
c77599d5 AC |
7047 | Rewrite (N, |
7048 | Unchecked_Convert_To (Typ, | |
9bebf0e9 AC |
7049 | Make_Op_Not (Loc, |
7050 | Unchecked_Convert_To (Utyp, Right_Opnd (N))))); | |
c77599d5 AC |
7051 | Analyze_And_Resolve (N, Typ); |
7052 | return; | |
7053 | end; | |
7054 | end if; | |
7055 | ||
70482933 RK |
7056 | -- Only array types need any other processing |
7057 | ||
7058 | if not Is_Array_Type (Typ) then | |
7059 | return; | |
7060 | end if; | |
7061 | ||
a9d8907c JM |
7062 | -- Case of array operand. If bit packed with a component size of 1, |
7063 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 7064 | |
a9d8907c JM |
7065 | if Is_Bit_Packed_Array (Typ) |
7066 | and then Component_Size (Typ) = 1 | |
7067 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
7068 | then | |
70482933 RK |
7069 | Expand_Packed_Not (N); |
7070 | return; | |
7071 | end if; | |
7072 | ||
fbf5a39b AC |
7073 | -- Case of array operand which is not bit-packed. If the context is |
7074 | -- a safe assignment, call in-place operation, If context is a larger | |
7075 | -- boolean expression in the context of a safe assignment, expansion is | |
7076 | -- done by enclosing operation. | |
70482933 RK |
7077 | |
7078 | Opnd := Relocate_Node (Right_Opnd (N)); | |
7079 | Convert_To_Actual_Subtype (Opnd); | |
7080 | Arr := Etype (Opnd); | |
7081 | Ensure_Defined (Arr, N); | |
b4592168 | 7082 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 7083 | |
fbf5a39b AC |
7084 | if Nkind (Parent (N)) = N_Assignment_Statement then |
7085 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
7086 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
7087 | return; | |
7088 | ||
5e1c00fa | 7089 | -- Special case the negation of a binary operation |
fbf5a39b | 7090 | |
303b4d58 | 7091 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 7092 | and then Safe_In_Place_Array_Op |
303b4d58 | 7093 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
7094 | then |
7095 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
7096 | return; | |
7097 | end if; | |
7098 | ||
7099 | elsif Nkind (Parent (N)) in N_Binary_Op | |
7100 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
7101 | then | |
7102 | declare | |
7103 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
7104 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
7105 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
7106 | ||
7107 | begin | |
7108 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
fbf5a39b | 7109 | |
aa9a7dd7 AC |
7110 | -- (not A) op (not B) can be reduced to a single call |
7111 | ||
7112 | if N = Op1 and then Nkind (Op2) = N_Op_Not then | |
fbf5a39b AC |
7113 | return; |
7114 | ||
bed8af19 AC |
7115 | elsif N = Op2 and then Nkind (Op1) = N_Op_Not then |
7116 | return; | |
7117 | ||
aa9a7dd7 | 7118 | -- A xor (not B) can also be special-cased |
fbf5a39b | 7119 | |
aa9a7dd7 | 7120 | elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then |
fbf5a39b AC |
7121 | return; |
7122 | end if; | |
7123 | end if; | |
7124 | end; | |
7125 | end if; | |
7126 | ||
70482933 RK |
7127 | A := Make_Defining_Identifier (Loc, Name_uA); |
7128 | B := Make_Defining_Identifier (Loc, Name_uB); | |
7129 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
7130 | ||
7131 | A_J := | |
7132 | Make_Indexed_Component (Loc, | |
7133 | Prefix => New_Reference_To (A, Loc), | |
7134 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7135 | ||
7136 | B_J := | |
7137 | Make_Indexed_Component (Loc, | |
7138 | Prefix => New_Reference_To (B, Loc), | |
7139 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7140 | ||
7141 | Loop_Statement := | |
7142 | Make_Implicit_Loop_Statement (N, | |
7143 | Identifier => Empty, | |
7144 | ||
7145 | Iteration_Scheme => | |
7146 | Make_Iteration_Scheme (Loc, | |
7147 | Loop_Parameter_Specification => | |
7148 | Make_Loop_Parameter_Specification (Loc, | |
0d901290 | 7149 | Defining_Identifier => J, |
70482933 RK |
7150 | Discrete_Subtype_Definition => |
7151 | Make_Attribute_Reference (Loc, | |
0d901290 | 7152 | Prefix => Make_Identifier (Loc, Chars (A)), |
70482933 RK |
7153 | Attribute_Name => Name_Range))), |
7154 | ||
7155 | Statements => New_List ( | |
7156 | Make_Assignment_Statement (Loc, | |
7157 | Name => B_J, | |
7158 | Expression => Make_Op_Not (Loc, A_J)))); | |
7159 | ||
191fcb3a | 7160 | Func_Name := Make_Temporary (Loc, 'N'); |
70482933 RK |
7161 | Set_Is_Inlined (Func_Name); |
7162 | ||
7163 | Insert_Action (N, | |
7164 | Make_Subprogram_Body (Loc, | |
7165 | Specification => | |
7166 | Make_Function_Specification (Loc, | |
7167 | Defining_Unit_Name => Func_Name, | |
7168 | Parameter_Specifications => New_List ( | |
7169 | Make_Parameter_Specification (Loc, | |
7170 | Defining_Identifier => A, | |
7171 | Parameter_Type => New_Reference_To (Typ, Loc))), | |
630d30e9 | 7172 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
7173 | |
7174 | Declarations => New_List ( | |
7175 | Make_Object_Declaration (Loc, | |
7176 | Defining_Identifier => B, | |
7177 | Object_Definition => New_Reference_To (Arr, Loc))), | |
7178 | ||
7179 | Handled_Statement_Sequence => | |
7180 | Make_Handled_Sequence_Of_Statements (Loc, | |
7181 | Statements => New_List ( | |
7182 | Loop_Statement, | |
d766cee3 | 7183 | Make_Simple_Return_Statement (Loc, |
0d901290 | 7184 | Expression => Make_Identifier (Loc, Chars (B))))))); |
70482933 RK |
7185 | |
7186 | Rewrite (N, | |
7187 | Make_Function_Call (Loc, | |
0d901290 | 7188 | Name => New_Reference_To (Func_Name, Loc), |
70482933 RK |
7189 | Parameter_Associations => New_List (Opnd))); |
7190 | ||
7191 | Analyze_And_Resolve (N, Typ); | |
7192 | end Expand_N_Op_Not; | |
7193 | ||
7194 | -------------------- | |
7195 | -- Expand_N_Op_Or -- | |
7196 | -------------------- | |
7197 | ||
7198 | procedure Expand_N_Op_Or (N : Node_Id) is | |
7199 | Typ : constant Entity_Id := Etype (N); | |
7200 | ||
7201 | begin | |
7202 | Binary_Op_Validity_Checks (N); | |
7203 | ||
7204 | if Is_Array_Type (Etype (N)) then | |
7205 | Expand_Boolean_Operator (N); | |
7206 | ||
7207 | elsif Is_Boolean_Type (Etype (N)) then | |
6a2afd13 | 7208 | |
0d901290 AC |
7209 | -- Replace OR by OR ELSE if Short_Circuit_And_Or active and the type |
7210 | -- is standard Boolean (do not mess with AND that uses a non-standard | |
7211 | -- Boolean type, because something strange is going on). | |
6a2afd13 AC |
7212 | |
7213 | if Short_Circuit_And_Or and then Typ = Standard_Boolean then | |
7214 | Rewrite (N, | |
7215 | Make_Or_Else (Sloc (N), | |
7216 | Left_Opnd => Relocate_Node (Left_Opnd (N)), | |
7217 | Right_Opnd => Relocate_Node (Right_Opnd (N)))); | |
7218 | Analyze_And_Resolve (N, Typ); | |
7219 | ||
7220 | -- Otherwise, adjust conditions | |
7221 | ||
7222 | else | |
7223 | Adjust_Condition (Left_Opnd (N)); | |
7224 | Adjust_Condition (Right_Opnd (N)); | |
7225 | Set_Etype (N, Standard_Boolean); | |
7226 | Adjust_Result_Type (N, Typ); | |
7227 | end if; | |
437f8c1e AC |
7228 | |
7229 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
7230 | Expand_Intrinsic_Call (N, Entity (N)); | |
7231 | ||
70482933 RK |
7232 | end if; |
7233 | end Expand_N_Op_Or; | |
7234 | ||
7235 | ---------------------- | |
7236 | -- Expand_N_Op_Plus -- | |
7237 | ---------------------- | |
7238 | ||
7239 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
7240 | begin | |
7241 | Unary_Op_Validity_Checks (N); | |
7242 | end Expand_N_Op_Plus; | |
7243 | ||
7244 | --------------------- | |
7245 | -- Expand_N_Op_Rem -- | |
7246 | --------------------- | |
7247 | ||
7248 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
7249 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 7250 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
7251 | |
7252 | Left : constant Node_Id := Left_Opnd (N); | |
7253 | Right : constant Node_Id := Right_Opnd (N); | |
7254 | ||
5d5e9775 AC |
7255 | Lo : Uint; |
7256 | Hi : Uint; | |
7257 | OK : Boolean; | |
70482933 | 7258 | |
5d5e9775 AC |
7259 | Lneg : Boolean; |
7260 | Rneg : Boolean; | |
7261 | -- Set if corresponding operand can be negative | |
7262 | ||
7263 | pragma Unreferenced (Hi); | |
1033834f | 7264 | |
70482933 RK |
7265 | begin |
7266 | Binary_Op_Validity_Checks (N); | |
7267 | ||
7268 | if Is_Integer_Type (Etype (N)) then | |
7269 | Apply_Divide_Check (N); | |
7270 | end if; | |
7271 | ||
685094bf RD |
7272 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
7273 | -- but it is useful with other back ends (e.g. AAMP), and is certainly | |
7274 | -- harmless. | |
fbf5a39b AC |
7275 | |
7276 | if Is_Integer_Type (Etype (N)) | |
7277 | and then Compile_Time_Known_Value (Right) | |
7278 | and then Expr_Value (Right) = Uint_1 | |
7279 | then | |
abcbd24c ST |
7280 | -- Call Remove_Side_Effects to ensure that any side effects in the |
7281 | -- ignored left operand (in particular function calls to user defined | |
7282 | -- functions) are properly executed. | |
7283 | ||
7284 | Remove_Side_Effects (Left); | |
7285 | ||
fbf5a39b AC |
7286 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
7287 | Analyze_And_Resolve (N, Typ); | |
7288 | return; | |
7289 | end if; | |
7290 | ||
685094bf RD |
7291 | -- Deal with annoying case of largest negative number remainder minus |
7292 | -- one. Gigi does not handle this case correctly, because it generates | |
7293 | -- a divide instruction which may trap in this case. | |
70482933 | 7294 | |
685094bf RD |
7295 | -- In fact the check is quite easy, if the right operand is -1, then |
7296 | -- the remainder is always 0, and we can just ignore the left operand | |
7297 | -- completely in this case. | |
70482933 | 7298 | |
5d5e9775 AC |
7299 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); |
7300 | Lneg := (not OK) or else Lo < 0; | |
fbf5a39b | 7301 | |
5d5e9775 AC |
7302 | Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True); |
7303 | Rneg := (not OK) or else Lo < 0; | |
fbf5a39b | 7304 | |
5d5e9775 AC |
7305 | -- We won't mess with trying to find out if the left operand can really |
7306 | -- be the largest negative number (that's a pain in the case of private | |
7307 | -- types and this is really marginal). We will just assume that we need | |
7308 | -- the test if the left operand can be negative at all. | |
fbf5a39b | 7309 | |
5d5e9775 | 7310 | if Lneg and Rneg then |
70482933 RK |
7311 | Rewrite (N, |
7312 | Make_Conditional_Expression (Loc, | |
7313 | Expressions => New_List ( | |
7314 | Make_Op_Eq (Loc, | |
0d901290 | 7315 | Left_Opnd => Duplicate_Subexpr (Right), |
70482933 | 7316 | Right_Opnd => |
0d901290 | 7317 | Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))), |
70482933 | 7318 | |
fbf5a39b AC |
7319 | Unchecked_Convert_To (Typ, |
7320 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
7321 | |
7322 | Relocate_Node (N)))); | |
7323 | ||
7324 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
7325 | Analyze_And_Resolve (N, Typ); | |
7326 | end if; | |
7327 | end Expand_N_Op_Rem; | |
7328 | ||
7329 | ----------------------------- | |
7330 | -- Expand_N_Op_Rotate_Left -- | |
7331 | ----------------------------- | |
7332 | ||
7333 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
7334 | begin | |
7335 | Binary_Op_Validity_Checks (N); | |
7336 | end Expand_N_Op_Rotate_Left; | |
7337 | ||
7338 | ------------------------------ | |
7339 | -- Expand_N_Op_Rotate_Right -- | |
7340 | ------------------------------ | |
7341 | ||
7342 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
7343 | begin | |
7344 | Binary_Op_Validity_Checks (N); | |
7345 | end Expand_N_Op_Rotate_Right; | |
7346 | ||
7347 | ---------------------------- | |
7348 | -- Expand_N_Op_Shift_Left -- | |
7349 | ---------------------------- | |
7350 | ||
7351 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is | |
7352 | begin | |
7353 | Binary_Op_Validity_Checks (N); | |
7354 | end Expand_N_Op_Shift_Left; | |
7355 | ||
7356 | ----------------------------- | |
7357 | -- Expand_N_Op_Shift_Right -- | |
7358 | ----------------------------- | |
7359 | ||
7360 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
7361 | begin | |
7362 | Binary_Op_Validity_Checks (N); | |
7363 | end Expand_N_Op_Shift_Right; | |
7364 | ||
7365 | ---------------------------------------- | |
7366 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
7367 | ---------------------------------------- | |
7368 | ||
7369 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
7370 | begin | |
7371 | Binary_Op_Validity_Checks (N); | |
7372 | end Expand_N_Op_Shift_Right_Arithmetic; | |
7373 | ||
7374 | -------------------------- | |
7375 | -- Expand_N_Op_Subtract -- | |
7376 | -------------------------- | |
7377 | ||
7378 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
7379 | Typ : constant Entity_Id := Etype (N); | |
7380 | ||
7381 | begin | |
7382 | Binary_Op_Validity_Checks (N); | |
7383 | ||
7384 | -- N - 0 = N for integer types | |
7385 | ||
7386 | if Is_Integer_Type (Typ) | |
7387 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
7388 | and then Expr_Value (Right_Opnd (N)) = 0 | |
7389 | then | |
7390 | Rewrite (N, Left_Opnd (N)); | |
7391 | return; | |
7392 | end if; | |
7393 | ||
8fc789c8 | 7394 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 | 7395 | |
aa9a7dd7 AC |
7396 | if Is_Signed_Integer_Type (Typ) |
7397 | or else | |
7398 | Is_Fixed_Point_Type (Typ) | |
7399 | then | |
70482933 RK |
7400 | Apply_Arithmetic_Overflow_Check (N); |
7401 | ||
0d901290 | 7402 | -- VAX floating-point types case |
70482933 RK |
7403 | |
7404 | elsif Vax_Float (Typ) then | |
7405 | Expand_Vax_Arith (N); | |
7406 | end if; | |
7407 | end Expand_N_Op_Subtract; | |
7408 | ||
7409 | --------------------- | |
7410 | -- Expand_N_Op_Xor -- | |
7411 | --------------------- | |
7412 | ||
7413 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
7414 | Typ : constant Entity_Id := Etype (N); | |
7415 | ||
7416 | begin | |
7417 | Binary_Op_Validity_Checks (N); | |
7418 | ||
7419 | if Is_Array_Type (Etype (N)) then | |
7420 | Expand_Boolean_Operator (N); | |
7421 | ||
7422 | elsif Is_Boolean_Type (Etype (N)) then | |
7423 | Adjust_Condition (Left_Opnd (N)); | |
7424 | Adjust_Condition (Right_Opnd (N)); | |
7425 | Set_Etype (N, Standard_Boolean); | |
7426 | Adjust_Result_Type (N, Typ); | |
437f8c1e AC |
7427 | |
7428 | elsif Is_Intrinsic_Subprogram (Entity (N)) then | |
7429 | Expand_Intrinsic_Call (N, Entity (N)); | |
7430 | ||
70482933 RK |
7431 | end if; |
7432 | end Expand_N_Op_Xor; | |
7433 | ||
7434 | ---------------------- | |
7435 | -- Expand_N_Or_Else -- | |
7436 | ---------------------- | |
7437 | ||
5875f8d6 AC |
7438 | procedure Expand_N_Or_Else (N : Node_Id) |
7439 | renames Expand_Short_Circuit_Operator; | |
70482933 RK |
7440 | |
7441 | ----------------------------------- | |
7442 | -- Expand_N_Qualified_Expression -- | |
7443 | ----------------------------------- | |
7444 | ||
7445 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
7446 | Operand : constant Node_Id := Expression (N); | |
7447 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
7448 | ||
7449 | begin | |
f82944b7 JM |
7450 | -- Do validity check if validity checking operands |
7451 | ||
7452 | if Validity_Checks_On | |
7453 | and then Validity_Check_Operands | |
7454 | then | |
7455 | Ensure_Valid (Operand); | |
7456 | end if; | |
7457 | ||
7458 | -- Apply possible constraint check | |
7459 | ||
70482933 | 7460 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
d79e621a GD |
7461 | |
7462 | if Do_Range_Check (Operand) then | |
7463 | Set_Do_Range_Check (Operand, False); | |
7464 | Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed); | |
7465 | end if; | |
70482933 RK |
7466 | end Expand_N_Qualified_Expression; |
7467 | ||
a961aa79 AC |
7468 | ------------------------------------ |
7469 | -- Expand_N_Quantified_Expression -- | |
7470 | ------------------------------------ | |
7471 | ||
c0f136cd AC |
7472 | -- We expand: |
7473 | ||
7474 | -- for all X in range => Cond | |
a961aa79 | 7475 | |
c0f136cd | 7476 | -- into: |
a961aa79 | 7477 | |
c0f136cd AC |
7478 | -- T := True; |
7479 | -- for X in range loop | |
7480 | -- if not Cond then | |
7481 | -- T := False; | |
7482 | -- exit; | |
7483 | -- end if; | |
7484 | -- end loop; | |
90c63b09 | 7485 | |
c0f136cd | 7486 | -- Conversely, an existentially quantified expression: |
90c63b09 | 7487 | |
c0f136cd | 7488 | -- for some X in range => Cond |
90c63b09 | 7489 | |
c0f136cd | 7490 | -- becomes: |
90c63b09 | 7491 | |
c0f136cd AC |
7492 | -- T := False; |
7493 | -- for X in range loop | |
7494 | -- if Cond then | |
7495 | -- T := True; | |
7496 | -- exit; | |
7497 | -- end if; | |
7498 | -- end loop; | |
90c63b09 | 7499 | |
c0f136cd AC |
7500 | -- In both cases, the iteration may be over a container in which case it is |
7501 | -- given by an iterator specification, not a loop parameter specification. | |
a961aa79 | 7502 | |
c0f136cd AC |
7503 | procedure Expand_N_Quantified_Expression (N : Node_Id) is |
7504 | Loc : constant Source_Ptr := Sloc (N); | |
7505 | Is_Universal : constant Boolean := All_Present (N); | |
7506 | Actions : constant List_Id := New_List; | |
7507 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); | |
7508 | Cond : Node_Id; | |
7509 | Decl : Node_Id; | |
7510 | I_Scheme : Node_Id; | |
7511 | Test : Node_Id; | |
c56a9ba4 | 7512 | |
a961aa79 | 7513 | begin |
90c63b09 AC |
7514 | Decl := |
7515 | Make_Object_Declaration (Loc, | |
7516 | Defining_Identifier => Tnn, | |
c0f136cd AC |
7517 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
7518 | Expression => | |
7519 | New_Occurrence_Of (Boolean_Literals (Is_Universal), Loc)); | |
a961aa79 AC |
7520 | Append_To (Actions, Decl); |
7521 | ||
c0f136cd | 7522 | Cond := Relocate_Node (Condition (N)); |
a961aa79 | 7523 | |
62be5d0a JM |
7524 | -- Reset flag analyzed in the condition to force its analysis. Required |
7525 | -- since the previous analysis was done with expansion disabled (see | |
7526 | -- Resolve_Quantified_Expression) and hence checks were not inserted | |
7527 | -- and record comparisons have not been expanded. | |
7528 | ||
7529 | Reset_Analyzed_Flags (Cond); | |
7530 | ||
c0f136cd AC |
7531 | if Is_Universal then |
7532 | Cond := Make_Op_Not (Loc, Cond); | |
a961aa79 AC |
7533 | end if; |
7534 | ||
c0f136cd AC |
7535 | Test := |
7536 | Make_Implicit_If_Statement (N, | |
7537 | Condition => Cond, | |
7538 | Then_Statements => New_List ( | |
7539 | Make_Assignment_Statement (Loc, | |
7540 | Name => New_Occurrence_Of (Tnn, Loc), | |
7541 | Expression => | |
7542 | New_Occurrence_Of (Boolean_Literals (not Is_Universal), Loc)), | |
7543 | Make_Exit_Statement (Loc))); | |
7544 | ||
c56a9ba4 AC |
7545 | if Present (Loop_Parameter_Specification (N)) then |
7546 | I_Scheme := | |
7547 | Make_Iteration_Scheme (Loc, | |
7548 | Loop_Parameter_Specification => | |
7549 | Loop_Parameter_Specification (N)); | |
7550 | else | |
7551 | I_Scheme := | |
7552 | Make_Iteration_Scheme (Loc, | |
7553 | Iterator_Specification => Iterator_Specification (N)); | |
7554 | end if; | |
7555 | ||
a961aa79 AC |
7556 | Append_To (Actions, |
7557 | Make_Loop_Statement (Loc, | |
c56a9ba4 | 7558 | Iteration_Scheme => I_Scheme, |
c0f136cd AC |
7559 | Statements => New_List (Test), |
7560 | End_Label => Empty)); | |
a961aa79 | 7561 | |
c0f136cd AC |
7562 | -- The components of the scheme have already been analyzed, and the loop |
7563 | -- parameter declaration has been processed. | |
c56a9ba4 AC |
7564 | |
7565 | Set_Analyzed (Iteration_Scheme (Last (Actions))); | |
7566 | ||
a961aa79 AC |
7567 | Rewrite (N, |
7568 | Make_Expression_With_Actions (Loc, | |
7569 | Expression => New_Occurrence_Of (Tnn, Loc), | |
7570 | Actions => Actions)); | |
7571 | ||
7572 | Analyze_And_Resolve (N, Standard_Boolean); | |
7573 | end Expand_N_Quantified_Expression; | |
7574 | ||
70482933 RK |
7575 | --------------------------------- |
7576 | -- Expand_N_Selected_Component -- | |
7577 | --------------------------------- | |
7578 | ||
7579 | -- If the selector is a discriminant of a concurrent object, rewrite the | |
7580 | -- prefix to denote the corresponding record type. | |
7581 | ||
7582 | procedure Expand_N_Selected_Component (N : Node_Id) is | |
7583 | Loc : constant Source_Ptr := Sloc (N); | |
7584 | Par : constant Node_Id := Parent (N); | |
7585 | P : constant Node_Id := Prefix (N); | |
fbf5a39b | 7586 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 7587 | Disc : Entity_Id; |
70482933 | 7588 | New_N : Node_Id; |
fbf5a39b | 7589 | Dcon : Elmt_Id; |
d606f1df | 7590 | Dval : Node_Id; |
70482933 RK |
7591 | |
7592 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
7593 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
7594 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
7595 | -- Don't we have a general routine that does this??? |
7596 | ||
7597 | ----------------------- | |
7598 | -- In_Left_Hand_Side -- | |
7599 | ----------------------- | |
70482933 RK |
7600 | |
7601 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
7602 | begin | |
fbf5a39b | 7603 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
90c63b09 | 7604 | and then Comp = Name (Parent (Comp))) |
fbf5a39b | 7605 | or else (Present (Parent (Comp)) |
90c63b09 AC |
7606 | and then Nkind (Parent (Comp)) in N_Subexpr |
7607 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
7608 | end In_Left_Hand_Side; |
7609 | ||
fbf5a39b AC |
7610 | -- Start of processing for Expand_N_Selected_Component |
7611 | ||
70482933 | 7612 | begin |
fbf5a39b AC |
7613 | -- Insert explicit dereference if required |
7614 | ||
7615 | if Is_Access_Type (Ptyp) then | |
7616 | Insert_Explicit_Dereference (P); | |
e6f69614 | 7617 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
7618 | |
7619 | if Ekind (Etype (P)) = E_Private_Subtype | |
7620 | and then Is_For_Access_Subtype (Etype (P)) | |
7621 | then | |
7622 | Set_Etype (P, Base_Type (Etype (P))); | |
7623 | end if; | |
7624 | ||
7625 | Ptyp := Etype (P); | |
7626 | end if; | |
7627 | ||
7628 | -- Deal with discriminant check required | |
7629 | ||
70482933 RK |
7630 | if Do_Discriminant_Check (N) then |
7631 | ||
685094bf RD |
7632 | -- Present the discriminant checking function to the backend, so that |
7633 | -- it can inline the call to the function. | |
70482933 RK |
7634 | |
7635 | Add_Inlined_Body | |
7636 | (Discriminant_Checking_Func | |
7637 | (Original_Record_Component (Entity (Selector_Name (N))))); | |
70482933 | 7638 | |
fbf5a39b | 7639 | -- Now reset the flag and generate the call |
70482933 | 7640 | |
fbf5a39b AC |
7641 | Set_Do_Discriminant_Check (N, False); |
7642 | Generate_Discriminant_Check (N); | |
70482933 RK |
7643 | end if; |
7644 | ||
b4592168 GD |
7645 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
7646 | -- function, then additional actuals must be passed. | |
7647 | ||
0791fbe9 | 7648 | if Ada_Version >= Ada_2005 |
b4592168 GD |
7649 | and then Is_Build_In_Place_Function_Call (P) |
7650 | then | |
7651 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
7652 | end if; | |
7653 | ||
fbf5a39b AC |
7654 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
7655 | -- selected component with discriminants. This must be checked during | |
7656 | -- expansion, because during analysis the type of the selector is not | |
7657 | -- known at the point the prefix is analyzed. If the conversion is the | |
7658 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
7659 | |
7660 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
7661 | and then Has_Discriminants (Etype (N)) | |
7662 | and then not In_Left_Hand_Side (N) | |
7663 | then | |
7664 | Force_Evaluation (Prefix (N)); | |
7665 | end if; | |
7666 | ||
7667 | -- Remaining processing applies only if selector is a discriminant | |
7668 | ||
7669 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
7670 | ||
7671 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
7672 | -- we may be able to rewrite the expression with the actual value |
7673 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
7674 | |
7675 | if Is_Record_Type (Ptyp) | |
7676 | and then Has_Discriminants (Ptyp) | |
7677 | and then Is_Constrained (Ptyp) | |
70482933 | 7678 | then |
fbf5a39b AC |
7679 | -- Do this optimization for discrete types only, and not for |
7680 | -- access types (access discriminants get us into trouble!) | |
70482933 | 7681 | |
fbf5a39b AC |
7682 | if not Is_Discrete_Type (Etype (N)) then |
7683 | null; | |
7684 | ||
7685 | -- Don't do this on the left hand of an assignment statement. | |
0d901290 AC |
7686 | -- Normally one would think that references like this would not |
7687 | -- occur, but they do in generated code, and mean that we really | |
7688 | -- do want to assign the discriminant! | |
fbf5a39b AC |
7689 | |
7690 | elsif Nkind (Par) = N_Assignment_Statement | |
7691 | and then Name (Par) = N | |
7692 | then | |
7693 | null; | |
7694 | ||
685094bf | 7695 | -- Don't do this optimization for the prefix of an attribute or |
e2534738 | 7696 | -- the name of an object renaming declaration since these are |
685094bf | 7697 | -- contexts where we do not want the value anyway. |
fbf5a39b AC |
7698 | |
7699 | elsif (Nkind (Par) = N_Attribute_Reference | |
7700 | and then Prefix (Par) = N) | |
7701 | or else Is_Renamed_Object (N) | |
7702 | then | |
7703 | null; | |
7704 | ||
7705 | -- Don't do this optimization if we are within the code for a | |
7706 | -- discriminant check, since the whole point of such a check may | |
7707 | -- be to verify the condition on which the code below depends! | |
7708 | ||
7709 | elsif Is_In_Discriminant_Check (N) then | |
7710 | null; | |
7711 | ||
7712 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
7713 | -- still one condition that inhibits the optimization below but |
7714 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
7715 | |
7716 | else | |
685094bf RD |
7717 | -- Loop through discriminants to find the matching discriminant |
7718 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
7719 | |
7720 | Disc := First_Discriminant (Ptyp); | |
7721 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
7722 | Discr_Loop : while Present (Dcon) loop | |
d606f1df | 7723 | Dval := Node (Dcon); |
fbf5a39b | 7724 | |
bd949ee2 RD |
7725 | -- Check if this is the matching discriminant and if the |
7726 | -- discriminant value is simple enough to make sense to | |
7727 | -- copy. We don't want to copy complex expressions, and | |
7728 | -- indeed to do so can cause trouble (before we put in | |
7729 | -- this guard, a discriminant expression containing an | |
e7d897b8 | 7730 | -- AND THEN was copied, causing problems for coverage |
c228a069 | 7731 | -- analysis tools). |
bd949ee2 RD |
7732 | |
7733 | if Disc = Entity (Selector_Name (N)) | |
7734 | and then (Is_Entity_Name (Dval) | |
7735 | or else Is_Static_Expression (Dval)) | |
7736 | then | |
fbf5a39b AC |
7737 | -- Here we have the matching discriminant. Check for |
7738 | -- the case of a discriminant of a component that is | |
7739 | -- constrained by an outer discriminant, which cannot | |
7740 | -- be optimized away. | |
7741 | ||
d606f1df AC |
7742 | if Denotes_Discriminant |
7743 | (Dval, Check_Concurrent => True) | |
7744 | then | |
7745 | exit Discr_Loop; | |
7746 | ||
7747 | elsif Nkind (Original_Node (Dval)) = N_Selected_Component | |
7748 | and then | |
7749 | Denotes_Discriminant | |
7750 | (Selector_Name (Original_Node (Dval)), True) | |
7751 | then | |
7752 | exit Discr_Loop; | |
7753 | ||
7754 | -- Do not retrieve value if constraint is not static. It | |
7755 | -- is generally not useful, and the constraint may be a | |
7756 | -- rewritten outer discriminant in which case it is in | |
7757 | -- fact incorrect. | |
7758 | ||
7759 | elsif Is_Entity_Name (Dval) | |
e7d897b8 AC |
7760 | and then Nkind (Parent (Entity (Dval))) = |
7761 | N_Object_Declaration | |
d606f1df AC |
7762 | and then Present (Expression (Parent (Entity (Dval)))) |
7763 | and then | |
7764 | not Is_Static_Expression | |
7765 | (Expression (Parent (Entity (Dval)))) | |
fbf5a39b AC |
7766 | then |
7767 | exit Discr_Loop; | |
70482933 | 7768 | |
685094bf RD |
7769 | -- In the context of a case statement, the expression may |
7770 | -- have the base type of the discriminant, and we need to | |
7771 | -- preserve the constraint to avoid spurious errors on | |
7772 | -- missing cases. | |
70482933 | 7773 | |
fbf5a39b | 7774 | elsif Nkind (Parent (N)) = N_Case_Statement |
d606f1df | 7775 | and then Etype (Dval) /= Etype (Disc) |
70482933 RK |
7776 | then |
7777 | Rewrite (N, | |
7778 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
7779 | Subtype_Mark => |
7780 | New_Occurrence_Of (Etype (Disc), Loc), | |
7781 | Expression => | |
d606f1df | 7782 | New_Copy_Tree (Dval))); |
ffe9aba8 | 7783 | Analyze_And_Resolve (N, Etype (Disc)); |
fbf5a39b AC |
7784 | |
7785 | -- In case that comes out as a static expression, | |
7786 | -- reset it (a selected component is never static). | |
7787 | ||
7788 | Set_Is_Static_Expression (N, False); | |
7789 | return; | |
7790 | ||
7791 | -- Otherwise we can just copy the constraint, but the | |
ffe9aba8 AC |
7792 | -- result is certainly not static! In some cases the |
7793 | -- discriminant constraint has been analyzed in the | |
7794 | -- context of the original subtype indication, but for | |
7795 | -- itypes the constraint might not have been analyzed | |
7796 | -- yet, and this must be done now. | |
fbf5a39b | 7797 | |
70482933 | 7798 | else |
d606f1df | 7799 | Rewrite (N, New_Copy_Tree (Dval)); |
ffe9aba8 | 7800 | Analyze_And_Resolve (N); |
fbf5a39b AC |
7801 | Set_Is_Static_Expression (N, False); |
7802 | return; | |
70482933 | 7803 | end if; |
70482933 RK |
7804 | end if; |
7805 | ||
fbf5a39b AC |
7806 | Next_Elmt (Dcon); |
7807 | Next_Discriminant (Disc); | |
7808 | end loop Discr_Loop; | |
70482933 | 7809 | |
fbf5a39b AC |
7810 | -- Note: the above loop should always find a matching |
7811 | -- discriminant, but if it does not, we just missed an | |
c228a069 AC |
7812 | -- optimization due to some glitch (perhaps a previous |
7813 | -- error), so ignore. | |
fbf5a39b AC |
7814 | |
7815 | end if; | |
70482933 RK |
7816 | end if; |
7817 | ||
7818 | -- The only remaining processing is in the case of a discriminant of | |
7819 | -- a concurrent object, where we rewrite the prefix to denote the | |
7820 | -- corresponding record type. If the type is derived and has renamed | |
7821 | -- discriminants, use corresponding discriminant, which is the one | |
7822 | -- that appears in the corresponding record. | |
7823 | ||
7824 | if not Is_Concurrent_Type (Ptyp) then | |
7825 | return; | |
7826 | end if; | |
7827 | ||
7828 | Disc := Entity (Selector_Name (N)); | |
7829 | ||
7830 | if Is_Derived_Type (Ptyp) | |
7831 | and then Present (Corresponding_Discriminant (Disc)) | |
7832 | then | |
7833 | Disc := Corresponding_Discriminant (Disc); | |
7834 | end if; | |
7835 | ||
7836 | New_N := | |
7837 | Make_Selected_Component (Loc, | |
7838 | Prefix => | |
7839 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
7840 | New_Copy_Tree (P)), | |
7841 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
7842 | ||
7843 | Rewrite (N, New_N); | |
7844 | Analyze (N); | |
7845 | end if; | |
70482933 RK |
7846 | end Expand_N_Selected_Component; |
7847 | ||
7848 | -------------------- | |
7849 | -- Expand_N_Slice -- | |
7850 | -------------------- | |
7851 | ||
7852 | procedure Expand_N_Slice (N : Node_Id) is | |
7853 | Loc : constant Source_Ptr := Sloc (N); | |
7854 | Typ : constant Entity_Id := Etype (N); | |
7855 | Pfx : constant Node_Id := Prefix (N); | |
7856 | Ptp : Entity_Id := Etype (Pfx); | |
fbf5a39b | 7857 | |
81a5b587 | 7858 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
7859 | -- Check whether the argument is an actual for a procedure call, in |
7860 | -- which case the expansion of a bit-packed slice is deferred until the | |
7861 | -- call itself is expanded. The reason this is required is that we might | |
7862 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
7863 | -- that copy out would be missed if we created a temporary here in | |
7864 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
7865 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
7866 | -- is harmless to defer expansion in the IN case, since the call | |
7867 | -- processing will still generate the appropriate copy in operation, | |
7868 | -- which will take care of the slice. | |
81a5b587 | 7869 | |
b01bf852 | 7870 | procedure Make_Temporary_For_Slice; |
685094bf RD |
7871 | -- Create a named variable for the value of the slice, in cases where |
7872 | -- the back-end cannot handle it properly, e.g. when packed types or | |
7873 | -- unaligned slices are involved. | |
fbf5a39b | 7874 | |
81a5b587 AC |
7875 | ------------------------- |
7876 | -- Is_Procedure_Actual -- | |
7877 | ------------------------- | |
7878 | ||
7879 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
7880 | Par : Node_Id := Parent (N); | |
08aa9a4a | 7881 | |
81a5b587 | 7882 | begin |
81a5b587 | 7883 | loop |
c6a60aa1 RD |
7884 | -- If our parent is a procedure call we can return |
7885 | ||
81a5b587 AC |
7886 | if Nkind (Par) = N_Procedure_Call_Statement then |
7887 | return True; | |
6b6fcd3e | 7888 | |
685094bf RD |
7889 | -- If our parent is a type conversion, keep climbing the tree, |
7890 | -- since a type conversion can be a procedure actual. Also keep | |
7891 | -- climbing if parameter association or a qualified expression, | |
7892 | -- since these are additional cases that do can appear on | |
7893 | -- procedure actuals. | |
6b6fcd3e | 7894 | |
303b4d58 AC |
7895 | elsif Nkind_In (Par, N_Type_Conversion, |
7896 | N_Parameter_Association, | |
7897 | N_Qualified_Expression) | |
c6a60aa1 | 7898 | then |
81a5b587 | 7899 | Par := Parent (Par); |
c6a60aa1 RD |
7900 | |
7901 | -- Any other case is not what we are looking for | |
7902 | ||
7903 | else | |
7904 | return False; | |
81a5b587 AC |
7905 | end if; |
7906 | end loop; | |
81a5b587 AC |
7907 | end Is_Procedure_Actual; |
7908 | ||
b01bf852 AC |
7909 | ------------------------------ |
7910 | -- Make_Temporary_For_Slice -- | |
7911 | ------------------------------ | |
fbf5a39b | 7912 | |
b01bf852 | 7913 | procedure Make_Temporary_For_Slice is |
fbf5a39b | 7914 | Decl : Node_Id; |
b01bf852 | 7915 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
13d923cc | 7916 | |
fbf5a39b AC |
7917 | begin |
7918 | Decl := | |
7919 | Make_Object_Declaration (Loc, | |
7920 | Defining_Identifier => Ent, | |
7921 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
7922 | ||
7923 | Set_No_Initialization (Decl); | |
7924 | ||
7925 | Insert_Actions (N, New_List ( | |
7926 | Decl, | |
7927 | Make_Assignment_Statement (Loc, | |
7928 | Name => New_Occurrence_Of (Ent, Loc), | |
7929 | Expression => Relocate_Node (N)))); | |
7930 | ||
7931 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
7932 | Analyze_And_Resolve (N, Typ); | |
b01bf852 | 7933 | end Make_Temporary_For_Slice; |
fbf5a39b AC |
7934 | |
7935 | -- Start of processing for Expand_N_Slice | |
70482933 RK |
7936 | |
7937 | begin | |
7938 | -- Special handling for access types | |
7939 | ||
7940 | if Is_Access_Type (Ptp) then | |
7941 | ||
70482933 RK |
7942 | Ptp := Designated_Type (Ptp); |
7943 | ||
e6f69614 AC |
7944 | Rewrite (Pfx, |
7945 | Make_Explicit_Dereference (Sloc (N), | |
7946 | Prefix => Relocate_Node (Pfx))); | |
70482933 | 7947 | |
e6f69614 | 7948 | Analyze_And_Resolve (Pfx, Ptp); |
70482933 RK |
7949 | end if; |
7950 | ||
b4592168 GD |
7951 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
7952 | -- function, then additional actuals must be passed. | |
7953 | ||
0791fbe9 | 7954 | if Ada_Version >= Ada_2005 |
b4592168 GD |
7955 | and then Is_Build_In_Place_Function_Call (Pfx) |
7956 | then | |
7957 | Make_Build_In_Place_Call_In_Anonymous_Context (Pfx); | |
7958 | end if; | |
7959 | ||
70482933 RK |
7960 | -- The remaining case to be handled is packed slices. We can leave |
7961 | -- packed slices as they are in the following situations: | |
7962 | ||
7963 | -- 1. Right or left side of an assignment (we can handle this | |
7964 | -- situation correctly in the assignment statement expansion). | |
7965 | ||
685094bf RD |
7966 | -- 2. Prefix of indexed component (the slide is optimized away in this |
7967 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 7968 | |
685094bf RD |
7969 | -- 3. Object renaming declaration, since we want the name of the |
7970 | -- slice, not the value. | |
70482933 | 7971 | |
685094bf RD |
7972 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
7973 | -- be required, and this is handled in the expansion of call | |
7974 | -- itself. | |
70482933 | 7975 | |
685094bf RD |
7976 | -- 5. Prefix of an address attribute (this is an error which is caught |
7977 | -- elsewhere, and the expansion would interfere with generating the | |
7978 | -- error message). | |
70482933 | 7979 | |
81a5b587 | 7980 | if not Is_Packed (Typ) then |
08aa9a4a | 7981 | |
685094bf RD |
7982 | -- Apply transformation for actuals of a function call, where |
7983 | -- Expand_Actuals is not used. | |
81a5b587 AC |
7984 | |
7985 | if Nkind (Parent (N)) = N_Function_Call | |
7986 | and then Is_Possibly_Unaligned_Slice (N) | |
7987 | then | |
b01bf852 | 7988 | Make_Temporary_For_Slice; |
81a5b587 AC |
7989 | end if; |
7990 | ||
7991 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
7992 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
7993 | and then Parent (N) = Name (Parent (Parent (N)))) | |
70482933 | 7994 | then |
81a5b587 | 7995 | return; |
70482933 | 7996 | |
81a5b587 AC |
7997 | elsif Nkind (Parent (N)) = N_Indexed_Component |
7998 | or else Is_Renamed_Object (N) | |
7999 | or else Is_Procedure_Actual (N) | |
8000 | then | |
8001 | return; | |
70482933 | 8002 | |
91b1417d AC |
8003 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
8004 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 8005 | then |
81a5b587 AC |
8006 | return; |
8007 | ||
8008 | else | |
b01bf852 | 8009 | Make_Temporary_For_Slice; |
70482933 RK |
8010 | end if; |
8011 | end Expand_N_Slice; | |
8012 | ||
8013 | ------------------------------ | |
8014 | -- Expand_N_Type_Conversion -- | |
8015 | ------------------------------ | |
8016 | ||
8017 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
8018 | Loc : constant Source_Ptr := Sloc (N); | |
8019 | Operand : constant Node_Id := Expression (N); | |
8020 | Target_Type : constant Entity_Id := Etype (N); | |
8021 | Operand_Type : Entity_Id := Etype (Operand); | |
8022 | ||
8023 | procedure Handle_Changed_Representation; | |
685094bf RD |
8024 | -- This is called in the case of record and array type conversions to |
8025 | -- see if there is a change of representation to be handled. Change of | |
8026 | -- representation is actually handled at the assignment statement level, | |
8027 | -- and what this procedure does is rewrite node N conversion as an | |
8028 | -- assignment to temporary. If there is no change of representation, | |
8029 | -- then the conversion node is unchanged. | |
70482933 | 8030 | |
426908f8 RD |
8031 | procedure Raise_Accessibility_Error; |
8032 | -- Called when we know that an accessibility check will fail. Rewrites | |
8033 | -- node N to an appropriate raise statement and outputs warning msgs. | |
8034 | -- The Etype of the raise node is set to Target_Type. | |
8035 | ||
70482933 RK |
8036 | procedure Real_Range_Check; |
8037 | -- Handles generation of range check for real target value | |
8038 | ||
8039 | ----------------------------------- | |
8040 | -- Handle_Changed_Representation -- | |
8041 | ----------------------------------- | |
8042 | ||
8043 | procedure Handle_Changed_Representation is | |
8044 | Temp : Entity_Id; | |
8045 | Decl : Node_Id; | |
8046 | Odef : Node_Id; | |
8047 | Disc : Node_Id; | |
8048 | N_Ix : Node_Id; | |
8049 | Cons : List_Id; | |
8050 | ||
8051 | begin | |
f82944b7 | 8052 | -- Nothing else to do if no change of representation |
70482933 RK |
8053 | |
8054 | if Same_Representation (Operand_Type, Target_Type) then | |
8055 | return; | |
8056 | ||
8057 | -- The real change of representation work is done by the assignment | |
8058 | -- statement processing. So if this type conversion is appearing as | |
8059 | -- the expression of an assignment statement, nothing needs to be | |
8060 | -- done to the conversion. | |
8061 | ||
8062 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
8063 | return; | |
8064 | ||
8065 | -- Otherwise we need to generate a temporary variable, and do the | |
8066 | -- change of representation assignment into that temporary variable. | |
8067 | -- The conversion is then replaced by a reference to this variable. | |
8068 | ||
8069 | else | |
8070 | Cons := No_List; | |
8071 | ||
685094bf RD |
8072 | -- If type is unconstrained we have to add a constraint, copied |
8073 | -- from the actual value of the left hand side. | |
70482933 RK |
8074 | |
8075 | if not Is_Constrained (Target_Type) then | |
8076 | if Has_Discriminants (Operand_Type) then | |
8077 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
8078 | |
8079 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
8080 | Disc := First_Stored_Discriminant (Operand_Type); | |
8081 | end if; | |
8082 | ||
70482933 RK |
8083 | Cons := New_List; |
8084 | while Present (Disc) loop | |
8085 | Append_To (Cons, | |
8086 | Make_Selected_Component (Loc, | |
7675ad4f AC |
8087 | Prefix => |
8088 | Duplicate_Subexpr_Move_Checks (Operand), | |
70482933 RK |
8089 | Selector_Name => |
8090 | Make_Identifier (Loc, Chars (Disc)))); | |
8091 | Next_Discriminant (Disc); | |
8092 | end loop; | |
8093 | ||
8094 | elsif Is_Array_Type (Operand_Type) then | |
8095 | N_Ix := First_Index (Target_Type); | |
8096 | Cons := New_List; | |
8097 | ||
8098 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
8099 | ||
8100 | -- We convert the bounds explicitly. We use an unchecked | |
8101 | -- conversion because bounds checks are done elsewhere. | |
8102 | ||
8103 | Append_To (Cons, | |
8104 | Make_Range (Loc, | |
8105 | Low_Bound => | |
8106 | Unchecked_Convert_To (Etype (N_Ix), | |
8107 | Make_Attribute_Reference (Loc, | |
8108 | Prefix => | |
fbf5a39b | 8109 | Duplicate_Subexpr_No_Checks |
70482933 RK |
8110 | (Operand, Name_Req => True), |
8111 | Attribute_Name => Name_First, | |
8112 | Expressions => New_List ( | |
8113 | Make_Integer_Literal (Loc, J)))), | |
8114 | ||
8115 | High_Bound => | |
8116 | Unchecked_Convert_To (Etype (N_Ix), | |
8117 | Make_Attribute_Reference (Loc, | |
8118 | Prefix => | |
fbf5a39b | 8119 | Duplicate_Subexpr_No_Checks |
70482933 RK |
8120 | (Operand, Name_Req => True), |
8121 | Attribute_Name => Name_Last, | |
8122 | Expressions => New_List ( | |
8123 | Make_Integer_Literal (Loc, J)))))); | |
8124 | ||
8125 | Next_Index (N_Ix); | |
8126 | end loop; | |
8127 | end if; | |
8128 | end if; | |
8129 | ||
8130 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
8131 | ||
8132 | if Present (Cons) then | |
8133 | Odef := | |
8134 | Make_Subtype_Indication (Loc, | |
8135 | Subtype_Mark => Odef, | |
8136 | Constraint => | |
8137 | Make_Index_Or_Discriminant_Constraint (Loc, | |
8138 | Constraints => Cons)); | |
8139 | end if; | |
8140 | ||
191fcb3a | 8141 | Temp := Make_Temporary (Loc, 'C'); |
70482933 RK |
8142 | Decl := |
8143 | Make_Object_Declaration (Loc, | |
8144 | Defining_Identifier => Temp, | |
8145 | Object_Definition => Odef); | |
8146 | ||
8147 | Set_No_Initialization (Decl, True); | |
8148 | ||
8149 | -- Insert required actions. It is essential to suppress checks | |
8150 | -- since we have suppressed default initialization, which means | |
8151 | -- that the variable we create may have no discriminants. | |
8152 | ||
8153 | Insert_Actions (N, | |
8154 | New_List ( | |
8155 | Decl, | |
8156 | Make_Assignment_Statement (Loc, | |
8157 | Name => New_Occurrence_Of (Temp, Loc), | |
8158 | Expression => Relocate_Node (N))), | |
8159 | Suppress => All_Checks); | |
8160 | ||
8161 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
8162 | return; | |
8163 | end if; | |
8164 | end Handle_Changed_Representation; | |
8165 | ||
426908f8 RD |
8166 | ------------------------------- |
8167 | -- Raise_Accessibility_Error -- | |
8168 | ------------------------------- | |
8169 | ||
8170 | procedure Raise_Accessibility_Error is | |
8171 | begin | |
8172 | Rewrite (N, | |
8173 | Make_Raise_Program_Error (Sloc (N), | |
8174 | Reason => PE_Accessibility_Check_Failed)); | |
8175 | Set_Etype (N, Target_Type); | |
8176 | ||
8177 | Error_Msg_N ("?accessibility check failure", N); | |
8178 | Error_Msg_NE | |
8179 | ("\?& will be raised at run time", N, Standard_Program_Error); | |
8180 | end Raise_Accessibility_Error; | |
8181 | ||
70482933 RK |
8182 | ---------------------- |
8183 | -- Real_Range_Check -- | |
8184 | ---------------------- | |
8185 | ||
685094bf RD |
8186 | -- Case of conversions to floating-point or fixed-point. If range checks |
8187 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
8188 | |
8189 | -- typ (x) | |
8190 | ||
8191 | -- to | |
8192 | ||
8193 | -- Tnn : typ'Base := typ'Base (x); | |
8194 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
8195 | -- Tnn | |
8196 | ||
685094bf RD |
8197 | -- This is necessary when there is a conversion of integer to float or |
8198 | -- to fixed-point to ensure that the correct checks are made. It is not | |
8199 | -- necessary for float to float where it is enough to simply set the | |
8200 | -- Do_Range_Check flag. | |
fbf5a39b | 8201 | |
70482933 RK |
8202 | procedure Real_Range_Check is |
8203 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
8204 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
8205 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 8206 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
8207 | Conv : Node_Id; |
8208 | Tnn : Entity_Id; | |
8209 | ||
8210 | begin | |
8211 | -- Nothing to do if conversion was rewritten | |
8212 | ||
8213 | if Nkind (N) /= N_Type_Conversion then | |
8214 | return; | |
8215 | end if; | |
8216 | ||
685094bf RD |
8217 | -- Nothing to do if range checks suppressed, or target has the same |
8218 | -- range as the base type (or is the base type). | |
70482933 RK |
8219 | |
8220 | if Range_Checks_Suppressed (Target_Type) | |
8221 | or else (Lo = Type_Low_Bound (Btyp) | |
8222 | and then | |
8223 | Hi = Type_High_Bound (Btyp)) | |
8224 | then | |
8225 | return; | |
8226 | end if; | |
8227 | ||
685094bf RD |
8228 | -- Nothing to do if expression is an entity on which checks have been |
8229 | -- suppressed. | |
70482933 | 8230 | |
fbf5a39b AC |
8231 | if Is_Entity_Name (Operand) |
8232 | and then Range_Checks_Suppressed (Entity (Operand)) | |
8233 | then | |
8234 | return; | |
8235 | end if; | |
8236 | ||
685094bf RD |
8237 | -- Nothing to do if bounds are all static and we can tell that the |
8238 | -- expression is within the bounds of the target. Note that if the | |
8239 | -- operand is of an unconstrained floating-point type, then we do | |
8240 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
8241 | |
8242 | declare | |
f02b8bb8 RD |
8243 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); |
8244 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
fbf5a39b AC |
8245 | |
8246 | begin | |
8247 | if (not Is_Floating_Point_Type (Xtyp) | |
8248 | or else Is_Constrained (Xtyp)) | |
8249 | and then Compile_Time_Known_Value (S_Lo) | |
8250 | and then Compile_Time_Known_Value (S_Hi) | |
8251 | and then Compile_Time_Known_Value (Hi) | |
8252 | and then Compile_Time_Known_Value (Lo) | |
8253 | then | |
8254 | declare | |
8255 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
8256 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
8257 | S_Lov : Ureal; | |
8258 | S_Hiv : Ureal; | |
8259 | ||
8260 | begin | |
8261 | if Is_Real_Type (Xtyp) then | |
8262 | S_Lov := Expr_Value_R (S_Lo); | |
8263 | S_Hiv := Expr_Value_R (S_Hi); | |
8264 | else | |
8265 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
8266 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
8267 | end if; | |
8268 | ||
8269 | if D_Hiv > D_Lov | |
8270 | and then S_Lov >= D_Lov | |
8271 | and then S_Hiv <= D_Hiv | |
8272 | then | |
8273 | Set_Do_Range_Check (Operand, False); | |
8274 | return; | |
8275 | end if; | |
8276 | end; | |
8277 | end if; | |
8278 | end; | |
8279 | ||
8280 | -- For float to float conversions, we are done | |
8281 | ||
8282 | if Is_Floating_Point_Type (Xtyp) | |
8283 | and then | |
8284 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
8285 | then |
8286 | return; | |
8287 | end if; | |
8288 | ||
fbf5a39b | 8289 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
8290 | |
8291 | Conv := Relocate_Node (N); | |
eaa826f8 | 8292 | Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); |
70482933 RK |
8293 | Set_Etype (Conv, Btyp); |
8294 | ||
f02b8bb8 RD |
8295 | -- Enable overflow except for case of integer to float conversions, |
8296 | -- where it is never required, since we can never have overflow in | |
8297 | -- this case. | |
70482933 | 8298 | |
fbf5a39b AC |
8299 | if not Is_Integer_Type (Etype (Operand)) then |
8300 | Enable_Overflow_Check (Conv); | |
70482933 RK |
8301 | end if; |
8302 | ||
191fcb3a | 8303 | Tnn := Make_Temporary (Loc, 'T', Conv); |
70482933 RK |
8304 | |
8305 | Insert_Actions (N, New_List ( | |
8306 | Make_Object_Declaration (Loc, | |
8307 | Defining_Identifier => Tnn, | |
8308 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
0ac2a660 AC |
8309 | Constant_Present => True, |
8310 | Expression => Conv), | |
70482933 RK |
8311 | |
8312 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
8313 | Condition => |
8314 | Make_Or_Else (Loc, | |
8315 | Left_Opnd => | |
8316 | Make_Op_Lt (Loc, | |
8317 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8318 | Right_Opnd => | |
8319 | Make_Attribute_Reference (Loc, | |
8320 | Attribute_Name => Name_First, | |
8321 | Prefix => | |
8322 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 8323 | |
07fc65c4 GB |
8324 | Right_Opnd => |
8325 | Make_Op_Gt (Loc, | |
8326 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8327 | Right_Opnd => | |
8328 | Make_Attribute_Reference (Loc, | |
8329 | Attribute_Name => Name_Last, | |
8330 | Prefix => | |
8331 | New_Occurrence_Of (Target_Type, Loc)))), | |
8332 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
8333 | |
8334 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
8335 | Analyze_And_Resolve (N, Btyp); | |
8336 | end Real_Range_Check; | |
8337 | ||
8338 | -- Start of processing for Expand_N_Type_Conversion | |
8339 | ||
8340 | begin | |
685094bf | 8341 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 AC |
8342 | -- the conversion completely, it is useless, except that it may carry |
8343 | -- an Assignment_OK attribute, which must be propagated to the operand. | |
70482933 RK |
8344 | |
8345 | if Operand_Type = Target_Type then | |
7b00e31d AC |
8346 | if Assignment_OK (N) then |
8347 | Set_Assignment_OK (Operand); | |
8348 | end if; | |
8349 | ||
fbf5a39b | 8350 | Rewrite (N, Relocate_Node (Operand)); |
e606088a | 8351 | goto Done; |
70482933 RK |
8352 | end if; |
8353 | ||
685094bf RD |
8354 | -- Nothing to do if this is the second argument of read. This is a |
8355 | -- "backwards" conversion that will be handled by the specialized code | |
8356 | -- in attribute processing. | |
70482933 RK |
8357 | |
8358 | if Nkind (Parent (N)) = N_Attribute_Reference | |
8359 | and then Attribute_Name (Parent (N)) = Name_Read | |
8360 | and then Next (First (Expressions (Parent (N)))) = N | |
8361 | then | |
e606088a AC |
8362 | goto Done; |
8363 | end if; | |
8364 | ||
8365 | -- Check for case of converting to a type that has an invariant | |
8366 | -- associated with it. This required an invariant check. We convert | |
8367 | ||
8368 | -- typ (expr) | |
8369 | ||
8370 | -- into | |
8371 | ||
8372 | -- do invariant_check (typ (expr)) in typ (expr); | |
8373 | ||
8374 | -- using Duplicate_Subexpr to avoid multiple side effects | |
8375 | ||
8376 | -- Note: the Comes_From_Source check, and then the resetting of this | |
8377 | -- flag prevents what would otherwise be an infinite recursion. | |
8378 | ||
fd0ff1cf RD |
8379 | if Has_Invariants (Target_Type) |
8380 | and then Present (Invariant_Procedure (Target_Type)) | |
e606088a AC |
8381 | and then Comes_From_Source (N) |
8382 | then | |
8383 | Set_Comes_From_Source (N, False); | |
8384 | Rewrite (N, | |
8385 | Make_Expression_With_Actions (Loc, | |
8386 | Actions => New_List ( | |
8387 | Make_Invariant_Call (Duplicate_Subexpr (N))), | |
8388 | Expression => Duplicate_Subexpr_No_Checks (N))); | |
8389 | Analyze_And_Resolve (N, Target_Type); | |
8390 | goto Done; | |
70482933 RK |
8391 | end if; |
8392 | ||
8393 | -- Here if we may need to expand conversion | |
8394 | ||
eaa826f8 RD |
8395 | -- If the operand of the type conversion is an arithmetic operation on |
8396 | -- signed integers, and the based type of the signed integer type in | |
8397 | -- question is smaller than Standard.Integer, we promote both of the | |
8398 | -- operands to type Integer. | |
8399 | ||
8400 | -- For example, if we have | |
8401 | ||
8402 | -- target-type (opnd1 + opnd2) | |
8403 | ||
8404 | -- and opnd1 and opnd2 are of type short integer, then we rewrite | |
8405 | -- this as: | |
8406 | ||
8407 | -- target-type (integer(opnd1) + integer(opnd2)) | |
8408 | ||
8409 | -- We do this because we are always allowed to compute in a larger type | |
8410 | -- if we do the right thing with the result, and in this case we are | |
8411 | -- going to do a conversion which will do an appropriate check to make | |
8412 | -- sure that things are in range of the target type in any case. This | |
8413 | -- avoids some unnecessary intermediate overflows. | |
8414 | ||
dfcfdc0a AC |
8415 | -- We might consider a similar transformation in the case where the |
8416 | -- target is a real type or a 64-bit integer type, and the operand | |
8417 | -- is an arithmetic operation using a 32-bit integer type. However, | |
8418 | -- we do not bother with this case, because it could cause significant | |
308e6f3a | 8419 | -- inefficiencies on 32-bit machines. On a 64-bit machine it would be |
dfcfdc0a AC |
8420 | -- much cheaper, but we don't want different behavior on 32-bit and |
8421 | -- 64-bit machines. Note that the exclusion of the 64-bit case also | |
8422 | -- handles the configurable run-time cases where 64-bit arithmetic | |
8423 | -- may simply be unavailable. | |
eaa826f8 RD |
8424 | |
8425 | -- Note: this circuit is partially redundant with respect to the circuit | |
8426 | -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in | |
8427 | -- the processing here. Also we still need the Checks circuit, since we | |
8428 | -- have to be sure not to generate junk overflow checks in the first | |
8429 | -- place, since it would be trick to remove them here! | |
8430 | ||
fdfcc663 | 8431 | if Integer_Promotion_Possible (N) then |
eaa826f8 | 8432 | |
fdfcc663 | 8433 | -- All conditions met, go ahead with transformation |
eaa826f8 | 8434 | |
fdfcc663 AC |
8435 | declare |
8436 | Opnd : Node_Id; | |
8437 | L, R : Node_Id; | |
dfcfdc0a | 8438 | |
fdfcc663 AC |
8439 | begin |
8440 | R := | |
8441 | Make_Type_Conversion (Loc, | |
8442 | Subtype_Mark => New_Reference_To (Standard_Integer, Loc), | |
8443 | Expression => Relocate_Node (Right_Opnd (Operand))); | |
eaa826f8 | 8444 | |
5f3f175d AC |
8445 | Opnd := New_Op_Node (Nkind (Operand), Loc); |
8446 | Set_Right_Opnd (Opnd, R); | |
eaa826f8 | 8447 | |
5f3f175d | 8448 | if Nkind (Operand) in N_Binary_Op then |
fdfcc663 | 8449 | L := |
eaa826f8 | 8450 | Make_Type_Conversion (Loc, |
dfcfdc0a | 8451 | Subtype_Mark => New_Reference_To (Standard_Integer, Loc), |
fdfcc663 AC |
8452 | Expression => Relocate_Node (Left_Opnd (Operand))); |
8453 | ||
5f3f175d AC |
8454 | Set_Left_Opnd (Opnd, L); |
8455 | end if; | |
eaa826f8 | 8456 | |
5f3f175d AC |
8457 | Rewrite (N, |
8458 | Make_Type_Conversion (Loc, | |
8459 | Subtype_Mark => Relocate_Node (Subtype_Mark (N)), | |
8460 | Expression => Opnd)); | |
dfcfdc0a | 8461 | |
5f3f175d | 8462 | Analyze_And_Resolve (N, Target_Type); |
e606088a | 8463 | goto Done; |
fdfcc663 AC |
8464 | end; |
8465 | end if; | |
eaa826f8 | 8466 | |
f82944b7 JM |
8467 | -- Do validity check if validity checking operands |
8468 | ||
8469 | if Validity_Checks_On | |
8470 | and then Validity_Check_Operands | |
8471 | then | |
8472 | Ensure_Valid (Operand); | |
8473 | end if; | |
8474 | ||
70482933 RK |
8475 | -- Special case of converting from non-standard boolean type |
8476 | ||
8477 | if Is_Boolean_Type (Operand_Type) | |
8478 | and then (Nonzero_Is_True (Operand_Type)) | |
8479 | then | |
8480 | Adjust_Condition (Operand); | |
8481 | Set_Etype (Operand, Standard_Boolean); | |
8482 | Operand_Type := Standard_Boolean; | |
8483 | end if; | |
8484 | ||
8485 | -- Case of converting to an access type | |
8486 | ||
8487 | if Is_Access_Type (Target_Type) then | |
8488 | ||
d766cee3 RD |
8489 | -- Apply an accessibility check when the conversion operand is an |
8490 | -- access parameter (or a renaming thereof), unless conversion was | |
e84e11ba GD |
8491 | -- expanded from an Unchecked_ or Unrestricted_Access attribute. |
8492 | -- Note that other checks may still need to be applied below (such | |
8493 | -- as tagged type checks). | |
70482933 RK |
8494 | |
8495 | if Is_Entity_Name (Operand) | |
d766cee3 RD |
8496 | and then |
8497 | (Is_Formal (Entity (Operand)) | |
8498 | or else | |
8499 | (Present (Renamed_Object (Entity (Operand))) | |
8500 | and then Is_Entity_Name (Renamed_Object (Entity (Operand))) | |
8501 | and then Is_Formal | |
8502 | (Entity (Renamed_Object (Entity (Operand)))))) | |
70482933 | 8503 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type |
d766cee3 RD |
8504 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
8505 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 8506 | then |
e84e11ba GD |
8507 | Apply_Accessibility_Check |
8508 | (Operand, Target_Type, Insert_Node => Operand); | |
70482933 | 8509 | |
e84e11ba | 8510 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
8511 | -- level of the target type, then force Program_Error. Note that this |
8512 | -- can only occur for cases where the attribute is within the body of | |
8513 | -- an instantiation (otherwise the conversion will already have been | |
8514 | -- rejected as illegal). Note: warnings are issued by the analyzer | |
8515 | -- for the instance cases. | |
70482933 RK |
8516 | |
8517 | elsif In_Instance_Body | |
07fc65c4 GB |
8518 | and then Type_Access_Level (Operand_Type) > |
8519 | Type_Access_Level (Target_Type) | |
70482933 | 8520 | then |
426908f8 | 8521 | Raise_Accessibility_Error; |
70482933 | 8522 | |
685094bf RD |
8523 | -- When the operand is a selected access discriminant the check needs |
8524 | -- to be made against the level of the object denoted by the prefix | |
8525 | -- of the selected name. Force Program_Error for this case as well | |
8526 | -- (this accessibility violation can only happen if within the body | |
8527 | -- of an instantiation). | |
70482933 RK |
8528 | |
8529 | elsif In_Instance_Body | |
8530 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
8531 | and then Nkind (Operand) = N_Selected_Component | |
8532 | and then Object_Access_Level (Operand) > | |
8533 | Type_Access_Level (Target_Type) | |
8534 | then | |
426908f8 | 8535 | Raise_Accessibility_Error; |
e606088a | 8536 | goto Done; |
70482933 RK |
8537 | end if; |
8538 | end if; | |
8539 | ||
8540 | -- Case of conversions of tagged types and access to tagged types | |
8541 | ||
685094bf RD |
8542 | -- When needed, that is to say when the expression is class-wide, Add |
8543 | -- runtime a tag check for (strict) downward conversion by using the | |
8544 | -- membership test, generating: | |
70482933 RK |
8545 | |
8546 | -- [constraint_error when Operand not in Target_Type'Class] | |
8547 | ||
8548 | -- or in the access type case | |
8549 | ||
8550 | -- [constraint_error | |
8551 | -- when Operand /= null | |
8552 | -- and then Operand.all not in | |
8553 | -- Designated_Type (Target_Type)'Class] | |
8554 | ||
8555 | if (Is_Access_Type (Target_Type) | |
8556 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
8557 | or else Is_Tagged_Type (Target_Type) | |
8558 | then | |
685094bf RD |
8559 | -- Do not do any expansion in the access type case if the parent is a |
8560 | -- renaming, since this is an error situation which will be caught by | |
8561 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 | 8562 | |
e7e4d230 | 8563 | if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then |
e606088a | 8564 | goto Done; |
70482933 RK |
8565 | end if; |
8566 | ||
0669bebe | 8567 | -- Otherwise, proceed with processing tagged conversion |
70482933 | 8568 | |
e7e4d230 | 8569 | Tagged_Conversion : declare |
8cea7b64 HK |
8570 | Actual_Op_Typ : Entity_Id; |
8571 | Actual_Targ_Typ : Entity_Id; | |
8572 | Make_Conversion : Boolean := False; | |
8573 | Root_Op_Typ : Entity_Id; | |
70482933 | 8574 | |
8cea7b64 HK |
8575 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
8576 | -- Create a membership check to test whether Operand is a member | |
8577 | -- of Targ_Typ. If the original Target_Type is an access, include | |
8578 | -- a test for null value. The check is inserted at N. | |
8579 | ||
8580 | -------------------- | |
8581 | -- Make_Tag_Check -- | |
8582 | -------------------- | |
8583 | ||
8584 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
8585 | Cond : Node_Id; | |
8586 | ||
8587 | begin | |
8588 | -- Generate: | |
8589 | -- [Constraint_Error | |
8590 | -- when Operand /= null | |
8591 | -- and then Operand.all not in Targ_Typ] | |
8592 | ||
8593 | if Is_Access_Type (Target_Type) then | |
8594 | Cond := | |
8595 | Make_And_Then (Loc, | |
8596 | Left_Opnd => | |
8597 | Make_Op_Ne (Loc, | |
8598 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
8599 | Right_Opnd => Make_Null (Loc)), | |
8600 | ||
8601 | Right_Opnd => | |
8602 | Make_Not_In (Loc, | |
8603 | Left_Opnd => | |
8604 | Make_Explicit_Dereference (Loc, | |
8605 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
8606 | Right_Opnd => New_Reference_To (Targ_Typ, Loc))); | |
8607 | ||
8608 | -- Generate: | |
8609 | -- [Constraint_Error when Operand not in Targ_Typ] | |
8610 | ||
8611 | else | |
8612 | Cond := | |
8613 | Make_Not_In (Loc, | |
8614 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
8615 | Right_Opnd => New_Reference_To (Targ_Typ, Loc)); | |
8616 | end if; | |
8617 | ||
8618 | Insert_Action (N, | |
8619 | Make_Raise_Constraint_Error (Loc, | |
8620 | Condition => Cond, | |
8621 | Reason => CE_Tag_Check_Failed)); | |
8622 | end Make_Tag_Check; | |
8623 | ||
e7e4d230 | 8624 | -- Start of processing for Tagged_Conversion |
70482933 RK |
8625 | |
8626 | begin | |
9732e886 | 8627 | -- Handle entities from the limited view |
852dba80 | 8628 | |
9732e886 | 8629 | if Is_Access_Type (Operand_Type) then |
852dba80 AC |
8630 | Actual_Op_Typ := |
8631 | Available_View (Designated_Type (Operand_Type)); | |
9732e886 JM |
8632 | else |
8633 | Actual_Op_Typ := Operand_Type; | |
8634 | end if; | |
8635 | ||
8636 | if Is_Access_Type (Target_Type) then | |
852dba80 AC |
8637 | Actual_Targ_Typ := |
8638 | Available_View (Designated_Type (Target_Type)); | |
70482933 | 8639 | else |
8cea7b64 | 8640 | Actual_Targ_Typ := Target_Type; |
70482933 RK |
8641 | end if; |
8642 | ||
8cea7b64 HK |
8643 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
8644 | ||
20b5d666 JM |
8645 | -- Ada 2005 (AI-251): Handle interface type conversion |
8646 | ||
8cea7b64 | 8647 | if Is_Interface (Actual_Op_Typ) then |
20b5d666 | 8648 | Expand_Interface_Conversion (N, Is_Static => False); |
e606088a | 8649 | goto Done; |
20b5d666 JM |
8650 | end if; |
8651 | ||
8cea7b64 | 8652 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 8653 | |
8cea7b64 HK |
8654 | -- Create a runtime tag check for a downward class-wide type |
8655 | -- conversion. | |
70482933 | 8656 | |
8cea7b64 | 8657 | if Is_Class_Wide_Type (Actual_Op_Typ) |
852dba80 | 8658 | and then Actual_Op_Typ /= Actual_Targ_Typ |
8cea7b64 | 8659 | and then Root_Op_Typ /= Actual_Targ_Typ |
4ac2477e JM |
8660 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ, |
8661 | Use_Full_View => True) | |
8cea7b64 HK |
8662 | then |
8663 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
8664 | Make_Conversion := True; | |
8665 | end if; | |
70482933 | 8666 | |
8cea7b64 HK |
8667 | -- AI05-0073: If the result subtype of the function is defined |
8668 | -- by an access_definition designating a specific tagged type | |
8669 | -- T, a check is made that the result value is null or the tag | |
8670 | -- of the object designated by the result value identifies T. | |
8671 | -- Constraint_Error is raised if this check fails. | |
70482933 | 8672 | |
8cea7b64 HK |
8673 | if Nkind (Parent (N)) = Sinfo.N_Return_Statement then |
8674 | declare | |
e886436a | 8675 | Func : Entity_Id; |
8cea7b64 HK |
8676 | Func_Typ : Entity_Id; |
8677 | ||
8678 | begin | |
e886436a | 8679 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 8680 | |
e886436a | 8681 | Func := Current_Scope; |
8cea7b64 HK |
8682 | while Present (Func) |
8683 | and then Ekind (Func) /= E_Function | |
8684 | loop | |
8685 | Func := Scope (Func); | |
8686 | end loop; | |
8687 | ||
8688 | -- The function's return subtype must be defined using | |
8689 | -- an access definition. | |
8690 | ||
8691 | if Nkind (Result_Definition (Parent (Func))) = | |
8692 | N_Access_Definition | |
8693 | then | |
8694 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
8695 | ||
8696 | -- The return subtype denotes a specific tagged type, | |
8697 | -- in other words, a non class-wide type. | |
8698 | ||
8699 | if Is_Tagged_Type (Func_Typ) | |
8700 | and then not Is_Class_Wide_Type (Func_Typ) | |
8701 | then | |
8702 | Make_Tag_Check (Actual_Targ_Typ); | |
8703 | Make_Conversion := True; | |
8704 | end if; | |
8705 | end if; | |
8706 | end; | |
70482933 RK |
8707 | end if; |
8708 | ||
8cea7b64 HK |
8709 | -- We have generated a tag check for either a class-wide type |
8710 | -- conversion or for AI05-0073. | |
70482933 | 8711 | |
8cea7b64 HK |
8712 | if Make_Conversion then |
8713 | declare | |
8714 | Conv : Node_Id; | |
8715 | begin | |
8716 | Conv := | |
8717 | Make_Unchecked_Type_Conversion (Loc, | |
8718 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
8719 | Expression => Relocate_Node (Expression (N))); | |
8720 | Rewrite (N, Conv); | |
8721 | Analyze_And_Resolve (N, Target_Type); | |
8722 | end; | |
8723 | end if; | |
70482933 | 8724 | end if; |
e7e4d230 | 8725 | end Tagged_Conversion; |
70482933 RK |
8726 | |
8727 | -- Case of other access type conversions | |
8728 | ||
8729 | elsif Is_Access_Type (Target_Type) then | |
8730 | Apply_Constraint_Check (Operand, Target_Type); | |
8731 | ||
8732 | -- Case of conversions from a fixed-point type | |
8733 | ||
685094bf RD |
8734 | -- These conversions require special expansion and processing, found in |
8735 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
8736 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
8737 | -- conversions, which do not need further processing. |
8738 | ||
8739 | elsif Is_Fixed_Point_Type (Operand_Type) | |
8740 | and then not Conversion_OK (N) | |
8741 | then | |
8742 | -- We should never see universal fixed at this case, since the | |
8743 | -- expansion of the constituent divide or multiply should have | |
8744 | -- eliminated the explicit mention of universal fixed. | |
8745 | ||
8746 | pragma Assert (Operand_Type /= Universal_Fixed); | |
8747 | ||
685094bf RD |
8748 | -- Check for special case of the conversion to universal real that |
8749 | -- occurs as a result of the use of a round attribute. In this case, | |
8750 | -- the real type for the conversion is taken from the target type of | |
8751 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
8752 | |
8753 | if Target_Type = Universal_Real | |
8754 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
8755 | and then Attribute_Name (Parent (N)) = Name_Round | |
8756 | then | |
8757 | Set_Rounded_Result (N); | |
8758 | Set_Etype (N, Etype (Parent (N))); | |
8759 | end if; | |
8760 | ||
8761 | -- Otherwise do correct fixed-conversion, but skip these if the | |
e7e4d230 AC |
8762 | -- Conversion_OK flag is set, because from a semantic point of view |
8763 | -- these are simple integer conversions needing no further processing | |
8764 | -- (the backend will simply treat them as integers). | |
70482933 RK |
8765 | |
8766 | if not Conversion_OK (N) then | |
8767 | if Is_Fixed_Point_Type (Etype (N)) then | |
8768 | Expand_Convert_Fixed_To_Fixed (N); | |
8769 | Real_Range_Check; | |
8770 | ||
8771 | elsif Is_Integer_Type (Etype (N)) then | |
8772 | Expand_Convert_Fixed_To_Integer (N); | |
8773 | ||
8774 | else | |
8775 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
8776 | Expand_Convert_Fixed_To_Float (N); | |
8777 | Real_Range_Check; | |
8778 | end if; | |
8779 | end if; | |
8780 | ||
8781 | -- Case of conversions to a fixed-point type | |
8782 | ||
685094bf RD |
8783 | -- These conversions require special expansion and processing, found in |
8784 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
8785 | -- since from a semantic point of view, these are simple integer | |
8786 | -- conversions, which do not need further processing. | |
70482933 RK |
8787 | |
8788 | elsif Is_Fixed_Point_Type (Target_Type) | |
8789 | and then not Conversion_OK (N) | |
8790 | then | |
8791 | if Is_Integer_Type (Operand_Type) then | |
8792 | Expand_Convert_Integer_To_Fixed (N); | |
8793 | Real_Range_Check; | |
8794 | else | |
8795 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
8796 | Expand_Convert_Float_To_Fixed (N); | |
8797 | Real_Range_Check; | |
8798 | end if; | |
8799 | ||
8800 | -- Case of float-to-integer conversions | |
8801 | ||
8802 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
8803 | -- since semantically the fixed-point target is treated as though it | |
8804 | -- were an integer in such cases. | |
8805 | ||
8806 | elsif Is_Floating_Point_Type (Operand_Type) | |
8807 | and then | |
8808 | (Is_Integer_Type (Target_Type) | |
8809 | or else | |
8810 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
8811 | then | |
70482933 RK |
8812 | -- One more check here, gcc is still not able to do conversions of |
8813 | -- this type with proper overflow checking, and so gigi is doing an | |
8814 | -- approximation of what is required by doing floating-point compares | |
8815 | -- with the end-point. But that can lose precision in some cases, and | |
f02b8bb8 | 8816 | -- give a wrong result. Converting the operand to Universal_Real is |
70482933 | 8817 | -- helpful, but still does not catch all cases with 64-bit integers |
e7e4d230 | 8818 | -- on targets with only 64-bit floats. |
0669bebe GB |
8819 | |
8820 | -- The above comment seems obsoleted by Apply_Float_Conversion_Check | |
8821 | -- Can this code be removed ??? | |
70482933 | 8822 | |
fbf5a39b AC |
8823 | if Do_Range_Check (Operand) then |
8824 | Rewrite (Operand, | |
70482933 RK |
8825 | Make_Type_Conversion (Loc, |
8826 | Subtype_Mark => | |
f02b8bb8 | 8827 | New_Occurrence_Of (Universal_Real, Loc), |
70482933 | 8828 | Expression => |
fbf5a39b | 8829 | Relocate_Node (Operand))); |
70482933 | 8830 | |
f02b8bb8 | 8831 | Set_Etype (Operand, Universal_Real); |
fbf5a39b AC |
8832 | Enable_Range_Check (Operand); |
8833 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
8834 | end if; |
8835 | ||
8836 | -- Case of array conversions | |
8837 | ||
685094bf RD |
8838 | -- Expansion of array conversions, add required length/range checks but |
8839 | -- only do this if there is no change of representation. For handling of | |
8840 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
8841 | |
8842 | elsif Is_Array_Type (Target_Type) then | |
70482933 RK |
8843 | if Is_Constrained (Target_Type) then |
8844 | Apply_Length_Check (Operand, Target_Type); | |
8845 | else | |
8846 | Apply_Range_Check (Operand, Target_Type); | |
8847 | end if; | |
8848 | ||
8849 | Handle_Changed_Representation; | |
8850 | ||
8851 | -- Case of conversions of discriminated types | |
8852 | ||
685094bf RD |
8853 | -- Add required discriminant checks if target is constrained. Again this |
8854 | -- change is skipped if we have a change of representation. | |
70482933 RK |
8855 | |
8856 | elsif Has_Discriminants (Target_Type) | |
8857 | and then Is_Constrained (Target_Type) | |
8858 | then | |
8859 | Apply_Discriminant_Check (Operand, Target_Type); | |
8860 | Handle_Changed_Representation; | |
8861 | ||
8862 | -- Case of all other record conversions. The only processing required | |
8863 | -- is to check for a change of representation requiring the special | |
8864 | -- assignment processing. | |
8865 | ||
8866 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
8867 | |
8868 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
8869 | -- a derived Unchecked_Union type to an unconstrained type that is |
8870 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
8871 | |
8872 | if Is_Derived_Type (Operand_Type) | |
8873 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
8874 | and then not Is_Constrained (Target_Type) | |
8875 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
8876 | and then not Has_Inferable_Discriminants (Operand) | |
8877 | then | |
685094bf | 8878 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e AC |
8879 | -- Program_Error node, but we give it the target type of the |
8880 | -- conversion. | |
8881 | ||
8882 | declare | |
8883 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
8884 | Reason => PE_Unchecked_Union_Restriction); | |
8885 | ||
8886 | begin | |
8887 | Set_Etype (PE, Target_Type); | |
8888 | Rewrite (N, PE); | |
8889 | ||
8890 | end; | |
8891 | else | |
8892 | Handle_Changed_Representation; | |
8893 | end if; | |
70482933 RK |
8894 | |
8895 | -- Case of conversions of enumeration types | |
8896 | ||
8897 | elsif Is_Enumeration_Type (Target_Type) then | |
8898 | ||
8899 | -- Special processing is required if there is a change of | |
e7e4d230 | 8900 | -- representation (from enumeration representation clauses). |
70482933 RK |
8901 | |
8902 | if not Same_Representation (Target_Type, Operand_Type) then | |
8903 | ||
8904 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
8905 | ||
8906 | Rewrite (N, | |
8907 | Make_Attribute_Reference (Loc, | |
8908 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
8909 | Attribute_Name => Name_Val, | |
8910 | Expressions => New_List ( | |
8911 | Make_Attribute_Reference (Loc, | |
8912 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
8913 | Attribute_Name => Name_Pos, | |
8914 | Expressions => New_List (Operand))))); | |
8915 | ||
8916 | Analyze_And_Resolve (N, Target_Type); | |
8917 | end if; | |
8918 | ||
8919 | -- Case of conversions to floating-point | |
8920 | ||
8921 | elsif Is_Floating_Point_Type (Target_Type) then | |
8922 | Real_Range_Check; | |
70482933 RK |
8923 | end if; |
8924 | ||
685094bf | 8925 | -- At this stage, either the conversion node has been transformed into |
e7e4d230 AC |
8926 | -- some other equivalent expression, or left as a conversion that can be |
8927 | -- handled by Gigi, in the following cases: | |
70482933 RK |
8928 | |
8929 | -- Conversions with no change of representation or type | |
8930 | ||
685094bf RD |
8931 | -- Numeric conversions involving integer, floating- and fixed-point |
8932 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
8933 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 8934 | |
5e1c00fa RD |
8935 | -- No other conversions should be passed to Gigi |
8936 | ||
8937 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 8938 | |
685094bf RD |
8939 | -- The only remaining step is to generate a range check if we still have |
8940 | -- a type conversion at this stage and Do_Range_Check is set. For now we | |
8941 | -- do this only for conversions of discrete types. | |
fbf5a39b AC |
8942 | |
8943 | if Nkind (N) = N_Type_Conversion | |
8944 | and then Is_Discrete_Type (Etype (N)) | |
8945 | then | |
8946 | declare | |
8947 | Expr : constant Node_Id := Expression (N); | |
8948 | Ftyp : Entity_Id; | |
8949 | Ityp : Entity_Id; | |
8950 | ||
8951 | begin | |
8952 | if Do_Range_Check (Expr) | |
8953 | and then Is_Discrete_Type (Etype (Expr)) | |
8954 | then | |
8955 | Set_Do_Range_Check (Expr, False); | |
8956 | ||
685094bf RD |
8957 | -- Before we do a range check, we have to deal with treating a |
8958 | -- fixed-point operand as an integer. The way we do this is | |
8959 | -- simply to do an unchecked conversion to an appropriate | |
fbf5a39b AC |
8960 | -- integer type large enough to hold the result. |
8961 | ||
8962 | -- This code is not active yet, because we are only dealing | |
8963 | -- with discrete types so far ??? | |
8964 | ||
8965 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer | |
8966 | and then Treat_Fixed_As_Integer (Expr) | |
8967 | then | |
8968 | Ftyp := Base_Type (Etype (Expr)); | |
8969 | ||
8970 | if Esize (Ftyp) >= Esize (Standard_Integer) then | |
8971 | Ityp := Standard_Long_Long_Integer; | |
8972 | else | |
8973 | Ityp := Standard_Integer; | |
8974 | end if; | |
8975 | ||
8976 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); | |
8977 | end if; | |
8978 | ||
8979 | -- Reset overflow flag, since the range check will include | |
e7e4d230 | 8980 | -- dealing with possible overflow, and generate the check. If |
685094bf | 8981 | -- Address is either a source type or target type, suppress |
8a36a0cc AC |
8982 | -- range check to avoid typing anomalies when it is a visible |
8983 | -- integer type. | |
fbf5a39b AC |
8984 | |
8985 | Set_Do_Overflow_Check (N, False); | |
8a36a0cc AC |
8986 | if not Is_Descendent_Of_Address (Etype (Expr)) |
8987 | and then not Is_Descendent_Of_Address (Target_Type) | |
8988 | then | |
8989 | Generate_Range_Check | |
8990 | (Expr, Target_Type, CE_Range_Check_Failed); | |
8991 | end if; | |
fbf5a39b AC |
8992 | end if; |
8993 | end; | |
8994 | end if; | |
f02b8bb8 RD |
8995 | |
8996 | -- Final step, if the result is a type conversion involving Vax_Float | |
8997 | -- types, then it is subject for further special processing. | |
8998 | ||
8999 | if Nkind (N) = N_Type_Conversion | |
9000 | and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type)) | |
9001 | then | |
9002 | Expand_Vax_Conversion (N); | |
e606088a | 9003 | goto Done; |
f02b8bb8 | 9004 | end if; |
e606088a AC |
9005 | |
9006 | -- Here at end of processing | |
9007 | ||
48f91b44 RD |
9008 | <<Done>> |
9009 | -- Apply predicate check if required. Note that we can't just call | |
9010 | -- Apply_Predicate_Check here, because the type looks right after | |
9011 | -- the conversion and it would omit the check. The Comes_From_Source | |
9012 | -- guard is necessary to prevent infinite recursions when we generate | |
9013 | -- internal conversions for the purpose of checking predicates. | |
9014 | ||
9015 | if Present (Predicate_Function (Target_Type)) | |
9016 | and then Target_Type /= Operand_Type | |
9017 | and then Comes_From_Source (N) | |
9018 | then | |
9019 | Insert_Action (N, | |
9020 | Make_Predicate_Check (Target_Type, Duplicate_Subexpr (N))); | |
9021 | end if; | |
70482933 RK |
9022 | end Expand_N_Type_Conversion; |
9023 | ||
9024 | ----------------------------------- | |
9025 | -- Expand_N_Unchecked_Expression -- | |
9026 | ----------------------------------- | |
9027 | ||
e7e4d230 | 9028 | -- Remove the unchecked expression node from the tree. Its job was simply |
70482933 RK |
9029 | -- to make sure that its constituent expression was handled with checks |
9030 | -- off, and now that that is done, we can remove it from the tree, and | |
e7e4d230 | 9031 | -- indeed must, since Gigi does not expect to see these nodes. |
70482933 RK |
9032 | |
9033 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
9034 | Exp : constant Node_Id := Expression (N); | |
70482933 | 9035 | begin |
e7e4d230 | 9036 | Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp)); |
70482933 RK |
9037 | Rewrite (N, Exp); |
9038 | end Expand_N_Unchecked_Expression; | |
9039 | ||
9040 | ---------------------------------------- | |
9041 | -- Expand_N_Unchecked_Type_Conversion -- | |
9042 | ---------------------------------------- | |
9043 | ||
685094bf RD |
9044 | -- If this cannot be handled by Gigi and we haven't already made a |
9045 | -- temporary for it, do it now. | |
70482933 RK |
9046 | |
9047 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
9048 | Target_Type : constant Entity_Id := Etype (N); | |
9049 | Operand : constant Node_Id := Expression (N); | |
9050 | Operand_Type : constant Entity_Id := Etype (Operand); | |
9051 | ||
9052 | begin | |
7b00e31d | 9053 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 | 9054 | -- the conversion completely, it is useless, except that it may carry |
e7e4d230 | 9055 | -- an Assignment_OK indication which must be propagated to the operand. |
7b00e31d AC |
9056 | |
9057 | if Operand_Type = Target_Type then | |
13d923cc | 9058 | |
e7e4d230 AC |
9059 | -- Code duplicates Expand_N_Unchecked_Expression above, factor??? |
9060 | ||
7b00e31d AC |
9061 | if Assignment_OK (N) then |
9062 | Set_Assignment_OK (Operand); | |
9063 | end if; | |
9064 | ||
9065 | Rewrite (N, Relocate_Node (Operand)); | |
9066 | return; | |
9067 | end if; | |
9068 | ||
70482933 RK |
9069 | -- If we have a conversion of a compile time known value to a target |
9070 | -- type and the value is in range of the target type, then we can simply | |
9071 | -- replace the construct by an integer literal of the correct type. We | |
9072 | -- only apply this to integer types being converted. Possibly it may | |
9073 | -- apply in other cases, but it is too much trouble to worry about. | |
9074 | ||
9075 | -- Note that we do not do this transformation if the Kill_Range_Check | |
9076 | -- flag is set, since then the value may be outside the expected range. | |
9077 | -- This happens in the Normalize_Scalars case. | |
9078 | ||
20b5d666 JM |
9079 | -- We also skip this if either the target or operand type is biased |
9080 | -- because in this case, the unchecked conversion is supposed to | |
9081 | -- preserve the bit pattern, not the integer value. | |
9082 | ||
70482933 | 9083 | if Is_Integer_Type (Target_Type) |
20b5d666 | 9084 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 9085 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 9086 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
9087 | and then Compile_Time_Known_Value (Operand) |
9088 | and then not Kill_Range_Check (N) | |
9089 | then | |
9090 | declare | |
9091 | Val : constant Uint := Expr_Value (Operand); | |
9092 | ||
9093 | begin | |
9094 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
9095 | and then | |
9096 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
9097 | and then | |
9098 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
9099 | and then | |
9100 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
9101 | then | |
9102 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 9103 | |
685094bf RD |
9104 | -- If Address is the target type, just set the type to avoid a |
9105 | -- spurious type error on the literal when Address is a visible | |
9106 | -- integer type. | |
8a36a0cc AC |
9107 | |
9108 | if Is_Descendent_Of_Address (Target_Type) then | |
9109 | Set_Etype (N, Target_Type); | |
9110 | else | |
9111 | Analyze_And_Resolve (N, Target_Type); | |
9112 | end if; | |
9113 | ||
70482933 RK |
9114 | return; |
9115 | end if; | |
9116 | end; | |
9117 | end if; | |
9118 | ||
9119 | -- Nothing to do if conversion is safe | |
9120 | ||
9121 | if Safe_Unchecked_Type_Conversion (N) then | |
9122 | return; | |
9123 | end if; | |
9124 | ||
9125 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
9126 | -- flag indicates ??? -- more comments needed here) | |
9127 | ||
9128 | if Assignment_OK (N) then | |
9129 | null; | |
9130 | else | |
9131 | Force_Evaluation (N); | |
9132 | end if; | |
9133 | end Expand_N_Unchecked_Type_Conversion; | |
9134 | ||
9135 | ---------------------------- | |
9136 | -- Expand_Record_Equality -- | |
9137 | ---------------------------- | |
9138 | ||
9139 | -- For non-variant records, Equality is expanded when needed into: | |
9140 | ||
9141 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
9142 | -- and then ... | |
9143 | -- and then Lhs.Discrn = Rhs.Discrn | |
9144 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
9145 | -- and then ... | |
9146 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
9147 | ||
9148 | -- The expression is folded by the back-end for adjacent fields. This | |
9149 | -- function is called for tagged record in only one occasion: for imple- | |
9150 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
9151 | -- otherwise the primitive "=" is used directly. | |
9152 | ||
9153 | function Expand_Record_Equality | |
9154 | (Nod : Node_Id; | |
9155 | Typ : Entity_Id; | |
9156 | Lhs : Node_Id; | |
9157 | Rhs : Node_Id; | |
2e071734 | 9158 | Bodies : List_Id) return Node_Id |
70482933 RK |
9159 | is |
9160 | Loc : constant Source_Ptr := Sloc (Nod); | |
9161 | ||
0ab80019 AC |
9162 | Result : Node_Id; |
9163 | C : Entity_Id; | |
9164 | ||
9165 | First_Time : Boolean := True; | |
9166 | ||
70482933 RK |
9167 | function Suitable_Element (C : Entity_Id) return Entity_Id; |
9168 | -- Return the first field to compare beginning with C, skipping the | |
0ab80019 AC |
9169 | -- inherited components. |
9170 | ||
9171 | ---------------------- | |
9172 | -- Suitable_Element -- | |
9173 | ---------------------- | |
70482933 RK |
9174 | |
9175 | function Suitable_Element (C : Entity_Id) return Entity_Id is | |
9176 | begin | |
9177 | if No (C) then | |
9178 | return Empty; | |
9179 | ||
9180 | elsif Ekind (C) /= E_Discriminant | |
9181 | and then Ekind (C) /= E_Component | |
9182 | then | |
9183 | return Suitable_Element (Next_Entity (C)); | |
9184 | ||
9185 | elsif Is_Tagged_Type (Typ) | |
9186 | and then C /= Original_Record_Component (C) | |
9187 | then | |
9188 | return Suitable_Element (Next_Entity (C)); | |
9189 | ||
9190 | elsif Chars (C) = Name_uController | |
9191 | or else Chars (C) = Name_uTag | |
9192 | then | |
9193 | return Suitable_Element (Next_Entity (C)); | |
9194 | ||
26bff3d9 JM |
9195 | elsif Is_Interface (Etype (C)) then |
9196 | return Suitable_Element (Next_Entity (C)); | |
9197 | ||
70482933 RK |
9198 | else |
9199 | return C; | |
9200 | end if; | |
9201 | end Suitable_Element; | |
9202 | ||
70482933 RK |
9203 | -- Start of processing for Expand_Record_Equality |
9204 | ||
9205 | begin | |
70482933 RK |
9206 | -- Generates the following code: (assuming that Typ has one Discr and |
9207 | -- component C2 is also a record) | |
9208 | ||
9209 | -- True | |
9210 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
9211 | -- and then Lhs.C1 = Rhs.C1 | |
9212 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
9213 | -- and then ... | |
9214 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
9215 | ||
9216 | Result := New_Reference_To (Standard_True, Loc); | |
9217 | C := Suitable_Element (First_Entity (Typ)); | |
70482933 | 9218 | while Present (C) loop |
70482933 RK |
9219 | declare |
9220 | New_Lhs : Node_Id; | |
9221 | New_Rhs : Node_Id; | |
8aceda64 | 9222 | Check : Node_Id; |
70482933 RK |
9223 | |
9224 | begin | |
9225 | if First_Time then | |
9226 | First_Time := False; | |
9227 | New_Lhs := Lhs; | |
9228 | New_Rhs := Rhs; | |
70482933 RK |
9229 | else |
9230 | New_Lhs := New_Copy_Tree (Lhs); | |
9231 | New_Rhs := New_Copy_Tree (Rhs); | |
9232 | end if; | |
9233 | ||
8aceda64 AC |
9234 | Check := |
9235 | Expand_Composite_Equality (Nod, Etype (C), | |
9236 | Lhs => | |
9237 | Make_Selected_Component (Loc, | |
9238 | Prefix => New_Lhs, | |
9239 | Selector_Name => New_Reference_To (C, Loc)), | |
9240 | Rhs => | |
9241 | Make_Selected_Component (Loc, | |
9242 | Prefix => New_Rhs, | |
9243 | Selector_Name => New_Reference_To (C, Loc)), | |
9244 | Bodies => Bodies); | |
9245 | ||
9246 | -- If some (sub)component is an unchecked_union, the whole | |
9247 | -- operation will raise program error. | |
9248 | ||
9249 | if Nkind (Check) = N_Raise_Program_Error then | |
9250 | Result := Check; | |
9251 | Set_Etype (Result, Standard_Boolean); | |
9252 | exit; | |
9253 | else | |
9254 | Result := | |
9255 | Make_And_Then (Loc, | |
9256 | Left_Opnd => Result, | |
9257 | Right_Opnd => Check); | |
9258 | end if; | |
70482933 RK |
9259 | end; |
9260 | ||
9261 | C := Suitable_Element (Next_Entity (C)); | |
9262 | end loop; | |
9263 | ||
9264 | return Result; | |
9265 | end Expand_Record_Equality; | |
9266 | ||
5875f8d6 AC |
9267 | ----------------------------------- |
9268 | -- Expand_Short_Circuit_Operator -- | |
9269 | ----------------------------------- | |
9270 | ||
955871d3 AC |
9271 | -- Deal with special expansion if actions are present for the right operand |
9272 | -- and deal with optimizing case of arguments being True or False. We also | |
9273 | -- deal with the special case of non-standard boolean values. | |
5875f8d6 AC |
9274 | |
9275 | procedure Expand_Short_Circuit_Operator (N : Node_Id) is | |
9276 | Loc : constant Source_Ptr := Sloc (N); | |
9277 | Typ : constant Entity_Id := Etype (N); | |
5875f8d6 AC |
9278 | Left : constant Node_Id := Left_Opnd (N); |
9279 | Right : constant Node_Id := Right_Opnd (N); | |
955871d3 | 9280 | LocR : constant Source_Ptr := Sloc (Right); |
5875f8d6 AC |
9281 | Actlist : List_Id; |
9282 | ||
9283 | Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else; | |
9284 | Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value); | |
9285 | -- If Left = Shortcut_Value then Right need not be evaluated | |
9286 | ||
25adc5fb AC |
9287 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id; |
9288 | -- For Opnd a boolean expression, return a Boolean expression equivalent | |
9289 | -- to Opnd /= Shortcut_Value. | |
9290 | ||
9291 | -------------------- | |
9292 | -- Make_Test_Expr -- | |
9293 | -------------------- | |
9294 | ||
9295 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id is | |
9296 | begin | |
9297 | if Shortcut_Value then | |
9298 | return Make_Op_Not (Sloc (Opnd), Opnd); | |
9299 | else | |
9300 | return Opnd; | |
9301 | end if; | |
9302 | end Make_Test_Expr; | |
9303 | ||
9304 | Op_Var : Entity_Id; | |
9305 | -- Entity for a temporary variable holding the value of the operator, | |
9306 | -- used for expansion in the case where actions are present. | |
9307 | ||
9308 | -- Start of processing for Expand_Short_Circuit_Operator | |
5875f8d6 AC |
9309 | |
9310 | begin | |
9311 | -- Deal with non-standard booleans | |
9312 | ||
9313 | if Is_Boolean_Type (Typ) then | |
9314 | Adjust_Condition (Left); | |
9315 | Adjust_Condition (Right); | |
9316 | Set_Etype (N, Standard_Boolean); | |
9317 | end if; | |
9318 | ||
9319 | -- Check for cases where left argument is known to be True or False | |
9320 | ||
9321 | if Compile_Time_Known_Value (Left) then | |
25adc5fb AC |
9322 | |
9323 | -- Mark SCO for left condition as compile time known | |
9324 | ||
9325 | if Generate_SCO and then Comes_From_Source (Left) then | |
9326 | Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True); | |
9327 | end if; | |
9328 | ||
5875f8d6 AC |
9329 | -- Rewrite True AND THEN Right / False OR ELSE Right to Right. |
9330 | -- Any actions associated with Right will be executed unconditionally | |
9331 | -- and can thus be inserted into the tree unconditionally. | |
9332 | ||
9333 | if Expr_Value_E (Left) /= Shortcut_Ent then | |
9334 | if Present (Actions (N)) then | |
9335 | Insert_Actions (N, Actions (N)); | |
9336 | end if; | |
9337 | ||
9338 | Rewrite (N, Right); | |
9339 | ||
9340 | -- Rewrite False AND THEN Right / True OR ELSE Right to Left. | |
9341 | -- In this case we can forget the actions associated with Right, | |
9342 | -- since they will never be executed. | |
9343 | ||
9344 | else | |
9345 | Kill_Dead_Code (Right); | |
9346 | Kill_Dead_Code (Actions (N)); | |
9347 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
9348 | end if; | |
9349 | ||
9350 | Adjust_Result_Type (N, Typ); | |
9351 | return; | |
9352 | end if; | |
9353 | ||
955871d3 AC |
9354 | -- If Actions are present for the right operand, we have to do some |
9355 | -- special processing. We can't just let these actions filter back into | |
9356 | -- code preceding the short circuit (which is what would have happened | |
9357 | -- if we had not trapped them in the short-circuit form), since they | |
9358 | -- must only be executed if the right operand of the short circuit is | |
9359 | -- executed and not otherwise. | |
5875f8d6 | 9360 | |
955871d3 | 9361 | -- the temporary variable C. |
5875f8d6 | 9362 | |
955871d3 AC |
9363 | if Present (Actions (N)) then |
9364 | Actlist := Actions (N); | |
5875f8d6 | 9365 | |
955871d3 | 9366 | -- The old approach is to expand: |
5875f8d6 | 9367 | |
955871d3 | 9368 | -- left AND THEN right |
25adc5fb | 9369 | |
955871d3 | 9370 | -- into |
25adc5fb | 9371 | |
955871d3 AC |
9372 | -- C : Boolean := False; |
9373 | -- IF left THEN | |
9374 | -- Actions; | |
9375 | -- IF right THEN | |
9376 | -- C := True; | |
9377 | -- END IF; | |
9378 | -- END IF; | |
5875f8d6 | 9379 | |
955871d3 AC |
9380 | -- and finally rewrite the operator into a reference to C. Similarly |
9381 | -- for left OR ELSE right, with negated values. Note that this | |
9382 | -- rewrite causes some difficulties for coverage analysis because | |
9383 | -- of the introduction of the new variable C, which obscures the | |
9384 | -- structure of the test. | |
5875f8d6 | 9385 | |
9cbfc269 AC |
9386 | -- We use this "old approach" if use of N_Expression_With_Actions |
9387 | -- is False (see description in Opt of when this is or is not set). | |
5875f8d6 | 9388 | |
9cbfc269 | 9389 | if not Use_Expression_With_Actions then |
955871d3 | 9390 | Op_Var := Make_Temporary (Loc, 'C', Related_Node => N); |
5875f8d6 | 9391 | |
955871d3 AC |
9392 | Insert_Action (N, |
9393 | Make_Object_Declaration (Loc, | |
9394 | Defining_Identifier => | |
9395 | Op_Var, | |
9396 | Object_Definition => | |
9397 | New_Occurrence_Of (Standard_Boolean, Loc), | |
9398 | Expression => | |
9399 | New_Occurrence_Of (Shortcut_Ent, Loc))); | |
9400 | ||
9401 | Append_To (Actlist, | |
9402 | Make_Implicit_If_Statement (Right, | |
9403 | Condition => Make_Test_Expr (Right), | |
9404 | Then_Statements => New_List ( | |
9405 | Make_Assignment_Statement (LocR, | |
9406 | Name => New_Occurrence_Of (Op_Var, LocR), | |
9407 | Expression => | |
9408 | New_Occurrence_Of | |
9409 | (Boolean_Literals (not Shortcut_Value), LocR))))); | |
5875f8d6 | 9410 | |
955871d3 AC |
9411 | Insert_Action (N, |
9412 | Make_Implicit_If_Statement (Left, | |
9413 | Condition => Make_Test_Expr (Left), | |
9414 | Then_Statements => Actlist)); | |
9415 | ||
9416 | Rewrite (N, New_Occurrence_Of (Op_Var, Loc)); | |
9417 | Analyze_And_Resolve (N, Standard_Boolean); | |
9418 | ||
9419 | -- The new approach, activated for now by the use of debug flag | |
9420 | -- -gnatd.X is to use the new Expression_With_Actions node for the | |
9421 | -- right operand of the short-circuit form. This should solve the | |
9422 | -- traceability problems for coverage analysis. | |
9423 | ||
9424 | else | |
9425 | Rewrite (Right, | |
9426 | Make_Expression_With_Actions (LocR, | |
9427 | Expression => Relocate_Node (Right), | |
9428 | Actions => Actlist)); | |
48b351d9 | 9429 | Set_Actions (N, No_List); |
955871d3 AC |
9430 | Analyze_And_Resolve (Right, Standard_Boolean); |
9431 | end if; | |
9432 | ||
5875f8d6 AC |
9433 | Adjust_Result_Type (N, Typ); |
9434 | return; | |
9435 | end if; | |
9436 | ||
9437 | -- No actions present, check for cases of right argument True/False | |
9438 | ||
9439 | if Compile_Time_Known_Value (Right) then | |
25adc5fb AC |
9440 | |
9441 | -- Mark SCO for left condition as compile time known | |
9442 | ||
9443 | if Generate_SCO and then Comes_From_Source (Right) then | |
9444 | Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True); | |
9445 | end if; | |
9446 | ||
5875f8d6 AC |
9447 | -- Change (Left and then True), (Left or else False) to Left. |
9448 | -- Note that we know there are no actions associated with the right | |
9449 | -- operand, since we just checked for this case above. | |
9450 | ||
9451 | if Expr_Value_E (Right) /= Shortcut_Ent then | |
9452 | Rewrite (N, Left); | |
9453 | ||
9454 | -- Change (Left and then False), (Left or else True) to Right, | |
9455 | -- making sure to preserve any side effects associated with the Left | |
9456 | -- operand. | |
9457 | ||
9458 | else | |
9459 | Remove_Side_Effects (Left); | |
9460 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
9461 | end if; | |
9462 | end if; | |
9463 | ||
9464 | Adjust_Result_Type (N, Typ); | |
9465 | end Expand_Short_Circuit_Operator; | |
9466 | ||
70482933 RK |
9467 | ------------------------------------- |
9468 | -- Fixup_Universal_Fixed_Operation -- | |
9469 | ------------------------------------- | |
9470 | ||
9471 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
9472 | Conv : constant Node_Id := Parent (N); | |
9473 | ||
9474 | begin | |
9475 | -- We must have a type conversion immediately above us | |
9476 | ||
9477 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
9478 | ||
9479 | -- Normally the type conversion gives our target type. The exception | |
9480 | -- occurs in the case of the Round attribute, where the conversion | |
9481 | -- will be to universal real, and our real type comes from the Round | |
9482 | -- attribute (as well as an indication that we must round the result) | |
9483 | ||
9484 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
9485 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
9486 | then | |
9487 | Set_Etype (N, Etype (Parent (Conv))); | |
9488 | Set_Rounded_Result (N); | |
9489 | ||
9490 | -- Normal case where type comes from conversion above us | |
9491 | ||
9492 | else | |
9493 | Set_Etype (N, Etype (Conv)); | |
9494 | end if; | |
9495 | end Fixup_Universal_Fixed_Operation; | |
9496 | ||
fbf5a39b AC |
9497 | ------------------------------ |
9498 | -- Get_Allocator_Final_List -- | |
9499 | ------------------------------ | |
9500 | ||
9501 | function Get_Allocator_Final_List | |
9502 | (N : Node_Id; | |
9503 | T : Entity_Id; | |
2e071734 | 9504 | PtrT : Entity_Id) return Entity_Id |
fbf5a39b AC |
9505 | is |
9506 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 9507 | |
0da2c8ac | 9508 | Owner : Entity_Id := PtrT; |
26bff3d9 | 9509 | -- The entity whose finalization list must be used to attach the |
0da2c8ac | 9510 | -- allocated object. |
fbf5a39b | 9511 | |
0da2c8ac | 9512 | begin |
fbf5a39b | 9513 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
26bff3d9 JM |
9514 | |
9515 | -- If the context is an access parameter, we need to create a | |
9516 | -- non-anonymous access type in order to have a usable final list, | |
9517 | -- because there is otherwise no pool to which the allocated object | |
9518 | -- can belong. We create both the type and the finalization chain | |
9519 | -- here, because freezing an internal type does not create such a | |
9520 | -- chain. The Final_Chain that is thus created is shared by the | |
9521 | -- access parameter. The access type is tested against the result | |
9522 | -- type of the function to exclude allocators whose type is an | |
8654a240 | 9523 | -- anonymous access result type. We freeze the type at once to |
9450205a ES |
9524 | -- ensure that it is properly decorated for the back-end, even |
9525 | -- if the context and current scope is a loop. | |
26bff3d9 | 9526 | |
0da2c8ac AC |
9527 | if Nkind (Associated_Node_For_Itype (PtrT)) |
9528 | in N_Subprogram_Specification | |
26bff3d9 JM |
9529 | and then |
9530 | PtrT /= | |
9531 | Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT))) | |
0da2c8ac | 9532 | then |
191fcb3a | 9533 | Owner := Make_Temporary (Loc, 'J'); |
0da2c8ac AC |
9534 | Insert_Action (N, |
9535 | Make_Full_Type_Declaration (Loc, | |
9536 | Defining_Identifier => Owner, | |
9537 | Type_Definition => | |
9538 | Make_Access_To_Object_Definition (Loc, | |
9539 | Subtype_Indication => | |
9540 | New_Occurrence_Of (T, Loc)))); | |
fbf5a39b | 9541 | |
9450205a | 9542 | Freeze_Before (N, Owner); |
0da2c8ac AC |
9543 | Build_Final_List (N, Owner); |
9544 | Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner)); | |
fbf5a39b | 9545 | |
26bff3d9 JM |
9546 | -- Ada 2005 (AI-318-02): If the context is a return object |
9547 | -- declaration, then the anonymous return subtype is defined to have | |
9548 | -- the same accessibility level as that of the function's result | |
9549 | -- subtype, which means that we want the scope where the function is | |
9550 | -- declared. | |
9551 | ||
9552 | elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration | |
9553 | and then Ekind (Scope (PtrT)) = E_Return_Statement | |
9554 | then | |
9555 | Owner := Scope (Return_Applies_To (Scope (PtrT))); | |
9556 | ||
e7e4d230 | 9557 | -- Case of an access discriminant, or (Ada 2005) of an anonymous |
26bff3d9 | 9558 | -- access component or anonymous access function result: find the |
d766cee3 RD |
9559 | -- final list associated with the scope of the type. (In the |
9560 | -- anonymous access component kind, a list controller will have | |
9561 | -- been allocated when freezing the record type, and PtrT has an | |
9562 | -- Associated_Final_Chain attribute designating it.) | |
0da2c8ac | 9563 | |
d766cee3 | 9564 | elsif No (Associated_Final_Chain (PtrT)) then |
0da2c8ac AC |
9565 | Owner := Scope (PtrT); |
9566 | end if; | |
fbf5a39b | 9567 | end if; |
0da2c8ac AC |
9568 | |
9569 | return Find_Final_List (Owner); | |
fbf5a39b AC |
9570 | end Get_Allocator_Final_List; |
9571 | ||
5d09245e AC |
9572 | --------------------------------- |
9573 | -- Has_Inferable_Discriminants -- | |
9574 | --------------------------------- | |
9575 | ||
9576 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
9577 | ||
9578 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
9579 | -- Determines whether the left-most prefix of a selected component is a | |
9580 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
9581 | ||
9582 | -------------------------------- | |
9583 | -- Prefix_Is_Formal_Parameter -- | |
9584 | -------------------------------- | |
9585 | ||
9586 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
9587 | Sel_Comp : Node_Id := N; | |
9588 | ||
9589 | begin | |
9590 | -- Move to the left-most prefix by climbing up the tree | |
9591 | ||
9592 | while Present (Parent (Sel_Comp)) | |
9593 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
9594 | loop | |
9595 | Sel_Comp := Parent (Sel_Comp); | |
9596 | end loop; | |
9597 | ||
9598 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
9599 | end Prefix_Is_Formal_Parameter; | |
9600 | ||
9601 | -- Start of processing for Has_Inferable_Discriminants | |
9602 | ||
9603 | begin | |
8fc789c8 | 9604 | -- For identifiers and indexed components, it is sufficient to have a |
5d09245e AC |
9605 | -- constrained Unchecked_Union nominal subtype. |
9606 | ||
303b4d58 | 9607 | if Nkind_In (N, N_Identifier, N_Indexed_Component) then |
5d09245e AC |
9608 | return Is_Unchecked_Union (Base_Type (Etype (N))) |
9609 | and then | |
9610 | Is_Constrained (Etype (N)); | |
9611 | ||
9612 | -- For selected components, the subtype of the selector must be a | |
9613 | -- constrained Unchecked_Union. If the component is subject to a | |
9614 | -- per-object constraint, then the enclosing object must have inferable | |
9615 | -- discriminants. | |
9616 | ||
9617 | elsif Nkind (N) = N_Selected_Component then | |
9618 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then | |
9619 | ||
9620 | -- A small hack. If we have a per-object constrained selected | |
9621 | -- component of a formal parameter, return True since we do not | |
9622 | -- know the actual parameter association yet. | |
9623 | ||
9624 | if Prefix_Is_Formal_Parameter (N) then | |
9625 | return True; | |
9626 | end if; | |
9627 | ||
9628 | -- Otherwise, check the enclosing object and the selector | |
9629 | ||
9630 | return Has_Inferable_Discriminants (Prefix (N)) | |
9631 | and then | |
9632 | Has_Inferable_Discriminants (Selector_Name (N)); | |
9633 | end if; | |
9634 | ||
9635 | -- The call to Has_Inferable_Discriminants will determine whether | |
9636 | -- the selector has a constrained Unchecked_Union nominal type. | |
9637 | ||
9638 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
9639 | ||
9640 | -- A qualified expression has inferable discriminants if its subtype | |
9641 | -- mark is a constrained Unchecked_Union subtype. | |
9642 | ||
9643 | elsif Nkind (N) = N_Qualified_Expression then | |
9644 | return Is_Unchecked_Union (Subtype_Mark (N)) | |
9645 | and then | |
9646 | Is_Constrained (Subtype_Mark (N)); | |
9647 | ||
9648 | end if; | |
9649 | ||
9650 | return False; | |
9651 | end Has_Inferable_Discriminants; | |
9652 | ||
70482933 RK |
9653 | ------------------------------- |
9654 | -- Insert_Dereference_Action -- | |
9655 | ------------------------------- | |
9656 | ||
9657 | procedure Insert_Dereference_Action (N : Node_Id) is | |
9658 | Loc : constant Source_Ptr := Sloc (N); | |
9659 | Typ : constant Entity_Id := Etype (N); | |
9660 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
0ab80019 | 9661 | Pnod : constant Node_Id := Parent (N); |
70482933 RK |
9662 | |
9663 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; | |
2e071734 AC |
9664 | -- Return true if type of P is derived from Checked_Pool; |
9665 | ||
9666 | ----------------------------- | |
9667 | -- Is_Checked_Storage_Pool -- | |
9668 | ----------------------------- | |
70482933 RK |
9669 | |
9670 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
9671 | T : Entity_Id; | |
9672 | ||
9673 | begin | |
9674 | if No (P) then | |
9675 | return False; | |
9676 | end if; | |
9677 | ||
9678 | T := Etype (P); | |
9679 | while T /= Etype (T) loop | |
9680 | if Is_RTE (T, RE_Checked_Pool) then | |
9681 | return True; | |
9682 | else | |
9683 | T := Etype (T); | |
9684 | end if; | |
9685 | end loop; | |
9686 | ||
9687 | return False; | |
9688 | end Is_Checked_Storage_Pool; | |
9689 | ||
9690 | -- Start of processing for Insert_Dereference_Action | |
9691 | ||
9692 | begin | |
e6f69614 AC |
9693 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
9694 | ||
0ab80019 AC |
9695 | if not (Is_Checked_Storage_Pool (Pool) |
9696 | and then Comes_From_Source (Original_Node (Pnod))) | |
e6f69614 | 9697 | then |
70482933 | 9698 | return; |
70482933 RK |
9699 | end if; |
9700 | ||
9701 | Insert_Action (N, | |
9702 | Make_Procedure_Call_Statement (Loc, | |
9703 | Name => New_Reference_To ( | |
9704 | Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), | |
9705 | ||
9706 | Parameter_Associations => New_List ( | |
9707 | ||
9708 | -- Pool | |
9709 | ||
9710 | New_Reference_To (Pool, Loc), | |
9711 | ||
685094bf RD |
9712 | -- Storage_Address. We use the attribute Pool_Address, which uses |
9713 | -- the pointer itself to find the address of the object, and which | |
9714 | -- handles unconstrained arrays properly by computing the address | |
9715 | -- of the template. i.e. the correct address of the corresponding | |
9716 | -- allocation. | |
70482933 RK |
9717 | |
9718 | Make_Attribute_Reference (Loc, | |
fbf5a39b AC |
9719 | Prefix => Duplicate_Subexpr_Move_Checks (N), |
9720 | Attribute_Name => Name_Pool_Address), | |
70482933 RK |
9721 | |
9722 | -- Size_In_Storage_Elements | |
9723 | ||
9724 | Make_Op_Divide (Loc, | |
9725 | Left_Opnd => | |
9726 | Make_Attribute_Reference (Loc, | |
9727 | Prefix => | |
fbf5a39b AC |
9728 | Make_Explicit_Dereference (Loc, |
9729 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
9730 | Attribute_Name => Name_Size), |
9731 | Right_Opnd => | |
9732 | Make_Integer_Literal (Loc, System_Storage_Unit)), | |
9733 | ||
9734 | -- Alignment | |
9735 | ||
9736 | Make_Attribute_Reference (Loc, | |
9737 | Prefix => | |
fbf5a39b AC |
9738 | Make_Explicit_Dereference (Loc, |
9739 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
9740 | Attribute_Name => Name_Alignment)))); |
9741 | ||
fbf5a39b AC |
9742 | exception |
9743 | when RE_Not_Available => | |
9744 | return; | |
70482933 RK |
9745 | end Insert_Dereference_Action; |
9746 | ||
fdfcc663 AC |
9747 | -------------------------------- |
9748 | -- Integer_Promotion_Possible -- | |
9749 | -------------------------------- | |
9750 | ||
9751 | function Integer_Promotion_Possible (N : Node_Id) return Boolean is | |
9752 | Operand : constant Node_Id := Expression (N); | |
9753 | Operand_Type : constant Entity_Id := Etype (Operand); | |
9754 | Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type); | |
9755 | ||
9756 | begin | |
9757 | pragma Assert (Nkind (N) = N_Type_Conversion); | |
9758 | ||
9759 | return | |
9760 | ||
9761 | -- We only do the transformation for source constructs. We assume | |
9762 | -- that the expander knows what it is doing when it generates code. | |
9763 | ||
9764 | Comes_From_Source (N) | |
9765 | ||
9766 | -- If the operand type is Short_Integer or Short_Short_Integer, | |
9767 | -- then we will promote to Integer, which is available on all | |
9768 | -- targets, and is sufficient to ensure no intermediate overflow. | |
9769 | -- Furthermore it is likely to be as efficient or more efficient | |
9770 | -- than using the smaller type for the computation so we do this | |
9771 | -- unconditionally. | |
9772 | ||
9773 | and then | |
9774 | (Root_Operand_Type = Base_Type (Standard_Short_Integer) | |
9775 | or else | |
9776 | Root_Operand_Type = Base_Type (Standard_Short_Short_Integer)) | |
9777 | ||
9778 | -- Test for interesting operation, which includes addition, | |
5f3f175d AC |
9779 | -- division, exponentiation, multiplication, subtraction, absolute |
9780 | -- value and unary negation. Unary "+" is omitted since it is a | |
9781 | -- no-op and thus can't overflow. | |
fdfcc663 | 9782 | |
5f3f175d AC |
9783 | and then Nkind_In (Operand, N_Op_Abs, |
9784 | N_Op_Add, | |
fdfcc663 AC |
9785 | N_Op_Divide, |
9786 | N_Op_Expon, | |
9787 | N_Op_Minus, | |
9788 | N_Op_Multiply, | |
9789 | N_Op_Subtract); | |
9790 | end Integer_Promotion_Possible; | |
9791 | ||
70482933 RK |
9792 | ------------------------------ |
9793 | -- Make_Array_Comparison_Op -- | |
9794 | ------------------------------ | |
9795 | ||
9796 | -- This is a hand-coded expansion of the following generic function: | |
9797 | ||
9798 | -- generic | |
9799 | -- type elem is (<>); | |
9800 | -- type index is (<>); | |
9801 | -- type a is array (index range <>) of elem; | |
20b5d666 | 9802 | |
70482933 RK |
9803 | -- function Gnnn (X : a; Y: a) return boolean is |
9804 | -- J : index := Y'first; | |
20b5d666 | 9805 | |
70482933 RK |
9806 | -- begin |
9807 | -- if X'length = 0 then | |
9808 | -- return false; | |
20b5d666 | 9809 | |
70482933 RK |
9810 | -- elsif Y'length = 0 then |
9811 | -- return true; | |
20b5d666 | 9812 | |
70482933 RK |
9813 | -- else |
9814 | -- for I in X'range loop | |
9815 | -- if X (I) = Y (J) then | |
9816 | -- if J = Y'last then | |
9817 | -- exit; | |
9818 | -- else | |
9819 | -- J := index'succ (J); | |
9820 | -- end if; | |
20b5d666 | 9821 | |
70482933 RK |
9822 | -- else |
9823 | -- return X (I) > Y (J); | |
9824 | -- end if; | |
9825 | -- end loop; | |
20b5d666 | 9826 | |
70482933 RK |
9827 | -- return X'length > Y'length; |
9828 | -- end if; | |
9829 | -- end Gnnn; | |
9830 | ||
9831 | -- Note that since we are essentially doing this expansion by hand, we | |
9832 | -- do not need to generate an actual or formal generic part, just the | |
9833 | -- instantiated function itself. | |
9834 | ||
9835 | function Make_Array_Comparison_Op | |
2e071734 AC |
9836 | (Typ : Entity_Id; |
9837 | Nod : Node_Id) return Node_Id | |
70482933 RK |
9838 | is |
9839 | Loc : constant Source_Ptr := Sloc (Nod); | |
9840 | ||
9841 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
9842 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
9843 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
9844 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
9845 | ||
9846 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
9847 | ||
9848 | Loop_Statement : Node_Id; | |
9849 | Loop_Body : Node_Id; | |
9850 | If_Stat : Node_Id; | |
9851 | Inner_If : Node_Id; | |
9852 | Final_Expr : Node_Id; | |
9853 | Func_Body : Node_Id; | |
9854 | Func_Name : Entity_Id; | |
9855 | Formals : List_Id; | |
9856 | Length1 : Node_Id; | |
9857 | Length2 : Node_Id; | |
9858 | ||
9859 | begin | |
9860 | -- if J = Y'last then | |
9861 | -- exit; | |
9862 | -- else | |
9863 | -- J := index'succ (J); | |
9864 | -- end if; | |
9865 | ||
9866 | Inner_If := | |
9867 | Make_Implicit_If_Statement (Nod, | |
9868 | Condition => | |
9869 | Make_Op_Eq (Loc, | |
9870 | Left_Opnd => New_Reference_To (J, Loc), | |
9871 | Right_Opnd => | |
9872 | Make_Attribute_Reference (Loc, | |
9873 | Prefix => New_Reference_To (Y, Loc), | |
9874 | Attribute_Name => Name_Last)), | |
9875 | ||
9876 | Then_Statements => New_List ( | |
9877 | Make_Exit_Statement (Loc)), | |
9878 | ||
9879 | Else_Statements => | |
9880 | New_List ( | |
9881 | Make_Assignment_Statement (Loc, | |
9882 | Name => New_Reference_To (J, Loc), | |
9883 | Expression => | |
9884 | Make_Attribute_Reference (Loc, | |
9885 | Prefix => New_Reference_To (Index, Loc), | |
9886 | Attribute_Name => Name_Succ, | |
9887 | Expressions => New_List (New_Reference_To (J, Loc)))))); | |
9888 | ||
9889 | -- if X (I) = Y (J) then | |
9890 | -- if ... end if; | |
9891 | -- else | |
9892 | -- return X (I) > Y (J); | |
9893 | -- end if; | |
9894 | ||
9895 | Loop_Body := | |
9896 | Make_Implicit_If_Statement (Nod, | |
9897 | Condition => | |
9898 | Make_Op_Eq (Loc, | |
9899 | Left_Opnd => | |
9900 | Make_Indexed_Component (Loc, | |
9901 | Prefix => New_Reference_To (X, Loc), | |
9902 | Expressions => New_List (New_Reference_To (I, Loc))), | |
9903 | ||
9904 | Right_Opnd => | |
9905 | Make_Indexed_Component (Loc, | |
9906 | Prefix => New_Reference_To (Y, Loc), | |
9907 | Expressions => New_List (New_Reference_To (J, Loc)))), | |
9908 | ||
9909 | Then_Statements => New_List (Inner_If), | |
9910 | ||
9911 | Else_Statements => New_List ( | |
d766cee3 | 9912 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9913 | Expression => |
9914 | Make_Op_Gt (Loc, | |
9915 | Left_Opnd => | |
9916 | Make_Indexed_Component (Loc, | |
9917 | Prefix => New_Reference_To (X, Loc), | |
9918 | Expressions => New_List (New_Reference_To (I, Loc))), | |
9919 | ||
9920 | Right_Opnd => | |
9921 | Make_Indexed_Component (Loc, | |
9922 | Prefix => New_Reference_To (Y, Loc), | |
9923 | Expressions => New_List ( | |
9924 | New_Reference_To (J, Loc))))))); | |
9925 | ||
9926 | -- for I in X'range loop | |
9927 | -- if ... end if; | |
9928 | -- end loop; | |
9929 | ||
9930 | Loop_Statement := | |
9931 | Make_Implicit_Loop_Statement (Nod, | |
9932 | Identifier => Empty, | |
9933 | ||
9934 | Iteration_Scheme => | |
9935 | Make_Iteration_Scheme (Loc, | |
9936 | Loop_Parameter_Specification => | |
9937 | Make_Loop_Parameter_Specification (Loc, | |
9938 | Defining_Identifier => I, | |
9939 | Discrete_Subtype_Definition => | |
9940 | Make_Attribute_Reference (Loc, | |
9941 | Prefix => New_Reference_To (X, Loc), | |
9942 | Attribute_Name => Name_Range))), | |
9943 | ||
9944 | Statements => New_List (Loop_Body)); | |
9945 | ||
9946 | -- if X'length = 0 then | |
9947 | -- return false; | |
9948 | -- elsif Y'length = 0 then | |
9949 | -- return true; | |
9950 | -- else | |
9951 | -- for ... loop ... end loop; | |
9952 | -- return X'length > Y'length; | |
9953 | -- end if; | |
9954 | ||
9955 | Length1 := | |
9956 | Make_Attribute_Reference (Loc, | |
9957 | Prefix => New_Reference_To (X, Loc), | |
9958 | Attribute_Name => Name_Length); | |
9959 | ||
9960 | Length2 := | |
9961 | Make_Attribute_Reference (Loc, | |
9962 | Prefix => New_Reference_To (Y, Loc), | |
9963 | Attribute_Name => Name_Length); | |
9964 | ||
9965 | Final_Expr := | |
9966 | Make_Op_Gt (Loc, | |
9967 | Left_Opnd => Length1, | |
9968 | Right_Opnd => Length2); | |
9969 | ||
9970 | If_Stat := | |
9971 | Make_Implicit_If_Statement (Nod, | |
9972 | Condition => | |
9973 | Make_Op_Eq (Loc, | |
9974 | Left_Opnd => | |
9975 | Make_Attribute_Reference (Loc, | |
9976 | Prefix => New_Reference_To (X, Loc), | |
9977 | Attribute_Name => Name_Length), | |
9978 | Right_Opnd => | |
9979 | Make_Integer_Literal (Loc, 0)), | |
9980 | ||
9981 | Then_Statements => | |
9982 | New_List ( | |
d766cee3 | 9983 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9984 | Expression => New_Reference_To (Standard_False, Loc))), |
9985 | ||
9986 | Elsif_Parts => New_List ( | |
9987 | Make_Elsif_Part (Loc, | |
9988 | Condition => | |
9989 | Make_Op_Eq (Loc, | |
9990 | Left_Opnd => | |
9991 | Make_Attribute_Reference (Loc, | |
9992 | Prefix => New_Reference_To (Y, Loc), | |
9993 | Attribute_Name => Name_Length), | |
9994 | Right_Opnd => | |
9995 | Make_Integer_Literal (Loc, 0)), | |
9996 | ||
9997 | Then_Statements => | |
9998 | New_List ( | |
d766cee3 | 9999 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10000 | Expression => New_Reference_To (Standard_True, Loc))))), |
10001 | ||
10002 | Else_Statements => New_List ( | |
10003 | Loop_Statement, | |
d766cee3 | 10004 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10005 | Expression => Final_Expr))); |
10006 | ||
10007 | -- (X : a; Y: a) | |
10008 | ||
10009 | Formals := New_List ( | |
10010 | Make_Parameter_Specification (Loc, | |
10011 | Defining_Identifier => X, | |
10012 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
10013 | ||
10014 | Make_Parameter_Specification (Loc, | |
10015 | Defining_Identifier => Y, | |
10016 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
10017 | ||
10018 | -- function Gnnn (...) return boolean is | |
10019 | -- J : index := Y'first; | |
10020 | -- begin | |
10021 | -- if ... end if; | |
10022 | -- end Gnnn; | |
10023 | ||
191fcb3a | 10024 | Func_Name := Make_Temporary (Loc, 'G'); |
70482933 RK |
10025 | |
10026 | Func_Body := | |
10027 | Make_Subprogram_Body (Loc, | |
10028 | Specification => | |
10029 | Make_Function_Specification (Loc, | |
10030 | Defining_Unit_Name => Func_Name, | |
10031 | Parameter_Specifications => Formals, | |
630d30e9 | 10032 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
70482933 RK |
10033 | |
10034 | Declarations => New_List ( | |
10035 | Make_Object_Declaration (Loc, | |
10036 | Defining_Identifier => J, | |
10037 | Object_Definition => New_Reference_To (Index, Loc), | |
10038 | Expression => | |
10039 | Make_Attribute_Reference (Loc, | |
10040 | Prefix => New_Reference_To (Y, Loc), | |
10041 | Attribute_Name => Name_First))), | |
10042 | ||
10043 | Handled_Statement_Sequence => | |
10044 | Make_Handled_Sequence_Of_Statements (Loc, | |
10045 | Statements => New_List (If_Stat))); | |
10046 | ||
10047 | return Func_Body; | |
70482933 RK |
10048 | end Make_Array_Comparison_Op; |
10049 | ||
10050 | --------------------------- | |
10051 | -- Make_Boolean_Array_Op -- | |
10052 | --------------------------- | |
10053 | ||
685094bf RD |
10054 | -- For logical operations on boolean arrays, expand in line the following, |
10055 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
10056 | |
10057 | -- function Annn (A : typ; B: typ) return typ is | |
10058 | -- C : typ; | |
10059 | -- begin | |
10060 | -- for J in A'range loop | |
10061 | -- C (J) := A (J) op B (J); | |
10062 | -- end loop; | |
10063 | -- return C; | |
10064 | -- end Annn; | |
10065 | ||
10066 | -- Here typ is the boolean array type | |
10067 | ||
10068 | function Make_Boolean_Array_Op | |
2e071734 AC |
10069 | (Typ : Entity_Id; |
10070 | N : Node_Id) return Node_Id | |
70482933 RK |
10071 | is |
10072 | Loc : constant Source_Ptr := Sloc (N); | |
10073 | ||
10074 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
10075 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
10076 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
10077 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
10078 | ||
10079 | A_J : Node_Id; | |
10080 | B_J : Node_Id; | |
10081 | C_J : Node_Id; | |
10082 | Op : Node_Id; | |
10083 | ||
10084 | Formals : List_Id; | |
10085 | Func_Name : Entity_Id; | |
10086 | Func_Body : Node_Id; | |
10087 | Loop_Statement : Node_Id; | |
10088 | ||
10089 | begin | |
10090 | A_J := | |
10091 | Make_Indexed_Component (Loc, | |
10092 | Prefix => New_Reference_To (A, Loc), | |
10093 | Expressions => New_List (New_Reference_To (J, Loc))); | |
10094 | ||
10095 | B_J := | |
10096 | Make_Indexed_Component (Loc, | |
10097 | Prefix => New_Reference_To (B, Loc), | |
10098 | Expressions => New_List (New_Reference_To (J, Loc))); | |
10099 | ||
10100 | C_J := | |
10101 | Make_Indexed_Component (Loc, | |
10102 | Prefix => New_Reference_To (C, Loc), | |
10103 | Expressions => New_List (New_Reference_To (J, Loc))); | |
10104 | ||
10105 | if Nkind (N) = N_Op_And then | |
10106 | Op := | |
10107 | Make_Op_And (Loc, | |
10108 | Left_Opnd => A_J, | |
10109 | Right_Opnd => B_J); | |
10110 | ||
10111 | elsif Nkind (N) = N_Op_Or then | |
10112 | Op := | |
10113 | Make_Op_Or (Loc, | |
10114 | Left_Opnd => A_J, | |
10115 | Right_Opnd => B_J); | |
10116 | ||
10117 | else | |
10118 | Op := | |
10119 | Make_Op_Xor (Loc, | |
10120 | Left_Opnd => A_J, | |
10121 | Right_Opnd => B_J); | |
10122 | end if; | |
10123 | ||
10124 | Loop_Statement := | |
10125 | Make_Implicit_Loop_Statement (N, | |
10126 | Identifier => Empty, | |
10127 | ||
10128 | Iteration_Scheme => | |
10129 | Make_Iteration_Scheme (Loc, | |
10130 | Loop_Parameter_Specification => | |
10131 | Make_Loop_Parameter_Specification (Loc, | |
10132 | Defining_Identifier => J, | |
10133 | Discrete_Subtype_Definition => | |
10134 | Make_Attribute_Reference (Loc, | |
10135 | Prefix => New_Reference_To (A, Loc), | |
10136 | Attribute_Name => Name_Range))), | |
10137 | ||
10138 | Statements => New_List ( | |
10139 | Make_Assignment_Statement (Loc, | |
10140 | Name => C_J, | |
10141 | Expression => Op))); | |
10142 | ||
10143 | Formals := New_List ( | |
10144 | Make_Parameter_Specification (Loc, | |
10145 | Defining_Identifier => A, | |
10146 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
10147 | ||
10148 | Make_Parameter_Specification (Loc, | |
10149 | Defining_Identifier => B, | |
10150 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
10151 | ||
191fcb3a | 10152 | Func_Name := Make_Temporary (Loc, 'A'); |
70482933 RK |
10153 | Set_Is_Inlined (Func_Name); |
10154 | ||
10155 | Func_Body := | |
10156 | Make_Subprogram_Body (Loc, | |
10157 | Specification => | |
10158 | Make_Function_Specification (Loc, | |
10159 | Defining_Unit_Name => Func_Name, | |
10160 | Parameter_Specifications => Formals, | |
630d30e9 | 10161 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
10162 | |
10163 | Declarations => New_List ( | |
10164 | Make_Object_Declaration (Loc, | |
10165 | Defining_Identifier => C, | |
10166 | Object_Definition => New_Reference_To (Typ, Loc))), | |
10167 | ||
10168 | Handled_Statement_Sequence => | |
10169 | Make_Handled_Sequence_Of_Statements (Loc, | |
10170 | Statements => New_List ( | |
10171 | Loop_Statement, | |
d766cee3 | 10172 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
10173 | Expression => New_Reference_To (C, Loc))))); |
10174 | ||
10175 | return Func_Body; | |
10176 | end Make_Boolean_Array_Op; | |
10177 | ||
0580d807 AC |
10178 | -------------------------------- |
10179 | -- Optimize_Length_Comparison -- | |
10180 | -------------------------------- | |
10181 | ||
10182 | procedure Optimize_Length_Comparison (N : Node_Id) is | |
10183 | Loc : constant Source_Ptr := Sloc (N); | |
10184 | Typ : constant Entity_Id := Etype (N); | |
10185 | Result : Node_Id; | |
10186 | ||
10187 | Left : Node_Id; | |
10188 | Right : Node_Id; | |
10189 | -- First and Last attribute reference nodes, which end up as left and | |
10190 | -- right operands of the optimized result. | |
10191 | ||
10192 | Is_Zero : Boolean; | |
10193 | -- True for comparison operand of zero | |
10194 | ||
10195 | Comp : Node_Id; | |
10196 | -- Comparison operand, set only if Is_Zero is false | |
10197 | ||
10198 | Ent : Entity_Id; | |
10199 | -- Entity whose length is being compared | |
10200 | ||
10201 | Index : Node_Id; | |
10202 | -- Integer_Literal node for length attribute expression, or Empty | |
10203 | -- if there is no such expression present. | |
10204 | ||
10205 | Ityp : Entity_Id; | |
10206 | -- Type of array index to which 'Length is applied | |
10207 | ||
10208 | Op : Node_Kind := Nkind (N); | |
10209 | -- Kind of comparison operator, gets flipped if operands backwards | |
10210 | ||
10211 | function Is_Optimizable (N : Node_Id) return Boolean; | |
abcd9db2 AC |
10212 | -- Tests N to see if it is an optimizable comparison value (defined as |
10213 | -- constant zero or one, or something else where the value is known to | |
10214 | -- be positive and in the range of 32-bits, and where the corresponding | |
10215 | -- Length value is also known to be 32-bits. If result is true, sets | |
10216 | -- Is_Zero, Ityp, and Comp accordingly. | |
0580d807 AC |
10217 | |
10218 | function Is_Entity_Length (N : Node_Id) return Boolean; | |
10219 | -- Tests if N is a length attribute applied to a simple entity. If so, | |
10220 | -- returns True, and sets Ent to the entity, and Index to the integer | |
10221 | -- literal provided as an attribute expression, or to Empty if none. | |
10222 | -- Also returns True if the expression is a generated type conversion | |
10223 | -- whose expression is of the desired form. This latter case arises | |
10224 | -- when Apply_Universal_Integer_Attribute_Check installs a conversion | |
10225 | -- to check for being in range, which is not needed in this context. | |
10226 | -- Returns False if neither condition holds. | |
10227 | ||
10228 | function Prepare_64 (N : Node_Id) return Node_Id; | |
10229 | -- Given a discrete expression, returns a Long_Long_Integer typed | |
10230 | -- expression representing the underlying value of the expression. | |
10231 | -- This is done with an unchecked conversion to the result type. We | |
10232 | -- use unchecked conversion to handle the enumeration type case. | |
10233 | ||
10234 | ---------------------- | |
10235 | -- Is_Entity_Length -- | |
10236 | ---------------------- | |
10237 | ||
10238 | function Is_Entity_Length (N : Node_Id) return Boolean is | |
10239 | begin | |
10240 | if Nkind (N) = N_Attribute_Reference | |
10241 | and then Attribute_Name (N) = Name_Length | |
10242 | and then Is_Entity_Name (Prefix (N)) | |
10243 | then | |
10244 | Ent := Entity (Prefix (N)); | |
10245 | ||
10246 | if Present (Expressions (N)) then | |
10247 | Index := First (Expressions (N)); | |
10248 | else | |
10249 | Index := Empty; | |
10250 | end if; | |
10251 | ||
10252 | return True; | |
10253 | ||
10254 | elsif Nkind (N) = N_Type_Conversion | |
10255 | and then not Comes_From_Source (N) | |
10256 | then | |
10257 | return Is_Entity_Length (Expression (N)); | |
10258 | ||
10259 | else | |
10260 | return False; | |
10261 | end if; | |
10262 | end Is_Entity_Length; | |
10263 | ||
10264 | -------------------- | |
10265 | -- Is_Optimizable -- | |
10266 | -------------------- | |
10267 | ||
10268 | function Is_Optimizable (N : Node_Id) return Boolean is | |
10269 | Val : Uint; | |
10270 | OK : Boolean; | |
10271 | Lo : Uint; | |
10272 | Hi : Uint; | |
10273 | Indx : Node_Id; | |
10274 | ||
10275 | begin | |
10276 | if Compile_Time_Known_Value (N) then | |
10277 | Val := Expr_Value (N); | |
10278 | ||
10279 | if Val = Uint_0 then | |
10280 | Is_Zero := True; | |
10281 | Comp := Empty; | |
10282 | return True; | |
10283 | ||
10284 | elsif Val = Uint_1 then | |
10285 | Is_Zero := False; | |
10286 | Comp := Empty; | |
10287 | return True; | |
10288 | end if; | |
10289 | end if; | |
10290 | ||
10291 | -- Here we have to make sure of being within 32-bits | |
10292 | ||
10293 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); | |
10294 | ||
10295 | if not OK | |
abcd9db2 | 10296 | or else Lo < Uint_1 |
0580d807 AC |
10297 | or else Hi > UI_From_Int (Int'Last) |
10298 | then | |
10299 | return False; | |
10300 | end if; | |
10301 | ||
abcd9db2 AC |
10302 | -- Comparison value was within range, so now we must check the index |
10303 | -- value to make sure it is also within 32-bits. | |
0580d807 AC |
10304 | |
10305 | Indx := First_Index (Etype (Ent)); | |
10306 | ||
10307 | if Present (Index) then | |
10308 | for J in 2 .. UI_To_Int (Intval (Index)) loop | |
10309 | Next_Index (Indx); | |
10310 | end loop; | |
10311 | end if; | |
10312 | ||
10313 | Ityp := Etype (Indx); | |
10314 | ||
10315 | if Esize (Ityp) > 32 then | |
10316 | return False; | |
10317 | end if; | |
10318 | ||
10319 | Is_Zero := False; | |
10320 | Comp := N; | |
10321 | return True; | |
10322 | end Is_Optimizable; | |
10323 | ||
10324 | ---------------- | |
10325 | -- Prepare_64 -- | |
10326 | ---------------- | |
10327 | ||
10328 | function Prepare_64 (N : Node_Id) return Node_Id is | |
10329 | begin | |
10330 | return Unchecked_Convert_To (Standard_Long_Long_Integer, N); | |
10331 | end Prepare_64; | |
10332 | ||
10333 | -- Start of processing for Optimize_Length_Comparison | |
10334 | ||
10335 | begin | |
10336 | -- Nothing to do if not a comparison | |
10337 | ||
10338 | if Op not in N_Op_Compare then | |
10339 | return; | |
10340 | end if; | |
10341 | ||
10342 | -- Nothing to do if special -gnatd.P debug flag set | |
10343 | ||
10344 | if Debug_Flag_Dot_PP then | |
10345 | return; | |
10346 | end if; | |
10347 | ||
10348 | -- Ent'Length op 0/1 | |
10349 | ||
10350 | if Is_Entity_Length (Left_Opnd (N)) | |
10351 | and then Is_Optimizable (Right_Opnd (N)) | |
10352 | then | |
10353 | null; | |
10354 | ||
10355 | -- 0/1 op Ent'Length | |
10356 | ||
10357 | elsif Is_Entity_Length (Right_Opnd (N)) | |
10358 | and then Is_Optimizable (Left_Opnd (N)) | |
10359 | then | |
10360 | -- Flip comparison to opposite sense | |
10361 | ||
10362 | case Op is | |
10363 | when N_Op_Lt => Op := N_Op_Gt; | |
10364 | when N_Op_Le => Op := N_Op_Ge; | |
10365 | when N_Op_Gt => Op := N_Op_Lt; | |
10366 | when N_Op_Ge => Op := N_Op_Le; | |
10367 | when others => null; | |
10368 | end case; | |
10369 | ||
10370 | -- Else optimization not possible | |
10371 | ||
10372 | else | |
10373 | return; | |
10374 | end if; | |
10375 | ||
10376 | -- Fall through if we will do the optimization | |
10377 | ||
10378 | -- Cases to handle: | |
10379 | ||
10380 | -- X'Length = 0 => X'First > X'Last | |
10381 | -- X'Length = 1 => X'First = X'Last | |
10382 | -- X'Length = n => X'First + (n - 1) = X'Last | |
10383 | ||
10384 | -- X'Length /= 0 => X'First <= X'Last | |
10385 | -- X'Length /= 1 => X'First /= X'Last | |
10386 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
10387 | ||
10388 | -- X'Length >= 0 => always true, warn | |
10389 | -- X'Length >= 1 => X'First <= X'Last | |
10390 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
10391 | ||
10392 | -- X'Length > 0 => X'First <= X'Last | |
10393 | -- X'Length > 1 => X'First < X'Last | |
10394 | -- X'Length > n => X'First + (n - 1) < X'Last | |
10395 | ||
10396 | -- X'Length <= 0 => X'First > X'Last (warn, could be =) | |
10397 | -- X'Length <= 1 => X'First >= X'Last | |
10398 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
10399 | ||
10400 | -- X'Length < 0 => always false (warn) | |
10401 | -- X'Length < 1 => X'First > X'Last | |
10402 | -- X'Length < n => X'First + (n - 1) > X'Last | |
10403 | ||
10404 | -- Note: for the cases of n (not constant 0,1), we require that the | |
10405 | -- corresponding index type be integer or shorter (i.e. not 64-bit), | |
10406 | -- and the same for the comparison value. Then we do the comparison | |
10407 | -- using 64-bit arithmetic (actually long long integer), so that we | |
10408 | -- cannot have overflow intefering with the result. | |
10409 | ||
10410 | -- First deal with warning cases | |
10411 | ||
10412 | if Is_Zero then | |
10413 | case Op is | |
10414 | ||
10415 | -- X'Length >= 0 | |
10416 | ||
10417 | when N_Op_Ge => | |
10418 | Rewrite (N, | |
10419 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc))); | |
10420 | Analyze_And_Resolve (N, Typ); | |
10421 | Warn_On_Known_Condition (N); | |
10422 | return; | |
10423 | ||
10424 | -- X'Length < 0 | |
10425 | ||
10426 | when N_Op_Lt => | |
10427 | Rewrite (N, | |
10428 | Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc))); | |
10429 | Analyze_And_Resolve (N, Typ); | |
10430 | Warn_On_Known_Condition (N); | |
10431 | return; | |
10432 | ||
10433 | when N_Op_Le => | |
10434 | if Constant_Condition_Warnings | |
10435 | and then Comes_From_Source (Original_Node (N)) | |
10436 | then | |
10437 | Error_Msg_N ("could replace by ""'=""?", N); | |
10438 | end if; | |
10439 | ||
10440 | Op := N_Op_Eq; | |
10441 | ||
10442 | when others => | |
10443 | null; | |
10444 | end case; | |
10445 | end if; | |
10446 | ||
10447 | -- Build the First reference we will use | |
10448 | ||
10449 | Left := | |
10450 | Make_Attribute_Reference (Loc, | |
10451 | Prefix => New_Occurrence_Of (Ent, Loc), | |
10452 | Attribute_Name => Name_First); | |
10453 | ||
10454 | if Present (Index) then | |
10455 | Set_Expressions (Left, New_List (New_Copy (Index))); | |
10456 | end if; | |
10457 | ||
10458 | -- If general value case, then do the addition of (n - 1), and | |
10459 | -- also add the needed conversions to type Long_Long_Integer. | |
10460 | ||
10461 | if Present (Comp) then | |
10462 | Left := | |
10463 | Make_Op_Add (Loc, | |
10464 | Left_Opnd => Prepare_64 (Left), | |
10465 | Right_Opnd => | |
10466 | Make_Op_Subtract (Loc, | |
10467 | Left_Opnd => Prepare_64 (Comp), | |
10468 | Right_Opnd => Make_Integer_Literal (Loc, 1))); | |
10469 | end if; | |
10470 | ||
10471 | -- Build the Last reference we will use | |
10472 | ||
10473 | Right := | |
10474 | Make_Attribute_Reference (Loc, | |
10475 | Prefix => New_Occurrence_Of (Ent, Loc), | |
10476 | Attribute_Name => Name_Last); | |
10477 | ||
10478 | if Present (Index) then | |
10479 | Set_Expressions (Right, New_List (New_Copy (Index))); | |
10480 | end if; | |
10481 | ||
10482 | -- If general operand, convert Last reference to Long_Long_Integer | |
10483 | ||
10484 | if Present (Comp) then | |
10485 | Right := Prepare_64 (Right); | |
10486 | end if; | |
10487 | ||
10488 | -- Check for cases to optimize | |
10489 | ||
10490 | -- X'Length = 0 => X'First > X'Last | |
10491 | -- X'Length < 1 => X'First > X'Last | |
10492 | -- X'Length < n => X'First + (n - 1) > X'Last | |
10493 | ||
10494 | if (Is_Zero and then Op = N_Op_Eq) | |
10495 | or else (not Is_Zero and then Op = N_Op_Lt) | |
10496 | then | |
10497 | Result := | |
10498 | Make_Op_Gt (Loc, | |
10499 | Left_Opnd => Left, | |
10500 | Right_Opnd => Right); | |
10501 | ||
10502 | -- X'Length = 1 => X'First = X'Last | |
10503 | -- X'Length = n => X'First + (n - 1) = X'Last | |
10504 | ||
10505 | elsif not Is_Zero and then Op = N_Op_Eq then | |
10506 | Result := | |
10507 | Make_Op_Eq (Loc, | |
10508 | Left_Opnd => Left, | |
10509 | Right_Opnd => Right); | |
10510 | ||
10511 | -- X'Length /= 0 => X'First <= X'Last | |
10512 | -- X'Length > 0 => X'First <= X'Last | |
10513 | ||
10514 | elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then | |
10515 | Result := | |
10516 | Make_Op_Le (Loc, | |
10517 | Left_Opnd => Left, | |
10518 | Right_Opnd => Right); | |
10519 | ||
10520 | -- X'Length /= 1 => X'First /= X'Last | |
10521 | -- X'Length /= n => X'First + (n - 1) /= X'Last | |
10522 | ||
10523 | elsif not Is_Zero and then Op = N_Op_Ne then | |
10524 | Result := | |
10525 | Make_Op_Ne (Loc, | |
10526 | Left_Opnd => Left, | |
10527 | Right_Opnd => Right); | |
10528 | ||
10529 | -- X'Length >= 1 => X'First <= X'Last | |
10530 | -- X'Length >= n => X'First + (n - 1) <= X'Last | |
10531 | ||
10532 | elsif not Is_Zero and then Op = N_Op_Ge then | |
10533 | Result := | |
10534 | Make_Op_Le (Loc, | |
10535 | Left_Opnd => Left, | |
10536 | Right_Opnd => Right); | |
10537 | ||
10538 | -- X'Length > 1 => X'First < X'Last | |
10539 | -- X'Length > n => X'First + (n = 1) < X'Last | |
10540 | ||
10541 | elsif not Is_Zero and then Op = N_Op_Gt then | |
10542 | Result := | |
10543 | Make_Op_Lt (Loc, | |
10544 | Left_Opnd => Left, | |
10545 | Right_Opnd => Right); | |
10546 | ||
10547 | -- X'Length <= 1 => X'First >= X'Last | |
10548 | -- X'Length <= n => X'First + (n - 1) >= X'Last | |
10549 | ||
10550 | elsif not Is_Zero and then Op = N_Op_Le then | |
10551 | Result := | |
10552 | Make_Op_Ge (Loc, | |
10553 | Left_Opnd => Left, | |
10554 | Right_Opnd => Right); | |
10555 | ||
10556 | -- Should not happen at this stage | |
10557 | ||
10558 | else | |
10559 | raise Program_Error; | |
10560 | end if; | |
10561 | ||
10562 | -- Rewrite and finish up | |
10563 | ||
10564 | Rewrite (N, Result); | |
10565 | Analyze_And_Resolve (N, Typ); | |
10566 | return; | |
10567 | end Optimize_Length_Comparison; | |
10568 | ||
70482933 RK |
10569 | ------------------------ |
10570 | -- Rewrite_Comparison -- | |
10571 | ------------------------ | |
10572 | ||
10573 | procedure Rewrite_Comparison (N : Node_Id) is | |
c800f862 RD |
10574 | Warning_Generated : Boolean := False; |
10575 | -- Set to True if first pass with Assume_Valid generates a warning in | |
10576 | -- which case we skip the second pass to avoid warning overloaded. | |
10577 | ||
10578 | Result : Node_Id; | |
10579 | -- Set to Standard_True or Standard_False | |
10580 | ||
d26dc4b5 AC |
10581 | begin |
10582 | if Nkind (N) = N_Type_Conversion then | |
10583 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 10584 | return; |
70482933 | 10585 | |
d26dc4b5 | 10586 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
10587 | return; |
10588 | end if; | |
70482933 | 10589 | |
c800f862 RD |
10590 | -- Now start looking at the comparison in detail. We potentially go |
10591 | -- through this loop twice. The first time, Assume_Valid is set False | |
10592 | -- in the call to Compile_Time_Compare. If this call results in a | |
10593 | -- clear result of always True or Always False, that's decisive and | |
10594 | -- we are done. Otherwise we repeat the processing with Assume_Valid | |
e7e4d230 | 10595 | -- set to True to generate additional warnings. We can skip that step |
c800f862 RD |
10596 | -- if Constant_Condition_Warnings is False. |
10597 | ||
10598 | for AV in False .. True loop | |
10599 | declare | |
10600 | Typ : constant Entity_Id := Etype (N); | |
10601 | Op1 : constant Node_Id := Left_Opnd (N); | |
10602 | Op2 : constant Node_Id := Right_Opnd (N); | |
70482933 | 10603 | |
c800f862 RD |
10604 | Res : constant Compare_Result := |
10605 | Compile_Time_Compare (Op1, Op2, Assume_Valid => AV); | |
10606 | -- Res indicates if compare outcome can be compile time determined | |
f02b8bb8 | 10607 | |
c800f862 RD |
10608 | True_Result : Boolean; |
10609 | False_Result : Boolean; | |
f02b8bb8 | 10610 | |
c800f862 RD |
10611 | begin |
10612 | case N_Op_Compare (Nkind (N)) is | |
d26dc4b5 AC |
10613 | when N_Op_Eq => |
10614 | True_Result := Res = EQ; | |
10615 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
10616 | ||
10617 | when N_Op_Ge => | |
10618 | True_Result := Res in Compare_GE; | |
10619 | False_Result := Res = LT; | |
10620 | ||
10621 | if Res = LE | |
10622 | and then Constant_Condition_Warnings | |
10623 | and then Comes_From_Source (Original_Node (N)) | |
10624 | and then Nkind (Original_Node (N)) = N_Op_Ge | |
10625 | and then not In_Instance | |
d26dc4b5 | 10626 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 10627 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 10628 | then |
ed2233dc | 10629 | Error_Msg_N |
d26dc4b5 | 10630 | ("can never be greater than, could replace by ""'=""?", N); |
c800f862 | 10631 | Warning_Generated := True; |
d26dc4b5 | 10632 | end if; |
70482933 | 10633 | |
d26dc4b5 AC |
10634 | when N_Op_Gt => |
10635 | True_Result := Res = GT; | |
10636 | False_Result := Res in Compare_LE; | |
10637 | ||
10638 | when N_Op_Lt => | |
10639 | True_Result := Res = LT; | |
10640 | False_Result := Res in Compare_GE; | |
10641 | ||
10642 | when N_Op_Le => | |
10643 | True_Result := Res in Compare_LE; | |
10644 | False_Result := Res = GT; | |
10645 | ||
10646 | if Res = GE | |
10647 | and then Constant_Condition_Warnings | |
10648 | and then Comes_From_Source (Original_Node (N)) | |
10649 | and then Nkind (Original_Node (N)) = N_Op_Le | |
10650 | and then not In_Instance | |
d26dc4b5 | 10651 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 10652 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 10653 | then |
ed2233dc | 10654 | Error_Msg_N |
d26dc4b5 | 10655 | ("can never be less than, could replace by ""'=""?", N); |
c800f862 | 10656 | Warning_Generated := True; |
d26dc4b5 | 10657 | end if; |
70482933 | 10658 | |
d26dc4b5 AC |
10659 | when N_Op_Ne => |
10660 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
10661 | False_Result := Res = EQ; | |
c800f862 | 10662 | end case; |
d26dc4b5 | 10663 | |
c800f862 RD |
10664 | -- If this is the first iteration, then we actually convert the |
10665 | -- comparison into True or False, if the result is certain. | |
d26dc4b5 | 10666 | |
c800f862 RD |
10667 | if AV = False then |
10668 | if True_Result or False_Result then | |
10669 | if True_Result then | |
10670 | Result := Standard_True; | |
10671 | else | |
10672 | Result := Standard_False; | |
10673 | end if; | |
10674 | ||
10675 | Rewrite (N, | |
10676 | Convert_To (Typ, | |
10677 | New_Occurrence_Of (Result, Sloc (N)))); | |
10678 | Analyze_And_Resolve (N, Typ); | |
10679 | Warn_On_Known_Condition (N); | |
10680 | return; | |
10681 | end if; | |
10682 | ||
10683 | -- If this is the second iteration (AV = True), and the original | |
e7e4d230 AC |
10684 | -- node comes from source and we are not in an instance, then give |
10685 | -- a warning if we know result would be True or False. Note: we | |
10686 | -- know Constant_Condition_Warnings is set if we get here. | |
c800f862 RD |
10687 | |
10688 | elsif Comes_From_Source (Original_Node (N)) | |
10689 | and then not In_Instance | |
10690 | then | |
10691 | if True_Result then | |
ed2233dc | 10692 | Error_Msg_N |
c800f862 RD |
10693 | ("condition can only be False if invalid values present?", |
10694 | N); | |
10695 | elsif False_Result then | |
ed2233dc | 10696 | Error_Msg_N |
c800f862 RD |
10697 | ("condition can only be True if invalid values present?", |
10698 | N); | |
10699 | end if; | |
10700 | end if; | |
10701 | end; | |
10702 | ||
10703 | -- Skip second iteration if not warning on constant conditions or | |
e7e4d230 AC |
10704 | -- if the first iteration already generated a warning of some kind or |
10705 | -- if we are in any case assuming all values are valid (so that the | |
10706 | -- first iteration took care of the valid case). | |
c800f862 RD |
10707 | |
10708 | exit when not Constant_Condition_Warnings; | |
10709 | exit when Warning_Generated; | |
10710 | exit when Assume_No_Invalid_Values; | |
10711 | end loop; | |
70482933 RK |
10712 | end Rewrite_Comparison; |
10713 | ||
fbf5a39b AC |
10714 | ---------------------------- |
10715 | -- Safe_In_Place_Array_Op -- | |
10716 | ---------------------------- | |
10717 | ||
10718 | function Safe_In_Place_Array_Op | |
2e071734 AC |
10719 | (Lhs : Node_Id; |
10720 | Op1 : Node_Id; | |
10721 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
10722 | is |
10723 | Target : Entity_Id; | |
10724 | ||
10725 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
10726 | -- Operand is safe if it cannot overlap part of the target of the | |
10727 | -- operation. If the operand and the target are identical, the operand | |
10728 | -- is safe. The operand can be empty in the case of negation. | |
10729 | ||
10730 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 10731 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
10732 | |
10733 | ------------------ | |
10734 | -- Is_Unaliased -- | |
10735 | ------------------ | |
10736 | ||
10737 | function Is_Unaliased (N : Node_Id) return Boolean is | |
10738 | begin | |
10739 | return | |
10740 | Is_Entity_Name (N) | |
10741 | and then No (Address_Clause (Entity (N))) | |
10742 | and then No (Renamed_Object (Entity (N))); | |
10743 | end Is_Unaliased; | |
10744 | ||
10745 | --------------------- | |
10746 | -- Is_Safe_Operand -- | |
10747 | --------------------- | |
10748 | ||
10749 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
10750 | begin | |
10751 | if No (Op) then | |
10752 | return True; | |
10753 | ||
10754 | elsif Is_Entity_Name (Op) then | |
10755 | return Is_Unaliased (Op); | |
10756 | ||
303b4d58 | 10757 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
10758 | return Is_Unaliased (Prefix (Op)); |
10759 | ||
10760 | elsif Nkind (Op) = N_Slice then | |
10761 | return | |
10762 | Is_Unaliased (Prefix (Op)) | |
10763 | and then Entity (Prefix (Op)) /= Target; | |
10764 | ||
10765 | elsif Nkind (Op) = N_Op_Not then | |
10766 | return Is_Safe_Operand (Right_Opnd (Op)); | |
10767 | ||
10768 | else | |
10769 | return False; | |
10770 | end if; | |
10771 | end Is_Safe_Operand; | |
10772 | ||
e7e4d230 | 10773 | -- Start of processing for Is_Safe_In_Place_Array_Op |
fbf5a39b AC |
10774 | |
10775 | begin | |
685094bf RD |
10776 | -- Skip this processing if the component size is different from system |
10777 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b | 10778 | |
eaa826f8 | 10779 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then |
fbf5a39b AC |
10780 | return False; |
10781 | ||
26bff3d9 | 10782 | -- Cannot do in place stuff on VM_Target since cannot pass addresses |
fbf5a39b | 10783 | |
26bff3d9 | 10784 | elsif VM_Target /= No_VM then |
fbf5a39b AC |
10785 | return False; |
10786 | ||
10787 | -- Cannot do in place stuff if non-standard Boolean representation | |
10788 | ||
eaa826f8 | 10789 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then |
fbf5a39b AC |
10790 | return False; |
10791 | ||
10792 | elsif not Is_Unaliased (Lhs) then | |
10793 | return False; | |
e7e4d230 | 10794 | |
fbf5a39b AC |
10795 | else |
10796 | Target := Entity (Lhs); | |
e7e4d230 | 10797 | return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2); |
fbf5a39b AC |
10798 | end if; |
10799 | end Safe_In_Place_Array_Op; | |
10800 | ||
70482933 RK |
10801 | ----------------------- |
10802 | -- Tagged_Membership -- | |
10803 | ----------------------- | |
10804 | ||
685094bf RD |
10805 | -- There are two different cases to consider depending on whether the right |
10806 | -- operand is a class-wide type or not. If not we just compare the actual | |
10807 | -- tag of the left expr to the target type tag: | |
70482933 RK |
10808 | -- |
10809 | -- Left_Expr.Tag = Right_Type'Tag; | |
10810 | -- | |
685094bf RD |
10811 | -- If it is a class-wide type we use the RT function CW_Membership which is |
10812 | -- usually implemented by looking in the ancestor tables contained in the | |
10813 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 10814 | |
0669bebe GB |
10815 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
10816 | -- function IW_Membership which is usually implemented by looking in the | |
10817 | -- table of abstract interface types plus the ancestor table contained in | |
10818 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
10819 | ||
82878151 AC |
10820 | procedure Tagged_Membership |
10821 | (N : Node_Id; | |
10822 | SCIL_Node : out Node_Id; | |
10823 | Result : out Node_Id) | |
10824 | is | |
70482933 RK |
10825 | Left : constant Node_Id := Left_Opnd (N); |
10826 | Right : constant Node_Id := Right_Opnd (N); | |
10827 | Loc : constant Source_Ptr := Sloc (N); | |
10828 | ||
38171f43 | 10829 | Full_R_Typ : Entity_Id; |
70482933 | 10830 | Left_Type : Entity_Id; |
82878151 | 10831 | New_Node : Node_Id; |
70482933 RK |
10832 | Right_Type : Entity_Id; |
10833 | Obj_Tag : Node_Id; | |
10834 | ||
10835 | begin | |
82878151 AC |
10836 | SCIL_Node := Empty; |
10837 | ||
852dba80 AC |
10838 | -- Handle entities from the limited view |
10839 | ||
10840 | Left_Type := Available_View (Etype (Left)); | |
10841 | Right_Type := Available_View (Etype (Right)); | |
70482933 RK |
10842 | |
10843 | if Is_Class_Wide_Type (Left_Type) then | |
10844 | Left_Type := Root_Type (Left_Type); | |
10845 | end if; | |
10846 | ||
38171f43 AC |
10847 | if Is_Class_Wide_Type (Right_Type) then |
10848 | Full_R_Typ := Underlying_Type (Root_Type (Right_Type)); | |
10849 | else | |
10850 | Full_R_Typ := Underlying_Type (Right_Type); | |
10851 | end if; | |
10852 | ||
70482933 RK |
10853 | Obj_Tag := |
10854 | Make_Selected_Component (Loc, | |
10855 | Prefix => Relocate_Node (Left), | |
a9d8907c JM |
10856 | Selector_Name => |
10857 | New_Reference_To (First_Tag_Component (Left_Type), Loc)); | |
70482933 RK |
10858 | |
10859 | if Is_Class_Wide_Type (Right_Type) then | |
758c442c | 10860 | |
0669bebe GB |
10861 | -- No need to issue a run-time check if we statically know that the |
10862 | -- result of this membership test is always true. For example, | |
10863 | -- considering the following declarations: | |
10864 | ||
10865 | -- type Iface is interface; | |
10866 | -- type T is tagged null record; | |
10867 | -- type DT is new T and Iface with null record; | |
10868 | ||
10869 | -- Obj1 : T; | |
10870 | -- Obj2 : DT; | |
10871 | ||
10872 | -- These membership tests are always true: | |
10873 | ||
10874 | -- Obj1 in T'Class | |
10875 | -- Obj2 in T'Class; | |
10876 | -- Obj2 in Iface'Class; | |
10877 | ||
10878 | -- We do not need to handle cases where the membership is illegal. | |
10879 | -- For example: | |
10880 | ||
10881 | -- Obj1 in DT'Class; -- Compile time error | |
10882 | -- Obj1 in Iface'Class; -- Compile time error | |
10883 | ||
10884 | if not Is_Class_Wide_Type (Left_Type) | |
4ac2477e JM |
10885 | and then (Is_Ancestor (Etype (Right_Type), Left_Type, |
10886 | Use_Full_View => True) | |
0669bebe GB |
10887 | or else (Is_Interface (Etype (Right_Type)) |
10888 | and then Interface_Present_In_Ancestor | |
10889 | (Typ => Left_Type, | |
10890 | Iface => Etype (Right_Type)))) | |
10891 | then | |
82878151 AC |
10892 | Result := New_Reference_To (Standard_True, Loc); |
10893 | return; | |
0669bebe GB |
10894 | end if; |
10895 | ||
758c442c GD |
10896 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
10897 | ||
630d30e9 RD |
10898 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
10899 | ||
0669bebe | 10900 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
10901 | |
10902 | or else Is_Interface (Left_Type) | |
10903 | then | |
dfd99a80 TQ |
10904 | -- Issue error if IW_Membership operation not available in a |
10905 | -- configurable run time setting. | |
10906 | ||
10907 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
10908 | Error_Msg_CRT |
10909 | ("dynamic membership test on interface types", N); | |
82878151 AC |
10910 | Result := Empty; |
10911 | return; | |
dfd99a80 TQ |
10912 | end if; |
10913 | ||
82878151 | 10914 | Result := |
758c442c GD |
10915 | Make_Function_Call (Loc, |
10916 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
10917 | Parameter_Associations => New_List ( | |
10918 | Make_Attribute_Reference (Loc, | |
10919 | Prefix => Obj_Tag, | |
10920 | Attribute_Name => Name_Address), | |
10921 | New_Reference_To ( | |
38171f43 | 10922 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), |
758c442c GD |
10923 | Loc))); |
10924 | ||
10925 | -- Ada 95: Normal case | |
10926 | ||
10927 | else | |
82878151 AC |
10928 | Build_CW_Membership (Loc, |
10929 | Obj_Tag_Node => Obj_Tag, | |
10930 | Typ_Tag_Node => | |
10931 | New_Reference_To ( | |
38171f43 | 10932 | Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc), |
82878151 AC |
10933 | Related_Nod => N, |
10934 | New_Node => New_Node); | |
10935 | ||
10936 | -- Generate the SCIL node for this class-wide membership test. | |
10937 | -- Done here because the previous call to Build_CW_Membership | |
10938 | -- relocates Obj_Tag. | |
10939 | ||
10940 | if Generate_SCIL then | |
10941 | SCIL_Node := Make_SCIL_Membership_Test (Sloc (N)); | |
10942 | Set_SCIL_Entity (SCIL_Node, Etype (Right_Type)); | |
10943 | Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag); | |
10944 | end if; | |
10945 | ||
10946 | Result := New_Node; | |
758c442c GD |
10947 | end if; |
10948 | ||
0669bebe GB |
10949 | -- Right_Type is not a class-wide type |
10950 | ||
70482933 | 10951 | else |
0669bebe GB |
10952 | -- No need to check the tag of the object if Right_Typ is abstract |
10953 | ||
10954 | if Is_Abstract_Type (Right_Type) then | |
82878151 | 10955 | Result := New_Reference_To (Standard_False, Loc); |
0669bebe GB |
10956 | |
10957 | else | |
82878151 | 10958 | Result := |
0669bebe GB |
10959 | Make_Op_Eq (Loc, |
10960 | Left_Opnd => Obj_Tag, | |
10961 | Right_Opnd => | |
10962 | New_Reference_To | |
38171f43 | 10963 | (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc)); |
0669bebe | 10964 | end if; |
70482933 | 10965 | end if; |
70482933 RK |
10966 | end Tagged_Membership; |
10967 | ||
10968 | ------------------------------ | |
10969 | -- Unary_Op_Validity_Checks -- | |
10970 | ------------------------------ | |
10971 | ||
10972 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
10973 | begin | |
10974 | if Validity_Checks_On and Validity_Check_Operands then | |
10975 | Ensure_Valid (Right_Opnd (N)); | |
10976 | end if; | |
10977 | end Unary_Op_Validity_Checks; | |
10978 | ||
10979 | end Exp_Ch4; |