]>
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 | -- -- | |
59ae6391 | 9 | -- Copyright (C) 1992-2008, 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 RK |
39 | with Exp_Fixd; use Exp_Fixd; |
40 | with Exp_Pakd; use Exp_Pakd; | |
41 | with Exp_Tss; use Exp_Tss; | |
42 | with Exp_Util; use Exp_Util; | |
43 | with Exp_VFpt; use Exp_VFpt; | |
f02b8bb8 | 44 | with Freeze; use Freeze; |
70482933 | 45 | with Inline; use Inline; |
26bff3d9 | 46 | with Namet; use Namet; |
70482933 RK |
47 | with Nlists; use Nlists; |
48 | with Nmake; use Nmake; | |
49 | with Opt; use Opt; | |
0669bebe GB |
50 | with Restrict; use Restrict; |
51 | with Rident; use Rident; | |
70482933 RK |
52 | with Rtsfind; use Rtsfind; |
53 | with Sem; use Sem; | |
a4100e55 | 54 | with Sem_Aux; use Sem_Aux; |
70482933 | 55 | with Sem_Cat; use Sem_Cat; |
5d09245e | 56 | with Sem_Ch3; use Sem_Ch3; |
26bff3d9 | 57 | with Sem_Ch8; use Sem_Ch8; |
70482933 RK |
58 | with Sem_Ch13; use Sem_Ch13; |
59 | with Sem_Eval; use Sem_Eval; | |
60 | with Sem_Res; use Sem_Res; | |
61 | with Sem_Type; use Sem_Type; | |
62 | with Sem_Util; use Sem_Util; | |
07fc65c4 | 63 | with Sem_Warn; use Sem_Warn; |
70482933 | 64 | with Sinfo; use Sinfo; |
70482933 RK |
65 | with Snames; use Snames; |
66 | with Stand; use Stand; | |
07fc65c4 | 67 | with Targparm; use Targparm; |
70482933 RK |
68 | with Tbuild; use Tbuild; |
69 | with Ttypes; use Ttypes; | |
70 | with Uintp; use Uintp; | |
71 | with Urealp; use Urealp; | |
72 | with Validsw; use Validsw; | |
73 | ||
74 | package body Exp_Ch4 is | |
75 | ||
15ce9ca2 AC |
76 | ----------------------- |
77 | -- Local Subprograms -- | |
78 | ----------------------- | |
70482933 RK |
79 | |
80 | procedure Binary_Op_Validity_Checks (N : Node_Id); | |
81 | pragma Inline (Binary_Op_Validity_Checks); | |
82 | -- Performs validity checks for a binary operator | |
83 | ||
fbf5a39b AC |
84 | procedure Build_Boolean_Array_Proc_Call |
85 | (N : Node_Id; | |
86 | Op1 : Node_Id; | |
87 | Op2 : Node_Id); | |
303b4d58 | 88 | -- If a boolean array assignment can be done in place, build call to |
fbf5a39b AC |
89 | -- corresponding library procedure. |
90 | ||
26bff3d9 JM |
91 | procedure Displace_Allocator_Pointer (N : Node_Id); |
92 | -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and | |
93 | -- Expand_Allocator_Expression. Allocating class-wide interface objects | |
94 | -- this routine displaces the pointer to the allocated object to reference | |
95 | -- the component referencing the corresponding secondary dispatch table. | |
96 | ||
fbf5a39b AC |
97 | procedure Expand_Allocator_Expression (N : Node_Id); |
98 | -- Subsidiary to Expand_N_Allocator, for the case when the expression | |
99 | -- is a qualified expression or an aggregate. | |
100 | ||
70482933 RK |
101 | procedure Expand_Array_Comparison (N : Node_Id); |
102 | -- This routine handles expansion of the comparison operators (N_Op_Lt, | |
103 | -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic | |
104 | -- code for these operators is similar, differing only in the details of | |
fbf5a39b AC |
105 | -- the actual comparison call that is made. Special processing (call a |
106 | -- run-time routine) | |
70482933 RK |
107 | |
108 | function Expand_Array_Equality | |
109 | (Nod : Node_Id; | |
70482933 RK |
110 | Lhs : Node_Id; |
111 | Rhs : Node_Id; | |
0da2c8ac AC |
112 | Bodies : List_Id; |
113 | Typ : Entity_Id) return Node_Id; | |
70482933 | 114 | -- Expand an array equality into a call to a function implementing this |
685094bf RD |
115 | -- equality, and a call to it. Loc is the location for the generated nodes. |
116 | -- Lhs and Rhs are the array expressions to be compared. Bodies is a list | |
117 | -- on which to attach bodies of local functions that are created in the | |
118 | -- process. It is the responsibility of the caller to insert those bodies | |
119 | -- at the right place. Nod provides the Sloc value for the generated code. | |
120 | -- Normally the types used for the generated equality routine are taken | |
121 | -- from Lhs and Rhs. However, in some situations of generated code, the | |
122 | -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies | |
123 | -- the type to be used for the formal parameters. | |
70482933 RK |
124 | |
125 | procedure Expand_Boolean_Operator (N : Node_Id); | |
685094bf RD |
126 | -- Common expansion processing for Boolean operators (And, Or, Xor) for the |
127 | -- case of array type arguments. | |
70482933 RK |
128 | |
129 | function Expand_Composite_Equality | |
130 | (Nod : Node_Id; | |
131 | Typ : Entity_Id; | |
132 | Lhs : Node_Id; | |
133 | Rhs : Node_Id; | |
2e071734 | 134 | Bodies : List_Id) return Node_Id; |
685094bf RD |
135 | -- Local recursive function used to expand equality for nested composite |
136 | -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which | |
137 | -- to attach bodies of local functions that are created in the process. | |
138 | -- This is the responsibility of the caller to insert those bodies at the | |
139 | -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs | |
140 | -- are the left and right sides for the comparison, and Typ is the type of | |
141 | -- the arrays to compare. | |
70482933 | 142 | |
fdac1f80 AC |
143 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id); |
144 | -- Routine to expand concatenation of a sequence of two or more operands | |
145 | -- (in the list Operands) and replace node Cnode with the result of the | |
146 | -- concatenation. The operands can be of any appropriate type, and can | |
147 | -- include both arrays and singleton elements. | |
70482933 RK |
148 | |
149 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); | |
685094bf RD |
150 | -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal |
151 | -- fixed. We do not have such a type at runtime, so the purpose of this | |
152 | -- routine is to find the real type by looking up the tree. We also | |
153 | -- determine if the operation must be rounded. | |
70482933 | 154 | |
fbf5a39b AC |
155 | function Get_Allocator_Final_List |
156 | (N : Node_Id; | |
157 | T : Entity_Id; | |
2e071734 | 158 | PtrT : Entity_Id) return Entity_Id; |
685094bf RD |
159 | -- If the designated type is controlled, build final_list expression for |
160 | -- created object. If context is an access parameter, create a local access | |
161 | -- type to have a usable finalization list. | |
fbf5a39b | 162 | |
5d09245e AC |
163 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean; |
164 | -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable | |
165 | -- discriminants if it has a constrained nominal type, unless the object | |
166 | -- is a component of an enclosing Unchecked_Union object that is subject | |
167 | -- to a per-object constraint and the enclosing object lacks inferable | |
168 | -- discriminants. | |
169 | -- | |
170 | -- An expression of an Unchecked_Union type has inferable discriminants | |
171 | -- if it is either a name of an object with inferable discriminants or a | |
172 | -- qualified expression whose subtype mark denotes a constrained subtype. | |
173 | ||
70482933 | 174 | procedure Insert_Dereference_Action (N : Node_Id); |
e6f69614 AC |
175 | -- N is an expression whose type is an access. When the type of the |
176 | -- associated storage pool is derived from Checked_Pool, generate a | |
177 | -- call to the 'Dereference' primitive operation. | |
70482933 RK |
178 | |
179 | function Make_Array_Comparison_Op | |
2e071734 AC |
180 | (Typ : Entity_Id; |
181 | Nod : Node_Id) return Node_Id; | |
685094bf RD |
182 | -- Comparisons between arrays are expanded in line. This function produces |
183 | -- the body of the implementation of (a > b), where a and b are one- | |
184 | -- dimensional arrays of some discrete type. The original node is then | |
185 | -- expanded into the appropriate call to this function. Nod provides the | |
186 | -- Sloc value for the generated code. | |
70482933 RK |
187 | |
188 | function Make_Boolean_Array_Op | |
2e071734 AC |
189 | (Typ : Entity_Id; |
190 | N : Node_Id) return Node_Id; | |
685094bf RD |
191 | -- Boolean operations on boolean arrays are expanded in line. This function |
192 | -- produce the body for the node N, which is (a and b), (a or b), or (a xor | |
193 | -- b). It is used only the normal case and not the packed case. The type | |
194 | -- involved, Typ, is the Boolean array type, and the logical operations in | |
195 | -- the body are simple boolean operations. Note that Typ is always a | |
196 | -- constrained type (the caller has ensured this by using | |
197 | -- Convert_To_Actual_Subtype if necessary). | |
70482933 RK |
198 | |
199 | procedure Rewrite_Comparison (N : Node_Id); | |
20b5d666 | 200 | -- If N is the node for a comparison whose outcome can be determined at |
d26dc4b5 AC |
201 | -- compile time, then the node N can be rewritten with True or False. If |
202 | -- the outcome cannot be determined at compile time, the call has no | |
203 | -- effect. If N is a type conversion, then this processing is applied to | |
204 | -- its expression. If N is neither comparison nor a type conversion, the | |
205 | -- call has no effect. | |
70482933 RK |
206 | |
207 | function Tagged_Membership (N : Node_Id) return Node_Id; | |
208 | -- Construct the expression corresponding to the tagged membership test. | |
209 | -- Deals with a second operand being (or not) a class-wide type. | |
210 | ||
fbf5a39b | 211 | function Safe_In_Place_Array_Op |
2e071734 AC |
212 | (Lhs : Node_Id; |
213 | Op1 : Node_Id; | |
214 | Op2 : Node_Id) return Boolean; | |
685094bf RD |
215 | -- In the context of an assignment, where the right-hand side is a boolean |
216 | -- operation on arrays, check whether operation can be performed in place. | |
fbf5a39b | 217 | |
70482933 RK |
218 | procedure Unary_Op_Validity_Checks (N : Node_Id); |
219 | pragma Inline (Unary_Op_Validity_Checks); | |
220 | -- Performs validity checks for a unary operator | |
221 | ||
222 | ------------------------------- | |
223 | -- Binary_Op_Validity_Checks -- | |
224 | ------------------------------- | |
225 | ||
226 | procedure Binary_Op_Validity_Checks (N : Node_Id) is | |
227 | begin | |
228 | if Validity_Checks_On and Validity_Check_Operands then | |
229 | Ensure_Valid (Left_Opnd (N)); | |
230 | Ensure_Valid (Right_Opnd (N)); | |
231 | end if; | |
232 | end Binary_Op_Validity_Checks; | |
233 | ||
fbf5a39b AC |
234 | ------------------------------------ |
235 | -- Build_Boolean_Array_Proc_Call -- | |
236 | ------------------------------------ | |
237 | ||
238 | procedure Build_Boolean_Array_Proc_Call | |
239 | (N : Node_Id; | |
240 | Op1 : Node_Id; | |
241 | Op2 : Node_Id) | |
242 | is | |
243 | Loc : constant Source_Ptr := Sloc (N); | |
244 | Kind : constant Node_Kind := Nkind (Expression (N)); | |
245 | Target : constant Node_Id := | |
246 | Make_Attribute_Reference (Loc, | |
247 | Prefix => Name (N), | |
248 | Attribute_Name => Name_Address); | |
249 | ||
250 | Arg1 : constant Node_Id := Op1; | |
251 | Arg2 : Node_Id := Op2; | |
252 | Call_Node : Node_Id; | |
253 | Proc_Name : Entity_Id; | |
254 | ||
255 | begin | |
256 | if Kind = N_Op_Not then | |
257 | if Nkind (Op1) in N_Binary_Op then | |
258 | ||
5e1c00fa | 259 | -- Use negated version of the binary operators |
fbf5a39b AC |
260 | |
261 | if Nkind (Op1) = N_Op_And then | |
262 | Proc_Name := RTE (RE_Vector_Nand); | |
263 | ||
264 | elsif Nkind (Op1) = N_Op_Or then | |
265 | Proc_Name := RTE (RE_Vector_Nor); | |
266 | ||
267 | else pragma Assert (Nkind (Op1) = N_Op_Xor); | |
268 | Proc_Name := RTE (RE_Vector_Xor); | |
269 | end if; | |
270 | ||
271 | Call_Node := | |
272 | Make_Procedure_Call_Statement (Loc, | |
273 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
274 | ||
275 | Parameter_Associations => New_List ( | |
276 | Target, | |
277 | Make_Attribute_Reference (Loc, | |
278 | Prefix => Left_Opnd (Op1), | |
279 | Attribute_Name => Name_Address), | |
280 | ||
281 | Make_Attribute_Reference (Loc, | |
282 | Prefix => Right_Opnd (Op1), | |
283 | Attribute_Name => Name_Address), | |
284 | ||
285 | Make_Attribute_Reference (Loc, | |
286 | Prefix => Left_Opnd (Op1), | |
287 | Attribute_Name => Name_Length))); | |
288 | ||
289 | else | |
290 | Proc_Name := RTE (RE_Vector_Not); | |
291 | ||
292 | Call_Node := | |
293 | Make_Procedure_Call_Statement (Loc, | |
294 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
295 | Parameter_Associations => New_List ( | |
296 | Target, | |
297 | ||
298 | Make_Attribute_Reference (Loc, | |
299 | Prefix => Op1, | |
300 | Attribute_Name => Name_Address), | |
301 | ||
302 | Make_Attribute_Reference (Loc, | |
303 | Prefix => Op1, | |
304 | Attribute_Name => Name_Length))); | |
305 | end if; | |
306 | ||
307 | else | |
308 | -- We use the following equivalences: | |
309 | ||
310 | -- (not X) or (not Y) = not (X and Y) = Nand (X, Y) | |
311 | -- (not X) and (not Y) = not (X or Y) = Nor (X, Y) | |
312 | -- (not X) xor (not Y) = X xor Y | |
313 | -- X xor (not Y) = not (X xor Y) = Nxor (X, Y) | |
314 | ||
315 | if Nkind (Op1) = N_Op_Not then | |
316 | if Kind = N_Op_And then | |
317 | Proc_Name := RTE (RE_Vector_Nor); | |
318 | ||
319 | elsif Kind = N_Op_Or then | |
320 | Proc_Name := RTE (RE_Vector_Nand); | |
321 | ||
322 | else | |
323 | Proc_Name := RTE (RE_Vector_Xor); | |
324 | end if; | |
325 | ||
326 | else | |
327 | if Kind = N_Op_And then | |
328 | Proc_Name := RTE (RE_Vector_And); | |
329 | ||
330 | elsif Kind = N_Op_Or then | |
331 | Proc_Name := RTE (RE_Vector_Or); | |
332 | ||
333 | elsif Nkind (Op2) = N_Op_Not then | |
334 | Proc_Name := RTE (RE_Vector_Nxor); | |
335 | Arg2 := Right_Opnd (Op2); | |
336 | ||
337 | else | |
338 | Proc_Name := RTE (RE_Vector_Xor); | |
339 | end if; | |
340 | end if; | |
341 | ||
342 | Call_Node := | |
343 | Make_Procedure_Call_Statement (Loc, | |
344 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
345 | Parameter_Associations => New_List ( | |
346 | Target, | |
347 | Make_Attribute_Reference (Loc, | |
348 | Prefix => Arg1, | |
349 | Attribute_Name => Name_Address), | |
350 | Make_Attribute_Reference (Loc, | |
351 | Prefix => Arg2, | |
352 | Attribute_Name => Name_Address), | |
353 | Make_Attribute_Reference (Loc, | |
354 | Prefix => Op1, | |
355 | Attribute_Name => Name_Length))); | |
356 | end if; | |
357 | ||
358 | Rewrite (N, Call_Node); | |
359 | Analyze (N); | |
360 | ||
361 | exception | |
362 | when RE_Not_Available => | |
363 | return; | |
364 | end Build_Boolean_Array_Proc_Call; | |
365 | ||
26bff3d9 JM |
366 | -------------------------------- |
367 | -- Displace_Allocator_Pointer -- | |
368 | -------------------------------- | |
369 | ||
370 | procedure Displace_Allocator_Pointer (N : Node_Id) is | |
371 | Loc : constant Source_Ptr := Sloc (N); | |
372 | Orig_Node : constant Node_Id := Original_Node (N); | |
373 | Dtyp : Entity_Id; | |
374 | Etyp : Entity_Id; | |
375 | PtrT : Entity_Id; | |
376 | ||
377 | begin | |
303b4d58 AC |
378 | -- Do nothing in case of VM targets: the virtual machine will handle |
379 | -- interfaces directly. | |
380 | ||
381 | if VM_Target /= No_VM then | |
382 | return; | |
383 | end if; | |
384 | ||
26bff3d9 JM |
385 | pragma Assert (Nkind (N) = N_Identifier |
386 | and then Nkind (Orig_Node) = N_Allocator); | |
387 | ||
388 | PtrT := Etype (Orig_Node); | |
389 | Dtyp := Designated_Type (PtrT); | |
390 | Etyp := Etype (Expression (Orig_Node)); | |
391 | ||
392 | if Is_Class_Wide_Type (Dtyp) | |
393 | and then Is_Interface (Dtyp) | |
394 | then | |
395 | -- If the type of the allocator expression is not an interface type | |
396 | -- we can generate code to reference the record component containing | |
397 | -- the pointer to the secondary dispatch table. | |
398 | ||
399 | if not Is_Interface (Etyp) then | |
400 | declare | |
401 | Saved_Typ : constant Entity_Id := Etype (Orig_Node); | |
402 | ||
403 | begin | |
404 | -- 1) Get access to the allocated object | |
405 | ||
406 | Rewrite (N, | |
407 | Make_Explicit_Dereference (Loc, | |
408 | Relocate_Node (N))); | |
409 | Set_Etype (N, Etyp); | |
410 | Set_Analyzed (N); | |
411 | ||
412 | -- 2) Add the conversion to displace the pointer to reference | |
413 | -- the secondary dispatch table. | |
414 | ||
415 | Rewrite (N, Convert_To (Dtyp, Relocate_Node (N))); | |
416 | Analyze_And_Resolve (N, Dtyp); | |
417 | ||
418 | -- 3) The 'access to the secondary dispatch table will be used | |
419 | -- as the value returned by the allocator. | |
420 | ||
421 | Rewrite (N, | |
422 | Make_Attribute_Reference (Loc, | |
423 | Prefix => Relocate_Node (N), | |
424 | Attribute_Name => Name_Access)); | |
425 | Set_Etype (N, Saved_Typ); | |
426 | Set_Analyzed (N); | |
427 | end; | |
428 | ||
429 | -- If the type of the allocator expression is an interface type we | |
430 | -- generate a run-time call to displace "this" to reference the | |
431 | -- component containing the pointer to the secondary dispatch table | |
432 | -- or else raise Constraint_Error if the actual object does not | |
433 | -- implement the target interface. This case corresponds with the | |
434 | -- following example: | |
435 | ||
8fc789c8 | 436 | -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is |
26bff3d9 JM |
437 | -- begin |
438 | -- return new Iface_2'Class'(Obj); | |
439 | -- end Op; | |
440 | ||
441 | else | |
442 | Rewrite (N, | |
443 | Unchecked_Convert_To (PtrT, | |
444 | Make_Function_Call (Loc, | |
445 | Name => New_Reference_To (RTE (RE_Displace), Loc), | |
446 | Parameter_Associations => New_List ( | |
447 | Unchecked_Convert_To (RTE (RE_Address), | |
448 | Relocate_Node (N)), | |
449 | ||
450 | New_Occurrence_Of | |
451 | (Elists.Node | |
452 | (First_Elmt | |
453 | (Access_Disp_Table (Etype (Base_Type (Dtyp))))), | |
454 | Loc))))); | |
455 | Analyze_And_Resolve (N, PtrT); | |
456 | end if; | |
457 | end if; | |
458 | end Displace_Allocator_Pointer; | |
459 | ||
fbf5a39b AC |
460 | --------------------------------- |
461 | -- Expand_Allocator_Expression -- | |
462 | --------------------------------- | |
463 | ||
464 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
f02b8bb8 RD |
465 | Loc : constant Source_Ptr := Sloc (N); |
466 | Exp : constant Node_Id := Expression (Expression (N)); | |
f02b8bb8 RD |
467 | PtrT : constant Entity_Id := Etype (N); |
468 | DesigT : constant Entity_Id := Designated_Type (PtrT); | |
26bff3d9 JM |
469 | |
470 | procedure Apply_Accessibility_Check | |
471 | (Ref : Node_Id; | |
472 | Built_In_Place : Boolean := False); | |
473 | -- Ada 2005 (AI-344): For an allocator with a class-wide designated | |
685094bf RD |
474 | -- type, generate an accessibility check to verify that the level of the |
475 | -- type of the created object is not deeper than the level of the access | |
476 | -- type. If the type of the qualified expression is class- wide, then | |
477 | -- always generate the check (except in the case where it is known to be | |
478 | -- unnecessary, see comment below). Otherwise, only generate the check | |
479 | -- if the level of the qualified expression type is statically deeper | |
480 | -- than the access type. | |
481 | -- | |
482 | -- Although the static accessibility will generally have been performed | |
483 | -- as a legality check, it won't have been done in cases where the | |
484 | -- allocator appears in generic body, so a run-time check is needed in | |
485 | -- general. One special case is when the access type is declared in the | |
486 | -- same scope as the class-wide allocator, in which case the check can | |
487 | -- never fail, so it need not be generated. | |
488 | -- | |
489 | -- As an open issue, there seem to be cases where the static level | |
490 | -- associated with the class-wide object's underlying type is not | |
491 | -- sufficient to perform the proper accessibility check, such as for | |
492 | -- allocators in nested subprograms or accept statements initialized by | |
493 | -- class-wide formals when the actual originates outside at a deeper | |
494 | -- static level. The nested subprogram case might require passing | |
495 | -- accessibility levels along with class-wide parameters, and the task | |
496 | -- case seems to be an actual gap in the language rules that needs to | |
497 | -- be fixed by the ARG. ??? | |
26bff3d9 JM |
498 | |
499 | ------------------------------- | |
500 | -- Apply_Accessibility_Check -- | |
501 | ------------------------------- | |
502 | ||
503 | procedure Apply_Accessibility_Check | |
504 | (Ref : Node_Id; | |
505 | Built_In_Place : Boolean := False) | |
506 | is | |
507 | Ref_Node : Node_Id; | |
508 | ||
509 | begin | |
510 | -- Note: we skip the accessibility check for the VM case, since | |
511 | -- there does not seem to be any practical way of implementing it. | |
512 | ||
513 | if Ada_Version >= Ada_05 | |
514 | and then VM_Target = No_VM | |
515 | and then Is_Class_Wide_Type (DesigT) | |
516 | and then not Scope_Suppress (Accessibility_Check) | |
517 | and then | |
518 | (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT) | |
519 | or else | |
520 | (Is_Class_Wide_Type (Etype (Exp)) | |
521 | and then Scope (PtrT) /= Current_Scope)) | |
522 | then | |
523 | -- If the allocator was built in place Ref is already a reference | |
524 | -- to the access object initialized to the result of the allocator | |
525 | -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise | |
526 | -- it is the entity associated with the object containing the | |
527 | -- address of the allocated object. | |
528 | ||
529 | if Built_In_Place then | |
530 | Ref_Node := New_Copy (Ref); | |
531 | else | |
532 | Ref_Node := New_Reference_To (Ref, Loc); | |
533 | end if; | |
534 | ||
535 | Insert_Action (N, | |
536 | Make_Raise_Program_Error (Loc, | |
537 | Condition => | |
538 | Make_Op_Gt (Loc, | |
539 | Left_Opnd => | |
540 | Build_Get_Access_Level (Loc, | |
541 | Make_Attribute_Reference (Loc, | |
542 | Prefix => Ref_Node, | |
543 | Attribute_Name => Name_Tag)), | |
544 | Right_Opnd => | |
545 | Make_Integer_Literal (Loc, | |
546 | Type_Access_Level (PtrT))), | |
547 | Reason => PE_Accessibility_Check_Failed)); | |
548 | end if; | |
549 | end Apply_Accessibility_Check; | |
550 | ||
551 | -- Local variables | |
552 | ||
553 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
554 | T : constant Entity_Id := Entity (Indic); | |
555 | Flist : Node_Id; | |
556 | Node : Node_Id; | |
557 | Temp : Entity_Id; | |
fbf5a39b | 558 | |
d26dc4b5 AC |
559 | TagT : Entity_Id := Empty; |
560 | -- Type used as source for tag assignment | |
561 | ||
562 | TagR : Node_Id := Empty; | |
563 | -- Target reference for tag assignment | |
564 | ||
fbf5a39b AC |
565 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); |
566 | ||
567 | Tag_Assign : Node_Id; | |
568 | Tmp_Node : Node_Id; | |
569 | ||
26bff3d9 JM |
570 | -- Start of processing for Expand_Allocator_Expression |
571 | ||
fbf5a39b | 572 | begin |
048e5cef | 573 | if Is_Tagged_Type (T) or else Needs_Finalization (T) then |
fbf5a39b | 574 | |
685094bf RD |
575 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
576 | -- to a build-in-place function, then access to the allocated object | |
577 | -- must be passed to the function. Currently we limit such functions | |
578 | -- to those with constrained limited result subtypes, but eventually | |
579 | -- we plan to expand the allowed forms of functions that are treated | |
580 | -- as build-in-place. | |
20b5d666 JM |
581 | |
582 | if Ada_Version >= Ada_05 | |
583 | and then Is_Build_In_Place_Function_Call (Exp) | |
584 | then | |
585 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
26bff3d9 JM |
586 | Apply_Accessibility_Check (N, Built_In_Place => True); |
587 | return; | |
20b5d666 JM |
588 | end if; |
589 | ||
fbf5a39b AC |
590 | -- Actions inserted before: |
591 | -- Temp : constant ptr_T := new T'(Expression); | |
592 | -- <no CW> Temp._tag := T'tag; | |
593 | -- <CTRL> Adjust (Finalizable (Temp.all)); | |
594 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
595 | ||
596 | -- We analyze by hand the new internal allocator to avoid | |
597 | -- any recursion and inappropriate call to Initialize | |
7324bf49 | 598 | |
20b5d666 JM |
599 | -- We don't want to remove side effects when the expression must be |
600 | -- built in place. In the case of a build-in-place function call, | |
601 | -- that could lead to a duplication of the call, which was already | |
602 | -- substituted for the allocator. | |
603 | ||
26bff3d9 | 604 | if not Aggr_In_Place then |
fbf5a39b AC |
605 | Remove_Side_Effects (Exp); |
606 | end if; | |
607 | ||
608 | Temp := | |
609 | Make_Defining_Identifier (Loc, New_Internal_Name ('P')); | |
610 | ||
611 | -- For a class wide allocation generate the following code: | |
612 | ||
613 | -- type Equiv_Record is record ... end record; | |
614 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
615 | -- temp : PtrT := new CW'(CW!(expr)); | |
616 | ||
617 | if Is_Class_Wide_Type (T) then | |
618 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
619 | ||
26bff3d9 JM |
620 | -- Ada 2005 (AI-251): If the expression is a class-wide interface |
621 | -- object we generate code to move up "this" to reference the | |
622 | -- base of the object before allocating the new object. | |
623 | ||
624 | -- Note that Exp'Address is recursively expanded into a call | |
625 | -- to Base_Address (Exp.Tag) | |
626 | ||
627 | if Is_Class_Wide_Type (Etype (Exp)) | |
628 | and then Is_Interface (Etype (Exp)) | |
303b4d58 | 629 | and then VM_Target = No_VM |
26bff3d9 JM |
630 | then |
631 | Set_Expression | |
632 | (Expression (N), | |
633 | Unchecked_Convert_To (Entity (Indic), | |
634 | Make_Explicit_Dereference (Loc, | |
635 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
636 | Make_Attribute_Reference (Loc, | |
637 | Prefix => Exp, | |
638 | Attribute_Name => Name_Address))))); | |
639 | ||
640 | else | |
641 | Set_Expression | |
642 | (Expression (N), | |
643 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
644 | end if; | |
fbf5a39b AC |
645 | |
646 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
647 | end if; | |
648 | ||
26bff3d9 | 649 | -- Keep separate the management of allocators returning interfaces |
fbf5a39b | 650 | |
26bff3d9 JM |
651 | if not Is_Interface (Directly_Designated_Type (PtrT)) then |
652 | if Aggr_In_Place then | |
653 | Tmp_Node := | |
654 | Make_Object_Declaration (Loc, | |
655 | Defining_Identifier => Temp, | |
656 | Object_Definition => New_Reference_To (PtrT, Loc), | |
657 | Expression => | |
658 | Make_Allocator (Loc, | |
659 | New_Reference_To (Etype (Exp), Loc))); | |
fbf5a39b | 660 | |
fad0600d AC |
661 | -- Copy the Comes_From_Source flag for the allocator we just |
662 | -- built, since logically this allocator is a replacement of | |
663 | -- the original allocator node. This is for proper handling of | |
664 | -- restriction No_Implicit_Heap_Allocations. | |
665 | ||
26bff3d9 JM |
666 | Set_Comes_From_Source |
667 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
fbf5a39b | 668 | |
26bff3d9 JM |
669 | Set_No_Initialization (Expression (Tmp_Node)); |
670 | Insert_Action (N, Tmp_Node); | |
fbf5a39b | 671 | |
048e5cef | 672 | if Needs_Finalization (T) |
26bff3d9 JM |
673 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
674 | then | |
675 | -- Create local finalization list for access parameter | |
676 | ||
677 | Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
678 | end if; | |
679 | ||
d766cee3 | 680 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
fad0600d | 681 | |
26bff3d9 JM |
682 | else |
683 | Node := Relocate_Node (N); | |
684 | Set_Analyzed (Node); | |
685 | Insert_Action (N, | |
686 | Make_Object_Declaration (Loc, | |
687 | Defining_Identifier => Temp, | |
688 | Constant_Present => True, | |
689 | Object_Definition => New_Reference_To (PtrT, Loc), | |
690 | Expression => Node)); | |
fbf5a39b AC |
691 | end if; |
692 | ||
26bff3d9 JM |
693 | -- Ada 2005 (AI-251): Handle allocators whose designated type is an |
694 | -- interface type. In this case we use the type of the qualified | |
695 | -- expression to allocate the object. | |
696 | ||
fbf5a39b | 697 | else |
26bff3d9 JM |
698 | declare |
699 | Def_Id : constant Entity_Id := | |
700 | Make_Defining_Identifier (Loc, | |
701 | New_Internal_Name ('T')); | |
702 | New_Decl : Node_Id; | |
fbf5a39b | 703 | |
26bff3d9 JM |
704 | begin |
705 | New_Decl := | |
706 | Make_Full_Type_Declaration (Loc, | |
707 | Defining_Identifier => Def_Id, | |
708 | Type_Definition => | |
709 | Make_Access_To_Object_Definition (Loc, | |
710 | All_Present => True, | |
711 | Null_Exclusion_Present => False, | |
712 | Constant_Present => False, | |
713 | Subtype_Indication => | |
714 | New_Reference_To (Etype (Exp), Loc))); | |
715 | ||
716 | Insert_Action (N, New_Decl); | |
717 | ||
718 | -- Inherit the final chain to ensure that the expansion of the | |
719 | -- aggregate is correct in case of controlled types | |
720 | ||
048e5cef | 721 | if Needs_Finalization (Directly_Designated_Type (PtrT)) then |
26bff3d9 JM |
722 | Set_Associated_Final_Chain (Def_Id, |
723 | Associated_Final_Chain (PtrT)); | |
724 | end if; | |
758c442c | 725 | |
26bff3d9 JM |
726 | -- Declare the object using the previous type declaration |
727 | ||
728 | if Aggr_In_Place then | |
729 | Tmp_Node := | |
730 | Make_Object_Declaration (Loc, | |
731 | Defining_Identifier => Temp, | |
732 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
733 | Expression => | |
734 | Make_Allocator (Loc, | |
735 | New_Reference_To (Etype (Exp), Loc))); | |
736 | ||
fad0600d AC |
737 | -- Copy the Comes_From_Source flag for the allocator we just |
738 | -- built, since logically this allocator is a replacement of | |
739 | -- the original allocator node. This is for proper handling | |
740 | -- of restriction No_Implicit_Heap_Allocations. | |
741 | ||
26bff3d9 JM |
742 | Set_Comes_From_Source |
743 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
744 | ||
745 | Set_No_Initialization (Expression (Tmp_Node)); | |
746 | Insert_Action (N, Tmp_Node); | |
747 | ||
048e5cef | 748 | if Needs_Finalization (T) |
26bff3d9 JM |
749 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
750 | then | |
751 | -- Create local finalization list for access parameter | |
752 | ||
753 | Flist := | |
754 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
755 | end if; | |
756 | ||
d766cee3 | 757 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
26bff3d9 JM |
758 | else |
759 | Node := Relocate_Node (N); | |
760 | Set_Analyzed (Node); | |
761 | Insert_Action (N, | |
762 | Make_Object_Declaration (Loc, | |
763 | Defining_Identifier => Temp, | |
764 | Constant_Present => True, | |
765 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
766 | Expression => Node)); | |
767 | end if; | |
768 | ||
769 | -- Generate an additional object containing the address of the | |
770 | -- returned object. The type of this second object declaration | |
685094bf RD |
771 | -- is the correct type required for the common processing that |
772 | -- is still performed by this subprogram. The displacement of | |
773 | -- this pointer to reference the component associated with the | |
774 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
775 | |
776 | New_Decl := | |
777 | Make_Object_Declaration (Loc, | |
778 | Defining_Identifier => Make_Defining_Identifier (Loc, | |
779 | New_Internal_Name ('P')), | |
780 | Object_Definition => New_Reference_To (PtrT, Loc), | |
781 | Expression => Unchecked_Convert_To (PtrT, | |
782 | New_Reference_To (Temp, Loc))); | |
783 | ||
784 | Insert_Action (N, New_Decl); | |
785 | ||
786 | Tmp_Node := New_Decl; | |
787 | Temp := Defining_Identifier (New_Decl); | |
788 | end; | |
758c442c GD |
789 | end if; |
790 | ||
26bff3d9 JM |
791 | Apply_Accessibility_Check (Temp); |
792 | ||
793 | -- Generate the tag assignment | |
794 | ||
795 | -- Suppress the tag assignment when VM_Target because VM tags are | |
796 | -- represented implicitly in objects. | |
797 | ||
798 | if VM_Target /= No_VM then | |
799 | null; | |
fbf5a39b | 800 | |
26bff3d9 JM |
801 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
802 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 803 | |
26bff3d9 JM |
804 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
805 | pragma Assert (Is_Class_Wide_Type | |
806 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
807 | null; |
808 | ||
809 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
810 | TagT := T; | |
811 | TagR := New_Reference_To (Temp, Loc); | |
812 | ||
813 | elsif Is_Private_Type (T) | |
814 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 815 | then |
d26dc4b5 | 816 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
817 | TagR := |
818 | Unchecked_Convert_To (Underlying_Type (T), | |
819 | Make_Explicit_Dereference (Loc, | |
820 | Prefix => New_Reference_To (Temp, Loc))); | |
d26dc4b5 AC |
821 | end if; |
822 | ||
823 | if Present (TagT) then | |
fbf5a39b AC |
824 | Tag_Assign := |
825 | Make_Assignment_Statement (Loc, | |
826 | Name => | |
827 | Make_Selected_Component (Loc, | |
d26dc4b5 | 828 | Prefix => TagR, |
fbf5a39b | 829 | Selector_Name => |
d26dc4b5 | 830 | New_Reference_To (First_Tag_Component (TagT), Loc)), |
fbf5a39b AC |
831 | |
832 | Expression => | |
833 | Unchecked_Convert_To (RTE (RE_Tag), | |
a9d8907c | 834 | New_Reference_To |
d26dc4b5 | 835 | (Elists.Node (First_Elmt (Access_Disp_Table (TagT))), |
a9d8907c | 836 | Loc))); |
fbf5a39b AC |
837 | |
838 | -- The previous assignment has to be done in any case | |
839 | ||
840 | Set_Assignment_OK (Name (Tag_Assign)); | |
841 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
842 | end if; |
843 | ||
048e5cef BD |
844 | if Needs_Finalization (DesigT) |
845 | and then Needs_Finalization (T) | |
fbf5a39b AC |
846 | then |
847 | declare | |
848 | Attach : Node_Id; | |
849 | Apool : constant Entity_Id := | |
850 | Associated_Storage_Pool (PtrT); | |
851 | ||
852 | begin | |
685094bf RD |
853 | -- If it is an allocation on the secondary stack (i.e. a value |
854 | -- returned from a function), the object is attached on the | |
855 | -- caller side as soon as the call is completed (see | |
856 | -- Expand_Ctrl_Function_Call) | |
fbf5a39b AC |
857 | |
858 | if Is_RTE (Apool, RE_SS_Pool) then | |
859 | declare | |
860 | F : constant Entity_Id := | |
861 | Make_Defining_Identifier (Loc, | |
862 | New_Internal_Name ('F')); | |
863 | begin | |
864 | Insert_Action (N, | |
865 | Make_Object_Declaration (Loc, | |
866 | Defining_Identifier => F, | |
867 | Object_Definition => New_Reference_To (RTE | |
868 | (RE_Finalizable_Ptr), Loc))); | |
869 | ||
870 | Flist := New_Reference_To (F, Loc); | |
871 | Attach := Make_Integer_Literal (Loc, 1); | |
872 | end; | |
873 | ||
874 | -- Normal case, not a secondary stack allocation | |
875 | ||
876 | else | |
048e5cef | 877 | if Needs_Finalization (T) |
615cbd95 AC |
878 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
879 | then | |
5e1c00fa | 880 | -- Create local finalization list for access parameter |
615cbd95 AC |
881 | |
882 | Flist := | |
883 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
884 | else | |
885 | Flist := Find_Final_List (PtrT); | |
886 | end if; | |
887 | ||
fbf5a39b AC |
888 | Attach := Make_Integer_Literal (Loc, 2); |
889 | end if; | |
890 | ||
26bff3d9 JM |
891 | -- Generate an Adjust call if the object will be moved. In Ada |
892 | -- 2005, the object may be inherently limited, in which case | |
893 | -- there is no Adjust procedure, and the object is built in | |
894 | -- place. In Ada 95, the object can be limited but not | |
895 | -- inherently limited if this allocator came from a return | |
896 | -- statement (we're allocating the result on the secondary | |
897 | -- stack). In that case, the object will be moved, so we _do_ | |
898 | -- want to Adjust. | |
899 | ||
900 | if not Aggr_In_Place | |
901 | and then not Is_Inherently_Limited_Type (T) | |
902 | then | |
fbf5a39b AC |
903 | Insert_Actions (N, |
904 | Make_Adjust_Call ( | |
905 | Ref => | |
906 | ||
685094bf RD |
907 | -- An unchecked conversion is needed in the classwide |
908 | -- case because the designated type can be an ancestor of | |
909 | -- the subtype mark of the allocator. | |
fbf5a39b AC |
910 | |
911 | Unchecked_Convert_To (T, | |
912 | Make_Explicit_Dereference (Loc, | |
dfd99a80 | 913 | Prefix => New_Reference_To (Temp, Loc))), |
fbf5a39b AC |
914 | |
915 | Typ => T, | |
916 | Flist_Ref => Flist, | |
dfd99a80 TQ |
917 | With_Attach => Attach, |
918 | Allocator => True)); | |
fbf5a39b AC |
919 | end if; |
920 | end; | |
921 | end if; | |
922 | ||
923 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
924 | Analyze_And_Resolve (N, PtrT); | |
925 | ||
685094bf RD |
926 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
927 | -- component containing the secondary dispatch table of the interface | |
928 | -- type. | |
26bff3d9 JM |
929 | |
930 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
931 | Displace_Allocator_Pointer (N); | |
932 | end if; | |
933 | ||
fbf5a39b AC |
934 | elsif Aggr_In_Place then |
935 | Temp := | |
936 | Make_Defining_Identifier (Loc, New_Internal_Name ('P')); | |
937 | Tmp_Node := | |
938 | Make_Object_Declaration (Loc, | |
939 | Defining_Identifier => Temp, | |
940 | Object_Definition => New_Reference_To (PtrT, Loc), | |
941 | Expression => Make_Allocator (Loc, | |
942 | New_Reference_To (Etype (Exp), Loc))); | |
943 | ||
fad0600d AC |
944 | -- Copy the Comes_From_Source flag for the allocator we just built, |
945 | -- since logically this allocator is a replacement of the original | |
946 | -- allocator node. This is for proper handling of restriction | |
947 | -- No_Implicit_Heap_Allocations. | |
948 | ||
fbf5a39b AC |
949 | Set_Comes_From_Source |
950 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
951 | ||
952 | Set_No_Initialization (Expression (Tmp_Node)); | |
953 | Insert_Action (N, Tmp_Node); | |
d766cee3 | 954 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
fbf5a39b AC |
955 | Rewrite (N, New_Reference_To (Temp, Loc)); |
956 | Analyze_And_Resolve (N, PtrT); | |
957 | ||
51e4c4b9 AC |
958 | elsif Is_Access_Type (T) |
959 | and then Can_Never_Be_Null (T) | |
960 | then | |
961 | Install_Null_Excluding_Check (Exp); | |
962 | ||
f02b8bb8 | 963 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
964 | and then Nkind (Exp) = N_Allocator |
965 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
966 | then | |
0da2c8ac | 967 | -- Apply constraint to designated subtype indication |
fbf5a39b AC |
968 | |
969 | Apply_Constraint_Check (Expression (Exp), | |
f02b8bb8 | 970 | Designated_Type (DesigT), |
fbf5a39b AC |
971 | No_Sliding => True); |
972 | ||
973 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
974 | ||
975 | -- Propagate constraint_error to enclosing allocator | |
976 | ||
977 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
978 | end if; | |
979 | else | |
980 | -- First check against the type of the qualified expression | |
981 | -- | |
685094bf RD |
982 | -- NOTE: The commented call should be correct, but for some reason |
983 | -- causes the compiler to bomb (sigsegv) on ACVC test c34007g, so for | |
984 | -- now we just perform the old (incorrect) test against the | |
985 | -- designated subtype with no sliding in the else part of the if | |
986 | -- statement below. ??? | |
fbf5a39b AC |
987 | -- |
988 | -- Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
989 | ||
685094bf RD |
990 | -- A check is also needed in cases where the designated subtype is |
991 | -- constrained and differs from the subtype given in the qualified | |
992 | -- expression. Note that the check on the qualified expression does | |
993 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 994 | |
f02b8bb8 | 995 | if Is_Constrained (DesigT) |
9450205a | 996 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
997 | then |
998 | Apply_Constraint_Check | |
f02b8bb8 | 999 | (Exp, DesigT, No_Sliding => False); |
fbf5a39b | 1000 | |
685094bf RD |
1001 | -- The nonsliding check should really be performed (unconditionally) |
1002 | -- against the subtype of the qualified expression, but that causes a | |
1003 | -- problem with c34007g (see above), so for now we retain this. | |
fbf5a39b AC |
1004 | |
1005 | else | |
1006 | Apply_Constraint_Check | |
f02b8bb8 RD |
1007 | (Exp, DesigT, No_Sliding => True); |
1008 | end if; | |
1009 | ||
685094bf RD |
1010 | -- For an access to unconstrained packed array, GIGI needs to see an |
1011 | -- expression with a constrained subtype in order to compute the | |
1012 | -- proper size for the allocator. | |
f02b8bb8 RD |
1013 | |
1014 | if Is_Array_Type (T) | |
1015 | and then not Is_Constrained (T) | |
1016 | and then Is_Packed (T) | |
1017 | then | |
1018 | declare | |
1019 | ConstrT : constant Entity_Id := | |
1020 | Make_Defining_Identifier (Loc, | |
1021 | Chars => New_Internal_Name ('A')); | |
1022 | Internal_Exp : constant Node_Id := Relocate_Node (Exp); | |
1023 | begin | |
1024 | Insert_Action (Exp, | |
1025 | Make_Subtype_Declaration (Loc, | |
1026 | Defining_Identifier => ConstrT, | |
1027 | Subtype_Indication => | |
1028 | Make_Subtype_From_Expr (Exp, T))); | |
1029 | Freeze_Itype (ConstrT, Exp); | |
1030 | Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp)); | |
1031 | end; | |
fbf5a39b | 1032 | end if; |
f02b8bb8 | 1033 | |
685094bf RD |
1034 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
1035 | -- to a build-in-place function, then access to the allocated object | |
1036 | -- must be passed to the function. Currently we limit such functions | |
1037 | -- to those with constrained limited result subtypes, but eventually | |
1038 | -- we plan to expand the allowed forms of functions that are treated | |
1039 | -- as build-in-place. | |
20b5d666 JM |
1040 | |
1041 | if Ada_Version >= Ada_05 | |
1042 | and then Is_Build_In_Place_Function_Call (Exp) | |
1043 | then | |
1044 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
1045 | end if; | |
fbf5a39b AC |
1046 | end if; |
1047 | ||
1048 | exception | |
1049 | when RE_Not_Available => | |
1050 | return; | |
1051 | end Expand_Allocator_Expression; | |
1052 | ||
70482933 RK |
1053 | ----------------------------- |
1054 | -- Expand_Array_Comparison -- | |
1055 | ----------------------------- | |
1056 | ||
685094bf RD |
1057 | -- Expansion is only required in the case of array types. For the unpacked |
1058 | -- case, an appropriate runtime routine is called. For packed cases, and | |
1059 | -- also in some other cases where a runtime routine cannot be called, the | |
1060 | -- form of the expansion is: | |
70482933 RK |
1061 | |
1062 | -- [body for greater_nn; boolean_expression] | |
1063 | ||
1064 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
1065 | -- Boolean expression depends on the operator involved. | |
1066 | ||
1067 | procedure Expand_Array_Comparison (N : Node_Id) is | |
1068 | Loc : constant Source_Ptr := Sloc (N); | |
1069 | Op1 : Node_Id := Left_Opnd (N); | |
1070 | Op2 : Node_Id := Right_Opnd (N); | |
1071 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 1072 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
1073 | |
1074 | Expr : Node_Id; | |
1075 | Func_Body : Node_Id; | |
1076 | Func_Name : Entity_Id; | |
1077 | ||
fbf5a39b AC |
1078 | Comp : RE_Id; |
1079 | ||
9bc43c53 AC |
1080 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
1081 | -- True for byte addressable target | |
91b1417d | 1082 | |
fbf5a39b | 1083 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
685094bf RD |
1084 | -- Returns True if the length of the given operand is known to be less |
1085 | -- than 4. Returns False if this length is known to be four or greater | |
1086 | -- or is not known at compile time. | |
fbf5a39b AC |
1087 | |
1088 | ------------------------ | |
1089 | -- Length_Less_Than_4 -- | |
1090 | ------------------------ | |
1091 | ||
1092 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
1093 | Otyp : constant Entity_Id := Etype (Opnd); | |
1094 | ||
1095 | begin | |
1096 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
1097 | return String_Literal_Length (Otyp) < 4; | |
1098 | ||
1099 | else | |
1100 | declare | |
1101 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
1102 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
1103 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
1104 | Lov : Uint; | |
1105 | Hiv : Uint; | |
1106 | ||
1107 | begin | |
1108 | if Compile_Time_Known_Value (Lo) then | |
1109 | Lov := Expr_Value (Lo); | |
1110 | else | |
1111 | return False; | |
1112 | end if; | |
1113 | ||
1114 | if Compile_Time_Known_Value (Hi) then | |
1115 | Hiv := Expr_Value (Hi); | |
1116 | else | |
1117 | return False; | |
1118 | end if; | |
1119 | ||
1120 | return Hiv < Lov + 3; | |
1121 | end; | |
1122 | end if; | |
1123 | end Length_Less_Than_4; | |
1124 | ||
1125 | -- Start of processing for Expand_Array_Comparison | |
1126 | ||
70482933 | 1127 | begin |
fbf5a39b AC |
1128 | -- Deal first with unpacked case, where we can call a runtime routine |
1129 | -- except that we avoid this for targets for which are not addressable | |
26bff3d9 | 1130 | -- by bytes, and for the JVM/CIL, since they do not support direct |
fbf5a39b AC |
1131 | -- addressing of array components. |
1132 | ||
1133 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 1134 | and then Byte_Addressable |
26bff3d9 | 1135 | and then VM_Target = No_VM |
fbf5a39b AC |
1136 | then |
1137 | -- The call we generate is: | |
1138 | ||
1139 | -- Compare_Array_xn[_Unaligned] | |
1140 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
1141 | ||
1142 | -- x = U for unsigned, S for signed | |
1143 | -- n = 8,16,32,64 for component size | |
1144 | -- Add _Unaligned if length < 4 and component size is 8. | |
1145 | -- <op> is the standard comparison operator | |
1146 | ||
1147 | if Component_Size (Typ1) = 8 then | |
1148 | if Length_Less_Than_4 (Op1) | |
1149 | or else | |
1150 | Length_Less_Than_4 (Op2) | |
1151 | then | |
1152 | if Is_Unsigned_Type (Ctyp) then | |
1153 | Comp := RE_Compare_Array_U8_Unaligned; | |
1154 | else | |
1155 | Comp := RE_Compare_Array_S8_Unaligned; | |
1156 | end if; | |
1157 | ||
1158 | else | |
1159 | if Is_Unsigned_Type (Ctyp) then | |
1160 | Comp := RE_Compare_Array_U8; | |
1161 | else | |
1162 | Comp := RE_Compare_Array_S8; | |
1163 | end if; | |
1164 | end if; | |
1165 | ||
1166 | elsif Component_Size (Typ1) = 16 then | |
1167 | if Is_Unsigned_Type (Ctyp) then | |
1168 | Comp := RE_Compare_Array_U16; | |
1169 | else | |
1170 | Comp := RE_Compare_Array_S16; | |
1171 | end if; | |
1172 | ||
1173 | elsif Component_Size (Typ1) = 32 then | |
1174 | if Is_Unsigned_Type (Ctyp) then | |
1175 | Comp := RE_Compare_Array_U32; | |
1176 | else | |
1177 | Comp := RE_Compare_Array_S32; | |
1178 | end if; | |
1179 | ||
1180 | else pragma Assert (Component_Size (Typ1) = 64); | |
1181 | if Is_Unsigned_Type (Ctyp) then | |
1182 | Comp := RE_Compare_Array_U64; | |
1183 | else | |
1184 | Comp := RE_Compare_Array_S64; | |
1185 | end if; | |
1186 | end if; | |
1187 | ||
1188 | Remove_Side_Effects (Op1, Name_Req => True); | |
1189 | Remove_Side_Effects (Op2, Name_Req => True); | |
1190 | ||
1191 | Rewrite (Op1, | |
1192 | Make_Function_Call (Sloc (Op1), | |
1193 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
1194 | ||
1195 | Parameter_Associations => New_List ( | |
1196 | Make_Attribute_Reference (Loc, | |
1197 | Prefix => Relocate_Node (Op1), | |
1198 | Attribute_Name => Name_Address), | |
1199 | ||
1200 | Make_Attribute_Reference (Loc, | |
1201 | Prefix => Relocate_Node (Op2), | |
1202 | Attribute_Name => Name_Address), | |
1203 | ||
1204 | Make_Attribute_Reference (Loc, | |
1205 | Prefix => Relocate_Node (Op1), | |
1206 | Attribute_Name => Name_Length), | |
1207 | ||
1208 | Make_Attribute_Reference (Loc, | |
1209 | Prefix => Relocate_Node (Op2), | |
1210 | Attribute_Name => Name_Length)))); | |
1211 | ||
1212 | Rewrite (Op2, | |
1213 | Make_Integer_Literal (Sloc (Op2), | |
1214 | Intval => Uint_0)); | |
1215 | ||
1216 | Analyze_And_Resolve (Op1, Standard_Integer); | |
1217 | Analyze_And_Resolve (Op2, Standard_Integer); | |
1218 | return; | |
1219 | end if; | |
1220 | ||
1221 | -- Cases where we cannot make runtime call | |
1222 | ||
70482933 RK |
1223 | -- For (a <= b) we convert to not (a > b) |
1224 | ||
1225 | if Chars (N) = Name_Op_Le then | |
1226 | Rewrite (N, | |
1227 | Make_Op_Not (Loc, | |
1228 | Right_Opnd => | |
1229 | Make_Op_Gt (Loc, | |
1230 | Left_Opnd => Op1, | |
1231 | Right_Opnd => Op2))); | |
1232 | Analyze_And_Resolve (N, Standard_Boolean); | |
1233 | return; | |
1234 | ||
1235 | -- For < the Boolean expression is | |
1236 | -- greater__nn (op2, op1) | |
1237 | ||
1238 | elsif Chars (N) = Name_Op_Lt then | |
1239 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1240 | ||
1241 | -- Switch operands | |
1242 | ||
1243 | Op1 := Right_Opnd (N); | |
1244 | Op2 := Left_Opnd (N); | |
1245 | ||
1246 | -- For (a >= b) we convert to not (a < b) | |
1247 | ||
1248 | elsif Chars (N) = Name_Op_Ge then | |
1249 | Rewrite (N, | |
1250 | Make_Op_Not (Loc, | |
1251 | Right_Opnd => | |
1252 | Make_Op_Lt (Loc, | |
1253 | Left_Opnd => Op1, | |
1254 | Right_Opnd => Op2))); | |
1255 | Analyze_And_Resolve (N, Standard_Boolean); | |
1256 | return; | |
1257 | ||
1258 | -- For > the Boolean expression is | |
1259 | -- greater__nn (op1, op2) | |
1260 | ||
1261 | else | |
1262 | pragma Assert (Chars (N) = Name_Op_Gt); | |
1263 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1264 | end if; | |
1265 | ||
1266 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1267 | Expr := | |
1268 | Make_Function_Call (Loc, | |
1269 | Name => New_Reference_To (Func_Name, Loc), | |
1270 | Parameter_Associations => New_List (Op1, Op2)); | |
1271 | ||
1272 | Insert_Action (N, Func_Body); | |
1273 | Rewrite (N, Expr); | |
1274 | Analyze_And_Resolve (N, Standard_Boolean); | |
1275 | ||
fbf5a39b AC |
1276 | exception |
1277 | when RE_Not_Available => | |
1278 | return; | |
70482933 RK |
1279 | end Expand_Array_Comparison; |
1280 | ||
1281 | --------------------------- | |
1282 | -- Expand_Array_Equality -- | |
1283 | --------------------------- | |
1284 | ||
685094bf RD |
1285 | -- Expand an equality function for multi-dimensional arrays. Here is an |
1286 | -- example of such a function for Nb_Dimension = 2 | |
70482933 | 1287 | |
0da2c8ac | 1288 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 1289 | -- begin |
fbf5a39b AC |
1290 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
1291 | -- and then | |
1292 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
1293 | -- then | |
1294 | -- return True; -- RM 4.5.2(22) | |
1295 | -- end if; | |
0da2c8ac | 1296 | |
fbf5a39b AC |
1297 | -- if A'length (1) /= B'length (1) |
1298 | -- or else | |
1299 | -- A'length (2) /= B'length (2) | |
1300 | -- then | |
1301 | -- return False; -- RM 4.5.2(23) | |
1302 | -- end if; | |
0da2c8ac | 1303 | |
fbf5a39b | 1304 | -- declare |
523456db AC |
1305 | -- A1 : Index_T1 := A'first (1); |
1306 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 1307 | -- begin |
523456db | 1308 | -- loop |
fbf5a39b | 1309 | -- declare |
523456db AC |
1310 | -- A2 : Index_T2 := A'first (2); |
1311 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 1312 | -- begin |
523456db | 1313 | -- loop |
fbf5a39b AC |
1314 | -- if A (A1, A2) /= B (B1, B2) then |
1315 | -- return False; | |
70482933 | 1316 | -- end if; |
0da2c8ac | 1317 | |
523456db AC |
1318 | -- exit when A2 = A'last (2); |
1319 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 1320 | -- B2 := Index_T2'succ (B2); |
70482933 | 1321 | -- end loop; |
fbf5a39b | 1322 | -- end; |
0da2c8ac | 1323 | |
523456db AC |
1324 | -- exit when A1 = A'last (1); |
1325 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 1326 | -- B1 := Index_T1'succ (B1); |
70482933 | 1327 | -- end loop; |
fbf5a39b | 1328 | -- end; |
0da2c8ac | 1329 | |
70482933 RK |
1330 | -- return true; |
1331 | -- end Enn; | |
1332 | ||
685094bf RD |
1333 | -- Note on the formal types used (atyp and btyp). If either of the arrays |
1334 | -- is of a private type, we use the underlying type, and do an unchecked | |
1335 | -- conversion of the actual. If either of the arrays has a bound depending | |
1336 | -- on a discriminant, then we use the base type since otherwise we have an | |
1337 | -- escaped discriminant in the function. | |
0da2c8ac | 1338 | |
685094bf RD |
1339 | -- If both arrays are constrained and have the same bounds, we can generate |
1340 | -- a loop with an explicit iteration scheme using a 'Range attribute over | |
1341 | -- the first array. | |
523456db | 1342 | |
70482933 RK |
1343 | function Expand_Array_Equality |
1344 | (Nod : Node_Id; | |
70482933 RK |
1345 | Lhs : Node_Id; |
1346 | Rhs : Node_Id; | |
0da2c8ac AC |
1347 | Bodies : List_Id; |
1348 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
1349 | is |
1350 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
1351 | Decls : constant List_Id := New_List; |
1352 | Index_List1 : constant List_Id := New_List; | |
1353 | Index_List2 : constant List_Id := New_List; | |
1354 | ||
1355 | Actuals : List_Id; | |
1356 | Formals : List_Id; | |
1357 | Func_Name : Entity_Id; | |
1358 | Func_Body : Node_Id; | |
70482933 RK |
1359 | |
1360 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
1361 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
1362 | ||
0da2c8ac AC |
1363 | Ltyp : Entity_Id; |
1364 | Rtyp : Entity_Id; | |
1365 | -- The parameter types to be used for the formals | |
1366 | ||
fbf5a39b AC |
1367 | function Arr_Attr |
1368 | (Arr : Entity_Id; | |
1369 | Nam : Name_Id; | |
2e071734 | 1370 | Num : Int) return Node_Id; |
5e1c00fa | 1371 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 1372 | |
70482933 | 1373 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
685094bf RD |
1374 | -- Create one statement to compare corresponding components, designated |
1375 | -- by a full set of indices. | |
70482933 | 1376 | |
0da2c8ac | 1377 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
685094bf RD |
1378 | -- Given one of the arguments, computes the appropriate type to be used |
1379 | -- for that argument in the corresponding function formal | |
0da2c8ac | 1380 | |
fbf5a39b | 1381 | function Handle_One_Dimension |
70482933 | 1382 | (N : Int; |
2e071734 | 1383 | Index : Node_Id) return Node_Id; |
0da2c8ac | 1384 | -- This procedure returns the following code |
fbf5a39b AC |
1385 | -- |
1386 | -- declare | |
523456db | 1387 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 1388 | -- begin |
523456db | 1389 | -- loop |
fbf5a39b | 1390 | -- xxx |
523456db AC |
1391 | -- exit when An = A'Last (N); |
1392 | -- An := Index_T'Succ (An) | |
0da2c8ac | 1393 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
1394 | -- end loop; |
1395 | -- end; | |
1396 | -- | |
523456db AC |
1397 | -- If both indices are constrained and identical, the procedure |
1398 | -- returns a simpler loop: | |
1399 | -- | |
1400 | -- for An in A'Range (N) loop | |
1401 | -- xxx | |
1402 | -- end loop | |
0da2c8ac | 1403 | -- |
523456db | 1404 | -- N is the dimension for which we are generating a loop. Index is the |
685094bf RD |
1405 | -- N'th index node, whose Etype is Index_Type_n in the above code. The |
1406 | -- xxx statement is either the loop or declare for the next dimension | |
1407 | -- or if this is the last dimension the comparison of corresponding | |
1408 | -- components of the arrays. | |
fbf5a39b | 1409 | -- |
685094bf RD |
1410 | -- The actual way the code works is to return the comparison of |
1411 | -- corresponding components for the N+1 call. That's neater! | |
fbf5a39b AC |
1412 | |
1413 | function Test_Empty_Arrays return Node_Id; | |
1414 | -- This function constructs the test for both arrays being empty | |
1415 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1416 | -- and then | |
1417 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1418 | ||
1419 | function Test_Lengths_Correspond return Node_Id; | |
685094bf RD |
1420 | -- This function constructs the test for arrays having different lengths |
1421 | -- in at least one index position, in which case the resulting code is: | |
fbf5a39b AC |
1422 | |
1423 | -- A'length (1) /= B'length (1) | |
1424 | -- or else | |
1425 | -- A'length (2) /= B'length (2) | |
1426 | -- or else | |
1427 | -- ... | |
1428 | ||
1429 | -------------- | |
1430 | -- Arr_Attr -- | |
1431 | -------------- | |
1432 | ||
1433 | function Arr_Attr | |
1434 | (Arr : Entity_Id; | |
1435 | Nam : Name_Id; | |
2e071734 | 1436 | Num : Int) return Node_Id |
fbf5a39b AC |
1437 | is |
1438 | begin | |
1439 | return | |
1440 | Make_Attribute_Reference (Loc, | |
1441 | Attribute_Name => Nam, | |
1442 | Prefix => New_Reference_To (Arr, Loc), | |
1443 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
1444 | end Arr_Attr; | |
70482933 RK |
1445 | |
1446 | ------------------------ | |
1447 | -- Component_Equality -- | |
1448 | ------------------------ | |
1449 | ||
1450 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1451 | Test : Node_Id; | |
1452 | L, R : Node_Id; | |
1453 | ||
1454 | begin | |
1455 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1456 | ||
1457 | L := | |
1458 | Make_Indexed_Component (Loc, | |
1459 | Prefix => Make_Identifier (Loc, Chars (A)), | |
1460 | Expressions => Index_List1); | |
1461 | ||
1462 | R := | |
1463 | Make_Indexed_Component (Loc, | |
1464 | Prefix => Make_Identifier (Loc, Chars (B)), | |
1465 | Expressions => Index_List2); | |
1466 | ||
1467 | Test := Expand_Composite_Equality | |
1468 | (Nod, Component_Type (Typ), L, R, Decls); | |
1469 | ||
a9d8907c JM |
1470 | -- If some (sub)component is an unchecked_union, the whole operation |
1471 | -- will raise program error. | |
8aceda64 AC |
1472 | |
1473 | if Nkind (Test) = N_Raise_Program_Error then | |
a9d8907c JM |
1474 | |
1475 | -- This node is going to be inserted at a location where a | |
685094bf RD |
1476 | -- statement is expected: clear its Etype so analysis will set |
1477 | -- it to the expected Standard_Void_Type. | |
a9d8907c JM |
1478 | |
1479 | Set_Etype (Test, Empty); | |
8aceda64 AC |
1480 | return Test; |
1481 | ||
1482 | else | |
1483 | return | |
1484 | Make_Implicit_If_Statement (Nod, | |
1485 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), | |
1486 | Then_Statements => New_List ( | |
d766cee3 | 1487 | Make_Simple_Return_Statement (Loc, |
8aceda64 AC |
1488 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
1489 | end if; | |
70482933 RK |
1490 | end Component_Equality; |
1491 | ||
0da2c8ac AC |
1492 | ------------------ |
1493 | -- Get_Arg_Type -- | |
1494 | ------------------ | |
1495 | ||
1496 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1497 | T : Entity_Id; | |
1498 | X : Node_Id; | |
1499 | ||
1500 | begin | |
1501 | T := Etype (N); | |
1502 | ||
1503 | if No (T) then | |
1504 | return Typ; | |
1505 | ||
1506 | else | |
1507 | T := Underlying_Type (T); | |
1508 | ||
1509 | X := First_Index (T); | |
1510 | while Present (X) loop | |
1511 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) | |
1512 | or else | |
1513 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
1514 | then | |
1515 | T := Base_Type (T); | |
1516 | exit; | |
1517 | end if; | |
1518 | ||
1519 | Next_Index (X); | |
1520 | end loop; | |
1521 | ||
1522 | return T; | |
1523 | end if; | |
1524 | end Get_Arg_Type; | |
1525 | ||
fbf5a39b AC |
1526 | -------------------------- |
1527 | -- Handle_One_Dimension -- | |
1528 | --------------------------- | |
70482933 | 1529 | |
fbf5a39b | 1530 | function Handle_One_Dimension |
70482933 | 1531 | (N : Int; |
2e071734 | 1532 | Index : Node_Id) return Node_Id |
70482933 | 1533 | is |
0da2c8ac AC |
1534 | Need_Separate_Indexes : constant Boolean := |
1535 | Ltyp /= Rtyp | |
1536 | or else not Is_Constrained (Ltyp); | |
1537 | -- If the index types are identical, and we are working with | |
685094bf RD |
1538 | -- constrained types, then we can use the same index for both |
1539 | -- of the arrays. | |
0da2c8ac | 1540 | |
fbf5a39b AC |
1541 | An : constant Entity_Id := Make_Defining_Identifier (Loc, |
1542 | Chars => New_Internal_Name ('A')); | |
0da2c8ac AC |
1543 | |
1544 | Bn : Entity_Id; | |
1545 | Index_T : Entity_Id; | |
1546 | Stm_List : List_Id; | |
1547 | Loop_Stm : Node_Id; | |
70482933 RK |
1548 | |
1549 | begin | |
0da2c8ac AC |
1550 | if N > Number_Dimensions (Ltyp) then |
1551 | return Component_Equality (Ltyp); | |
fbf5a39b | 1552 | end if; |
70482933 | 1553 | |
0da2c8ac AC |
1554 | -- Case where we generate a loop |
1555 | ||
1556 | Index_T := Base_Type (Etype (Index)); | |
1557 | ||
1558 | if Need_Separate_Indexes then | |
1559 | Bn := | |
1560 | Make_Defining_Identifier (Loc, | |
1561 | Chars => New_Internal_Name ('B')); | |
1562 | else | |
1563 | Bn := An; | |
1564 | end if; | |
70482933 | 1565 | |
fbf5a39b AC |
1566 | Append (New_Reference_To (An, Loc), Index_List1); |
1567 | Append (New_Reference_To (Bn, Loc), Index_List2); | |
70482933 | 1568 | |
0da2c8ac AC |
1569 | Stm_List := New_List ( |
1570 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1571 | |
0da2c8ac | 1572 | if Need_Separate_Indexes then |
a9d8907c | 1573 | |
5e1c00fa | 1574 | -- Generate guard for loop, followed by increments of indices |
523456db AC |
1575 | |
1576 | Append_To (Stm_List, | |
1577 | Make_Exit_Statement (Loc, | |
1578 | Condition => | |
1579 | Make_Op_Eq (Loc, | |
1580 | Left_Opnd => New_Reference_To (An, Loc), | |
1581 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); | |
1582 | ||
1583 | Append_To (Stm_List, | |
1584 | Make_Assignment_Statement (Loc, | |
1585 | Name => New_Reference_To (An, Loc), | |
1586 | Expression => | |
1587 | Make_Attribute_Reference (Loc, | |
1588 | Prefix => New_Reference_To (Index_T, Loc), | |
1589 | Attribute_Name => Name_Succ, | |
1590 | Expressions => New_List (New_Reference_To (An, Loc))))); | |
1591 | ||
0da2c8ac AC |
1592 | Append_To (Stm_List, |
1593 | Make_Assignment_Statement (Loc, | |
1594 | Name => New_Reference_To (Bn, Loc), | |
1595 | Expression => | |
1596 | Make_Attribute_Reference (Loc, | |
1597 | Prefix => New_Reference_To (Index_T, Loc), | |
1598 | Attribute_Name => Name_Succ, | |
1599 | Expressions => New_List (New_Reference_To (Bn, Loc))))); | |
1600 | end if; | |
1601 | ||
a9d8907c JM |
1602 | -- If separate indexes, we need a declare block for An and Bn, and a |
1603 | -- loop without an iteration scheme. | |
0da2c8ac AC |
1604 | |
1605 | if Need_Separate_Indexes then | |
523456db AC |
1606 | Loop_Stm := |
1607 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
1608 | ||
0da2c8ac AC |
1609 | return |
1610 | Make_Block_Statement (Loc, | |
1611 | Declarations => New_List ( | |
523456db AC |
1612 | Make_Object_Declaration (Loc, |
1613 | Defining_Identifier => An, | |
1614 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1615 | Expression => Arr_Attr (A, Name_First, N)), | |
1616 | ||
0da2c8ac AC |
1617 | Make_Object_Declaration (Loc, |
1618 | Defining_Identifier => Bn, | |
1619 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1620 | Expression => Arr_Attr (B, Name_First, N))), | |
523456db | 1621 | |
0da2c8ac AC |
1622 | Handled_Statement_Sequence => |
1623 | Make_Handled_Sequence_Of_Statements (Loc, | |
1624 | Statements => New_List (Loop_Stm))); | |
1625 | ||
523456db AC |
1626 | -- If no separate indexes, return loop statement with explicit |
1627 | -- iteration scheme on its own | |
0da2c8ac AC |
1628 | |
1629 | else | |
523456db AC |
1630 | Loop_Stm := |
1631 | Make_Implicit_Loop_Statement (Nod, | |
1632 | Statements => Stm_List, | |
1633 | Iteration_Scheme => | |
1634 | Make_Iteration_Scheme (Loc, | |
1635 | Loop_Parameter_Specification => | |
1636 | Make_Loop_Parameter_Specification (Loc, | |
1637 | Defining_Identifier => An, | |
1638 | Discrete_Subtype_Definition => | |
1639 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
1640 | return Loop_Stm; |
1641 | end if; | |
fbf5a39b AC |
1642 | end Handle_One_Dimension; |
1643 | ||
1644 | ----------------------- | |
1645 | -- Test_Empty_Arrays -- | |
1646 | ----------------------- | |
1647 | ||
1648 | function Test_Empty_Arrays return Node_Id is | |
1649 | Alist : Node_Id; | |
1650 | Blist : Node_Id; | |
1651 | ||
1652 | Atest : Node_Id; | |
1653 | Btest : Node_Id; | |
70482933 | 1654 | |
fbf5a39b AC |
1655 | begin |
1656 | Alist := Empty; | |
1657 | Blist := Empty; | |
0da2c8ac | 1658 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1659 | Atest := |
1660 | Make_Op_Eq (Loc, | |
1661 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1662 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1663 | ||
1664 | Btest := | |
1665 | Make_Op_Eq (Loc, | |
1666 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
1667 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1668 | ||
1669 | if No (Alist) then | |
1670 | Alist := Atest; | |
1671 | Blist := Btest; | |
70482933 | 1672 | |
fbf5a39b AC |
1673 | else |
1674 | Alist := | |
1675 | Make_Or_Else (Loc, | |
1676 | Left_Opnd => Relocate_Node (Alist), | |
1677 | Right_Opnd => Atest); | |
1678 | ||
1679 | Blist := | |
1680 | Make_Or_Else (Loc, | |
1681 | Left_Opnd => Relocate_Node (Blist), | |
1682 | Right_Opnd => Btest); | |
1683 | end if; | |
1684 | end loop; | |
70482933 | 1685 | |
fbf5a39b AC |
1686 | return |
1687 | Make_And_Then (Loc, | |
1688 | Left_Opnd => Alist, | |
1689 | Right_Opnd => Blist); | |
1690 | end Test_Empty_Arrays; | |
70482933 | 1691 | |
fbf5a39b AC |
1692 | ----------------------------- |
1693 | -- Test_Lengths_Correspond -- | |
1694 | ----------------------------- | |
70482933 | 1695 | |
fbf5a39b AC |
1696 | function Test_Lengths_Correspond return Node_Id is |
1697 | Result : Node_Id; | |
1698 | Rtest : Node_Id; | |
1699 | ||
1700 | begin | |
1701 | Result := Empty; | |
0da2c8ac | 1702 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1703 | Rtest := |
1704 | Make_Op_Ne (Loc, | |
1705 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1706 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
1707 | ||
1708 | if No (Result) then | |
1709 | Result := Rtest; | |
1710 | else | |
1711 | Result := | |
1712 | Make_Or_Else (Loc, | |
1713 | Left_Opnd => Relocate_Node (Result), | |
1714 | Right_Opnd => Rtest); | |
1715 | end if; | |
1716 | end loop; | |
1717 | ||
1718 | return Result; | |
1719 | end Test_Lengths_Correspond; | |
70482933 RK |
1720 | |
1721 | -- Start of processing for Expand_Array_Equality | |
1722 | ||
1723 | begin | |
0da2c8ac AC |
1724 | Ltyp := Get_Arg_Type (Lhs); |
1725 | Rtyp := Get_Arg_Type (Rhs); | |
1726 | ||
685094bf RD |
1727 | -- For now, if the argument types are not the same, go to the base type, |
1728 | -- since the code assumes that the formals have the same type. This is | |
1729 | -- fixable in future ??? | |
0da2c8ac AC |
1730 | |
1731 | if Ltyp /= Rtyp then | |
1732 | Ltyp := Base_Type (Ltyp); | |
1733 | Rtyp := Base_Type (Rtyp); | |
1734 | pragma Assert (Ltyp = Rtyp); | |
1735 | end if; | |
1736 | ||
1737 | -- Build list of formals for function | |
1738 | ||
70482933 RK |
1739 | Formals := New_List ( |
1740 | Make_Parameter_Specification (Loc, | |
1741 | Defining_Identifier => A, | |
0da2c8ac | 1742 | Parameter_Type => New_Reference_To (Ltyp, Loc)), |
70482933 RK |
1743 | |
1744 | Make_Parameter_Specification (Loc, | |
1745 | Defining_Identifier => B, | |
0da2c8ac | 1746 | Parameter_Type => New_Reference_To (Rtyp, Loc))); |
70482933 RK |
1747 | |
1748 | Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E')); | |
1749 | ||
fbf5a39b | 1750 | -- Build statement sequence for function |
70482933 RK |
1751 | |
1752 | Func_Body := | |
1753 | Make_Subprogram_Body (Loc, | |
1754 | Specification => | |
1755 | Make_Function_Specification (Loc, | |
1756 | Defining_Unit_Name => Func_Name, | |
1757 | Parameter_Specifications => Formals, | |
630d30e9 | 1758 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
fbf5a39b AC |
1759 | |
1760 | Declarations => Decls, | |
1761 | ||
70482933 RK |
1762 | Handled_Statement_Sequence => |
1763 | Make_Handled_Sequence_Of_Statements (Loc, | |
1764 | Statements => New_List ( | |
fbf5a39b AC |
1765 | |
1766 | Make_Implicit_If_Statement (Nod, | |
1767 | Condition => Test_Empty_Arrays, | |
1768 | Then_Statements => New_List ( | |
d766cee3 | 1769 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
1770 | Expression => |
1771 | New_Occurrence_Of (Standard_True, Loc)))), | |
1772 | ||
1773 | Make_Implicit_If_Statement (Nod, | |
1774 | Condition => Test_Lengths_Correspond, | |
1775 | Then_Statements => New_List ( | |
d766cee3 | 1776 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
1777 | Expression => |
1778 | New_Occurrence_Of (Standard_False, Loc)))), | |
1779 | ||
0da2c8ac | 1780 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 1781 | |
d766cee3 | 1782 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
1783 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
1784 | ||
1785 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 1786 | Set_Is_Inlined (Func_Name); |
70482933 | 1787 | |
685094bf RD |
1788 | -- If the array type is distinct from the type of the arguments, it |
1789 | -- is the full view of a private type. Apply an unchecked conversion | |
1790 | -- to insure that analysis of the call succeeds. | |
70482933 | 1791 | |
0da2c8ac AC |
1792 | declare |
1793 | L, R : Node_Id; | |
1794 | ||
1795 | begin | |
1796 | L := Lhs; | |
1797 | R := Rhs; | |
1798 | ||
1799 | if No (Etype (Lhs)) | |
1800 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
1801 | then | |
1802 | L := OK_Convert_To (Ltyp, Lhs); | |
1803 | end if; | |
1804 | ||
1805 | if No (Etype (Rhs)) | |
1806 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
1807 | then | |
1808 | R := OK_Convert_To (Rtyp, Rhs); | |
1809 | end if; | |
1810 | ||
1811 | Actuals := New_List (L, R); | |
1812 | end; | |
70482933 RK |
1813 | |
1814 | Append_To (Bodies, Func_Body); | |
1815 | ||
1816 | return | |
1817 | Make_Function_Call (Loc, | |
0da2c8ac | 1818 | Name => New_Reference_To (Func_Name, Loc), |
70482933 RK |
1819 | Parameter_Associations => Actuals); |
1820 | end Expand_Array_Equality; | |
1821 | ||
1822 | ----------------------------- | |
1823 | -- Expand_Boolean_Operator -- | |
1824 | ----------------------------- | |
1825 | ||
685094bf RD |
1826 | -- Note that we first get the actual subtypes of the operands, since we |
1827 | -- always want to deal with types that have bounds. | |
70482933 RK |
1828 | |
1829 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 1830 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
1831 | |
1832 | begin | |
685094bf RD |
1833 | -- Special case of bit packed array where both operands are known to be |
1834 | -- properly aligned. In this case we use an efficient run time routine | |
1835 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
1836 | |
1837 | if Is_Bit_Packed_Array (Typ) | |
1838 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
1839 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
1840 | then | |
70482933 | 1841 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
1842 | return; |
1843 | end if; | |
70482933 | 1844 | |
a9d8907c JM |
1845 | -- For the normal non-packed case, the general expansion is to build |
1846 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
1847 | -- and then inserting it into the tree. The original operator node is | |
1848 | -- then rewritten as a call to this function. We also use this in the | |
1849 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 1850 | |
a9d8907c JM |
1851 | declare |
1852 | Loc : constant Source_Ptr := Sloc (N); | |
1853 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
1854 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
1855 | Func_Body : Node_Id; | |
1856 | Func_Name : Entity_Id; | |
fbf5a39b | 1857 | |
a9d8907c JM |
1858 | begin |
1859 | Convert_To_Actual_Subtype (L); | |
1860 | Convert_To_Actual_Subtype (R); | |
1861 | Ensure_Defined (Etype (L), N); | |
1862 | Ensure_Defined (Etype (R), N); | |
1863 | Apply_Length_Check (R, Etype (L)); | |
1864 | ||
b4592168 GD |
1865 | if Nkind (N) = N_Op_Xor then |
1866 | Silly_Boolean_Array_Xor_Test (N, Etype (L)); | |
1867 | end if; | |
1868 | ||
a9d8907c JM |
1869 | if Nkind (Parent (N)) = N_Assignment_Statement |
1870 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
1871 | then | |
1872 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 1873 | |
a9d8907c JM |
1874 | elsif Nkind (Parent (N)) = N_Op_Not |
1875 | and then Nkind (N) = N_Op_And | |
1876 | and then | |
b4592168 | 1877 | Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
1878 | then |
1879 | return; | |
1880 | else | |
fbf5a39b | 1881 | |
a9d8907c JM |
1882 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
1883 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1884 | Insert_Action (N, Func_Body); | |
70482933 | 1885 | |
a9d8907c | 1886 | -- Now rewrite the expression with a call |
70482933 | 1887 | |
a9d8907c JM |
1888 | Rewrite (N, |
1889 | Make_Function_Call (Loc, | |
1890 | Name => New_Reference_To (Func_Name, Loc), | |
1891 | Parameter_Associations => | |
1892 | New_List ( | |
1893 | L, | |
1894 | Make_Type_Conversion | |
1895 | (Loc, New_Reference_To (Etype (L), Loc), R)))); | |
70482933 | 1896 | |
a9d8907c JM |
1897 | Analyze_And_Resolve (N, Typ); |
1898 | end if; | |
1899 | end; | |
70482933 RK |
1900 | end Expand_Boolean_Operator; |
1901 | ||
1902 | ------------------------------- | |
1903 | -- Expand_Composite_Equality -- | |
1904 | ------------------------------- | |
1905 | ||
1906 | -- This function is only called for comparing internal fields of composite | |
1907 | -- types when these fields are themselves composites. This is a special | |
1908 | -- case because it is not possible to respect normal Ada visibility rules. | |
1909 | ||
1910 | function Expand_Composite_Equality | |
1911 | (Nod : Node_Id; | |
1912 | Typ : Entity_Id; | |
1913 | Lhs : Node_Id; | |
1914 | Rhs : Node_Id; | |
2e071734 | 1915 | Bodies : List_Id) return Node_Id |
70482933 RK |
1916 | is |
1917 | Loc : constant Source_Ptr := Sloc (Nod); | |
1918 | Full_Type : Entity_Id; | |
1919 | Prim : Elmt_Id; | |
1920 | Eq_Op : Entity_Id; | |
1921 | ||
1922 | begin | |
1923 | if Is_Private_Type (Typ) then | |
1924 | Full_Type := Underlying_Type (Typ); | |
1925 | else | |
1926 | Full_Type := Typ; | |
1927 | end if; | |
1928 | ||
685094bf RD |
1929 | -- Defense against malformed private types with no completion the error |
1930 | -- will be diagnosed later by check_completion | |
70482933 RK |
1931 | |
1932 | if No (Full_Type) then | |
1933 | return New_Reference_To (Standard_False, Loc); | |
1934 | end if; | |
1935 | ||
1936 | Full_Type := Base_Type (Full_Type); | |
1937 | ||
1938 | if Is_Array_Type (Full_Type) then | |
1939 | ||
1940 | -- If the operand is an elementary type other than a floating-point | |
1941 | -- type, then we can simply use the built-in block bitwise equality, | |
1942 | -- since the predefined equality operators always apply and bitwise | |
1943 | -- equality is fine for all these cases. | |
1944 | ||
1945 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
1946 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
1947 | then | |
1948 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
1949 | ||
685094bf RD |
1950 | -- For composite component types, and floating-point types, use the |
1951 | -- expansion. This deals with tagged component types (where we use | |
1952 | -- the applicable equality routine) and floating-point, (where we | |
1953 | -- need to worry about negative zeroes), and also the case of any | |
1954 | -- composite type recursively containing such fields. | |
70482933 RK |
1955 | |
1956 | else | |
0da2c8ac | 1957 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
1958 | end if; |
1959 | ||
1960 | elsif Is_Tagged_Type (Full_Type) then | |
1961 | ||
1962 | -- Call the primitive operation "=" of this type | |
1963 | ||
1964 | if Is_Class_Wide_Type (Full_Type) then | |
1965 | Full_Type := Root_Type (Full_Type); | |
1966 | end if; | |
1967 | ||
685094bf RD |
1968 | -- If this is derived from an untagged private type completed with a |
1969 | -- tagged type, it does not have a full view, so we use the primitive | |
1970 | -- operations of the private type. This check should no longer be | |
1971 | -- necessary when these types receive their full views ??? | |
70482933 RK |
1972 | |
1973 | if Is_Private_Type (Typ) | |
1974 | and then not Is_Tagged_Type (Typ) | |
1975 | and then not Is_Controlled (Typ) | |
1976 | and then Is_Derived_Type (Typ) | |
1977 | and then No (Full_View (Typ)) | |
1978 | then | |
1979 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
1980 | else | |
1981 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
1982 | end if; | |
1983 | ||
1984 | loop | |
1985 | Eq_Op := Node (Prim); | |
1986 | exit when Chars (Eq_Op) = Name_Op_Eq | |
1987 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
1988 | Etype (Next_Formal (First_Formal (Eq_Op))) |
1989 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
1990 | Next_Elmt (Prim); |
1991 | pragma Assert (Present (Prim)); | |
1992 | end loop; | |
1993 | ||
1994 | Eq_Op := Node (Prim); | |
1995 | ||
1996 | return | |
1997 | Make_Function_Call (Loc, | |
1998 | Name => New_Reference_To (Eq_Op, Loc), | |
1999 | Parameter_Associations => | |
2000 | New_List | |
2001 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2002 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2003 | ||
2004 | elsif Is_Record_Type (Full_Type) then | |
fbf5a39b | 2005 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2006 | |
2007 | if Present (Eq_Op) then | |
2008 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2009 | ||
685094bf RD |
2010 | -- Inherited equality from parent type. Convert the actuals to |
2011 | -- match signature of operation. | |
70482933 RK |
2012 | |
2013 | declare | |
fbf5a39b | 2014 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2015 | |
2016 | begin | |
2017 | return | |
2018 | Make_Function_Call (Loc, | |
2019 | Name => New_Reference_To (Eq_Op, Loc), | |
2020 | Parameter_Associations => | |
2021 | New_List (OK_Convert_To (T, Lhs), | |
2022 | OK_Convert_To (T, Rhs))); | |
2023 | end; | |
2024 | ||
2025 | else | |
5d09245e AC |
2026 | -- Comparison between Unchecked_Union components |
2027 | ||
2028 | if Is_Unchecked_Union (Full_Type) then | |
2029 | declare | |
2030 | Lhs_Type : Node_Id := Full_Type; | |
2031 | Rhs_Type : Node_Id := Full_Type; | |
2032 | Lhs_Discr_Val : Node_Id; | |
2033 | Rhs_Discr_Val : Node_Id; | |
2034 | ||
2035 | begin | |
2036 | -- Lhs subtype | |
2037 | ||
2038 | if Nkind (Lhs) = N_Selected_Component then | |
2039 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2040 | end if; | |
2041 | ||
2042 | -- Rhs subtype | |
2043 | ||
2044 | if Nkind (Rhs) = N_Selected_Component then | |
2045 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2046 | end if; | |
2047 | ||
2048 | -- Lhs of the composite equality | |
2049 | ||
2050 | if Is_Constrained (Lhs_Type) then | |
2051 | ||
685094bf | 2052 | -- Since the enclosing record type can never be an |
5d09245e AC |
2053 | -- Unchecked_Union (this code is executed for records |
2054 | -- that do not have variants), we may reference its | |
2055 | -- discriminant(s). | |
2056 | ||
2057 | if Nkind (Lhs) = N_Selected_Component | |
2058 | and then Has_Per_Object_Constraint ( | |
2059 | Entity (Selector_Name (Lhs))) | |
2060 | then | |
2061 | Lhs_Discr_Val := | |
2062 | Make_Selected_Component (Loc, | |
2063 | Prefix => Prefix (Lhs), | |
2064 | Selector_Name => | |
2065 | New_Copy ( | |
2066 | Get_Discriminant_Value ( | |
2067 | First_Discriminant (Lhs_Type), | |
2068 | Lhs_Type, | |
2069 | Stored_Constraint (Lhs_Type)))); | |
2070 | ||
2071 | else | |
2072 | Lhs_Discr_Val := New_Copy ( | |
2073 | Get_Discriminant_Value ( | |
2074 | First_Discriminant (Lhs_Type), | |
2075 | Lhs_Type, | |
2076 | Stored_Constraint (Lhs_Type))); | |
2077 | ||
2078 | end if; | |
2079 | else | |
2080 | -- It is not possible to infer the discriminant since | |
2081 | -- the subtype is not constrained. | |
2082 | ||
8aceda64 | 2083 | return |
5d09245e | 2084 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2085 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2086 | end if; |
2087 | ||
2088 | -- Rhs of the composite equality | |
2089 | ||
2090 | if Is_Constrained (Rhs_Type) then | |
2091 | if Nkind (Rhs) = N_Selected_Component | |
2092 | and then Has_Per_Object_Constraint ( | |
2093 | Entity (Selector_Name (Rhs))) | |
2094 | then | |
2095 | Rhs_Discr_Val := | |
2096 | Make_Selected_Component (Loc, | |
2097 | Prefix => Prefix (Rhs), | |
2098 | Selector_Name => | |
2099 | New_Copy ( | |
2100 | Get_Discriminant_Value ( | |
2101 | First_Discriminant (Rhs_Type), | |
2102 | Rhs_Type, | |
2103 | Stored_Constraint (Rhs_Type)))); | |
2104 | ||
2105 | else | |
2106 | Rhs_Discr_Val := New_Copy ( | |
2107 | Get_Discriminant_Value ( | |
2108 | First_Discriminant (Rhs_Type), | |
2109 | Rhs_Type, | |
2110 | Stored_Constraint (Rhs_Type))); | |
2111 | ||
2112 | end if; | |
2113 | else | |
8aceda64 | 2114 | return |
5d09245e | 2115 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2116 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2117 | end if; |
2118 | ||
2119 | -- Call the TSS equality function with the inferred | |
2120 | -- discriminant values. | |
2121 | ||
2122 | return | |
2123 | Make_Function_Call (Loc, | |
2124 | Name => New_Reference_To (Eq_Op, Loc), | |
2125 | Parameter_Associations => New_List ( | |
2126 | Lhs, | |
2127 | Rhs, | |
2128 | Lhs_Discr_Val, | |
2129 | Rhs_Discr_Val)); | |
2130 | end; | |
2131 | end if; | |
2132 | ||
685094bf RD |
2133 | -- Shouldn't this be an else, we can't fall through the above |
2134 | -- IF, right??? | |
5d09245e | 2135 | |
70482933 RK |
2136 | return |
2137 | Make_Function_Call (Loc, | |
2138 | Name => New_Reference_To (Eq_Op, Loc), | |
2139 | Parameter_Associations => New_List (Lhs, Rhs)); | |
2140 | end if; | |
2141 | ||
2142 | else | |
2143 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2144 | end if; | |
2145 | ||
2146 | else | |
2147 | -- It can be a simple record or the full view of a scalar private | |
2148 | ||
2149 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
2150 | end if; | |
2151 | end Expand_Composite_Equality; | |
2152 | ||
fdac1f80 AC |
2153 | ------------------------ |
2154 | -- Expand_Concatenate -- | |
2155 | ------------------------ | |
70482933 | 2156 | |
fdac1f80 AC |
2157 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2158 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2159 | |
fdac1f80 AC |
2160 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2161 | -- Result type of concatenation | |
70482933 | 2162 | |
fdac1f80 AC |
2163 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2164 | -- Component type. Elements of this component type can appear as one | |
2165 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2166 | |
ecc4ddde AC |
2167 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2168 | -- Index subtype | |
2169 | ||
2170 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2171 | -- Index type. This is the base type of the index subtype, and is used | |
2172 | -- for all computed bounds (which may be out of range of Istyp in the | |
2173 | -- case of null ranges). | |
70482933 | 2174 | |
46ff89f3 | 2175 | Artyp : Entity_Id; |
fdac1f80 AC |
2176 | -- This is the type we use to do arithmetic to compute the bounds and |
2177 | -- lengths of operands. The choice of this type is a little subtle and | |
2178 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2179 | |
fdac1f80 AC |
2180 | Concatenation_Error : exception; |
2181 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2182 | |
0ac73189 AC |
2183 | Result_May_Be_Null : Boolean := True; |
2184 | -- Reset to False if at least one operand is encountered which is known | |
2185 | -- at compile time to be non-null. Used for handling the special case | |
2186 | -- of setting the high bound to the last operand high bound for a null | |
2187 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2188 | ||
df46b832 | 2189 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2190 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2191 | |
2192 | NN : Nat := 0; | |
a29262fd AC |
2193 | -- Number of operands excluding any known to be null, except that the |
2194 | -- last operand is always retained, in case it provides the bounds for | |
2195 | -- a null result. | |
2196 | ||
2197 | Opnd : Node_Id; | |
2198 | -- Current operand being processed in the loop through operands. After | |
2199 | -- this loop is complete, always contains the last operand (which is not | |
2200 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2201 | |
2202 | -- Arrays describing the operands, only the first NN entries of each | |
2203 | -- array are set (NN < N when we exclude known null operands). | |
2204 | ||
2205 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2206 | -- True if length of corresponding operand known at compile time | |
2207 | ||
2208 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2209 | -- Set to the corresponding entry in the Opnds list (but note that null |
2210 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2211 | |
2212 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2213 | -- Set to length of operand. Entries in this array are set only if the |
2214 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2215 | |
0ac73189 AC |
2216 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2217 | -- Set to lower bound of operand. Either an integer literal in the case | |
2218 | -- where the bound is known at compile time, else actual lower bound. | |
2219 | -- The operand low bound is of type Ityp. | |
2220 | ||
df46b832 AC |
2221 | Var_Length : array (1 .. N) of Entity_Id; |
2222 | -- Set to an entity of type Natural that contains the length of an | |
2223 | -- operand whose length is not known at compile time. Entries in this | |
2224 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2225 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2226 | |
2227 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2228 | -- The J'th entry in an expression node that represents the total length |
2229 | -- of operands 1 through J. It is either an integer literal node, or a | |
2230 | -- reference to a constant entity with the right value, so it is fine | |
2231 | -- to just do a Copy_Node to get an appropriate copy. The extra zero'th | |
46ff89f3 | 2232 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 AC |
2233 | |
2234 | Low_Bound : Node_Id; | |
0ac73189 AC |
2235 | -- A tree node representing the low bound of the result (of type Ityp). |
2236 | -- This is either an integer literal node, or an identifier reference to | |
2237 | -- a constant entity initialized to the appropriate value. | |
2238 | ||
a29262fd AC |
2239 | Last_Opnd_High_Bound : Node_Id; |
2240 | -- A tree node representing the high bound of the last operand. This | |
2241 | -- need only be set if the result could be null. It is used for the | |
2242 | -- special case of setting the right high bound for a null result. | |
2243 | -- This is of type Ityp. | |
2244 | ||
0ac73189 AC |
2245 | High_Bound : Node_Id; |
2246 | -- A tree node representing the high bound of the result (of type Ityp) | |
df46b832 AC |
2247 | |
2248 | Result : Node_Id; | |
0ac73189 | 2249 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2250 | |
fa969310 AC |
2251 | Known_Non_Null_Operand_Seen : Boolean; |
2252 | -- Set True during generation of the assignements of operands into | |
2253 | -- result once an operand known to be non-null has been seen. | |
2254 | ||
2255 | function Make_Artyp_Literal (Val : Nat) return Node_Id; | |
2256 | -- This function makes an N_Integer_Literal node that is returned in | |
2257 | -- analyzed form with the type set to Artyp. Importantly this literal | |
2258 | -- is not flagged as static, so that if we do computations with it that | |
2259 | -- result in statically detected out of range conditions, we will not | |
2260 | -- generate error messages but instead warning messages. | |
2261 | ||
46ff89f3 | 2262 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 2263 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
2264 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
2265 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
2266 | |
2267 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 2268 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 2269 | |
fa969310 AC |
2270 | ------------------------ |
2271 | -- Make_Artyp_Literal -- | |
2272 | ------------------------ | |
2273 | ||
2274 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
2275 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
2276 | begin | |
2277 | Set_Etype (Result, Artyp); | |
2278 | Set_Analyzed (Result, True); | |
2279 | Set_Is_Static_Expression (Result, False); | |
2280 | return Result; | |
2281 | end Make_Artyp_Literal; | |
76c597a1 | 2282 | |
fdac1f80 | 2283 | -------------- |
46ff89f3 | 2284 | -- To_Artyp -- |
fdac1f80 AC |
2285 | -------------- |
2286 | ||
46ff89f3 | 2287 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 2288 | begin |
46ff89f3 | 2289 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
2290 | return X; |
2291 | ||
2292 | elsif Is_Enumeration_Type (Ityp) then | |
2293 | return | |
2294 | Make_Attribute_Reference (Loc, | |
2295 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2296 | Attribute_Name => Name_Pos, | |
2297 | Expressions => New_List (X)); | |
2298 | ||
2299 | else | |
46ff89f3 | 2300 | return Convert_To (Artyp, X); |
fdac1f80 | 2301 | end if; |
46ff89f3 | 2302 | end To_Artyp; |
fdac1f80 AC |
2303 | |
2304 | ------------- | |
2305 | -- To_Ityp -- | |
2306 | ------------- | |
2307 | ||
2308 | function To_Ityp (X : Node_Id) return Node_Id is | |
2309 | begin | |
2fc05e3d | 2310 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
2311 | return |
2312 | Make_Attribute_Reference (Loc, | |
2313 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2314 | Attribute_Name => Name_Val, | |
2315 | Expressions => New_List (X)); | |
2316 | ||
2317 | -- Case where we will do a type conversion | |
2318 | ||
2319 | else | |
76c597a1 AC |
2320 | if Ityp = Base_Type (Artyp) then |
2321 | return X; | |
fdac1f80 | 2322 | else |
76c597a1 | 2323 | return Convert_To (Ityp, X); |
fdac1f80 AC |
2324 | end if; |
2325 | end if; | |
2326 | end To_Ityp; | |
2327 | ||
2328 | -- Local Declarations | |
2329 | ||
0ac73189 AC |
2330 | Opnd_Typ : Entity_Id; |
2331 | Ent : Entity_Id; | |
2332 | Len : Uint; | |
2333 | J : Nat; | |
2334 | Clen : Node_Id; | |
2335 | Set : Boolean; | |
70482933 RK |
2336 | |
2337 | begin | |
fdac1f80 AC |
2338 | -- Choose an appropriate computational type |
2339 | ||
2340 | -- We will be doing calculations of lengths and bounds in this routine | |
2341 | -- and computing one from the other in some cases, e.g. getting the high | |
2342 | -- bound by adding the length-1 to the low bound. | |
2343 | ||
2344 | -- We can't just use the index type, or even its base type for this | |
2345 | -- purpose for two reasons. First it might be an enumeration type which | |
2346 | -- is not suitable fo computations of any kind, and second it may simply | |
2347 | -- not have enough range. For example if the index type is -128..+127 | |
2348 | -- then lengths can be up to 256, which is out of range of the type. | |
2349 | ||
2350 | -- For enumeration types, we can simply use Standard_Integer, this is | |
2351 | -- sufficient since the actual number of enumeration literals cannot | |
2352 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 2353 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
2354 | |
2355 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 2356 | Artyp := Standard_Integer; |
fdac1f80 | 2357 | |
59262ebb AC |
2358 | -- If index type is Positive, we use the standard unsigned type, to give |
2359 | -- more room on the top of the range, obviating the need for an overflow | |
2360 | -- check when creating the upper bound. This is needed to avoid junk | |
2361 | -- overflow checks in the common case of String types. | |
2362 | ||
2363 | -- ??? Disabled for now | |
2364 | ||
2365 | -- elsif Istyp = Standard_Positive then | |
2366 | -- Artyp := Standard_Unsigned; | |
2367 | ||
2fc05e3d AC |
2368 | -- For modular types, we use a 32-bit modular type for types whose size |
2369 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
2370 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 2371 | |
2fc05e3d | 2372 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 2373 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 2374 | Artyp := Standard_Unsigned; |
ecc4ddde | 2375 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 2376 | Artyp := Ityp; |
fdac1f80 | 2377 | else |
46ff89f3 | 2378 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
2379 | end if; |
2380 | ||
2fc05e3d | 2381 | -- Similar treatment for signed types |
fdac1f80 AC |
2382 | |
2383 | else | |
ecc4ddde | 2384 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 2385 | Artyp := Standard_Integer; |
ecc4ddde | 2386 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 2387 | Artyp := Ityp; |
fdac1f80 | 2388 | else |
46ff89f3 | 2389 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
2390 | end if; |
2391 | end if; | |
2392 | ||
fa969310 AC |
2393 | -- Supply dummy entry at start of length array |
2394 | ||
2395 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
2396 | ||
fdac1f80 | 2397 | -- Go through operands setting up the above arrays |
70482933 | 2398 | |
df46b832 AC |
2399 | J := 1; |
2400 | while J <= N loop | |
2401 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 2402 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
2403 | |
2404 | -- The parent got messed up when we put the operands in a list, | |
2405 | -- so now put back the proper parent for the saved operand. | |
2406 | ||
df46b832 | 2407 | Set_Parent (Opnd, Parent (Cnode)); |
fdac1f80 AC |
2408 | |
2409 | -- Set will be True when we have setup one entry in the array | |
2410 | ||
df46b832 AC |
2411 | Set := False; |
2412 | ||
fdac1f80 | 2413 | -- Singleton element (or character literal) case |
df46b832 | 2414 | |
0ac73189 | 2415 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
2416 | NN := NN + 1; |
2417 | Operands (NN) := Opnd; | |
2418 | Is_Fixed_Length (NN) := True; | |
2419 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 2420 | Result_May_Be_Null := False; |
fdac1f80 | 2421 | |
a29262fd AC |
2422 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
2423 | -- since we know that the result cannot be null). | |
fdac1f80 | 2424 | |
0ac73189 AC |
2425 | Opnd_Low_Bound (NN) := |
2426 | Make_Attribute_Reference (Loc, | |
ecc4ddde | 2427 | Prefix => New_Reference_To (Istyp, Loc), |
0ac73189 AC |
2428 | Attribute_Name => Name_First); |
2429 | ||
df46b832 AC |
2430 | Set := True; |
2431 | ||
fdac1f80 | 2432 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
2433 | |
2434 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 2435 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 2436 | |
a29262fd AC |
2437 | if Len /= 0 then |
2438 | Result_May_Be_Null := False; | |
2439 | end if; | |
2440 | ||
2441 | -- Capture last operand high bound if result could be null | |
2442 | ||
2443 | if J = N and then Result_May_Be_Null then | |
2444 | Last_Opnd_High_Bound := | |
2445 | Make_Op_Add (Loc, | |
2446 | Left_Opnd => | |
2447 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 2448 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
2449 | end if; |
2450 | ||
2451 | -- Skip null string literal | |
fdac1f80 | 2452 | |
0ac73189 | 2453 | if J < N and then Len = 0 then |
df46b832 AC |
2454 | goto Continue; |
2455 | end if; | |
2456 | ||
2457 | NN := NN + 1; | |
2458 | Operands (NN) := Opnd; | |
2459 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
2460 | |
2461 | -- Set length and bounds | |
2462 | ||
df46b832 | 2463 | Fixed_Length (NN) := Len; |
0ac73189 AC |
2464 | |
2465 | Opnd_Low_Bound (NN) := | |
2466 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
2467 | ||
df46b832 AC |
2468 | Set := True; |
2469 | ||
2470 | -- All other cases | |
2471 | ||
2472 | else | |
2473 | -- Check constrained case with known bounds | |
2474 | ||
0ac73189 | 2475 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 2476 | declare |
df46b832 AC |
2477 | Index : constant Node_Id := First_Index (Opnd_Typ); |
2478 | Indx_Typ : constant Entity_Id := Etype (Index); | |
2479 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
2480 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
2481 | ||
2482 | begin | |
fdac1f80 AC |
2483 | -- Fixed length constrained array type with known at compile |
2484 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
2485 | |
2486 | if Compile_Time_Known_Value (Lo) | |
2487 | and then | |
2488 | Compile_Time_Known_Value (Hi) | |
2489 | then | |
2490 | declare | |
2491 | Loval : constant Uint := Expr_Value (Lo); | |
2492 | Hival : constant Uint := Expr_Value (Hi); | |
2493 | Len : constant Uint := | |
2494 | UI_Max (Hival - Loval + 1, Uint_0); | |
2495 | ||
2496 | begin | |
0ac73189 AC |
2497 | if Len > 0 then |
2498 | Result_May_Be_Null := False; | |
df46b832 | 2499 | end if; |
0ac73189 | 2500 | |
a29262fd AC |
2501 | -- Capture last operand bound if result could be null |
2502 | ||
2503 | if J = N and then Result_May_Be_Null then | |
2504 | Last_Opnd_High_Bound := | |
2505 | Convert_To (Ityp, | |
2506 | Make_Integer_Literal (Loc, | |
2507 | Intval => Expr_Value (Hi))); | |
2508 | end if; | |
2509 | ||
2510 | -- Exclude null length case unless last operand | |
0ac73189 | 2511 | |
a29262fd | 2512 | if J < N and then Len = 0 then |
0ac73189 AC |
2513 | goto Continue; |
2514 | end if; | |
2515 | ||
2516 | NN := NN + 1; | |
2517 | Operands (NN) := Opnd; | |
2518 | Is_Fixed_Length (NN) := True; | |
2519 | Fixed_Length (NN) := Len; | |
2520 | ||
a2dc5812 | 2521 | Opnd_Low_Bound (NN) := To_Ityp ( |
0ac73189 | 2522 | Make_Integer_Literal (Loc, |
a2dc5812 | 2523 | Intval => Expr_Value (Lo))); |
0ac73189 | 2524 | |
0ac73189 | 2525 | Set := True; |
df46b832 AC |
2526 | end; |
2527 | end if; | |
2528 | end; | |
2529 | end if; | |
2530 | ||
0ac73189 AC |
2531 | -- All cases where the length is not known at compile time, or the |
2532 | -- special case of an operand which is known to be null but has a | |
2533 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
2534 | |
2535 | if not Set then | |
2536 | NN := NN + 1; | |
0ac73189 AC |
2537 | |
2538 | -- Capture operand bounds | |
2539 | ||
2540 | Opnd_Low_Bound (NN) := | |
2541 | Make_Attribute_Reference (Loc, | |
2542 | Prefix => | |
2543 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2544 | Attribute_Name => Name_First); | |
2545 | ||
a29262fd AC |
2546 | if J = N and Result_May_Be_Null then |
2547 | Last_Opnd_High_Bound := | |
2548 | Convert_To (Ityp, | |
2549 | Make_Attribute_Reference (Loc, | |
2550 | Prefix => | |
2551 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2552 | Attribute_Name => Name_Last)); | |
2553 | end if; | |
0ac73189 AC |
2554 | |
2555 | -- Capture length of operand in entity | |
2556 | ||
df46b832 AC |
2557 | Operands (NN) := Opnd; |
2558 | Is_Fixed_Length (NN) := False; | |
2559 | ||
2560 | Var_Length (NN) := | |
2561 | Make_Defining_Identifier (Loc, | |
2562 | Chars => New_Internal_Name ('L')); | |
2563 | ||
2564 | Insert_Action (Cnode, | |
2565 | Make_Object_Declaration (Loc, | |
2566 | Defining_Identifier => Var_Length (NN), | |
2567 | Constant_Present => True, | |
2568 | ||
2569 | Object_Definition => | |
46ff89f3 | 2570 | New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2571 | |
2572 | Expression => | |
2573 | Make_Attribute_Reference (Loc, | |
2574 | Prefix => | |
2575 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2576 | Attribute_Name => Name_Length)), | |
2577 | ||
2578 | Suppress => All_Checks); | |
2579 | end if; | |
2580 | end if; | |
2581 | ||
2582 | -- Set next entry in aggregate length array | |
2583 | ||
2584 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 2585 | -- or a reference to the saved length for variable length. |
df46b832 AC |
2586 | |
2587 | if NN = 1 then | |
2588 | if Is_Fixed_Length (1) then | |
2589 | Aggr_Length (1) := | |
2590 | Make_Integer_Literal (Loc, | |
2591 | Intval => Fixed_Length (1)); | |
2592 | else | |
2593 | Aggr_Length (1) := | |
2594 | New_Reference_To (Var_Length (1), Loc); | |
2595 | end if; | |
2596 | ||
2597 | -- If entry is fixed length and only fixed lengths so far, make | |
2598 | -- appropriate new integer literal adding new length. | |
2599 | ||
2600 | elsif Is_Fixed_Length (NN) | |
2601 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
2602 | then | |
2603 | Aggr_Length (NN) := | |
2604 | Make_Integer_Literal (Loc, | |
2605 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
2606 | ||
2607 | -- All other cases, construct an addition node for the length and | |
2608 | -- create an entity initialized to this length. | |
2609 | ||
2610 | else | |
2611 | Ent := | |
2612 | Make_Defining_Identifier (Loc, | |
2613 | Chars => New_Internal_Name ('L')); | |
2614 | ||
2615 | if Is_Fixed_Length (NN) then | |
2616 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
2617 | else | |
2618 | Clen := New_Reference_To (Var_Length (NN), Loc); | |
2619 | end if; | |
2620 | ||
2621 | Insert_Action (Cnode, | |
2622 | Make_Object_Declaration (Loc, | |
2623 | Defining_Identifier => Ent, | |
2624 | Constant_Present => True, | |
2625 | ||
2626 | Object_Definition => | |
46ff89f3 | 2627 | New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2628 | |
2629 | Expression => | |
2630 | Make_Op_Add (Loc, | |
2631 | Left_Opnd => New_Copy (Aggr_Length (NN - 1)), | |
2632 | Right_Opnd => Clen)), | |
2633 | ||
2634 | Suppress => All_Checks); | |
2635 | ||
76c597a1 | 2636 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
2637 | end if; |
2638 | ||
2639 | <<Continue>> | |
2640 | J := J + 1; | |
2641 | end loop; | |
2642 | ||
a29262fd | 2643 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
2644 | |
2645 | if NN = 0 then | |
a29262fd | 2646 | Result := Opnd; |
df46b832 AC |
2647 | goto Done; |
2648 | end if; | |
2649 | ||
2650 | -- If we have only one non-null operand, return it and we are done. | |
2651 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
2652 | -- the sole operand is of the element type, in which case it must be |
2653 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
2654 | -- through the normal general circuit. |
2655 | ||
2656 | if NN = 1 | |
fdac1f80 | 2657 | and then Base_Type (Etype (Operands (1))) /= Ctyp |
df46b832 AC |
2658 | then |
2659 | Result := Operands (1); | |
2660 | goto Done; | |
2661 | end if; | |
2662 | ||
2663 | -- Cases where we have a real concatenation | |
2664 | ||
fdac1f80 AC |
2665 | -- Next step is to find the low bound for the result array that we |
2666 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
2667 | ||
2668 | -- If the ultimate ancestor of the index subtype is a constrained array | |
2669 | -- definition, then the lower bound is that of the index subtype as | |
2670 | -- specified by (RM 4.5.3(6)). | |
2671 | ||
2672 | -- The right test here is to go to the root type, and then the ultimate | |
2673 | -- ancestor is the first subtype of this root type. | |
2674 | ||
2675 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 2676 | Low_Bound := |
fdac1f80 AC |
2677 | Make_Attribute_Reference (Loc, |
2678 | Prefix => | |
2679 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 2680 | Attribute_Name => Name_First); |
df46b832 AC |
2681 | |
2682 | -- If the first operand in the list has known length we know that | |
2683 | -- the lower bound of the result is the lower bound of this operand. | |
2684 | ||
fdac1f80 | 2685 | elsif Is_Fixed_Length (1) then |
0ac73189 | 2686 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
2687 | |
2688 | -- OK, we don't know the lower bound, we have to build a horrible | |
2689 | -- expression actions node of the form | |
2690 | ||
2691 | -- if Cond1'Length /= 0 then | |
0ac73189 | 2692 | -- Opnd1 low bound |
df46b832 AC |
2693 | -- else |
2694 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 2695 | -- Opnd2 low bound |
df46b832 AC |
2696 | -- else |
2697 | -- ... | |
2698 | ||
2699 | -- The nesting ends either when we hit an operand whose length is known | |
2700 | -- at compile time, or on reaching the last operand, whose low bound we | |
2701 | -- take unconditionally whether or not it is null. It's easiest to do | |
2702 | -- this with a recursive procedure: | |
2703 | ||
2704 | else | |
2705 | declare | |
2706 | function Get_Known_Bound (J : Nat) return Node_Id; | |
2707 | -- Returns the lower bound determined by operands J .. NN | |
2708 | ||
2709 | --------------------- | |
2710 | -- Get_Known_Bound -- | |
2711 | --------------------- | |
2712 | ||
2713 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 2714 | begin |
0ac73189 AC |
2715 | if Is_Fixed_Length (J) or else J = NN then |
2716 | return New_Copy (Opnd_Low_Bound (J)); | |
70482933 RK |
2717 | |
2718 | else | |
df46b832 AC |
2719 | return |
2720 | Make_Conditional_Expression (Loc, | |
2721 | Expressions => New_List ( | |
2722 | ||
2723 | Make_Op_Ne (Loc, | |
2724 | Left_Opnd => New_Reference_To (Var_Length (J), Loc), | |
2725 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
2726 | ||
0ac73189 | 2727 | New_Copy (Opnd_Low_Bound (J)), |
df46b832 | 2728 | Get_Known_Bound (J + 1))); |
70482933 | 2729 | end if; |
df46b832 | 2730 | end Get_Known_Bound; |
70482933 | 2731 | |
df46b832 AC |
2732 | begin |
2733 | Ent := | |
76c597a1 | 2734 | Make_Defining_Identifier (Loc, Chars => New_Internal_Name ('L')); |
df46b832 AC |
2735 | |
2736 | Insert_Action (Cnode, | |
2737 | Make_Object_Declaration (Loc, | |
2738 | Defining_Identifier => Ent, | |
2739 | Constant_Present => True, | |
0ac73189 | 2740 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
df46b832 AC |
2741 | Expression => Get_Known_Bound (1)), |
2742 | Suppress => All_Checks); | |
2743 | ||
2744 | Low_Bound := New_Reference_To (Ent, Loc); | |
2745 | end; | |
2746 | end if; | |
70482933 | 2747 | |
76c597a1 AC |
2748 | -- Now we can safely compute the upper bound, normally |
2749 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
2750 | |
2751 | High_Bound := | |
2752 | To_Ityp ( | |
2753 | Make_Op_Add (Loc, | |
46ff89f3 | 2754 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
0ac73189 AC |
2755 | Right_Opnd => |
2756 | Make_Op_Subtract (Loc, | |
2757 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 2758 | Right_Opnd => Make_Artyp_Literal (1)))); |
0ac73189 | 2759 | |
59262ebb | 2760 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
2761 | -- very weird cases, so in the general case we need an overflow check on |
2762 | -- the high bound. We can avoid this for the common case of string types | |
2763 | -- and other types whose index is Positive, since we chose a wider range | |
2764 | -- for the arithmetic type. | |
76c597a1 | 2765 | |
59262ebb AC |
2766 | if Istyp /= Standard_Positive then |
2767 | Activate_Overflow_Check (High_Bound); | |
2768 | end if; | |
76c597a1 AC |
2769 | |
2770 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
2771 | -- case the bounds come from the last operand (so that we get the proper |
2772 | -- bounds if the last operand is super-flat). | |
2773 | ||
0ac73189 AC |
2774 | if Result_May_Be_Null then |
2775 | High_Bound := | |
2776 | Make_Conditional_Expression (Loc, | |
2777 | Expressions => New_List ( | |
2778 | Make_Op_Eq (Loc, | |
2779 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 2780 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 2781 | Last_Opnd_High_Bound, |
0ac73189 AC |
2782 | High_Bound)); |
2783 | end if; | |
2784 | ||
2785 | -- Now we construct an array object with appropriate bounds | |
70482933 | 2786 | |
df46b832 AC |
2787 | Ent := |
2788 | Make_Defining_Identifier (Loc, | |
2789 | Chars => New_Internal_Name ('S')); | |
70482933 | 2790 | |
76c597a1 | 2791 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
2792 | -- to abort, we want a warning and a runtime constraint error. Note that |
2793 | -- we have arranged that the result will not be treated as a static | |
2794 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 2795 | |
df46b832 AC |
2796 | Insert_Action (Cnode, |
2797 | Make_Object_Declaration (Loc, | |
2798 | Defining_Identifier => Ent, | |
df46b832 AC |
2799 | Object_Definition => |
2800 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 2801 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
2802 | Constraint => |
2803 | Make_Index_Or_Discriminant_Constraint (Loc, | |
2804 | Constraints => New_List ( | |
2805 | Make_Range (Loc, | |
0ac73189 AC |
2806 | Low_Bound => Low_Bound, |
2807 | High_Bound => High_Bound))))), | |
df46b832 AC |
2808 | Suppress => All_Checks); |
2809 | ||
76c597a1 AC |
2810 | -- Catch the static out of range case now |
2811 | ||
2812 | if Raises_Constraint_Error (High_Bound) then | |
2813 | raise Concatenation_Error; | |
2814 | end if; | |
2815 | ||
df46b832 AC |
2816 | -- Now we will generate the assignments to do the actual concatenation |
2817 | ||
bded454f RD |
2818 | -- There is one case in which we will not do this, namely when all the |
2819 | -- following conditions are met: | |
2820 | ||
2821 | -- The result type is Standard.String | |
2822 | ||
2823 | -- There are nine or fewer retained (non-null) operands | |
2824 | ||
2825 | -- The optimization level is -O0 or -Os | |
2826 | ||
2827 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
2828 | -- available in the run time. | |
2829 | ||
2830 | -- The debug flag gnatd.c is not set | |
2831 | ||
2832 | -- If all these conditions are met then we generate a call to the | |
2833 | -- relevant concatenation routine. The purpose of this is to avoid | |
2834 | -- undesirable code bloat at -O0. | |
2835 | ||
2836 | if Atyp = Standard_String | |
2837 | and then NN in 2 .. 9 | |
2838 | and then (Opt.Optimization_Level = 0 | |
fa702fbd | 2839 | or else Opt.Optimize_Size /= 0 |
bded454f RD |
2840 | or else Debug_Flag_Dot_CC) |
2841 | and then not Debug_Flag_Dot_C | |
2842 | then | |
2843 | declare | |
2844 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
2845 | (RE_Str_Concat_2, | |
2846 | RE_Str_Concat_3, | |
2847 | RE_Str_Concat_4, | |
2848 | RE_Str_Concat_5, | |
2849 | RE_Str_Concat_6, | |
2850 | RE_Str_Concat_7, | |
2851 | RE_Str_Concat_8, | |
2852 | RE_Str_Concat_9); | |
2853 | ||
2854 | begin | |
2855 | if RTE_Available (RR (NN)) then | |
2856 | declare | |
2857 | Opnds : constant List_Id := | |
2858 | New_List (New_Occurrence_Of (Ent, Loc)); | |
2859 | ||
2860 | begin | |
2861 | for J in 1 .. NN loop | |
2862 | if Is_List_Member (Operands (J)) then | |
2863 | Remove (Operands (J)); | |
2864 | end if; | |
2865 | ||
2866 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
2867 | Append_To (Opnds, | |
2868 | Make_Aggregate (Loc, | |
2869 | Component_Associations => New_List ( | |
2870 | Make_Component_Association (Loc, | |
2871 | Choices => New_List ( | |
2872 | Make_Integer_Literal (Loc, 1)), | |
2873 | Expression => Operands (J))))); | |
2874 | ||
2875 | else | |
2876 | Append_To (Opnds, Operands (J)); | |
2877 | end if; | |
2878 | end loop; | |
2879 | ||
2880 | Insert_Action (Cnode, | |
2881 | Make_Procedure_Call_Statement (Loc, | |
2882 | Name => New_Reference_To (RTE (RR (NN)), Loc), | |
2883 | Parameter_Associations => Opnds)); | |
2884 | ||
2885 | Result := New_Reference_To (Ent, Loc); | |
2886 | goto Done; | |
2887 | end; | |
2888 | end if; | |
2889 | end; | |
2890 | end if; | |
2891 | ||
2892 | -- Not special case so generate the assignments | |
2893 | ||
76c597a1 AC |
2894 | Known_Non_Null_Operand_Seen := False; |
2895 | ||
df46b832 AC |
2896 | for J in 1 .. NN loop |
2897 | declare | |
2898 | Lo : constant Node_Id := | |
2899 | Make_Op_Add (Loc, | |
46ff89f3 | 2900 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
2901 | Right_Opnd => Aggr_Length (J - 1)); |
2902 | ||
2903 | Hi : constant Node_Id := | |
2904 | Make_Op_Add (Loc, | |
46ff89f3 | 2905 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
2906 | Right_Opnd => |
2907 | Make_Op_Subtract (Loc, | |
2908 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 2909 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 2910 | |
df46b832 | 2911 | begin |
fdac1f80 AC |
2912 | -- Singleton case, simple assignment |
2913 | ||
2914 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 2915 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
2916 | Insert_Action (Cnode, |
2917 | Make_Assignment_Statement (Loc, | |
2918 | Name => | |
2919 | Make_Indexed_Component (Loc, | |
2920 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 2921 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
2922 | Expression => Operands (J)), |
2923 | Suppress => All_Checks); | |
70482933 | 2924 | |
76c597a1 AC |
2925 | -- Array case, slice assignment, skipped when argument is fixed |
2926 | -- length and known to be null. | |
fdac1f80 | 2927 | |
76c597a1 AC |
2928 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
2929 | declare | |
2930 | Assign : Node_Id := | |
2931 | Make_Assignment_Statement (Loc, | |
2932 | Name => | |
2933 | Make_Slice (Loc, | |
2934 | Prefix => | |
2935 | New_Occurrence_Of (Ent, Loc), | |
2936 | Discrete_Range => | |
2937 | Make_Range (Loc, | |
2938 | Low_Bound => To_Ityp (Lo), | |
2939 | High_Bound => To_Ityp (Hi))), | |
2940 | Expression => Operands (J)); | |
2941 | begin | |
2942 | if Is_Fixed_Length (J) then | |
2943 | Known_Non_Null_Operand_Seen := True; | |
2944 | ||
2945 | elsif not Known_Non_Null_Operand_Seen then | |
2946 | ||
2947 | -- Here if operand length is not statically known and no | |
2948 | -- operand known to be non-null has been processed yet. | |
2949 | -- If operand length is 0, we do not need to perform the | |
2950 | -- assignment, and we must avoid the evaluation of the | |
2951 | -- high bound of the slice, since it may underflow if the | |
2952 | -- low bound is Ityp'First. | |
2953 | ||
2954 | Assign := | |
2955 | Make_Implicit_If_Statement (Cnode, | |
2956 | Condition => | |
2957 | Make_Op_Ne (Loc, | |
2958 | Left_Opnd => | |
2959 | New_Occurrence_Of (Var_Length (J), Loc), | |
2960 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
2961 | Then_Statements => | |
2962 | New_List (Assign)); | |
2963 | end if; | |
fa969310 | 2964 | |
76c597a1 AC |
2965 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
2966 | end; | |
df46b832 AC |
2967 | end if; |
2968 | end; | |
2969 | end loop; | |
70482933 | 2970 | |
0ac73189 AC |
2971 | -- Finally we build the result, which is a reference to the array object |
2972 | ||
df46b832 | 2973 | Result := New_Reference_To (Ent, Loc); |
70482933 | 2974 | |
df46b832 AC |
2975 | <<Done>> |
2976 | Rewrite (Cnode, Result); | |
fdac1f80 AC |
2977 | Analyze_And_Resolve (Cnode, Atyp); |
2978 | ||
2979 | exception | |
2980 | when Concatenation_Error => | |
76c597a1 AC |
2981 | |
2982 | -- Kill warning generated for the declaration of the static out of | |
2983 | -- range high bound, and instead generate a Constraint_Error with | |
2984 | -- an appropriate specific message. | |
2985 | ||
2986 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
2987 | Apply_Compile_Time_Constraint_Error | |
2988 | (N => Cnode, | |
2989 | Msg => "concatenation result upper bound out of range?", | |
2990 | Reason => CE_Range_Check_Failed); | |
2991 | -- Set_Etype (Cnode, Atyp); | |
fdac1f80 | 2992 | end Expand_Concatenate; |
70482933 RK |
2993 | |
2994 | ------------------------ | |
2995 | -- Expand_N_Allocator -- | |
2996 | ------------------------ | |
2997 | ||
2998 | procedure Expand_N_Allocator (N : Node_Id) is | |
2999 | PtrT : constant Entity_Id := Etype (N); | |
0da2c8ac | 3000 | Dtyp : constant Entity_Id := Designated_Type (PtrT); |
f82944b7 | 3001 | Etyp : constant Entity_Id := Etype (Expression (N)); |
70482933 | 3002 | Loc : constant Source_Ptr := Sloc (N); |
f82944b7 | 3003 | Desig : Entity_Id; |
70482933 | 3004 | Temp : Entity_Id; |
26bff3d9 | 3005 | Nod : Node_Id; |
70482933 | 3006 | |
26bff3d9 JM |
3007 | procedure Complete_Coextension_Finalization; |
3008 | -- Generate finalization calls for all nested coextensions of N. This | |
3009 | -- routine may allocate list controllers if necessary. | |
0669bebe | 3010 | |
26bff3d9 JM |
3011 | procedure Rewrite_Coextension (N : Node_Id); |
3012 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 3013 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 3014 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 3015 | |
8aec446b | 3016 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd AC |
3017 | -- Given a constrained array type E, returns a node representing the |
3018 | -- code to compute the size in storage elements for the given type. | |
205c14b0 | 3019 | -- This is done without using the attribute (which malfunctions for |
507ed3fd | 3020 | -- large sizes ???) |
8aec446b | 3021 | |
26bff3d9 JM |
3022 | --------------------------------------- |
3023 | -- Complete_Coextension_Finalization -- | |
3024 | --------------------------------------- | |
0669bebe | 3025 | |
26bff3d9 JM |
3026 | procedure Complete_Coextension_Finalization is |
3027 | Coext : Node_Id; | |
3028 | Coext_Elmt : Elmt_Id; | |
3029 | Flist : Node_Id; | |
3030 | Ref : Node_Id; | |
0669bebe | 3031 | |
26bff3d9 JM |
3032 | function Inside_A_Return_Statement (N : Node_Id) return Boolean; |
3033 | -- Determine whether node N is part of a return statement | |
3034 | ||
3035 | function Needs_Initialization_Call (N : Node_Id) return Boolean; | |
3036 | -- Determine whether node N is a subtype indicator allocator which | |
b4592168 | 3037 | -- acts a coextension. Such coextensions need initialization. |
26bff3d9 JM |
3038 | |
3039 | ------------------------------- | |
3040 | -- Inside_A_Return_Statement -- | |
3041 | ------------------------------- | |
3042 | ||
3043 | function Inside_A_Return_Statement (N : Node_Id) return Boolean is | |
3044 | P : Node_Id; | |
3045 | ||
3046 | begin | |
3047 | P := Parent (N); | |
3048 | while Present (P) loop | |
303b4d58 AC |
3049 | if Nkind_In |
3050 | (P, N_Extended_Return_Statement, N_Simple_Return_Statement) | |
26bff3d9 JM |
3051 | then |
3052 | return True; | |
3053 | ||
3054 | -- Stop the traversal when we reach a subprogram body | |
3055 | ||
3056 | elsif Nkind (P) = N_Subprogram_Body then | |
3057 | return False; | |
3058 | end if; | |
3059 | ||
3060 | P := Parent (P); | |
3061 | end loop; | |
3062 | ||
3063 | return False; | |
3064 | end Inside_A_Return_Statement; | |
3065 | ||
3066 | ------------------------------- | |
3067 | -- Needs_Initialization_Call -- | |
3068 | ------------------------------- | |
3069 | ||
3070 | function Needs_Initialization_Call (N : Node_Id) return Boolean is | |
3071 | Obj_Decl : Node_Id; | |
3072 | ||
3073 | begin | |
3074 | if Nkind (N) = N_Explicit_Dereference | |
3075 | and then Nkind (Prefix (N)) = N_Identifier | |
3076 | and then Nkind (Parent (Entity (Prefix (N)))) = | |
3077 | N_Object_Declaration | |
3078 | then | |
3079 | Obj_Decl := Parent (Entity (Prefix (N))); | |
0669bebe | 3080 | |
26bff3d9 JM |
3081 | return |
3082 | Present (Expression (Obj_Decl)) | |
3083 | and then Nkind (Expression (Obj_Decl)) = N_Allocator | |
3084 | and then Nkind (Expression (Expression (Obj_Decl))) /= | |
3085 | N_Qualified_Expression; | |
0669bebe GB |
3086 | end if; |
3087 | ||
26bff3d9 JM |
3088 | return False; |
3089 | end Needs_Initialization_Call; | |
3090 | ||
3091 | -- Start of processing for Complete_Coextension_Finalization | |
3092 | ||
3093 | begin | |
3094 | -- When a coextension root is inside a return statement, we need to | |
3095 | -- use the finalization chain of the function's scope. This does not | |
3096 | -- apply for controlled named access types because in those cases we | |
3097 | -- can use the finalization chain of the type itself. | |
3098 | ||
3099 | if Inside_A_Return_Statement (N) | |
3100 | and then | |
3101 | (Ekind (PtrT) = E_Anonymous_Access_Type | |
3102 | or else | |
3103 | (Ekind (PtrT) = E_Access_Type | |
3104 | and then No (Associated_Final_Chain (PtrT)))) | |
3105 | then | |
0669bebe | 3106 | declare |
26bff3d9 JM |
3107 | Decl : Node_Id; |
3108 | Outer_S : Entity_Id; | |
3109 | S : Entity_Id := Current_Scope; | |
0669bebe GB |
3110 | |
3111 | begin | |
26bff3d9 JM |
3112 | while Present (S) and then S /= Standard_Standard loop |
3113 | if Ekind (S) = E_Function then | |
3114 | Outer_S := Scope (S); | |
3115 | ||
3116 | -- Retrieve the declaration of the body | |
3117 | ||
8aec446b AC |
3118 | Decl := |
3119 | Parent | |
3120 | (Parent | |
3121 | (Corresponding_Body (Parent (Parent (S))))); | |
26bff3d9 JM |
3122 | exit; |
3123 | end if; | |
3124 | ||
3125 | S := Scope (S); | |
0669bebe GB |
3126 | end loop; |
3127 | ||
26bff3d9 JM |
3128 | -- Push the scope of the function body since we are inserting |
3129 | -- the list before the body, but we are currently in the body | |
3130 | -- itself. Override the finalization list of PtrT since the | |
3131 | -- finalization context is now different. | |
3132 | ||
3133 | Push_Scope (Outer_S); | |
3134 | Build_Final_List (Decl, PtrT); | |
3135 | Pop_Scope; | |
0669bebe GB |
3136 | end; |
3137 | ||
26bff3d9 JM |
3138 | -- The root allocator may not be controlled, but it still needs a |
3139 | -- finalization list for all nested coextensions. | |
0669bebe | 3140 | |
26bff3d9 JM |
3141 | elsif No (Associated_Final_Chain (PtrT)) then |
3142 | Build_Final_List (N, PtrT); | |
3143 | end if; | |
0669bebe | 3144 | |
26bff3d9 JM |
3145 | Flist := |
3146 | Make_Selected_Component (Loc, | |
3147 | Prefix => | |
3148 | New_Reference_To (Associated_Final_Chain (PtrT), Loc), | |
3149 | Selector_Name => | |
3150 | Make_Identifier (Loc, Name_F)); | |
3151 | ||
3152 | Coext_Elmt := First_Elmt (Coextensions (N)); | |
3153 | while Present (Coext_Elmt) loop | |
3154 | Coext := Node (Coext_Elmt); | |
3155 | ||
3156 | -- Generate: | |
3157 | -- typ! (coext.all) | |
3158 | ||
3159 | if Nkind (Coext) = N_Identifier then | |
685094bf RD |
3160 | Ref := |
3161 | Make_Unchecked_Type_Conversion (Loc, | |
3162 | Subtype_Mark => New_Reference_To (Etype (Coext), Loc), | |
3163 | Expression => | |
3164 | Make_Explicit_Dereference (Loc, | |
3165 | Prefix => New_Copy_Tree (Coext))); | |
26bff3d9 JM |
3166 | else |
3167 | Ref := New_Copy_Tree (Coext); | |
3168 | end if; | |
0669bebe | 3169 | |
b4592168 | 3170 | -- No initialization call if not allowed |
26bff3d9 | 3171 | |
b4592168 | 3172 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 | 3173 | |
b4592168 | 3174 | if not Restriction_Active (No_Default_Initialization) then |
26bff3d9 | 3175 | |
b4592168 GD |
3176 | -- Generate: |
3177 | -- initialize (Ref) | |
3178 | -- attach_to_final_list (Ref, Flist, 2) | |
3179 | ||
3180 | if Needs_Initialization_Call (Coext) then | |
3181 | Insert_Actions (N, | |
3182 | Make_Init_Call ( | |
3183 | Ref => Ref, | |
3184 | Typ => Etype (Coext), | |
3185 | Flist_Ref => Flist, | |
3186 | With_Attach => Make_Integer_Literal (Loc, Uint_2))); | |
3187 | ||
3188 | -- Generate: | |
3189 | -- attach_to_final_list (Ref, Flist, 2) | |
3190 | ||
3191 | else | |
3192 | Insert_Action (N, | |
3193 | Make_Attach_Call ( | |
3194 | Obj_Ref => Ref, | |
3195 | Flist_Ref => New_Copy_Tree (Flist), | |
3196 | With_Attach => Make_Integer_Literal (Loc, Uint_2))); | |
3197 | end if; | |
26bff3d9 JM |
3198 | end if; |
3199 | ||
3200 | Next_Elmt (Coext_Elmt); | |
3201 | end loop; | |
3202 | end Complete_Coextension_Finalization; | |
3203 | ||
3204 | ------------------------- | |
3205 | -- Rewrite_Coextension -- | |
3206 | ------------------------- | |
3207 | ||
3208 | procedure Rewrite_Coextension (N : Node_Id) is | |
3209 | Temp : constant Node_Id := | |
3210 | Make_Defining_Identifier (Loc, | |
3211 | New_Internal_Name ('C')); | |
3212 | ||
3213 | -- Generate: | |
3214 | -- Cnn : aliased Etyp; | |
3215 | ||
3216 | Decl : constant Node_Id := | |
3217 | Make_Object_Declaration (Loc, | |
3218 | Defining_Identifier => Temp, | |
3219 | Aliased_Present => True, | |
3220 | Object_Definition => | |
3221 | New_Occurrence_Of (Etyp, Loc)); | |
3222 | Nod : Node_Id; | |
3223 | ||
3224 | begin | |
3225 | if Nkind (Expression (N)) = N_Qualified_Expression then | |
3226 | Set_Expression (Decl, Expression (Expression (N))); | |
0669bebe | 3227 | end if; |
26bff3d9 JM |
3228 | |
3229 | -- Find the proper insertion node for the declaration | |
3230 | ||
3231 | Nod := Parent (N); | |
3232 | while Present (Nod) loop | |
3233 | exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call | |
3234 | or else Nkind (Nod) = N_Procedure_Call_Statement | |
3235 | or else Nkind (Nod) in N_Declaration; | |
3236 | Nod := Parent (Nod); | |
3237 | end loop; | |
3238 | ||
3239 | Insert_Before (Nod, Decl); | |
3240 | Analyze (Decl); | |
3241 | ||
3242 | Rewrite (N, | |
3243 | Make_Attribute_Reference (Loc, | |
3244 | Prefix => New_Occurrence_Of (Temp, Loc), | |
3245 | Attribute_Name => Name_Unrestricted_Access)); | |
3246 | ||
3247 | Analyze_And_Resolve (N, PtrT); | |
3248 | end Rewrite_Coextension; | |
0669bebe | 3249 | |
8aec446b AC |
3250 | ------------------------------ |
3251 | -- Size_In_Storage_Elements -- | |
3252 | ------------------------------ | |
3253 | ||
3254 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
3255 | begin | |
3256 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
3257 | -- However, the reason for the existence of this function is | |
3258 | -- to construct a test for sizes too large, which means near the | |
3259 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
3260 | -- is that we get overflows when sizes are greater than 2**31. | |
3261 | ||
507ed3fd | 3262 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
3263 | |
3264 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
3265 | ||
3266 | -- which avoids this problem. All this is a big bogus, but it does | |
3267 | -- mean we catch common cases of trying to allocate arrays that | |
3268 | -- are too large, and which in the absence of a check results in | |
3269 | -- undetected chaos ??? | |
3270 | ||
507ed3fd AC |
3271 | declare |
3272 | Len : Node_Id; | |
3273 | Res : Node_Id; | |
8aec446b | 3274 | |
507ed3fd AC |
3275 | begin |
3276 | for J in 1 .. Number_Dimensions (E) loop | |
3277 | Len := | |
3278 | Make_Attribute_Reference (Loc, | |
3279 | Prefix => New_Occurrence_Of (E, Loc), | |
3280 | Attribute_Name => Name_Length, | |
3281 | Expressions => New_List ( | |
3282 | Make_Integer_Literal (Loc, J))); | |
8aec446b | 3283 | |
507ed3fd AC |
3284 | if J = 1 then |
3285 | Res := Len; | |
8aec446b | 3286 | |
507ed3fd AC |
3287 | else |
3288 | Res := | |
3289 | Make_Op_Multiply (Loc, | |
3290 | Left_Opnd => Res, | |
3291 | Right_Opnd => Len); | |
3292 | end if; | |
3293 | end loop; | |
8aec446b | 3294 | |
8aec446b | 3295 | return |
507ed3fd AC |
3296 | Make_Op_Multiply (Loc, |
3297 | Left_Opnd => Len, | |
3298 | Right_Opnd => | |
3299 | Make_Attribute_Reference (Loc, | |
3300 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
3301 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
3302 | end; | |
8aec446b AC |
3303 | end Size_In_Storage_Elements; |
3304 | ||
0669bebe GB |
3305 | -- Start of processing for Expand_N_Allocator |
3306 | ||
70482933 RK |
3307 | begin |
3308 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
3309 | -- shall not be a remote access-to-class-wide-limited-private type | |
3310 | ||
3311 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
3312 | ||
3313 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
3314 | ||
3315 | -- Set the Storage Pool | |
3316 | ||
3317 | Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT))); | |
3318 | ||
3319 | if Present (Storage_Pool (N)) then | |
3320 | if Is_RTE (Storage_Pool (N), RE_SS_Pool) then | |
26bff3d9 | 3321 | if VM_Target = No_VM then |
70482933 RK |
3322 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); |
3323 | end if; | |
fbf5a39b AC |
3324 | |
3325 | elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then | |
3326 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
3327 | ||
70482933 RK |
3328 | else |
3329 | Set_Procedure_To_Call (N, | |
3330 | Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate)); | |
3331 | end if; | |
3332 | end if; | |
3333 | ||
685094bf RD |
3334 | -- Under certain circumstances we can replace an allocator by an access |
3335 | -- to statically allocated storage. The conditions, as noted in AARM | |
3336 | -- 3.10 (10c) are as follows: | |
70482933 RK |
3337 | |
3338 | -- Size and initial value is known at compile time | |
3339 | -- Access type is access-to-constant | |
3340 | ||
fbf5a39b AC |
3341 | -- The allocator is not part of a constraint on a record component, |
3342 | -- because in that case the inserted actions are delayed until the | |
3343 | -- record declaration is fully analyzed, which is too late for the | |
3344 | -- analysis of the rewritten allocator. | |
3345 | ||
70482933 RK |
3346 | if Is_Access_Constant (PtrT) |
3347 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
3348 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
3349 | and then Size_Known_At_Compile_Time (Etype (Expression | |
3350 | (Expression (N)))) | |
fbf5a39b | 3351 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
3352 | then |
3353 | -- Here we can do the optimization. For the allocator | |
3354 | ||
3355 | -- new x'(y) | |
3356 | ||
3357 | -- We insert an object declaration | |
3358 | ||
3359 | -- Tnn : aliased x := y; | |
3360 | ||
685094bf RD |
3361 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
3362 | -- marked as requiring static allocation. | |
70482933 RK |
3363 | |
3364 | Temp := | |
3365 | Make_Defining_Identifier (Loc, New_Internal_Name ('T')); | |
3366 | ||
3367 | Desig := Subtype_Mark (Expression (N)); | |
3368 | ||
3369 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 3370 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
3371 | -- unconstrained subtype. |
3372 | ||
0da2c8ac AC |
3373 | if Entity (Desig) = Base_Type (Dtyp) then |
3374 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
3375 | end if; |
3376 | ||
3377 | Insert_Action (N, | |
3378 | Make_Object_Declaration (Loc, | |
3379 | Defining_Identifier => Temp, | |
3380 | Aliased_Present => True, | |
3381 | Constant_Present => Is_Access_Constant (PtrT), | |
3382 | Object_Definition => Desig, | |
3383 | Expression => Expression (Expression (N)))); | |
3384 | ||
3385 | Rewrite (N, | |
3386 | Make_Attribute_Reference (Loc, | |
3387 | Prefix => New_Occurrence_Of (Temp, Loc), | |
3388 | Attribute_Name => Name_Unrestricted_Access)); | |
3389 | ||
3390 | Analyze_And_Resolve (N, PtrT); | |
3391 | ||
685094bf RD |
3392 | -- We set the variable as statically allocated, since we don't want |
3393 | -- it going on the stack of the current procedure! | |
70482933 RK |
3394 | |
3395 | Set_Is_Statically_Allocated (Temp); | |
3396 | return; | |
3397 | end if; | |
3398 | ||
0669bebe GB |
3399 | -- Same if the allocator is an access discriminant for a local object: |
3400 | -- instead of an allocator we create a local value and constrain the | |
3401 | -- the enclosing object with the corresponding access attribute. | |
3402 | ||
26bff3d9 JM |
3403 | if Is_Static_Coextension (N) then |
3404 | Rewrite_Coextension (N); | |
0669bebe GB |
3405 | return; |
3406 | end if; | |
3407 | ||
26bff3d9 JM |
3408 | -- The current allocator creates an object which may contain nested |
3409 | -- coextensions. Use the current allocator's finalization list to | |
3410 | -- generate finalization call for all nested coextensions. | |
3411 | ||
3412 | if Is_Coextension_Root (N) then | |
3413 | Complete_Coextension_Finalization; | |
3414 | end if; | |
3415 | ||
8aec446b AC |
3416 | -- Check for size too large, we do this because the back end misses |
3417 | -- proper checks here and can generate rubbish allocation calls when | |
3418 | -- we are near the limit. We only do this for the 32-bit address case | |
3419 | -- since that is from a practical point of view where we see a problem. | |
3420 | ||
3421 | if System_Address_Size = 32 | |
3422 | and then not Storage_Checks_Suppressed (PtrT) | |
3423 | and then not Storage_Checks_Suppressed (Dtyp) | |
3424 | and then not Storage_Checks_Suppressed (Etyp) | |
3425 | then | |
3426 | -- The check we want to generate should look like | |
3427 | ||
3428 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
3429 | -- raise Storage_Error; | |
3430 | -- end if; | |
3431 | ||
507ed3fd AC |
3432 | -- where 3.5 gigabytes is a constant large enough to accomodate any |
3433 | -- reasonable request for. But we can't do it this way because at | |
3434 | -- least at the moment we don't compute this attribute right, and | |
3435 | -- can silently give wrong results when the result gets large. Since | |
3436 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 3437 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 3438 | -- value of the attribute ??? |
8aec446b | 3439 | |
507ed3fd AC |
3440 | if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then |
3441 | Insert_Action (N, | |
3442 | Make_Raise_Storage_Error (Loc, | |
3443 | Condition => | |
3444 | Make_Op_Gt (Loc, | |
3445 | Left_Opnd => Size_In_Storage_Elements (Etyp), | |
3446 | Right_Opnd => | |
3447 | Make_Integer_Literal (Loc, | |
3448 | Intval => Uint_7 * (Uint_2 ** 29))), | |
3449 | Reason => SE_Object_Too_Large)); | |
3450 | end if; | |
8aec446b AC |
3451 | end if; |
3452 | ||
0da2c8ac AC |
3453 | -- Handle case of qualified expression (other than optimization above) |
3454 | ||
70482933 | 3455 | if Nkind (Expression (N)) = N_Qualified_Expression then |
fbf5a39b | 3456 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
3457 | return; |
3458 | end if; | |
fbf5a39b | 3459 | |
26bff3d9 JM |
3460 | -- If the allocator is for a type which requires initialization, and |
3461 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
3462 | -- rather than a qualified expression), then we must generate a call to |
3463 | -- the initialization routine using an expressions action node: | |
70482933 | 3464 | |
26bff3d9 | 3465 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 3466 | |
26bff3d9 JM |
3467 | -- Here ptr_T is the pointer type for the allocator, and T is the |
3468 | -- subtype of the allocator. A special case arises if the designated | |
3469 | -- type of the access type is a task or contains tasks. In this case | |
3470 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
3471 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
3472 | -- for details). In addition, if the type T is a task T, then the | |
3473 | -- first argument to Init must be converted to the task record type. | |
70482933 | 3474 | |
26bff3d9 JM |
3475 | declare |
3476 | T : constant Entity_Id := Entity (Expression (N)); | |
3477 | Init : Entity_Id; | |
3478 | Arg1 : Node_Id; | |
3479 | Args : List_Id; | |
3480 | Decls : List_Id; | |
3481 | Decl : Node_Id; | |
3482 | Discr : Elmt_Id; | |
3483 | Flist : Node_Id; | |
3484 | Temp_Decl : Node_Id; | |
3485 | Temp_Type : Entity_Id; | |
3486 | Attach_Level : Uint; | |
70482933 | 3487 | |
26bff3d9 JM |
3488 | begin |
3489 | if No_Initialization (N) then | |
3490 | null; | |
70482933 | 3491 | |
26bff3d9 | 3492 | -- Case of no initialization procedure present |
70482933 | 3493 | |
26bff3d9 | 3494 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 3495 | |
26bff3d9 | 3496 | -- Case of simple initialization required |
70482933 | 3497 | |
26bff3d9 | 3498 | if Needs_Simple_Initialization (T) then |
b4592168 | 3499 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
3500 | Rewrite (Expression (N), |
3501 | Make_Qualified_Expression (Loc, | |
3502 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 3503 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 3504 | |
26bff3d9 JM |
3505 | Analyze_And_Resolve (Expression (Expression (N)), T); |
3506 | Analyze_And_Resolve (Expression (N), T); | |
3507 | Set_Paren_Count (Expression (Expression (N)), 1); | |
3508 | Expand_N_Allocator (N); | |
70482933 | 3509 | |
26bff3d9 | 3510 | -- No initialization required |
70482933 RK |
3511 | |
3512 | else | |
26bff3d9 JM |
3513 | null; |
3514 | end if; | |
70482933 | 3515 | |
26bff3d9 | 3516 | -- Case of initialization procedure present, must be called |
70482933 | 3517 | |
26bff3d9 | 3518 | else |
b4592168 | 3519 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 3520 | |
b4592168 GD |
3521 | if not Restriction_Active (No_Default_Initialization) then |
3522 | Init := Base_Init_Proc (T); | |
3523 | Nod := N; | |
3524 | Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P')); | |
70482933 | 3525 | |
b4592168 | 3526 | -- Construct argument list for the initialization routine call |
70482933 | 3527 | |
26bff3d9 | 3528 | Arg1 := |
b4592168 GD |
3529 | Make_Explicit_Dereference (Loc, |
3530 | Prefix => New_Reference_To (Temp, Loc)); | |
3531 | Set_Assignment_OK (Arg1); | |
3532 | Temp_Type := PtrT; | |
26bff3d9 | 3533 | |
b4592168 GD |
3534 | -- The initialization procedure expects a specific type. if the |
3535 | -- context is access to class wide, indicate that the object | |
3536 | -- being allocated has the right specific type. | |
70482933 | 3537 | |
b4592168 GD |
3538 | if Is_Class_Wide_Type (Dtyp) then |
3539 | Arg1 := Unchecked_Convert_To (T, Arg1); | |
3540 | end if; | |
70482933 | 3541 | |
b4592168 GD |
3542 | -- If designated type is a concurrent type or if it is private |
3543 | -- type whose definition is a concurrent type, the first | |
3544 | -- argument in the Init routine has to be unchecked conversion | |
3545 | -- to the corresponding record type. If the designated type is | |
3546 | -- a derived type, we also convert the argument to its root | |
3547 | -- type. | |
20b5d666 | 3548 | |
b4592168 GD |
3549 | if Is_Concurrent_Type (T) then |
3550 | Arg1 := | |
3551 | Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1); | |
70482933 | 3552 | |
b4592168 GD |
3553 | elsif Is_Private_Type (T) |
3554 | and then Present (Full_View (T)) | |
3555 | and then Is_Concurrent_Type (Full_View (T)) | |
3556 | then | |
3557 | Arg1 := | |
3558 | Unchecked_Convert_To | |
3559 | (Corresponding_Record_Type (Full_View (T)), Arg1); | |
70482933 | 3560 | |
b4592168 GD |
3561 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
3562 | declare | |
3563 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
3564 | begin | |
3565 | Arg1 := OK_Convert_To (Etype (Ftyp), Arg1); | |
3566 | Set_Etype (Arg1, Ftyp); | |
3567 | end; | |
3568 | end if; | |
70482933 | 3569 | |
b4592168 | 3570 | Args := New_List (Arg1); |
70482933 | 3571 | |
b4592168 GD |
3572 | -- For the task case, pass the Master_Id of the access type as |
3573 | -- the value of the _Master parameter, and _Chain as the value | |
3574 | -- of the _Chain parameter (_Chain will be defined as part of | |
3575 | -- the generated code for the allocator). | |
70482933 | 3576 | |
b4592168 GD |
3577 | -- In Ada 2005, the context may be a function that returns an |
3578 | -- anonymous access type. In that case the Master_Id has been | |
3579 | -- created when expanding the function declaration. | |
70482933 | 3580 | |
b4592168 GD |
3581 | if Has_Task (T) then |
3582 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 3583 | |
b4592168 GD |
3584 | -- If we have a non-library level task with restriction |
3585 | -- No_Task_Hierarchy set, then no point in expanding. | |
70482933 | 3586 | |
b4592168 GD |
3587 | if not Is_Library_Level_Entity (T) |
3588 | and then Restriction_Active (No_Task_Hierarchy) | |
26bff3d9 | 3589 | then |
b4592168 | 3590 | return; |
26bff3d9 | 3591 | end if; |
70482933 | 3592 | |
b4592168 GD |
3593 | -- The designated type was an incomplete type, and the |
3594 | -- access type did not get expanded. Salvage it now. | |
70482933 | 3595 | |
b4592168 GD |
3596 | pragma Assert (Present (Parent (Base_Type (PtrT)))); |
3597 | Expand_N_Full_Type_Declaration | |
3598 | (Parent (Base_Type (PtrT))); | |
3599 | end if; | |
70482933 | 3600 | |
b4592168 GD |
3601 | -- If the context of the allocator is a declaration or an |
3602 | -- assignment, we can generate a meaningful image for it, | |
3603 | -- even though subsequent assignments might remove the | |
3604 | -- connection between task and entity. We build this image | |
3605 | -- when the left-hand side is a simple variable, a simple | |
3606 | -- indexed assignment or a simple selected component. | |
3607 | ||
3608 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
3609 | declare | |
3610 | Nam : constant Node_Id := Name (Parent (N)); | |
3611 | ||
3612 | begin | |
3613 | if Is_Entity_Name (Nam) then | |
3614 | Decls := | |
3615 | Build_Task_Image_Decls | |
3616 | (Loc, | |
3617 | New_Occurrence_Of | |
3618 | (Entity (Nam), Sloc (Nam)), T); | |
3619 | ||
3620 | elsif Nkind_In | |
3621 | (Nam, N_Indexed_Component, N_Selected_Component) | |
3622 | and then Is_Entity_Name (Prefix (Nam)) | |
3623 | then | |
3624 | Decls := | |
3625 | Build_Task_Image_Decls | |
3626 | (Loc, Nam, Etype (Prefix (Nam))); | |
3627 | else | |
3628 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3629 | end if; | |
3630 | end; | |
70482933 | 3631 | |
b4592168 GD |
3632 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
3633 | Decls := | |
3634 | Build_Task_Image_Decls | |
3635 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 3636 | |
b4592168 GD |
3637 | else |
3638 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3639 | end if; | |
26bff3d9 | 3640 | |
b4592168 GD |
3641 | Append_To (Args, |
3642 | New_Reference_To | |
3643 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); | |
3644 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); | |
26bff3d9 | 3645 | |
b4592168 GD |
3646 | Decl := Last (Decls); |
3647 | Append_To (Args, | |
3648 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 3649 | |
b4592168 | 3650 | -- Has_Task is false, Decls not used |
26bff3d9 | 3651 | |
b4592168 GD |
3652 | else |
3653 | Decls := No_List; | |
26bff3d9 JM |
3654 | end if; |
3655 | ||
b4592168 GD |
3656 | -- Add discriminants if discriminated type |
3657 | ||
3658 | declare | |
3659 | Dis : Boolean := False; | |
3660 | Typ : Entity_Id; | |
3661 | ||
3662 | begin | |
3663 | if Has_Discriminants (T) then | |
3664 | Dis := True; | |
3665 | Typ := T; | |
3666 | ||
3667 | elsif Is_Private_Type (T) | |
3668 | and then Present (Full_View (T)) | |
3669 | and then Has_Discriminants (Full_View (T)) | |
20b5d666 | 3670 | then |
b4592168 GD |
3671 | Dis := True; |
3672 | Typ := Full_View (T); | |
20b5d666 | 3673 | end if; |
70482933 | 3674 | |
b4592168 | 3675 | if Dis then |
26bff3d9 | 3676 | |
b4592168 | 3677 | -- If the allocated object will be constrained by the |
685094bf RD |
3678 | -- default values for discriminants, then build a subtype |
3679 | -- with those defaults, and change the allocated subtype | |
3680 | -- to that. Note that this happens in fewer cases in Ada | |
3681 | -- 2005 (AI-363). | |
26bff3d9 | 3682 | |
b4592168 GD |
3683 | if not Is_Constrained (Typ) |
3684 | and then Present (Discriminant_Default_Value | |
3685 | (First_Discriminant (Typ))) | |
3686 | and then (Ada_Version < Ada_05 | |
3687 | or else | |
3688 | not Has_Constrained_Partial_View (Typ)) | |
20b5d666 | 3689 | then |
b4592168 GD |
3690 | Typ := Build_Default_Subtype (Typ, N); |
3691 | Set_Expression (N, New_Reference_To (Typ, Loc)); | |
20b5d666 JM |
3692 | end if; |
3693 | ||
b4592168 GD |
3694 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
3695 | while Present (Discr) loop | |
3696 | Nod := Node (Discr); | |
3697 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 3698 | |
b4592168 GD |
3699 | -- AI-416: when the discriminant constraint is an |
3700 | -- anonymous access type make sure an accessibility | |
3701 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 3702 | |
b4592168 GD |
3703 | if Ada_Version >= Ada_05 |
3704 | and then | |
3705 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
3706 | then | |
e84e11ba GD |
3707 | Apply_Accessibility_Check |
3708 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 3709 | end if; |
20b5d666 | 3710 | |
b4592168 GD |
3711 | Next_Elmt (Discr); |
3712 | end loop; | |
3713 | end if; | |
3714 | end; | |
70482933 | 3715 | |
b4592168 GD |
3716 | -- We set the allocator as analyzed so that when we analyze the |
3717 | -- expression actions node, we do not get an unwanted recursive | |
3718 | -- expansion of the allocator expression. | |
70482933 | 3719 | |
b4592168 GD |
3720 | Set_Analyzed (N, True); |
3721 | Nod := Relocate_Node (N); | |
70482933 | 3722 | |
b4592168 GD |
3723 | -- Here is the transformation: |
3724 | -- input: new T | |
3725 | -- output: Temp : constant ptr_T := new T; | |
3726 | -- Init (Temp.all, ...); | |
3727 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
3728 | -- <CTRL> Initialize (Finalizable (Temp.all)); | |
70482933 | 3729 | |
b4592168 GD |
3730 | -- Here ptr_T is the pointer type for the allocator, and is the |
3731 | -- subtype of the allocator. | |
70482933 | 3732 | |
b4592168 GD |
3733 | Temp_Decl := |
3734 | Make_Object_Declaration (Loc, | |
3735 | Defining_Identifier => Temp, | |
3736 | Constant_Present => True, | |
3737 | Object_Definition => New_Reference_To (Temp_Type, Loc), | |
3738 | Expression => Nod); | |
70482933 | 3739 | |
b4592168 GD |
3740 | Set_Assignment_OK (Temp_Decl); |
3741 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 3742 | |
b4592168 GD |
3743 | -- If the designated type is a task type or contains tasks, |
3744 | -- create block to activate created tasks, and insert | |
3745 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 3746 | |
b4592168 GD |
3747 | if Has_Task (T) then |
3748 | declare | |
3749 | L : constant List_Id := New_List; | |
3750 | Blk : Node_Id; | |
3751 | begin | |
3752 | Build_Task_Allocate_Block (L, Nod, Args); | |
3753 | Blk := Last (L); | |
3754 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
3755 | Insert_Actions (N, L); | |
3756 | end; | |
70482933 | 3757 | |
b4592168 GD |
3758 | else |
3759 | Insert_Action (N, | |
3760 | Make_Procedure_Call_Statement (Loc, | |
3761 | Name => New_Reference_To (Init, Loc), | |
3762 | Parameter_Associations => Args)); | |
3763 | end if; | |
70482933 | 3764 | |
048e5cef | 3765 | if Needs_Finalization (T) then |
70482933 | 3766 | |
b4592168 GD |
3767 | -- Postpone the generation of a finalization call for the |
3768 | -- current allocator if it acts as a coextension. | |
26bff3d9 | 3769 | |
b4592168 GD |
3770 | if Is_Dynamic_Coextension (N) then |
3771 | if No (Coextensions (N)) then | |
3772 | Set_Coextensions (N, New_Elmt_List); | |
3773 | end if; | |
70482933 | 3774 | |
b4592168 GD |
3775 | Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N)); |
3776 | ||
3777 | else | |
3778 | Flist := | |
3779 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
0669bebe | 3780 | |
b4592168 GD |
3781 | -- Anonymous access types created for access parameters |
3782 | -- are attached to an explicitly constructed controller, | |
3783 | -- which ensures that they can be finalized properly, | |
3784 | -- even if their deallocation might not happen. The list | |
3785 | -- associated with the controller is doubly-linked. For | |
3786 | -- other anonymous access types, the object may end up | |
3787 | -- on the global final list which is singly-linked. | |
3788 | -- Work needed for access discriminants in Ada 2005 ??? | |
0669bebe | 3789 | |
a523b302 | 3790 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
b4592168 GD |
3791 | Attach_Level := Uint_1; |
3792 | else | |
3793 | Attach_Level := Uint_2; | |
3794 | end if; | |
0669bebe | 3795 | |
b4592168 GD |
3796 | Insert_Actions (N, |
3797 | Make_Init_Call ( | |
3798 | Ref => New_Copy_Tree (Arg1), | |
3799 | Typ => T, | |
3800 | Flist_Ref => Flist, | |
3801 | With_Attach => Make_Integer_Literal (Loc, | |
3802 | Intval => Attach_Level))); | |
3803 | end if; | |
70482933 RK |
3804 | end if; |
3805 | ||
b4592168 GD |
3806 | Rewrite (N, New_Reference_To (Temp, Loc)); |
3807 | Analyze_And_Resolve (N, PtrT); | |
3808 | end if; | |
26bff3d9 JM |
3809 | end if; |
3810 | end; | |
f82944b7 | 3811 | |
26bff3d9 JM |
3812 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
3813 | -- object that has been rewritten as a reference, we displace "this" | |
3814 | -- to reference properly its secondary dispatch table. | |
3815 | ||
3816 | if Nkind (N) = N_Identifier | |
f82944b7 JM |
3817 | and then Is_Interface (Dtyp) |
3818 | then | |
26bff3d9 | 3819 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
3820 | end if; |
3821 | ||
fbf5a39b AC |
3822 | exception |
3823 | when RE_Not_Available => | |
3824 | return; | |
70482933 RK |
3825 | end Expand_N_Allocator; |
3826 | ||
3827 | ----------------------- | |
3828 | -- Expand_N_And_Then -- | |
3829 | ----------------------- | |
3830 | ||
20b5d666 JM |
3831 | -- Expand into conditional expression if Actions present, and also deal |
3832 | -- with optimizing case of arguments being True or False. | |
70482933 RK |
3833 | |
3834 | procedure Expand_N_And_Then (N : Node_Id) is | |
3835 | Loc : constant Source_Ptr := Sloc (N); | |
3836 | Typ : constant Entity_Id := Etype (N); | |
3837 | Left : constant Node_Id := Left_Opnd (N); | |
3838 | Right : constant Node_Id := Right_Opnd (N); | |
3839 | Actlist : List_Id; | |
3840 | ||
3841 | begin | |
3842 | -- Deal with non-standard booleans | |
3843 | ||
3844 | if Is_Boolean_Type (Typ) then | |
3845 | Adjust_Condition (Left); | |
3846 | Adjust_Condition (Right); | |
3847 | Set_Etype (N, Standard_Boolean); | |
3848 | end if; | |
3849 | ||
deff3e5e | 3850 | -- Check for cases where left argument is known to be True or False |
70482933 | 3851 | |
deff3e5e | 3852 | if Compile_Time_Known_Value (Left) then |
70482933 RK |
3853 | |
3854 | -- If left argument is True, change (True and then Right) to Right. | |
3855 | -- Any actions associated with Right will be executed unconditionally | |
3856 | -- and can thus be inserted into the tree unconditionally. | |
3857 | ||
deff3e5e | 3858 | if Expr_Value_E (Left) = Standard_True then |
70482933 RK |
3859 | if Present (Actions (N)) then |
3860 | Insert_Actions (N, Actions (N)); | |
3861 | end if; | |
3862 | ||
3863 | Rewrite (N, Right); | |
70482933 | 3864 | |
20b5d666 JM |
3865 | -- If left argument is False, change (False and then Right) to False. |
3866 | -- In this case we can forget the actions associated with Right, | |
3867 | -- since they will never be executed. | |
70482933 | 3868 | |
deff3e5e | 3869 | else pragma Assert (Expr_Value_E (Left) = Standard_False); |
70482933 RK |
3870 | Kill_Dead_Code (Right); |
3871 | Kill_Dead_Code (Actions (N)); | |
3872 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
70482933 | 3873 | end if; |
deff3e5e BD |
3874 | |
3875 | Adjust_Result_Type (N, Typ); | |
3876 | return; | |
70482933 RK |
3877 | end if; |
3878 | ||
3879 | -- If Actions are present, we expand | |
3880 | ||
3881 | -- left and then right | |
3882 | ||
3883 | -- into | |
3884 | ||
3885 | -- if left then right else false end | |
3886 | ||
3887 | -- with the actions becoming the Then_Actions of the conditional | |
3888 | -- expression. This conditional expression is then further expanded | |
3889 | -- (and will eventually disappear) | |
3890 | ||
3891 | if Present (Actions (N)) then | |
3892 | Actlist := Actions (N); | |
3893 | Rewrite (N, | |
3894 | Make_Conditional_Expression (Loc, | |
3895 | Expressions => New_List ( | |
3896 | Left, | |
3897 | Right, | |
3898 | New_Occurrence_Of (Standard_False, Loc)))); | |
3899 | ||
3900 | Set_Then_Actions (N, Actlist); | |
3901 | Analyze_And_Resolve (N, Standard_Boolean); | |
3902 | Adjust_Result_Type (N, Typ); | |
3903 | return; | |
3904 | end if; | |
3905 | ||
3906 | -- No actions present, check for cases of right argument True/False | |
3907 | ||
deff3e5e | 3908 | if Compile_Time_Known_Value (Right) then |
70482933 | 3909 | |
685094bf RD |
3910 | -- Change (Left and then True) to Left. Note that we know there are |
3911 | -- no actions associated with the True operand, since we just checked | |
3912 | -- for this case above. | |
70482933 | 3913 | |
deff3e5e | 3914 | if Expr_Value_E (Right) = Standard_True then |
70482933 RK |
3915 | Rewrite (N, Left); |
3916 | ||
685094bf RD |
3917 | -- Change (Left and then False) to False, making sure to preserve any |
3918 | -- side effects associated with the Left operand. | |
70482933 | 3919 | |
deff3e5e | 3920 | else pragma Assert (Expr_Value_E (Right) = Standard_False); |
70482933 RK |
3921 | Remove_Side_Effects (Left); |
3922 | Rewrite | |
3923 | (N, New_Occurrence_Of (Standard_False, Loc)); | |
3924 | end if; | |
3925 | end if; | |
3926 | ||
3927 | Adjust_Result_Type (N, Typ); | |
3928 | end Expand_N_And_Then; | |
3929 | ||
3930 | ------------------------------------- | |
3931 | -- Expand_N_Conditional_Expression -- | |
3932 | ------------------------------------- | |
3933 | ||
3934 | -- Expand into expression actions if then/else actions present | |
3935 | ||
3936 | procedure Expand_N_Conditional_Expression (N : Node_Id) is | |
3937 | Loc : constant Source_Ptr := Sloc (N); | |
3938 | Cond : constant Node_Id := First (Expressions (N)); | |
3939 | Thenx : constant Node_Id := Next (Cond); | |
3940 | Elsex : constant Node_Id := Next (Thenx); | |
3941 | Typ : constant Entity_Id := Etype (N); | |
3942 | Cnn : Entity_Id; | |
3943 | New_If : Node_Id; | |
3944 | ||
3945 | begin | |
3946 | -- If either then or else actions are present, then given: | |
3947 | ||
3948 | -- if cond then then-expr else else-expr end | |
3949 | ||
3950 | -- we insert the following sequence of actions (using Insert_Actions): | |
3951 | ||
3952 | -- Cnn : typ; | |
3953 | -- if cond then | |
3954 | -- <<then actions>> | |
3955 | -- Cnn := then-expr; | |
3956 | -- else | |
3957 | -- <<else actions>> | |
3958 | -- Cnn := else-expr | |
3959 | -- end if; | |
3960 | ||
2717634d | 3961 | -- and replace the conditional expression by a reference to Cnn |
70482933 RK |
3962 | |
3963 | if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
3964 | Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C')); | |
3965 | ||
3966 | New_If := | |
3967 | Make_Implicit_If_Statement (N, | |
3968 | Condition => Relocate_Node (Cond), | |
3969 | ||
3970 | Then_Statements => New_List ( | |
3971 | Make_Assignment_Statement (Sloc (Thenx), | |
3972 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
3973 | Expression => Relocate_Node (Thenx))), | |
3974 | ||
3975 | Else_Statements => New_List ( | |
3976 | Make_Assignment_Statement (Sloc (Elsex), | |
3977 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
3978 | Expression => Relocate_Node (Elsex)))); | |
3979 | ||
fbf5a39b AC |
3980 | Set_Assignment_OK (Name (First (Then_Statements (New_If)))); |
3981 | Set_Assignment_OK (Name (First (Else_Statements (New_If)))); | |
3982 | ||
70482933 RK |
3983 | if Present (Then_Actions (N)) then |
3984 | Insert_List_Before | |
3985 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
3986 | end if; | |
3987 | ||
3988 | if Present (Else_Actions (N)) then | |
3989 | Insert_List_Before | |
3990 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
3991 | end if; | |
3992 | ||
3993 | Rewrite (N, New_Occurrence_Of (Cnn, Loc)); | |
3994 | ||
3995 | Insert_Action (N, | |
3996 | Make_Object_Declaration (Loc, | |
3997 | Defining_Identifier => Cnn, | |
3998 | Object_Definition => New_Occurrence_Of (Typ, Loc))); | |
3999 | ||
4000 | Insert_Action (N, New_If); | |
4001 | Analyze_And_Resolve (N, Typ); | |
4002 | end if; | |
4003 | end Expand_N_Conditional_Expression; | |
4004 | ||
4005 | ----------------------------------- | |
4006 | -- Expand_N_Explicit_Dereference -- | |
4007 | ----------------------------------- | |
4008 | ||
4009 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
4010 | begin | |
dfd99a80 | 4011 | -- Insert explicit dereference call for the checked storage pool case |
70482933 RK |
4012 | |
4013 | Insert_Dereference_Action (Prefix (N)); | |
4014 | end Expand_N_Explicit_Dereference; | |
4015 | ||
4016 | ----------------- | |
4017 | -- Expand_N_In -- | |
4018 | ----------------- | |
4019 | ||
4020 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 AC |
4021 | Loc : constant Source_Ptr := Sloc (N); |
4022 | Rtyp : constant Entity_Id := Etype (N); | |
4023 | Lop : constant Node_Id := Left_Opnd (N); | |
4024 | Rop : constant Node_Id := Right_Opnd (N); | |
4025 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 4026 | |
630d30e9 RD |
4027 | procedure Substitute_Valid_Check; |
4028 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
4029 | -- test for the left operand being in range of its subtype. | |
4030 | ||
4031 | ---------------------------- | |
4032 | -- Substitute_Valid_Check -- | |
4033 | ---------------------------- | |
4034 | ||
4035 | procedure Substitute_Valid_Check is | |
4036 | begin | |
4037 | Rewrite (N, | |
4038 | Make_Attribute_Reference (Loc, | |
4039 | Prefix => Relocate_Node (Lop), | |
4040 | Attribute_Name => Name_Valid)); | |
4041 | ||
4042 | Analyze_And_Resolve (N, Rtyp); | |
4043 | ||
4044 | Error_Msg_N ("?explicit membership test may be optimized away", N); | |
4045 | Error_Msg_N ("\?use ''Valid attribute instead", N); | |
4046 | return; | |
4047 | end Substitute_Valid_Check; | |
4048 | ||
4049 | -- Start of processing for Expand_N_In | |
4050 | ||
70482933 | 4051 | begin |
630d30e9 RD |
4052 | -- Check case of explicit test for an expression in range of its |
4053 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
4054 | -- test and give a warning. | |
4055 | ||
4056 | if Is_Scalar_Type (Etype (Lop)) | |
4057 | and then Nkind (Rop) in N_Has_Entity | |
4058 | and then Etype (Lop) = Entity (Rop) | |
4059 | and then Comes_From_Source (N) | |
26bff3d9 | 4060 | and then VM_Target = No_VM |
630d30e9 RD |
4061 | then |
4062 | Substitute_Valid_Check; | |
4063 | return; | |
4064 | end if; | |
4065 | ||
20b5d666 JM |
4066 | -- Do validity check on operands |
4067 | ||
4068 | if Validity_Checks_On and Validity_Check_Operands then | |
4069 | Ensure_Valid (Left_Opnd (N)); | |
4070 | Validity_Check_Range (Right_Opnd (N)); | |
4071 | end if; | |
4072 | ||
630d30e9 | 4073 | -- Case of explicit range |
fbf5a39b AC |
4074 | |
4075 | if Nkind (Rop) = N_Range then | |
4076 | declare | |
630d30e9 RD |
4077 | Lo : constant Node_Id := Low_Bound (Rop); |
4078 | Hi : constant Node_Id := High_Bound (Rop); | |
4079 | ||
d766cee3 RD |
4080 | Ltyp : constant Entity_Id := Etype (Lop); |
4081 | ||
630d30e9 RD |
4082 | Lo_Orig : constant Node_Id := Original_Node (Lo); |
4083 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
4084 | ||
c800f862 RD |
4085 | Lcheck : Compare_Result; |
4086 | Ucheck : Compare_Result; | |
fbf5a39b | 4087 | |
d766cee3 RD |
4088 | Warn1 : constant Boolean := |
4089 | Constant_Condition_Warnings | |
c800f862 RD |
4090 | and then Comes_From_Source (N) |
4091 | and then not In_Instance; | |
d766cee3 RD |
4092 | -- This must be true for any of the optimization warnings, we |
4093 | -- clearly want to give them only for source with the flag on. | |
c800f862 RD |
4094 | -- We also skip these warnings in an instance since it may be |
4095 | -- the case that different instantiations have different ranges. | |
d766cee3 RD |
4096 | |
4097 | Warn2 : constant Boolean := | |
4098 | Warn1 | |
4099 | and then Nkind (Original_Node (Rop)) = N_Range | |
4100 | and then Is_Integer_Type (Etype (Lo)); | |
4101 | -- For the case where only one bound warning is elided, we also | |
4102 | -- insist on an explicit range and an integer type. The reason is | |
4103 | -- that the use of enumeration ranges including an end point is | |
4104 | -- common, as is the use of a subtype name, one of whose bounds | |
4105 | -- is the same as the type of the expression. | |
4106 | ||
fbf5a39b | 4107 | begin |
630d30e9 RD |
4108 | -- If test is explicit x'first .. x'last, replace by valid check |
4109 | ||
d766cee3 | 4110 | if Is_Scalar_Type (Ltyp) |
630d30e9 RD |
4111 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
4112 | and then Attribute_Name (Lo_Orig) = Name_First | |
4113 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 4114 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
630d30e9 RD |
4115 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
4116 | and then Attribute_Name (Hi_Orig) = Name_Last | |
4117 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 4118 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
630d30e9 | 4119 | and then Comes_From_Source (N) |
26bff3d9 | 4120 | and then VM_Target = No_VM |
630d30e9 RD |
4121 | then |
4122 | Substitute_Valid_Check; | |
4123 | return; | |
4124 | end if; | |
4125 | ||
d766cee3 RD |
4126 | -- If bounds of type are known at compile time, and the end points |
4127 | -- are known at compile time and identical, this is another case | |
4128 | -- for substituting a valid test. We only do this for discrete | |
4129 | -- types, since it won't arise in practice for float types. | |
4130 | ||
4131 | if Comes_From_Source (N) | |
4132 | and then Is_Discrete_Type (Ltyp) | |
4133 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
4134 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
4135 | and then Compile_Time_Known_Value (Lo) | |
4136 | and then Compile_Time_Known_Value (Hi) | |
4137 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
4138 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e RD |
4139 | |
4140 | -- Kill warnings in instances, since they may be cases where we | |
4141 | -- have a test in the generic that makes sense with some types | |
4142 | -- and not with other types. | |
4143 | ||
4144 | and then not In_Instance | |
d766cee3 RD |
4145 | then |
4146 | Substitute_Valid_Check; | |
4147 | return; | |
4148 | end if; | |
4149 | ||
630d30e9 RD |
4150 | -- If we have an explicit range, do a bit of optimization based |
4151 | -- on range analysis (we may be able to kill one or both checks). | |
4152 | ||
c800f862 RD |
4153 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
4154 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
4155 | ||
630d30e9 RD |
4156 | -- If either check is known to fail, replace result by False since |
4157 | -- the other check does not matter. Preserve the static flag for | |
4158 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
4159 | |
4160 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 4161 | if Warn1 then |
d766cee3 RD |
4162 | Error_Msg_N ("?range test optimized away", N); |
4163 | Error_Msg_N ("\?value is known to be out of range", N); | |
4164 | end if; | |
4165 | ||
fbf5a39b AC |
4166 | Rewrite (N, |
4167 | New_Reference_To (Standard_False, Loc)); | |
4168 | Analyze_And_Resolve (N, Rtyp); | |
7324bf49 | 4169 | Set_Is_Static_Expression (N, Static); |
d766cee3 | 4170 | |
fbf5a39b AC |
4171 | return; |
4172 | ||
685094bf RD |
4173 | -- If both checks are known to succeed, replace result by True, |
4174 | -- since we know we are in range. | |
fbf5a39b AC |
4175 | |
4176 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 4177 | if Warn1 then |
d766cee3 RD |
4178 | Error_Msg_N ("?range test optimized away", N); |
4179 | Error_Msg_N ("\?value is known to be in range", N); | |
4180 | end if; | |
4181 | ||
fbf5a39b AC |
4182 | Rewrite (N, |
4183 | New_Reference_To (Standard_True, Loc)); | |
4184 | Analyze_And_Resolve (N, Rtyp); | |
7324bf49 | 4185 | Set_Is_Static_Expression (N, Static); |
d766cee3 | 4186 | |
fbf5a39b AC |
4187 | return; |
4188 | ||
d766cee3 RD |
4189 | -- If lower bound check succeeds and upper bound check is not |
4190 | -- known to succeed or fail, then replace the range check with | |
4191 | -- a comparison against the upper bound. | |
fbf5a39b AC |
4192 | |
4193 | elsif Lcheck in Compare_GE then | |
94eefd2e | 4194 | if Warn2 and then not In_Instance then |
d766cee3 RD |
4195 | Error_Msg_N ("?lower bound test optimized away", Lo); |
4196 | Error_Msg_N ("\?value is known to be in range", Lo); | |
4197 | end if; | |
4198 | ||
fbf5a39b AC |
4199 | Rewrite (N, |
4200 | Make_Op_Le (Loc, | |
4201 | Left_Opnd => Lop, | |
4202 | Right_Opnd => High_Bound (Rop))); | |
4203 | Analyze_And_Resolve (N, Rtyp); | |
d766cee3 | 4204 | |
fbf5a39b AC |
4205 | return; |
4206 | ||
d766cee3 RD |
4207 | -- If upper bound check succeeds and lower bound check is not |
4208 | -- known to succeed or fail, then replace the range check with | |
4209 | -- a comparison against the lower bound. | |
fbf5a39b AC |
4210 | |
4211 | elsif Ucheck in Compare_LE then | |
94eefd2e | 4212 | if Warn2 and then not In_Instance then |
d766cee3 RD |
4213 | Error_Msg_N ("?upper bound test optimized away", Hi); |
4214 | Error_Msg_N ("\?value is known to be in range", Hi); | |
4215 | end if; | |
4216 | ||
fbf5a39b AC |
4217 | Rewrite (N, |
4218 | Make_Op_Ge (Loc, | |
4219 | Left_Opnd => Lop, | |
4220 | Right_Opnd => Low_Bound (Rop))); | |
4221 | Analyze_And_Resolve (N, Rtyp); | |
d766cee3 | 4222 | |
fbf5a39b AC |
4223 | return; |
4224 | end if; | |
c800f862 RD |
4225 | |
4226 | -- We couldn't optimize away the range check, but there is one | |
4227 | -- more issue. If we are checking constant conditionals, then we | |
4228 | -- see if we can determine the outcome assuming everything is | |
4229 | -- valid, and if so give an appropriate warning. | |
4230 | ||
4231 | if Warn1 and then not Assume_No_Invalid_Values then | |
4232 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
4233 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
4234 | ||
4235 | -- Result is out of range for valid value | |
4236 | ||
4237 | if Lcheck = LT or else Ucheck = GT then | |
4238 | Error_Msg_N | |
4239 | ("?value can only be in range if it is invalid", N); | |
4240 | ||
4241 | -- Result is in range for valid value | |
4242 | ||
4243 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
4244 | Error_Msg_N | |
4245 | ("?value can only be out of range if it is invalid", N); | |
4246 | ||
4247 | -- Lower bound check succeeds if value is valid | |
4248 | ||
4249 | elsif Warn2 and then Lcheck in Compare_GE then | |
4250 | Error_Msg_N | |
4251 | ("?lower bound check only fails if it is invalid", Lo); | |
4252 | ||
4253 | -- Upper bound check succeeds if value is valid | |
4254 | ||
4255 | elsif Warn2 and then Ucheck in Compare_LE then | |
4256 | Error_Msg_N | |
4257 | ("?upper bound check only fails for invalid values", Hi); | |
4258 | end if; | |
4259 | end if; | |
fbf5a39b AC |
4260 | end; |
4261 | ||
4262 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 4263 | |
70482933 RK |
4264 | return; |
4265 | ||
4266 | -- Here right operand is a subtype mark | |
4267 | ||
4268 | else | |
4269 | declare | |
fbf5a39b AC |
4270 | Typ : Entity_Id := Etype (Rop); |
4271 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
4272 | Obj : Node_Id := Lop; | |
4273 | Cond : Node_Id := Empty; | |
70482933 RK |
4274 | |
4275 | begin | |
4276 | Remove_Side_Effects (Obj); | |
4277 | ||
4278 | -- For tagged type, do tagged membership operation | |
4279 | ||
4280 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 4281 | |
26bff3d9 JM |
4282 | -- No expansion will be performed when VM_Target, as the VM |
4283 | -- back-ends will handle the membership tests directly (tags | |
4284 | -- are not explicitly represented in Java objects, so the | |
4285 | -- normal tagged membership expansion is not what we want). | |
70482933 | 4286 | |
26bff3d9 | 4287 | if VM_Target = No_VM then |
70482933 RK |
4288 | Rewrite (N, Tagged_Membership (N)); |
4289 | Analyze_And_Resolve (N, Rtyp); | |
4290 | end if; | |
4291 | ||
4292 | return; | |
4293 | ||
20b5d666 | 4294 | -- If type is scalar type, rewrite as x in t'first .. t'last. |
70482933 | 4295 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
4296 | -- type if they come from the original type definition. Also this |
4297 | -- way we get all the processing above for an explicit range. | |
70482933 RK |
4298 | |
4299 | elsif Is_Scalar_Type (Typ) then | |
fbf5a39b | 4300 | Rewrite (Rop, |
70482933 RK |
4301 | Make_Range (Loc, |
4302 | Low_Bound => | |
4303 | Make_Attribute_Reference (Loc, | |
4304 | Attribute_Name => Name_First, | |
4305 | Prefix => New_Reference_To (Typ, Loc)), | |
4306 | ||
4307 | High_Bound => | |
4308 | Make_Attribute_Reference (Loc, | |
4309 | Attribute_Name => Name_Last, | |
4310 | Prefix => New_Reference_To (Typ, Loc)))); | |
4311 | Analyze_And_Resolve (N, Rtyp); | |
4312 | return; | |
5d09245e AC |
4313 | |
4314 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
4315 | -- a membership test if the subtype mark denotes a constrained | |
4316 | -- Unchecked_Union subtype and the expression lacks inferable | |
4317 | -- discriminants. | |
4318 | ||
4319 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
4320 | and then Is_Constrained (Typ) | |
4321 | and then not Has_Inferable_Discriminants (Lop) | |
4322 | then | |
4323 | Insert_Action (N, | |
4324 | Make_Raise_Program_Error (Loc, | |
4325 | Reason => PE_Unchecked_Union_Restriction)); | |
4326 | ||
4327 | -- Prevent Gigi from generating incorrect code by rewriting | |
4328 | -- the test as a standard False. | |
4329 | ||
4330 | Rewrite (N, | |
4331 | New_Occurrence_Of (Standard_False, Loc)); | |
4332 | ||
4333 | return; | |
70482933 RK |
4334 | end if; |
4335 | ||
fbf5a39b AC |
4336 | -- Here we have a non-scalar type |
4337 | ||
70482933 RK |
4338 | if Is_Acc then |
4339 | Typ := Designated_Type (Typ); | |
4340 | end if; | |
4341 | ||
4342 | if not Is_Constrained (Typ) then | |
4343 | Rewrite (N, | |
4344 | New_Reference_To (Standard_True, Loc)); | |
4345 | Analyze_And_Resolve (N, Rtyp); | |
4346 | ||
685094bf RD |
4347 | -- For the constrained array case, we have to check the subscripts |
4348 | -- for an exact match if the lengths are non-zero (the lengths | |
4349 | -- must match in any case). | |
70482933 RK |
4350 | |
4351 | elsif Is_Array_Type (Typ) then | |
4352 | ||
fbf5a39b | 4353 | Check_Subscripts : declare |
70482933 | 4354 | function Construct_Attribute_Reference |
2e071734 AC |
4355 | (E : Node_Id; |
4356 | Nam : Name_Id; | |
4357 | Dim : Nat) return Node_Id; | |
70482933 RK |
4358 | -- Build attribute reference E'Nam(Dim) |
4359 | ||
fbf5a39b AC |
4360 | ----------------------------------- |
4361 | -- Construct_Attribute_Reference -- | |
4362 | ----------------------------------- | |
4363 | ||
70482933 | 4364 | function Construct_Attribute_Reference |
2e071734 AC |
4365 | (E : Node_Id; |
4366 | Nam : Name_Id; | |
4367 | Dim : Nat) return Node_Id | |
70482933 RK |
4368 | is |
4369 | begin | |
4370 | return | |
4371 | Make_Attribute_Reference (Loc, | |
4372 | Prefix => E, | |
4373 | Attribute_Name => Nam, | |
4374 | Expressions => New_List ( | |
4375 | Make_Integer_Literal (Loc, Dim))); | |
4376 | end Construct_Attribute_Reference; | |
4377 | ||
fad0600d | 4378 | -- Start of processing for Check_Subscripts |
fbf5a39b | 4379 | |
70482933 RK |
4380 | begin |
4381 | for J in 1 .. Number_Dimensions (Typ) loop | |
4382 | Evolve_And_Then (Cond, | |
4383 | Make_Op_Eq (Loc, | |
4384 | Left_Opnd => | |
4385 | Construct_Attribute_Reference | |
fbf5a39b AC |
4386 | (Duplicate_Subexpr_No_Checks (Obj), |
4387 | Name_First, J), | |
70482933 RK |
4388 | Right_Opnd => |
4389 | Construct_Attribute_Reference | |
4390 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); | |
4391 | ||
4392 | Evolve_And_Then (Cond, | |
4393 | Make_Op_Eq (Loc, | |
4394 | Left_Opnd => | |
4395 | Construct_Attribute_Reference | |
fbf5a39b AC |
4396 | (Duplicate_Subexpr_No_Checks (Obj), |
4397 | Name_Last, J), | |
70482933 RK |
4398 | Right_Opnd => |
4399 | Construct_Attribute_Reference | |
4400 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); | |
4401 | end loop; | |
4402 | ||
4403 | if Is_Acc then | |
fbf5a39b AC |
4404 | Cond := |
4405 | Make_Or_Else (Loc, | |
4406 | Left_Opnd => | |
4407 | Make_Op_Eq (Loc, | |
4408 | Left_Opnd => Obj, | |
4409 | Right_Opnd => Make_Null (Loc)), | |
4410 | Right_Opnd => Cond); | |
70482933 RK |
4411 | end if; |
4412 | ||
4413 | Rewrite (N, Cond); | |
4414 | Analyze_And_Resolve (N, Rtyp); | |
fbf5a39b | 4415 | end Check_Subscripts; |
70482933 | 4416 | |
685094bf RD |
4417 | -- These are the cases where constraint checks may be required, |
4418 | -- e.g. records with possible discriminants | |
70482933 RK |
4419 | |
4420 | else | |
4421 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
4422 | -- The expression that is built is the negation of the one that |
4423 | -- is used for checking discriminant constraints. | |
70482933 RK |
4424 | |
4425 | Obj := Relocate_Node (Left_Opnd (N)); | |
4426 | ||
4427 | if Has_Discriminants (Typ) then | |
4428 | Cond := Make_Op_Not (Loc, | |
4429 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
4430 | ||
4431 | if Is_Acc then | |
4432 | Cond := Make_Or_Else (Loc, | |
4433 | Left_Opnd => | |
4434 | Make_Op_Eq (Loc, | |
4435 | Left_Opnd => Obj, | |
4436 | Right_Opnd => Make_Null (Loc)), | |
4437 | Right_Opnd => Cond); | |
4438 | end if; | |
4439 | ||
4440 | else | |
4441 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
4442 | end if; | |
4443 | ||
4444 | Rewrite (N, Cond); | |
4445 | Analyze_And_Resolve (N, Rtyp); | |
4446 | end if; | |
4447 | end; | |
4448 | end if; | |
4449 | end Expand_N_In; | |
4450 | ||
4451 | -------------------------------- | |
4452 | -- Expand_N_Indexed_Component -- | |
4453 | -------------------------------- | |
4454 | ||
4455 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
4456 | Loc : constant Source_Ptr := Sloc (N); | |
4457 | Typ : constant Entity_Id := Etype (N); | |
4458 | P : constant Node_Id := Prefix (N); | |
4459 | T : constant Entity_Id := Etype (P); | |
4460 | ||
4461 | begin | |
685094bf RD |
4462 | -- A special optimization, if we have an indexed component that is |
4463 | -- selecting from a slice, then we can eliminate the slice, since, for | |
4464 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
4465 | -- the range check required by the slice. The range check for the slice | |
4466 | -- itself has already been generated. The range check for the | |
4467 | -- subscripting operation is ensured by converting the subject to | |
4468 | -- the subtype of the slice. | |
4469 | ||
4470 | -- This optimization not only generates better code, avoiding slice | |
4471 | -- messing especially in the packed case, but more importantly bypasses | |
4472 | -- some problems in handling this peculiar case, for example, the issue | |
4473 | -- of dealing specially with object renamings. | |
70482933 RK |
4474 | |
4475 | if Nkind (P) = N_Slice then | |
4476 | Rewrite (N, | |
4477 | Make_Indexed_Component (Loc, | |
4478 | Prefix => Prefix (P), | |
4479 | Expressions => New_List ( | |
4480 | Convert_To | |
4481 | (Etype (First_Index (Etype (P))), | |
4482 | First (Expressions (N)))))); | |
4483 | Analyze_And_Resolve (N, Typ); | |
4484 | return; | |
4485 | end if; | |
4486 | ||
b4592168 GD |
4487 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
4488 | -- function, then additional actuals must be passed. | |
4489 | ||
4490 | if Ada_Version >= Ada_05 | |
4491 | and then Is_Build_In_Place_Function_Call (P) | |
4492 | then | |
4493 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
4494 | end if; | |
4495 | ||
685094bf RD |
4496 | -- If the prefix is an access type, then we unconditionally rewrite if |
4497 | -- as an explicit deference. This simplifies processing for several | |
4498 | -- cases, including packed array cases and certain cases in which checks | |
4499 | -- must be generated. We used to try to do this only when it was | |
4500 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
4501 | |
4502 | if Is_Access_Type (T) then | |
2717634d | 4503 | Insert_Explicit_Dereference (P); |
70482933 RK |
4504 | Analyze_And_Resolve (P, Designated_Type (T)); |
4505 | end if; | |
4506 | ||
fbf5a39b AC |
4507 | -- Generate index and validity checks |
4508 | ||
4509 | Generate_Index_Checks (N); | |
4510 | ||
70482933 RK |
4511 | if Validity_Checks_On and then Validity_Check_Subscripts then |
4512 | Apply_Subscript_Validity_Checks (N); | |
4513 | end if; | |
4514 | ||
4515 | -- All done for the non-packed case | |
4516 | ||
4517 | if not Is_Packed (Etype (Prefix (N))) then | |
4518 | return; | |
4519 | end if; | |
4520 | ||
4521 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 4522 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
4523 | -- we can always use the normal packed element get circuit. |
4524 | ||
4525 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
4526 | Expand_Packed_Element_Reference (N); | |
4527 | return; | |
4528 | end if; | |
4529 | ||
4530 | -- For a reference to a component of a bit packed array, we have to | |
4531 | -- convert it to a reference to the corresponding Packed_Array_Type. | |
4532 | -- We only want to do this for simple references, and not for: | |
4533 | ||
685094bf RD |
4534 | -- Left side of assignment, or prefix of left side of assignment, or |
4535 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
4536 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
4537 | ||
4538 | -- Renaming objects in renaming associations | |
4539 | -- This case is handled when a use of the renamed variable occurs | |
4540 | ||
4541 | -- Actual parameters for a procedure call | |
4542 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
4543 | ||
4544 | -- The second expression in a 'Read attribute reference | |
4545 | ||
4546 | -- The prefix of an address or size attribute reference | |
4547 | ||
4548 | -- The following circuit detects these exceptions | |
4549 | ||
4550 | declare | |
4551 | Child : Node_Id := N; | |
4552 | Parnt : Node_Id := Parent (N); | |
4553 | ||
4554 | begin | |
4555 | loop | |
4556 | if Nkind (Parnt) = N_Unchecked_Expression then | |
4557 | null; | |
4558 | ||
303b4d58 AC |
4559 | elsif Nkind_In (Parnt, N_Object_Renaming_Declaration, |
4560 | N_Procedure_Call_Statement) | |
70482933 RK |
4561 | or else (Nkind (Parnt) = N_Parameter_Association |
4562 | and then | |
4563 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
4564 | then | |
4565 | return; | |
4566 | ||
4567 | elsif Nkind (Parnt) = N_Attribute_Reference | |
4568 | and then (Attribute_Name (Parnt) = Name_Address | |
4569 | or else | |
4570 | Attribute_Name (Parnt) = Name_Size) | |
4571 | and then Prefix (Parnt) = Child | |
4572 | then | |
4573 | return; | |
4574 | ||
4575 | elsif Nkind (Parnt) = N_Assignment_Statement | |
4576 | and then Name (Parnt) = Child | |
4577 | then | |
4578 | return; | |
4579 | ||
685094bf RD |
4580 | -- If the expression is an index of an indexed component, it must |
4581 | -- be expanded regardless of context. | |
fbf5a39b AC |
4582 | |
4583 | elsif Nkind (Parnt) = N_Indexed_Component | |
4584 | and then Child /= Prefix (Parnt) | |
4585 | then | |
4586 | Expand_Packed_Element_Reference (N); | |
4587 | return; | |
4588 | ||
4589 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
4590 | and then Name (Parent (Parnt)) = Parnt | |
4591 | then | |
4592 | return; | |
4593 | ||
70482933 RK |
4594 | elsif Nkind (Parnt) = N_Attribute_Reference |
4595 | and then Attribute_Name (Parnt) = Name_Read | |
4596 | and then Next (First (Expressions (Parnt))) = Child | |
4597 | then | |
4598 | return; | |
4599 | ||
303b4d58 | 4600 | elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component) |
70482933 RK |
4601 | and then Prefix (Parnt) = Child |
4602 | then | |
4603 | null; | |
4604 | ||
4605 | else | |
4606 | Expand_Packed_Element_Reference (N); | |
4607 | return; | |
4608 | end if; | |
4609 | ||
685094bf RD |
4610 | -- Keep looking up tree for unchecked expression, or if we are the |
4611 | -- prefix of a possible assignment left side. | |
70482933 RK |
4612 | |
4613 | Child := Parnt; | |
4614 | Parnt := Parent (Child); | |
4615 | end loop; | |
4616 | end; | |
70482933 RK |
4617 | end Expand_N_Indexed_Component; |
4618 | ||
4619 | --------------------- | |
4620 | -- Expand_N_Not_In -- | |
4621 | --------------------- | |
4622 | ||
4623 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
4624 | -- can be done. This avoids needing to duplicate this expansion code. | |
4625 | ||
4626 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
4627 | Loc : constant Source_Ptr := Sloc (N); |
4628 | Typ : constant Entity_Id := Etype (N); | |
4629 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
4630 | |
4631 | begin | |
4632 | Rewrite (N, | |
4633 | Make_Op_Not (Loc, | |
4634 | Right_Opnd => | |
4635 | Make_In (Loc, | |
4636 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 4637 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 4638 | |
d766cee3 | 4639 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 4640 | -- transformations in Expand_N_In). |
630d30e9 RD |
4641 | |
4642 | Set_Comes_From_Source (N, Cfs); | |
4643 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
4644 | ||
8fc789c8 | 4645 | -- Now analyze transformed node |
630d30e9 | 4646 | |
70482933 RK |
4647 | Analyze_And_Resolve (N, Typ); |
4648 | end Expand_N_Not_In; | |
4649 | ||
4650 | ------------------- | |
4651 | -- Expand_N_Null -- | |
4652 | ------------------- | |
4653 | ||
685094bf RD |
4654 | -- The only replacement required is for the case of a null of type that is |
4655 | -- an access to protected subprogram. We represent such access values as a | |
4656 | -- record, and so we must replace the occurrence of null by the equivalent | |
4657 | -- record (with a null address and a null pointer in it), so that the | |
4658 | -- backend creates the proper value. | |
70482933 RK |
4659 | |
4660 | procedure Expand_N_Null (N : Node_Id) is | |
4661 | Loc : constant Source_Ptr := Sloc (N); | |
4662 | Typ : constant Entity_Id := Etype (N); | |
4663 | Agg : Node_Id; | |
4664 | ||
4665 | begin | |
26bff3d9 | 4666 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
4667 | Agg := |
4668 | Make_Aggregate (Loc, | |
4669 | Expressions => New_List ( | |
4670 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
4671 | Make_Null (Loc))); | |
4672 | ||
4673 | Rewrite (N, Agg); | |
4674 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
4675 | ||
685094bf RD |
4676 | -- For subsequent semantic analysis, the node must retain its type. |
4677 | -- Gigi in any case replaces this type by the corresponding record | |
4678 | -- type before processing the node. | |
70482933 RK |
4679 | |
4680 | Set_Etype (N, Typ); | |
4681 | end if; | |
fbf5a39b AC |
4682 | |
4683 | exception | |
4684 | when RE_Not_Available => | |
4685 | return; | |
70482933 RK |
4686 | end Expand_N_Null; |
4687 | ||
4688 | --------------------- | |
4689 | -- Expand_N_Op_Abs -- | |
4690 | --------------------- | |
4691 | ||
4692 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
4693 | Loc : constant Source_Ptr := Sloc (N); | |
4694 | Expr : constant Node_Id := Right_Opnd (N); | |
4695 | ||
4696 | begin | |
4697 | Unary_Op_Validity_Checks (N); | |
4698 | ||
4699 | -- Deal with software overflow checking | |
4700 | ||
07fc65c4 | 4701 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
4702 | and then Is_Signed_Integer_Type (Etype (N)) |
4703 | and then Do_Overflow_Check (N) | |
4704 | then | |
685094bf RD |
4705 | -- The only case to worry about is when the argument is equal to the |
4706 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 4707 | |
fbf5a39b | 4708 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
4709 | |
4710 | -- with the usual Duplicate_Subexpr use coding for expr | |
4711 | ||
fbf5a39b AC |
4712 | Insert_Action (N, |
4713 | Make_Raise_Constraint_Error (Loc, | |
4714 | Condition => | |
4715 | Make_Op_Eq (Loc, | |
70482933 | 4716 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
4717 | Right_Opnd => |
4718 | Make_Attribute_Reference (Loc, | |
4719 | Prefix => | |
4720 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), | |
4721 | Attribute_Name => Name_First)), | |
4722 | Reason => CE_Overflow_Check_Failed)); | |
4723 | end if; | |
70482933 RK |
4724 | |
4725 | -- Vax floating-point types case | |
4726 | ||
fbf5a39b | 4727 | if Vax_Float (Etype (N)) then |
70482933 RK |
4728 | Expand_Vax_Arith (N); |
4729 | end if; | |
4730 | end Expand_N_Op_Abs; | |
4731 | ||
4732 | --------------------- | |
4733 | -- Expand_N_Op_Add -- | |
4734 | --------------------- | |
4735 | ||
4736 | procedure Expand_N_Op_Add (N : Node_Id) is | |
4737 | Typ : constant Entity_Id := Etype (N); | |
4738 | ||
4739 | begin | |
4740 | Binary_Op_Validity_Checks (N); | |
4741 | ||
4742 | -- N + 0 = 0 + N = N for integer types | |
4743 | ||
4744 | if Is_Integer_Type (Typ) then | |
4745 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
4746 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
4747 | then | |
4748 | Rewrite (N, Left_Opnd (N)); | |
4749 | return; | |
4750 | ||
4751 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
4752 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
4753 | then | |
4754 | Rewrite (N, Right_Opnd (N)); | |
4755 | return; | |
4756 | end if; | |
4757 | end if; | |
4758 | ||
fbf5a39b | 4759 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 RK |
4760 | |
4761 | if Is_Signed_Integer_Type (Typ) | |
4762 | or else Is_Fixed_Point_Type (Typ) | |
4763 | then | |
4764 | Apply_Arithmetic_Overflow_Check (N); | |
4765 | return; | |
4766 | ||
4767 | -- Vax floating-point types case | |
4768 | ||
4769 | elsif Vax_Float (Typ) then | |
4770 | Expand_Vax_Arith (N); | |
4771 | end if; | |
4772 | end Expand_N_Op_Add; | |
4773 | ||
4774 | --------------------- | |
4775 | -- Expand_N_Op_And -- | |
4776 | --------------------- | |
4777 | ||
4778 | procedure Expand_N_Op_And (N : Node_Id) is | |
4779 | Typ : constant Entity_Id := Etype (N); | |
4780 | ||
4781 | begin | |
4782 | Binary_Op_Validity_Checks (N); | |
4783 | ||
4784 | if Is_Array_Type (Etype (N)) then | |
4785 | Expand_Boolean_Operator (N); | |
4786 | ||
4787 | elsif Is_Boolean_Type (Etype (N)) then | |
4788 | Adjust_Condition (Left_Opnd (N)); | |
4789 | Adjust_Condition (Right_Opnd (N)); | |
4790 | Set_Etype (N, Standard_Boolean); | |
4791 | Adjust_Result_Type (N, Typ); | |
4792 | end if; | |
4793 | end Expand_N_Op_And; | |
4794 | ||
4795 | ------------------------ | |
4796 | -- Expand_N_Op_Concat -- | |
4797 | ------------------------ | |
4798 | ||
4799 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
4800 | Opnds : List_Id; |
4801 | -- List of operands to be concatenated | |
4802 | ||
70482933 | 4803 | Cnode : Node_Id; |
685094bf RD |
4804 | -- Node which is to be replaced by the result of concatenating the nodes |
4805 | -- in the list Opnds. | |
70482933 | 4806 | |
70482933 | 4807 | begin |
fbf5a39b AC |
4808 | -- Ensure validity of both operands |
4809 | ||
70482933 RK |
4810 | Binary_Op_Validity_Checks (N); |
4811 | ||
685094bf RD |
4812 | -- If we are the left operand of a concatenation higher up the tree, |
4813 | -- then do nothing for now, since we want to deal with a series of | |
4814 | -- concatenations as a unit. | |
70482933 RK |
4815 | |
4816 | if Nkind (Parent (N)) = N_Op_Concat | |
4817 | and then N = Left_Opnd (Parent (N)) | |
4818 | then | |
4819 | return; | |
4820 | end if; | |
4821 | ||
4822 | -- We get here with a concatenation whose left operand may be a | |
4823 | -- concatenation itself with a consistent type. We need to process | |
4824 | -- these concatenation operands from left to right, which means | |
4825 | -- from the deepest node in the tree to the highest node. | |
4826 | ||
4827 | Cnode := N; | |
4828 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
4829 | Cnode := Left_Opnd (Cnode); | |
4830 | end loop; | |
4831 | ||
4832 | -- Now Opnd is the deepest Opnd, and its parents are the concatenation | |
4833 | -- nodes above, so now we process bottom up, doing the operations. We | |
4834 | -- gather a string that is as long as possible up to five operands | |
4835 | ||
df46b832 AC |
4836 | -- The outer loop runs more than once if more than one concatenation |
4837 | -- type is involved. | |
70482933 RK |
4838 | |
4839 | Outer : loop | |
4840 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
4841 | Set_Parent (Opnds, N); | |
4842 | ||
df46b832 | 4843 | -- The inner loop gathers concatenation operands |
70482933 RK |
4844 | |
4845 | Inner : while Cnode /= N | |
70482933 RK |
4846 | and then Base_Type (Etype (Cnode)) = |
4847 | Base_Type (Etype (Parent (Cnode))) | |
4848 | loop | |
4849 | Cnode := Parent (Cnode); | |
4850 | Append (Right_Opnd (Cnode), Opnds); | |
4851 | end loop Inner; | |
4852 | ||
fdac1f80 | 4853 | Expand_Concatenate (Cnode, Opnds); |
70482933 RK |
4854 | |
4855 | exit Outer when Cnode = N; | |
4856 | Cnode := Parent (Cnode); | |
4857 | end loop Outer; | |
4858 | end Expand_N_Op_Concat; | |
4859 | ||
4860 | ------------------------ | |
4861 | -- Expand_N_Op_Divide -- | |
4862 | ------------------------ | |
4863 | ||
4864 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
4865 | Loc : constant Source_Ptr := Sloc (N); |
4866 | Lopnd : constant Node_Id := Left_Opnd (N); | |
4867 | Ropnd : constant Node_Id := Right_Opnd (N); | |
4868 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
4869 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
4870 | Typ : Entity_Id := Etype (N); | |
4871 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
4872 | and then | |
4873 | Compile_Time_Known_Value (Ropnd); | |
4874 | Rval : Uint; | |
70482933 RK |
4875 | |
4876 | begin | |
4877 | Binary_Op_Validity_Checks (N); | |
4878 | ||
f82944b7 JM |
4879 | if Rknow then |
4880 | Rval := Expr_Value (Ropnd); | |
4881 | end if; | |
4882 | ||
70482933 RK |
4883 | -- N / 1 = N for integer types |
4884 | ||
f82944b7 JM |
4885 | if Rknow and then Rval = Uint_1 then |
4886 | Rewrite (N, Lopnd); | |
70482933 RK |
4887 | return; |
4888 | end if; | |
4889 | ||
4890 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
4891 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
4892 | -- operand is an unsigned integer, as required for this to work. | |
4893 | ||
f82944b7 JM |
4894 | if Nkind (Ropnd) = N_Op_Expon |
4895 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
4896 | |
4897 | -- We cannot do this transformation in configurable run time mode if we | |
4898 | -- have 64-bit -- integers and long shifts are not available. | |
4899 | ||
4900 | and then | |
4901 | (Esize (Ltyp) <= 32 | |
4902 | or else Support_Long_Shifts_On_Target) | |
70482933 RK |
4903 | then |
4904 | Rewrite (N, | |
4905 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 4906 | Left_Opnd => Lopnd, |
70482933 | 4907 | Right_Opnd => |
f82944b7 | 4908 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
4909 | Analyze_And_Resolve (N, Typ); |
4910 | return; | |
4911 | end if; | |
4912 | ||
4913 | -- Do required fixup of universal fixed operation | |
4914 | ||
4915 | if Typ = Universal_Fixed then | |
4916 | Fixup_Universal_Fixed_Operation (N); | |
4917 | Typ := Etype (N); | |
4918 | end if; | |
4919 | ||
4920 | -- Divisions with fixed-point results | |
4921 | ||
4922 | if Is_Fixed_Point_Type (Typ) then | |
4923 | ||
685094bf RD |
4924 | -- No special processing if Treat_Fixed_As_Integer is set, since |
4925 | -- from a semantic point of view such operations are simply integer | |
4926 | -- operations and will be treated that way. | |
70482933 RK |
4927 | |
4928 | if not Treat_Fixed_As_Integer (N) then | |
4929 | if Is_Integer_Type (Rtyp) then | |
4930 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
4931 | else | |
4932 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
4933 | end if; | |
4934 | end if; | |
4935 | ||
685094bf RD |
4936 | -- Other cases of division of fixed-point operands. Again we exclude the |
4937 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 RK |
4938 | |
4939 | elsif (Is_Fixed_Point_Type (Ltyp) or else | |
4940 | Is_Fixed_Point_Type (Rtyp)) | |
4941 | and then not Treat_Fixed_As_Integer (N) | |
4942 | then | |
4943 | if Is_Integer_Type (Typ) then | |
4944 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
4945 | else | |
4946 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
4947 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
4948 | end if; | |
4949 | ||
685094bf RD |
4950 | -- Mixed-mode operations can appear in a non-static universal context, |
4951 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
4952 | |
4953 | elsif Typ = Universal_Real | |
4954 | and then Is_Integer_Type (Rtyp) | |
4955 | then | |
f82944b7 JM |
4956 | Rewrite (Ropnd, |
4957 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 4958 | |
f82944b7 | 4959 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 RK |
4960 | |
4961 | elsif Typ = Universal_Real | |
4962 | and then Is_Integer_Type (Ltyp) | |
4963 | then | |
f82944b7 JM |
4964 | Rewrite (Lopnd, |
4965 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 4966 | |
f82944b7 | 4967 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 4968 | |
f02b8bb8 | 4969 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
4970 | |
4971 | elsif Is_Integer_Type (Typ) then | |
4972 | Apply_Divide_Check (N); | |
fbf5a39b | 4973 | |
f82944b7 JM |
4974 | -- Check for 64-bit division available, or long shifts if the divisor |
4975 | -- is a small power of 2 (since such divides will be converted into | |
1147c704 | 4976 | -- long shifts). |
fbf5a39b AC |
4977 | |
4978 | if Esize (Ltyp) > 32 | |
4979 | and then not Support_64_Bit_Divides_On_Target | |
f82944b7 JM |
4980 | and then |
4981 | (not Rknow | |
4982 | or else not Support_Long_Shifts_On_Target | |
4983 | or else (Rval /= Uint_2 and then | |
4984 | Rval /= Uint_4 and then | |
4985 | Rval /= Uint_8 and then | |
4986 | Rval /= Uint_16 and then | |
4987 | Rval /= Uint_32 and then | |
4988 | Rval /= Uint_64)) | |
fbf5a39b AC |
4989 | then |
4990 | Error_Msg_CRT ("64-bit division", N); | |
4991 | end if; | |
f02b8bb8 RD |
4992 | |
4993 | -- Deal with Vax_Float | |
4994 | ||
4995 | elsif Vax_Float (Typ) then | |
4996 | Expand_Vax_Arith (N); | |
4997 | return; | |
70482933 RK |
4998 | end if; |
4999 | end Expand_N_Op_Divide; | |
5000 | ||
5001 | -------------------- | |
5002 | -- Expand_N_Op_Eq -- | |
5003 | -------------------- | |
5004 | ||
5005 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
5006 | Loc : constant Source_Ptr := Sloc (N); |
5007 | Typ : constant Entity_Id := Etype (N); | |
5008 | Lhs : constant Node_Id := Left_Opnd (N); | |
5009 | Rhs : constant Node_Id := Right_Opnd (N); | |
5010 | Bodies : constant List_Id := New_List; | |
5011 | A_Typ : constant Entity_Id := Etype (Lhs); | |
5012 | ||
70482933 RK |
5013 | Typl : Entity_Id := A_Typ; |
5014 | Op_Name : Entity_Id; | |
5015 | Prim : Elmt_Id; | |
70482933 RK |
5016 | |
5017 | procedure Build_Equality_Call (Eq : Entity_Id); | |
5018 | -- If a constructed equality exists for the type or for its parent, | |
5019 | -- build and analyze call, adding conversions if the operation is | |
5020 | -- inherited. | |
5021 | ||
5d09245e | 5022 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
8fc789c8 | 5023 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
5024 | -- Unchecked_Union subtype. Typ is a record type. |
5025 | ||
70482933 RK |
5026 | ------------------------- |
5027 | -- Build_Equality_Call -- | |
5028 | ------------------------- | |
5029 | ||
5030 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
5031 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
5032 | L_Exp : Node_Id := Relocate_Node (Lhs); | |
5033 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
5034 | ||
5035 | begin | |
5036 | if Base_Type (Op_Type) /= Base_Type (A_Typ) | |
5037 | and then not Is_Class_Wide_Type (A_Typ) | |
5038 | then | |
5039 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
5040 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
5041 | end if; | |
5042 | ||
5d09245e AC |
5043 | -- If we have an Unchecked_Union, we need to add the inferred |
5044 | -- discriminant values as actuals in the function call. At this | |
5045 | -- point, the expansion has determined that both operands have | |
5046 | -- inferable discriminants. | |
5047 | ||
5048 | if Is_Unchecked_Union (Op_Type) then | |
5049 | declare | |
5050 | Lhs_Type : constant Node_Id := Etype (L_Exp); | |
5051 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
5052 | Lhs_Discr_Val : Node_Id; | |
5053 | Rhs_Discr_Val : Node_Id; | |
5054 | ||
5055 | begin | |
5056 | -- Per-object constrained selected components require special | |
5057 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 5058 | -- Unchecked_Union, we cannot reference its discriminants |
5d09245e AC |
5059 | -- directly. This is why we use the two extra parameters of |
5060 | -- the equality function of the enclosing Unchecked_Union. | |
5061 | ||
5062 | -- type UU_Type (Discr : Integer := 0) is | |
5063 | -- . . . | |
5064 | -- end record; | |
5065 | -- pragma Unchecked_Union (UU_Type); | |
5066 | ||
5067 | -- 1. Unchecked_Union enclosing record: | |
5068 | ||
5069 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
5070 | -- . . . | |
5071 | -- Comp : UU_Type (Discr); | |
5072 | -- . . . | |
5073 | -- end Enclosing_UU_Type; | |
5074 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
5075 | ||
5076 | -- Obj1 : Enclosing_UU_Type; | |
5077 | -- Obj2 : Enclosing_UU_Type (1); | |
5078 | ||
2717634d | 5079 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
5080 | |
5081 | -- Generated code: | |
5082 | ||
5083 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
5084 | ||
5085 | -- A and B are the formal parameters of the equality function | |
5086 | -- of Enclosing_UU_Type. The function always has two extra | |
5087 | -- formals to capture the inferred discriminant values. | |
5088 | ||
5089 | -- 2. Non-Unchecked_Union enclosing record: | |
5090 | ||
5091 | -- type | |
5092 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
5093 | -- is record | |
5094 | -- . . . | |
5095 | -- Comp : UU_Type (Discr); | |
5096 | -- . . . | |
5097 | -- end Enclosing_Non_UU_Type; | |
5098 | ||
5099 | -- Obj1 : Enclosing_Non_UU_Type; | |
5100 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
5101 | ||
630d30e9 | 5102 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
5103 | |
5104 | -- Generated code: | |
5105 | ||
5106 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
5107 | -- obj1.discr, obj2.discr)) then | |
5108 | ||
5109 | -- In this case we can directly reference the discriminants of | |
5110 | -- the enclosing record. | |
5111 | ||
5112 | -- Lhs of equality | |
5113 | ||
5114 | if Nkind (Lhs) = N_Selected_Component | |
5e1c00fa RD |
5115 | and then Has_Per_Object_Constraint |
5116 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
5117 | then |
5118 | -- Enclosing record is an Unchecked_Union, use formal A | |
5119 | ||
5120 | if Is_Unchecked_Union (Scope | |
5121 | (Entity (Selector_Name (Lhs)))) | |
5122 | then | |
5123 | Lhs_Discr_Val := | |
5124 | Make_Identifier (Loc, | |
5125 | Chars => Name_A); | |
5126 | ||
5127 | -- Enclosing record is of a non-Unchecked_Union type, it is | |
5128 | -- possible to reference the discriminant. | |
5129 | ||
5130 | else | |
5131 | Lhs_Discr_Val := | |
5132 | Make_Selected_Component (Loc, | |
5133 | Prefix => Prefix (Lhs), | |
5134 | Selector_Name => | |
5e1c00fa RD |
5135 | New_Copy |
5136 | (Get_Discriminant_Value | |
5137 | (First_Discriminant (Lhs_Type), | |
5138 | Lhs_Type, | |
5139 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
5140 | end if; |
5141 | ||
5142 | -- Comment needed here ??? | |
5143 | ||
5144 | else | |
5145 | -- Infer the discriminant value | |
5146 | ||
5147 | Lhs_Discr_Val := | |
5e1c00fa RD |
5148 | New_Copy |
5149 | (Get_Discriminant_Value | |
5150 | (First_Discriminant (Lhs_Type), | |
5151 | Lhs_Type, | |
5152 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
5153 | end if; |
5154 | ||
5155 | -- Rhs of equality | |
5156 | ||
5157 | if Nkind (Rhs) = N_Selected_Component | |
5e1c00fa RD |
5158 | and then Has_Per_Object_Constraint |
5159 | (Entity (Selector_Name (Rhs))) | |
5d09245e | 5160 | then |
5e1c00fa RD |
5161 | if Is_Unchecked_Union |
5162 | (Scope (Entity (Selector_Name (Rhs)))) | |
5d09245e AC |
5163 | then |
5164 | Rhs_Discr_Val := | |
5165 | Make_Identifier (Loc, | |
5166 | Chars => Name_B); | |
5167 | ||
5168 | else | |
5169 | Rhs_Discr_Val := | |
5170 | Make_Selected_Component (Loc, | |
5171 | Prefix => Prefix (Rhs), | |
5172 | Selector_Name => | |
5173 | New_Copy (Get_Discriminant_Value ( | |
5174 | First_Discriminant (Rhs_Type), | |
5175 | Rhs_Type, | |
5176 | Stored_Constraint (Rhs_Type)))); | |
5177 | ||
5178 | end if; | |
5179 | else | |
5180 | Rhs_Discr_Val := | |
5181 | New_Copy (Get_Discriminant_Value ( | |
5182 | First_Discriminant (Rhs_Type), | |
5183 | Rhs_Type, | |
5184 | Stored_Constraint (Rhs_Type))); | |
5185 | ||
5186 | end if; | |
5187 | ||
5188 | Rewrite (N, | |
5189 | Make_Function_Call (Loc, | |
5190 | Name => New_Reference_To (Eq, Loc), | |
5191 | Parameter_Associations => New_List ( | |
5192 | L_Exp, | |
5193 | R_Exp, | |
5194 | Lhs_Discr_Val, | |
5195 | Rhs_Discr_Val))); | |
5196 | end; | |
5197 | ||
5198 | -- Normal case, not an unchecked union | |
5199 | ||
5200 | else | |
5201 | Rewrite (N, | |
5202 | Make_Function_Call (Loc, | |
5203 | Name => New_Reference_To (Eq, Loc), | |
5204 | Parameter_Associations => New_List (L_Exp, R_Exp))); | |
5205 | end if; | |
70482933 RK |
5206 | |
5207 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5208 | end Build_Equality_Call; | |
5209 | ||
5d09245e AC |
5210 | ------------------------------------ |
5211 | -- Has_Unconstrained_UU_Component -- | |
5212 | ------------------------------------ | |
5213 | ||
5214 | function Has_Unconstrained_UU_Component | |
5215 | (Typ : Node_Id) return Boolean | |
5216 | is | |
5217 | Tdef : constant Node_Id := | |
57848bf7 | 5218 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
5219 | Clist : Node_Id; |
5220 | Vpart : Node_Id; | |
5221 | ||
5222 | function Component_Is_Unconstrained_UU | |
5223 | (Comp : Node_Id) return Boolean; | |
5224 | -- Determines whether the subtype of the component is an | |
5225 | -- unconstrained Unchecked_Union. | |
5226 | ||
5227 | function Variant_Is_Unconstrained_UU | |
5228 | (Variant : Node_Id) return Boolean; | |
5229 | -- Determines whether a component of the variant has an unconstrained | |
5230 | -- Unchecked_Union subtype. | |
5231 | ||
5232 | ----------------------------------- | |
5233 | -- Component_Is_Unconstrained_UU -- | |
5234 | ----------------------------------- | |
5235 | ||
5236 | function Component_Is_Unconstrained_UU | |
5237 | (Comp : Node_Id) return Boolean | |
5238 | is | |
5239 | begin | |
5240 | if Nkind (Comp) /= N_Component_Declaration then | |
5241 | return False; | |
5242 | end if; | |
5243 | ||
5244 | declare | |
5245 | Sindic : constant Node_Id := | |
5246 | Subtype_Indication (Component_Definition (Comp)); | |
5247 | ||
5248 | begin | |
5249 | -- Unconstrained nominal type. In the case of a constraint | |
5250 | -- present, the node kind would have been N_Subtype_Indication. | |
5251 | ||
5252 | if Nkind (Sindic) = N_Identifier then | |
5253 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
5254 | end if; | |
5255 | ||
5256 | return False; | |
5257 | end; | |
5258 | end Component_Is_Unconstrained_UU; | |
5259 | ||
5260 | --------------------------------- | |
5261 | -- Variant_Is_Unconstrained_UU -- | |
5262 | --------------------------------- | |
5263 | ||
5264 | function Variant_Is_Unconstrained_UU | |
5265 | (Variant : Node_Id) return Boolean | |
5266 | is | |
5267 | Clist : constant Node_Id := Component_List (Variant); | |
5268 | ||
5269 | begin | |
5270 | if Is_Empty_List (Component_Items (Clist)) then | |
5271 | return False; | |
5272 | end if; | |
5273 | ||
f02b8bb8 RD |
5274 | -- We only need to test one component |
5275 | ||
5d09245e AC |
5276 | declare |
5277 | Comp : Node_Id := First (Component_Items (Clist)); | |
5278 | ||
5279 | begin | |
5280 | while Present (Comp) loop | |
5d09245e AC |
5281 | if Component_Is_Unconstrained_UU (Comp) then |
5282 | return True; | |
5283 | end if; | |
5284 | ||
5285 | Next (Comp); | |
5286 | end loop; | |
5287 | end; | |
5288 | ||
5289 | -- None of the components withing the variant were of | |
5290 | -- unconstrained Unchecked_Union type. | |
5291 | ||
5292 | return False; | |
5293 | end Variant_Is_Unconstrained_UU; | |
5294 | ||
5295 | -- Start of processing for Has_Unconstrained_UU_Component | |
5296 | ||
5297 | begin | |
5298 | if Null_Present (Tdef) then | |
5299 | return False; | |
5300 | end if; | |
5301 | ||
5302 | Clist := Component_List (Tdef); | |
5303 | Vpart := Variant_Part (Clist); | |
5304 | ||
5305 | -- Inspect available components | |
5306 | ||
5307 | if Present (Component_Items (Clist)) then | |
5308 | declare | |
5309 | Comp : Node_Id := First (Component_Items (Clist)); | |
5310 | ||
5311 | begin | |
5312 | while Present (Comp) loop | |
5313 | ||
8fc789c8 | 5314 | -- One component is sufficient |
5d09245e AC |
5315 | |
5316 | if Component_Is_Unconstrained_UU (Comp) then | |
5317 | return True; | |
5318 | end if; | |
5319 | ||
5320 | Next (Comp); | |
5321 | end loop; | |
5322 | end; | |
5323 | end if; | |
5324 | ||
5325 | -- Inspect available components withing variants | |
5326 | ||
5327 | if Present (Vpart) then | |
5328 | declare | |
5329 | Variant : Node_Id := First (Variants (Vpart)); | |
5330 | ||
5331 | begin | |
5332 | while Present (Variant) loop | |
5333 | ||
8fc789c8 | 5334 | -- One component within a variant is sufficient |
5d09245e AC |
5335 | |
5336 | if Variant_Is_Unconstrained_UU (Variant) then | |
5337 | return True; | |
5338 | end if; | |
5339 | ||
5340 | Next (Variant); | |
5341 | end loop; | |
5342 | end; | |
5343 | end if; | |
5344 | ||
5345 | -- Neither the available components, nor the components inside the | |
5346 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
5347 | ||
5348 | return False; | |
5349 | end Has_Unconstrained_UU_Component; | |
5350 | ||
70482933 RK |
5351 | -- Start of processing for Expand_N_Op_Eq |
5352 | ||
5353 | begin | |
5354 | Binary_Op_Validity_Checks (N); | |
5355 | ||
5356 | if Ekind (Typl) = E_Private_Type then | |
5357 | Typl := Underlying_Type (Typl); | |
70482933 RK |
5358 | elsif Ekind (Typl) = E_Private_Subtype then |
5359 | Typl := Underlying_Type (Base_Type (Typl)); | |
f02b8bb8 RD |
5360 | else |
5361 | null; | |
70482933 RK |
5362 | end if; |
5363 | ||
5364 | -- It may happen in error situations that the underlying type is not | |
5365 | -- set. The error will be detected later, here we just defend the | |
5366 | -- expander code. | |
5367 | ||
5368 | if No (Typl) then | |
5369 | return; | |
5370 | end if; | |
5371 | ||
5372 | Typl := Base_Type (Typl); | |
5373 | ||
70482933 RK |
5374 | -- Boolean types (requiring handling of non-standard case) |
5375 | ||
f02b8bb8 | 5376 | if Is_Boolean_Type (Typl) then |
70482933 RK |
5377 | Adjust_Condition (Left_Opnd (N)); |
5378 | Adjust_Condition (Right_Opnd (N)); | |
5379 | Set_Etype (N, Standard_Boolean); | |
5380 | Adjust_Result_Type (N, Typ); | |
5381 | ||
5382 | -- Array types | |
5383 | ||
5384 | elsif Is_Array_Type (Typl) then | |
5385 | ||
1033834f RD |
5386 | -- If we are doing full validity checking, and it is possible for the |
5387 | -- array elements to be invalid then expand out array comparisons to | |
5388 | -- make sure that we check the array elements. | |
fbf5a39b | 5389 | |
1033834f RD |
5390 | if Validity_Check_Operands |
5391 | and then not Is_Known_Valid (Component_Type (Typl)) | |
5392 | then | |
fbf5a39b AC |
5393 | declare |
5394 | Save_Force_Validity_Checks : constant Boolean := | |
5395 | Force_Validity_Checks; | |
5396 | begin | |
5397 | Force_Validity_Checks := True; | |
5398 | Rewrite (N, | |
0da2c8ac AC |
5399 | Expand_Array_Equality |
5400 | (N, | |
5401 | Relocate_Node (Lhs), | |
5402 | Relocate_Node (Rhs), | |
5403 | Bodies, | |
5404 | Typl)); | |
5405 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
5406 | Analyze_And_Resolve (N, Standard_Boolean); |
5407 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
5408 | end; | |
5409 | ||
a9d8907c | 5410 | -- Packed case where both operands are known aligned |
70482933 | 5411 | |
a9d8907c JM |
5412 | elsif Is_Bit_Packed_Array (Typl) |
5413 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
5414 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
5415 | then | |
70482933 RK |
5416 | Expand_Packed_Eq (N); |
5417 | ||
5e1c00fa RD |
5418 | -- Where the component type is elementary we can use a block bit |
5419 | -- comparison (if supported on the target) exception in the case | |
5420 | -- of floating-point (negative zero issues require element by | |
5421 | -- element comparison), and atomic types (where we must be sure | |
a9d8907c | 5422 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 5423 | |
70482933 RK |
5424 | elsif Is_Elementary_Type (Component_Type (Typl)) |
5425 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 5426 | and then not Is_Atomic (Component_Type (Typl)) |
a9d8907c JM |
5427 | and then not Is_Possibly_Unaligned_Object (Lhs) |
5428 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
fbf5a39b | 5429 | and then Support_Composite_Compare_On_Target |
70482933 RK |
5430 | then |
5431 | null; | |
5432 | ||
685094bf RD |
5433 | -- For composite and floating-point cases, expand equality loop to |
5434 | -- make sure of using proper comparisons for tagged types, and | |
5435 | -- correctly handling the floating-point case. | |
70482933 RK |
5436 | |
5437 | else | |
5438 | Rewrite (N, | |
0da2c8ac AC |
5439 | Expand_Array_Equality |
5440 | (N, | |
5441 | Relocate_Node (Lhs), | |
5442 | Relocate_Node (Rhs), | |
5443 | Bodies, | |
5444 | Typl)); | |
70482933 RK |
5445 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
5446 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5447 | end if; | |
5448 | ||
5449 | -- Record Types | |
5450 | ||
5451 | elsif Is_Record_Type (Typl) then | |
5452 | ||
5453 | -- For tagged types, use the primitive "=" | |
5454 | ||
5455 | if Is_Tagged_Type (Typl) then | |
5456 | ||
0669bebe GB |
5457 | -- No need to do anything else compiling under restriction |
5458 | -- No_Dispatching_Calls. During the semantic analysis we | |
5459 | -- already notified such violation. | |
5460 | ||
5461 | if Restriction_Active (No_Dispatching_Calls) then | |
5462 | return; | |
5463 | end if; | |
5464 | ||
685094bf RD |
5465 | -- If this is derived from an untagged private type completed with |
5466 | -- a tagged type, it does not have a full view, so we use the | |
5467 | -- primitive operations of the private type. This check should no | |
5468 | -- longer be necessary when these types get their full views??? | |
70482933 RK |
5469 | |
5470 | if Is_Private_Type (A_Typ) | |
5471 | and then not Is_Tagged_Type (A_Typ) | |
5472 | and then Is_Derived_Type (A_Typ) | |
5473 | and then No (Full_View (A_Typ)) | |
5474 | then | |
685094bf RD |
5475 | -- Search for equality operation, checking that the operands |
5476 | -- have the same type. Note that we must find a matching entry, | |
5477 | -- or something is very wrong! | |
2e071734 | 5478 | |
70482933 RK |
5479 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
5480 | ||
2e071734 AC |
5481 | while Present (Prim) loop |
5482 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
5483 | and then Etype (First_Formal (Node (Prim))) = | |
5484 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
5485 | and then | |
5486 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
5487 | ||
70482933 | 5488 | Next_Elmt (Prim); |
70482933 RK |
5489 | end loop; |
5490 | ||
2e071734 | 5491 | pragma Assert (Present (Prim)); |
70482933 | 5492 | Op_Name := Node (Prim); |
fbf5a39b AC |
5493 | |
5494 | -- Find the type's predefined equality or an overriding | |
685094bf | 5495 | -- user- defined equality. The reason for not simply calling |
fbf5a39b | 5496 | -- Find_Prim_Op here is that there may be a user-defined |
685094bf RD |
5497 | -- overloaded equality op that precedes the equality that we want, |
5498 | -- so we have to explicitly search (e.g., there could be an | |
5499 | -- equality with two different parameter types). | |
fbf5a39b | 5500 | |
70482933 | 5501 | else |
fbf5a39b AC |
5502 | if Is_Class_Wide_Type (Typl) then |
5503 | Typl := Root_Type (Typl); | |
5504 | end if; | |
5505 | ||
5506 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
5507 | while Present (Prim) loop |
5508 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
5509 | and then Etype (First_Formal (Node (Prim))) = | |
5510 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
5511 | and then |
5512 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
5513 | |
5514 | Next_Elmt (Prim); | |
fbf5a39b AC |
5515 | end loop; |
5516 | ||
2e071734 | 5517 | pragma Assert (Present (Prim)); |
fbf5a39b | 5518 | Op_Name := Node (Prim); |
70482933 RK |
5519 | end if; |
5520 | ||
5521 | Build_Equality_Call (Op_Name); | |
5522 | ||
5d09245e AC |
5523 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
5524 | -- predefined equality operator for a type which has a subcomponent | |
5525 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
5526 | ||
5527 | elsif Has_Unconstrained_UU_Component (Typl) then | |
5528 | Insert_Action (N, | |
5529 | Make_Raise_Program_Error (Loc, | |
5530 | Reason => PE_Unchecked_Union_Restriction)); | |
5531 | ||
5532 | -- Prevent Gigi from generating incorrect code by rewriting the | |
5533 | -- equality as a standard False. | |
5534 | ||
5535 | Rewrite (N, | |
5536 | New_Occurrence_Of (Standard_False, Loc)); | |
5537 | ||
5538 | elsif Is_Unchecked_Union (Typl) then | |
5539 | ||
5540 | -- If we can infer the discriminants of the operands, we make a | |
5541 | -- call to the TSS equality function. | |
5542 | ||
5543 | if Has_Inferable_Discriminants (Lhs) | |
5544 | and then | |
5545 | Has_Inferable_Discriminants (Rhs) | |
5546 | then | |
5547 | Build_Equality_Call | |
5548 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
5549 | ||
5550 | else | |
5551 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
5552 | -- the predefined equality operator for an Unchecked_Union type | |
5553 | -- if either of the operands lack inferable discriminants. | |
5554 | ||
5555 | Insert_Action (N, | |
5556 | Make_Raise_Program_Error (Loc, | |
5557 | Reason => PE_Unchecked_Union_Restriction)); | |
5558 | ||
5559 | -- Prevent Gigi from generating incorrect code by rewriting | |
5560 | -- the equality as a standard False. | |
5561 | ||
5562 | Rewrite (N, | |
5563 | New_Occurrence_Of (Standard_False, Loc)); | |
5564 | ||
5565 | end if; | |
5566 | ||
70482933 RK |
5567 | -- If a type support function is present (for complex cases), use it |
5568 | ||
fbf5a39b AC |
5569 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
5570 | Build_Equality_Call | |
5571 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 RK |
5572 | |
5573 | -- Otherwise expand the component by component equality. Note that | |
8fc789c8 | 5574 | -- we never use block-bit comparisons for records, because of the |
70482933 RK |
5575 | -- problems with gaps. The backend will often be able to recombine |
5576 | -- the separate comparisons that we generate here. | |
5577 | ||
5578 | else | |
5579 | Remove_Side_Effects (Lhs); | |
5580 | Remove_Side_Effects (Rhs); | |
5581 | Rewrite (N, | |
5582 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
5583 | ||
5584 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
5585 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5586 | end if; | |
5587 | end if; | |
5588 | ||
d26dc4b5 | 5589 | -- Test if result is known at compile time |
70482933 | 5590 | |
d26dc4b5 | 5591 | Rewrite_Comparison (N); |
f02b8bb8 RD |
5592 | |
5593 | -- If we still have comparison for Vax_Float, process it | |
5594 | ||
5595 | if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then | |
5596 | Expand_Vax_Comparison (N); | |
5597 | return; | |
5598 | end if; | |
70482933 RK |
5599 | end Expand_N_Op_Eq; |
5600 | ||
5601 | ----------------------- | |
5602 | -- Expand_N_Op_Expon -- | |
5603 | ----------------------- | |
5604 | ||
5605 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
5606 | Loc : constant Source_Ptr := Sloc (N); | |
5607 | Typ : constant Entity_Id := Etype (N); | |
5608 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
5609 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 5610 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
5611 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
5612 | Exptyp : constant Entity_Id := Etype (Exp); | |
5613 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
5614 | Expv : Uint; | |
5615 | Xnode : Node_Id; | |
5616 | Temp : Node_Id; | |
5617 | Rent : RE_Id; | |
5618 | Ent : Entity_Id; | |
fbf5a39b | 5619 | Etyp : Entity_Id; |
70482933 RK |
5620 | |
5621 | begin | |
5622 | Binary_Op_Validity_Checks (N); | |
5623 | ||
685094bf RD |
5624 | -- If either operand is of a private type, then we have the use of an |
5625 | -- intrinsic operator, and we get rid of the privateness, by using root | |
5626 | -- types of underlying types for the actual operation. Otherwise the | |
5627 | -- private types will cause trouble if we expand multiplications or | |
5628 | -- shifts etc. We also do this transformation if the result type is | |
5629 | -- different from the base type. | |
07fc65c4 GB |
5630 | |
5631 | if Is_Private_Type (Etype (Base)) | |
5632 | or else | |
5633 | Is_Private_Type (Typ) | |
5634 | or else | |
5635 | Is_Private_Type (Exptyp) | |
5636 | or else | |
5637 | Rtyp /= Root_Type (Bastyp) | |
5638 | then | |
5639 | declare | |
5640 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
5641 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
5642 | ||
5643 | begin | |
5644 | Rewrite (N, | |
5645 | Unchecked_Convert_To (Typ, | |
5646 | Make_Op_Expon (Loc, | |
5647 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
5648 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
5649 | Analyze_And_Resolve (N, Typ); | |
5650 | return; | |
5651 | end; | |
5652 | end if; | |
5653 | ||
fbf5a39b | 5654 | -- Test for case of known right argument |
70482933 RK |
5655 | |
5656 | if Compile_Time_Known_Value (Exp) then | |
5657 | Expv := Expr_Value (Exp); | |
5658 | ||
5659 | -- We only fold small non-negative exponents. You might think we | |
5660 | -- could fold small negative exponents for the real case, but we | |
5661 | -- can't because we are required to raise Constraint_Error for | |
5662 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
5663 | -- See ACVC test C4A012B. | |
5664 | ||
5665 | if Expv >= 0 and then Expv <= 4 then | |
5666 | ||
5667 | -- X ** 0 = 1 (or 1.0) | |
5668 | ||
5669 | if Expv = 0 then | |
abcbd24c ST |
5670 | |
5671 | -- Call Remove_Side_Effects to ensure that any side effects | |
5672 | -- in the ignored left operand (in particular function calls | |
5673 | -- to user defined functions) are properly executed. | |
5674 | ||
5675 | Remove_Side_Effects (Base); | |
5676 | ||
70482933 RK |
5677 | if Ekind (Typ) in Integer_Kind then |
5678 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
5679 | else | |
5680 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
5681 | end if; | |
5682 | ||
5683 | -- X ** 1 = X | |
5684 | ||
5685 | elsif Expv = 1 then | |
5686 | Xnode := Base; | |
5687 | ||
5688 | -- X ** 2 = X * X | |
5689 | ||
5690 | elsif Expv = 2 then | |
5691 | Xnode := | |
5692 | Make_Op_Multiply (Loc, | |
5693 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 5694 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
5695 | |
5696 | -- X ** 3 = X * X * X | |
5697 | ||
5698 | elsif Expv = 3 then | |
5699 | Xnode := | |
5700 | Make_Op_Multiply (Loc, | |
5701 | Left_Opnd => | |
5702 | Make_Op_Multiply (Loc, | |
5703 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
5704 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
5705 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
5706 | |
5707 | -- X ** 4 -> | |
5708 | -- En : constant base'type := base * base; | |
5709 | -- ... | |
5710 | -- En * En | |
5711 | ||
5712 | else -- Expv = 4 | |
5713 | Temp := | |
5714 | Make_Defining_Identifier (Loc, New_Internal_Name ('E')); | |
5715 | ||
5716 | Insert_Actions (N, New_List ( | |
5717 | Make_Object_Declaration (Loc, | |
5718 | Defining_Identifier => Temp, | |
5719 | Constant_Present => True, | |
5720 | Object_Definition => New_Reference_To (Typ, Loc), | |
5721 | Expression => | |
5722 | Make_Op_Multiply (Loc, | |
5723 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 5724 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base))))); |
70482933 RK |
5725 | |
5726 | Xnode := | |
5727 | Make_Op_Multiply (Loc, | |
5728 | Left_Opnd => New_Reference_To (Temp, Loc), | |
5729 | Right_Opnd => New_Reference_To (Temp, Loc)); | |
5730 | end if; | |
5731 | ||
5732 | Rewrite (N, Xnode); | |
5733 | Analyze_And_Resolve (N, Typ); | |
5734 | return; | |
5735 | end if; | |
5736 | end if; | |
5737 | ||
5738 | -- Case of (2 ** expression) appearing as an argument of an integer | |
5739 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 5740 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
5741 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
5742 | -- of the higher level node converts it into a shift. | |
5743 | ||
685094bf RD |
5744 | -- Note: this transformation is not applicable for a modular type with |
5745 | -- a non-binary modulus in the multiplication case, since we get a wrong | |
5746 | -- result if the shift causes an overflow before the modular reduction. | |
5747 | ||
70482933 RK |
5748 | if Nkind (Base) = N_Integer_Literal |
5749 | and then Intval (Base) = 2 | |
5750 | and then Is_Integer_Type (Root_Type (Exptyp)) | |
5751 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
5752 | and then Is_Unsigned_Type (Exptyp) | |
5753 | and then not Ovflo | |
5754 | and then Nkind (Parent (N)) in N_Binary_Op | |
5755 | then | |
5756 | declare | |
5757 | P : constant Node_Id := Parent (N); | |
5758 | L : constant Node_Id := Left_Opnd (P); | |
5759 | R : constant Node_Id := Right_Opnd (P); | |
5760 | ||
5761 | begin | |
5762 | if (Nkind (P) = N_Op_Multiply | |
685094bf | 5763 | and then not Non_Binary_Modulus (Typ) |
70482933 RK |
5764 | and then |
5765 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
5766 | or else | |
5767 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
5768 | and then not Do_Overflow_Check (P)) | |
5769 | ||
5770 | or else | |
5771 | (Nkind (P) = N_Op_Divide | |
5772 | and then Is_Integer_Type (Etype (L)) | |
5773 | and then Is_Unsigned_Type (Etype (L)) | |
5774 | and then R = N | |
5775 | and then not Do_Overflow_Check (P)) | |
5776 | then | |
5777 | Set_Is_Power_Of_2_For_Shift (N); | |
5778 | return; | |
5779 | end if; | |
5780 | end; | |
5781 | end if; | |
5782 | ||
07fc65c4 GB |
5783 | -- Fall through if exponentiation must be done using a runtime routine |
5784 | ||
07fc65c4 | 5785 | -- First deal with modular case |
70482933 RK |
5786 | |
5787 | if Is_Modular_Integer_Type (Rtyp) then | |
5788 | ||
5789 | -- Non-binary case, we call the special exponentiation routine for | |
5790 | -- the non-binary case, converting the argument to Long_Long_Integer | |
5791 | -- and passing the modulus value. Then the result is converted back | |
5792 | -- to the base type. | |
5793 | ||
5794 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
5795 | Rewrite (N, |
5796 | Convert_To (Typ, | |
5797 | Make_Function_Call (Loc, | |
5798 | Name => New_Reference_To (RTE (RE_Exp_Modular), Loc), | |
5799 | Parameter_Associations => New_List ( | |
5800 | Convert_To (Standard_Integer, Base), | |
5801 | Make_Integer_Literal (Loc, Modulus (Rtyp)), | |
5802 | Exp)))); | |
5803 | ||
685094bf RD |
5804 | -- Binary case, in this case, we call one of two routines, either the |
5805 | -- unsigned integer case, or the unsigned long long integer case, | |
5806 | -- with a final "and" operation to do the required mod. | |
70482933 RK |
5807 | |
5808 | else | |
5809 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
5810 | Ent := RTE (RE_Exp_Unsigned); | |
5811 | else | |
5812 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
5813 | end if; | |
5814 | ||
5815 | Rewrite (N, | |
5816 | Convert_To (Typ, | |
5817 | Make_Op_And (Loc, | |
5818 | Left_Opnd => | |
5819 | Make_Function_Call (Loc, | |
5820 | Name => New_Reference_To (Ent, Loc), | |
5821 | Parameter_Associations => New_List ( | |
5822 | Convert_To (Etype (First_Formal (Ent)), Base), | |
5823 | Exp)), | |
5824 | Right_Opnd => | |
5825 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
5826 | ||
5827 | end if; | |
5828 | ||
5829 | -- Common exit point for modular type case | |
5830 | ||
5831 | Analyze_And_Resolve (N, Typ); | |
5832 | return; | |
5833 | ||
fbf5a39b AC |
5834 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
5835 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
5836 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 5837 | -- might need certification when a certified run time is required. |
70482933 | 5838 | |
fbf5a39b | 5839 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
5840 | -- checks are required, and one when they are not required, since there |
5841 | -- is a real gain in omitting checks on many machines. | |
70482933 | 5842 | |
fbf5a39b AC |
5843 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
5844 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
5845 | and then | |
5846 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
5847 | or else (Rtyp = Universal_Integer) | |
70482933 | 5848 | then |
fbf5a39b AC |
5849 | Etyp := Standard_Long_Long_Integer; |
5850 | ||
70482933 RK |
5851 | if Ovflo then |
5852 | Rent := RE_Exp_Long_Long_Integer; | |
5853 | else | |
5854 | Rent := RE_Exn_Long_Long_Integer; | |
5855 | end if; | |
5856 | ||
fbf5a39b AC |
5857 | elsif Is_Signed_Integer_Type (Rtyp) then |
5858 | Etyp := Standard_Integer; | |
70482933 RK |
5859 | |
5860 | if Ovflo then | |
fbf5a39b | 5861 | Rent := RE_Exp_Integer; |
70482933 | 5862 | else |
fbf5a39b | 5863 | Rent := RE_Exn_Integer; |
70482933 | 5864 | end if; |
fbf5a39b AC |
5865 | |
5866 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
5867 | -- need separate routines for the overflow case here, since in the case | |
5868 | -- of floating-point, we generate infinities anyway as a rule (either | |
5869 | -- that or we automatically trap overflow), and if there is an infinity | |
5870 | -- generated and a range check is required, the check will fail anyway. | |
5871 | ||
5872 | else | |
5873 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
5874 | Etyp := Standard_Long_Long_Float; | |
5875 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
5876 | end if; |
5877 | ||
5878 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 5879 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 5880 | |
fbf5a39b | 5881 | if Typ = Etyp |
70482933 RK |
5882 | and then Rtyp /= Universal_Integer |
5883 | and then Rtyp /= Universal_Real | |
5884 | then | |
5885 | Rewrite (N, | |
5886 | Make_Function_Call (Loc, | |
5887 | Name => New_Reference_To (RTE (Rent), Loc), | |
5888 | Parameter_Associations => New_List (Base, Exp))); | |
5889 | ||
5890 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 5891 | -- required in the universal cases, since the runtime routine is |
1147c704 | 5892 | -- typed using one of the standard types). |
70482933 RK |
5893 | |
5894 | else | |
5895 | Rewrite (N, | |
5896 | Convert_To (Typ, | |
5897 | Make_Function_Call (Loc, | |
5898 | Name => New_Reference_To (RTE (Rent), Loc), | |
5899 | Parameter_Associations => New_List ( | |
fbf5a39b | 5900 | Convert_To (Etyp, Base), |
70482933 RK |
5901 | Exp)))); |
5902 | end if; | |
5903 | ||
5904 | Analyze_And_Resolve (N, Typ); | |
5905 | return; | |
5906 | ||
fbf5a39b AC |
5907 | exception |
5908 | when RE_Not_Available => | |
5909 | return; | |
70482933 RK |
5910 | end Expand_N_Op_Expon; |
5911 | ||
5912 | -------------------- | |
5913 | -- Expand_N_Op_Ge -- | |
5914 | -------------------- | |
5915 | ||
5916 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
5917 | Typ : constant Entity_Id := Etype (N); | |
5918 | Op1 : constant Node_Id := Left_Opnd (N); | |
5919 | Op2 : constant Node_Id := Right_Opnd (N); | |
5920 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
5921 | ||
5922 | begin | |
5923 | Binary_Op_Validity_Checks (N); | |
5924 | ||
f02b8bb8 | 5925 | if Is_Array_Type (Typ1) then |
70482933 RK |
5926 | Expand_Array_Comparison (N); |
5927 | return; | |
5928 | end if; | |
5929 | ||
5930 | if Is_Boolean_Type (Typ1) then | |
5931 | Adjust_Condition (Op1); | |
5932 | Adjust_Condition (Op2); | |
5933 | Set_Etype (N, Standard_Boolean); | |
5934 | Adjust_Result_Type (N, Typ); | |
5935 | end if; | |
5936 | ||
5937 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
5938 | |
5939 | -- If we still have comparison, and Vax_Float type, process it | |
5940 | ||
5941 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
5942 | Expand_Vax_Comparison (N); | |
5943 | return; | |
5944 | end if; | |
70482933 RK |
5945 | end Expand_N_Op_Ge; |
5946 | ||
5947 | -------------------- | |
5948 | -- Expand_N_Op_Gt -- | |
5949 | -------------------- | |
5950 | ||
5951 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
5952 | Typ : constant Entity_Id := Etype (N); | |
5953 | Op1 : constant Node_Id := Left_Opnd (N); | |
5954 | Op2 : constant Node_Id := Right_Opnd (N); | |
5955 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
5956 | ||
5957 | begin | |
5958 | Binary_Op_Validity_Checks (N); | |
5959 | ||
f02b8bb8 | 5960 | if Is_Array_Type (Typ1) then |
70482933 RK |
5961 | Expand_Array_Comparison (N); |
5962 | return; | |
5963 | end if; | |
5964 | ||
5965 | if Is_Boolean_Type (Typ1) then | |
5966 | Adjust_Condition (Op1); | |
5967 | Adjust_Condition (Op2); | |
5968 | Set_Etype (N, Standard_Boolean); | |
5969 | Adjust_Result_Type (N, Typ); | |
5970 | end if; | |
5971 | ||
5972 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
5973 | |
5974 | -- If we still have comparison, and Vax_Float type, process it | |
5975 | ||
5976 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
5977 | Expand_Vax_Comparison (N); | |
5978 | return; | |
5979 | end if; | |
70482933 RK |
5980 | end Expand_N_Op_Gt; |
5981 | ||
5982 | -------------------- | |
5983 | -- Expand_N_Op_Le -- | |
5984 | -------------------- | |
5985 | ||
5986 | procedure Expand_N_Op_Le (N : Node_Id) is | |
5987 | Typ : constant Entity_Id := Etype (N); | |
5988 | Op1 : constant Node_Id := Left_Opnd (N); | |
5989 | Op2 : constant Node_Id := Right_Opnd (N); | |
5990 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
5991 | ||
5992 | begin | |
5993 | Binary_Op_Validity_Checks (N); | |
5994 | ||
f02b8bb8 | 5995 | if Is_Array_Type (Typ1) then |
70482933 RK |
5996 | Expand_Array_Comparison (N); |
5997 | return; | |
5998 | end if; | |
5999 | ||
6000 | if Is_Boolean_Type (Typ1) then | |
6001 | Adjust_Condition (Op1); | |
6002 | Adjust_Condition (Op2); | |
6003 | Set_Etype (N, Standard_Boolean); | |
6004 | Adjust_Result_Type (N, Typ); | |
6005 | end if; | |
6006 | ||
6007 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6008 | |
6009 | -- If we still have comparison, and Vax_Float type, process it | |
6010 | ||
6011 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6012 | Expand_Vax_Comparison (N); | |
6013 | return; | |
6014 | end if; | |
70482933 RK |
6015 | end Expand_N_Op_Le; |
6016 | ||
6017 | -------------------- | |
6018 | -- Expand_N_Op_Lt -- | |
6019 | -------------------- | |
6020 | ||
6021 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
6022 | Typ : constant Entity_Id := Etype (N); | |
6023 | Op1 : constant Node_Id := Left_Opnd (N); | |
6024 | Op2 : constant Node_Id := Right_Opnd (N); | |
6025 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6026 | ||
6027 | begin | |
6028 | Binary_Op_Validity_Checks (N); | |
6029 | ||
f02b8bb8 | 6030 | if Is_Array_Type (Typ1) then |
70482933 RK |
6031 | Expand_Array_Comparison (N); |
6032 | return; | |
6033 | end if; | |
6034 | ||
6035 | if Is_Boolean_Type (Typ1) then | |
6036 | Adjust_Condition (Op1); | |
6037 | Adjust_Condition (Op2); | |
6038 | Set_Etype (N, Standard_Boolean); | |
6039 | Adjust_Result_Type (N, Typ); | |
6040 | end if; | |
6041 | ||
6042 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6043 | |
6044 | -- If we still have comparison, and Vax_Float type, process it | |
6045 | ||
6046 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6047 | Expand_Vax_Comparison (N); | |
6048 | return; | |
6049 | end if; | |
70482933 RK |
6050 | end Expand_N_Op_Lt; |
6051 | ||
6052 | ----------------------- | |
6053 | -- Expand_N_Op_Minus -- | |
6054 | ----------------------- | |
6055 | ||
6056 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
6057 | Loc : constant Source_Ptr := Sloc (N); | |
6058 | Typ : constant Entity_Id := Etype (N); | |
6059 | ||
6060 | begin | |
6061 | Unary_Op_Validity_Checks (N); | |
6062 | ||
07fc65c4 | 6063 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
6064 | and then Is_Signed_Integer_Type (Etype (N)) |
6065 | and then Do_Overflow_Check (N) | |
6066 | then | |
6067 | -- Software overflow checking expands -expr into (0 - expr) | |
6068 | ||
6069 | Rewrite (N, | |
6070 | Make_Op_Subtract (Loc, | |
6071 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
6072 | Right_Opnd => Right_Opnd (N))); | |
6073 | ||
6074 | Analyze_And_Resolve (N, Typ); | |
6075 | ||
6076 | -- Vax floating-point types case | |
6077 | ||
6078 | elsif Vax_Float (Etype (N)) then | |
6079 | Expand_Vax_Arith (N); | |
6080 | end if; | |
6081 | end Expand_N_Op_Minus; | |
6082 | ||
6083 | --------------------- | |
6084 | -- Expand_N_Op_Mod -- | |
6085 | --------------------- | |
6086 | ||
6087 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
6088 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 6089 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
6090 | Left : constant Node_Id := Left_Opnd (N); |
6091 | Right : constant Node_Id := Right_Opnd (N); | |
6092 | DOC : constant Boolean := Do_Overflow_Check (N); | |
6093 | DDC : constant Boolean := Do_Division_Check (N); | |
6094 | ||
6095 | LLB : Uint; | |
6096 | Llo : Uint; | |
6097 | Lhi : Uint; | |
6098 | LOK : Boolean; | |
6099 | Rlo : Uint; | |
6100 | Rhi : Uint; | |
6101 | ROK : Boolean; | |
6102 | ||
1033834f RD |
6103 | pragma Warnings (Off, Lhi); |
6104 | ||
70482933 RK |
6105 | begin |
6106 | Binary_Op_Validity_Checks (N); | |
6107 | ||
6108 | Determine_Range (Right, ROK, Rlo, Rhi); | |
6109 | Determine_Range (Left, LOK, Llo, Lhi); | |
6110 | ||
6111 | -- Convert mod to rem if operands are known non-negative. We do this | |
6112 | -- since it is quite likely that this will improve the quality of code, | |
6113 | -- (the operation now corresponds to the hardware remainder), and it | |
6114 | -- does not seem likely that it could be harmful. | |
6115 | ||
6116 | if LOK and then Llo >= 0 | |
6117 | and then | |
6118 | ROK and then Rlo >= 0 | |
6119 | then | |
6120 | Rewrite (N, | |
6121 | Make_Op_Rem (Sloc (N), | |
6122 | Left_Opnd => Left_Opnd (N), | |
6123 | Right_Opnd => Right_Opnd (N))); | |
6124 | ||
685094bf RD |
6125 | -- Instead of reanalyzing the node we do the analysis manually. This |
6126 | -- avoids anomalies when the replacement is done in an instance and | |
6127 | -- is epsilon more efficient. | |
70482933 RK |
6128 | |
6129 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 6130 | Set_Etype (N, Typ); |
70482933 RK |
6131 | Set_Do_Overflow_Check (N, DOC); |
6132 | Set_Do_Division_Check (N, DDC); | |
6133 | Expand_N_Op_Rem (N); | |
6134 | Set_Analyzed (N); | |
6135 | ||
6136 | -- Otherwise, normal mod processing | |
6137 | ||
6138 | else | |
6139 | if Is_Integer_Type (Etype (N)) then | |
6140 | Apply_Divide_Check (N); | |
6141 | end if; | |
6142 | ||
fbf5a39b AC |
6143 | -- Apply optimization x mod 1 = 0. We don't really need that with |
6144 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
6145 | -- certainly harmless. | |
6146 | ||
6147 | if Is_Integer_Type (Etype (N)) | |
6148 | and then Compile_Time_Known_Value (Right) | |
6149 | and then Expr_Value (Right) = Uint_1 | |
6150 | then | |
abcbd24c ST |
6151 | -- Call Remove_Side_Effects to ensure that any side effects in |
6152 | -- the ignored left operand (in particular function calls to | |
6153 | -- user defined functions) are properly executed. | |
6154 | ||
6155 | Remove_Side_Effects (Left); | |
6156 | ||
fbf5a39b AC |
6157 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
6158 | Analyze_And_Resolve (N, Typ); | |
6159 | return; | |
6160 | end if; | |
6161 | ||
70482933 RK |
6162 | -- Deal with annoying case of largest negative number remainder |
6163 | -- minus one. Gigi does not handle this case correctly, because | |
6164 | -- it generates a divide instruction which may trap in this case. | |
6165 | ||
685094bf RD |
6166 | -- In fact the check is quite easy, if the right operand is -1, then |
6167 | -- the mod value is always 0, and we can just ignore the left operand | |
6168 | -- completely in this case. | |
70482933 | 6169 | |
30783513 | 6170 | -- The operand type may be private (e.g. in the expansion of an |
685094bf RD |
6171 | -- intrinsic operation) so we must use the underlying type to get the |
6172 | -- bounds, and convert the literals explicitly. | |
fbf5a39b AC |
6173 | |
6174 | LLB := | |
6175 | Expr_Value | |
6176 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
70482933 RK |
6177 | |
6178 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
6179 | and then | |
6180 | ((not LOK) or else (Llo = LLB)) | |
6181 | then | |
6182 | Rewrite (N, | |
6183 | Make_Conditional_Expression (Loc, | |
6184 | Expressions => New_List ( | |
6185 | Make_Op_Eq (Loc, | |
6186 | Left_Opnd => Duplicate_Subexpr (Right), | |
6187 | Right_Opnd => | |
fbf5a39b AC |
6188 | Unchecked_Convert_To (Typ, |
6189 | Make_Integer_Literal (Loc, -1))), | |
6190 | Unchecked_Convert_To (Typ, | |
6191 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
6192 | Relocate_Node (N)))); |
6193 | ||
6194 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
fbf5a39b | 6195 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
6196 | end if; |
6197 | end if; | |
6198 | end Expand_N_Op_Mod; | |
6199 | ||
6200 | -------------------------- | |
6201 | -- Expand_N_Op_Multiply -- | |
6202 | -------------------------- | |
6203 | ||
6204 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
6205 | Loc : constant Source_Ptr := Sloc (N); |
6206 | Lop : constant Node_Id := Left_Opnd (N); | |
6207 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 6208 | |
abcbd24c ST |
6209 | Lp2 : constant Boolean := |
6210 | Nkind (Lop) = N_Op_Expon | |
6211 | and then Is_Power_Of_2_For_Shift (Lop); | |
fbf5a39b | 6212 | |
abcbd24c ST |
6213 | Rp2 : constant Boolean := |
6214 | Nkind (Rop) = N_Op_Expon | |
6215 | and then Is_Power_Of_2_For_Shift (Rop); | |
fbf5a39b | 6216 | |
70482933 RK |
6217 | Ltyp : constant Entity_Id := Etype (Lop); |
6218 | Rtyp : constant Entity_Id := Etype (Rop); | |
6219 | Typ : Entity_Id := Etype (N); | |
6220 | ||
6221 | begin | |
6222 | Binary_Op_Validity_Checks (N); | |
6223 | ||
6224 | -- Special optimizations for integer types | |
6225 | ||
6226 | if Is_Integer_Type (Typ) then | |
6227 | ||
abcbd24c | 6228 | -- N * 0 = 0 for integer types |
70482933 | 6229 | |
abcbd24c ST |
6230 | if Compile_Time_Known_Value (Rop) |
6231 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 6232 | then |
abcbd24c ST |
6233 | -- Call Remove_Side_Effects to ensure that any side effects in |
6234 | -- the ignored left operand (in particular function calls to | |
6235 | -- user defined functions) are properly executed. | |
6236 | ||
6237 | Remove_Side_Effects (Lop); | |
6238 | ||
6239 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
6240 | Analyze_And_Resolve (N, Typ); | |
6241 | return; | |
6242 | end if; | |
6243 | ||
6244 | -- Similar handling for 0 * N = 0 | |
6245 | ||
6246 | if Compile_Time_Known_Value (Lop) | |
6247 | and then Expr_Value (Lop) = Uint_0 | |
6248 | then | |
6249 | Remove_Side_Effects (Rop); | |
70482933 RK |
6250 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
6251 | Analyze_And_Resolve (N, Typ); | |
6252 | return; | |
6253 | end if; | |
6254 | ||
6255 | -- N * 1 = 1 * N = N for integer types | |
6256 | ||
fbf5a39b AC |
6257 | -- This optimisation is not done if we are going to |
6258 | -- rewrite the product 1 * 2 ** N to a shift. | |
6259 | ||
6260 | if Compile_Time_Known_Value (Rop) | |
6261 | and then Expr_Value (Rop) = Uint_1 | |
6262 | and then not Lp2 | |
70482933 | 6263 | then |
fbf5a39b | 6264 | Rewrite (N, Lop); |
70482933 RK |
6265 | return; |
6266 | ||
fbf5a39b AC |
6267 | elsif Compile_Time_Known_Value (Lop) |
6268 | and then Expr_Value (Lop) = Uint_1 | |
6269 | and then not Rp2 | |
70482933 | 6270 | then |
fbf5a39b | 6271 | Rewrite (N, Rop); |
70482933 RK |
6272 | return; |
6273 | end if; | |
6274 | end if; | |
6275 | ||
70482933 RK |
6276 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
6277 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
6278 | -- operand is an integer, as required for this to work. | |
6279 | ||
fbf5a39b AC |
6280 | if Rp2 then |
6281 | if Lp2 then | |
70482933 | 6282 | |
fbf5a39b | 6283 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
6284 | |
6285 | Rewrite (N, | |
6286 | Make_Op_Expon (Loc, | |
6287 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
6288 | Right_Opnd => | |
6289 | Make_Op_Add (Loc, | |
6290 | Left_Opnd => Right_Opnd (Lop), | |
6291 | Right_Opnd => Right_Opnd (Rop)))); | |
6292 | Analyze_And_Resolve (N, Typ); | |
6293 | return; | |
6294 | ||
6295 | else | |
6296 | Rewrite (N, | |
6297 | Make_Op_Shift_Left (Loc, | |
6298 | Left_Opnd => Lop, | |
6299 | Right_Opnd => | |
6300 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
6301 | Analyze_And_Resolve (N, Typ); | |
6302 | return; | |
6303 | end if; | |
6304 | ||
6305 | -- Same processing for the operands the other way round | |
6306 | ||
fbf5a39b | 6307 | elsif Lp2 then |
70482933 RK |
6308 | Rewrite (N, |
6309 | Make_Op_Shift_Left (Loc, | |
6310 | Left_Opnd => Rop, | |
6311 | Right_Opnd => | |
6312 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
6313 | Analyze_And_Resolve (N, Typ); | |
6314 | return; | |
6315 | end if; | |
6316 | ||
6317 | -- Do required fixup of universal fixed operation | |
6318 | ||
6319 | if Typ = Universal_Fixed then | |
6320 | Fixup_Universal_Fixed_Operation (N); | |
6321 | Typ := Etype (N); | |
6322 | end if; | |
6323 | ||
6324 | -- Multiplications with fixed-point results | |
6325 | ||
6326 | if Is_Fixed_Point_Type (Typ) then | |
6327 | ||
685094bf RD |
6328 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
6329 | -- a semantic point of view such operations are simply integer | |
6330 | -- operations and will be treated that way. | |
70482933 RK |
6331 | |
6332 | if not Treat_Fixed_As_Integer (N) then | |
6333 | ||
6334 | -- Case of fixed * integer => fixed | |
6335 | ||
6336 | if Is_Integer_Type (Rtyp) then | |
6337 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
6338 | ||
6339 | -- Case of integer * fixed => fixed | |
6340 | ||
6341 | elsif Is_Integer_Type (Ltyp) then | |
6342 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
6343 | ||
6344 | -- Case of fixed * fixed => fixed | |
6345 | ||
6346 | else | |
6347 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
6348 | end if; | |
6349 | end if; | |
6350 | ||
685094bf RD |
6351 | -- Other cases of multiplication of fixed-point operands. Again we |
6352 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
6353 | |
6354 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
6355 | and then not Treat_Fixed_As_Integer (N) | |
6356 | then | |
6357 | if Is_Integer_Type (Typ) then | |
6358 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
6359 | else | |
6360 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
6361 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
6362 | end if; | |
6363 | ||
685094bf RD |
6364 | -- Mixed-mode operations can appear in a non-static universal context, |
6365 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
6366 | |
6367 | elsif Typ = Universal_Real | |
6368 | and then Is_Integer_Type (Rtyp) | |
6369 | then | |
6370 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); | |
6371 | ||
6372 | Analyze_And_Resolve (Rop, Universal_Real); | |
6373 | ||
6374 | elsif Typ = Universal_Real | |
6375 | and then Is_Integer_Type (Ltyp) | |
6376 | then | |
6377 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); | |
6378 | ||
6379 | Analyze_And_Resolve (Lop, Universal_Real); | |
6380 | ||
6381 | -- Non-fixed point cases, check software overflow checking required | |
6382 | ||
6383 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
6384 | Apply_Arithmetic_Overflow_Check (N); | |
f02b8bb8 RD |
6385 | |
6386 | -- Deal with VAX float case | |
6387 | ||
6388 | elsif Vax_Float (Typ) then | |
6389 | Expand_Vax_Arith (N); | |
6390 | return; | |
70482933 RK |
6391 | end if; |
6392 | end Expand_N_Op_Multiply; | |
6393 | ||
6394 | -------------------- | |
6395 | -- Expand_N_Op_Ne -- | |
6396 | -------------------- | |
6397 | ||
70482933 | 6398 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 6399 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
6400 | |
6401 | begin | |
f02b8bb8 | 6402 | -- Case of elementary type with standard operator |
70482933 | 6403 | |
f02b8bb8 RD |
6404 | if Is_Elementary_Type (Typ) |
6405 | and then Sloc (Entity (N)) = Standard_Location | |
6406 | then | |
6407 | Binary_Op_Validity_Checks (N); | |
70482933 | 6408 | |
f02b8bb8 | 6409 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 6410 | |
f02b8bb8 RD |
6411 | if Is_Boolean_Type (Typ) then |
6412 | Adjust_Condition (Left_Opnd (N)); | |
6413 | Adjust_Condition (Right_Opnd (N)); | |
6414 | Set_Etype (N, Standard_Boolean); | |
6415 | Adjust_Result_Type (N, Typ); | |
6416 | end if; | |
fbf5a39b | 6417 | |
f02b8bb8 RD |
6418 | Rewrite_Comparison (N); |
6419 | ||
6420 | -- If we still have comparison for Vax_Float, process it | |
6421 | ||
6422 | if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then | |
6423 | Expand_Vax_Comparison (N); | |
6424 | return; | |
6425 | end if; | |
6426 | ||
6427 | -- For all cases other than elementary types, we rewrite node as the | |
6428 | -- negation of an equality operation, and reanalyze. The equality to be | |
6429 | -- used is defined in the same scope and has the same signature. This | |
6430 | -- signature must be set explicitly since in an instance it may not have | |
6431 | -- the same visibility as in the generic unit. This avoids duplicating | |
6432 | -- or factoring the complex code for record/array equality tests etc. | |
6433 | ||
6434 | else | |
6435 | declare | |
6436 | Loc : constant Source_Ptr := Sloc (N); | |
6437 | Neg : Node_Id; | |
6438 | Ne : constant Entity_Id := Entity (N); | |
6439 | ||
6440 | begin | |
6441 | Binary_Op_Validity_Checks (N); | |
6442 | ||
6443 | Neg := | |
6444 | Make_Op_Not (Loc, | |
6445 | Right_Opnd => | |
6446 | Make_Op_Eq (Loc, | |
6447 | Left_Opnd => Left_Opnd (N), | |
6448 | Right_Opnd => Right_Opnd (N))); | |
6449 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
6450 | ||
6451 | if Scope (Ne) /= Standard_Standard then | |
6452 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
6453 | end if; | |
6454 | ||
6455 | -- For navigation purposes, the inequality is treated as an | |
6456 | -- implicit reference to the corresponding equality. Preserve the | |
6457 | -- Comes_From_ source flag so that the proper Xref entry is | |
6458 | -- generated. | |
6459 | ||
6460 | Preserve_Comes_From_Source (Neg, N); | |
6461 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
6462 | Rewrite (N, Neg); | |
6463 | Analyze_And_Resolve (N, Standard_Boolean); | |
6464 | end; | |
6465 | end if; | |
70482933 RK |
6466 | end Expand_N_Op_Ne; |
6467 | ||
6468 | --------------------- | |
6469 | -- Expand_N_Op_Not -- | |
6470 | --------------------- | |
6471 | ||
685094bf RD |
6472 | -- If the argument is other than a Boolean array type, there is no special |
6473 | -- expansion required. | |
70482933 RK |
6474 | |
6475 | -- For the packed case, we call the special routine in Exp_Pakd, except | |
6476 | -- that if the component size is greater than one, we use the standard | |
6477 | -- routine generating a gruesome loop (it is so peculiar to have packed | |
685094bf RD |
6478 | -- arrays with non-standard Boolean representations anyway, so it does not |
6479 | -- matter that we do not handle this case efficiently). | |
70482933 | 6480 | |
685094bf RD |
6481 | -- For the unpacked case (and for the special packed case where we have non |
6482 | -- standard Booleans, as discussed above), we generate and insert into the | |
6483 | -- tree the following function definition: | |
70482933 RK |
6484 | |
6485 | -- function Nnnn (A : arr) is | |
6486 | -- B : arr; | |
6487 | -- begin | |
6488 | -- for J in a'range loop | |
6489 | -- B (J) := not A (J); | |
6490 | -- end loop; | |
6491 | -- return B; | |
6492 | -- end Nnnn; | |
6493 | ||
6494 | -- Here arr is the actual subtype of the parameter (and hence always | |
6495 | -- constrained). Then we replace the not with a call to this function. | |
6496 | ||
6497 | procedure Expand_N_Op_Not (N : Node_Id) is | |
6498 | Loc : constant Source_Ptr := Sloc (N); | |
6499 | Typ : constant Entity_Id := Etype (N); | |
6500 | Opnd : Node_Id; | |
6501 | Arr : Entity_Id; | |
6502 | A : Entity_Id; | |
6503 | B : Entity_Id; | |
6504 | J : Entity_Id; | |
6505 | A_J : Node_Id; | |
6506 | B_J : Node_Id; | |
6507 | ||
6508 | Func_Name : Entity_Id; | |
6509 | Loop_Statement : Node_Id; | |
6510 | ||
6511 | begin | |
6512 | Unary_Op_Validity_Checks (N); | |
6513 | ||
6514 | -- For boolean operand, deal with non-standard booleans | |
6515 | ||
6516 | if Is_Boolean_Type (Typ) then | |
6517 | Adjust_Condition (Right_Opnd (N)); | |
6518 | Set_Etype (N, Standard_Boolean); | |
6519 | Adjust_Result_Type (N, Typ); | |
6520 | return; | |
6521 | end if; | |
6522 | ||
6523 | -- Only array types need any other processing | |
6524 | ||
6525 | if not Is_Array_Type (Typ) then | |
6526 | return; | |
6527 | end if; | |
6528 | ||
a9d8907c JM |
6529 | -- Case of array operand. If bit packed with a component size of 1, |
6530 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 6531 | |
a9d8907c JM |
6532 | if Is_Bit_Packed_Array (Typ) |
6533 | and then Component_Size (Typ) = 1 | |
6534 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
6535 | then | |
70482933 RK |
6536 | Expand_Packed_Not (N); |
6537 | return; | |
6538 | end if; | |
6539 | ||
fbf5a39b AC |
6540 | -- Case of array operand which is not bit-packed. If the context is |
6541 | -- a safe assignment, call in-place operation, If context is a larger | |
6542 | -- boolean expression in the context of a safe assignment, expansion is | |
6543 | -- done by enclosing operation. | |
70482933 RK |
6544 | |
6545 | Opnd := Relocate_Node (Right_Opnd (N)); | |
6546 | Convert_To_Actual_Subtype (Opnd); | |
6547 | Arr := Etype (Opnd); | |
6548 | Ensure_Defined (Arr, N); | |
b4592168 | 6549 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 6550 | |
fbf5a39b AC |
6551 | if Nkind (Parent (N)) = N_Assignment_Statement then |
6552 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
6553 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
6554 | return; | |
6555 | ||
5e1c00fa | 6556 | -- Special case the negation of a binary operation |
fbf5a39b | 6557 | |
303b4d58 | 6558 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 6559 | and then Safe_In_Place_Array_Op |
303b4d58 | 6560 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
6561 | then |
6562 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
6563 | return; | |
6564 | end if; | |
6565 | ||
6566 | elsif Nkind (Parent (N)) in N_Binary_Op | |
6567 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
6568 | then | |
6569 | declare | |
6570 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
6571 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
6572 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
6573 | ||
6574 | begin | |
6575 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
6576 | if N = Op1 | |
6577 | and then Nkind (Op2) = N_Op_Not | |
6578 | then | |
5e1c00fa | 6579 | -- (not A) op (not B) can be reduced to a single call |
fbf5a39b AC |
6580 | |
6581 | return; | |
6582 | ||
6583 | elsif N = Op2 | |
6584 | and then Nkind (Parent (N)) = N_Op_Xor | |
6585 | then | |
5e1c00fa | 6586 | -- A xor (not B) can also be special-cased |
fbf5a39b AC |
6587 | |
6588 | return; | |
6589 | end if; | |
6590 | end if; | |
6591 | end; | |
6592 | end if; | |
6593 | ||
70482933 RK |
6594 | A := Make_Defining_Identifier (Loc, Name_uA); |
6595 | B := Make_Defining_Identifier (Loc, Name_uB); | |
6596 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
6597 | ||
6598 | A_J := | |
6599 | Make_Indexed_Component (Loc, | |
6600 | Prefix => New_Reference_To (A, Loc), | |
6601 | Expressions => New_List (New_Reference_To (J, Loc))); | |
6602 | ||
6603 | B_J := | |
6604 | Make_Indexed_Component (Loc, | |
6605 | Prefix => New_Reference_To (B, Loc), | |
6606 | Expressions => New_List (New_Reference_To (J, Loc))); | |
6607 | ||
6608 | Loop_Statement := | |
6609 | Make_Implicit_Loop_Statement (N, | |
6610 | Identifier => Empty, | |
6611 | ||
6612 | Iteration_Scheme => | |
6613 | Make_Iteration_Scheme (Loc, | |
6614 | Loop_Parameter_Specification => | |
6615 | Make_Loop_Parameter_Specification (Loc, | |
6616 | Defining_Identifier => J, | |
6617 | Discrete_Subtype_Definition => | |
6618 | Make_Attribute_Reference (Loc, | |
6619 | Prefix => Make_Identifier (Loc, Chars (A)), | |
6620 | Attribute_Name => Name_Range))), | |
6621 | ||
6622 | Statements => New_List ( | |
6623 | Make_Assignment_Statement (Loc, | |
6624 | Name => B_J, | |
6625 | Expression => Make_Op_Not (Loc, A_J)))); | |
6626 | ||
6627 | Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N')); | |
6628 | Set_Is_Inlined (Func_Name); | |
6629 | ||
6630 | Insert_Action (N, | |
6631 | Make_Subprogram_Body (Loc, | |
6632 | Specification => | |
6633 | Make_Function_Specification (Loc, | |
6634 | Defining_Unit_Name => Func_Name, | |
6635 | Parameter_Specifications => New_List ( | |
6636 | Make_Parameter_Specification (Loc, | |
6637 | Defining_Identifier => A, | |
6638 | Parameter_Type => New_Reference_To (Typ, Loc))), | |
630d30e9 | 6639 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
6640 | |
6641 | Declarations => New_List ( | |
6642 | Make_Object_Declaration (Loc, | |
6643 | Defining_Identifier => B, | |
6644 | Object_Definition => New_Reference_To (Arr, Loc))), | |
6645 | ||
6646 | Handled_Statement_Sequence => | |
6647 | Make_Handled_Sequence_Of_Statements (Loc, | |
6648 | Statements => New_List ( | |
6649 | Loop_Statement, | |
d766cee3 | 6650 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
6651 | Expression => |
6652 | Make_Identifier (Loc, Chars (B))))))); | |
6653 | ||
6654 | Rewrite (N, | |
6655 | Make_Function_Call (Loc, | |
6656 | Name => New_Reference_To (Func_Name, Loc), | |
6657 | Parameter_Associations => New_List (Opnd))); | |
6658 | ||
6659 | Analyze_And_Resolve (N, Typ); | |
6660 | end Expand_N_Op_Not; | |
6661 | ||
6662 | -------------------- | |
6663 | -- Expand_N_Op_Or -- | |
6664 | -------------------- | |
6665 | ||
6666 | procedure Expand_N_Op_Or (N : Node_Id) is | |
6667 | Typ : constant Entity_Id := Etype (N); | |
6668 | ||
6669 | begin | |
6670 | Binary_Op_Validity_Checks (N); | |
6671 | ||
6672 | if Is_Array_Type (Etype (N)) then | |
6673 | Expand_Boolean_Operator (N); | |
6674 | ||
6675 | elsif Is_Boolean_Type (Etype (N)) then | |
6676 | Adjust_Condition (Left_Opnd (N)); | |
6677 | Adjust_Condition (Right_Opnd (N)); | |
6678 | Set_Etype (N, Standard_Boolean); | |
6679 | Adjust_Result_Type (N, Typ); | |
6680 | end if; | |
6681 | end Expand_N_Op_Or; | |
6682 | ||
6683 | ---------------------- | |
6684 | -- Expand_N_Op_Plus -- | |
6685 | ---------------------- | |
6686 | ||
6687 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
6688 | begin | |
6689 | Unary_Op_Validity_Checks (N); | |
6690 | end Expand_N_Op_Plus; | |
6691 | ||
6692 | --------------------- | |
6693 | -- Expand_N_Op_Rem -- | |
6694 | --------------------- | |
6695 | ||
6696 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
6697 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 6698 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
6699 | |
6700 | Left : constant Node_Id := Left_Opnd (N); | |
6701 | Right : constant Node_Id := Right_Opnd (N); | |
6702 | ||
6703 | LLB : Uint; | |
6704 | Llo : Uint; | |
6705 | Lhi : Uint; | |
6706 | LOK : Boolean; | |
6707 | Rlo : Uint; | |
6708 | Rhi : Uint; | |
6709 | ROK : Boolean; | |
70482933 | 6710 | |
1033834f RD |
6711 | pragma Warnings (Off, Lhi); |
6712 | ||
70482933 RK |
6713 | begin |
6714 | Binary_Op_Validity_Checks (N); | |
6715 | ||
6716 | if Is_Integer_Type (Etype (N)) then | |
6717 | Apply_Divide_Check (N); | |
6718 | end if; | |
6719 | ||
685094bf RD |
6720 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
6721 | -- but it is useful with other back ends (e.g. AAMP), and is certainly | |
6722 | -- harmless. | |
fbf5a39b AC |
6723 | |
6724 | if Is_Integer_Type (Etype (N)) | |
6725 | and then Compile_Time_Known_Value (Right) | |
6726 | and then Expr_Value (Right) = Uint_1 | |
6727 | then | |
abcbd24c ST |
6728 | -- Call Remove_Side_Effects to ensure that any side effects in the |
6729 | -- ignored left operand (in particular function calls to user defined | |
6730 | -- functions) are properly executed. | |
6731 | ||
6732 | Remove_Side_Effects (Left); | |
6733 | ||
fbf5a39b AC |
6734 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
6735 | Analyze_And_Resolve (N, Typ); | |
6736 | return; | |
6737 | end if; | |
6738 | ||
685094bf RD |
6739 | -- Deal with annoying case of largest negative number remainder minus |
6740 | -- one. Gigi does not handle this case correctly, because it generates | |
6741 | -- a divide instruction which may trap in this case. | |
70482933 | 6742 | |
685094bf RD |
6743 | -- In fact the check is quite easy, if the right operand is -1, then |
6744 | -- the remainder is always 0, and we can just ignore the left operand | |
6745 | -- completely in this case. | |
70482933 RK |
6746 | |
6747 | Determine_Range (Right, ROK, Rlo, Rhi); | |
6748 | Determine_Range (Left, LOK, Llo, Lhi); | |
fbf5a39b | 6749 | |
16b05213 | 6750 | -- The operand type may be private (e.g. in the expansion of an |
685094bf RD |
6751 | -- intrinsic operation) so we must use the underlying type to get the |
6752 | -- bounds, and convert the literals explicitly. | |
fbf5a39b AC |
6753 | |
6754 | LLB := | |
6755 | Expr_Value | |
6756 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
6757 | ||
6758 | -- Now perform the test, generating code only if needed | |
70482933 RK |
6759 | |
6760 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
6761 | and then | |
6762 | ((not LOK) or else (Llo = LLB)) | |
6763 | then | |
6764 | Rewrite (N, | |
6765 | Make_Conditional_Expression (Loc, | |
6766 | Expressions => New_List ( | |
6767 | Make_Op_Eq (Loc, | |
6768 | Left_Opnd => Duplicate_Subexpr (Right), | |
6769 | Right_Opnd => | |
fbf5a39b AC |
6770 | Unchecked_Convert_To (Typ, |
6771 | Make_Integer_Literal (Loc, -1))), | |
70482933 | 6772 | |
fbf5a39b AC |
6773 | Unchecked_Convert_To (Typ, |
6774 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
6775 | |
6776 | Relocate_Node (N)))); | |
6777 | ||
6778 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
6779 | Analyze_And_Resolve (N, Typ); | |
6780 | end if; | |
6781 | end Expand_N_Op_Rem; | |
6782 | ||
6783 | ----------------------------- | |
6784 | -- Expand_N_Op_Rotate_Left -- | |
6785 | ----------------------------- | |
6786 | ||
6787 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
6788 | begin | |
6789 | Binary_Op_Validity_Checks (N); | |
6790 | end Expand_N_Op_Rotate_Left; | |
6791 | ||
6792 | ------------------------------ | |
6793 | -- Expand_N_Op_Rotate_Right -- | |
6794 | ------------------------------ | |
6795 | ||
6796 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
6797 | begin | |
6798 | Binary_Op_Validity_Checks (N); | |
6799 | end Expand_N_Op_Rotate_Right; | |
6800 | ||
6801 | ---------------------------- | |
6802 | -- Expand_N_Op_Shift_Left -- | |
6803 | ---------------------------- | |
6804 | ||
6805 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is | |
6806 | begin | |
6807 | Binary_Op_Validity_Checks (N); | |
6808 | end Expand_N_Op_Shift_Left; | |
6809 | ||
6810 | ----------------------------- | |
6811 | -- Expand_N_Op_Shift_Right -- | |
6812 | ----------------------------- | |
6813 | ||
6814 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
6815 | begin | |
6816 | Binary_Op_Validity_Checks (N); | |
6817 | end Expand_N_Op_Shift_Right; | |
6818 | ||
6819 | ---------------------------------------- | |
6820 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
6821 | ---------------------------------------- | |
6822 | ||
6823 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
6824 | begin | |
6825 | Binary_Op_Validity_Checks (N); | |
6826 | end Expand_N_Op_Shift_Right_Arithmetic; | |
6827 | ||
6828 | -------------------------- | |
6829 | -- Expand_N_Op_Subtract -- | |
6830 | -------------------------- | |
6831 | ||
6832 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
6833 | Typ : constant Entity_Id := Etype (N); | |
6834 | ||
6835 | begin | |
6836 | Binary_Op_Validity_Checks (N); | |
6837 | ||
6838 | -- N - 0 = N for integer types | |
6839 | ||
6840 | if Is_Integer_Type (Typ) | |
6841 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
6842 | and then Expr_Value (Right_Opnd (N)) = 0 | |
6843 | then | |
6844 | Rewrite (N, Left_Opnd (N)); | |
6845 | return; | |
6846 | end if; | |
6847 | ||
8fc789c8 | 6848 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 RK |
6849 | |
6850 | if Is_Signed_Integer_Type (Typ) | |
6851 | or else Is_Fixed_Point_Type (Typ) | |
6852 | then | |
6853 | Apply_Arithmetic_Overflow_Check (N); | |
6854 | ||
6855 | -- Vax floating-point types case | |
6856 | ||
6857 | elsif Vax_Float (Typ) then | |
6858 | Expand_Vax_Arith (N); | |
6859 | end if; | |
6860 | end Expand_N_Op_Subtract; | |
6861 | ||
6862 | --------------------- | |
6863 | -- Expand_N_Op_Xor -- | |
6864 | --------------------- | |
6865 | ||
6866 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
6867 | Typ : constant Entity_Id := Etype (N); | |
6868 | ||
6869 | begin | |
6870 | Binary_Op_Validity_Checks (N); | |
6871 | ||
6872 | if Is_Array_Type (Etype (N)) then | |
6873 | Expand_Boolean_Operator (N); | |
6874 | ||
6875 | elsif Is_Boolean_Type (Etype (N)) then | |
6876 | Adjust_Condition (Left_Opnd (N)); | |
6877 | Adjust_Condition (Right_Opnd (N)); | |
6878 | Set_Etype (N, Standard_Boolean); | |
6879 | Adjust_Result_Type (N, Typ); | |
6880 | end if; | |
6881 | end Expand_N_Op_Xor; | |
6882 | ||
6883 | ---------------------- | |
6884 | -- Expand_N_Or_Else -- | |
6885 | ---------------------- | |
6886 | ||
6887 | -- Expand into conditional expression if Actions present, and also | |
6888 | -- deal with optimizing case of arguments being True or False. | |
6889 | ||
6890 | procedure Expand_N_Or_Else (N : Node_Id) is | |
6891 | Loc : constant Source_Ptr := Sloc (N); | |
6892 | Typ : constant Entity_Id := Etype (N); | |
6893 | Left : constant Node_Id := Left_Opnd (N); | |
6894 | Right : constant Node_Id := Right_Opnd (N); | |
6895 | Actlist : List_Id; | |
6896 | ||
6897 | begin | |
6898 | -- Deal with non-standard booleans | |
6899 | ||
6900 | if Is_Boolean_Type (Typ) then | |
6901 | Adjust_Condition (Left); | |
6902 | Adjust_Condition (Right); | |
6903 | Set_Etype (N, Standard_Boolean); | |
fbf5a39b | 6904 | end if; |
70482933 | 6905 | |
deff3e5e | 6906 | -- Check for cases where left argument is known to be True or False |
70482933 | 6907 | |
deff3e5e | 6908 | if Compile_Time_Known_Value (Left) then |
70482933 RK |
6909 | |
6910 | -- If left argument is False, change (False or else Right) to Right. | |
6911 | -- Any actions associated with Right will be executed unconditionally | |
6912 | -- and can thus be inserted into the tree unconditionally. | |
6913 | ||
deff3e5e | 6914 | if Expr_Value_E (Left) = Standard_False then |
70482933 RK |
6915 | if Present (Actions (N)) then |
6916 | Insert_Actions (N, Actions (N)); | |
6917 | end if; | |
6918 | ||
6919 | Rewrite (N, Right); | |
70482933 | 6920 | |
685094bf RD |
6921 | -- If left argument is True, change (True and then Right) to True. In |
6922 | -- this case we can forget the actions associated with Right, since | |
6923 | -- they will never be executed. | |
70482933 | 6924 | |
deff3e5e | 6925 | else pragma Assert (Expr_Value_E (Left) = Standard_True); |
70482933 RK |
6926 | Kill_Dead_Code (Right); |
6927 | Kill_Dead_Code (Actions (N)); | |
6928 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
70482933 | 6929 | end if; |
deff3e5e BD |
6930 | |
6931 | Adjust_Result_Type (N, Typ); | |
6932 | return; | |
70482933 RK |
6933 | end if; |
6934 | ||
6935 | -- If Actions are present, we expand | |
6936 | ||
6937 | -- left or else right | |
6938 | ||
6939 | -- into | |
6940 | ||
6941 | -- if left then True else right end | |
6942 | ||
6943 | -- with the actions becoming the Else_Actions of the conditional | |
6944 | -- expression. This conditional expression is then further expanded | |
6945 | -- (and will eventually disappear) | |
6946 | ||
6947 | if Present (Actions (N)) then | |
6948 | Actlist := Actions (N); | |
6949 | Rewrite (N, | |
6950 | Make_Conditional_Expression (Loc, | |
6951 | Expressions => New_List ( | |
6952 | Left, | |
6953 | New_Occurrence_Of (Standard_True, Loc), | |
6954 | Right))); | |
6955 | ||
6956 | Set_Else_Actions (N, Actlist); | |
6957 | Analyze_And_Resolve (N, Standard_Boolean); | |
6958 | Adjust_Result_Type (N, Typ); | |
6959 | return; | |
6960 | end if; | |
6961 | ||
6962 | -- No actions present, check for cases of right argument True/False | |
6963 | ||
deff3e5e | 6964 | if Compile_Time_Known_Value (Right) then |
70482933 | 6965 | |
685094bf RD |
6966 | -- Change (Left or else False) to Left. Note that we know there are |
6967 | -- no actions associated with the True operand, since we just checked | |
6968 | -- for this case above. | |
70482933 | 6969 | |
deff3e5e | 6970 | if Expr_Value_E (Right) = Standard_False then |
70482933 RK |
6971 | Rewrite (N, Left); |
6972 | ||
685094bf RD |
6973 | -- Change (Left or else True) to True, making sure to preserve any |
6974 | -- side effects associated with the Left operand. | |
70482933 | 6975 | |
deff3e5e | 6976 | else pragma Assert (Expr_Value_E (Right) = Standard_True); |
70482933 RK |
6977 | Remove_Side_Effects (Left); |
6978 | Rewrite | |
6979 | (N, New_Occurrence_Of (Standard_True, Loc)); | |
6980 | end if; | |
6981 | end if; | |
6982 | ||
6983 | Adjust_Result_Type (N, Typ); | |
6984 | end Expand_N_Or_Else; | |
6985 | ||
6986 | ----------------------------------- | |
6987 | -- Expand_N_Qualified_Expression -- | |
6988 | ----------------------------------- | |
6989 | ||
6990 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
6991 | Operand : constant Node_Id := Expression (N); | |
6992 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
6993 | ||
6994 | begin | |
f82944b7 JM |
6995 | -- Do validity check if validity checking operands |
6996 | ||
6997 | if Validity_Checks_On | |
6998 | and then Validity_Check_Operands | |
6999 | then | |
7000 | Ensure_Valid (Operand); | |
7001 | end if; | |
7002 | ||
7003 | -- Apply possible constraint check | |
7004 | ||
70482933 RK |
7005 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
7006 | end Expand_N_Qualified_Expression; | |
7007 | ||
7008 | --------------------------------- | |
7009 | -- Expand_N_Selected_Component -- | |
7010 | --------------------------------- | |
7011 | ||
7012 | -- If the selector is a discriminant of a concurrent object, rewrite the | |
7013 | -- prefix to denote the corresponding record type. | |
7014 | ||
7015 | procedure Expand_N_Selected_Component (N : Node_Id) is | |
7016 | Loc : constant Source_Ptr := Sloc (N); | |
7017 | Par : constant Node_Id := Parent (N); | |
7018 | P : constant Node_Id := Prefix (N); | |
fbf5a39b | 7019 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 7020 | Disc : Entity_Id; |
70482933 | 7021 | New_N : Node_Id; |
fbf5a39b | 7022 | Dcon : Elmt_Id; |
70482933 RK |
7023 | |
7024 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
7025 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
7026 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
7027 | -- Don't we have a general routine that does this??? |
7028 | ||
7029 | ----------------------- | |
7030 | -- In_Left_Hand_Side -- | |
7031 | ----------------------- | |
70482933 RK |
7032 | |
7033 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
7034 | begin | |
fbf5a39b AC |
7035 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
7036 | and then Comp = Name (Parent (Comp))) | |
7037 | or else (Present (Parent (Comp)) | |
7038 | and then Nkind (Parent (Comp)) in N_Subexpr | |
7039 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
7040 | end In_Left_Hand_Side; |
7041 | ||
fbf5a39b AC |
7042 | -- Start of processing for Expand_N_Selected_Component |
7043 | ||
70482933 | 7044 | begin |
fbf5a39b AC |
7045 | -- Insert explicit dereference if required |
7046 | ||
7047 | if Is_Access_Type (Ptyp) then | |
7048 | Insert_Explicit_Dereference (P); | |
e6f69614 | 7049 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
7050 | |
7051 | if Ekind (Etype (P)) = E_Private_Subtype | |
7052 | and then Is_For_Access_Subtype (Etype (P)) | |
7053 | then | |
7054 | Set_Etype (P, Base_Type (Etype (P))); | |
7055 | end if; | |
7056 | ||
7057 | Ptyp := Etype (P); | |
7058 | end if; | |
7059 | ||
7060 | -- Deal with discriminant check required | |
7061 | ||
70482933 RK |
7062 | if Do_Discriminant_Check (N) then |
7063 | ||
685094bf RD |
7064 | -- Present the discriminant checking function to the backend, so that |
7065 | -- it can inline the call to the function. | |
70482933 RK |
7066 | |
7067 | Add_Inlined_Body | |
7068 | (Discriminant_Checking_Func | |
7069 | (Original_Record_Component (Entity (Selector_Name (N))))); | |
70482933 | 7070 | |
fbf5a39b | 7071 | -- Now reset the flag and generate the call |
70482933 | 7072 | |
fbf5a39b AC |
7073 | Set_Do_Discriminant_Check (N, False); |
7074 | Generate_Discriminant_Check (N); | |
70482933 RK |
7075 | end if; |
7076 | ||
b4592168 GD |
7077 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
7078 | -- function, then additional actuals must be passed. | |
7079 | ||
7080 | if Ada_Version >= Ada_05 | |
7081 | and then Is_Build_In_Place_Function_Call (P) | |
7082 | then | |
7083 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
7084 | end if; | |
7085 | ||
fbf5a39b AC |
7086 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
7087 | -- selected component with discriminants. This must be checked during | |
7088 | -- expansion, because during analysis the type of the selector is not | |
7089 | -- known at the point the prefix is analyzed. If the conversion is the | |
7090 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
7091 | |
7092 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
7093 | and then Has_Discriminants (Etype (N)) | |
7094 | and then not In_Left_Hand_Side (N) | |
7095 | then | |
7096 | Force_Evaluation (Prefix (N)); | |
7097 | end if; | |
7098 | ||
7099 | -- Remaining processing applies only if selector is a discriminant | |
7100 | ||
7101 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
7102 | ||
7103 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
7104 | -- we may be able to rewrite the expression with the actual value |
7105 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
7106 | |
7107 | if Is_Record_Type (Ptyp) | |
7108 | and then Has_Discriminants (Ptyp) | |
7109 | and then Is_Constrained (Ptyp) | |
70482933 | 7110 | then |
fbf5a39b AC |
7111 | -- Do this optimization for discrete types only, and not for |
7112 | -- access types (access discriminants get us into trouble!) | |
70482933 | 7113 | |
fbf5a39b AC |
7114 | if not Is_Discrete_Type (Etype (N)) then |
7115 | null; | |
7116 | ||
7117 | -- Don't do this on the left hand of an assignment statement. | |
7118 | -- Normally one would think that references like this would | |
7119 | -- not occur, but they do in generated code, and mean that | |
7120 | -- we really do want to assign the discriminant! | |
7121 | ||
7122 | elsif Nkind (Par) = N_Assignment_Statement | |
7123 | and then Name (Par) = N | |
7124 | then | |
7125 | null; | |
7126 | ||
685094bf RD |
7127 | -- Don't do this optimization for the prefix of an attribute or |
7128 | -- the operand of an object renaming declaration since these are | |
7129 | -- contexts where we do not want the value anyway. | |
fbf5a39b AC |
7130 | |
7131 | elsif (Nkind (Par) = N_Attribute_Reference | |
7132 | and then Prefix (Par) = N) | |
7133 | or else Is_Renamed_Object (N) | |
7134 | then | |
7135 | null; | |
7136 | ||
7137 | -- Don't do this optimization if we are within the code for a | |
7138 | -- discriminant check, since the whole point of such a check may | |
7139 | -- be to verify the condition on which the code below depends! | |
7140 | ||
7141 | elsif Is_In_Discriminant_Check (N) then | |
7142 | null; | |
7143 | ||
7144 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
7145 | -- still one condition that inhibits the optimization below but |
7146 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
7147 | |
7148 | else | |
685094bf RD |
7149 | -- Loop through discriminants to find the matching discriminant |
7150 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
7151 | |
7152 | Disc := First_Discriminant (Ptyp); | |
7153 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
7154 | Discr_Loop : while Present (Dcon) loop | |
7155 | ||
7156 | -- Check if this is the matching discriminant | |
7157 | ||
7158 | if Disc = Entity (Selector_Name (N)) then | |
70482933 | 7159 | |
fbf5a39b AC |
7160 | -- Here we have the matching discriminant. Check for |
7161 | -- the case of a discriminant of a component that is | |
7162 | -- constrained by an outer discriminant, which cannot | |
7163 | -- be optimized away. | |
7164 | ||
7165 | if | |
7166 | Denotes_Discriminant | |
20b5d666 | 7167 | (Node (Dcon), Check_Concurrent => True) |
fbf5a39b AC |
7168 | then |
7169 | exit Discr_Loop; | |
70482933 | 7170 | |
685094bf RD |
7171 | -- In the context of a case statement, the expression may |
7172 | -- have the base type of the discriminant, and we need to | |
7173 | -- preserve the constraint to avoid spurious errors on | |
7174 | -- missing cases. | |
70482933 | 7175 | |
fbf5a39b AC |
7176 | elsif Nkind (Parent (N)) = N_Case_Statement |
7177 | and then Etype (Node (Dcon)) /= Etype (Disc) | |
70482933 RK |
7178 | then |
7179 | Rewrite (N, | |
7180 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
7181 | Subtype_Mark => |
7182 | New_Occurrence_Of (Etype (Disc), Loc), | |
7183 | Expression => | |
ffe9aba8 AC |
7184 | New_Copy_Tree (Node (Dcon)))); |
7185 | Analyze_And_Resolve (N, Etype (Disc)); | |
fbf5a39b AC |
7186 | |
7187 | -- In case that comes out as a static expression, | |
7188 | -- reset it (a selected component is never static). | |
7189 | ||
7190 | Set_Is_Static_Expression (N, False); | |
7191 | return; | |
7192 | ||
7193 | -- Otherwise we can just copy the constraint, but the | |
ffe9aba8 AC |
7194 | -- result is certainly not static! In some cases the |
7195 | -- discriminant constraint has been analyzed in the | |
7196 | -- context of the original subtype indication, but for | |
7197 | -- itypes the constraint might not have been analyzed | |
7198 | -- yet, and this must be done now. | |
fbf5a39b | 7199 | |
70482933 | 7200 | else |
ffe9aba8 AC |
7201 | Rewrite (N, New_Copy_Tree (Node (Dcon))); |
7202 | Analyze_And_Resolve (N); | |
fbf5a39b AC |
7203 | Set_Is_Static_Expression (N, False); |
7204 | return; | |
70482933 | 7205 | end if; |
70482933 RK |
7206 | end if; |
7207 | ||
fbf5a39b AC |
7208 | Next_Elmt (Dcon); |
7209 | Next_Discriminant (Disc); | |
7210 | end loop Discr_Loop; | |
70482933 | 7211 | |
fbf5a39b AC |
7212 | -- Note: the above loop should always find a matching |
7213 | -- discriminant, but if it does not, we just missed an | |
685094bf RD |
7214 | -- optimization due to some glitch (perhaps a previous error), |
7215 | -- so ignore. | |
fbf5a39b AC |
7216 | |
7217 | end if; | |
70482933 RK |
7218 | end if; |
7219 | ||
7220 | -- The only remaining processing is in the case of a discriminant of | |
7221 | -- a concurrent object, where we rewrite the prefix to denote the | |
7222 | -- corresponding record type. If the type is derived and has renamed | |
7223 | -- discriminants, use corresponding discriminant, which is the one | |
7224 | -- that appears in the corresponding record. | |
7225 | ||
7226 | if not Is_Concurrent_Type (Ptyp) then | |
7227 | return; | |
7228 | end if; | |
7229 | ||
7230 | Disc := Entity (Selector_Name (N)); | |
7231 | ||
7232 | if Is_Derived_Type (Ptyp) | |
7233 | and then Present (Corresponding_Discriminant (Disc)) | |
7234 | then | |
7235 | Disc := Corresponding_Discriminant (Disc); | |
7236 | end if; | |
7237 | ||
7238 | New_N := | |
7239 | Make_Selected_Component (Loc, | |
7240 | Prefix => | |
7241 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
7242 | New_Copy_Tree (P)), | |
7243 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
7244 | ||
7245 | Rewrite (N, New_N); | |
7246 | Analyze (N); | |
7247 | end if; | |
70482933 RK |
7248 | end Expand_N_Selected_Component; |
7249 | ||
7250 | -------------------- | |
7251 | -- Expand_N_Slice -- | |
7252 | -------------------- | |
7253 | ||
7254 | procedure Expand_N_Slice (N : Node_Id) is | |
7255 | Loc : constant Source_Ptr := Sloc (N); | |
7256 | Typ : constant Entity_Id := Etype (N); | |
7257 | Pfx : constant Node_Id := Prefix (N); | |
7258 | Ptp : Entity_Id := Etype (Pfx); | |
fbf5a39b | 7259 | |
81a5b587 | 7260 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
7261 | -- Check whether the argument is an actual for a procedure call, in |
7262 | -- which case the expansion of a bit-packed slice is deferred until the | |
7263 | -- call itself is expanded. The reason this is required is that we might | |
7264 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
7265 | -- that copy out would be missed if we created a temporary here in | |
7266 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
7267 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
7268 | -- is harmless to defer expansion in the IN case, since the call | |
7269 | -- processing will still generate the appropriate copy in operation, | |
7270 | -- which will take care of the slice. | |
81a5b587 | 7271 | |
fbf5a39b | 7272 | procedure Make_Temporary; |
685094bf RD |
7273 | -- Create a named variable for the value of the slice, in cases where |
7274 | -- the back-end cannot handle it properly, e.g. when packed types or | |
7275 | -- unaligned slices are involved. | |
fbf5a39b | 7276 | |
81a5b587 AC |
7277 | ------------------------- |
7278 | -- Is_Procedure_Actual -- | |
7279 | ------------------------- | |
7280 | ||
7281 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
7282 | Par : Node_Id := Parent (N); | |
08aa9a4a | 7283 | |
81a5b587 | 7284 | begin |
81a5b587 | 7285 | loop |
c6a60aa1 RD |
7286 | -- If our parent is a procedure call we can return |
7287 | ||
81a5b587 AC |
7288 | if Nkind (Par) = N_Procedure_Call_Statement then |
7289 | return True; | |
6b6fcd3e | 7290 | |
685094bf RD |
7291 | -- If our parent is a type conversion, keep climbing the tree, |
7292 | -- since a type conversion can be a procedure actual. Also keep | |
7293 | -- climbing if parameter association or a qualified expression, | |
7294 | -- since these are additional cases that do can appear on | |
7295 | -- procedure actuals. | |
6b6fcd3e | 7296 | |
303b4d58 AC |
7297 | elsif Nkind_In (Par, N_Type_Conversion, |
7298 | N_Parameter_Association, | |
7299 | N_Qualified_Expression) | |
c6a60aa1 | 7300 | then |
81a5b587 | 7301 | Par := Parent (Par); |
c6a60aa1 RD |
7302 | |
7303 | -- Any other case is not what we are looking for | |
7304 | ||
7305 | else | |
7306 | return False; | |
81a5b587 AC |
7307 | end if; |
7308 | end loop; | |
81a5b587 AC |
7309 | end Is_Procedure_Actual; |
7310 | ||
fbf5a39b AC |
7311 | -------------------- |
7312 | -- Make_Temporary -- | |
7313 | -------------------- | |
7314 | ||
7315 | procedure Make_Temporary is | |
7316 | Decl : Node_Id; | |
7317 | Ent : constant Entity_Id := | |
7318 | Make_Defining_Identifier (Loc, New_Internal_Name ('T')); | |
7319 | begin | |
7320 | Decl := | |
7321 | Make_Object_Declaration (Loc, | |
7322 | Defining_Identifier => Ent, | |
7323 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
7324 | ||
7325 | Set_No_Initialization (Decl); | |
7326 | ||
7327 | Insert_Actions (N, New_List ( | |
7328 | Decl, | |
7329 | Make_Assignment_Statement (Loc, | |
7330 | Name => New_Occurrence_Of (Ent, Loc), | |
7331 | Expression => Relocate_Node (N)))); | |
7332 | ||
7333 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
7334 | Analyze_And_Resolve (N, Typ); | |
7335 | end Make_Temporary; | |
7336 | ||
7337 | -- Start of processing for Expand_N_Slice | |
70482933 RK |
7338 | |
7339 | begin | |
7340 | -- Special handling for access types | |
7341 | ||
7342 | if Is_Access_Type (Ptp) then | |
7343 | ||
70482933 RK |
7344 | Ptp := Designated_Type (Ptp); |
7345 | ||
e6f69614 AC |
7346 | Rewrite (Pfx, |
7347 | Make_Explicit_Dereference (Sloc (N), | |
7348 | Prefix => Relocate_Node (Pfx))); | |
70482933 | 7349 | |
e6f69614 | 7350 | Analyze_And_Resolve (Pfx, Ptp); |
70482933 RK |
7351 | end if; |
7352 | ||
b4592168 GD |
7353 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
7354 | -- function, then additional actuals must be passed. | |
7355 | ||
7356 | if Ada_Version >= Ada_05 | |
7357 | and then Is_Build_In_Place_Function_Call (Pfx) | |
7358 | then | |
7359 | Make_Build_In_Place_Call_In_Anonymous_Context (Pfx); | |
7360 | end if; | |
7361 | ||
685094bf RD |
7362 | -- Range checks are potentially also needed for cases involving a slice |
7363 | -- indexed by a subtype indication, but Do_Range_Check can currently | |
7364 | -- only be set for expressions ??? | |
70482933 RK |
7365 | |
7366 | if not Index_Checks_Suppressed (Ptp) | |
7367 | and then (not Is_Entity_Name (Pfx) | |
7368 | or else not Index_Checks_Suppressed (Entity (Pfx))) | |
7369 | and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication | |
0669bebe GB |
7370 | |
7371 | -- Do not enable range check to nodes associated with the frontend | |
7372 | -- expansion of the dispatch table. We first check if Ada.Tags is | |
7373 | -- already loaded to avoid the addition of an undesired dependence | |
7374 | -- on such run-time unit. | |
7375 | ||
26bff3d9 JM |
7376 | and then |
7377 | (VM_Target /= No_VM | |
7378 | or else not | |
7379 | (RTU_Loaded (Ada_Tags) | |
7380 | and then Nkind (Prefix (N)) = N_Selected_Component | |
7381 | and then Present (Entity (Selector_Name (Prefix (N)))) | |
7382 | and then Entity (Selector_Name (Prefix (N))) = | |
7383 | RTE_Record_Component (RE_Prims_Ptr))) | |
70482933 RK |
7384 | then |
7385 | Enable_Range_Check (Discrete_Range (N)); | |
7386 | end if; | |
7387 | ||
7388 | -- The remaining case to be handled is packed slices. We can leave | |
7389 | -- packed slices as they are in the following situations: | |
7390 | ||
7391 | -- 1. Right or left side of an assignment (we can handle this | |
7392 | -- situation correctly in the assignment statement expansion). | |
7393 | ||
685094bf RD |
7394 | -- 2. Prefix of indexed component (the slide is optimized away in this |
7395 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 7396 | |
685094bf RD |
7397 | -- 3. Object renaming declaration, since we want the name of the |
7398 | -- slice, not the value. | |
70482933 | 7399 | |
685094bf RD |
7400 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
7401 | -- be required, and this is handled in the expansion of call | |
7402 | -- itself. | |
70482933 | 7403 | |
685094bf RD |
7404 | -- 5. Prefix of an address attribute (this is an error which is caught |
7405 | -- elsewhere, and the expansion would interfere with generating the | |
7406 | -- error message). | |
70482933 | 7407 | |
81a5b587 | 7408 | if not Is_Packed (Typ) then |
08aa9a4a | 7409 | |
685094bf RD |
7410 | -- Apply transformation for actuals of a function call, where |
7411 | -- Expand_Actuals is not used. | |
81a5b587 AC |
7412 | |
7413 | if Nkind (Parent (N)) = N_Function_Call | |
7414 | and then Is_Possibly_Unaligned_Slice (N) | |
7415 | then | |
7416 | Make_Temporary; | |
7417 | end if; | |
7418 | ||
7419 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
7420 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
7421 | and then Parent (N) = Name (Parent (Parent (N)))) | |
70482933 | 7422 | then |
81a5b587 | 7423 | return; |
70482933 | 7424 | |
81a5b587 AC |
7425 | elsif Nkind (Parent (N)) = N_Indexed_Component |
7426 | or else Is_Renamed_Object (N) | |
7427 | or else Is_Procedure_Actual (N) | |
7428 | then | |
7429 | return; | |
70482933 | 7430 | |
91b1417d AC |
7431 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
7432 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 7433 | then |
81a5b587 AC |
7434 | return; |
7435 | ||
7436 | else | |
fbf5a39b | 7437 | Make_Temporary; |
70482933 RK |
7438 | end if; |
7439 | end Expand_N_Slice; | |
7440 | ||
7441 | ------------------------------ | |
7442 | -- Expand_N_Type_Conversion -- | |
7443 | ------------------------------ | |
7444 | ||
7445 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
7446 | Loc : constant Source_Ptr := Sloc (N); | |
7447 | Operand : constant Node_Id := Expression (N); | |
7448 | Target_Type : constant Entity_Id := Etype (N); | |
7449 | Operand_Type : Entity_Id := Etype (Operand); | |
7450 | ||
7451 | procedure Handle_Changed_Representation; | |
685094bf RD |
7452 | -- This is called in the case of record and array type conversions to |
7453 | -- see if there is a change of representation to be handled. Change of | |
7454 | -- representation is actually handled at the assignment statement level, | |
7455 | -- and what this procedure does is rewrite node N conversion as an | |
7456 | -- assignment to temporary. If there is no change of representation, | |
7457 | -- then the conversion node is unchanged. | |
70482933 RK |
7458 | |
7459 | procedure Real_Range_Check; | |
7460 | -- Handles generation of range check for real target value | |
7461 | ||
7462 | ----------------------------------- | |
7463 | -- Handle_Changed_Representation -- | |
7464 | ----------------------------------- | |
7465 | ||
7466 | procedure Handle_Changed_Representation is | |
7467 | Temp : Entity_Id; | |
7468 | Decl : Node_Id; | |
7469 | Odef : Node_Id; | |
7470 | Disc : Node_Id; | |
7471 | N_Ix : Node_Id; | |
7472 | Cons : List_Id; | |
7473 | ||
7474 | begin | |
f82944b7 | 7475 | -- Nothing else to do if no change of representation |
70482933 RK |
7476 | |
7477 | if Same_Representation (Operand_Type, Target_Type) then | |
7478 | return; | |
7479 | ||
7480 | -- The real change of representation work is done by the assignment | |
7481 | -- statement processing. So if this type conversion is appearing as | |
7482 | -- the expression of an assignment statement, nothing needs to be | |
7483 | -- done to the conversion. | |
7484 | ||
7485 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
7486 | return; | |
7487 | ||
7488 | -- Otherwise we need to generate a temporary variable, and do the | |
7489 | -- change of representation assignment into that temporary variable. | |
7490 | -- The conversion is then replaced by a reference to this variable. | |
7491 | ||
7492 | else | |
7493 | Cons := No_List; | |
7494 | ||
685094bf RD |
7495 | -- If type is unconstrained we have to add a constraint, copied |
7496 | -- from the actual value of the left hand side. | |
70482933 RK |
7497 | |
7498 | if not Is_Constrained (Target_Type) then | |
7499 | if Has_Discriminants (Operand_Type) then | |
7500 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
7501 | |
7502 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
7503 | Disc := First_Stored_Discriminant (Operand_Type); | |
7504 | end if; | |
7505 | ||
70482933 RK |
7506 | Cons := New_List; |
7507 | while Present (Disc) loop | |
7508 | Append_To (Cons, | |
7509 | Make_Selected_Component (Loc, | |
fbf5a39b | 7510 | Prefix => Duplicate_Subexpr_Move_Checks (Operand), |
70482933 RK |
7511 | Selector_Name => |
7512 | Make_Identifier (Loc, Chars (Disc)))); | |
7513 | Next_Discriminant (Disc); | |
7514 | end loop; | |
7515 | ||
7516 | elsif Is_Array_Type (Operand_Type) then | |
7517 | N_Ix := First_Index (Target_Type); | |
7518 | Cons := New_List; | |
7519 | ||
7520 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
7521 | ||
7522 | -- We convert the bounds explicitly. We use an unchecked | |
7523 | -- conversion because bounds checks are done elsewhere. | |
7524 | ||
7525 | Append_To (Cons, | |
7526 | Make_Range (Loc, | |
7527 | Low_Bound => | |
7528 | Unchecked_Convert_To (Etype (N_Ix), | |
7529 | Make_Attribute_Reference (Loc, | |
7530 | Prefix => | |
fbf5a39b | 7531 | Duplicate_Subexpr_No_Checks |
70482933 RK |
7532 | (Operand, Name_Req => True), |
7533 | Attribute_Name => Name_First, | |
7534 | Expressions => New_List ( | |
7535 | Make_Integer_Literal (Loc, J)))), | |
7536 | ||
7537 | High_Bound => | |
7538 | Unchecked_Convert_To (Etype (N_Ix), | |
7539 | Make_Attribute_Reference (Loc, | |
7540 | Prefix => | |
fbf5a39b | 7541 | Duplicate_Subexpr_No_Checks |
70482933 RK |
7542 | (Operand, Name_Req => True), |
7543 | Attribute_Name => Name_Last, | |
7544 | Expressions => New_List ( | |
7545 | Make_Integer_Literal (Loc, J)))))); | |
7546 | ||
7547 | Next_Index (N_Ix); | |
7548 | end loop; | |
7549 | end if; | |
7550 | end if; | |
7551 | ||
7552 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
7553 | ||
7554 | if Present (Cons) then | |
7555 | Odef := | |
7556 | Make_Subtype_Indication (Loc, | |
7557 | Subtype_Mark => Odef, | |
7558 | Constraint => | |
7559 | Make_Index_Or_Discriminant_Constraint (Loc, | |
7560 | Constraints => Cons)); | |
7561 | end if; | |
7562 | ||
7563 | Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C')); | |
7564 | Decl := | |
7565 | Make_Object_Declaration (Loc, | |
7566 | Defining_Identifier => Temp, | |
7567 | Object_Definition => Odef); | |
7568 | ||
7569 | Set_No_Initialization (Decl, True); | |
7570 | ||
7571 | -- Insert required actions. It is essential to suppress checks | |
7572 | -- since we have suppressed default initialization, which means | |
7573 | -- that the variable we create may have no discriminants. | |
7574 | ||
7575 | Insert_Actions (N, | |
7576 | New_List ( | |
7577 | Decl, | |
7578 | Make_Assignment_Statement (Loc, | |
7579 | Name => New_Occurrence_Of (Temp, Loc), | |
7580 | Expression => Relocate_Node (N))), | |
7581 | Suppress => All_Checks); | |
7582 | ||
7583 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7584 | return; | |
7585 | end if; | |
7586 | end Handle_Changed_Representation; | |
7587 | ||
7588 | ---------------------- | |
7589 | -- Real_Range_Check -- | |
7590 | ---------------------- | |
7591 | ||
685094bf RD |
7592 | -- Case of conversions to floating-point or fixed-point. If range checks |
7593 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
7594 | |
7595 | -- typ (x) | |
7596 | ||
7597 | -- to | |
7598 | ||
7599 | -- Tnn : typ'Base := typ'Base (x); | |
7600 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
7601 | -- Tnn | |
7602 | ||
685094bf RD |
7603 | -- This is necessary when there is a conversion of integer to float or |
7604 | -- to fixed-point to ensure that the correct checks are made. It is not | |
7605 | -- necessary for float to float where it is enough to simply set the | |
7606 | -- Do_Range_Check flag. | |
fbf5a39b | 7607 | |
70482933 RK |
7608 | procedure Real_Range_Check is |
7609 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
7610 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
7611 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 7612 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
7613 | Conv : Node_Id; |
7614 | Tnn : Entity_Id; | |
7615 | ||
7616 | begin | |
7617 | -- Nothing to do if conversion was rewritten | |
7618 | ||
7619 | if Nkind (N) /= N_Type_Conversion then | |
7620 | return; | |
7621 | end if; | |
7622 | ||
685094bf RD |
7623 | -- Nothing to do if range checks suppressed, or target has the same |
7624 | -- range as the base type (or is the base type). | |
70482933 RK |
7625 | |
7626 | if Range_Checks_Suppressed (Target_Type) | |
7627 | or else (Lo = Type_Low_Bound (Btyp) | |
7628 | and then | |
7629 | Hi = Type_High_Bound (Btyp)) | |
7630 | then | |
7631 | return; | |
7632 | end if; | |
7633 | ||
685094bf RD |
7634 | -- Nothing to do if expression is an entity on which checks have been |
7635 | -- suppressed. | |
70482933 | 7636 | |
fbf5a39b AC |
7637 | if Is_Entity_Name (Operand) |
7638 | and then Range_Checks_Suppressed (Entity (Operand)) | |
7639 | then | |
7640 | return; | |
7641 | end if; | |
7642 | ||
685094bf RD |
7643 | -- Nothing to do if bounds are all static and we can tell that the |
7644 | -- expression is within the bounds of the target. Note that if the | |
7645 | -- operand is of an unconstrained floating-point type, then we do | |
7646 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
7647 | |
7648 | declare | |
f02b8bb8 RD |
7649 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); |
7650 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
fbf5a39b AC |
7651 | |
7652 | begin | |
7653 | if (not Is_Floating_Point_Type (Xtyp) | |
7654 | or else Is_Constrained (Xtyp)) | |
7655 | and then Compile_Time_Known_Value (S_Lo) | |
7656 | and then Compile_Time_Known_Value (S_Hi) | |
7657 | and then Compile_Time_Known_Value (Hi) | |
7658 | and then Compile_Time_Known_Value (Lo) | |
7659 | then | |
7660 | declare | |
7661 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
7662 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
7663 | S_Lov : Ureal; | |
7664 | S_Hiv : Ureal; | |
7665 | ||
7666 | begin | |
7667 | if Is_Real_Type (Xtyp) then | |
7668 | S_Lov := Expr_Value_R (S_Lo); | |
7669 | S_Hiv := Expr_Value_R (S_Hi); | |
7670 | else | |
7671 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
7672 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
7673 | end if; | |
7674 | ||
7675 | if D_Hiv > D_Lov | |
7676 | and then S_Lov >= D_Lov | |
7677 | and then S_Hiv <= D_Hiv | |
7678 | then | |
7679 | Set_Do_Range_Check (Operand, False); | |
7680 | return; | |
7681 | end if; | |
7682 | end; | |
7683 | end if; | |
7684 | end; | |
7685 | ||
7686 | -- For float to float conversions, we are done | |
7687 | ||
7688 | if Is_Floating_Point_Type (Xtyp) | |
7689 | and then | |
7690 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
7691 | then |
7692 | return; | |
7693 | end if; | |
7694 | ||
fbf5a39b | 7695 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
7696 | |
7697 | Conv := Relocate_Node (N); | |
7698 | Rewrite | |
7699 | (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); | |
7700 | Set_Etype (Conv, Btyp); | |
7701 | ||
f02b8bb8 RD |
7702 | -- Enable overflow except for case of integer to float conversions, |
7703 | -- where it is never required, since we can never have overflow in | |
7704 | -- this case. | |
70482933 | 7705 | |
fbf5a39b AC |
7706 | if not Is_Integer_Type (Etype (Operand)) then |
7707 | Enable_Overflow_Check (Conv); | |
70482933 RK |
7708 | end if; |
7709 | ||
7710 | Tnn := | |
7711 | Make_Defining_Identifier (Loc, | |
7712 | Chars => New_Internal_Name ('T')); | |
7713 | ||
7714 | Insert_Actions (N, New_List ( | |
7715 | Make_Object_Declaration (Loc, | |
7716 | Defining_Identifier => Tnn, | |
7717 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
7718 | Expression => Conv), | |
7719 | ||
7720 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
7721 | Condition => |
7722 | Make_Or_Else (Loc, | |
7723 | Left_Opnd => | |
7724 | Make_Op_Lt (Loc, | |
7725 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
7726 | Right_Opnd => | |
7727 | Make_Attribute_Reference (Loc, | |
7728 | Attribute_Name => Name_First, | |
7729 | Prefix => | |
7730 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 7731 | |
07fc65c4 GB |
7732 | Right_Opnd => |
7733 | Make_Op_Gt (Loc, | |
7734 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
7735 | Right_Opnd => | |
7736 | Make_Attribute_Reference (Loc, | |
7737 | Attribute_Name => Name_Last, | |
7738 | Prefix => | |
7739 | New_Occurrence_Of (Target_Type, Loc)))), | |
7740 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
7741 | |
7742 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
7743 | Analyze_And_Resolve (N, Btyp); | |
7744 | end Real_Range_Check; | |
7745 | ||
7746 | -- Start of processing for Expand_N_Type_Conversion | |
7747 | ||
7748 | begin | |
685094bf RD |
7749 | -- Nothing at all to do if conversion is to the identical type so remove |
7750 | -- the conversion completely, it is useless. | |
70482933 RK |
7751 | |
7752 | if Operand_Type = Target_Type then | |
fbf5a39b | 7753 | Rewrite (N, Relocate_Node (Operand)); |
70482933 RK |
7754 | return; |
7755 | end if; | |
7756 | ||
685094bf RD |
7757 | -- Nothing to do if this is the second argument of read. This is a |
7758 | -- "backwards" conversion that will be handled by the specialized code | |
7759 | -- in attribute processing. | |
70482933 RK |
7760 | |
7761 | if Nkind (Parent (N)) = N_Attribute_Reference | |
7762 | and then Attribute_Name (Parent (N)) = Name_Read | |
7763 | and then Next (First (Expressions (Parent (N)))) = N | |
7764 | then | |
7765 | return; | |
7766 | end if; | |
7767 | ||
7768 | -- Here if we may need to expand conversion | |
7769 | ||
f82944b7 JM |
7770 | -- Do validity check if validity checking operands |
7771 | ||
7772 | if Validity_Checks_On | |
7773 | and then Validity_Check_Operands | |
7774 | then | |
7775 | Ensure_Valid (Operand); | |
7776 | end if; | |
7777 | ||
70482933 RK |
7778 | -- Special case of converting from non-standard boolean type |
7779 | ||
7780 | if Is_Boolean_Type (Operand_Type) | |
7781 | and then (Nonzero_Is_True (Operand_Type)) | |
7782 | then | |
7783 | Adjust_Condition (Operand); | |
7784 | Set_Etype (Operand, Standard_Boolean); | |
7785 | Operand_Type := Standard_Boolean; | |
7786 | end if; | |
7787 | ||
7788 | -- Case of converting to an access type | |
7789 | ||
7790 | if Is_Access_Type (Target_Type) then | |
7791 | ||
d766cee3 RD |
7792 | -- Apply an accessibility check when the conversion operand is an |
7793 | -- access parameter (or a renaming thereof), unless conversion was | |
e84e11ba GD |
7794 | -- expanded from an Unchecked_ or Unrestricted_Access attribute. |
7795 | -- Note that other checks may still need to be applied below (such | |
7796 | -- as tagged type checks). | |
70482933 RK |
7797 | |
7798 | if Is_Entity_Name (Operand) | |
d766cee3 RD |
7799 | and then |
7800 | (Is_Formal (Entity (Operand)) | |
7801 | or else | |
7802 | (Present (Renamed_Object (Entity (Operand))) | |
7803 | and then Is_Entity_Name (Renamed_Object (Entity (Operand))) | |
7804 | and then Is_Formal | |
7805 | (Entity (Renamed_Object (Entity (Operand)))))) | |
70482933 | 7806 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type |
d766cee3 RD |
7807 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
7808 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 7809 | then |
e84e11ba GD |
7810 | Apply_Accessibility_Check |
7811 | (Operand, Target_Type, Insert_Node => Operand); | |
70482933 | 7812 | |
e84e11ba | 7813 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
7814 | -- level of the target type, then force Program_Error. Note that this |
7815 | -- can only occur for cases where the attribute is within the body of | |
7816 | -- an instantiation (otherwise the conversion will already have been | |
7817 | -- rejected as illegal). Note: warnings are issued by the analyzer | |
7818 | -- for the instance cases. | |
70482933 RK |
7819 | |
7820 | elsif In_Instance_Body | |
07fc65c4 GB |
7821 | and then Type_Access_Level (Operand_Type) > |
7822 | Type_Access_Level (Target_Type) | |
70482933 | 7823 | then |
07fc65c4 GB |
7824 | Rewrite (N, |
7825 | Make_Raise_Program_Error (Sloc (N), | |
7826 | Reason => PE_Accessibility_Check_Failed)); | |
70482933 RK |
7827 | Set_Etype (N, Target_Type); |
7828 | ||
685094bf RD |
7829 | -- When the operand is a selected access discriminant the check needs |
7830 | -- to be made against the level of the object denoted by the prefix | |
7831 | -- of the selected name. Force Program_Error for this case as well | |
7832 | -- (this accessibility violation can only happen if within the body | |
7833 | -- of an instantiation). | |
70482933 RK |
7834 | |
7835 | elsif In_Instance_Body | |
7836 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
7837 | and then Nkind (Operand) = N_Selected_Component | |
7838 | and then Object_Access_Level (Operand) > | |
7839 | Type_Access_Level (Target_Type) | |
7840 | then | |
07fc65c4 GB |
7841 | Rewrite (N, |
7842 | Make_Raise_Program_Error (Sloc (N), | |
7843 | Reason => PE_Accessibility_Check_Failed)); | |
70482933 RK |
7844 | Set_Etype (N, Target_Type); |
7845 | end if; | |
7846 | end if; | |
7847 | ||
7848 | -- Case of conversions of tagged types and access to tagged types | |
7849 | ||
685094bf RD |
7850 | -- When needed, that is to say when the expression is class-wide, Add |
7851 | -- runtime a tag check for (strict) downward conversion by using the | |
7852 | -- membership test, generating: | |
70482933 RK |
7853 | |
7854 | -- [constraint_error when Operand not in Target_Type'Class] | |
7855 | ||
7856 | -- or in the access type case | |
7857 | ||
7858 | -- [constraint_error | |
7859 | -- when Operand /= null | |
7860 | -- and then Operand.all not in | |
7861 | -- Designated_Type (Target_Type)'Class] | |
7862 | ||
7863 | if (Is_Access_Type (Target_Type) | |
7864 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
7865 | or else Is_Tagged_Type (Target_Type) | |
7866 | then | |
685094bf RD |
7867 | -- Do not do any expansion in the access type case if the parent is a |
7868 | -- renaming, since this is an error situation which will be caught by | |
7869 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 RK |
7870 | |
7871 | if Is_Access_Type (Target_Type) | |
7872 | and then Is_Renamed_Object (N) | |
7873 | then | |
7874 | return; | |
7875 | end if; | |
7876 | ||
0669bebe | 7877 | -- Otherwise, proceed with processing tagged conversion |
70482933 RK |
7878 | |
7879 | declare | |
8cea7b64 HK |
7880 | Actual_Op_Typ : Entity_Id; |
7881 | Actual_Targ_Typ : Entity_Id; | |
7882 | Make_Conversion : Boolean := False; | |
7883 | Root_Op_Typ : Entity_Id; | |
70482933 | 7884 | |
8cea7b64 HK |
7885 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
7886 | -- Create a membership check to test whether Operand is a member | |
7887 | -- of Targ_Typ. If the original Target_Type is an access, include | |
7888 | -- a test for null value. The check is inserted at N. | |
7889 | ||
7890 | -------------------- | |
7891 | -- Make_Tag_Check -- | |
7892 | -------------------- | |
7893 | ||
7894 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
7895 | Cond : Node_Id; | |
7896 | ||
7897 | begin | |
7898 | -- Generate: | |
7899 | -- [Constraint_Error | |
7900 | -- when Operand /= null | |
7901 | -- and then Operand.all not in Targ_Typ] | |
7902 | ||
7903 | if Is_Access_Type (Target_Type) then | |
7904 | Cond := | |
7905 | Make_And_Then (Loc, | |
7906 | Left_Opnd => | |
7907 | Make_Op_Ne (Loc, | |
7908 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
7909 | Right_Opnd => Make_Null (Loc)), | |
7910 | ||
7911 | Right_Opnd => | |
7912 | Make_Not_In (Loc, | |
7913 | Left_Opnd => | |
7914 | Make_Explicit_Dereference (Loc, | |
7915 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
7916 | Right_Opnd => New_Reference_To (Targ_Typ, Loc))); | |
7917 | ||
7918 | -- Generate: | |
7919 | -- [Constraint_Error when Operand not in Targ_Typ] | |
7920 | ||
7921 | else | |
7922 | Cond := | |
7923 | Make_Not_In (Loc, | |
7924 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
7925 | Right_Opnd => New_Reference_To (Targ_Typ, Loc)); | |
7926 | end if; | |
7927 | ||
7928 | Insert_Action (N, | |
7929 | Make_Raise_Constraint_Error (Loc, | |
7930 | Condition => Cond, | |
7931 | Reason => CE_Tag_Check_Failed)); | |
7932 | end Make_Tag_Check; | |
7933 | ||
7934 | -- Start of processing | |
70482933 RK |
7935 | |
7936 | begin | |
7937 | if Is_Access_Type (Target_Type) then | |
8cea7b64 HK |
7938 | Actual_Op_Typ := Designated_Type (Operand_Type); |
7939 | Actual_Targ_Typ := Designated_Type (Target_Type); | |
70482933 RK |
7940 | |
7941 | else | |
8cea7b64 HK |
7942 | Actual_Op_Typ := Operand_Type; |
7943 | Actual_Targ_Typ := Target_Type; | |
70482933 RK |
7944 | end if; |
7945 | ||
8cea7b64 HK |
7946 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
7947 | ||
20b5d666 JM |
7948 | -- Ada 2005 (AI-251): Handle interface type conversion |
7949 | ||
8cea7b64 | 7950 | if Is_Interface (Actual_Op_Typ) then |
20b5d666 JM |
7951 | Expand_Interface_Conversion (N, Is_Static => False); |
7952 | return; | |
7953 | end if; | |
7954 | ||
8cea7b64 | 7955 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 7956 | |
8cea7b64 HK |
7957 | -- Create a runtime tag check for a downward class-wide type |
7958 | -- conversion. | |
70482933 | 7959 | |
8cea7b64 HK |
7960 | if Is_Class_Wide_Type (Actual_Op_Typ) |
7961 | and then Root_Op_Typ /= Actual_Targ_Typ | |
7962 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ) | |
7963 | then | |
7964 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
7965 | Make_Conversion := True; | |
7966 | end if; | |
70482933 | 7967 | |
8cea7b64 HK |
7968 | -- AI05-0073: If the result subtype of the function is defined |
7969 | -- by an access_definition designating a specific tagged type | |
7970 | -- T, a check is made that the result value is null or the tag | |
7971 | -- of the object designated by the result value identifies T. | |
7972 | -- Constraint_Error is raised if this check fails. | |
70482933 | 7973 | |
8cea7b64 HK |
7974 | if Nkind (Parent (N)) = Sinfo.N_Return_Statement then |
7975 | declare | |
e886436a | 7976 | Func : Entity_Id; |
8cea7b64 HK |
7977 | Func_Typ : Entity_Id; |
7978 | ||
7979 | begin | |
e886436a | 7980 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 7981 | |
e886436a | 7982 | Func := Current_Scope; |
8cea7b64 HK |
7983 | while Present (Func) |
7984 | and then Ekind (Func) /= E_Function | |
7985 | loop | |
7986 | Func := Scope (Func); | |
7987 | end loop; | |
7988 | ||
7989 | -- The function's return subtype must be defined using | |
7990 | -- an access definition. | |
7991 | ||
7992 | if Nkind (Result_Definition (Parent (Func))) = | |
7993 | N_Access_Definition | |
7994 | then | |
7995 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
7996 | ||
7997 | -- The return subtype denotes a specific tagged type, | |
7998 | -- in other words, a non class-wide type. | |
7999 | ||
8000 | if Is_Tagged_Type (Func_Typ) | |
8001 | and then not Is_Class_Wide_Type (Func_Typ) | |
8002 | then | |
8003 | Make_Tag_Check (Actual_Targ_Typ); | |
8004 | Make_Conversion := True; | |
8005 | end if; | |
8006 | end if; | |
8007 | end; | |
70482933 RK |
8008 | end if; |
8009 | ||
8cea7b64 HK |
8010 | -- We have generated a tag check for either a class-wide type |
8011 | -- conversion or for AI05-0073. | |
70482933 | 8012 | |
8cea7b64 HK |
8013 | if Make_Conversion then |
8014 | declare | |
8015 | Conv : Node_Id; | |
8016 | begin | |
8017 | Conv := | |
8018 | Make_Unchecked_Type_Conversion (Loc, | |
8019 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
8020 | Expression => Relocate_Node (Expression (N))); | |
8021 | Rewrite (N, Conv); | |
8022 | Analyze_And_Resolve (N, Target_Type); | |
8023 | end; | |
8024 | end if; | |
70482933 RK |
8025 | end if; |
8026 | end; | |
8027 | ||
8028 | -- Case of other access type conversions | |
8029 | ||
8030 | elsif Is_Access_Type (Target_Type) then | |
8031 | Apply_Constraint_Check (Operand, Target_Type); | |
8032 | ||
8033 | -- Case of conversions from a fixed-point type | |
8034 | ||
685094bf RD |
8035 | -- These conversions require special expansion and processing, found in |
8036 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
8037 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
8038 | -- conversions, which do not need further processing. |
8039 | ||
8040 | elsif Is_Fixed_Point_Type (Operand_Type) | |
8041 | and then not Conversion_OK (N) | |
8042 | then | |
8043 | -- We should never see universal fixed at this case, since the | |
8044 | -- expansion of the constituent divide or multiply should have | |
8045 | -- eliminated the explicit mention of universal fixed. | |
8046 | ||
8047 | pragma Assert (Operand_Type /= Universal_Fixed); | |
8048 | ||
685094bf RD |
8049 | -- Check for special case of the conversion to universal real that |
8050 | -- occurs as a result of the use of a round attribute. In this case, | |
8051 | -- the real type for the conversion is taken from the target type of | |
8052 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
8053 | |
8054 | if Target_Type = Universal_Real | |
8055 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
8056 | and then Attribute_Name (Parent (N)) = Name_Round | |
8057 | then | |
8058 | Set_Rounded_Result (N); | |
8059 | Set_Etype (N, Etype (Parent (N))); | |
8060 | end if; | |
8061 | ||
8062 | -- Otherwise do correct fixed-conversion, but skip these if the | |
8063 | -- Conversion_OK flag is set, because from a semantic point of | |
8064 | -- view these are simple integer conversions needing no further | |
8065 | -- processing (the backend will simply treat them as integers) | |
8066 | ||
8067 | if not Conversion_OK (N) then | |
8068 | if Is_Fixed_Point_Type (Etype (N)) then | |
8069 | Expand_Convert_Fixed_To_Fixed (N); | |
8070 | Real_Range_Check; | |
8071 | ||
8072 | elsif Is_Integer_Type (Etype (N)) then | |
8073 | Expand_Convert_Fixed_To_Integer (N); | |
8074 | ||
8075 | else | |
8076 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
8077 | Expand_Convert_Fixed_To_Float (N); | |
8078 | Real_Range_Check; | |
8079 | end if; | |
8080 | end if; | |
8081 | ||
8082 | -- Case of conversions to a fixed-point type | |
8083 | ||
685094bf RD |
8084 | -- These conversions require special expansion and processing, found in |
8085 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
8086 | -- since from a semantic point of view, these are simple integer | |
8087 | -- conversions, which do not need further processing. | |
70482933 RK |
8088 | |
8089 | elsif Is_Fixed_Point_Type (Target_Type) | |
8090 | and then not Conversion_OK (N) | |
8091 | then | |
8092 | if Is_Integer_Type (Operand_Type) then | |
8093 | Expand_Convert_Integer_To_Fixed (N); | |
8094 | Real_Range_Check; | |
8095 | else | |
8096 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
8097 | Expand_Convert_Float_To_Fixed (N); | |
8098 | Real_Range_Check; | |
8099 | end if; | |
8100 | ||
8101 | -- Case of float-to-integer conversions | |
8102 | ||
8103 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
8104 | -- since semantically the fixed-point target is treated as though it | |
8105 | -- were an integer in such cases. | |
8106 | ||
8107 | elsif Is_Floating_Point_Type (Operand_Type) | |
8108 | and then | |
8109 | (Is_Integer_Type (Target_Type) | |
8110 | or else | |
8111 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
8112 | then | |
70482933 RK |
8113 | -- One more check here, gcc is still not able to do conversions of |
8114 | -- this type with proper overflow checking, and so gigi is doing an | |
8115 | -- approximation of what is required by doing floating-point compares | |
8116 | -- with the end-point. But that can lose precision in some cases, and | |
f02b8bb8 | 8117 | -- give a wrong result. Converting the operand to Universal_Real is |
70482933 | 8118 | -- helpful, but still does not catch all cases with 64-bit integers |
0669bebe GB |
8119 | -- on targets with only 64-bit floats |
8120 | ||
8121 | -- The above comment seems obsoleted by Apply_Float_Conversion_Check | |
8122 | -- Can this code be removed ??? | |
70482933 | 8123 | |
fbf5a39b AC |
8124 | if Do_Range_Check (Operand) then |
8125 | Rewrite (Operand, | |
70482933 RK |
8126 | Make_Type_Conversion (Loc, |
8127 | Subtype_Mark => | |
f02b8bb8 | 8128 | New_Occurrence_Of (Universal_Real, Loc), |
70482933 | 8129 | Expression => |
fbf5a39b | 8130 | Relocate_Node (Operand))); |
70482933 | 8131 | |
f02b8bb8 | 8132 | Set_Etype (Operand, Universal_Real); |
fbf5a39b AC |
8133 | Enable_Range_Check (Operand); |
8134 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
8135 | end if; |
8136 | ||
8137 | -- Case of array conversions | |
8138 | ||
685094bf RD |
8139 | -- Expansion of array conversions, add required length/range checks but |
8140 | -- only do this if there is no change of representation. For handling of | |
8141 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
8142 | |
8143 | elsif Is_Array_Type (Target_Type) then | |
8144 | ||
8145 | if Is_Constrained (Target_Type) then | |
8146 | Apply_Length_Check (Operand, Target_Type); | |
8147 | else | |
8148 | Apply_Range_Check (Operand, Target_Type); | |
8149 | end if; | |
8150 | ||
8151 | Handle_Changed_Representation; | |
8152 | ||
8153 | -- Case of conversions of discriminated types | |
8154 | ||
685094bf RD |
8155 | -- Add required discriminant checks if target is constrained. Again this |
8156 | -- change is skipped if we have a change of representation. | |
70482933 RK |
8157 | |
8158 | elsif Has_Discriminants (Target_Type) | |
8159 | and then Is_Constrained (Target_Type) | |
8160 | then | |
8161 | Apply_Discriminant_Check (Operand, Target_Type); | |
8162 | Handle_Changed_Representation; | |
8163 | ||
8164 | -- Case of all other record conversions. The only processing required | |
8165 | -- is to check for a change of representation requiring the special | |
8166 | -- assignment processing. | |
8167 | ||
8168 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
8169 | |
8170 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
8171 | -- a derived Unchecked_Union type to an unconstrained type that is |
8172 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
8173 | |
8174 | if Is_Derived_Type (Operand_Type) | |
8175 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
8176 | and then not Is_Constrained (Target_Type) | |
8177 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
8178 | and then not Has_Inferable_Discriminants (Operand) | |
8179 | then | |
685094bf | 8180 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e AC |
8181 | -- Program_Error node, but we give it the target type of the |
8182 | -- conversion. | |
8183 | ||
8184 | declare | |
8185 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
8186 | Reason => PE_Unchecked_Union_Restriction); | |
8187 | ||
8188 | begin | |
8189 | Set_Etype (PE, Target_Type); | |
8190 | Rewrite (N, PE); | |
8191 | ||
8192 | end; | |
8193 | else | |
8194 | Handle_Changed_Representation; | |
8195 | end if; | |
70482933 RK |
8196 | |
8197 | -- Case of conversions of enumeration types | |
8198 | ||
8199 | elsif Is_Enumeration_Type (Target_Type) then | |
8200 | ||
8201 | -- Special processing is required if there is a change of | |
8202 | -- representation (from enumeration representation clauses) | |
8203 | ||
8204 | if not Same_Representation (Target_Type, Operand_Type) then | |
8205 | ||
8206 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
8207 | ||
8208 | Rewrite (N, | |
8209 | Make_Attribute_Reference (Loc, | |
8210 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
8211 | Attribute_Name => Name_Val, | |
8212 | Expressions => New_List ( | |
8213 | Make_Attribute_Reference (Loc, | |
8214 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
8215 | Attribute_Name => Name_Pos, | |
8216 | Expressions => New_List (Operand))))); | |
8217 | ||
8218 | Analyze_And_Resolve (N, Target_Type); | |
8219 | end if; | |
8220 | ||
8221 | -- Case of conversions to floating-point | |
8222 | ||
8223 | elsif Is_Floating_Point_Type (Target_Type) then | |
8224 | Real_Range_Check; | |
70482933 RK |
8225 | end if; |
8226 | ||
685094bf RD |
8227 | -- At this stage, either the conversion node has been transformed into |
8228 | -- some other equivalent expression, or left as a conversion that can | |
8229 | -- be handled by Gigi. The conversions that Gigi can handle are the | |
8230 | -- following: | |
70482933 RK |
8231 | |
8232 | -- Conversions with no change of representation or type | |
8233 | ||
685094bf RD |
8234 | -- Numeric conversions involving integer, floating- and fixed-point |
8235 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
8236 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 8237 | |
5e1c00fa RD |
8238 | -- No other conversions should be passed to Gigi |
8239 | ||
8240 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 8241 | |
685094bf RD |
8242 | -- The only remaining step is to generate a range check if we still have |
8243 | -- a type conversion at this stage and Do_Range_Check is set. For now we | |
8244 | -- do this only for conversions of discrete types. | |
fbf5a39b AC |
8245 | |
8246 | if Nkind (N) = N_Type_Conversion | |
8247 | and then Is_Discrete_Type (Etype (N)) | |
8248 | then | |
8249 | declare | |
8250 | Expr : constant Node_Id := Expression (N); | |
8251 | Ftyp : Entity_Id; | |
8252 | Ityp : Entity_Id; | |
8253 | ||
8254 | begin | |
8255 | if Do_Range_Check (Expr) | |
8256 | and then Is_Discrete_Type (Etype (Expr)) | |
8257 | then | |
8258 | Set_Do_Range_Check (Expr, False); | |
8259 | ||
685094bf RD |
8260 | -- Before we do a range check, we have to deal with treating a |
8261 | -- fixed-point operand as an integer. The way we do this is | |
8262 | -- simply to do an unchecked conversion to an appropriate | |
fbf5a39b AC |
8263 | -- integer type large enough to hold the result. |
8264 | ||
8265 | -- This code is not active yet, because we are only dealing | |
8266 | -- with discrete types so far ??? | |
8267 | ||
8268 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer | |
8269 | and then Treat_Fixed_As_Integer (Expr) | |
8270 | then | |
8271 | Ftyp := Base_Type (Etype (Expr)); | |
8272 | ||
8273 | if Esize (Ftyp) >= Esize (Standard_Integer) then | |
8274 | Ityp := Standard_Long_Long_Integer; | |
8275 | else | |
8276 | Ityp := Standard_Integer; | |
8277 | end if; | |
8278 | ||
8279 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); | |
8280 | end if; | |
8281 | ||
8282 | -- Reset overflow flag, since the range check will include | |
685094bf RD |
8283 | -- dealing with possible overflow, and generate the check If |
8284 | -- Address is either a source type or target type, suppress | |
8a36a0cc AC |
8285 | -- range check to avoid typing anomalies when it is a visible |
8286 | -- integer type. | |
fbf5a39b AC |
8287 | |
8288 | Set_Do_Overflow_Check (N, False); | |
8a36a0cc AC |
8289 | if not Is_Descendent_Of_Address (Etype (Expr)) |
8290 | and then not Is_Descendent_Of_Address (Target_Type) | |
8291 | then | |
8292 | Generate_Range_Check | |
8293 | (Expr, Target_Type, CE_Range_Check_Failed); | |
8294 | end if; | |
fbf5a39b AC |
8295 | end if; |
8296 | end; | |
8297 | end if; | |
f02b8bb8 RD |
8298 | |
8299 | -- Final step, if the result is a type conversion involving Vax_Float | |
8300 | -- types, then it is subject for further special processing. | |
8301 | ||
8302 | if Nkind (N) = N_Type_Conversion | |
8303 | and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type)) | |
8304 | then | |
8305 | Expand_Vax_Conversion (N); | |
8306 | return; | |
8307 | end if; | |
70482933 RK |
8308 | end Expand_N_Type_Conversion; |
8309 | ||
8310 | ----------------------------------- | |
8311 | -- Expand_N_Unchecked_Expression -- | |
8312 | ----------------------------------- | |
8313 | ||
8314 | -- Remove the unchecked expression node from the tree. It's job was simply | |
8315 | -- to make sure that its constituent expression was handled with checks | |
8316 | -- off, and now that that is done, we can remove it from the tree, and | |
8317 | -- indeed must, since gigi does not expect to see these nodes. | |
8318 | ||
8319 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
8320 | Exp : constant Node_Id := Expression (N); | |
8321 | ||
8322 | begin | |
8323 | Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp)); | |
8324 | Rewrite (N, Exp); | |
8325 | end Expand_N_Unchecked_Expression; | |
8326 | ||
8327 | ---------------------------------------- | |
8328 | -- Expand_N_Unchecked_Type_Conversion -- | |
8329 | ---------------------------------------- | |
8330 | ||
685094bf RD |
8331 | -- If this cannot be handled by Gigi and we haven't already made a |
8332 | -- temporary for it, do it now. | |
70482933 RK |
8333 | |
8334 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
8335 | Target_Type : constant Entity_Id := Etype (N); | |
8336 | Operand : constant Node_Id := Expression (N); | |
8337 | Operand_Type : constant Entity_Id := Etype (Operand); | |
8338 | ||
8339 | begin | |
8340 | -- If we have a conversion of a compile time known value to a target | |
8341 | -- type and the value is in range of the target type, then we can simply | |
8342 | -- replace the construct by an integer literal of the correct type. We | |
8343 | -- only apply this to integer types being converted. Possibly it may | |
8344 | -- apply in other cases, but it is too much trouble to worry about. | |
8345 | ||
8346 | -- Note that we do not do this transformation if the Kill_Range_Check | |
8347 | -- flag is set, since then the value may be outside the expected range. | |
8348 | -- This happens in the Normalize_Scalars case. | |
8349 | ||
20b5d666 JM |
8350 | -- We also skip this if either the target or operand type is biased |
8351 | -- because in this case, the unchecked conversion is supposed to | |
8352 | -- preserve the bit pattern, not the integer value. | |
8353 | ||
70482933 | 8354 | if Is_Integer_Type (Target_Type) |
20b5d666 | 8355 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 8356 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 8357 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
8358 | and then Compile_Time_Known_Value (Operand) |
8359 | and then not Kill_Range_Check (N) | |
8360 | then | |
8361 | declare | |
8362 | Val : constant Uint := Expr_Value (Operand); | |
8363 | ||
8364 | begin | |
8365 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
8366 | and then | |
8367 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
8368 | and then | |
8369 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
8370 | and then | |
8371 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
8372 | then | |
8373 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 8374 | |
685094bf RD |
8375 | -- If Address is the target type, just set the type to avoid a |
8376 | -- spurious type error on the literal when Address is a visible | |
8377 | -- integer type. | |
8a36a0cc AC |
8378 | |
8379 | if Is_Descendent_Of_Address (Target_Type) then | |
8380 | Set_Etype (N, Target_Type); | |
8381 | else | |
8382 | Analyze_And_Resolve (N, Target_Type); | |
8383 | end if; | |
8384 | ||
70482933 RK |
8385 | return; |
8386 | end if; | |
8387 | end; | |
8388 | end if; | |
8389 | ||
8390 | -- Nothing to do if conversion is safe | |
8391 | ||
8392 | if Safe_Unchecked_Type_Conversion (N) then | |
8393 | return; | |
8394 | end if; | |
8395 | ||
8396 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
8397 | -- flag indicates ??? -- more comments needed here) | |
8398 | ||
8399 | if Assignment_OK (N) then | |
8400 | null; | |
8401 | else | |
8402 | Force_Evaluation (N); | |
8403 | end if; | |
8404 | end Expand_N_Unchecked_Type_Conversion; | |
8405 | ||
8406 | ---------------------------- | |
8407 | -- Expand_Record_Equality -- | |
8408 | ---------------------------- | |
8409 | ||
8410 | -- For non-variant records, Equality is expanded when needed into: | |
8411 | ||
8412 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
8413 | -- and then ... | |
8414 | -- and then Lhs.Discrn = Rhs.Discrn | |
8415 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
8416 | -- and then ... | |
8417 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
8418 | ||
8419 | -- The expression is folded by the back-end for adjacent fields. This | |
8420 | -- function is called for tagged record in only one occasion: for imple- | |
8421 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
8422 | -- otherwise the primitive "=" is used directly. | |
8423 | ||
8424 | function Expand_Record_Equality | |
8425 | (Nod : Node_Id; | |
8426 | Typ : Entity_Id; | |
8427 | Lhs : Node_Id; | |
8428 | Rhs : Node_Id; | |
2e071734 | 8429 | Bodies : List_Id) return Node_Id |
70482933 RK |
8430 | is |
8431 | Loc : constant Source_Ptr := Sloc (Nod); | |
8432 | ||
0ab80019 AC |
8433 | Result : Node_Id; |
8434 | C : Entity_Id; | |
8435 | ||
8436 | First_Time : Boolean := True; | |
8437 | ||
70482933 RK |
8438 | function Suitable_Element (C : Entity_Id) return Entity_Id; |
8439 | -- Return the first field to compare beginning with C, skipping the | |
0ab80019 AC |
8440 | -- inherited components. |
8441 | ||
8442 | ---------------------- | |
8443 | -- Suitable_Element -- | |
8444 | ---------------------- | |
70482933 RK |
8445 | |
8446 | function Suitable_Element (C : Entity_Id) return Entity_Id is | |
8447 | begin | |
8448 | if No (C) then | |
8449 | return Empty; | |
8450 | ||
8451 | elsif Ekind (C) /= E_Discriminant | |
8452 | and then Ekind (C) /= E_Component | |
8453 | then | |
8454 | return Suitable_Element (Next_Entity (C)); | |
8455 | ||
8456 | elsif Is_Tagged_Type (Typ) | |
8457 | and then C /= Original_Record_Component (C) | |
8458 | then | |
8459 | return Suitable_Element (Next_Entity (C)); | |
8460 | ||
8461 | elsif Chars (C) = Name_uController | |
8462 | or else Chars (C) = Name_uTag | |
8463 | then | |
8464 | return Suitable_Element (Next_Entity (C)); | |
8465 | ||
26bff3d9 JM |
8466 | elsif Is_Interface (Etype (C)) then |
8467 | return Suitable_Element (Next_Entity (C)); | |
8468 | ||
70482933 RK |
8469 | else |
8470 | return C; | |
8471 | end if; | |
8472 | end Suitable_Element; | |
8473 | ||
70482933 RK |
8474 | -- Start of processing for Expand_Record_Equality |
8475 | ||
8476 | begin | |
70482933 RK |
8477 | -- Generates the following code: (assuming that Typ has one Discr and |
8478 | -- component C2 is also a record) | |
8479 | ||
8480 | -- True | |
8481 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
8482 | -- and then Lhs.C1 = Rhs.C1 | |
8483 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
8484 | -- and then ... | |
8485 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
8486 | ||
8487 | Result := New_Reference_To (Standard_True, Loc); | |
8488 | C := Suitable_Element (First_Entity (Typ)); | |
8489 | ||
8490 | while Present (C) loop | |
70482933 RK |
8491 | declare |
8492 | New_Lhs : Node_Id; | |
8493 | New_Rhs : Node_Id; | |
8aceda64 | 8494 | Check : Node_Id; |
70482933 RK |
8495 | |
8496 | begin | |
8497 | if First_Time then | |
8498 | First_Time := False; | |
8499 | New_Lhs := Lhs; | |
8500 | New_Rhs := Rhs; | |
70482933 RK |
8501 | else |
8502 | New_Lhs := New_Copy_Tree (Lhs); | |
8503 | New_Rhs := New_Copy_Tree (Rhs); | |
8504 | end if; | |
8505 | ||
8aceda64 AC |
8506 | Check := |
8507 | Expand_Composite_Equality (Nod, Etype (C), | |
8508 | Lhs => | |
8509 | Make_Selected_Component (Loc, | |
8510 | Prefix => New_Lhs, | |
8511 | Selector_Name => New_Reference_To (C, Loc)), | |
8512 | Rhs => | |
8513 | Make_Selected_Component (Loc, | |
8514 | Prefix => New_Rhs, | |
8515 | Selector_Name => New_Reference_To (C, Loc)), | |
8516 | Bodies => Bodies); | |
8517 | ||
8518 | -- If some (sub)component is an unchecked_union, the whole | |
8519 | -- operation will raise program error. | |
8520 | ||
8521 | if Nkind (Check) = N_Raise_Program_Error then | |
8522 | Result := Check; | |
8523 | Set_Etype (Result, Standard_Boolean); | |
8524 | exit; | |
8525 | else | |
8526 | Result := | |
8527 | Make_And_Then (Loc, | |
8528 | Left_Opnd => Result, | |
8529 | Right_Opnd => Check); | |
8530 | end if; | |
70482933 RK |
8531 | end; |
8532 | ||
8533 | C := Suitable_Element (Next_Entity (C)); | |
8534 | end loop; | |
8535 | ||
8536 | return Result; | |
8537 | end Expand_Record_Equality; | |
8538 | ||
8539 | ------------------------------------- | |
8540 | -- Fixup_Universal_Fixed_Operation -- | |
8541 | ------------------------------------- | |
8542 | ||
8543 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
8544 | Conv : constant Node_Id := Parent (N); | |
8545 | ||
8546 | begin | |
8547 | -- We must have a type conversion immediately above us | |
8548 | ||
8549 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
8550 | ||
8551 | -- Normally the type conversion gives our target type. The exception | |
8552 | -- occurs in the case of the Round attribute, where the conversion | |
8553 | -- will be to universal real, and our real type comes from the Round | |
8554 | -- attribute (as well as an indication that we must round the result) | |
8555 | ||
8556 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
8557 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
8558 | then | |
8559 | Set_Etype (N, Etype (Parent (Conv))); | |
8560 | Set_Rounded_Result (N); | |
8561 | ||
8562 | -- Normal case where type comes from conversion above us | |
8563 | ||
8564 | else | |
8565 | Set_Etype (N, Etype (Conv)); | |
8566 | end if; | |
8567 | end Fixup_Universal_Fixed_Operation; | |
8568 | ||
fbf5a39b AC |
8569 | ------------------------------ |
8570 | -- Get_Allocator_Final_List -- | |
8571 | ------------------------------ | |
8572 | ||
8573 | function Get_Allocator_Final_List | |
8574 | (N : Node_Id; | |
8575 | T : Entity_Id; | |
2e071734 | 8576 | PtrT : Entity_Id) return Entity_Id |
fbf5a39b AC |
8577 | is |
8578 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 8579 | |
0da2c8ac | 8580 | Owner : Entity_Id := PtrT; |
26bff3d9 | 8581 | -- The entity whose finalization list must be used to attach the |
0da2c8ac | 8582 | -- allocated object. |
fbf5a39b | 8583 | |
0da2c8ac | 8584 | begin |
fbf5a39b | 8585 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
26bff3d9 JM |
8586 | |
8587 | -- If the context is an access parameter, we need to create a | |
8588 | -- non-anonymous access type in order to have a usable final list, | |
8589 | -- because there is otherwise no pool to which the allocated object | |
8590 | -- can belong. We create both the type and the finalization chain | |
8591 | -- here, because freezing an internal type does not create such a | |
8592 | -- chain. The Final_Chain that is thus created is shared by the | |
8593 | -- access parameter. The access type is tested against the result | |
8594 | -- type of the function to exclude allocators whose type is an | |
8654a240 | 8595 | -- anonymous access result type. We freeze the type at once to |
9450205a ES |
8596 | -- ensure that it is properly decorated for the back-end, even |
8597 | -- if the context and current scope is a loop. | |
26bff3d9 | 8598 | |
0da2c8ac AC |
8599 | if Nkind (Associated_Node_For_Itype (PtrT)) |
8600 | in N_Subprogram_Specification | |
26bff3d9 JM |
8601 | and then |
8602 | PtrT /= | |
8603 | Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT))) | |
0da2c8ac | 8604 | then |
0da2c8ac AC |
8605 | Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J')); |
8606 | Insert_Action (N, | |
8607 | Make_Full_Type_Declaration (Loc, | |
8608 | Defining_Identifier => Owner, | |
8609 | Type_Definition => | |
8610 | Make_Access_To_Object_Definition (Loc, | |
8611 | Subtype_Indication => | |
8612 | New_Occurrence_Of (T, Loc)))); | |
fbf5a39b | 8613 | |
9450205a | 8614 | Freeze_Before (N, Owner); |
0da2c8ac AC |
8615 | Build_Final_List (N, Owner); |
8616 | Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner)); | |
fbf5a39b | 8617 | |
26bff3d9 JM |
8618 | -- Ada 2005 (AI-318-02): If the context is a return object |
8619 | -- declaration, then the anonymous return subtype is defined to have | |
8620 | -- the same accessibility level as that of the function's result | |
8621 | -- subtype, which means that we want the scope where the function is | |
8622 | -- declared. | |
8623 | ||
8624 | elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration | |
8625 | and then Ekind (Scope (PtrT)) = E_Return_Statement | |
8626 | then | |
8627 | Owner := Scope (Return_Applies_To (Scope (PtrT))); | |
8628 | ||
8629 | -- Case of an access discriminant, or (Ada 2005), of an anonymous | |
8630 | -- access component or anonymous access function result: find the | |
d766cee3 RD |
8631 | -- final list associated with the scope of the type. (In the |
8632 | -- anonymous access component kind, a list controller will have | |
8633 | -- been allocated when freezing the record type, and PtrT has an | |
8634 | -- Associated_Final_Chain attribute designating it.) | |
0da2c8ac | 8635 | |
d766cee3 | 8636 | elsif No (Associated_Final_Chain (PtrT)) then |
0da2c8ac AC |
8637 | Owner := Scope (PtrT); |
8638 | end if; | |
fbf5a39b | 8639 | end if; |
0da2c8ac AC |
8640 | |
8641 | return Find_Final_List (Owner); | |
fbf5a39b AC |
8642 | end Get_Allocator_Final_List; |
8643 | ||
5d09245e AC |
8644 | --------------------------------- |
8645 | -- Has_Inferable_Discriminants -- | |
8646 | --------------------------------- | |
8647 | ||
8648 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
8649 | ||
8650 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
8651 | -- Determines whether the left-most prefix of a selected component is a | |
8652 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
8653 | ||
8654 | -------------------------------- | |
8655 | -- Prefix_Is_Formal_Parameter -- | |
8656 | -------------------------------- | |
8657 | ||
8658 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
8659 | Sel_Comp : Node_Id := N; | |
8660 | ||
8661 | begin | |
8662 | -- Move to the left-most prefix by climbing up the tree | |
8663 | ||
8664 | while Present (Parent (Sel_Comp)) | |
8665 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
8666 | loop | |
8667 | Sel_Comp := Parent (Sel_Comp); | |
8668 | end loop; | |
8669 | ||
8670 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
8671 | end Prefix_Is_Formal_Parameter; | |
8672 | ||
8673 | -- Start of processing for Has_Inferable_Discriminants | |
8674 | ||
8675 | begin | |
8fc789c8 | 8676 | -- For identifiers and indexed components, it is sufficient to have a |
5d09245e AC |
8677 | -- constrained Unchecked_Union nominal subtype. |
8678 | ||
303b4d58 | 8679 | if Nkind_In (N, N_Identifier, N_Indexed_Component) then |
5d09245e AC |
8680 | return Is_Unchecked_Union (Base_Type (Etype (N))) |
8681 | and then | |
8682 | Is_Constrained (Etype (N)); | |
8683 | ||
8684 | -- For selected components, the subtype of the selector must be a | |
8685 | -- constrained Unchecked_Union. If the component is subject to a | |
8686 | -- per-object constraint, then the enclosing object must have inferable | |
8687 | -- discriminants. | |
8688 | ||
8689 | elsif Nkind (N) = N_Selected_Component then | |
8690 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then | |
8691 | ||
8692 | -- A small hack. If we have a per-object constrained selected | |
8693 | -- component of a formal parameter, return True since we do not | |
8694 | -- know the actual parameter association yet. | |
8695 | ||
8696 | if Prefix_Is_Formal_Parameter (N) then | |
8697 | return True; | |
8698 | end if; | |
8699 | ||
8700 | -- Otherwise, check the enclosing object and the selector | |
8701 | ||
8702 | return Has_Inferable_Discriminants (Prefix (N)) | |
8703 | and then | |
8704 | Has_Inferable_Discriminants (Selector_Name (N)); | |
8705 | end if; | |
8706 | ||
8707 | -- The call to Has_Inferable_Discriminants will determine whether | |
8708 | -- the selector has a constrained Unchecked_Union nominal type. | |
8709 | ||
8710 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
8711 | ||
8712 | -- A qualified expression has inferable discriminants if its subtype | |
8713 | -- mark is a constrained Unchecked_Union subtype. | |
8714 | ||
8715 | elsif Nkind (N) = N_Qualified_Expression then | |
8716 | return Is_Unchecked_Union (Subtype_Mark (N)) | |
8717 | and then | |
8718 | Is_Constrained (Subtype_Mark (N)); | |
8719 | ||
8720 | end if; | |
8721 | ||
8722 | return False; | |
8723 | end Has_Inferable_Discriminants; | |
8724 | ||
70482933 RK |
8725 | ------------------------------- |
8726 | -- Insert_Dereference_Action -- | |
8727 | ------------------------------- | |
8728 | ||
8729 | procedure Insert_Dereference_Action (N : Node_Id) is | |
8730 | Loc : constant Source_Ptr := Sloc (N); | |
8731 | Typ : constant Entity_Id := Etype (N); | |
8732 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
0ab80019 | 8733 | Pnod : constant Node_Id := Parent (N); |
70482933 RK |
8734 | |
8735 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; | |
2e071734 AC |
8736 | -- Return true if type of P is derived from Checked_Pool; |
8737 | ||
8738 | ----------------------------- | |
8739 | -- Is_Checked_Storage_Pool -- | |
8740 | ----------------------------- | |
70482933 RK |
8741 | |
8742 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
8743 | T : Entity_Id; | |
8744 | ||
8745 | begin | |
8746 | if No (P) then | |
8747 | return False; | |
8748 | end if; | |
8749 | ||
8750 | T := Etype (P); | |
8751 | while T /= Etype (T) loop | |
8752 | if Is_RTE (T, RE_Checked_Pool) then | |
8753 | return True; | |
8754 | else | |
8755 | T := Etype (T); | |
8756 | end if; | |
8757 | end loop; | |
8758 | ||
8759 | return False; | |
8760 | end Is_Checked_Storage_Pool; | |
8761 | ||
8762 | -- Start of processing for Insert_Dereference_Action | |
8763 | ||
8764 | begin | |
e6f69614 AC |
8765 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
8766 | ||
0ab80019 AC |
8767 | if not (Is_Checked_Storage_Pool (Pool) |
8768 | and then Comes_From_Source (Original_Node (Pnod))) | |
e6f69614 | 8769 | then |
70482933 | 8770 | return; |
70482933 RK |
8771 | end if; |
8772 | ||
8773 | Insert_Action (N, | |
8774 | Make_Procedure_Call_Statement (Loc, | |
8775 | Name => New_Reference_To ( | |
8776 | Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), | |
8777 | ||
8778 | Parameter_Associations => New_List ( | |
8779 | ||
8780 | -- Pool | |
8781 | ||
8782 | New_Reference_To (Pool, Loc), | |
8783 | ||
685094bf RD |
8784 | -- Storage_Address. We use the attribute Pool_Address, which uses |
8785 | -- the pointer itself to find the address of the object, and which | |
8786 | -- handles unconstrained arrays properly by computing the address | |
8787 | -- of the template. i.e. the correct address of the corresponding | |
8788 | -- allocation. | |
70482933 RK |
8789 | |
8790 | Make_Attribute_Reference (Loc, | |
fbf5a39b AC |
8791 | Prefix => Duplicate_Subexpr_Move_Checks (N), |
8792 | Attribute_Name => Name_Pool_Address), | |
70482933 RK |
8793 | |
8794 | -- Size_In_Storage_Elements | |
8795 | ||
8796 | Make_Op_Divide (Loc, | |
8797 | Left_Opnd => | |
8798 | Make_Attribute_Reference (Loc, | |
8799 | Prefix => | |
fbf5a39b AC |
8800 | Make_Explicit_Dereference (Loc, |
8801 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
8802 | Attribute_Name => Name_Size), |
8803 | Right_Opnd => | |
8804 | Make_Integer_Literal (Loc, System_Storage_Unit)), | |
8805 | ||
8806 | -- Alignment | |
8807 | ||
8808 | Make_Attribute_Reference (Loc, | |
8809 | Prefix => | |
fbf5a39b AC |
8810 | Make_Explicit_Dereference (Loc, |
8811 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
8812 | Attribute_Name => Name_Alignment)))); |
8813 | ||
fbf5a39b AC |
8814 | exception |
8815 | when RE_Not_Available => | |
8816 | return; | |
70482933 RK |
8817 | end Insert_Dereference_Action; |
8818 | ||
8819 | ------------------------------ | |
8820 | -- Make_Array_Comparison_Op -- | |
8821 | ------------------------------ | |
8822 | ||
8823 | -- This is a hand-coded expansion of the following generic function: | |
8824 | ||
8825 | -- generic | |
8826 | -- type elem is (<>); | |
8827 | -- type index is (<>); | |
8828 | -- type a is array (index range <>) of elem; | |
20b5d666 | 8829 | |
70482933 RK |
8830 | -- function Gnnn (X : a; Y: a) return boolean is |
8831 | -- J : index := Y'first; | |
20b5d666 | 8832 | |
70482933 RK |
8833 | -- begin |
8834 | -- if X'length = 0 then | |
8835 | -- return false; | |
20b5d666 | 8836 | |
70482933 RK |
8837 | -- elsif Y'length = 0 then |
8838 | -- return true; | |
20b5d666 | 8839 | |
70482933 RK |
8840 | -- else |
8841 | -- for I in X'range loop | |
8842 | -- if X (I) = Y (J) then | |
8843 | -- if J = Y'last then | |
8844 | -- exit; | |
8845 | -- else | |
8846 | -- J := index'succ (J); | |
8847 | -- end if; | |
20b5d666 | 8848 | |
70482933 RK |
8849 | -- else |
8850 | -- return X (I) > Y (J); | |
8851 | -- end if; | |
8852 | -- end loop; | |
20b5d666 | 8853 | |
70482933 RK |
8854 | -- return X'length > Y'length; |
8855 | -- end if; | |
8856 | -- end Gnnn; | |
8857 | ||
8858 | -- Note that since we are essentially doing this expansion by hand, we | |
8859 | -- do not need to generate an actual or formal generic part, just the | |
8860 | -- instantiated function itself. | |
8861 | ||
8862 | function Make_Array_Comparison_Op | |
2e071734 AC |
8863 | (Typ : Entity_Id; |
8864 | Nod : Node_Id) return Node_Id | |
70482933 RK |
8865 | is |
8866 | Loc : constant Source_Ptr := Sloc (Nod); | |
8867 | ||
8868 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
8869 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
8870 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
8871 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
8872 | ||
8873 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
8874 | ||
8875 | Loop_Statement : Node_Id; | |
8876 | Loop_Body : Node_Id; | |
8877 | If_Stat : Node_Id; | |
8878 | Inner_If : Node_Id; | |
8879 | Final_Expr : Node_Id; | |
8880 | Func_Body : Node_Id; | |
8881 | Func_Name : Entity_Id; | |
8882 | Formals : List_Id; | |
8883 | Length1 : Node_Id; | |
8884 | Length2 : Node_Id; | |
8885 | ||
8886 | begin | |
8887 | -- if J = Y'last then | |
8888 | -- exit; | |
8889 | -- else | |
8890 | -- J := index'succ (J); | |
8891 | -- end if; | |
8892 | ||
8893 | Inner_If := | |
8894 | Make_Implicit_If_Statement (Nod, | |
8895 | Condition => | |
8896 | Make_Op_Eq (Loc, | |
8897 | Left_Opnd => New_Reference_To (J, Loc), | |
8898 | Right_Opnd => | |
8899 | Make_Attribute_Reference (Loc, | |
8900 | Prefix => New_Reference_To (Y, Loc), | |
8901 | Attribute_Name => Name_Last)), | |
8902 | ||
8903 | Then_Statements => New_List ( | |
8904 | Make_Exit_Statement (Loc)), | |
8905 | ||
8906 | Else_Statements => | |
8907 | New_List ( | |
8908 | Make_Assignment_Statement (Loc, | |
8909 | Name => New_Reference_To (J, Loc), | |
8910 | Expression => | |
8911 | Make_Attribute_Reference (Loc, | |
8912 | Prefix => New_Reference_To (Index, Loc), | |
8913 | Attribute_Name => Name_Succ, | |
8914 | Expressions => New_List (New_Reference_To (J, Loc)))))); | |
8915 | ||
8916 | -- if X (I) = Y (J) then | |
8917 | -- if ... end if; | |
8918 | -- else | |
8919 | -- return X (I) > Y (J); | |
8920 | -- end if; | |
8921 | ||
8922 | Loop_Body := | |
8923 | Make_Implicit_If_Statement (Nod, | |
8924 | Condition => | |
8925 | Make_Op_Eq (Loc, | |
8926 | Left_Opnd => | |
8927 | Make_Indexed_Component (Loc, | |
8928 | Prefix => New_Reference_To (X, Loc), | |
8929 | Expressions => New_List (New_Reference_To (I, Loc))), | |
8930 | ||
8931 | Right_Opnd => | |
8932 | Make_Indexed_Component (Loc, | |
8933 | Prefix => New_Reference_To (Y, Loc), | |
8934 | Expressions => New_List (New_Reference_To (J, Loc)))), | |
8935 | ||
8936 | Then_Statements => New_List (Inner_If), | |
8937 | ||
8938 | Else_Statements => New_List ( | |
d766cee3 | 8939 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
8940 | Expression => |
8941 | Make_Op_Gt (Loc, | |
8942 | Left_Opnd => | |
8943 | Make_Indexed_Component (Loc, | |
8944 | Prefix => New_Reference_To (X, Loc), | |
8945 | Expressions => New_List (New_Reference_To (I, Loc))), | |
8946 | ||
8947 | Right_Opnd => | |
8948 | Make_Indexed_Component (Loc, | |
8949 | Prefix => New_Reference_To (Y, Loc), | |
8950 | Expressions => New_List ( | |
8951 | New_Reference_To (J, Loc))))))); | |
8952 | ||
8953 | -- for I in X'range loop | |
8954 | -- if ... end if; | |
8955 | -- end loop; | |
8956 | ||
8957 | Loop_Statement := | |
8958 | Make_Implicit_Loop_Statement (Nod, | |
8959 | Identifier => Empty, | |
8960 | ||
8961 | Iteration_Scheme => | |
8962 | Make_Iteration_Scheme (Loc, | |
8963 | Loop_Parameter_Specification => | |
8964 | Make_Loop_Parameter_Specification (Loc, | |
8965 | Defining_Identifier => I, | |
8966 | Discrete_Subtype_Definition => | |
8967 | Make_Attribute_Reference (Loc, | |
8968 | Prefix => New_Reference_To (X, Loc), | |
8969 | Attribute_Name => Name_Range))), | |
8970 | ||
8971 | Statements => New_List (Loop_Body)); | |
8972 | ||
8973 | -- if X'length = 0 then | |
8974 | -- return false; | |
8975 | -- elsif Y'length = 0 then | |
8976 | -- return true; | |
8977 | -- else | |
8978 | -- for ... loop ... end loop; | |
8979 | -- return X'length > Y'length; | |
8980 | -- end if; | |
8981 | ||
8982 | Length1 := | |
8983 | Make_Attribute_Reference (Loc, | |
8984 | Prefix => New_Reference_To (X, Loc), | |
8985 | Attribute_Name => Name_Length); | |
8986 | ||
8987 | Length2 := | |
8988 | Make_Attribute_Reference (Loc, | |
8989 | Prefix => New_Reference_To (Y, Loc), | |
8990 | Attribute_Name => Name_Length); | |
8991 | ||
8992 | Final_Expr := | |
8993 | Make_Op_Gt (Loc, | |
8994 | Left_Opnd => Length1, | |
8995 | Right_Opnd => Length2); | |
8996 | ||
8997 | If_Stat := | |
8998 | Make_Implicit_If_Statement (Nod, | |
8999 | Condition => | |
9000 | Make_Op_Eq (Loc, | |
9001 | Left_Opnd => | |
9002 | Make_Attribute_Reference (Loc, | |
9003 | Prefix => New_Reference_To (X, Loc), | |
9004 | Attribute_Name => Name_Length), | |
9005 | Right_Opnd => | |
9006 | Make_Integer_Literal (Loc, 0)), | |
9007 | ||
9008 | Then_Statements => | |
9009 | New_List ( | |
d766cee3 | 9010 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9011 | Expression => New_Reference_To (Standard_False, Loc))), |
9012 | ||
9013 | Elsif_Parts => New_List ( | |
9014 | Make_Elsif_Part (Loc, | |
9015 | Condition => | |
9016 | Make_Op_Eq (Loc, | |
9017 | Left_Opnd => | |
9018 | Make_Attribute_Reference (Loc, | |
9019 | Prefix => New_Reference_To (Y, Loc), | |
9020 | Attribute_Name => Name_Length), | |
9021 | Right_Opnd => | |
9022 | Make_Integer_Literal (Loc, 0)), | |
9023 | ||
9024 | Then_Statements => | |
9025 | New_List ( | |
d766cee3 | 9026 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9027 | Expression => New_Reference_To (Standard_True, Loc))))), |
9028 | ||
9029 | Else_Statements => New_List ( | |
9030 | Loop_Statement, | |
d766cee3 | 9031 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9032 | Expression => Final_Expr))); |
9033 | ||
9034 | -- (X : a; Y: a) | |
9035 | ||
9036 | Formals := New_List ( | |
9037 | Make_Parameter_Specification (Loc, | |
9038 | Defining_Identifier => X, | |
9039 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
9040 | ||
9041 | Make_Parameter_Specification (Loc, | |
9042 | Defining_Identifier => Y, | |
9043 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
9044 | ||
9045 | -- function Gnnn (...) return boolean is | |
9046 | -- J : index := Y'first; | |
9047 | -- begin | |
9048 | -- if ... end if; | |
9049 | -- end Gnnn; | |
9050 | ||
9051 | Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G')); | |
9052 | ||
9053 | Func_Body := | |
9054 | Make_Subprogram_Body (Loc, | |
9055 | Specification => | |
9056 | Make_Function_Specification (Loc, | |
9057 | Defining_Unit_Name => Func_Name, | |
9058 | Parameter_Specifications => Formals, | |
630d30e9 | 9059 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
70482933 RK |
9060 | |
9061 | Declarations => New_List ( | |
9062 | Make_Object_Declaration (Loc, | |
9063 | Defining_Identifier => J, | |
9064 | Object_Definition => New_Reference_To (Index, Loc), | |
9065 | Expression => | |
9066 | Make_Attribute_Reference (Loc, | |
9067 | Prefix => New_Reference_To (Y, Loc), | |
9068 | Attribute_Name => Name_First))), | |
9069 | ||
9070 | Handled_Statement_Sequence => | |
9071 | Make_Handled_Sequence_Of_Statements (Loc, | |
9072 | Statements => New_List (If_Stat))); | |
9073 | ||
9074 | return Func_Body; | |
70482933 RK |
9075 | end Make_Array_Comparison_Op; |
9076 | ||
9077 | --------------------------- | |
9078 | -- Make_Boolean_Array_Op -- | |
9079 | --------------------------- | |
9080 | ||
685094bf RD |
9081 | -- For logical operations on boolean arrays, expand in line the following, |
9082 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
9083 | |
9084 | -- function Annn (A : typ; B: typ) return typ is | |
9085 | -- C : typ; | |
9086 | -- begin | |
9087 | -- for J in A'range loop | |
9088 | -- C (J) := A (J) op B (J); | |
9089 | -- end loop; | |
9090 | -- return C; | |
9091 | -- end Annn; | |
9092 | ||
9093 | -- Here typ is the boolean array type | |
9094 | ||
9095 | function Make_Boolean_Array_Op | |
2e071734 AC |
9096 | (Typ : Entity_Id; |
9097 | N : Node_Id) return Node_Id | |
70482933 RK |
9098 | is |
9099 | Loc : constant Source_Ptr := Sloc (N); | |
9100 | ||
9101 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
9102 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
9103 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
9104 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
9105 | ||
9106 | A_J : Node_Id; | |
9107 | B_J : Node_Id; | |
9108 | C_J : Node_Id; | |
9109 | Op : Node_Id; | |
9110 | ||
9111 | Formals : List_Id; | |
9112 | Func_Name : Entity_Id; | |
9113 | Func_Body : Node_Id; | |
9114 | Loop_Statement : Node_Id; | |
9115 | ||
9116 | begin | |
9117 | A_J := | |
9118 | Make_Indexed_Component (Loc, | |
9119 | Prefix => New_Reference_To (A, Loc), | |
9120 | Expressions => New_List (New_Reference_To (J, Loc))); | |
9121 | ||
9122 | B_J := | |
9123 | Make_Indexed_Component (Loc, | |
9124 | Prefix => New_Reference_To (B, Loc), | |
9125 | Expressions => New_List (New_Reference_To (J, Loc))); | |
9126 | ||
9127 | C_J := | |
9128 | Make_Indexed_Component (Loc, | |
9129 | Prefix => New_Reference_To (C, Loc), | |
9130 | Expressions => New_List (New_Reference_To (J, Loc))); | |
9131 | ||
9132 | if Nkind (N) = N_Op_And then | |
9133 | Op := | |
9134 | Make_Op_And (Loc, | |
9135 | Left_Opnd => A_J, | |
9136 | Right_Opnd => B_J); | |
9137 | ||
9138 | elsif Nkind (N) = N_Op_Or then | |
9139 | Op := | |
9140 | Make_Op_Or (Loc, | |
9141 | Left_Opnd => A_J, | |
9142 | Right_Opnd => B_J); | |
9143 | ||
9144 | else | |
9145 | Op := | |
9146 | Make_Op_Xor (Loc, | |
9147 | Left_Opnd => A_J, | |
9148 | Right_Opnd => B_J); | |
9149 | end if; | |
9150 | ||
9151 | Loop_Statement := | |
9152 | Make_Implicit_Loop_Statement (N, | |
9153 | Identifier => Empty, | |
9154 | ||
9155 | Iteration_Scheme => | |
9156 | Make_Iteration_Scheme (Loc, | |
9157 | Loop_Parameter_Specification => | |
9158 | Make_Loop_Parameter_Specification (Loc, | |
9159 | Defining_Identifier => J, | |
9160 | Discrete_Subtype_Definition => | |
9161 | Make_Attribute_Reference (Loc, | |
9162 | Prefix => New_Reference_To (A, Loc), | |
9163 | Attribute_Name => Name_Range))), | |
9164 | ||
9165 | Statements => New_List ( | |
9166 | Make_Assignment_Statement (Loc, | |
9167 | Name => C_J, | |
9168 | Expression => Op))); | |
9169 | ||
9170 | Formals := New_List ( | |
9171 | Make_Parameter_Specification (Loc, | |
9172 | Defining_Identifier => A, | |
9173 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
9174 | ||
9175 | Make_Parameter_Specification (Loc, | |
9176 | Defining_Identifier => B, | |
9177 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
9178 | ||
9179 | Func_Name := | |
9180 | Make_Defining_Identifier (Loc, New_Internal_Name ('A')); | |
9181 | Set_Is_Inlined (Func_Name); | |
9182 | ||
9183 | Func_Body := | |
9184 | Make_Subprogram_Body (Loc, | |
9185 | Specification => | |
9186 | Make_Function_Specification (Loc, | |
9187 | Defining_Unit_Name => Func_Name, | |
9188 | Parameter_Specifications => Formals, | |
630d30e9 | 9189 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
9190 | |
9191 | Declarations => New_List ( | |
9192 | Make_Object_Declaration (Loc, | |
9193 | Defining_Identifier => C, | |
9194 | Object_Definition => New_Reference_To (Typ, Loc))), | |
9195 | ||
9196 | Handled_Statement_Sequence => | |
9197 | Make_Handled_Sequence_Of_Statements (Loc, | |
9198 | Statements => New_List ( | |
9199 | Loop_Statement, | |
d766cee3 | 9200 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9201 | Expression => New_Reference_To (C, Loc))))); |
9202 | ||
9203 | return Func_Body; | |
9204 | end Make_Boolean_Array_Op; | |
9205 | ||
9206 | ------------------------ | |
9207 | -- Rewrite_Comparison -- | |
9208 | ------------------------ | |
9209 | ||
9210 | procedure Rewrite_Comparison (N : Node_Id) is | |
c800f862 RD |
9211 | Warning_Generated : Boolean := False; |
9212 | -- Set to True if first pass with Assume_Valid generates a warning in | |
9213 | -- which case we skip the second pass to avoid warning overloaded. | |
9214 | ||
9215 | Result : Node_Id; | |
9216 | -- Set to Standard_True or Standard_False | |
9217 | ||
d26dc4b5 AC |
9218 | begin |
9219 | if Nkind (N) = N_Type_Conversion then | |
9220 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 9221 | return; |
70482933 | 9222 | |
d26dc4b5 | 9223 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
9224 | return; |
9225 | end if; | |
70482933 | 9226 | |
c800f862 RD |
9227 | -- Now start looking at the comparison in detail. We potentially go |
9228 | -- through this loop twice. The first time, Assume_Valid is set False | |
9229 | -- in the call to Compile_Time_Compare. If this call results in a | |
9230 | -- clear result of always True or Always False, that's decisive and | |
9231 | -- we are done. Otherwise we repeat the processing with Assume_Valid | |
9232 | -- set to True to generate additional warnings. We can stil that step | |
9233 | -- if Constant_Condition_Warnings is False. | |
9234 | ||
9235 | for AV in False .. True loop | |
9236 | declare | |
9237 | Typ : constant Entity_Id := Etype (N); | |
9238 | Op1 : constant Node_Id := Left_Opnd (N); | |
9239 | Op2 : constant Node_Id := Right_Opnd (N); | |
70482933 | 9240 | |
c800f862 RD |
9241 | Res : constant Compare_Result := |
9242 | Compile_Time_Compare (Op1, Op2, Assume_Valid => AV); | |
9243 | -- Res indicates if compare outcome can be compile time determined | |
f02b8bb8 | 9244 | |
c800f862 RD |
9245 | True_Result : Boolean; |
9246 | False_Result : Boolean; | |
f02b8bb8 | 9247 | |
c800f862 RD |
9248 | begin |
9249 | case N_Op_Compare (Nkind (N)) is | |
d26dc4b5 AC |
9250 | when N_Op_Eq => |
9251 | True_Result := Res = EQ; | |
9252 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
9253 | ||
9254 | when N_Op_Ge => | |
9255 | True_Result := Res in Compare_GE; | |
9256 | False_Result := Res = LT; | |
9257 | ||
9258 | if Res = LE | |
9259 | and then Constant_Condition_Warnings | |
9260 | and then Comes_From_Source (Original_Node (N)) | |
9261 | and then Nkind (Original_Node (N)) = N_Op_Ge | |
9262 | and then not In_Instance | |
d26dc4b5 | 9263 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 9264 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 AC |
9265 | then |
9266 | Error_Msg_N | |
9267 | ("can never be greater than, could replace by ""'=""?", N); | |
c800f862 | 9268 | Warning_Generated := True; |
d26dc4b5 | 9269 | end if; |
70482933 | 9270 | |
d26dc4b5 AC |
9271 | when N_Op_Gt => |
9272 | True_Result := Res = GT; | |
9273 | False_Result := Res in Compare_LE; | |
9274 | ||
9275 | when N_Op_Lt => | |
9276 | True_Result := Res = LT; | |
9277 | False_Result := Res in Compare_GE; | |
9278 | ||
9279 | when N_Op_Le => | |
9280 | True_Result := Res in Compare_LE; | |
9281 | False_Result := Res = GT; | |
9282 | ||
9283 | if Res = GE | |
9284 | and then Constant_Condition_Warnings | |
9285 | and then Comes_From_Source (Original_Node (N)) | |
9286 | and then Nkind (Original_Node (N)) = N_Op_Le | |
9287 | and then not In_Instance | |
d26dc4b5 | 9288 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 9289 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 AC |
9290 | then |
9291 | Error_Msg_N | |
9292 | ("can never be less than, could replace by ""'=""?", N); | |
c800f862 | 9293 | Warning_Generated := True; |
d26dc4b5 | 9294 | end if; |
70482933 | 9295 | |
d26dc4b5 AC |
9296 | when N_Op_Ne => |
9297 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
9298 | False_Result := Res = EQ; | |
c800f862 | 9299 | end case; |
d26dc4b5 | 9300 | |
c800f862 RD |
9301 | -- If this is the first iteration, then we actually convert the |
9302 | -- comparison into True or False, if the result is certain. | |
d26dc4b5 | 9303 | |
c800f862 RD |
9304 | if AV = False then |
9305 | if True_Result or False_Result then | |
9306 | if True_Result then | |
9307 | Result := Standard_True; | |
9308 | else | |
9309 | Result := Standard_False; | |
9310 | end if; | |
9311 | ||
9312 | Rewrite (N, | |
9313 | Convert_To (Typ, | |
9314 | New_Occurrence_Of (Result, Sloc (N)))); | |
9315 | Analyze_And_Resolve (N, Typ); | |
9316 | Warn_On_Known_Condition (N); | |
9317 | return; | |
9318 | end if; | |
9319 | ||
9320 | -- If this is the second iteration (AV = True), and the original | |
9321 | -- node comes from source and we are not in an instance, then | |
9322 | -- give a warning if we know result would be True or False. Note | |
9323 | -- we know Constant_Condition_Warnings is set if we get here. | |
9324 | ||
9325 | elsif Comes_From_Source (Original_Node (N)) | |
9326 | and then not In_Instance | |
9327 | then | |
9328 | if True_Result then | |
9329 | Error_Msg_N | |
9330 | ("condition can only be False if invalid values present?", | |
9331 | N); | |
9332 | elsif False_Result then | |
9333 | Error_Msg_N | |
9334 | ("condition can only be True if invalid values present?", | |
9335 | N); | |
9336 | end if; | |
9337 | end if; | |
9338 | end; | |
9339 | ||
9340 | -- Skip second iteration if not warning on constant conditions or | |
9341 | -- if the first iteration already generated a warning of some kind | |
9342 | -- or if we are in any case assuming all values are valid (so that | |
9343 | -- the first iteration took care of the valid case). | |
9344 | ||
9345 | exit when not Constant_Condition_Warnings; | |
9346 | exit when Warning_Generated; | |
9347 | exit when Assume_No_Invalid_Values; | |
9348 | end loop; | |
70482933 RK |
9349 | end Rewrite_Comparison; |
9350 | ||
fbf5a39b AC |
9351 | ---------------------------- |
9352 | -- Safe_In_Place_Array_Op -- | |
9353 | ---------------------------- | |
9354 | ||
9355 | function Safe_In_Place_Array_Op | |
2e071734 AC |
9356 | (Lhs : Node_Id; |
9357 | Op1 : Node_Id; | |
9358 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
9359 | is |
9360 | Target : Entity_Id; | |
9361 | ||
9362 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
9363 | -- Operand is safe if it cannot overlap part of the target of the | |
9364 | -- operation. If the operand and the target are identical, the operand | |
9365 | -- is safe. The operand can be empty in the case of negation. | |
9366 | ||
9367 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 9368 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
9369 | |
9370 | ------------------ | |
9371 | -- Is_Unaliased -- | |
9372 | ------------------ | |
9373 | ||
9374 | function Is_Unaliased (N : Node_Id) return Boolean is | |
9375 | begin | |
9376 | return | |
9377 | Is_Entity_Name (N) | |
9378 | and then No (Address_Clause (Entity (N))) | |
9379 | and then No (Renamed_Object (Entity (N))); | |
9380 | end Is_Unaliased; | |
9381 | ||
9382 | --------------------- | |
9383 | -- Is_Safe_Operand -- | |
9384 | --------------------- | |
9385 | ||
9386 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
9387 | begin | |
9388 | if No (Op) then | |
9389 | return True; | |
9390 | ||
9391 | elsif Is_Entity_Name (Op) then | |
9392 | return Is_Unaliased (Op); | |
9393 | ||
303b4d58 | 9394 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
9395 | return Is_Unaliased (Prefix (Op)); |
9396 | ||
9397 | elsif Nkind (Op) = N_Slice then | |
9398 | return | |
9399 | Is_Unaliased (Prefix (Op)) | |
9400 | and then Entity (Prefix (Op)) /= Target; | |
9401 | ||
9402 | elsif Nkind (Op) = N_Op_Not then | |
9403 | return Is_Safe_Operand (Right_Opnd (Op)); | |
9404 | ||
9405 | else | |
9406 | return False; | |
9407 | end if; | |
9408 | end Is_Safe_Operand; | |
9409 | ||
9410 | -- Start of processing for Is_Safe_In_Place_Array_Op | |
9411 | ||
9412 | begin | |
685094bf RD |
9413 | -- Skip this processing if the component size is different from system |
9414 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b AC |
9415 | |
9416 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then | |
9417 | return False; | |
9418 | ||
26bff3d9 | 9419 | -- Cannot do in place stuff on VM_Target since cannot pass addresses |
fbf5a39b | 9420 | |
26bff3d9 | 9421 | elsif VM_Target /= No_VM then |
fbf5a39b AC |
9422 | return False; |
9423 | ||
9424 | -- Cannot do in place stuff if non-standard Boolean representation | |
9425 | ||
9426 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then | |
9427 | return False; | |
9428 | ||
9429 | elsif not Is_Unaliased (Lhs) then | |
9430 | return False; | |
9431 | else | |
9432 | Target := Entity (Lhs); | |
9433 | ||
9434 | return | |
9435 | Is_Safe_Operand (Op1) | |
9436 | and then Is_Safe_Operand (Op2); | |
9437 | end if; | |
9438 | end Safe_In_Place_Array_Op; | |
9439 | ||
70482933 RK |
9440 | ----------------------- |
9441 | -- Tagged_Membership -- | |
9442 | ----------------------- | |
9443 | ||
685094bf RD |
9444 | -- There are two different cases to consider depending on whether the right |
9445 | -- operand is a class-wide type or not. If not we just compare the actual | |
9446 | -- tag of the left expr to the target type tag: | |
70482933 RK |
9447 | -- |
9448 | -- Left_Expr.Tag = Right_Type'Tag; | |
9449 | -- | |
685094bf RD |
9450 | -- If it is a class-wide type we use the RT function CW_Membership which is |
9451 | -- usually implemented by looking in the ancestor tables contained in the | |
9452 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 9453 | |
0669bebe GB |
9454 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
9455 | -- function IW_Membership which is usually implemented by looking in the | |
9456 | -- table of abstract interface types plus the ancestor table contained in | |
9457 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
9458 | ||
70482933 RK |
9459 | function Tagged_Membership (N : Node_Id) return Node_Id is |
9460 | Left : constant Node_Id := Left_Opnd (N); | |
9461 | Right : constant Node_Id := Right_Opnd (N); | |
9462 | Loc : constant Source_Ptr := Sloc (N); | |
9463 | ||
9464 | Left_Type : Entity_Id; | |
9465 | Right_Type : Entity_Id; | |
9466 | Obj_Tag : Node_Id; | |
9467 | ||
9468 | begin | |
9469 | Left_Type := Etype (Left); | |
9470 | Right_Type := Etype (Right); | |
9471 | ||
9472 | if Is_Class_Wide_Type (Left_Type) then | |
9473 | Left_Type := Root_Type (Left_Type); | |
9474 | end if; | |
9475 | ||
9476 | Obj_Tag := | |
9477 | Make_Selected_Component (Loc, | |
9478 | Prefix => Relocate_Node (Left), | |
a9d8907c JM |
9479 | Selector_Name => |
9480 | New_Reference_To (First_Tag_Component (Left_Type), Loc)); | |
70482933 RK |
9481 | |
9482 | if Is_Class_Wide_Type (Right_Type) then | |
758c442c | 9483 | |
0669bebe GB |
9484 | -- No need to issue a run-time check if we statically know that the |
9485 | -- result of this membership test is always true. For example, | |
9486 | -- considering the following declarations: | |
9487 | ||
9488 | -- type Iface is interface; | |
9489 | -- type T is tagged null record; | |
9490 | -- type DT is new T and Iface with null record; | |
9491 | ||
9492 | -- Obj1 : T; | |
9493 | -- Obj2 : DT; | |
9494 | ||
9495 | -- These membership tests are always true: | |
9496 | ||
9497 | -- Obj1 in T'Class | |
9498 | -- Obj2 in T'Class; | |
9499 | -- Obj2 in Iface'Class; | |
9500 | ||
9501 | -- We do not need to handle cases where the membership is illegal. | |
9502 | -- For example: | |
9503 | ||
9504 | -- Obj1 in DT'Class; -- Compile time error | |
9505 | -- Obj1 in Iface'Class; -- Compile time error | |
9506 | ||
9507 | if not Is_Class_Wide_Type (Left_Type) | |
ce2b6ba5 | 9508 | and then (Is_Ancestor (Etype (Right_Type), Left_Type) |
0669bebe GB |
9509 | or else (Is_Interface (Etype (Right_Type)) |
9510 | and then Interface_Present_In_Ancestor | |
9511 | (Typ => Left_Type, | |
9512 | Iface => Etype (Right_Type)))) | |
9513 | then | |
9514 | return New_Reference_To (Standard_True, Loc); | |
9515 | end if; | |
9516 | ||
758c442c GD |
9517 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
9518 | ||
630d30e9 RD |
9519 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
9520 | ||
0669bebe | 9521 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
9522 | |
9523 | or else Is_Interface (Left_Type) | |
9524 | then | |
dfd99a80 TQ |
9525 | -- Issue error if IW_Membership operation not available in a |
9526 | -- configurable run time setting. | |
9527 | ||
9528 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
9529 | Error_Msg_CRT |
9530 | ("dynamic membership test on interface types", N); | |
dfd99a80 TQ |
9531 | return Empty; |
9532 | end if; | |
9533 | ||
758c442c GD |
9534 | return |
9535 | Make_Function_Call (Loc, | |
9536 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
9537 | Parameter_Associations => New_List ( | |
9538 | Make_Attribute_Reference (Loc, | |
9539 | Prefix => Obj_Tag, | |
9540 | Attribute_Name => Name_Address), | |
9541 | New_Reference_To ( | |
9542 | Node (First_Elmt | |
9543 | (Access_Disp_Table (Root_Type (Right_Type)))), | |
9544 | Loc))); | |
9545 | ||
9546 | -- Ada 95: Normal case | |
9547 | ||
9548 | else | |
9549 | return | |
0669bebe GB |
9550 | Build_CW_Membership (Loc, |
9551 | Obj_Tag_Node => Obj_Tag, | |
9552 | Typ_Tag_Node => | |
758c442c GD |
9553 | New_Reference_To ( |
9554 | Node (First_Elmt | |
9555 | (Access_Disp_Table (Root_Type (Right_Type)))), | |
0669bebe | 9556 | Loc)); |
758c442c GD |
9557 | end if; |
9558 | ||
0669bebe GB |
9559 | -- Right_Type is not a class-wide type |
9560 | ||
70482933 | 9561 | else |
0669bebe GB |
9562 | -- No need to check the tag of the object if Right_Typ is abstract |
9563 | ||
9564 | if Is_Abstract_Type (Right_Type) then | |
9565 | return New_Reference_To (Standard_False, Loc); | |
9566 | ||
9567 | else | |
9568 | return | |
9569 | Make_Op_Eq (Loc, | |
9570 | Left_Opnd => Obj_Tag, | |
9571 | Right_Opnd => | |
9572 | New_Reference_To | |
9573 | (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc)); | |
9574 | end if; | |
70482933 | 9575 | end if; |
70482933 RK |
9576 | end Tagged_Membership; |
9577 | ||
9578 | ------------------------------ | |
9579 | -- Unary_Op_Validity_Checks -- | |
9580 | ------------------------------ | |
9581 | ||
9582 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
9583 | begin | |
9584 | if Validity_Checks_On and Validity_Check_Operands then | |
9585 | Ensure_Valid (Right_Opnd (N)); | |
9586 | end if; | |
9587 | end Unary_Op_Validity_Checks; | |
9588 | ||
9589 | end Exp_Ch4; |