]>
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 | -- -- | |
9cbfc269 | 9 | -- Copyright (C) 1992-2010, 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; | |
25adc5fb | 50 | with Par_SCO; use Par_SCO; |
0669bebe GB |
51 | with Restrict; use Restrict; |
52 | with Rident; use Rident; | |
70482933 RK |
53 | with Rtsfind; use Rtsfind; |
54 | with Sem; use Sem; | |
a4100e55 | 55 | with Sem_Aux; use Sem_Aux; |
70482933 | 56 | with Sem_Cat; use Sem_Cat; |
5d09245e | 57 | with Sem_Ch3; use Sem_Ch3; |
26bff3d9 | 58 | with Sem_Ch8; use Sem_Ch8; |
70482933 RK |
59 | with Sem_Ch13; use Sem_Ch13; |
60 | with Sem_Eval; use Sem_Eval; | |
61 | with Sem_Res; use Sem_Res; | |
62 | with Sem_Type; use Sem_Type; | |
63 | with Sem_Util; use Sem_Util; | |
07fc65c4 | 64 | with Sem_Warn; use Sem_Warn; |
70482933 | 65 | with Sinfo; use Sinfo; |
70482933 RK |
66 | with Snames; use Snames; |
67 | with Stand; use Stand; | |
7665e4bd | 68 | with SCIL_LL; use SCIL_LL; |
07fc65c4 | 69 | with Targparm; use Targparm; |
70482933 RK |
70 | with Tbuild; use Tbuild; |
71 | with Ttypes; use Ttypes; | |
72 | with Uintp; use Uintp; | |
73 | with Urealp; use Urealp; | |
74 | with Validsw; use Validsw; | |
75 | ||
76 | package body Exp_Ch4 is | |
77 | ||
15ce9ca2 AC |
78 | ----------------------- |
79 | -- Local Subprograms -- | |
80 | ----------------------- | |
70482933 RK |
81 | |
82 | procedure Binary_Op_Validity_Checks (N : Node_Id); | |
83 | pragma Inline (Binary_Op_Validity_Checks); | |
84 | -- Performs validity checks for a binary operator | |
85 | ||
fbf5a39b AC |
86 | procedure Build_Boolean_Array_Proc_Call |
87 | (N : Node_Id; | |
88 | Op1 : Node_Id; | |
89 | Op2 : Node_Id); | |
303b4d58 | 90 | -- If a boolean array assignment can be done in place, build call to |
fbf5a39b AC |
91 | -- corresponding library procedure. |
92 | ||
26bff3d9 JM |
93 | procedure Displace_Allocator_Pointer (N : Node_Id); |
94 | -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and | |
95 | -- Expand_Allocator_Expression. Allocating class-wide interface objects | |
96 | -- this routine displaces the pointer to the allocated object to reference | |
97 | -- the component referencing the corresponding secondary dispatch table. | |
98 | ||
fbf5a39b AC |
99 | procedure Expand_Allocator_Expression (N : Node_Id); |
100 | -- Subsidiary to Expand_N_Allocator, for the case when the expression | |
101 | -- is a qualified expression or an aggregate. | |
102 | ||
70482933 RK |
103 | procedure Expand_Array_Comparison (N : Node_Id); |
104 | -- This routine handles expansion of the comparison operators (N_Op_Lt, | |
105 | -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic | |
106 | -- code for these operators is similar, differing only in the details of | |
fbf5a39b AC |
107 | -- the actual comparison call that is made. Special processing (call a |
108 | -- run-time routine) | |
70482933 RK |
109 | |
110 | function Expand_Array_Equality | |
111 | (Nod : Node_Id; | |
70482933 RK |
112 | Lhs : Node_Id; |
113 | Rhs : Node_Id; | |
0da2c8ac AC |
114 | Bodies : List_Id; |
115 | Typ : Entity_Id) return Node_Id; | |
70482933 | 116 | -- Expand an array equality into a call to a function implementing this |
685094bf RD |
117 | -- equality, and a call to it. Loc is the location for the generated nodes. |
118 | -- Lhs and Rhs are the array expressions to be compared. Bodies is a list | |
119 | -- on which to attach bodies of local functions that are created in the | |
120 | -- process. It is the responsibility of the caller to insert those bodies | |
121 | -- at the right place. Nod provides the Sloc value for the generated code. | |
122 | -- Normally the types used for the generated equality routine are taken | |
123 | -- from Lhs and Rhs. However, in some situations of generated code, the | |
124 | -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies | |
125 | -- the type to be used for the formal parameters. | |
70482933 RK |
126 | |
127 | procedure Expand_Boolean_Operator (N : Node_Id); | |
685094bf RD |
128 | -- Common expansion processing for Boolean operators (And, Or, Xor) for the |
129 | -- case of array type arguments. | |
70482933 | 130 | |
5875f8d6 AC |
131 | procedure Expand_Short_Circuit_Operator (N : Node_Id); |
132 | -- Common expansion processing for short-circuit boolean operators | |
133 | ||
70482933 RK |
134 | function Expand_Composite_Equality |
135 | (Nod : Node_Id; | |
136 | Typ : Entity_Id; | |
137 | Lhs : Node_Id; | |
138 | Rhs : Node_Id; | |
2e071734 | 139 | Bodies : List_Id) return Node_Id; |
685094bf RD |
140 | -- Local recursive function used to expand equality for nested composite |
141 | -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which | |
142 | -- to attach bodies of local functions that are created in the process. | |
143 | -- This is the responsibility of the caller to insert those bodies at the | |
144 | -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs | |
145 | -- are the left and right sides for the comparison, and Typ is the type of | |
146 | -- the arrays to compare. | |
70482933 | 147 | |
fdac1f80 AC |
148 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id); |
149 | -- Routine to expand concatenation of a sequence of two or more operands | |
150 | -- (in the list Operands) and replace node Cnode with the result of the | |
151 | -- concatenation. The operands can be of any appropriate type, and can | |
152 | -- include both arrays and singleton elements. | |
70482933 RK |
153 | |
154 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); | |
685094bf RD |
155 | -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal |
156 | -- fixed. We do not have such a type at runtime, so the purpose of this | |
157 | -- routine is to find the real type by looking up the tree. We also | |
158 | -- determine if the operation must be rounded. | |
70482933 | 159 | |
fbf5a39b AC |
160 | function Get_Allocator_Final_List |
161 | (N : Node_Id; | |
162 | T : Entity_Id; | |
2e071734 | 163 | PtrT : Entity_Id) return Entity_Id; |
685094bf RD |
164 | -- If the designated type is controlled, build final_list expression for |
165 | -- created object. If context is an access parameter, create a local access | |
166 | -- type to have a usable finalization list. | |
fbf5a39b | 167 | |
5d09245e AC |
168 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean; |
169 | -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable | |
170 | -- discriminants if it has a constrained nominal type, unless the object | |
171 | -- is a component of an enclosing Unchecked_Union object that is subject | |
172 | -- to a per-object constraint and the enclosing object lacks inferable | |
173 | -- discriminants. | |
174 | -- | |
175 | -- An expression of an Unchecked_Union type has inferable discriminants | |
176 | -- if it is either a name of an object with inferable discriminants or a | |
177 | -- qualified expression whose subtype mark denotes a constrained subtype. | |
178 | ||
70482933 | 179 | procedure Insert_Dereference_Action (N : Node_Id); |
e6f69614 AC |
180 | -- N is an expression whose type is an access. When the type of the |
181 | -- associated storage pool is derived from Checked_Pool, generate a | |
182 | -- call to the 'Dereference' primitive operation. | |
70482933 RK |
183 | |
184 | function Make_Array_Comparison_Op | |
2e071734 AC |
185 | (Typ : Entity_Id; |
186 | Nod : Node_Id) return Node_Id; | |
685094bf RD |
187 | -- Comparisons between arrays are expanded in line. This function produces |
188 | -- the body of the implementation of (a > b), where a and b are one- | |
189 | -- dimensional arrays of some discrete type. The original node is then | |
190 | -- expanded into the appropriate call to this function. Nod provides the | |
191 | -- Sloc value for the generated code. | |
70482933 RK |
192 | |
193 | function Make_Boolean_Array_Op | |
2e071734 AC |
194 | (Typ : Entity_Id; |
195 | N : Node_Id) return Node_Id; | |
685094bf RD |
196 | -- Boolean operations on boolean arrays are expanded in line. This function |
197 | -- produce the body for the node N, which is (a and b), (a or b), or (a xor | |
198 | -- b). It is used only the normal case and not the packed case. The type | |
199 | -- involved, Typ, is the Boolean array type, and the logical operations in | |
200 | -- the body are simple boolean operations. Note that Typ is always a | |
201 | -- constrained type (the caller has ensured this by using | |
202 | -- Convert_To_Actual_Subtype if necessary). | |
70482933 RK |
203 | |
204 | procedure Rewrite_Comparison (N : Node_Id); | |
20b5d666 | 205 | -- If N is the node for a comparison whose outcome can be determined at |
d26dc4b5 AC |
206 | -- compile time, then the node N can be rewritten with True or False. If |
207 | -- the outcome cannot be determined at compile time, the call has no | |
208 | -- effect. If N is a type conversion, then this processing is applied to | |
209 | -- its expression. If N is neither comparison nor a type conversion, the | |
210 | -- call has no effect. | |
70482933 | 211 | |
82878151 AC |
212 | procedure Tagged_Membership |
213 | (N : Node_Id; | |
214 | SCIL_Node : out Node_Id; | |
215 | Result : out Node_Id); | |
70482933 RK |
216 | -- Construct the expression corresponding to the tagged membership test. |
217 | -- Deals with a second operand being (or not) a class-wide type. | |
218 | ||
fbf5a39b | 219 | function Safe_In_Place_Array_Op |
2e071734 AC |
220 | (Lhs : Node_Id; |
221 | Op1 : Node_Id; | |
222 | Op2 : Node_Id) return Boolean; | |
685094bf RD |
223 | -- In the context of an assignment, where the right-hand side is a boolean |
224 | -- operation on arrays, check whether operation can be performed in place. | |
fbf5a39b | 225 | |
70482933 RK |
226 | procedure Unary_Op_Validity_Checks (N : Node_Id); |
227 | pragma Inline (Unary_Op_Validity_Checks); | |
228 | -- Performs validity checks for a unary operator | |
229 | ||
230 | ------------------------------- | |
231 | -- Binary_Op_Validity_Checks -- | |
232 | ------------------------------- | |
233 | ||
234 | procedure Binary_Op_Validity_Checks (N : Node_Id) is | |
235 | begin | |
236 | if Validity_Checks_On and Validity_Check_Operands then | |
237 | Ensure_Valid (Left_Opnd (N)); | |
238 | Ensure_Valid (Right_Opnd (N)); | |
239 | end if; | |
240 | end Binary_Op_Validity_Checks; | |
241 | ||
fbf5a39b AC |
242 | ------------------------------------ |
243 | -- Build_Boolean_Array_Proc_Call -- | |
244 | ------------------------------------ | |
245 | ||
246 | procedure Build_Boolean_Array_Proc_Call | |
247 | (N : Node_Id; | |
248 | Op1 : Node_Id; | |
249 | Op2 : Node_Id) | |
250 | is | |
251 | Loc : constant Source_Ptr := Sloc (N); | |
252 | Kind : constant Node_Kind := Nkind (Expression (N)); | |
253 | Target : constant Node_Id := | |
254 | Make_Attribute_Reference (Loc, | |
255 | Prefix => Name (N), | |
256 | Attribute_Name => Name_Address); | |
257 | ||
258 | Arg1 : constant Node_Id := Op1; | |
259 | Arg2 : Node_Id := Op2; | |
260 | Call_Node : Node_Id; | |
261 | Proc_Name : Entity_Id; | |
262 | ||
263 | begin | |
264 | if Kind = N_Op_Not then | |
265 | if Nkind (Op1) in N_Binary_Op then | |
266 | ||
5e1c00fa | 267 | -- Use negated version of the binary operators |
fbf5a39b AC |
268 | |
269 | if Nkind (Op1) = N_Op_And then | |
270 | Proc_Name := RTE (RE_Vector_Nand); | |
271 | ||
272 | elsif Nkind (Op1) = N_Op_Or then | |
273 | Proc_Name := RTE (RE_Vector_Nor); | |
274 | ||
275 | else pragma Assert (Nkind (Op1) = N_Op_Xor); | |
276 | Proc_Name := RTE (RE_Vector_Xor); | |
277 | end if; | |
278 | ||
279 | Call_Node := | |
280 | Make_Procedure_Call_Statement (Loc, | |
281 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
282 | ||
283 | Parameter_Associations => New_List ( | |
284 | Target, | |
285 | Make_Attribute_Reference (Loc, | |
286 | Prefix => Left_Opnd (Op1), | |
287 | Attribute_Name => Name_Address), | |
288 | ||
289 | Make_Attribute_Reference (Loc, | |
290 | Prefix => Right_Opnd (Op1), | |
291 | Attribute_Name => Name_Address), | |
292 | ||
293 | Make_Attribute_Reference (Loc, | |
294 | Prefix => Left_Opnd (Op1), | |
295 | Attribute_Name => Name_Length))); | |
296 | ||
297 | else | |
298 | Proc_Name := RTE (RE_Vector_Not); | |
299 | ||
300 | Call_Node := | |
301 | Make_Procedure_Call_Statement (Loc, | |
302 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
303 | Parameter_Associations => New_List ( | |
304 | Target, | |
305 | ||
306 | Make_Attribute_Reference (Loc, | |
307 | Prefix => Op1, | |
308 | Attribute_Name => Name_Address), | |
309 | ||
310 | Make_Attribute_Reference (Loc, | |
311 | Prefix => Op1, | |
312 | Attribute_Name => Name_Length))); | |
313 | end if; | |
314 | ||
315 | else | |
316 | -- We use the following equivalences: | |
317 | ||
318 | -- (not X) or (not Y) = not (X and Y) = Nand (X, Y) | |
319 | -- (not X) and (not Y) = not (X or Y) = Nor (X, Y) | |
320 | -- (not X) xor (not Y) = X xor Y | |
321 | -- X xor (not Y) = not (X xor Y) = Nxor (X, Y) | |
322 | ||
323 | if Nkind (Op1) = N_Op_Not then | |
324 | if Kind = N_Op_And then | |
325 | Proc_Name := RTE (RE_Vector_Nor); | |
fbf5a39b AC |
326 | elsif Kind = N_Op_Or then |
327 | Proc_Name := RTE (RE_Vector_Nand); | |
fbf5a39b AC |
328 | else |
329 | Proc_Name := RTE (RE_Vector_Xor); | |
330 | end if; | |
331 | ||
332 | else | |
333 | if Kind = N_Op_And then | |
334 | Proc_Name := RTE (RE_Vector_And); | |
fbf5a39b AC |
335 | elsif Kind = N_Op_Or then |
336 | Proc_Name := RTE (RE_Vector_Or); | |
fbf5a39b AC |
337 | elsif Nkind (Op2) = N_Op_Not then |
338 | Proc_Name := RTE (RE_Vector_Nxor); | |
339 | Arg2 := Right_Opnd (Op2); | |
fbf5a39b AC |
340 | else |
341 | Proc_Name := RTE (RE_Vector_Xor); | |
342 | end if; | |
343 | end if; | |
344 | ||
345 | Call_Node := | |
346 | Make_Procedure_Call_Statement (Loc, | |
347 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
348 | Parameter_Associations => New_List ( | |
349 | Target, | |
955871d3 AC |
350 | Make_Attribute_Reference (Loc, |
351 | Prefix => Arg1, | |
352 | Attribute_Name => Name_Address), | |
353 | Make_Attribute_Reference (Loc, | |
354 | Prefix => Arg2, | |
355 | Attribute_Name => Name_Address), | |
356 | Make_Attribute_Reference (Loc, | |
357 | Prefix => Op1, | |
358 | Attribute_Name => Name_Length))); | |
fbf5a39b AC |
359 | end if; |
360 | ||
361 | Rewrite (N, Call_Node); | |
362 | Analyze (N); | |
363 | ||
364 | exception | |
365 | when RE_Not_Available => | |
366 | return; | |
367 | end Build_Boolean_Array_Proc_Call; | |
368 | ||
26bff3d9 JM |
369 | -------------------------------- |
370 | -- Displace_Allocator_Pointer -- | |
371 | -------------------------------- | |
372 | ||
373 | procedure Displace_Allocator_Pointer (N : Node_Id) is | |
374 | Loc : constant Source_Ptr := Sloc (N); | |
375 | Orig_Node : constant Node_Id := Original_Node (N); | |
376 | Dtyp : Entity_Id; | |
377 | Etyp : Entity_Id; | |
378 | PtrT : Entity_Id; | |
379 | ||
380 | begin | |
303b4d58 AC |
381 | -- Do nothing in case of VM targets: the virtual machine will handle |
382 | -- interfaces directly. | |
383 | ||
1f110335 | 384 | if not Tagged_Type_Expansion then |
303b4d58 AC |
385 | return; |
386 | end if; | |
387 | ||
26bff3d9 JM |
388 | pragma Assert (Nkind (N) = N_Identifier |
389 | and then Nkind (Orig_Node) = N_Allocator); | |
390 | ||
391 | PtrT := Etype (Orig_Node); | |
d6a24cdb | 392 | Dtyp := Available_View (Designated_Type (PtrT)); |
26bff3d9 JM |
393 | Etyp := Etype (Expression (Orig_Node)); |
394 | ||
395 | if Is_Class_Wide_Type (Dtyp) | |
396 | and then Is_Interface (Dtyp) | |
397 | then | |
398 | -- If the type of the allocator expression is not an interface type | |
399 | -- we can generate code to reference the record component containing | |
400 | -- the pointer to the secondary dispatch table. | |
401 | ||
402 | if not Is_Interface (Etyp) then | |
403 | declare | |
404 | Saved_Typ : constant Entity_Id := Etype (Orig_Node); | |
405 | ||
406 | begin | |
407 | -- 1) Get access to the allocated object | |
408 | ||
409 | Rewrite (N, | |
410 | Make_Explicit_Dereference (Loc, | |
411 | Relocate_Node (N))); | |
412 | Set_Etype (N, Etyp); | |
413 | Set_Analyzed (N); | |
414 | ||
415 | -- 2) Add the conversion to displace the pointer to reference | |
416 | -- the secondary dispatch table. | |
417 | ||
418 | Rewrite (N, Convert_To (Dtyp, Relocate_Node (N))); | |
419 | Analyze_And_Resolve (N, Dtyp); | |
420 | ||
421 | -- 3) The 'access to the secondary dispatch table will be used | |
422 | -- as the value returned by the allocator. | |
423 | ||
424 | Rewrite (N, | |
425 | Make_Attribute_Reference (Loc, | |
426 | Prefix => Relocate_Node (N), | |
427 | Attribute_Name => Name_Access)); | |
428 | Set_Etype (N, Saved_Typ); | |
429 | Set_Analyzed (N); | |
430 | end; | |
431 | ||
432 | -- If the type of the allocator expression is an interface type we | |
433 | -- generate a run-time call to displace "this" to reference the | |
434 | -- component containing the pointer to the secondary dispatch table | |
435 | -- or else raise Constraint_Error if the actual object does not | |
436 | -- implement the target interface. This case corresponds with the | |
437 | -- following example: | |
438 | ||
8fc789c8 | 439 | -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is |
26bff3d9 JM |
440 | -- begin |
441 | -- return new Iface_2'Class'(Obj); | |
442 | -- end Op; | |
443 | ||
444 | else | |
445 | Rewrite (N, | |
446 | Unchecked_Convert_To (PtrT, | |
447 | Make_Function_Call (Loc, | |
448 | Name => New_Reference_To (RTE (RE_Displace), Loc), | |
449 | Parameter_Associations => New_List ( | |
450 | Unchecked_Convert_To (RTE (RE_Address), | |
451 | Relocate_Node (N)), | |
452 | ||
453 | New_Occurrence_Of | |
454 | (Elists.Node | |
455 | (First_Elmt | |
456 | (Access_Disp_Table (Etype (Base_Type (Dtyp))))), | |
457 | Loc))))); | |
458 | Analyze_And_Resolve (N, PtrT); | |
459 | end if; | |
460 | end if; | |
461 | end Displace_Allocator_Pointer; | |
462 | ||
fbf5a39b AC |
463 | --------------------------------- |
464 | -- Expand_Allocator_Expression -- | |
465 | --------------------------------- | |
466 | ||
467 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
f02b8bb8 RD |
468 | Loc : constant Source_Ptr := Sloc (N); |
469 | Exp : constant Node_Id := Expression (Expression (N)); | |
f02b8bb8 RD |
470 | PtrT : constant Entity_Id := Etype (N); |
471 | DesigT : constant Entity_Id := Designated_Type (PtrT); | |
26bff3d9 JM |
472 | |
473 | procedure Apply_Accessibility_Check | |
474 | (Ref : Node_Id; | |
475 | Built_In_Place : Boolean := False); | |
476 | -- Ada 2005 (AI-344): For an allocator with a class-wide designated | |
685094bf RD |
477 | -- type, generate an accessibility check to verify that the level of the |
478 | -- type of the created object is not deeper than the level of the access | |
479 | -- type. If the type of the qualified expression is class- wide, then | |
480 | -- always generate the check (except in the case where it is known to be | |
481 | -- unnecessary, see comment below). Otherwise, only generate the check | |
482 | -- if the level of the qualified expression type is statically deeper | |
483 | -- than the access type. | |
484 | -- | |
485 | -- Although the static accessibility will generally have been performed | |
486 | -- as a legality check, it won't have been done in cases where the | |
487 | -- allocator appears in generic body, so a run-time check is needed in | |
488 | -- general. One special case is when the access type is declared in the | |
489 | -- same scope as the class-wide allocator, in which case the check can | |
490 | -- never fail, so it need not be generated. | |
491 | -- | |
492 | -- As an open issue, there seem to be cases where the static level | |
493 | -- associated with the class-wide object's underlying type is not | |
494 | -- sufficient to perform the proper accessibility check, such as for | |
495 | -- allocators in nested subprograms or accept statements initialized by | |
496 | -- class-wide formals when the actual originates outside at a deeper | |
497 | -- static level. The nested subprogram case might require passing | |
498 | -- accessibility levels along with class-wide parameters, and the task | |
499 | -- case seems to be an actual gap in the language rules that needs to | |
500 | -- be fixed by the ARG. ??? | |
26bff3d9 JM |
501 | |
502 | ------------------------------- | |
503 | -- Apply_Accessibility_Check -- | |
504 | ------------------------------- | |
505 | ||
506 | procedure Apply_Accessibility_Check | |
507 | (Ref : Node_Id; | |
508 | Built_In_Place : Boolean := False) | |
509 | is | |
510 | Ref_Node : Node_Id; | |
511 | ||
512 | begin | |
513 | -- Note: we skip the accessibility check for the VM case, since | |
514 | -- there does not seem to be any practical way of implementing it. | |
515 | ||
516 | if Ada_Version >= Ada_05 | |
1f110335 | 517 | and then Tagged_Type_Expansion |
26bff3d9 JM |
518 | and then Is_Class_Wide_Type (DesigT) |
519 | and then not Scope_Suppress (Accessibility_Check) | |
520 | and then | |
521 | (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT) | |
522 | or else | |
523 | (Is_Class_Wide_Type (Etype (Exp)) | |
524 | and then Scope (PtrT) /= Current_Scope)) | |
525 | then | |
526 | -- If the allocator was built in place Ref is already a reference | |
527 | -- to the access object initialized to the result of the allocator | |
528 | -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise | |
529 | -- it is the entity associated with the object containing the | |
530 | -- address of the allocated object. | |
531 | ||
532 | if Built_In_Place then | |
533 | Ref_Node := New_Copy (Ref); | |
534 | else | |
535 | Ref_Node := New_Reference_To (Ref, Loc); | |
536 | end if; | |
537 | ||
538 | Insert_Action (N, | |
539 | Make_Raise_Program_Error (Loc, | |
540 | Condition => | |
541 | Make_Op_Gt (Loc, | |
542 | Left_Opnd => | |
543 | Build_Get_Access_Level (Loc, | |
544 | Make_Attribute_Reference (Loc, | |
545 | Prefix => Ref_Node, | |
546 | Attribute_Name => Name_Tag)), | |
547 | Right_Opnd => | |
548 | Make_Integer_Literal (Loc, | |
549 | Type_Access_Level (PtrT))), | |
550 | Reason => PE_Accessibility_Check_Failed)); | |
551 | end if; | |
552 | end Apply_Accessibility_Check; | |
553 | ||
554 | -- Local variables | |
555 | ||
556 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
557 | T : constant Entity_Id := Entity (Indic); | |
558 | Flist : Node_Id; | |
559 | Node : Node_Id; | |
560 | Temp : Entity_Id; | |
fbf5a39b | 561 | |
d26dc4b5 AC |
562 | TagT : Entity_Id := Empty; |
563 | -- Type used as source for tag assignment | |
564 | ||
565 | TagR : Node_Id := Empty; | |
566 | -- Target reference for tag assignment | |
567 | ||
fbf5a39b AC |
568 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); |
569 | ||
570 | Tag_Assign : Node_Id; | |
571 | Tmp_Node : Node_Id; | |
572 | ||
26bff3d9 JM |
573 | -- Start of processing for Expand_Allocator_Expression |
574 | ||
fbf5a39b | 575 | begin |
048e5cef | 576 | if Is_Tagged_Type (T) or else Needs_Finalization (T) then |
fbf5a39b | 577 | |
fadcf313 AC |
578 | if Is_CPP_Constructor_Call (Exp) then |
579 | ||
580 | -- Generate: | |
581 | -- Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn | |
582 | ||
583 | -- Allocate the object with no expression | |
584 | ||
585 | Node := Relocate_Node (N); | |
7b4db06c | 586 | Set_Expression (Node, New_Reference_To (Etype (Exp), Loc)); |
fadcf313 AC |
587 | |
588 | -- Avoid its expansion to avoid generating a call to the default | |
589 | -- C++ constructor | |
590 | ||
591 | Set_Analyzed (Node); | |
592 | ||
e86a3a7e | 593 | Temp := Make_Temporary (Loc, 'P', N); |
fadcf313 AC |
594 | |
595 | Insert_Action (N, | |
596 | Make_Object_Declaration (Loc, | |
597 | Defining_Identifier => Temp, | |
598 | Constant_Present => True, | |
599 | Object_Definition => New_Reference_To (PtrT, Loc), | |
600 | Expression => Node)); | |
601 | ||
602 | Apply_Accessibility_Check (Temp); | |
603 | ||
ffa5876f | 604 | -- Locate the enclosing list and insert the C++ constructor call |
fadcf313 AC |
605 | |
606 | declare | |
ffa5876f | 607 | P : Node_Id; |
fadcf313 AC |
608 | |
609 | begin | |
ffa5876f | 610 | P := Parent (Node); |
fadcf313 AC |
611 | while not Is_List_Member (P) loop |
612 | P := Parent (P); | |
613 | end loop; | |
614 | ||
615 | Insert_List_After_And_Analyze (P, | |
616 | Build_Initialization_Call (Loc, | |
ffa5876f AC |
617 | Id_Ref => |
618 | Make_Explicit_Dereference (Loc, | |
619 | Prefix => New_Reference_To (Temp, Loc)), | |
7b4db06c | 620 | Typ => Etype (Exp), |
fadcf313 AC |
621 | Constructor_Ref => Exp)); |
622 | end; | |
623 | ||
624 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
625 | Analyze_And_Resolve (N, PtrT); | |
fadcf313 AC |
626 | return; |
627 | end if; | |
628 | ||
685094bf RD |
629 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
630 | -- to a build-in-place function, then access to the allocated object | |
631 | -- must be passed to the function. Currently we limit such functions | |
632 | -- to those with constrained limited result subtypes, but eventually | |
633 | -- we plan to expand the allowed forms of functions that are treated | |
634 | -- as build-in-place. | |
20b5d666 JM |
635 | |
636 | if Ada_Version >= Ada_05 | |
637 | and then Is_Build_In_Place_Function_Call (Exp) | |
638 | then | |
639 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
26bff3d9 JM |
640 | Apply_Accessibility_Check (N, Built_In_Place => True); |
641 | return; | |
20b5d666 JM |
642 | end if; |
643 | ||
fbf5a39b AC |
644 | -- Actions inserted before: |
645 | -- Temp : constant ptr_T := new T'(Expression); | |
646 | -- <no CW> Temp._tag := T'tag; | |
647 | -- <CTRL> Adjust (Finalizable (Temp.all)); | |
648 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
649 | ||
650 | -- We analyze by hand the new internal allocator to avoid | |
651 | -- any recursion and inappropriate call to Initialize | |
7324bf49 | 652 | |
20b5d666 JM |
653 | -- We don't want to remove side effects when the expression must be |
654 | -- built in place. In the case of a build-in-place function call, | |
655 | -- that could lead to a duplication of the call, which was already | |
656 | -- substituted for the allocator. | |
657 | ||
26bff3d9 | 658 | if not Aggr_In_Place then |
fbf5a39b AC |
659 | Remove_Side_Effects (Exp); |
660 | end if; | |
661 | ||
e86a3a7e | 662 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
663 | |
664 | -- For a class wide allocation generate the following code: | |
665 | ||
666 | -- type Equiv_Record is record ... end record; | |
667 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
668 | -- temp : PtrT := new CW'(CW!(expr)); | |
669 | ||
670 | if Is_Class_Wide_Type (T) then | |
671 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
672 | ||
26bff3d9 JM |
673 | -- Ada 2005 (AI-251): If the expression is a class-wide interface |
674 | -- object we generate code to move up "this" to reference the | |
675 | -- base of the object before allocating the new object. | |
676 | ||
677 | -- Note that Exp'Address is recursively expanded into a call | |
678 | -- to Base_Address (Exp.Tag) | |
679 | ||
680 | if Is_Class_Wide_Type (Etype (Exp)) | |
681 | and then Is_Interface (Etype (Exp)) | |
1f110335 | 682 | and then Tagged_Type_Expansion |
26bff3d9 JM |
683 | then |
684 | Set_Expression | |
685 | (Expression (N), | |
686 | Unchecked_Convert_To (Entity (Indic), | |
687 | Make_Explicit_Dereference (Loc, | |
688 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
689 | Make_Attribute_Reference (Loc, | |
690 | Prefix => Exp, | |
691 | Attribute_Name => Name_Address))))); | |
692 | ||
693 | else | |
694 | Set_Expression | |
695 | (Expression (N), | |
696 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
697 | end if; | |
fbf5a39b AC |
698 | |
699 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
700 | end if; | |
701 | ||
26bff3d9 | 702 | -- Keep separate the management of allocators returning interfaces |
fbf5a39b | 703 | |
26bff3d9 JM |
704 | if not Is_Interface (Directly_Designated_Type (PtrT)) then |
705 | if Aggr_In_Place then | |
706 | Tmp_Node := | |
707 | Make_Object_Declaration (Loc, | |
708 | Defining_Identifier => Temp, | |
709 | Object_Definition => New_Reference_To (PtrT, Loc), | |
710 | Expression => | |
711 | Make_Allocator (Loc, | |
712 | New_Reference_To (Etype (Exp), Loc))); | |
fbf5a39b | 713 | |
fad0600d AC |
714 | -- Copy the Comes_From_Source flag for the allocator we just |
715 | -- built, since logically this allocator is a replacement of | |
716 | -- the original allocator node. This is for proper handling of | |
717 | -- restriction No_Implicit_Heap_Allocations. | |
718 | ||
26bff3d9 JM |
719 | Set_Comes_From_Source |
720 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
fbf5a39b | 721 | |
26bff3d9 JM |
722 | Set_No_Initialization (Expression (Tmp_Node)); |
723 | Insert_Action (N, Tmp_Node); | |
fbf5a39b | 724 | |
048e5cef | 725 | if Needs_Finalization (T) |
26bff3d9 JM |
726 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
727 | then | |
728 | -- Create local finalization list for access parameter | |
729 | ||
730 | Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
731 | end if; | |
732 | ||
d766cee3 | 733 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
fad0600d | 734 | |
26bff3d9 JM |
735 | else |
736 | Node := Relocate_Node (N); | |
737 | Set_Analyzed (Node); | |
738 | Insert_Action (N, | |
739 | Make_Object_Declaration (Loc, | |
740 | Defining_Identifier => Temp, | |
741 | Constant_Present => True, | |
742 | Object_Definition => New_Reference_To (PtrT, Loc), | |
743 | Expression => Node)); | |
fbf5a39b AC |
744 | end if; |
745 | ||
26bff3d9 JM |
746 | -- Ada 2005 (AI-251): Handle allocators whose designated type is an |
747 | -- interface type. In this case we use the type of the qualified | |
748 | -- expression to allocate the object. | |
749 | ||
fbf5a39b | 750 | else |
26bff3d9 | 751 | declare |
191fcb3a | 752 | Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); |
26bff3d9 | 753 | New_Decl : Node_Id; |
fbf5a39b | 754 | |
26bff3d9 JM |
755 | begin |
756 | New_Decl := | |
757 | Make_Full_Type_Declaration (Loc, | |
758 | Defining_Identifier => Def_Id, | |
759 | Type_Definition => | |
760 | Make_Access_To_Object_Definition (Loc, | |
761 | All_Present => True, | |
762 | Null_Exclusion_Present => False, | |
763 | Constant_Present => False, | |
764 | Subtype_Indication => | |
765 | New_Reference_To (Etype (Exp), Loc))); | |
766 | ||
767 | Insert_Action (N, New_Decl); | |
768 | ||
769 | -- Inherit the final chain to ensure that the expansion of the | |
770 | -- aggregate is correct in case of controlled types | |
771 | ||
048e5cef | 772 | if Needs_Finalization (Directly_Designated_Type (PtrT)) then |
26bff3d9 JM |
773 | Set_Associated_Final_Chain (Def_Id, |
774 | Associated_Final_Chain (PtrT)); | |
775 | end if; | |
758c442c | 776 | |
26bff3d9 JM |
777 | -- Declare the object using the previous type declaration |
778 | ||
779 | if Aggr_In_Place then | |
780 | Tmp_Node := | |
781 | Make_Object_Declaration (Loc, | |
782 | Defining_Identifier => Temp, | |
783 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
784 | Expression => | |
785 | Make_Allocator (Loc, | |
786 | New_Reference_To (Etype (Exp), Loc))); | |
787 | ||
fad0600d AC |
788 | -- Copy the Comes_From_Source flag for the allocator we just |
789 | -- built, since logically this allocator is a replacement of | |
790 | -- the original allocator node. This is for proper handling | |
791 | -- of restriction No_Implicit_Heap_Allocations. | |
792 | ||
26bff3d9 JM |
793 | Set_Comes_From_Source |
794 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
795 | ||
796 | Set_No_Initialization (Expression (Tmp_Node)); | |
797 | Insert_Action (N, Tmp_Node); | |
798 | ||
048e5cef | 799 | if Needs_Finalization (T) |
26bff3d9 JM |
800 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
801 | then | |
802 | -- Create local finalization list for access parameter | |
803 | ||
804 | Flist := | |
805 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
806 | end if; | |
807 | ||
d766cee3 | 808 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
26bff3d9 JM |
809 | else |
810 | Node := Relocate_Node (N); | |
811 | Set_Analyzed (Node); | |
812 | Insert_Action (N, | |
813 | Make_Object_Declaration (Loc, | |
814 | Defining_Identifier => Temp, | |
815 | Constant_Present => True, | |
816 | Object_Definition => New_Reference_To (Def_Id, Loc), | |
817 | Expression => Node)); | |
818 | end if; | |
819 | ||
820 | -- Generate an additional object containing the address of the | |
821 | -- returned object. The type of this second object declaration | |
685094bf RD |
822 | -- is the correct type required for the common processing that |
823 | -- is still performed by this subprogram. The displacement of | |
824 | -- this pointer to reference the component associated with the | |
825 | -- interface type will be done at the end of common processing. | |
26bff3d9 JM |
826 | |
827 | New_Decl := | |
828 | Make_Object_Declaration (Loc, | |
191fcb3a | 829 | Defining_Identifier => Make_Temporary (Loc, 'P'), |
26bff3d9 JM |
830 | Object_Definition => New_Reference_To (PtrT, Loc), |
831 | Expression => Unchecked_Convert_To (PtrT, | |
832 | New_Reference_To (Temp, Loc))); | |
833 | ||
834 | Insert_Action (N, New_Decl); | |
835 | ||
836 | Tmp_Node := New_Decl; | |
837 | Temp := Defining_Identifier (New_Decl); | |
838 | end; | |
758c442c GD |
839 | end if; |
840 | ||
26bff3d9 JM |
841 | Apply_Accessibility_Check (Temp); |
842 | ||
843 | -- Generate the tag assignment | |
844 | ||
845 | -- Suppress the tag assignment when VM_Target because VM tags are | |
846 | -- represented implicitly in objects. | |
847 | ||
1f110335 | 848 | if not Tagged_Type_Expansion then |
26bff3d9 | 849 | null; |
fbf5a39b | 850 | |
26bff3d9 JM |
851 | -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide |
852 | -- interface objects because in this case the tag does not change. | |
d26dc4b5 | 853 | |
26bff3d9 JM |
854 | elsif Is_Interface (Directly_Designated_Type (Etype (N))) then |
855 | pragma Assert (Is_Class_Wide_Type | |
856 | (Directly_Designated_Type (Etype (N)))); | |
d26dc4b5 AC |
857 | null; |
858 | ||
859 | elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then | |
860 | TagT := T; | |
861 | TagR := New_Reference_To (Temp, Loc); | |
862 | ||
863 | elsif Is_Private_Type (T) | |
864 | and then Is_Tagged_Type (Underlying_Type (T)) | |
fbf5a39b | 865 | then |
d26dc4b5 | 866 | TagT := Underlying_Type (T); |
dfd99a80 TQ |
867 | TagR := |
868 | Unchecked_Convert_To (Underlying_Type (T), | |
869 | Make_Explicit_Dereference (Loc, | |
870 | Prefix => New_Reference_To (Temp, Loc))); | |
d26dc4b5 AC |
871 | end if; |
872 | ||
873 | if Present (TagT) then | |
fbf5a39b AC |
874 | Tag_Assign := |
875 | Make_Assignment_Statement (Loc, | |
876 | Name => | |
877 | Make_Selected_Component (Loc, | |
d26dc4b5 | 878 | Prefix => TagR, |
fbf5a39b | 879 | Selector_Name => |
d26dc4b5 | 880 | New_Reference_To (First_Tag_Component (TagT), Loc)), |
fbf5a39b AC |
881 | |
882 | Expression => | |
883 | Unchecked_Convert_To (RTE (RE_Tag), | |
a9d8907c | 884 | New_Reference_To |
d26dc4b5 | 885 | (Elists.Node (First_Elmt (Access_Disp_Table (TagT))), |
a9d8907c | 886 | Loc))); |
fbf5a39b AC |
887 | |
888 | -- The previous assignment has to be done in any case | |
889 | ||
890 | Set_Assignment_OK (Name (Tag_Assign)); | |
891 | Insert_Action (N, Tag_Assign); | |
fbf5a39b AC |
892 | end if; |
893 | ||
048e5cef BD |
894 | if Needs_Finalization (DesigT) |
895 | and then Needs_Finalization (T) | |
fbf5a39b AC |
896 | then |
897 | declare | |
898 | Attach : Node_Id; | |
899 | Apool : constant Entity_Id := | |
900 | Associated_Storage_Pool (PtrT); | |
901 | ||
902 | begin | |
685094bf RD |
903 | -- If it is an allocation on the secondary stack (i.e. a value |
904 | -- returned from a function), the object is attached on the | |
905 | -- caller side as soon as the call is completed (see | |
906 | -- Expand_Ctrl_Function_Call) | |
fbf5a39b AC |
907 | |
908 | if Is_RTE (Apool, RE_SS_Pool) then | |
909 | declare | |
191fcb3a | 910 | F : constant Entity_Id := Make_Temporary (Loc, 'F'); |
fbf5a39b AC |
911 | begin |
912 | Insert_Action (N, | |
913 | Make_Object_Declaration (Loc, | |
914 | Defining_Identifier => F, | |
191fcb3a RD |
915 | Object_Definition => |
916 | New_Reference_To (RTE (RE_Finalizable_Ptr), Loc))); | |
fbf5a39b AC |
917 | Flist := New_Reference_To (F, Loc); |
918 | Attach := Make_Integer_Literal (Loc, 1); | |
919 | end; | |
920 | ||
921 | -- Normal case, not a secondary stack allocation | |
922 | ||
923 | else | |
048e5cef | 924 | if Needs_Finalization (T) |
615cbd95 AC |
925 | and then Ekind (PtrT) = E_Anonymous_Access_Type |
926 | then | |
5e1c00fa | 927 | -- Create local finalization list for access parameter |
615cbd95 AC |
928 | |
929 | Flist := | |
930 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
931 | else | |
932 | Flist := Find_Final_List (PtrT); | |
933 | end if; | |
934 | ||
fbf5a39b AC |
935 | Attach := Make_Integer_Literal (Loc, 2); |
936 | end if; | |
937 | ||
26bff3d9 JM |
938 | -- Generate an Adjust call if the object will be moved. In Ada |
939 | -- 2005, the object may be inherently limited, in which case | |
940 | -- there is no Adjust procedure, and the object is built in | |
941 | -- place. In Ada 95, the object can be limited but not | |
942 | -- inherently limited if this allocator came from a return | |
943 | -- statement (we're allocating the result on the secondary | |
944 | -- stack). In that case, the object will be moved, so we _do_ | |
945 | -- want to Adjust. | |
946 | ||
947 | if not Aggr_In_Place | |
948 | and then not Is_Inherently_Limited_Type (T) | |
949 | then | |
fbf5a39b AC |
950 | Insert_Actions (N, |
951 | Make_Adjust_Call ( | |
952 | Ref => | |
953 | ||
685094bf RD |
954 | -- An unchecked conversion is needed in the classwide |
955 | -- case because the designated type can be an ancestor of | |
956 | -- the subtype mark of the allocator. | |
fbf5a39b AC |
957 | |
958 | Unchecked_Convert_To (T, | |
959 | Make_Explicit_Dereference (Loc, | |
dfd99a80 | 960 | Prefix => New_Reference_To (Temp, Loc))), |
fbf5a39b AC |
961 | |
962 | Typ => T, | |
963 | Flist_Ref => Flist, | |
dfd99a80 TQ |
964 | With_Attach => Attach, |
965 | Allocator => True)); | |
fbf5a39b AC |
966 | end if; |
967 | end; | |
968 | end if; | |
969 | ||
970 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
971 | Analyze_And_Resolve (N, PtrT); | |
972 | ||
685094bf RD |
973 | -- Ada 2005 (AI-251): Displace the pointer to reference the record |
974 | -- component containing the secondary dispatch table of the interface | |
975 | -- type. | |
26bff3d9 JM |
976 | |
977 | if Is_Interface (Directly_Designated_Type (PtrT)) then | |
978 | Displace_Allocator_Pointer (N); | |
979 | end if; | |
980 | ||
fbf5a39b | 981 | elsif Aggr_In_Place then |
e86a3a7e | 982 | Temp := Make_Temporary (Loc, 'P', N); |
fbf5a39b AC |
983 | Tmp_Node := |
984 | Make_Object_Declaration (Loc, | |
985 | Defining_Identifier => Temp, | |
986 | Object_Definition => New_Reference_To (PtrT, Loc), | |
987 | Expression => Make_Allocator (Loc, | |
988 | New_Reference_To (Etype (Exp), Loc))); | |
989 | ||
fad0600d AC |
990 | -- Copy the Comes_From_Source flag for the allocator we just built, |
991 | -- since logically this allocator is a replacement of the original | |
992 | -- allocator node. This is for proper handling of restriction | |
993 | -- No_Implicit_Heap_Allocations. | |
994 | ||
fbf5a39b AC |
995 | Set_Comes_From_Source |
996 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
997 | ||
998 | Set_No_Initialization (Expression (Tmp_Node)); | |
999 | Insert_Action (N, Tmp_Node); | |
d766cee3 | 1000 | Convert_Aggr_In_Allocator (N, Tmp_Node, Exp); |
fbf5a39b AC |
1001 | Rewrite (N, New_Reference_To (Temp, Loc)); |
1002 | Analyze_And_Resolve (N, PtrT); | |
1003 | ||
51e4c4b9 AC |
1004 | elsif Is_Access_Type (T) |
1005 | and then Can_Never_Be_Null (T) | |
1006 | then | |
1007 | Install_Null_Excluding_Check (Exp); | |
1008 | ||
f02b8bb8 | 1009 | elsif Is_Access_Type (DesigT) |
fbf5a39b AC |
1010 | and then Nkind (Exp) = N_Allocator |
1011 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
1012 | then | |
0da2c8ac | 1013 | -- Apply constraint to designated subtype indication |
fbf5a39b AC |
1014 | |
1015 | Apply_Constraint_Check (Expression (Exp), | |
f02b8bb8 | 1016 | Designated_Type (DesigT), |
fbf5a39b AC |
1017 | No_Sliding => True); |
1018 | ||
1019 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
1020 | ||
1021 | -- Propagate constraint_error to enclosing allocator | |
1022 | ||
1023 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
1024 | end if; | |
1025 | else | |
36c73552 AC |
1026 | -- If we have: |
1027 | -- type A is access T1; | |
1028 | -- X : A := new T2'(...); | |
1029 | -- T1 and T2 can be different subtypes, and we might need to check | |
1030 | -- both constraints. First check against the type of the qualified | |
1031 | -- expression. | |
1032 | ||
1033 | Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
fbf5a39b | 1034 | |
d79e621a GD |
1035 | if Do_Range_Check (Exp) then |
1036 | Set_Do_Range_Check (Exp, False); | |
1037 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); | |
1038 | end if; | |
1039 | ||
685094bf RD |
1040 | -- A check is also needed in cases where the designated subtype is |
1041 | -- constrained and differs from the subtype given in the qualified | |
1042 | -- expression. Note that the check on the qualified expression does | |
1043 | -- not allow sliding, but this check does (a relaxation from Ada 83). | |
fbf5a39b | 1044 | |
f02b8bb8 | 1045 | if Is_Constrained (DesigT) |
9450205a | 1046 | and then not Subtypes_Statically_Match (T, DesigT) |
fbf5a39b AC |
1047 | then |
1048 | Apply_Constraint_Check | |
f02b8bb8 | 1049 | (Exp, DesigT, No_Sliding => False); |
d79e621a GD |
1050 | |
1051 | if Do_Range_Check (Exp) then | |
1052 | Set_Do_Range_Check (Exp, False); | |
1053 | Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed); | |
1054 | end if; | |
f02b8bb8 RD |
1055 | end if; |
1056 | ||
685094bf RD |
1057 | -- For an access to unconstrained packed array, GIGI needs to see an |
1058 | -- expression with a constrained subtype in order to compute the | |
1059 | -- proper size for the allocator. | |
f02b8bb8 RD |
1060 | |
1061 | if Is_Array_Type (T) | |
1062 | and then not Is_Constrained (T) | |
1063 | and then Is_Packed (T) | |
1064 | then | |
1065 | declare | |
191fcb3a | 1066 | ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A'); |
f02b8bb8 RD |
1067 | Internal_Exp : constant Node_Id := Relocate_Node (Exp); |
1068 | begin | |
1069 | Insert_Action (Exp, | |
1070 | Make_Subtype_Declaration (Loc, | |
1071 | Defining_Identifier => ConstrT, | |
1072 | Subtype_Indication => | |
1073 | Make_Subtype_From_Expr (Exp, T))); | |
1074 | Freeze_Itype (ConstrT, Exp); | |
1075 | Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp)); | |
1076 | end; | |
fbf5a39b | 1077 | end if; |
f02b8bb8 | 1078 | |
685094bf RD |
1079 | -- Ada 2005 (AI-318-02): If the initialization expression is a call |
1080 | -- to a build-in-place function, then access to the allocated object | |
1081 | -- must be passed to the function. Currently we limit such functions | |
1082 | -- to those with constrained limited result subtypes, but eventually | |
1083 | -- we plan to expand the allowed forms of functions that are treated | |
1084 | -- as build-in-place. | |
20b5d666 JM |
1085 | |
1086 | if Ada_Version >= Ada_05 | |
1087 | and then Is_Build_In_Place_Function_Call (Exp) | |
1088 | then | |
1089 | Make_Build_In_Place_Call_In_Allocator (N, Exp); | |
1090 | end if; | |
fbf5a39b AC |
1091 | end if; |
1092 | ||
1093 | exception | |
1094 | when RE_Not_Available => | |
1095 | return; | |
1096 | end Expand_Allocator_Expression; | |
1097 | ||
70482933 RK |
1098 | ----------------------------- |
1099 | -- Expand_Array_Comparison -- | |
1100 | ----------------------------- | |
1101 | ||
685094bf RD |
1102 | -- Expansion is only required in the case of array types. For the unpacked |
1103 | -- case, an appropriate runtime routine is called. For packed cases, and | |
1104 | -- also in some other cases where a runtime routine cannot be called, the | |
1105 | -- form of the expansion is: | |
70482933 RK |
1106 | |
1107 | -- [body for greater_nn; boolean_expression] | |
1108 | ||
1109 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
1110 | -- Boolean expression depends on the operator involved. | |
1111 | ||
1112 | procedure Expand_Array_Comparison (N : Node_Id) is | |
1113 | Loc : constant Source_Ptr := Sloc (N); | |
1114 | Op1 : Node_Id := Left_Opnd (N); | |
1115 | Op2 : Node_Id := Right_Opnd (N); | |
1116 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 1117 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
1118 | |
1119 | Expr : Node_Id; | |
1120 | Func_Body : Node_Id; | |
1121 | Func_Name : Entity_Id; | |
1122 | ||
fbf5a39b AC |
1123 | Comp : RE_Id; |
1124 | ||
9bc43c53 AC |
1125 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
1126 | -- True for byte addressable target | |
91b1417d | 1127 | |
fbf5a39b | 1128 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
685094bf RD |
1129 | -- Returns True if the length of the given operand is known to be less |
1130 | -- than 4. Returns False if this length is known to be four or greater | |
1131 | -- or is not known at compile time. | |
fbf5a39b AC |
1132 | |
1133 | ------------------------ | |
1134 | -- Length_Less_Than_4 -- | |
1135 | ------------------------ | |
1136 | ||
1137 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
1138 | Otyp : constant Entity_Id := Etype (Opnd); | |
1139 | ||
1140 | begin | |
1141 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
1142 | return String_Literal_Length (Otyp) < 4; | |
1143 | ||
1144 | else | |
1145 | declare | |
1146 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
1147 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
1148 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
1149 | Lov : Uint; | |
1150 | Hiv : Uint; | |
1151 | ||
1152 | begin | |
1153 | if Compile_Time_Known_Value (Lo) then | |
1154 | Lov := Expr_Value (Lo); | |
1155 | else | |
1156 | return False; | |
1157 | end if; | |
1158 | ||
1159 | if Compile_Time_Known_Value (Hi) then | |
1160 | Hiv := Expr_Value (Hi); | |
1161 | else | |
1162 | return False; | |
1163 | end if; | |
1164 | ||
1165 | return Hiv < Lov + 3; | |
1166 | end; | |
1167 | end if; | |
1168 | end Length_Less_Than_4; | |
1169 | ||
1170 | -- Start of processing for Expand_Array_Comparison | |
1171 | ||
70482933 | 1172 | begin |
fbf5a39b AC |
1173 | -- Deal first with unpacked case, where we can call a runtime routine |
1174 | -- except that we avoid this for targets for which are not addressable | |
26bff3d9 | 1175 | -- by bytes, and for the JVM/CIL, since they do not support direct |
fbf5a39b AC |
1176 | -- addressing of array components. |
1177 | ||
1178 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 1179 | and then Byte_Addressable |
26bff3d9 | 1180 | and then VM_Target = No_VM |
fbf5a39b AC |
1181 | then |
1182 | -- The call we generate is: | |
1183 | ||
1184 | -- Compare_Array_xn[_Unaligned] | |
1185 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
1186 | ||
1187 | -- x = U for unsigned, S for signed | |
1188 | -- n = 8,16,32,64 for component size | |
1189 | -- Add _Unaligned if length < 4 and component size is 8. | |
1190 | -- <op> is the standard comparison operator | |
1191 | ||
1192 | if Component_Size (Typ1) = 8 then | |
1193 | if Length_Less_Than_4 (Op1) | |
1194 | or else | |
1195 | Length_Less_Than_4 (Op2) | |
1196 | then | |
1197 | if Is_Unsigned_Type (Ctyp) then | |
1198 | Comp := RE_Compare_Array_U8_Unaligned; | |
1199 | else | |
1200 | Comp := RE_Compare_Array_S8_Unaligned; | |
1201 | end if; | |
1202 | ||
1203 | else | |
1204 | if Is_Unsigned_Type (Ctyp) then | |
1205 | Comp := RE_Compare_Array_U8; | |
1206 | else | |
1207 | Comp := RE_Compare_Array_S8; | |
1208 | end if; | |
1209 | end if; | |
1210 | ||
1211 | elsif Component_Size (Typ1) = 16 then | |
1212 | if Is_Unsigned_Type (Ctyp) then | |
1213 | Comp := RE_Compare_Array_U16; | |
1214 | else | |
1215 | Comp := RE_Compare_Array_S16; | |
1216 | end if; | |
1217 | ||
1218 | elsif Component_Size (Typ1) = 32 then | |
1219 | if Is_Unsigned_Type (Ctyp) then | |
1220 | Comp := RE_Compare_Array_U32; | |
1221 | else | |
1222 | Comp := RE_Compare_Array_S32; | |
1223 | end if; | |
1224 | ||
1225 | else pragma Assert (Component_Size (Typ1) = 64); | |
1226 | if Is_Unsigned_Type (Ctyp) then | |
1227 | Comp := RE_Compare_Array_U64; | |
1228 | else | |
1229 | Comp := RE_Compare_Array_S64; | |
1230 | end if; | |
1231 | end if; | |
1232 | ||
1233 | Remove_Side_Effects (Op1, Name_Req => True); | |
1234 | Remove_Side_Effects (Op2, Name_Req => True); | |
1235 | ||
1236 | Rewrite (Op1, | |
1237 | Make_Function_Call (Sloc (Op1), | |
1238 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
1239 | ||
1240 | Parameter_Associations => New_List ( | |
1241 | Make_Attribute_Reference (Loc, | |
1242 | Prefix => Relocate_Node (Op1), | |
1243 | Attribute_Name => Name_Address), | |
1244 | ||
1245 | Make_Attribute_Reference (Loc, | |
1246 | Prefix => Relocate_Node (Op2), | |
1247 | Attribute_Name => Name_Address), | |
1248 | ||
1249 | Make_Attribute_Reference (Loc, | |
1250 | Prefix => Relocate_Node (Op1), | |
1251 | Attribute_Name => Name_Length), | |
1252 | ||
1253 | Make_Attribute_Reference (Loc, | |
1254 | Prefix => Relocate_Node (Op2), | |
1255 | Attribute_Name => Name_Length)))); | |
1256 | ||
1257 | Rewrite (Op2, | |
1258 | Make_Integer_Literal (Sloc (Op2), | |
1259 | Intval => Uint_0)); | |
1260 | ||
1261 | Analyze_And_Resolve (Op1, Standard_Integer); | |
1262 | Analyze_And_Resolve (Op2, Standard_Integer); | |
1263 | return; | |
1264 | end if; | |
1265 | ||
1266 | -- Cases where we cannot make runtime call | |
1267 | ||
70482933 RK |
1268 | -- For (a <= b) we convert to not (a > b) |
1269 | ||
1270 | if Chars (N) = Name_Op_Le then | |
1271 | Rewrite (N, | |
1272 | Make_Op_Not (Loc, | |
1273 | Right_Opnd => | |
1274 | Make_Op_Gt (Loc, | |
1275 | Left_Opnd => Op1, | |
1276 | Right_Opnd => Op2))); | |
1277 | Analyze_And_Resolve (N, Standard_Boolean); | |
1278 | return; | |
1279 | ||
1280 | -- For < the Boolean expression is | |
1281 | -- greater__nn (op2, op1) | |
1282 | ||
1283 | elsif Chars (N) = Name_Op_Lt then | |
1284 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1285 | ||
1286 | -- Switch operands | |
1287 | ||
1288 | Op1 := Right_Opnd (N); | |
1289 | Op2 := Left_Opnd (N); | |
1290 | ||
1291 | -- For (a >= b) we convert to not (a < b) | |
1292 | ||
1293 | elsif Chars (N) = Name_Op_Ge then | |
1294 | Rewrite (N, | |
1295 | Make_Op_Not (Loc, | |
1296 | Right_Opnd => | |
1297 | Make_Op_Lt (Loc, | |
1298 | Left_Opnd => Op1, | |
1299 | Right_Opnd => Op2))); | |
1300 | Analyze_And_Resolve (N, Standard_Boolean); | |
1301 | return; | |
1302 | ||
1303 | -- For > the Boolean expression is | |
1304 | -- greater__nn (op1, op2) | |
1305 | ||
1306 | else | |
1307 | pragma Assert (Chars (N) = Name_Op_Gt); | |
1308 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
1309 | end if; | |
1310 | ||
1311 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1312 | Expr := | |
1313 | Make_Function_Call (Loc, | |
1314 | Name => New_Reference_To (Func_Name, Loc), | |
1315 | Parameter_Associations => New_List (Op1, Op2)); | |
1316 | ||
1317 | Insert_Action (N, Func_Body); | |
1318 | Rewrite (N, Expr); | |
1319 | Analyze_And_Resolve (N, Standard_Boolean); | |
1320 | ||
fbf5a39b AC |
1321 | exception |
1322 | when RE_Not_Available => | |
1323 | return; | |
70482933 RK |
1324 | end Expand_Array_Comparison; |
1325 | ||
1326 | --------------------------- | |
1327 | -- Expand_Array_Equality -- | |
1328 | --------------------------- | |
1329 | ||
685094bf RD |
1330 | -- Expand an equality function for multi-dimensional arrays. Here is an |
1331 | -- example of such a function for Nb_Dimension = 2 | |
70482933 | 1332 | |
0da2c8ac | 1333 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 1334 | -- begin |
fbf5a39b AC |
1335 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
1336 | -- and then | |
1337 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
1338 | -- then | |
1339 | -- return True; -- RM 4.5.2(22) | |
1340 | -- end if; | |
0da2c8ac | 1341 | |
fbf5a39b AC |
1342 | -- if A'length (1) /= B'length (1) |
1343 | -- or else | |
1344 | -- A'length (2) /= B'length (2) | |
1345 | -- then | |
1346 | -- return False; -- RM 4.5.2(23) | |
1347 | -- end if; | |
0da2c8ac | 1348 | |
fbf5a39b | 1349 | -- declare |
523456db AC |
1350 | -- A1 : Index_T1 := A'first (1); |
1351 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 1352 | -- begin |
523456db | 1353 | -- loop |
fbf5a39b | 1354 | -- declare |
523456db AC |
1355 | -- A2 : Index_T2 := A'first (2); |
1356 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 1357 | -- begin |
523456db | 1358 | -- loop |
fbf5a39b AC |
1359 | -- if A (A1, A2) /= B (B1, B2) then |
1360 | -- return False; | |
70482933 | 1361 | -- end if; |
0da2c8ac | 1362 | |
523456db AC |
1363 | -- exit when A2 = A'last (2); |
1364 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 1365 | -- B2 := Index_T2'succ (B2); |
70482933 | 1366 | -- end loop; |
fbf5a39b | 1367 | -- end; |
0da2c8ac | 1368 | |
523456db AC |
1369 | -- exit when A1 = A'last (1); |
1370 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 1371 | -- B1 := Index_T1'succ (B1); |
70482933 | 1372 | -- end loop; |
fbf5a39b | 1373 | -- end; |
0da2c8ac | 1374 | |
70482933 RK |
1375 | -- return true; |
1376 | -- end Enn; | |
1377 | ||
685094bf RD |
1378 | -- Note on the formal types used (atyp and btyp). If either of the arrays |
1379 | -- is of a private type, we use the underlying type, and do an unchecked | |
1380 | -- conversion of the actual. If either of the arrays has a bound depending | |
1381 | -- on a discriminant, then we use the base type since otherwise we have an | |
1382 | -- escaped discriminant in the function. | |
0da2c8ac | 1383 | |
685094bf RD |
1384 | -- If both arrays are constrained and have the same bounds, we can generate |
1385 | -- a loop with an explicit iteration scheme using a 'Range attribute over | |
1386 | -- the first array. | |
523456db | 1387 | |
70482933 RK |
1388 | function Expand_Array_Equality |
1389 | (Nod : Node_Id; | |
70482933 RK |
1390 | Lhs : Node_Id; |
1391 | Rhs : Node_Id; | |
0da2c8ac AC |
1392 | Bodies : List_Id; |
1393 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
1394 | is |
1395 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
1396 | Decls : constant List_Id := New_List; |
1397 | Index_List1 : constant List_Id := New_List; | |
1398 | Index_List2 : constant List_Id := New_List; | |
1399 | ||
1400 | Actuals : List_Id; | |
1401 | Formals : List_Id; | |
1402 | Func_Name : Entity_Id; | |
1403 | Func_Body : Node_Id; | |
70482933 RK |
1404 | |
1405 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
1406 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
1407 | ||
0da2c8ac AC |
1408 | Ltyp : Entity_Id; |
1409 | Rtyp : Entity_Id; | |
1410 | -- The parameter types to be used for the formals | |
1411 | ||
fbf5a39b AC |
1412 | function Arr_Attr |
1413 | (Arr : Entity_Id; | |
1414 | Nam : Name_Id; | |
2e071734 | 1415 | Num : Int) return Node_Id; |
5e1c00fa | 1416 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 1417 | |
70482933 | 1418 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
685094bf RD |
1419 | -- Create one statement to compare corresponding components, designated |
1420 | -- by a full set of indices. | |
70482933 | 1421 | |
0da2c8ac | 1422 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
685094bf RD |
1423 | -- Given one of the arguments, computes the appropriate type to be used |
1424 | -- for that argument in the corresponding function formal | |
0da2c8ac | 1425 | |
fbf5a39b | 1426 | function Handle_One_Dimension |
70482933 | 1427 | (N : Int; |
2e071734 | 1428 | Index : Node_Id) return Node_Id; |
0da2c8ac | 1429 | -- This procedure returns the following code |
fbf5a39b AC |
1430 | -- |
1431 | -- declare | |
523456db | 1432 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 1433 | -- begin |
523456db | 1434 | -- loop |
fbf5a39b | 1435 | -- xxx |
523456db AC |
1436 | -- exit when An = A'Last (N); |
1437 | -- An := Index_T'Succ (An) | |
0da2c8ac | 1438 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
1439 | -- end loop; |
1440 | -- end; | |
1441 | -- | |
523456db AC |
1442 | -- If both indices are constrained and identical, the procedure |
1443 | -- returns a simpler loop: | |
1444 | -- | |
1445 | -- for An in A'Range (N) loop | |
1446 | -- xxx | |
1447 | -- end loop | |
0da2c8ac | 1448 | -- |
523456db | 1449 | -- N is the dimension for which we are generating a loop. Index is the |
685094bf RD |
1450 | -- N'th index node, whose Etype is Index_Type_n in the above code. The |
1451 | -- xxx statement is either the loop or declare for the next dimension | |
1452 | -- or if this is the last dimension the comparison of corresponding | |
1453 | -- components of the arrays. | |
fbf5a39b | 1454 | -- |
685094bf RD |
1455 | -- The actual way the code works is to return the comparison of |
1456 | -- corresponding components for the N+1 call. That's neater! | |
fbf5a39b AC |
1457 | |
1458 | function Test_Empty_Arrays return Node_Id; | |
1459 | -- This function constructs the test for both arrays being empty | |
1460 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1461 | -- and then | |
1462 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1463 | ||
1464 | function Test_Lengths_Correspond return Node_Id; | |
685094bf RD |
1465 | -- This function constructs the test for arrays having different lengths |
1466 | -- in at least one index position, in which case the resulting code is: | |
fbf5a39b AC |
1467 | |
1468 | -- A'length (1) /= B'length (1) | |
1469 | -- or else | |
1470 | -- A'length (2) /= B'length (2) | |
1471 | -- or else | |
1472 | -- ... | |
1473 | ||
1474 | -------------- | |
1475 | -- Arr_Attr -- | |
1476 | -------------- | |
1477 | ||
1478 | function Arr_Attr | |
1479 | (Arr : Entity_Id; | |
1480 | Nam : Name_Id; | |
2e071734 | 1481 | Num : Int) return Node_Id |
fbf5a39b AC |
1482 | is |
1483 | begin | |
1484 | return | |
1485 | Make_Attribute_Reference (Loc, | |
1486 | Attribute_Name => Nam, | |
1487 | Prefix => New_Reference_To (Arr, Loc), | |
1488 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
1489 | end Arr_Attr; | |
70482933 RK |
1490 | |
1491 | ------------------------ | |
1492 | -- Component_Equality -- | |
1493 | ------------------------ | |
1494 | ||
1495 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1496 | Test : Node_Id; | |
1497 | L, R : Node_Id; | |
1498 | ||
1499 | begin | |
1500 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1501 | ||
1502 | L := | |
1503 | Make_Indexed_Component (Loc, | |
1504 | Prefix => Make_Identifier (Loc, Chars (A)), | |
1505 | Expressions => Index_List1); | |
1506 | ||
1507 | R := | |
1508 | Make_Indexed_Component (Loc, | |
1509 | Prefix => Make_Identifier (Loc, Chars (B)), | |
1510 | Expressions => Index_List2); | |
1511 | ||
1512 | Test := Expand_Composite_Equality | |
1513 | (Nod, Component_Type (Typ), L, R, Decls); | |
1514 | ||
a9d8907c JM |
1515 | -- If some (sub)component is an unchecked_union, the whole operation |
1516 | -- will raise program error. | |
8aceda64 AC |
1517 | |
1518 | if Nkind (Test) = N_Raise_Program_Error then | |
a9d8907c JM |
1519 | |
1520 | -- This node is going to be inserted at a location where a | |
685094bf RD |
1521 | -- statement is expected: clear its Etype so analysis will set |
1522 | -- it to the expected Standard_Void_Type. | |
a9d8907c JM |
1523 | |
1524 | Set_Etype (Test, Empty); | |
8aceda64 AC |
1525 | return Test; |
1526 | ||
1527 | else | |
1528 | return | |
1529 | Make_Implicit_If_Statement (Nod, | |
1530 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), | |
1531 | Then_Statements => New_List ( | |
d766cee3 | 1532 | Make_Simple_Return_Statement (Loc, |
8aceda64 AC |
1533 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
1534 | end if; | |
70482933 RK |
1535 | end Component_Equality; |
1536 | ||
0da2c8ac AC |
1537 | ------------------ |
1538 | -- Get_Arg_Type -- | |
1539 | ------------------ | |
1540 | ||
1541 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1542 | T : Entity_Id; | |
1543 | X : Node_Id; | |
1544 | ||
1545 | begin | |
1546 | T := Etype (N); | |
1547 | ||
1548 | if No (T) then | |
1549 | return Typ; | |
1550 | ||
1551 | else | |
1552 | T := Underlying_Type (T); | |
1553 | ||
1554 | X := First_Index (T); | |
1555 | while Present (X) loop | |
1556 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) | |
1557 | or else | |
1558 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
1559 | then | |
1560 | T := Base_Type (T); | |
1561 | exit; | |
1562 | end if; | |
1563 | ||
1564 | Next_Index (X); | |
1565 | end loop; | |
1566 | ||
1567 | return T; | |
1568 | end if; | |
1569 | end Get_Arg_Type; | |
1570 | ||
fbf5a39b AC |
1571 | -------------------------- |
1572 | -- Handle_One_Dimension -- | |
1573 | --------------------------- | |
70482933 | 1574 | |
fbf5a39b | 1575 | function Handle_One_Dimension |
70482933 | 1576 | (N : Int; |
2e071734 | 1577 | Index : Node_Id) return Node_Id |
70482933 | 1578 | is |
0da2c8ac AC |
1579 | Need_Separate_Indexes : constant Boolean := |
1580 | Ltyp /= Rtyp | |
1581 | or else not Is_Constrained (Ltyp); | |
1582 | -- If the index types are identical, and we are working with | |
685094bf RD |
1583 | -- constrained types, then we can use the same index for both |
1584 | -- of the arrays. | |
0da2c8ac | 1585 | |
191fcb3a | 1586 | An : constant Entity_Id := Make_Temporary (Loc, 'A'); |
0da2c8ac AC |
1587 | |
1588 | Bn : Entity_Id; | |
1589 | Index_T : Entity_Id; | |
1590 | Stm_List : List_Id; | |
1591 | Loop_Stm : Node_Id; | |
70482933 RK |
1592 | |
1593 | begin | |
0da2c8ac AC |
1594 | if N > Number_Dimensions (Ltyp) then |
1595 | return Component_Equality (Ltyp); | |
fbf5a39b | 1596 | end if; |
70482933 | 1597 | |
0da2c8ac AC |
1598 | -- Case where we generate a loop |
1599 | ||
1600 | Index_T := Base_Type (Etype (Index)); | |
1601 | ||
1602 | if Need_Separate_Indexes then | |
191fcb3a | 1603 | Bn := Make_Temporary (Loc, 'B'); |
0da2c8ac AC |
1604 | else |
1605 | Bn := An; | |
1606 | end if; | |
70482933 | 1607 | |
fbf5a39b AC |
1608 | Append (New_Reference_To (An, Loc), Index_List1); |
1609 | Append (New_Reference_To (Bn, Loc), Index_List2); | |
70482933 | 1610 | |
0da2c8ac AC |
1611 | Stm_List := New_List ( |
1612 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1613 | |
0da2c8ac | 1614 | if Need_Separate_Indexes then |
a9d8907c | 1615 | |
5e1c00fa | 1616 | -- Generate guard for loop, followed by increments of indices |
523456db AC |
1617 | |
1618 | Append_To (Stm_List, | |
1619 | Make_Exit_Statement (Loc, | |
1620 | Condition => | |
1621 | Make_Op_Eq (Loc, | |
1622 | Left_Opnd => New_Reference_To (An, Loc), | |
1623 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); | |
1624 | ||
1625 | Append_To (Stm_List, | |
1626 | Make_Assignment_Statement (Loc, | |
1627 | Name => New_Reference_To (An, Loc), | |
1628 | Expression => | |
1629 | Make_Attribute_Reference (Loc, | |
1630 | Prefix => New_Reference_To (Index_T, Loc), | |
1631 | Attribute_Name => Name_Succ, | |
1632 | Expressions => New_List (New_Reference_To (An, Loc))))); | |
1633 | ||
0da2c8ac AC |
1634 | Append_To (Stm_List, |
1635 | Make_Assignment_Statement (Loc, | |
1636 | Name => New_Reference_To (Bn, Loc), | |
1637 | Expression => | |
1638 | Make_Attribute_Reference (Loc, | |
1639 | Prefix => New_Reference_To (Index_T, Loc), | |
1640 | Attribute_Name => Name_Succ, | |
1641 | Expressions => New_List (New_Reference_To (Bn, Loc))))); | |
1642 | end if; | |
1643 | ||
a9d8907c JM |
1644 | -- If separate indexes, we need a declare block for An and Bn, and a |
1645 | -- loop without an iteration scheme. | |
0da2c8ac AC |
1646 | |
1647 | if Need_Separate_Indexes then | |
523456db AC |
1648 | Loop_Stm := |
1649 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
1650 | ||
0da2c8ac AC |
1651 | return |
1652 | Make_Block_Statement (Loc, | |
1653 | Declarations => New_List ( | |
523456db AC |
1654 | Make_Object_Declaration (Loc, |
1655 | Defining_Identifier => An, | |
1656 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1657 | Expression => Arr_Attr (A, Name_First, N)), | |
1658 | ||
0da2c8ac AC |
1659 | Make_Object_Declaration (Loc, |
1660 | Defining_Identifier => Bn, | |
1661 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1662 | Expression => Arr_Attr (B, Name_First, N))), | |
523456db | 1663 | |
0da2c8ac AC |
1664 | Handled_Statement_Sequence => |
1665 | Make_Handled_Sequence_Of_Statements (Loc, | |
1666 | Statements => New_List (Loop_Stm))); | |
1667 | ||
523456db AC |
1668 | -- If no separate indexes, return loop statement with explicit |
1669 | -- iteration scheme on its own | |
0da2c8ac AC |
1670 | |
1671 | else | |
523456db AC |
1672 | Loop_Stm := |
1673 | Make_Implicit_Loop_Statement (Nod, | |
1674 | Statements => Stm_List, | |
1675 | Iteration_Scheme => | |
1676 | Make_Iteration_Scheme (Loc, | |
1677 | Loop_Parameter_Specification => | |
1678 | Make_Loop_Parameter_Specification (Loc, | |
1679 | Defining_Identifier => An, | |
1680 | Discrete_Subtype_Definition => | |
1681 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
1682 | return Loop_Stm; |
1683 | end if; | |
fbf5a39b AC |
1684 | end Handle_One_Dimension; |
1685 | ||
1686 | ----------------------- | |
1687 | -- Test_Empty_Arrays -- | |
1688 | ----------------------- | |
1689 | ||
1690 | function Test_Empty_Arrays return Node_Id is | |
1691 | Alist : Node_Id; | |
1692 | Blist : Node_Id; | |
1693 | ||
1694 | Atest : Node_Id; | |
1695 | Btest : Node_Id; | |
70482933 | 1696 | |
fbf5a39b AC |
1697 | begin |
1698 | Alist := Empty; | |
1699 | Blist := Empty; | |
0da2c8ac | 1700 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1701 | Atest := |
1702 | Make_Op_Eq (Loc, | |
1703 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1704 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1705 | ||
1706 | Btest := | |
1707 | Make_Op_Eq (Loc, | |
1708 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
1709 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1710 | ||
1711 | if No (Alist) then | |
1712 | Alist := Atest; | |
1713 | Blist := Btest; | |
70482933 | 1714 | |
fbf5a39b AC |
1715 | else |
1716 | Alist := | |
1717 | Make_Or_Else (Loc, | |
1718 | Left_Opnd => Relocate_Node (Alist), | |
1719 | Right_Opnd => Atest); | |
1720 | ||
1721 | Blist := | |
1722 | Make_Or_Else (Loc, | |
1723 | Left_Opnd => Relocate_Node (Blist), | |
1724 | Right_Opnd => Btest); | |
1725 | end if; | |
1726 | end loop; | |
70482933 | 1727 | |
fbf5a39b AC |
1728 | return |
1729 | Make_And_Then (Loc, | |
1730 | Left_Opnd => Alist, | |
1731 | Right_Opnd => Blist); | |
1732 | end Test_Empty_Arrays; | |
70482933 | 1733 | |
fbf5a39b AC |
1734 | ----------------------------- |
1735 | -- Test_Lengths_Correspond -- | |
1736 | ----------------------------- | |
70482933 | 1737 | |
fbf5a39b AC |
1738 | function Test_Lengths_Correspond return Node_Id is |
1739 | Result : Node_Id; | |
1740 | Rtest : Node_Id; | |
1741 | ||
1742 | begin | |
1743 | Result := Empty; | |
0da2c8ac | 1744 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1745 | Rtest := |
1746 | Make_Op_Ne (Loc, | |
1747 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1748 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
1749 | ||
1750 | if No (Result) then | |
1751 | Result := Rtest; | |
1752 | else | |
1753 | Result := | |
1754 | Make_Or_Else (Loc, | |
1755 | Left_Opnd => Relocate_Node (Result), | |
1756 | Right_Opnd => Rtest); | |
1757 | end if; | |
1758 | end loop; | |
1759 | ||
1760 | return Result; | |
1761 | end Test_Lengths_Correspond; | |
70482933 RK |
1762 | |
1763 | -- Start of processing for Expand_Array_Equality | |
1764 | ||
1765 | begin | |
0da2c8ac AC |
1766 | Ltyp := Get_Arg_Type (Lhs); |
1767 | Rtyp := Get_Arg_Type (Rhs); | |
1768 | ||
685094bf RD |
1769 | -- For now, if the argument types are not the same, go to the base type, |
1770 | -- since the code assumes that the formals have the same type. This is | |
1771 | -- fixable in future ??? | |
0da2c8ac AC |
1772 | |
1773 | if Ltyp /= Rtyp then | |
1774 | Ltyp := Base_Type (Ltyp); | |
1775 | Rtyp := Base_Type (Rtyp); | |
1776 | pragma Assert (Ltyp = Rtyp); | |
1777 | end if; | |
1778 | ||
1779 | -- Build list of formals for function | |
1780 | ||
70482933 RK |
1781 | Formals := New_List ( |
1782 | Make_Parameter_Specification (Loc, | |
1783 | Defining_Identifier => A, | |
0da2c8ac | 1784 | Parameter_Type => New_Reference_To (Ltyp, Loc)), |
70482933 RK |
1785 | |
1786 | Make_Parameter_Specification (Loc, | |
1787 | Defining_Identifier => B, | |
0da2c8ac | 1788 | Parameter_Type => New_Reference_To (Rtyp, Loc))); |
70482933 | 1789 | |
191fcb3a | 1790 | Func_Name := Make_Temporary (Loc, 'E'); |
70482933 | 1791 | |
fbf5a39b | 1792 | -- Build statement sequence for function |
70482933 RK |
1793 | |
1794 | Func_Body := | |
1795 | Make_Subprogram_Body (Loc, | |
1796 | Specification => | |
1797 | Make_Function_Specification (Loc, | |
1798 | Defining_Unit_Name => Func_Name, | |
1799 | Parameter_Specifications => Formals, | |
630d30e9 | 1800 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
fbf5a39b AC |
1801 | |
1802 | Declarations => Decls, | |
1803 | ||
70482933 RK |
1804 | Handled_Statement_Sequence => |
1805 | Make_Handled_Sequence_Of_Statements (Loc, | |
1806 | Statements => New_List ( | |
fbf5a39b AC |
1807 | |
1808 | Make_Implicit_If_Statement (Nod, | |
1809 | Condition => Test_Empty_Arrays, | |
1810 | Then_Statements => New_List ( | |
d766cee3 | 1811 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
1812 | Expression => |
1813 | New_Occurrence_Of (Standard_True, Loc)))), | |
1814 | ||
1815 | Make_Implicit_If_Statement (Nod, | |
1816 | Condition => Test_Lengths_Correspond, | |
1817 | Then_Statements => New_List ( | |
d766cee3 | 1818 | Make_Simple_Return_Statement (Loc, |
fbf5a39b AC |
1819 | Expression => |
1820 | New_Occurrence_Of (Standard_False, Loc)))), | |
1821 | ||
0da2c8ac | 1822 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 1823 | |
d766cee3 | 1824 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
1825 | Expression => New_Occurrence_Of (Standard_True, Loc))))); |
1826 | ||
1827 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 1828 | Set_Is_Inlined (Func_Name); |
70482933 | 1829 | |
685094bf RD |
1830 | -- If the array type is distinct from the type of the arguments, it |
1831 | -- is the full view of a private type. Apply an unchecked conversion | |
1832 | -- to insure that analysis of the call succeeds. | |
70482933 | 1833 | |
0da2c8ac AC |
1834 | declare |
1835 | L, R : Node_Id; | |
1836 | ||
1837 | begin | |
1838 | L := Lhs; | |
1839 | R := Rhs; | |
1840 | ||
1841 | if No (Etype (Lhs)) | |
1842 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
1843 | then | |
1844 | L := OK_Convert_To (Ltyp, Lhs); | |
1845 | end if; | |
1846 | ||
1847 | if No (Etype (Rhs)) | |
1848 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
1849 | then | |
1850 | R := OK_Convert_To (Rtyp, Rhs); | |
1851 | end if; | |
1852 | ||
1853 | Actuals := New_List (L, R); | |
1854 | end; | |
70482933 RK |
1855 | |
1856 | Append_To (Bodies, Func_Body); | |
1857 | ||
1858 | return | |
1859 | Make_Function_Call (Loc, | |
0da2c8ac | 1860 | Name => New_Reference_To (Func_Name, Loc), |
70482933 RK |
1861 | Parameter_Associations => Actuals); |
1862 | end Expand_Array_Equality; | |
1863 | ||
1864 | ----------------------------- | |
1865 | -- Expand_Boolean_Operator -- | |
1866 | ----------------------------- | |
1867 | ||
685094bf RD |
1868 | -- Note that we first get the actual subtypes of the operands, since we |
1869 | -- always want to deal with types that have bounds. | |
70482933 RK |
1870 | |
1871 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 1872 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
1873 | |
1874 | begin | |
685094bf RD |
1875 | -- Special case of bit packed array where both operands are known to be |
1876 | -- properly aligned. In this case we use an efficient run time routine | |
1877 | -- to carry out the operation (see System.Bit_Ops). | |
a9d8907c JM |
1878 | |
1879 | if Is_Bit_Packed_Array (Typ) | |
1880 | and then not Is_Possibly_Unaligned_Object (Left_Opnd (N)) | |
1881 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
1882 | then | |
70482933 | 1883 | Expand_Packed_Boolean_Operator (N); |
a9d8907c JM |
1884 | return; |
1885 | end if; | |
70482933 | 1886 | |
a9d8907c JM |
1887 | -- For the normal non-packed case, the general expansion is to build |
1888 | -- function for carrying out the comparison (use Make_Boolean_Array_Op) | |
1889 | -- and then inserting it into the tree. The original operator node is | |
1890 | -- then rewritten as a call to this function. We also use this in the | |
1891 | -- packed case if either operand is a possibly unaligned object. | |
70482933 | 1892 | |
a9d8907c JM |
1893 | declare |
1894 | Loc : constant Source_Ptr := Sloc (N); | |
1895 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
1896 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
1897 | Func_Body : Node_Id; | |
1898 | Func_Name : Entity_Id; | |
fbf5a39b | 1899 | |
a9d8907c JM |
1900 | begin |
1901 | Convert_To_Actual_Subtype (L); | |
1902 | Convert_To_Actual_Subtype (R); | |
1903 | Ensure_Defined (Etype (L), N); | |
1904 | Ensure_Defined (Etype (R), N); | |
1905 | Apply_Length_Check (R, Etype (L)); | |
1906 | ||
b4592168 GD |
1907 | if Nkind (N) = N_Op_Xor then |
1908 | Silly_Boolean_Array_Xor_Test (N, Etype (L)); | |
1909 | end if; | |
1910 | ||
a9d8907c JM |
1911 | if Nkind (Parent (N)) = N_Assignment_Statement |
1912 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
1913 | then | |
1914 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
fbf5a39b | 1915 | |
a9d8907c JM |
1916 | elsif Nkind (Parent (N)) = N_Op_Not |
1917 | and then Nkind (N) = N_Op_And | |
1918 | and then | |
b4592168 | 1919 | Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) |
a9d8907c JM |
1920 | then |
1921 | return; | |
1922 | else | |
fbf5a39b | 1923 | |
a9d8907c JM |
1924 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); |
1925 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1926 | Insert_Action (N, Func_Body); | |
70482933 | 1927 | |
a9d8907c | 1928 | -- Now rewrite the expression with a call |
70482933 | 1929 | |
a9d8907c JM |
1930 | Rewrite (N, |
1931 | Make_Function_Call (Loc, | |
1932 | Name => New_Reference_To (Func_Name, Loc), | |
1933 | Parameter_Associations => | |
1934 | New_List ( | |
1935 | L, | |
1936 | Make_Type_Conversion | |
1937 | (Loc, New_Reference_To (Etype (L), Loc), R)))); | |
70482933 | 1938 | |
a9d8907c JM |
1939 | Analyze_And_Resolve (N, Typ); |
1940 | end if; | |
1941 | end; | |
70482933 RK |
1942 | end Expand_Boolean_Operator; |
1943 | ||
1944 | ------------------------------- | |
1945 | -- Expand_Composite_Equality -- | |
1946 | ------------------------------- | |
1947 | ||
1948 | -- This function is only called for comparing internal fields of composite | |
1949 | -- types when these fields are themselves composites. This is a special | |
1950 | -- case because it is not possible to respect normal Ada visibility rules. | |
1951 | ||
1952 | function Expand_Composite_Equality | |
1953 | (Nod : Node_Id; | |
1954 | Typ : Entity_Id; | |
1955 | Lhs : Node_Id; | |
1956 | Rhs : Node_Id; | |
2e071734 | 1957 | Bodies : List_Id) return Node_Id |
70482933 RK |
1958 | is |
1959 | Loc : constant Source_Ptr := Sloc (Nod); | |
1960 | Full_Type : Entity_Id; | |
1961 | Prim : Elmt_Id; | |
1962 | Eq_Op : Entity_Id; | |
1963 | ||
1964 | begin | |
1965 | if Is_Private_Type (Typ) then | |
1966 | Full_Type := Underlying_Type (Typ); | |
1967 | else | |
1968 | Full_Type := Typ; | |
1969 | end if; | |
1970 | ||
685094bf RD |
1971 | -- Defense against malformed private types with no completion the error |
1972 | -- will be diagnosed later by check_completion | |
70482933 RK |
1973 | |
1974 | if No (Full_Type) then | |
1975 | return New_Reference_To (Standard_False, Loc); | |
1976 | end if; | |
1977 | ||
1978 | Full_Type := Base_Type (Full_Type); | |
1979 | ||
1980 | if Is_Array_Type (Full_Type) then | |
1981 | ||
1982 | -- If the operand is an elementary type other than a floating-point | |
1983 | -- type, then we can simply use the built-in block bitwise equality, | |
1984 | -- since the predefined equality operators always apply and bitwise | |
1985 | -- equality is fine for all these cases. | |
1986 | ||
1987 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
1988 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
1989 | then | |
1990 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
1991 | ||
685094bf RD |
1992 | -- For composite component types, and floating-point types, use the |
1993 | -- expansion. This deals with tagged component types (where we use | |
1994 | -- the applicable equality routine) and floating-point, (where we | |
1995 | -- need to worry about negative zeroes), and also the case of any | |
1996 | -- composite type recursively containing such fields. | |
70482933 RK |
1997 | |
1998 | else | |
0da2c8ac | 1999 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
2000 | end if; |
2001 | ||
2002 | elsif Is_Tagged_Type (Full_Type) then | |
2003 | ||
2004 | -- Call the primitive operation "=" of this type | |
2005 | ||
2006 | if Is_Class_Wide_Type (Full_Type) then | |
2007 | Full_Type := Root_Type (Full_Type); | |
2008 | end if; | |
2009 | ||
685094bf RD |
2010 | -- If this is derived from an untagged private type completed with a |
2011 | -- tagged type, it does not have a full view, so we use the primitive | |
2012 | -- operations of the private type. This check should no longer be | |
2013 | -- necessary when these types receive their full views ??? | |
70482933 RK |
2014 | |
2015 | if Is_Private_Type (Typ) | |
2016 | and then not Is_Tagged_Type (Typ) | |
2017 | and then not Is_Controlled (Typ) | |
2018 | and then Is_Derived_Type (Typ) | |
2019 | and then No (Full_View (Typ)) | |
2020 | then | |
2021 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
2022 | else | |
2023 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
2024 | end if; | |
2025 | ||
2026 | loop | |
2027 | Eq_Op := Node (Prim); | |
2028 | exit when Chars (Eq_Op) = Name_Op_Eq | |
2029 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
2030 | Etype (Next_Formal (First_Formal (Eq_Op))) |
2031 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
2032 | Next_Elmt (Prim); |
2033 | pragma Assert (Present (Prim)); | |
2034 | end loop; | |
2035 | ||
2036 | Eq_Op := Node (Prim); | |
2037 | ||
2038 | return | |
2039 | Make_Function_Call (Loc, | |
2040 | Name => New_Reference_To (Eq_Op, Loc), | |
2041 | Parameter_Associations => | |
2042 | New_List | |
2043 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
2044 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
2045 | ||
2046 | elsif Is_Record_Type (Full_Type) then | |
fbf5a39b | 2047 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
2048 | |
2049 | if Present (Eq_Op) then | |
2050 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
2051 | ||
685094bf RD |
2052 | -- Inherited equality from parent type. Convert the actuals to |
2053 | -- match signature of operation. | |
70482933 RK |
2054 | |
2055 | declare | |
fbf5a39b | 2056 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
2057 | |
2058 | begin | |
2059 | return | |
2060 | Make_Function_Call (Loc, | |
2061 | Name => New_Reference_To (Eq_Op, Loc), | |
2062 | Parameter_Associations => | |
2063 | New_List (OK_Convert_To (T, Lhs), | |
2064 | OK_Convert_To (T, Rhs))); | |
2065 | end; | |
2066 | ||
2067 | else | |
5d09245e AC |
2068 | -- Comparison between Unchecked_Union components |
2069 | ||
2070 | if Is_Unchecked_Union (Full_Type) then | |
2071 | declare | |
2072 | Lhs_Type : Node_Id := Full_Type; | |
2073 | Rhs_Type : Node_Id := Full_Type; | |
2074 | Lhs_Discr_Val : Node_Id; | |
2075 | Rhs_Discr_Val : Node_Id; | |
2076 | ||
2077 | begin | |
2078 | -- Lhs subtype | |
2079 | ||
2080 | if Nkind (Lhs) = N_Selected_Component then | |
2081 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
2082 | end if; | |
2083 | ||
2084 | -- Rhs subtype | |
2085 | ||
2086 | if Nkind (Rhs) = N_Selected_Component then | |
2087 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
2088 | end if; | |
2089 | ||
2090 | -- Lhs of the composite equality | |
2091 | ||
2092 | if Is_Constrained (Lhs_Type) then | |
2093 | ||
685094bf | 2094 | -- Since the enclosing record type can never be an |
5d09245e AC |
2095 | -- Unchecked_Union (this code is executed for records |
2096 | -- that do not have variants), we may reference its | |
2097 | -- discriminant(s). | |
2098 | ||
2099 | if Nkind (Lhs) = N_Selected_Component | |
2100 | and then Has_Per_Object_Constraint ( | |
2101 | Entity (Selector_Name (Lhs))) | |
2102 | then | |
2103 | Lhs_Discr_Val := | |
2104 | Make_Selected_Component (Loc, | |
2105 | Prefix => Prefix (Lhs), | |
2106 | Selector_Name => | |
2107 | New_Copy ( | |
2108 | Get_Discriminant_Value ( | |
2109 | First_Discriminant (Lhs_Type), | |
2110 | Lhs_Type, | |
2111 | Stored_Constraint (Lhs_Type)))); | |
2112 | ||
2113 | else | |
2114 | Lhs_Discr_Val := New_Copy ( | |
2115 | Get_Discriminant_Value ( | |
2116 | First_Discriminant (Lhs_Type), | |
2117 | Lhs_Type, | |
2118 | Stored_Constraint (Lhs_Type))); | |
2119 | ||
2120 | end if; | |
2121 | else | |
2122 | -- It is not possible to infer the discriminant since | |
2123 | -- the subtype is not constrained. | |
2124 | ||
8aceda64 | 2125 | return |
5d09245e | 2126 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2127 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2128 | end if; |
2129 | ||
2130 | -- Rhs of the composite equality | |
2131 | ||
2132 | if Is_Constrained (Rhs_Type) then | |
2133 | if Nkind (Rhs) = N_Selected_Component | |
2134 | and then Has_Per_Object_Constraint ( | |
2135 | Entity (Selector_Name (Rhs))) | |
2136 | then | |
2137 | Rhs_Discr_Val := | |
2138 | Make_Selected_Component (Loc, | |
2139 | Prefix => Prefix (Rhs), | |
2140 | Selector_Name => | |
2141 | New_Copy ( | |
2142 | Get_Discriminant_Value ( | |
2143 | First_Discriminant (Rhs_Type), | |
2144 | Rhs_Type, | |
2145 | Stored_Constraint (Rhs_Type)))); | |
2146 | ||
2147 | else | |
2148 | Rhs_Discr_Val := New_Copy ( | |
2149 | Get_Discriminant_Value ( | |
2150 | First_Discriminant (Rhs_Type), | |
2151 | Rhs_Type, | |
2152 | Stored_Constraint (Rhs_Type))); | |
2153 | ||
2154 | end if; | |
2155 | else | |
8aceda64 | 2156 | return |
5d09245e | 2157 | Make_Raise_Program_Error (Loc, |
8aceda64 | 2158 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
2159 | end if; |
2160 | ||
2161 | -- Call the TSS equality function with the inferred | |
2162 | -- discriminant values. | |
2163 | ||
2164 | return | |
2165 | Make_Function_Call (Loc, | |
2166 | Name => New_Reference_To (Eq_Op, Loc), | |
2167 | Parameter_Associations => New_List ( | |
2168 | Lhs, | |
2169 | Rhs, | |
2170 | Lhs_Discr_Val, | |
2171 | Rhs_Discr_Val)); | |
2172 | end; | |
2173 | end if; | |
2174 | ||
685094bf RD |
2175 | -- Shouldn't this be an else, we can't fall through the above |
2176 | -- IF, right??? | |
5d09245e | 2177 | |
70482933 RK |
2178 | return |
2179 | Make_Function_Call (Loc, | |
2180 | Name => New_Reference_To (Eq_Op, Loc), | |
2181 | Parameter_Associations => New_List (Lhs, Rhs)); | |
2182 | end if; | |
2183 | ||
2184 | else | |
2185 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
2186 | end if; | |
2187 | ||
2188 | else | |
2189 | -- It can be a simple record or the full view of a scalar private | |
2190 | ||
2191 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
2192 | end if; | |
2193 | end Expand_Composite_Equality; | |
2194 | ||
fdac1f80 AC |
2195 | ------------------------ |
2196 | -- Expand_Concatenate -- | |
2197 | ------------------------ | |
70482933 | 2198 | |
fdac1f80 AC |
2199 | procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is |
2200 | Loc : constant Source_Ptr := Sloc (Cnode); | |
70482933 | 2201 | |
fdac1f80 AC |
2202 | Atyp : constant Entity_Id := Base_Type (Etype (Cnode)); |
2203 | -- Result type of concatenation | |
70482933 | 2204 | |
fdac1f80 AC |
2205 | Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode))); |
2206 | -- Component type. Elements of this component type can appear as one | |
2207 | -- of the operands of concatenation as well as arrays. | |
70482933 | 2208 | |
ecc4ddde AC |
2209 | Istyp : constant Entity_Id := Etype (First_Index (Atyp)); |
2210 | -- Index subtype | |
2211 | ||
2212 | Ityp : constant Entity_Id := Base_Type (Istyp); | |
2213 | -- Index type. This is the base type of the index subtype, and is used | |
2214 | -- for all computed bounds (which may be out of range of Istyp in the | |
2215 | -- case of null ranges). | |
70482933 | 2216 | |
46ff89f3 | 2217 | Artyp : Entity_Id; |
fdac1f80 AC |
2218 | -- This is the type we use to do arithmetic to compute the bounds and |
2219 | -- lengths of operands. The choice of this type is a little subtle and | |
2220 | -- is discussed in a separate section at the start of the body code. | |
70482933 | 2221 | |
fdac1f80 AC |
2222 | Concatenation_Error : exception; |
2223 | -- Raised if concatenation is sure to raise a CE | |
70482933 | 2224 | |
0ac73189 AC |
2225 | Result_May_Be_Null : Boolean := True; |
2226 | -- Reset to False if at least one operand is encountered which is known | |
2227 | -- at compile time to be non-null. Used for handling the special case | |
2228 | -- of setting the high bound to the last operand high bound for a null | |
2229 | -- result, thus ensuring a proper high bound in the super-flat case. | |
2230 | ||
df46b832 | 2231 | N : constant Nat := List_Length (Opnds); |
fdac1f80 | 2232 | -- Number of concatenation operands including possibly null operands |
df46b832 AC |
2233 | |
2234 | NN : Nat := 0; | |
a29262fd AC |
2235 | -- Number of operands excluding any known to be null, except that the |
2236 | -- last operand is always retained, in case it provides the bounds for | |
2237 | -- a null result. | |
2238 | ||
2239 | Opnd : Node_Id; | |
2240 | -- Current operand being processed in the loop through operands. After | |
2241 | -- this loop is complete, always contains the last operand (which is not | |
2242 | -- the same as Operands (NN), since null operands are skipped). | |
df46b832 AC |
2243 | |
2244 | -- Arrays describing the operands, only the first NN entries of each | |
2245 | -- array are set (NN < N when we exclude known null operands). | |
2246 | ||
2247 | Is_Fixed_Length : array (1 .. N) of Boolean; | |
2248 | -- True if length of corresponding operand known at compile time | |
2249 | ||
2250 | Operands : array (1 .. N) of Node_Id; | |
a29262fd AC |
2251 | -- Set to the corresponding entry in the Opnds list (but note that null |
2252 | -- operands are excluded, so not all entries in the list are stored). | |
df46b832 AC |
2253 | |
2254 | Fixed_Length : array (1 .. N) of Uint; | |
fdac1f80 AC |
2255 | -- Set to length of operand. Entries in this array are set only if the |
2256 | -- corresponding entry in Is_Fixed_Length is True. | |
df46b832 | 2257 | |
0ac73189 AC |
2258 | Opnd_Low_Bound : array (1 .. N) of Node_Id; |
2259 | -- Set to lower bound of operand. Either an integer literal in the case | |
2260 | -- where the bound is known at compile time, else actual lower bound. | |
2261 | -- The operand low bound is of type Ityp. | |
2262 | ||
df46b832 AC |
2263 | Var_Length : array (1 .. N) of Entity_Id; |
2264 | -- Set to an entity of type Natural that contains the length of an | |
2265 | -- operand whose length is not known at compile time. Entries in this | |
2266 | -- array are set only if the corresponding entry in Is_Fixed_Length | |
46ff89f3 | 2267 | -- is False. The entity is of type Artyp. |
df46b832 AC |
2268 | |
2269 | Aggr_Length : array (0 .. N) of Node_Id; | |
fdac1f80 AC |
2270 | -- The J'th entry in an expression node that represents the total length |
2271 | -- of operands 1 through J. It is either an integer literal node, or a | |
2272 | -- reference to a constant entity with the right value, so it is fine | |
2273 | -- to just do a Copy_Node to get an appropriate copy. The extra zero'th | |
46ff89f3 | 2274 | -- entry always is set to zero. The length is of type Artyp. |
df46b832 AC |
2275 | |
2276 | Low_Bound : Node_Id; | |
0ac73189 AC |
2277 | -- A tree node representing the low bound of the result (of type Ityp). |
2278 | -- This is either an integer literal node, or an identifier reference to | |
2279 | -- a constant entity initialized to the appropriate value. | |
2280 | ||
a29262fd AC |
2281 | Last_Opnd_High_Bound : Node_Id; |
2282 | -- A tree node representing the high bound of the last operand. This | |
2283 | -- need only be set if the result could be null. It is used for the | |
2284 | -- special case of setting the right high bound for a null result. | |
2285 | -- This is of type Ityp. | |
2286 | ||
0ac73189 AC |
2287 | High_Bound : Node_Id; |
2288 | -- A tree node representing the high bound of the result (of type Ityp) | |
df46b832 AC |
2289 | |
2290 | Result : Node_Id; | |
0ac73189 | 2291 | -- Result of the concatenation (of type Ityp) |
df46b832 | 2292 | |
d0f8d157 AC |
2293 | Actions : constant List_Id := New_List; |
2294 | -- Collect actions to be inserted if Save_Space is False | |
2295 | ||
2296 | Save_Space : Boolean; | |
2297 | pragma Warnings (Off, Save_Space); | |
2298 | -- Set to True if we are saving generated code space by calling routines | |
2299 | -- in packages System.Concat_n. | |
2300 | ||
fa969310 AC |
2301 | Known_Non_Null_Operand_Seen : Boolean; |
2302 | -- Set True during generation of the assignements of operands into | |
2303 | -- result once an operand known to be non-null has been seen. | |
2304 | ||
2305 | function Make_Artyp_Literal (Val : Nat) return Node_Id; | |
2306 | -- This function makes an N_Integer_Literal node that is returned in | |
2307 | -- analyzed form with the type set to Artyp. Importantly this literal | |
2308 | -- is not flagged as static, so that if we do computations with it that | |
2309 | -- result in statically detected out of range conditions, we will not | |
2310 | -- generate error messages but instead warning messages. | |
2311 | ||
46ff89f3 | 2312 | function To_Artyp (X : Node_Id) return Node_Id; |
fdac1f80 | 2313 | -- Given a node of type Ityp, returns the corresponding value of type |
76c597a1 AC |
2314 | -- Artyp. For non-enumeration types, this is a plain integer conversion. |
2315 | -- For enum types, the Pos of the value is returned. | |
fdac1f80 AC |
2316 | |
2317 | function To_Ityp (X : Node_Id) return Node_Id; | |
0ac73189 | 2318 | -- The inverse function (uses Val in the case of enumeration types) |
fdac1f80 | 2319 | |
fa969310 AC |
2320 | ------------------------ |
2321 | -- Make_Artyp_Literal -- | |
2322 | ------------------------ | |
2323 | ||
2324 | function Make_Artyp_Literal (Val : Nat) return Node_Id is | |
2325 | Result : constant Node_Id := Make_Integer_Literal (Loc, Val); | |
2326 | begin | |
2327 | Set_Etype (Result, Artyp); | |
2328 | Set_Analyzed (Result, True); | |
2329 | Set_Is_Static_Expression (Result, False); | |
2330 | return Result; | |
2331 | end Make_Artyp_Literal; | |
76c597a1 | 2332 | |
fdac1f80 | 2333 | -------------- |
46ff89f3 | 2334 | -- To_Artyp -- |
fdac1f80 AC |
2335 | -------------- |
2336 | ||
46ff89f3 | 2337 | function To_Artyp (X : Node_Id) return Node_Id is |
fdac1f80 | 2338 | begin |
46ff89f3 | 2339 | if Ityp = Base_Type (Artyp) then |
fdac1f80 AC |
2340 | return X; |
2341 | ||
2342 | elsif Is_Enumeration_Type (Ityp) then | |
2343 | return | |
2344 | Make_Attribute_Reference (Loc, | |
2345 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2346 | Attribute_Name => Name_Pos, | |
2347 | Expressions => New_List (X)); | |
2348 | ||
2349 | else | |
46ff89f3 | 2350 | return Convert_To (Artyp, X); |
fdac1f80 | 2351 | end if; |
46ff89f3 | 2352 | end To_Artyp; |
fdac1f80 AC |
2353 | |
2354 | ------------- | |
2355 | -- To_Ityp -- | |
2356 | ------------- | |
2357 | ||
2358 | function To_Ityp (X : Node_Id) return Node_Id is | |
2359 | begin | |
2fc05e3d | 2360 | if Is_Enumeration_Type (Ityp) then |
fdac1f80 AC |
2361 | return |
2362 | Make_Attribute_Reference (Loc, | |
2363 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
2364 | Attribute_Name => Name_Val, | |
2365 | Expressions => New_List (X)); | |
2366 | ||
2367 | -- Case where we will do a type conversion | |
2368 | ||
2369 | else | |
76c597a1 AC |
2370 | if Ityp = Base_Type (Artyp) then |
2371 | return X; | |
fdac1f80 | 2372 | else |
76c597a1 | 2373 | return Convert_To (Ityp, X); |
fdac1f80 AC |
2374 | end if; |
2375 | end if; | |
2376 | end To_Ityp; | |
2377 | ||
2378 | -- Local Declarations | |
2379 | ||
0ac73189 AC |
2380 | Opnd_Typ : Entity_Id; |
2381 | Ent : Entity_Id; | |
2382 | Len : Uint; | |
2383 | J : Nat; | |
2384 | Clen : Node_Id; | |
2385 | Set : Boolean; | |
70482933 RK |
2386 | |
2387 | begin | |
fdac1f80 AC |
2388 | -- Choose an appropriate computational type |
2389 | ||
2390 | -- We will be doing calculations of lengths and bounds in this routine | |
2391 | -- and computing one from the other in some cases, e.g. getting the high | |
2392 | -- bound by adding the length-1 to the low bound. | |
2393 | ||
2394 | -- We can't just use the index type, or even its base type for this | |
2395 | -- purpose for two reasons. First it might be an enumeration type which | |
2396 | -- is not suitable fo computations of any kind, and second it may simply | |
2397 | -- not have enough range. For example if the index type is -128..+127 | |
2398 | -- then lengths can be up to 256, which is out of range of the type. | |
2399 | ||
2400 | -- For enumeration types, we can simply use Standard_Integer, this is | |
2401 | -- sufficient since the actual number of enumeration literals cannot | |
2402 | -- possibly exceed the range of integer (remember we will be doing the | |
0ac73189 | 2403 | -- arithmetic with POS values, not representation values). |
fdac1f80 AC |
2404 | |
2405 | if Is_Enumeration_Type (Ityp) then | |
46ff89f3 | 2406 | Artyp := Standard_Integer; |
fdac1f80 | 2407 | |
59262ebb AC |
2408 | -- If index type is Positive, we use the standard unsigned type, to give |
2409 | -- more room on the top of the range, obviating the need for an overflow | |
2410 | -- check when creating the upper bound. This is needed to avoid junk | |
2411 | -- overflow checks in the common case of String types. | |
2412 | ||
2413 | -- ??? Disabled for now | |
2414 | ||
2415 | -- elsif Istyp = Standard_Positive then | |
2416 | -- Artyp := Standard_Unsigned; | |
2417 | ||
2fc05e3d AC |
2418 | -- For modular types, we use a 32-bit modular type for types whose size |
2419 | -- is in the range 1-31 bits. For 32-bit unsigned types, we use the | |
2420 | -- identity type, and for larger unsigned types we use 64-bits. | |
fdac1f80 | 2421 | |
2fc05e3d | 2422 | elsif Is_Modular_Integer_Type (Ityp) then |
ecc4ddde | 2423 | if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then |
46ff89f3 | 2424 | Artyp := Standard_Unsigned; |
ecc4ddde | 2425 | elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then |
46ff89f3 | 2426 | Artyp := Ityp; |
fdac1f80 | 2427 | else |
46ff89f3 | 2428 | Artyp := RTE (RE_Long_Long_Unsigned); |
fdac1f80 AC |
2429 | end if; |
2430 | ||
2fc05e3d | 2431 | -- Similar treatment for signed types |
fdac1f80 AC |
2432 | |
2433 | else | |
ecc4ddde | 2434 | if RM_Size (Ityp) < RM_Size (Standard_Integer) then |
46ff89f3 | 2435 | Artyp := Standard_Integer; |
ecc4ddde | 2436 | elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then |
46ff89f3 | 2437 | Artyp := Ityp; |
fdac1f80 | 2438 | else |
46ff89f3 | 2439 | Artyp := Standard_Long_Long_Integer; |
fdac1f80 AC |
2440 | end if; |
2441 | end if; | |
2442 | ||
fa969310 AC |
2443 | -- Supply dummy entry at start of length array |
2444 | ||
2445 | Aggr_Length (0) := Make_Artyp_Literal (0); | |
2446 | ||
fdac1f80 | 2447 | -- Go through operands setting up the above arrays |
70482933 | 2448 | |
df46b832 AC |
2449 | J := 1; |
2450 | while J <= N loop | |
2451 | Opnd := Remove_Head (Opnds); | |
0ac73189 | 2452 | Opnd_Typ := Etype (Opnd); |
fdac1f80 AC |
2453 | |
2454 | -- The parent got messed up when we put the operands in a list, | |
2455 | -- so now put back the proper parent for the saved operand. | |
2456 | ||
df46b832 | 2457 | Set_Parent (Opnd, Parent (Cnode)); |
fdac1f80 AC |
2458 | |
2459 | -- Set will be True when we have setup one entry in the array | |
2460 | ||
df46b832 AC |
2461 | Set := False; |
2462 | ||
fdac1f80 | 2463 | -- Singleton element (or character literal) case |
df46b832 | 2464 | |
0ac73189 | 2465 | if Base_Type (Opnd_Typ) = Ctyp then |
df46b832 AC |
2466 | NN := NN + 1; |
2467 | Operands (NN) := Opnd; | |
2468 | Is_Fixed_Length (NN) := True; | |
2469 | Fixed_Length (NN) := Uint_1; | |
0ac73189 | 2470 | Result_May_Be_Null := False; |
fdac1f80 | 2471 | |
a29262fd AC |
2472 | -- Set low bound of operand (no need to set Last_Opnd_High_Bound |
2473 | -- since we know that the result cannot be null). | |
fdac1f80 | 2474 | |
0ac73189 AC |
2475 | Opnd_Low_Bound (NN) := |
2476 | Make_Attribute_Reference (Loc, | |
ecc4ddde | 2477 | Prefix => New_Reference_To (Istyp, Loc), |
0ac73189 AC |
2478 | Attribute_Name => Name_First); |
2479 | ||
df46b832 AC |
2480 | Set := True; |
2481 | ||
fdac1f80 | 2482 | -- String literal case (can only occur for strings of course) |
df46b832 AC |
2483 | |
2484 | elsif Nkind (Opnd) = N_String_Literal then | |
0ac73189 | 2485 | Len := String_Literal_Length (Opnd_Typ); |
df46b832 | 2486 | |
a29262fd AC |
2487 | if Len /= 0 then |
2488 | Result_May_Be_Null := False; | |
2489 | end if; | |
2490 | ||
2491 | -- Capture last operand high bound if result could be null | |
2492 | ||
2493 | if J = N and then Result_May_Be_Null then | |
2494 | Last_Opnd_High_Bound := | |
2495 | Make_Op_Add (Loc, | |
2496 | Left_Opnd => | |
2497 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)), | |
59262ebb | 2498 | Right_Opnd => Make_Integer_Literal (Loc, 1)); |
a29262fd AC |
2499 | end if; |
2500 | ||
2501 | -- Skip null string literal | |
fdac1f80 | 2502 | |
0ac73189 | 2503 | if J < N and then Len = 0 then |
df46b832 AC |
2504 | goto Continue; |
2505 | end if; | |
2506 | ||
2507 | NN := NN + 1; | |
2508 | Operands (NN) := Opnd; | |
2509 | Is_Fixed_Length (NN) := True; | |
0ac73189 AC |
2510 | |
2511 | -- Set length and bounds | |
2512 | ||
df46b832 | 2513 | Fixed_Length (NN) := Len; |
0ac73189 AC |
2514 | |
2515 | Opnd_Low_Bound (NN) := | |
2516 | New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)); | |
2517 | ||
df46b832 AC |
2518 | Set := True; |
2519 | ||
2520 | -- All other cases | |
2521 | ||
2522 | else | |
2523 | -- Check constrained case with known bounds | |
2524 | ||
0ac73189 | 2525 | if Is_Constrained (Opnd_Typ) then |
df46b832 | 2526 | declare |
df46b832 AC |
2527 | Index : constant Node_Id := First_Index (Opnd_Typ); |
2528 | Indx_Typ : constant Entity_Id := Etype (Index); | |
2529 | Lo : constant Node_Id := Type_Low_Bound (Indx_Typ); | |
2530 | Hi : constant Node_Id := Type_High_Bound (Indx_Typ); | |
2531 | ||
2532 | begin | |
fdac1f80 AC |
2533 | -- Fixed length constrained array type with known at compile |
2534 | -- time bounds is last case of fixed length operand. | |
df46b832 AC |
2535 | |
2536 | if Compile_Time_Known_Value (Lo) | |
2537 | and then | |
2538 | Compile_Time_Known_Value (Hi) | |
2539 | then | |
2540 | declare | |
2541 | Loval : constant Uint := Expr_Value (Lo); | |
2542 | Hival : constant Uint := Expr_Value (Hi); | |
2543 | Len : constant Uint := | |
2544 | UI_Max (Hival - Loval + 1, Uint_0); | |
2545 | ||
2546 | begin | |
0ac73189 AC |
2547 | if Len > 0 then |
2548 | Result_May_Be_Null := False; | |
df46b832 | 2549 | end if; |
0ac73189 | 2550 | |
a29262fd AC |
2551 | -- Capture last operand bound if result could be null |
2552 | ||
2553 | if J = N and then Result_May_Be_Null then | |
2554 | Last_Opnd_High_Bound := | |
2555 | Convert_To (Ityp, | |
2556 | Make_Integer_Literal (Loc, | |
2557 | Intval => Expr_Value (Hi))); | |
2558 | end if; | |
2559 | ||
2560 | -- Exclude null length case unless last operand | |
0ac73189 | 2561 | |
a29262fd | 2562 | if J < N and then Len = 0 then |
0ac73189 AC |
2563 | goto Continue; |
2564 | end if; | |
2565 | ||
2566 | NN := NN + 1; | |
2567 | Operands (NN) := Opnd; | |
2568 | Is_Fixed_Length (NN) := True; | |
2569 | Fixed_Length (NN) := Len; | |
2570 | ||
a2dc5812 | 2571 | Opnd_Low_Bound (NN) := To_Ityp ( |
0ac73189 | 2572 | Make_Integer_Literal (Loc, |
a2dc5812 | 2573 | Intval => Expr_Value (Lo))); |
0ac73189 | 2574 | |
0ac73189 | 2575 | Set := True; |
df46b832 AC |
2576 | end; |
2577 | end if; | |
2578 | end; | |
2579 | end if; | |
2580 | ||
0ac73189 AC |
2581 | -- All cases where the length is not known at compile time, or the |
2582 | -- special case of an operand which is known to be null but has a | |
2583 | -- lower bound other than 1 or is other than a string type. | |
df46b832 AC |
2584 | |
2585 | if not Set then | |
2586 | NN := NN + 1; | |
0ac73189 AC |
2587 | |
2588 | -- Capture operand bounds | |
2589 | ||
2590 | Opnd_Low_Bound (NN) := | |
2591 | Make_Attribute_Reference (Loc, | |
2592 | Prefix => | |
2593 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2594 | Attribute_Name => Name_First); | |
2595 | ||
a29262fd AC |
2596 | if J = N and Result_May_Be_Null then |
2597 | Last_Opnd_High_Bound := | |
2598 | Convert_To (Ityp, | |
2599 | Make_Attribute_Reference (Loc, | |
2600 | Prefix => | |
2601 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
2602 | Attribute_Name => Name_Last)); | |
2603 | end if; | |
0ac73189 AC |
2604 | |
2605 | -- Capture length of operand in entity | |
2606 | ||
df46b832 AC |
2607 | Operands (NN) := Opnd; |
2608 | Is_Fixed_Length (NN) := False; | |
2609 | ||
191fcb3a | 2610 | Var_Length (NN) := Make_Temporary (Loc, 'L'); |
df46b832 | 2611 | |
d0f8d157 | 2612 | Append_To (Actions, |
df46b832 AC |
2613 | Make_Object_Declaration (Loc, |
2614 | Defining_Identifier => Var_Length (NN), | |
2615 | Constant_Present => True, | |
2616 | ||
2617 | Object_Definition => | |
46ff89f3 | 2618 | New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2619 | |
2620 | Expression => | |
2621 | Make_Attribute_Reference (Loc, | |
2622 | Prefix => | |
2623 | Duplicate_Subexpr (Opnd, Name_Req => True), | |
d0f8d157 | 2624 | Attribute_Name => Name_Length))); |
df46b832 AC |
2625 | end if; |
2626 | end if; | |
2627 | ||
2628 | -- Set next entry in aggregate length array | |
2629 | ||
2630 | -- For first entry, make either integer literal for fixed length | |
0ac73189 | 2631 | -- or a reference to the saved length for variable length. |
df46b832 AC |
2632 | |
2633 | if NN = 1 then | |
2634 | if Is_Fixed_Length (1) then | |
2635 | Aggr_Length (1) := | |
2636 | Make_Integer_Literal (Loc, | |
2637 | Intval => Fixed_Length (1)); | |
2638 | else | |
2639 | Aggr_Length (1) := | |
2640 | New_Reference_To (Var_Length (1), Loc); | |
2641 | end if; | |
2642 | ||
2643 | -- If entry is fixed length and only fixed lengths so far, make | |
2644 | -- appropriate new integer literal adding new length. | |
2645 | ||
2646 | elsif Is_Fixed_Length (NN) | |
2647 | and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal | |
2648 | then | |
2649 | Aggr_Length (NN) := | |
2650 | Make_Integer_Literal (Loc, | |
2651 | Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1))); | |
2652 | ||
d0f8d157 AC |
2653 | -- All other cases, construct an addition node for the length and |
2654 | -- create an entity initialized to this length. | |
df46b832 AC |
2655 | |
2656 | else | |
191fcb3a | 2657 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 AC |
2658 | |
2659 | if Is_Fixed_Length (NN) then | |
2660 | Clen := Make_Integer_Literal (Loc, Fixed_Length (NN)); | |
2661 | else | |
2662 | Clen := New_Reference_To (Var_Length (NN), Loc); | |
2663 | end if; | |
2664 | ||
d0f8d157 | 2665 | Append_To (Actions, |
df46b832 AC |
2666 | Make_Object_Declaration (Loc, |
2667 | Defining_Identifier => Ent, | |
2668 | Constant_Present => True, | |
2669 | ||
2670 | Object_Definition => | |
46ff89f3 | 2671 | New_Occurrence_Of (Artyp, Loc), |
df46b832 AC |
2672 | |
2673 | Expression => | |
2674 | Make_Op_Add (Loc, | |
2675 | Left_Opnd => New_Copy (Aggr_Length (NN - 1)), | |
d0f8d157 | 2676 | Right_Opnd => Clen))); |
df46b832 | 2677 | |
76c597a1 | 2678 | Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent)); |
df46b832 AC |
2679 | end if; |
2680 | ||
2681 | <<Continue>> | |
2682 | J := J + 1; | |
2683 | end loop; | |
2684 | ||
a29262fd | 2685 | -- If we have only skipped null operands, return the last operand |
df46b832 AC |
2686 | |
2687 | if NN = 0 then | |
a29262fd | 2688 | Result := Opnd; |
df46b832 AC |
2689 | goto Done; |
2690 | end if; | |
2691 | ||
2692 | -- If we have only one non-null operand, return it and we are done. | |
2693 | -- There is one case in which this cannot be done, and that is when | |
fdac1f80 AC |
2694 | -- the sole operand is of the element type, in which case it must be |
2695 | -- converted to an array, and the easiest way of doing that is to go | |
df46b832 AC |
2696 | -- through the normal general circuit. |
2697 | ||
2698 | if NN = 1 | |
fdac1f80 | 2699 | and then Base_Type (Etype (Operands (1))) /= Ctyp |
df46b832 AC |
2700 | then |
2701 | Result := Operands (1); | |
2702 | goto Done; | |
2703 | end if; | |
2704 | ||
2705 | -- Cases where we have a real concatenation | |
2706 | ||
fdac1f80 AC |
2707 | -- Next step is to find the low bound for the result array that we |
2708 | -- will allocate. The rules for this are in (RM 4.5.6(5-7)). | |
2709 | ||
2710 | -- If the ultimate ancestor of the index subtype is a constrained array | |
2711 | -- definition, then the lower bound is that of the index subtype as | |
2712 | -- specified by (RM 4.5.3(6)). | |
2713 | ||
2714 | -- The right test here is to go to the root type, and then the ultimate | |
2715 | -- ancestor is the first subtype of this root type. | |
2716 | ||
2717 | if Is_Constrained (First_Subtype (Root_Type (Atyp))) then | |
0ac73189 | 2718 | Low_Bound := |
fdac1f80 AC |
2719 | Make_Attribute_Reference (Loc, |
2720 | Prefix => | |
2721 | New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc), | |
0ac73189 | 2722 | Attribute_Name => Name_First); |
df46b832 AC |
2723 | |
2724 | -- If the first operand in the list has known length we know that | |
2725 | -- the lower bound of the result is the lower bound of this operand. | |
2726 | ||
fdac1f80 | 2727 | elsif Is_Fixed_Length (1) then |
0ac73189 | 2728 | Low_Bound := Opnd_Low_Bound (1); |
df46b832 AC |
2729 | |
2730 | -- OK, we don't know the lower bound, we have to build a horrible | |
2731 | -- expression actions node of the form | |
2732 | ||
2733 | -- if Cond1'Length /= 0 then | |
0ac73189 | 2734 | -- Opnd1 low bound |
df46b832 AC |
2735 | -- else |
2736 | -- if Opnd2'Length /= 0 then | |
0ac73189 | 2737 | -- Opnd2 low bound |
df46b832 AC |
2738 | -- else |
2739 | -- ... | |
2740 | ||
2741 | -- The nesting ends either when we hit an operand whose length is known | |
2742 | -- at compile time, or on reaching the last operand, whose low bound we | |
2743 | -- take unconditionally whether or not it is null. It's easiest to do | |
2744 | -- this with a recursive procedure: | |
2745 | ||
2746 | else | |
2747 | declare | |
2748 | function Get_Known_Bound (J : Nat) return Node_Id; | |
2749 | -- Returns the lower bound determined by operands J .. NN | |
2750 | ||
2751 | --------------------- | |
2752 | -- Get_Known_Bound -- | |
2753 | --------------------- | |
2754 | ||
2755 | function Get_Known_Bound (J : Nat) return Node_Id is | |
df46b832 | 2756 | begin |
0ac73189 AC |
2757 | if Is_Fixed_Length (J) or else J = NN then |
2758 | return New_Copy (Opnd_Low_Bound (J)); | |
70482933 RK |
2759 | |
2760 | else | |
df46b832 AC |
2761 | return |
2762 | Make_Conditional_Expression (Loc, | |
2763 | Expressions => New_List ( | |
2764 | ||
2765 | Make_Op_Ne (Loc, | |
2766 | Left_Opnd => New_Reference_To (Var_Length (J), Loc), | |
2767 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
2768 | ||
0ac73189 | 2769 | New_Copy (Opnd_Low_Bound (J)), |
df46b832 | 2770 | Get_Known_Bound (J + 1))); |
70482933 | 2771 | end if; |
df46b832 | 2772 | end Get_Known_Bound; |
70482933 | 2773 | |
df46b832 | 2774 | begin |
191fcb3a | 2775 | Ent := Make_Temporary (Loc, 'L'); |
df46b832 | 2776 | |
d0f8d157 | 2777 | Append_To (Actions, |
df46b832 AC |
2778 | Make_Object_Declaration (Loc, |
2779 | Defining_Identifier => Ent, | |
2780 | Constant_Present => True, | |
0ac73189 | 2781 | Object_Definition => New_Occurrence_Of (Ityp, Loc), |
d0f8d157 | 2782 | Expression => Get_Known_Bound (1))); |
df46b832 AC |
2783 | |
2784 | Low_Bound := New_Reference_To (Ent, Loc); | |
2785 | end; | |
2786 | end if; | |
70482933 | 2787 | |
76c597a1 AC |
2788 | -- Now we can safely compute the upper bound, normally |
2789 | -- Low_Bound + Length - 1. | |
0ac73189 AC |
2790 | |
2791 | High_Bound := | |
2792 | To_Ityp ( | |
2793 | Make_Op_Add (Loc, | |
46ff89f3 | 2794 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
0ac73189 AC |
2795 | Right_Opnd => |
2796 | Make_Op_Subtract (Loc, | |
2797 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 2798 | Right_Opnd => Make_Artyp_Literal (1)))); |
0ac73189 | 2799 | |
59262ebb | 2800 | -- Note that calculation of the high bound may cause overflow in some |
bded454f RD |
2801 | -- very weird cases, so in the general case we need an overflow check on |
2802 | -- the high bound. We can avoid this for the common case of string types | |
2803 | -- and other types whose index is Positive, since we chose a wider range | |
2804 | -- for the arithmetic type. | |
76c597a1 | 2805 | |
59262ebb AC |
2806 | if Istyp /= Standard_Positive then |
2807 | Activate_Overflow_Check (High_Bound); | |
2808 | end if; | |
76c597a1 AC |
2809 | |
2810 | -- Handle the exceptional case where the result is null, in which case | |
a29262fd AC |
2811 | -- case the bounds come from the last operand (so that we get the proper |
2812 | -- bounds if the last operand is super-flat). | |
2813 | ||
0ac73189 AC |
2814 | if Result_May_Be_Null then |
2815 | High_Bound := | |
2816 | Make_Conditional_Expression (Loc, | |
2817 | Expressions => New_List ( | |
2818 | Make_Op_Eq (Loc, | |
2819 | Left_Opnd => New_Copy (Aggr_Length (NN)), | |
fa969310 | 2820 | Right_Opnd => Make_Artyp_Literal (0)), |
a29262fd | 2821 | Last_Opnd_High_Bound, |
0ac73189 AC |
2822 | High_Bound)); |
2823 | end if; | |
2824 | ||
d0f8d157 AC |
2825 | -- Here is where we insert the saved up actions |
2826 | ||
2827 | Insert_Actions (Cnode, Actions, Suppress => All_Checks); | |
2828 | ||
602a7ec0 AC |
2829 | -- Now we construct an array object with appropriate bounds. We mark |
2830 | -- the target as internal to prevent useless initialization when | |
2831 | -- Initialize_Scalars is enabled. | |
70482933 | 2832 | |
191fcb3a | 2833 | Ent := Make_Temporary (Loc, 'S'); |
008f6fd3 | 2834 | Set_Is_Internal (Ent); |
70482933 | 2835 | |
76c597a1 | 2836 | -- If the bound is statically known to be out of range, we do not want |
fa969310 AC |
2837 | -- to abort, we want a warning and a runtime constraint error. Note that |
2838 | -- we have arranged that the result will not be treated as a static | |
2839 | -- constant, so we won't get an illegality during this insertion. | |
76c597a1 | 2840 | |
df46b832 AC |
2841 | Insert_Action (Cnode, |
2842 | Make_Object_Declaration (Loc, | |
2843 | Defining_Identifier => Ent, | |
df46b832 AC |
2844 | Object_Definition => |
2845 | Make_Subtype_Indication (Loc, | |
fdac1f80 | 2846 | Subtype_Mark => New_Occurrence_Of (Atyp, Loc), |
df46b832 AC |
2847 | Constraint => |
2848 | Make_Index_Or_Discriminant_Constraint (Loc, | |
2849 | Constraints => New_List ( | |
2850 | Make_Range (Loc, | |
0ac73189 AC |
2851 | Low_Bound => Low_Bound, |
2852 | High_Bound => High_Bound))))), | |
df46b832 AC |
2853 | Suppress => All_Checks); |
2854 | ||
d1f453b7 RD |
2855 | -- If the result of the concatenation appears as the initializing |
2856 | -- expression of an object declaration, we can just rename the | |
2857 | -- result, rather than copying it. | |
2858 | ||
2859 | Set_OK_To_Rename (Ent); | |
2860 | ||
76c597a1 AC |
2861 | -- Catch the static out of range case now |
2862 | ||
2863 | if Raises_Constraint_Error (High_Bound) then | |
2864 | raise Concatenation_Error; | |
2865 | end if; | |
2866 | ||
df46b832 AC |
2867 | -- Now we will generate the assignments to do the actual concatenation |
2868 | ||
bded454f RD |
2869 | -- There is one case in which we will not do this, namely when all the |
2870 | -- following conditions are met: | |
2871 | ||
2872 | -- The result type is Standard.String | |
2873 | ||
2874 | -- There are nine or fewer retained (non-null) operands | |
2875 | ||
ffec8e81 | 2876 | -- The optimization level is -O0 |
bded454f RD |
2877 | |
2878 | -- The corresponding System.Concat_n.Str_Concat_n routine is | |
2879 | -- available in the run time. | |
2880 | ||
2881 | -- The debug flag gnatd.c is not set | |
2882 | ||
2883 | -- If all these conditions are met then we generate a call to the | |
2884 | -- relevant concatenation routine. The purpose of this is to avoid | |
2885 | -- undesirable code bloat at -O0. | |
2886 | ||
2887 | if Atyp = Standard_String | |
2888 | and then NN in 2 .. 9 | |
ffec8e81 | 2889 | and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC) |
bded454f RD |
2890 | and then not Debug_Flag_Dot_C |
2891 | then | |
2892 | declare | |
2893 | RR : constant array (Nat range 2 .. 9) of RE_Id := | |
2894 | (RE_Str_Concat_2, | |
2895 | RE_Str_Concat_3, | |
2896 | RE_Str_Concat_4, | |
2897 | RE_Str_Concat_5, | |
2898 | RE_Str_Concat_6, | |
2899 | RE_Str_Concat_7, | |
2900 | RE_Str_Concat_8, | |
2901 | RE_Str_Concat_9); | |
2902 | ||
2903 | begin | |
2904 | if RTE_Available (RR (NN)) then | |
2905 | declare | |
2906 | Opnds : constant List_Id := | |
2907 | New_List (New_Occurrence_Of (Ent, Loc)); | |
2908 | ||
2909 | begin | |
2910 | for J in 1 .. NN loop | |
2911 | if Is_List_Member (Operands (J)) then | |
2912 | Remove (Operands (J)); | |
2913 | end if; | |
2914 | ||
2915 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
2916 | Append_To (Opnds, | |
2917 | Make_Aggregate (Loc, | |
2918 | Component_Associations => New_List ( | |
2919 | Make_Component_Association (Loc, | |
2920 | Choices => New_List ( | |
2921 | Make_Integer_Literal (Loc, 1)), | |
2922 | Expression => Operands (J))))); | |
2923 | ||
2924 | else | |
2925 | Append_To (Opnds, Operands (J)); | |
2926 | end if; | |
2927 | end loop; | |
2928 | ||
2929 | Insert_Action (Cnode, | |
2930 | Make_Procedure_Call_Statement (Loc, | |
2931 | Name => New_Reference_To (RTE (RR (NN)), Loc), | |
2932 | Parameter_Associations => Opnds)); | |
2933 | ||
2934 | Result := New_Reference_To (Ent, Loc); | |
2935 | goto Done; | |
2936 | end; | |
2937 | end if; | |
2938 | end; | |
2939 | end if; | |
2940 | ||
2941 | -- Not special case so generate the assignments | |
2942 | ||
76c597a1 AC |
2943 | Known_Non_Null_Operand_Seen := False; |
2944 | ||
df46b832 AC |
2945 | for J in 1 .. NN loop |
2946 | declare | |
2947 | Lo : constant Node_Id := | |
2948 | Make_Op_Add (Loc, | |
46ff89f3 | 2949 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
2950 | Right_Opnd => Aggr_Length (J - 1)); |
2951 | ||
2952 | Hi : constant Node_Id := | |
2953 | Make_Op_Add (Loc, | |
46ff89f3 | 2954 | Left_Opnd => To_Artyp (New_Copy (Low_Bound)), |
df46b832 AC |
2955 | Right_Opnd => |
2956 | Make_Op_Subtract (Loc, | |
2957 | Left_Opnd => Aggr_Length (J), | |
fa969310 | 2958 | Right_Opnd => Make_Artyp_Literal (1))); |
70482933 | 2959 | |
df46b832 | 2960 | begin |
fdac1f80 AC |
2961 | -- Singleton case, simple assignment |
2962 | ||
2963 | if Base_Type (Etype (Operands (J))) = Ctyp then | |
76c597a1 | 2964 | Known_Non_Null_Operand_Seen := True; |
df46b832 AC |
2965 | Insert_Action (Cnode, |
2966 | Make_Assignment_Statement (Loc, | |
2967 | Name => | |
2968 | Make_Indexed_Component (Loc, | |
2969 | Prefix => New_Occurrence_Of (Ent, Loc), | |
fdac1f80 | 2970 | Expressions => New_List (To_Ityp (Lo))), |
df46b832 AC |
2971 | Expression => Operands (J)), |
2972 | Suppress => All_Checks); | |
70482933 | 2973 | |
76c597a1 AC |
2974 | -- Array case, slice assignment, skipped when argument is fixed |
2975 | -- length and known to be null. | |
fdac1f80 | 2976 | |
76c597a1 AC |
2977 | elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then |
2978 | declare | |
2979 | Assign : Node_Id := | |
2980 | Make_Assignment_Statement (Loc, | |
2981 | Name => | |
2982 | Make_Slice (Loc, | |
2983 | Prefix => | |
2984 | New_Occurrence_Of (Ent, Loc), | |
2985 | Discrete_Range => | |
2986 | Make_Range (Loc, | |
2987 | Low_Bound => To_Ityp (Lo), | |
2988 | High_Bound => To_Ityp (Hi))), | |
2989 | Expression => Operands (J)); | |
2990 | begin | |
2991 | if Is_Fixed_Length (J) then | |
2992 | Known_Non_Null_Operand_Seen := True; | |
2993 | ||
2994 | elsif not Known_Non_Null_Operand_Seen then | |
2995 | ||
2996 | -- Here if operand length is not statically known and no | |
2997 | -- operand known to be non-null has been processed yet. | |
2998 | -- If operand length is 0, we do not need to perform the | |
2999 | -- assignment, and we must avoid the evaluation of the | |
3000 | -- high bound of the slice, since it may underflow if the | |
3001 | -- low bound is Ityp'First. | |
3002 | ||
3003 | Assign := | |
3004 | Make_Implicit_If_Statement (Cnode, | |
3005 | Condition => | |
3006 | Make_Op_Ne (Loc, | |
3007 | Left_Opnd => | |
3008 | New_Occurrence_Of (Var_Length (J), Loc), | |
3009 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
3010 | Then_Statements => | |
3011 | New_List (Assign)); | |
3012 | end if; | |
fa969310 | 3013 | |
76c597a1 AC |
3014 | Insert_Action (Cnode, Assign, Suppress => All_Checks); |
3015 | end; | |
df46b832 AC |
3016 | end if; |
3017 | end; | |
3018 | end loop; | |
70482933 | 3019 | |
0ac73189 AC |
3020 | -- Finally we build the result, which is a reference to the array object |
3021 | ||
df46b832 | 3022 | Result := New_Reference_To (Ent, Loc); |
70482933 | 3023 | |
df46b832 AC |
3024 | <<Done>> |
3025 | Rewrite (Cnode, Result); | |
fdac1f80 AC |
3026 | Analyze_And_Resolve (Cnode, Atyp); |
3027 | ||
3028 | exception | |
3029 | when Concatenation_Error => | |
76c597a1 AC |
3030 | |
3031 | -- Kill warning generated for the declaration of the static out of | |
3032 | -- range high bound, and instead generate a Constraint_Error with | |
3033 | -- an appropriate specific message. | |
3034 | ||
3035 | Kill_Dead_Code (Declaration_Node (Entity (High_Bound))); | |
3036 | Apply_Compile_Time_Constraint_Error | |
3037 | (N => Cnode, | |
3038 | Msg => "concatenation result upper bound out of range?", | |
3039 | Reason => CE_Range_Check_Failed); | |
3040 | -- Set_Etype (Cnode, Atyp); | |
fdac1f80 | 3041 | end Expand_Concatenate; |
70482933 RK |
3042 | |
3043 | ------------------------ | |
3044 | -- Expand_N_Allocator -- | |
3045 | ------------------------ | |
3046 | ||
3047 | procedure Expand_N_Allocator (N : Node_Id) is | |
3048 | PtrT : constant Entity_Id := Etype (N); | |
d6a24cdb | 3049 | Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT)); |
f82944b7 | 3050 | Etyp : constant Entity_Id := Etype (Expression (N)); |
70482933 | 3051 | Loc : constant Source_Ptr := Sloc (N); |
f82944b7 | 3052 | Desig : Entity_Id; |
70482933 | 3053 | Temp : Entity_Id; |
26bff3d9 | 3054 | Nod : Node_Id; |
70482933 | 3055 | |
26bff3d9 JM |
3056 | procedure Complete_Coextension_Finalization; |
3057 | -- Generate finalization calls for all nested coextensions of N. This | |
3058 | -- routine may allocate list controllers if necessary. | |
0669bebe | 3059 | |
26bff3d9 JM |
3060 | procedure Rewrite_Coextension (N : Node_Id); |
3061 | -- Static coextensions have the same lifetime as the entity they | |
8fc789c8 | 3062 | -- constrain. Such occurrences can be rewritten as aliased objects |
26bff3d9 | 3063 | -- and their unrestricted access used instead of the coextension. |
0669bebe | 3064 | |
8aec446b | 3065 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id; |
507ed3fd AC |
3066 | -- Given a constrained array type E, returns a node representing the |
3067 | -- code to compute the size in storage elements for the given type. | |
205c14b0 | 3068 | -- This is done without using the attribute (which malfunctions for |
507ed3fd | 3069 | -- large sizes ???) |
8aec446b | 3070 | |
26bff3d9 JM |
3071 | --------------------------------------- |
3072 | -- Complete_Coextension_Finalization -- | |
3073 | --------------------------------------- | |
0669bebe | 3074 | |
26bff3d9 JM |
3075 | procedure Complete_Coextension_Finalization is |
3076 | Coext : Node_Id; | |
3077 | Coext_Elmt : Elmt_Id; | |
3078 | Flist : Node_Id; | |
3079 | Ref : Node_Id; | |
0669bebe | 3080 | |
26bff3d9 JM |
3081 | function Inside_A_Return_Statement (N : Node_Id) return Boolean; |
3082 | -- Determine whether node N is part of a return statement | |
3083 | ||
3084 | function Needs_Initialization_Call (N : Node_Id) return Boolean; | |
3085 | -- Determine whether node N is a subtype indicator allocator which | |
b4592168 | 3086 | -- acts a coextension. Such coextensions need initialization. |
26bff3d9 JM |
3087 | |
3088 | ------------------------------- | |
3089 | -- Inside_A_Return_Statement -- | |
3090 | ------------------------------- | |
3091 | ||
3092 | function Inside_A_Return_Statement (N : Node_Id) return Boolean is | |
3093 | P : Node_Id; | |
3094 | ||
3095 | begin | |
3096 | P := Parent (N); | |
3097 | while Present (P) loop | |
303b4d58 AC |
3098 | if Nkind_In |
3099 | (P, N_Extended_Return_Statement, N_Simple_Return_Statement) | |
26bff3d9 JM |
3100 | then |
3101 | return True; | |
3102 | ||
3103 | -- Stop the traversal when we reach a subprogram body | |
3104 | ||
3105 | elsif Nkind (P) = N_Subprogram_Body then | |
3106 | return False; | |
3107 | end if; | |
3108 | ||
3109 | P := Parent (P); | |
3110 | end loop; | |
3111 | ||
3112 | return False; | |
3113 | end Inside_A_Return_Statement; | |
3114 | ||
3115 | ------------------------------- | |
3116 | -- Needs_Initialization_Call -- | |
3117 | ------------------------------- | |
3118 | ||
3119 | function Needs_Initialization_Call (N : Node_Id) return Boolean is | |
3120 | Obj_Decl : Node_Id; | |
3121 | ||
3122 | begin | |
3123 | if Nkind (N) = N_Explicit_Dereference | |
3124 | and then Nkind (Prefix (N)) = N_Identifier | |
3125 | and then Nkind (Parent (Entity (Prefix (N)))) = | |
3126 | N_Object_Declaration | |
3127 | then | |
3128 | Obj_Decl := Parent (Entity (Prefix (N))); | |
0669bebe | 3129 | |
26bff3d9 JM |
3130 | return |
3131 | Present (Expression (Obj_Decl)) | |
3132 | and then Nkind (Expression (Obj_Decl)) = N_Allocator | |
3133 | and then Nkind (Expression (Expression (Obj_Decl))) /= | |
3134 | N_Qualified_Expression; | |
0669bebe GB |
3135 | end if; |
3136 | ||
26bff3d9 JM |
3137 | return False; |
3138 | end Needs_Initialization_Call; | |
3139 | ||
3140 | -- Start of processing for Complete_Coextension_Finalization | |
3141 | ||
3142 | begin | |
3143 | -- When a coextension root is inside a return statement, we need to | |
3144 | -- use the finalization chain of the function's scope. This does not | |
3145 | -- apply for controlled named access types because in those cases we | |
3146 | -- can use the finalization chain of the type itself. | |
3147 | ||
3148 | if Inside_A_Return_Statement (N) | |
3149 | and then | |
3150 | (Ekind (PtrT) = E_Anonymous_Access_Type | |
3151 | or else | |
3152 | (Ekind (PtrT) = E_Access_Type | |
3153 | and then No (Associated_Final_Chain (PtrT)))) | |
3154 | then | |
0669bebe | 3155 | declare |
26bff3d9 JM |
3156 | Decl : Node_Id; |
3157 | Outer_S : Entity_Id; | |
13d923cc | 3158 | S : Entity_Id; |
0669bebe GB |
3159 | |
3160 | begin | |
13d923cc | 3161 | S := Current_Scope; |
26bff3d9 JM |
3162 | while Present (S) and then S /= Standard_Standard loop |
3163 | if Ekind (S) = E_Function then | |
3164 | Outer_S := Scope (S); | |
3165 | ||
3166 | -- Retrieve the declaration of the body | |
3167 | ||
8aec446b AC |
3168 | Decl := |
3169 | Parent | |
3170 | (Parent | |
3171 | (Corresponding_Body (Parent (Parent (S))))); | |
26bff3d9 JM |
3172 | exit; |
3173 | end if; | |
3174 | ||
3175 | S := Scope (S); | |
0669bebe GB |
3176 | end loop; |
3177 | ||
26bff3d9 JM |
3178 | -- Push the scope of the function body since we are inserting |
3179 | -- the list before the body, but we are currently in the body | |
3180 | -- itself. Override the finalization list of PtrT since the | |
3181 | -- finalization context is now different. | |
3182 | ||
3183 | Push_Scope (Outer_S); | |
3184 | Build_Final_List (Decl, PtrT); | |
3185 | Pop_Scope; | |
0669bebe GB |
3186 | end; |
3187 | ||
26bff3d9 JM |
3188 | -- The root allocator may not be controlled, but it still needs a |
3189 | -- finalization list for all nested coextensions. | |
0669bebe | 3190 | |
26bff3d9 JM |
3191 | elsif No (Associated_Final_Chain (PtrT)) then |
3192 | Build_Final_List (N, PtrT); | |
3193 | end if; | |
0669bebe | 3194 | |
26bff3d9 JM |
3195 | Flist := |
3196 | Make_Selected_Component (Loc, | |
3197 | Prefix => | |
3198 | New_Reference_To (Associated_Final_Chain (PtrT), Loc), | |
3199 | Selector_Name => | |
3200 | Make_Identifier (Loc, Name_F)); | |
3201 | ||
3202 | Coext_Elmt := First_Elmt (Coextensions (N)); | |
3203 | while Present (Coext_Elmt) loop | |
3204 | Coext := Node (Coext_Elmt); | |
3205 | ||
3206 | -- Generate: | |
3207 | -- typ! (coext.all) | |
3208 | ||
3209 | if Nkind (Coext) = N_Identifier then | |
685094bf RD |
3210 | Ref := |
3211 | Make_Unchecked_Type_Conversion (Loc, | |
3212 | Subtype_Mark => New_Reference_To (Etype (Coext), Loc), | |
3213 | Expression => | |
3214 | Make_Explicit_Dereference (Loc, | |
3215 | Prefix => New_Copy_Tree (Coext))); | |
26bff3d9 JM |
3216 | else |
3217 | Ref := New_Copy_Tree (Coext); | |
3218 | end if; | |
0669bebe | 3219 | |
b4592168 | 3220 | -- No initialization call if not allowed |
26bff3d9 | 3221 | |
b4592168 | 3222 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 | 3223 | |
b4592168 | 3224 | if not Restriction_Active (No_Default_Initialization) then |
26bff3d9 | 3225 | |
b4592168 GD |
3226 | -- Generate: |
3227 | -- initialize (Ref) | |
3228 | -- attach_to_final_list (Ref, Flist, 2) | |
3229 | ||
3230 | if Needs_Initialization_Call (Coext) then | |
3231 | Insert_Actions (N, | |
3232 | Make_Init_Call ( | |
3233 | Ref => Ref, | |
3234 | Typ => Etype (Coext), | |
3235 | Flist_Ref => Flist, | |
3236 | With_Attach => Make_Integer_Literal (Loc, Uint_2))); | |
3237 | ||
3238 | -- Generate: | |
3239 | -- attach_to_final_list (Ref, Flist, 2) | |
3240 | ||
3241 | else | |
3242 | Insert_Action (N, | |
3243 | Make_Attach_Call ( | |
3244 | Obj_Ref => Ref, | |
3245 | Flist_Ref => New_Copy_Tree (Flist), | |
3246 | With_Attach => Make_Integer_Literal (Loc, Uint_2))); | |
3247 | end if; | |
26bff3d9 JM |
3248 | end if; |
3249 | ||
3250 | Next_Elmt (Coext_Elmt); | |
3251 | end loop; | |
3252 | end Complete_Coextension_Finalization; | |
3253 | ||
3254 | ------------------------- | |
3255 | -- Rewrite_Coextension -- | |
3256 | ------------------------- | |
3257 | ||
3258 | procedure Rewrite_Coextension (N : Node_Id) is | |
191fcb3a | 3259 | Temp : constant Node_Id := Make_Temporary (Loc, 'C'); |
26bff3d9 JM |
3260 | |
3261 | -- Generate: | |
3262 | -- Cnn : aliased Etyp; | |
3263 | ||
3264 | Decl : constant Node_Id := | |
3265 | Make_Object_Declaration (Loc, | |
3266 | Defining_Identifier => Temp, | |
3267 | Aliased_Present => True, | |
3268 | Object_Definition => | |
3269 | New_Occurrence_Of (Etyp, Loc)); | |
3270 | Nod : Node_Id; | |
3271 | ||
3272 | begin | |
3273 | if Nkind (Expression (N)) = N_Qualified_Expression then | |
3274 | Set_Expression (Decl, Expression (Expression (N))); | |
0669bebe | 3275 | end if; |
26bff3d9 JM |
3276 | |
3277 | -- Find the proper insertion node for the declaration | |
3278 | ||
3279 | Nod := Parent (N); | |
3280 | while Present (Nod) loop | |
3281 | exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call | |
3282 | or else Nkind (Nod) = N_Procedure_Call_Statement | |
3283 | or else Nkind (Nod) in N_Declaration; | |
3284 | Nod := Parent (Nod); | |
3285 | end loop; | |
3286 | ||
3287 | Insert_Before (Nod, Decl); | |
3288 | Analyze (Decl); | |
3289 | ||
3290 | Rewrite (N, | |
3291 | Make_Attribute_Reference (Loc, | |
3292 | Prefix => New_Occurrence_Of (Temp, Loc), | |
3293 | Attribute_Name => Name_Unrestricted_Access)); | |
3294 | ||
3295 | Analyze_And_Resolve (N, PtrT); | |
3296 | end Rewrite_Coextension; | |
0669bebe | 3297 | |
8aec446b AC |
3298 | ------------------------------ |
3299 | -- Size_In_Storage_Elements -- | |
3300 | ------------------------------ | |
3301 | ||
3302 | function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is | |
3303 | begin | |
3304 | -- Logically this just returns E'Max_Size_In_Storage_Elements. | |
3305 | -- However, the reason for the existence of this function is | |
3306 | -- to construct a test for sizes too large, which means near the | |
3307 | -- 32-bit limit on a 32-bit machine, and precisely the trouble | |
3308 | -- is that we get overflows when sizes are greater than 2**31. | |
3309 | ||
507ed3fd | 3310 | -- So what we end up doing for array types is to use the expression: |
8aec446b AC |
3311 | |
3312 | -- number-of-elements * component_type'Max_Size_In_Storage_Elements | |
3313 | ||
3314 | -- which avoids this problem. All this is a big bogus, but it does | |
3315 | -- mean we catch common cases of trying to allocate arrays that | |
3316 | -- are too large, and which in the absence of a check results in | |
3317 | -- undetected chaos ??? | |
3318 | ||
507ed3fd AC |
3319 | declare |
3320 | Len : Node_Id; | |
3321 | Res : Node_Id; | |
8aec446b | 3322 | |
507ed3fd AC |
3323 | begin |
3324 | for J in 1 .. Number_Dimensions (E) loop | |
3325 | Len := | |
3326 | Make_Attribute_Reference (Loc, | |
3327 | Prefix => New_Occurrence_Of (E, Loc), | |
3328 | Attribute_Name => Name_Length, | |
3329 | Expressions => New_List ( | |
3330 | Make_Integer_Literal (Loc, J))); | |
8aec446b | 3331 | |
507ed3fd AC |
3332 | if J = 1 then |
3333 | Res := Len; | |
8aec446b | 3334 | |
507ed3fd AC |
3335 | else |
3336 | Res := | |
3337 | Make_Op_Multiply (Loc, | |
3338 | Left_Opnd => Res, | |
3339 | Right_Opnd => Len); | |
3340 | end if; | |
3341 | end loop; | |
8aec446b | 3342 | |
8aec446b | 3343 | return |
507ed3fd AC |
3344 | Make_Op_Multiply (Loc, |
3345 | Left_Opnd => Len, | |
3346 | Right_Opnd => | |
3347 | Make_Attribute_Reference (Loc, | |
3348 | Prefix => New_Occurrence_Of (Component_Type (E), Loc), | |
3349 | Attribute_Name => Name_Max_Size_In_Storage_Elements)); | |
3350 | end; | |
8aec446b AC |
3351 | end Size_In_Storage_Elements; |
3352 | ||
0669bebe GB |
3353 | -- Start of processing for Expand_N_Allocator |
3354 | ||
70482933 RK |
3355 | begin |
3356 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
3357 | -- shall not be a remote access-to-class-wide-limited-private type | |
3358 | ||
3359 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
3360 | ||
3361 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
3362 | ||
3363 | -- Set the Storage Pool | |
3364 | ||
3365 | Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT))); | |
3366 | ||
3367 | if Present (Storage_Pool (N)) then | |
3368 | if Is_RTE (Storage_Pool (N), RE_SS_Pool) then | |
26bff3d9 | 3369 | if VM_Target = No_VM then |
70482933 RK |
3370 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); |
3371 | end if; | |
fbf5a39b AC |
3372 | |
3373 | elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then | |
3374 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
3375 | ||
70482933 RK |
3376 | else |
3377 | Set_Procedure_To_Call (N, | |
3378 | Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate)); | |
3379 | end if; | |
3380 | end if; | |
3381 | ||
685094bf RD |
3382 | -- Under certain circumstances we can replace an allocator by an access |
3383 | -- to statically allocated storage. The conditions, as noted in AARM | |
3384 | -- 3.10 (10c) are as follows: | |
70482933 RK |
3385 | |
3386 | -- Size and initial value is known at compile time | |
3387 | -- Access type is access-to-constant | |
3388 | ||
fbf5a39b AC |
3389 | -- The allocator is not part of a constraint on a record component, |
3390 | -- because in that case the inserted actions are delayed until the | |
3391 | -- record declaration is fully analyzed, which is too late for the | |
3392 | -- analysis of the rewritten allocator. | |
3393 | ||
70482933 RK |
3394 | if Is_Access_Constant (PtrT) |
3395 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
3396 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
3397 | and then Size_Known_At_Compile_Time (Etype (Expression | |
3398 | (Expression (N)))) | |
fbf5a39b | 3399 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
3400 | then |
3401 | -- Here we can do the optimization. For the allocator | |
3402 | ||
3403 | -- new x'(y) | |
3404 | ||
3405 | -- We insert an object declaration | |
3406 | ||
3407 | -- Tnn : aliased x := y; | |
3408 | ||
685094bf RD |
3409 | -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is |
3410 | -- marked as requiring static allocation. | |
70482933 | 3411 | |
191fcb3a | 3412 | Temp := Make_Temporary (Loc, 'T', Expression (Expression (N))); |
70482933 RK |
3413 | Desig := Subtype_Mark (Expression (N)); |
3414 | ||
3415 | -- If context is constrained, use constrained subtype directly, | |
8fc789c8 | 3416 | -- so that the constant is not labelled as having a nominally |
70482933 RK |
3417 | -- unconstrained subtype. |
3418 | ||
0da2c8ac AC |
3419 | if Entity (Desig) = Base_Type (Dtyp) then |
3420 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
3421 | end if; |
3422 | ||
3423 | Insert_Action (N, | |
3424 | Make_Object_Declaration (Loc, | |
3425 | Defining_Identifier => Temp, | |
3426 | Aliased_Present => True, | |
3427 | Constant_Present => Is_Access_Constant (PtrT), | |
3428 | Object_Definition => Desig, | |
3429 | Expression => Expression (Expression (N)))); | |
3430 | ||
3431 | Rewrite (N, | |
3432 | Make_Attribute_Reference (Loc, | |
3433 | Prefix => New_Occurrence_Of (Temp, Loc), | |
3434 | Attribute_Name => Name_Unrestricted_Access)); | |
3435 | ||
3436 | Analyze_And_Resolve (N, PtrT); | |
3437 | ||
685094bf RD |
3438 | -- We set the variable as statically allocated, since we don't want |
3439 | -- it going on the stack of the current procedure! | |
70482933 RK |
3440 | |
3441 | Set_Is_Statically_Allocated (Temp); | |
3442 | return; | |
3443 | end if; | |
3444 | ||
0669bebe GB |
3445 | -- Same if the allocator is an access discriminant for a local object: |
3446 | -- instead of an allocator we create a local value and constrain the | |
3447 | -- the enclosing object with the corresponding access attribute. | |
3448 | ||
26bff3d9 JM |
3449 | if Is_Static_Coextension (N) then |
3450 | Rewrite_Coextension (N); | |
0669bebe GB |
3451 | return; |
3452 | end if; | |
3453 | ||
26bff3d9 JM |
3454 | -- The current allocator creates an object which may contain nested |
3455 | -- coextensions. Use the current allocator's finalization list to | |
3456 | -- generate finalization call for all nested coextensions. | |
3457 | ||
3458 | if Is_Coextension_Root (N) then | |
3459 | Complete_Coextension_Finalization; | |
3460 | end if; | |
3461 | ||
8aec446b AC |
3462 | -- Check for size too large, we do this because the back end misses |
3463 | -- proper checks here and can generate rubbish allocation calls when | |
3464 | -- we are near the limit. We only do this for the 32-bit address case | |
3465 | -- since that is from a practical point of view where we see a problem. | |
3466 | ||
3467 | if System_Address_Size = 32 | |
3468 | and then not Storage_Checks_Suppressed (PtrT) | |
3469 | and then not Storage_Checks_Suppressed (Dtyp) | |
3470 | and then not Storage_Checks_Suppressed (Etyp) | |
3471 | then | |
3472 | -- The check we want to generate should look like | |
3473 | ||
3474 | -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then | |
3475 | -- raise Storage_Error; | |
3476 | -- end if; | |
3477 | ||
507ed3fd AC |
3478 | -- where 3.5 gigabytes is a constant large enough to accomodate any |
3479 | -- reasonable request for. But we can't do it this way because at | |
3480 | -- least at the moment we don't compute this attribute right, and | |
3481 | -- can silently give wrong results when the result gets large. Since | |
3482 | -- this is all about large results, that's bad, so instead we only | |
205c14b0 | 3483 | -- apply the check for constrained arrays, and manually compute the |
507ed3fd | 3484 | -- value of the attribute ??? |
8aec446b | 3485 | |
507ed3fd AC |
3486 | if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then |
3487 | Insert_Action (N, | |
3488 | Make_Raise_Storage_Error (Loc, | |
3489 | Condition => | |
3490 | Make_Op_Gt (Loc, | |
3491 | Left_Opnd => Size_In_Storage_Elements (Etyp), | |
3492 | Right_Opnd => | |
3493 | Make_Integer_Literal (Loc, | |
3494 | Intval => Uint_7 * (Uint_2 ** 29))), | |
3495 | Reason => SE_Object_Too_Large)); | |
3496 | end if; | |
8aec446b AC |
3497 | end if; |
3498 | ||
0da2c8ac | 3499 | -- Handle case of qualified expression (other than optimization above) |
cac5a801 AC |
3500 | -- First apply constraint checks, because the bounds or discriminants |
3501 | -- in the aggregate might not match the subtype mark in the allocator. | |
0da2c8ac | 3502 | |
70482933 | 3503 | if Nkind (Expression (N)) = N_Qualified_Expression then |
cac5a801 AC |
3504 | Apply_Constraint_Check |
3505 | (Expression (Expression (N)), Etype (Expression (N))); | |
3506 | ||
fbf5a39b | 3507 | Expand_Allocator_Expression (N); |
26bff3d9 JM |
3508 | return; |
3509 | end if; | |
fbf5a39b | 3510 | |
26bff3d9 JM |
3511 | -- If the allocator is for a type which requires initialization, and |
3512 | -- there is no initial value (i.e. operand is a subtype indication | |
685094bf RD |
3513 | -- rather than a qualified expression), then we must generate a call to |
3514 | -- the initialization routine using an expressions action node: | |
70482933 | 3515 | |
26bff3d9 | 3516 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] |
70482933 | 3517 | |
26bff3d9 JM |
3518 | -- Here ptr_T is the pointer type for the allocator, and T is the |
3519 | -- subtype of the allocator. A special case arises if the designated | |
3520 | -- type of the access type is a task or contains tasks. In this case | |
3521 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
3522 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
3523 | -- for details). In addition, if the type T is a task T, then the | |
3524 | -- first argument to Init must be converted to the task record type. | |
70482933 | 3525 | |
26bff3d9 JM |
3526 | declare |
3527 | T : constant Entity_Id := Entity (Expression (N)); | |
3528 | Init : Entity_Id; | |
3529 | Arg1 : Node_Id; | |
3530 | Args : List_Id; | |
3531 | Decls : List_Id; | |
3532 | Decl : Node_Id; | |
3533 | Discr : Elmt_Id; | |
3534 | Flist : Node_Id; | |
3535 | Temp_Decl : Node_Id; | |
3536 | Temp_Type : Entity_Id; | |
3537 | Attach_Level : Uint; | |
70482933 | 3538 | |
26bff3d9 JM |
3539 | begin |
3540 | if No_Initialization (N) then | |
3541 | null; | |
70482933 | 3542 | |
26bff3d9 | 3543 | -- Case of no initialization procedure present |
70482933 | 3544 | |
26bff3d9 | 3545 | elsif not Has_Non_Null_Base_Init_Proc (T) then |
70482933 | 3546 | |
26bff3d9 | 3547 | -- Case of simple initialization required |
70482933 | 3548 | |
26bff3d9 | 3549 | if Needs_Simple_Initialization (T) then |
b4592168 | 3550 | Check_Restriction (No_Default_Initialization, N); |
26bff3d9 JM |
3551 | Rewrite (Expression (N), |
3552 | Make_Qualified_Expression (Loc, | |
3553 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
b4592168 | 3554 | Expression => Get_Simple_Init_Val (T, N))); |
70482933 | 3555 | |
26bff3d9 JM |
3556 | Analyze_And_Resolve (Expression (Expression (N)), T); |
3557 | Analyze_And_Resolve (Expression (N), T); | |
3558 | Set_Paren_Count (Expression (Expression (N)), 1); | |
3559 | Expand_N_Allocator (N); | |
70482933 | 3560 | |
26bff3d9 | 3561 | -- No initialization required |
70482933 RK |
3562 | |
3563 | else | |
26bff3d9 JM |
3564 | null; |
3565 | end if; | |
70482933 | 3566 | |
26bff3d9 | 3567 | -- Case of initialization procedure present, must be called |
70482933 | 3568 | |
26bff3d9 | 3569 | else |
b4592168 | 3570 | Check_Restriction (No_Default_Initialization, N); |
70482933 | 3571 | |
b4592168 GD |
3572 | if not Restriction_Active (No_Default_Initialization) then |
3573 | Init := Base_Init_Proc (T); | |
3574 | Nod := N; | |
191fcb3a | 3575 | Temp := Make_Temporary (Loc, 'P'); |
70482933 | 3576 | |
b4592168 | 3577 | -- Construct argument list for the initialization routine call |
70482933 | 3578 | |
26bff3d9 | 3579 | Arg1 := |
b4592168 GD |
3580 | Make_Explicit_Dereference (Loc, |
3581 | Prefix => New_Reference_To (Temp, Loc)); | |
3582 | Set_Assignment_OK (Arg1); | |
3583 | Temp_Type := PtrT; | |
26bff3d9 | 3584 | |
b4592168 GD |
3585 | -- The initialization procedure expects a specific type. if the |
3586 | -- context is access to class wide, indicate that the object | |
3587 | -- being allocated has the right specific type. | |
70482933 | 3588 | |
b4592168 GD |
3589 | if Is_Class_Wide_Type (Dtyp) then |
3590 | Arg1 := Unchecked_Convert_To (T, Arg1); | |
3591 | end if; | |
70482933 | 3592 | |
b4592168 GD |
3593 | -- If designated type is a concurrent type or if it is private |
3594 | -- type whose definition is a concurrent type, the first | |
3595 | -- argument in the Init routine has to be unchecked conversion | |
3596 | -- to the corresponding record type. If the designated type is | |
3597 | -- a derived type, we also convert the argument to its root | |
3598 | -- type. | |
20b5d666 | 3599 | |
b4592168 GD |
3600 | if Is_Concurrent_Type (T) then |
3601 | Arg1 := | |
3602 | Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1); | |
70482933 | 3603 | |
b4592168 GD |
3604 | elsif Is_Private_Type (T) |
3605 | and then Present (Full_View (T)) | |
3606 | and then Is_Concurrent_Type (Full_View (T)) | |
3607 | then | |
3608 | Arg1 := | |
3609 | Unchecked_Convert_To | |
3610 | (Corresponding_Record_Type (Full_View (T)), Arg1); | |
70482933 | 3611 | |
b4592168 GD |
3612 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then |
3613 | declare | |
3614 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
3615 | begin | |
3616 | Arg1 := OK_Convert_To (Etype (Ftyp), Arg1); | |
3617 | Set_Etype (Arg1, Ftyp); | |
3618 | end; | |
3619 | end if; | |
70482933 | 3620 | |
b4592168 | 3621 | Args := New_List (Arg1); |
70482933 | 3622 | |
b4592168 GD |
3623 | -- For the task case, pass the Master_Id of the access type as |
3624 | -- the value of the _Master parameter, and _Chain as the value | |
3625 | -- of the _Chain parameter (_Chain will be defined as part of | |
3626 | -- the generated code for the allocator). | |
70482933 | 3627 | |
b4592168 GD |
3628 | -- In Ada 2005, the context may be a function that returns an |
3629 | -- anonymous access type. In that case the Master_Id has been | |
3630 | -- created when expanding the function declaration. | |
70482933 | 3631 | |
b4592168 GD |
3632 | if Has_Task (T) then |
3633 | if No (Master_Id (Base_Type (PtrT))) then | |
70482933 | 3634 | |
b4592168 GD |
3635 | -- If we have a non-library level task with restriction |
3636 | -- No_Task_Hierarchy set, then no point in expanding. | |
70482933 | 3637 | |
b4592168 GD |
3638 | if not Is_Library_Level_Entity (T) |
3639 | and then Restriction_Active (No_Task_Hierarchy) | |
26bff3d9 | 3640 | then |
b4592168 | 3641 | return; |
26bff3d9 | 3642 | end if; |
70482933 | 3643 | |
b4592168 GD |
3644 | -- The designated type was an incomplete type, and the |
3645 | -- access type did not get expanded. Salvage it now. | |
70482933 | 3646 | |
b4592168 GD |
3647 | pragma Assert (Present (Parent (Base_Type (PtrT)))); |
3648 | Expand_N_Full_Type_Declaration | |
3649 | (Parent (Base_Type (PtrT))); | |
3650 | end if; | |
70482933 | 3651 | |
b4592168 GD |
3652 | -- If the context of the allocator is a declaration or an |
3653 | -- assignment, we can generate a meaningful image for it, | |
3654 | -- even though subsequent assignments might remove the | |
3655 | -- connection between task and entity. We build this image | |
3656 | -- when the left-hand side is a simple variable, a simple | |
3657 | -- indexed assignment or a simple selected component. | |
3658 | ||
3659 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
3660 | declare | |
3661 | Nam : constant Node_Id := Name (Parent (N)); | |
3662 | ||
3663 | begin | |
3664 | if Is_Entity_Name (Nam) then | |
3665 | Decls := | |
3666 | Build_Task_Image_Decls | |
3667 | (Loc, | |
3668 | New_Occurrence_Of | |
3669 | (Entity (Nam), Sloc (Nam)), T); | |
3670 | ||
3671 | elsif Nkind_In | |
3672 | (Nam, N_Indexed_Component, N_Selected_Component) | |
3673 | and then Is_Entity_Name (Prefix (Nam)) | |
3674 | then | |
3675 | Decls := | |
3676 | Build_Task_Image_Decls | |
3677 | (Loc, Nam, Etype (Prefix (Nam))); | |
3678 | else | |
3679 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3680 | end if; | |
3681 | end; | |
70482933 | 3682 | |
b4592168 GD |
3683 | elsif Nkind (Parent (N)) = N_Object_Declaration then |
3684 | Decls := | |
3685 | Build_Task_Image_Decls | |
3686 | (Loc, Defining_Identifier (Parent (N)), T); | |
70482933 | 3687 | |
b4592168 GD |
3688 | else |
3689 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
3690 | end if; | |
26bff3d9 | 3691 | |
b4592168 GD |
3692 | Append_To (Args, |
3693 | New_Reference_To | |
3694 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); | |
3695 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); | |
26bff3d9 | 3696 | |
b4592168 GD |
3697 | Decl := Last (Decls); |
3698 | Append_To (Args, | |
3699 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
26bff3d9 | 3700 | |
b4592168 | 3701 | -- Has_Task is false, Decls not used |
26bff3d9 | 3702 | |
b4592168 GD |
3703 | else |
3704 | Decls := No_List; | |
26bff3d9 JM |
3705 | end if; |
3706 | ||
b4592168 GD |
3707 | -- Add discriminants if discriminated type |
3708 | ||
3709 | declare | |
3710 | Dis : Boolean := False; | |
3711 | Typ : Entity_Id; | |
3712 | ||
3713 | begin | |
3714 | if Has_Discriminants (T) then | |
3715 | Dis := True; | |
3716 | Typ := T; | |
3717 | ||
3718 | elsif Is_Private_Type (T) | |
3719 | and then Present (Full_View (T)) | |
3720 | and then Has_Discriminants (Full_View (T)) | |
20b5d666 | 3721 | then |
b4592168 GD |
3722 | Dis := True; |
3723 | Typ := Full_View (T); | |
20b5d666 | 3724 | end if; |
70482933 | 3725 | |
b4592168 | 3726 | if Dis then |
26bff3d9 | 3727 | |
b4592168 | 3728 | -- If the allocated object will be constrained by the |
685094bf RD |
3729 | -- default values for discriminants, then build a subtype |
3730 | -- with those defaults, and change the allocated subtype | |
3731 | -- to that. Note that this happens in fewer cases in Ada | |
3732 | -- 2005 (AI-363). | |
26bff3d9 | 3733 | |
b4592168 GD |
3734 | if not Is_Constrained (Typ) |
3735 | and then Present (Discriminant_Default_Value | |
3736 | (First_Discriminant (Typ))) | |
3737 | and then (Ada_Version < Ada_05 | |
3738 | or else | |
3739 | not Has_Constrained_Partial_View (Typ)) | |
20b5d666 | 3740 | then |
b4592168 GD |
3741 | Typ := Build_Default_Subtype (Typ, N); |
3742 | Set_Expression (N, New_Reference_To (Typ, Loc)); | |
20b5d666 JM |
3743 | end if; |
3744 | ||
b4592168 GD |
3745 | Discr := First_Elmt (Discriminant_Constraint (Typ)); |
3746 | while Present (Discr) loop | |
3747 | Nod := Node (Discr); | |
3748 | Append (New_Copy_Tree (Node (Discr)), Args); | |
20b5d666 | 3749 | |
b4592168 GD |
3750 | -- AI-416: when the discriminant constraint is an |
3751 | -- anonymous access type make sure an accessibility | |
3752 | -- check is inserted if necessary (3.10.2(22.q/2)) | |
20b5d666 | 3753 | |
b4592168 GD |
3754 | if Ada_Version >= Ada_05 |
3755 | and then | |
3756 | Ekind (Etype (Nod)) = E_Anonymous_Access_Type | |
3757 | then | |
e84e11ba GD |
3758 | Apply_Accessibility_Check |
3759 | (Nod, Typ, Insert_Node => Nod); | |
b4592168 | 3760 | end if; |
20b5d666 | 3761 | |
b4592168 GD |
3762 | Next_Elmt (Discr); |
3763 | end loop; | |
3764 | end if; | |
3765 | end; | |
70482933 | 3766 | |
b4592168 GD |
3767 | -- We set the allocator as analyzed so that when we analyze the |
3768 | -- expression actions node, we do not get an unwanted recursive | |
3769 | -- expansion of the allocator expression. | |
70482933 | 3770 | |
b4592168 GD |
3771 | Set_Analyzed (N, True); |
3772 | Nod := Relocate_Node (N); | |
70482933 | 3773 | |
b4592168 GD |
3774 | -- Here is the transformation: |
3775 | -- input: new T | |
3776 | -- output: Temp : constant ptr_T := new T; | |
3777 | -- Init (Temp.all, ...); | |
3778 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
3779 | -- <CTRL> Initialize (Finalizable (Temp.all)); | |
70482933 | 3780 | |
b4592168 GD |
3781 | -- Here ptr_T is the pointer type for the allocator, and is the |
3782 | -- subtype of the allocator. | |
70482933 | 3783 | |
b4592168 GD |
3784 | Temp_Decl := |
3785 | Make_Object_Declaration (Loc, | |
3786 | Defining_Identifier => Temp, | |
3787 | Constant_Present => True, | |
3788 | Object_Definition => New_Reference_To (Temp_Type, Loc), | |
3789 | Expression => Nod); | |
70482933 | 3790 | |
b4592168 GD |
3791 | Set_Assignment_OK (Temp_Decl); |
3792 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
70482933 | 3793 | |
b4592168 GD |
3794 | -- If the designated type is a task type or contains tasks, |
3795 | -- create block to activate created tasks, and insert | |
3796 | -- declaration for Task_Image variable ahead of call. | |
70482933 | 3797 | |
b4592168 GD |
3798 | if Has_Task (T) then |
3799 | declare | |
3800 | L : constant List_Id := New_List; | |
3801 | Blk : Node_Id; | |
3802 | begin | |
3803 | Build_Task_Allocate_Block (L, Nod, Args); | |
3804 | Blk := Last (L); | |
3805 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
3806 | Insert_Actions (N, L); | |
3807 | end; | |
70482933 | 3808 | |
b4592168 GD |
3809 | else |
3810 | Insert_Action (N, | |
3811 | Make_Procedure_Call_Statement (Loc, | |
3812 | Name => New_Reference_To (Init, Loc), | |
3813 | Parameter_Associations => Args)); | |
3814 | end if; | |
70482933 | 3815 | |
048e5cef | 3816 | if Needs_Finalization (T) then |
70482933 | 3817 | |
b4592168 GD |
3818 | -- Postpone the generation of a finalization call for the |
3819 | -- current allocator if it acts as a coextension. | |
26bff3d9 | 3820 | |
b4592168 GD |
3821 | if Is_Dynamic_Coextension (N) then |
3822 | if No (Coextensions (N)) then | |
3823 | Set_Coextensions (N, New_Elmt_List); | |
3824 | end if; | |
70482933 | 3825 | |
b4592168 GD |
3826 | Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N)); |
3827 | ||
3828 | else | |
3829 | Flist := | |
3830 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
0669bebe | 3831 | |
b4592168 GD |
3832 | -- Anonymous access types created for access parameters |
3833 | -- are attached to an explicitly constructed controller, | |
3834 | -- which ensures that they can be finalized properly, | |
3835 | -- even if their deallocation might not happen. The list | |
3836 | -- associated with the controller is doubly-linked. For | |
3837 | -- other anonymous access types, the object may end up | |
3838 | -- on the global final list which is singly-linked. | |
3839 | -- Work needed for access discriminants in Ada 2005 ??? | |
0669bebe | 3840 | |
a523b302 | 3841 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
b4592168 GD |
3842 | Attach_Level := Uint_1; |
3843 | else | |
3844 | Attach_Level := Uint_2; | |
3845 | end if; | |
0669bebe | 3846 | |
b4592168 GD |
3847 | Insert_Actions (N, |
3848 | Make_Init_Call ( | |
3849 | Ref => New_Copy_Tree (Arg1), | |
3850 | Typ => T, | |
3851 | Flist_Ref => Flist, | |
3852 | With_Attach => Make_Integer_Literal (Loc, | |
3853 | Intval => Attach_Level))); | |
3854 | end if; | |
70482933 RK |
3855 | end if; |
3856 | ||
b4592168 GD |
3857 | Rewrite (N, New_Reference_To (Temp, Loc)); |
3858 | Analyze_And_Resolve (N, PtrT); | |
3859 | end if; | |
26bff3d9 JM |
3860 | end if; |
3861 | end; | |
f82944b7 | 3862 | |
26bff3d9 JM |
3863 | -- Ada 2005 (AI-251): If the allocator is for a class-wide interface |
3864 | -- object that has been rewritten as a reference, we displace "this" | |
3865 | -- to reference properly its secondary dispatch table. | |
3866 | ||
3867 | if Nkind (N) = N_Identifier | |
f82944b7 JM |
3868 | and then Is_Interface (Dtyp) |
3869 | then | |
26bff3d9 | 3870 | Displace_Allocator_Pointer (N); |
f82944b7 JM |
3871 | end if; |
3872 | ||
fbf5a39b AC |
3873 | exception |
3874 | when RE_Not_Available => | |
3875 | return; | |
70482933 RK |
3876 | end Expand_N_Allocator; |
3877 | ||
3878 | ----------------------- | |
3879 | -- Expand_N_And_Then -- | |
3880 | ----------------------- | |
3881 | ||
5875f8d6 AC |
3882 | procedure Expand_N_And_Then (N : Node_Id) |
3883 | renames Expand_Short_Circuit_Operator; | |
70482933 | 3884 | |
19d846a0 RD |
3885 | ------------------------------ |
3886 | -- Expand_N_Case_Expression -- | |
3887 | ------------------------------ | |
3888 | ||
3889 | procedure Expand_N_Case_Expression (N : Node_Id) is | |
3890 | Loc : constant Source_Ptr := Sloc (N); | |
3891 | Typ : constant Entity_Id := Etype (N); | |
3892 | Cstmt : Node_Id; | |
3893 | Tnn : Entity_Id; | |
3894 | Pnn : Entity_Id; | |
3895 | Actions : List_Id; | |
3896 | Ttyp : Entity_Id; | |
3897 | Alt : Node_Id; | |
3898 | Fexp : Node_Id; | |
3899 | ||
3900 | begin | |
3901 | -- We expand | |
3902 | ||
3903 | -- case X is when A => AX, when B => BX ... | |
3904 | ||
3905 | -- to | |
3906 | ||
3907 | -- do | |
3908 | -- Tnn : typ; | |
3909 | -- case X is | |
3910 | -- when A => | |
3911 | -- Tnn := AX; | |
3912 | -- when B => | |
3913 | -- Tnn := BX; | |
3914 | -- ... | |
3915 | -- end case; | |
3916 | -- in Tnn end; | |
3917 | ||
3918 | -- However, this expansion is wrong for limited types, and also | |
3919 | -- wrong for unconstrained types (since the bounds may not be the | |
3920 | -- same in all branches). Furthermore it involves an extra copy | |
3921 | -- for large objects. So we take care of this by using the following | |
3922 | -- modified expansion for non-scalar types: | |
3923 | ||
3924 | -- do | |
3925 | -- type Pnn is access all typ; | |
3926 | -- Tnn : Pnn; | |
3927 | -- case X is | |
3928 | -- when A => | |
3929 | -- T := AX'Unrestricted_Access; | |
3930 | -- when B => | |
3931 | -- T := BX'Unrestricted_Access; | |
3932 | -- ... | |
3933 | -- end case; | |
3934 | -- in Tnn.all end; | |
3935 | ||
3936 | Cstmt := | |
3937 | Make_Case_Statement (Loc, | |
3938 | Expression => Expression (N), | |
3939 | Alternatives => New_List); | |
3940 | ||
3941 | Actions := New_List; | |
3942 | ||
3943 | -- Scalar case | |
3944 | ||
3945 | if Is_Scalar_Type (Typ) then | |
3946 | Ttyp := Typ; | |
3947 | ||
3948 | else | |
3949 | Pnn := Make_Temporary (Loc, 'P'); | |
3950 | Append_To (Actions, | |
3951 | Make_Full_Type_Declaration (Loc, | |
3952 | Defining_Identifier => Pnn, | |
3953 | Type_Definition => | |
3954 | Make_Access_To_Object_Definition (Loc, | |
3955 | All_Present => True, | |
3956 | Subtype_Indication => | |
3957 | New_Reference_To (Typ, Loc)))); | |
3958 | Ttyp := Pnn; | |
3959 | end if; | |
3960 | ||
3961 | Tnn := Make_Temporary (Loc, 'T'); | |
3962 | Append_To (Actions, | |
3963 | Make_Object_Declaration (Loc, | |
3964 | Defining_Identifier => Tnn, | |
3965 | Object_Definition => New_Occurrence_Of (Ttyp, Loc))); | |
3966 | ||
3967 | -- Now process the alternatives | |
3968 | ||
3969 | Alt := First (Alternatives (N)); | |
3970 | while Present (Alt) loop | |
3971 | declare | |
3972 | Aexp : Node_Id := Expression (Alt); | |
3973 | Aloc : constant Source_Ptr := Sloc (Aexp); | |
3974 | ||
3975 | begin | |
3976 | if not Is_Scalar_Type (Typ) then | |
3977 | Aexp := | |
3978 | Make_Attribute_Reference (Aloc, | |
3979 | Prefix => Relocate_Node (Aexp), | |
3980 | Attribute_Name => Name_Unrestricted_Access); | |
3981 | end if; | |
3982 | ||
3983 | Append_To | |
3984 | (Alternatives (Cstmt), | |
3985 | Make_Case_Statement_Alternative (Sloc (Alt), | |
3986 | Discrete_Choices => Discrete_Choices (Alt), | |
3987 | Statements => New_List ( | |
3988 | Make_Assignment_Statement (Aloc, | |
3989 | Name => New_Occurrence_Of (Tnn, Loc), | |
3990 | Expression => Aexp)))); | |
3991 | end; | |
3992 | ||
3993 | Next (Alt); | |
3994 | end loop; | |
3995 | ||
3996 | Append_To (Actions, Cstmt); | |
3997 | ||
3998 | -- Construct and return final expression with actions | |
3999 | ||
4000 | if Is_Scalar_Type (Typ) then | |
4001 | Fexp := New_Occurrence_Of (Tnn, Loc); | |
4002 | else | |
4003 | Fexp := | |
4004 | Make_Explicit_Dereference (Loc, | |
4005 | Prefix => New_Occurrence_Of (Tnn, Loc)); | |
4006 | end if; | |
4007 | ||
4008 | Rewrite (N, | |
4009 | Make_Expression_With_Actions (Loc, | |
4010 | Expression => Fexp, | |
4011 | Actions => Actions)); | |
4012 | ||
4013 | Analyze_And_Resolve (N, Typ); | |
4014 | end Expand_N_Case_Expression; | |
4015 | ||
70482933 RK |
4016 | ------------------------------------- |
4017 | -- Expand_N_Conditional_Expression -- | |
4018 | ------------------------------------- | |
4019 | ||
305caf42 | 4020 | -- Deal with limited types and expression actions |
70482933 RK |
4021 | |
4022 | procedure Expand_N_Conditional_Expression (N : Node_Id) is | |
4023 | Loc : constant Source_Ptr := Sloc (N); | |
4024 | Cond : constant Node_Id := First (Expressions (N)); | |
4025 | Thenx : constant Node_Id := Next (Cond); | |
4026 | Elsex : constant Node_Id := Next (Thenx); | |
4027 | Typ : constant Entity_Id := Etype (N); | |
c471e2da | 4028 | |
602a7ec0 AC |
4029 | Cnn : Entity_Id; |
4030 | Decl : Node_Id; | |
4031 | New_If : Node_Id; | |
4032 | New_N : Node_Id; | |
4033 | P_Decl : Node_Id; | |
4034 | Expr : Node_Id; | |
4035 | Actions : List_Id; | |
70482933 RK |
4036 | |
4037 | begin | |
602a7ec0 AC |
4038 | -- Fold at compile time if condition known. We have already folded |
4039 | -- static conditional expressions, but it is possible to fold any | |
4040 | -- case in which the condition is known at compile time, even though | |
4041 | -- the result is non-static. | |
4042 | ||
4043 | -- Note that we don't do the fold of such cases in Sem_Elab because | |
4044 | -- it can cause infinite loops with the expander adding a conditional | |
4045 | -- expression, and Sem_Elab circuitry removing it repeatedly. | |
4046 | ||
4047 | if Compile_Time_Known_Value (Cond) then | |
4048 | if Is_True (Expr_Value (Cond)) then | |
4049 | Expr := Thenx; | |
4050 | Actions := Then_Actions (N); | |
4051 | else | |
4052 | Expr := Elsex; | |
4053 | Actions := Else_Actions (N); | |
4054 | end if; | |
4055 | ||
4056 | Remove (Expr); | |
ae77c68b AC |
4057 | |
4058 | if Present (Actions) then | |
4059 | ||
4060 | -- If we are not allowed to use Expression_With_Actions, just | |
4061 | -- skip the optimization, it is not critical for correctness. | |
4062 | ||
4063 | if not Use_Expression_With_Actions then | |
4064 | goto Skip_Optimization; | |
4065 | end if; | |
4066 | ||
4067 | Rewrite (N, | |
4068 | Make_Expression_With_Actions (Loc, | |
4069 | Expression => Relocate_Node (Expr), | |
4070 | Actions => Actions)); | |
4071 | Analyze_And_Resolve (N, Typ); | |
4072 | ||
4073 | else | |
4074 | Rewrite (N, Relocate_Node (Expr)); | |
4075 | end if; | |
602a7ec0 AC |
4076 | |
4077 | -- Note that the result is never static (legitimate cases of static | |
4078 | -- conditional expressions were folded in Sem_Eval). | |
4079 | ||
4080 | Set_Is_Static_Expression (N, False); | |
4081 | return; | |
4082 | end if; | |
4083 | ||
ae77c68b AC |
4084 | <<Skip_Optimization>> |
4085 | ||
305caf42 AC |
4086 | -- If the type is limited or unconstrained, we expand as follows to |
4087 | -- avoid any possibility of improper copies. | |
70482933 | 4088 | |
305caf42 AC |
4089 | -- Note: it may be possible to avoid this special processing if the |
4090 | -- back end uses its own mechanisms for handling by-reference types ??? | |
ac7120ce | 4091 | |
c471e2da AC |
4092 | -- type Ptr is access all Typ; |
4093 | -- Cnn : Ptr; | |
ac7120ce RD |
4094 | -- if cond then |
4095 | -- <<then actions>> | |
4096 | -- Cnn := then-expr'Unrestricted_Access; | |
4097 | -- else | |
4098 | -- <<else actions>> | |
4099 | -- Cnn := else-expr'Unrestricted_Access; | |
4100 | -- end if; | |
4101 | ||
c471e2da | 4102 | -- and replace the conditional expresion by a reference to Cnn.all. |
ac7120ce | 4103 | |
305caf42 AC |
4104 | -- This special case can be skipped if the back end handles limited |
4105 | -- types properly and ensures that no incorrect copies are made. | |
4106 | ||
4107 | if Is_By_Reference_Type (Typ) | |
4108 | and then not Back_End_Handles_Limited_Types | |
4109 | then | |
faf387e1 | 4110 | Cnn := Make_Temporary (Loc, 'C', N); |
70482933 | 4111 | |
c471e2da AC |
4112 | P_Decl := |
4113 | Make_Full_Type_Declaration (Loc, | |
191fcb3a | 4114 | Defining_Identifier => Make_Temporary (Loc, 'A'), |
c471e2da AC |
4115 | Type_Definition => |
4116 | Make_Access_To_Object_Definition (Loc, | |
4117 | All_Present => True, | |
4118 | Subtype_Indication => | |
4119 | New_Reference_To (Typ, Loc))); | |
4120 | ||
4121 | Insert_Action (N, P_Decl); | |
4122 | ||
4123 | Decl := | |
4124 | Make_Object_Declaration (Loc, | |
4125 | Defining_Identifier => Cnn, | |
4126 | Object_Definition => | |
4127 | New_Occurrence_Of (Defining_Identifier (P_Decl), Loc)); | |
4128 | ||
70482933 RK |
4129 | New_If := |
4130 | Make_Implicit_If_Statement (N, | |
4131 | Condition => Relocate_Node (Cond), | |
4132 | ||
4133 | Then_Statements => New_List ( | |
4134 | Make_Assignment_Statement (Sloc (Thenx), | |
4135 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
c471e2da AC |
4136 | Expression => |
4137 | Make_Attribute_Reference (Loc, | |
4138 | Attribute_Name => Name_Unrestricted_Access, | |
4139 | Prefix => Relocate_Node (Thenx)))), | |
70482933 RK |
4140 | |
4141 | Else_Statements => New_List ( | |
4142 | Make_Assignment_Statement (Sloc (Elsex), | |
4143 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
c471e2da AC |
4144 | Expression => |
4145 | Make_Attribute_Reference (Loc, | |
4146 | Attribute_Name => Name_Unrestricted_Access, | |
4147 | Prefix => Relocate_Node (Elsex))))); | |
70482933 | 4148 | |
c471e2da AC |
4149 | New_N := |
4150 | Make_Explicit_Dereference (Loc, | |
4151 | Prefix => New_Occurrence_Of (Cnn, Loc)); | |
fb1949a0 | 4152 | |
c471e2da AC |
4153 | -- For other types, we only need to expand if there are other actions |
4154 | -- associated with either branch. | |
4155 | ||
4156 | elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
c471e2da | 4157 | |
305caf42 AC |
4158 | -- We have two approaches to handling this. If we are allowed to use |
4159 | -- N_Expression_With_Actions, then we can just wrap the actions into | |
4160 | -- the appropriate expression. | |
4161 | ||
4162 | if Use_Expression_With_Actions then | |
4163 | if Present (Then_Actions (N)) then | |
4164 | Rewrite (Thenx, | |
4165 | Make_Expression_With_Actions (Sloc (Thenx), | |
4166 | Actions => Then_Actions (N), | |
4167 | Expression => Relocate_Node (Thenx))); | |
48b351d9 | 4168 | Set_Then_Actions (N, No_List); |
305caf42 AC |
4169 | Analyze_And_Resolve (Thenx, Typ); |
4170 | end if; | |
c471e2da | 4171 | |
305caf42 AC |
4172 | if Present (Else_Actions (N)) then |
4173 | Rewrite (Elsex, | |
4174 | Make_Expression_With_Actions (Sloc (Elsex), | |
4175 | Actions => Else_Actions (N), | |
4176 | Expression => Relocate_Node (Elsex))); | |
48b351d9 | 4177 | Set_Else_Actions (N, No_List); |
305caf42 AC |
4178 | Analyze_And_Resolve (Elsex, Typ); |
4179 | end if; | |
c471e2da | 4180 | |
305caf42 | 4181 | return; |
c471e2da | 4182 | |
305caf42 AC |
4183 | -- if we can't use N_Expression_With_Actions nodes, then we insert |
4184 | -- the following sequence of actions (using Insert_Actions): | |
fb1949a0 | 4185 | |
305caf42 AC |
4186 | -- Cnn : typ; |
4187 | -- if cond then | |
4188 | -- <<then actions>> | |
4189 | -- Cnn := then-expr; | |
4190 | -- else | |
4191 | -- <<else actions>> | |
4192 | -- Cnn := else-expr | |
4193 | -- end if; | |
fbf5a39b | 4194 | |
305caf42 | 4195 | -- and replace the conditional expression by a reference to Cnn |
70482933 | 4196 | |
305caf42 AC |
4197 | else |
4198 | Cnn := Make_Temporary (Loc, 'C', N); | |
4199 | ||
4200 | Decl := | |
4201 | Make_Object_Declaration (Loc, | |
4202 | Defining_Identifier => Cnn, | |
4203 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
4204 | ||
4205 | New_If := | |
4206 | Make_Implicit_If_Statement (N, | |
4207 | Condition => Relocate_Node (Cond), | |
4208 | ||
4209 | Then_Statements => New_List ( | |
4210 | Make_Assignment_Statement (Sloc (Thenx), | |
4211 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
4212 | Expression => Relocate_Node (Thenx))), | |
4213 | ||
4214 | Else_Statements => New_List ( | |
4215 | Make_Assignment_Statement (Sloc (Elsex), | |
4216 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
4217 | Expression => Relocate_Node (Elsex)))); | |
70482933 | 4218 | |
305caf42 AC |
4219 | Set_Assignment_OK (Name (First (Then_Statements (New_If)))); |
4220 | Set_Assignment_OK (Name (First (Else_Statements (New_If)))); | |
4221 | ||
4222 | New_N := New_Occurrence_Of (Cnn, Loc); | |
4223 | end if; | |
4224 | ||
4225 | -- If no actions then no expansion needed, gigi will handle it using | |
4226 | -- the same approach as a C conditional expression. | |
4227 | ||
4228 | else | |
c471e2da AC |
4229 | return; |
4230 | end if; | |
4231 | ||
305caf42 AC |
4232 | -- Fall through here for either the limited expansion, or the case of |
4233 | -- inserting actions for non-limited types. In both these cases, we must | |
4234 | -- move the SLOC of the parent If statement to the newly created one and | |
3fc5d116 RD |
4235 | -- change it to the SLOC of the expression which, after expansion, will |
4236 | -- correspond to what is being evaluated. | |
c471e2da AC |
4237 | |
4238 | if Present (Parent (N)) | |
4239 | and then Nkind (Parent (N)) = N_If_Statement | |
4240 | then | |
4241 | Set_Sloc (New_If, Sloc (Parent (N))); | |
4242 | Set_Sloc (Parent (N), Loc); | |
4243 | end if; | |
70482933 | 4244 | |
3fc5d116 RD |
4245 | -- Make sure Then_Actions and Else_Actions are appropriately moved |
4246 | -- to the new if statement. | |
4247 | ||
c471e2da AC |
4248 | if Present (Then_Actions (N)) then |
4249 | Insert_List_Before | |
4250 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
70482933 | 4251 | end if; |
c471e2da AC |
4252 | |
4253 | if Present (Else_Actions (N)) then | |
4254 | Insert_List_Before | |
4255 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
4256 | end if; | |
4257 | ||
4258 | Insert_Action (N, Decl); | |
4259 | Insert_Action (N, New_If); | |
4260 | Rewrite (N, New_N); | |
4261 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
4262 | end Expand_N_Conditional_Expression; |
4263 | ||
4264 | ----------------------------------- | |
4265 | -- Expand_N_Explicit_Dereference -- | |
4266 | ----------------------------------- | |
4267 | ||
4268 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
4269 | begin | |
dfd99a80 | 4270 | -- Insert explicit dereference call for the checked storage pool case |
70482933 RK |
4271 | |
4272 | Insert_Dereference_Action (Prefix (N)); | |
4273 | end Expand_N_Explicit_Dereference; | |
4274 | ||
4275 | ----------------- | |
4276 | -- Expand_N_In -- | |
4277 | ----------------- | |
4278 | ||
4279 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 AC |
4280 | Loc : constant Source_Ptr := Sloc (N); |
4281 | Rtyp : constant Entity_Id := Etype (N); | |
4282 | Lop : constant Node_Id := Left_Opnd (N); | |
4283 | Rop : constant Node_Id := Right_Opnd (N); | |
4284 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 | 4285 | |
197e4514 AC |
4286 | procedure Expand_Set_Membership; |
4287 | -- For each disjunct we create a simple equality or membership test. | |
4288 | -- The whole membership is rewritten as a short-circuit disjunction. | |
4289 | ||
4290 | --------------------------- | |
4291 | -- Expand_Set_Membership -- | |
4292 | --------------------------- | |
4293 | ||
4294 | procedure Expand_Set_Membership is | |
4295 | Alt : Node_Id; | |
4296 | Res : Node_Id; | |
4297 | ||
4298 | function Make_Cond (Alt : Node_Id) return Node_Id; | |
4299 | -- If the alternative is a subtype mark, create a simple membership | |
4300 | -- test. Otherwise create an equality test for it. | |
4301 | ||
4302 | --------------- | |
4303 | -- Make_Cond -- | |
4304 | --------------- | |
4305 | ||
4306 | function Make_Cond (Alt : Node_Id) return Node_Id is | |
4307 | Cond : Node_Id; | |
4308 | L : constant Node_Id := New_Copy (Lop); | |
4309 | R : constant Node_Id := Relocate_Node (Alt); | |
4310 | ||
4311 | begin | |
4312 | if Is_Entity_Name (Alt) | |
4313 | and then Is_Type (Entity (Alt)) | |
4314 | then | |
4315 | Cond := | |
4316 | Make_In (Sloc (Alt), | |
4317 | Left_Opnd => L, | |
4318 | Right_Opnd => R); | |
4319 | else | |
4320 | Cond := Make_Op_Eq (Sloc (Alt), | |
4321 | Left_Opnd => L, | |
4322 | Right_Opnd => R); | |
4323 | end if; | |
4324 | ||
4325 | return Cond; | |
4326 | end Make_Cond; | |
4327 | ||
4328 | -- Start of proessing for Expand_N_In | |
4329 | ||
4330 | begin | |
4331 | Alt := Last (Alternatives (N)); | |
4332 | Res := Make_Cond (Alt); | |
4333 | ||
4334 | Prev (Alt); | |
4335 | while Present (Alt) loop | |
4336 | Res := | |
4337 | Make_Or_Else (Sloc (Alt), | |
4338 | Left_Opnd => Make_Cond (Alt), | |
4339 | Right_Opnd => Res); | |
4340 | Prev (Alt); | |
4341 | end loop; | |
4342 | ||
4343 | Rewrite (N, Res); | |
4344 | Analyze_And_Resolve (N, Standard_Boolean); | |
4345 | end Expand_Set_Membership; | |
4346 | ||
630d30e9 RD |
4347 | procedure Substitute_Valid_Check; |
4348 | -- Replaces node N by Lop'Valid. This is done when we have an explicit | |
4349 | -- test for the left operand being in range of its subtype. | |
4350 | ||
4351 | ---------------------------- | |
4352 | -- Substitute_Valid_Check -- | |
4353 | ---------------------------- | |
4354 | ||
4355 | procedure Substitute_Valid_Check is | |
4356 | begin | |
4357 | Rewrite (N, | |
4358 | Make_Attribute_Reference (Loc, | |
4359 | Prefix => Relocate_Node (Lop), | |
4360 | Attribute_Name => Name_Valid)); | |
4361 | ||
4362 | Analyze_And_Resolve (N, Rtyp); | |
4363 | ||
4364 | Error_Msg_N ("?explicit membership test may be optimized away", N); | |
305caf42 AC |
4365 | Error_Msg_N -- CODEFIX |
4366 | ("\?use ''Valid attribute instead", N); | |
630d30e9 RD |
4367 | return; |
4368 | end Substitute_Valid_Check; | |
4369 | ||
4370 | -- Start of processing for Expand_N_In | |
4371 | ||
70482933 | 4372 | begin |
197e4514 AC |
4373 | if Present (Alternatives (N)) then |
4374 | Remove_Side_Effects (Lop); | |
4375 | Expand_Set_Membership; | |
4376 | return; | |
4377 | end if; | |
4378 | ||
630d30e9 RD |
4379 | -- Check case of explicit test for an expression in range of its |
4380 | -- subtype. This is suspicious usage and we replace it with a 'Valid | |
545cb5be AC |
4381 | -- test and give a warning. For floating point types however, this |
4382 | -- is a standard way to check for finite numbers, and using 'Valid | |
4383 | -- would typically be a pessimization | |
630d30e9 RD |
4384 | |
4385 | if Is_Scalar_Type (Etype (Lop)) | |
545cb5be | 4386 | and then not Is_Floating_Point_Type (Etype (Lop)) |
630d30e9 RD |
4387 | and then Nkind (Rop) in N_Has_Entity |
4388 | and then Etype (Lop) = Entity (Rop) | |
4389 | and then Comes_From_Source (N) | |
26bff3d9 | 4390 | and then VM_Target = No_VM |
630d30e9 RD |
4391 | then |
4392 | Substitute_Valid_Check; | |
4393 | return; | |
4394 | end if; | |
4395 | ||
20b5d666 JM |
4396 | -- Do validity check on operands |
4397 | ||
4398 | if Validity_Checks_On and Validity_Check_Operands then | |
4399 | Ensure_Valid (Left_Opnd (N)); | |
4400 | Validity_Check_Range (Right_Opnd (N)); | |
4401 | end if; | |
4402 | ||
630d30e9 | 4403 | -- Case of explicit range |
fbf5a39b AC |
4404 | |
4405 | if Nkind (Rop) = N_Range then | |
4406 | declare | |
630d30e9 RD |
4407 | Lo : constant Node_Id := Low_Bound (Rop); |
4408 | Hi : constant Node_Id := High_Bound (Rop); | |
4409 | ||
d766cee3 RD |
4410 | Ltyp : constant Entity_Id := Etype (Lop); |
4411 | ||
630d30e9 RD |
4412 | Lo_Orig : constant Node_Id := Original_Node (Lo); |
4413 | Hi_Orig : constant Node_Id := Original_Node (Hi); | |
4414 | ||
c800f862 RD |
4415 | Lcheck : Compare_Result; |
4416 | Ucheck : Compare_Result; | |
fbf5a39b | 4417 | |
d766cee3 RD |
4418 | Warn1 : constant Boolean := |
4419 | Constant_Condition_Warnings | |
c800f862 RD |
4420 | and then Comes_From_Source (N) |
4421 | and then not In_Instance; | |
d766cee3 RD |
4422 | -- This must be true for any of the optimization warnings, we |
4423 | -- clearly want to give them only for source with the flag on. | |
c800f862 RD |
4424 | -- We also skip these warnings in an instance since it may be |
4425 | -- the case that different instantiations have different ranges. | |
d766cee3 RD |
4426 | |
4427 | Warn2 : constant Boolean := | |
4428 | Warn1 | |
4429 | and then Nkind (Original_Node (Rop)) = N_Range | |
4430 | and then Is_Integer_Type (Etype (Lo)); | |
4431 | -- For the case where only one bound warning is elided, we also | |
4432 | -- insist on an explicit range and an integer type. The reason is | |
4433 | -- that the use of enumeration ranges including an end point is | |
4434 | -- common, as is the use of a subtype name, one of whose bounds | |
4435 | -- is the same as the type of the expression. | |
4436 | ||
fbf5a39b | 4437 | begin |
630d30e9 RD |
4438 | -- If test is explicit x'first .. x'last, replace by valid check |
4439 | ||
d766cee3 | 4440 | if Is_Scalar_Type (Ltyp) |
630d30e9 RD |
4441 | and then Nkind (Lo_Orig) = N_Attribute_Reference |
4442 | and then Attribute_Name (Lo_Orig) = Name_First | |
4443 | and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity | |
d766cee3 | 4444 | and then Entity (Prefix (Lo_Orig)) = Ltyp |
630d30e9 RD |
4445 | and then Nkind (Hi_Orig) = N_Attribute_Reference |
4446 | and then Attribute_Name (Hi_Orig) = Name_Last | |
4447 | and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity | |
d766cee3 | 4448 | and then Entity (Prefix (Hi_Orig)) = Ltyp |
630d30e9 | 4449 | and then Comes_From_Source (N) |
26bff3d9 | 4450 | and then VM_Target = No_VM |
630d30e9 RD |
4451 | then |
4452 | Substitute_Valid_Check; | |
4453 | return; | |
4454 | end if; | |
4455 | ||
d766cee3 RD |
4456 | -- If bounds of type are known at compile time, and the end points |
4457 | -- are known at compile time and identical, this is another case | |
4458 | -- for substituting a valid test. We only do this for discrete | |
4459 | -- types, since it won't arise in practice for float types. | |
4460 | ||
4461 | if Comes_From_Source (N) | |
4462 | and then Is_Discrete_Type (Ltyp) | |
4463 | and then Compile_Time_Known_Value (Type_High_Bound (Ltyp)) | |
4464 | and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp)) | |
4465 | and then Compile_Time_Known_Value (Lo) | |
4466 | and then Compile_Time_Known_Value (Hi) | |
4467 | and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi) | |
4468 | and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo) | |
94eefd2e RD |
4469 | |
4470 | -- Kill warnings in instances, since they may be cases where we | |
4471 | -- have a test in the generic that makes sense with some types | |
4472 | -- and not with other types. | |
4473 | ||
4474 | and then not In_Instance | |
d766cee3 RD |
4475 | then |
4476 | Substitute_Valid_Check; | |
4477 | return; | |
4478 | end if; | |
4479 | ||
630d30e9 RD |
4480 | -- If we have an explicit range, do a bit of optimization based |
4481 | -- on range analysis (we may be able to kill one or both checks). | |
4482 | ||
c800f862 RD |
4483 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False); |
4484 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False); | |
4485 | ||
630d30e9 RD |
4486 | -- If either check is known to fail, replace result by False since |
4487 | -- the other check does not matter. Preserve the static flag for | |
4488 | -- legality checks, because we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
4489 | |
4490 | if Lcheck = LT or else Ucheck = GT then | |
c800f862 | 4491 | if Warn1 then |
ed2233dc AC |
4492 | Error_Msg_N ("?range test optimized away", N); |
4493 | Error_Msg_N ("\?value is known to be out of range", N); | |
d766cee3 RD |
4494 | end if; |
4495 | ||
fbf5a39b AC |
4496 | Rewrite (N, |
4497 | New_Reference_To (Standard_False, Loc)); | |
4498 | Analyze_And_Resolve (N, Rtyp); | |
7324bf49 | 4499 | Set_Is_Static_Expression (N, Static); |
d766cee3 | 4500 | |
fbf5a39b AC |
4501 | return; |
4502 | ||
685094bf RD |
4503 | -- If both checks are known to succeed, replace result by True, |
4504 | -- since we know we are in range. | |
fbf5a39b AC |
4505 | |
4506 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
c800f862 | 4507 | if Warn1 then |
ed2233dc AC |
4508 | Error_Msg_N ("?range test optimized away", N); |
4509 | Error_Msg_N ("\?value is known to be in range", N); | |
d766cee3 RD |
4510 | end if; |
4511 | ||
fbf5a39b AC |
4512 | Rewrite (N, |
4513 | New_Reference_To (Standard_True, Loc)); | |
4514 | Analyze_And_Resolve (N, Rtyp); | |
7324bf49 | 4515 | Set_Is_Static_Expression (N, Static); |
d766cee3 | 4516 | |
fbf5a39b AC |
4517 | return; |
4518 | ||
d766cee3 RD |
4519 | -- If lower bound check succeeds and upper bound check is not |
4520 | -- known to succeed or fail, then replace the range check with | |
4521 | -- a comparison against the upper bound. | |
fbf5a39b AC |
4522 | |
4523 | elsif Lcheck in Compare_GE then | |
94eefd2e | 4524 | if Warn2 and then not In_Instance then |
ed2233dc AC |
4525 | Error_Msg_N ("?lower bound test optimized away", Lo); |
4526 | Error_Msg_N ("\?value is known to be in range", Lo); | |
d766cee3 RD |
4527 | end if; |
4528 | ||
fbf5a39b AC |
4529 | Rewrite (N, |
4530 | Make_Op_Le (Loc, | |
4531 | Left_Opnd => Lop, | |
4532 | Right_Opnd => High_Bound (Rop))); | |
4533 | Analyze_And_Resolve (N, Rtyp); | |
d766cee3 | 4534 | |
fbf5a39b AC |
4535 | return; |
4536 | ||
d766cee3 RD |
4537 | -- If upper bound check succeeds and lower bound check is not |
4538 | -- known to succeed or fail, then replace the range check with | |
4539 | -- a comparison against the lower bound. | |
fbf5a39b AC |
4540 | |
4541 | elsif Ucheck in Compare_LE then | |
94eefd2e | 4542 | if Warn2 and then not In_Instance then |
ed2233dc AC |
4543 | Error_Msg_N ("?upper bound test optimized away", Hi); |
4544 | Error_Msg_N ("\?value is known to be in range", Hi); | |
d766cee3 RD |
4545 | end if; |
4546 | ||
fbf5a39b AC |
4547 | Rewrite (N, |
4548 | Make_Op_Ge (Loc, | |
4549 | Left_Opnd => Lop, | |
4550 | Right_Opnd => Low_Bound (Rop))); | |
4551 | Analyze_And_Resolve (N, Rtyp); | |
d766cee3 | 4552 | |
fbf5a39b AC |
4553 | return; |
4554 | end if; | |
c800f862 RD |
4555 | |
4556 | -- We couldn't optimize away the range check, but there is one | |
4557 | -- more issue. If we are checking constant conditionals, then we | |
4558 | -- see if we can determine the outcome assuming everything is | |
4559 | -- valid, and if so give an appropriate warning. | |
4560 | ||
4561 | if Warn1 and then not Assume_No_Invalid_Values then | |
4562 | Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True); | |
4563 | Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True); | |
4564 | ||
4565 | -- Result is out of range for valid value | |
4566 | ||
4567 | if Lcheck = LT or else Ucheck = GT then | |
ed2233dc | 4568 | Error_Msg_N |
c800f862 RD |
4569 | ("?value can only be in range if it is invalid", N); |
4570 | ||
4571 | -- Result is in range for valid value | |
4572 | ||
4573 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
ed2233dc | 4574 | Error_Msg_N |
c800f862 RD |
4575 | ("?value can only be out of range if it is invalid", N); |
4576 | ||
4577 | -- Lower bound check succeeds if value is valid | |
4578 | ||
4579 | elsif Warn2 and then Lcheck in Compare_GE then | |
ed2233dc | 4580 | Error_Msg_N |
c800f862 RD |
4581 | ("?lower bound check only fails if it is invalid", Lo); |
4582 | ||
4583 | -- Upper bound check succeeds if value is valid | |
4584 | ||
4585 | elsif Warn2 and then Ucheck in Compare_LE then | |
ed2233dc | 4586 | Error_Msg_N |
c800f862 RD |
4587 | ("?upper bound check only fails for invalid values", Hi); |
4588 | end if; | |
4589 | end if; | |
fbf5a39b AC |
4590 | end; |
4591 | ||
4592 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 4593 | |
70482933 RK |
4594 | return; |
4595 | ||
4596 | -- Here right operand is a subtype mark | |
4597 | ||
4598 | else | |
4599 | declare | |
82878151 AC |
4600 | Typ : Entity_Id := Etype (Rop); |
4601 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
4602 | Cond : Node_Id := Empty; | |
4603 | New_N : Node_Id; | |
4604 | Obj : Node_Id := Lop; | |
4605 | SCIL_Node : Node_Id; | |
70482933 RK |
4606 | |
4607 | begin | |
4608 | Remove_Side_Effects (Obj); | |
4609 | ||
4610 | -- For tagged type, do tagged membership operation | |
4611 | ||
4612 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 4613 | |
26bff3d9 JM |
4614 | -- No expansion will be performed when VM_Target, as the VM |
4615 | -- back-ends will handle the membership tests directly (tags | |
4616 | -- are not explicitly represented in Java objects, so the | |
4617 | -- normal tagged membership expansion is not what we want). | |
70482933 | 4618 | |
1f110335 | 4619 | if Tagged_Type_Expansion then |
82878151 AC |
4620 | Tagged_Membership (N, SCIL_Node, New_N); |
4621 | Rewrite (N, New_N); | |
70482933 | 4622 | Analyze_And_Resolve (N, Rtyp); |
82878151 AC |
4623 | |
4624 | -- Update decoration of relocated node referenced by the | |
4625 | -- SCIL node. | |
4626 | ||
4627 | if Generate_SCIL | |
4628 | and then Present (SCIL_Node) | |
4629 | then | |
7665e4bd | 4630 | Set_SCIL_Node (N, SCIL_Node); |
82878151 | 4631 | end if; |
70482933 RK |
4632 | end if; |
4633 | ||
4634 | return; | |
4635 | ||
20b5d666 | 4636 | -- If type is scalar type, rewrite as x in t'first .. t'last. |
70482933 | 4637 | -- This reason we do this is that the bounds may have the wrong |
c800f862 RD |
4638 | -- type if they come from the original type definition. Also this |
4639 | -- way we get all the processing above for an explicit range. | |
70482933 RK |
4640 | |
4641 | elsif Is_Scalar_Type (Typ) then | |
fbf5a39b | 4642 | Rewrite (Rop, |
70482933 RK |
4643 | Make_Range (Loc, |
4644 | Low_Bound => | |
4645 | Make_Attribute_Reference (Loc, | |
4646 | Attribute_Name => Name_First, | |
4647 | Prefix => New_Reference_To (Typ, Loc)), | |
4648 | ||
4649 | High_Bound => | |
4650 | Make_Attribute_Reference (Loc, | |
4651 | Attribute_Name => Name_Last, | |
4652 | Prefix => New_Reference_To (Typ, Loc)))); | |
4653 | Analyze_And_Resolve (N, Rtyp); | |
4654 | return; | |
5d09245e AC |
4655 | |
4656 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
4657 | -- a membership test if the subtype mark denotes a constrained | |
4658 | -- Unchecked_Union subtype and the expression lacks inferable | |
4659 | -- discriminants. | |
4660 | ||
4661 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
4662 | and then Is_Constrained (Typ) | |
4663 | and then not Has_Inferable_Discriminants (Lop) | |
4664 | then | |
4665 | Insert_Action (N, | |
4666 | Make_Raise_Program_Error (Loc, | |
4667 | Reason => PE_Unchecked_Union_Restriction)); | |
4668 | ||
4669 | -- Prevent Gigi from generating incorrect code by rewriting | |
4670 | -- the test as a standard False. | |
4671 | ||
4672 | Rewrite (N, | |
4673 | New_Occurrence_Of (Standard_False, Loc)); | |
4674 | ||
4675 | return; | |
70482933 RK |
4676 | end if; |
4677 | ||
fbf5a39b AC |
4678 | -- Here we have a non-scalar type |
4679 | ||
70482933 RK |
4680 | if Is_Acc then |
4681 | Typ := Designated_Type (Typ); | |
4682 | end if; | |
4683 | ||
4684 | if not Is_Constrained (Typ) then | |
4685 | Rewrite (N, | |
4686 | New_Reference_To (Standard_True, Loc)); | |
4687 | Analyze_And_Resolve (N, Rtyp); | |
4688 | ||
685094bf RD |
4689 | -- For the constrained array case, we have to check the subscripts |
4690 | -- for an exact match if the lengths are non-zero (the lengths | |
4691 | -- must match in any case). | |
70482933 RK |
4692 | |
4693 | elsif Is_Array_Type (Typ) then | |
4694 | ||
fbf5a39b | 4695 | Check_Subscripts : declare |
70482933 | 4696 | function Construct_Attribute_Reference |
2e071734 AC |
4697 | (E : Node_Id; |
4698 | Nam : Name_Id; | |
4699 | Dim : Nat) return Node_Id; | |
70482933 RK |
4700 | -- Build attribute reference E'Nam(Dim) |
4701 | ||
fbf5a39b AC |
4702 | ----------------------------------- |
4703 | -- Construct_Attribute_Reference -- | |
4704 | ----------------------------------- | |
4705 | ||
70482933 | 4706 | function Construct_Attribute_Reference |
2e071734 AC |
4707 | (E : Node_Id; |
4708 | Nam : Name_Id; | |
4709 | Dim : Nat) return Node_Id | |
70482933 RK |
4710 | is |
4711 | begin | |
4712 | return | |
4713 | Make_Attribute_Reference (Loc, | |
4714 | Prefix => E, | |
4715 | Attribute_Name => Nam, | |
4716 | Expressions => New_List ( | |
4717 | Make_Integer_Literal (Loc, Dim))); | |
4718 | end Construct_Attribute_Reference; | |
4719 | ||
fad0600d | 4720 | -- Start of processing for Check_Subscripts |
fbf5a39b | 4721 | |
70482933 RK |
4722 | begin |
4723 | for J in 1 .. Number_Dimensions (Typ) loop | |
4724 | Evolve_And_Then (Cond, | |
4725 | Make_Op_Eq (Loc, | |
4726 | Left_Opnd => | |
4727 | Construct_Attribute_Reference | |
fbf5a39b AC |
4728 | (Duplicate_Subexpr_No_Checks (Obj), |
4729 | Name_First, J), | |
70482933 RK |
4730 | Right_Opnd => |
4731 | Construct_Attribute_Reference | |
4732 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); | |
4733 | ||
4734 | Evolve_And_Then (Cond, | |
4735 | Make_Op_Eq (Loc, | |
4736 | Left_Opnd => | |
4737 | Construct_Attribute_Reference | |
fbf5a39b AC |
4738 | (Duplicate_Subexpr_No_Checks (Obj), |
4739 | Name_Last, J), | |
70482933 RK |
4740 | Right_Opnd => |
4741 | Construct_Attribute_Reference | |
4742 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); | |
4743 | end loop; | |
4744 | ||
4745 | if Is_Acc then | |
fbf5a39b AC |
4746 | Cond := |
4747 | Make_Or_Else (Loc, | |
4748 | Left_Opnd => | |
4749 | Make_Op_Eq (Loc, | |
4750 | Left_Opnd => Obj, | |
4751 | Right_Opnd => Make_Null (Loc)), | |
4752 | Right_Opnd => Cond); | |
70482933 RK |
4753 | end if; |
4754 | ||
4755 | Rewrite (N, Cond); | |
4756 | Analyze_And_Resolve (N, Rtyp); | |
fbf5a39b | 4757 | end Check_Subscripts; |
70482933 | 4758 | |
685094bf RD |
4759 | -- These are the cases where constraint checks may be required, |
4760 | -- e.g. records with possible discriminants | |
70482933 RK |
4761 | |
4762 | else | |
4763 | -- Expand the test into a series of discriminant comparisons. | |
685094bf RD |
4764 | -- The expression that is built is the negation of the one that |
4765 | -- is used for checking discriminant constraints. | |
70482933 RK |
4766 | |
4767 | Obj := Relocate_Node (Left_Opnd (N)); | |
4768 | ||
4769 | if Has_Discriminants (Typ) then | |
4770 | Cond := Make_Op_Not (Loc, | |
4771 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
4772 | ||
4773 | if Is_Acc then | |
4774 | Cond := Make_Or_Else (Loc, | |
4775 | Left_Opnd => | |
4776 | Make_Op_Eq (Loc, | |
4777 | Left_Opnd => Obj, | |
4778 | Right_Opnd => Make_Null (Loc)), | |
4779 | Right_Opnd => Cond); | |
4780 | end if; | |
4781 | ||
4782 | else | |
4783 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
4784 | end if; | |
4785 | ||
4786 | Rewrite (N, Cond); | |
4787 | Analyze_And_Resolve (N, Rtyp); | |
4788 | end if; | |
4789 | end; | |
4790 | end if; | |
4791 | end Expand_N_In; | |
4792 | ||
4793 | -------------------------------- | |
4794 | -- Expand_N_Indexed_Component -- | |
4795 | -------------------------------- | |
4796 | ||
4797 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
4798 | Loc : constant Source_Ptr := Sloc (N); | |
4799 | Typ : constant Entity_Id := Etype (N); | |
4800 | P : constant Node_Id := Prefix (N); | |
4801 | T : constant Entity_Id := Etype (P); | |
4802 | ||
4803 | begin | |
685094bf RD |
4804 | -- A special optimization, if we have an indexed component that is |
4805 | -- selecting from a slice, then we can eliminate the slice, since, for | |
4806 | -- example, x (i .. j)(k) is identical to x(k). The only difference is | |
4807 | -- the range check required by the slice. The range check for the slice | |
4808 | -- itself has already been generated. The range check for the | |
4809 | -- subscripting operation is ensured by converting the subject to | |
4810 | -- the subtype of the slice. | |
4811 | ||
4812 | -- This optimization not only generates better code, avoiding slice | |
4813 | -- messing especially in the packed case, but more importantly bypasses | |
4814 | -- some problems in handling this peculiar case, for example, the issue | |
4815 | -- of dealing specially with object renamings. | |
70482933 RK |
4816 | |
4817 | if Nkind (P) = N_Slice then | |
4818 | Rewrite (N, | |
4819 | Make_Indexed_Component (Loc, | |
4820 | Prefix => Prefix (P), | |
4821 | Expressions => New_List ( | |
4822 | Convert_To | |
4823 | (Etype (First_Index (Etype (P))), | |
4824 | First (Expressions (N)))))); | |
4825 | Analyze_And_Resolve (N, Typ); | |
4826 | return; | |
4827 | end if; | |
4828 | ||
b4592168 GD |
4829 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
4830 | -- function, then additional actuals must be passed. | |
4831 | ||
4832 | if Ada_Version >= Ada_05 | |
4833 | and then Is_Build_In_Place_Function_Call (P) | |
4834 | then | |
4835 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
4836 | end if; | |
4837 | ||
685094bf | 4838 | -- If the prefix is an access type, then we unconditionally rewrite if |
09494c32 | 4839 | -- as an explicit dereference. This simplifies processing for several |
685094bf RD |
4840 | -- cases, including packed array cases and certain cases in which checks |
4841 | -- must be generated. We used to try to do this only when it was | |
4842 | -- necessary, but it cleans up the code to do it all the time. | |
70482933 RK |
4843 | |
4844 | if Is_Access_Type (T) then | |
2717634d | 4845 | Insert_Explicit_Dereference (P); |
70482933 RK |
4846 | Analyze_And_Resolve (P, Designated_Type (T)); |
4847 | end if; | |
4848 | ||
fbf5a39b AC |
4849 | -- Generate index and validity checks |
4850 | ||
4851 | Generate_Index_Checks (N); | |
4852 | ||
70482933 RK |
4853 | if Validity_Checks_On and then Validity_Check_Subscripts then |
4854 | Apply_Subscript_Validity_Checks (N); | |
4855 | end if; | |
4856 | ||
4857 | -- All done for the non-packed case | |
4858 | ||
4859 | if not Is_Packed (Etype (Prefix (N))) then | |
4860 | return; | |
4861 | end if; | |
4862 | ||
4863 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
8fc789c8 | 4864 | -- with one or more index types with a non-contiguous enumeration type), |
70482933 RK |
4865 | -- we can always use the normal packed element get circuit. |
4866 | ||
4867 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
4868 | Expand_Packed_Element_Reference (N); | |
4869 | return; | |
4870 | end if; | |
4871 | ||
4872 | -- For a reference to a component of a bit packed array, we have to | |
4873 | -- convert it to a reference to the corresponding Packed_Array_Type. | |
4874 | -- We only want to do this for simple references, and not for: | |
4875 | ||
685094bf RD |
4876 | -- Left side of assignment, or prefix of left side of assignment, or |
4877 | -- prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
4878 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
4879 | ||
4880 | -- Renaming objects in renaming associations | |
4881 | -- This case is handled when a use of the renamed variable occurs | |
4882 | ||
4883 | -- Actual parameters for a procedure call | |
4884 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
4885 | ||
4886 | -- The second expression in a 'Read attribute reference | |
4887 | ||
47d3b920 | 4888 | -- The prefix of an address or bit or size attribute reference |
70482933 RK |
4889 | |
4890 | -- The following circuit detects these exceptions | |
4891 | ||
4892 | declare | |
4893 | Child : Node_Id := N; | |
4894 | Parnt : Node_Id := Parent (N); | |
4895 | ||
4896 | begin | |
4897 | loop | |
4898 | if Nkind (Parnt) = N_Unchecked_Expression then | |
4899 | null; | |
4900 | ||
303b4d58 AC |
4901 | elsif Nkind_In (Parnt, N_Object_Renaming_Declaration, |
4902 | N_Procedure_Call_Statement) | |
70482933 RK |
4903 | or else (Nkind (Parnt) = N_Parameter_Association |
4904 | and then | |
4905 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
4906 | then | |
4907 | return; | |
4908 | ||
4909 | elsif Nkind (Parnt) = N_Attribute_Reference | |
4910 | and then (Attribute_Name (Parnt) = Name_Address | |
4911 | or else | |
47d3b920 AC |
4912 | Attribute_Name (Parnt) = Name_Bit |
4913 | or else | |
70482933 RK |
4914 | Attribute_Name (Parnt) = Name_Size) |
4915 | and then Prefix (Parnt) = Child | |
4916 | then | |
4917 | return; | |
4918 | ||
4919 | elsif Nkind (Parnt) = N_Assignment_Statement | |
4920 | and then Name (Parnt) = Child | |
4921 | then | |
4922 | return; | |
4923 | ||
685094bf RD |
4924 | -- If the expression is an index of an indexed component, it must |
4925 | -- be expanded regardless of context. | |
fbf5a39b AC |
4926 | |
4927 | elsif Nkind (Parnt) = N_Indexed_Component | |
4928 | and then Child /= Prefix (Parnt) | |
4929 | then | |
4930 | Expand_Packed_Element_Reference (N); | |
4931 | return; | |
4932 | ||
4933 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
4934 | and then Name (Parent (Parnt)) = Parnt | |
4935 | then | |
4936 | return; | |
4937 | ||
70482933 RK |
4938 | elsif Nkind (Parnt) = N_Attribute_Reference |
4939 | and then Attribute_Name (Parnt) = Name_Read | |
4940 | and then Next (First (Expressions (Parnt))) = Child | |
4941 | then | |
4942 | return; | |
4943 | ||
303b4d58 | 4944 | elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component) |
70482933 RK |
4945 | and then Prefix (Parnt) = Child |
4946 | then | |
4947 | null; | |
4948 | ||
4949 | else | |
4950 | Expand_Packed_Element_Reference (N); | |
4951 | return; | |
4952 | end if; | |
4953 | ||
685094bf RD |
4954 | -- Keep looking up tree for unchecked expression, or if we are the |
4955 | -- prefix of a possible assignment left side. | |
70482933 RK |
4956 | |
4957 | Child := Parnt; | |
4958 | Parnt := Parent (Child); | |
4959 | end loop; | |
4960 | end; | |
70482933 RK |
4961 | end Expand_N_Indexed_Component; |
4962 | ||
4963 | --------------------- | |
4964 | -- Expand_N_Not_In -- | |
4965 | --------------------- | |
4966 | ||
4967 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
4968 | -- can be done. This avoids needing to duplicate this expansion code. | |
4969 | ||
4970 | procedure Expand_N_Not_In (N : Node_Id) is | |
630d30e9 RD |
4971 | Loc : constant Source_Ptr := Sloc (N); |
4972 | Typ : constant Entity_Id := Etype (N); | |
4973 | Cfs : constant Boolean := Comes_From_Source (N); | |
70482933 RK |
4974 | |
4975 | begin | |
4976 | Rewrite (N, | |
4977 | Make_Op_Not (Loc, | |
4978 | Right_Opnd => | |
4979 | Make_In (Loc, | |
4980 | Left_Opnd => Left_Opnd (N), | |
d766cee3 | 4981 | Right_Opnd => Right_Opnd (N)))); |
630d30e9 | 4982 | |
197e4514 AC |
4983 | -- If this is a set membership, preserve list of alternatives |
4984 | ||
4985 | Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N))); | |
4986 | ||
d766cee3 | 4987 | -- We want this to appear as coming from source if original does (see |
8fc789c8 | 4988 | -- transformations in Expand_N_In). |
630d30e9 RD |
4989 | |
4990 | Set_Comes_From_Source (N, Cfs); | |
4991 | Set_Comes_From_Source (Right_Opnd (N), Cfs); | |
4992 | ||
8fc789c8 | 4993 | -- Now analyze transformed node |
630d30e9 | 4994 | |
70482933 RK |
4995 | Analyze_And_Resolve (N, Typ); |
4996 | end Expand_N_Not_In; | |
4997 | ||
4998 | ------------------- | |
4999 | -- Expand_N_Null -- | |
5000 | ------------------- | |
5001 | ||
685094bf RD |
5002 | -- The only replacement required is for the case of a null of type that is |
5003 | -- an access to protected subprogram. We represent such access values as a | |
5004 | -- record, and so we must replace the occurrence of null by the equivalent | |
5005 | -- record (with a null address and a null pointer in it), so that the | |
5006 | -- backend creates the proper value. | |
70482933 RK |
5007 | |
5008 | procedure Expand_N_Null (N : Node_Id) is | |
5009 | Loc : constant Source_Ptr := Sloc (N); | |
5010 | Typ : constant Entity_Id := Etype (N); | |
5011 | Agg : Node_Id; | |
5012 | ||
5013 | begin | |
26bff3d9 | 5014 | if Is_Access_Protected_Subprogram_Type (Typ) then |
70482933 RK |
5015 | Agg := |
5016 | Make_Aggregate (Loc, | |
5017 | Expressions => New_List ( | |
5018 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
5019 | Make_Null (Loc))); | |
5020 | ||
5021 | Rewrite (N, Agg); | |
5022 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
5023 | ||
685094bf RD |
5024 | -- For subsequent semantic analysis, the node must retain its type. |
5025 | -- Gigi in any case replaces this type by the corresponding record | |
5026 | -- type before processing the node. | |
70482933 RK |
5027 | |
5028 | Set_Etype (N, Typ); | |
5029 | end if; | |
fbf5a39b AC |
5030 | |
5031 | exception | |
5032 | when RE_Not_Available => | |
5033 | return; | |
70482933 RK |
5034 | end Expand_N_Null; |
5035 | ||
5036 | --------------------- | |
5037 | -- Expand_N_Op_Abs -- | |
5038 | --------------------- | |
5039 | ||
5040 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
5041 | Loc : constant Source_Ptr := Sloc (N); | |
5042 | Expr : constant Node_Id := Right_Opnd (N); | |
5043 | ||
5044 | begin | |
5045 | Unary_Op_Validity_Checks (N); | |
5046 | ||
5047 | -- Deal with software overflow checking | |
5048 | ||
07fc65c4 | 5049 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
5050 | and then Is_Signed_Integer_Type (Etype (N)) |
5051 | and then Do_Overflow_Check (N) | |
5052 | then | |
685094bf RD |
5053 | -- The only case to worry about is when the argument is equal to the |
5054 | -- largest negative number, so what we do is to insert the check: | |
70482933 | 5055 | |
fbf5a39b | 5056 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
5057 | |
5058 | -- with the usual Duplicate_Subexpr use coding for expr | |
5059 | ||
fbf5a39b AC |
5060 | Insert_Action (N, |
5061 | Make_Raise_Constraint_Error (Loc, | |
5062 | Condition => | |
5063 | Make_Op_Eq (Loc, | |
70482933 | 5064 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
5065 | Right_Opnd => |
5066 | Make_Attribute_Reference (Loc, | |
5067 | Prefix => | |
5068 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), | |
5069 | Attribute_Name => Name_First)), | |
5070 | Reason => CE_Overflow_Check_Failed)); | |
5071 | end if; | |
70482933 RK |
5072 | |
5073 | -- Vax floating-point types case | |
5074 | ||
fbf5a39b | 5075 | if Vax_Float (Etype (N)) then |
70482933 RK |
5076 | Expand_Vax_Arith (N); |
5077 | end if; | |
5078 | end Expand_N_Op_Abs; | |
5079 | ||
5080 | --------------------- | |
5081 | -- Expand_N_Op_Add -- | |
5082 | --------------------- | |
5083 | ||
5084 | procedure Expand_N_Op_Add (N : Node_Id) is | |
5085 | Typ : constant Entity_Id := Etype (N); | |
5086 | ||
5087 | begin | |
5088 | Binary_Op_Validity_Checks (N); | |
5089 | ||
5090 | -- N + 0 = 0 + N = N for integer types | |
5091 | ||
5092 | if Is_Integer_Type (Typ) then | |
5093 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
5094 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
5095 | then | |
5096 | Rewrite (N, Left_Opnd (N)); | |
5097 | return; | |
5098 | ||
5099 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
5100 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
5101 | then | |
5102 | Rewrite (N, Right_Opnd (N)); | |
5103 | return; | |
5104 | end if; | |
5105 | end if; | |
5106 | ||
fbf5a39b | 5107 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 RK |
5108 | |
5109 | if Is_Signed_Integer_Type (Typ) | |
5110 | or else Is_Fixed_Point_Type (Typ) | |
5111 | then | |
5112 | Apply_Arithmetic_Overflow_Check (N); | |
5113 | return; | |
5114 | ||
5115 | -- Vax floating-point types case | |
5116 | ||
5117 | elsif Vax_Float (Typ) then | |
5118 | Expand_Vax_Arith (N); | |
5119 | end if; | |
5120 | end Expand_N_Op_Add; | |
5121 | ||
5122 | --------------------- | |
5123 | -- Expand_N_Op_And -- | |
5124 | --------------------- | |
5125 | ||
5126 | procedure Expand_N_Op_And (N : Node_Id) is | |
5127 | Typ : constant Entity_Id := Etype (N); | |
5128 | ||
5129 | begin | |
5130 | Binary_Op_Validity_Checks (N); | |
5131 | ||
5132 | if Is_Array_Type (Etype (N)) then | |
5133 | Expand_Boolean_Operator (N); | |
5134 | ||
5135 | elsif Is_Boolean_Type (Etype (N)) then | |
6a2afd13 AC |
5136 | |
5137 | -- Replace AND by AND THEN if Short_Circuit_And_Or active and the | |
5138 | -- type is standard Boolean (do not mess with AND that uses a non- | |
5139 | -- standard Boolean type, because something strange is going on). | |
5140 | ||
5141 | if Short_Circuit_And_Or and then Typ = Standard_Boolean then | |
5142 | Rewrite (N, | |
5143 | Make_And_Then (Sloc (N), | |
5144 | Left_Opnd => Relocate_Node (Left_Opnd (N)), | |
5145 | Right_Opnd => Relocate_Node (Right_Opnd (N)))); | |
5146 | Analyze_And_Resolve (N, Typ); | |
5147 | ||
5148 | -- Otherwise, adjust conditions | |
5149 | ||
5150 | else | |
5151 | Adjust_Condition (Left_Opnd (N)); | |
5152 | Adjust_Condition (Right_Opnd (N)); | |
5153 | Set_Etype (N, Standard_Boolean); | |
5154 | Adjust_Result_Type (N, Typ); | |
5155 | end if; | |
70482933 RK |
5156 | end if; |
5157 | end Expand_N_Op_And; | |
5158 | ||
5159 | ------------------------ | |
5160 | -- Expand_N_Op_Concat -- | |
5161 | ------------------------ | |
5162 | ||
5163 | procedure Expand_N_Op_Concat (N : Node_Id) is | |
70482933 RK |
5164 | Opnds : List_Id; |
5165 | -- List of operands to be concatenated | |
5166 | ||
70482933 | 5167 | Cnode : Node_Id; |
685094bf RD |
5168 | -- Node which is to be replaced by the result of concatenating the nodes |
5169 | -- in the list Opnds. | |
70482933 | 5170 | |
70482933 | 5171 | begin |
fbf5a39b AC |
5172 | -- Ensure validity of both operands |
5173 | ||
70482933 RK |
5174 | Binary_Op_Validity_Checks (N); |
5175 | ||
685094bf RD |
5176 | -- If we are the left operand of a concatenation higher up the tree, |
5177 | -- then do nothing for now, since we want to deal with a series of | |
5178 | -- concatenations as a unit. | |
70482933 RK |
5179 | |
5180 | if Nkind (Parent (N)) = N_Op_Concat | |
5181 | and then N = Left_Opnd (Parent (N)) | |
5182 | then | |
5183 | return; | |
5184 | end if; | |
5185 | ||
5186 | -- We get here with a concatenation whose left operand may be a | |
5187 | -- concatenation itself with a consistent type. We need to process | |
5188 | -- these concatenation operands from left to right, which means | |
5189 | -- from the deepest node in the tree to the highest node. | |
5190 | ||
5191 | Cnode := N; | |
5192 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
5193 | Cnode := Left_Opnd (Cnode); | |
5194 | end loop; | |
5195 | ||
64425dff BD |
5196 | -- Now Cnode is the deepest concatenation, and its parents are the |
5197 | -- concatenation nodes above, so now we process bottom up, doing the | |
5198 | -- operations. We gather a string that is as long as possible up to five | |
5199 | -- operands. | |
70482933 | 5200 | |
df46b832 AC |
5201 | -- The outer loop runs more than once if more than one concatenation |
5202 | -- type is involved. | |
70482933 RK |
5203 | |
5204 | Outer : loop | |
5205 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
5206 | Set_Parent (Opnds, N); | |
5207 | ||
df46b832 | 5208 | -- The inner loop gathers concatenation operands |
70482933 RK |
5209 | |
5210 | Inner : while Cnode /= N | |
70482933 RK |
5211 | and then Base_Type (Etype (Cnode)) = |
5212 | Base_Type (Etype (Parent (Cnode))) | |
5213 | loop | |
5214 | Cnode := Parent (Cnode); | |
5215 | Append (Right_Opnd (Cnode), Opnds); | |
5216 | end loop Inner; | |
5217 | ||
fdac1f80 | 5218 | Expand_Concatenate (Cnode, Opnds); |
70482933 RK |
5219 | |
5220 | exit Outer when Cnode = N; | |
5221 | Cnode := Parent (Cnode); | |
5222 | end loop Outer; | |
5223 | end Expand_N_Op_Concat; | |
5224 | ||
5225 | ------------------------ | |
5226 | -- Expand_N_Op_Divide -- | |
5227 | ------------------------ | |
5228 | ||
5229 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
f82944b7 JM |
5230 | Loc : constant Source_Ptr := Sloc (N); |
5231 | Lopnd : constant Node_Id := Left_Opnd (N); | |
5232 | Ropnd : constant Node_Id := Right_Opnd (N); | |
5233 | Ltyp : constant Entity_Id := Etype (Lopnd); | |
5234 | Rtyp : constant Entity_Id := Etype (Ropnd); | |
5235 | Typ : Entity_Id := Etype (N); | |
5236 | Rknow : constant Boolean := Is_Integer_Type (Typ) | |
5237 | and then | |
5238 | Compile_Time_Known_Value (Ropnd); | |
5239 | Rval : Uint; | |
70482933 RK |
5240 | |
5241 | begin | |
5242 | Binary_Op_Validity_Checks (N); | |
5243 | ||
f82944b7 JM |
5244 | if Rknow then |
5245 | Rval := Expr_Value (Ropnd); | |
5246 | end if; | |
5247 | ||
70482933 RK |
5248 | -- N / 1 = N for integer types |
5249 | ||
f82944b7 JM |
5250 | if Rknow and then Rval = Uint_1 then |
5251 | Rewrite (N, Lopnd); | |
70482933 RK |
5252 | return; |
5253 | end if; | |
5254 | ||
5255 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
5256 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
5257 | -- operand is an unsigned integer, as required for this to work. | |
5258 | ||
f82944b7 JM |
5259 | if Nkind (Ropnd) = N_Op_Expon |
5260 | and then Is_Power_Of_2_For_Shift (Ropnd) | |
fbf5a39b AC |
5261 | |
5262 | -- We cannot do this transformation in configurable run time mode if we | |
51bf9bdf | 5263 | -- have 64-bit integers and long shifts are not available. |
fbf5a39b AC |
5264 | |
5265 | and then | |
5266 | (Esize (Ltyp) <= 32 | |
5267 | or else Support_Long_Shifts_On_Target) | |
70482933 RK |
5268 | then |
5269 | Rewrite (N, | |
5270 | Make_Op_Shift_Right (Loc, | |
f82944b7 | 5271 | Left_Opnd => Lopnd, |
70482933 | 5272 | Right_Opnd => |
f82944b7 | 5273 | Convert_To (Standard_Natural, Right_Opnd (Ropnd)))); |
70482933 RK |
5274 | Analyze_And_Resolve (N, Typ); |
5275 | return; | |
5276 | end if; | |
5277 | ||
5278 | -- Do required fixup of universal fixed operation | |
5279 | ||
5280 | if Typ = Universal_Fixed then | |
5281 | Fixup_Universal_Fixed_Operation (N); | |
5282 | Typ := Etype (N); | |
5283 | end if; | |
5284 | ||
5285 | -- Divisions with fixed-point results | |
5286 | ||
5287 | if Is_Fixed_Point_Type (Typ) then | |
5288 | ||
685094bf RD |
5289 | -- No special processing if Treat_Fixed_As_Integer is set, since |
5290 | -- from a semantic point of view such operations are simply integer | |
5291 | -- operations and will be treated that way. | |
70482933 RK |
5292 | |
5293 | if not Treat_Fixed_As_Integer (N) then | |
5294 | if Is_Integer_Type (Rtyp) then | |
5295 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
5296 | else | |
5297 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
5298 | end if; | |
5299 | end if; | |
5300 | ||
685094bf RD |
5301 | -- Other cases of division of fixed-point operands. Again we exclude the |
5302 | -- case where Treat_Fixed_As_Integer is set. | |
70482933 RK |
5303 | |
5304 | elsif (Is_Fixed_Point_Type (Ltyp) or else | |
5305 | Is_Fixed_Point_Type (Rtyp)) | |
5306 | and then not Treat_Fixed_As_Integer (N) | |
5307 | then | |
5308 | if Is_Integer_Type (Typ) then | |
5309 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
5310 | else | |
5311 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
5312 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
5313 | end if; | |
5314 | ||
685094bf RD |
5315 | -- Mixed-mode operations can appear in a non-static universal context, |
5316 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
5317 | |
5318 | elsif Typ = Universal_Real | |
5319 | and then Is_Integer_Type (Rtyp) | |
5320 | then | |
f82944b7 JM |
5321 | Rewrite (Ropnd, |
5322 | Convert_To (Universal_Real, Relocate_Node (Ropnd))); | |
70482933 | 5323 | |
f82944b7 | 5324 | Analyze_And_Resolve (Ropnd, Universal_Real); |
70482933 RK |
5325 | |
5326 | elsif Typ = Universal_Real | |
5327 | and then Is_Integer_Type (Ltyp) | |
5328 | then | |
f82944b7 JM |
5329 | Rewrite (Lopnd, |
5330 | Convert_To (Universal_Real, Relocate_Node (Lopnd))); | |
70482933 | 5331 | |
f82944b7 | 5332 | Analyze_And_Resolve (Lopnd, Universal_Real); |
70482933 | 5333 | |
f02b8bb8 | 5334 | -- Non-fixed point cases, do integer zero divide and overflow checks |
70482933 RK |
5335 | |
5336 | elsif Is_Integer_Type (Typ) then | |
5337 | Apply_Divide_Check (N); | |
fbf5a39b | 5338 | |
f82944b7 JM |
5339 | -- Check for 64-bit division available, or long shifts if the divisor |
5340 | -- is a small power of 2 (since such divides will be converted into | |
1147c704 | 5341 | -- long shifts). |
fbf5a39b AC |
5342 | |
5343 | if Esize (Ltyp) > 32 | |
5344 | and then not Support_64_Bit_Divides_On_Target | |
f82944b7 JM |
5345 | and then |
5346 | (not Rknow | |
5347 | or else not Support_Long_Shifts_On_Target | |
5348 | or else (Rval /= Uint_2 and then | |
5349 | Rval /= Uint_4 and then | |
5350 | Rval /= Uint_8 and then | |
5351 | Rval /= Uint_16 and then | |
5352 | Rval /= Uint_32 and then | |
5353 | Rval /= Uint_64)) | |
fbf5a39b AC |
5354 | then |
5355 | Error_Msg_CRT ("64-bit division", N); | |
5356 | end if; | |
f02b8bb8 RD |
5357 | |
5358 | -- Deal with Vax_Float | |
5359 | ||
5360 | elsif Vax_Float (Typ) then | |
5361 | Expand_Vax_Arith (N); | |
5362 | return; | |
70482933 RK |
5363 | end if; |
5364 | end Expand_N_Op_Divide; | |
5365 | ||
5366 | -------------------- | |
5367 | -- Expand_N_Op_Eq -- | |
5368 | -------------------- | |
5369 | ||
5370 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
5371 | Loc : constant Source_Ptr := Sloc (N); |
5372 | Typ : constant Entity_Id := Etype (N); | |
5373 | Lhs : constant Node_Id := Left_Opnd (N); | |
5374 | Rhs : constant Node_Id := Right_Opnd (N); | |
5375 | Bodies : constant List_Id := New_List; | |
5376 | A_Typ : constant Entity_Id := Etype (Lhs); | |
5377 | ||
70482933 RK |
5378 | Typl : Entity_Id := A_Typ; |
5379 | Op_Name : Entity_Id; | |
5380 | Prim : Elmt_Id; | |
70482933 RK |
5381 | |
5382 | procedure Build_Equality_Call (Eq : Entity_Id); | |
5383 | -- If a constructed equality exists for the type or for its parent, | |
5384 | -- build and analyze call, adding conversions if the operation is | |
5385 | -- inherited. | |
5386 | ||
5d09245e | 5387 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
8fc789c8 | 5388 | -- Determines whether a type has a subcomponent of an unconstrained |
5d09245e AC |
5389 | -- Unchecked_Union subtype. Typ is a record type. |
5390 | ||
70482933 RK |
5391 | ------------------------- |
5392 | -- Build_Equality_Call -- | |
5393 | ------------------------- | |
5394 | ||
5395 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
5396 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
5397 | L_Exp : Node_Id := Relocate_Node (Lhs); | |
5398 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
5399 | ||
5400 | begin | |
5401 | if Base_Type (Op_Type) /= Base_Type (A_Typ) | |
5402 | and then not Is_Class_Wide_Type (A_Typ) | |
5403 | then | |
5404 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
5405 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
5406 | end if; | |
5407 | ||
5d09245e AC |
5408 | -- If we have an Unchecked_Union, we need to add the inferred |
5409 | -- discriminant values as actuals in the function call. At this | |
5410 | -- point, the expansion has determined that both operands have | |
5411 | -- inferable discriminants. | |
5412 | ||
5413 | if Is_Unchecked_Union (Op_Type) then | |
5414 | declare | |
5415 | Lhs_Type : constant Node_Id := Etype (L_Exp); | |
5416 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
5417 | Lhs_Discr_Val : Node_Id; | |
5418 | Rhs_Discr_Val : Node_Id; | |
5419 | ||
5420 | begin | |
5421 | -- Per-object constrained selected components require special | |
5422 | -- attention. If the enclosing scope of the component is an | |
f02b8bb8 | 5423 | -- Unchecked_Union, we cannot reference its discriminants |
5d09245e AC |
5424 | -- directly. This is why we use the two extra parameters of |
5425 | -- the equality function of the enclosing Unchecked_Union. | |
5426 | ||
5427 | -- type UU_Type (Discr : Integer := 0) is | |
5428 | -- . . . | |
5429 | -- end record; | |
5430 | -- pragma Unchecked_Union (UU_Type); | |
5431 | ||
5432 | -- 1. Unchecked_Union enclosing record: | |
5433 | ||
5434 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
5435 | -- . . . | |
5436 | -- Comp : UU_Type (Discr); | |
5437 | -- . . . | |
5438 | -- end Enclosing_UU_Type; | |
5439 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
5440 | ||
5441 | -- Obj1 : Enclosing_UU_Type; | |
5442 | -- Obj2 : Enclosing_UU_Type (1); | |
5443 | ||
2717634d | 5444 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
5445 | |
5446 | -- Generated code: | |
5447 | ||
5448 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
5449 | ||
5450 | -- A and B are the formal parameters of the equality function | |
5451 | -- of Enclosing_UU_Type. The function always has two extra | |
5452 | -- formals to capture the inferred discriminant values. | |
5453 | ||
5454 | -- 2. Non-Unchecked_Union enclosing record: | |
5455 | ||
5456 | -- type | |
5457 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
5458 | -- is record | |
5459 | -- . . . | |
5460 | -- Comp : UU_Type (Discr); | |
5461 | -- . . . | |
5462 | -- end Enclosing_Non_UU_Type; | |
5463 | ||
5464 | -- Obj1 : Enclosing_Non_UU_Type; | |
5465 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
5466 | ||
630d30e9 | 5467 | -- ... Obj1 = Obj2 ... |
5d09245e AC |
5468 | |
5469 | -- Generated code: | |
5470 | ||
5471 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
5472 | -- obj1.discr, obj2.discr)) then | |
5473 | ||
5474 | -- In this case we can directly reference the discriminants of | |
5475 | -- the enclosing record. | |
5476 | ||
5477 | -- Lhs of equality | |
5478 | ||
5479 | if Nkind (Lhs) = N_Selected_Component | |
5e1c00fa RD |
5480 | and then Has_Per_Object_Constraint |
5481 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
5482 | then |
5483 | -- Enclosing record is an Unchecked_Union, use formal A | |
5484 | ||
5485 | if Is_Unchecked_Union (Scope | |
5486 | (Entity (Selector_Name (Lhs)))) | |
5487 | then | |
5488 | Lhs_Discr_Val := | |
5489 | Make_Identifier (Loc, | |
5490 | Chars => Name_A); | |
5491 | ||
5492 | -- Enclosing record is of a non-Unchecked_Union type, it is | |
5493 | -- possible to reference the discriminant. | |
5494 | ||
5495 | else | |
5496 | Lhs_Discr_Val := | |
5497 | Make_Selected_Component (Loc, | |
5498 | Prefix => Prefix (Lhs), | |
5499 | Selector_Name => | |
5e1c00fa RD |
5500 | New_Copy |
5501 | (Get_Discriminant_Value | |
5502 | (First_Discriminant (Lhs_Type), | |
5503 | Lhs_Type, | |
5504 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
5505 | end if; |
5506 | ||
5507 | -- Comment needed here ??? | |
5508 | ||
5509 | else | |
5510 | -- Infer the discriminant value | |
5511 | ||
5512 | Lhs_Discr_Val := | |
5e1c00fa RD |
5513 | New_Copy |
5514 | (Get_Discriminant_Value | |
5515 | (First_Discriminant (Lhs_Type), | |
5516 | Lhs_Type, | |
5517 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
5518 | end if; |
5519 | ||
5520 | -- Rhs of equality | |
5521 | ||
5522 | if Nkind (Rhs) = N_Selected_Component | |
5e1c00fa RD |
5523 | and then Has_Per_Object_Constraint |
5524 | (Entity (Selector_Name (Rhs))) | |
5d09245e | 5525 | then |
5e1c00fa RD |
5526 | if Is_Unchecked_Union |
5527 | (Scope (Entity (Selector_Name (Rhs)))) | |
5d09245e AC |
5528 | then |
5529 | Rhs_Discr_Val := | |
5530 | Make_Identifier (Loc, | |
5531 | Chars => Name_B); | |
5532 | ||
5533 | else | |
5534 | Rhs_Discr_Val := | |
5535 | Make_Selected_Component (Loc, | |
5536 | Prefix => Prefix (Rhs), | |
5537 | Selector_Name => | |
5538 | New_Copy (Get_Discriminant_Value ( | |
5539 | First_Discriminant (Rhs_Type), | |
5540 | Rhs_Type, | |
5541 | Stored_Constraint (Rhs_Type)))); | |
5542 | ||
5543 | end if; | |
5544 | else | |
5545 | Rhs_Discr_Val := | |
5546 | New_Copy (Get_Discriminant_Value ( | |
5547 | First_Discriminant (Rhs_Type), | |
5548 | Rhs_Type, | |
5549 | Stored_Constraint (Rhs_Type))); | |
5550 | ||
5551 | end if; | |
5552 | ||
5553 | Rewrite (N, | |
5554 | Make_Function_Call (Loc, | |
5555 | Name => New_Reference_To (Eq, Loc), | |
5556 | Parameter_Associations => New_List ( | |
5557 | L_Exp, | |
5558 | R_Exp, | |
5559 | Lhs_Discr_Val, | |
5560 | Rhs_Discr_Val))); | |
5561 | end; | |
5562 | ||
5563 | -- Normal case, not an unchecked union | |
5564 | ||
5565 | else | |
5566 | Rewrite (N, | |
5567 | Make_Function_Call (Loc, | |
5568 | Name => New_Reference_To (Eq, Loc), | |
5569 | Parameter_Associations => New_List (L_Exp, R_Exp))); | |
5570 | end if; | |
70482933 RK |
5571 | |
5572 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5573 | end Build_Equality_Call; | |
5574 | ||
5d09245e AC |
5575 | ------------------------------------ |
5576 | -- Has_Unconstrained_UU_Component -- | |
5577 | ------------------------------------ | |
5578 | ||
5579 | function Has_Unconstrained_UU_Component | |
5580 | (Typ : Node_Id) return Boolean | |
5581 | is | |
5582 | Tdef : constant Node_Id := | |
57848bf7 | 5583 | Type_Definition (Declaration_Node (Base_Type (Typ))); |
5d09245e AC |
5584 | Clist : Node_Id; |
5585 | Vpart : Node_Id; | |
5586 | ||
5587 | function Component_Is_Unconstrained_UU | |
5588 | (Comp : Node_Id) return Boolean; | |
5589 | -- Determines whether the subtype of the component is an | |
5590 | -- unconstrained Unchecked_Union. | |
5591 | ||
5592 | function Variant_Is_Unconstrained_UU | |
5593 | (Variant : Node_Id) return Boolean; | |
5594 | -- Determines whether a component of the variant has an unconstrained | |
5595 | -- Unchecked_Union subtype. | |
5596 | ||
5597 | ----------------------------------- | |
5598 | -- Component_Is_Unconstrained_UU -- | |
5599 | ----------------------------------- | |
5600 | ||
5601 | function Component_Is_Unconstrained_UU | |
5602 | (Comp : Node_Id) return Boolean | |
5603 | is | |
5604 | begin | |
5605 | if Nkind (Comp) /= N_Component_Declaration then | |
5606 | return False; | |
5607 | end if; | |
5608 | ||
5609 | declare | |
5610 | Sindic : constant Node_Id := | |
5611 | Subtype_Indication (Component_Definition (Comp)); | |
5612 | ||
5613 | begin | |
5614 | -- Unconstrained nominal type. In the case of a constraint | |
5615 | -- present, the node kind would have been N_Subtype_Indication. | |
5616 | ||
5617 | if Nkind (Sindic) = N_Identifier then | |
5618 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
5619 | end if; | |
5620 | ||
5621 | return False; | |
5622 | end; | |
5623 | end Component_Is_Unconstrained_UU; | |
5624 | ||
5625 | --------------------------------- | |
5626 | -- Variant_Is_Unconstrained_UU -- | |
5627 | --------------------------------- | |
5628 | ||
5629 | function Variant_Is_Unconstrained_UU | |
5630 | (Variant : Node_Id) return Boolean | |
5631 | is | |
5632 | Clist : constant Node_Id := Component_List (Variant); | |
5633 | ||
5634 | begin | |
5635 | if Is_Empty_List (Component_Items (Clist)) then | |
5636 | return False; | |
5637 | end if; | |
5638 | ||
f02b8bb8 RD |
5639 | -- We only need to test one component |
5640 | ||
5d09245e AC |
5641 | declare |
5642 | Comp : Node_Id := First (Component_Items (Clist)); | |
5643 | ||
5644 | begin | |
5645 | while Present (Comp) loop | |
5d09245e AC |
5646 | if Component_Is_Unconstrained_UU (Comp) then |
5647 | return True; | |
5648 | end if; | |
5649 | ||
5650 | Next (Comp); | |
5651 | end loop; | |
5652 | end; | |
5653 | ||
5654 | -- None of the components withing the variant were of | |
5655 | -- unconstrained Unchecked_Union type. | |
5656 | ||
5657 | return False; | |
5658 | end Variant_Is_Unconstrained_UU; | |
5659 | ||
5660 | -- Start of processing for Has_Unconstrained_UU_Component | |
5661 | ||
5662 | begin | |
5663 | if Null_Present (Tdef) then | |
5664 | return False; | |
5665 | end if; | |
5666 | ||
5667 | Clist := Component_List (Tdef); | |
5668 | Vpart := Variant_Part (Clist); | |
5669 | ||
5670 | -- Inspect available components | |
5671 | ||
5672 | if Present (Component_Items (Clist)) then | |
5673 | declare | |
5674 | Comp : Node_Id := First (Component_Items (Clist)); | |
5675 | ||
5676 | begin | |
5677 | while Present (Comp) loop | |
5678 | ||
8fc789c8 | 5679 | -- One component is sufficient |
5d09245e AC |
5680 | |
5681 | if Component_Is_Unconstrained_UU (Comp) then | |
5682 | return True; | |
5683 | end if; | |
5684 | ||
5685 | Next (Comp); | |
5686 | end loop; | |
5687 | end; | |
5688 | end if; | |
5689 | ||
5690 | -- Inspect available components withing variants | |
5691 | ||
5692 | if Present (Vpart) then | |
5693 | declare | |
5694 | Variant : Node_Id := First (Variants (Vpart)); | |
5695 | ||
5696 | begin | |
5697 | while Present (Variant) loop | |
5698 | ||
8fc789c8 | 5699 | -- One component within a variant is sufficient |
5d09245e AC |
5700 | |
5701 | if Variant_Is_Unconstrained_UU (Variant) then | |
5702 | return True; | |
5703 | end if; | |
5704 | ||
5705 | Next (Variant); | |
5706 | end loop; | |
5707 | end; | |
5708 | end if; | |
5709 | ||
5710 | -- Neither the available components, nor the components inside the | |
5711 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
5712 | ||
5713 | return False; | |
5714 | end Has_Unconstrained_UU_Component; | |
5715 | ||
70482933 RK |
5716 | -- Start of processing for Expand_N_Op_Eq |
5717 | ||
5718 | begin | |
5719 | Binary_Op_Validity_Checks (N); | |
5720 | ||
5721 | if Ekind (Typl) = E_Private_Type then | |
5722 | Typl := Underlying_Type (Typl); | |
70482933 RK |
5723 | elsif Ekind (Typl) = E_Private_Subtype then |
5724 | Typl := Underlying_Type (Base_Type (Typl)); | |
f02b8bb8 RD |
5725 | else |
5726 | null; | |
70482933 RK |
5727 | end if; |
5728 | ||
5729 | -- It may happen in error situations that the underlying type is not | |
5730 | -- set. The error will be detected later, here we just defend the | |
5731 | -- expander code. | |
5732 | ||
5733 | if No (Typl) then | |
5734 | return; | |
5735 | end if; | |
5736 | ||
5737 | Typl := Base_Type (Typl); | |
5738 | ||
70482933 RK |
5739 | -- Boolean types (requiring handling of non-standard case) |
5740 | ||
f02b8bb8 | 5741 | if Is_Boolean_Type (Typl) then |
70482933 RK |
5742 | Adjust_Condition (Left_Opnd (N)); |
5743 | Adjust_Condition (Right_Opnd (N)); | |
5744 | Set_Etype (N, Standard_Boolean); | |
5745 | Adjust_Result_Type (N, Typ); | |
5746 | ||
5747 | -- Array types | |
5748 | ||
5749 | elsif Is_Array_Type (Typl) then | |
5750 | ||
1033834f RD |
5751 | -- If we are doing full validity checking, and it is possible for the |
5752 | -- array elements to be invalid then expand out array comparisons to | |
5753 | -- make sure that we check the array elements. | |
fbf5a39b | 5754 | |
1033834f RD |
5755 | if Validity_Check_Operands |
5756 | and then not Is_Known_Valid (Component_Type (Typl)) | |
5757 | then | |
fbf5a39b AC |
5758 | declare |
5759 | Save_Force_Validity_Checks : constant Boolean := | |
5760 | Force_Validity_Checks; | |
5761 | begin | |
5762 | Force_Validity_Checks := True; | |
5763 | Rewrite (N, | |
0da2c8ac AC |
5764 | Expand_Array_Equality |
5765 | (N, | |
5766 | Relocate_Node (Lhs), | |
5767 | Relocate_Node (Rhs), | |
5768 | Bodies, | |
5769 | Typl)); | |
5770 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
5771 | Analyze_And_Resolve (N, Standard_Boolean); |
5772 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
5773 | end; | |
5774 | ||
a9d8907c | 5775 | -- Packed case where both operands are known aligned |
70482933 | 5776 | |
a9d8907c JM |
5777 | elsif Is_Bit_Packed_Array (Typl) |
5778 | and then not Is_Possibly_Unaligned_Object (Lhs) | |
5779 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
5780 | then | |
70482933 RK |
5781 | Expand_Packed_Eq (N); |
5782 | ||
5e1c00fa RD |
5783 | -- Where the component type is elementary we can use a block bit |
5784 | -- comparison (if supported on the target) exception in the case | |
5785 | -- of floating-point (negative zero issues require element by | |
5786 | -- element comparison), and atomic types (where we must be sure | |
a9d8907c | 5787 | -- to load elements independently) and possibly unaligned arrays. |
70482933 | 5788 | |
70482933 RK |
5789 | elsif Is_Elementary_Type (Component_Type (Typl)) |
5790 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 5791 | and then not Is_Atomic (Component_Type (Typl)) |
a9d8907c JM |
5792 | and then not Is_Possibly_Unaligned_Object (Lhs) |
5793 | and then not Is_Possibly_Unaligned_Object (Rhs) | |
fbf5a39b | 5794 | and then Support_Composite_Compare_On_Target |
70482933 RK |
5795 | then |
5796 | null; | |
5797 | ||
685094bf RD |
5798 | -- For composite and floating-point cases, expand equality loop to |
5799 | -- make sure of using proper comparisons for tagged types, and | |
5800 | -- correctly handling the floating-point case. | |
70482933 RK |
5801 | |
5802 | else | |
5803 | Rewrite (N, | |
0da2c8ac AC |
5804 | Expand_Array_Equality |
5805 | (N, | |
5806 | Relocate_Node (Lhs), | |
5807 | Relocate_Node (Rhs), | |
5808 | Bodies, | |
5809 | Typl)); | |
70482933 RK |
5810 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
5811 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5812 | end if; | |
5813 | ||
5814 | -- Record Types | |
5815 | ||
5816 | elsif Is_Record_Type (Typl) then | |
5817 | ||
5818 | -- For tagged types, use the primitive "=" | |
5819 | ||
5820 | if Is_Tagged_Type (Typl) then | |
5821 | ||
0669bebe GB |
5822 | -- No need to do anything else compiling under restriction |
5823 | -- No_Dispatching_Calls. During the semantic analysis we | |
5824 | -- already notified such violation. | |
5825 | ||
5826 | if Restriction_Active (No_Dispatching_Calls) then | |
5827 | return; | |
5828 | end if; | |
5829 | ||
685094bf RD |
5830 | -- If this is derived from an untagged private type completed with |
5831 | -- a tagged type, it does not have a full view, so we use the | |
5832 | -- primitive operations of the private type. This check should no | |
5833 | -- longer be necessary when these types get their full views??? | |
70482933 RK |
5834 | |
5835 | if Is_Private_Type (A_Typ) | |
5836 | and then not Is_Tagged_Type (A_Typ) | |
5837 | and then Is_Derived_Type (A_Typ) | |
5838 | and then No (Full_View (A_Typ)) | |
5839 | then | |
685094bf RD |
5840 | -- Search for equality operation, checking that the operands |
5841 | -- have the same type. Note that we must find a matching entry, | |
5842 | -- or something is very wrong! | |
2e071734 | 5843 | |
70482933 RK |
5844 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
5845 | ||
2e071734 AC |
5846 | while Present (Prim) loop |
5847 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
5848 | and then Etype (First_Formal (Node (Prim))) = | |
5849 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
5850 | and then | |
5851 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
5852 | ||
70482933 | 5853 | Next_Elmt (Prim); |
70482933 RK |
5854 | end loop; |
5855 | ||
2e071734 | 5856 | pragma Assert (Present (Prim)); |
70482933 | 5857 | Op_Name := Node (Prim); |
fbf5a39b AC |
5858 | |
5859 | -- Find the type's predefined equality or an overriding | |
685094bf | 5860 | -- user- defined equality. The reason for not simply calling |
fbf5a39b | 5861 | -- Find_Prim_Op here is that there may be a user-defined |
685094bf RD |
5862 | -- overloaded equality op that precedes the equality that we want, |
5863 | -- so we have to explicitly search (e.g., there could be an | |
5864 | -- equality with two different parameter types). | |
fbf5a39b | 5865 | |
70482933 | 5866 | else |
fbf5a39b AC |
5867 | if Is_Class_Wide_Type (Typl) then |
5868 | Typl := Root_Type (Typl); | |
5869 | end if; | |
5870 | ||
5871 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
5872 | while Present (Prim) loop |
5873 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
5874 | and then Etype (First_Formal (Node (Prim))) = | |
5875 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
5876 | and then |
5877 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
5878 | |
5879 | Next_Elmt (Prim); | |
fbf5a39b AC |
5880 | end loop; |
5881 | ||
2e071734 | 5882 | pragma Assert (Present (Prim)); |
fbf5a39b | 5883 | Op_Name := Node (Prim); |
70482933 RK |
5884 | end if; |
5885 | ||
5886 | Build_Equality_Call (Op_Name); | |
5887 | ||
5d09245e AC |
5888 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
5889 | -- predefined equality operator for a type which has a subcomponent | |
5890 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
5891 | ||
5892 | elsif Has_Unconstrained_UU_Component (Typl) then | |
5893 | Insert_Action (N, | |
5894 | Make_Raise_Program_Error (Loc, | |
5895 | Reason => PE_Unchecked_Union_Restriction)); | |
5896 | ||
5897 | -- Prevent Gigi from generating incorrect code by rewriting the | |
5898 | -- equality as a standard False. | |
5899 | ||
5900 | Rewrite (N, | |
5901 | New_Occurrence_Of (Standard_False, Loc)); | |
5902 | ||
5903 | elsif Is_Unchecked_Union (Typl) then | |
5904 | ||
5905 | -- If we can infer the discriminants of the operands, we make a | |
5906 | -- call to the TSS equality function. | |
5907 | ||
5908 | if Has_Inferable_Discriminants (Lhs) | |
5909 | and then | |
5910 | Has_Inferable_Discriminants (Rhs) | |
5911 | then | |
5912 | Build_Equality_Call | |
5913 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
5914 | ||
5915 | else | |
5916 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
5917 | -- the predefined equality operator for an Unchecked_Union type | |
5918 | -- if either of the operands lack inferable discriminants. | |
5919 | ||
5920 | Insert_Action (N, | |
5921 | Make_Raise_Program_Error (Loc, | |
5922 | Reason => PE_Unchecked_Union_Restriction)); | |
5923 | ||
5924 | -- Prevent Gigi from generating incorrect code by rewriting | |
5925 | -- the equality as a standard False. | |
5926 | ||
5927 | Rewrite (N, | |
5928 | New_Occurrence_Of (Standard_False, Loc)); | |
5929 | ||
5930 | end if; | |
5931 | ||
70482933 RK |
5932 | -- If a type support function is present (for complex cases), use it |
5933 | ||
fbf5a39b AC |
5934 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
5935 | Build_Equality_Call | |
5936 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 RK |
5937 | |
5938 | -- Otherwise expand the component by component equality. Note that | |
8fc789c8 | 5939 | -- we never use block-bit comparisons for records, because of the |
70482933 RK |
5940 | -- problems with gaps. The backend will often be able to recombine |
5941 | -- the separate comparisons that we generate here. | |
5942 | ||
5943 | else | |
5944 | Remove_Side_Effects (Lhs); | |
5945 | Remove_Side_Effects (Rhs); | |
5946 | Rewrite (N, | |
5947 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
5948 | ||
5949 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
5950 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
5951 | end if; | |
5952 | end if; | |
5953 | ||
d26dc4b5 | 5954 | -- Test if result is known at compile time |
70482933 | 5955 | |
d26dc4b5 | 5956 | Rewrite_Comparison (N); |
f02b8bb8 RD |
5957 | |
5958 | -- If we still have comparison for Vax_Float, process it | |
5959 | ||
5960 | if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then | |
5961 | Expand_Vax_Comparison (N); | |
5962 | return; | |
5963 | end if; | |
70482933 RK |
5964 | end Expand_N_Op_Eq; |
5965 | ||
5966 | ----------------------- | |
5967 | -- Expand_N_Op_Expon -- | |
5968 | ----------------------- | |
5969 | ||
5970 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
5971 | Loc : constant Source_Ptr := Sloc (N); | |
5972 | Typ : constant Entity_Id := Etype (N); | |
5973 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
5974 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 5975 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
5976 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
5977 | Exptyp : constant Entity_Id := Etype (Exp); | |
5978 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
5979 | Expv : Uint; | |
5980 | Xnode : Node_Id; | |
5981 | Temp : Node_Id; | |
5982 | Rent : RE_Id; | |
5983 | Ent : Entity_Id; | |
fbf5a39b | 5984 | Etyp : Entity_Id; |
70482933 RK |
5985 | |
5986 | begin | |
5987 | Binary_Op_Validity_Checks (N); | |
5988 | ||
685094bf RD |
5989 | -- If either operand is of a private type, then we have the use of an |
5990 | -- intrinsic operator, and we get rid of the privateness, by using root | |
5991 | -- types of underlying types for the actual operation. Otherwise the | |
5992 | -- private types will cause trouble if we expand multiplications or | |
5993 | -- shifts etc. We also do this transformation if the result type is | |
5994 | -- different from the base type. | |
07fc65c4 GB |
5995 | |
5996 | if Is_Private_Type (Etype (Base)) | |
5997 | or else | |
5998 | Is_Private_Type (Typ) | |
5999 | or else | |
6000 | Is_Private_Type (Exptyp) | |
6001 | or else | |
6002 | Rtyp /= Root_Type (Bastyp) | |
6003 | then | |
6004 | declare | |
6005 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
6006 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
6007 | ||
6008 | begin | |
6009 | Rewrite (N, | |
6010 | Unchecked_Convert_To (Typ, | |
6011 | Make_Op_Expon (Loc, | |
6012 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
6013 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
6014 | Analyze_And_Resolve (N, Typ); | |
6015 | return; | |
6016 | end; | |
6017 | end if; | |
6018 | ||
fbf5a39b | 6019 | -- Test for case of known right argument |
70482933 RK |
6020 | |
6021 | if Compile_Time_Known_Value (Exp) then | |
6022 | Expv := Expr_Value (Exp); | |
6023 | ||
6024 | -- We only fold small non-negative exponents. You might think we | |
6025 | -- could fold small negative exponents for the real case, but we | |
6026 | -- can't because we are required to raise Constraint_Error for | |
6027 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
6028 | -- See ACVC test C4A012B. | |
6029 | ||
6030 | if Expv >= 0 and then Expv <= 4 then | |
6031 | ||
6032 | -- X ** 0 = 1 (or 1.0) | |
6033 | ||
6034 | if Expv = 0 then | |
abcbd24c ST |
6035 | |
6036 | -- Call Remove_Side_Effects to ensure that any side effects | |
6037 | -- in the ignored left operand (in particular function calls | |
6038 | -- to user defined functions) are properly executed. | |
6039 | ||
6040 | Remove_Side_Effects (Base); | |
6041 | ||
70482933 RK |
6042 | if Ekind (Typ) in Integer_Kind then |
6043 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
6044 | else | |
6045 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
6046 | end if; | |
6047 | ||
6048 | -- X ** 1 = X | |
6049 | ||
6050 | elsif Expv = 1 then | |
6051 | Xnode := Base; | |
6052 | ||
6053 | -- X ** 2 = X * X | |
6054 | ||
6055 | elsif Expv = 2 then | |
6056 | Xnode := | |
6057 | Make_Op_Multiply (Loc, | |
6058 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 6059 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
6060 | |
6061 | -- X ** 3 = X * X * X | |
6062 | ||
6063 | elsif Expv = 3 then | |
6064 | Xnode := | |
6065 | Make_Op_Multiply (Loc, | |
6066 | Left_Opnd => | |
6067 | Make_Op_Multiply (Loc, | |
6068 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
6069 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
6070 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
6071 | |
6072 | -- X ** 4 -> | |
6073 | -- En : constant base'type := base * base; | |
6074 | -- ... | |
6075 | -- En * En | |
6076 | ||
6077 | else -- Expv = 4 | |
191fcb3a | 6078 | Temp := Make_Temporary (Loc, 'E', Base); |
70482933 RK |
6079 | |
6080 | Insert_Actions (N, New_List ( | |
6081 | Make_Object_Declaration (Loc, | |
6082 | Defining_Identifier => Temp, | |
6083 | Constant_Present => True, | |
6084 | Object_Definition => New_Reference_To (Typ, Loc), | |
6085 | Expression => | |
6086 | Make_Op_Multiply (Loc, | |
6087 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 6088 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base))))); |
70482933 RK |
6089 | |
6090 | Xnode := | |
6091 | Make_Op_Multiply (Loc, | |
6092 | Left_Opnd => New_Reference_To (Temp, Loc), | |
6093 | Right_Opnd => New_Reference_To (Temp, Loc)); | |
6094 | end if; | |
6095 | ||
6096 | Rewrite (N, Xnode); | |
6097 | Analyze_And_Resolve (N, Typ); | |
6098 | return; | |
6099 | end if; | |
6100 | end if; | |
6101 | ||
6102 | -- Case of (2 ** expression) appearing as an argument of an integer | |
6103 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 6104 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
6105 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
6106 | -- of the higher level node converts it into a shift. | |
6107 | ||
51bf9bdf AC |
6108 | -- Another case is 2 ** N in any other context. We simply convert |
6109 | -- this to 1 * 2 ** N, and then the above transformation applies. | |
6110 | ||
685094bf RD |
6111 | -- Note: this transformation is not applicable for a modular type with |
6112 | -- a non-binary modulus in the multiplication case, since we get a wrong | |
6113 | -- result if the shift causes an overflow before the modular reduction. | |
6114 | ||
70482933 RK |
6115 | if Nkind (Base) = N_Integer_Literal |
6116 | and then Intval (Base) = 2 | |
6117 | and then Is_Integer_Type (Root_Type (Exptyp)) | |
6118 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
6119 | and then Is_Unsigned_Type (Exptyp) | |
6120 | and then not Ovflo | |
70482933 | 6121 | then |
51bf9bdf | 6122 | -- First the multiply and divide cases |
70482933 | 6123 | |
51bf9bdf AC |
6124 | if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then |
6125 | declare | |
6126 | P : constant Node_Id := Parent (N); | |
6127 | L : constant Node_Id := Left_Opnd (P); | |
6128 | R : constant Node_Id := Right_Opnd (P); | |
6129 | ||
6130 | begin | |
6131 | if (Nkind (P) = N_Op_Multiply | |
6132 | and then not Non_Binary_Modulus (Typ) | |
6133 | and then | |
6134 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
6135 | or else | |
6136 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
6137 | and then not Do_Overflow_Check (P)) | |
6138 | or else | |
6139 | (Nkind (P) = N_Op_Divide | |
6140 | and then Is_Integer_Type (Etype (L)) | |
6141 | and then Is_Unsigned_Type (Etype (L)) | |
6142 | and then R = N | |
6143 | and then not Do_Overflow_Check (P)) | |
6144 | then | |
6145 | Set_Is_Power_Of_2_For_Shift (N); | |
6146 | return; | |
6147 | end if; | |
6148 | end; | |
6149 | ||
6150 | -- Now the other cases | |
6151 | ||
6152 | elsif not Non_Binary_Modulus (Typ) then | |
6153 | Rewrite (N, | |
6154 | Make_Op_Multiply (Loc, | |
6155 | Left_Opnd => Make_Integer_Literal (Loc, 1), | |
6156 | Right_Opnd => Relocate_Node (N))); | |
6157 | Analyze_And_Resolve (N, Typ); | |
6158 | return; | |
6159 | end if; | |
70482933 RK |
6160 | end if; |
6161 | ||
07fc65c4 GB |
6162 | -- Fall through if exponentiation must be done using a runtime routine |
6163 | ||
07fc65c4 | 6164 | -- First deal with modular case |
70482933 RK |
6165 | |
6166 | if Is_Modular_Integer_Type (Rtyp) then | |
6167 | ||
6168 | -- Non-binary case, we call the special exponentiation routine for | |
6169 | -- the non-binary case, converting the argument to Long_Long_Integer | |
6170 | -- and passing the modulus value. Then the result is converted back | |
6171 | -- to the base type. | |
6172 | ||
6173 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
6174 | Rewrite (N, |
6175 | Convert_To (Typ, | |
6176 | Make_Function_Call (Loc, | |
6177 | Name => New_Reference_To (RTE (RE_Exp_Modular), Loc), | |
6178 | Parameter_Associations => New_List ( | |
6179 | Convert_To (Standard_Integer, Base), | |
6180 | Make_Integer_Literal (Loc, Modulus (Rtyp)), | |
6181 | Exp)))); | |
6182 | ||
685094bf RD |
6183 | -- Binary case, in this case, we call one of two routines, either the |
6184 | -- unsigned integer case, or the unsigned long long integer case, | |
6185 | -- with a final "and" operation to do the required mod. | |
70482933 RK |
6186 | |
6187 | else | |
6188 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
6189 | Ent := RTE (RE_Exp_Unsigned); | |
6190 | else | |
6191 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
6192 | end if; | |
6193 | ||
6194 | Rewrite (N, | |
6195 | Convert_To (Typ, | |
6196 | Make_Op_And (Loc, | |
6197 | Left_Opnd => | |
6198 | Make_Function_Call (Loc, | |
6199 | Name => New_Reference_To (Ent, Loc), | |
6200 | Parameter_Associations => New_List ( | |
6201 | Convert_To (Etype (First_Formal (Ent)), Base), | |
6202 | Exp)), | |
6203 | Right_Opnd => | |
6204 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
6205 | ||
6206 | end if; | |
6207 | ||
6208 | -- Common exit point for modular type case | |
6209 | ||
6210 | Analyze_And_Resolve (N, Typ); | |
6211 | return; | |
6212 | ||
fbf5a39b AC |
6213 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
6214 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
6215 | -- most machines it would not help, and it would generate more code that | |
dfd99a80 | 6216 | -- might need certification when a certified run time is required. |
70482933 | 6217 | |
fbf5a39b | 6218 | -- In the integer cases, we have two routines, one for when overflow |
dfd99a80 TQ |
6219 | -- checks are required, and one when they are not required, since there |
6220 | -- is a real gain in omitting checks on many machines. | |
70482933 | 6221 | |
fbf5a39b AC |
6222 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
6223 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
6224 | and then | |
6225 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
6226 | or else (Rtyp = Universal_Integer) | |
70482933 | 6227 | then |
fbf5a39b AC |
6228 | Etyp := Standard_Long_Long_Integer; |
6229 | ||
70482933 RK |
6230 | if Ovflo then |
6231 | Rent := RE_Exp_Long_Long_Integer; | |
6232 | else | |
6233 | Rent := RE_Exn_Long_Long_Integer; | |
6234 | end if; | |
6235 | ||
fbf5a39b AC |
6236 | elsif Is_Signed_Integer_Type (Rtyp) then |
6237 | Etyp := Standard_Integer; | |
70482933 RK |
6238 | |
6239 | if Ovflo then | |
fbf5a39b | 6240 | Rent := RE_Exp_Integer; |
70482933 | 6241 | else |
fbf5a39b | 6242 | Rent := RE_Exn_Integer; |
70482933 | 6243 | end if; |
fbf5a39b AC |
6244 | |
6245 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
6246 | -- need separate routines for the overflow case here, since in the case | |
6247 | -- of floating-point, we generate infinities anyway as a rule (either | |
6248 | -- that or we automatically trap overflow), and if there is an infinity | |
6249 | -- generated and a range check is required, the check will fail anyway. | |
6250 | ||
6251 | else | |
6252 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
6253 | Etyp := Standard_Long_Long_Float; | |
6254 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
6255 | end if; |
6256 | ||
6257 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 6258 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 6259 | |
fbf5a39b | 6260 | if Typ = Etyp |
70482933 RK |
6261 | and then Rtyp /= Universal_Integer |
6262 | and then Rtyp /= Universal_Real | |
6263 | then | |
6264 | Rewrite (N, | |
6265 | Make_Function_Call (Loc, | |
6266 | Name => New_Reference_To (RTE (Rent), Loc), | |
6267 | Parameter_Associations => New_List (Base, Exp))); | |
6268 | ||
6269 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b | 6270 | -- required in the universal cases, since the runtime routine is |
1147c704 | 6271 | -- typed using one of the standard types). |
70482933 RK |
6272 | |
6273 | else | |
6274 | Rewrite (N, | |
6275 | Convert_To (Typ, | |
6276 | Make_Function_Call (Loc, | |
6277 | Name => New_Reference_To (RTE (Rent), Loc), | |
6278 | Parameter_Associations => New_List ( | |
fbf5a39b | 6279 | Convert_To (Etyp, Base), |
70482933 RK |
6280 | Exp)))); |
6281 | end if; | |
6282 | ||
6283 | Analyze_And_Resolve (N, Typ); | |
6284 | return; | |
6285 | ||
fbf5a39b AC |
6286 | exception |
6287 | when RE_Not_Available => | |
6288 | return; | |
70482933 RK |
6289 | end Expand_N_Op_Expon; |
6290 | ||
6291 | -------------------- | |
6292 | -- Expand_N_Op_Ge -- | |
6293 | -------------------- | |
6294 | ||
6295 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
6296 | Typ : constant Entity_Id := Etype (N); | |
6297 | Op1 : constant Node_Id := Left_Opnd (N); | |
6298 | Op2 : constant Node_Id := Right_Opnd (N); | |
6299 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6300 | ||
6301 | begin | |
6302 | Binary_Op_Validity_Checks (N); | |
6303 | ||
f02b8bb8 | 6304 | if Is_Array_Type (Typ1) then |
70482933 RK |
6305 | Expand_Array_Comparison (N); |
6306 | return; | |
6307 | end if; | |
6308 | ||
6309 | if Is_Boolean_Type (Typ1) then | |
6310 | Adjust_Condition (Op1); | |
6311 | Adjust_Condition (Op2); | |
6312 | Set_Etype (N, Standard_Boolean); | |
6313 | Adjust_Result_Type (N, Typ); | |
6314 | end if; | |
6315 | ||
6316 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6317 | |
6318 | -- If we still have comparison, and Vax_Float type, process it | |
6319 | ||
6320 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6321 | Expand_Vax_Comparison (N); | |
6322 | return; | |
6323 | end if; | |
70482933 RK |
6324 | end Expand_N_Op_Ge; |
6325 | ||
6326 | -------------------- | |
6327 | -- Expand_N_Op_Gt -- | |
6328 | -------------------- | |
6329 | ||
6330 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
6331 | Typ : constant Entity_Id := Etype (N); | |
6332 | Op1 : constant Node_Id := Left_Opnd (N); | |
6333 | Op2 : constant Node_Id := Right_Opnd (N); | |
6334 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6335 | ||
6336 | begin | |
6337 | Binary_Op_Validity_Checks (N); | |
6338 | ||
f02b8bb8 | 6339 | if Is_Array_Type (Typ1) then |
70482933 RK |
6340 | Expand_Array_Comparison (N); |
6341 | return; | |
6342 | end if; | |
6343 | ||
6344 | if Is_Boolean_Type (Typ1) then | |
6345 | Adjust_Condition (Op1); | |
6346 | Adjust_Condition (Op2); | |
6347 | Set_Etype (N, Standard_Boolean); | |
6348 | Adjust_Result_Type (N, Typ); | |
6349 | end if; | |
6350 | ||
6351 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6352 | |
6353 | -- If we still have comparison, and Vax_Float type, process it | |
6354 | ||
6355 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6356 | Expand_Vax_Comparison (N); | |
6357 | return; | |
6358 | end if; | |
70482933 RK |
6359 | end Expand_N_Op_Gt; |
6360 | ||
6361 | -------------------- | |
6362 | -- Expand_N_Op_Le -- | |
6363 | -------------------- | |
6364 | ||
6365 | procedure Expand_N_Op_Le (N : Node_Id) is | |
6366 | Typ : constant Entity_Id := Etype (N); | |
6367 | Op1 : constant Node_Id := Left_Opnd (N); | |
6368 | Op2 : constant Node_Id := Right_Opnd (N); | |
6369 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6370 | ||
6371 | begin | |
6372 | Binary_Op_Validity_Checks (N); | |
6373 | ||
f02b8bb8 | 6374 | if Is_Array_Type (Typ1) then |
70482933 RK |
6375 | Expand_Array_Comparison (N); |
6376 | return; | |
6377 | end if; | |
6378 | ||
6379 | if Is_Boolean_Type (Typ1) then | |
6380 | Adjust_Condition (Op1); | |
6381 | Adjust_Condition (Op2); | |
6382 | Set_Etype (N, Standard_Boolean); | |
6383 | Adjust_Result_Type (N, Typ); | |
6384 | end if; | |
6385 | ||
6386 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6387 | |
6388 | -- If we still have comparison, and Vax_Float type, process it | |
6389 | ||
6390 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6391 | Expand_Vax_Comparison (N); | |
6392 | return; | |
6393 | end if; | |
70482933 RK |
6394 | end Expand_N_Op_Le; |
6395 | ||
6396 | -------------------- | |
6397 | -- Expand_N_Op_Lt -- | |
6398 | -------------------- | |
6399 | ||
6400 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
6401 | Typ : constant Entity_Id := Etype (N); | |
6402 | Op1 : constant Node_Id := Left_Opnd (N); | |
6403 | Op2 : constant Node_Id := Right_Opnd (N); | |
6404 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
6405 | ||
6406 | begin | |
6407 | Binary_Op_Validity_Checks (N); | |
6408 | ||
f02b8bb8 | 6409 | if Is_Array_Type (Typ1) then |
70482933 RK |
6410 | Expand_Array_Comparison (N); |
6411 | return; | |
6412 | end if; | |
6413 | ||
6414 | if Is_Boolean_Type (Typ1) then | |
6415 | Adjust_Condition (Op1); | |
6416 | Adjust_Condition (Op2); | |
6417 | Set_Etype (N, Standard_Boolean); | |
6418 | Adjust_Result_Type (N, Typ); | |
6419 | end if; | |
6420 | ||
6421 | Rewrite_Comparison (N); | |
f02b8bb8 RD |
6422 | |
6423 | -- If we still have comparison, and Vax_Float type, process it | |
6424 | ||
6425 | if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then | |
6426 | Expand_Vax_Comparison (N); | |
6427 | return; | |
6428 | end if; | |
70482933 RK |
6429 | end Expand_N_Op_Lt; |
6430 | ||
6431 | ----------------------- | |
6432 | -- Expand_N_Op_Minus -- | |
6433 | ----------------------- | |
6434 | ||
6435 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
6436 | Loc : constant Source_Ptr := Sloc (N); | |
6437 | Typ : constant Entity_Id := Etype (N); | |
6438 | ||
6439 | begin | |
6440 | Unary_Op_Validity_Checks (N); | |
6441 | ||
07fc65c4 | 6442 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
6443 | and then Is_Signed_Integer_Type (Etype (N)) |
6444 | and then Do_Overflow_Check (N) | |
6445 | then | |
6446 | -- Software overflow checking expands -expr into (0 - expr) | |
6447 | ||
6448 | Rewrite (N, | |
6449 | Make_Op_Subtract (Loc, | |
6450 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
6451 | Right_Opnd => Right_Opnd (N))); | |
6452 | ||
6453 | Analyze_And_Resolve (N, Typ); | |
6454 | ||
6455 | -- Vax floating-point types case | |
6456 | ||
6457 | elsif Vax_Float (Etype (N)) then | |
6458 | Expand_Vax_Arith (N); | |
6459 | end if; | |
6460 | end Expand_N_Op_Minus; | |
6461 | ||
6462 | --------------------- | |
6463 | -- Expand_N_Op_Mod -- | |
6464 | --------------------- | |
6465 | ||
6466 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
6467 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 6468 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
6469 | Left : constant Node_Id := Left_Opnd (N); |
6470 | Right : constant Node_Id := Right_Opnd (N); | |
6471 | DOC : constant Boolean := Do_Overflow_Check (N); | |
6472 | DDC : constant Boolean := Do_Division_Check (N); | |
6473 | ||
6474 | LLB : Uint; | |
6475 | Llo : Uint; | |
6476 | Lhi : Uint; | |
6477 | LOK : Boolean; | |
6478 | Rlo : Uint; | |
6479 | Rhi : Uint; | |
6480 | ROK : Boolean; | |
6481 | ||
1033834f RD |
6482 | pragma Warnings (Off, Lhi); |
6483 | ||
70482933 RK |
6484 | begin |
6485 | Binary_Op_Validity_Checks (N); | |
6486 | ||
5d5e9775 AC |
6487 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
6488 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); | |
70482933 RK |
6489 | |
6490 | -- Convert mod to rem if operands are known non-negative. We do this | |
6491 | -- since it is quite likely that this will improve the quality of code, | |
6492 | -- (the operation now corresponds to the hardware remainder), and it | |
6493 | -- does not seem likely that it could be harmful. | |
6494 | ||
6495 | if LOK and then Llo >= 0 | |
6496 | and then | |
6497 | ROK and then Rlo >= 0 | |
6498 | then | |
6499 | Rewrite (N, | |
6500 | Make_Op_Rem (Sloc (N), | |
6501 | Left_Opnd => Left_Opnd (N), | |
6502 | Right_Opnd => Right_Opnd (N))); | |
6503 | ||
685094bf RD |
6504 | -- Instead of reanalyzing the node we do the analysis manually. This |
6505 | -- avoids anomalies when the replacement is done in an instance and | |
6506 | -- is epsilon more efficient. | |
70482933 RK |
6507 | |
6508 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 6509 | Set_Etype (N, Typ); |
70482933 RK |
6510 | Set_Do_Overflow_Check (N, DOC); |
6511 | Set_Do_Division_Check (N, DDC); | |
6512 | Expand_N_Op_Rem (N); | |
6513 | Set_Analyzed (N); | |
6514 | ||
6515 | -- Otherwise, normal mod processing | |
6516 | ||
6517 | else | |
6518 | if Is_Integer_Type (Etype (N)) then | |
6519 | Apply_Divide_Check (N); | |
6520 | end if; | |
6521 | ||
fbf5a39b AC |
6522 | -- Apply optimization x mod 1 = 0. We don't really need that with |
6523 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
6524 | -- certainly harmless. | |
6525 | ||
6526 | if Is_Integer_Type (Etype (N)) | |
6527 | and then Compile_Time_Known_Value (Right) | |
6528 | and then Expr_Value (Right) = Uint_1 | |
6529 | then | |
abcbd24c ST |
6530 | -- Call Remove_Side_Effects to ensure that any side effects in |
6531 | -- the ignored left operand (in particular function calls to | |
6532 | -- user defined functions) are properly executed. | |
6533 | ||
6534 | Remove_Side_Effects (Left); | |
6535 | ||
fbf5a39b AC |
6536 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
6537 | Analyze_And_Resolve (N, Typ); | |
6538 | return; | |
6539 | end if; | |
6540 | ||
70482933 RK |
6541 | -- Deal with annoying case of largest negative number remainder |
6542 | -- minus one. Gigi does not handle this case correctly, because | |
6543 | -- it generates a divide instruction which may trap in this case. | |
6544 | ||
685094bf RD |
6545 | -- In fact the check is quite easy, if the right operand is -1, then |
6546 | -- the mod value is always 0, and we can just ignore the left operand | |
6547 | -- completely in this case. | |
70482933 | 6548 | |
30783513 | 6549 | -- The operand type may be private (e.g. in the expansion of an |
685094bf RD |
6550 | -- intrinsic operation) so we must use the underlying type to get the |
6551 | -- bounds, and convert the literals explicitly. | |
fbf5a39b AC |
6552 | |
6553 | LLB := | |
6554 | Expr_Value | |
6555 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
70482933 RK |
6556 | |
6557 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
6558 | and then | |
6559 | ((not LOK) or else (Llo = LLB)) | |
6560 | then | |
6561 | Rewrite (N, | |
6562 | Make_Conditional_Expression (Loc, | |
6563 | Expressions => New_List ( | |
6564 | Make_Op_Eq (Loc, | |
6565 | Left_Opnd => Duplicate_Subexpr (Right), | |
6566 | Right_Opnd => | |
fbf5a39b AC |
6567 | Unchecked_Convert_To (Typ, |
6568 | Make_Integer_Literal (Loc, -1))), | |
6569 | Unchecked_Convert_To (Typ, | |
6570 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
6571 | Relocate_Node (N)))); |
6572 | ||
6573 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
fbf5a39b | 6574 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
6575 | end if; |
6576 | end if; | |
6577 | end Expand_N_Op_Mod; | |
6578 | ||
6579 | -------------------------- | |
6580 | -- Expand_N_Op_Multiply -- | |
6581 | -------------------------- | |
6582 | ||
6583 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
abcbd24c ST |
6584 | Loc : constant Source_Ptr := Sloc (N); |
6585 | Lop : constant Node_Id := Left_Opnd (N); | |
6586 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b | 6587 | |
abcbd24c ST |
6588 | Lp2 : constant Boolean := |
6589 | Nkind (Lop) = N_Op_Expon | |
6590 | and then Is_Power_Of_2_For_Shift (Lop); | |
fbf5a39b | 6591 | |
abcbd24c ST |
6592 | Rp2 : constant Boolean := |
6593 | Nkind (Rop) = N_Op_Expon | |
6594 | and then Is_Power_Of_2_For_Shift (Rop); | |
fbf5a39b | 6595 | |
70482933 RK |
6596 | Ltyp : constant Entity_Id := Etype (Lop); |
6597 | Rtyp : constant Entity_Id := Etype (Rop); | |
6598 | Typ : Entity_Id := Etype (N); | |
6599 | ||
6600 | begin | |
6601 | Binary_Op_Validity_Checks (N); | |
6602 | ||
6603 | -- Special optimizations for integer types | |
6604 | ||
6605 | if Is_Integer_Type (Typ) then | |
6606 | ||
abcbd24c | 6607 | -- N * 0 = 0 for integer types |
70482933 | 6608 | |
abcbd24c ST |
6609 | if Compile_Time_Known_Value (Rop) |
6610 | and then Expr_Value (Rop) = Uint_0 | |
70482933 | 6611 | then |
abcbd24c ST |
6612 | -- Call Remove_Side_Effects to ensure that any side effects in |
6613 | -- the ignored left operand (in particular function calls to | |
6614 | -- user defined functions) are properly executed. | |
6615 | ||
6616 | Remove_Side_Effects (Lop); | |
6617 | ||
6618 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
6619 | Analyze_And_Resolve (N, Typ); | |
6620 | return; | |
6621 | end if; | |
6622 | ||
6623 | -- Similar handling for 0 * N = 0 | |
6624 | ||
6625 | if Compile_Time_Known_Value (Lop) | |
6626 | and then Expr_Value (Lop) = Uint_0 | |
6627 | then | |
6628 | Remove_Side_Effects (Rop); | |
70482933 RK |
6629 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); |
6630 | Analyze_And_Resolve (N, Typ); | |
6631 | return; | |
6632 | end if; | |
6633 | ||
6634 | -- N * 1 = 1 * N = N for integer types | |
6635 | ||
fbf5a39b AC |
6636 | -- This optimisation is not done if we are going to |
6637 | -- rewrite the product 1 * 2 ** N to a shift. | |
6638 | ||
6639 | if Compile_Time_Known_Value (Rop) | |
6640 | and then Expr_Value (Rop) = Uint_1 | |
6641 | and then not Lp2 | |
70482933 | 6642 | then |
fbf5a39b | 6643 | Rewrite (N, Lop); |
70482933 RK |
6644 | return; |
6645 | ||
fbf5a39b AC |
6646 | elsif Compile_Time_Known_Value (Lop) |
6647 | and then Expr_Value (Lop) = Uint_1 | |
6648 | and then not Rp2 | |
70482933 | 6649 | then |
fbf5a39b | 6650 | Rewrite (N, Rop); |
70482933 RK |
6651 | return; |
6652 | end if; | |
6653 | end if; | |
6654 | ||
70482933 RK |
6655 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that |
6656 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
6657 | -- operand is an integer, as required for this to work. | |
6658 | ||
fbf5a39b AC |
6659 | if Rp2 then |
6660 | if Lp2 then | |
70482933 | 6661 | |
fbf5a39b | 6662 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
6663 | |
6664 | Rewrite (N, | |
6665 | Make_Op_Expon (Loc, | |
6666 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
6667 | Right_Opnd => | |
6668 | Make_Op_Add (Loc, | |
6669 | Left_Opnd => Right_Opnd (Lop), | |
6670 | Right_Opnd => Right_Opnd (Rop)))); | |
6671 | Analyze_And_Resolve (N, Typ); | |
6672 | return; | |
6673 | ||
6674 | else | |
6675 | Rewrite (N, | |
6676 | Make_Op_Shift_Left (Loc, | |
6677 | Left_Opnd => Lop, | |
6678 | Right_Opnd => | |
6679 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
6680 | Analyze_And_Resolve (N, Typ); | |
6681 | return; | |
6682 | end if; | |
6683 | ||
6684 | -- Same processing for the operands the other way round | |
6685 | ||
fbf5a39b | 6686 | elsif Lp2 then |
70482933 RK |
6687 | Rewrite (N, |
6688 | Make_Op_Shift_Left (Loc, | |
6689 | Left_Opnd => Rop, | |
6690 | Right_Opnd => | |
6691 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
6692 | Analyze_And_Resolve (N, Typ); | |
6693 | return; | |
6694 | end if; | |
6695 | ||
6696 | -- Do required fixup of universal fixed operation | |
6697 | ||
6698 | if Typ = Universal_Fixed then | |
6699 | Fixup_Universal_Fixed_Operation (N); | |
6700 | Typ := Etype (N); | |
6701 | end if; | |
6702 | ||
6703 | -- Multiplications with fixed-point results | |
6704 | ||
6705 | if Is_Fixed_Point_Type (Typ) then | |
6706 | ||
685094bf RD |
6707 | -- No special processing if Treat_Fixed_As_Integer is set, since from |
6708 | -- a semantic point of view such operations are simply integer | |
6709 | -- operations and will be treated that way. | |
70482933 RK |
6710 | |
6711 | if not Treat_Fixed_As_Integer (N) then | |
6712 | ||
6713 | -- Case of fixed * integer => fixed | |
6714 | ||
6715 | if Is_Integer_Type (Rtyp) then | |
6716 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
6717 | ||
6718 | -- Case of integer * fixed => fixed | |
6719 | ||
6720 | elsif Is_Integer_Type (Ltyp) then | |
6721 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
6722 | ||
6723 | -- Case of fixed * fixed => fixed | |
6724 | ||
6725 | else | |
6726 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
6727 | end if; | |
6728 | end if; | |
6729 | ||
685094bf RD |
6730 | -- Other cases of multiplication of fixed-point operands. Again we |
6731 | -- exclude the cases where Treat_Fixed_As_Integer flag is set. | |
70482933 RK |
6732 | |
6733 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
6734 | and then not Treat_Fixed_As_Integer (N) | |
6735 | then | |
6736 | if Is_Integer_Type (Typ) then | |
6737 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
6738 | else | |
6739 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
6740 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
6741 | end if; | |
6742 | ||
685094bf RD |
6743 | -- Mixed-mode operations can appear in a non-static universal context, |
6744 | -- in which case the integer argument must be converted explicitly. | |
70482933 RK |
6745 | |
6746 | elsif Typ = Universal_Real | |
6747 | and then Is_Integer_Type (Rtyp) | |
6748 | then | |
6749 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); | |
6750 | ||
6751 | Analyze_And_Resolve (Rop, Universal_Real); | |
6752 | ||
6753 | elsif Typ = Universal_Real | |
6754 | and then Is_Integer_Type (Ltyp) | |
6755 | then | |
6756 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); | |
6757 | ||
6758 | Analyze_And_Resolve (Lop, Universal_Real); | |
6759 | ||
6760 | -- Non-fixed point cases, check software overflow checking required | |
6761 | ||
6762 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
6763 | Apply_Arithmetic_Overflow_Check (N); | |
f02b8bb8 RD |
6764 | |
6765 | -- Deal with VAX float case | |
6766 | ||
6767 | elsif Vax_Float (Typ) then | |
6768 | Expand_Vax_Arith (N); | |
6769 | return; | |
70482933 RK |
6770 | end if; |
6771 | end Expand_N_Op_Multiply; | |
6772 | ||
6773 | -------------------- | |
6774 | -- Expand_N_Op_Ne -- | |
6775 | -------------------- | |
6776 | ||
70482933 | 6777 | procedure Expand_N_Op_Ne (N : Node_Id) is |
f02b8bb8 | 6778 | Typ : constant Entity_Id := Etype (Left_Opnd (N)); |
70482933 RK |
6779 | |
6780 | begin | |
f02b8bb8 | 6781 | -- Case of elementary type with standard operator |
70482933 | 6782 | |
f02b8bb8 RD |
6783 | if Is_Elementary_Type (Typ) |
6784 | and then Sloc (Entity (N)) = Standard_Location | |
6785 | then | |
6786 | Binary_Op_Validity_Checks (N); | |
70482933 | 6787 | |
f02b8bb8 | 6788 | -- Boolean types (requiring handling of non-standard case) |
70482933 | 6789 | |
f02b8bb8 RD |
6790 | if Is_Boolean_Type (Typ) then |
6791 | Adjust_Condition (Left_Opnd (N)); | |
6792 | Adjust_Condition (Right_Opnd (N)); | |
6793 | Set_Etype (N, Standard_Boolean); | |
6794 | Adjust_Result_Type (N, Typ); | |
6795 | end if; | |
fbf5a39b | 6796 | |
f02b8bb8 RD |
6797 | Rewrite_Comparison (N); |
6798 | ||
6799 | -- If we still have comparison for Vax_Float, process it | |
6800 | ||
6801 | if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then | |
6802 | Expand_Vax_Comparison (N); | |
6803 | return; | |
6804 | end if; | |
6805 | ||
6806 | -- For all cases other than elementary types, we rewrite node as the | |
6807 | -- negation of an equality operation, and reanalyze. The equality to be | |
6808 | -- used is defined in the same scope and has the same signature. This | |
6809 | -- signature must be set explicitly since in an instance it may not have | |
6810 | -- the same visibility as in the generic unit. This avoids duplicating | |
6811 | -- or factoring the complex code for record/array equality tests etc. | |
6812 | ||
6813 | else | |
6814 | declare | |
6815 | Loc : constant Source_Ptr := Sloc (N); | |
6816 | Neg : Node_Id; | |
6817 | Ne : constant Entity_Id := Entity (N); | |
6818 | ||
6819 | begin | |
6820 | Binary_Op_Validity_Checks (N); | |
6821 | ||
6822 | Neg := | |
6823 | Make_Op_Not (Loc, | |
6824 | Right_Opnd => | |
6825 | Make_Op_Eq (Loc, | |
6826 | Left_Opnd => Left_Opnd (N), | |
6827 | Right_Opnd => Right_Opnd (N))); | |
6828 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
6829 | ||
6830 | if Scope (Ne) /= Standard_Standard then | |
6831 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
6832 | end if; | |
6833 | ||
6834 | -- For navigation purposes, the inequality is treated as an | |
6835 | -- implicit reference to the corresponding equality. Preserve the | |
6836 | -- Comes_From_ source flag so that the proper Xref entry is | |
6837 | -- generated. | |
6838 | ||
6839 | Preserve_Comes_From_Source (Neg, N); | |
6840 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
6841 | Rewrite (N, Neg); | |
6842 | Analyze_And_Resolve (N, Standard_Boolean); | |
6843 | end; | |
6844 | end if; | |
70482933 RK |
6845 | end Expand_N_Op_Ne; |
6846 | ||
6847 | --------------------- | |
6848 | -- Expand_N_Op_Not -- | |
6849 | --------------------- | |
6850 | ||
685094bf | 6851 | -- If the argument is other than a Boolean array type, there is no special |
c77599d5 | 6852 | -- expansion required, except for VMS operations on signed integers. |
70482933 RK |
6853 | |
6854 | -- For the packed case, we call the special routine in Exp_Pakd, except | |
6855 | -- that if the component size is greater than one, we use the standard | |
6856 | -- routine generating a gruesome loop (it is so peculiar to have packed | |
685094bf RD |
6857 | -- arrays with non-standard Boolean representations anyway, so it does not |
6858 | -- matter that we do not handle this case efficiently). | |
70482933 | 6859 | |
685094bf RD |
6860 | -- For the unpacked case (and for the special packed case where we have non |
6861 | -- standard Booleans, as discussed above), we generate and insert into the | |
6862 | -- tree the following function definition: | |
70482933 RK |
6863 | |
6864 | -- function Nnnn (A : arr) is | |
6865 | -- B : arr; | |
6866 | -- begin | |
6867 | -- for J in a'range loop | |
6868 | -- B (J) := not A (J); | |
6869 | -- end loop; | |
6870 | -- return B; | |
6871 | -- end Nnnn; | |
6872 | ||
6873 | -- Here arr is the actual subtype of the parameter (and hence always | |
6874 | -- constrained). Then we replace the not with a call to this function. | |
6875 | ||
6876 | procedure Expand_N_Op_Not (N : Node_Id) is | |
6877 | Loc : constant Source_Ptr := Sloc (N); | |
6878 | Typ : constant Entity_Id := Etype (N); | |
6879 | Opnd : Node_Id; | |
6880 | Arr : Entity_Id; | |
6881 | A : Entity_Id; | |
6882 | B : Entity_Id; | |
6883 | J : Entity_Id; | |
6884 | A_J : Node_Id; | |
6885 | B_J : Node_Id; | |
6886 | ||
6887 | Func_Name : Entity_Id; | |
6888 | Loop_Statement : Node_Id; | |
6889 | ||
6890 | begin | |
6891 | Unary_Op_Validity_Checks (N); | |
6892 | ||
6893 | -- For boolean operand, deal with non-standard booleans | |
6894 | ||
6895 | if Is_Boolean_Type (Typ) then | |
6896 | Adjust_Condition (Right_Opnd (N)); | |
6897 | Set_Etype (N, Standard_Boolean); | |
6898 | Adjust_Result_Type (N, Typ); | |
6899 | return; | |
6900 | end if; | |
6901 | ||
c77599d5 AC |
6902 | -- For the VMS "not" on signed integer types, use conversion to and |
6903 | -- from a predefined modular type. | |
6904 | ||
6905 | if Is_VMS_Operator (Entity (N)) then | |
6906 | declare | |
9bebf0e9 AC |
6907 | Rtyp : Entity_Id; |
6908 | Utyp : Entity_Id; | |
6909 | ||
c77599d5 | 6910 | begin |
9bebf0e9 AC |
6911 | -- If this is a derived type, retrieve original VMS type so that |
6912 | -- the proper sized type is used for intermediate values. | |
6913 | ||
6914 | if Is_Derived_Type (Typ) then | |
6915 | Rtyp := First_Subtype (Etype (Typ)); | |
6916 | else | |
6917 | Rtyp := Typ; | |
6918 | end if; | |
6919 | ||
6920 | -- The proper unsigned type must have a size compatible with | |
6921 | -- the operand, to prevent misalignment.. | |
6922 | ||
6923 | if RM_Size (Rtyp) <= 8 then | |
6924 | Utyp := RTE (RE_Unsigned_8); | |
6925 | ||
6926 | elsif RM_Size (Rtyp) <= 16 then | |
6927 | Utyp := RTE (RE_Unsigned_16); | |
6928 | ||
6929 | elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then | |
6930 | Utyp := Typ; | |
6931 | ||
6932 | else | |
6933 | Utyp := RTE (RE_Long_Long_Unsigned); | |
6934 | end if; | |
6935 | ||
c77599d5 AC |
6936 | Rewrite (N, |
6937 | Unchecked_Convert_To (Typ, | |
9bebf0e9 AC |
6938 | Make_Op_Not (Loc, |
6939 | Unchecked_Convert_To (Utyp, Right_Opnd (N))))); | |
c77599d5 AC |
6940 | Analyze_And_Resolve (N, Typ); |
6941 | return; | |
6942 | end; | |
6943 | end if; | |
6944 | ||
70482933 RK |
6945 | -- Only array types need any other processing |
6946 | ||
6947 | if not Is_Array_Type (Typ) then | |
6948 | return; | |
6949 | end if; | |
6950 | ||
a9d8907c JM |
6951 | -- Case of array operand. If bit packed with a component size of 1, |
6952 | -- handle it in Exp_Pakd if the operand is known to be aligned. | |
70482933 | 6953 | |
a9d8907c JM |
6954 | if Is_Bit_Packed_Array (Typ) |
6955 | and then Component_Size (Typ) = 1 | |
6956 | and then not Is_Possibly_Unaligned_Object (Right_Opnd (N)) | |
6957 | then | |
70482933 RK |
6958 | Expand_Packed_Not (N); |
6959 | return; | |
6960 | end if; | |
6961 | ||
fbf5a39b AC |
6962 | -- Case of array operand which is not bit-packed. If the context is |
6963 | -- a safe assignment, call in-place operation, If context is a larger | |
6964 | -- boolean expression in the context of a safe assignment, expansion is | |
6965 | -- done by enclosing operation. | |
70482933 RK |
6966 | |
6967 | Opnd := Relocate_Node (Right_Opnd (N)); | |
6968 | Convert_To_Actual_Subtype (Opnd); | |
6969 | Arr := Etype (Opnd); | |
6970 | Ensure_Defined (Arr, N); | |
b4592168 | 6971 | Silly_Boolean_Array_Not_Test (N, Arr); |
70482933 | 6972 | |
fbf5a39b AC |
6973 | if Nkind (Parent (N)) = N_Assignment_Statement then |
6974 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
6975 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
6976 | return; | |
6977 | ||
5e1c00fa | 6978 | -- Special case the negation of a binary operation |
fbf5a39b | 6979 | |
303b4d58 | 6980 | elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor) |
fbf5a39b | 6981 | and then Safe_In_Place_Array_Op |
303b4d58 | 6982 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) |
fbf5a39b AC |
6983 | then |
6984 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
6985 | return; | |
6986 | end if; | |
6987 | ||
6988 | elsif Nkind (Parent (N)) in N_Binary_Op | |
6989 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
6990 | then | |
6991 | declare | |
6992 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
6993 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
6994 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
6995 | ||
6996 | begin | |
6997 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
6998 | if N = Op1 | |
6999 | and then Nkind (Op2) = N_Op_Not | |
7000 | then | |
5e1c00fa | 7001 | -- (not A) op (not B) can be reduced to a single call |
fbf5a39b AC |
7002 | |
7003 | return; | |
7004 | ||
7005 | elsif N = Op2 | |
7006 | and then Nkind (Parent (N)) = N_Op_Xor | |
7007 | then | |
5e1c00fa | 7008 | -- A xor (not B) can also be special-cased |
fbf5a39b AC |
7009 | |
7010 | return; | |
7011 | end if; | |
7012 | end if; | |
7013 | end; | |
7014 | end if; | |
7015 | ||
70482933 RK |
7016 | A := Make_Defining_Identifier (Loc, Name_uA); |
7017 | B := Make_Defining_Identifier (Loc, Name_uB); | |
7018 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
7019 | ||
7020 | A_J := | |
7021 | Make_Indexed_Component (Loc, | |
7022 | Prefix => New_Reference_To (A, Loc), | |
7023 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7024 | ||
7025 | B_J := | |
7026 | Make_Indexed_Component (Loc, | |
7027 | Prefix => New_Reference_To (B, Loc), | |
7028 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7029 | ||
7030 | Loop_Statement := | |
7031 | Make_Implicit_Loop_Statement (N, | |
7032 | Identifier => Empty, | |
7033 | ||
7034 | Iteration_Scheme => | |
7035 | Make_Iteration_Scheme (Loc, | |
7036 | Loop_Parameter_Specification => | |
7037 | Make_Loop_Parameter_Specification (Loc, | |
7038 | Defining_Identifier => J, | |
7039 | Discrete_Subtype_Definition => | |
7040 | Make_Attribute_Reference (Loc, | |
7041 | Prefix => Make_Identifier (Loc, Chars (A)), | |
7042 | Attribute_Name => Name_Range))), | |
7043 | ||
7044 | Statements => New_List ( | |
7045 | Make_Assignment_Statement (Loc, | |
7046 | Name => B_J, | |
7047 | Expression => Make_Op_Not (Loc, A_J)))); | |
7048 | ||
191fcb3a | 7049 | Func_Name := Make_Temporary (Loc, 'N'); |
70482933 RK |
7050 | Set_Is_Inlined (Func_Name); |
7051 | ||
7052 | Insert_Action (N, | |
7053 | Make_Subprogram_Body (Loc, | |
7054 | Specification => | |
7055 | Make_Function_Specification (Loc, | |
7056 | Defining_Unit_Name => Func_Name, | |
7057 | Parameter_Specifications => New_List ( | |
7058 | Make_Parameter_Specification (Loc, | |
7059 | Defining_Identifier => A, | |
7060 | Parameter_Type => New_Reference_To (Typ, Loc))), | |
630d30e9 | 7061 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
7062 | |
7063 | Declarations => New_List ( | |
7064 | Make_Object_Declaration (Loc, | |
7065 | Defining_Identifier => B, | |
7066 | Object_Definition => New_Reference_To (Arr, Loc))), | |
7067 | ||
7068 | Handled_Statement_Sequence => | |
7069 | Make_Handled_Sequence_Of_Statements (Loc, | |
7070 | Statements => New_List ( | |
7071 | Loop_Statement, | |
d766cee3 | 7072 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
7073 | Expression => |
7074 | Make_Identifier (Loc, Chars (B))))))); | |
7075 | ||
7076 | Rewrite (N, | |
7077 | Make_Function_Call (Loc, | |
7078 | Name => New_Reference_To (Func_Name, Loc), | |
7079 | Parameter_Associations => New_List (Opnd))); | |
7080 | ||
7081 | Analyze_And_Resolve (N, Typ); | |
7082 | end Expand_N_Op_Not; | |
7083 | ||
7084 | -------------------- | |
7085 | -- Expand_N_Op_Or -- | |
7086 | -------------------- | |
7087 | ||
7088 | procedure Expand_N_Op_Or (N : Node_Id) is | |
7089 | Typ : constant Entity_Id := Etype (N); | |
7090 | ||
7091 | begin | |
7092 | Binary_Op_Validity_Checks (N); | |
7093 | ||
7094 | if Is_Array_Type (Etype (N)) then | |
7095 | Expand_Boolean_Operator (N); | |
7096 | ||
7097 | elsif Is_Boolean_Type (Etype (N)) then | |
6a2afd13 AC |
7098 | |
7099 | -- Replace OR by OR ELSE if Short_Circuit_And_Or active and the | |
7100 | -- type is standard Boolean (do not mess with AND that uses a non- | |
7101 | -- standard Boolean type, because something strange is going on). | |
7102 | ||
7103 | if Short_Circuit_And_Or and then Typ = Standard_Boolean then | |
7104 | Rewrite (N, | |
7105 | Make_Or_Else (Sloc (N), | |
7106 | Left_Opnd => Relocate_Node (Left_Opnd (N)), | |
7107 | Right_Opnd => Relocate_Node (Right_Opnd (N)))); | |
7108 | Analyze_And_Resolve (N, Typ); | |
7109 | ||
7110 | -- Otherwise, adjust conditions | |
7111 | ||
7112 | else | |
7113 | Adjust_Condition (Left_Opnd (N)); | |
7114 | Adjust_Condition (Right_Opnd (N)); | |
7115 | Set_Etype (N, Standard_Boolean); | |
7116 | Adjust_Result_Type (N, Typ); | |
7117 | end if; | |
70482933 RK |
7118 | end if; |
7119 | end Expand_N_Op_Or; | |
7120 | ||
7121 | ---------------------- | |
7122 | -- Expand_N_Op_Plus -- | |
7123 | ---------------------- | |
7124 | ||
7125 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
7126 | begin | |
7127 | Unary_Op_Validity_Checks (N); | |
7128 | end Expand_N_Op_Plus; | |
7129 | ||
7130 | --------------------- | |
7131 | -- Expand_N_Op_Rem -- | |
7132 | --------------------- | |
7133 | ||
7134 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
7135 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 7136 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
7137 | |
7138 | Left : constant Node_Id := Left_Opnd (N); | |
7139 | Right : constant Node_Id := Right_Opnd (N); | |
7140 | ||
5d5e9775 AC |
7141 | Lo : Uint; |
7142 | Hi : Uint; | |
7143 | OK : Boolean; | |
70482933 | 7144 | |
5d5e9775 AC |
7145 | Lneg : Boolean; |
7146 | Rneg : Boolean; | |
7147 | -- Set if corresponding operand can be negative | |
7148 | ||
7149 | pragma Unreferenced (Hi); | |
1033834f | 7150 | |
70482933 RK |
7151 | begin |
7152 | Binary_Op_Validity_Checks (N); | |
7153 | ||
7154 | if Is_Integer_Type (Etype (N)) then | |
7155 | Apply_Divide_Check (N); | |
7156 | end if; | |
7157 | ||
685094bf RD |
7158 | -- Apply optimization x rem 1 = 0. We don't really need that with gcc, |
7159 | -- but it is useful with other back ends (e.g. AAMP), and is certainly | |
7160 | -- harmless. | |
fbf5a39b AC |
7161 | |
7162 | if Is_Integer_Type (Etype (N)) | |
7163 | and then Compile_Time_Known_Value (Right) | |
7164 | and then Expr_Value (Right) = Uint_1 | |
7165 | then | |
abcbd24c ST |
7166 | -- Call Remove_Side_Effects to ensure that any side effects in the |
7167 | -- ignored left operand (in particular function calls to user defined | |
7168 | -- functions) are properly executed. | |
7169 | ||
7170 | Remove_Side_Effects (Left); | |
7171 | ||
fbf5a39b AC |
7172 | Rewrite (N, Make_Integer_Literal (Loc, 0)); |
7173 | Analyze_And_Resolve (N, Typ); | |
7174 | return; | |
7175 | end if; | |
7176 | ||
685094bf RD |
7177 | -- Deal with annoying case of largest negative number remainder minus |
7178 | -- one. Gigi does not handle this case correctly, because it generates | |
7179 | -- a divide instruction which may trap in this case. | |
70482933 | 7180 | |
685094bf RD |
7181 | -- In fact the check is quite easy, if the right operand is -1, then |
7182 | -- the remainder is always 0, and we can just ignore the left operand | |
7183 | -- completely in this case. | |
70482933 | 7184 | |
5d5e9775 AC |
7185 | Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True); |
7186 | Lneg := (not OK) or else Lo < 0; | |
fbf5a39b | 7187 | |
5d5e9775 AC |
7188 | Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True); |
7189 | Rneg := (not OK) or else Lo < 0; | |
fbf5a39b | 7190 | |
5d5e9775 AC |
7191 | -- We won't mess with trying to find out if the left operand can really |
7192 | -- be the largest negative number (that's a pain in the case of private | |
7193 | -- types and this is really marginal). We will just assume that we need | |
7194 | -- the test if the left operand can be negative at all. | |
fbf5a39b | 7195 | |
5d5e9775 | 7196 | if Lneg and Rneg then |
70482933 RK |
7197 | Rewrite (N, |
7198 | Make_Conditional_Expression (Loc, | |
7199 | Expressions => New_List ( | |
7200 | Make_Op_Eq (Loc, | |
7201 | Left_Opnd => Duplicate_Subexpr (Right), | |
7202 | Right_Opnd => | |
fbf5a39b AC |
7203 | Unchecked_Convert_To (Typ, |
7204 | Make_Integer_Literal (Loc, -1))), | |
70482933 | 7205 | |
fbf5a39b AC |
7206 | Unchecked_Convert_To (Typ, |
7207 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
7208 | |
7209 | Relocate_Node (N)))); | |
7210 | ||
7211 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
7212 | Analyze_And_Resolve (N, Typ); | |
7213 | end if; | |
7214 | end Expand_N_Op_Rem; | |
7215 | ||
7216 | ----------------------------- | |
7217 | -- Expand_N_Op_Rotate_Left -- | |
7218 | ----------------------------- | |
7219 | ||
7220 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
7221 | begin | |
7222 | Binary_Op_Validity_Checks (N); | |
7223 | end Expand_N_Op_Rotate_Left; | |
7224 | ||
7225 | ------------------------------ | |
7226 | -- Expand_N_Op_Rotate_Right -- | |
7227 | ------------------------------ | |
7228 | ||
7229 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
7230 | begin | |
7231 | Binary_Op_Validity_Checks (N); | |
7232 | end Expand_N_Op_Rotate_Right; | |
7233 | ||
7234 | ---------------------------- | |
7235 | -- Expand_N_Op_Shift_Left -- | |
7236 | ---------------------------- | |
7237 | ||
7238 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is | |
7239 | begin | |
7240 | Binary_Op_Validity_Checks (N); | |
7241 | end Expand_N_Op_Shift_Left; | |
7242 | ||
7243 | ----------------------------- | |
7244 | -- Expand_N_Op_Shift_Right -- | |
7245 | ----------------------------- | |
7246 | ||
7247 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
7248 | begin | |
7249 | Binary_Op_Validity_Checks (N); | |
7250 | end Expand_N_Op_Shift_Right; | |
7251 | ||
7252 | ---------------------------------------- | |
7253 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
7254 | ---------------------------------------- | |
7255 | ||
7256 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
7257 | begin | |
7258 | Binary_Op_Validity_Checks (N); | |
7259 | end Expand_N_Op_Shift_Right_Arithmetic; | |
7260 | ||
7261 | -------------------------- | |
7262 | -- Expand_N_Op_Subtract -- | |
7263 | -------------------------- | |
7264 | ||
7265 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
7266 | Typ : constant Entity_Id := Etype (N); | |
7267 | ||
7268 | begin | |
7269 | Binary_Op_Validity_Checks (N); | |
7270 | ||
7271 | -- N - 0 = N for integer types | |
7272 | ||
7273 | if Is_Integer_Type (Typ) | |
7274 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
7275 | and then Expr_Value (Right_Opnd (N)) = 0 | |
7276 | then | |
7277 | Rewrite (N, Left_Opnd (N)); | |
7278 | return; | |
7279 | end if; | |
7280 | ||
8fc789c8 | 7281 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 RK |
7282 | |
7283 | if Is_Signed_Integer_Type (Typ) | |
7284 | or else Is_Fixed_Point_Type (Typ) | |
7285 | then | |
7286 | Apply_Arithmetic_Overflow_Check (N); | |
7287 | ||
7288 | -- Vax floating-point types case | |
7289 | ||
7290 | elsif Vax_Float (Typ) then | |
7291 | Expand_Vax_Arith (N); | |
7292 | end if; | |
7293 | end Expand_N_Op_Subtract; | |
7294 | ||
7295 | --------------------- | |
7296 | -- Expand_N_Op_Xor -- | |
7297 | --------------------- | |
7298 | ||
7299 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
7300 | Typ : constant Entity_Id := Etype (N); | |
7301 | ||
7302 | begin | |
7303 | Binary_Op_Validity_Checks (N); | |
7304 | ||
7305 | if Is_Array_Type (Etype (N)) then | |
7306 | Expand_Boolean_Operator (N); | |
7307 | ||
7308 | elsif Is_Boolean_Type (Etype (N)) then | |
7309 | Adjust_Condition (Left_Opnd (N)); | |
7310 | Adjust_Condition (Right_Opnd (N)); | |
7311 | Set_Etype (N, Standard_Boolean); | |
7312 | Adjust_Result_Type (N, Typ); | |
7313 | end if; | |
7314 | end Expand_N_Op_Xor; | |
7315 | ||
7316 | ---------------------- | |
7317 | -- Expand_N_Or_Else -- | |
7318 | ---------------------- | |
7319 | ||
5875f8d6 AC |
7320 | procedure Expand_N_Or_Else (N : Node_Id) |
7321 | renames Expand_Short_Circuit_Operator; | |
70482933 RK |
7322 | |
7323 | ----------------------------------- | |
7324 | -- Expand_N_Qualified_Expression -- | |
7325 | ----------------------------------- | |
7326 | ||
7327 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
7328 | Operand : constant Node_Id := Expression (N); | |
7329 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
7330 | ||
7331 | begin | |
f82944b7 JM |
7332 | -- Do validity check if validity checking operands |
7333 | ||
7334 | if Validity_Checks_On | |
7335 | and then Validity_Check_Operands | |
7336 | then | |
7337 | Ensure_Valid (Operand); | |
7338 | end if; | |
7339 | ||
7340 | -- Apply possible constraint check | |
7341 | ||
70482933 | 7342 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); |
d79e621a GD |
7343 | |
7344 | if Do_Range_Check (Operand) then | |
7345 | Set_Do_Range_Check (Operand, False); | |
7346 | Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed); | |
7347 | end if; | |
70482933 RK |
7348 | end Expand_N_Qualified_Expression; |
7349 | ||
7350 | --------------------------------- | |
7351 | -- Expand_N_Selected_Component -- | |
7352 | --------------------------------- | |
7353 | ||
7354 | -- If the selector is a discriminant of a concurrent object, rewrite the | |
7355 | -- prefix to denote the corresponding record type. | |
7356 | ||
7357 | procedure Expand_N_Selected_Component (N : Node_Id) is | |
7358 | Loc : constant Source_Ptr := Sloc (N); | |
7359 | Par : constant Node_Id := Parent (N); | |
7360 | P : constant Node_Id := Prefix (N); | |
fbf5a39b | 7361 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 7362 | Disc : Entity_Id; |
70482933 | 7363 | New_N : Node_Id; |
fbf5a39b | 7364 | Dcon : Elmt_Id; |
70482933 RK |
7365 | |
7366 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
7367 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
7368 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
7369 | -- Don't we have a general routine that does this??? |
7370 | ||
7371 | ----------------------- | |
7372 | -- In_Left_Hand_Side -- | |
7373 | ----------------------- | |
70482933 RK |
7374 | |
7375 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
7376 | begin | |
fbf5a39b AC |
7377 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
7378 | and then Comp = Name (Parent (Comp))) | |
7379 | or else (Present (Parent (Comp)) | |
7380 | and then Nkind (Parent (Comp)) in N_Subexpr | |
7381 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
7382 | end In_Left_Hand_Side; |
7383 | ||
fbf5a39b AC |
7384 | -- Start of processing for Expand_N_Selected_Component |
7385 | ||
70482933 | 7386 | begin |
fbf5a39b AC |
7387 | -- Insert explicit dereference if required |
7388 | ||
7389 | if Is_Access_Type (Ptyp) then | |
7390 | Insert_Explicit_Dereference (P); | |
e6f69614 | 7391 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
7392 | |
7393 | if Ekind (Etype (P)) = E_Private_Subtype | |
7394 | and then Is_For_Access_Subtype (Etype (P)) | |
7395 | then | |
7396 | Set_Etype (P, Base_Type (Etype (P))); | |
7397 | end if; | |
7398 | ||
7399 | Ptyp := Etype (P); | |
7400 | end if; | |
7401 | ||
7402 | -- Deal with discriminant check required | |
7403 | ||
70482933 RK |
7404 | if Do_Discriminant_Check (N) then |
7405 | ||
685094bf RD |
7406 | -- Present the discriminant checking function to the backend, so that |
7407 | -- it can inline the call to the function. | |
70482933 RK |
7408 | |
7409 | Add_Inlined_Body | |
7410 | (Discriminant_Checking_Func | |
7411 | (Original_Record_Component (Entity (Selector_Name (N))))); | |
70482933 | 7412 | |
fbf5a39b | 7413 | -- Now reset the flag and generate the call |
70482933 | 7414 | |
fbf5a39b AC |
7415 | Set_Do_Discriminant_Check (N, False); |
7416 | Generate_Discriminant_Check (N); | |
70482933 RK |
7417 | end if; |
7418 | ||
b4592168 GD |
7419 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
7420 | -- function, then additional actuals must be passed. | |
7421 | ||
7422 | if Ada_Version >= Ada_05 | |
7423 | and then Is_Build_In_Place_Function_Call (P) | |
7424 | then | |
7425 | Make_Build_In_Place_Call_In_Anonymous_Context (P); | |
7426 | end if; | |
7427 | ||
fbf5a39b AC |
7428 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
7429 | -- selected component with discriminants. This must be checked during | |
7430 | -- expansion, because during analysis the type of the selector is not | |
7431 | -- known at the point the prefix is analyzed. If the conversion is the | |
7432 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
7433 | |
7434 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
7435 | and then Has_Discriminants (Etype (N)) | |
7436 | and then not In_Left_Hand_Side (N) | |
7437 | then | |
7438 | Force_Evaluation (Prefix (N)); | |
7439 | end if; | |
7440 | ||
7441 | -- Remaining processing applies only if selector is a discriminant | |
7442 | ||
7443 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
7444 | ||
7445 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
7446 | -- we may be able to rewrite the expression with the actual value |
7447 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
7448 | |
7449 | if Is_Record_Type (Ptyp) | |
7450 | and then Has_Discriminants (Ptyp) | |
7451 | and then Is_Constrained (Ptyp) | |
70482933 | 7452 | then |
fbf5a39b AC |
7453 | -- Do this optimization for discrete types only, and not for |
7454 | -- access types (access discriminants get us into trouble!) | |
70482933 | 7455 | |
fbf5a39b AC |
7456 | if not Is_Discrete_Type (Etype (N)) then |
7457 | null; | |
7458 | ||
7459 | -- Don't do this on the left hand of an assignment statement. | |
7460 | -- Normally one would think that references like this would | |
7461 | -- not occur, but they do in generated code, and mean that | |
7462 | -- we really do want to assign the discriminant! | |
7463 | ||
7464 | elsif Nkind (Par) = N_Assignment_Statement | |
7465 | and then Name (Par) = N | |
7466 | then | |
7467 | null; | |
7468 | ||
685094bf RD |
7469 | -- Don't do this optimization for the prefix of an attribute or |
7470 | -- the operand of an object renaming declaration since these are | |
7471 | -- contexts where we do not want the value anyway. | |
fbf5a39b AC |
7472 | |
7473 | elsif (Nkind (Par) = N_Attribute_Reference | |
7474 | and then Prefix (Par) = N) | |
7475 | or else Is_Renamed_Object (N) | |
7476 | then | |
7477 | null; | |
7478 | ||
7479 | -- Don't do this optimization if we are within the code for a | |
7480 | -- discriminant check, since the whole point of such a check may | |
7481 | -- be to verify the condition on which the code below depends! | |
7482 | ||
7483 | elsif Is_In_Discriminant_Check (N) then | |
7484 | null; | |
7485 | ||
7486 | -- Green light to see if we can do the optimization. There is | |
685094bf RD |
7487 | -- still one condition that inhibits the optimization below but |
7488 | -- now is the time to check the particular discriminant. | |
fbf5a39b AC |
7489 | |
7490 | else | |
685094bf RD |
7491 | -- Loop through discriminants to find the matching discriminant |
7492 | -- constraint to see if we can copy it. | |
fbf5a39b AC |
7493 | |
7494 | Disc := First_Discriminant (Ptyp); | |
7495 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
7496 | Discr_Loop : while Present (Dcon) loop | |
7497 | ||
7498 | -- Check if this is the matching discriminant | |
7499 | ||
7500 | if Disc = Entity (Selector_Name (N)) then | |
70482933 | 7501 | |
fbf5a39b AC |
7502 | -- Here we have the matching discriminant. Check for |
7503 | -- the case of a discriminant of a component that is | |
7504 | -- constrained by an outer discriminant, which cannot | |
7505 | -- be optimized away. | |
7506 | ||
7507 | if | |
7508 | Denotes_Discriminant | |
20b5d666 | 7509 | (Node (Dcon), Check_Concurrent => True) |
fbf5a39b AC |
7510 | then |
7511 | exit Discr_Loop; | |
70482933 | 7512 | |
685094bf RD |
7513 | -- In the context of a case statement, the expression may |
7514 | -- have the base type of the discriminant, and we need to | |
7515 | -- preserve the constraint to avoid spurious errors on | |
7516 | -- missing cases. | |
70482933 | 7517 | |
fbf5a39b AC |
7518 | elsif Nkind (Parent (N)) = N_Case_Statement |
7519 | and then Etype (Node (Dcon)) /= Etype (Disc) | |
70482933 RK |
7520 | then |
7521 | Rewrite (N, | |
7522 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
7523 | Subtype_Mark => |
7524 | New_Occurrence_Of (Etype (Disc), Loc), | |
7525 | Expression => | |
ffe9aba8 AC |
7526 | New_Copy_Tree (Node (Dcon)))); |
7527 | Analyze_And_Resolve (N, Etype (Disc)); | |
fbf5a39b AC |
7528 | |
7529 | -- In case that comes out as a static expression, | |
7530 | -- reset it (a selected component is never static). | |
7531 | ||
7532 | Set_Is_Static_Expression (N, False); | |
7533 | return; | |
7534 | ||
7535 | -- Otherwise we can just copy the constraint, but the | |
ffe9aba8 AC |
7536 | -- result is certainly not static! In some cases the |
7537 | -- discriminant constraint has been analyzed in the | |
7538 | -- context of the original subtype indication, but for | |
7539 | -- itypes the constraint might not have been analyzed | |
7540 | -- yet, and this must be done now. | |
fbf5a39b | 7541 | |
70482933 | 7542 | else |
ffe9aba8 AC |
7543 | Rewrite (N, New_Copy_Tree (Node (Dcon))); |
7544 | Analyze_And_Resolve (N); | |
fbf5a39b AC |
7545 | Set_Is_Static_Expression (N, False); |
7546 | return; | |
70482933 | 7547 | end if; |
70482933 RK |
7548 | end if; |
7549 | ||
fbf5a39b AC |
7550 | Next_Elmt (Dcon); |
7551 | Next_Discriminant (Disc); | |
7552 | end loop Discr_Loop; | |
70482933 | 7553 | |
fbf5a39b AC |
7554 | -- Note: the above loop should always find a matching |
7555 | -- discriminant, but if it does not, we just missed an | |
685094bf RD |
7556 | -- optimization due to some glitch (perhaps a previous error), |
7557 | -- so ignore. | |
fbf5a39b AC |
7558 | |
7559 | end if; | |
70482933 RK |
7560 | end if; |
7561 | ||
7562 | -- The only remaining processing is in the case of a discriminant of | |
7563 | -- a concurrent object, where we rewrite the prefix to denote the | |
7564 | -- corresponding record type. If the type is derived and has renamed | |
7565 | -- discriminants, use corresponding discriminant, which is the one | |
7566 | -- that appears in the corresponding record. | |
7567 | ||
7568 | if not Is_Concurrent_Type (Ptyp) then | |
7569 | return; | |
7570 | end if; | |
7571 | ||
7572 | Disc := Entity (Selector_Name (N)); | |
7573 | ||
7574 | if Is_Derived_Type (Ptyp) | |
7575 | and then Present (Corresponding_Discriminant (Disc)) | |
7576 | then | |
7577 | Disc := Corresponding_Discriminant (Disc); | |
7578 | end if; | |
7579 | ||
7580 | New_N := | |
7581 | Make_Selected_Component (Loc, | |
7582 | Prefix => | |
7583 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
7584 | New_Copy_Tree (P)), | |
7585 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
7586 | ||
7587 | Rewrite (N, New_N); | |
7588 | Analyze (N); | |
7589 | end if; | |
70482933 RK |
7590 | end Expand_N_Selected_Component; |
7591 | ||
7592 | -------------------- | |
7593 | -- Expand_N_Slice -- | |
7594 | -------------------- | |
7595 | ||
7596 | procedure Expand_N_Slice (N : Node_Id) is | |
7597 | Loc : constant Source_Ptr := Sloc (N); | |
7598 | Typ : constant Entity_Id := Etype (N); | |
7599 | Pfx : constant Node_Id := Prefix (N); | |
7600 | Ptp : Entity_Id := Etype (Pfx); | |
fbf5a39b | 7601 | |
81a5b587 | 7602 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
685094bf RD |
7603 | -- Check whether the argument is an actual for a procedure call, in |
7604 | -- which case the expansion of a bit-packed slice is deferred until the | |
7605 | -- call itself is expanded. The reason this is required is that we might | |
7606 | -- have an IN OUT or OUT parameter, and the copy out is essential, and | |
7607 | -- that copy out would be missed if we created a temporary here in | |
7608 | -- Expand_N_Slice. Note that we don't bother to test specifically for an | |
7609 | -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it | |
7610 | -- is harmless to defer expansion in the IN case, since the call | |
7611 | -- processing will still generate the appropriate copy in operation, | |
7612 | -- which will take care of the slice. | |
81a5b587 | 7613 | |
b01bf852 | 7614 | procedure Make_Temporary_For_Slice; |
685094bf RD |
7615 | -- Create a named variable for the value of the slice, in cases where |
7616 | -- the back-end cannot handle it properly, e.g. when packed types or | |
7617 | -- unaligned slices are involved. | |
fbf5a39b | 7618 | |
81a5b587 AC |
7619 | ------------------------- |
7620 | -- Is_Procedure_Actual -- | |
7621 | ------------------------- | |
7622 | ||
7623 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
7624 | Par : Node_Id := Parent (N); | |
08aa9a4a | 7625 | |
81a5b587 | 7626 | begin |
81a5b587 | 7627 | loop |
c6a60aa1 RD |
7628 | -- If our parent is a procedure call we can return |
7629 | ||
81a5b587 AC |
7630 | if Nkind (Par) = N_Procedure_Call_Statement then |
7631 | return True; | |
6b6fcd3e | 7632 | |
685094bf RD |
7633 | -- If our parent is a type conversion, keep climbing the tree, |
7634 | -- since a type conversion can be a procedure actual. Also keep | |
7635 | -- climbing if parameter association or a qualified expression, | |
7636 | -- since these are additional cases that do can appear on | |
7637 | -- procedure actuals. | |
6b6fcd3e | 7638 | |
303b4d58 AC |
7639 | elsif Nkind_In (Par, N_Type_Conversion, |
7640 | N_Parameter_Association, | |
7641 | N_Qualified_Expression) | |
c6a60aa1 | 7642 | then |
81a5b587 | 7643 | Par := Parent (Par); |
c6a60aa1 RD |
7644 | |
7645 | -- Any other case is not what we are looking for | |
7646 | ||
7647 | else | |
7648 | return False; | |
81a5b587 AC |
7649 | end if; |
7650 | end loop; | |
81a5b587 AC |
7651 | end Is_Procedure_Actual; |
7652 | ||
b01bf852 AC |
7653 | ------------------------------ |
7654 | -- Make_Temporary_For_Slice -- | |
7655 | ------------------------------ | |
fbf5a39b | 7656 | |
b01bf852 | 7657 | procedure Make_Temporary_For_Slice is |
fbf5a39b | 7658 | Decl : Node_Id; |
b01bf852 | 7659 | Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
13d923cc | 7660 | |
fbf5a39b AC |
7661 | begin |
7662 | Decl := | |
7663 | Make_Object_Declaration (Loc, | |
7664 | Defining_Identifier => Ent, | |
7665 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
7666 | ||
7667 | Set_No_Initialization (Decl); | |
7668 | ||
7669 | Insert_Actions (N, New_List ( | |
7670 | Decl, | |
7671 | Make_Assignment_Statement (Loc, | |
7672 | Name => New_Occurrence_Of (Ent, Loc), | |
7673 | Expression => Relocate_Node (N)))); | |
7674 | ||
7675 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
7676 | Analyze_And_Resolve (N, Typ); | |
b01bf852 | 7677 | end Make_Temporary_For_Slice; |
fbf5a39b AC |
7678 | |
7679 | -- Start of processing for Expand_N_Slice | |
70482933 RK |
7680 | |
7681 | begin | |
7682 | -- Special handling for access types | |
7683 | ||
7684 | if Is_Access_Type (Ptp) then | |
7685 | ||
70482933 RK |
7686 | Ptp := Designated_Type (Ptp); |
7687 | ||
e6f69614 AC |
7688 | Rewrite (Pfx, |
7689 | Make_Explicit_Dereference (Sloc (N), | |
7690 | Prefix => Relocate_Node (Pfx))); | |
70482933 | 7691 | |
e6f69614 | 7692 | Analyze_And_Resolve (Pfx, Ptp); |
70482933 RK |
7693 | end if; |
7694 | ||
b4592168 GD |
7695 | -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place |
7696 | -- function, then additional actuals must be passed. | |
7697 | ||
7698 | if Ada_Version >= Ada_05 | |
7699 | and then Is_Build_In_Place_Function_Call (Pfx) | |
7700 | then | |
7701 | Make_Build_In_Place_Call_In_Anonymous_Context (Pfx); | |
7702 | end if; | |
7703 | ||
70482933 RK |
7704 | -- The remaining case to be handled is packed slices. We can leave |
7705 | -- packed slices as they are in the following situations: | |
7706 | ||
7707 | -- 1. Right or left side of an assignment (we can handle this | |
7708 | -- situation correctly in the assignment statement expansion). | |
7709 | ||
685094bf RD |
7710 | -- 2. Prefix of indexed component (the slide is optimized away in this |
7711 | -- case, see the start of Expand_N_Slice.) | |
70482933 | 7712 | |
685094bf RD |
7713 | -- 3. Object renaming declaration, since we want the name of the |
7714 | -- slice, not the value. | |
70482933 | 7715 | |
685094bf RD |
7716 | -- 4. Argument to procedure call, since copy-in/copy-out handling may |
7717 | -- be required, and this is handled in the expansion of call | |
7718 | -- itself. | |
70482933 | 7719 | |
685094bf RD |
7720 | -- 5. Prefix of an address attribute (this is an error which is caught |
7721 | -- elsewhere, and the expansion would interfere with generating the | |
7722 | -- error message). | |
70482933 | 7723 | |
81a5b587 | 7724 | if not Is_Packed (Typ) then |
08aa9a4a | 7725 | |
685094bf RD |
7726 | -- Apply transformation for actuals of a function call, where |
7727 | -- Expand_Actuals is not used. | |
81a5b587 AC |
7728 | |
7729 | if Nkind (Parent (N)) = N_Function_Call | |
7730 | and then Is_Possibly_Unaligned_Slice (N) | |
7731 | then | |
b01bf852 | 7732 | Make_Temporary_For_Slice; |
81a5b587 AC |
7733 | end if; |
7734 | ||
7735 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
7736 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
7737 | and then Parent (N) = Name (Parent (Parent (N)))) | |
70482933 | 7738 | then |
81a5b587 | 7739 | return; |
70482933 | 7740 | |
81a5b587 AC |
7741 | elsif Nkind (Parent (N)) = N_Indexed_Component |
7742 | or else Is_Renamed_Object (N) | |
7743 | or else Is_Procedure_Actual (N) | |
7744 | then | |
7745 | return; | |
70482933 | 7746 | |
91b1417d AC |
7747 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
7748 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 7749 | then |
81a5b587 AC |
7750 | return; |
7751 | ||
7752 | else | |
b01bf852 | 7753 | Make_Temporary_For_Slice; |
70482933 RK |
7754 | end if; |
7755 | end Expand_N_Slice; | |
7756 | ||
7757 | ------------------------------ | |
7758 | -- Expand_N_Type_Conversion -- | |
7759 | ------------------------------ | |
7760 | ||
7761 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
7762 | Loc : constant Source_Ptr := Sloc (N); | |
7763 | Operand : constant Node_Id := Expression (N); | |
7764 | Target_Type : constant Entity_Id := Etype (N); | |
7765 | Operand_Type : Entity_Id := Etype (Operand); | |
7766 | ||
7767 | procedure Handle_Changed_Representation; | |
685094bf RD |
7768 | -- This is called in the case of record and array type conversions to |
7769 | -- see if there is a change of representation to be handled. Change of | |
7770 | -- representation is actually handled at the assignment statement level, | |
7771 | -- and what this procedure does is rewrite node N conversion as an | |
7772 | -- assignment to temporary. If there is no change of representation, | |
7773 | -- then the conversion node is unchanged. | |
70482933 | 7774 | |
426908f8 RD |
7775 | procedure Raise_Accessibility_Error; |
7776 | -- Called when we know that an accessibility check will fail. Rewrites | |
7777 | -- node N to an appropriate raise statement and outputs warning msgs. | |
7778 | -- The Etype of the raise node is set to Target_Type. | |
7779 | ||
70482933 RK |
7780 | procedure Real_Range_Check; |
7781 | -- Handles generation of range check for real target value | |
7782 | ||
7783 | ----------------------------------- | |
7784 | -- Handle_Changed_Representation -- | |
7785 | ----------------------------------- | |
7786 | ||
7787 | procedure Handle_Changed_Representation is | |
7788 | Temp : Entity_Id; | |
7789 | Decl : Node_Id; | |
7790 | Odef : Node_Id; | |
7791 | Disc : Node_Id; | |
7792 | N_Ix : Node_Id; | |
7793 | Cons : List_Id; | |
7794 | ||
7795 | begin | |
f82944b7 | 7796 | -- Nothing else to do if no change of representation |
70482933 RK |
7797 | |
7798 | if Same_Representation (Operand_Type, Target_Type) then | |
7799 | return; | |
7800 | ||
7801 | -- The real change of representation work is done by the assignment | |
7802 | -- statement processing. So if this type conversion is appearing as | |
7803 | -- the expression of an assignment statement, nothing needs to be | |
7804 | -- done to the conversion. | |
7805 | ||
7806 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
7807 | return; | |
7808 | ||
7809 | -- Otherwise we need to generate a temporary variable, and do the | |
7810 | -- change of representation assignment into that temporary variable. | |
7811 | -- The conversion is then replaced by a reference to this variable. | |
7812 | ||
7813 | else | |
7814 | Cons := No_List; | |
7815 | ||
685094bf RD |
7816 | -- If type is unconstrained we have to add a constraint, copied |
7817 | -- from the actual value of the left hand side. | |
70482933 RK |
7818 | |
7819 | if not Is_Constrained (Target_Type) then | |
7820 | if Has_Discriminants (Operand_Type) then | |
7821 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
7822 | |
7823 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
7824 | Disc := First_Stored_Discriminant (Operand_Type); | |
7825 | end if; | |
7826 | ||
70482933 RK |
7827 | Cons := New_List; |
7828 | while Present (Disc) loop | |
7829 | Append_To (Cons, | |
7830 | Make_Selected_Component (Loc, | |
fbf5a39b | 7831 | Prefix => Duplicate_Subexpr_Move_Checks (Operand), |
70482933 RK |
7832 | Selector_Name => |
7833 | Make_Identifier (Loc, Chars (Disc)))); | |
7834 | Next_Discriminant (Disc); | |
7835 | end loop; | |
7836 | ||
7837 | elsif Is_Array_Type (Operand_Type) then | |
7838 | N_Ix := First_Index (Target_Type); | |
7839 | Cons := New_List; | |
7840 | ||
7841 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
7842 | ||
7843 | -- We convert the bounds explicitly. We use an unchecked | |
7844 | -- conversion because bounds checks are done elsewhere. | |
7845 | ||
7846 | Append_To (Cons, | |
7847 | Make_Range (Loc, | |
7848 | Low_Bound => | |
7849 | Unchecked_Convert_To (Etype (N_Ix), | |
7850 | Make_Attribute_Reference (Loc, | |
7851 | Prefix => | |
fbf5a39b | 7852 | Duplicate_Subexpr_No_Checks |
70482933 RK |
7853 | (Operand, Name_Req => True), |
7854 | Attribute_Name => Name_First, | |
7855 | Expressions => New_List ( | |
7856 | Make_Integer_Literal (Loc, J)))), | |
7857 | ||
7858 | High_Bound => | |
7859 | Unchecked_Convert_To (Etype (N_Ix), | |
7860 | Make_Attribute_Reference (Loc, | |
7861 | Prefix => | |
fbf5a39b | 7862 | Duplicate_Subexpr_No_Checks |
70482933 RK |
7863 | (Operand, Name_Req => True), |
7864 | Attribute_Name => Name_Last, | |
7865 | Expressions => New_List ( | |
7866 | Make_Integer_Literal (Loc, J)))))); | |
7867 | ||
7868 | Next_Index (N_Ix); | |
7869 | end loop; | |
7870 | end if; | |
7871 | end if; | |
7872 | ||
7873 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
7874 | ||
7875 | if Present (Cons) then | |
7876 | Odef := | |
7877 | Make_Subtype_Indication (Loc, | |
7878 | Subtype_Mark => Odef, | |
7879 | Constraint => | |
7880 | Make_Index_Or_Discriminant_Constraint (Loc, | |
7881 | Constraints => Cons)); | |
7882 | end if; | |
7883 | ||
191fcb3a | 7884 | Temp := Make_Temporary (Loc, 'C'); |
70482933 RK |
7885 | Decl := |
7886 | Make_Object_Declaration (Loc, | |
7887 | Defining_Identifier => Temp, | |
7888 | Object_Definition => Odef); | |
7889 | ||
7890 | Set_No_Initialization (Decl, True); | |
7891 | ||
7892 | -- Insert required actions. It is essential to suppress checks | |
7893 | -- since we have suppressed default initialization, which means | |
7894 | -- that the variable we create may have no discriminants. | |
7895 | ||
7896 | Insert_Actions (N, | |
7897 | New_List ( | |
7898 | Decl, | |
7899 | Make_Assignment_Statement (Loc, | |
7900 | Name => New_Occurrence_Of (Temp, Loc), | |
7901 | Expression => Relocate_Node (N))), | |
7902 | Suppress => All_Checks); | |
7903 | ||
7904 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
7905 | return; | |
7906 | end if; | |
7907 | end Handle_Changed_Representation; | |
7908 | ||
426908f8 RD |
7909 | ------------------------------- |
7910 | -- Raise_Accessibility_Error -- | |
7911 | ------------------------------- | |
7912 | ||
7913 | procedure Raise_Accessibility_Error is | |
7914 | begin | |
7915 | Rewrite (N, | |
7916 | Make_Raise_Program_Error (Sloc (N), | |
7917 | Reason => PE_Accessibility_Check_Failed)); | |
7918 | Set_Etype (N, Target_Type); | |
7919 | ||
7920 | Error_Msg_N ("?accessibility check failure", N); | |
7921 | Error_Msg_NE | |
7922 | ("\?& will be raised at run time", N, Standard_Program_Error); | |
7923 | end Raise_Accessibility_Error; | |
7924 | ||
70482933 RK |
7925 | ---------------------- |
7926 | -- Real_Range_Check -- | |
7927 | ---------------------- | |
7928 | ||
685094bf RD |
7929 | -- Case of conversions to floating-point or fixed-point. If range checks |
7930 | -- are enabled and the target type has a range constraint, we convert: | |
70482933 RK |
7931 | |
7932 | -- typ (x) | |
7933 | ||
7934 | -- to | |
7935 | ||
7936 | -- Tnn : typ'Base := typ'Base (x); | |
7937 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
7938 | -- Tnn | |
7939 | ||
685094bf RD |
7940 | -- This is necessary when there is a conversion of integer to float or |
7941 | -- to fixed-point to ensure that the correct checks are made. It is not | |
7942 | -- necessary for float to float where it is enough to simply set the | |
7943 | -- Do_Range_Check flag. | |
fbf5a39b | 7944 | |
70482933 RK |
7945 | procedure Real_Range_Check is |
7946 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
7947 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
7948 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 7949 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
7950 | Conv : Node_Id; |
7951 | Tnn : Entity_Id; | |
7952 | ||
7953 | begin | |
7954 | -- Nothing to do if conversion was rewritten | |
7955 | ||
7956 | if Nkind (N) /= N_Type_Conversion then | |
7957 | return; | |
7958 | end if; | |
7959 | ||
685094bf RD |
7960 | -- Nothing to do if range checks suppressed, or target has the same |
7961 | -- range as the base type (or is the base type). | |
70482933 RK |
7962 | |
7963 | if Range_Checks_Suppressed (Target_Type) | |
7964 | or else (Lo = Type_Low_Bound (Btyp) | |
7965 | and then | |
7966 | Hi = Type_High_Bound (Btyp)) | |
7967 | then | |
7968 | return; | |
7969 | end if; | |
7970 | ||
685094bf RD |
7971 | -- Nothing to do if expression is an entity on which checks have been |
7972 | -- suppressed. | |
70482933 | 7973 | |
fbf5a39b AC |
7974 | if Is_Entity_Name (Operand) |
7975 | and then Range_Checks_Suppressed (Entity (Operand)) | |
7976 | then | |
7977 | return; | |
7978 | end if; | |
7979 | ||
685094bf RD |
7980 | -- Nothing to do if bounds are all static and we can tell that the |
7981 | -- expression is within the bounds of the target. Note that if the | |
7982 | -- operand is of an unconstrained floating-point type, then we do | |
7983 | -- not trust it to be in range (might be infinite) | |
fbf5a39b AC |
7984 | |
7985 | declare | |
f02b8bb8 RD |
7986 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); |
7987 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
fbf5a39b AC |
7988 | |
7989 | begin | |
7990 | if (not Is_Floating_Point_Type (Xtyp) | |
7991 | or else Is_Constrained (Xtyp)) | |
7992 | and then Compile_Time_Known_Value (S_Lo) | |
7993 | and then Compile_Time_Known_Value (S_Hi) | |
7994 | and then Compile_Time_Known_Value (Hi) | |
7995 | and then Compile_Time_Known_Value (Lo) | |
7996 | then | |
7997 | declare | |
7998 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
7999 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
8000 | S_Lov : Ureal; | |
8001 | S_Hiv : Ureal; | |
8002 | ||
8003 | begin | |
8004 | if Is_Real_Type (Xtyp) then | |
8005 | S_Lov := Expr_Value_R (S_Lo); | |
8006 | S_Hiv := Expr_Value_R (S_Hi); | |
8007 | else | |
8008 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
8009 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
8010 | end if; | |
8011 | ||
8012 | if D_Hiv > D_Lov | |
8013 | and then S_Lov >= D_Lov | |
8014 | and then S_Hiv <= D_Hiv | |
8015 | then | |
8016 | Set_Do_Range_Check (Operand, False); | |
8017 | return; | |
8018 | end if; | |
8019 | end; | |
8020 | end if; | |
8021 | end; | |
8022 | ||
8023 | -- For float to float conversions, we are done | |
8024 | ||
8025 | if Is_Floating_Point_Type (Xtyp) | |
8026 | and then | |
8027 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
8028 | then |
8029 | return; | |
8030 | end if; | |
8031 | ||
fbf5a39b | 8032 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
8033 | |
8034 | Conv := Relocate_Node (N); | |
eaa826f8 | 8035 | Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); |
70482933 RK |
8036 | Set_Etype (Conv, Btyp); |
8037 | ||
f02b8bb8 RD |
8038 | -- Enable overflow except for case of integer to float conversions, |
8039 | -- where it is never required, since we can never have overflow in | |
8040 | -- this case. | |
70482933 | 8041 | |
fbf5a39b AC |
8042 | if not Is_Integer_Type (Etype (Operand)) then |
8043 | Enable_Overflow_Check (Conv); | |
70482933 RK |
8044 | end if; |
8045 | ||
191fcb3a | 8046 | Tnn := Make_Temporary (Loc, 'T', Conv); |
70482933 RK |
8047 | |
8048 | Insert_Actions (N, New_List ( | |
8049 | Make_Object_Declaration (Loc, | |
8050 | Defining_Identifier => Tnn, | |
8051 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
8052 | Expression => Conv), | |
8053 | ||
8054 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
8055 | Condition => |
8056 | Make_Or_Else (Loc, | |
8057 | Left_Opnd => | |
8058 | Make_Op_Lt (Loc, | |
8059 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8060 | Right_Opnd => | |
8061 | Make_Attribute_Reference (Loc, | |
8062 | Attribute_Name => Name_First, | |
8063 | Prefix => | |
8064 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 8065 | |
07fc65c4 GB |
8066 | Right_Opnd => |
8067 | Make_Op_Gt (Loc, | |
8068 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
8069 | Right_Opnd => | |
8070 | Make_Attribute_Reference (Loc, | |
8071 | Attribute_Name => Name_Last, | |
8072 | Prefix => | |
8073 | New_Occurrence_Of (Target_Type, Loc)))), | |
8074 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
8075 | |
8076 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
8077 | Analyze_And_Resolve (N, Btyp); | |
8078 | end Real_Range_Check; | |
8079 | ||
8080 | -- Start of processing for Expand_N_Type_Conversion | |
8081 | ||
8082 | begin | |
685094bf | 8083 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 AC |
8084 | -- the conversion completely, it is useless, except that it may carry |
8085 | -- an Assignment_OK attribute, which must be propagated to the operand. | |
70482933 RK |
8086 | |
8087 | if Operand_Type = Target_Type then | |
7b00e31d AC |
8088 | if Assignment_OK (N) then |
8089 | Set_Assignment_OK (Operand); | |
8090 | end if; | |
8091 | ||
fbf5a39b | 8092 | Rewrite (N, Relocate_Node (Operand)); |
70482933 RK |
8093 | return; |
8094 | end if; | |
8095 | ||
685094bf RD |
8096 | -- Nothing to do if this is the second argument of read. This is a |
8097 | -- "backwards" conversion that will be handled by the specialized code | |
8098 | -- in attribute processing. | |
70482933 RK |
8099 | |
8100 | if Nkind (Parent (N)) = N_Attribute_Reference | |
8101 | and then Attribute_Name (Parent (N)) = Name_Read | |
8102 | and then Next (First (Expressions (Parent (N)))) = N | |
8103 | then | |
8104 | return; | |
8105 | end if; | |
8106 | ||
8107 | -- Here if we may need to expand conversion | |
8108 | ||
eaa826f8 RD |
8109 | -- If the operand of the type conversion is an arithmetic operation on |
8110 | -- signed integers, and the based type of the signed integer type in | |
8111 | -- question is smaller than Standard.Integer, we promote both of the | |
8112 | -- operands to type Integer. | |
8113 | ||
8114 | -- For example, if we have | |
8115 | ||
8116 | -- target-type (opnd1 + opnd2) | |
8117 | ||
8118 | -- and opnd1 and opnd2 are of type short integer, then we rewrite | |
8119 | -- this as: | |
8120 | ||
8121 | -- target-type (integer(opnd1) + integer(opnd2)) | |
8122 | ||
8123 | -- We do this because we are always allowed to compute in a larger type | |
8124 | -- if we do the right thing with the result, and in this case we are | |
8125 | -- going to do a conversion which will do an appropriate check to make | |
8126 | -- sure that things are in range of the target type in any case. This | |
8127 | -- avoids some unnecessary intermediate overflows. | |
8128 | ||
dfcfdc0a AC |
8129 | -- We might consider a similar transformation in the case where the |
8130 | -- target is a real type or a 64-bit integer type, and the operand | |
8131 | -- is an arithmetic operation using a 32-bit integer type. However, | |
8132 | -- we do not bother with this case, because it could cause significant | |
8133 | -- ineffiencies on 32-bit machines. On a 64-bit machine it would be | |
8134 | -- much cheaper, but we don't want different behavior on 32-bit and | |
8135 | -- 64-bit machines. Note that the exclusion of the 64-bit case also | |
8136 | -- handles the configurable run-time cases where 64-bit arithmetic | |
8137 | -- may simply be unavailable. | |
eaa826f8 RD |
8138 | |
8139 | -- Note: this circuit is partially redundant with respect to the circuit | |
8140 | -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in | |
8141 | -- the processing here. Also we still need the Checks circuit, since we | |
8142 | -- have to be sure not to generate junk overflow checks in the first | |
8143 | -- place, since it would be trick to remove them here! | |
8144 | ||
fdfcc663 | 8145 | if Integer_Promotion_Possible (N) then |
eaa826f8 | 8146 | |
fdfcc663 | 8147 | -- All conditions met, go ahead with transformation |
eaa826f8 | 8148 | |
fdfcc663 AC |
8149 | declare |
8150 | Opnd : Node_Id; | |
8151 | L, R : Node_Id; | |
dfcfdc0a | 8152 | |
fdfcc663 AC |
8153 | begin |
8154 | R := | |
8155 | Make_Type_Conversion (Loc, | |
8156 | Subtype_Mark => New_Reference_To (Standard_Integer, Loc), | |
8157 | Expression => Relocate_Node (Right_Opnd (Operand))); | |
eaa826f8 | 8158 | |
5f3f175d AC |
8159 | Opnd := New_Op_Node (Nkind (Operand), Loc); |
8160 | Set_Right_Opnd (Opnd, R); | |
eaa826f8 | 8161 | |
5f3f175d | 8162 | if Nkind (Operand) in N_Binary_Op then |
fdfcc663 | 8163 | L := |
eaa826f8 | 8164 | Make_Type_Conversion (Loc, |
dfcfdc0a | 8165 | Subtype_Mark => New_Reference_To (Standard_Integer, Loc), |
fdfcc663 AC |
8166 | Expression => Relocate_Node (Left_Opnd (Operand))); |
8167 | ||
5f3f175d AC |
8168 | Set_Left_Opnd (Opnd, L); |
8169 | end if; | |
eaa826f8 | 8170 | |
5f3f175d AC |
8171 | Rewrite (N, |
8172 | Make_Type_Conversion (Loc, | |
8173 | Subtype_Mark => Relocate_Node (Subtype_Mark (N)), | |
8174 | Expression => Opnd)); | |
dfcfdc0a | 8175 | |
5f3f175d AC |
8176 | Analyze_And_Resolve (N, Target_Type); |
8177 | return; | |
fdfcc663 AC |
8178 | end; |
8179 | end if; | |
eaa826f8 | 8180 | |
f82944b7 JM |
8181 | -- Do validity check if validity checking operands |
8182 | ||
8183 | if Validity_Checks_On | |
8184 | and then Validity_Check_Operands | |
8185 | then | |
8186 | Ensure_Valid (Operand); | |
8187 | end if; | |
8188 | ||
70482933 RK |
8189 | -- Special case of converting from non-standard boolean type |
8190 | ||
8191 | if Is_Boolean_Type (Operand_Type) | |
8192 | and then (Nonzero_Is_True (Operand_Type)) | |
8193 | then | |
8194 | Adjust_Condition (Operand); | |
8195 | Set_Etype (Operand, Standard_Boolean); | |
8196 | Operand_Type := Standard_Boolean; | |
8197 | end if; | |
8198 | ||
8199 | -- Case of converting to an access type | |
8200 | ||
8201 | if Is_Access_Type (Target_Type) then | |
8202 | ||
d766cee3 RD |
8203 | -- Apply an accessibility check when the conversion operand is an |
8204 | -- access parameter (or a renaming thereof), unless conversion was | |
e84e11ba GD |
8205 | -- expanded from an Unchecked_ or Unrestricted_Access attribute. |
8206 | -- Note that other checks may still need to be applied below (such | |
8207 | -- as tagged type checks). | |
70482933 RK |
8208 | |
8209 | if Is_Entity_Name (Operand) | |
d766cee3 RD |
8210 | and then |
8211 | (Is_Formal (Entity (Operand)) | |
8212 | or else | |
8213 | (Present (Renamed_Object (Entity (Operand))) | |
8214 | and then Is_Entity_Name (Renamed_Object (Entity (Operand))) | |
8215 | and then Is_Formal | |
8216 | (Entity (Renamed_Object (Entity (Operand)))))) | |
70482933 | 8217 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type |
d766cee3 RD |
8218 | and then (Nkind (Original_Node (N)) /= N_Attribute_Reference |
8219 | or else Attribute_Name (Original_Node (N)) = Name_Access) | |
70482933 | 8220 | then |
e84e11ba GD |
8221 | Apply_Accessibility_Check |
8222 | (Operand, Target_Type, Insert_Node => Operand); | |
70482933 | 8223 | |
e84e11ba | 8224 | -- If the level of the operand type is statically deeper than the |
685094bf RD |
8225 | -- level of the target type, then force Program_Error. Note that this |
8226 | -- can only occur for cases where the attribute is within the body of | |
8227 | -- an instantiation (otherwise the conversion will already have been | |
8228 | -- rejected as illegal). Note: warnings are issued by the analyzer | |
8229 | -- for the instance cases. | |
70482933 RK |
8230 | |
8231 | elsif In_Instance_Body | |
07fc65c4 GB |
8232 | and then Type_Access_Level (Operand_Type) > |
8233 | Type_Access_Level (Target_Type) | |
70482933 | 8234 | then |
426908f8 | 8235 | Raise_Accessibility_Error; |
70482933 | 8236 | |
685094bf RD |
8237 | -- When the operand is a selected access discriminant the check needs |
8238 | -- to be made against the level of the object denoted by the prefix | |
8239 | -- of the selected name. Force Program_Error for this case as well | |
8240 | -- (this accessibility violation can only happen if within the body | |
8241 | -- of an instantiation). | |
70482933 RK |
8242 | |
8243 | elsif In_Instance_Body | |
8244 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
8245 | and then Nkind (Operand) = N_Selected_Component | |
8246 | and then Object_Access_Level (Operand) > | |
8247 | Type_Access_Level (Target_Type) | |
8248 | then | |
426908f8 | 8249 | Raise_Accessibility_Error; |
950d217a | 8250 | return; |
70482933 RK |
8251 | end if; |
8252 | end if; | |
8253 | ||
8254 | -- Case of conversions of tagged types and access to tagged types | |
8255 | ||
685094bf RD |
8256 | -- When needed, that is to say when the expression is class-wide, Add |
8257 | -- runtime a tag check for (strict) downward conversion by using the | |
8258 | -- membership test, generating: | |
70482933 RK |
8259 | |
8260 | -- [constraint_error when Operand not in Target_Type'Class] | |
8261 | ||
8262 | -- or in the access type case | |
8263 | ||
8264 | -- [constraint_error | |
8265 | -- when Operand /= null | |
8266 | -- and then Operand.all not in | |
8267 | -- Designated_Type (Target_Type)'Class] | |
8268 | ||
8269 | if (Is_Access_Type (Target_Type) | |
8270 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
8271 | or else Is_Tagged_Type (Target_Type) | |
8272 | then | |
685094bf RD |
8273 | -- Do not do any expansion in the access type case if the parent is a |
8274 | -- renaming, since this is an error situation which will be caught by | |
8275 | -- Sem_Ch8, and the expansion can interfere with this error check. | |
70482933 | 8276 | |
e7e4d230 | 8277 | if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then |
70482933 RK |
8278 | return; |
8279 | end if; | |
8280 | ||
0669bebe | 8281 | -- Otherwise, proceed with processing tagged conversion |
70482933 | 8282 | |
e7e4d230 | 8283 | Tagged_Conversion : declare |
8cea7b64 HK |
8284 | Actual_Op_Typ : Entity_Id; |
8285 | Actual_Targ_Typ : Entity_Id; | |
8286 | Make_Conversion : Boolean := False; | |
8287 | Root_Op_Typ : Entity_Id; | |
70482933 | 8288 | |
8cea7b64 HK |
8289 | procedure Make_Tag_Check (Targ_Typ : Entity_Id); |
8290 | -- Create a membership check to test whether Operand is a member | |
8291 | -- of Targ_Typ. If the original Target_Type is an access, include | |
8292 | -- a test for null value. The check is inserted at N. | |
8293 | ||
8294 | -------------------- | |
8295 | -- Make_Tag_Check -- | |
8296 | -------------------- | |
8297 | ||
8298 | procedure Make_Tag_Check (Targ_Typ : Entity_Id) is | |
8299 | Cond : Node_Id; | |
8300 | ||
8301 | begin | |
8302 | -- Generate: | |
8303 | -- [Constraint_Error | |
8304 | -- when Operand /= null | |
8305 | -- and then Operand.all not in Targ_Typ] | |
8306 | ||
8307 | if Is_Access_Type (Target_Type) then | |
8308 | Cond := | |
8309 | Make_And_Then (Loc, | |
8310 | Left_Opnd => | |
8311 | Make_Op_Ne (Loc, | |
8312 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
8313 | Right_Opnd => Make_Null (Loc)), | |
8314 | ||
8315 | Right_Opnd => | |
8316 | Make_Not_In (Loc, | |
8317 | Left_Opnd => | |
8318 | Make_Explicit_Dereference (Loc, | |
8319 | Prefix => Duplicate_Subexpr_No_Checks (Operand)), | |
8320 | Right_Opnd => New_Reference_To (Targ_Typ, Loc))); | |
8321 | ||
8322 | -- Generate: | |
8323 | -- [Constraint_Error when Operand not in Targ_Typ] | |
8324 | ||
8325 | else | |
8326 | Cond := | |
8327 | Make_Not_In (Loc, | |
8328 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), | |
8329 | Right_Opnd => New_Reference_To (Targ_Typ, Loc)); | |
8330 | end if; | |
8331 | ||
8332 | Insert_Action (N, | |
8333 | Make_Raise_Constraint_Error (Loc, | |
8334 | Condition => Cond, | |
8335 | Reason => CE_Tag_Check_Failed)); | |
8336 | end Make_Tag_Check; | |
8337 | ||
e7e4d230 | 8338 | -- Start of processing for Tagged_Conversion |
70482933 RK |
8339 | |
8340 | begin | |
8341 | if Is_Access_Type (Target_Type) then | |
70482933 | 8342 | |
852dba80 AC |
8343 | -- Handle entities from the limited view |
8344 | ||
8345 | Actual_Op_Typ := | |
8346 | Available_View (Designated_Type (Operand_Type)); | |
8347 | Actual_Targ_Typ := | |
8348 | Available_View (Designated_Type (Target_Type)); | |
70482933 | 8349 | else |
8cea7b64 HK |
8350 | Actual_Op_Typ := Operand_Type; |
8351 | Actual_Targ_Typ := Target_Type; | |
70482933 RK |
8352 | end if; |
8353 | ||
8cea7b64 HK |
8354 | Root_Op_Typ := Root_Type (Actual_Op_Typ); |
8355 | ||
20b5d666 JM |
8356 | -- Ada 2005 (AI-251): Handle interface type conversion |
8357 | ||
8cea7b64 | 8358 | if Is_Interface (Actual_Op_Typ) then |
20b5d666 JM |
8359 | Expand_Interface_Conversion (N, Is_Static => False); |
8360 | return; | |
8361 | end if; | |
8362 | ||
8cea7b64 | 8363 | if not Tag_Checks_Suppressed (Actual_Targ_Typ) then |
70482933 | 8364 | |
8cea7b64 HK |
8365 | -- Create a runtime tag check for a downward class-wide type |
8366 | -- conversion. | |
70482933 | 8367 | |
8cea7b64 | 8368 | if Is_Class_Wide_Type (Actual_Op_Typ) |
852dba80 | 8369 | and then Actual_Op_Typ /= Actual_Targ_Typ |
8cea7b64 HK |
8370 | and then Root_Op_Typ /= Actual_Targ_Typ |
8371 | and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ) | |
8372 | then | |
8373 | Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ)); | |
8374 | Make_Conversion := True; | |
8375 | end if; | |
70482933 | 8376 | |
8cea7b64 HK |
8377 | -- AI05-0073: If the result subtype of the function is defined |
8378 | -- by an access_definition designating a specific tagged type | |
8379 | -- T, a check is made that the result value is null or the tag | |
8380 | -- of the object designated by the result value identifies T. | |
8381 | -- Constraint_Error is raised if this check fails. | |
70482933 | 8382 | |
8cea7b64 HK |
8383 | if Nkind (Parent (N)) = Sinfo.N_Return_Statement then |
8384 | declare | |
e886436a | 8385 | Func : Entity_Id; |
8cea7b64 HK |
8386 | Func_Typ : Entity_Id; |
8387 | ||
8388 | begin | |
e886436a | 8389 | -- Climb scope stack looking for the enclosing function |
8cea7b64 | 8390 | |
e886436a | 8391 | Func := Current_Scope; |
8cea7b64 HK |
8392 | while Present (Func) |
8393 | and then Ekind (Func) /= E_Function | |
8394 | loop | |
8395 | Func := Scope (Func); | |
8396 | end loop; | |
8397 | ||
8398 | -- The function's return subtype must be defined using | |
8399 | -- an access definition. | |
8400 | ||
8401 | if Nkind (Result_Definition (Parent (Func))) = | |
8402 | N_Access_Definition | |
8403 | then | |
8404 | Func_Typ := Directly_Designated_Type (Etype (Func)); | |
8405 | ||
8406 | -- The return subtype denotes a specific tagged type, | |
8407 | -- in other words, a non class-wide type. | |
8408 | ||
8409 | if Is_Tagged_Type (Func_Typ) | |
8410 | and then not Is_Class_Wide_Type (Func_Typ) | |
8411 | then | |
8412 | Make_Tag_Check (Actual_Targ_Typ); | |
8413 | Make_Conversion := True; | |
8414 | end if; | |
8415 | end if; | |
8416 | end; | |
70482933 RK |
8417 | end if; |
8418 | ||
8cea7b64 HK |
8419 | -- We have generated a tag check for either a class-wide type |
8420 | -- conversion or for AI05-0073. | |
70482933 | 8421 | |
8cea7b64 HK |
8422 | if Make_Conversion then |
8423 | declare | |
8424 | Conv : Node_Id; | |
8425 | begin | |
8426 | Conv := | |
8427 | Make_Unchecked_Type_Conversion (Loc, | |
8428 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
8429 | Expression => Relocate_Node (Expression (N))); | |
8430 | Rewrite (N, Conv); | |
8431 | Analyze_And_Resolve (N, Target_Type); | |
8432 | end; | |
8433 | end if; | |
70482933 | 8434 | end if; |
e7e4d230 | 8435 | end Tagged_Conversion; |
70482933 RK |
8436 | |
8437 | -- Case of other access type conversions | |
8438 | ||
8439 | elsif Is_Access_Type (Target_Type) then | |
8440 | Apply_Constraint_Check (Operand, Target_Type); | |
8441 | ||
8442 | -- Case of conversions from a fixed-point type | |
8443 | ||
685094bf RD |
8444 | -- These conversions require special expansion and processing, found in |
8445 | -- the Exp_Fixd package. We ignore cases where Conversion_OK is set, | |
8446 | -- since from a semantic point of view, these are simple integer | |
70482933 RK |
8447 | -- conversions, which do not need further processing. |
8448 | ||
8449 | elsif Is_Fixed_Point_Type (Operand_Type) | |
8450 | and then not Conversion_OK (N) | |
8451 | then | |
8452 | -- We should never see universal fixed at this case, since the | |
8453 | -- expansion of the constituent divide or multiply should have | |
8454 | -- eliminated the explicit mention of universal fixed. | |
8455 | ||
8456 | pragma Assert (Operand_Type /= Universal_Fixed); | |
8457 | ||
685094bf RD |
8458 | -- Check for special case of the conversion to universal real that |
8459 | -- occurs as a result of the use of a round attribute. In this case, | |
8460 | -- the real type for the conversion is taken from the target type of | |
8461 | -- the Round attribute and the result must be marked as rounded. | |
70482933 RK |
8462 | |
8463 | if Target_Type = Universal_Real | |
8464 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
8465 | and then Attribute_Name (Parent (N)) = Name_Round | |
8466 | then | |
8467 | Set_Rounded_Result (N); | |
8468 | Set_Etype (N, Etype (Parent (N))); | |
8469 | end if; | |
8470 | ||
8471 | -- Otherwise do correct fixed-conversion, but skip these if the | |
e7e4d230 AC |
8472 | -- Conversion_OK flag is set, because from a semantic point of view |
8473 | -- these are simple integer conversions needing no further processing | |
8474 | -- (the backend will simply treat them as integers). | |
70482933 RK |
8475 | |
8476 | if not Conversion_OK (N) then | |
8477 | if Is_Fixed_Point_Type (Etype (N)) then | |
8478 | Expand_Convert_Fixed_To_Fixed (N); | |
8479 | Real_Range_Check; | |
8480 | ||
8481 | elsif Is_Integer_Type (Etype (N)) then | |
8482 | Expand_Convert_Fixed_To_Integer (N); | |
8483 | ||
8484 | else | |
8485 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
8486 | Expand_Convert_Fixed_To_Float (N); | |
8487 | Real_Range_Check; | |
8488 | end if; | |
8489 | end if; | |
8490 | ||
8491 | -- Case of conversions to a fixed-point type | |
8492 | ||
685094bf RD |
8493 | -- These conversions require special expansion and processing, found in |
8494 | -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set, | |
8495 | -- since from a semantic point of view, these are simple integer | |
8496 | -- conversions, which do not need further processing. | |
70482933 RK |
8497 | |
8498 | elsif Is_Fixed_Point_Type (Target_Type) | |
8499 | and then not Conversion_OK (N) | |
8500 | then | |
8501 | if Is_Integer_Type (Operand_Type) then | |
8502 | Expand_Convert_Integer_To_Fixed (N); | |
8503 | Real_Range_Check; | |
8504 | else | |
8505 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
8506 | Expand_Convert_Float_To_Fixed (N); | |
8507 | Real_Range_Check; | |
8508 | end if; | |
8509 | ||
8510 | -- Case of float-to-integer conversions | |
8511 | ||
8512 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
8513 | -- since semantically the fixed-point target is treated as though it | |
8514 | -- were an integer in such cases. | |
8515 | ||
8516 | elsif Is_Floating_Point_Type (Operand_Type) | |
8517 | and then | |
8518 | (Is_Integer_Type (Target_Type) | |
8519 | or else | |
8520 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
8521 | then | |
70482933 RK |
8522 | -- One more check here, gcc is still not able to do conversions of |
8523 | -- this type with proper overflow checking, and so gigi is doing an | |
8524 | -- approximation of what is required by doing floating-point compares | |
8525 | -- with the end-point. But that can lose precision in some cases, and | |
f02b8bb8 | 8526 | -- give a wrong result. Converting the operand to Universal_Real is |
70482933 | 8527 | -- helpful, but still does not catch all cases with 64-bit integers |
e7e4d230 | 8528 | -- on targets with only 64-bit floats. |
0669bebe GB |
8529 | |
8530 | -- The above comment seems obsoleted by Apply_Float_Conversion_Check | |
8531 | -- Can this code be removed ??? | |
70482933 | 8532 | |
fbf5a39b AC |
8533 | if Do_Range_Check (Operand) then |
8534 | Rewrite (Operand, | |
70482933 RK |
8535 | Make_Type_Conversion (Loc, |
8536 | Subtype_Mark => | |
f02b8bb8 | 8537 | New_Occurrence_Of (Universal_Real, Loc), |
70482933 | 8538 | Expression => |
fbf5a39b | 8539 | Relocate_Node (Operand))); |
70482933 | 8540 | |
f02b8bb8 | 8541 | Set_Etype (Operand, Universal_Real); |
fbf5a39b AC |
8542 | Enable_Range_Check (Operand); |
8543 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
8544 | end if; |
8545 | ||
8546 | -- Case of array conversions | |
8547 | ||
685094bf RD |
8548 | -- Expansion of array conversions, add required length/range checks but |
8549 | -- only do this if there is no change of representation. For handling of | |
8550 | -- this case, see Handle_Changed_Representation. | |
70482933 RK |
8551 | |
8552 | elsif Is_Array_Type (Target_Type) then | |
8553 | ||
8554 | if Is_Constrained (Target_Type) then | |
8555 | Apply_Length_Check (Operand, Target_Type); | |
8556 | else | |
8557 | Apply_Range_Check (Operand, Target_Type); | |
8558 | end if; | |
8559 | ||
8560 | Handle_Changed_Representation; | |
8561 | ||
8562 | -- Case of conversions of discriminated types | |
8563 | ||
685094bf RD |
8564 | -- Add required discriminant checks if target is constrained. Again this |
8565 | -- change is skipped if we have a change of representation. | |
70482933 RK |
8566 | |
8567 | elsif Has_Discriminants (Target_Type) | |
8568 | and then Is_Constrained (Target_Type) | |
8569 | then | |
8570 | Apply_Discriminant_Check (Operand, Target_Type); | |
8571 | Handle_Changed_Representation; | |
8572 | ||
8573 | -- Case of all other record conversions. The only processing required | |
8574 | -- is to check for a change of representation requiring the special | |
8575 | -- assignment processing. | |
8576 | ||
8577 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
8578 | |
8579 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
685094bf RD |
8580 | -- a derived Unchecked_Union type to an unconstrained type that is |
8581 | -- not Unchecked_Union if the operand lacks inferable discriminants. | |
5d09245e AC |
8582 | |
8583 | if Is_Derived_Type (Operand_Type) | |
8584 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
8585 | and then not Is_Constrained (Target_Type) | |
8586 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
8587 | and then not Has_Inferable_Discriminants (Operand) | |
8588 | then | |
685094bf | 8589 | -- To prevent Gigi from generating illegal code, we generate a |
5d09245e AC |
8590 | -- Program_Error node, but we give it the target type of the |
8591 | -- conversion. | |
8592 | ||
8593 | declare | |
8594 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
8595 | Reason => PE_Unchecked_Union_Restriction); | |
8596 | ||
8597 | begin | |
8598 | Set_Etype (PE, Target_Type); | |
8599 | Rewrite (N, PE); | |
8600 | ||
8601 | end; | |
8602 | else | |
8603 | Handle_Changed_Representation; | |
8604 | end if; | |
70482933 RK |
8605 | |
8606 | -- Case of conversions of enumeration types | |
8607 | ||
8608 | elsif Is_Enumeration_Type (Target_Type) then | |
8609 | ||
8610 | -- Special processing is required if there is a change of | |
e7e4d230 | 8611 | -- representation (from enumeration representation clauses). |
70482933 RK |
8612 | |
8613 | if not Same_Representation (Target_Type, Operand_Type) then | |
8614 | ||
8615 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
8616 | ||
8617 | Rewrite (N, | |
8618 | Make_Attribute_Reference (Loc, | |
8619 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
8620 | Attribute_Name => Name_Val, | |
8621 | Expressions => New_List ( | |
8622 | Make_Attribute_Reference (Loc, | |
8623 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
8624 | Attribute_Name => Name_Pos, | |
8625 | Expressions => New_List (Operand))))); | |
8626 | ||
8627 | Analyze_And_Resolve (N, Target_Type); | |
8628 | end if; | |
8629 | ||
8630 | -- Case of conversions to floating-point | |
8631 | ||
8632 | elsif Is_Floating_Point_Type (Target_Type) then | |
8633 | Real_Range_Check; | |
70482933 RK |
8634 | end if; |
8635 | ||
685094bf | 8636 | -- At this stage, either the conversion node has been transformed into |
e7e4d230 AC |
8637 | -- some other equivalent expression, or left as a conversion that can be |
8638 | -- handled by Gigi, in the following cases: | |
70482933 RK |
8639 | |
8640 | -- Conversions with no change of representation or type | |
8641 | ||
685094bf RD |
8642 | -- Numeric conversions involving integer, floating- and fixed-point |
8643 | -- values. Fixed-point values are allowed only if Conversion_OK is | |
8644 | -- set, i.e. if the fixed-point values are to be treated as integers. | |
70482933 | 8645 | |
5e1c00fa RD |
8646 | -- No other conversions should be passed to Gigi |
8647 | ||
8648 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 8649 | |
685094bf RD |
8650 | -- The only remaining step is to generate a range check if we still have |
8651 | -- a type conversion at this stage and Do_Range_Check is set. For now we | |
8652 | -- do this only for conversions of discrete types. | |
fbf5a39b AC |
8653 | |
8654 | if Nkind (N) = N_Type_Conversion | |
8655 | and then Is_Discrete_Type (Etype (N)) | |
8656 | then | |
8657 | declare | |
8658 | Expr : constant Node_Id := Expression (N); | |
8659 | Ftyp : Entity_Id; | |
8660 | Ityp : Entity_Id; | |
8661 | ||
8662 | begin | |
8663 | if Do_Range_Check (Expr) | |
8664 | and then Is_Discrete_Type (Etype (Expr)) | |
8665 | then | |
8666 | Set_Do_Range_Check (Expr, False); | |
8667 | ||
685094bf RD |
8668 | -- Before we do a range check, we have to deal with treating a |
8669 | -- fixed-point operand as an integer. The way we do this is | |
8670 | -- simply to do an unchecked conversion to an appropriate | |
fbf5a39b AC |
8671 | -- integer type large enough to hold the result. |
8672 | ||
8673 | -- This code is not active yet, because we are only dealing | |
8674 | -- with discrete types so far ??? | |
8675 | ||
8676 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer | |
8677 | and then Treat_Fixed_As_Integer (Expr) | |
8678 | then | |
8679 | Ftyp := Base_Type (Etype (Expr)); | |
8680 | ||
8681 | if Esize (Ftyp) >= Esize (Standard_Integer) then | |
8682 | Ityp := Standard_Long_Long_Integer; | |
8683 | else | |
8684 | Ityp := Standard_Integer; | |
8685 | end if; | |
8686 | ||
8687 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); | |
8688 | end if; | |
8689 | ||
8690 | -- Reset overflow flag, since the range check will include | |
e7e4d230 | 8691 | -- dealing with possible overflow, and generate the check. If |
685094bf | 8692 | -- Address is either a source type or target type, suppress |
8a36a0cc AC |
8693 | -- range check to avoid typing anomalies when it is a visible |
8694 | -- integer type. | |
fbf5a39b AC |
8695 | |
8696 | Set_Do_Overflow_Check (N, False); | |
8a36a0cc AC |
8697 | if not Is_Descendent_Of_Address (Etype (Expr)) |
8698 | and then not Is_Descendent_Of_Address (Target_Type) | |
8699 | then | |
8700 | Generate_Range_Check | |
8701 | (Expr, Target_Type, CE_Range_Check_Failed); | |
8702 | end if; | |
fbf5a39b AC |
8703 | end if; |
8704 | end; | |
8705 | end if; | |
f02b8bb8 RD |
8706 | |
8707 | -- Final step, if the result is a type conversion involving Vax_Float | |
8708 | -- types, then it is subject for further special processing. | |
8709 | ||
8710 | if Nkind (N) = N_Type_Conversion | |
8711 | and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type)) | |
8712 | then | |
8713 | Expand_Vax_Conversion (N); | |
8714 | return; | |
8715 | end if; | |
70482933 RK |
8716 | end Expand_N_Type_Conversion; |
8717 | ||
8718 | ----------------------------------- | |
8719 | -- Expand_N_Unchecked_Expression -- | |
8720 | ----------------------------------- | |
8721 | ||
e7e4d230 | 8722 | -- Remove the unchecked expression node from the tree. Its job was simply |
70482933 RK |
8723 | -- to make sure that its constituent expression was handled with checks |
8724 | -- off, and now that that is done, we can remove it from the tree, and | |
e7e4d230 | 8725 | -- indeed must, since Gigi does not expect to see these nodes. |
70482933 RK |
8726 | |
8727 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
8728 | Exp : constant Node_Id := Expression (N); | |
70482933 | 8729 | begin |
e7e4d230 | 8730 | Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp)); |
70482933 RK |
8731 | Rewrite (N, Exp); |
8732 | end Expand_N_Unchecked_Expression; | |
8733 | ||
8734 | ---------------------------------------- | |
8735 | -- Expand_N_Unchecked_Type_Conversion -- | |
8736 | ---------------------------------------- | |
8737 | ||
685094bf RD |
8738 | -- If this cannot be handled by Gigi and we haven't already made a |
8739 | -- temporary for it, do it now. | |
70482933 RK |
8740 | |
8741 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
8742 | Target_Type : constant Entity_Id := Etype (N); | |
8743 | Operand : constant Node_Id := Expression (N); | |
8744 | Operand_Type : constant Entity_Id := Etype (Operand); | |
8745 | ||
8746 | begin | |
7b00e31d | 8747 | -- Nothing at all to do if conversion is to the identical type so remove |
76efd572 | 8748 | -- the conversion completely, it is useless, except that it may carry |
e7e4d230 | 8749 | -- an Assignment_OK indication which must be propagated to the operand. |
7b00e31d AC |
8750 | |
8751 | if Operand_Type = Target_Type then | |
13d923cc | 8752 | |
e7e4d230 AC |
8753 | -- Code duplicates Expand_N_Unchecked_Expression above, factor??? |
8754 | ||
7b00e31d AC |
8755 | if Assignment_OK (N) then |
8756 | Set_Assignment_OK (Operand); | |
8757 | end if; | |
8758 | ||
8759 | Rewrite (N, Relocate_Node (Operand)); | |
8760 | return; | |
8761 | end if; | |
8762 | ||
70482933 RK |
8763 | -- If we have a conversion of a compile time known value to a target |
8764 | -- type and the value is in range of the target type, then we can simply | |
8765 | -- replace the construct by an integer literal of the correct type. We | |
8766 | -- only apply this to integer types being converted. Possibly it may | |
8767 | -- apply in other cases, but it is too much trouble to worry about. | |
8768 | ||
8769 | -- Note that we do not do this transformation if the Kill_Range_Check | |
8770 | -- flag is set, since then the value may be outside the expected range. | |
8771 | -- This happens in the Normalize_Scalars case. | |
8772 | ||
20b5d666 JM |
8773 | -- We also skip this if either the target or operand type is biased |
8774 | -- because in this case, the unchecked conversion is supposed to | |
8775 | -- preserve the bit pattern, not the integer value. | |
8776 | ||
70482933 | 8777 | if Is_Integer_Type (Target_Type) |
20b5d666 | 8778 | and then not Has_Biased_Representation (Target_Type) |
70482933 | 8779 | and then Is_Integer_Type (Operand_Type) |
20b5d666 | 8780 | and then not Has_Biased_Representation (Operand_Type) |
70482933 RK |
8781 | and then Compile_Time_Known_Value (Operand) |
8782 | and then not Kill_Range_Check (N) | |
8783 | then | |
8784 | declare | |
8785 | Val : constant Uint := Expr_Value (Operand); | |
8786 | ||
8787 | begin | |
8788 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
8789 | and then | |
8790 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
8791 | and then | |
8792 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
8793 | and then | |
8794 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
8795 | then | |
8796 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc | 8797 | |
685094bf RD |
8798 | -- If Address is the target type, just set the type to avoid a |
8799 | -- spurious type error on the literal when Address is a visible | |
8800 | -- integer type. | |
8a36a0cc AC |
8801 | |
8802 | if Is_Descendent_Of_Address (Target_Type) then | |
8803 | Set_Etype (N, Target_Type); | |
8804 | else | |
8805 | Analyze_And_Resolve (N, Target_Type); | |
8806 | end if; | |
8807 | ||
70482933 RK |
8808 | return; |
8809 | end if; | |
8810 | end; | |
8811 | end if; | |
8812 | ||
8813 | -- Nothing to do if conversion is safe | |
8814 | ||
8815 | if Safe_Unchecked_Type_Conversion (N) then | |
8816 | return; | |
8817 | end if; | |
8818 | ||
8819 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
8820 | -- flag indicates ??? -- more comments needed here) | |
8821 | ||
8822 | if Assignment_OK (N) then | |
8823 | null; | |
8824 | else | |
8825 | Force_Evaluation (N); | |
8826 | end if; | |
8827 | end Expand_N_Unchecked_Type_Conversion; | |
8828 | ||
8829 | ---------------------------- | |
8830 | -- Expand_Record_Equality -- | |
8831 | ---------------------------- | |
8832 | ||
8833 | -- For non-variant records, Equality is expanded when needed into: | |
8834 | ||
8835 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
8836 | -- and then ... | |
8837 | -- and then Lhs.Discrn = Rhs.Discrn | |
8838 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
8839 | -- and then ... | |
8840 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
8841 | ||
8842 | -- The expression is folded by the back-end for adjacent fields. This | |
8843 | -- function is called for tagged record in only one occasion: for imple- | |
8844 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
8845 | -- otherwise the primitive "=" is used directly. | |
8846 | ||
8847 | function Expand_Record_Equality | |
8848 | (Nod : Node_Id; | |
8849 | Typ : Entity_Id; | |
8850 | Lhs : Node_Id; | |
8851 | Rhs : Node_Id; | |
2e071734 | 8852 | Bodies : List_Id) return Node_Id |
70482933 RK |
8853 | is |
8854 | Loc : constant Source_Ptr := Sloc (Nod); | |
8855 | ||
0ab80019 AC |
8856 | Result : Node_Id; |
8857 | C : Entity_Id; | |
8858 | ||
8859 | First_Time : Boolean := True; | |
8860 | ||
70482933 RK |
8861 | function Suitable_Element (C : Entity_Id) return Entity_Id; |
8862 | -- Return the first field to compare beginning with C, skipping the | |
0ab80019 AC |
8863 | -- inherited components. |
8864 | ||
8865 | ---------------------- | |
8866 | -- Suitable_Element -- | |
8867 | ---------------------- | |
70482933 RK |
8868 | |
8869 | function Suitable_Element (C : Entity_Id) return Entity_Id is | |
8870 | begin | |
8871 | if No (C) then | |
8872 | return Empty; | |
8873 | ||
8874 | elsif Ekind (C) /= E_Discriminant | |
8875 | and then Ekind (C) /= E_Component | |
8876 | then | |
8877 | return Suitable_Element (Next_Entity (C)); | |
8878 | ||
8879 | elsif Is_Tagged_Type (Typ) | |
8880 | and then C /= Original_Record_Component (C) | |
8881 | then | |
8882 | return Suitable_Element (Next_Entity (C)); | |
8883 | ||
8884 | elsif Chars (C) = Name_uController | |
8885 | or else Chars (C) = Name_uTag | |
8886 | then | |
8887 | return Suitable_Element (Next_Entity (C)); | |
8888 | ||
26bff3d9 JM |
8889 | elsif Is_Interface (Etype (C)) then |
8890 | return Suitable_Element (Next_Entity (C)); | |
8891 | ||
70482933 RK |
8892 | else |
8893 | return C; | |
8894 | end if; | |
8895 | end Suitable_Element; | |
8896 | ||
70482933 RK |
8897 | -- Start of processing for Expand_Record_Equality |
8898 | ||
8899 | begin | |
70482933 RK |
8900 | -- Generates the following code: (assuming that Typ has one Discr and |
8901 | -- component C2 is also a record) | |
8902 | ||
8903 | -- True | |
8904 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
8905 | -- and then Lhs.C1 = Rhs.C1 | |
8906 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
8907 | -- and then ... | |
8908 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
8909 | ||
8910 | Result := New_Reference_To (Standard_True, Loc); | |
8911 | C := Suitable_Element (First_Entity (Typ)); | |
70482933 | 8912 | while Present (C) loop |
70482933 RK |
8913 | declare |
8914 | New_Lhs : Node_Id; | |
8915 | New_Rhs : Node_Id; | |
8aceda64 | 8916 | Check : Node_Id; |
70482933 RK |
8917 | |
8918 | begin | |
8919 | if First_Time then | |
8920 | First_Time := False; | |
8921 | New_Lhs := Lhs; | |
8922 | New_Rhs := Rhs; | |
70482933 RK |
8923 | else |
8924 | New_Lhs := New_Copy_Tree (Lhs); | |
8925 | New_Rhs := New_Copy_Tree (Rhs); | |
8926 | end if; | |
8927 | ||
8aceda64 AC |
8928 | Check := |
8929 | Expand_Composite_Equality (Nod, Etype (C), | |
8930 | Lhs => | |
8931 | Make_Selected_Component (Loc, | |
8932 | Prefix => New_Lhs, | |
8933 | Selector_Name => New_Reference_To (C, Loc)), | |
8934 | Rhs => | |
8935 | Make_Selected_Component (Loc, | |
8936 | Prefix => New_Rhs, | |
8937 | Selector_Name => New_Reference_To (C, Loc)), | |
8938 | Bodies => Bodies); | |
8939 | ||
8940 | -- If some (sub)component is an unchecked_union, the whole | |
8941 | -- operation will raise program error. | |
8942 | ||
8943 | if Nkind (Check) = N_Raise_Program_Error then | |
8944 | Result := Check; | |
8945 | Set_Etype (Result, Standard_Boolean); | |
8946 | exit; | |
8947 | else | |
8948 | Result := | |
8949 | Make_And_Then (Loc, | |
8950 | Left_Opnd => Result, | |
8951 | Right_Opnd => Check); | |
8952 | end if; | |
70482933 RK |
8953 | end; |
8954 | ||
8955 | C := Suitable_Element (Next_Entity (C)); | |
8956 | end loop; | |
8957 | ||
8958 | return Result; | |
8959 | end Expand_Record_Equality; | |
8960 | ||
5875f8d6 AC |
8961 | ----------------------------------- |
8962 | -- Expand_Short_Circuit_Operator -- | |
8963 | ----------------------------------- | |
8964 | ||
955871d3 AC |
8965 | -- Deal with special expansion if actions are present for the right operand |
8966 | -- and deal with optimizing case of arguments being True or False. We also | |
8967 | -- deal with the special case of non-standard boolean values. | |
5875f8d6 AC |
8968 | |
8969 | procedure Expand_Short_Circuit_Operator (N : Node_Id) is | |
8970 | Loc : constant Source_Ptr := Sloc (N); | |
8971 | Typ : constant Entity_Id := Etype (N); | |
5875f8d6 AC |
8972 | Left : constant Node_Id := Left_Opnd (N); |
8973 | Right : constant Node_Id := Right_Opnd (N); | |
955871d3 | 8974 | LocR : constant Source_Ptr := Sloc (Right); |
5875f8d6 AC |
8975 | Actlist : List_Id; |
8976 | ||
8977 | Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else; | |
8978 | Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value); | |
8979 | -- If Left = Shortcut_Value then Right need not be evaluated | |
8980 | ||
25adc5fb AC |
8981 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id; |
8982 | -- For Opnd a boolean expression, return a Boolean expression equivalent | |
8983 | -- to Opnd /= Shortcut_Value. | |
8984 | ||
8985 | -------------------- | |
8986 | -- Make_Test_Expr -- | |
8987 | -------------------- | |
8988 | ||
8989 | function Make_Test_Expr (Opnd : Node_Id) return Node_Id is | |
8990 | begin | |
8991 | if Shortcut_Value then | |
8992 | return Make_Op_Not (Sloc (Opnd), Opnd); | |
8993 | else | |
8994 | return Opnd; | |
8995 | end if; | |
8996 | end Make_Test_Expr; | |
8997 | ||
8998 | Op_Var : Entity_Id; | |
8999 | -- Entity for a temporary variable holding the value of the operator, | |
9000 | -- used for expansion in the case where actions are present. | |
9001 | ||
9002 | -- Start of processing for Expand_Short_Circuit_Operator | |
5875f8d6 AC |
9003 | |
9004 | begin | |
9005 | -- Deal with non-standard booleans | |
9006 | ||
9007 | if Is_Boolean_Type (Typ) then | |
9008 | Adjust_Condition (Left); | |
9009 | Adjust_Condition (Right); | |
9010 | Set_Etype (N, Standard_Boolean); | |
9011 | end if; | |
9012 | ||
9013 | -- Check for cases where left argument is known to be True or False | |
9014 | ||
9015 | if Compile_Time_Known_Value (Left) then | |
25adc5fb AC |
9016 | |
9017 | -- Mark SCO for left condition as compile time known | |
9018 | ||
9019 | if Generate_SCO and then Comes_From_Source (Left) then | |
9020 | Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True); | |
9021 | end if; | |
9022 | ||
5875f8d6 AC |
9023 | -- Rewrite True AND THEN Right / False OR ELSE Right to Right. |
9024 | -- Any actions associated with Right will be executed unconditionally | |
9025 | -- and can thus be inserted into the tree unconditionally. | |
9026 | ||
9027 | if Expr_Value_E (Left) /= Shortcut_Ent then | |
9028 | if Present (Actions (N)) then | |
9029 | Insert_Actions (N, Actions (N)); | |
9030 | end if; | |
9031 | ||
9032 | Rewrite (N, Right); | |
9033 | ||
9034 | -- Rewrite False AND THEN Right / True OR ELSE Right to Left. | |
9035 | -- In this case we can forget the actions associated with Right, | |
9036 | -- since they will never be executed. | |
9037 | ||
9038 | else | |
9039 | Kill_Dead_Code (Right); | |
9040 | Kill_Dead_Code (Actions (N)); | |
9041 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
9042 | end if; | |
9043 | ||
9044 | Adjust_Result_Type (N, Typ); | |
9045 | return; | |
9046 | end if; | |
9047 | ||
955871d3 AC |
9048 | -- If Actions are present for the right operand, we have to do some |
9049 | -- special processing. We can't just let these actions filter back into | |
9050 | -- code preceding the short circuit (which is what would have happened | |
9051 | -- if we had not trapped them in the short-circuit form), since they | |
9052 | -- must only be executed if the right operand of the short circuit is | |
9053 | -- executed and not otherwise. | |
5875f8d6 | 9054 | |
955871d3 | 9055 | -- the temporary variable C. |
5875f8d6 | 9056 | |
955871d3 AC |
9057 | if Present (Actions (N)) then |
9058 | Actlist := Actions (N); | |
5875f8d6 | 9059 | |
955871d3 | 9060 | -- The old approach is to expand: |
5875f8d6 | 9061 | |
955871d3 | 9062 | -- left AND THEN right |
25adc5fb | 9063 | |
955871d3 | 9064 | -- into |
25adc5fb | 9065 | |
955871d3 AC |
9066 | -- C : Boolean := False; |
9067 | -- IF left THEN | |
9068 | -- Actions; | |
9069 | -- IF right THEN | |
9070 | -- C := True; | |
9071 | -- END IF; | |
9072 | -- END IF; | |
5875f8d6 | 9073 | |
955871d3 AC |
9074 | -- and finally rewrite the operator into a reference to C. Similarly |
9075 | -- for left OR ELSE right, with negated values. Note that this | |
9076 | -- rewrite causes some difficulties for coverage analysis because | |
9077 | -- of the introduction of the new variable C, which obscures the | |
9078 | -- structure of the test. | |
5875f8d6 | 9079 | |
9cbfc269 AC |
9080 | -- We use this "old approach" if use of N_Expression_With_Actions |
9081 | -- is False (see description in Opt of when this is or is not set). | |
5875f8d6 | 9082 | |
9cbfc269 | 9083 | if not Use_Expression_With_Actions then |
955871d3 | 9084 | Op_Var := Make_Temporary (Loc, 'C', Related_Node => N); |
5875f8d6 | 9085 | |
955871d3 AC |
9086 | Insert_Action (N, |
9087 | Make_Object_Declaration (Loc, | |
9088 | Defining_Identifier => | |
9089 | Op_Var, | |
9090 | Object_Definition => | |
9091 | New_Occurrence_Of (Standard_Boolean, Loc), | |
9092 | Expression => | |
9093 | New_Occurrence_Of (Shortcut_Ent, Loc))); | |
9094 | ||
9095 | Append_To (Actlist, | |
9096 | Make_Implicit_If_Statement (Right, | |
9097 | Condition => Make_Test_Expr (Right), | |
9098 | Then_Statements => New_List ( | |
9099 | Make_Assignment_Statement (LocR, | |
9100 | Name => New_Occurrence_Of (Op_Var, LocR), | |
9101 | Expression => | |
9102 | New_Occurrence_Of | |
9103 | (Boolean_Literals (not Shortcut_Value), LocR))))); | |
5875f8d6 | 9104 | |
955871d3 AC |
9105 | Insert_Action (N, |
9106 | Make_Implicit_If_Statement (Left, | |
9107 | Condition => Make_Test_Expr (Left), | |
9108 | Then_Statements => Actlist)); | |
9109 | ||
9110 | Rewrite (N, New_Occurrence_Of (Op_Var, Loc)); | |
9111 | Analyze_And_Resolve (N, Standard_Boolean); | |
9112 | ||
9113 | -- The new approach, activated for now by the use of debug flag | |
9114 | -- -gnatd.X is to use the new Expression_With_Actions node for the | |
9115 | -- right operand of the short-circuit form. This should solve the | |
9116 | -- traceability problems for coverage analysis. | |
9117 | ||
9118 | else | |
9119 | Rewrite (Right, | |
9120 | Make_Expression_With_Actions (LocR, | |
9121 | Expression => Relocate_Node (Right), | |
9122 | Actions => Actlist)); | |
48b351d9 | 9123 | Set_Actions (N, No_List); |
955871d3 AC |
9124 | Analyze_And_Resolve (Right, Standard_Boolean); |
9125 | end if; | |
9126 | ||
5875f8d6 AC |
9127 | Adjust_Result_Type (N, Typ); |
9128 | return; | |
9129 | end if; | |
9130 | ||
9131 | -- No actions present, check for cases of right argument True/False | |
9132 | ||
9133 | if Compile_Time_Known_Value (Right) then | |
25adc5fb AC |
9134 | |
9135 | -- Mark SCO for left condition as compile time known | |
9136 | ||
9137 | if Generate_SCO and then Comes_From_Source (Right) then | |
9138 | Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True); | |
9139 | end if; | |
9140 | ||
5875f8d6 AC |
9141 | -- Change (Left and then True), (Left or else False) to Left. |
9142 | -- Note that we know there are no actions associated with the right | |
9143 | -- operand, since we just checked for this case above. | |
9144 | ||
9145 | if Expr_Value_E (Right) /= Shortcut_Ent then | |
9146 | Rewrite (N, Left); | |
9147 | ||
9148 | -- Change (Left and then False), (Left or else True) to Right, | |
9149 | -- making sure to preserve any side effects associated with the Left | |
9150 | -- operand. | |
9151 | ||
9152 | else | |
9153 | Remove_Side_Effects (Left); | |
9154 | Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc)); | |
9155 | end if; | |
9156 | end if; | |
9157 | ||
9158 | Adjust_Result_Type (N, Typ); | |
9159 | end Expand_Short_Circuit_Operator; | |
9160 | ||
70482933 RK |
9161 | ------------------------------------- |
9162 | -- Fixup_Universal_Fixed_Operation -- | |
9163 | ------------------------------------- | |
9164 | ||
9165 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
9166 | Conv : constant Node_Id := Parent (N); | |
9167 | ||
9168 | begin | |
9169 | -- We must have a type conversion immediately above us | |
9170 | ||
9171 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
9172 | ||
9173 | -- Normally the type conversion gives our target type. The exception | |
9174 | -- occurs in the case of the Round attribute, where the conversion | |
9175 | -- will be to universal real, and our real type comes from the Round | |
9176 | -- attribute (as well as an indication that we must round the result) | |
9177 | ||
9178 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
9179 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
9180 | then | |
9181 | Set_Etype (N, Etype (Parent (Conv))); | |
9182 | Set_Rounded_Result (N); | |
9183 | ||
9184 | -- Normal case where type comes from conversion above us | |
9185 | ||
9186 | else | |
9187 | Set_Etype (N, Etype (Conv)); | |
9188 | end if; | |
9189 | end Fixup_Universal_Fixed_Operation; | |
9190 | ||
fbf5a39b AC |
9191 | ------------------------------ |
9192 | -- Get_Allocator_Final_List -- | |
9193 | ------------------------------ | |
9194 | ||
9195 | function Get_Allocator_Final_List | |
9196 | (N : Node_Id; | |
9197 | T : Entity_Id; | |
2e071734 | 9198 | PtrT : Entity_Id) return Entity_Id |
fbf5a39b AC |
9199 | is |
9200 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 9201 | |
0da2c8ac | 9202 | Owner : Entity_Id := PtrT; |
26bff3d9 | 9203 | -- The entity whose finalization list must be used to attach the |
0da2c8ac | 9204 | -- allocated object. |
fbf5a39b | 9205 | |
0da2c8ac | 9206 | begin |
fbf5a39b | 9207 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
26bff3d9 JM |
9208 | |
9209 | -- If the context is an access parameter, we need to create a | |
9210 | -- non-anonymous access type in order to have a usable final list, | |
9211 | -- because there is otherwise no pool to which the allocated object | |
9212 | -- can belong. We create both the type and the finalization chain | |
9213 | -- here, because freezing an internal type does not create such a | |
9214 | -- chain. The Final_Chain that is thus created is shared by the | |
9215 | -- access parameter. The access type is tested against the result | |
9216 | -- type of the function to exclude allocators whose type is an | |
8654a240 | 9217 | -- anonymous access result type. We freeze the type at once to |
9450205a ES |
9218 | -- ensure that it is properly decorated for the back-end, even |
9219 | -- if the context and current scope is a loop. | |
26bff3d9 | 9220 | |
0da2c8ac AC |
9221 | if Nkind (Associated_Node_For_Itype (PtrT)) |
9222 | in N_Subprogram_Specification | |
26bff3d9 JM |
9223 | and then |
9224 | PtrT /= | |
9225 | Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT))) | |
0da2c8ac | 9226 | then |
191fcb3a | 9227 | Owner := Make_Temporary (Loc, 'J'); |
0da2c8ac AC |
9228 | Insert_Action (N, |
9229 | Make_Full_Type_Declaration (Loc, | |
9230 | Defining_Identifier => Owner, | |
9231 | Type_Definition => | |
9232 | Make_Access_To_Object_Definition (Loc, | |
9233 | Subtype_Indication => | |
9234 | New_Occurrence_Of (T, Loc)))); | |
fbf5a39b | 9235 | |
9450205a | 9236 | Freeze_Before (N, Owner); |
0da2c8ac AC |
9237 | Build_Final_List (N, Owner); |
9238 | Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner)); | |
fbf5a39b | 9239 | |
26bff3d9 JM |
9240 | -- Ada 2005 (AI-318-02): If the context is a return object |
9241 | -- declaration, then the anonymous return subtype is defined to have | |
9242 | -- the same accessibility level as that of the function's result | |
9243 | -- subtype, which means that we want the scope where the function is | |
9244 | -- declared. | |
9245 | ||
9246 | elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration | |
9247 | and then Ekind (Scope (PtrT)) = E_Return_Statement | |
9248 | then | |
9249 | Owner := Scope (Return_Applies_To (Scope (PtrT))); | |
9250 | ||
e7e4d230 | 9251 | -- Case of an access discriminant, or (Ada 2005) of an anonymous |
26bff3d9 | 9252 | -- access component or anonymous access function result: find the |
d766cee3 RD |
9253 | -- final list associated with the scope of the type. (In the |
9254 | -- anonymous access component kind, a list controller will have | |
9255 | -- been allocated when freezing the record type, and PtrT has an | |
9256 | -- Associated_Final_Chain attribute designating it.) | |
0da2c8ac | 9257 | |
d766cee3 | 9258 | elsif No (Associated_Final_Chain (PtrT)) then |
0da2c8ac AC |
9259 | Owner := Scope (PtrT); |
9260 | end if; | |
fbf5a39b | 9261 | end if; |
0da2c8ac AC |
9262 | |
9263 | return Find_Final_List (Owner); | |
fbf5a39b AC |
9264 | end Get_Allocator_Final_List; |
9265 | ||
5d09245e AC |
9266 | --------------------------------- |
9267 | -- Has_Inferable_Discriminants -- | |
9268 | --------------------------------- | |
9269 | ||
9270 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
9271 | ||
9272 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
9273 | -- Determines whether the left-most prefix of a selected component is a | |
9274 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
9275 | ||
9276 | -------------------------------- | |
9277 | -- Prefix_Is_Formal_Parameter -- | |
9278 | -------------------------------- | |
9279 | ||
9280 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
9281 | Sel_Comp : Node_Id := N; | |
9282 | ||
9283 | begin | |
9284 | -- Move to the left-most prefix by climbing up the tree | |
9285 | ||
9286 | while Present (Parent (Sel_Comp)) | |
9287 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
9288 | loop | |
9289 | Sel_Comp := Parent (Sel_Comp); | |
9290 | end loop; | |
9291 | ||
9292 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
9293 | end Prefix_Is_Formal_Parameter; | |
9294 | ||
9295 | -- Start of processing for Has_Inferable_Discriminants | |
9296 | ||
9297 | begin | |
8fc789c8 | 9298 | -- For identifiers and indexed components, it is sufficient to have a |
5d09245e AC |
9299 | -- constrained Unchecked_Union nominal subtype. |
9300 | ||
303b4d58 | 9301 | if Nkind_In (N, N_Identifier, N_Indexed_Component) then |
5d09245e AC |
9302 | return Is_Unchecked_Union (Base_Type (Etype (N))) |
9303 | and then | |
9304 | Is_Constrained (Etype (N)); | |
9305 | ||
9306 | -- For selected components, the subtype of the selector must be a | |
9307 | -- constrained Unchecked_Union. If the component is subject to a | |
9308 | -- per-object constraint, then the enclosing object must have inferable | |
9309 | -- discriminants. | |
9310 | ||
9311 | elsif Nkind (N) = N_Selected_Component then | |
9312 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then | |
9313 | ||
9314 | -- A small hack. If we have a per-object constrained selected | |
9315 | -- component of a formal parameter, return True since we do not | |
9316 | -- know the actual parameter association yet. | |
9317 | ||
9318 | if Prefix_Is_Formal_Parameter (N) then | |
9319 | return True; | |
9320 | end if; | |
9321 | ||
9322 | -- Otherwise, check the enclosing object and the selector | |
9323 | ||
9324 | return Has_Inferable_Discriminants (Prefix (N)) | |
9325 | and then | |
9326 | Has_Inferable_Discriminants (Selector_Name (N)); | |
9327 | end if; | |
9328 | ||
9329 | -- The call to Has_Inferable_Discriminants will determine whether | |
9330 | -- the selector has a constrained Unchecked_Union nominal type. | |
9331 | ||
9332 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
9333 | ||
9334 | -- A qualified expression has inferable discriminants if its subtype | |
9335 | -- mark is a constrained Unchecked_Union subtype. | |
9336 | ||
9337 | elsif Nkind (N) = N_Qualified_Expression then | |
9338 | return Is_Unchecked_Union (Subtype_Mark (N)) | |
9339 | and then | |
9340 | Is_Constrained (Subtype_Mark (N)); | |
9341 | ||
9342 | end if; | |
9343 | ||
9344 | return False; | |
9345 | end Has_Inferable_Discriminants; | |
9346 | ||
70482933 RK |
9347 | ------------------------------- |
9348 | -- Insert_Dereference_Action -- | |
9349 | ------------------------------- | |
9350 | ||
9351 | procedure Insert_Dereference_Action (N : Node_Id) is | |
9352 | Loc : constant Source_Ptr := Sloc (N); | |
9353 | Typ : constant Entity_Id := Etype (N); | |
9354 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
0ab80019 | 9355 | Pnod : constant Node_Id := Parent (N); |
70482933 RK |
9356 | |
9357 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; | |
2e071734 AC |
9358 | -- Return true if type of P is derived from Checked_Pool; |
9359 | ||
9360 | ----------------------------- | |
9361 | -- Is_Checked_Storage_Pool -- | |
9362 | ----------------------------- | |
70482933 RK |
9363 | |
9364 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
9365 | T : Entity_Id; | |
9366 | ||
9367 | begin | |
9368 | if No (P) then | |
9369 | return False; | |
9370 | end if; | |
9371 | ||
9372 | T := Etype (P); | |
9373 | while T /= Etype (T) loop | |
9374 | if Is_RTE (T, RE_Checked_Pool) then | |
9375 | return True; | |
9376 | else | |
9377 | T := Etype (T); | |
9378 | end if; | |
9379 | end loop; | |
9380 | ||
9381 | return False; | |
9382 | end Is_Checked_Storage_Pool; | |
9383 | ||
9384 | -- Start of processing for Insert_Dereference_Action | |
9385 | ||
9386 | begin | |
e6f69614 AC |
9387 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
9388 | ||
0ab80019 AC |
9389 | if not (Is_Checked_Storage_Pool (Pool) |
9390 | and then Comes_From_Source (Original_Node (Pnod))) | |
e6f69614 | 9391 | then |
70482933 | 9392 | return; |
70482933 RK |
9393 | end if; |
9394 | ||
9395 | Insert_Action (N, | |
9396 | Make_Procedure_Call_Statement (Loc, | |
9397 | Name => New_Reference_To ( | |
9398 | Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), | |
9399 | ||
9400 | Parameter_Associations => New_List ( | |
9401 | ||
9402 | -- Pool | |
9403 | ||
9404 | New_Reference_To (Pool, Loc), | |
9405 | ||
685094bf RD |
9406 | -- Storage_Address. We use the attribute Pool_Address, which uses |
9407 | -- the pointer itself to find the address of the object, and which | |
9408 | -- handles unconstrained arrays properly by computing the address | |
9409 | -- of the template. i.e. the correct address of the corresponding | |
9410 | -- allocation. | |
70482933 RK |
9411 | |
9412 | Make_Attribute_Reference (Loc, | |
fbf5a39b AC |
9413 | Prefix => Duplicate_Subexpr_Move_Checks (N), |
9414 | Attribute_Name => Name_Pool_Address), | |
70482933 RK |
9415 | |
9416 | -- Size_In_Storage_Elements | |
9417 | ||
9418 | Make_Op_Divide (Loc, | |
9419 | Left_Opnd => | |
9420 | Make_Attribute_Reference (Loc, | |
9421 | Prefix => | |
fbf5a39b AC |
9422 | Make_Explicit_Dereference (Loc, |
9423 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
9424 | Attribute_Name => Name_Size), |
9425 | Right_Opnd => | |
9426 | Make_Integer_Literal (Loc, System_Storage_Unit)), | |
9427 | ||
9428 | -- Alignment | |
9429 | ||
9430 | Make_Attribute_Reference (Loc, | |
9431 | Prefix => | |
fbf5a39b AC |
9432 | Make_Explicit_Dereference (Loc, |
9433 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
9434 | Attribute_Name => Name_Alignment)))); |
9435 | ||
fbf5a39b AC |
9436 | exception |
9437 | when RE_Not_Available => | |
9438 | return; | |
70482933 RK |
9439 | end Insert_Dereference_Action; |
9440 | ||
fdfcc663 AC |
9441 | -------------------------------- |
9442 | -- Integer_Promotion_Possible -- | |
9443 | -------------------------------- | |
9444 | ||
9445 | function Integer_Promotion_Possible (N : Node_Id) return Boolean is | |
9446 | Operand : constant Node_Id := Expression (N); | |
9447 | Operand_Type : constant Entity_Id := Etype (Operand); | |
9448 | Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type); | |
9449 | ||
9450 | begin | |
9451 | pragma Assert (Nkind (N) = N_Type_Conversion); | |
9452 | ||
9453 | return | |
9454 | ||
9455 | -- We only do the transformation for source constructs. We assume | |
9456 | -- that the expander knows what it is doing when it generates code. | |
9457 | ||
9458 | Comes_From_Source (N) | |
9459 | ||
9460 | -- If the operand type is Short_Integer or Short_Short_Integer, | |
9461 | -- then we will promote to Integer, which is available on all | |
9462 | -- targets, and is sufficient to ensure no intermediate overflow. | |
9463 | -- Furthermore it is likely to be as efficient or more efficient | |
9464 | -- than using the smaller type for the computation so we do this | |
9465 | -- unconditionally. | |
9466 | ||
9467 | and then | |
9468 | (Root_Operand_Type = Base_Type (Standard_Short_Integer) | |
9469 | or else | |
9470 | Root_Operand_Type = Base_Type (Standard_Short_Short_Integer)) | |
9471 | ||
9472 | -- Test for interesting operation, which includes addition, | |
5f3f175d AC |
9473 | -- division, exponentiation, multiplication, subtraction, absolute |
9474 | -- value and unary negation. Unary "+" is omitted since it is a | |
9475 | -- no-op and thus can't overflow. | |
fdfcc663 | 9476 | |
5f3f175d AC |
9477 | and then Nkind_In (Operand, N_Op_Abs, |
9478 | N_Op_Add, | |
fdfcc663 AC |
9479 | N_Op_Divide, |
9480 | N_Op_Expon, | |
9481 | N_Op_Minus, | |
9482 | N_Op_Multiply, | |
9483 | N_Op_Subtract); | |
9484 | end Integer_Promotion_Possible; | |
9485 | ||
70482933 RK |
9486 | ------------------------------ |
9487 | -- Make_Array_Comparison_Op -- | |
9488 | ------------------------------ | |
9489 | ||
9490 | -- This is a hand-coded expansion of the following generic function: | |
9491 | ||
9492 | -- generic | |
9493 | -- type elem is (<>); | |
9494 | -- type index is (<>); | |
9495 | -- type a is array (index range <>) of elem; | |
20b5d666 | 9496 | |
70482933 RK |
9497 | -- function Gnnn (X : a; Y: a) return boolean is |
9498 | -- J : index := Y'first; | |
20b5d666 | 9499 | |
70482933 RK |
9500 | -- begin |
9501 | -- if X'length = 0 then | |
9502 | -- return false; | |
20b5d666 | 9503 | |
70482933 RK |
9504 | -- elsif Y'length = 0 then |
9505 | -- return true; | |
20b5d666 | 9506 | |
70482933 RK |
9507 | -- else |
9508 | -- for I in X'range loop | |
9509 | -- if X (I) = Y (J) then | |
9510 | -- if J = Y'last then | |
9511 | -- exit; | |
9512 | -- else | |
9513 | -- J := index'succ (J); | |
9514 | -- end if; | |
20b5d666 | 9515 | |
70482933 RK |
9516 | -- else |
9517 | -- return X (I) > Y (J); | |
9518 | -- end if; | |
9519 | -- end loop; | |
20b5d666 | 9520 | |
70482933 RK |
9521 | -- return X'length > Y'length; |
9522 | -- end if; | |
9523 | -- end Gnnn; | |
9524 | ||
9525 | -- Note that since we are essentially doing this expansion by hand, we | |
9526 | -- do not need to generate an actual or formal generic part, just the | |
9527 | -- instantiated function itself. | |
9528 | ||
9529 | function Make_Array_Comparison_Op | |
2e071734 AC |
9530 | (Typ : Entity_Id; |
9531 | Nod : Node_Id) return Node_Id | |
70482933 RK |
9532 | is |
9533 | Loc : constant Source_Ptr := Sloc (Nod); | |
9534 | ||
9535 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
9536 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
9537 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
9538 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
9539 | ||
9540 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
9541 | ||
9542 | Loop_Statement : Node_Id; | |
9543 | Loop_Body : Node_Id; | |
9544 | If_Stat : Node_Id; | |
9545 | Inner_If : Node_Id; | |
9546 | Final_Expr : Node_Id; | |
9547 | Func_Body : Node_Id; | |
9548 | Func_Name : Entity_Id; | |
9549 | Formals : List_Id; | |
9550 | Length1 : Node_Id; | |
9551 | Length2 : Node_Id; | |
9552 | ||
9553 | begin | |
9554 | -- if J = Y'last then | |
9555 | -- exit; | |
9556 | -- else | |
9557 | -- J := index'succ (J); | |
9558 | -- end if; | |
9559 | ||
9560 | Inner_If := | |
9561 | Make_Implicit_If_Statement (Nod, | |
9562 | Condition => | |
9563 | Make_Op_Eq (Loc, | |
9564 | Left_Opnd => New_Reference_To (J, Loc), | |
9565 | Right_Opnd => | |
9566 | Make_Attribute_Reference (Loc, | |
9567 | Prefix => New_Reference_To (Y, Loc), | |
9568 | Attribute_Name => Name_Last)), | |
9569 | ||
9570 | Then_Statements => New_List ( | |
9571 | Make_Exit_Statement (Loc)), | |
9572 | ||
9573 | Else_Statements => | |
9574 | New_List ( | |
9575 | Make_Assignment_Statement (Loc, | |
9576 | Name => New_Reference_To (J, Loc), | |
9577 | Expression => | |
9578 | Make_Attribute_Reference (Loc, | |
9579 | Prefix => New_Reference_To (Index, Loc), | |
9580 | Attribute_Name => Name_Succ, | |
9581 | Expressions => New_List (New_Reference_To (J, Loc)))))); | |
9582 | ||
9583 | -- if X (I) = Y (J) then | |
9584 | -- if ... end if; | |
9585 | -- else | |
9586 | -- return X (I) > Y (J); | |
9587 | -- end if; | |
9588 | ||
9589 | Loop_Body := | |
9590 | Make_Implicit_If_Statement (Nod, | |
9591 | Condition => | |
9592 | Make_Op_Eq (Loc, | |
9593 | Left_Opnd => | |
9594 | Make_Indexed_Component (Loc, | |
9595 | Prefix => New_Reference_To (X, Loc), | |
9596 | Expressions => New_List (New_Reference_To (I, Loc))), | |
9597 | ||
9598 | Right_Opnd => | |
9599 | Make_Indexed_Component (Loc, | |
9600 | Prefix => New_Reference_To (Y, Loc), | |
9601 | Expressions => New_List (New_Reference_To (J, Loc)))), | |
9602 | ||
9603 | Then_Statements => New_List (Inner_If), | |
9604 | ||
9605 | Else_Statements => New_List ( | |
d766cee3 | 9606 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9607 | Expression => |
9608 | Make_Op_Gt (Loc, | |
9609 | Left_Opnd => | |
9610 | Make_Indexed_Component (Loc, | |
9611 | Prefix => New_Reference_To (X, Loc), | |
9612 | Expressions => New_List (New_Reference_To (I, Loc))), | |
9613 | ||
9614 | Right_Opnd => | |
9615 | Make_Indexed_Component (Loc, | |
9616 | Prefix => New_Reference_To (Y, Loc), | |
9617 | Expressions => New_List ( | |
9618 | New_Reference_To (J, Loc))))))); | |
9619 | ||
9620 | -- for I in X'range loop | |
9621 | -- if ... end if; | |
9622 | -- end loop; | |
9623 | ||
9624 | Loop_Statement := | |
9625 | Make_Implicit_Loop_Statement (Nod, | |
9626 | Identifier => Empty, | |
9627 | ||
9628 | Iteration_Scheme => | |
9629 | Make_Iteration_Scheme (Loc, | |
9630 | Loop_Parameter_Specification => | |
9631 | Make_Loop_Parameter_Specification (Loc, | |
9632 | Defining_Identifier => I, | |
9633 | Discrete_Subtype_Definition => | |
9634 | Make_Attribute_Reference (Loc, | |
9635 | Prefix => New_Reference_To (X, Loc), | |
9636 | Attribute_Name => Name_Range))), | |
9637 | ||
9638 | Statements => New_List (Loop_Body)); | |
9639 | ||
9640 | -- if X'length = 0 then | |
9641 | -- return false; | |
9642 | -- elsif Y'length = 0 then | |
9643 | -- return true; | |
9644 | -- else | |
9645 | -- for ... loop ... end loop; | |
9646 | -- return X'length > Y'length; | |
9647 | -- end if; | |
9648 | ||
9649 | Length1 := | |
9650 | Make_Attribute_Reference (Loc, | |
9651 | Prefix => New_Reference_To (X, Loc), | |
9652 | Attribute_Name => Name_Length); | |
9653 | ||
9654 | Length2 := | |
9655 | Make_Attribute_Reference (Loc, | |
9656 | Prefix => New_Reference_To (Y, Loc), | |
9657 | Attribute_Name => Name_Length); | |
9658 | ||
9659 | Final_Expr := | |
9660 | Make_Op_Gt (Loc, | |
9661 | Left_Opnd => Length1, | |
9662 | Right_Opnd => Length2); | |
9663 | ||
9664 | If_Stat := | |
9665 | Make_Implicit_If_Statement (Nod, | |
9666 | Condition => | |
9667 | Make_Op_Eq (Loc, | |
9668 | Left_Opnd => | |
9669 | Make_Attribute_Reference (Loc, | |
9670 | Prefix => New_Reference_To (X, Loc), | |
9671 | Attribute_Name => Name_Length), | |
9672 | Right_Opnd => | |
9673 | Make_Integer_Literal (Loc, 0)), | |
9674 | ||
9675 | Then_Statements => | |
9676 | New_List ( | |
d766cee3 | 9677 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9678 | Expression => New_Reference_To (Standard_False, Loc))), |
9679 | ||
9680 | Elsif_Parts => New_List ( | |
9681 | Make_Elsif_Part (Loc, | |
9682 | Condition => | |
9683 | Make_Op_Eq (Loc, | |
9684 | Left_Opnd => | |
9685 | Make_Attribute_Reference (Loc, | |
9686 | Prefix => New_Reference_To (Y, Loc), | |
9687 | Attribute_Name => Name_Length), | |
9688 | Right_Opnd => | |
9689 | Make_Integer_Literal (Loc, 0)), | |
9690 | ||
9691 | Then_Statements => | |
9692 | New_List ( | |
d766cee3 | 9693 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9694 | Expression => New_Reference_To (Standard_True, Loc))))), |
9695 | ||
9696 | Else_Statements => New_List ( | |
9697 | Loop_Statement, | |
d766cee3 | 9698 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9699 | Expression => Final_Expr))); |
9700 | ||
9701 | -- (X : a; Y: a) | |
9702 | ||
9703 | Formals := New_List ( | |
9704 | Make_Parameter_Specification (Loc, | |
9705 | Defining_Identifier => X, | |
9706 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
9707 | ||
9708 | Make_Parameter_Specification (Loc, | |
9709 | Defining_Identifier => Y, | |
9710 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
9711 | ||
9712 | -- function Gnnn (...) return boolean is | |
9713 | -- J : index := Y'first; | |
9714 | -- begin | |
9715 | -- if ... end if; | |
9716 | -- end Gnnn; | |
9717 | ||
191fcb3a | 9718 | Func_Name := Make_Temporary (Loc, 'G'); |
70482933 RK |
9719 | |
9720 | Func_Body := | |
9721 | Make_Subprogram_Body (Loc, | |
9722 | Specification => | |
9723 | Make_Function_Specification (Loc, | |
9724 | Defining_Unit_Name => Func_Name, | |
9725 | Parameter_Specifications => Formals, | |
630d30e9 | 9726 | Result_Definition => New_Reference_To (Standard_Boolean, Loc)), |
70482933 RK |
9727 | |
9728 | Declarations => New_List ( | |
9729 | Make_Object_Declaration (Loc, | |
9730 | Defining_Identifier => J, | |
9731 | Object_Definition => New_Reference_To (Index, Loc), | |
9732 | Expression => | |
9733 | Make_Attribute_Reference (Loc, | |
9734 | Prefix => New_Reference_To (Y, Loc), | |
9735 | Attribute_Name => Name_First))), | |
9736 | ||
9737 | Handled_Statement_Sequence => | |
9738 | Make_Handled_Sequence_Of_Statements (Loc, | |
9739 | Statements => New_List (If_Stat))); | |
9740 | ||
9741 | return Func_Body; | |
70482933 RK |
9742 | end Make_Array_Comparison_Op; |
9743 | ||
9744 | --------------------------- | |
9745 | -- Make_Boolean_Array_Op -- | |
9746 | --------------------------- | |
9747 | ||
685094bf RD |
9748 | -- For logical operations on boolean arrays, expand in line the following, |
9749 | -- replacing 'and' with 'or' or 'xor' where needed: | |
70482933 RK |
9750 | |
9751 | -- function Annn (A : typ; B: typ) return typ is | |
9752 | -- C : typ; | |
9753 | -- begin | |
9754 | -- for J in A'range loop | |
9755 | -- C (J) := A (J) op B (J); | |
9756 | -- end loop; | |
9757 | -- return C; | |
9758 | -- end Annn; | |
9759 | ||
9760 | -- Here typ is the boolean array type | |
9761 | ||
9762 | function Make_Boolean_Array_Op | |
2e071734 AC |
9763 | (Typ : Entity_Id; |
9764 | N : Node_Id) return Node_Id | |
70482933 RK |
9765 | is |
9766 | Loc : constant Source_Ptr := Sloc (N); | |
9767 | ||
9768 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
9769 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
9770 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
9771 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
9772 | ||
9773 | A_J : Node_Id; | |
9774 | B_J : Node_Id; | |
9775 | C_J : Node_Id; | |
9776 | Op : Node_Id; | |
9777 | ||
9778 | Formals : List_Id; | |
9779 | Func_Name : Entity_Id; | |
9780 | Func_Body : Node_Id; | |
9781 | Loop_Statement : Node_Id; | |
9782 | ||
9783 | begin | |
9784 | A_J := | |
9785 | Make_Indexed_Component (Loc, | |
9786 | Prefix => New_Reference_To (A, Loc), | |
9787 | Expressions => New_List (New_Reference_To (J, Loc))); | |
9788 | ||
9789 | B_J := | |
9790 | Make_Indexed_Component (Loc, | |
9791 | Prefix => New_Reference_To (B, Loc), | |
9792 | Expressions => New_List (New_Reference_To (J, Loc))); | |
9793 | ||
9794 | C_J := | |
9795 | Make_Indexed_Component (Loc, | |
9796 | Prefix => New_Reference_To (C, Loc), | |
9797 | Expressions => New_List (New_Reference_To (J, Loc))); | |
9798 | ||
9799 | if Nkind (N) = N_Op_And then | |
9800 | Op := | |
9801 | Make_Op_And (Loc, | |
9802 | Left_Opnd => A_J, | |
9803 | Right_Opnd => B_J); | |
9804 | ||
9805 | elsif Nkind (N) = N_Op_Or then | |
9806 | Op := | |
9807 | Make_Op_Or (Loc, | |
9808 | Left_Opnd => A_J, | |
9809 | Right_Opnd => B_J); | |
9810 | ||
9811 | else | |
9812 | Op := | |
9813 | Make_Op_Xor (Loc, | |
9814 | Left_Opnd => A_J, | |
9815 | Right_Opnd => B_J); | |
9816 | end if; | |
9817 | ||
9818 | Loop_Statement := | |
9819 | Make_Implicit_Loop_Statement (N, | |
9820 | Identifier => Empty, | |
9821 | ||
9822 | Iteration_Scheme => | |
9823 | Make_Iteration_Scheme (Loc, | |
9824 | Loop_Parameter_Specification => | |
9825 | Make_Loop_Parameter_Specification (Loc, | |
9826 | Defining_Identifier => J, | |
9827 | Discrete_Subtype_Definition => | |
9828 | Make_Attribute_Reference (Loc, | |
9829 | Prefix => New_Reference_To (A, Loc), | |
9830 | Attribute_Name => Name_Range))), | |
9831 | ||
9832 | Statements => New_List ( | |
9833 | Make_Assignment_Statement (Loc, | |
9834 | Name => C_J, | |
9835 | Expression => Op))); | |
9836 | ||
9837 | Formals := New_List ( | |
9838 | Make_Parameter_Specification (Loc, | |
9839 | Defining_Identifier => A, | |
9840 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
9841 | ||
9842 | Make_Parameter_Specification (Loc, | |
9843 | Defining_Identifier => B, | |
9844 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
9845 | ||
191fcb3a | 9846 | Func_Name := Make_Temporary (Loc, 'A'); |
70482933 RK |
9847 | Set_Is_Inlined (Func_Name); |
9848 | ||
9849 | Func_Body := | |
9850 | Make_Subprogram_Body (Loc, | |
9851 | Specification => | |
9852 | Make_Function_Specification (Loc, | |
9853 | Defining_Unit_Name => Func_Name, | |
9854 | Parameter_Specifications => Formals, | |
630d30e9 | 9855 | Result_Definition => New_Reference_To (Typ, Loc)), |
70482933 RK |
9856 | |
9857 | Declarations => New_List ( | |
9858 | Make_Object_Declaration (Loc, | |
9859 | Defining_Identifier => C, | |
9860 | Object_Definition => New_Reference_To (Typ, Loc))), | |
9861 | ||
9862 | Handled_Statement_Sequence => | |
9863 | Make_Handled_Sequence_Of_Statements (Loc, | |
9864 | Statements => New_List ( | |
9865 | Loop_Statement, | |
d766cee3 | 9866 | Make_Simple_Return_Statement (Loc, |
70482933 RK |
9867 | Expression => New_Reference_To (C, Loc))))); |
9868 | ||
9869 | return Func_Body; | |
9870 | end Make_Boolean_Array_Op; | |
9871 | ||
9872 | ------------------------ | |
9873 | -- Rewrite_Comparison -- | |
9874 | ------------------------ | |
9875 | ||
9876 | procedure Rewrite_Comparison (N : Node_Id) is | |
c800f862 RD |
9877 | Warning_Generated : Boolean := False; |
9878 | -- Set to True if first pass with Assume_Valid generates a warning in | |
9879 | -- which case we skip the second pass to avoid warning overloaded. | |
9880 | ||
9881 | Result : Node_Id; | |
9882 | -- Set to Standard_True or Standard_False | |
9883 | ||
d26dc4b5 AC |
9884 | begin |
9885 | if Nkind (N) = N_Type_Conversion then | |
9886 | Rewrite_Comparison (Expression (N)); | |
20b5d666 | 9887 | return; |
70482933 | 9888 | |
d26dc4b5 | 9889 | elsif Nkind (N) not in N_Op_Compare then |
20b5d666 JM |
9890 | return; |
9891 | end if; | |
70482933 | 9892 | |
c800f862 RD |
9893 | -- Now start looking at the comparison in detail. We potentially go |
9894 | -- through this loop twice. The first time, Assume_Valid is set False | |
9895 | -- in the call to Compile_Time_Compare. If this call results in a | |
9896 | -- clear result of always True or Always False, that's decisive and | |
9897 | -- we are done. Otherwise we repeat the processing with Assume_Valid | |
e7e4d230 | 9898 | -- set to True to generate additional warnings. We can skip that step |
c800f862 RD |
9899 | -- if Constant_Condition_Warnings is False. |
9900 | ||
9901 | for AV in False .. True loop | |
9902 | declare | |
9903 | Typ : constant Entity_Id := Etype (N); | |
9904 | Op1 : constant Node_Id := Left_Opnd (N); | |
9905 | Op2 : constant Node_Id := Right_Opnd (N); | |
70482933 | 9906 | |
c800f862 RD |
9907 | Res : constant Compare_Result := |
9908 | Compile_Time_Compare (Op1, Op2, Assume_Valid => AV); | |
9909 | -- Res indicates if compare outcome can be compile time determined | |
f02b8bb8 | 9910 | |
c800f862 RD |
9911 | True_Result : Boolean; |
9912 | False_Result : Boolean; | |
f02b8bb8 | 9913 | |
c800f862 RD |
9914 | begin |
9915 | case N_Op_Compare (Nkind (N)) is | |
d26dc4b5 AC |
9916 | when N_Op_Eq => |
9917 | True_Result := Res = EQ; | |
9918 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
9919 | ||
9920 | when N_Op_Ge => | |
9921 | True_Result := Res in Compare_GE; | |
9922 | False_Result := Res = LT; | |
9923 | ||
9924 | if Res = LE | |
9925 | and then Constant_Condition_Warnings | |
9926 | and then Comes_From_Source (Original_Node (N)) | |
9927 | and then Nkind (Original_Node (N)) = N_Op_Ge | |
9928 | and then not In_Instance | |
d26dc4b5 | 9929 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 9930 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 9931 | then |
ed2233dc | 9932 | Error_Msg_N |
d26dc4b5 | 9933 | ("can never be greater than, could replace by ""'=""?", N); |
c800f862 | 9934 | Warning_Generated := True; |
d26dc4b5 | 9935 | end if; |
70482933 | 9936 | |
d26dc4b5 AC |
9937 | when N_Op_Gt => |
9938 | True_Result := Res = GT; | |
9939 | False_Result := Res in Compare_LE; | |
9940 | ||
9941 | when N_Op_Lt => | |
9942 | True_Result := Res = LT; | |
9943 | False_Result := Res in Compare_GE; | |
9944 | ||
9945 | when N_Op_Le => | |
9946 | True_Result := Res in Compare_LE; | |
9947 | False_Result := Res = GT; | |
9948 | ||
9949 | if Res = GE | |
9950 | and then Constant_Condition_Warnings | |
9951 | and then Comes_From_Source (Original_Node (N)) | |
9952 | and then Nkind (Original_Node (N)) = N_Op_Le | |
9953 | and then not In_Instance | |
d26dc4b5 | 9954 | and then Is_Integer_Type (Etype (Left_Opnd (N))) |
59ae6391 | 9955 | and then not Has_Warnings_Off (Etype (Left_Opnd (N))) |
d26dc4b5 | 9956 | then |
ed2233dc | 9957 | Error_Msg_N |
d26dc4b5 | 9958 | ("can never be less than, could replace by ""'=""?", N); |
c800f862 | 9959 | Warning_Generated := True; |
d26dc4b5 | 9960 | end if; |
70482933 | 9961 | |
d26dc4b5 AC |
9962 | when N_Op_Ne => |
9963 | True_Result := Res = NE or else Res = GT or else Res = LT; | |
9964 | False_Result := Res = EQ; | |
c800f862 | 9965 | end case; |
d26dc4b5 | 9966 | |
c800f862 RD |
9967 | -- If this is the first iteration, then we actually convert the |
9968 | -- comparison into True or False, if the result is certain. | |
d26dc4b5 | 9969 | |
c800f862 RD |
9970 | if AV = False then |
9971 | if True_Result or False_Result then | |
9972 | if True_Result then | |
9973 | Result := Standard_True; | |
9974 | else | |
9975 | Result := Standard_False; | |
9976 | end if; | |
9977 | ||
9978 | Rewrite (N, | |
9979 | Convert_To (Typ, | |
9980 | New_Occurrence_Of (Result, Sloc (N)))); | |
9981 | Analyze_And_Resolve (N, Typ); | |
9982 | Warn_On_Known_Condition (N); | |
9983 | return; | |
9984 | end if; | |
9985 | ||
9986 | -- If this is the second iteration (AV = True), and the original | |
e7e4d230 AC |
9987 | -- node comes from source and we are not in an instance, then give |
9988 | -- a warning if we know result would be True or False. Note: we | |
9989 | -- know Constant_Condition_Warnings is set if we get here. | |
c800f862 RD |
9990 | |
9991 | elsif Comes_From_Source (Original_Node (N)) | |
9992 | and then not In_Instance | |
9993 | then | |
9994 | if True_Result then | |
ed2233dc | 9995 | Error_Msg_N |
c800f862 RD |
9996 | ("condition can only be False if invalid values present?", |
9997 | N); | |
9998 | elsif False_Result then | |
ed2233dc | 9999 | Error_Msg_N |
c800f862 RD |
10000 | ("condition can only be True if invalid values present?", |
10001 | N); | |
10002 | end if; | |
10003 | end if; | |
10004 | end; | |
10005 | ||
10006 | -- Skip second iteration if not warning on constant conditions or | |
e7e4d230 AC |
10007 | -- if the first iteration already generated a warning of some kind or |
10008 | -- if we are in any case assuming all values are valid (so that the | |
10009 | -- first iteration took care of the valid case). | |
c800f862 RD |
10010 | |
10011 | exit when not Constant_Condition_Warnings; | |
10012 | exit when Warning_Generated; | |
10013 | exit when Assume_No_Invalid_Values; | |
10014 | end loop; | |
70482933 RK |
10015 | end Rewrite_Comparison; |
10016 | ||
fbf5a39b AC |
10017 | ---------------------------- |
10018 | -- Safe_In_Place_Array_Op -- | |
10019 | ---------------------------- | |
10020 | ||
10021 | function Safe_In_Place_Array_Op | |
2e071734 AC |
10022 | (Lhs : Node_Id; |
10023 | Op1 : Node_Id; | |
10024 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
10025 | is |
10026 | Target : Entity_Id; | |
10027 | ||
10028 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
10029 | -- Operand is safe if it cannot overlap part of the target of the | |
10030 | -- operation. If the operand and the target are identical, the operand | |
10031 | -- is safe. The operand can be empty in the case of negation. | |
10032 | ||
10033 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 10034 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
10035 | |
10036 | ------------------ | |
10037 | -- Is_Unaliased -- | |
10038 | ------------------ | |
10039 | ||
10040 | function Is_Unaliased (N : Node_Id) return Boolean is | |
10041 | begin | |
10042 | return | |
10043 | Is_Entity_Name (N) | |
10044 | and then No (Address_Clause (Entity (N))) | |
10045 | and then No (Renamed_Object (Entity (N))); | |
10046 | end Is_Unaliased; | |
10047 | ||
10048 | --------------------- | |
10049 | -- Is_Safe_Operand -- | |
10050 | --------------------- | |
10051 | ||
10052 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
10053 | begin | |
10054 | if No (Op) then | |
10055 | return True; | |
10056 | ||
10057 | elsif Is_Entity_Name (Op) then | |
10058 | return Is_Unaliased (Op); | |
10059 | ||
303b4d58 | 10060 | elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then |
fbf5a39b AC |
10061 | return Is_Unaliased (Prefix (Op)); |
10062 | ||
10063 | elsif Nkind (Op) = N_Slice then | |
10064 | return | |
10065 | Is_Unaliased (Prefix (Op)) | |
10066 | and then Entity (Prefix (Op)) /= Target; | |
10067 | ||
10068 | elsif Nkind (Op) = N_Op_Not then | |
10069 | return Is_Safe_Operand (Right_Opnd (Op)); | |
10070 | ||
10071 | else | |
10072 | return False; | |
10073 | end if; | |
10074 | end Is_Safe_Operand; | |
10075 | ||
e7e4d230 | 10076 | -- Start of processing for Is_Safe_In_Place_Array_Op |
fbf5a39b AC |
10077 | |
10078 | begin | |
685094bf RD |
10079 | -- Skip this processing if the component size is different from system |
10080 | -- storage unit (since at least for NOT this would cause problems). | |
fbf5a39b | 10081 | |
eaa826f8 | 10082 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then |
fbf5a39b AC |
10083 | return False; |
10084 | ||
26bff3d9 | 10085 | -- Cannot do in place stuff on VM_Target since cannot pass addresses |
fbf5a39b | 10086 | |
26bff3d9 | 10087 | elsif VM_Target /= No_VM then |
fbf5a39b AC |
10088 | return False; |
10089 | ||
10090 | -- Cannot do in place stuff if non-standard Boolean representation | |
10091 | ||
eaa826f8 | 10092 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then |
fbf5a39b AC |
10093 | return False; |
10094 | ||
10095 | elsif not Is_Unaliased (Lhs) then | |
10096 | return False; | |
e7e4d230 | 10097 | |
fbf5a39b AC |
10098 | else |
10099 | Target := Entity (Lhs); | |
e7e4d230 | 10100 | return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2); |
fbf5a39b AC |
10101 | end if; |
10102 | end Safe_In_Place_Array_Op; | |
10103 | ||
70482933 RK |
10104 | ----------------------- |
10105 | -- Tagged_Membership -- | |
10106 | ----------------------- | |
10107 | ||
685094bf RD |
10108 | -- There are two different cases to consider depending on whether the right |
10109 | -- operand is a class-wide type or not. If not we just compare the actual | |
10110 | -- tag of the left expr to the target type tag: | |
70482933 RK |
10111 | -- |
10112 | -- Left_Expr.Tag = Right_Type'Tag; | |
10113 | -- | |
685094bf RD |
10114 | -- If it is a class-wide type we use the RT function CW_Membership which is |
10115 | -- usually implemented by looking in the ancestor tables contained in the | |
10116 | -- dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
70482933 | 10117 | |
0669bebe GB |
10118 | -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT |
10119 | -- function IW_Membership which is usually implemented by looking in the | |
10120 | -- table of abstract interface types plus the ancestor table contained in | |
10121 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
10122 | ||
82878151 AC |
10123 | procedure Tagged_Membership |
10124 | (N : Node_Id; | |
10125 | SCIL_Node : out Node_Id; | |
10126 | Result : out Node_Id) | |
10127 | is | |
70482933 RK |
10128 | Left : constant Node_Id := Left_Opnd (N); |
10129 | Right : constant Node_Id := Right_Opnd (N); | |
10130 | Loc : constant Source_Ptr := Sloc (N); | |
10131 | ||
10132 | Left_Type : Entity_Id; | |
82878151 | 10133 | New_Node : Node_Id; |
70482933 RK |
10134 | Right_Type : Entity_Id; |
10135 | Obj_Tag : Node_Id; | |
10136 | ||
10137 | begin | |
82878151 AC |
10138 | SCIL_Node := Empty; |
10139 | ||
852dba80 AC |
10140 | -- Handle entities from the limited view |
10141 | ||
10142 | Left_Type := Available_View (Etype (Left)); | |
10143 | Right_Type := Available_View (Etype (Right)); | |
70482933 RK |
10144 | |
10145 | if Is_Class_Wide_Type (Left_Type) then | |
10146 | Left_Type := Root_Type (Left_Type); | |
10147 | end if; | |
10148 | ||
10149 | Obj_Tag := | |
10150 | Make_Selected_Component (Loc, | |
10151 | Prefix => Relocate_Node (Left), | |
a9d8907c JM |
10152 | Selector_Name => |
10153 | New_Reference_To (First_Tag_Component (Left_Type), Loc)); | |
70482933 RK |
10154 | |
10155 | if Is_Class_Wide_Type (Right_Type) then | |
758c442c | 10156 | |
0669bebe GB |
10157 | -- No need to issue a run-time check if we statically know that the |
10158 | -- result of this membership test is always true. For example, | |
10159 | -- considering the following declarations: | |
10160 | ||
10161 | -- type Iface is interface; | |
10162 | -- type T is tagged null record; | |
10163 | -- type DT is new T and Iface with null record; | |
10164 | ||
10165 | -- Obj1 : T; | |
10166 | -- Obj2 : DT; | |
10167 | ||
10168 | -- These membership tests are always true: | |
10169 | ||
10170 | -- Obj1 in T'Class | |
10171 | -- Obj2 in T'Class; | |
10172 | -- Obj2 in Iface'Class; | |
10173 | ||
10174 | -- We do not need to handle cases where the membership is illegal. | |
10175 | -- For example: | |
10176 | ||
10177 | -- Obj1 in DT'Class; -- Compile time error | |
10178 | -- Obj1 in Iface'Class; -- Compile time error | |
10179 | ||
10180 | if not Is_Class_Wide_Type (Left_Type) | |
ce2b6ba5 | 10181 | and then (Is_Ancestor (Etype (Right_Type), Left_Type) |
0669bebe GB |
10182 | or else (Is_Interface (Etype (Right_Type)) |
10183 | and then Interface_Present_In_Ancestor | |
10184 | (Typ => Left_Type, | |
10185 | Iface => Etype (Right_Type)))) | |
10186 | then | |
82878151 AC |
10187 | Result := New_Reference_To (Standard_True, Loc); |
10188 | return; | |
0669bebe GB |
10189 | end if; |
10190 | ||
758c442c GD |
10191 | -- Ada 2005 (AI-251): Class-wide applied to interfaces |
10192 | ||
630d30e9 RD |
10193 | if Is_Interface (Etype (Class_Wide_Type (Right_Type))) |
10194 | ||
0669bebe | 10195 | -- Support to: "Iface_CW_Typ in Typ'Class" |
630d30e9 RD |
10196 | |
10197 | or else Is_Interface (Left_Type) | |
10198 | then | |
dfd99a80 TQ |
10199 | -- Issue error if IW_Membership operation not available in a |
10200 | -- configurable run time setting. | |
10201 | ||
10202 | if not RTE_Available (RE_IW_Membership) then | |
b4592168 GD |
10203 | Error_Msg_CRT |
10204 | ("dynamic membership test on interface types", N); | |
82878151 AC |
10205 | Result := Empty; |
10206 | return; | |
dfd99a80 TQ |
10207 | end if; |
10208 | ||
82878151 | 10209 | Result := |
758c442c GD |
10210 | Make_Function_Call (Loc, |
10211 | Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc), | |
10212 | Parameter_Associations => New_List ( | |
10213 | Make_Attribute_Reference (Loc, | |
10214 | Prefix => Obj_Tag, | |
10215 | Attribute_Name => Name_Address), | |
10216 | New_Reference_To ( | |
10217 | Node (First_Elmt | |
10218 | (Access_Disp_Table (Root_Type (Right_Type)))), | |
10219 | Loc))); | |
10220 | ||
10221 | -- Ada 95: Normal case | |
10222 | ||
10223 | else | |
82878151 AC |
10224 | Build_CW_Membership (Loc, |
10225 | Obj_Tag_Node => Obj_Tag, | |
10226 | Typ_Tag_Node => | |
10227 | New_Reference_To ( | |
10228 | Node (First_Elmt | |
10229 | (Access_Disp_Table (Root_Type (Right_Type)))), | |
10230 | Loc), | |
10231 | Related_Nod => N, | |
10232 | New_Node => New_Node); | |
10233 | ||
10234 | -- Generate the SCIL node for this class-wide membership test. | |
10235 | -- Done here because the previous call to Build_CW_Membership | |
10236 | -- relocates Obj_Tag. | |
10237 | ||
10238 | if Generate_SCIL then | |
10239 | SCIL_Node := Make_SCIL_Membership_Test (Sloc (N)); | |
10240 | Set_SCIL_Entity (SCIL_Node, Etype (Right_Type)); | |
10241 | Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag); | |
10242 | end if; | |
10243 | ||
10244 | Result := New_Node; | |
758c442c GD |
10245 | end if; |
10246 | ||
0669bebe GB |
10247 | -- Right_Type is not a class-wide type |
10248 | ||
70482933 | 10249 | else |
0669bebe GB |
10250 | -- No need to check the tag of the object if Right_Typ is abstract |
10251 | ||
10252 | if Is_Abstract_Type (Right_Type) then | |
82878151 | 10253 | Result := New_Reference_To (Standard_False, Loc); |
0669bebe GB |
10254 | |
10255 | else | |
82878151 | 10256 | Result := |
0669bebe GB |
10257 | Make_Op_Eq (Loc, |
10258 | Left_Opnd => Obj_Tag, | |
10259 | Right_Opnd => | |
10260 | New_Reference_To | |
10261 | (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc)); | |
10262 | end if; | |
70482933 | 10263 | end if; |
70482933 RK |
10264 | end Tagged_Membership; |
10265 | ||
10266 | ------------------------------ | |
10267 | -- Unary_Op_Validity_Checks -- | |
10268 | ------------------------------ | |
10269 | ||
10270 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
10271 | begin | |
10272 | if Validity_Checks_On and Validity_Check_Operands then | |
10273 | Ensure_Valid (Right_Opnd (N)); | |
10274 | end if; | |
10275 | end Unary_Op_Validity_Checks; | |
10276 | ||
10277 | end Exp_Ch4; |