]>
Commit | Line | Data |
---|---|---|
70482933 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ C H 4 -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
8aceda64 | 9 | -- Copyright (C) 1992-2005, 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- -- | |
13 | -- ware Foundation; either version 2, or (at your option) any later ver- -- | |
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 -- | |
18 | -- Public License distributed with GNAT; see file COPYING. If not, write -- | |
19 | -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, -- | |
20 | -- MA 02111-1307, USA. -- | |
21 | -- -- | |
22 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 23 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
70482933 RK |
24 | -- -- |
25 | ------------------------------------------------------------------------------ | |
26 | ||
27 | with Atree; use Atree; | |
28 | with Checks; use Checks; | |
29 | with Einfo; use Einfo; | |
30 | with Elists; use Elists; | |
31 | with Errout; use Errout; | |
32 | with Exp_Aggr; use Exp_Aggr; | |
33 | with Exp_Ch3; use Exp_Ch3; | |
34 | with Exp_Ch7; use Exp_Ch7; | |
35 | with Exp_Ch9; use Exp_Ch9; | |
36 | with Exp_Disp; use Exp_Disp; | |
37 | with Exp_Fixd; use Exp_Fixd; | |
38 | with Exp_Pakd; use Exp_Pakd; | |
39 | with Exp_Tss; use Exp_Tss; | |
40 | with Exp_Util; use Exp_Util; | |
41 | with Exp_VFpt; use Exp_VFpt; | |
42 | with Hostparm; use Hostparm; | |
43 | with Inline; use Inline; | |
44 | with Nlists; use Nlists; | |
45 | with Nmake; use Nmake; | |
46 | with Opt; use Opt; | |
47 | with Rtsfind; use Rtsfind; | |
48 | with Sem; use Sem; | |
49 | with Sem_Cat; use Sem_Cat; | |
5d09245e | 50 | with Sem_Ch3; use Sem_Ch3; |
70482933 RK |
51 | with Sem_Ch13; use Sem_Ch13; |
52 | with Sem_Eval; use Sem_Eval; | |
53 | with Sem_Res; use Sem_Res; | |
54 | with Sem_Type; use Sem_Type; | |
55 | with Sem_Util; use Sem_Util; | |
07fc65c4 | 56 | with Sem_Warn; use Sem_Warn; |
70482933 | 57 | with Sinfo; use Sinfo; |
70482933 RK |
58 | with Snames; use Snames; |
59 | with Stand; use Stand; | |
07fc65c4 | 60 | with Targparm; use Targparm; |
70482933 RK |
61 | with Tbuild; use Tbuild; |
62 | with Ttypes; use Ttypes; | |
63 | with Uintp; use Uintp; | |
64 | with Urealp; use Urealp; | |
65 | with Validsw; use Validsw; | |
66 | ||
67 | package body Exp_Ch4 is | |
68 | ||
15ce9ca2 AC |
69 | ----------------------- |
70 | -- Local Subprograms -- | |
71 | ----------------------- | |
70482933 RK |
72 | |
73 | procedure Binary_Op_Validity_Checks (N : Node_Id); | |
74 | pragma Inline (Binary_Op_Validity_Checks); | |
75 | -- Performs validity checks for a binary operator | |
76 | ||
fbf5a39b AC |
77 | procedure Build_Boolean_Array_Proc_Call |
78 | (N : Node_Id; | |
79 | Op1 : Node_Id; | |
80 | Op2 : Node_Id); | |
81 | -- If an boolean array assignment can be done in place, build call to | |
82 | -- corresponding library procedure. | |
83 | ||
84 | procedure Expand_Allocator_Expression (N : Node_Id); | |
85 | -- Subsidiary to Expand_N_Allocator, for the case when the expression | |
86 | -- is a qualified expression or an aggregate. | |
87 | ||
70482933 RK |
88 | procedure Expand_Array_Comparison (N : Node_Id); |
89 | -- This routine handles expansion of the comparison operators (N_Op_Lt, | |
90 | -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic | |
91 | -- code for these operators is similar, differing only in the details of | |
fbf5a39b AC |
92 | -- the actual comparison call that is made. Special processing (call a |
93 | -- run-time routine) | |
70482933 RK |
94 | |
95 | function Expand_Array_Equality | |
96 | (Nod : Node_Id; | |
70482933 RK |
97 | Lhs : Node_Id; |
98 | Rhs : Node_Id; | |
0da2c8ac AC |
99 | Bodies : List_Id; |
100 | Typ : Entity_Id) return Node_Id; | |
70482933 RK |
101 | -- Expand an array equality into a call to a function implementing this |
102 | -- equality, and a call to it. Loc is the location for the generated | |
0da2c8ac | 103 | -- nodes. Lhs and Rhs are the array expressions to be compared. |
70482933 | 104 | -- Bodies is a list on which to attach bodies of local functions that |
0da2c8ac | 105 | -- are created in the process. It is the responsibility of the |
70482933 | 106 | -- caller to insert those bodies at the right place. Nod provides |
0da2c8ac AC |
107 | -- the Sloc value for the generated code. Normally the types used |
108 | -- for the generated equality routine are taken from Lhs and Rhs. | |
109 | -- However, in some situations of generated code, the Etype fields | |
110 | -- of Lhs and Rhs are not set yet. In such cases, Typ supplies the | |
111 | -- type to be used for the formal parameters. | |
70482933 RK |
112 | |
113 | procedure Expand_Boolean_Operator (N : Node_Id); | |
114 | -- Common expansion processing for Boolean operators (And, Or, Xor) | |
115 | -- for the case of array type arguments. | |
116 | ||
117 | function Expand_Composite_Equality | |
118 | (Nod : Node_Id; | |
119 | Typ : Entity_Id; | |
120 | Lhs : Node_Id; | |
121 | Rhs : Node_Id; | |
2e071734 | 122 | Bodies : List_Id) return Node_Id; |
70482933 RK |
123 | -- Local recursive function used to expand equality for nested |
124 | -- composite types. Used by Expand_Record/Array_Equality, Bodies | |
125 | -- is a list on which to attach bodies of local functions that are | |
126 | -- created in the process. This is the responsability of the caller | |
127 | -- to insert those bodies at the right place. Nod provides the Sloc | |
0da2c8ac AC |
128 | -- value for generated code. Lhs and Rhs are the left and right sides |
129 | -- for the comparison, and Typ is the type of the arrays to compare. | |
70482933 RK |
130 | |
131 | procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id); | |
132 | -- This routine handles expansion of concatenation operations, where | |
133 | -- N is the N_Op_Concat node being expanded and Operands is the list | |
134 | -- of operands (at least two are present). The caller has dealt with | |
135 | -- converting any singleton operands into singleton aggregates. | |
136 | ||
137 | procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id); | |
138 | -- Routine to expand concatenation of 2-5 operands (in the list Operands) | |
139 | -- and replace node Cnode with the result of the contatenation. If there | |
140 | -- are two operands, they can be string or character. If there are more | |
141 | -- than two operands, then are always of type string (i.e. the caller has | |
142 | -- already converted character operands to strings in this case). | |
143 | ||
144 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id); | |
145 | -- N is either an N_Op_Divide or N_Op_Multiply node whose result is | |
146 | -- universal fixed. We do not have such a type at runtime, so the | |
147 | -- purpose of this routine is to find the real type by looking up | |
148 | -- the tree. We also determine if the operation must be rounded. | |
149 | ||
fbf5a39b AC |
150 | function Get_Allocator_Final_List |
151 | (N : Node_Id; | |
152 | T : Entity_Id; | |
2e071734 | 153 | PtrT : Entity_Id) return Entity_Id; |
fbf5a39b AC |
154 | -- If the designated type is controlled, build final_list expression |
155 | -- for created object. If context is an access parameter, create a | |
156 | -- local access type to have a usable finalization list. | |
157 | ||
5d09245e AC |
158 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean; |
159 | -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable | |
160 | -- discriminants if it has a constrained nominal type, unless the object | |
161 | -- is a component of an enclosing Unchecked_Union object that is subject | |
162 | -- to a per-object constraint and the enclosing object lacks inferable | |
163 | -- discriminants. | |
164 | -- | |
165 | -- An expression of an Unchecked_Union type has inferable discriminants | |
166 | -- if it is either a name of an object with inferable discriminants or a | |
167 | -- qualified expression whose subtype mark denotes a constrained subtype. | |
168 | ||
70482933 | 169 | procedure Insert_Dereference_Action (N : Node_Id); |
e6f69614 AC |
170 | -- N is an expression whose type is an access. When the type of the |
171 | -- associated storage pool is derived from Checked_Pool, generate a | |
172 | -- call to the 'Dereference' primitive operation. | |
70482933 RK |
173 | |
174 | function Make_Array_Comparison_Op | |
2e071734 AC |
175 | (Typ : Entity_Id; |
176 | Nod : Node_Id) return Node_Id; | |
70482933 RK |
177 | -- Comparisons between arrays are expanded in line. This function |
178 | -- produces the body of the implementation of (a > b), where a and b | |
179 | -- are one-dimensional arrays of some discrete type. The original | |
180 | -- node is then expanded into the appropriate call to this function. | |
181 | -- Nod provides the Sloc value for the generated code. | |
182 | ||
183 | function Make_Boolean_Array_Op | |
2e071734 AC |
184 | (Typ : Entity_Id; |
185 | N : Node_Id) return Node_Id; | |
70482933 RK |
186 | -- Boolean operations on boolean arrays are expanded in line. This |
187 | -- function produce the body for the node N, which is (a and b), | |
188 | -- (a or b), or (a xor b). It is used only the normal case and not | |
189 | -- the packed case. The type involved, Typ, is the Boolean array type, | |
190 | -- and the logical operations in the body are simple boolean operations. | |
191 | -- Note that Typ is always a constrained type (the caller has ensured | |
192 | -- this by using Convert_To_Actual_Subtype if necessary). | |
193 | ||
194 | procedure Rewrite_Comparison (N : Node_Id); | |
195 | -- N is the node for a compile time comparison. If this outcome of this | |
196 | -- comparison can be determined at compile time, then the node N can be | |
197 | -- rewritten with True or False. If the outcome cannot be determined at | |
198 | -- compile time, the call has no effect. | |
199 | ||
200 | function Tagged_Membership (N : Node_Id) return Node_Id; | |
201 | -- Construct the expression corresponding to the tagged membership test. | |
202 | -- Deals with a second operand being (or not) a class-wide type. | |
203 | ||
fbf5a39b | 204 | function Safe_In_Place_Array_Op |
2e071734 AC |
205 | (Lhs : Node_Id; |
206 | Op1 : Node_Id; | |
207 | Op2 : Node_Id) return Boolean; | |
fbf5a39b AC |
208 | -- In the context of an assignment, where the right-hand side is a |
209 | -- boolean operation on arrays, check whether operation can be performed | |
210 | -- in place. | |
211 | ||
70482933 RK |
212 | procedure Unary_Op_Validity_Checks (N : Node_Id); |
213 | pragma Inline (Unary_Op_Validity_Checks); | |
214 | -- Performs validity checks for a unary operator | |
215 | ||
216 | ------------------------------- | |
217 | -- Binary_Op_Validity_Checks -- | |
218 | ------------------------------- | |
219 | ||
220 | procedure Binary_Op_Validity_Checks (N : Node_Id) is | |
221 | begin | |
222 | if Validity_Checks_On and Validity_Check_Operands then | |
223 | Ensure_Valid (Left_Opnd (N)); | |
224 | Ensure_Valid (Right_Opnd (N)); | |
225 | end if; | |
226 | end Binary_Op_Validity_Checks; | |
227 | ||
fbf5a39b AC |
228 | ------------------------------------ |
229 | -- Build_Boolean_Array_Proc_Call -- | |
230 | ------------------------------------ | |
231 | ||
232 | procedure Build_Boolean_Array_Proc_Call | |
233 | (N : Node_Id; | |
234 | Op1 : Node_Id; | |
235 | Op2 : Node_Id) | |
236 | is | |
237 | Loc : constant Source_Ptr := Sloc (N); | |
238 | Kind : constant Node_Kind := Nkind (Expression (N)); | |
239 | Target : constant Node_Id := | |
240 | Make_Attribute_Reference (Loc, | |
241 | Prefix => Name (N), | |
242 | Attribute_Name => Name_Address); | |
243 | ||
244 | Arg1 : constant Node_Id := Op1; | |
245 | Arg2 : Node_Id := Op2; | |
246 | Call_Node : Node_Id; | |
247 | Proc_Name : Entity_Id; | |
248 | ||
249 | begin | |
250 | if Kind = N_Op_Not then | |
251 | if Nkind (Op1) in N_Binary_Op then | |
252 | ||
5e1c00fa | 253 | -- Use negated version of the binary operators |
fbf5a39b AC |
254 | |
255 | if Nkind (Op1) = N_Op_And then | |
256 | Proc_Name := RTE (RE_Vector_Nand); | |
257 | ||
258 | elsif Nkind (Op1) = N_Op_Or then | |
259 | Proc_Name := RTE (RE_Vector_Nor); | |
260 | ||
261 | else pragma Assert (Nkind (Op1) = N_Op_Xor); | |
262 | Proc_Name := RTE (RE_Vector_Xor); | |
263 | end if; | |
264 | ||
265 | Call_Node := | |
266 | Make_Procedure_Call_Statement (Loc, | |
267 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
268 | ||
269 | Parameter_Associations => New_List ( | |
270 | Target, | |
271 | Make_Attribute_Reference (Loc, | |
272 | Prefix => Left_Opnd (Op1), | |
273 | Attribute_Name => Name_Address), | |
274 | ||
275 | Make_Attribute_Reference (Loc, | |
276 | Prefix => Right_Opnd (Op1), | |
277 | Attribute_Name => Name_Address), | |
278 | ||
279 | Make_Attribute_Reference (Loc, | |
280 | Prefix => Left_Opnd (Op1), | |
281 | Attribute_Name => Name_Length))); | |
282 | ||
283 | else | |
284 | Proc_Name := RTE (RE_Vector_Not); | |
285 | ||
286 | Call_Node := | |
287 | Make_Procedure_Call_Statement (Loc, | |
288 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
289 | Parameter_Associations => New_List ( | |
290 | Target, | |
291 | ||
292 | Make_Attribute_Reference (Loc, | |
293 | Prefix => Op1, | |
294 | Attribute_Name => Name_Address), | |
295 | ||
296 | Make_Attribute_Reference (Loc, | |
297 | Prefix => Op1, | |
298 | Attribute_Name => Name_Length))); | |
299 | end if; | |
300 | ||
301 | else | |
302 | -- We use the following equivalences: | |
303 | ||
304 | -- (not X) or (not Y) = not (X and Y) = Nand (X, Y) | |
305 | -- (not X) and (not Y) = not (X or Y) = Nor (X, Y) | |
306 | -- (not X) xor (not Y) = X xor Y | |
307 | -- X xor (not Y) = not (X xor Y) = Nxor (X, Y) | |
308 | ||
309 | if Nkind (Op1) = N_Op_Not then | |
310 | if Kind = N_Op_And then | |
311 | Proc_Name := RTE (RE_Vector_Nor); | |
312 | ||
313 | elsif Kind = N_Op_Or then | |
314 | Proc_Name := RTE (RE_Vector_Nand); | |
315 | ||
316 | else | |
317 | Proc_Name := RTE (RE_Vector_Xor); | |
318 | end if; | |
319 | ||
320 | else | |
321 | if Kind = N_Op_And then | |
322 | Proc_Name := RTE (RE_Vector_And); | |
323 | ||
324 | elsif Kind = N_Op_Or then | |
325 | Proc_Name := RTE (RE_Vector_Or); | |
326 | ||
327 | elsif Nkind (Op2) = N_Op_Not then | |
328 | Proc_Name := RTE (RE_Vector_Nxor); | |
329 | Arg2 := Right_Opnd (Op2); | |
330 | ||
331 | else | |
332 | Proc_Name := RTE (RE_Vector_Xor); | |
333 | end if; | |
334 | end if; | |
335 | ||
336 | Call_Node := | |
337 | Make_Procedure_Call_Statement (Loc, | |
338 | Name => New_Occurrence_Of (Proc_Name, Loc), | |
339 | Parameter_Associations => New_List ( | |
340 | Target, | |
341 | Make_Attribute_Reference (Loc, | |
342 | Prefix => Arg1, | |
343 | Attribute_Name => Name_Address), | |
344 | Make_Attribute_Reference (Loc, | |
345 | Prefix => Arg2, | |
346 | Attribute_Name => Name_Address), | |
347 | Make_Attribute_Reference (Loc, | |
348 | Prefix => Op1, | |
349 | Attribute_Name => Name_Length))); | |
350 | end if; | |
351 | ||
352 | Rewrite (N, Call_Node); | |
353 | Analyze (N); | |
354 | ||
355 | exception | |
356 | when RE_Not_Available => | |
357 | return; | |
358 | end Build_Boolean_Array_Proc_Call; | |
359 | ||
360 | --------------------------------- | |
361 | -- Expand_Allocator_Expression -- | |
362 | --------------------------------- | |
363 | ||
364 | procedure Expand_Allocator_Expression (N : Node_Id) is | |
365 | Loc : constant Source_Ptr := Sloc (N); | |
366 | Exp : constant Node_Id := Expression (Expression (N)); | |
367 | Indic : constant Node_Id := Subtype_Mark (Expression (N)); | |
368 | PtrT : constant Entity_Id := Etype (N); | |
369 | T : constant Entity_Id := Entity (Indic); | |
370 | Flist : Node_Id; | |
371 | Node : Node_Id; | |
372 | Temp : Entity_Id; | |
373 | ||
374 | Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp); | |
375 | ||
376 | Tag_Assign : Node_Id; | |
377 | Tmp_Node : Node_Id; | |
378 | ||
379 | begin | |
380 | if Is_Tagged_Type (T) or else Controlled_Type (T) then | |
381 | ||
382 | -- Actions inserted before: | |
383 | -- Temp : constant ptr_T := new T'(Expression); | |
384 | -- <no CW> Temp._tag := T'tag; | |
385 | -- <CTRL> Adjust (Finalizable (Temp.all)); | |
386 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
387 | ||
388 | -- We analyze by hand the new internal allocator to avoid | |
389 | -- any recursion and inappropriate call to Initialize | |
7324bf49 | 390 | |
fbf5a39b AC |
391 | if not Aggr_In_Place then |
392 | Remove_Side_Effects (Exp); | |
393 | end if; | |
394 | ||
395 | Temp := | |
396 | Make_Defining_Identifier (Loc, New_Internal_Name ('P')); | |
397 | ||
398 | -- For a class wide allocation generate the following code: | |
399 | ||
400 | -- type Equiv_Record is record ... end record; | |
401 | -- implicit subtype CW is <Class_Wide_Subytpe>; | |
402 | -- temp : PtrT := new CW'(CW!(expr)); | |
403 | ||
404 | if Is_Class_Wide_Type (T) then | |
405 | Expand_Subtype_From_Expr (Empty, T, Indic, Exp); | |
406 | ||
407 | Set_Expression (Expression (N), | |
408 | Unchecked_Convert_To (Entity (Indic), Exp)); | |
409 | ||
410 | Analyze_And_Resolve (Expression (N), Entity (Indic)); | |
411 | end if; | |
412 | ||
413 | if Aggr_In_Place then | |
414 | Tmp_Node := | |
415 | Make_Object_Declaration (Loc, | |
416 | Defining_Identifier => Temp, | |
417 | Object_Definition => New_Reference_To (PtrT, Loc), | |
418 | Expression => | |
419 | Make_Allocator (Loc, | |
420 | New_Reference_To (Etype (Exp), Loc))); | |
421 | ||
422 | Set_Comes_From_Source | |
423 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
424 | ||
425 | Set_No_Initialization (Expression (Tmp_Node)); | |
426 | Insert_Action (N, Tmp_Node); | |
427 | ||
428 | if Controlled_Type (T) | |
429 | and then Ekind (PtrT) = E_Anonymous_Access_Type | |
430 | then | |
5e1c00fa | 431 | -- Create local finalization list for access parameter |
fbf5a39b AC |
432 | |
433 | Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
434 | end if; | |
435 | ||
436 | Convert_Aggr_In_Allocator (Tmp_Node, Exp); | |
437 | else | |
438 | Node := Relocate_Node (N); | |
439 | Set_Analyzed (Node); | |
440 | Insert_Action (N, | |
441 | Make_Object_Declaration (Loc, | |
442 | Defining_Identifier => Temp, | |
443 | Constant_Present => True, | |
444 | Object_Definition => New_Reference_To (PtrT, Loc), | |
445 | Expression => Node)); | |
446 | end if; | |
447 | ||
448 | -- Suppress the tag assignment when Java_VM because JVM tags | |
449 | -- are represented implicitly in objects. | |
450 | ||
451 | if Is_Tagged_Type (T) | |
452 | and then not Is_Class_Wide_Type (T) | |
453 | and then not Java_VM | |
454 | then | |
455 | Tag_Assign := | |
456 | Make_Assignment_Statement (Loc, | |
457 | Name => | |
458 | Make_Selected_Component (Loc, | |
459 | Prefix => New_Reference_To (Temp, Loc), | |
460 | Selector_Name => | |
461 | New_Reference_To (Tag_Component (T), Loc)), | |
462 | ||
463 | Expression => | |
464 | Unchecked_Convert_To (RTE (RE_Tag), | |
465 | New_Reference_To (Access_Disp_Table (T), Loc))); | |
466 | ||
467 | -- The previous assignment has to be done in any case | |
468 | ||
469 | Set_Assignment_OK (Name (Tag_Assign)); | |
470 | Insert_Action (N, Tag_Assign); | |
471 | ||
472 | elsif Is_Private_Type (T) | |
473 | and then Is_Tagged_Type (Underlying_Type (T)) | |
474 | and then not Java_VM | |
475 | then | |
476 | declare | |
477 | Utyp : constant Entity_Id := Underlying_Type (T); | |
478 | Ref : constant Node_Id := | |
479 | Unchecked_Convert_To (Utyp, | |
480 | Make_Explicit_Dereference (Loc, | |
481 | New_Reference_To (Temp, Loc))); | |
482 | ||
483 | begin | |
484 | Tag_Assign := | |
485 | Make_Assignment_Statement (Loc, | |
486 | Name => | |
487 | Make_Selected_Component (Loc, | |
488 | Prefix => Ref, | |
489 | Selector_Name => | |
490 | New_Reference_To (Tag_Component (Utyp), Loc)), | |
491 | ||
492 | Expression => | |
493 | Unchecked_Convert_To (RTE (RE_Tag), | |
494 | New_Reference_To ( | |
495 | Access_Disp_Table (Utyp), Loc))); | |
496 | ||
497 | Set_Assignment_OK (Name (Tag_Assign)); | |
498 | Insert_Action (N, Tag_Assign); | |
499 | end; | |
500 | end if; | |
501 | ||
502 | if Controlled_Type (Designated_Type (PtrT)) | |
503 | and then Controlled_Type (T) | |
504 | then | |
505 | declare | |
506 | Attach : Node_Id; | |
507 | Apool : constant Entity_Id := | |
508 | Associated_Storage_Pool (PtrT); | |
509 | ||
510 | begin | |
511 | -- If it is an allocation on the secondary stack | |
512 | -- (i.e. a value returned from a function), the object | |
513 | -- is attached on the caller side as soon as the call | |
514 | -- is completed (see Expand_Ctrl_Function_Call) | |
515 | ||
516 | if Is_RTE (Apool, RE_SS_Pool) then | |
517 | declare | |
518 | F : constant Entity_Id := | |
519 | Make_Defining_Identifier (Loc, | |
520 | New_Internal_Name ('F')); | |
521 | begin | |
522 | Insert_Action (N, | |
523 | Make_Object_Declaration (Loc, | |
524 | Defining_Identifier => F, | |
525 | Object_Definition => New_Reference_To (RTE | |
526 | (RE_Finalizable_Ptr), Loc))); | |
527 | ||
528 | Flist := New_Reference_To (F, Loc); | |
529 | Attach := Make_Integer_Literal (Loc, 1); | |
530 | end; | |
531 | ||
532 | -- Normal case, not a secondary stack allocation | |
533 | ||
534 | else | |
615cbd95 AC |
535 | if Controlled_Type (T) |
536 | and then Ekind (PtrT) = E_Anonymous_Access_Type | |
537 | then | |
5e1c00fa | 538 | -- Create local finalization list for access parameter |
615cbd95 AC |
539 | |
540 | Flist := | |
541 | Get_Allocator_Final_List (N, Base_Type (T), PtrT); | |
542 | else | |
543 | Flist := Find_Final_List (PtrT); | |
544 | end if; | |
545 | ||
fbf5a39b AC |
546 | Attach := Make_Integer_Literal (Loc, 2); |
547 | end if; | |
548 | ||
549 | if not Aggr_In_Place then | |
550 | Insert_Actions (N, | |
551 | Make_Adjust_Call ( | |
552 | Ref => | |
553 | ||
554 | -- An unchecked conversion is needed in the | |
555 | -- classwide case because the designated type | |
556 | -- can be an ancestor of the subtype mark of | |
557 | -- the allocator. | |
558 | ||
559 | Unchecked_Convert_To (T, | |
560 | Make_Explicit_Dereference (Loc, | |
561 | New_Reference_To (Temp, Loc))), | |
562 | ||
563 | Typ => T, | |
564 | Flist_Ref => Flist, | |
565 | With_Attach => Attach)); | |
566 | end if; | |
567 | end; | |
568 | end if; | |
569 | ||
570 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
571 | Analyze_And_Resolve (N, PtrT); | |
572 | ||
573 | elsif Aggr_In_Place then | |
574 | Temp := | |
575 | Make_Defining_Identifier (Loc, New_Internal_Name ('P')); | |
576 | Tmp_Node := | |
577 | Make_Object_Declaration (Loc, | |
578 | Defining_Identifier => Temp, | |
579 | Object_Definition => New_Reference_To (PtrT, Loc), | |
580 | Expression => Make_Allocator (Loc, | |
581 | New_Reference_To (Etype (Exp), Loc))); | |
582 | ||
583 | Set_Comes_From_Source | |
584 | (Expression (Tmp_Node), Comes_From_Source (N)); | |
585 | ||
586 | Set_No_Initialization (Expression (Tmp_Node)); | |
587 | Insert_Action (N, Tmp_Node); | |
588 | Convert_Aggr_In_Allocator (Tmp_Node, Exp); | |
589 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
590 | Analyze_And_Resolve (N, PtrT); | |
591 | ||
592 | elsif Is_Access_Type (Designated_Type (PtrT)) | |
593 | and then Nkind (Exp) = N_Allocator | |
594 | and then Nkind (Expression (Exp)) /= N_Qualified_Expression | |
595 | then | |
0da2c8ac | 596 | -- Apply constraint to designated subtype indication |
fbf5a39b AC |
597 | |
598 | Apply_Constraint_Check (Expression (Exp), | |
599 | Designated_Type (Designated_Type (PtrT)), | |
600 | No_Sliding => True); | |
601 | ||
602 | if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then | |
603 | ||
604 | -- Propagate constraint_error to enclosing allocator | |
605 | ||
606 | Rewrite (Exp, New_Copy (Expression (Exp))); | |
607 | end if; | |
608 | else | |
609 | -- First check against the type of the qualified expression | |
610 | -- | |
611 | -- NOTE: The commented call should be correct, but for | |
612 | -- some reason causes the compiler to bomb (sigsegv) on | |
613 | -- ACVC test c34007g, so for now we just perform the old | |
614 | -- (incorrect) test against the designated subtype with | |
615 | -- no sliding in the else part of the if statement below. | |
616 | -- ??? | |
617 | -- | |
618 | -- Apply_Constraint_Check (Exp, T, No_Sliding => True); | |
619 | ||
620 | -- A check is also needed in cases where the designated | |
621 | -- subtype is constrained and differs from the subtype | |
622 | -- given in the qualified expression. Note that the check | |
623 | -- on the qualified expression does not allow sliding, | |
624 | -- but this check does (a relaxation from Ada 83). | |
625 | ||
626 | if Is_Constrained (Designated_Type (PtrT)) | |
627 | and then not Subtypes_Statically_Match | |
628 | (T, Designated_Type (PtrT)) | |
629 | then | |
630 | Apply_Constraint_Check | |
631 | (Exp, Designated_Type (PtrT), No_Sliding => False); | |
632 | ||
633 | -- The nonsliding check should really be performed | |
634 | -- (unconditionally) against the subtype of the | |
635 | -- qualified expression, but that causes a problem | |
636 | -- with c34007g (see above), so for now we retain this. | |
637 | ||
638 | else | |
639 | Apply_Constraint_Check | |
640 | (Exp, Designated_Type (PtrT), No_Sliding => True); | |
641 | end if; | |
642 | end if; | |
643 | ||
644 | exception | |
645 | when RE_Not_Available => | |
646 | return; | |
647 | end Expand_Allocator_Expression; | |
648 | ||
70482933 RK |
649 | ----------------------------- |
650 | -- Expand_Array_Comparison -- | |
651 | ----------------------------- | |
652 | ||
fbf5a39b AC |
653 | -- Expansion is only required in the case of array types. For the |
654 | -- unpacked case, an appropriate runtime routine is called. For | |
655 | -- packed cases, and also in some other cases where a runtime | |
656 | -- routine cannot be called, the form of the expansion is: | |
70482933 RK |
657 | |
658 | -- [body for greater_nn; boolean_expression] | |
659 | ||
660 | -- The body is built by Make_Array_Comparison_Op, and the form of the | |
661 | -- Boolean expression depends on the operator involved. | |
662 | ||
663 | procedure Expand_Array_Comparison (N : Node_Id) is | |
664 | Loc : constant Source_Ptr := Sloc (N); | |
665 | Op1 : Node_Id := Left_Opnd (N); | |
666 | Op2 : Node_Id := Right_Opnd (N); | |
667 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
fbf5a39b | 668 | Ctyp : constant Entity_Id := Component_Type (Typ1); |
70482933 RK |
669 | |
670 | Expr : Node_Id; | |
671 | Func_Body : Node_Id; | |
672 | Func_Name : Entity_Id; | |
673 | ||
fbf5a39b AC |
674 | Comp : RE_Id; |
675 | ||
9bc43c53 AC |
676 | Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size; |
677 | -- True for byte addressable target | |
91b1417d | 678 | |
fbf5a39b AC |
679 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean; |
680 | -- Returns True if the length of the given operand is known to be | |
681 | -- less than 4. Returns False if this length is known to be four | |
682 | -- or greater or is not known at compile time. | |
683 | ||
684 | ------------------------ | |
685 | -- Length_Less_Than_4 -- | |
686 | ------------------------ | |
687 | ||
688 | function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is | |
689 | Otyp : constant Entity_Id := Etype (Opnd); | |
690 | ||
691 | begin | |
692 | if Ekind (Otyp) = E_String_Literal_Subtype then | |
693 | return String_Literal_Length (Otyp) < 4; | |
694 | ||
695 | else | |
696 | declare | |
697 | Ityp : constant Entity_Id := Etype (First_Index (Otyp)); | |
698 | Lo : constant Node_Id := Type_Low_Bound (Ityp); | |
699 | Hi : constant Node_Id := Type_High_Bound (Ityp); | |
700 | Lov : Uint; | |
701 | Hiv : Uint; | |
702 | ||
703 | begin | |
704 | if Compile_Time_Known_Value (Lo) then | |
705 | Lov := Expr_Value (Lo); | |
706 | else | |
707 | return False; | |
708 | end if; | |
709 | ||
710 | if Compile_Time_Known_Value (Hi) then | |
711 | Hiv := Expr_Value (Hi); | |
712 | else | |
713 | return False; | |
714 | end if; | |
715 | ||
716 | return Hiv < Lov + 3; | |
717 | end; | |
718 | end if; | |
719 | end Length_Less_Than_4; | |
720 | ||
721 | -- Start of processing for Expand_Array_Comparison | |
722 | ||
70482933 | 723 | begin |
fbf5a39b AC |
724 | -- Deal first with unpacked case, where we can call a runtime routine |
725 | -- except that we avoid this for targets for which are not addressable | |
726 | -- by bytes, and for the JVM, since the JVM does not support direct | |
727 | -- addressing of array components. | |
728 | ||
729 | if not Is_Bit_Packed_Array (Typ1) | |
9bc43c53 | 730 | and then Byte_Addressable |
fbf5a39b AC |
731 | and then not Java_VM |
732 | then | |
733 | -- The call we generate is: | |
734 | ||
735 | -- Compare_Array_xn[_Unaligned] | |
736 | -- (left'address, right'address, left'length, right'length) <op> 0 | |
737 | ||
738 | -- x = U for unsigned, S for signed | |
739 | -- n = 8,16,32,64 for component size | |
740 | -- Add _Unaligned if length < 4 and component size is 8. | |
741 | -- <op> is the standard comparison operator | |
742 | ||
743 | if Component_Size (Typ1) = 8 then | |
744 | if Length_Less_Than_4 (Op1) | |
745 | or else | |
746 | Length_Less_Than_4 (Op2) | |
747 | then | |
748 | if Is_Unsigned_Type (Ctyp) then | |
749 | Comp := RE_Compare_Array_U8_Unaligned; | |
750 | else | |
751 | Comp := RE_Compare_Array_S8_Unaligned; | |
752 | end if; | |
753 | ||
754 | else | |
755 | if Is_Unsigned_Type (Ctyp) then | |
756 | Comp := RE_Compare_Array_U8; | |
757 | else | |
758 | Comp := RE_Compare_Array_S8; | |
759 | end if; | |
760 | end if; | |
761 | ||
762 | elsif Component_Size (Typ1) = 16 then | |
763 | if Is_Unsigned_Type (Ctyp) then | |
764 | Comp := RE_Compare_Array_U16; | |
765 | else | |
766 | Comp := RE_Compare_Array_S16; | |
767 | end if; | |
768 | ||
769 | elsif Component_Size (Typ1) = 32 then | |
770 | if Is_Unsigned_Type (Ctyp) then | |
771 | Comp := RE_Compare_Array_U32; | |
772 | else | |
773 | Comp := RE_Compare_Array_S32; | |
774 | end if; | |
775 | ||
776 | else pragma Assert (Component_Size (Typ1) = 64); | |
777 | if Is_Unsigned_Type (Ctyp) then | |
778 | Comp := RE_Compare_Array_U64; | |
779 | else | |
780 | Comp := RE_Compare_Array_S64; | |
781 | end if; | |
782 | end if; | |
783 | ||
784 | Remove_Side_Effects (Op1, Name_Req => True); | |
785 | Remove_Side_Effects (Op2, Name_Req => True); | |
786 | ||
787 | Rewrite (Op1, | |
788 | Make_Function_Call (Sloc (Op1), | |
789 | Name => New_Occurrence_Of (RTE (Comp), Loc), | |
790 | ||
791 | Parameter_Associations => New_List ( | |
792 | Make_Attribute_Reference (Loc, | |
793 | Prefix => Relocate_Node (Op1), | |
794 | Attribute_Name => Name_Address), | |
795 | ||
796 | Make_Attribute_Reference (Loc, | |
797 | Prefix => Relocate_Node (Op2), | |
798 | Attribute_Name => Name_Address), | |
799 | ||
800 | Make_Attribute_Reference (Loc, | |
801 | Prefix => Relocate_Node (Op1), | |
802 | Attribute_Name => Name_Length), | |
803 | ||
804 | Make_Attribute_Reference (Loc, | |
805 | Prefix => Relocate_Node (Op2), | |
806 | Attribute_Name => Name_Length)))); | |
807 | ||
808 | Rewrite (Op2, | |
809 | Make_Integer_Literal (Sloc (Op2), | |
810 | Intval => Uint_0)); | |
811 | ||
812 | Analyze_And_Resolve (Op1, Standard_Integer); | |
813 | Analyze_And_Resolve (Op2, Standard_Integer); | |
814 | return; | |
815 | end if; | |
816 | ||
817 | -- Cases where we cannot make runtime call | |
818 | ||
70482933 RK |
819 | -- For (a <= b) we convert to not (a > b) |
820 | ||
821 | if Chars (N) = Name_Op_Le then | |
822 | Rewrite (N, | |
823 | Make_Op_Not (Loc, | |
824 | Right_Opnd => | |
825 | Make_Op_Gt (Loc, | |
826 | Left_Opnd => Op1, | |
827 | Right_Opnd => Op2))); | |
828 | Analyze_And_Resolve (N, Standard_Boolean); | |
829 | return; | |
830 | ||
831 | -- For < the Boolean expression is | |
832 | -- greater__nn (op2, op1) | |
833 | ||
834 | elsif Chars (N) = Name_Op_Lt then | |
835 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
836 | ||
837 | -- Switch operands | |
838 | ||
839 | Op1 := Right_Opnd (N); | |
840 | Op2 := Left_Opnd (N); | |
841 | ||
842 | -- For (a >= b) we convert to not (a < b) | |
843 | ||
844 | elsif Chars (N) = Name_Op_Ge then | |
845 | Rewrite (N, | |
846 | Make_Op_Not (Loc, | |
847 | Right_Opnd => | |
848 | Make_Op_Lt (Loc, | |
849 | Left_Opnd => Op1, | |
850 | Right_Opnd => Op2))); | |
851 | Analyze_And_Resolve (N, Standard_Boolean); | |
852 | return; | |
853 | ||
854 | -- For > the Boolean expression is | |
855 | -- greater__nn (op1, op2) | |
856 | ||
857 | else | |
858 | pragma Assert (Chars (N) = Name_Op_Gt); | |
859 | Func_Body := Make_Array_Comparison_Op (Typ1, N); | |
860 | end if; | |
861 | ||
862 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
863 | Expr := | |
864 | Make_Function_Call (Loc, | |
865 | Name => New_Reference_To (Func_Name, Loc), | |
866 | Parameter_Associations => New_List (Op1, Op2)); | |
867 | ||
868 | Insert_Action (N, Func_Body); | |
869 | Rewrite (N, Expr); | |
870 | Analyze_And_Resolve (N, Standard_Boolean); | |
871 | ||
fbf5a39b AC |
872 | exception |
873 | when RE_Not_Available => | |
874 | return; | |
70482933 RK |
875 | end Expand_Array_Comparison; |
876 | ||
877 | --------------------------- | |
878 | -- Expand_Array_Equality -- | |
879 | --------------------------- | |
880 | ||
881 | -- Expand an equality function for multi-dimensional arrays. Here is | |
882 | -- an example of such a function for Nb_Dimension = 2 | |
883 | ||
0da2c8ac | 884 | -- function Enn (A : atyp; B : btyp) return boolean is |
70482933 | 885 | -- begin |
fbf5a39b AC |
886 | -- if (A'length (1) = 0 or else A'length (2) = 0) |
887 | -- and then | |
888 | -- (B'length (1) = 0 or else B'length (2) = 0) | |
889 | -- then | |
890 | -- return True; -- RM 4.5.2(22) | |
891 | -- end if; | |
0da2c8ac | 892 | |
fbf5a39b AC |
893 | -- if A'length (1) /= B'length (1) |
894 | -- or else | |
895 | -- A'length (2) /= B'length (2) | |
896 | -- then | |
897 | -- return False; -- RM 4.5.2(23) | |
898 | -- end if; | |
0da2c8ac | 899 | |
fbf5a39b | 900 | -- declare |
523456db AC |
901 | -- A1 : Index_T1 := A'first (1); |
902 | -- B1 : Index_T1 := B'first (1); | |
fbf5a39b | 903 | -- begin |
523456db | 904 | -- loop |
fbf5a39b | 905 | -- declare |
523456db AC |
906 | -- A2 : Index_T2 := A'first (2); |
907 | -- B2 : Index_T2 := B'first (2); | |
fbf5a39b | 908 | -- begin |
523456db | 909 | -- loop |
fbf5a39b AC |
910 | -- if A (A1, A2) /= B (B1, B2) then |
911 | -- return False; | |
70482933 | 912 | -- end if; |
0da2c8ac | 913 | |
523456db AC |
914 | -- exit when A2 = A'last (2); |
915 | -- A2 := Index_T2'succ (A2); | |
0da2c8ac | 916 | -- B2 := Index_T2'succ (B2); |
70482933 | 917 | -- end loop; |
fbf5a39b | 918 | -- end; |
0da2c8ac | 919 | |
523456db AC |
920 | -- exit when A1 = A'last (1); |
921 | -- A1 := Index_T1'succ (A1); | |
0da2c8ac | 922 | -- B1 := Index_T1'succ (B1); |
70482933 | 923 | -- end loop; |
fbf5a39b | 924 | -- end; |
0da2c8ac | 925 | |
70482933 RK |
926 | -- return true; |
927 | -- end Enn; | |
928 | ||
0da2c8ac AC |
929 | -- Note on the formal types used (atyp and btyp). If either of the |
930 | -- arrays is of a private type, we use the underlying type, and | |
931 | -- do an unchecked conversion of the actual. If either of the arrays | |
932 | -- has a bound depending on a discriminant, then we use the base type | |
933 | -- since otherwise we have an escaped discriminant in the function. | |
934 | ||
523456db AC |
935 | -- If both arrays are constrained and have the same bounds, we can |
936 | -- generate a loop with an explicit iteration scheme using a 'Range | |
937 | -- attribute over the first array. | |
938 | ||
70482933 RK |
939 | function Expand_Array_Equality |
940 | (Nod : Node_Id; | |
70482933 RK |
941 | Lhs : Node_Id; |
942 | Rhs : Node_Id; | |
0da2c8ac AC |
943 | Bodies : List_Id; |
944 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
945 | is |
946 | Loc : constant Source_Ptr := Sloc (Nod); | |
fbf5a39b AC |
947 | Decls : constant List_Id := New_List; |
948 | Index_List1 : constant List_Id := New_List; | |
949 | Index_List2 : constant List_Id := New_List; | |
950 | ||
951 | Actuals : List_Id; | |
952 | Formals : List_Id; | |
953 | Func_Name : Entity_Id; | |
954 | Func_Body : Node_Id; | |
70482933 RK |
955 | |
956 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
957 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
958 | ||
0da2c8ac AC |
959 | Ltyp : Entity_Id; |
960 | Rtyp : Entity_Id; | |
961 | -- The parameter types to be used for the formals | |
962 | ||
fbf5a39b AC |
963 | function Arr_Attr |
964 | (Arr : Entity_Id; | |
965 | Nam : Name_Id; | |
2e071734 | 966 | Num : Int) return Node_Id; |
5e1c00fa | 967 | -- This builds the attribute reference Arr'Nam (Expr) |
fbf5a39b | 968 | |
70482933 | 969 | function Component_Equality (Typ : Entity_Id) return Node_Id; |
fbf5a39b AC |
970 | -- Create one statement to compare corresponding components, |
971 | -- designated by a full set of indices. | |
70482933 | 972 | |
0da2c8ac AC |
973 | function Get_Arg_Type (N : Node_Id) return Entity_Id; |
974 | -- Given one of the arguments, computes the appropriate type to | |
975 | -- be used for that argument in the corresponding function formal | |
976 | ||
fbf5a39b | 977 | function Handle_One_Dimension |
70482933 | 978 | (N : Int; |
2e071734 | 979 | Index : Node_Id) return Node_Id; |
0da2c8ac | 980 | -- This procedure returns the following code |
fbf5a39b AC |
981 | -- |
982 | -- declare | |
523456db | 983 | -- Bn : Index_T := B'First (N); |
fbf5a39b | 984 | -- begin |
523456db | 985 | -- loop |
fbf5a39b | 986 | -- xxx |
523456db AC |
987 | -- exit when An = A'Last (N); |
988 | -- An := Index_T'Succ (An) | |
0da2c8ac | 989 | -- Bn := Index_T'Succ (Bn) |
fbf5a39b AC |
990 | -- end loop; |
991 | -- end; | |
992 | -- | |
523456db AC |
993 | -- If both indices are constrained and identical, the procedure |
994 | -- returns a simpler loop: | |
995 | -- | |
996 | -- for An in A'Range (N) loop | |
997 | -- xxx | |
998 | -- end loop | |
0da2c8ac | 999 | -- |
523456db | 1000 | -- N is the dimension for which we are generating a loop. Index is the |
fbf5a39b | 1001 | -- N'th index node, whose Etype is Index_Type_n in the above code. |
0da2c8ac | 1002 | -- The xxx statement is either the loop or declare for the next |
fbf5a39b AC |
1003 | -- dimension or if this is the last dimension the comparison |
1004 | -- of corresponding components of the arrays. | |
1005 | -- | |
1006 | -- The actual way the code works is to return the comparison | |
1007 | -- of corresponding components for the N+1 call. That's neater! | |
1008 | ||
1009 | function Test_Empty_Arrays return Node_Id; | |
1010 | -- This function constructs the test for both arrays being empty | |
1011 | -- (A'length (1) = 0 or else A'length (2) = 0 or else ...) | |
1012 | -- and then | |
1013 | -- (B'length (1) = 0 or else B'length (2) = 0 or else ...) | |
1014 | ||
1015 | function Test_Lengths_Correspond return Node_Id; | |
1016 | -- This function constructs the test for arrays having different | |
1017 | -- lengths in at least one index position, in which case resull | |
1018 | ||
1019 | -- A'length (1) /= B'length (1) | |
1020 | -- or else | |
1021 | -- A'length (2) /= B'length (2) | |
1022 | -- or else | |
1023 | -- ... | |
1024 | ||
1025 | -------------- | |
1026 | -- Arr_Attr -- | |
1027 | -------------- | |
1028 | ||
1029 | function Arr_Attr | |
1030 | (Arr : Entity_Id; | |
1031 | Nam : Name_Id; | |
2e071734 | 1032 | Num : Int) return Node_Id |
fbf5a39b AC |
1033 | is |
1034 | begin | |
1035 | return | |
1036 | Make_Attribute_Reference (Loc, | |
1037 | Attribute_Name => Nam, | |
1038 | Prefix => New_Reference_To (Arr, Loc), | |
1039 | Expressions => New_List (Make_Integer_Literal (Loc, Num))); | |
1040 | end Arr_Attr; | |
70482933 RK |
1041 | |
1042 | ------------------------ | |
1043 | -- Component_Equality -- | |
1044 | ------------------------ | |
1045 | ||
1046 | function Component_Equality (Typ : Entity_Id) return Node_Id is | |
1047 | Test : Node_Id; | |
1048 | L, R : Node_Id; | |
1049 | ||
1050 | begin | |
1051 | -- if a(i1...) /= b(j1...) then return false; end if; | |
1052 | ||
1053 | L := | |
1054 | Make_Indexed_Component (Loc, | |
1055 | Prefix => Make_Identifier (Loc, Chars (A)), | |
1056 | Expressions => Index_List1); | |
1057 | ||
1058 | R := | |
1059 | Make_Indexed_Component (Loc, | |
1060 | Prefix => Make_Identifier (Loc, Chars (B)), | |
1061 | Expressions => Index_List2); | |
1062 | ||
1063 | Test := Expand_Composite_Equality | |
1064 | (Nod, Component_Type (Typ), L, R, Decls); | |
1065 | ||
8aceda64 AC |
1066 | -- If some (sub)component is an unchecked_union, the whole |
1067 | -- operation will raise program error. | |
1068 | ||
1069 | if Nkind (Test) = N_Raise_Program_Error then | |
1070 | return Test; | |
1071 | ||
1072 | else | |
1073 | return | |
1074 | Make_Implicit_If_Statement (Nod, | |
1075 | Condition => Make_Op_Not (Loc, Right_Opnd => Test), | |
1076 | Then_Statements => New_List ( | |
1077 | Make_Return_Statement (Loc, | |
1078 | Expression => New_Occurrence_Of (Standard_False, Loc)))); | |
1079 | end if; | |
70482933 RK |
1080 | end Component_Equality; |
1081 | ||
0da2c8ac AC |
1082 | ------------------ |
1083 | -- Get_Arg_Type -- | |
1084 | ------------------ | |
1085 | ||
1086 | function Get_Arg_Type (N : Node_Id) return Entity_Id is | |
1087 | T : Entity_Id; | |
1088 | X : Node_Id; | |
1089 | ||
1090 | begin | |
1091 | T := Etype (N); | |
1092 | ||
1093 | if No (T) then | |
1094 | return Typ; | |
1095 | ||
1096 | else | |
1097 | T := Underlying_Type (T); | |
1098 | ||
1099 | X := First_Index (T); | |
1100 | while Present (X) loop | |
1101 | if Denotes_Discriminant (Type_Low_Bound (Etype (X))) | |
1102 | or else | |
1103 | Denotes_Discriminant (Type_High_Bound (Etype (X))) | |
1104 | then | |
1105 | T := Base_Type (T); | |
1106 | exit; | |
1107 | end if; | |
1108 | ||
1109 | Next_Index (X); | |
1110 | end loop; | |
1111 | ||
1112 | return T; | |
1113 | end if; | |
1114 | end Get_Arg_Type; | |
1115 | ||
fbf5a39b AC |
1116 | -------------------------- |
1117 | -- Handle_One_Dimension -- | |
1118 | --------------------------- | |
70482933 | 1119 | |
fbf5a39b | 1120 | function Handle_One_Dimension |
70482933 | 1121 | (N : Int; |
2e071734 | 1122 | Index : Node_Id) return Node_Id |
70482933 | 1123 | is |
0da2c8ac AC |
1124 | Need_Separate_Indexes : constant Boolean := |
1125 | Ltyp /= Rtyp | |
1126 | or else not Is_Constrained (Ltyp); | |
1127 | -- If the index types are identical, and we are working with | |
1128 | -- constrained types, then we can use the same index for both of | |
1129 | -- the arrays. | |
1130 | ||
fbf5a39b AC |
1131 | An : constant Entity_Id := Make_Defining_Identifier (Loc, |
1132 | Chars => New_Internal_Name ('A')); | |
0da2c8ac AC |
1133 | |
1134 | Bn : Entity_Id; | |
1135 | Index_T : Entity_Id; | |
1136 | Stm_List : List_Id; | |
1137 | Loop_Stm : Node_Id; | |
70482933 RK |
1138 | |
1139 | begin | |
0da2c8ac AC |
1140 | if N > Number_Dimensions (Ltyp) then |
1141 | return Component_Equality (Ltyp); | |
fbf5a39b | 1142 | end if; |
70482933 | 1143 | |
0da2c8ac AC |
1144 | -- Case where we generate a loop |
1145 | ||
1146 | Index_T := Base_Type (Etype (Index)); | |
1147 | ||
1148 | if Need_Separate_Indexes then | |
1149 | Bn := | |
1150 | Make_Defining_Identifier (Loc, | |
1151 | Chars => New_Internal_Name ('B')); | |
1152 | else | |
1153 | Bn := An; | |
1154 | end if; | |
70482933 | 1155 | |
fbf5a39b AC |
1156 | Append (New_Reference_To (An, Loc), Index_List1); |
1157 | Append (New_Reference_To (Bn, Loc), Index_List2); | |
70482933 | 1158 | |
0da2c8ac AC |
1159 | Stm_List := New_List ( |
1160 | Handle_One_Dimension (N + 1, Next_Index (Index))); | |
70482933 | 1161 | |
0da2c8ac | 1162 | if Need_Separate_Indexes then |
5e1c00fa | 1163 | -- Generate guard for loop, followed by increments of indices |
523456db AC |
1164 | |
1165 | Append_To (Stm_List, | |
1166 | Make_Exit_Statement (Loc, | |
1167 | Condition => | |
1168 | Make_Op_Eq (Loc, | |
1169 | Left_Opnd => New_Reference_To (An, Loc), | |
1170 | Right_Opnd => Arr_Attr (A, Name_Last, N)))); | |
1171 | ||
1172 | Append_To (Stm_List, | |
1173 | Make_Assignment_Statement (Loc, | |
1174 | Name => New_Reference_To (An, Loc), | |
1175 | Expression => | |
1176 | Make_Attribute_Reference (Loc, | |
1177 | Prefix => New_Reference_To (Index_T, Loc), | |
1178 | Attribute_Name => Name_Succ, | |
1179 | Expressions => New_List (New_Reference_To (An, Loc))))); | |
1180 | ||
0da2c8ac AC |
1181 | Append_To (Stm_List, |
1182 | Make_Assignment_Statement (Loc, | |
1183 | Name => New_Reference_To (Bn, Loc), | |
1184 | Expression => | |
1185 | Make_Attribute_Reference (Loc, | |
1186 | Prefix => New_Reference_To (Index_T, Loc), | |
1187 | Attribute_Name => Name_Succ, | |
1188 | Expressions => New_List (New_Reference_To (Bn, Loc))))); | |
1189 | end if; | |
1190 | ||
523456db AC |
1191 | -- If separate indexes, we need a declare block for An and Bn, |
1192 | -- and a loop without an iteration scheme. | |
0da2c8ac AC |
1193 | |
1194 | if Need_Separate_Indexes then | |
523456db AC |
1195 | Loop_Stm := |
1196 | Make_Implicit_Loop_Statement (Nod, Statements => Stm_List); | |
1197 | ||
0da2c8ac AC |
1198 | return |
1199 | Make_Block_Statement (Loc, | |
1200 | Declarations => New_List ( | |
523456db AC |
1201 | Make_Object_Declaration (Loc, |
1202 | Defining_Identifier => An, | |
1203 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1204 | Expression => Arr_Attr (A, Name_First, N)), | |
1205 | ||
0da2c8ac AC |
1206 | Make_Object_Declaration (Loc, |
1207 | Defining_Identifier => Bn, | |
1208 | Object_Definition => New_Reference_To (Index_T, Loc), | |
1209 | Expression => Arr_Attr (B, Name_First, N))), | |
523456db | 1210 | |
0da2c8ac AC |
1211 | Handled_Statement_Sequence => |
1212 | Make_Handled_Sequence_Of_Statements (Loc, | |
1213 | Statements => New_List (Loop_Stm))); | |
1214 | ||
523456db AC |
1215 | -- If no separate indexes, return loop statement with explicit |
1216 | -- iteration scheme on its own | |
0da2c8ac AC |
1217 | |
1218 | else | |
523456db AC |
1219 | Loop_Stm := |
1220 | Make_Implicit_Loop_Statement (Nod, | |
1221 | Statements => Stm_List, | |
1222 | Iteration_Scheme => | |
1223 | Make_Iteration_Scheme (Loc, | |
1224 | Loop_Parameter_Specification => | |
1225 | Make_Loop_Parameter_Specification (Loc, | |
1226 | Defining_Identifier => An, | |
1227 | Discrete_Subtype_Definition => | |
1228 | Arr_Attr (A, Name_Range, N)))); | |
0da2c8ac AC |
1229 | return Loop_Stm; |
1230 | end if; | |
fbf5a39b AC |
1231 | end Handle_One_Dimension; |
1232 | ||
1233 | ----------------------- | |
1234 | -- Test_Empty_Arrays -- | |
1235 | ----------------------- | |
1236 | ||
1237 | function Test_Empty_Arrays return Node_Id is | |
1238 | Alist : Node_Id; | |
1239 | Blist : Node_Id; | |
1240 | ||
1241 | Atest : Node_Id; | |
1242 | Btest : Node_Id; | |
70482933 | 1243 | |
fbf5a39b AC |
1244 | begin |
1245 | Alist := Empty; | |
1246 | Blist := Empty; | |
0da2c8ac | 1247 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1248 | Atest := |
1249 | Make_Op_Eq (Loc, | |
1250 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1251 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1252 | ||
1253 | Btest := | |
1254 | Make_Op_Eq (Loc, | |
1255 | Left_Opnd => Arr_Attr (B, Name_Length, J), | |
1256 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
1257 | ||
1258 | if No (Alist) then | |
1259 | Alist := Atest; | |
1260 | Blist := Btest; | |
70482933 | 1261 | |
fbf5a39b AC |
1262 | else |
1263 | Alist := | |
1264 | Make_Or_Else (Loc, | |
1265 | Left_Opnd => Relocate_Node (Alist), | |
1266 | Right_Opnd => Atest); | |
1267 | ||
1268 | Blist := | |
1269 | Make_Or_Else (Loc, | |
1270 | Left_Opnd => Relocate_Node (Blist), | |
1271 | Right_Opnd => Btest); | |
1272 | end if; | |
1273 | end loop; | |
70482933 | 1274 | |
fbf5a39b AC |
1275 | return |
1276 | Make_And_Then (Loc, | |
1277 | Left_Opnd => Alist, | |
1278 | Right_Opnd => Blist); | |
1279 | end Test_Empty_Arrays; | |
70482933 | 1280 | |
fbf5a39b AC |
1281 | ----------------------------- |
1282 | -- Test_Lengths_Correspond -- | |
1283 | ----------------------------- | |
70482933 | 1284 | |
fbf5a39b AC |
1285 | function Test_Lengths_Correspond return Node_Id is |
1286 | Result : Node_Id; | |
1287 | Rtest : Node_Id; | |
1288 | ||
1289 | begin | |
1290 | Result := Empty; | |
0da2c8ac | 1291 | for J in 1 .. Number_Dimensions (Ltyp) loop |
fbf5a39b AC |
1292 | Rtest := |
1293 | Make_Op_Ne (Loc, | |
1294 | Left_Opnd => Arr_Attr (A, Name_Length, J), | |
1295 | Right_Opnd => Arr_Attr (B, Name_Length, J)); | |
1296 | ||
1297 | if No (Result) then | |
1298 | Result := Rtest; | |
1299 | else | |
1300 | Result := | |
1301 | Make_Or_Else (Loc, | |
1302 | Left_Opnd => Relocate_Node (Result), | |
1303 | Right_Opnd => Rtest); | |
1304 | end if; | |
1305 | end loop; | |
1306 | ||
1307 | return Result; | |
1308 | end Test_Lengths_Correspond; | |
70482933 RK |
1309 | |
1310 | -- Start of processing for Expand_Array_Equality | |
1311 | ||
1312 | begin | |
0da2c8ac AC |
1313 | Ltyp := Get_Arg_Type (Lhs); |
1314 | Rtyp := Get_Arg_Type (Rhs); | |
1315 | ||
1316 | -- For now, if the argument types are not the same, go to the | |
1317 | -- base type, since the code assumes that the formals have the | |
1318 | -- same type. This is fixable in future ??? | |
1319 | ||
1320 | if Ltyp /= Rtyp then | |
1321 | Ltyp := Base_Type (Ltyp); | |
1322 | Rtyp := Base_Type (Rtyp); | |
1323 | pragma Assert (Ltyp = Rtyp); | |
1324 | end if; | |
1325 | ||
1326 | -- Build list of formals for function | |
1327 | ||
70482933 RK |
1328 | Formals := New_List ( |
1329 | Make_Parameter_Specification (Loc, | |
1330 | Defining_Identifier => A, | |
0da2c8ac | 1331 | Parameter_Type => New_Reference_To (Ltyp, Loc)), |
70482933 RK |
1332 | |
1333 | Make_Parameter_Specification (Loc, | |
1334 | Defining_Identifier => B, | |
0da2c8ac | 1335 | Parameter_Type => New_Reference_To (Rtyp, Loc))); |
70482933 RK |
1336 | |
1337 | Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E')); | |
1338 | ||
fbf5a39b | 1339 | -- Build statement sequence for function |
70482933 RK |
1340 | |
1341 | Func_Body := | |
1342 | Make_Subprogram_Body (Loc, | |
1343 | Specification => | |
1344 | Make_Function_Specification (Loc, | |
1345 | Defining_Unit_Name => Func_Name, | |
1346 | Parameter_Specifications => Formals, | |
1347 | Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)), | |
fbf5a39b AC |
1348 | |
1349 | Declarations => Decls, | |
1350 | ||
70482933 RK |
1351 | Handled_Statement_Sequence => |
1352 | Make_Handled_Sequence_Of_Statements (Loc, | |
1353 | Statements => New_List ( | |
fbf5a39b AC |
1354 | |
1355 | Make_Implicit_If_Statement (Nod, | |
1356 | Condition => Test_Empty_Arrays, | |
1357 | Then_Statements => New_List ( | |
1358 | Make_Return_Statement (Loc, | |
1359 | Expression => | |
1360 | New_Occurrence_Of (Standard_True, Loc)))), | |
1361 | ||
1362 | Make_Implicit_If_Statement (Nod, | |
1363 | Condition => Test_Lengths_Correspond, | |
1364 | Then_Statements => New_List ( | |
1365 | Make_Return_Statement (Loc, | |
1366 | Expression => | |
1367 | New_Occurrence_Of (Standard_False, Loc)))), | |
1368 | ||
0da2c8ac | 1369 | Handle_One_Dimension (1, First_Index (Ltyp)), |
fbf5a39b | 1370 | |
70482933 RK |
1371 | Make_Return_Statement (Loc, |
1372 | Expression => New_Occurrence_Of (Standard_True, Loc))))); | |
1373 | ||
1374 | Set_Has_Completion (Func_Name, True); | |
0da2c8ac | 1375 | Set_Is_Inlined (Func_Name); |
70482933 RK |
1376 | |
1377 | -- If the array type is distinct from the type of the arguments, | |
1378 | -- it is the full view of a private type. Apply an unchecked | |
1379 | -- conversion to insure that analysis of the call succeeds. | |
1380 | ||
0da2c8ac AC |
1381 | declare |
1382 | L, R : Node_Id; | |
1383 | ||
1384 | begin | |
1385 | L := Lhs; | |
1386 | R := Rhs; | |
1387 | ||
1388 | if No (Etype (Lhs)) | |
1389 | or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp) | |
1390 | then | |
1391 | L := OK_Convert_To (Ltyp, Lhs); | |
1392 | end if; | |
1393 | ||
1394 | if No (Etype (Rhs)) | |
1395 | or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp) | |
1396 | then | |
1397 | R := OK_Convert_To (Rtyp, Rhs); | |
1398 | end if; | |
1399 | ||
1400 | Actuals := New_List (L, R); | |
1401 | end; | |
70482933 RK |
1402 | |
1403 | Append_To (Bodies, Func_Body); | |
1404 | ||
1405 | return | |
1406 | Make_Function_Call (Loc, | |
0da2c8ac | 1407 | Name => New_Reference_To (Func_Name, Loc), |
70482933 RK |
1408 | Parameter_Associations => Actuals); |
1409 | end Expand_Array_Equality; | |
1410 | ||
1411 | ----------------------------- | |
1412 | -- Expand_Boolean_Operator -- | |
1413 | ----------------------------- | |
1414 | ||
1415 | -- Note that we first get the actual subtypes of the operands, | |
1416 | -- since we always want to deal with types that have bounds. | |
1417 | ||
1418 | procedure Expand_Boolean_Operator (N : Node_Id) is | |
fbf5a39b | 1419 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
1420 | |
1421 | begin | |
1422 | if Is_Bit_Packed_Array (Typ) then | |
1423 | Expand_Packed_Boolean_Operator (N); | |
1424 | ||
1425 | else | |
fbf5a39b AC |
1426 | -- For the normal non-packed case, the general expansion is |
1427 | -- to build a function for carrying out the comparison (using | |
1428 | -- Make_Boolean_Array_Op) and then inserting it into the tree. | |
1429 | -- The original operator node is then rewritten as a call to | |
1430 | -- this function. | |
70482933 RK |
1431 | |
1432 | declare | |
1433 | Loc : constant Source_Ptr := Sloc (N); | |
1434 | L : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
1435 | R : constant Node_Id := Relocate_Node (Right_Opnd (N)); | |
1436 | Func_Body : Node_Id; | |
1437 | Func_Name : Entity_Id; | |
fbf5a39b | 1438 | |
70482933 RK |
1439 | begin |
1440 | Convert_To_Actual_Subtype (L); | |
1441 | Convert_To_Actual_Subtype (R); | |
1442 | Ensure_Defined (Etype (L), N); | |
1443 | Ensure_Defined (Etype (R), N); | |
1444 | Apply_Length_Check (R, Etype (L)); | |
1445 | ||
fbf5a39b AC |
1446 | if Nkind (Parent (N)) = N_Assignment_Statement |
1447 | and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R) | |
1448 | then | |
1449 | Build_Boolean_Array_Proc_Call (Parent (N), L, R); | |
1450 | ||
1451 | elsif Nkind (Parent (N)) = N_Op_Not | |
1452 | and then Nkind (N) = N_Op_And | |
1453 | and then | |
1454 | Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R) | |
1455 | then | |
1456 | return; | |
1457 | else | |
1458 | ||
1459 | Func_Body := Make_Boolean_Array_Op (Etype (L), N); | |
1460 | Func_Name := Defining_Unit_Name (Specification (Func_Body)); | |
1461 | Insert_Action (N, Func_Body); | |
70482933 | 1462 | |
fbf5a39b | 1463 | -- Now rewrite the expression with a call |
70482933 | 1464 | |
fbf5a39b AC |
1465 | Rewrite (N, |
1466 | Make_Function_Call (Loc, | |
1467 | Name => New_Reference_To (Func_Name, Loc), | |
1468 | Parameter_Associations => | |
1469 | New_List | |
1470 | (L, Make_Type_Conversion | |
70482933 RK |
1471 | (Loc, New_Reference_To (Etype (L), Loc), R)))); |
1472 | ||
fbf5a39b AC |
1473 | Analyze_And_Resolve (N, Typ); |
1474 | end if; | |
70482933 RK |
1475 | end; |
1476 | end if; | |
1477 | end Expand_Boolean_Operator; | |
1478 | ||
1479 | ------------------------------- | |
1480 | -- Expand_Composite_Equality -- | |
1481 | ------------------------------- | |
1482 | ||
1483 | -- This function is only called for comparing internal fields of composite | |
1484 | -- types when these fields are themselves composites. This is a special | |
1485 | -- case because it is not possible to respect normal Ada visibility rules. | |
1486 | ||
1487 | function Expand_Composite_Equality | |
1488 | (Nod : Node_Id; | |
1489 | Typ : Entity_Id; | |
1490 | Lhs : Node_Id; | |
1491 | Rhs : Node_Id; | |
2e071734 | 1492 | Bodies : List_Id) return Node_Id |
70482933 RK |
1493 | is |
1494 | Loc : constant Source_Ptr := Sloc (Nod); | |
1495 | Full_Type : Entity_Id; | |
1496 | Prim : Elmt_Id; | |
1497 | Eq_Op : Entity_Id; | |
1498 | ||
1499 | begin | |
1500 | if Is_Private_Type (Typ) then | |
1501 | Full_Type := Underlying_Type (Typ); | |
1502 | else | |
1503 | Full_Type := Typ; | |
1504 | end if; | |
1505 | ||
1506 | -- Defense against malformed private types with no completion | |
1507 | -- the error will be diagnosed later by check_completion | |
1508 | ||
1509 | if No (Full_Type) then | |
1510 | return New_Reference_To (Standard_False, Loc); | |
1511 | end if; | |
1512 | ||
1513 | Full_Type := Base_Type (Full_Type); | |
1514 | ||
1515 | if Is_Array_Type (Full_Type) then | |
1516 | ||
1517 | -- If the operand is an elementary type other than a floating-point | |
1518 | -- type, then we can simply use the built-in block bitwise equality, | |
1519 | -- since the predefined equality operators always apply and bitwise | |
1520 | -- equality is fine for all these cases. | |
1521 | ||
1522 | if Is_Elementary_Type (Component_Type (Full_Type)) | |
1523 | and then not Is_Floating_Point_Type (Component_Type (Full_Type)) | |
1524 | then | |
1525 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
1526 | ||
1527 | -- For composite component types, and floating-point types, use | |
1528 | -- the expansion. This deals with tagged component types (where | |
1529 | -- we use the applicable equality routine) and floating-point, | |
1530 | -- (where we need to worry about negative zeroes), and also the | |
1531 | -- case of any composite type recursively containing such fields. | |
1532 | ||
1533 | else | |
0da2c8ac | 1534 | return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type); |
70482933 RK |
1535 | end if; |
1536 | ||
1537 | elsif Is_Tagged_Type (Full_Type) then | |
1538 | ||
1539 | -- Call the primitive operation "=" of this type | |
1540 | ||
1541 | if Is_Class_Wide_Type (Full_Type) then | |
1542 | Full_Type := Root_Type (Full_Type); | |
1543 | end if; | |
1544 | ||
1545 | -- If this is derived from an untagged private type completed | |
1546 | -- with a tagged type, it does not have a full view, so we | |
1547 | -- use the primitive operations of the private type. | |
1548 | -- This check should no longer be necessary when these | |
1549 | -- types receive their full views ??? | |
1550 | ||
1551 | if Is_Private_Type (Typ) | |
1552 | and then not Is_Tagged_Type (Typ) | |
1553 | and then not Is_Controlled (Typ) | |
1554 | and then Is_Derived_Type (Typ) | |
1555 | and then No (Full_View (Typ)) | |
1556 | then | |
1557 | Prim := First_Elmt (Collect_Primitive_Operations (Typ)); | |
1558 | else | |
1559 | Prim := First_Elmt (Primitive_Operations (Full_Type)); | |
1560 | end if; | |
1561 | ||
1562 | loop | |
1563 | Eq_Op := Node (Prim); | |
1564 | exit when Chars (Eq_Op) = Name_Op_Eq | |
1565 | and then Etype (First_Formal (Eq_Op)) = | |
e6f69614 AC |
1566 | Etype (Next_Formal (First_Formal (Eq_Op))) |
1567 | and then Base_Type (Etype (Eq_Op)) = Standard_Boolean; | |
70482933 RK |
1568 | Next_Elmt (Prim); |
1569 | pragma Assert (Present (Prim)); | |
1570 | end loop; | |
1571 | ||
1572 | Eq_Op := Node (Prim); | |
1573 | ||
1574 | return | |
1575 | Make_Function_Call (Loc, | |
1576 | Name => New_Reference_To (Eq_Op, Loc), | |
1577 | Parameter_Associations => | |
1578 | New_List | |
1579 | (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs), | |
1580 | Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs))); | |
1581 | ||
1582 | elsif Is_Record_Type (Full_Type) then | |
fbf5a39b | 1583 | Eq_Op := TSS (Full_Type, TSS_Composite_Equality); |
70482933 RK |
1584 | |
1585 | if Present (Eq_Op) then | |
1586 | if Etype (First_Formal (Eq_Op)) /= Full_Type then | |
1587 | ||
1588 | -- Inherited equality from parent type. Convert the actuals | |
1589 | -- to match signature of operation. | |
1590 | ||
1591 | declare | |
fbf5a39b | 1592 | T : constant Entity_Id := Etype (First_Formal (Eq_Op)); |
70482933 RK |
1593 | |
1594 | begin | |
1595 | return | |
1596 | Make_Function_Call (Loc, | |
1597 | Name => New_Reference_To (Eq_Op, Loc), | |
1598 | Parameter_Associations => | |
1599 | New_List (OK_Convert_To (T, Lhs), | |
1600 | OK_Convert_To (T, Rhs))); | |
1601 | end; | |
1602 | ||
1603 | else | |
5d09245e AC |
1604 | -- Comparison between Unchecked_Union components |
1605 | ||
1606 | if Is_Unchecked_Union (Full_Type) then | |
1607 | declare | |
1608 | Lhs_Type : Node_Id := Full_Type; | |
1609 | Rhs_Type : Node_Id := Full_Type; | |
1610 | Lhs_Discr_Val : Node_Id; | |
1611 | Rhs_Discr_Val : Node_Id; | |
1612 | ||
1613 | begin | |
1614 | -- Lhs subtype | |
1615 | ||
1616 | if Nkind (Lhs) = N_Selected_Component then | |
1617 | Lhs_Type := Etype (Entity (Selector_Name (Lhs))); | |
1618 | end if; | |
1619 | ||
1620 | -- Rhs subtype | |
1621 | ||
1622 | if Nkind (Rhs) = N_Selected_Component then | |
1623 | Rhs_Type := Etype (Entity (Selector_Name (Rhs))); | |
1624 | end if; | |
1625 | ||
1626 | -- Lhs of the composite equality | |
1627 | ||
1628 | if Is_Constrained (Lhs_Type) then | |
1629 | ||
1630 | -- Since the enclosing record can never be an | |
1631 | -- Unchecked_Union (this code is executed for records | |
1632 | -- that do not have variants), we may reference its | |
1633 | -- discriminant(s). | |
1634 | ||
1635 | if Nkind (Lhs) = N_Selected_Component | |
1636 | and then Has_Per_Object_Constraint ( | |
1637 | Entity (Selector_Name (Lhs))) | |
1638 | then | |
1639 | Lhs_Discr_Val := | |
1640 | Make_Selected_Component (Loc, | |
1641 | Prefix => Prefix (Lhs), | |
1642 | Selector_Name => | |
1643 | New_Copy ( | |
1644 | Get_Discriminant_Value ( | |
1645 | First_Discriminant (Lhs_Type), | |
1646 | Lhs_Type, | |
1647 | Stored_Constraint (Lhs_Type)))); | |
1648 | ||
1649 | else | |
1650 | Lhs_Discr_Val := New_Copy ( | |
1651 | Get_Discriminant_Value ( | |
1652 | First_Discriminant (Lhs_Type), | |
1653 | Lhs_Type, | |
1654 | Stored_Constraint (Lhs_Type))); | |
1655 | ||
1656 | end if; | |
1657 | else | |
1658 | -- It is not possible to infer the discriminant since | |
1659 | -- the subtype is not constrained. | |
1660 | ||
8aceda64 | 1661 | return |
5d09245e | 1662 | Make_Raise_Program_Error (Loc, |
8aceda64 | 1663 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
1664 | end if; |
1665 | ||
1666 | -- Rhs of the composite equality | |
1667 | ||
1668 | if Is_Constrained (Rhs_Type) then | |
1669 | if Nkind (Rhs) = N_Selected_Component | |
1670 | and then Has_Per_Object_Constraint ( | |
1671 | Entity (Selector_Name (Rhs))) | |
1672 | then | |
1673 | Rhs_Discr_Val := | |
1674 | Make_Selected_Component (Loc, | |
1675 | Prefix => Prefix (Rhs), | |
1676 | Selector_Name => | |
1677 | New_Copy ( | |
1678 | Get_Discriminant_Value ( | |
1679 | First_Discriminant (Rhs_Type), | |
1680 | Rhs_Type, | |
1681 | Stored_Constraint (Rhs_Type)))); | |
1682 | ||
1683 | else | |
1684 | Rhs_Discr_Val := New_Copy ( | |
1685 | Get_Discriminant_Value ( | |
1686 | First_Discriminant (Rhs_Type), | |
1687 | Rhs_Type, | |
1688 | Stored_Constraint (Rhs_Type))); | |
1689 | ||
1690 | end if; | |
1691 | else | |
8aceda64 | 1692 | return |
5d09245e | 1693 | Make_Raise_Program_Error (Loc, |
8aceda64 | 1694 | Reason => PE_Unchecked_Union_Restriction); |
5d09245e AC |
1695 | end if; |
1696 | ||
1697 | -- Call the TSS equality function with the inferred | |
1698 | -- discriminant values. | |
1699 | ||
1700 | return | |
1701 | Make_Function_Call (Loc, | |
1702 | Name => New_Reference_To (Eq_Op, Loc), | |
1703 | Parameter_Associations => New_List ( | |
1704 | Lhs, | |
1705 | Rhs, | |
1706 | Lhs_Discr_Val, | |
1707 | Rhs_Discr_Val)); | |
1708 | end; | |
1709 | end if; | |
1710 | ||
1711 | -- Shouldn't this be an else, we can't fall through | |
1712 | -- the above IF, right??? | |
1713 | ||
70482933 RK |
1714 | return |
1715 | Make_Function_Call (Loc, | |
1716 | Name => New_Reference_To (Eq_Op, Loc), | |
1717 | Parameter_Associations => New_List (Lhs, Rhs)); | |
1718 | end if; | |
1719 | ||
1720 | else | |
1721 | return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies); | |
1722 | end if; | |
1723 | ||
1724 | else | |
1725 | -- It can be a simple record or the full view of a scalar private | |
1726 | ||
1727 | return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs); | |
1728 | end if; | |
1729 | end Expand_Composite_Equality; | |
1730 | ||
1731 | ------------------------------ | |
1732 | -- Expand_Concatenate_Other -- | |
1733 | ------------------------------ | |
1734 | ||
1735 | -- Let n be the number of array operands to be concatenated, Base_Typ | |
1736 | -- their base type, Ind_Typ their index type, and Arr_Typ the original | |
1737 | -- array type to which the concatenantion operator applies, then the | |
1738 | -- following subprogram is constructed: | |
6c1e24d3 | 1739 | |
70482933 RK |
1740 | -- [function Cnn (S1 : Base_Typ; ...; Sn : Base_Typ) return Base_Typ is |
1741 | -- L : Ind_Typ; | |
1742 | -- begin | |
1743 | -- if S1'Length /= 0 then | |
1744 | -- L := XXX; --> XXX = S1'First if Arr_Typ is unconstrained | |
1745 | -- XXX = Arr_Typ'First otherwise | |
1746 | -- elsif S2'Length /= 0 then | |
1747 | -- L := YYY; --> YYY = S2'First if Arr_Typ is unconstrained | |
1748 | -- YYY = Arr_Typ'First otherwise | |
1749 | -- ... | |
1750 | -- elsif Sn-1'Length /= 0 then | |
1751 | -- L := ZZZ; --> ZZZ = Sn-1'First if Arr_Typ is unconstrained | |
1752 | -- ZZZ = Arr_Typ'First otherwise | |
1753 | -- else | |
1754 | -- return Sn; | |
1755 | -- end if; | |
6c1e24d3 | 1756 | |
70482933 RK |
1757 | -- declare |
1758 | -- P : Ind_Typ; | |
1759 | -- H : Ind_Typ := | |
1760 | -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length) | |
1761 | -- + Ind_Typ'Pos (L)); | |
1762 | -- R : Base_Typ (L .. H); | |
1763 | -- begin | |
1764 | -- if S1'Length /= 0 then | |
1765 | -- P := S1'First; | |
1766 | -- loop | |
1767 | -- R (L) := S1 (P); | |
1768 | -- L := Ind_Typ'Succ (L); | |
1769 | -- exit when P = S1'Last; | |
1770 | -- P := Ind_Typ'Succ (P); | |
1771 | -- end loop; | |
1772 | -- end if; | |
1773 | -- | |
1774 | -- if S2'Length /= 0 then | |
1775 | -- L := Ind_Typ'Succ (L); | |
1776 | -- loop | |
1777 | -- R (L) := S2 (P); | |
1778 | -- L := Ind_Typ'Succ (L); | |
1779 | -- exit when P = S2'Last; | |
1780 | -- P := Ind_Typ'Succ (P); | |
1781 | -- end loop; | |
1782 | -- end if; | |
6c1e24d3 | 1783 | |
2717634d | 1784 | -- . . . |
6c1e24d3 | 1785 | |
70482933 RK |
1786 | -- if Sn'Length /= 0 then |
1787 | -- P := Sn'First; | |
1788 | -- loop | |
1789 | -- R (L) := Sn (P); | |
1790 | -- L := Ind_Typ'Succ (L); | |
1791 | -- exit when P = Sn'Last; | |
1792 | -- P := Ind_Typ'Succ (P); | |
1793 | -- end loop; | |
1794 | -- end if; | |
6c1e24d3 | 1795 | |
70482933 RK |
1796 | -- return R; |
1797 | -- end; | |
1798 | -- end Cnn;] | |
1799 | ||
1800 | procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id) is | |
1801 | Loc : constant Source_Ptr := Sloc (Cnode); | |
1802 | Nb_Opnds : constant Nat := List_Length (Opnds); | |
1803 | ||
1804 | Arr_Typ : constant Entity_Id := Etype (Entity (Cnode)); | |
1805 | Base_Typ : constant Entity_Id := Base_Type (Etype (Cnode)); | |
1806 | Ind_Typ : constant Entity_Id := Etype (First_Index (Base_Typ)); | |
1807 | ||
1808 | Func_Id : Node_Id; | |
1809 | Func_Spec : Node_Id; | |
1810 | Param_Specs : List_Id; | |
1811 | ||
1812 | Func_Body : Node_Id; | |
1813 | Func_Decls : List_Id; | |
1814 | Func_Stmts : List_Id; | |
1815 | ||
1816 | L_Decl : Node_Id; | |
1817 | ||
1818 | If_Stmt : Node_Id; | |
1819 | Elsif_List : List_Id; | |
1820 | ||
1821 | Declare_Block : Node_Id; | |
1822 | Declare_Decls : List_Id; | |
1823 | Declare_Stmts : List_Id; | |
1824 | ||
1825 | H_Decl : Node_Id; | |
1826 | H_Init : Node_Id; | |
1827 | P_Decl : Node_Id; | |
1828 | R_Decl : Node_Id; | |
1829 | R_Constr : Node_Id; | |
1830 | R_Range : Node_Id; | |
1831 | ||
1832 | Params : List_Id; | |
1833 | Operand : Node_Id; | |
1834 | ||
fbf5a39b | 1835 | function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id; |
70482933 RK |
1836 | -- Builds the sequence of statement: |
1837 | -- P := Si'First; | |
1838 | -- loop | |
1839 | -- R (L) := Si (P); | |
1840 | -- L := Ind_Typ'Succ (L); | |
1841 | -- exit when P = Si'Last; | |
1842 | -- P := Ind_Typ'Succ (P); | |
1843 | -- end loop; | |
1844 | -- | |
1845 | -- where i is the input parameter I given. | |
fbf5a39b AC |
1846 | -- If the flag Last is true, the exit statement is emitted before |
1847 | -- incrementing the lower bound, to prevent the creation out of | |
1848 | -- bound values. | |
70482933 RK |
1849 | |
1850 | function Init_L (I : Nat) return Node_Id; | |
1851 | -- Builds the statement: | |
1852 | -- L := Arr_Typ'First; If Arr_Typ is constrained | |
1853 | -- L := Si'First; otherwise (where I is the input param given) | |
1854 | ||
1855 | function H return Node_Id; | |
5e1c00fa | 1856 | -- Builds reference to identifier H |
70482933 RK |
1857 | |
1858 | function Ind_Val (E : Node_Id) return Node_Id; | |
1859 | -- Builds expression Ind_Typ'Val (E); | |
1860 | ||
1861 | function L return Node_Id; | |
5e1c00fa | 1862 | -- Builds reference to identifier L |
70482933 RK |
1863 | |
1864 | function L_Pos return Node_Id; | |
5e1c00fa RD |
1865 | -- Builds expression Integer_Type'(Ind_Typ'Pos (L)). We qualify the |
1866 | -- expression to avoid universal_integer computations whenever possible, | |
1867 | -- in the expression for the upper bound H. | |
70482933 RK |
1868 | |
1869 | function L_Succ return Node_Id; | |
5e1c00fa | 1870 | -- Builds expression Ind_Typ'Succ (L) |
70482933 RK |
1871 | |
1872 | function One return Node_Id; | |
5e1c00fa | 1873 | -- Builds integer literal one |
70482933 RK |
1874 | |
1875 | function P return Node_Id; | |
5e1c00fa | 1876 | -- Builds reference to identifier P |
70482933 RK |
1877 | |
1878 | function P_Succ return Node_Id; | |
5e1c00fa | 1879 | -- Builds expression Ind_Typ'Succ (P) |
70482933 RK |
1880 | |
1881 | function R return Node_Id; | |
5e1c00fa | 1882 | -- Builds reference to identifier R |
70482933 RK |
1883 | |
1884 | function S (I : Nat) return Node_Id; | |
5e1c00fa | 1885 | -- Builds reference to identifier Si, where I is the value given |
70482933 RK |
1886 | |
1887 | function S_First (I : Nat) return Node_Id; | |
5e1c00fa | 1888 | -- Builds expression Si'First, where I is the value given |
70482933 RK |
1889 | |
1890 | function S_Last (I : Nat) return Node_Id; | |
5e1c00fa | 1891 | -- Builds expression Si'Last, where I is the value given |
70482933 RK |
1892 | |
1893 | function S_Length (I : Nat) return Node_Id; | |
5e1c00fa | 1894 | -- Builds expression Si'Length, where I is the value given |
70482933 RK |
1895 | |
1896 | function S_Length_Test (I : Nat) return Node_Id; | |
5e1c00fa | 1897 | -- Builds expression Si'Length /= 0, where I is the value given |
70482933 RK |
1898 | |
1899 | ------------------- | |
1900 | -- Copy_Into_R_S -- | |
1901 | ------------------- | |
1902 | ||
fbf5a39b AC |
1903 | function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id is |
1904 | Stmts : constant List_Id := New_List; | |
70482933 RK |
1905 | P_Start : Node_Id; |
1906 | Loop_Stmt : Node_Id; | |
1907 | R_Copy : Node_Id; | |
1908 | Exit_Stmt : Node_Id; | |
1909 | L_Inc : Node_Id; | |
1910 | P_Inc : Node_Id; | |
1911 | ||
1912 | begin | |
1913 | -- First construct the initializations | |
1914 | ||
1915 | P_Start := Make_Assignment_Statement (Loc, | |
1916 | Name => P, | |
1917 | Expression => S_First (I)); | |
1918 | Append_To (Stmts, P_Start); | |
1919 | ||
1920 | -- Then build the loop | |
1921 | ||
1922 | R_Copy := Make_Assignment_Statement (Loc, | |
1923 | Name => Make_Indexed_Component (Loc, | |
1924 | Prefix => R, | |
1925 | Expressions => New_List (L)), | |
1926 | Expression => Make_Indexed_Component (Loc, | |
1927 | Prefix => S (I), | |
1928 | Expressions => New_List (P))); | |
1929 | ||
1930 | L_Inc := Make_Assignment_Statement (Loc, | |
1931 | Name => L, | |
1932 | Expression => L_Succ); | |
1933 | ||
1934 | Exit_Stmt := Make_Exit_Statement (Loc, | |
1935 | Condition => Make_Op_Eq (Loc, P, S_Last (I))); | |
1936 | ||
1937 | P_Inc := Make_Assignment_Statement (Loc, | |
1938 | Name => P, | |
1939 | Expression => P_Succ); | |
1940 | ||
fbf5a39b AC |
1941 | if Last then |
1942 | Loop_Stmt := | |
1943 | Make_Implicit_Loop_Statement (Cnode, | |
1944 | Statements => New_List (R_Copy, Exit_Stmt, L_Inc, P_Inc)); | |
1945 | else | |
1946 | Loop_Stmt := | |
1947 | Make_Implicit_Loop_Statement (Cnode, | |
1948 | Statements => New_List (R_Copy, L_Inc, Exit_Stmt, P_Inc)); | |
1949 | end if; | |
70482933 RK |
1950 | |
1951 | Append_To (Stmts, Loop_Stmt); | |
1952 | ||
1953 | return Stmts; | |
1954 | end Copy_Into_R_S; | |
1955 | ||
1956 | ------- | |
1957 | -- H -- | |
1958 | ------- | |
1959 | ||
1960 | function H return Node_Id is | |
1961 | begin | |
1962 | return Make_Identifier (Loc, Name_uH); | |
1963 | end H; | |
1964 | ||
1965 | ------------- | |
1966 | -- Ind_Val -- | |
1967 | ------------- | |
1968 | ||
1969 | function Ind_Val (E : Node_Id) return Node_Id is | |
1970 | begin | |
1971 | return | |
1972 | Make_Attribute_Reference (Loc, | |
1973 | Prefix => New_Reference_To (Ind_Typ, Loc), | |
1974 | Attribute_Name => Name_Val, | |
1975 | Expressions => New_List (E)); | |
1976 | end Ind_Val; | |
1977 | ||
1978 | ------------ | |
1979 | -- Init_L -- | |
1980 | ------------ | |
1981 | ||
1982 | function Init_L (I : Nat) return Node_Id is | |
1983 | E : Node_Id; | |
1984 | ||
1985 | begin | |
1986 | if Is_Constrained (Arr_Typ) then | |
1987 | E := Make_Attribute_Reference (Loc, | |
1988 | Prefix => New_Reference_To (Arr_Typ, Loc), | |
1989 | Attribute_Name => Name_First); | |
1990 | ||
1991 | else | |
1992 | E := S_First (I); | |
1993 | end if; | |
1994 | ||
1995 | return Make_Assignment_Statement (Loc, Name => L, Expression => E); | |
1996 | end Init_L; | |
1997 | ||
1998 | ------- | |
1999 | -- L -- | |
2000 | ------- | |
2001 | ||
2002 | function L return Node_Id is | |
2003 | begin | |
2004 | return Make_Identifier (Loc, Name_uL); | |
2005 | end L; | |
2006 | ||
2007 | ----------- | |
2008 | -- L_Pos -- | |
2009 | ----------- | |
2010 | ||
2011 | function L_Pos return Node_Id is | |
6c1e24d3 AC |
2012 | Target_Type : Entity_Id; |
2013 | ||
70482933 | 2014 | begin |
6c1e24d3 AC |
2015 | -- If the index type is an enumeration type, the computation |
2016 | -- can be done in standard integer. Otherwise, choose a large | |
2017 | -- enough integer type. | |
2018 | ||
2019 | if Is_Enumeration_Type (Ind_Typ) | |
2020 | or else Root_Type (Ind_Typ) = Standard_Integer | |
2021 | or else Root_Type (Ind_Typ) = Standard_Short_Integer | |
2022 | or else Root_Type (Ind_Typ) = Standard_Short_Short_Integer | |
2023 | then | |
2024 | Target_Type := Standard_Integer; | |
2025 | else | |
2026 | Target_Type := Root_Type (Ind_Typ); | |
2027 | end if; | |
2028 | ||
70482933 | 2029 | return |
6c1e24d3 AC |
2030 | Make_Qualified_Expression (Loc, |
2031 | Subtype_Mark => New_Reference_To (Target_Type, Loc), | |
2032 | Expression => | |
2033 | Make_Attribute_Reference (Loc, | |
2034 | Prefix => New_Reference_To (Ind_Typ, Loc), | |
2035 | Attribute_Name => Name_Pos, | |
2036 | Expressions => New_List (L))); | |
70482933 RK |
2037 | end L_Pos; |
2038 | ||
2039 | ------------ | |
2040 | -- L_Succ -- | |
2041 | ------------ | |
2042 | ||
2043 | function L_Succ return Node_Id is | |
2044 | begin | |
2045 | return | |
2046 | Make_Attribute_Reference (Loc, | |
2047 | Prefix => New_Reference_To (Ind_Typ, Loc), | |
2048 | Attribute_Name => Name_Succ, | |
2049 | Expressions => New_List (L)); | |
2050 | end L_Succ; | |
2051 | ||
2052 | --------- | |
2053 | -- One -- | |
2054 | --------- | |
2055 | ||
2056 | function One return Node_Id is | |
2057 | begin | |
2058 | return Make_Integer_Literal (Loc, 1); | |
2059 | end One; | |
2060 | ||
2061 | ------- | |
2062 | -- P -- | |
2063 | ------- | |
2064 | ||
2065 | function P return Node_Id is | |
2066 | begin | |
2067 | return Make_Identifier (Loc, Name_uP); | |
2068 | end P; | |
2069 | ||
2070 | ------------ | |
2071 | -- P_Succ -- | |
2072 | ------------ | |
2073 | ||
2074 | function P_Succ return Node_Id is | |
2075 | begin | |
2076 | return | |
2077 | Make_Attribute_Reference (Loc, | |
2078 | Prefix => New_Reference_To (Ind_Typ, Loc), | |
2079 | Attribute_Name => Name_Succ, | |
2080 | Expressions => New_List (P)); | |
2081 | end P_Succ; | |
2082 | ||
2083 | ------- | |
2084 | -- R -- | |
2085 | ------- | |
2086 | ||
2087 | function R return Node_Id is | |
2088 | begin | |
2089 | return Make_Identifier (Loc, Name_uR); | |
2090 | end R; | |
2091 | ||
2092 | ------- | |
2093 | -- S -- | |
2094 | ------- | |
2095 | ||
2096 | function S (I : Nat) return Node_Id is | |
2097 | begin | |
2098 | return Make_Identifier (Loc, New_External_Name ('S', I)); | |
2099 | end S; | |
2100 | ||
2101 | ------------- | |
2102 | -- S_First -- | |
2103 | ------------- | |
2104 | ||
2105 | function S_First (I : Nat) return Node_Id is | |
2106 | begin | |
2107 | return Make_Attribute_Reference (Loc, | |
2108 | Prefix => S (I), | |
2109 | Attribute_Name => Name_First); | |
2110 | end S_First; | |
2111 | ||
2112 | ------------ | |
2113 | -- S_Last -- | |
2114 | ------------ | |
2115 | ||
2116 | function S_Last (I : Nat) return Node_Id is | |
2117 | begin | |
2118 | return Make_Attribute_Reference (Loc, | |
2119 | Prefix => S (I), | |
2120 | Attribute_Name => Name_Last); | |
2121 | end S_Last; | |
2122 | ||
2123 | -------------- | |
2124 | -- S_Length -- | |
2125 | -------------- | |
2126 | ||
2127 | function S_Length (I : Nat) return Node_Id is | |
2128 | begin | |
2129 | return Make_Attribute_Reference (Loc, | |
2130 | Prefix => S (I), | |
2131 | Attribute_Name => Name_Length); | |
2132 | end S_Length; | |
2133 | ||
2134 | ------------------- | |
2135 | -- S_Length_Test -- | |
2136 | ------------------- | |
2137 | ||
2138 | function S_Length_Test (I : Nat) return Node_Id is | |
2139 | begin | |
2140 | return | |
2141 | Make_Op_Ne (Loc, | |
2142 | Left_Opnd => S_Length (I), | |
2143 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
2144 | end S_Length_Test; | |
2145 | ||
2146 | -- Start of processing for Expand_Concatenate_Other | |
2147 | ||
2148 | begin | |
2149 | -- Construct the parameter specs and the overall function spec | |
2150 | ||
2151 | Param_Specs := New_List; | |
2152 | for I in 1 .. Nb_Opnds loop | |
2153 | Append_To | |
2154 | (Param_Specs, | |
2155 | Make_Parameter_Specification (Loc, | |
2156 | Defining_Identifier => | |
2157 | Make_Defining_Identifier (Loc, New_External_Name ('S', I)), | |
2158 | Parameter_Type => New_Reference_To (Base_Typ, Loc))); | |
2159 | end loop; | |
2160 | ||
2161 | Func_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('C')); | |
2162 | Func_Spec := | |
2163 | Make_Function_Specification (Loc, | |
2164 | Defining_Unit_Name => Func_Id, | |
2165 | Parameter_Specifications => Param_Specs, | |
2166 | Subtype_Mark => New_Reference_To (Base_Typ, Loc)); | |
2167 | ||
2168 | -- Construct L's object declaration | |
2169 | ||
2170 | L_Decl := | |
2171 | Make_Object_Declaration (Loc, | |
2172 | Defining_Identifier => Make_Defining_Identifier (Loc, Name_uL), | |
2173 | Object_Definition => New_Reference_To (Ind_Typ, Loc)); | |
2174 | ||
2175 | Func_Decls := New_List (L_Decl); | |
2176 | ||
2177 | -- Construct the if-then-elsif statements | |
2178 | ||
2179 | Elsif_List := New_List; | |
2180 | for I in 2 .. Nb_Opnds - 1 loop | |
2181 | Append_To (Elsif_List, Make_Elsif_Part (Loc, | |
2182 | Condition => S_Length_Test (I), | |
2183 | Then_Statements => New_List (Init_L (I)))); | |
2184 | end loop; | |
2185 | ||
2186 | If_Stmt := | |
2187 | Make_Implicit_If_Statement (Cnode, | |
2188 | Condition => S_Length_Test (1), | |
2189 | Then_Statements => New_List (Init_L (1)), | |
2190 | Elsif_Parts => Elsif_List, | |
2191 | Else_Statements => New_List (Make_Return_Statement (Loc, | |
2192 | Expression => S (Nb_Opnds)))); | |
2193 | ||
2194 | -- Construct the declaration for H | |
2195 | ||
2196 | P_Decl := | |
2197 | Make_Object_Declaration (Loc, | |
2198 | Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP), | |
2199 | Object_Definition => New_Reference_To (Ind_Typ, Loc)); | |
2200 | ||
2201 | H_Init := Make_Op_Subtract (Loc, S_Length (1), One); | |
2202 | for I in 2 .. Nb_Opnds loop | |
2203 | H_Init := Make_Op_Add (Loc, H_Init, S_Length (I)); | |
2204 | end loop; | |
2205 | H_Init := Ind_Val (Make_Op_Add (Loc, H_Init, L_Pos)); | |
2206 | ||
2207 | H_Decl := | |
2208 | Make_Object_Declaration (Loc, | |
2209 | Defining_Identifier => Make_Defining_Identifier (Loc, Name_uH), | |
2210 | Object_Definition => New_Reference_To (Ind_Typ, Loc), | |
2211 | Expression => H_Init); | |
2212 | ||
2213 | -- Construct the declaration for R | |
2214 | ||
2215 | R_Range := Make_Range (Loc, Low_Bound => L, High_Bound => H); | |
2216 | R_Constr := | |
2217 | Make_Index_Or_Discriminant_Constraint (Loc, | |
2218 | Constraints => New_List (R_Range)); | |
2219 | ||
2220 | R_Decl := | |
2221 | Make_Object_Declaration (Loc, | |
2222 | Defining_Identifier => Make_Defining_Identifier (Loc, Name_uR), | |
2223 | Object_Definition => | |
2224 | Make_Subtype_Indication (Loc, | |
2225 | Subtype_Mark => New_Reference_To (Base_Typ, Loc), | |
2226 | Constraint => R_Constr)); | |
2227 | ||
2228 | -- Construct the declarations for the declare block | |
2229 | ||
2230 | Declare_Decls := New_List (P_Decl, H_Decl, R_Decl); | |
2231 | ||
2232 | -- Construct list of statements for the declare block | |
2233 | ||
2234 | Declare_Stmts := New_List; | |
2235 | for I in 1 .. Nb_Opnds loop | |
2236 | Append_To (Declare_Stmts, | |
2237 | Make_Implicit_If_Statement (Cnode, | |
2238 | Condition => S_Length_Test (I), | |
fbf5a39b | 2239 | Then_Statements => Copy_Into_R_S (I, I = Nb_Opnds))); |
70482933 RK |
2240 | end loop; |
2241 | ||
2242 | Append_To (Declare_Stmts, Make_Return_Statement (Loc, Expression => R)); | |
2243 | ||
2244 | -- Construct the declare block | |
2245 | ||
2246 | Declare_Block := Make_Block_Statement (Loc, | |
2247 | Declarations => Declare_Decls, | |
2248 | Handled_Statement_Sequence => | |
2249 | Make_Handled_Sequence_Of_Statements (Loc, Declare_Stmts)); | |
2250 | ||
2251 | -- Construct the list of function statements | |
2252 | ||
2253 | Func_Stmts := New_List (If_Stmt, Declare_Block); | |
2254 | ||
2255 | -- Construct the function body | |
2256 | ||
2257 | Func_Body := | |
2258 | Make_Subprogram_Body (Loc, | |
2259 | Specification => Func_Spec, | |
2260 | Declarations => Func_Decls, | |
2261 | Handled_Statement_Sequence => | |
2262 | Make_Handled_Sequence_Of_Statements (Loc, Func_Stmts)); | |
2263 | ||
2264 | -- Insert the newly generated function in the code. This is analyzed | |
2265 | -- with all checks off, since we have completed all the checks. | |
2266 | ||
2267 | -- Note that this does *not* fix the array concatenation bug when the | |
2268 | -- low bound is Integer'first sibce that bug comes from the pointer | |
44d6a706 | 2269 | -- dereferencing an unconstrained array. An there we need a constraint |
70482933 RK |
2270 | -- check to make sure the length of the concatenated array is ok. ??? |
2271 | ||
2272 | Insert_Action (Cnode, Func_Body, Suppress => All_Checks); | |
2273 | ||
2274 | -- Construct list of arguments for the function call | |
2275 | ||
2276 | Params := New_List; | |
2277 | Operand := First (Opnds); | |
2278 | for I in 1 .. Nb_Opnds loop | |
2279 | Append_To (Params, Relocate_Node (Operand)); | |
2280 | Next (Operand); | |
2281 | end loop; | |
2282 | ||
2283 | -- Insert the function call | |
2284 | ||
2285 | Rewrite | |
2286 | (Cnode, | |
2287 | Make_Function_Call (Loc, New_Reference_To (Func_Id, Loc), Params)); | |
2288 | ||
2289 | Analyze_And_Resolve (Cnode, Base_Typ); | |
2290 | Set_Is_Inlined (Func_Id); | |
2291 | end Expand_Concatenate_Other; | |
2292 | ||
2293 | ------------------------------- | |
2294 | -- Expand_Concatenate_String -- | |
2295 | ------------------------------- | |
2296 | ||
2297 | procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id) is | |
2298 | Loc : constant Source_Ptr := Sloc (Cnode); | |
2299 | Opnd1 : constant Node_Id := First (Opnds); | |
2300 | Opnd2 : constant Node_Id := Next (Opnd1); | |
2301 | Typ1 : constant Entity_Id := Base_Type (Etype (Opnd1)); | |
2302 | Typ2 : constant Entity_Id := Base_Type (Etype (Opnd2)); | |
2303 | ||
2304 | R : RE_Id; | |
2305 | -- RE_Id value for function to be called | |
2306 | ||
2307 | begin | |
2308 | -- In all cases, we build a call to a routine giving the list of | |
2309 | -- arguments as the parameter list to the routine. | |
2310 | ||
2311 | case List_Length (Opnds) is | |
2312 | when 2 => | |
2313 | if Typ1 = Standard_Character then | |
2314 | if Typ2 = Standard_Character then | |
2315 | R := RE_Str_Concat_CC; | |
2316 | ||
2317 | else | |
2318 | pragma Assert (Typ2 = Standard_String); | |
2319 | R := RE_Str_Concat_CS; | |
2320 | end if; | |
2321 | ||
2322 | elsif Typ1 = Standard_String then | |
2323 | if Typ2 = Standard_Character then | |
2324 | R := RE_Str_Concat_SC; | |
2325 | ||
2326 | else | |
2327 | pragma Assert (Typ2 = Standard_String); | |
2328 | R := RE_Str_Concat; | |
2329 | end if; | |
2330 | ||
2331 | -- If we have anything other than Standard_Character or | |
07fc65c4 GB |
2332 | -- Standard_String, then we must have had a serious error |
2333 | -- earlier, so we just abandon the attempt at expansion. | |
70482933 RK |
2334 | |
2335 | else | |
07fc65c4 | 2336 | pragma Assert (Serious_Errors_Detected > 0); |
70482933 RK |
2337 | return; |
2338 | end if; | |
2339 | ||
2340 | when 3 => | |
2341 | R := RE_Str_Concat_3; | |
2342 | ||
2343 | when 4 => | |
2344 | R := RE_Str_Concat_4; | |
2345 | ||
2346 | when 5 => | |
2347 | R := RE_Str_Concat_5; | |
2348 | ||
2349 | when others => | |
2350 | R := RE_Null; | |
2351 | raise Program_Error; | |
2352 | end case; | |
2353 | ||
2354 | -- Now generate the appropriate call | |
2355 | ||
2356 | Rewrite (Cnode, | |
2357 | Make_Function_Call (Sloc (Cnode), | |
2358 | Name => New_Occurrence_Of (RTE (R), Loc), | |
2359 | Parameter_Associations => Opnds)); | |
2360 | ||
2361 | Analyze_And_Resolve (Cnode, Standard_String); | |
fbf5a39b AC |
2362 | |
2363 | exception | |
2364 | when RE_Not_Available => | |
2365 | return; | |
70482933 RK |
2366 | end Expand_Concatenate_String; |
2367 | ||
2368 | ------------------------ | |
2369 | -- Expand_N_Allocator -- | |
2370 | ------------------------ | |
2371 | ||
2372 | procedure Expand_N_Allocator (N : Node_Id) is | |
2373 | PtrT : constant Entity_Id := Etype (N); | |
0da2c8ac | 2374 | Dtyp : constant Entity_Id := Designated_Type (PtrT); |
70482933 RK |
2375 | Desig : Entity_Id; |
2376 | Loc : constant Source_Ptr := Sloc (N); | |
2377 | Temp : Entity_Id; | |
2378 | Node : Node_Id; | |
2379 | ||
2380 | begin | |
2381 | -- RM E.2.3(22). We enforce that the expected type of an allocator | |
2382 | -- shall not be a remote access-to-class-wide-limited-private type | |
2383 | ||
2384 | -- Why is this being done at expansion time, seems clearly wrong ??? | |
2385 | ||
2386 | Validate_Remote_Access_To_Class_Wide_Type (N); | |
2387 | ||
2388 | -- Set the Storage Pool | |
2389 | ||
2390 | Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT))); | |
2391 | ||
2392 | if Present (Storage_Pool (N)) then | |
2393 | if Is_RTE (Storage_Pool (N), RE_SS_Pool) then | |
2394 | if not Java_VM then | |
2395 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); | |
2396 | end if; | |
fbf5a39b AC |
2397 | |
2398 | elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then | |
2399 | Set_Procedure_To_Call (N, RTE (RE_Allocate_Any)); | |
2400 | ||
70482933 RK |
2401 | else |
2402 | Set_Procedure_To_Call (N, | |
2403 | Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate)); | |
2404 | end if; | |
2405 | end if; | |
2406 | ||
2407 | -- Under certain circumstances we can replace an allocator by an | |
2408 | -- access to statically allocated storage. The conditions, as noted | |
2409 | -- in AARM 3.10 (10c) are as follows: | |
2410 | ||
2411 | -- Size and initial value is known at compile time | |
2412 | -- Access type is access-to-constant | |
2413 | ||
fbf5a39b AC |
2414 | -- The allocator is not part of a constraint on a record component, |
2415 | -- because in that case the inserted actions are delayed until the | |
2416 | -- record declaration is fully analyzed, which is too late for the | |
2417 | -- analysis of the rewritten allocator. | |
2418 | ||
70482933 RK |
2419 | if Is_Access_Constant (PtrT) |
2420 | and then Nkind (Expression (N)) = N_Qualified_Expression | |
2421 | and then Compile_Time_Known_Value (Expression (Expression (N))) | |
2422 | and then Size_Known_At_Compile_Time (Etype (Expression | |
2423 | (Expression (N)))) | |
fbf5a39b | 2424 | and then not Is_Record_Type (Current_Scope) |
70482933 RK |
2425 | then |
2426 | -- Here we can do the optimization. For the allocator | |
2427 | ||
2428 | -- new x'(y) | |
2429 | ||
2430 | -- We insert an object declaration | |
2431 | ||
2432 | -- Tnn : aliased x := y; | |
2433 | ||
2434 | -- and replace the allocator by Tnn'Unrestricted_Access. | |
2435 | -- Tnn is marked as requiring static allocation. | |
2436 | ||
2437 | Temp := | |
2438 | Make_Defining_Identifier (Loc, New_Internal_Name ('T')); | |
2439 | ||
2440 | Desig := Subtype_Mark (Expression (N)); | |
2441 | ||
2442 | -- If context is constrained, use constrained subtype directly, | |
2443 | -- so that the constant is not labelled as having a nomimally | |
2444 | -- unconstrained subtype. | |
2445 | ||
0da2c8ac AC |
2446 | if Entity (Desig) = Base_Type (Dtyp) then |
2447 | Desig := New_Occurrence_Of (Dtyp, Loc); | |
70482933 RK |
2448 | end if; |
2449 | ||
2450 | Insert_Action (N, | |
2451 | Make_Object_Declaration (Loc, | |
2452 | Defining_Identifier => Temp, | |
2453 | Aliased_Present => True, | |
2454 | Constant_Present => Is_Access_Constant (PtrT), | |
2455 | Object_Definition => Desig, | |
2456 | Expression => Expression (Expression (N)))); | |
2457 | ||
2458 | Rewrite (N, | |
2459 | Make_Attribute_Reference (Loc, | |
2460 | Prefix => New_Occurrence_Of (Temp, Loc), | |
2461 | Attribute_Name => Name_Unrestricted_Access)); | |
2462 | ||
2463 | Analyze_And_Resolve (N, PtrT); | |
2464 | ||
2465 | -- We set the variable as statically allocated, since we don't | |
2466 | -- want it going on the stack of the current procedure! | |
2467 | ||
2468 | Set_Is_Statically_Allocated (Temp); | |
2469 | return; | |
2470 | end if; | |
2471 | ||
0da2c8ac AC |
2472 | -- Handle case of qualified expression (other than optimization above) |
2473 | ||
70482933 | 2474 | if Nkind (Expression (N)) = N_Qualified_Expression then |
fbf5a39b AC |
2475 | Expand_Allocator_Expression (N); |
2476 | ||
2477 | -- If the allocator is for a type which requires initialization, and | |
2478 | -- there is no initial value (i.e. operand is a subtype indication | |
2479 | -- rather than a qualifed expression), then we must generate a call | |
2480 | -- to the initialization routine. This is done using an expression | |
2481 | -- actions node: | |
2482 | -- | |
2483 | -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn] | |
2484 | -- | |
2485 | -- Here ptr_T is the pointer type for the allocator, and T is the | |
2486 | -- subtype of the allocator. A special case arises if the designated | |
2487 | -- type of the access type is a task or contains tasks. In this case | |
2488 | -- the call to Init (Temp.all ...) is replaced by code that ensures | |
2489 | -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block | |
2490 | -- for details). In addition, if the type T is a task T, then the | |
2491 | -- first argument to Init must be converted to the task record type. | |
70482933 RK |
2492 | |
2493 | else | |
2494 | declare | |
0da2c8ac AC |
2495 | T : constant Entity_Id := Entity (Expression (N)); |
2496 | Init : Entity_Id; | |
2497 | Arg1 : Node_Id; | |
2498 | Args : List_Id; | |
2499 | Decls : List_Id; | |
2500 | Decl : Node_Id; | |
2501 | Discr : Elmt_Id; | |
2502 | Flist : Node_Id; | |
2503 | Temp_Decl : Node_Id; | |
2504 | Temp_Type : Entity_Id; | |
2505 | Attach_Level : Uint; | |
70482933 RK |
2506 | |
2507 | begin | |
70482933 RK |
2508 | if No_Initialization (N) then |
2509 | null; | |
2510 | ||
2511 | -- Case of no initialization procedure present | |
2512 | ||
2513 | elsif not Has_Non_Null_Base_Init_Proc (T) then | |
2514 | ||
2515 | -- Case of simple initialization required | |
2516 | ||
2517 | if Needs_Simple_Initialization (T) then | |
2518 | Rewrite (Expression (N), | |
2519 | Make_Qualified_Expression (Loc, | |
2520 | Subtype_Mark => New_Occurrence_Of (T, Loc), | |
2521 | Expression => Get_Simple_Init_Val (T, Loc))); | |
2522 | ||
2523 | Analyze_And_Resolve (Expression (Expression (N)), T); | |
2524 | Analyze_And_Resolve (Expression (N), T); | |
2525 | Set_Paren_Count (Expression (Expression (N)), 1); | |
2526 | Expand_N_Allocator (N); | |
2527 | ||
2528 | -- No initialization required | |
2529 | ||
2530 | else | |
2531 | null; | |
2532 | end if; | |
2533 | ||
2534 | -- Case of initialization procedure present, must be called | |
2535 | ||
2536 | else | |
2537 | Init := Base_Init_Proc (T); | |
2538 | Node := N; | |
2539 | Temp := | |
2540 | Make_Defining_Identifier (Loc, New_Internal_Name ('P')); | |
2541 | ||
2542 | -- Construct argument list for the initialization routine call | |
2543 | -- The CPP constructor needs the address directly | |
2544 | ||
2545 | if Is_CPP_Class (T) then | |
2546 | Arg1 := New_Reference_To (Temp, Loc); | |
2547 | Temp_Type := T; | |
2548 | ||
2549 | else | |
2550 | Arg1 := | |
2551 | Make_Explicit_Dereference (Loc, | |
2552 | Prefix => New_Reference_To (Temp, Loc)); | |
2553 | Set_Assignment_OK (Arg1); | |
2554 | Temp_Type := PtrT; | |
2555 | ||
2556 | -- The initialization procedure expects a specific type. | |
2557 | -- if the context is access to class wide, indicate that | |
2558 | -- the object being allocated has the right specific type. | |
2559 | ||
0da2c8ac | 2560 | if Is_Class_Wide_Type (Dtyp) then |
70482933 RK |
2561 | Arg1 := Unchecked_Convert_To (T, Arg1); |
2562 | end if; | |
2563 | end if; | |
2564 | ||
2565 | -- If designated type is a concurrent type or if it is a | |
2566 | -- private type whose definition is a concurrent type, | |
2567 | -- the first argument in the Init routine has to be | |
2568 | -- unchecked conversion to the corresponding record type. | |
2569 | -- If the designated type is a derived type, we also | |
2570 | -- convert the argument to its root type. | |
2571 | ||
2572 | if Is_Concurrent_Type (T) then | |
2573 | Arg1 := | |
2574 | Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1); | |
2575 | ||
2576 | elsif Is_Private_Type (T) | |
2577 | and then Present (Full_View (T)) | |
2578 | and then Is_Concurrent_Type (Full_View (T)) | |
2579 | then | |
2580 | Arg1 := | |
2581 | Unchecked_Convert_To | |
2582 | (Corresponding_Record_Type (Full_View (T)), Arg1); | |
2583 | ||
2584 | elsif Etype (First_Formal (Init)) /= Base_Type (T) then | |
2585 | ||
2586 | declare | |
2587 | Ftyp : constant Entity_Id := Etype (First_Formal (Init)); | |
2588 | ||
2589 | begin | |
2590 | Arg1 := OK_Convert_To (Etype (Ftyp), Arg1); | |
2591 | Set_Etype (Arg1, Ftyp); | |
2592 | end; | |
2593 | end if; | |
2594 | ||
2595 | Args := New_List (Arg1); | |
2596 | ||
2597 | -- For the task case, pass the Master_Id of the access type | |
2598 | -- as the value of the _Master parameter, and _Chain as the | |
2599 | -- value of the _Chain parameter (_Chain will be defined as | |
2600 | -- part of the generated code for the allocator). | |
2601 | ||
2602 | if Has_Task (T) then | |
70482933 RK |
2603 | if No (Master_Id (Base_Type (PtrT))) then |
2604 | ||
2605 | -- The designated type was an incomplete type, and | |
2606 | -- the access type did not get expanded. Salvage | |
2607 | -- it now. | |
2608 | ||
2609 | Expand_N_Full_Type_Declaration | |
2610 | (Parent (Base_Type (PtrT))); | |
2611 | end if; | |
2612 | ||
2613 | -- If the context of the allocator is a declaration or | |
2614 | -- an assignment, we can generate a meaningful image for | |
2615 | -- it, even though subsequent assignments might remove | |
7bc1c7df ES |
2616 | -- the connection between task and entity. We build this |
2617 | -- image when the left-hand side is a simple variable, | |
2618 | -- a simple indexed assignment or a simple selected | |
2619 | -- component. | |
70482933 RK |
2620 | |
2621 | if Nkind (Parent (N)) = N_Assignment_Statement then | |
2622 | declare | |
2623 | Nam : constant Node_Id := Name (Parent (N)); | |
2624 | ||
2625 | begin | |
2626 | if Is_Entity_Name (Nam) then | |
2627 | Decls := | |
2628 | Build_Task_Image_Decls ( | |
2629 | Loc, | |
2630 | New_Occurrence_Of | |
2631 | (Entity (Nam), Sloc (Nam)), T); | |
2632 | ||
7bc1c7df ES |
2633 | elsif (Nkind (Nam) = N_Indexed_Component |
2634 | or else Nkind (Nam) = N_Selected_Component) | |
2635 | and then Is_Entity_Name (Prefix (Nam)) | |
2636 | then | |
2637 | Decls := | |
316ad9c5 RD |
2638 | Build_Task_Image_Decls |
2639 | (Loc, Nam, Etype (Prefix (Nam))); | |
70482933 RK |
2640 | else |
2641 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
2642 | end if; | |
2643 | end; | |
2644 | ||
2645 | elsif Nkind (Parent (N)) = N_Object_Declaration then | |
2646 | Decls := | |
2647 | Build_Task_Image_Decls ( | |
2648 | Loc, Defining_Identifier (Parent (N)), T); | |
2649 | ||
2650 | else | |
2651 | Decls := Build_Task_Image_Decls (Loc, T, T); | |
2652 | end if; | |
2653 | ||
2654 | Append_To (Args, | |
2655 | New_Reference_To | |
2656 | (Master_Id (Base_Type (Root_Type (PtrT))), Loc)); | |
2657 | Append_To (Args, Make_Identifier (Loc, Name_uChain)); | |
2658 | ||
2659 | Decl := Last (Decls); | |
2660 | Append_To (Args, | |
2661 | New_Occurrence_Of (Defining_Identifier (Decl), Loc)); | |
2662 | ||
2663 | -- Has_Task is false, Decls not used | |
2664 | ||
2665 | else | |
2666 | Decls := No_List; | |
2667 | end if; | |
2668 | ||
2669 | -- Add discriminants if discriminated type | |
2670 | ||
2671 | if Has_Discriminants (T) then | |
2672 | Discr := First_Elmt (Discriminant_Constraint (T)); | |
2673 | ||
2674 | while Present (Discr) loop | |
fbf5a39b | 2675 | Append (New_Copy_Tree (Elists.Node (Discr)), Args); |
70482933 RK |
2676 | Next_Elmt (Discr); |
2677 | end loop; | |
2678 | ||
2679 | elsif Is_Private_Type (T) | |
2680 | and then Present (Full_View (T)) | |
2681 | and then Has_Discriminants (Full_View (T)) | |
2682 | then | |
2683 | Discr := | |
2684 | First_Elmt (Discriminant_Constraint (Full_View (T))); | |
2685 | ||
2686 | while Present (Discr) loop | |
fbf5a39b | 2687 | Append (New_Copy_Tree (Elists.Node (Discr)), Args); |
70482933 RK |
2688 | Next_Elmt (Discr); |
2689 | end loop; | |
2690 | end if; | |
2691 | ||
2692 | -- We set the allocator as analyzed so that when we analyze the | |
2693 | -- expression actions node, we do not get an unwanted recursive | |
2694 | -- expansion of the allocator expression. | |
2695 | ||
2696 | Set_Analyzed (N, True); | |
2697 | Node := Relocate_Node (N); | |
2698 | ||
2699 | -- Here is the transformation: | |
2700 | -- input: new T | |
2701 | -- output: Temp : constant ptr_T := new T; | |
2702 | -- Init (Temp.all, ...); | |
2703 | -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all)); | |
2704 | -- <CTRL> Initialize (Finalizable (Temp.all)); | |
2705 | ||
2706 | -- Here ptr_T is the pointer type for the allocator, and T | |
2707 | -- is the subtype of the allocator. | |
2708 | ||
2709 | Temp_Decl := | |
2710 | Make_Object_Declaration (Loc, | |
2711 | Defining_Identifier => Temp, | |
2712 | Constant_Present => True, | |
2713 | Object_Definition => New_Reference_To (Temp_Type, Loc), | |
2714 | Expression => Node); | |
2715 | ||
2716 | Set_Assignment_OK (Temp_Decl); | |
2717 | ||
2718 | if Is_CPP_Class (T) then | |
2719 | Set_Aliased_Present (Temp_Decl); | |
2720 | end if; | |
2721 | ||
2722 | Insert_Action (N, Temp_Decl, Suppress => All_Checks); | |
2723 | ||
fbf5a39b | 2724 | -- If the designated type is task type or contains tasks, |
70482933 RK |
2725 | -- Create block to activate created tasks, and insert |
2726 | -- declaration for Task_Image variable ahead of call. | |
2727 | ||
2728 | if Has_Task (T) then | |
2729 | declare | |
fbf5a39b | 2730 | L : constant List_Id := New_List; |
70482933 RK |
2731 | Blk : Node_Id; |
2732 | ||
2733 | begin | |
2734 | Build_Task_Allocate_Block (L, Node, Args); | |
2735 | Blk := Last (L); | |
2736 | ||
2737 | Insert_List_Before (First (Declarations (Blk)), Decls); | |
2738 | Insert_Actions (N, L); | |
2739 | end; | |
2740 | ||
2741 | else | |
2742 | Insert_Action (N, | |
2743 | Make_Procedure_Call_Statement (Loc, | |
2744 | Name => New_Reference_To (Init, Loc), | |
2745 | Parameter_Associations => Args)); | |
2746 | end if; | |
2747 | ||
2748 | if Controlled_Type (T) then | |
fbf5a39b | 2749 | Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT); |
0da2c8ac AC |
2750 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
2751 | Attach_Level := Uint_1; | |
2752 | else | |
2753 | Attach_Level := Uint_2; | |
2754 | end if; | |
70482933 RK |
2755 | Insert_Actions (N, |
2756 | Make_Init_Call ( | |
2757 | Ref => New_Copy_Tree (Arg1), | |
2758 | Typ => T, | |
2759 | Flist_Ref => Flist, | |
0da2c8ac AC |
2760 | With_Attach => Make_Integer_Literal (Loc, |
2761 | Attach_Level))); | |
70482933 RK |
2762 | end if; |
2763 | ||
2764 | if Is_CPP_Class (T) then | |
2765 | Rewrite (N, | |
2766 | Make_Attribute_Reference (Loc, | |
2767 | Prefix => New_Reference_To (Temp, Loc), | |
2768 | Attribute_Name => Name_Unchecked_Access)); | |
2769 | else | |
2770 | Rewrite (N, New_Reference_To (Temp, Loc)); | |
2771 | end if; | |
2772 | ||
2773 | Analyze_And_Resolve (N, PtrT); | |
2774 | end if; | |
2775 | end; | |
2776 | end if; | |
fbf5a39b AC |
2777 | |
2778 | exception | |
2779 | when RE_Not_Available => | |
2780 | return; | |
70482933 RK |
2781 | end Expand_N_Allocator; |
2782 | ||
2783 | ----------------------- | |
2784 | -- Expand_N_And_Then -- | |
2785 | ----------------------- | |
2786 | ||
2787 | -- Expand into conditional expression if Actions present, and also | |
2788 | -- deal with optimizing case of arguments being True or False. | |
2789 | ||
2790 | procedure Expand_N_And_Then (N : Node_Id) is | |
2791 | Loc : constant Source_Ptr := Sloc (N); | |
2792 | Typ : constant Entity_Id := Etype (N); | |
2793 | Left : constant Node_Id := Left_Opnd (N); | |
2794 | Right : constant Node_Id := Right_Opnd (N); | |
2795 | Actlist : List_Id; | |
2796 | ||
2797 | begin | |
2798 | -- Deal with non-standard booleans | |
2799 | ||
2800 | if Is_Boolean_Type (Typ) then | |
2801 | Adjust_Condition (Left); | |
2802 | Adjust_Condition (Right); | |
2803 | Set_Etype (N, Standard_Boolean); | |
2804 | end if; | |
2805 | ||
2806 | -- Check for cases of left argument is True or False | |
2807 | ||
2808 | if Nkind (Left) = N_Identifier then | |
2809 | ||
2810 | -- If left argument is True, change (True and then Right) to Right. | |
2811 | -- Any actions associated with Right will be executed unconditionally | |
2812 | -- and can thus be inserted into the tree unconditionally. | |
2813 | ||
2814 | if Entity (Left) = Standard_True then | |
2815 | if Present (Actions (N)) then | |
2816 | Insert_Actions (N, Actions (N)); | |
2817 | end if; | |
2818 | ||
2819 | Rewrite (N, Right); | |
2820 | Adjust_Result_Type (N, Typ); | |
2821 | return; | |
2822 | ||
2823 | -- If left argument is False, change (False and then Right) to | |
2824 | -- False. In this case we can forget the actions associated with | |
2825 | -- Right, since they will never be executed. | |
2826 | ||
2827 | elsif Entity (Left) = Standard_False then | |
2828 | Kill_Dead_Code (Right); | |
2829 | Kill_Dead_Code (Actions (N)); | |
2830 | Rewrite (N, New_Occurrence_Of (Standard_False, Loc)); | |
2831 | Adjust_Result_Type (N, Typ); | |
2832 | return; | |
2833 | end if; | |
2834 | end if; | |
2835 | ||
2836 | -- If Actions are present, we expand | |
2837 | ||
2838 | -- left and then right | |
2839 | ||
2840 | -- into | |
2841 | ||
2842 | -- if left then right else false end | |
2843 | ||
2844 | -- with the actions becoming the Then_Actions of the conditional | |
2845 | -- expression. This conditional expression is then further expanded | |
2846 | -- (and will eventually disappear) | |
2847 | ||
2848 | if Present (Actions (N)) then | |
2849 | Actlist := Actions (N); | |
2850 | Rewrite (N, | |
2851 | Make_Conditional_Expression (Loc, | |
2852 | Expressions => New_List ( | |
2853 | Left, | |
2854 | Right, | |
2855 | New_Occurrence_Of (Standard_False, Loc)))); | |
2856 | ||
2857 | Set_Then_Actions (N, Actlist); | |
2858 | Analyze_And_Resolve (N, Standard_Boolean); | |
2859 | Adjust_Result_Type (N, Typ); | |
2860 | return; | |
2861 | end if; | |
2862 | ||
2863 | -- No actions present, check for cases of right argument True/False | |
2864 | ||
2865 | if Nkind (Right) = N_Identifier then | |
2866 | ||
2867 | -- Change (Left and then True) to Left. Note that we know there | |
2868 | -- are no actions associated with the True operand, since we | |
2869 | -- just checked for this case above. | |
2870 | ||
2871 | if Entity (Right) = Standard_True then | |
2872 | Rewrite (N, Left); | |
2873 | ||
2874 | -- Change (Left and then False) to False, making sure to preserve | |
2875 | -- any side effects associated with the Left operand. | |
2876 | ||
2877 | elsif Entity (Right) = Standard_False then | |
2878 | Remove_Side_Effects (Left); | |
2879 | Rewrite | |
2880 | (N, New_Occurrence_Of (Standard_False, Loc)); | |
2881 | end if; | |
2882 | end if; | |
2883 | ||
2884 | Adjust_Result_Type (N, Typ); | |
2885 | end Expand_N_And_Then; | |
2886 | ||
2887 | ------------------------------------- | |
2888 | -- Expand_N_Conditional_Expression -- | |
2889 | ------------------------------------- | |
2890 | ||
2891 | -- Expand into expression actions if then/else actions present | |
2892 | ||
2893 | procedure Expand_N_Conditional_Expression (N : Node_Id) is | |
2894 | Loc : constant Source_Ptr := Sloc (N); | |
2895 | Cond : constant Node_Id := First (Expressions (N)); | |
2896 | Thenx : constant Node_Id := Next (Cond); | |
2897 | Elsex : constant Node_Id := Next (Thenx); | |
2898 | Typ : constant Entity_Id := Etype (N); | |
2899 | Cnn : Entity_Id; | |
2900 | New_If : Node_Id; | |
2901 | ||
2902 | begin | |
2903 | -- If either then or else actions are present, then given: | |
2904 | ||
2905 | -- if cond then then-expr else else-expr end | |
2906 | ||
2907 | -- we insert the following sequence of actions (using Insert_Actions): | |
2908 | ||
2909 | -- Cnn : typ; | |
2910 | -- if cond then | |
2911 | -- <<then actions>> | |
2912 | -- Cnn := then-expr; | |
2913 | -- else | |
2914 | -- <<else actions>> | |
2915 | -- Cnn := else-expr | |
2916 | -- end if; | |
2917 | ||
2717634d | 2918 | -- and replace the conditional expression by a reference to Cnn |
70482933 RK |
2919 | |
2920 | if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then | |
2921 | Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C')); | |
2922 | ||
2923 | New_If := | |
2924 | Make_Implicit_If_Statement (N, | |
2925 | Condition => Relocate_Node (Cond), | |
2926 | ||
2927 | Then_Statements => New_List ( | |
2928 | Make_Assignment_Statement (Sloc (Thenx), | |
2929 | Name => New_Occurrence_Of (Cnn, Sloc (Thenx)), | |
2930 | Expression => Relocate_Node (Thenx))), | |
2931 | ||
2932 | Else_Statements => New_List ( | |
2933 | Make_Assignment_Statement (Sloc (Elsex), | |
2934 | Name => New_Occurrence_Of (Cnn, Sloc (Elsex)), | |
2935 | Expression => Relocate_Node (Elsex)))); | |
2936 | ||
fbf5a39b AC |
2937 | Set_Assignment_OK (Name (First (Then_Statements (New_If)))); |
2938 | Set_Assignment_OK (Name (First (Else_Statements (New_If)))); | |
2939 | ||
70482933 RK |
2940 | if Present (Then_Actions (N)) then |
2941 | Insert_List_Before | |
2942 | (First (Then_Statements (New_If)), Then_Actions (N)); | |
2943 | end if; | |
2944 | ||
2945 | if Present (Else_Actions (N)) then | |
2946 | Insert_List_Before | |
2947 | (First (Else_Statements (New_If)), Else_Actions (N)); | |
2948 | end if; | |
2949 | ||
2950 | Rewrite (N, New_Occurrence_Of (Cnn, Loc)); | |
2951 | ||
2952 | Insert_Action (N, | |
2953 | Make_Object_Declaration (Loc, | |
2954 | Defining_Identifier => Cnn, | |
2955 | Object_Definition => New_Occurrence_Of (Typ, Loc))); | |
2956 | ||
2957 | Insert_Action (N, New_If); | |
2958 | Analyze_And_Resolve (N, Typ); | |
2959 | end if; | |
2960 | end Expand_N_Conditional_Expression; | |
2961 | ||
2962 | ----------------------------------- | |
2963 | -- Expand_N_Explicit_Dereference -- | |
2964 | ----------------------------------- | |
2965 | ||
2966 | procedure Expand_N_Explicit_Dereference (N : Node_Id) is | |
2967 | begin | |
2968 | -- The only processing required is an insertion of an explicit | |
2969 | -- dereference call for the checked storage pool case. | |
2970 | ||
2971 | Insert_Dereference_Action (Prefix (N)); | |
2972 | end Expand_N_Explicit_Dereference; | |
2973 | ||
2974 | ----------------- | |
2975 | -- Expand_N_In -- | |
2976 | ----------------- | |
2977 | ||
2978 | procedure Expand_N_In (N : Node_Id) is | |
7324bf49 AC |
2979 | Loc : constant Source_Ptr := Sloc (N); |
2980 | Rtyp : constant Entity_Id := Etype (N); | |
2981 | Lop : constant Node_Id := Left_Opnd (N); | |
2982 | Rop : constant Node_Id := Right_Opnd (N); | |
2983 | Static : constant Boolean := Is_OK_Static_Expression (N); | |
70482933 RK |
2984 | |
2985 | begin | |
fbf5a39b AC |
2986 | -- If we have an explicit range, do a bit of optimization based |
2987 | -- on range analysis (we may be able to kill one or both checks). | |
2988 | ||
2989 | if Nkind (Rop) = N_Range then | |
2990 | declare | |
2991 | Lcheck : constant Compare_Result := | |
2992 | Compile_Time_Compare (Lop, Low_Bound (Rop)); | |
2993 | Ucheck : constant Compare_Result := | |
2994 | Compile_Time_Compare (Lop, High_Bound (Rop)); | |
2995 | ||
2996 | begin | |
2997 | -- If either check is known to fail, replace result | |
2998 | -- by False, since the other check does not matter. | |
7324bf49 AC |
2999 | -- Preserve the static flag for legality checks, because |
3000 | -- we are constant-folding beyond RM 4.9. | |
fbf5a39b AC |
3001 | |
3002 | if Lcheck = LT or else Ucheck = GT then | |
3003 | Rewrite (N, | |
3004 | New_Reference_To (Standard_False, Loc)); | |
3005 | Analyze_And_Resolve (N, Rtyp); | |
7324bf49 | 3006 | Set_Is_Static_Expression (N, Static); |
fbf5a39b AC |
3007 | return; |
3008 | ||
3009 | -- If both checks are known to succeed, replace result | |
3010 | -- by True, since we know we are in range. | |
3011 | ||
3012 | elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then | |
3013 | Rewrite (N, | |
3014 | New_Reference_To (Standard_True, Loc)); | |
3015 | Analyze_And_Resolve (N, Rtyp); | |
7324bf49 | 3016 | Set_Is_Static_Expression (N, Static); |
fbf5a39b AC |
3017 | return; |
3018 | ||
3019 | -- If lower bound check succeeds and upper bound check is | |
3020 | -- not known to succeed or fail, then replace the range check | |
3021 | -- with a comparison against the upper bound. | |
3022 | ||
3023 | elsif Lcheck in Compare_GE then | |
3024 | Rewrite (N, | |
3025 | Make_Op_Le (Loc, | |
3026 | Left_Opnd => Lop, | |
3027 | Right_Opnd => High_Bound (Rop))); | |
3028 | Analyze_And_Resolve (N, Rtyp); | |
3029 | return; | |
3030 | ||
3031 | -- If upper bound check succeeds and lower bound check is | |
3032 | -- not known to succeed or fail, then replace the range check | |
3033 | -- with a comparison against the lower bound. | |
3034 | ||
3035 | elsif Ucheck in Compare_LE then | |
3036 | Rewrite (N, | |
3037 | Make_Op_Ge (Loc, | |
3038 | Left_Opnd => Lop, | |
3039 | Right_Opnd => Low_Bound (Rop))); | |
3040 | Analyze_And_Resolve (N, Rtyp); | |
3041 | return; | |
3042 | end if; | |
3043 | end; | |
3044 | ||
3045 | -- For all other cases of an explicit range, nothing to be done | |
70482933 | 3046 | |
70482933 RK |
3047 | return; |
3048 | ||
3049 | -- Here right operand is a subtype mark | |
3050 | ||
3051 | else | |
3052 | declare | |
fbf5a39b AC |
3053 | Typ : Entity_Id := Etype (Rop); |
3054 | Is_Acc : constant Boolean := Is_Access_Type (Typ); | |
3055 | Obj : Node_Id := Lop; | |
3056 | Cond : Node_Id := Empty; | |
70482933 RK |
3057 | |
3058 | begin | |
3059 | Remove_Side_Effects (Obj); | |
3060 | ||
3061 | -- For tagged type, do tagged membership operation | |
3062 | ||
3063 | if Is_Tagged_Type (Typ) then | |
fbf5a39b | 3064 | |
70482933 RK |
3065 | -- No expansion will be performed when Java_VM, as the |
3066 | -- JVM back end will handle the membership tests directly | |
3067 | -- (tags are not explicitly represented in Java objects, | |
3068 | -- so the normal tagged membership expansion is not what | |
3069 | -- we want). | |
3070 | ||
3071 | if not Java_VM then | |
3072 | Rewrite (N, Tagged_Membership (N)); | |
3073 | Analyze_And_Resolve (N, Rtyp); | |
3074 | end if; | |
3075 | ||
3076 | return; | |
3077 | ||
3078 | -- If type is scalar type, rewrite as x in t'first .. t'last | |
3079 | -- This reason we do this is that the bounds may have the wrong | |
3080 | -- type if they come from the original type definition. | |
3081 | ||
3082 | elsif Is_Scalar_Type (Typ) then | |
fbf5a39b | 3083 | Rewrite (Rop, |
70482933 RK |
3084 | Make_Range (Loc, |
3085 | Low_Bound => | |
3086 | Make_Attribute_Reference (Loc, | |
3087 | Attribute_Name => Name_First, | |
3088 | Prefix => New_Reference_To (Typ, Loc)), | |
3089 | ||
3090 | High_Bound => | |
3091 | Make_Attribute_Reference (Loc, | |
3092 | Attribute_Name => Name_Last, | |
3093 | Prefix => New_Reference_To (Typ, Loc)))); | |
3094 | Analyze_And_Resolve (N, Rtyp); | |
3095 | return; | |
5d09245e AC |
3096 | |
3097 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
3098 | -- a membership test if the subtype mark denotes a constrained | |
3099 | -- Unchecked_Union subtype and the expression lacks inferable | |
3100 | -- discriminants. | |
3101 | ||
3102 | elsif Is_Unchecked_Union (Base_Type (Typ)) | |
3103 | and then Is_Constrained (Typ) | |
3104 | and then not Has_Inferable_Discriminants (Lop) | |
3105 | then | |
3106 | Insert_Action (N, | |
3107 | Make_Raise_Program_Error (Loc, | |
3108 | Reason => PE_Unchecked_Union_Restriction)); | |
3109 | ||
3110 | -- Prevent Gigi from generating incorrect code by rewriting | |
3111 | -- the test as a standard False. | |
3112 | ||
3113 | Rewrite (N, | |
3114 | New_Occurrence_Of (Standard_False, Loc)); | |
3115 | ||
3116 | return; | |
70482933 RK |
3117 | end if; |
3118 | ||
fbf5a39b AC |
3119 | -- Here we have a non-scalar type |
3120 | ||
70482933 RK |
3121 | if Is_Acc then |
3122 | Typ := Designated_Type (Typ); | |
3123 | end if; | |
3124 | ||
3125 | if not Is_Constrained (Typ) then | |
3126 | Rewrite (N, | |
3127 | New_Reference_To (Standard_True, Loc)); | |
3128 | Analyze_And_Resolve (N, Rtyp); | |
3129 | ||
3130 | -- For the constrained array case, we have to check the | |
3131 | -- subscripts for an exact match if the lengths are | |
3132 | -- non-zero (the lengths must match in any case). | |
3133 | ||
3134 | elsif Is_Array_Type (Typ) then | |
3135 | ||
fbf5a39b | 3136 | Check_Subscripts : declare |
70482933 | 3137 | function Construct_Attribute_Reference |
2e071734 AC |
3138 | (E : Node_Id; |
3139 | Nam : Name_Id; | |
3140 | Dim : Nat) return Node_Id; | |
70482933 RK |
3141 | -- Build attribute reference E'Nam(Dim) |
3142 | ||
fbf5a39b AC |
3143 | ----------------------------------- |
3144 | -- Construct_Attribute_Reference -- | |
3145 | ----------------------------------- | |
3146 | ||
70482933 | 3147 | function Construct_Attribute_Reference |
2e071734 AC |
3148 | (E : Node_Id; |
3149 | Nam : Name_Id; | |
3150 | Dim : Nat) return Node_Id | |
70482933 RK |
3151 | is |
3152 | begin | |
3153 | return | |
3154 | Make_Attribute_Reference (Loc, | |
3155 | Prefix => E, | |
3156 | Attribute_Name => Nam, | |
3157 | Expressions => New_List ( | |
3158 | Make_Integer_Literal (Loc, Dim))); | |
3159 | end Construct_Attribute_Reference; | |
3160 | ||
fbf5a39b AC |
3161 | -- Start processing for Check_Subscripts |
3162 | ||
70482933 RK |
3163 | begin |
3164 | for J in 1 .. Number_Dimensions (Typ) loop | |
3165 | Evolve_And_Then (Cond, | |
3166 | Make_Op_Eq (Loc, | |
3167 | Left_Opnd => | |
3168 | Construct_Attribute_Reference | |
fbf5a39b AC |
3169 | (Duplicate_Subexpr_No_Checks (Obj), |
3170 | Name_First, J), | |
70482933 RK |
3171 | Right_Opnd => |
3172 | Construct_Attribute_Reference | |
3173 | (New_Occurrence_Of (Typ, Loc), Name_First, J))); | |
3174 | ||
3175 | Evolve_And_Then (Cond, | |
3176 | Make_Op_Eq (Loc, | |
3177 | Left_Opnd => | |
3178 | Construct_Attribute_Reference | |
fbf5a39b AC |
3179 | (Duplicate_Subexpr_No_Checks (Obj), |
3180 | Name_Last, J), | |
70482933 RK |
3181 | Right_Opnd => |
3182 | Construct_Attribute_Reference | |
3183 | (New_Occurrence_Of (Typ, Loc), Name_Last, J))); | |
3184 | end loop; | |
3185 | ||
3186 | if Is_Acc then | |
fbf5a39b AC |
3187 | Cond := |
3188 | Make_Or_Else (Loc, | |
3189 | Left_Opnd => | |
3190 | Make_Op_Eq (Loc, | |
3191 | Left_Opnd => Obj, | |
3192 | Right_Opnd => Make_Null (Loc)), | |
3193 | Right_Opnd => Cond); | |
70482933 RK |
3194 | end if; |
3195 | ||
3196 | Rewrite (N, Cond); | |
3197 | Analyze_And_Resolve (N, Rtyp); | |
fbf5a39b | 3198 | end Check_Subscripts; |
70482933 RK |
3199 | |
3200 | -- These are the cases where constraint checks may be | |
3201 | -- required, e.g. records with possible discriminants | |
3202 | ||
3203 | else | |
3204 | -- Expand the test into a series of discriminant comparisons. | |
3205 | -- The expression that is built is the negation of the one | |
3206 | -- that is used for checking discriminant constraints. | |
3207 | ||
3208 | Obj := Relocate_Node (Left_Opnd (N)); | |
3209 | ||
3210 | if Has_Discriminants (Typ) then | |
3211 | Cond := Make_Op_Not (Loc, | |
3212 | Right_Opnd => Build_Discriminant_Checks (Obj, Typ)); | |
3213 | ||
3214 | if Is_Acc then | |
3215 | Cond := Make_Or_Else (Loc, | |
3216 | Left_Opnd => | |
3217 | Make_Op_Eq (Loc, | |
3218 | Left_Opnd => Obj, | |
3219 | Right_Opnd => Make_Null (Loc)), | |
3220 | Right_Opnd => Cond); | |
3221 | end if; | |
3222 | ||
3223 | else | |
3224 | Cond := New_Occurrence_Of (Standard_True, Loc); | |
3225 | end if; | |
3226 | ||
3227 | Rewrite (N, Cond); | |
3228 | Analyze_And_Resolve (N, Rtyp); | |
3229 | end if; | |
3230 | end; | |
3231 | end if; | |
3232 | end Expand_N_In; | |
3233 | ||
3234 | -------------------------------- | |
3235 | -- Expand_N_Indexed_Component -- | |
3236 | -------------------------------- | |
3237 | ||
3238 | procedure Expand_N_Indexed_Component (N : Node_Id) is | |
3239 | Loc : constant Source_Ptr := Sloc (N); | |
3240 | Typ : constant Entity_Id := Etype (N); | |
3241 | P : constant Node_Id := Prefix (N); | |
3242 | T : constant Entity_Id := Etype (P); | |
3243 | ||
3244 | begin | |
3245 | -- A special optimization, if we have an indexed component that | |
3246 | -- is selecting from a slice, then we can eliminate the slice, | |
3247 | -- since, for example, x (i .. j)(k) is identical to x(k). The | |
3248 | -- only difference is the range check required by the slice. The | |
3249 | -- range check for the slice itself has already been generated. | |
3250 | -- The range check for the subscripting operation is ensured | |
3251 | -- by converting the subject to the subtype of the slice. | |
3252 | ||
3253 | -- This optimization not only generates better code, avoiding | |
3254 | -- slice messing especially in the packed case, but more importantly | |
3255 | -- bypasses some problems in handling this peculiar case, for | |
3256 | -- example, the issue of dealing specially with object renamings. | |
3257 | ||
3258 | if Nkind (P) = N_Slice then | |
3259 | Rewrite (N, | |
3260 | Make_Indexed_Component (Loc, | |
3261 | Prefix => Prefix (P), | |
3262 | Expressions => New_List ( | |
3263 | Convert_To | |
3264 | (Etype (First_Index (Etype (P))), | |
3265 | First (Expressions (N)))))); | |
3266 | Analyze_And_Resolve (N, Typ); | |
3267 | return; | |
3268 | end if; | |
3269 | ||
3270 | -- If the prefix is an access type, then we unconditionally rewrite | |
3271 | -- if as an explicit deference. This simplifies processing for several | |
3272 | -- cases, including packed array cases and certain cases in which | |
3273 | -- checks must be generated. We used to try to do this only when it | |
3274 | -- was necessary, but it cleans up the code to do it all the time. | |
3275 | ||
3276 | if Is_Access_Type (T) then | |
2717634d | 3277 | Insert_Explicit_Dereference (P); |
70482933 RK |
3278 | Analyze_And_Resolve (P, Designated_Type (T)); |
3279 | end if; | |
3280 | ||
fbf5a39b AC |
3281 | -- Generate index and validity checks |
3282 | ||
3283 | Generate_Index_Checks (N); | |
3284 | ||
70482933 RK |
3285 | if Validity_Checks_On and then Validity_Check_Subscripts then |
3286 | Apply_Subscript_Validity_Checks (N); | |
3287 | end if; | |
3288 | ||
3289 | -- All done for the non-packed case | |
3290 | ||
3291 | if not Is_Packed (Etype (Prefix (N))) then | |
3292 | return; | |
3293 | end if; | |
3294 | ||
3295 | -- For packed arrays that are not bit-packed (i.e. the case of an array | |
3296 | -- with one or more index types with a non-coniguous enumeration type), | |
3297 | -- we can always use the normal packed element get circuit. | |
3298 | ||
3299 | if not Is_Bit_Packed_Array (Etype (Prefix (N))) then | |
3300 | Expand_Packed_Element_Reference (N); | |
3301 | return; | |
3302 | end if; | |
3303 | ||
3304 | -- For a reference to a component of a bit packed array, we have to | |
3305 | -- convert it to a reference to the corresponding Packed_Array_Type. | |
3306 | -- We only want to do this for simple references, and not for: | |
3307 | ||
fbf5a39b AC |
3308 | -- Left side of assignment, or prefix of left side of assignment, |
3309 | -- or prefix of the prefix, to handle packed arrays of packed arrays, | |
70482933 RK |
3310 | -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement |
3311 | ||
3312 | -- Renaming objects in renaming associations | |
3313 | -- This case is handled when a use of the renamed variable occurs | |
3314 | ||
3315 | -- Actual parameters for a procedure call | |
3316 | -- This case is handled in Exp_Ch6.Expand_Actuals | |
3317 | ||
3318 | -- The second expression in a 'Read attribute reference | |
3319 | ||
3320 | -- The prefix of an address or size attribute reference | |
3321 | ||
3322 | -- The following circuit detects these exceptions | |
3323 | ||
3324 | declare | |
3325 | Child : Node_Id := N; | |
3326 | Parnt : Node_Id := Parent (N); | |
3327 | ||
3328 | begin | |
3329 | loop | |
3330 | if Nkind (Parnt) = N_Unchecked_Expression then | |
3331 | null; | |
3332 | ||
3333 | elsif Nkind (Parnt) = N_Object_Renaming_Declaration | |
3334 | or else Nkind (Parnt) = N_Procedure_Call_Statement | |
3335 | or else (Nkind (Parnt) = N_Parameter_Association | |
3336 | and then | |
3337 | Nkind (Parent (Parnt)) = N_Procedure_Call_Statement) | |
3338 | then | |
3339 | return; | |
3340 | ||
3341 | elsif Nkind (Parnt) = N_Attribute_Reference | |
3342 | and then (Attribute_Name (Parnt) = Name_Address | |
3343 | or else | |
3344 | Attribute_Name (Parnt) = Name_Size) | |
3345 | and then Prefix (Parnt) = Child | |
3346 | then | |
3347 | return; | |
3348 | ||
3349 | elsif Nkind (Parnt) = N_Assignment_Statement | |
3350 | and then Name (Parnt) = Child | |
3351 | then | |
3352 | return; | |
3353 | ||
fbf5a39b AC |
3354 | -- If the expression is an index of an indexed component, |
3355 | -- it must be expanded regardless of context. | |
3356 | ||
3357 | elsif Nkind (Parnt) = N_Indexed_Component | |
3358 | and then Child /= Prefix (Parnt) | |
3359 | then | |
3360 | Expand_Packed_Element_Reference (N); | |
3361 | return; | |
3362 | ||
3363 | elsif Nkind (Parent (Parnt)) = N_Assignment_Statement | |
3364 | and then Name (Parent (Parnt)) = Parnt | |
3365 | then | |
3366 | return; | |
3367 | ||
70482933 RK |
3368 | elsif Nkind (Parnt) = N_Attribute_Reference |
3369 | and then Attribute_Name (Parnt) = Name_Read | |
3370 | and then Next (First (Expressions (Parnt))) = Child | |
3371 | then | |
3372 | return; | |
3373 | ||
3374 | elsif (Nkind (Parnt) = N_Indexed_Component | |
3375 | or else Nkind (Parnt) = N_Selected_Component) | |
3376 | and then Prefix (Parnt) = Child | |
3377 | then | |
3378 | null; | |
3379 | ||
3380 | else | |
3381 | Expand_Packed_Element_Reference (N); | |
3382 | return; | |
3383 | end if; | |
3384 | ||
3385 | -- Keep looking up tree for unchecked expression, or if we are | |
3386 | -- the prefix of a possible assignment left side. | |
3387 | ||
3388 | Child := Parnt; | |
3389 | Parnt := Parent (Child); | |
3390 | end loop; | |
3391 | end; | |
3392 | ||
3393 | end Expand_N_Indexed_Component; | |
3394 | ||
3395 | --------------------- | |
3396 | -- Expand_N_Not_In -- | |
3397 | --------------------- | |
3398 | ||
3399 | -- Replace a not in b by not (a in b) so that the expansions for (a in b) | |
3400 | -- can be done. This avoids needing to duplicate this expansion code. | |
3401 | ||
3402 | procedure Expand_N_Not_In (N : Node_Id) is | |
3403 | Loc : constant Source_Ptr := Sloc (N); | |
3404 | Typ : constant Entity_Id := Etype (N); | |
3405 | ||
3406 | begin | |
3407 | Rewrite (N, | |
3408 | Make_Op_Not (Loc, | |
3409 | Right_Opnd => | |
3410 | Make_In (Loc, | |
3411 | Left_Opnd => Left_Opnd (N), | |
3412 | Right_Opnd => Right_Opnd (N)))); | |
3413 | Analyze_And_Resolve (N, Typ); | |
3414 | end Expand_N_Not_In; | |
3415 | ||
3416 | ------------------- | |
3417 | -- Expand_N_Null -- | |
3418 | ------------------- | |
3419 | ||
3420 | -- The only replacement required is for the case of a null of type | |
3421 | -- that is an access to protected subprogram. We represent such | |
3422 | -- access values as a record, and so we must replace the occurrence | |
3423 | -- of null by the equivalent record (with a null address and a null | |
3424 | -- pointer in it), so that the backend creates the proper value. | |
3425 | ||
3426 | procedure Expand_N_Null (N : Node_Id) is | |
3427 | Loc : constant Source_Ptr := Sloc (N); | |
3428 | Typ : constant Entity_Id := Etype (N); | |
3429 | Agg : Node_Id; | |
3430 | ||
3431 | begin | |
3432 | if Ekind (Typ) = E_Access_Protected_Subprogram_Type then | |
3433 | Agg := | |
3434 | Make_Aggregate (Loc, | |
3435 | Expressions => New_List ( | |
3436 | New_Occurrence_Of (RTE (RE_Null_Address), Loc), | |
3437 | Make_Null (Loc))); | |
3438 | ||
3439 | Rewrite (N, Agg); | |
3440 | Analyze_And_Resolve (N, Equivalent_Type (Typ)); | |
3441 | ||
3442 | -- For subsequent semantic analysis, the node must retain its | |
3443 | -- type. Gigi in any case replaces this type by the corresponding | |
3444 | -- record type before processing the node. | |
3445 | ||
3446 | Set_Etype (N, Typ); | |
3447 | end if; | |
fbf5a39b AC |
3448 | |
3449 | exception | |
3450 | when RE_Not_Available => | |
3451 | return; | |
70482933 RK |
3452 | end Expand_N_Null; |
3453 | ||
3454 | --------------------- | |
3455 | -- Expand_N_Op_Abs -- | |
3456 | --------------------- | |
3457 | ||
3458 | procedure Expand_N_Op_Abs (N : Node_Id) is | |
3459 | Loc : constant Source_Ptr := Sloc (N); | |
3460 | Expr : constant Node_Id := Right_Opnd (N); | |
3461 | ||
3462 | begin | |
3463 | Unary_Op_Validity_Checks (N); | |
3464 | ||
3465 | -- Deal with software overflow checking | |
3466 | ||
07fc65c4 | 3467 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
3468 | and then Is_Signed_Integer_Type (Etype (N)) |
3469 | and then Do_Overflow_Check (N) | |
3470 | then | |
fbf5a39b AC |
3471 | -- The only case to worry about is when the argument is |
3472 | -- equal to the largest negative number, so what we do is | |
3473 | -- to insert the check: | |
70482933 | 3474 | |
fbf5a39b | 3475 | -- [constraint_error when Expr = typ'Base'First] |
70482933 RK |
3476 | |
3477 | -- with the usual Duplicate_Subexpr use coding for expr | |
3478 | ||
fbf5a39b AC |
3479 | Insert_Action (N, |
3480 | Make_Raise_Constraint_Error (Loc, | |
3481 | Condition => | |
3482 | Make_Op_Eq (Loc, | |
70482933 | 3483 | Left_Opnd => Duplicate_Subexpr (Expr), |
fbf5a39b AC |
3484 | Right_Opnd => |
3485 | Make_Attribute_Reference (Loc, | |
3486 | Prefix => | |
3487 | New_Occurrence_Of (Base_Type (Etype (Expr)), Loc), | |
3488 | Attribute_Name => Name_First)), | |
3489 | Reason => CE_Overflow_Check_Failed)); | |
3490 | end if; | |
70482933 RK |
3491 | |
3492 | -- Vax floating-point types case | |
3493 | ||
fbf5a39b | 3494 | if Vax_Float (Etype (N)) then |
70482933 RK |
3495 | Expand_Vax_Arith (N); |
3496 | end if; | |
3497 | end Expand_N_Op_Abs; | |
3498 | ||
3499 | --------------------- | |
3500 | -- Expand_N_Op_Add -- | |
3501 | --------------------- | |
3502 | ||
3503 | procedure Expand_N_Op_Add (N : Node_Id) is | |
3504 | Typ : constant Entity_Id := Etype (N); | |
3505 | ||
3506 | begin | |
3507 | Binary_Op_Validity_Checks (N); | |
3508 | ||
3509 | -- N + 0 = 0 + N = N for integer types | |
3510 | ||
3511 | if Is_Integer_Type (Typ) then | |
3512 | if Compile_Time_Known_Value (Right_Opnd (N)) | |
3513 | and then Expr_Value (Right_Opnd (N)) = Uint_0 | |
3514 | then | |
3515 | Rewrite (N, Left_Opnd (N)); | |
3516 | return; | |
3517 | ||
3518 | elsif Compile_Time_Known_Value (Left_Opnd (N)) | |
3519 | and then Expr_Value (Left_Opnd (N)) = Uint_0 | |
3520 | then | |
3521 | Rewrite (N, Right_Opnd (N)); | |
3522 | return; | |
3523 | end if; | |
3524 | end if; | |
3525 | ||
fbf5a39b | 3526 | -- Arithmetic overflow checks for signed integer/fixed point types |
70482933 RK |
3527 | |
3528 | if Is_Signed_Integer_Type (Typ) | |
3529 | or else Is_Fixed_Point_Type (Typ) | |
3530 | then | |
3531 | Apply_Arithmetic_Overflow_Check (N); | |
3532 | return; | |
3533 | ||
3534 | -- Vax floating-point types case | |
3535 | ||
3536 | elsif Vax_Float (Typ) then | |
3537 | Expand_Vax_Arith (N); | |
3538 | end if; | |
3539 | end Expand_N_Op_Add; | |
3540 | ||
3541 | --------------------- | |
3542 | -- Expand_N_Op_And -- | |
3543 | --------------------- | |
3544 | ||
3545 | procedure Expand_N_Op_And (N : Node_Id) is | |
3546 | Typ : constant Entity_Id := Etype (N); | |
3547 | ||
3548 | begin | |
3549 | Binary_Op_Validity_Checks (N); | |
3550 | ||
3551 | if Is_Array_Type (Etype (N)) then | |
3552 | Expand_Boolean_Operator (N); | |
3553 | ||
3554 | elsif Is_Boolean_Type (Etype (N)) then | |
3555 | Adjust_Condition (Left_Opnd (N)); | |
3556 | Adjust_Condition (Right_Opnd (N)); | |
3557 | Set_Etype (N, Standard_Boolean); | |
3558 | Adjust_Result_Type (N, Typ); | |
3559 | end if; | |
3560 | end Expand_N_Op_And; | |
3561 | ||
3562 | ------------------------ | |
3563 | -- Expand_N_Op_Concat -- | |
3564 | ------------------------ | |
3565 | ||
fbf5a39b AC |
3566 | Max_Available_String_Operands : Int := -1; |
3567 | -- This is initialized the first time this routine is called. It records | |
3568 | -- a value of 0,2,3,4,5 depending on what Str_Concat_n procedures are | |
3569 | -- available in the run-time: | |
3570 | -- | |
3571 | -- 0 None available | |
3572 | -- 2 RE_Str_Concat available, RE_Str_Concat_3 not available | |
3573 | -- 3 RE_Str_Concat/Concat_2 available, RE_Str_Concat_4 not available | |
3574 | -- 4 RE_Str_Concat/Concat_2/3 available, RE_Str_Concat_5 not available | |
3575 | -- 5 All routines including RE_Str_Concat_5 available | |
3576 | ||
3577 | Char_Concat_Available : Boolean; | |
3578 | -- Records if the routines RE_Str_Concat_CC/CS/SC are available. True if | |
3579 | -- all three are available, False if any one of these is unavailable. | |
3580 | ||
70482933 | 3581 | procedure Expand_N_Op_Concat (N : Node_Id) is |
70482933 RK |
3582 | Opnds : List_Id; |
3583 | -- List of operands to be concatenated | |
3584 | ||
3585 | Opnd : Node_Id; | |
3586 | -- Single operand for concatenation | |
3587 | ||
3588 | Cnode : Node_Id; | |
3589 | -- Node which is to be replaced by the result of concatenating | |
3590 | -- the nodes in the list Opnds. | |
3591 | ||
3592 | Atyp : Entity_Id; | |
3593 | -- Array type of concatenation result type | |
3594 | ||
3595 | Ctyp : Entity_Id; | |
3596 | -- Component type of concatenation represented by Cnode | |
3597 | ||
3598 | begin | |
fbf5a39b AC |
3599 | -- Initialize global variables showing run-time status |
3600 | ||
3601 | if Max_Available_String_Operands < 1 then | |
3602 | if not RTE_Available (RE_Str_Concat) then | |
3603 | Max_Available_String_Operands := 0; | |
3604 | elsif not RTE_Available (RE_Str_Concat_3) then | |
3605 | Max_Available_String_Operands := 2; | |
3606 | elsif not RTE_Available (RE_Str_Concat_4) then | |
3607 | Max_Available_String_Operands := 3; | |
3608 | elsif not RTE_Available (RE_Str_Concat_5) then | |
3609 | Max_Available_String_Operands := 4; | |
3610 | else | |
3611 | Max_Available_String_Operands := 5; | |
3612 | end if; | |
3613 | ||
3614 | Char_Concat_Available := | |
3615 | RTE_Available (RE_Str_Concat_CC) | |
3616 | and then | |
3617 | RTE_Available (RE_Str_Concat_CS) | |
3618 | and then | |
3619 | RTE_Available (RE_Str_Concat_SC); | |
3620 | end if; | |
3621 | ||
3622 | -- Ensure validity of both operands | |
3623 | ||
70482933 RK |
3624 | Binary_Op_Validity_Checks (N); |
3625 | ||
3626 | -- If we are the left operand of a concatenation higher up the | |
3627 | -- tree, then do nothing for now, since we want to deal with a | |
3628 | -- series of concatenations as a unit. | |
3629 | ||
3630 | if Nkind (Parent (N)) = N_Op_Concat | |
3631 | and then N = Left_Opnd (Parent (N)) | |
3632 | then | |
3633 | return; | |
3634 | end if; | |
3635 | ||
3636 | -- We get here with a concatenation whose left operand may be a | |
3637 | -- concatenation itself with a consistent type. We need to process | |
3638 | -- these concatenation operands from left to right, which means | |
3639 | -- from the deepest node in the tree to the highest node. | |
3640 | ||
3641 | Cnode := N; | |
3642 | while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop | |
3643 | Cnode := Left_Opnd (Cnode); | |
3644 | end loop; | |
3645 | ||
3646 | -- Now Opnd is the deepest Opnd, and its parents are the concatenation | |
3647 | -- nodes above, so now we process bottom up, doing the operations. We | |
3648 | -- gather a string that is as long as possible up to five operands | |
3649 | ||
3650 | -- The outer loop runs more than once if there are more than five | |
3651 | -- concatenations of type Standard.String, the most we handle for | |
3652 | -- this case, or if more than one concatenation type is involved. | |
3653 | ||
3654 | Outer : loop | |
3655 | Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode)); | |
3656 | Set_Parent (Opnds, N); | |
3657 | ||
fbf5a39b AC |
3658 | -- The inner loop gathers concatenation operands. We gather any |
3659 | -- number of these in the non-string case, or if no concatenation | |
3660 | -- routines are available for string (since in that case we will | |
3661 | -- treat string like any other non-string case). Otherwise we only | |
3662 | -- gather as many operands as can be handled by the available | |
3663 | -- procedures in the run-time library (normally 5, but may be | |
3664 | -- less for the configurable run-time case). | |
70482933 RK |
3665 | |
3666 | Inner : while Cnode /= N | |
3667 | and then (Base_Type (Etype (Cnode)) /= Standard_String | |
3668 | or else | |
fbf5a39b AC |
3669 | Max_Available_String_Operands = 0 |
3670 | or else | |
3671 | List_Length (Opnds) < | |
3672 | Max_Available_String_Operands) | |
70482933 RK |
3673 | and then Base_Type (Etype (Cnode)) = |
3674 | Base_Type (Etype (Parent (Cnode))) | |
3675 | loop | |
3676 | Cnode := Parent (Cnode); | |
3677 | Append (Right_Opnd (Cnode), Opnds); | |
3678 | end loop Inner; | |
3679 | ||
3680 | -- Here we process the collected operands. First we convert | |
3681 | -- singleton operands to singleton aggregates. This is skipped | |
3682 | -- however for the case of two operands of type String, since | |
3683 | -- we have special routines for these cases. | |
3684 | ||
3685 | Atyp := Base_Type (Etype (Cnode)); | |
3686 | Ctyp := Base_Type (Component_Type (Etype (Cnode))); | |
3687 | ||
fbf5a39b AC |
3688 | if (List_Length (Opnds) > 2 or else Atyp /= Standard_String) |
3689 | or else not Char_Concat_Available | |
3690 | then | |
70482933 RK |
3691 | Opnd := First (Opnds); |
3692 | loop | |
3693 | if Base_Type (Etype (Opnd)) = Ctyp then | |
3694 | Rewrite (Opnd, | |
3695 | Make_Aggregate (Sloc (Cnode), | |
3696 | Expressions => New_List (Relocate_Node (Opnd)))); | |
3697 | Analyze_And_Resolve (Opnd, Atyp); | |
3698 | end if; | |
3699 | ||
3700 | Next (Opnd); | |
3701 | exit when No (Opnd); | |
3702 | end loop; | |
3703 | end if; | |
3704 | ||
3705 | -- Now call appropriate continuation routine | |
3706 | ||
fbf5a39b AC |
3707 | if Atyp = Standard_String |
3708 | and then Max_Available_String_Operands > 0 | |
3709 | then | |
70482933 RK |
3710 | Expand_Concatenate_String (Cnode, Opnds); |
3711 | else | |
3712 | Expand_Concatenate_Other (Cnode, Opnds); | |
3713 | end if; | |
3714 | ||
3715 | exit Outer when Cnode = N; | |
3716 | Cnode := Parent (Cnode); | |
3717 | end loop Outer; | |
3718 | end Expand_N_Op_Concat; | |
3719 | ||
3720 | ------------------------ | |
3721 | -- Expand_N_Op_Divide -- | |
3722 | ------------------------ | |
3723 | ||
3724 | procedure Expand_N_Op_Divide (N : Node_Id) is | |
3725 | Loc : constant Source_Ptr := Sloc (N); | |
3726 | Ltyp : constant Entity_Id := Etype (Left_Opnd (N)); | |
3727 | Rtyp : constant Entity_Id := Etype (Right_Opnd (N)); | |
3728 | Typ : Entity_Id := Etype (N); | |
3729 | ||
3730 | begin | |
3731 | Binary_Op_Validity_Checks (N); | |
3732 | ||
3733 | -- Vax_Float is a special case | |
3734 | ||
3735 | if Vax_Float (Typ) then | |
3736 | Expand_Vax_Arith (N); | |
3737 | return; | |
3738 | end if; | |
3739 | ||
3740 | -- N / 1 = N for integer types | |
3741 | ||
3742 | if Is_Integer_Type (Typ) | |
3743 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
3744 | and then Expr_Value (Right_Opnd (N)) = Uint_1 | |
3745 | then | |
3746 | Rewrite (N, Left_Opnd (N)); | |
3747 | return; | |
3748 | end if; | |
3749 | ||
3750 | -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that | |
3751 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
3752 | -- operand is an unsigned integer, as required for this to work. | |
3753 | ||
3754 | if Nkind (Right_Opnd (N)) = N_Op_Expon | |
3755 | and then Is_Power_Of_2_For_Shift (Right_Opnd (N)) | |
fbf5a39b AC |
3756 | |
3757 | -- We cannot do this transformation in configurable run time mode if we | |
3758 | -- have 64-bit -- integers and long shifts are not available. | |
3759 | ||
3760 | and then | |
3761 | (Esize (Ltyp) <= 32 | |
3762 | or else Support_Long_Shifts_On_Target) | |
70482933 RK |
3763 | then |
3764 | Rewrite (N, | |
3765 | Make_Op_Shift_Right (Loc, | |
3766 | Left_Opnd => Left_Opnd (N), | |
3767 | Right_Opnd => | |
3768 | Convert_To (Standard_Natural, Right_Opnd (Right_Opnd (N))))); | |
3769 | Analyze_And_Resolve (N, Typ); | |
3770 | return; | |
3771 | end if; | |
3772 | ||
3773 | -- Do required fixup of universal fixed operation | |
3774 | ||
3775 | if Typ = Universal_Fixed then | |
3776 | Fixup_Universal_Fixed_Operation (N); | |
3777 | Typ := Etype (N); | |
3778 | end if; | |
3779 | ||
3780 | -- Divisions with fixed-point results | |
3781 | ||
3782 | if Is_Fixed_Point_Type (Typ) then | |
3783 | ||
3784 | -- No special processing if Treat_Fixed_As_Integer is set, | |
3785 | -- since from a semantic point of view such operations are | |
3786 | -- simply integer operations and will be treated that way. | |
3787 | ||
3788 | if not Treat_Fixed_As_Integer (N) then | |
3789 | if Is_Integer_Type (Rtyp) then | |
3790 | Expand_Divide_Fixed_By_Integer_Giving_Fixed (N); | |
3791 | else | |
3792 | Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N); | |
3793 | end if; | |
3794 | end if; | |
3795 | ||
3796 | -- Other cases of division of fixed-point operands. Again we | |
3797 | -- exclude the case where Treat_Fixed_As_Integer is set. | |
3798 | ||
3799 | elsif (Is_Fixed_Point_Type (Ltyp) or else | |
3800 | Is_Fixed_Point_Type (Rtyp)) | |
3801 | and then not Treat_Fixed_As_Integer (N) | |
3802 | then | |
3803 | if Is_Integer_Type (Typ) then | |
3804 | Expand_Divide_Fixed_By_Fixed_Giving_Integer (N); | |
3805 | else | |
3806 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
3807 | Expand_Divide_Fixed_By_Fixed_Giving_Float (N); | |
3808 | end if; | |
3809 | ||
3810 | -- Mixed-mode operations can appear in a non-static universal | |
3811 | -- context, in which case the integer argument must be converted | |
3812 | -- explicitly. | |
3813 | ||
3814 | elsif Typ = Universal_Real | |
3815 | and then Is_Integer_Type (Rtyp) | |
3816 | then | |
3817 | Rewrite (Right_Opnd (N), | |
3818 | Convert_To (Universal_Real, Relocate_Node (Right_Opnd (N)))); | |
3819 | ||
3820 | Analyze_And_Resolve (Right_Opnd (N), Universal_Real); | |
3821 | ||
3822 | elsif Typ = Universal_Real | |
3823 | and then Is_Integer_Type (Ltyp) | |
3824 | then | |
3825 | Rewrite (Left_Opnd (N), | |
3826 | Convert_To (Universal_Real, Relocate_Node (Left_Opnd (N)))); | |
3827 | ||
3828 | Analyze_And_Resolve (Left_Opnd (N), Universal_Real); | |
3829 | ||
3830 | -- Non-fixed point cases, do zero divide and overflow checks | |
3831 | ||
3832 | elsif Is_Integer_Type (Typ) then | |
3833 | Apply_Divide_Check (N); | |
fbf5a39b AC |
3834 | |
3835 | -- Check for 64-bit division available | |
3836 | ||
3837 | if Esize (Ltyp) > 32 | |
3838 | and then not Support_64_Bit_Divides_On_Target | |
3839 | then | |
3840 | Error_Msg_CRT ("64-bit division", N); | |
3841 | end if; | |
70482933 RK |
3842 | end if; |
3843 | end Expand_N_Op_Divide; | |
3844 | ||
3845 | -------------------- | |
3846 | -- Expand_N_Op_Eq -- | |
3847 | -------------------- | |
3848 | ||
3849 | procedure Expand_N_Op_Eq (N : Node_Id) is | |
fbf5a39b AC |
3850 | Loc : constant Source_Ptr := Sloc (N); |
3851 | Typ : constant Entity_Id := Etype (N); | |
3852 | Lhs : constant Node_Id := Left_Opnd (N); | |
3853 | Rhs : constant Node_Id := Right_Opnd (N); | |
3854 | Bodies : constant List_Id := New_List; | |
3855 | A_Typ : constant Entity_Id := Etype (Lhs); | |
3856 | ||
70482933 RK |
3857 | Typl : Entity_Id := A_Typ; |
3858 | Op_Name : Entity_Id; | |
3859 | Prim : Elmt_Id; | |
70482933 RK |
3860 | |
3861 | procedure Build_Equality_Call (Eq : Entity_Id); | |
3862 | -- If a constructed equality exists for the type or for its parent, | |
3863 | -- build and analyze call, adding conversions if the operation is | |
3864 | -- inherited. | |
3865 | ||
5d09245e AC |
3866 | function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean; |
3867 | -- Determines whether a type has a subcompoment of an unconstrained | |
3868 | -- Unchecked_Union subtype. Typ is a record type. | |
3869 | ||
70482933 RK |
3870 | ------------------------- |
3871 | -- Build_Equality_Call -- | |
3872 | ------------------------- | |
3873 | ||
3874 | procedure Build_Equality_Call (Eq : Entity_Id) is | |
3875 | Op_Type : constant Entity_Id := Etype (First_Formal (Eq)); | |
3876 | L_Exp : Node_Id := Relocate_Node (Lhs); | |
3877 | R_Exp : Node_Id := Relocate_Node (Rhs); | |
3878 | ||
3879 | begin | |
3880 | if Base_Type (Op_Type) /= Base_Type (A_Typ) | |
3881 | and then not Is_Class_Wide_Type (A_Typ) | |
3882 | then | |
3883 | L_Exp := OK_Convert_To (Op_Type, L_Exp); | |
3884 | R_Exp := OK_Convert_To (Op_Type, R_Exp); | |
3885 | end if; | |
3886 | ||
5d09245e AC |
3887 | -- If we have an Unchecked_Union, we need to add the inferred |
3888 | -- discriminant values as actuals in the function call. At this | |
3889 | -- point, the expansion has determined that both operands have | |
3890 | -- inferable discriminants. | |
3891 | ||
3892 | if Is_Unchecked_Union (Op_Type) then | |
3893 | declare | |
3894 | Lhs_Type : constant Node_Id := Etype (L_Exp); | |
3895 | Rhs_Type : constant Node_Id := Etype (R_Exp); | |
3896 | Lhs_Discr_Val : Node_Id; | |
3897 | Rhs_Discr_Val : Node_Id; | |
3898 | ||
3899 | begin | |
3900 | -- Per-object constrained selected components require special | |
3901 | -- attention. If the enclosing scope of the component is an | |
3902 | -- Unchecked_Union, we can not reference its discriminants | |
3903 | -- directly. This is why we use the two extra parameters of | |
3904 | -- the equality function of the enclosing Unchecked_Union. | |
3905 | ||
3906 | -- type UU_Type (Discr : Integer := 0) is | |
3907 | -- . . . | |
3908 | -- end record; | |
3909 | -- pragma Unchecked_Union (UU_Type); | |
3910 | ||
3911 | -- 1. Unchecked_Union enclosing record: | |
3912 | ||
3913 | -- type Enclosing_UU_Type (Discr : Integer := 0) is record | |
3914 | -- . . . | |
3915 | -- Comp : UU_Type (Discr); | |
3916 | -- . . . | |
3917 | -- end Enclosing_UU_Type; | |
3918 | -- pragma Unchecked_Union (Enclosing_UU_Type); | |
3919 | ||
3920 | -- Obj1 : Enclosing_UU_Type; | |
3921 | -- Obj2 : Enclosing_UU_Type (1); | |
3922 | ||
2717634d | 3923 | -- [. . .] Obj1 = Obj2 [. . .] |
5d09245e AC |
3924 | |
3925 | -- Generated code: | |
3926 | ||
3927 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then | |
3928 | ||
3929 | -- A and B are the formal parameters of the equality function | |
3930 | -- of Enclosing_UU_Type. The function always has two extra | |
3931 | -- formals to capture the inferred discriminant values. | |
3932 | ||
3933 | -- 2. Non-Unchecked_Union enclosing record: | |
3934 | ||
3935 | -- type | |
3936 | -- Enclosing_Non_UU_Type (Discr : Integer := 0) | |
3937 | -- is record | |
3938 | -- . . . | |
3939 | -- Comp : UU_Type (Discr); | |
3940 | -- . . . | |
3941 | -- end Enclosing_Non_UU_Type; | |
3942 | ||
3943 | -- Obj1 : Enclosing_Non_UU_Type; | |
3944 | -- Obj2 : Enclosing_Non_UU_Type (1); | |
3945 | ||
3946 | -- . . . Obj1 = Obj2 . . . | |
3947 | ||
3948 | -- Generated code: | |
3949 | ||
3950 | -- if not (uu_typeEQ (obj1.comp, obj2.comp, | |
3951 | -- obj1.discr, obj2.discr)) then | |
3952 | ||
3953 | -- In this case we can directly reference the discriminants of | |
3954 | -- the enclosing record. | |
3955 | ||
3956 | -- Lhs of equality | |
3957 | ||
3958 | if Nkind (Lhs) = N_Selected_Component | |
5e1c00fa RD |
3959 | and then Has_Per_Object_Constraint |
3960 | (Entity (Selector_Name (Lhs))) | |
5d09245e AC |
3961 | then |
3962 | -- Enclosing record is an Unchecked_Union, use formal A | |
3963 | ||
3964 | if Is_Unchecked_Union (Scope | |
3965 | (Entity (Selector_Name (Lhs)))) | |
3966 | then | |
3967 | Lhs_Discr_Val := | |
3968 | Make_Identifier (Loc, | |
3969 | Chars => Name_A); | |
3970 | ||
3971 | -- Enclosing record is of a non-Unchecked_Union type, it is | |
3972 | -- possible to reference the discriminant. | |
3973 | ||
3974 | else | |
3975 | Lhs_Discr_Val := | |
3976 | Make_Selected_Component (Loc, | |
3977 | Prefix => Prefix (Lhs), | |
3978 | Selector_Name => | |
5e1c00fa RD |
3979 | New_Copy |
3980 | (Get_Discriminant_Value | |
3981 | (First_Discriminant (Lhs_Type), | |
3982 | Lhs_Type, | |
3983 | Stored_Constraint (Lhs_Type)))); | |
5d09245e AC |
3984 | end if; |
3985 | ||
3986 | -- Comment needed here ??? | |
3987 | ||
3988 | else | |
3989 | -- Infer the discriminant value | |
3990 | ||
3991 | Lhs_Discr_Val := | |
5e1c00fa RD |
3992 | New_Copy |
3993 | (Get_Discriminant_Value | |
3994 | (First_Discriminant (Lhs_Type), | |
3995 | Lhs_Type, | |
3996 | Stored_Constraint (Lhs_Type))); | |
5d09245e AC |
3997 | end if; |
3998 | ||
3999 | -- Rhs of equality | |
4000 | ||
4001 | if Nkind (Rhs) = N_Selected_Component | |
5e1c00fa RD |
4002 | and then Has_Per_Object_Constraint |
4003 | (Entity (Selector_Name (Rhs))) | |
5d09245e | 4004 | then |
5e1c00fa RD |
4005 | if Is_Unchecked_Union |
4006 | (Scope (Entity (Selector_Name (Rhs)))) | |
5d09245e AC |
4007 | then |
4008 | Rhs_Discr_Val := | |
4009 | Make_Identifier (Loc, | |
4010 | Chars => Name_B); | |
4011 | ||
4012 | else | |
4013 | Rhs_Discr_Val := | |
4014 | Make_Selected_Component (Loc, | |
4015 | Prefix => Prefix (Rhs), | |
4016 | Selector_Name => | |
4017 | New_Copy (Get_Discriminant_Value ( | |
4018 | First_Discriminant (Rhs_Type), | |
4019 | Rhs_Type, | |
4020 | Stored_Constraint (Rhs_Type)))); | |
4021 | ||
4022 | end if; | |
4023 | else | |
4024 | Rhs_Discr_Val := | |
4025 | New_Copy (Get_Discriminant_Value ( | |
4026 | First_Discriminant (Rhs_Type), | |
4027 | Rhs_Type, | |
4028 | Stored_Constraint (Rhs_Type))); | |
4029 | ||
4030 | end if; | |
4031 | ||
4032 | Rewrite (N, | |
4033 | Make_Function_Call (Loc, | |
4034 | Name => New_Reference_To (Eq, Loc), | |
4035 | Parameter_Associations => New_List ( | |
4036 | L_Exp, | |
4037 | R_Exp, | |
4038 | Lhs_Discr_Val, | |
4039 | Rhs_Discr_Val))); | |
4040 | end; | |
4041 | ||
4042 | -- Normal case, not an unchecked union | |
4043 | ||
4044 | else | |
4045 | Rewrite (N, | |
4046 | Make_Function_Call (Loc, | |
4047 | Name => New_Reference_To (Eq, Loc), | |
4048 | Parameter_Associations => New_List (L_Exp, R_Exp))); | |
4049 | end if; | |
70482933 RK |
4050 | |
4051 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
4052 | end Build_Equality_Call; | |
4053 | ||
5d09245e AC |
4054 | ------------------------------------ |
4055 | -- Has_Unconstrained_UU_Component -- | |
4056 | ------------------------------------ | |
4057 | ||
4058 | function Has_Unconstrained_UU_Component | |
4059 | (Typ : Node_Id) return Boolean | |
4060 | is | |
4061 | Tdef : constant Node_Id := | |
4062 | Type_Definition (Declaration_Node (Typ)); | |
4063 | Clist : Node_Id; | |
4064 | Vpart : Node_Id; | |
4065 | ||
4066 | function Component_Is_Unconstrained_UU | |
4067 | (Comp : Node_Id) return Boolean; | |
4068 | -- Determines whether the subtype of the component is an | |
4069 | -- unconstrained Unchecked_Union. | |
4070 | ||
4071 | function Variant_Is_Unconstrained_UU | |
4072 | (Variant : Node_Id) return Boolean; | |
4073 | -- Determines whether a component of the variant has an unconstrained | |
4074 | -- Unchecked_Union subtype. | |
4075 | ||
4076 | ----------------------------------- | |
4077 | -- Component_Is_Unconstrained_UU -- | |
4078 | ----------------------------------- | |
4079 | ||
4080 | function Component_Is_Unconstrained_UU | |
4081 | (Comp : Node_Id) return Boolean | |
4082 | is | |
4083 | begin | |
4084 | if Nkind (Comp) /= N_Component_Declaration then | |
4085 | return False; | |
4086 | end if; | |
4087 | ||
4088 | declare | |
4089 | Sindic : constant Node_Id := | |
4090 | Subtype_Indication (Component_Definition (Comp)); | |
4091 | ||
4092 | begin | |
4093 | -- Unconstrained nominal type. In the case of a constraint | |
4094 | -- present, the node kind would have been N_Subtype_Indication. | |
4095 | ||
4096 | if Nkind (Sindic) = N_Identifier then | |
4097 | return Is_Unchecked_Union (Base_Type (Etype (Sindic))); | |
4098 | end if; | |
4099 | ||
4100 | return False; | |
4101 | end; | |
4102 | end Component_Is_Unconstrained_UU; | |
4103 | ||
4104 | --------------------------------- | |
4105 | -- Variant_Is_Unconstrained_UU -- | |
4106 | --------------------------------- | |
4107 | ||
4108 | function Variant_Is_Unconstrained_UU | |
4109 | (Variant : Node_Id) return Boolean | |
4110 | is | |
4111 | Clist : constant Node_Id := Component_List (Variant); | |
4112 | ||
4113 | begin | |
4114 | if Is_Empty_List (Component_Items (Clist)) then | |
4115 | return False; | |
4116 | end if; | |
4117 | ||
4118 | declare | |
4119 | Comp : Node_Id := First (Component_Items (Clist)); | |
4120 | ||
4121 | begin | |
4122 | while Present (Comp) loop | |
4123 | ||
4124 | -- One component is sufficent | |
4125 | ||
4126 | if Component_Is_Unconstrained_UU (Comp) then | |
4127 | return True; | |
4128 | end if; | |
4129 | ||
4130 | Next (Comp); | |
4131 | end loop; | |
4132 | end; | |
4133 | ||
4134 | -- None of the components withing the variant were of | |
4135 | -- unconstrained Unchecked_Union type. | |
4136 | ||
4137 | return False; | |
4138 | end Variant_Is_Unconstrained_UU; | |
4139 | ||
4140 | -- Start of processing for Has_Unconstrained_UU_Component | |
4141 | ||
4142 | begin | |
4143 | if Null_Present (Tdef) then | |
4144 | return False; | |
4145 | end if; | |
4146 | ||
4147 | Clist := Component_List (Tdef); | |
4148 | Vpart := Variant_Part (Clist); | |
4149 | ||
4150 | -- Inspect available components | |
4151 | ||
4152 | if Present (Component_Items (Clist)) then | |
4153 | declare | |
4154 | Comp : Node_Id := First (Component_Items (Clist)); | |
4155 | ||
4156 | begin | |
4157 | while Present (Comp) loop | |
4158 | ||
4159 | -- One component is sufficent | |
4160 | ||
4161 | if Component_Is_Unconstrained_UU (Comp) then | |
4162 | return True; | |
4163 | end if; | |
4164 | ||
4165 | Next (Comp); | |
4166 | end loop; | |
4167 | end; | |
4168 | end if; | |
4169 | ||
4170 | -- Inspect available components withing variants | |
4171 | ||
4172 | if Present (Vpart) then | |
4173 | declare | |
4174 | Variant : Node_Id := First (Variants (Vpart)); | |
4175 | ||
4176 | begin | |
4177 | while Present (Variant) loop | |
4178 | ||
4179 | -- One component within a variant is sufficent | |
4180 | ||
4181 | if Variant_Is_Unconstrained_UU (Variant) then | |
4182 | return True; | |
4183 | end if; | |
4184 | ||
4185 | Next (Variant); | |
4186 | end loop; | |
4187 | end; | |
4188 | end if; | |
4189 | ||
4190 | -- Neither the available components, nor the components inside the | |
4191 | -- variant parts were of an unconstrained Unchecked_Union subtype. | |
4192 | ||
4193 | return False; | |
4194 | end Has_Unconstrained_UU_Component; | |
4195 | ||
70482933 RK |
4196 | -- Start of processing for Expand_N_Op_Eq |
4197 | ||
4198 | begin | |
4199 | Binary_Op_Validity_Checks (N); | |
4200 | ||
4201 | if Ekind (Typl) = E_Private_Type then | |
4202 | Typl := Underlying_Type (Typl); | |
4203 | ||
4204 | elsif Ekind (Typl) = E_Private_Subtype then | |
4205 | Typl := Underlying_Type (Base_Type (Typl)); | |
4206 | end if; | |
4207 | ||
4208 | -- It may happen in error situations that the underlying type is not | |
4209 | -- set. The error will be detected later, here we just defend the | |
4210 | -- expander code. | |
4211 | ||
4212 | if No (Typl) then | |
4213 | return; | |
4214 | end if; | |
4215 | ||
4216 | Typl := Base_Type (Typl); | |
4217 | ||
4218 | -- Vax float types | |
4219 | ||
4220 | if Vax_Float (Typl) then | |
4221 | Expand_Vax_Comparison (N); | |
4222 | return; | |
4223 | ||
4224 | -- Boolean types (requiring handling of non-standard case) | |
4225 | ||
4226 | elsif Is_Boolean_Type (Typl) then | |
4227 | Adjust_Condition (Left_Opnd (N)); | |
4228 | Adjust_Condition (Right_Opnd (N)); | |
4229 | Set_Etype (N, Standard_Boolean); | |
4230 | Adjust_Result_Type (N, Typ); | |
4231 | ||
4232 | -- Array types | |
4233 | ||
4234 | elsif Is_Array_Type (Typl) then | |
4235 | ||
fbf5a39b AC |
4236 | -- If we are doing full validity checking, then expand out array |
4237 | -- comparisons to make sure that we check the array elements. | |
4238 | ||
4239 | if Validity_Check_Operands then | |
4240 | declare | |
4241 | Save_Force_Validity_Checks : constant Boolean := | |
4242 | Force_Validity_Checks; | |
4243 | begin | |
4244 | Force_Validity_Checks := True; | |
4245 | Rewrite (N, | |
0da2c8ac AC |
4246 | Expand_Array_Equality |
4247 | (N, | |
4248 | Relocate_Node (Lhs), | |
4249 | Relocate_Node (Rhs), | |
4250 | Bodies, | |
4251 | Typl)); | |
4252 | Insert_Actions (N, Bodies); | |
fbf5a39b AC |
4253 | Analyze_And_Resolve (N, Standard_Boolean); |
4254 | Force_Validity_Checks := Save_Force_Validity_Checks; | |
4255 | end; | |
4256 | ||
70482933 RK |
4257 | -- Packed case |
4258 | ||
fbf5a39b | 4259 | elsif Is_Bit_Packed_Array (Typl) then |
70482933 RK |
4260 | Expand_Packed_Eq (N); |
4261 | ||
5e1c00fa RD |
4262 | -- Where the component type is elementary we can use a block bit |
4263 | -- comparison (if supported on the target) exception in the case | |
4264 | -- of floating-point (negative zero issues require element by | |
4265 | -- element comparison), and atomic types (where we must be sure | |
4266 | -- to load elements independently). | |
70482933 | 4267 | |
70482933 RK |
4268 | elsif Is_Elementary_Type (Component_Type (Typl)) |
4269 | and then not Is_Floating_Point_Type (Component_Type (Typl)) | |
5e1c00fa | 4270 | and then not Is_Atomic (Component_Type (Typl)) |
fbf5a39b | 4271 | and then Support_Composite_Compare_On_Target |
70482933 RK |
4272 | then |
4273 | null; | |
4274 | ||
4275 | -- For composite and floating-point cases, expand equality loop | |
4276 | -- to make sure of using proper comparisons for tagged types, | |
4277 | -- and correctly handling the floating-point case. | |
4278 | ||
4279 | else | |
4280 | Rewrite (N, | |
0da2c8ac AC |
4281 | Expand_Array_Equality |
4282 | (N, | |
4283 | Relocate_Node (Lhs), | |
4284 | Relocate_Node (Rhs), | |
4285 | Bodies, | |
4286 | Typl)); | |
70482933 RK |
4287 | Insert_Actions (N, Bodies, Suppress => All_Checks); |
4288 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
4289 | end if; | |
4290 | ||
4291 | -- Record Types | |
4292 | ||
4293 | elsif Is_Record_Type (Typl) then | |
4294 | ||
4295 | -- For tagged types, use the primitive "=" | |
4296 | ||
4297 | if Is_Tagged_Type (Typl) then | |
4298 | ||
4299 | -- If this is derived from an untagged private type completed | |
4300 | -- with a tagged type, it does not have a full view, so we | |
4301 | -- use the primitive operations of the private type. | |
4302 | -- This check should no longer be necessary when these | |
4303 | -- types receive their full views ??? | |
4304 | ||
4305 | if Is_Private_Type (A_Typ) | |
4306 | and then not Is_Tagged_Type (A_Typ) | |
4307 | and then Is_Derived_Type (A_Typ) | |
4308 | and then No (Full_View (A_Typ)) | |
4309 | then | |
2e071734 AC |
4310 | -- Search for equality operation, checking that the |
4311 | -- operands have the same type. Note that we must find | |
4312 | -- a matching entry, or something is very wrong! | |
4313 | ||
70482933 RK |
4314 | Prim := First_Elmt (Collect_Primitive_Operations (A_Typ)); |
4315 | ||
2e071734 AC |
4316 | while Present (Prim) loop |
4317 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
4318 | and then Etype (First_Formal (Node (Prim))) = | |
4319 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
4320 | and then | |
4321 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
4322 | ||
70482933 | 4323 | Next_Elmt (Prim); |
70482933 RK |
4324 | end loop; |
4325 | ||
2e071734 | 4326 | pragma Assert (Present (Prim)); |
70482933 | 4327 | Op_Name := Node (Prim); |
fbf5a39b AC |
4328 | |
4329 | -- Find the type's predefined equality or an overriding | |
4330 | -- user-defined equality. The reason for not simply calling | |
4331 | -- Find_Prim_Op here is that there may be a user-defined | |
4332 | -- overloaded equality op that precedes the equality that | |
4333 | -- we want, so we have to explicitly search (e.g., there | |
4334 | -- could be an equality with two different parameter types). | |
4335 | ||
70482933 | 4336 | else |
fbf5a39b AC |
4337 | if Is_Class_Wide_Type (Typl) then |
4338 | Typl := Root_Type (Typl); | |
4339 | end if; | |
4340 | ||
4341 | Prim := First_Elmt (Primitive_Operations (Typl)); | |
fbf5a39b AC |
4342 | while Present (Prim) loop |
4343 | exit when Chars (Node (Prim)) = Name_Op_Eq | |
4344 | and then Etype (First_Formal (Node (Prim))) = | |
4345 | Etype (Next_Formal (First_Formal (Node (Prim)))) | |
12e0c41c AC |
4346 | and then |
4347 | Base_Type (Etype (Node (Prim))) = Standard_Boolean; | |
fbf5a39b AC |
4348 | |
4349 | Next_Elmt (Prim); | |
fbf5a39b AC |
4350 | end loop; |
4351 | ||
2e071734 | 4352 | pragma Assert (Present (Prim)); |
fbf5a39b | 4353 | Op_Name := Node (Prim); |
70482933 RK |
4354 | end if; |
4355 | ||
4356 | Build_Equality_Call (Op_Name); | |
4357 | ||
5d09245e AC |
4358 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating the |
4359 | -- predefined equality operator for a type which has a subcomponent | |
4360 | -- of an Unchecked_Union type whose nominal subtype is unconstrained. | |
4361 | ||
4362 | elsif Has_Unconstrained_UU_Component (Typl) then | |
4363 | Insert_Action (N, | |
4364 | Make_Raise_Program_Error (Loc, | |
4365 | Reason => PE_Unchecked_Union_Restriction)); | |
4366 | ||
4367 | -- Prevent Gigi from generating incorrect code by rewriting the | |
4368 | -- equality as a standard False. | |
4369 | ||
4370 | Rewrite (N, | |
4371 | New_Occurrence_Of (Standard_False, Loc)); | |
4372 | ||
4373 | elsif Is_Unchecked_Union (Typl) then | |
4374 | ||
4375 | -- If we can infer the discriminants of the operands, we make a | |
4376 | -- call to the TSS equality function. | |
4377 | ||
4378 | if Has_Inferable_Discriminants (Lhs) | |
4379 | and then | |
4380 | Has_Inferable_Discriminants (Rhs) | |
4381 | then | |
4382 | Build_Equality_Call | |
4383 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
4384 | ||
4385 | else | |
4386 | -- Ada 2005 (AI-216): Program_Error is raised when evaluating | |
4387 | -- the predefined equality operator for an Unchecked_Union type | |
4388 | -- if either of the operands lack inferable discriminants. | |
4389 | ||
4390 | Insert_Action (N, | |
4391 | Make_Raise_Program_Error (Loc, | |
4392 | Reason => PE_Unchecked_Union_Restriction)); | |
4393 | ||
4394 | -- Prevent Gigi from generating incorrect code by rewriting | |
4395 | -- the equality as a standard False. | |
4396 | ||
4397 | Rewrite (N, | |
4398 | New_Occurrence_Of (Standard_False, Loc)); | |
4399 | ||
4400 | end if; | |
4401 | ||
70482933 RK |
4402 | -- If a type support function is present (for complex cases), use it |
4403 | ||
fbf5a39b AC |
4404 | elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then |
4405 | Build_Equality_Call | |
4406 | (TSS (Root_Type (Typl), TSS_Composite_Equality)); | |
70482933 RK |
4407 | |
4408 | -- Otherwise expand the component by component equality. Note that | |
4409 | -- we never use block-bit coparisons for records, because of the | |
4410 | -- problems with gaps. The backend will often be able to recombine | |
4411 | -- the separate comparisons that we generate here. | |
4412 | ||
4413 | else | |
4414 | Remove_Side_Effects (Lhs); | |
4415 | Remove_Side_Effects (Rhs); | |
4416 | Rewrite (N, | |
4417 | Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies)); | |
4418 | ||
4419 | Insert_Actions (N, Bodies, Suppress => All_Checks); | |
4420 | Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks); | |
4421 | end if; | |
4422 | end if; | |
4423 | ||
4424 | -- If we still have an equality comparison (i.e. it was not rewritten | |
4425 | -- in some way), then we can test if result is needed at compile time). | |
4426 | ||
4427 | if Nkind (N) = N_Op_Eq then | |
4428 | Rewrite_Comparison (N); | |
4429 | end if; | |
4430 | end Expand_N_Op_Eq; | |
4431 | ||
4432 | ----------------------- | |
4433 | -- Expand_N_Op_Expon -- | |
4434 | ----------------------- | |
4435 | ||
4436 | procedure Expand_N_Op_Expon (N : Node_Id) is | |
4437 | Loc : constant Source_Ptr := Sloc (N); | |
4438 | Typ : constant Entity_Id := Etype (N); | |
4439 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
4440 | Base : constant Node_Id := Relocate_Node (Left_Opnd (N)); | |
07fc65c4 | 4441 | Bastyp : constant Node_Id := Etype (Base); |
70482933 RK |
4442 | Exp : constant Node_Id := Relocate_Node (Right_Opnd (N)); |
4443 | Exptyp : constant Entity_Id := Etype (Exp); | |
4444 | Ovflo : constant Boolean := Do_Overflow_Check (N); | |
4445 | Expv : Uint; | |
4446 | Xnode : Node_Id; | |
4447 | Temp : Node_Id; | |
4448 | Rent : RE_Id; | |
4449 | Ent : Entity_Id; | |
fbf5a39b | 4450 | Etyp : Entity_Id; |
70482933 RK |
4451 | |
4452 | begin | |
4453 | Binary_Op_Validity_Checks (N); | |
4454 | ||
07fc65c4 GB |
4455 | -- If either operand is of a private type, then we have the use of |
4456 | -- an intrinsic operator, and we get rid of the privateness, by using | |
4457 | -- root types of underlying types for the actual operation. Otherwise | |
4458 | -- the private types will cause trouble if we expand multiplications | |
4459 | -- or shifts etc. We also do this transformation if the result type | |
4460 | -- is different from the base type. | |
4461 | ||
4462 | if Is_Private_Type (Etype (Base)) | |
4463 | or else | |
4464 | Is_Private_Type (Typ) | |
4465 | or else | |
4466 | Is_Private_Type (Exptyp) | |
4467 | or else | |
4468 | Rtyp /= Root_Type (Bastyp) | |
4469 | then | |
4470 | declare | |
4471 | Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp)); | |
4472 | Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp)); | |
4473 | ||
4474 | begin | |
4475 | Rewrite (N, | |
4476 | Unchecked_Convert_To (Typ, | |
4477 | Make_Op_Expon (Loc, | |
4478 | Left_Opnd => Unchecked_Convert_To (Bt, Base), | |
4479 | Right_Opnd => Unchecked_Convert_To (Et, Exp)))); | |
4480 | Analyze_And_Resolve (N, Typ); | |
4481 | return; | |
4482 | end; | |
4483 | end if; | |
4484 | ||
fbf5a39b | 4485 | -- Test for case of known right argument |
70482933 RK |
4486 | |
4487 | if Compile_Time_Known_Value (Exp) then | |
4488 | Expv := Expr_Value (Exp); | |
4489 | ||
4490 | -- We only fold small non-negative exponents. You might think we | |
4491 | -- could fold small negative exponents for the real case, but we | |
4492 | -- can't because we are required to raise Constraint_Error for | |
4493 | -- the case of 0.0 ** (negative) even if Machine_Overflows = False. | |
4494 | -- See ACVC test C4A012B. | |
4495 | ||
4496 | if Expv >= 0 and then Expv <= 4 then | |
4497 | ||
4498 | -- X ** 0 = 1 (or 1.0) | |
4499 | ||
4500 | if Expv = 0 then | |
4501 | if Ekind (Typ) in Integer_Kind then | |
4502 | Xnode := Make_Integer_Literal (Loc, Intval => 1); | |
4503 | else | |
4504 | Xnode := Make_Real_Literal (Loc, Ureal_1); | |
4505 | end if; | |
4506 | ||
4507 | -- X ** 1 = X | |
4508 | ||
4509 | elsif Expv = 1 then | |
4510 | Xnode := Base; | |
4511 | ||
4512 | -- X ** 2 = X * X | |
4513 | ||
4514 | elsif Expv = 2 then | |
4515 | Xnode := | |
4516 | Make_Op_Multiply (Loc, | |
4517 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 4518 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); |
70482933 RK |
4519 | |
4520 | -- X ** 3 = X * X * X | |
4521 | ||
4522 | elsif Expv = 3 then | |
4523 | Xnode := | |
4524 | Make_Op_Multiply (Loc, | |
4525 | Left_Opnd => | |
4526 | Make_Op_Multiply (Loc, | |
4527 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b AC |
4528 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)), |
4529 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base)); | |
70482933 RK |
4530 | |
4531 | -- X ** 4 -> | |
4532 | -- En : constant base'type := base * base; | |
4533 | -- ... | |
4534 | -- En * En | |
4535 | ||
4536 | else -- Expv = 4 | |
4537 | Temp := | |
4538 | Make_Defining_Identifier (Loc, New_Internal_Name ('E')); | |
4539 | ||
4540 | Insert_Actions (N, New_List ( | |
4541 | Make_Object_Declaration (Loc, | |
4542 | Defining_Identifier => Temp, | |
4543 | Constant_Present => True, | |
4544 | Object_Definition => New_Reference_To (Typ, Loc), | |
4545 | Expression => | |
4546 | Make_Op_Multiply (Loc, | |
4547 | Left_Opnd => Duplicate_Subexpr (Base), | |
fbf5a39b | 4548 | Right_Opnd => Duplicate_Subexpr_No_Checks (Base))))); |
70482933 RK |
4549 | |
4550 | Xnode := | |
4551 | Make_Op_Multiply (Loc, | |
4552 | Left_Opnd => New_Reference_To (Temp, Loc), | |
4553 | Right_Opnd => New_Reference_To (Temp, Loc)); | |
4554 | end if; | |
4555 | ||
4556 | Rewrite (N, Xnode); | |
4557 | Analyze_And_Resolve (N, Typ); | |
4558 | return; | |
4559 | end if; | |
4560 | end if; | |
4561 | ||
4562 | -- Case of (2 ** expression) appearing as an argument of an integer | |
4563 | -- multiplication, or as the right argument of a division of a non- | |
fbf5a39b | 4564 | -- negative integer. In such cases we leave the node untouched, setting |
70482933 RK |
4565 | -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion |
4566 | -- of the higher level node converts it into a shift. | |
4567 | ||
4568 | if Nkind (Base) = N_Integer_Literal | |
4569 | and then Intval (Base) = 2 | |
4570 | and then Is_Integer_Type (Root_Type (Exptyp)) | |
4571 | and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer) | |
4572 | and then Is_Unsigned_Type (Exptyp) | |
4573 | and then not Ovflo | |
4574 | and then Nkind (Parent (N)) in N_Binary_Op | |
4575 | then | |
4576 | declare | |
4577 | P : constant Node_Id := Parent (N); | |
4578 | L : constant Node_Id := Left_Opnd (P); | |
4579 | R : constant Node_Id := Right_Opnd (P); | |
4580 | ||
4581 | begin | |
4582 | if (Nkind (P) = N_Op_Multiply | |
4583 | and then | |
4584 | ((Is_Integer_Type (Etype (L)) and then R = N) | |
4585 | or else | |
4586 | (Is_Integer_Type (Etype (R)) and then L = N)) | |
4587 | and then not Do_Overflow_Check (P)) | |
4588 | ||
4589 | or else | |
4590 | (Nkind (P) = N_Op_Divide | |
4591 | and then Is_Integer_Type (Etype (L)) | |
4592 | and then Is_Unsigned_Type (Etype (L)) | |
4593 | and then R = N | |
4594 | and then not Do_Overflow_Check (P)) | |
4595 | then | |
4596 | Set_Is_Power_Of_2_For_Shift (N); | |
4597 | return; | |
4598 | end if; | |
4599 | end; | |
4600 | end if; | |
4601 | ||
07fc65c4 GB |
4602 | -- Fall through if exponentiation must be done using a runtime routine |
4603 | ||
07fc65c4 | 4604 | -- First deal with modular case |
70482933 RK |
4605 | |
4606 | if Is_Modular_Integer_Type (Rtyp) then | |
4607 | ||
4608 | -- Non-binary case, we call the special exponentiation routine for | |
4609 | -- the non-binary case, converting the argument to Long_Long_Integer | |
4610 | -- and passing the modulus value. Then the result is converted back | |
4611 | -- to the base type. | |
4612 | ||
4613 | if Non_Binary_Modulus (Rtyp) then | |
70482933 RK |
4614 | Rewrite (N, |
4615 | Convert_To (Typ, | |
4616 | Make_Function_Call (Loc, | |
4617 | Name => New_Reference_To (RTE (RE_Exp_Modular), Loc), | |
4618 | Parameter_Associations => New_List ( | |
4619 | Convert_To (Standard_Integer, Base), | |
4620 | Make_Integer_Literal (Loc, Modulus (Rtyp)), | |
4621 | Exp)))); | |
4622 | ||
4623 | -- Binary case, in this case, we call one of two routines, either | |
4624 | -- the unsigned integer case, or the unsigned long long integer | |
4625 | -- case, with a final "and" operation to do the required mod. | |
4626 | ||
4627 | else | |
4628 | if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then | |
4629 | Ent := RTE (RE_Exp_Unsigned); | |
4630 | else | |
4631 | Ent := RTE (RE_Exp_Long_Long_Unsigned); | |
4632 | end if; | |
4633 | ||
4634 | Rewrite (N, | |
4635 | Convert_To (Typ, | |
4636 | Make_Op_And (Loc, | |
4637 | Left_Opnd => | |
4638 | Make_Function_Call (Loc, | |
4639 | Name => New_Reference_To (Ent, Loc), | |
4640 | Parameter_Associations => New_List ( | |
4641 | Convert_To (Etype (First_Formal (Ent)), Base), | |
4642 | Exp)), | |
4643 | Right_Opnd => | |
4644 | Make_Integer_Literal (Loc, Modulus (Rtyp) - 1)))); | |
4645 | ||
4646 | end if; | |
4647 | ||
4648 | -- Common exit point for modular type case | |
4649 | ||
4650 | Analyze_And_Resolve (N, Typ); | |
4651 | return; | |
4652 | ||
fbf5a39b AC |
4653 | -- Signed integer cases, done using either Integer or Long_Long_Integer. |
4654 | -- It is not worth having routines for Short_[Short_]Integer, since for | |
4655 | -- most machines it would not help, and it would generate more code that | |
4656 | -- might need certification in the HI-E case. | |
70482933 | 4657 | |
fbf5a39b AC |
4658 | -- In the integer cases, we have two routines, one for when overflow |
4659 | -- checks are required, and one when they are not required, since | |
4660 | -- there is a real gain in ommitting checks on many machines. | |
70482933 | 4661 | |
fbf5a39b AC |
4662 | elsif Rtyp = Base_Type (Standard_Long_Long_Integer) |
4663 | or else (Rtyp = Base_Type (Standard_Long_Integer) | |
4664 | and then | |
4665 | Esize (Standard_Long_Integer) > Esize (Standard_Integer)) | |
4666 | or else (Rtyp = Universal_Integer) | |
70482933 | 4667 | then |
fbf5a39b AC |
4668 | Etyp := Standard_Long_Long_Integer; |
4669 | ||
70482933 RK |
4670 | if Ovflo then |
4671 | Rent := RE_Exp_Long_Long_Integer; | |
4672 | else | |
4673 | Rent := RE_Exn_Long_Long_Integer; | |
4674 | end if; | |
4675 | ||
fbf5a39b AC |
4676 | elsif Is_Signed_Integer_Type (Rtyp) then |
4677 | Etyp := Standard_Integer; | |
70482933 RK |
4678 | |
4679 | if Ovflo then | |
fbf5a39b | 4680 | Rent := RE_Exp_Integer; |
70482933 | 4681 | else |
fbf5a39b | 4682 | Rent := RE_Exn_Integer; |
70482933 | 4683 | end if; |
fbf5a39b AC |
4684 | |
4685 | -- Floating-point cases, always done using Long_Long_Float. We do not | |
4686 | -- need separate routines for the overflow case here, since in the case | |
4687 | -- of floating-point, we generate infinities anyway as a rule (either | |
4688 | -- that or we automatically trap overflow), and if there is an infinity | |
4689 | -- generated and a range check is required, the check will fail anyway. | |
4690 | ||
4691 | else | |
4692 | pragma Assert (Is_Floating_Point_Type (Rtyp)); | |
4693 | Etyp := Standard_Long_Long_Float; | |
4694 | Rent := RE_Exn_Long_Long_Float; | |
70482933 RK |
4695 | end if; |
4696 | ||
4697 | -- Common processing for integer cases and floating-point cases. | |
fbf5a39b | 4698 | -- If we are in the right type, we can call runtime routine directly |
70482933 | 4699 | |
fbf5a39b | 4700 | if Typ = Etyp |
70482933 RK |
4701 | and then Rtyp /= Universal_Integer |
4702 | and then Rtyp /= Universal_Real | |
4703 | then | |
4704 | Rewrite (N, | |
4705 | Make_Function_Call (Loc, | |
4706 | Name => New_Reference_To (RTE (Rent), Loc), | |
4707 | Parameter_Associations => New_List (Base, Exp))); | |
4708 | ||
4709 | -- Otherwise we have to introduce conversions (conversions are also | |
fbf5a39b AC |
4710 | -- required in the universal cases, since the runtime routine is |
4711 | -- typed using one of the standard types. | |
70482933 RK |
4712 | |
4713 | else | |
4714 | Rewrite (N, | |
4715 | Convert_To (Typ, | |
4716 | Make_Function_Call (Loc, | |
4717 | Name => New_Reference_To (RTE (Rent), Loc), | |
4718 | Parameter_Associations => New_List ( | |
fbf5a39b | 4719 | Convert_To (Etyp, Base), |
70482933 RK |
4720 | Exp)))); |
4721 | end if; | |
4722 | ||
4723 | Analyze_And_Resolve (N, Typ); | |
4724 | return; | |
4725 | ||
fbf5a39b AC |
4726 | exception |
4727 | when RE_Not_Available => | |
4728 | return; | |
70482933 RK |
4729 | end Expand_N_Op_Expon; |
4730 | ||
4731 | -------------------- | |
4732 | -- Expand_N_Op_Ge -- | |
4733 | -------------------- | |
4734 | ||
4735 | procedure Expand_N_Op_Ge (N : Node_Id) is | |
4736 | Typ : constant Entity_Id := Etype (N); | |
4737 | Op1 : constant Node_Id := Left_Opnd (N); | |
4738 | Op2 : constant Node_Id := Right_Opnd (N); | |
4739 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
4740 | ||
4741 | begin | |
4742 | Binary_Op_Validity_Checks (N); | |
4743 | ||
4744 | if Vax_Float (Typ1) then | |
4745 | Expand_Vax_Comparison (N); | |
4746 | return; | |
4747 | ||
4748 | elsif Is_Array_Type (Typ1) then | |
4749 | Expand_Array_Comparison (N); | |
4750 | return; | |
4751 | end if; | |
4752 | ||
4753 | if Is_Boolean_Type (Typ1) then | |
4754 | Adjust_Condition (Op1); | |
4755 | Adjust_Condition (Op2); | |
4756 | Set_Etype (N, Standard_Boolean); | |
4757 | Adjust_Result_Type (N, Typ); | |
4758 | end if; | |
4759 | ||
4760 | Rewrite_Comparison (N); | |
4761 | end Expand_N_Op_Ge; | |
4762 | ||
4763 | -------------------- | |
4764 | -- Expand_N_Op_Gt -- | |
4765 | -------------------- | |
4766 | ||
4767 | procedure Expand_N_Op_Gt (N : Node_Id) is | |
4768 | Typ : constant Entity_Id := Etype (N); | |
4769 | Op1 : constant Node_Id := Left_Opnd (N); | |
4770 | Op2 : constant Node_Id := Right_Opnd (N); | |
4771 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
4772 | ||
4773 | begin | |
4774 | Binary_Op_Validity_Checks (N); | |
4775 | ||
4776 | if Vax_Float (Typ1) then | |
4777 | Expand_Vax_Comparison (N); | |
4778 | return; | |
4779 | ||
4780 | elsif Is_Array_Type (Typ1) then | |
4781 | Expand_Array_Comparison (N); | |
4782 | return; | |
4783 | end if; | |
4784 | ||
4785 | if Is_Boolean_Type (Typ1) then | |
4786 | Adjust_Condition (Op1); | |
4787 | Adjust_Condition (Op2); | |
4788 | Set_Etype (N, Standard_Boolean); | |
4789 | Adjust_Result_Type (N, Typ); | |
4790 | end if; | |
4791 | ||
4792 | Rewrite_Comparison (N); | |
4793 | end Expand_N_Op_Gt; | |
4794 | ||
4795 | -------------------- | |
4796 | -- Expand_N_Op_Le -- | |
4797 | -------------------- | |
4798 | ||
4799 | procedure Expand_N_Op_Le (N : Node_Id) is | |
4800 | Typ : constant Entity_Id := Etype (N); | |
4801 | Op1 : constant Node_Id := Left_Opnd (N); | |
4802 | Op2 : constant Node_Id := Right_Opnd (N); | |
4803 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
4804 | ||
4805 | begin | |
4806 | Binary_Op_Validity_Checks (N); | |
4807 | ||
4808 | if Vax_Float (Typ1) then | |
4809 | Expand_Vax_Comparison (N); | |
4810 | return; | |
4811 | ||
4812 | elsif Is_Array_Type (Typ1) then | |
4813 | Expand_Array_Comparison (N); | |
4814 | return; | |
4815 | end if; | |
4816 | ||
4817 | if Is_Boolean_Type (Typ1) then | |
4818 | Adjust_Condition (Op1); | |
4819 | Adjust_Condition (Op2); | |
4820 | Set_Etype (N, Standard_Boolean); | |
4821 | Adjust_Result_Type (N, Typ); | |
4822 | end if; | |
4823 | ||
4824 | Rewrite_Comparison (N); | |
4825 | end Expand_N_Op_Le; | |
4826 | ||
4827 | -------------------- | |
4828 | -- Expand_N_Op_Lt -- | |
4829 | -------------------- | |
4830 | ||
4831 | procedure Expand_N_Op_Lt (N : Node_Id) is | |
4832 | Typ : constant Entity_Id := Etype (N); | |
4833 | Op1 : constant Node_Id := Left_Opnd (N); | |
4834 | Op2 : constant Node_Id := Right_Opnd (N); | |
4835 | Typ1 : constant Entity_Id := Base_Type (Etype (Op1)); | |
4836 | ||
4837 | begin | |
4838 | Binary_Op_Validity_Checks (N); | |
4839 | ||
4840 | if Vax_Float (Typ1) then | |
4841 | Expand_Vax_Comparison (N); | |
4842 | return; | |
4843 | ||
4844 | elsif Is_Array_Type (Typ1) then | |
4845 | Expand_Array_Comparison (N); | |
4846 | return; | |
4847 | end if; | |
4848 | ||
4849 | if Is_Boolean_Type (Typ1) then | |
4850 | Adjust_Condition (Op1); | |
4851 | Adjust_Condition (Op2); | |
4852 | Set_Etype (N, Standard_Boolean); | |
4853 | Adjust_Result_Type (N, Typ); | |
4854 | end if; | |
4855 | ||
4856 | Rewrite_Comparison (N); | |
4857 | end Expand_N_Op_Lt; | |
4858 | ||
4859 | ----------------------- | |
4860 | -- Expand_N_Op_Minus -- | |
4861 | ----------------------- | |
4862 | ||
4863 | procedure Expand_N_Op_Minus (N : Node_Id) is | |
4864 | Loc : constant Source_Ptr := Sloc (N); | |
4865 | Typ : constant Entity_Id := Etype (N); | |
4866 | ||
4867 | begin | |
4868 | Unary_Op_Validity_Checks (N); | |
4869 | ||
07fc65c4 | 4870 | if not Backend_Overflow_Checks_On_Target |
70482933 RK |
4871 | and then Is_Signed_Integer_Type (Etype (N)) |
4872 | and then Do_Overflow_Check (N) | |
4873 | then | |
4874 | -- Software overflow checking expands -expr into (0 - expr) | |
4875 | ||
4876 | Rewrite (N, | |
4877 | Make_Op_Subtract (Loc, | |
4878 | Left_Opnd => Make_Integer_Literal (Loc, 0), | |
4879 | Right_Opnd => Right_Opnd (N))); | |
4880 | ||
4881 | Analyze_And_Resolve (N, Typ); | |
4882 | ||
4883 | -- Vax floating-point types case | |
4884 | ||
4885 | elsif Vax_Float (Etype (N)) then | |
4886 | Expand_Vax_Arith (N); | |
4887 | end if; | |
4888 | end Expand_N_Op_Minus; | |
4889 | ||
4890 | --------------------- | |
4891 | -- Expand_N_Op_Mod -- | |
4892 | --------------------- | |
4893 | ||
4894 | procedure Expand_N_Op_Mod (N : Node_Id) is | |
4895 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 4896 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
4897 | Left : constant Node_Id := Left_Opnd (N); |
4898 | Right : constant Node_Id := Right_Opnd (N); | |
4899 | DOC : constant Boolean := Do_Overflow_Check (N); | |
4900 | DDC : constant Boolean := Do_Division_Check (N); | |
4901 | ||
4902 | LLB : Uint; | |
4903 | Llo : Uint; | |
4904 | Lhi : Uint; | |
4905 | LOK : Boolean; | |
4906 | Rlo : Uint; | |
4907 | Rhi : Uint; | |
4908 | ROK : Boolean; | |
4909 | ||
4910 | begin | |
4911 | Binary_Op_Validity_Checks (N); | |
4912 | ||
4913 | Determine_Range (Right, ROK, Rlo, Rhi); | |
4914 | Determine_Range (Left, LOK, Llo, Lhi); | |
4915 | ||
4916 | -- Convert mod to rem if operands are known non-negative. We do this | |
4917 | -- since it is quite likely that this will improve the quality of code, | |
4918 | -- (the operation now corresponds to the hardware remainder), and it | |
4919 | -- does not seem likely that it could be harmful. | |
4920 | ||
4921 | if LOK and then Llo >= 0 | |
4922 | and then | |
4923 | ROK and then Rlo >= 0 | |
4924 | then | |
4925 | Rewrite (N, | |
4926 | Make_Op_Rem (Sloc (N), | |
4927 | Left_Opnd => Left_Opnd (N), | |
4928 | Right_Opnd => Right_Opnd (N))); | |
4929 | ||
4930 | -- Instead of reanalyzing the node we do the analysis manually. | |
4931 | -- This avoids anomalies when the replacement is done in an | |
4932 | -- instance and is epsilon more efficient. | |
4933 | ||
4934 | Set_Entity (N, Standard_Entity (S_Op_Rem)); | |
fbf5a39b | 4935 | Set_Etype (N, Typ); |
70482933 RK |
4936 | Set_Do_Overflow_Check (N, DOC); |
4937 | Set_Do_Division_Check (N, DDC); | |
4938 | Expand_N_Op_Rem (N); | |
4939 | Set_Analyzed (N); | |
4940 | ||
4941 | -- Otherwise, normal mod processing | |
4942 | ||
4943 | else | |
4944 | if Is_Integer_Type (Etype (N)) then | |
4945 | Apply_Divide_Check (N); | |
4946 | end if; | |
4947 | ||
fbf5a39b AC |
4948 | -- Apply optimization x mod 1 = 0. We don't really need that with |
4949 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
4950 | -- certainly harmless. | |
4951 | ||
4952 | if Is_Integer_Type (Etype (N)) | |
4953 | and then Compile_Time_Known_Value (Right) | |
4954 | and then Expr_Value (Right) = Uint_1 | |
4955 | then | |
4956 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
4957 | Analyze_And_Resolve (N, Typ); | |
4958 | return; | |
4959 | end if; | |
4960 | ||
70482933 RK |
4961 | -- Deal with annoying case of largest negative number remainder |
4962 | -- minus one. Gigi does not handle this case correctly, because | |
4963 | -- it generates a divide instruction which may trap in this case. | |
4964 | ||
4965 | -- In fact the check is quite easy, if the right operand is -1, | |
4966 | -- then the mod value is always 0, and we can just ignore the | |
4967 | -- left operand completely in this case. | |
4968 | ||
fbf5a39b AC |
4969 | -- The operand type may be private (e.g. in the expansion of an |
4970 | -- an intrinsic operation) so we must use the underlying type to | |
4971 | -- get the bounds, and convert the literals explicitly. | |
4972 | ||
4973 | LLB := | |
4974 | Expr_Value | |
4975 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
70482933 RK |
4976 | |
4977 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
4978 | and then | |
4979 | ((not LOK) or else (Llo = LLB)) | |
4980 | then | |
4981 | Rewrite (N, | |
4982 | Make_Conditional_Expression (Loc, | |
4983 | Expressions => New_List ( | |
4984 | Make_Op_Eq (Loc, | |
4985 | Left_Opnd => Duplicate_Subexpr (Right), | |
4986 | Right_Opnd => | |
fbf5a39b AC |
4987 | Unchecked_Convert_To (Typ, |
4988 | Make_Integer_Literal (Loc, -1))), | |
4989 | Unchecked_Convert_To (Typ, | |
4990 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
4991 | Relocate_Node (N)))); |
4992 | ||
4993 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
fbf5a39b | 4994 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
4995 | end if; |
4996 | end if; | |
4997 | end Expand_N_Op_Mod; | |
4998 | ||
4999 | -------------------------- | |
5000 | -- Expand_N_Op_Multiply -- | |
5001 | -------------------------- | |
5002 | ||
5003 | procedure Expand_N_Op_Multiply (N : Node_Id) is | |
5004 | Loc : constant Source_Ptr := Sloc (N); | |
5005 | Lop : constant Node_Id := Left_Opnd (N); | |
5006 | Rop : constant Node_Id := Right_Opnd (N); | |
fbf5a39b AC |
5007 | |
5008 | Lp2 : constant Boolean := | |
5009 | Nkind (Lop) = N_Op_Expon | |
5010 | and then Is_Power_Of_2_For_Shift (Lop); | |
5011 | ||
5012 | Rp2 : constant Boolean := | |
5013 | Nkind (Rop) = N_Op_Expon | |
5014 | and then Is_Power_Of_2_For_Shift (Rop); | |
5015 | ||
70482933 RK |
5016 | Ltyp : constant Entity_Id := Etype (Lop); |
5017 | Rtyp : constant Entity_Id := Etype (Rop); | |
5018 | Typ : Entity_Id := Etype (N); | |
5019 | ||
5020 | begin | |
5021 | Binary_Op_Validity_Checks (N); | |
5022 | ||
5023 | -- Special optimizations for integer types | |
5024 | ||
5025 | if Is_Integer_Type (Typ) then | |
5026 | ||
5027 | -- N * 0 = 0 * N = 0 for integer types | |
5028 | ||
fbf5a39b AC |
5029 | if (Compile_Time_Known_Value (Rop) |
5030 | and then Expr_Value (Rop) = Uint_0) | |
70482933 | 5031 | or else |
fbf5a39b AC |
5032 | (Compile_Time_Known_Value (Lop) |
5033 | and then Expr_Value (Lop) = Uint_0) | |
70482933 RK |
5034 | then |
5035 | Rewrite (N, Make_Integer_Literal (Loc, Uint_0)); | |
5036 | Analyze_And_Resolve (N, Typ); | |
5037 | return; | |
5038 | end if; | |
5039 | ||
5040 | -- N * 1 = 1 * N = N for integer types | |
5041 | ||
fbf5a39b AC |
5042 | -- This optimisation is not done if we are going to |
5043 | -- rewrite the product 1 * 2 ** N to a shift. | |
5044 | ||
5045 | if Compile_Time_Known_Value (Rop) | |
5046 | and then Expr_Value (Rop) = Uint_1 | |
5047 | and then not Lp2 | |
70482933 | 5048 | then |
fbf5a39b | 5049 | Rewrite (N, Lop); |
70482933 RK |
5050 | return; |
5051 | ||
fbf5a39b AC |
5052 | elsif Compile_Time_Known_Value (Lop) |
5053 | and then Expr_Value (Lop) = Uint_1 | |
5054 | and then not Rp2 | |
70482933 | 5055 | then |
fbf5a39b | 5056 | Rewrite (N, Rop); |
70482933 RK |
5057 | return; |
5058 | end if; | |
5059 | end if; | |
5060 | ||
5061 | -- Deal with VAX float case | |
5062 | ||
5063 | if Vax_Float (Typ) then | |
5064 | Expand_Vax_Arith (N); | |
5065 | return; | |
5066 | end if; | |
5067 | ||
5068 | -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that | |
5069 | -- Is_Power_Of_2_For_Shift is set means that we know that our left | |
5070 | -- operand is an integer, as required for this to work. | |
5071 | ||
fbf5a39b AC |
5072 | if Rp2 then |
5073 | if Lp2 then | |
70482933 | 5074 | |
fbf5a39b | 5075 | -- Convert 2 ** A * 2 ** B into 2 ** (A + B) |
70482933 RK |
5076 | |
5077 | Rewrite (N, | |
5078 | Make_Op_Expon (Loc, | |
5079 | Left_Opnd => Make_Integer_Literal (Loc, 2), | |
5080 | Right_Opnd => | |
5081 | Make_Op_Add (Loc, | |
5082 | Left_Opnd => Right_Opnd (Lop), | |
5083 | Right_Opnd => Right_Opnd (Rop)))); | |
5084 | Analyze_And_Resolve (N, Typ); | |
5085 | return; | |
5086 | ||
5087 | else | |
5088 | Rewrite (N, | |
5089 | Make_Op_Shift_Left (Loc, | |
5090 | Left_Opnd => Lop, | |
5091 | Right_Opnd => | |
5092 | Convert_To (Standard_Natural, Right_Opnd (Rop)))); | |
5093 | Analyze_And_Resolve (N, Typ); | |
5094 | return; | |
5095 | end if; | |
5096 | ||
5097 | -- Same processing for the operands the other way round | |
5098 | ||
fbf5a39b | 5099 | elsif Lp2 then |
70482933 RK |
5100 | Rewrite (N, |
5101 | Make_Op_Shift_Left (Loc, | |
5102 | Left_Opnd => Rop, | |
5103 | Right_Opnd => | |
5104 | Convert_To (Standard_Natural, Right_Opnd (Lop)))); | |
5105 | Analyze_And_Resolve (N, Typ); | |
5106 | return; | |
5107 | end if; | |
5108 | ||
5109 | -- Do required fixup of universal fixed operation | |
5110 | ||
5111 | if Typ = Universal_Fixed then | |
5112 | Fixup_Universal_Fixed_Operation (N); | |
5113 | Typ := Etype (N); | |
5114 | end if; | |
5115 | ||
5116 | -- Multiplications with fixed-point results | |
5117 | ||
5118 | if Is_Fixed_Point_Type (Typ) then | |
5119 | ||
5120 | -- No special processing if Treat_Fixed_As_Integer is set, | |
5121 | -- since from a semantic point of view such operations are | |
5122 | -- simply integer operations and will be treated that way. | |
5123 | ||
5124 | if not Treat_Fixed_As_Integer (N) then | |
5125 | ||
5126 | -- Case of fixed * integer => fixed | |
5127 | ||
5128 | if Is_Integer_Type (Rtyp) then | |
5129 | Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N); | |
5130 | ||
5131 | -- Case of integer * fixed => fixed | |
5132 | ||
5133 | elsif Is_Integer_Type (Ltyp) then | |
5134 | Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N); | |
5135 | ||
5136 | -- Case of fixed * fixed => fixed | |
5137 | ||
5138 | else | |
5139 | Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N); | |
5140 | end if; | |
5141 | end if; | |
5142 | ||
5143 | -- Other cases of multiplication of fixed-point operands. Again | |
5144 | -- we exclude the cases where Treat_Fixed_As_Integer flag is set. | |
5145 | ||
5146 | elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp)) | |
5147 | and then not Treat_Fixed_As_Integer (N) | |
5148 | then | |
5149 | if Is_Integer_Type (Typ) then | |
5150 | Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N); | |
5151 | else | |
5152 | pragma Assert (Is_Floating_Point_Type (Typ)); | |
5153 | Expand_Multiply_Fixed_By_Fixed_Giving_Float (N); | |
5154 | end if; | |
5155 | ||
5156 | -- Mixed-mode operations can appear in a non-static universal | |
5157 | -- context, in which case the integer argument must be converted | |
5158 | -- explicitly. | |
5159 | ||
5160 | elsif Typ = Universal_Real | |
5161 | and then Is_Integer_Type (Rtyp) | |
5162 | then | |
5163 | Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop))); | |
5164 | ||
5165 | Analyze_And_Resolve (Rop, Universal_Real); | |
5166 | ||
5167 | elsif Typ = Universal_Real | |
5168 | and then Is_Integer_Type (Ltyp) | |
5169 | then | |
5170 | Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop))); | |
5171 | ||
5172 | Analyze_And_Resolve (Lop, Universal_Real); | |
5173 | ||
5174 | -- Non-fixed point cases, check software overflow checking required | |
5175 | ||
5176 | elsif Is_Signed_Integer_Type (Etype (N)) then | |
5177 | Apply_Arithmetic_Overflow_Check (N); | |
5178 | end if; | |
5179 | end Expand_N_Op_Multiply; | |
5180 | ||
5181 | -------------------- | |
5182 | -- Expand_N_Op_Ne -- | |
5183 | -------------------- | |
5184 | ||
5185 | -- Rewrite node as the negation of an equality operation, and reanalyze. | |
5186 | -- The equality to be used is defined in the same scope and has the same | |
5187 | -- signature. It must be set explicitly because in an instance it may not | |
5188 | -- have the same visibility as in the generic unit. | |
5189 | ||
5190 | procedure Expand_N_Op_Ne (N : Node_Id) is | |
5191 | Loc : constant Source_Ptr := Sloc (N); | |
5192 | Neg : Node_Id; | |
5193 | Ne : constant Entity_Id := Entity (N); | |
5194 | ||
5195 | begin | |
5196 | Binary_Op_Validity_Checks (N); | |
5197 | ||
5198 | Neg := | |
5199 | Make_Op_Not (Loc, | |
5200 | Right_Opnd => | |
5201 | Make_Op_Eq (Loc, | |
5202 | Left_Opnd => Left_Opnd (N), | |
5203 | Right_Opnd => Right_Opnd (N))); | |
5204 | Set_Paren_Count (Right_Opnd (Neg), 1); | |
5205 | ||
5206 | if Scope (Ne) /= Standard_Standard then | |
5207 | Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne)); | |
5208 | end if; | |
5209 | ||
fbf5a39b AC |
5210 | -- For navigation purposes, the inequality is treated as an implicit |
5211 | -- reference to the corresponding equality. Preserve the Comes_From_ | |
5212 | -- source flag so that the proper Xref entry is generated. | |
5213 | ||
5214 | Preserve_Comes_From_Source (Neg, N); | |
5215 | Preserve_Comes_From_Source (Right_Opnd (Neg), N); | |
70482933 RK |
5216 | Rewrite (N, Neg); |
5217 | Analyze_And_Resolve (N, Standard_Boolean); | |
5218 | end Expand_N_Op_Ne; | |
5219 | ||
5220 | --------------------- | |
5221 | -- Expand_N_Op_Not -- | |
5222 | --------------------- | |
5223 | ||
5224 | -- If the argument is other than a Boolean array type, there is no | |
5225 | -- special expansion required. | |
5226 | ||
5227 | -- For the packed case, we call the special routine in Exp_Pakd, except | |
5228 | -- that if the component size is greater than one, we use the standard | |
5229 | -- routine generating a gruesome loop (it is so peculiar to have packed | |
5230 | -- arrays with non-standard Boolean representations anyway, so it does | |
5231 | -- not matter that we do not handle this case efficiently). | |
5232 | ||
5233 | -- For the unpacked case (and for the special packed case where we have | |
5234 | -- non standard Booleans, as discussed above), we generate and insert | |
5235 | -- into the tree the following function definition: | |
5236 | ||
5237 | -- function Nnnn (A : arr) is | |
5238 | -- B : arr; | |
5239 | -- begin | |
5240 | -- for J in a'range loop | |
5241 | -- B (J) := not A (J); | |
5242 | -- end loop; | |
5243 | -- return B; | |
5244 | -- end Nnnn; | |
5245 | ||
5246 | -- Here arr is the actual subtype of the parameter (and hence always | |
5247 | -- constrained). Then we replace the not with a call to this function. | |
5248 | ||
5249 | procedure Expand_N_Op_Not (N : Node_Id) is | |
5250 | Loc : constant Source_Ptr := Sloc (N); | |
5251 | Typ : constant Entity_Id := Etype (N); | |
5252 | Opnd : Node_Id; | |
5253 | Arr : Entity_Id; | |
5254 | A : Entity_Id; | |
5255 | B : Entity_Id; | |
5256 | J : Entity_Id; | |
5257 | A_J : Node_Id; | |
5258 | B_J : Node_Id; | |
5259 | ||
5260 | Func_Name : Entity_Id; | |
5261 | Loop_Statement : Node_Id; | |
5262 | ||
5263 | begin | |
5264 | Unary_Op_Validity_Checks (N); | |
5265 | ||
5266 | -- For boolean operand, deal with non-standard booleans | |
5267 | ||
5268 | if Is_Boolean_Type (Typ) then | |
5269 | Adjust_Condition (Right_Opnd (N)); | |
5270 | Set_Etype (N, Standard_Boolean); | |
5271 | Adjust_Result_Type (N, Typ); | |
5272 | return; | |
5273 | end if; | |
5274 | ||
5275 | -- Only array types need any other processing | |
5276 | ||
5277 | if not Is_Array_Type (Typ) then | |
5278 | return; | |
5279 | end if; | |
5280 | ||
5281 | -- Case of array operand. If bit packed, handle it in Exp_Pakd | |
5282 | ||
5283 | if Is_Bit_Packed_Array (Typ) and then Component_Size (Typ) = 1 then | |
5284 | Expand_Packed_Not (N); | |
5285 | return; | |
5286 | end if; | |
5287 | ||
fbf5a39b AC |
5288 | -- Case of array operand which is not bit-packed. If the context is |
5289 | -- a safe assignment, call in-place operation, If context is a larger | |
5290 | -- boolean expression in the context of a safe assignment, expansion is | |
5291 | -- done by enclosing operation. | |
70482933 RK |
5292 | |
5293 | Opnd := Relocate_Node (Right_Opnd (N)); | |
5294 | Convert_To_Actual_Subtype (Opnd); | |
5295 | Arr := Etype (Opnd); | |
5296 | Ensure_Defined (Arr, N); | |
5297 | ||
fbf5a39b AC |
5298 | if Nkind (Parent (N)) = N_Assignment_Statement then |
5299 | if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then | |
5300 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
5301 | return; | |
5302 | ||
5e1c00fa | 5303 | -- Special case the negation of a binary operation |
fbf5a39b AC |
5304 | |
5305 | elsif (Nkind (Opnd) = N_Op_And | |
5306 | or else Nkind (Opnd) = N_Op_Or | |
5307 | or else Nkind (Opnd) = N_Op_Xor) | |
5308 | and then Safe_In_Place_Array_Op | |
5309 | (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd)) | |
5310 | then | |
5311 | Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty); | |
5312 | return; | |
5313 | end if; | |
5314 | ||
5315 | elsif Nkind (Parent (N)) in N_Binary_Op | |
5316 | and then Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
5317 | then | |
5318 | declare | |
5319 | Op1 : constant Node_Id := Left_Opnd (Parent (N)); | |
5320 | Op2 : constant Node_Id := Right_Opnd (Parent (N)); | |
5321 | Lhs : constant Node_Id := Name (Parent (Parent (N))); | |
5322 | ||
5323 | begin | |
5324 | if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then | |
5325 | if N = Op1 | |
5326 | and then Nkind (Op2) = N_Op_Not | |
5327 | then | |
5e1c00fa | 5328 | -- (not A) op (not B) can be reduced to a single call |
fbf5a39b AC |
5329 | |
5330 | return; | |
5331 | ||
5332 | elsif N = Op2 | |
5333 | and then Nkind (Parent (N)) = N_Op_Xor | |
5334 | then | |
5e1c00fa | 5335 | -- A xor (not B) can also be special-cased |
fbf5a39b AC |
5336 | |
5337 | return; | |
5338 | end if; | |
5339 | end if; | |
5340 | end; | |
5341 | end if; | |
5342 | ||
70482933 RK |
5343 | A := Make_Defining_Identifier (Loc, Name_uA); |
5344 | B := Make_Defining_Identifier (Loc, Name_uB); | |
5345 | J := Make_Defining_Identifier (Loc, Name_uJ); | |
5346 | ||
5347 | A_J := | |
5348 | Make_Indexed_Component (Loc, | |
5349 | Prefix => New_Reference_To (A, Loc), | |
5350 | Expressions => New_List (New_Reference_To (J, Loc))); | |
5351 | ||
5352 | B_J := | |
5353 | Make_Indexed_Component (Loc, | |
5354 | Prefix => New_Reference_To (B, Loc), | |
5355 | Expressions => New_List (New_Reference_To (J, Loc))); | |
5356 | ||
5357 | Loop_Statement := | |
5358 | Make_Implicit_Loop_Statement (N, | |
5359 | Identifier => Empty, | |
5360 | ||
5361 | Iteration_Scheme => | |
5362 | Make_Iteration_Scheme (Loc, | |
5363 | Loop_Parameter_Specification => | |
5364 | Make_Loop_Parameter_Specification (Loc, | |
5365 | Defining_Identifier => J, | |
5366 | Discrete_Subtype_Definition => | |
5367 | Make_Attribute_Reference (Loc, | |
5368 | Prefix => Make_Identifier (Loc, Chars (A)), | |
5369 | Attribute_Name => Name_Range))), | |
5370 | ||
5371 | Statements => New_List ( | |
5372 | Make_Assignment_Statement (Loc, | |
5373 | Name => B_J, | |
5374 | Expression => Make_Op_Not (Loc, A_J)))); | |
5375 | ||
5376 | Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N')); | |
5377 | Set_Is_Inlined (Func_Name); | |
5378 | ||
5379 | Insert_Action (N, | |
5380 | Make_Subprogram_Body (Loc, | |
5381 | Specification => | |
5382 | Make_Function_Specification (Loc, | |
5383 | Defining_Unit_Name => Func_Name, | |
5384 | Parameter_Specifications => New_List ( | |
5385 | Make_Parameter_Specification (Loc, | |
5386 | Defining_Identifier => A, | |
5387 | Parameter_Type => New_Reference_To (Typ, Loc))), | |
5388 | Subtype_Mark => New_Reference_To (Typ, Loc)), | |
5389 | ||
5390 | Declarations => New_List ( | |
5391 | Make_Object_Declaration (Loc, | |
5392 | Defining_Identifier => B, | |
5393 | Object_Definition => New_Reference_To (Arr, Loc))), | |
5394 | ||
5395 | Handled_Statement_Sequence => | |
5396 | Make_Handled_Sequence_Of_Statements (Loc, | |
5397 | Statements => New_List ( | |
5398 | Loop_Statement, | |
5399 | Make_Return_Statement (Loc, | |
5400 | Expression => | |
5401 | Make_Identifier (Loc, Chars (B))))))); | |
5402 | ||
5403 | Rewrite (N, | |
5404 | Make_Function_Call (Loc, | |
5405 | Name => New_Reference_To (Func_Name, Loc), | |
5406 | Parameter_Associations => New_List (Opnd))); | |
5407 | ||
5408 | Analyze_And_Resolve (N, Typ); | |
5409 | end Expand_N_Op_Not; | |
5410 | ||
5411 | -------------------- | |
5412 | -- Expand_N_Op_Or -- | |
5413 | -------------------- | |
5414 | ||
5415 | procedure Expand_N_Op_Or (N : Node_Id) is | |
5416 | Typ : constant Entity_Id := Etype (N); | |
5417 | ||
5418 | begin | |
5419 | Binary_Op_Validity_Checks (N); | |
5420 | ||
5421 | if Is_Array_Type (Etype (N)) then | |
5422 | Expand_Boolean_Operator (N); | |
5423 | ||
5424 | elsif Is_Boolean_Type (Etype (N)) then | |
5425 | Adjust_Condition (Left_Opnd (N)); | |
5426 | Adjust_Condition (Right_Opnd (N)); | |
5427 | Set_Etype (N, Standard_Boolean); | |
5428 | Adjust_Result_Type (N, Typ); | |
5429 | end if; | |
5430 | end Expand_N_Op_Or; | |
5431 | ||
5432 | ---------------------- | |
5433 | -- Expand_N_Op_Plus -- | |
5434 | ---------------------- | |
5435 | ||
5436 | procedure Expand_N_Op_Plus (N : Node_Id) is | |
5437 | begin | |
5438 | Unary_Op_Validity_Checks (N); | |
5439 | end Expand_N_Op_Plus; | |
5440 | ||
5441 | --------------------- | |
5442 | -- Expand_N_Op_Rem -- | |
5443 | --------------------- | |
5444 | ||
5445 | procedure Expand_N_Op_Rem (N : Node_Id) is | |
5446 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 5447 | Typ : constant Entity_Id := Etype (N); |
70482933 RK |
5448 | |
5449 | Left : constant Node_Id := Left_Opnd (N); | |
5450 | Right : constant Node_Id := Right_Opnd (N); | |
5451 | ||
5452 | LLB : Uint; | |
5453 | Llo : Uint; | |
5454 | Lhi : Uint; | |
5455 | LOK : Boolean; | |
5456 | Rlo : Uint; | |
5457 | Rhi : Uint; | |
5458 | ROK : Boolean; | |
70482933 RK |
5459 | |
5460 | begin | |
5461 | Binary_Op_Validity_Checks (N); | |
5462 | ||
5463 | if Is_Integer_Type (Etype (N)) then | |
5464 | Apply_Divide_Check (N); | |
5465 | end if; | |
5466 | ||
fbf5a39b AC |
5467 | -- Apply optimization x rem 1 = 0. We don't really need that with |
5468 | -- gcc, but it is useful with other back ends (e.g. AAMP), and is | |
5469 | -- certainly harmless. | |
5470 | ||
5471 | if Is_Integer_Type (Etype (N)) | |
5472 | and then Compile_Time_Known_Value (Right) | |
5473 | and then Expr_Value (Right) = Uint_1 | |
5474 | then | |
5475 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
5476 | Analyze_And_Resolve (N, Typ); | |
5477 | return; | |
5478 | end if; | |
5479 | ||
70482933 RK |
5480 | -- Deal with annoying case of largest negative number remainder |
5481 | -- minus one. Gigi does not handle this case correctly, because | |
5482 | -- it generates a divide instruction which may trap in this case. | |
5483 | ||
5484 | -- In fact the check is quite easy, if the right operand is -1, | |
5485 | -- then the remainder is always 0, and we can just ignore the | |
5486 | -- left operand completely in this case. | |
5487 | ||
5488 | Determine_Range (Right, ROK, Rlo, Rhi); | |
5489 | Determine_Range (Left, LOK, Llo, Lhi); | |
fbf5a39b AC |
5490 | |
5491 | -- The operand type may be private (e.g. in the expansion of an | |
5492 | -- an intrinsic operation) so we must use the underlying type to | |
5493 | -- get the bounds, and convert the literals explicitly. | |
5494 | ||
5495 | LLB := | |
5496 | Expr_Value | |
5497 | (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left))))); | |
5498 | ||
5499 | -- Now perform the test, generating code only if needed | |
70482933 RK |
5500 | |
5501 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
5502 | and then | |
5503 | ((not LOK) or else (Llo = LLB)) | |
5504 | then | |
5505 | Rewrite (N, | |
5506 | Make_Conditional_Expression (Loc, | |
5507 | Expressions => New_List ( | |
5508 | Make_Op_Eq (Loc, | |
5509 | Left_Opnd => Duplicate_Subexpr (Right), | |
5510 | Right_Opnd => | |
fbf5a39b AC |
5511 | Unchecked_Convert_To (Typ, |
5512 | Make_Integer_Literal (Loc, -1))), | |
70482933 | 5513 | |
fbf5a39b AC |
5514 | Unchecked_Convert_To (Typ, |
5515 | Make_Integer_Literal (Loc, Uint_0)), | |
70482933 RK |
5516 | |
5517 | Relocate_Node (N)))); | |
5518 | ||
5519 | Set_Analyzed (Next (Next (First (Expressions (N))))); | |
5520 | Analyze_And_Resolve (N, Typ); | |
5521 | end if; | |
5522 | end Expand_N_Op_Rem; | |
5523 | ||
5524 | ----------------------------- | |
5525 | -- Expand_N_Op_Rotate_Left -- | |
5526 | ----------------------------- | |
5527 | ||
5528 | procedure Expand_N_Op_Rotate_Left (N : Node_Id) is | |
5529 | begin | |
5530 | Binary_Op_Validity_Checks (N); | |
5531 | end Expand_N_Op_Rotate_Left; | |
5532 | ||
5533 | ------------------------------ | |
5534 | -- Expand_N_Op_Rotate_Right -- | |
5535 | ------------------------------ | |
5536 | ||
5537 | procedure Expand_N_Op_Rotate_Right (N : Node_Id) is | |
5538 | begin | |
5539 | Binary_Op_Validity_Checks (N); | |
5540 | end Expand_N_Op_Rotate_Right; | |
5541 | ||
5542 | ---------------------------- | |
5543 | -- Expand_N_Op_Shift_Left -- | |
5544 | ---------------------------- | |
5545 | ||
5546 | procedure Expand_N_Op_Shift_Left (N : Node_Id) is | |
5547 | begin | |
5548 | Binary_Op_Validity_Checks (N); | |
5549 | end Expand_N_Op_Shift_Left; | |
5550 | ||
5551 | ----------------------------- | |
5552 | -- Expand_N_Op_Shift_Right -- | |
5553 | ----------------------------- | |
5554 | ||
5555 | procedure Expand_N_Op_Shift_Right (N : Node_Id) is | |
5556 | begin | |
5557 | Binary_Op_Validity_Checks (N); | |
5558 | end Expand_N_Op_Shift_Right; | |
5559 | ||
5560 | ---------------------------------------- | |
5561 | -- Expand_N_Op_Shift_Right_Arithmetic -- | |
5562 | ---------------------------------------- | |
5563 | ||
5564 | procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is | |
5565 | begin | |
5566 | Binary_Op_Validity_Checks (N); | |
5567 | end Expand_N_Op_Shift_Right_Arithmetic; | |
5568 | ||
5569 | -------------------------- | |
5570 | -- Expand_N_Op_Subtract -- | |
5571 | -------------------------- | |
5572 | ||
5573 | procedure Expand_N_Op_Subtract (N : Node_Id) is | |
5574 | Typ : constant Entity_Id := Etype (N); | |
5575 | ||
5576 | begin | |
5577 | Binary_Op_Validity_Checks (N); | |
5578 | ||
5579 | -- N - 0 = N for integer types | |
5580 | ||
5581 | if Is_Integer_Type (Typ) | |
5582 | and then Compile_Time_Known_Value (Right_Opnd (N)) | |
5583 | and then Expr_Value (Right_Opnd (N)) = 0 | |
5584 | then | |
5585 | Rewrite (N, Left_Opnd (N)); | |
5586 | return; | |
5587 | end if; | |
5588 | ||
5589 | -- Arithemtic overflow checks for signed integer/fixed point types | |
5590 | ||
5591 | if Is_Signed_Integer_Type (Typ) | |
5592 | or else Is_Fixed_Point_Type (Typ) | |
5593 | then | |
5594 | Apply_Arithmetic_Overflow_Check (N); | |
5595 | ||
5596 | -- Vax floating-point types case | |
5597 | ||
5598 | elsif Vax_Float (Typ) then | |
5599 | Expand_Vax_Arith (N); | |
5600 | end if; | |
5601 | end Expand_N_Op_Subtract; | |
5602 | ||
5603 | --------------------- | |
5604 | -- Expand_N_Op_Xor -- | |
5605 | --------------------- | |
5606 | ||
5607 | procedure Expand_N_Op_Xor (N : Node_Id) is | |
5608 | Typ : constant Entity_Id := Etype (N); | |
5609 | ||
5610 | begin | |
5611 | Binary_Op_Validity_Checks (N); | |
5612 | ||
5613 | if Is_Array_Type (Etype (N)) then | |
5614 | Expand_Boolean_Operator (N); | |
5615 | ||
5616 | elsif Is_Boolean_Type (Etype (N)) then | |
5617 | Adjust_Condition (Left_Opnd (N)); | |
5618 | Adjust_Condition (Right_Opnd (N)); | |
5619 | Set_Etype (N, Standard_Boolean); | |
5620 | Adjust_Result_Type (N, Typ); | |
5621 | end if; | |
5622 | end Expand_N_Op_Xor; | |
5623 | ||
5624 | ---------------------- | |
5625 | -- Expand_N_Or_Else -- | |
5626 | ---------------------- | |
5627 | ||
5628 | -- Expand into conditional expression if Actions present, and also | |
5629 | -- deal with optimizing case of arguments being True or False. | |
5630 | ||
5631 | procedure Expand_N_Or_Else (N : Node_Id) is | |
5632 | Loc : constant Source_Ptr := Sloc (N); | |
5633 | Typ : constant Entity_Id := Etype (N); | |
5634 | Left : constant Node_Id := Left_Opnd (N); | |
5635 | Right : constant Node_Id := Right_Opnd (N); | |
5636 | Actlist : List_Id; | |
5637 | ||
5638 | begin | |
5639 | -- Deal with non-standard booleans | |
5640 | ||
5641 | if Is_Boolean_Type (Typ) then | |
5642 | Adjust_Condition (Left); | |
5643 | Adjust_Condition (Right); | |
5644 | Set_Etype (N, Standard_Boolean); | |
fbf5a39b | 5645 | end if; |
70482933 RK |
5646 | |
5647 | -- Check for cases of left argument is True or False | |
5648 | ||
fbf5a39b | 5649 | if Nkind (Left) = N_Identifier then |
70482933 RK |
5650 | |
5651 | -- If left argument is False, change (False or else Right) to Right. | |
5652 | -- Any actions associated with Right will be executed unconditionally | |
5653 | -- and can thus be inserted into the tree unconditionally. | |
5654 | ||
5655 | if Entity (Left) = Standard_False then | |
5656 | if Present (Actions (N)) then | |
5657 | Insert_Actions (N, Actions (N)); | |
5658 | end if; | |
5659 | ||
5660 | Rewrite (N, Right); | |
5661 | Adjust_Result_Type (N, Typ); | |
5662 | return; | |
5663 | ||
5664 | -- If left argument is True, change (True and then Right) to | |
5665 | -- True. In this case we can forget the actions associated with | |
5666 | -- Right, since they will never be executed. | |
5667 | ||
5668 | elsif Entity (Left) = Standard_True then | |
5669 | Kill_Dead_Code (Right); | |
5670 | Kill_Dead_Code (Actions (N)); | |
5671 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
5672 | Adjust_Result_Type (N, Typ); | |
5673 | return; | |
5674 | end if; | |
5675 | end if; | |
5676 | ||
5677 | -- If Actions are present, we expand | |
5678 | ||
5679 | -- left or else right | |
5680 | ||
5681 | -- into | |
5682 | ||
5683 | -- if left then True else right end | |
5684 | ||
5685 | -- with the actions becoming the Else_Actions of the conditional | |
5686 | -- expression. This conditional expression is then further expanded | |
5687 | -- (and will eventually disappear) | |
5688 | ||
5689 | if Present (Actions (N)) then | |
5690 | Actlist := Actions (N); | |
5691 | Rewrite (N, | |
5692 | Make_Conditional_Expression (Loc, | |
5693 | Expressions => New_List ( | |
5694 | Left, | |
5695 | New_Occurrence_Of (Standard_True, Loc), | |
5696 | Right))); | |
5697 | ||
5698 | Set_Else_Actions (N, Actlist); | |
5699 | Analyze_And_Resolve (N, Standard_Boolean); | |
5700 | Adjust_Result_Type (N, Typ); | |
5701 | return; | |
5702 | end if; | |
5703 | ||
5704 | -- No actions present, check for cases of right argument True/False | |
5705 | ||
5706 | if Nkind (Right) = N_Identifier then | |
5707 | ||
5708 | -- Change (Left or else False) to Left. Note that we know there | |
5709 | -- are no actions associated with the True operand, since we | |
5710 | -- just checked for this case above. | |
5711 | ||
5712 | if Entity (Right) = Standard_False then | |
5713 | Rewrite (N, Left); | |
5714 | ||
5715 | -- Change (Left or else True) to True, making sure to preserve | |
5716 | -- any side effects associated with the Left operand. | |
5717 | ||
5718 | elsif Entity (Right) = Standard_True then | |
5719 | Remove_Side_Effects (Left); | |
5720 | Rewrite | |
5721 | (N, New_Occurrence_Of (Standard_True, Loc)); | |
5722 | end if; | |
5723 | end if; | |
5724 | ||
5725 | Adjust_Result_Type (N, Typ); | |
5726 | end Expand_N_Or_Else; | |
5727 | ||
5728 | ----------------------------------- | |
5729 | -- Expand_N_Qualified_Expression -- | |
5730 | ----------------------------------- | |
5731 | ||
5732 | procedure Expand_N_Qualified_Expression (N : Node_Id) is | |
5733 | Operand : constant Node_Id := Expression (N); | |
5734 | Target_Type : constant Entity_Id := Entity (Subtype_Mark (N)); | |
5735 | ||
5736 | begin | |
5737 | Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True); | |
5738 | end Expand_N_Qualified_Expression; | |
5739 | ||
5740 | --------------------------------- | |
5741 | -- Expand_N_Selected_Component -- | |
5742 | --------------------------------- | |
5743 | ||
5744 | -- If the selector is a discriminant of a concurrent object, rewrite the | |
5745 | -- prefix to denote the corresponding record type. | |
5746 | ||
5747 | procedure Expand_N_Selected_Component (N : Node_Id) is | |
5748 | Loc : constant Source_Ptr := Sloc (N); | |
5749 | Par : constant Node_Id := Parent (N); | |
5750 | P : constant Node_Id := Prefix (N); | |
fbf5a39b | 5751 | Ptyp : Entity_Id := Underlying_Type (Etype (P)); |
70482933 | 5752 | Disc : Entity_Id; |
70482933 | 5753 | New_N : Node_Id; |
fbf5a39b | 5754 | Dcon : Elmt_Id; |
70482933 RK |
5755 | |
5756 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean; | |
5757 | -- Gigi needs a temporary for prefixes that depend on a discriminant, | |
5758 | -- unless the context of an assignment can provide size information. | |
fbf5a39b AC |
5759 | -- Don't we have a general routine that does this??? |
5760 | ||
5761 | ----------------------- | |
5762 | -- In_Left_Hand_Side -- | |
5763 | ----------------------- | |
70482933 RK |
5764 | |
5765 | function In_Left_Hand_Side (Comp : Node_Id) return Boolean is | |
5766 | begin | |
fbf5a39b AC |
5767 | return (Nkind (Parent (Comp)) = N_Assignment_Statement |
5768 | and then Comp = Name (Parent (Comp))) | |
5769 | or else (Present (Parent (Comp)) | |
5770 | and then Nkind (Parent (Comp)) in N_Subexpr | |
5771 | and then In_Left_Hand_Side (Parent (Comp))); | |
70482933 RK |
5772 | end In_Left_Hand_Side; |
5773 | ||
fbf5a39b AC |
5774 | -- Start of processing for Expand_N_Selected_Component |
5775 | ||
70482933 | 5776 | begin |
fbf5a39b AC |
5777 | -- Insert explicit dereference if required |
5778 | ||
5779 | if Is_Access_Type (Ptyp) then | |
5780 | Insert_Explicit_Dereference (P); | |
e6f69614 | 5781 | Analyze_And_Resolve (P, Designated_Type (Ptyp)); |
fbf5a39b AC |
5782 | |
5783 | if Ekind (Etype (P)) = E_Private_Subtype | |
5784 | and then Is_For_Access_Subtype (Etype (P)) | |
5785 | then | |
5786 | Set_Etype (P, Base_Type (Etype (P))); | |
5787 | end if; | |
5788 | ||
5789 | Ptyp := Etype (P); | |
5790 | end if; | |
5791 | ||
5792 | -- Deal with discriminant check required | |
5793 | ||
70482933 RK |
5794 | if Do_Discriminant_Check (N) then |
5795 | ||
5796 | -- Present the discrminant checking function to the backend, | |
5797 | -- so that it can inline the call to the function. | |
5798 | ||
5799 | Add_Inlined_Body | |
5800 | (Discriminant_Checking_Func | |
5801 | (Original_Record_Component (Entity (Selector_Name (N))))); | |
70482933 | 5802 | |
fbf5a39b | 5803 | -- Now reset the flag and generate the call |
70482933 | 5804 | |
fbf5a39b AC |
5805 | Set_Do_Discriminant_Check (N, False); |
5806 | Generate_Discriminant_Check (N); | |
70482933 RK |
5807 | end if; |
5808 | ||
fbf5a39b AC |
5809 | -- Gigi cannot handle unchecked conversions that are the prefix of a |
5810 | -- selected component with discriminants. This must be checked during | |
5811 | -- expansion, because during analysis the type of the selector is not | |
5812 | -- known at the point the prefix is analyzed. If the conversion is the | |
5813 | -- target of an assignment, then we cannot force the evaluation. | |
70482933 RK |
5814 | |
5815 | if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion | |
5816 | and then Has_Discriminants (Etype (N)) | |
5817 | and then not In_Left_Hand_Side (N) | |
5818 | then | |
5819 | Force_Evaluation (Prefix (N)); | |
5820 | end if; | |
5821 | ||
5822 | -- Remaining processing applies only if selector is a discriminant | |
5823 | ||
5824 | if Ekind (Entity (Selector_Name (N))) = E_Discriminant then | |
5825 | ||
5826 | -- If the selector is a discriminant of a constrained record type, | |
fbf5a39b AC |
5827 | -- we may be able to rewrite the expression with the actual value |
5828 | -- of the discriminant, a useful optimization in some cases. | |
70482933 RK |
5829 | |
5830 | if Is_Record_Type (Ptyp) | |
5831 | and then Has_Discriminants (Ptyp) | |
5832 | and then Is_Constrained (Ptyp) | |
70482933 | 5833 | then |
fbf5a39b AC |
5834 | -- Do this optimization for discrete types only, and not for |
5835 | -- access types (access discriminants get us into trouble!) | |
70482933 | 5836 | |
fbf5a39b AC |
5837 | if not Is_Discrete_Type (Etype (N)) then |
5838 | null; | |
5839 | ||
5840 | -- Don't do this on the left hand of an assignment statement. | |
5841 | -- Normally one would think that references like this would | |
5842 | -- not occur, but they do in generated code, and mean that | |
5843 | -- we really do want to assign the discriminant! | |
5844 | ||
5845 | elsif Nkind (Par) = N_Assignment_Statement | |
5846 | and then Name (Par) = N | |
5847 | then | |
5848 | null; | |
5849 | ||
5850 | -- Don't do this optimization for the prefix of an attribute | |
5851 | -- or the operand of an object renaming declaration since these | |
5852 | -- are contexts where we do not want the value anyway. | |
5853 | ||
5854 | elsif (Nkind (Par) = N_Attribute_Reference | |
5855 | and then Prefix (Par) = N) | |
5856 | or else Is_Renamed_Object (N) | |
5857 | then | |
5858 | null; | |
5859 | ||
5860 | -- Don't do this optimization if we are within the code for a | |
5861 | -- discriminant check, since the whole point of such a check may | |
5862 | -- be to verify the condition on which the code below depends! | |
5863 | ||
5864 | elsif Is_In_Discriminant_Check (N) then | |
5865 | null; | |
5866 | ||
5867 | -- Green light to see if we can do the optimization. There is | |
5868 | -- still one condition that inhibits the optimization below | |
5869 | -- but now is the time to check the particular discriminant. | |
5870 | ||
5871 | else | |
5872 | -- Loop through discriminants to find the matching | |
5873 | -- discriminant constraint to see if we can copy it. | |
5874 | ||
5875 | Disc := First_Discriminant (Ptyp); | |
5876 | Dcon := First_Elmt (Discriminant_Constraint (Ptyp)); | |
5877 | Discr_Loop : while Present (Dcon) loop | |
5878 | ||
5879 | -- Check if this is the matching discriminant | |
5880 | ||
5881 | if Disc = Entity (Selector_Name (N)) then | |
70482933 | 5882 | |
fbf5a39b AC |
5883 | -- Here we have the matching discriminant. Check for |
5884 | -- the case of a discriminant of a component that is | |
5885 | -- constrained by an outer discriminant, which cannot | |
5886 | -- be optimized away. | |
5887 | ||
5888 | if | |
5889 | Denotes_Discriminant | |
5890 | (Node (Dcon), Check_Protected => True) | |
5891 | then | |
5892 | exit Discr_Loop; | |
70482933 RK |
5893 | |
5894 | -- In the context of a case statement, the expression | |
5895 | -- may have the base type of the discriminant, and we | |
5896 | -- need to preserve the constraint to avoid spurious | |
5897 | -- errors on missing cases. | |
5898 | ||
fbf5a39b AC |
5899 | elsif Nkind (Parent (N)) = N_Case_Statement |
5900 | and then Etype (Node (Dcon)) /= Etype (Disc) | |
70482933 RK |
5901 | then |
5902 | Rewrite (N, | |
5903 | Make_Qualified_Expression (Loc, | |
fbf5a39b AC |
5904 | Subtype_Mark => |
5905 | New_Occurrence_Of (Etype (Disc), Loc), | |
5906 | Expression => | |
ffe9aba8 AC |
5907 | New_Copy_Tree (Node (Dcon)))); |
5908 | Analyze_And_Resolve (N, Etype (Disc)); | |
fbf5a39b AC |
5909 | |
5910 | -- In case that comes out as a static expression, | |
5911 | -- reset it (a selected component is never static). | |
5912 | ||
5913 | Set_Is_Static_Expression (N, False); | |
5914 | return; | |
5915 | ||
5916 | -- Otherwise we can just copy the constraint, but the | |
ffe9aba8 AC |
5917 | -- result is certainly not static! In some cases the |
5918 | -- discriminant constraint has been analyzed in the | |
5919 | -- context of the original subtype indication, but for | |
5920 | -- itypes the constraint might not have been analyzed | |
5921 | -- yet, and this must be done now. | |
fbf5a39b | 5922 | |
70482933 | 5923 | else |
ffe9aba8 AC |
5924 | Rewrite (N, New_Copy_Tree (Node (Dcon))); |
5925 | Analyze_And_Resolve (N); | |
fbf5a39b AC |
5926 | Set_Is_Static_Expression (N, False); |
5927 | return; | |
70482933 | 5928 | end if; |
70482933 RK |
5929 | end if; |
5930 | ||
fbf5a39b AC |
5931 | Next_Elmt (Dcon); |
5932 | Next_Discriminant (Disc); | |
5933 | end loop Discr_Loop; | |
70482933 | 5934 | |
fbf5a39b AC |
5935 | -- Note: the above loop should always find a matching |
5936 | -- discriminant, but if it does not, we just missed an | |
5937 | -- optimization due to some glitch (perhaps a previous | |
5938 | -- error), so ignore. | |
5939 | ||
5940 | end if; | |
70482933 RK |
5941 | end if; |
5942 | ||
5943 | -- The only remaining processing is in the case of a discriminant of | |
5944 | -- a concurrent object, where we rewrite the prefix to denote the | |
5945 | -- corresponding record type. If the type is derived and has renamed | |
5946 | -- discriminants, use corresponding discriminant, which is the one | |
5947 | -- that appears in the corresponding record. | |
5948 | ||
5949 | if not Is_Concurrent_Type (Ptyp) then | |
5950 | return; | |
5951 | end if; | |
5952 | ||
5953 | Disc := Entity (Selector_Name (N)); | |
5954 | ||
5955 | if Is_Derived_Type (Ptyp) | |
5956 | and then Present (Corresponding_Discriminant (Disc)) | |
5957 | then | |
5958 | Disc := Corresponding_Discriminant (Disc); | |
5959 | end if; | |
5960 | ||
5961 | New_N := | |
5962 | Make_Selected_Component (Loc, | |
5963 | Prefix => | |
5964 | Unchecked_Convert_To (Corresponding_Record_Type (Ptyp), | |
5965 | New_Copy_Tree (P)), | |
5966 | Selector_Name => Make_Identifier (Loc, Chars (Disc))); | |
5967 | ||
5968 | Rewrite (N, New_N); | |
5969 | Analyze (N); | |
5970 | end if; | |
70482933 RK |
5971 | end Expand_N_Selected_Component; |
5972 | ||
5973 | -------------------- | |
5974 | -- Expand_N_Slice -- | |
5975 | -------------------- | |
5976 | ||
5977 | procedure Expand_N_Slice (N : Node_Id) is | |
5978 | Loc : constant Source_Ptr := Sloc (N); | |
5979 | Typ : constant Entity_Id := Etype (N); | |
5980 | Pfx : constant Node_Id := Prefix (N); | |
5981 | Ptp : Entity_Id := Etype (Pfx); | |
fbf5a39b | 5982 | |
81a5b587 | 5983 | function Is_Procedure_Actual (N : Node_Id) return Boolean; |
c6a60aa1 RD |
5984 | -- Check whether the argument is an actual for a procedure call, |
5985 | -- in which case the expansion of a bit-packed slice is deferred | |
5986 | -- until the call itself is expanded. The reason this is required | |
5987 | -- is that we might have an IN OUT or OUT parameter, and the copy out | |
5988 | -- is essential, and that copy out would be missed if we created a | |
5989 | -- temporary here in Expand_N_Slice. Note that we don't bother | |
5990 | -- to test specifically for an IN OUT or OUT mode parameter, since it | |
5991 | -- is a bit tricky to do, and it is harmless to defer expansion | |
5992 | -- in the IN case, since the call processing will still generate the | |
5993 | -- appropriate copy in operation, which will take care of the slice. | |
81a5b587 | 5994 | |
fbf5a39b AC |
5995 | procedure Make_Temporary; |
5996 | -- Create a named variable for the value of the slice, in | |
5997 | -- cases where the back-end cannot handle it properly, e.g. | |
5998 | -- when packed types or unaligned slices are involved. | |
5999 | ||
81a5b587 AC |
6000 | ------------------------- |
6001 | -- Is_Procedure_Actual -- | |
6002 | ------------------------- | |
6003 | ||
6004 | function Is_Procedure_Actual (N : Node_Id) return Boolean is | |
6005 | Par : Node_Id := Parent (N); | |
08aa9a4a | 6006 | |
81a5b587 | 6007 | begin |
81a5b587 | 6008 | loop |
c6a60aa1 RD |
6009 | -- If our parent is a procedure call we can return |
6010 | ||
81a5b587 AC |
6011 | if Nkind (Par) = N_Procedure_Call_Statement then |
6012 | return True; | |
6b6fcd3e | 6013 | |
c6a60aa1 RD |
6014 | -- If our parent is a type conversion, keep climbing the |
6015 | -- tree, since a type conversion can be a procedure actual. | |
6016 | -- Also keep climbing if parameter association or a qualified | |
6017 | -- expression, since these are additional cases that do can | |
6018 | -- appear on procedure actuals. | |
6b6fcd3e | 6019 | |
c6a60aa1 RD |
6020 | elsif Nkind (Par) = N_Type_Conversion |
6021 | or else Nkind (Par) = N_Parameter_Association | |
6022 | or else Nkind (Par) = N_Qualified_Expression | |
6023 | then | |
81a5b587 | 6024 | Par := Parent (Par); |
c6a60aa1 RD |
6025 | |
6026 | -- Any other case is not what we are looking for | |
6027 | ||
6028 | else | |
6029 | return False; | |
81a5b587 AC |
6030 | end if; |
6031 | end loop; | |
81a5b587 AC |
6032 | end Is_Procedure_Actual; |
6033 | ||
fbf5a39b AC |
6034 | -------------------- |
6035 | -- Make_Temporary -- | |
6036 | -------------------- | |
6037 | ||
6038 | procedure Make_Temporary is | |
6039 | Decl : Node_Id; | |
6040 | Ent : constant Entity_Id := | |
6041 | Make_Defining_Identifier (Loc, New_Internal_Name ('T')); | |
6042 | begin | |
6043 | Decl := | |
6044 | Make_Object_Declaration (Loc, | |
6045 | Defining_Identifier => Ent, | |
6046 | Object_Definition => New_Occurrence_Of (Typ, Loc)); | |
6047 | ||
6048 | Set_No_Initialization (Decl); | |
6049 | ||
6050 | Insert_Actions (N, New_List ( | |
6051 | Decl, | |
6052 | Make_Assignment_Statement (Loc, | |
6053 | Name => New_Occurrence_Of (Ent, Loc), | |
6054 | Expression => Relocate_Node (N)))); | |
6055 | ||
6056 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
6057 | Analyze_And_Resolve (N, Typ); | |
6058 | end Make_Temporary; | |
6059 | ||
6060 | -- Start of processing for Expand_N_Slice | |
70482933 RK |
6061 | |
6062 | begin | |
6063 | -- Special handling for access types | |
6064 | ||
6065 | if Is_Access_Type (Ptp) then | |
6066 | ||
70482933 RK |
6067 | Ptp := Designated_Type (Ptp); |
6068 | ||
e6f69614 AC |
6069 | Rewrite (Pfx, |
6070 | Make_Explicit_Dereference (Sloc (N), | |
6071 | Prefix => Relocate_Node (Pfx))); | |
70482933 | 6072 | |
e6f69614 | 6073 | Analyze_And_Resolve (Pfx, Ptp); |
70482933 RK |
6074 | end if; |
6075 | ||
6076 | -- Range checks are potentially also needed for cases involving | |
6077 | -- a slice indexed by a subtype indication, but Do_Range_Check | |
6078 | -- can currently only be set for expressions ??? | |
6079 | ||
6080 | if not Index_Checks_Suppressed (Ptp) | |
6081 | and then (not Is_Entity_Name (Pfx) | |
6082 | or else not Index_Checks_Suppressed (Entity (Pfx))) | |
6083 | and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication | |
6084 | then | |
6085 | Enable_Range_Check (Discrete_Range (N)); | |
6086 | end if; | |
6087 | ||
6088 | -- The remaining case to be handled is packed slices. We can leave | |
6089 | -- packed slices as they are in the following situations: | |
6090 | ||
6091 | -- 1. Right or left side of an assignment (we can handle this | |
6092 | -- situation correctly in the assignment statement expansion). | |
6093 | ||
6094 | -- 2. Prefix of indexed component (the slide is optimized away | |
6095 | -- in this case, see the start of Expand_N_Slice. | |
6096 | ||
6097 | -- 3. Object renaming declaration, since we want the name of | |
6098 | -- the slice, not the value. | |
6099 | ||
6100 | -- 4. Argument to procedure call, since copy-in/copy-out handling | |
6101 | -- may be required, and this is handled in the expansion of | |
6102 | -- call itself. | |
6103 | ||
6104 | -- 5. Prefix of an address attribute (this is an error which | |
6105 | -- is caught elsewhere, and the expansion would intefere | |
6106 | -- with generating the error message). | |
6107 | ||
81a5b587 | 6108 | if not Is_Packed (Typ) then |
08aa9a4a AC |
6109 | |
6110 | -- Apply transformation for actuals of a function call, | |
6111 | -- where Expand_Actuals is not used. | |
81a5b587 AC |
6112 | |
6113 | if Nkind (Parent (N)) = N_Function_Call | |
6114 | and then Is_Possibly_Unaligned_Slice (N) | |
6115 | then | |
6116 | Make_Temporary; | |
6117 | end if; | |
6118 | ||
6119 | elsif Nkind (Parent (N)) = N_Assignment_Statement | |
6120 | or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement | |
6121 | and then Parent (N) = Name (Parent (Parent (N)))) | |
70482933 | 6122 | then |
81a5b587 | 6123 | return; |
70482933 | 6124 | |
81a5b587 AC |
6125 | elsif Nkind (Parent (N)) = N_Indexed_Component |
6126 | or else Is_Renamed_Object (N) | |
6127 | or else Is_Procedure_Actual (N) | |
6128 | then | |
6129 | return; | |
70482933 | 6130 | |
91b1417d AC |
6131 | elsif Nkind (Parent (N)) = N_Attribute_Reference |
6132 | and then Attribute_Name (Parent (N)) = Name_Address | |
fbf5a39b | 6133 | then |
81a5b587 AC |
6134 | return; |
6135 | ||
6136 | else | |
fbf5a39b | 6137 | Make_Temporary; |
70482933 RK |
6138 | end if; |
6139 | end Expand_N_Slice; | |
6140 | ||
6141 | ------------------------------ | |
6142 | -- Expand_N_Type_Conversion -- | |
6143 | ------------------------------ | |
6144 | ||
6145 | procedure Expand_N_Type_Conversion (N : Node_Id) is | |
6146 | Loc : constant Source_Ptr := Sloc (N); | |
6147 | Operand : constant Node_Id := Expression (N); | |
6148 | Target_Type : constant Entity_Id := Etype (N); | |
6149 | Operand_Type : Entity_Id := Etype (Operand); | |
6150 | ||
6151 | procedure Handle_Changed_Representation; | |
6152 | -- This is called in the case of record and array type conversions | |
6153 | -- to see if there is a change of representation to be handled. | |
6154 | -- Change of representation is actually handled at the assignment | |
6155 | -- statement level, and what this procedure does is rewrite node N | |
6156 | -- conversion as an assignment to temporary. If there is no change | |
6157 | -- of representation, then the conversion node is unchanged. | |
6158 | ||
6159 | procedure Real_Range_Check; | |
6160 | -- Handles generation of range check for real target value | |
6161 | ||
6162 | ----------------------------------- | |
6163 | -- Handle_Changed_Representation -- | |
6164 | ----------------------------------- | |
6165 | ||
6166 | procedure Handle_Changed_Representation is | |
6167 | Temp : Entity_Id; | |
6168 | Decl : Node_Id; | |
6169 | Odef : Node_Id; | |
6170 | Disc : Node_Id; | |
6171 | N_Ix : Node_Id; | |
6172 | Cons : List_Id; | |
6173 | ||
6174 | begin | |
6175 | -- Nothing to do if no change of representation | |
6176 | ||
6177 | if Same_Representation (Operand_Type, Target_Type) then | |
6178 | return; | |
6179 | ||
6180 | -- The real change of representation work is done by the assignment | |
6181 | -- statement processing. So if this type conversion is appearing as | |
6182 | -- the expression of an assignment statement, nothing needs to be | |
6183 | -- done to the conversion. | |
6184 | ||
6185 | elsif Nkind (Parent (N)) = N_Assignment_Statement then | |
6186 | return; | |
6187 | ||
6188 | -- Otherwise we need to generate a temporary variable, and do the | |
6189 | -- change of representation assignment into that temporary variable. | |
6190 | -- The conversion is then replaced by a reference to this variable. | |
6191 | ||
6192 | else | |
6193 | Cons := No_List; | |
6194 | ||
6195 | -- If type is unconstrained we have to add a constraint, | |
6196 | -- copied from the actual value of the left hand side. | |
6197 | ||
6198 | if not Is_Constrained (Target_Type) then | |
6199 | if Has_Discriminants (Operand_Type) then | |
6200 | Disc := First_Discriminant (Operand_Type); | |
fbf5a39b AC |
6201 | |
6202 | if Disc /= First_Stored_Discriminant (Operand_Type) then | |
6203 | Disc := First_Stored_Discriminant (Operand_Type); | |
6204 | end if; | |
6205 | ||
70482933 RK |
6206 | Cons := New_List; |
6207 | while Present (Disc) loop | |
6208 | Append_To (Cons, | |
6209 | Make_Selected_Component (Loc, | |
fbf5a39b | 6210 | Prefix => Duplicate_Subexpr_Move_Checks (Operand), |
70482933 RK |
6211 | Selector_Name => |
6212 | Make_Identifier (Loc, Chars (Disc)))); | |
6213 | Next_Discriminant (Disc); | |
6214 | end loop; | |
6215 | ||
6216 | elsif Is_Array_Type (Operand_Type) then | |
6217 | N_Ix := First_Index (Target_Type); | |
6218 | Cons := New_List; | |
6219 | ||
6220 | for J in 1 .. Number_Dimensions (Operand_Type) loop | |
6221 | ||
6222 | -- We convert the bounds explicitly. We use an unchecked | |
6223 | -- conversion because bounds checks are done elsewhere. | |
6224 | ||
6225 | Append_To (Cons, | |
6226 | Make_Range (Loc, | |
6227 | Low_Bound => | |
6228 | Unchecked_Convert_To (Etype (N_Ix), | |
6229 | Make_Attribute_Reference (Loc, | |
6230 | Prefix => | |
fbf5a39b | 6231 | Duplicate_Subexpr_No_Checks |
70482933 RK |
6232 | (Operand, Name_Req => True), |
6233 | Attribute_Name => Name_First, | |
6234 | Expressions => New_List ( | |
6235 | Make_Integer_Literal (Loc, J)))), | |
6236 | ||
6237 | High_Bound => | |
6238 | Unchecked_Convert_To (Etype (N_Ix), | |
6239 | Make_Attribute_Reference (Loc, | |
6240 | Prefix => | |
fbf5a39b | 6241 | Duplicate_Subexpr_No_Checks |
70482933 RK |
6242 | (Operand, Name_Req => True), |
6243 | Attribute_Name => Name_Last, | |
6244 | Expressions => New_List ( | |
6245 | Make_Integer_Literal (Loc, J)))))); | |
6246 | ||
6247 | Next_Index (N_Ix); | |
6248 | end loop; | |
6249 | end if; | |
6250 | end if; | |
6251 | ||
6252 | Odef := New_Occurrence_Of (Target_Type, Loc); | |
6253 | ||
6254 | if Present (Cons) then | |
6255 | Odef := | |
6256 | Make_Subtype_Indication (Loc, | |
6257 | Subtype_Mark => Odef, | |
6258 | Constraint => | |
6259 | Make_Index_Or_Discriminant_Constraint (Loc, | |
6260 | Constraints => Cons)); | |
6261 | end if; | |
6262 | ||
6263 | Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C')); | |
6264 | Decl := | |
6265 | Make_Object_Declaration (Loc, | |
6266 | Defining_Identifier => Temp, | |
6267 | Object_Definition => Odef); | |
6268 | ||
6269 | Set_No_Initialization (Decl, True); | |
6270 | ||
6271 | -- Insert required actions. It is essential to suppress checks | |
6272 | -- since we have suppressed default initialization, which means | |
6273 | -- that the variable we create may have no discriminants. | |
6274 | ||
6275 | Insert_Actions (N, | |
6276 | New_List ( | |
6277 | Decl, | |
6278 | Make_Assignment_Statement (Loc, | |
6279 | Name => New_Occurrence_Of (Temp, Loc), | |
6280 | Expression => Relocate_Node (N))), | |
6281 | Suppress => All_Checks); | |
6282 | ||
6283 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); | |
6284 | return; | |
6285 | end if; | |
6286 | end Handle_Changed_Representation; | |
6287 | ||
6288 | ---------------------- | |
6289 | -- Real_Range_Check -- | |
6290 | ---------------------- | |
6291 | ||
6292 | -- Case of conversions to floating-point or fixed-point. If range | |
6293 | -- checks are enabled and the target type has a range constraint, | |
6294 | -- we convert: | |
6295 | ||
6296 | -- typ (x) | |
6297 | ||
6298 | -- to | |
6299 | ||
6300 | -- Tnn : typ'Base := typ'Base (x); | |
6301 | -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last] | |
6302 | -- Tnn | |
6303 | ||
fbf5a39b AC |
6304 | -- This is necessary when there is a conversion of integer to float |
6305 | -- or to fixed-point to ensure that the correct checks are made. It | |
6306 | -- is not necessary for float to float where it is enough to simply | |
6307 | -- set the Do_Range_Check flag. | |
6308 | ||
70482933 RK |
6309 | procedure Real_Range_Check is |
6310 | Btyp : constant Entity_Id := Base_Type (Target_Type); | |
6311 | Lo : constant Node_Id := Type_Low_Bound (Target_Type); | |
6312 | Hi : constant Node_Id := Type_High_Bound (Target_Type); | |
fbf5a39b | 6313 | Xtyp : constant Entity_Id := Etype (Operand); |
70482933 RK |
6314 | Conv : Node_Id; |
6315 | Tnn : Entity_Id; | |
6316 | ||
6317 | begin | |
6318 | -- Nothing to do if conversion was rewritten | |
6319 | ||
6320 | if Nkind (N) /= N_Type_Conversion then | |
6321 | return; | |
6322 | end if; | |
6323 | ||
6324 | -- Nothing to do if range checks suppressed, or target has the | |
6325 | -- same range as the base type (or is the base type). | |
6326 | ||
6327 | if Range_Checks_Suppressed (Target_Type) | |
6328 | or else (Lo = Type_Low_Bound (Btyp) | |
6329 | and then | |
6330 | Hi = Type_High_Bound (Btyp)) | |
6331 | then | |
6332 | return; | |
6333 | end if; | |
6334 | ||
6335 | -- Nothing to do if expression is an entity on which checks | |
6336 | -- have been suppressed. | |
6337 | ||
fbf5a39b AC |
6338 | if Is_Entity_Name (Operand) |
6339 | and then Range_Checks_Suppressed (Entity (Operand)) | |
6340 | then | |
6341 | return; | |
6342 | end if; | |
6343 | ||
6344 | -- Nothing to do if bounds are all static and we can tell that | |
6345 | -- the expression is within the bounds of the target. Note that | |
6346 | -- if the operand is of an unconstrained floating-point type, | |
6347 | -- then we do not trust it to be in range (might be infinite) | |
6348 | ||
6349 | declare | |
6350 | S_Lo : constant Node_Id := Type_Low_Bound (Xtyp); | |
6351 | S_Hi : constant Node_Id := Type_High_Bound (Xtyp); | |
6352 | ||
6353 | begin | |
6354 | if (not Is_Floating_Point_Type (Xtyp) | |
6355 | or else Is_Constrained (Xtyp)) | |
6356 | and then Compile_Time_Known_Value (S_Lo) | |
6357 | and then Compile_Time_Known_Value (S_Hi) | |
6358 | and then Compile_Time_Known_Value (Hi) | |
6359 | and then Compile_Time_Known_Value (Lo) | |
6360 | then | |
6361 | declare | |
6362 | D_Lov : constant Ureal := Expr_Value_R (Lo); | |
6363 | D_Hiv : constant Ureal := Expr_Value_R (Hi); | |
6364 | S_Lov : Ureal; | |
6365 | S_Hiv : Ureal; | |
6366 | ||
6367 | begin | |
6368 | if Is_Real_Type (Xtyp) then | |
6369 | S_Lov := Expr_Value_R (S_Lo); | |
6370 | S_Hiv := Expr_Value_R (S_Hi); | |
6371 | else | |
6372 | S_Lov := UR_From_Uint (Expr_Value (S_Lo)); | |
6373 | S_Hiv := UR_From_Uint (Expr_Value (S_Hi)); | |
6374 | end if; | |
6375 | ||
6376 | if D_Hiv > D_Lov | |
6377 | and then S_Lov >= D_Lov | |
6378 | and then S_Hiv <= D_Hiv | |
6379 | then | |
6380 | Set_Do_Range_Check (Operand, False); | |
6381 | return; | |
6382 | end if; | |
6383 | end; | |
6384 | end if; | |
6385 | end; | |
6386 | ||
6387 | -- For float to float conversions, we are done | |
6388 | ||
6389 | if Is_Floating_Point_Type (Xtyp) | |
6390 | and then | |
6391 | Is_Floating_Point_Type (Btyp) | |
70482933 RK |
6392 | then |
6393 | return; | |
6394 | end if; | |
6395 | ||
fbf5a39b | 6396 | -- Otherwise rewrite the conversion as described above |
70482933 RK |
6397 | |
6398 | Conv := Relocate_Node (N); | |
6399 | Rewrite | |
6400 | (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc)); | |
6401 | Set_Etype (Conv, Btyp); | |
6402 | ||
fbf5a39b AC |
6403 | -- Enable overflow except in the case of integer to float |
6404 | -- conversions, where it is never required, since we can | |
6405 | -- never have overflow in this case. | |
70482933 | 6406 | |
fbf5a39b AC |
6407 | if not Is_Integer_Type (Etype (Operand)) then |
6408 | Enable_Overflow_Check (Conv); | |
70482933 RK |
6409 | end if; |
6410 | ||
6411 | Tnn := | |
6412 | Make_Defining_Identifier (Loc, | |
6413 | Chars => New_Internal_Name ('T')); | |
6414 | ||
6415 | Insert_Actions (N, New_List ( | |
6416 | Make_Object_Declaration (Loc, | |
6417 | Defining_Identifier => Tnn, | |
6418 | Object_Definition => New_Occurrence_Of (Btyp, Loc), | |
6419 | Expression => Conv), | |
6420 | ||
6421 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
6422 | Condition => |
6423 | Make_Or_Else (Loc, | |
6424 | Left_Opnd => | |
6425 | Make_Op_Lt (Loc, | |
6426 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6427 | Right_Opnd => | |
6428 | Make_Attribute_Reference (Loc, | |
6429 | Attribute_Name => Name_First, | |
6430 | Prefix => | |
6431 | New_Occurrence_Of (Target_Type, Loc))), | |
70482933 | 6432 | |
07fc65c4 GB |
6433 | Right_Opnd => |
6434 | Make_Op_Gt (Loc, | |
6435 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6436 | Right_Opnd => | |
6437 | Make_Attribute_Reference (Loc, | |
6438 | Attribute_Name => Name_Last, | |
6439 | Prefix => | |
6440 | New_Occurrence_Of (Target_Type, Loc)))), | |
6441 | Reason => CE_Range_Check_Failed))); | |
70482933 RK |
6442 | |
6443 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
6444 | Analyze_And_Resolve (N, Btyp); | |
6445 | end Real_Range_Check; | |
6446 | ||
6447 | -- Start of processing for Expand_N_Type_Conversion | |
6448 | ||
6449 | begin | |
6450 | -- Nothing at all to do if conversion is to the identical type | |
6451 | -- so remove the conversion completely, it is useless. | |
6452 | ||
6453 | if Operand_Type = Target_Type then | |
fbf5a39b | 6454 | Rewrite (N, Relocate_Node (Operand)); |
70482933 RK |
6455 | return; |
6456 | end if; | |
6457 | ||
6458 | -- Deal with Vax floating-point cases | |
6459 | ||
6460 | if Vax_Float (Operand_Type) or else Vax_Float (Target_Type) then | |
6461 | Expand_Vax_Conversion (N); | |
6462 | return; | |
6463 | end if; | |
6464 | ||
6465 | -- Nothing to do if this is the second argument of read. This | |
6466 | -- is a "backwards" conversion that will be handled by the | |
6467 | -- specialized code in attribute processing. | |
6468 | ||
6469 | if Nkind (Parent (N)) = N_Attribute_Reference | |
6470 | and then Attribute_Name (Parent (N)) = Name_Read | |
6471 | and then Next (First (Expressions (Parent (N)))) = N | |
6472 | then | |
6473 | return; | |
6474 | end if; | |
6475 | ||
6476 | -- Here if we may need to expand conversion | |
6477 | ||
6478 | -- Special case of converting from non-standard boolean type | |
6479 | ||
6480 | if Is_Boolean_Type (Operand_Type) | |
6481 | and then (Nonzero_Is_True (Operand_Type)) | |
6482 | then | |
6483 | Adjust_Condition (Operand); | |
6484 | Set_Etype (Operand, Standard_Boolean); | |
6485 | Operand_Type := Standard_Boolean; | |
6486 | end if; | |
6487 | ||
6488 | -- Case of converting to an access type | |
6489 | ||
6490 | if Is_Access_Type (Target_Type) then | |
6491 | ||
6492 | -- Apply an accessibility check if the operand is an | |
6493 | -- access parameter. Note that other checks may still | |
6494 | -- need to be applied below (such as tagged type checks). | |
6495 | ||
6496 | if Is_Entity_Name (Operand) | |
6497 | and then Ekind (Entity (Operand)) in Formal_Kind | |
6498 | and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type | |
6499 | then | |
6500 | Apply_Accessibility_Check (Operand, Target_Type); | |
6501 | ||
6502 | -- If the level of the operand type is statically deeper | |
6503 | -- then the level of the target type, then force Program_Error. | |
6504 | -- Note that this can only occur for cases where the attribute | |
6505 | -- is within the body of an instantiation (otherwise the | |
6506 | -- conversion will already have been rejected as illegal). | |
6507 | -- Note: warnings are issued by the analyzer for the instance | |
6508 | -- cases. | |
6509 | ||
6510 | elsif In_Instance_Body | |
07fc65c4 GB |
6511 | and then Type_Access_Level (Operand_Type) > |
6512 | Type_Access_Level (Target_Type) | |
70482933 | 6513 | then |
07fc65c4 GB |
6514 | Rewrite (N, |
6515 | Make_Raise_Program_Error (Sloc (N), | |
6516 | Reason => PE_Accessibility_Check_Failed)); | |
70482933 RK |
6517 | Set_Etype (N, Target_Type); |
6518 | ||
6519 | -- When the operand is a selected access discriminant | |
6520 | -- the check needs to be made against the level of the | |
6521 | -- object denoted by the prefix of the selected name. | |
6522 | -- Force Program_Error for this case as well (this | |
6523 | -- accessibility violation can only happen if within | |
6524 | -- the body of an instantiation). | |
6525 | ||
6526 | elsif In_Instance_Body | |
6527 | and then Ekind (Operand_Type) = E_Anonymous_Access_Type | |
6528 | and then Nkind (Operand) = N_Selected_Component | |
6529 | and then Object_Access_Level (Operand) > | |
6530 | Type_Access_Level (Target_Type) | |
6531 | then | |
07fc65c4 GB |
6532 | Rewrite (N, |
6533 | Make_Raise_Program_Error (Sloc (N), | |
6534 | Reason => PE_Accessibility_Check_Failed)); | |
70482933 RK |
6535 | Set_Etype (N, Target_Type); |
6536 | end if; | |
6537 | end if; | |
6538 | ||
6539 | -- Case of conversions of tagged types and access to tagged types | |
6540 | ||
6541 | -- When needed, that is to say when the expression is class-wide, | |
6542 | -- Add runtime a tag check for (strict) downward conversion by using | |
6543 | -- the membership test, generating: | |
6544 | ||
6545 | -- [constraint_error when Operand not in Target_Type'Class] | |
6546 | ||
6547 | -- or in the access type case | |
6548 | ||
6549 | -- [constraint_error | |
6550 | -- when Operand /= null | |
6551 | -- and then Operand.all not in | |
6552 | -- Designated_Type (Target_Type)'Class] | |
6553 | ||
6554 | if (Is_Access_Type (Target_Type) | |
6555 | and then Is_Tagged_Type (Designated_Type (Target_Type))) | |
6556 | or else Is_Tagged_Type (Target_Type) | |
6557 | then | |
6558 | -- Do not do any expansion in the access type case if the | |
6559 | -- parent is a renaming, since this is an error situation | |
6560 | -- which will be caught by Sem_Ch8, and the expansion can | |
6561 | -- intefere with this error check. | |
6562 | ||
6563 | if Is_Access_Type (Target_Type) | |
6564 | and then Is_Renamed_Object (N) | |
6565 | then | |
6566 | return; | |
6567 | end if; | |
6568 | ||
6569 | -- Oherwise, proceed with processing tagged conversion | |
6570 | ||
6571 | declare | |
6572 | Actual_Operand_Type : Entity_Id; | |
6573 | Actual_Target_Type : Entity_Id; | |
6574 | ||
6575 | Cond : Node_Id; | |
6576 | ||
6577 | begin | |
6578 | if Is_Access_Type (Target_Type) then | |
6579 | Actual_Operand_Type := Designated_Type (Operand_Type); | |
6580 | Actual_Target_Type := Designated_Type (Target_Type); | |
6581 | ||
6582 | else | |
6583 | Actual_Operand_Type := Operand_Type; | |
6584 | Actual_Target_Type := Target_Type; | |
6585 | end if; | |
6586 | ||
6587 | if Is_Class_Wide_Type (Actual_Operand_Type) | |
6588 | and then Root_Type (Actual_Operand_Type) /= Actual_Target_Type | |
6589 | and then Is_Ancestor | |
6590 | (Root_Type (Actual_Operand_Type), | |
6591 | Actual_Target_Type) | |
6592 | and then not Tag_Checks_Suppressed (Actual_Target_Type) | |
6593 | then | |
6594 | -- The conversion is valid for any descendant of the | |
6595 | -- target type | |
6596 | ||
6597 | Actual_Target_Type := Class_Wide_Type (Actual_Target_Type); | |
6598 | ||
6599 | if Is_Access_Type (Target_Type) then | |
6600 | Cond := | |
6601 | Make_And_Then (Loc, | |
6602 | Left_Opnd => | |
6603 | Make_Op_Ne (Loc, | |
fbf5a39b | 6604 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), |
70482933 RK |
6605 | Right_Opnd => Make_Null (Loc)), |
6606 | ||
6607 | Right_Opnd => | |
6608 | Make_Not_In (Loc, | |
6609 | Left_Opnd => | |
6610 | Make_Explicit_Dereference (Loc, | |
fbf5a39b AC |
6611 | Prefix => |
6612 | Duplicate_Subexpr_No_Checks (Operand)), | |
70482933 RK |
6613 | Right_Opnd => |
6614 | New_Reference_To (Actual_Target_Type, Loc))); | |
6615 | ||
6616 | else | |
6617 | Cond := | |
6618 | Make_Not_In (Loc, | |
fbf5a39b | 6619 | Left_Opnd => Duplicate_Subexpr_No_Checks (Operand), |
70482933 RK |
6620 | Right_Opnd => |
6621 | New_Reference_To (Actual_Target_Type, Loc)); | |
6622 | end if; | |
6623 | ||
6624 | Insert_Action (N, | |
6625 | Make_Raise_Constraint_Error (Loc, | |
07fc65c4 GB |
6626 | Condition => Cond, |
6627 | Reason => CE_Tag_Check_Failed)); | |
70482933 | 6628 | |
615cbd95 AC |
6629 | declare |
6630 | Conv : Node_Id; | |
6631 | begin | |
6632 | Conv := | |
6633 | Make_Unchecked_Type_Conversion (Loc, | |
6634 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
6635 | Expression => Relocate_Node (Expression (N))); | |
6636 | Rewrite (N, Conv); | |
6637 | Analyze_And_Resolve (N, Target_Type); | |
6638 | end; | |
70482933 RK |
6639 | end if; |
6640 | end; | |
6641 | ||
6642 | -- Case of other access type conversions | |
6643 | ||
6644 | elsif Is_Access_Type (Target_Type) then | |
6645 | Apply_Constraint_Check (Operand, Target_Type); | |
6646 | ||
6647 | -- Case of conversions from a fixed-point type | |
6648 | ||
6649 | -- These conversions require special expansion and processing, found | |
6650 | -- in the Exp_Fixd package. We ignore cases where Conversion_OK is | |
6651 | -- set, since from a semantic point of view, these are simple integer | |
6652 | -- conversions, which do not need further processing. | |
6653 | ||
6654 | elsif Is_Fixed_Point_Type (Operand_Type) | |
6655 | and then not Conversion_OK (N) | |
6656 | then | |
6657 | -- We should never see universal fixed at this case, since the | |
6658 | -- expansion of the constituent divide or multiply should have | |
6659 | -- eliminated the explicit mention of universal fixed. | |
6660 | ||
6661 | pragma Assert (Operand_Type /= Universal_Fixed); | |
6662 | ||
6663 | -- Check for special case of the conversion to universal real | |
6664 | -- that occurs as a result of the use of a round attribute. | |
6665 | -- In this case, the real type for the conversion is taken | |
6666 | -- from the target type of the Round attribute and the | |
6667 | -- result must be marked as rounded. | |
6668 | ||
6669 | if Target_Type = Universal_Real | |
6670 | and then Nkind (Parent (N)) = N_Attribute_Reference | |
6671 | and then Attribute_Name (Parent (N)) = Name_Round | |
6672 | then | |
6673 | Set_Rounded_Result (N); | |
6674 | Set_Etype (N, Etype (Parent (N))); | |
6675 | end if; | |
6676 | ||
6677 | -- Otherwise do correct fixed-conversion, but skip these if the | |
6678 | -- Conversion_OK flag is set, because from a semantic point of | |
6679 | -- view these are simple integer conversions needing no further | |
6680 | -- processing (the backend will simply treat them as integers) | |
6681 | ||
6682 | if not Conversion_OK (N) then | |
6683 | if Is_Fixed_Point_Type (Etype (N)) then | |
6684 | Expand_Convert_Fixed_To_Fixed (N); | |
6685 | Real_Range_Check; | |
6686 | ||
6687 | elsif Is_Integer_Type (Etype (N)) then | |
6688 | Expand_Convert_Fixed_To_Integer (N); | |
6689 | ||
6690 | else | |
6691 | pragma Assert (Is_Floating_Point_Type (Etype (N))); | |
6692 | Expand_Convert_Fixed_To_Float (N); | |
6693 | Real_Range_Check; | |
6694 | end if; | |
6695 | end if; | |
6696 | ||
6697 | -- Case of conversions to a fixed-point type | |
6698 | ||
6699 | -- These conversions require special expansion and processing, found | |
6700 | -- in the Exp_Fixd package. Again, ignore cases where Conversion_OK | |
6701 | -- is set, since from a semantic point of view, these are simple | |
6702 | -- integer conversions, which do not need further processing. | |
6703 | ||
6704 | elsif Is_Fixed_Point_Type (Target_Type) | |
6705 | and then not Conversion_OK (N) | |
6706 | then | |
6707 | if Is_Integer_Type (Operand_Type) then | |
6708 | Expand_Convert_Integer_To_Fixed (N); | |
6709 | Real_Range_Check; | |
6710 | else | |
6711 | pragma Assert (Is_Floating_Point_Type (Operand_Type)); | |
6712 | Expand_Convert_Float_To_Fixed (N); | |
6713 | Real_Range_Check; | |
6714 | end if; | |
6715 | ||
6716 | -- Case of float-to-integer conversions | |
6717 | ||
6718 | -- We also handle float-to-fixed conversions with Conversion_OK set | |
6719 | -- since semantically the fixed-point target is treated as though it | |
6720 | -- were an integer in such cases. | |
6721 | ||
6722 | elsif Is_Floating_Point_Type (Operand_Type) | |
6723 | and then | |
6724 | (Is_Integer_Type (Target_Type) | |
6725 | or else | |
6726 | (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N))) | |
6727 | then | |
6728 | -- Special processing required if the conversion is the expression | |
6729 | -- of a Truncation attribute reference. In this case we replace: | |
6730 | ||
6731 | -- ityp (ftyp'Truncation (x)) | |
6732 | ||
6733 | -- by | |
6734 | ||
6735 | -- ityp (x) | |
6736 | ||
2717634d | 6737 | -- with the Float_Truncate flag set. This is clearly more efficient |
70482933 RK |
6738 | |
6739 | if Nkind (Operand) = N_Attribute_Reference | |
6740 | and then Attribute_Name (Operand) = Name_Truncation | |
6741 | then | |
6742 | Rewrite (Operand, | |
6743 | Relocate_Node (First (Expressions (Operand)))); | |
6744 | Set_Float_Truncate (N, True); | |
6745 | end if; | |
6746 | ||
6747 | -- One more check here, gcc is still not able to do conversions of | |
6748 | -- this type with proper overflow checking, and so gigi is doing an | |
6749 | -- approximation of what is required by doing floating-point compares | |
6750 | -- with the end-point. But that can lose precision in some cases, and | |
6751 | -- give a wrong result. Converting the operand to Long_Long_Float is | |
6752 | -- helpful, but still does not catch all cases with 64-bit integers | |
6753 | -- on targets with only 64-bit floats ??? | |
6754 | ||
fbf5a39b AC |
6755 | if Do_Range_Check (Operand) then |
6756 | Rewrite (Operand, | |
70482933 RK |
6757 | Make_Type_Conversion (Loc, |
6758 | Subtype_Mark => | |
6759 | New_Occurrence_Of (Standard_Long_Long_Float, Loc), | |
6760 | Expression => | |
fbf5a39b | 6761 | Relocate_Node (Operand))); |
70482933 | 6762 | |
fbf5a39b AC |
6763 | Set_Etype (Operand, Standard_Long_Long_Float); |
6764 | Enable_Range_Check (Operand); | |
6765 | Set_Do_Range_Check (Expression (Operand), False); | |
70482933 RK |
6766 | end if; |
6767 | ||
6768 | -- Case of array conversions | |
6769 | ||
6770 | -- Expansion of array conversions, add required length/range checks | |
6771 | -- but only do this if there is no change of representation. For | |
6772 | -- handling of this case, see Handle_Changed_Representation. | |
6773 | ||
6774 | elsif Is_Array_Type (Target_Type) then | |
6775 | ||
6776 | if Is_Constrained (Target_Type) then | |
6777 | Apply_Length_Check (Operand, Target_Type); | |
6778 | else | |
6779 | Apply_Range_Check (Operand, Target_Type); | |
6780 | end if; | |
6781 | ||
6782 | Handle_Changed_Representation; | |
6783 | ||
6784 | -- Case of conversions of discriminated types | |
6785 | ||
6786 | -- Add required discriminant checks if target is constrained. Again | |
6787 | -- this change is skipped if we have a change of representation. | |
6788 | ||
6789 | elsif Has_Discriminants (Target_Type) | |
6790 | and then Is_Constrained (Target_Type) | |
6791 | then | |
6792 | Apply_Discriminant_Check (Operand, Target_Type); | |
6793 | Handle_Changed_Representation; | |
6794 | ||
6795 | -- Case of all other record conversions. The only processing required | |
6796 | -- is to check for a change of representation requiring the special | |
6797 | -- assignment processing. | |
6798 | ||
6799 | elsif Is_Record_Type (Target_Type) then | |
5d09245e AC |
6800 | |
6801 | -- Ada 2005 (AI-216): Program_Error is raised when converting from | |
6802 | -- a derived Unchecked_Union type to an unconstrained non-Unchecked_ | |
6803 | -- Union type if the operand lacks inferable discriminants. | |
6804 | ||
6805 | if Is_Derived_Type (Operand_Type) | |
6806 | and then Is_Unchecked_Union (Base_Type (Operand_Type)) | |
6807 | and then not Is_Constrained (Target_Type) | |
6808 | and then not Is_Unchecked_Union (Base_Type (Target_Type)) | |
6809 | and then not Has_Inferable_Discriminants (Operand) | |
6810 | then | |
6811 | -- To prevent Gigi from generating illegal code, we make a | |
6812 | -- Program_Error node, but we give it the target type of the | |
6813 | -- conversion. | |
6814 | ||
6815 | declare | |
6816 | PE : constant Node_Id := Make_Raise_Program_Error (Loc, | |
6817 | Reason => PE_Unchecked_Union_Restriction); | |
6818 | ||
6819 | begin | |
6820 | Set_Etype (PE, Target_Type); | |
6821 | Rewrite (N, PE); | |
6822 | ||
6823 | end; | |
6824 | else | |
6825 | Handle_Changed_Representation; | |
6826 | end if; | |
70482933 RK |
6827 | |
6828 | -- Case of conversions of enumeration types | |
6829 | ||
6830 | elsif Is_Enumeration_Type (Target_Type) then | |
6831 | ||
6832 | -- Special processing is required if there is a change of | |
6833 | -- representation (from enumeration representation clauses) | |
6834 | ||
6835 | if not Same_Representation (Target_Type, Operand_Type) then | |
6836 | ||
6837 | -- Convert: x(y) to x'val (ytyp'val (y)) | |
6838 | ||
6839 | Rewrite (N, | |
6840 | Make_Attribute_Reference (Loc, | |
6841 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
6842 | Attribute_Name => Name_Val, | |
6843 | Expressions => New_List ( | |
6844 | Make_Attribute_Reference (Loc, | |
6845 | Prefix => New_Occurrence_Of (Operand_Type, Loc), | |
6846 | Attribute_Name => Name_Pos, | |
6847 | Expressions => New_List (Operand))))); | |
6848 | ||
6849 | Analyze_And_Resolve (N, Target_Type); | |
6850 | end if; | |
6851 | ||
6852 | -- Case of conversions to floating-point | |
6853 | ||
6854 | elsif Is_Floating_Point_Type (Target_Type) then | |
6855 | Real_Range_Check; | |
6856 | ||
6857 | -- The remaining cases require no front end processing | |
6858 | ||
6859 | else | |
6860 | null; | |
6861 | end if; | |
6862 | ||
6863 | -- At this stage, either the conversion node has been transformed | |
6864 | -- into some other equivalent expression, or left as a conversion | |
6865 | -- that can be handled by Gigi. The conversions that Gigi can handle | |
6866 | -- are the following: | |
6867 | ||
6868 | -- Conversions with no change of representation or type | |
6869 | ||
6870 | -- Numeric conversions involving integer values, floating-point | |
6871 | -- values, and fixed-point values. Fixed-point values are allowed | |
6872 | -- only if Conversion_OK is set, i.e. if the fixed-point values | |
6873 | -- are to be treated as integers. | |
6874 | ||
5e1c00fa RD |
6875 | -- No other conversions should be passed to Gigi |
6876 | ||
6877 | -- Check: are these rules stated in sinfo??? if so, why restate here??? | |
70482933 | 6878 | |
fbf5a39b AC |
6879 | -- The only remaining step is to generate a range check if we still |
6880 | -- have a type conversion at this stage and Do_Range_Check is set. | |
6881 | -- For now we do this only for conversions of discrete types. | |
6882 | ||
6883 | if Nkind (N) = N_Type_Conversion | |
6884 | and then Is_Discrete_Type (Etype (N)) | |
6885 | then | |
6886 | declare | |
6887 | Expr : constant Node_Id := Expression (N); | |
6888 | Ftyp : Entity_Id; | |
6889 | Ityp : Entity_Id; | |
6890 | ||
6891 | begin | |
6892 | if Do_Range_Check (Expr) | |
6893 | and then Is_Discrete_Type (Etype (Expr)) | |
6894 | then | |
6895 | Set_Do_Range_Check (Expr, False); | |
6896 | ||
6897 | -- Before we do a range check, we have to deal with treating | |
6898 | -- a fixed-point operand as an integer. The way we do this | |
6899 | -- is simply to do an unchecked conversion to an appropriate | |
6900 | -- integer type large enough to hold the result. | |
6901 | ||
6902 | -- This code is not active yet, because we are only dealing | |
6903 | -- with discrete types so far ??? | |
6904 | ||
6905 | if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer | |
6906 | and then Treat_Fixed_As_Integer (Expr) | |
6907 | then | |
6908 | Ftyp := Base_Type (Etype (Expr)); | |
6909 | ||
6910 | if Esize (Ftyp) >= Esize (Standard_Integer) then | |
6911 | Ityp := Standard_Long_Long_Integer; | |
6912 | else | |
6913 | Ityp := Standard_Integer; | |
6914 | end if; | |
6915 | ||
6916 | Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr)); | |
6917 | end if; | |
6918 | ||
6919 | -- Reset overflow flag, since the range check will include | |
6920 | -- dealing with possible overflow, and generate the check | |
8a36a0cc AC |
6921 | -- If Address is either source or target type, suppress |
6922 | -- range check to avoid typing anomalies when it is a visible | |
6923 | -- integer type. | |
fbf5a39b AC |
6924 | |
6925 | Set_Do_Overflow_Check (N, False); | |
8a36a0cc AC |
6926 | if not Is_Descendent_Of_Address (Etype (Expr)) |
6927 | and then not Is_Descendent_Of_Address (Target_Type) | |
6928 | then | |
6929 | Generate_Range_Check | |
6930 | (Expr, Target_Type, CE_Range_Check_Failed); | |
6931 | end if; | |
fbf5a39b AC |
6932 | end if; |
6933 | end; | |
6934 | end if; | |
70482933 RK |
6935 | end Expand_N_Type_Conversion; |
6936 | ||
6937 | ----------------------------------- | |
6938 | -- Expand_N_Unchecked_Expression -- | |
6939 | ----------------------------------- | |
6940 | ||
6941 | -- Remove the unchecked expression node from the tree. It's job was simply | |
6942 | -- to make sure that its constituent expression was handled with checks | |
6943 | -- off, and now that that is done, we can remove it from the tree, and | |
6944 | -- indeed must, since gigi does not expect to see these nodes. | |
6945 | ||
6946 | procedure Expand_N_Unchecked_Expression (N : Node_Id) is | |
6947 | Exp : constant Node_Id := Expression (N); | |
6948 | ||
6949 | begin | |
6950 | Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp)); | |
6951 | Rewrite (N, Exp); | |
6952 | end Expand_N_Unchecked_Expression; | |
6953 | ||
6954 | ---------------------------------------- | |
6955 | -- Expand_N_Unchecked_Type_Conversion -- | |
6956 | ---------------------------------------- | |
6957 | ||
6958 | -- If this cannot be handled by Gigi and we haven't already made | |
6959 | -- a temporary for it, do it now. | |
6960 | ||
6961 | procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is | |
6962 | Target_Type : constant Entity_Id := Etype (N); | |
6963 | Operand : constant Node_Id := Expression (N); | |
6964 | Operand_Type : constant Entity_Id := Etype (Operand); | |
6965 | ||
6966 | begin | |
6967 | -- If we have a conversion of a compile time known value to a target | |
6968 | -- type and the value is in range of the target type, then we can simply | |
6969 | -- replace the construct by an integer literal of the correct type. We | |
6970 | -- only apply this to integer types being converted. Possibly it may | |
6971 | -- apply in other cases, but it is too much trouble to worry about. | |
6972 | ||
6973 | -- Note that we do not do this transformation if the Kill_Range_Check | |
6974 | -- flag is set, since then the value may be outside the expected range. | |
6975 | -- This happens in the Normalize_Scalars case. | |
6976 | ||
6977 | if Is_Integer_Type (Target_Type) | |
6978 | and then Is_Integer_Type (Operand_Type) | |
6979 | and then Compile_Time_Known_Value (Operand) | |
6980 | and then not Kill_Range_Check (N) | |
6981 | then | |
6982 | declare | |
6983 | Val : constant Uint := Expr_Value (Operand); | |
6984 | ||
6985 | begin | |
6986 | if Compile_Time_Known_Value (Type_Low_Bound (Target_Type)) | |
6987 | and then | |
6988 | Compile_Time_Known_Value (Type_High_Bound (Target_Type)) | |
6989 | and then | |
6990 | Val >= Expr_Value (Type_Low_Bound (Target_Type)) | |
6991 | and then | |
6992 | Val <= Expr_Value (Type_High_Bound (Target_Type)) | |
6993 | then | |
6994 | Rewrite (N, Make_Integer_Literal (Sloc (N), Val)); | |
8a36a0cc AC |
6995 | |
6996 | -- If Address is the target type, just set the type | |
6997 | -- to avoid a spurious type error on the literal when | |
6998 | -- Address is a visible integer type. | |
6999 | ||
7000 | if Is_Descendent_Of_Address (Target_Type) then | |
7001 | Set_Etype (N, Target_Type); | |
7002 | else | |
7003 | Analyze_And_Resolve (N, Target_Type); | |
7004 | end if; | |
7005 | ||
70482933 RK |
7006 | return; |
7007 | end if; | |
7008 | end; | |
7009 | end if; | |
7010 | ||
7011 | -- Nothing to do if conversion is safe | |
7012 | ||
7013 | if Safe_Unchecked_Type_Conversion (N) then | |
7014 | return; | |
7015 | end if; | |
7016 | ||
7017 | -- Otherwise force evaluation unless Assignment_OK flag is set (this | |
7018 | -- flag indicates ??? -- more comments needed here) | |
7019 | ||
7020 | if Assignment_OK (N) then | |
7021 | null; | |
7022 | else | |
7023 | Force_Evaluation (N); | |
7024 | end if; | |
7025 | end Expand_N_Unchecked_Type_Conversion; | |
7026 | ||
7027 | ---------------------------- | |
7028 | -- Expand_Record_Equality -- | |
7029 | ---------------------------- | |
7030 | ||
7031 | -- For non-variant records, Equality is expanded when needed into: | |
7032 | ||
7033 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
7034 | -- and then ... | |
7035 | -- and then Lhs.Discrn = Rhs.Discrn | |
7036 | -- and then Lhs.Cmp1 = Rhs.Cmp1 | |
7037 | -- and then ... | |
7038 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
7039 | ||
7040 | -- The expression is folded by the back-end for adjacent fields. This | |
7041 | -- function is called for tagged record in only one occasion: for imple- | |
7042 | -- menting predefined primitive equality (see Predefined_Primitives_Bodies) | |
7043 | -- otherwise the primitive "=" is used directly. | |
7044 | ||
7045 | function Expand_Record_Equality | |
7046 | (Nod : Node_Id; | |
7047 | Typ : Entity_Id; | |
7048 | Lhs : Node_Id; | |
7049 | Rhs : Node_Id; | |
2e071734 | 7050 | Bodies : List_Id) return Node_Id |
70482933 RK |
7051 | is |
7052 | Loc : constant Source_Ptr := Sloc (Nod); | |
7053 | ||
0ab80019 AC |
7054 | Result : Node_Id; |
7055 | C : Entity_Id; | |
7056 | ||
7057 | First_Time : Boolean := True; | |
7058 | ||
70482933 RK |
7059 | function Suitable_Element (C : Entity_Id) return Entity_Id; |
7060 | -- Return the first field to compare beginning with C, skipping the | |
0ab80019 AC |
7061 | -- inherited components. |
7062 | ||
7063 | ---------------------- | |
7064 | -- Suitable_Element -- | |
7065 | ---------------------- | |
70482933 RK |
7066 | |
7067 | function Suitable_Element (C : Entity_Id) return Entity_Id is | |
7068 | begin | |
7069 | if No (C) then | |
7070 | return Empty; | |
7071 | ||
7072 | elsif Ekind (C) /= E_Discriminant | |
7073 | and then Ekind (C) /= E_Component | |
7074 | then | |
7075 | return Suitable_Element (Next_Entity (C)); | |
7076 | ||
7077 | elsif Is_Tagged_Type (Typ) | |
7078 | and then C /= Original_Record_Component (C) | |
7079 | then | |
7080 | return Suitable_Element (Next_Entity (C)); | |
7081 | ||
7082 | elsif Chars (C) = Name_uController | |
7083 | or else Chars (C) = Name_uTag | |
7084 | then | |
7085 | return Suitable_Element (Next_Entity (C)); | |
7086 | ||
7087 | else | |
7088 | return C; | |
7089 | end if; | |
7090 | end Suitable_Element; | |
7091 | ||
70482933 RK |
7092 | -- Start of processing for Expand_Record_Equality |
7093 | ||
7094 | begin | |
70482933 RK |
7095 | -- Generates the following code: (assuming that Typ has one Discr and |
7096 | -- component C2 is also a record) | |
7097 | ||
7098 | -- True | |
7099 | -- and then Lhs.Discr1 = Rhs.Discr1 | |
7100 | -- and then Lhs.C1 = Rhs.C1 | |
7101 | -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn | |
7102 | -- and then ... | |
7103 | -- and then Lhs.Cmpn = Rhs.Cmpn | |
7104 | ||
7105 | Result := New_Reference_To (Standard_True, Loc); | |
7106 | C := Suitable_Element (First_Entity (Typ)); | |
7107 | ||
7108 | while Present (C) loop | |
70482933 RK |
7109 | declare |
7110 | New_Lhs : Node_Id; | |
7111 | New_Rhs : Node_Id; | |
8aceda64 | 7112 | Check : Node_Id; |
70482933 RK |
7113 | |
7114 | begin | |
7115 | if First_Time then | |
7116 | First_Time := False; | |
7117 | New_Lhs := Lhs; | |
7118 | New_Rhs := Rhs; | |
70482933 RK |
7119 | else |
7120 | New_Lhs := New_Copy_Tree (Lhs); | |
7121 | New_Rhs := New_Copy_Tree (Rhs); | |
7122 | end if; | |
7123 | ||
8aceda64 AC |
7124 | Check := |
7125 | Expand_Composite_Equality (Nod, Etype (C), | |
7126 | Lhs => | |
7127 | Make_Selected_Component (Loc, | |
7128 | Prefix => New_Lhs, | |
7129 | Selector_Name => New_Reference_To (C, Loc)), | |
7130 | Rhs => | |
7131 | Make_Selected_Component (Loc, | |
7132 | Prefix => New_Rhs, | |
7133 | Selector_Name => New_Reference_To (C, Loc)), | |
7134 | Bodies => Bodies); | |
7135 | ||
7136 | -- If some (sub)component is an unchecked_union, the whole | |
7137 | -- operation will raise program error. | |
7138 | ||
7139 | if Nkind (Check) = N_Raise_Program_Error then | |
7140 | Result := Check; | |
7141 | Set_Etype (Result, Standard_Boolean); | |
7142 | exit; | |
7143 | else | |
7144 | Result := | |
7145 | Make_And_Then (Loc, | |
7146 | Left_Opnd => Result, | |
7147 | Right_Opnd => Check); | |
7148 | end if; | |
70482933 RK |
7149 | end; |
7150 | ||
7151 | C := Suitable_Element (Next_Entity (C)); | |
7152 | end loop; | |
7153 | ||
7154 | return Result; | |
7155 | end Expand_Record_Equality; | |
7156 | ||
7157 | ------------------------------------- | |
7158 | -- Fixup_Universal_Fixed_Operation -- | |
7159 | ------------------------------------- | |
7160 | ||
7161 | procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is | |
7162 | Conv : constant Node_Id := Parent (N); | |
7163 | ||
7164 | begin | |
7165 | -- We must have a type conversion immediately above us | |
7166 | ||
7167 | pragma Assert (Nkind (Conv) = N_Type_Conversion); | |
7168 | ||
7169 | -- Normally the type conversion gives our target type. The exception | |
7170 | -- occurs in the case of the Round attribute, where the conversion | |
7171 | -- will be to universal real, and our real type comes from the Round | |
7172 | -- attribute (as well as an indication that we must round the result) | |
7173 | ||
7174 | if Nkind (Parent (Conv)) = N_Attribute_Reference | |
7175 | and then Attribute_Name (Parent (Conv)) = Name_Round | |
7176 | then | |
7177 | Set_Etype (N, Etype (Parent (Conv))); | |
7178 | Set_Rounded_Result (N); | |
7179 | ||
7180 | -- Normal case where type comes from conversion above us | |
7181 | ||
7182 | else | |
7183 | Set_Etype (N, Etype (Conv)); | |
7184 | end if; | |
7185 | end Fixup_Universal_Fixed_Operation; | |
7186 | ||
fbf5a39b AC |
7187 | ------------------------------ |
7188 | -- Get_Allocator_Final_List -- | |
7189 | ------------------------------ | |
7190 | ||
7191 | function Get_Allocator_Final_List | |
7192 | (N : Node_Id; | |
7193 | T : Entity_Id; | |
2e071734 | 7194 | PtrT : Entity_Id) return Entity_Id |
fbf5a39b AC |
7195 | is |
7196 | Loc : constant Source_Ptr := Sloc (N); | |
fbf5a39b | 7197 | |
0da2c8ac AC |
7198 | Owner : Entity_Id := PtrT; |
7199 | -- The entity whose finalisation list must be used to attach the | |
7200 | -- allocated object. | |
fbf5a39b | 7201 | |
0da2c8ac | 7202 | begin |
fbf5a39b | 7203 | if Ekind (PtrT) = E_Anonymous_Access_Type then |
0da2c8ac AC |
7204 | if Nkind (Associated_Node_For_Itype (PtrT)) |
7205 | in N_Subprogram_Specification | |
7206 | then | |
7207 | -- If the context is an access parameter, we need to create | |
7208 | -- a non-anonymous access type in order to have a usable | |
7209 | -- final list, because there is otherwise no pool to which | |
7210 | -- the allocated object can belong. We create both the type | |
7211 | -- and the finalization chain here, because freezing an | |
7212 | -- internal type does not create such a chain. The Final_Chain | |
7213 | -- that is thus created is shared by the access parameter. | |
7214 | ||
7215 | Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J')); | |
7216 | Insert_Action (N, | |
7217 | Make_Full_Type_Declaration (Loc, | |
7218 | Defining_Identifier => Owner, | |
7219 | Type_Definition => | |
7220 | Make_Access_To_Object_Definition (Loc, | |
7221 | Subtype_Indication => | |
7222 | New_Occurrence_Of (T, Loc)))); | |
fbf5a39b | 7223 | |
0da2c8ac AC |
7224 | Build_Final_List (N, Owner); |
7225 | Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner)); | |
fbf5a39b | 7226 | |
0da2c8ac AC |
7227 | else |
7228 | -- Case of an access discriminant, or (Ada 2005) of | |
7229 | -- an anonymous access component: find the final list | |
7230 | -- associated with the scope of the type. | |
7231 | ||
7232 | Owner := Scope (PtrT); | |
7233 | end if; | |
fbf5a39b | 7234 | end if; |
0da2c8ac AC |
7235 | |
7236 | return Find_Final_List (Owner); | |
fbf5a39b AC |
7237 | end Get_Allocator_Final_List; |
7238 | ||
5d09245e AC |
7239 | --------------------------------- |
7240 | -- Has_Inferable_Discriminants -- | |
7241 | --------------------------------- | |
7242 | ||
7243 | function Has_Inferable_Discriminants (N : Node_Id) return Boolean is | |
7244 | ||
7245 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean; | |
7246 | -- Determines whether the left-most prefix of a selected component is a | |
7247 | -- formal parameter in a subprogram. Assumes N is a selected component. | |
7248 | ||
7249 | -------------------------------- | |
7250 | -- Prefix_Is_Formal_Parameter -- | |
7251 | -------------------------------- | |
7252 | ||
7253 | function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is | |
7254 | Sel_Comp : Node_Id := N; | |
7255 | ||
7256 | begin | |
7257 | -- Move to the left-most prefix by climbing up the tree | |
7258 | ||
7259 | while Present (Parent (Sel_Comp)) | |
7260 | and then Nkind (Parent (Sel_Comp)) = N_Selected_Component | |
7261 | loop | |
7262 | Sel_Comp := Parent (Sel_Comp); | |
7263 | end loop; | |
7264 | ||
7265 | return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind; | |
7266 | end Prefix_Is_Formal_Parameter; | |
7267 | ||
7268 | -- Start of processing for Has_Inferable_Discriminants | |
7269 | ||
7270 | begin | |
7271 | -- For identifiers and indexed components, it is sufficent to have a | |
7272 | -- constrained Unchecked_Union nominal subtype. | |
7273 | ||
7274 | if Nkind (N) = N_Identifier | |
7275 | or else | |
7276 | Nkind (N) = N_Indexed_Component | |
7277 | then | |
7278 | return Is_Unchecked_Union (Base_Type (Etype (N))) | |
7279 | and then | |
7280 | Is_Constrained (Etype (N)); | |
7281 | ||
7282 | -- For selected components, the subtype of the selector must be a | |
7283 | -- constrained Unchecked_Union. If the component is subject to a | |
7284 | -- per-object constraint, then the enclosing object must have inferable | |
7285 | -- discriminants. | |
7286 | ||
7287 | elsif Nkind (N) = N_Selected_Component then | |
7288 | if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then | |
7289 | ||
7290 | -- A small hack. If we have a per-object constrained selected | |
7291 | -- component of a formal parameter, return True since we do not | |
7292 | -- know the actual parameter association yet. | |
7293 | ||
7294 | if Prefix_Is_Formal_Parameter (N) then | |
7295 | return True; | |
7296 | end if; | |
7297 | ||
7298 | -- Otherwise, check the enclosing object and the selector | |
7299 | ||
7300 | return Has_Inferable_Discriminants (Prefix (N)) | |
7301 | and then | |
7302 | Has_Inferable_Discriminants (Selector_Name (N)); | |
7303 | end if; | |
7304 | ||
7305 | -- The call to Has_Inferable_Discriminants will determine whether | |
7306 | -- the selector has a constrained Unchecked_Union nominal type. | |
7307 | ||
7308 | return Has_Inferable_Discriminants (Selector_Name (N)); | |
7309 | ||
7310 | -- A qualified expression has inferable discriminants if its subtype | |
7311 | -- mark is a constrained Unchecked_Union subtype. | |
7312 | ||
7313 | elsif Nkind (N) = N_Qualified_Expression then | |
7314 | return Is_Unchecked_Union (Subtype_Mark (N)) | |
7315 | and then | |
7316 | Is_Constrained (Subtype_Mark (N)); | |
7317 | ||
7318 | end if; | |
7319 | ||
7320 | return False; | |
7321 | end Has_Inferable_Discriminants; | |
7322 | ||
70482933 RK |
7323 | ------------------------------- |
7324 | -- Insert_Dereference_Action -- | |
7325 | ------------------------------- | |
7326 | ||
7327 | procedure Insert_Dereference_Action (N : Node_Id) is | |
7328 | Loc : constant Source_Ptr := Sloc (N); | |
7329 | Typ : constant Entity_Id := Etype (N); | |
7330 | Pool : constant Entity_Id := Associated_Storage_Pool (Typ); | |
0ab80019 | 7331 | Pnod : constant Node_Id := Parent (N); |
70482933 RK |
7332 | |
7333 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean; | |
2e071734 AC |
7334 | -- Return true if type of P is derived from Checked_Pool; |
7335 | ||
7336 | ----------------------------- | |
7337 | -- Is_Checked_Storage_Pool -- | |
7338 | ----------------------------- | |
70482933 RK |
7339 | |
7340 | function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is | |
7341 | T : Entity_Id; | |
7342 | ||
7343 | begin | |
7344 | if No (P) then | |
7345 | return False; | |
7346 | end if; | |
7347 | ||
7348 | T := Etype (P); | |
7349 | while T /= Etype (T) loop | |
7350 | if Is_RTE (T, RE_Checked_Pool) then | |
7351 | return True; | |
7352 | else | |
7353 | T := Etype (T); | |
7354 | end if; | |
7355 | end loop; | |
7356 | ||
7357 | return False; | |
7358 | end Is_Checked_Storage_Pool; | |
7359 | ||
7360 | -- Start of processing for Insert_Dereference_Action | |
7361 | ||
7362 | begin | |
e6f69614 AC |
7363 | pragma Assert (Nkind (Pnod) = N_Explicit_Dereference); |
7364 | ||
0ab80019 AC |
7365 | if not (Is_Checked_Storage_Pool (Pool) |
7366 | and then Comes_From_Source (Original_Node (Pnod))) | |
e6f69614 | 7367 | then |
70482933 | 7368 | return; |
70482933 RK |
7369 | end if; |
7370 | ||
7371 | Insert_Action (N, | |
7372 | Make_Procedure_Call_Statement (Loc, | |
7373 | Name => New_Reference_To ( | |
7374 | Find_Prim_Op (Etype (Pool), Name_Dereference), Loc), | |
7375 | ||
7376 | Parameter_Associations => New_List ( | |
7377 | ||
7378 | -- Pool | |
7379 | ||
7380 | New_Reference_To (Pool, Loc), | |
7381 | ||
fbf5a39b AC |
7382 | -- Storage_Address. We use the attribute Pool_Address, |
7383 | -- which uses the pointer itself to find the address of | |
7384 | -- the object, and which handles unconstrained arrays | |
7385 | -- properly by computing the address of the template. | |
7386 | -- i.e. the correct address of the corresponding allocation. | |
70482933 RK |
7387 | |
7388 | Make_Attribute_Reference (Loc, | |
fbf5a39b AC |
7389 | Prefix => Duplicate_Subexpr_Move_Checks (N), |
7390 | Attribute_Name => Name_Pool_Address), | |
70482933 RK |
7391 | |
7392 | -- Size_In_Storage_Elements | |
7393 | ||
7394 | Make_Op_Divide (Loc, | |
7395 | Left_Opnd => | |
7396 | Make_Attribute_Reference (Loc, | |
7397 | Prefix => | |
fbf5a39b AC |
7398 | Make_Explicit_Dereference (Loc, |
7399 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
7400 | Attribute_Name => Name_Size), |
7401 | Right_Opnd => | |
7402 | Make_Integer_Literal (Loc, System_Storage_Unit)), | |
7403 | ||
7404 | -- Alignment | |
7405 | ||
7406 | Make_Attribute_Reference (Loc, | |
7407 | Prefix => | |
fbf5a39b AC |
7408 | Make_Explicit_Dereference (Loc, |
7409 | Duplicate_Subexpr_Move_Checks (N)), | |
70482933 RK |
7410 | Attribute_Name => Name_Alignment)))); |
7411 | ||
fbf5a39b AC |
7412 | exception |
7413 | when RE_Not_Available => | |
7414 | return; | |
70482933 RK |
7415 | end Insert_Dereference_Action; |
7416 | ||
7417 | ------------------------------ | |
7418 | -- Make_Array_Comparison_Op -- | |
7419 | ------------------------------ | |
7420 | ||
7421 | -- This is a hand-coded expansion of the following generic function: | |
7422 | ||
7423 | -- generic | |
7424 | -- type elem is (<>); | |
7425 | -- type index is (<>); | |
7426 | -- type a is array (index range <>) of elem; | |
7427 | -- | |
7428 | -- function Gnnn (X : a; Y: a) return boolean is | |
7429 | -- J : index := Y'first; | |
7430 | -- | |
7431 | -- begin | |
7432 | -- if X'length = 0 then | |
7433 | -- return false; | |
7434 | -- | |
7435 | -- elsif Y'length = 0 then | |
7436 | -- return true; | |
7437 | -- | |
7438 | -- else | |
7439 | -- for I in X'range loop | |
7440 | -- if X (I) = Y (J) then | |
7441 | -- if J = Y'last then | |
7442 | -- exit; | |
7443 | -- else | |
7444 | -- J := index'succ (J); | |
7445 | -- end if; | |
7446 | -- | |
7447 | -- else | |
7448 | -- return X (I) > Y (J); | |
7449 | -- end if; | |
7450 | -- end loop; | |
7451 | -- | |
7452 | -- return X'length > Y'length; | |
7453 | -- end if; | |
7454 | -- end Gnnn; | |
7455 | ||
7456 | -- Note that since we are essentially doing this expansion by hand, we | |
7457 | -- do not need to generate an actual or formal generic part, just the | |
7458 | -- instantiated function itself. | |
7459 | ||
7460 | function Make_Array_Comparison_Op | |
2e071734 AC |
7461 | (Typ : Entity_Id; |
7462 | Nod : Node_Id) return Node_Id | |
70482933 RK |
7463 | is |
7464 | Loc : constant Source_Ptr := Sloc (Nod); | |
7465 | ||
7466 | X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX); | |
7467 | Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY); | |
7468 | I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI); | |
7469 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
7470 | ||
7471 | Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ))); | |
7472 | ||
7473 | Loop_Statement : Node_Id; | |
7474 | Loop_Body : Node_Id; | |
7475 | If_Stat : Node_Id; | |
7476 | Inner_If : Node_Id; | |
7477 | Final_Expr : Node_Id; | |
7478 | Func_Body : Node_Id; | |
7479 | Func_Name : Entity_Id; | |
7480 | Formals : List_Id; | |
7481 | Length1 : Node_Id; | |
7482 | Length2 : Node_Id; | |
7483 | ||
7484 | begin | |
7485 | -- if J = Y'last then | |
7486 | -- exit; | |
7487 | -- else | |
7488 | -- J := index'succ (J); | |
7489 | -- end if; | |
7490 | ||
7491 | Inner_If := | |
7492 | Make_Implicit_If_Statement (Nod, | |
7493 | Condition => | |
7494 | Make_Op_Eq (Loc, | |
7495 | Left_Opnd => New_Reference_To (J, Loc), | |
7496 | Right_Opnd => | |
7497 | Make_Attribute_Reference (Loc, | |
7498 | Prefix => New_Reference_To (Y, Loc), | |
7499 | Attribute_Name => Name_Last)), | |
7500 | ||
7501 | Then_Statements => New_List ( | |
7502 | Make_Exit_Statement (Loc)), | |
7503 | ||
7504 | Else_Statements => | |
7505 | New_List ( | |
7506 | Make_Assignment_Statement (Loc, | |
7507 | Name => New_Reference_To (J, Loc), | |
7508 | Expression => | |
7509 | Make_Attribute_Reference (Loc, | |
7510 | Prefix => New_Reference_To (Index, Loc), | |
7511 | Attribute_Name => Name_Succ, | |
7512 | Expressions => New_List (New_Reference_To (J, Loc)))))); | |
7513 | ||
7514 | -- if X (I) = Y (J) then | |
7515 | -- if ... end if; | |
7516 | -- else | |
7517 | -- return X (I) > Y (J); | |
7518 | -- end if; | |
7519 | ||
7520 | Loop_Body := | |
7521 | Make_Implicit_If_Statement (Nod, | |
7522 | Condition => | |
7523 | Make_Op_Eq (Loc, | |
7524 | Left_Opnd => | |
7525 | Make_Indexed_Component (Loc, | |
7526 | Prefix => New_Reference_To (X, Loc), | |
7527 | Expressions => New_List (New_Reference_To (I, Loc))), | |
7528 | ||
7529 | Right_Opnd => | |
7530 | Make_Indexed_Component (Loc, | |
7531 | Prefix => New_Reference_To (Y, Loc), | |
7532 | Expressions => New_List (New_Reference_To (J, Loc)))), | |
7533 | ||
7534 | Then_Statements => New_List (Inner_If), | |
7535 | ||
7536 | Else_Statements => New_List ( | |
7537 | Make_Return_Statement (Loc, | |
7538 | Expression => | |
7539 | Make_Op_Gt (Loc, | |
7540 | Left_Opnd => | |
7541 | Make_Indexed_Component (Loc, | |
7542 | Prefix => New_Reference_To (X, Loc), | |
7543 | Expressions => New_List (New_Reference_To (I, Loc))), | |
7544 | ||
7545 | Right_Opnd => | |
7546 | Make_Indexed_Component (Loc, | |
7547 | Prefix => New_Reference_To (Y, Loc), | |
7548 | Expressions => New_List ( | |
7549 | New_Reference_To (J, Loc))))))); | |
7550 | ||
7551 | -- for I in X'range loop | |
7552 | -- if ... end if; | |
7553 | -- end loop; | |
7554 | ||
7555 | Loop_Statement := | |
7556 | Make_Implicit_Loop_Statement (Nod, | |
7557 | Identifier => Empty, | |
7558 | ||
7559 | Iteration_Scheme => | |
7560 | Make_Iteration_Scheme (Loc, | |
7561 | Loop_Parameter_Specification => | |
7562 | Make_Loop_Parameter_Specification (Loc, | |
7563 | Defining_Identifier => I, | |
7564 | Discrete_Subtype_Definition => | |
7565 | Make_Attribute_Reference (Loc, | |
7566 | Prefix => New_Reference_To (X, Loc), | |
7567 | Attribute_Name => Name_Range))), | |
7568 | ||
7569 | Statements => New_List (Loop_Body)); | |
7570 | ||
7571 | -- if X'length = 0 then | |
7572 | -- return false; | |
7573 | -- elsif Y'length = 0 then | |
7574 | -- return true; | |
7575 | -- else | |
7576 | -- for ... loop ... end loop; | |
7577 | -- return X'length > Y'length; | |
7578 | -- end if; | |
7579 | ||
7580 | Length1 := | |
7581 | Make_Attribute_Reference (Loc, | |
7582 | Prefix => New_Reference_To (X, Loc), | |
7583 | Attribute_Name => Name_Length); | |
7584 | ||
7585 | Length2 := | |
7586 | Make_Attribute_Reference (Loc, | |
7587 | Prefix => New_Reference_To (Y, Loc), | |
7588 | Attribute_Name => Name_Length); | |
7589 | ||
7590 | Final_Expr := | |
7591 | Make_Op_Gt (Loc, | |
7592 | Left_Opnd => Length1, | |
7593 | Right_Opnd => Length2); | |
7594 | ||
7595 | If_Stat := | |
7596 | Make_Implicit_If_Statement (Nod, | |
7597 | Condition => | |
7598 | Make_Op_Eq (Loc, | |
7599 | Left_Opnd => | |
7600 | Make_Attribute_Reference (Loc, | |
7601 | Prefix => New_Reference_To (X, Loc), | |
7602 | Attribute_Name => Name_Length), | |
7603 | Right_Opnd => | |
7604 | Make_Integer_Literal (Loc, 0)), | |
7605 | ||
7606 | Then_Statements => | |
7607 | New_List ( | |
7608 | Make_Return_Statement (Loc, | |
7609 | Expression => New_Reference_To (Standard_False, Loc))), | |
7610 | ||
7611 | Elsif_Parts => New_List ( | |
7612 | Make_Elsif_Part (Loc, | |
7613 | Condition => | |
7614 | Make_Op_Eq (Loc, | |
7615 | Left_Opnd => | |
7616 | Make_Attribute_Reference (Loc, | |
7617 | Prefix => New_Reference_To (Y, Loc), | |
7618 | Attribute_Name => Name_Length), | |
7619 | Right_Opnd => | |
7620 | Make_Integer_Literal (Loc, 0)), | |
7621 | ||
7622 | Then_Statements => | |
7623 | New_List ( | |
7624 | Make_Return_Statement (Loc, | |
7625 | Expression => New_Reference_To (Standard_True, Loc))))), | |
7626 | ||
7627 | Else_Statements => New_List ( | |
7628 | Loop_Statement, | |
7629 | Make_Return_Statement (Loc, | |
7630 | Expression => Final_Expr))); | |
7631 | ||
7632 | -- (X : a; Y: a) | |
7633 | ||
7634 | Formals := New_List ( | |
7635 | Make_Parameter_Specification (Loc, | |
7636 | Defining_Identifier => X, | |
7637 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
7638 | ||
7639 | Make_Parameter_Specification (Loc, | |
7640 | Defining_Identifier => Y, | |
7641 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
7642 | ||
7643 | -- function Gnnn (...) return boolean is | |
7644 | -- J : index := Y'first; | |
7645 | -- begin | |
7646 | -- if ... end if; | |
7647 | -- end Gnnn; | |
7648 | ||
7649 | Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G')); | |
7650 | ||
7651 | Func_Body := | |
7652 | Make_Subprogram_Body (Loc, | |
7653 | Specification => | |
7654 | Make_Function_Specification (Loc, | |
7655 | Defining_Unit_Name => Func_Name, | |
7656 | Parameter_Specifications => Formals, | |
7657 | Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)), | |
7658 | ||
7659 | Declarations => New_List ( | |
7660 | Make_Object_Declaration (Loc, | |
7661 | Defining_Identifier => J, | |
7662 | Object_Definition => New_Reference_To (Index, Loc), | |
7663 | Expression => | |
7664 | Make_Attribute_Reference (Loc, | |
7665 | Prefix => New_Reference_To (Y, Loc), | |
7666 | Attribute_Name => Name_First))), | |
7667 | ||
7668 | Handled_Statement_Sequence => | |
7669 | Make_Handled_Sequence_Of_Statements (Loc, | |
7670 | Statements => New_List (If_Stat))); | |
7671 | ||
7672 | return Func_Body; | |
7673 | ||
7674 | end Make_Array_Comparison_Op; | |
7675 | ||
7676 | --------------------------- | |
7677 | -- Make_Boolean_Array_Op -- | |
7678 | --------------------------- | |
7679 | ||
7680 | -- For logical operations on boolean arrays, expand in line the | |
7681 | -- following, replacing 'and' with 'or' or 'xor' where needed: | |
7682 | ||
7683 | -- function Annn (A : typ; B: typ) return typ is | |
7684 | -- C : typ; | |
7685 | -- begin | |
7686 | -- for J in A'range loop | |
7687 | -- C (J) := A (J) op B (J); | |
7688 | -- end loop; | |
7689 | -- return C; | |
7690 | -- end Annn; | |
7691 | ||
7692 | -- Here typ is the boolean array type | |
7693 | ||
7694 | function Make_Boolean_Array_Op | |
2e071734 AC |
7695 | (Typ : Entity_Id; |
7696 | N : Node_Id) return Node_Id | |
70482933 RK |
7697 | is |
7698 | Loc : constant Source_Ptr := Sloc (N); | |
7699 | ||
7700 | A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA); | |
7701 | B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB); | |
7702 | C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC); | |
7703 | J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ); | |
7704 | ||
7705 | A_J : Node_Id; | |
7706 | B_J : Node_Id; | |
7707 | C_J : Node_Id; | |
7708 | Op : Node_Id; | |
7709 | ||
7710 | Formals : List_Id; | |
7711 | Func_Name : Entity_Id; | |
7712 | Func_Body : Node_Id; | |
7713 | Loop_Statement : Node_Id; | |
7714 | ||
7715 | begin | |
7716 | A_J := | |
7717 | Make_Indexed_Component (Loc, | |
7718 | Prefix => New_Reference_To (A, Loc), | |
7719 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7720 | ||
7721 | B_J := | |
7722 | Make_Indexed_Component (Loc, | |
7723 | Prefix => New_Reference_To (B, Loc), | |
7724 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7725 | ||
7726 | C_J := | |
7727 | Make_Indexed_Component (Loc, | |
7728 | Prefix => New_Reference_To (C, Loc), | |
7729 | Expressions => New_List (New_Reference_To (J, Loc))); | |
7730 | ||
7731 | if Nkind (N) = N_Op_And then | |
7732 | Op := | |
7733 | Make_Op_And (Loc, | |
7734 | Left_Opnd => A_J, | |
7735 | Right_Opnd => B_J); | |
7736 | ||
7737 | elsif Nkind (N) = N_Op_Or then | |
7738 | Op := | |
7739 | Make_Op_Or (Loc, | |
7740 | Left_Opnd => A_J, | |
7741 | Right_Opnd => B_J); | |
7742 | ||
7743 | else | |
7744 | Op := | |
7745 | Make_Op_Xor (Loc, | |
7746 | Left_Opnd => A_J, | |
7747 | Right_Opnd => B_J); | |
7748 | end if; | |
7749 | ||
7750 | Loop_Statement := | |
7751 | Make_Implicit_Loop_Statement (N, | |
7752 | Identifier => Empty, | |
7753 | ||
7754 | Iteration_Scheme => | |
7755 | Make_Iteration_Scheme (Loc, | |
7756 | Loop_Parameter_Specification => | |
7757 | Make_Loop_Parameter_Specification (Loc, | |
7758 | Defining_Identifier => J, | |
7759 | Discrete_Subtype_Definition => | |
7760 | Make_Attribute_Reference (Loc, | |
7761 | Prefix => New_Reference_To (A, Loc), | |
7762 | Attribute_Name => Name_Range))), | |
7763 | ||
7764 | Statements => New_List ( | |
7765 | Make_Assignment_Statement (Loc, | |
7766 | Name => C_J, | |
7767 | Expression => Op))); | |
7768 | ||
7769 | Formals := New_List ( | |
7770 | Make_Parameter_Specification (Loc, | |
7771 | Defining_Identifier => A, | |
7772 | Parameter_Type => New_Reference_To (Typ, Loc)), | |
7773 | ||
7774 | Make_Parameter_Specification (Loc, | |
7775 | Defining_Identifier => B, | |
7776 | Parameter_Type => New_Reference_To (Typ, Loc))); | |
7777 | ||
7778 | Func_Name := | |
7779 | Make_Defining_Identifier (Loc, New_Internal_Name ('A')); | |
7780 | Set_Is_Inlined (Func_Name); | |
7781 | ||
7782 | Func_Body := | |
7783 | Make_Subprogram_Body (Loc, | |
7784 | Specification => | |
7785 | Make_Function_Specification (Loc, | |
7786 | Defining_Unit_Name => Func_Name, | |
7787 | Parameter_Specifications => Formals, | |
7788 | Subtype_Mark => New_Reference_To (Typ, Loc)), | |
7789 | ||
7790 | Declarations => New_List ( | |
7791 | Make_Object_Declaration (Loc, | |
7792 | Defining_Identifier => C, | |
7793 | Object_Definition => New_Reference_To (Typ, Loc))), | |
7794 | ||
7795 | Handled_Statement_Sequence => | |
7796 | Make_Handled_Sequence_Of_Statements (Loc, | |
7797 | Statements => New_List ( | |
7798 | Loop_Statement, | |
7799 | Make_Return_Statement (Loc, | |
7800 | Expression => New_Reference_To (C, Loc))))); | |
7801 | ||
7802 | return Func_Body; | |
7803 | end Make_Boolean_Array_Op; | |
7804 | ||
7805 | ------------------------ | |
7806 | -- Rewrite_Comparison -- | |
7807 | ------------------------ | |
7808 | ||
7809 | procedure Rewrite_Comparison (N : Node_Id) is | |
7810 | Typ : constant Entity_Id := Etype (N); | |
7811 | Op1 : constant Node_Id := Left_Opnd (N); | |
7812 | Op2 : constant Node_Id := Right_Opnd (N); | |
7813 | ||
7814 | Res : constant Compare_Result := Compile_Time_Compare (Op1, Op2); | |
7815 | -- Res indicates if compare outcome can be determined at compile time | |
7816 | ||
7817 | True_Result : Boolean; | |
7818 | False_Result : Boolean; | |
7819 | ||
7820 | begin | |
7821 | case N_Op_Compare (Nkind (N)) is | |
7822 | when N_Op_Eq => | |
7823 | True_Result := Res = EQ; | |
7824 | False_Result := Res = LT or else Res = GT or else Res = NE; | |
7825 | ||
7826 | when N_Op_Ge => | |
7827 | True_Result := Res in Compare_GE; | |
7828 | False_Result := Res = LT; | |
7829 | ||
7830 | when N_Op_Gt => | |
7831 | True_Result := Res = GT; | |
7832 | False_Result := Res in Compare_LE; | |
7833 | ||
7834 | when N_Op_Lt => | |
7835 | True_Result := Res = LT; | |
7836 | False_Result := Res in Compare_GE; | |
7837 | ||
7838 | when N_Op_Le => | |
7839 | True_Result := Res in Compare_LE; | |
7840 | False_Result := Res = GT; | |
7841 | ||
7842 | when N_Op_Ne => | |
7843 | True_Result := Res = NE; | |
7844 | False_Result := Res = LT or else Res = GT or else Res = EQ; | |
7845 | end case; | |
7846 | ||
7847 | if True_Result then | |
7848 | Rewrite (N, | |
7849 | Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)))); | |
7850 | Analyze_And_Resolve (N, Typ); | |
07fc65c4 | 7851 | Warn_On_Known_Condition (N); |
70482933 RK |
7852 | |
7853 | elsif False_Result then | |
7854 | Rewrite (N, | |
7855 | Convert_To (Typ, New_Occurrence_Of (Standard_False, Sloc (N)))); | |
7856 | Analyze_And_Resolve (N, Typ); | |
07fc65c4 | 7857 | Warn_On_Known_Condition (N); |
70482933 RK |
7858 | end if; |
7859 | end Rewrite_Comparison; | |
7860 | ||
fbf5a39b AC |
7861 | ---------------------------- |
7862 | -- Safe_In_Place_Array_Op -- | |
7863 | ---------------------------- | |
7864 | ||
7865 | function Safe_In_Place_Array_Op | |
2e071734 AC |
7866 | (Lhs : Node_Id; |
7867 | Op1 : Node_Id; | |
7868 | Op2 : Node_Id) return Boolean | |
fbf5a39b AC |
7869 | is |
7870 | Target : Entity_Id; | |
7871 | ||
7872 | function Is_Safe_Operand (Op : Node_Id) return Boolean; | |
7873 | -- Operand is safe if it cannot overlap part of the target of the | |
7874 | -- operation. If the operand and the target are identical, the operand | |
7875 | -- is safe. The operand can be empty in the case of negation. | |
7876 | ||
7877 | function Is_Unaliased (N : Node_Id) return Boolean; | |
5e1c00fa | 7878 | -- Check that N is a stand-alone entity |
fbf5a39b AC |
7879 | |
7880 | ------------------ | |
7881 | -- Is_Unaliased -- | |
7882 | ------------------ | |
7883 | ||
7884 | function Is_Unaliased (N : Node_Id) return Boolean is | |
7885 | begin | |
7886 | return | |
7887 | Is_Entity_Name (N) | |
7888 | and then No (Address_Clause (Entity (N))) | |
7889 | and then No (Renamed_Object (Entity (N))); | |
7890 | end Is_Unaliased; | |
7891 | ||
7892 | --------------------- | |
7893 | -- Is_Safe_Operand -- | |
7894 | --------------------- | |
7895 | ||
7896 | function Is_Safe_Operand (Op : Node_Id) return Boolean is | |
7897 | begin | |
7898 | if No (Op) then | |
7899 | return True; | |
7900 | ||
7901 | elsif Is_Entity_Name (Op) then | |
7902 | return Is_Unaliased (Op); | |
7903 | ||
7904 | elsif Nkind (Op) = N_Indexed_Component | |
7905 | or else Nkind (Op) = N_Selected_Component | |
7906 | then | |
7907 | return Is_Unaliased (Prefix (Op)); | |
7908 | ||
7909 | elsif Nkind (Op) = N_Slice then | |
7910 | return | |
7911 | Is_Unaliased (Prefix (Op)) | |
7912 | and then Entity (Prefix (Op)) /= Target; | |
7913 | ||
7914 | elsif Nkind (Op) = N_Op_Not then | |
7915 | return Is_Safe_Operand (Right_Opnd (Op)); | |
7916 | ||
7917 | else | |
7918 | return False; | |
7919 | end if; | |
7920 | end Is_Safe_Operand; | |
7921 | ||
7922 | -- Start of processing for Is_Safe_In_Place_Array_Op | |
7923 | ||
7924 | begin | |
7925 | -- We skip this processing if the component size is not the | |
7926 | -- same as a system storage unit (since at least for NOT | |
7927 | -- this would cause problems). | |
7928 | ||
7929 | if Component_Size (Etype (Lhs)) /= System_Storage_Unit then | |
7930 | return False; | |
7931 | ||
7932 | -- Cannot do in place stuff on Java_VM since cannot pass addresses | |
7933 | ||
7934 | elsif Java_VM then | |
7935 | return False; | |
7936 | ||
7937 | -- Cannot do in place stuff if non-standard Boolean representation | |
7938 | ||
7939 | elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then | |
7940 | return False; | |
7941 | ||
7942 | elsif not Is_Unaliased (Lhs) then | |
7943 | return False; | |
7944 | else | |
7945 | Target := Entity (Lhs); | |
7946 | ||
7947 | return | |
7948 | Is_Safe_Operand (Op1) | |
7949 | and then Is_Safe_Operand (Op2); | |
7950 | end if; | |
7951 | end Safe_In_Place_Array_Op; | |
7952 | ||
70482933 RK |
7953 | ----------------------- |
7954 | -- Tagged_Membership -- | |
7955 | ----------------------- | |
7956 | ||
7957 | -- There are two different cases to consider depending on whether | |
7958 | -- the right operand is a class-wide type or not. If not we just | |
7959 | -- compare the actual tag of the left expr to the target type tag: | |
7960 | -- | |
7961 | -- Left_Expr.Tag = Right_Type'Tag; | |
7962 | -- | |
7963 | -- If it is a class-wide type we use the RT function CW_Membership which | |
7964 | -- is usually implemented by looking in the ancestor tables contained in | |
7965 | -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag | |
7966 | ||
7967 | function Tagged_Membership (N : Node_Id) return Node_Id is | |
7968 | Left : constant Node_Id := Left_Opnd (N); | |
7969 | Right : constant Node_Id := Right_Opnd (N); | |
7970 | Loc : constant Source_Ptr := Sloc (N); | |
7971 | ||
7972 | Left_Type : Entity_Id; | |
7973 | Right_Type : Entity_Id; | |
7974 | Obj_Tag : Node_Id; | |
7975 | ||
7976 | begin | |
7977 | Left_Type := Etype (Left); | |
7978 | Right_Type := Etype (Right); | |
7979 | ||
7980 | if Is_Class_Wide_Type (Left_Type) then | |
7981 | Left_Type := Root_Type (Left_Type); | |
7982 | end if; | |
7983 | ||
7984 | Obj_Tag := | |
7985 | Make_Selected_Component (Loc, | |
7986 | Prefix => Relocate_Node (Left), | |
7987 | Selector_Name => New_Reference_To (Tag_Component (Left_Type), Loc)); | |
7988 | ||
7989 | if Is_Class_Wide_Type (Right_Type) then | |
7990 | return | |
7991 | Make_DT_Access_Action (Left_Type, | |
7992 | Action => CW_Membership, | |
7993 | Args => New_List ( | |
7994 | Obj_Tag, | |
7995 | New_Reference_To ( | |
7996 | Access_Disp_Table (Root_Type (Right_Type)), Loc))); | |
7997 | else | |
7998 | return | |
7999 | Make_Op_Eq (Loc, | |
8000 | Left_Opnd => Obj_Tag, | |
8001 | Right_Opnd => | |
8002 | New_Reference_To (Access_Disp_Table (Right_Type), Loc)); | |
8003 | end if; | |
8004 | ||
8005 | end Tagged_Membership; | |
8006 | ||
8007 | ------------------------------ | |
8008 | -- Unary_Op_Validity_Checks -- | |
8009 | ------------------------------ | |
8010 | ||
8011 | procedure Unary_Op_Validity_Checks (N : Node_Id) is | |
8012 | begin | |
8013 | if Validity_Checks_On and Validity_Check_Operands then | |
8014 | Ensure_Valid (Right_Opnd (N)); | |
8015 | end if; | |
8016 | end Unary_Op_Validity_Checks; | |
8017 | ||
8018 | end Exp_Ch4; |