]>
Commit | Line | Data |
---|---|---|
70482933 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ A T T R -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
4b490c1e | 9 | -- Copyright (C) 1992-2020, 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- -- | |
748086b7 | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
70482933 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
9eea4346 GB |
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 COPYING3. If not, go to -- | |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
70482933 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
70482933 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
8575023c | 26 | with Aspects; use Aspects; |
70482933 RK |
27 | with Atree; use Atree; |
28 | with Checks; use Checks; | |
29 | with Einfo; use Einfo; | |
5d09245e | 30 | with Elists; use Elists; |
0669bebe | 31 | with Exp_Atag; use Exp_Atag; |
70482933 | 32 | with Exp_Ch2; use Exp_Ch2; |
21d27997 RD |
33 | with Exp_Ch3; use Exp_Ch3; |
34 | with Exp_Ch6; use Exp_Ch6; | |
70482933 | 35 | with Exp_Ch9; use Exp_Ch9; |
54838d1f | 36 | with Exp_Dist; use Exp_Dist; |
70482933 RK |
37 | with Exp_Imgv; use Exp_Imgv; |
38 | with Exp_Pakd; use Exp_Pakd; | |
39 | with Exp_Strm; use Exp_Strm; | |
110d0820 | 40 | with Exp_Put_Image; |
70482933 RK |
41 | with Exp_Tss; use Exp_Tss; |
42 | with Exp_Util; use Exp_Util; | |
8113b0c7 | 43 | with Expander; use Expander; |
16f67b79 | 44 | with Freeze; use Freeze; |
70482933 | 45 | with Gnatvsn; use Gnatvsn; |
16f67b79 | 46 | with Itypes; use Itypes; |
70482933 RK |
47 | with Lib; use Lib; |
48 | with Namet; use Namet; | |
49 | with Nmake; use Nmake; | |
50 | with Nlists; use Nlists; | |
51 | with Opt; use Opt; | |
52 | with Restrict; use Restrict; | |
6e937c1c | 53 | with Rident; use Rident; |
70482933 RK |
54 | with Rtsfind; use Rtsfind; |
55 | with Sem; use Sem; | |
a4100e55 | 56 | with Sem_Aux; use Sem_Aux; |
e10dab7f | 57 | with Sem_Ch6; use Sem_Ch6; |
70482933 RK |
58 | with Sem_Ch7; use Sem_Ch7; |
59 | with Sem_Ch8; use Sem_Ch8; | |
70482933 RK |
60 | with Sem_Eval; use Sem_Eval; |
61 | with Sem_Res; use Sem_Res; | |
62 | with Sem_Util; use Sem_Util; | |
63 | with Sinfo; use Sinfo; | |
64 | with Snames; use Snames; | |
65 | with Stand; use Stand; | |
66 | with Stringt; use Stringt; | |
67 | with Tbuild; use Tbuild; | |
68 | with Ttypes; use Ttypes; | |
69 | with Uintp; use Uintp; | |
70 | with Uname; use Uname; | |
71 | with Validsw; use Validsw; | |
72 | ||
73 | package body Exp_Attr is | |
74 | ||
75 | ----------------------- | |
76 | -- Local Subprograms -- | |
77 | ----------------------- | |
78 | ||
99fc068e | 79 | function Build_Array_VS_Func |
f16cb8df HK |
80 | (Attr : Node_Id; |
81 | Formal_Typ : Entity_Id; | |
a4dafb8b | 82 | Array_Typ : Entity_Id) return Entity_Id; |
f16cb8df HK |
83 | -- Validate the components of an array type by means of a function. Return |
84 | -- the entity of the validation function. The parameters are as follows: | |
85 | -- | |
86 | -- * Attr - the 'Valid_Scalars attribute for which the function is | |
87 | -- generated. | |
88 | -- | |
89 | -- * Formal_Typ - the type of the generated function's only formal | |
90 | -- parameter. | |
91 | -- | |
92 | -- * Array_Typ - the array type whose components are to be validated | |
99fc068e | 93 | |
99bba92c AC |
94 | function Build_Disp_Get_Task_Id_Call (Actual : Node_Id) return Node_Id; |
95 | -- Build a call to Disp_Get_Task_Id, passing Actual as actual parameter | |
96 | ||
45ec05e1 | 97 | function Build_Record_VS_Func |
f16cb8df HK |
98 | (Attr : Node_Id; |
99 | Formal_Typ : Entity_Id; | |
100 | Rec_Typ : Entity_Id) return Entity_Id; | |
101 | -- Validate the components, discriminants, and variants of a record type by | |
102 | -- means of a function. Return the entity of the validation function. The | |
103 | -- parameters are as follows: | |
104 | -- | |
105 | -- * Attr - the 'Valid_Scalars attribute for which the function is | |
106 | -- generated. | |
107 | -- | |
108 | -- * Formal_Typ - the type of the generated function's only formal | |
109 | -- parameter. | |
110 | -- | |
111 | -- * Rec_Typ - the record type whose internals are to be validated | |
45ec05e1 | 112 | |
70482933 RK |
113 | procedure Compile_Stream_Body_In_Scope |
114 | (N : Node_Id; | |
115 | Decl : Node_Id; | |
116 | Arr : Entity_Id; | |
117 | Check : Boolean); | |
118 | -- The body for a stream subprogram may be generated outside of the scope | |
119 | -- of the type. If the type is fully private, it may depend on the full | |
3b42c566 | 120 | -- view of other types (e.g. indexes) that are currently private as well. |
70482933 RK |
121 | -- We install the declarations of the package in which the type is declared |
122 | -- before compiling the body in what is its proper environment. The Check | |
123 | -- parameter indicates if checks are to be suppressed for the stream body. | |
124 | -- We suppress checks for array/record reads, since the rule is that these | |
125 | -- are like assignments, out of range values due to uninitialized storage, | |
126 | -- or other invalid values do NOT cause a Constraint_Error to be raised. | |
3dddb11e ES |
127 | -- If we are within an instance body all visibility has been established |
128 | -- already and there is no need to install the package. | |
70482933 | 129 | |
73f05f9f ES |
130 | -- This mechanism is now extended to the component types of the array type, |
131 | -- when the component type is not in scope and is private, to handle | |
132 | -- properly the case when the full view has defaulted discriminants. | |
133 | ||
134 | -- This special processing is ultimately caused by the fact that the | |
135 | -- compiler lacks a well-defined phase when full views are visible | |
136 | -- everywhere. Having such a separate pass would remove much of the | |
137 | -- special-case code that shuffles partial and full views in the middle | |
138 | -- of semantic analysis and expansion. | |
139 | ||
7ce611e2 ES |
140 | procedure Expand_Access_To_Protected_Op |
141 | (N : Node_Id; | |
142 | Pref : Node_Id; | |
143 | Typ : Entity_Id); | |
7ce611e2 ES |
144 | -- An attribute reference to a protected subprogram is transformed into |
145 | -- a pair of pointers: one to the object, and one to the operations. | |
146 | -- This expansion is performed for 'Access and for 'Unrestricted_Access. | |
147 | ||
70482933 | 148 | procedure Expand_Fpt_Attribute |
fbf5a39b | 149 | (N : Node_Id; |
65f01153 | 150 | Pkg : RE_Id; |
fbf5a39b | 151 | Nam : Name_Id; |
70482933 RK |
152 | Args : List_Id); |
153 | -- This procedure expands a call to a floating-point attribute function. | |
154 | -- N is the attribute reference node, and Args is a list of arguments to | |
65f01153 RD |
155 | -- be passed to the function call. Pkg identifies the package containing |
156 | -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args | |
157 | -- have already been converted to the floating-point type for which Pkg was | |
158 | -- instantiated. The Nam argument is the relevant attribute processing | |
159 | -- routine to be called. This is the same as the attribute name, except in | |
160 | -- the Unaligned_Valid case. | |
70482933 RK |
161 | |
162 | procedure Expand_Fpt_Attribute_R (N : Node_Id); | |
163 | -- This procedure expands a call to a floating-point attribute function | |
fbf5a39b AC |
164 | -- that takes a single floating-point argument. The function to be called |
165 | -- is always the same as the attribute name. | |
70482933 RK |
166 | |
167 | procedure Expand_Fpt_Attribute_RI (N : Node_Id); | |
168 | -- This procedure expands a call to a floating-point attribute function | |
fbf5a39b AC |
169 | -- that takes one floating-point argument and one integer argument. The |
170 | -- function to be called is always the same as the attribute name. | |
70482933 RK |
171 | |
172 | procedure Expand_Fpt_Attribute_RR (N : Node_Id); | |
173 | -- This procedure expands a call to a floating-point attribute function | |
fbf5a39b AC |
174 | -- that takes two floating-point arguments. The function to be called |
175 | -- is always the same as the attribute name. | |
70482933 | 176 | |
aa9b151a | 177 | procedure Expand_Loop_Entry_Attribute (N : Node_Id); |
d436b30d AC |
178 | -- Handle the expansion of attribute 'Loop_Entry. As a result, the related |
179 | -- loop may be converted into a conditional block. See body for details. | |
180 | ||
e0f63680 AC |
181 | procedure Expand_Min_Max_Attribute (N : Node_Id); |
182 | -- Handle the expansion of attributes 'Max and 'Min, including expanding | |
183 | -- then out if we are in Modify_Tree_For_C mode. | |
184 | ||
aa9b151a | 185 | procedure Expand_Pred_Succ_Attribute (N : Node_Id); |
70482933 RK |
186 | -- Handles expansion of Pred or Succ attributes for case of non-real |
187 | -- operand with overflow checking required. | |
188 | ||
18a2ad5d AC |
189 | procedure Expand_Update_Attribute (N : Node_Id); |
190 | -- Handle the expansion of attribute Update | |
191 | ||
65f01153 RD |
192 | procedure Find_Fat_Info |
193 | (T : Entity_Id; | |
194 | Fat_Type : out Entity_Id; | |
195 | Fat_Pkg : out RE_Id); | |
196 | -- Given a floating-point type T, identifies the package containing the | |
197 | -- attributes for this type (returned in Fat_Pkg), and the corresponding | |
198 | -- type for which this package was instantiated from Fat_Gen. Error if T | |
199 | -- is not a floating-point type. | |
200 | ||
fbf5a39b AC |
201 | function Find_Stream_Subprogram |
202 | (Typ : Entity_Id; | |
203 | Nam : TSS_Name_Type) return Entity_Id; | |
204 | -- Returns the stream-oriented subprogram attribute for Typ. For tagged | |
205 | -- types, the corresponding primitive operation is looked up, else the | |
206 | -- appropriate TSS from the type itself, or from its closest ancestor | |
207 | -- defining it, is returned. In both cases, inheritance of representation | |
208 | -- aspects is thus taken into account. | |
70482933 | 209 | |
96d2756f AC |
210 | function Full_Base (T : Entity_Id) return Entity_Id; |
211 | -- The stream functions need to examine the underlying representation of | |
212 | -- composite types. In some cases T may be non-private but its base type | |
213 | -- is, in which case the function returns the corresponding full view. | |
214 | ||
1d571f3b AC |
215 | function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id; |
216 | -- Given a type, find a corresponding stream convert pragma that applies to | |
217 | -- the implementation base type of this type (Typ). If found, return the | |
218 | -- pragma node, otherwise return Empty if no pragma is found. | |
219 | ||
70482933 RK |
220 | function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean; |
221 | -- Utility for array attributes, returns true on packed constrained | |
222 | -- arrays, and on access to same. | |
223 | ||
0669bebe GB |
224 | function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean; |
225 | -- Returns true iff the given node refers to an attribute call that | |
226 | -- can be expanded directly by the back end and does not need front end | |
227 | -- expansion. Typically used for rounding and truncation attributes that | |
228 | -- appear directly inside a conversion to integer. | |
229 | ||
99fc068e RD |
230 | ------------------------- |
231 | -- Build_Array_VS_Func -- | |
232 | ------------------------- | |
233 | ||
234 | function Build_Array_VS_Func | |
f16cb8df HK |
235 | (Attr : Node_Id; |
236 | Formal_Typ : Entity_Id; | |
a4dafb8b | 237 | Array_Typ : Entity_Id) return Entity_Id |
99fc068e | 238 | is |
a4dafb8b PT |
239 | Loc : constant Source_Ptr := Sloc (Attr); |
240 | Comp_Typ : constant Entity_Id := | |
241 | Validated_View (Component_Type (Array_Typ)); | |
f16cb8df HK |
242 | |
243 | function Validate_Component | |
244 | (Obj_Id : Entity_Id; | |
245 | Indexes : List_Id) return Node_Id; | |
246 | -- Process a single component denoted by indexes Indexes. Obj_Id denotes | |
247 | -- the entity of the validation parameter. Return the check associated | |
248 | -- with the component. | |
249 | ||
250 | function Validate_Dimension | |
251 | (Obj_Id : Entity_Id; | |
252 | Dim : Int; | |
253 | Indexes : List_Id) return Node_Id; | |
254 | -- Process dimension Dim of the array type. Obj_Id denotes the entity | |
255 | -- of the validation parameter. Indexes is a list where each dimension | |
256 | -- deposits its loop variable, which will later identify a component. | |
257 | -- Return the loop associated with the current dimension. | |
99fc068e | 258 | |
f16cb8df HK |
259 | ------------------------ |
260 | -- Validate_Component -- | |
261 | ------------------------ | |
99fc068e | 262 | |
f16cb8df HK |
263 | function Validate_Component |
264 | (Obj_Id : Entity_Id; | |
265 | Indexes : List_Id) return Node_Id | |
266 | is | |
267 | Attr_Nam : Name_Id; | |
99fc068e RD |
268 | |
269 | begin | |
f16cb8df HK |
270 | if Is_Scalar_Type (Comp_Typ) then |
271 | Attr_Nam := Name_Valid; | |
99fc068e | 272 | else |
f16cb8df | 273 | Attr_Nam := Name_Valid_Scalars; |
99fc068e RD |
274 | end if; |
275 | ||
f16cb8df HK |
276 | -- Generate: |
277 | -- if not Array_Typ (Obj_Id) (Indexes)'Valid[_Scalars] then | |
278 | -- return False; | |
279 | -- end if; | |
280 | ||
281 | return | |
99fc068e RD |
282 | Make_If_Statement (Loc, |
283 | Condition => | |
284 | Make_Op_Not (Loc, | |
285 | Right_Opnd => | |
286 | Make_Attribute_Reference (Loc, | |
f16cb8df HK |
287 | Prefix => |
288 | Make_Indexed_Component (Loc, | |
289 | Prefix => | |
290 | Unchecked_Convert_To (Array_Typ, | |
291 | New_Occurrence_Of (Obj_Id, Loc)), | |
292 | Expressions => Indexes), | |
293 | Attribute_Name => Attr_Nam)), | |
294 | ||
99fc068e RD |
295 | Then_Statements => New_List ( |
296 | Make_Simple_Return_Statement (Loc, | |
f16cb8df HK |
297 | Expression => New_Occurrence_Of (Standard_False, Loc)))); |
298 | end Validate_Component; | |
99fc068e RD |
299 | |
300 | ------------------------ | |
f16cb8df | 301 | -- Validate_Dimension -- |
99fc068e RD |
302 | ------------------------ |
303 | ||
f16cb8df HK |
304 | function Validate_Dimension |
305 | (Obj_Id : Entity_Id; | |
306 | Dim : Int; | |
307 | Indexes : List_Id) return Node_Id | |
308 | is | |
99fc068e RD |
309 | Index : Entity_Id; |
310 | ||
311 | begin | |
f16cb8df HK |
312 | -- Validate the component once all dimensions have produced their |
313 | -- individual loops. | |
99fc068e | 314 | |
f16cb8df HK |
315 | if Dim > Number_Dimensions (Array_Typ) then |
316 | return Validate_Component (Obj_Id, Indexes); | |
99fc068e | 317 | |
f16cb8df | 318 | -- Process the current dimension |
99fc068e RD |
319 | |
320 | else | |
321 | Index := | |
f16cb8df | 322 | Make_Defining_Identifier (Loc, New_External_Name ('J', Dim)); |
99fc068e | 323 | |
f16cb8df | 324 | Append_To (Indexes, New_Occurrence_Of (Index, Loc)); |
99fc068e | 325 | |
f16cb8df HK |
326 | -- Generate: |
327 | -- for J1 in Array_Typ (Obj_Id)'Range (1) loop | |
328 | -- for JN in Array_Typ (Obj_Id)'Range (N) loop | |
329 | -- if not Array_Typ (Obj_Id) (Indexes)'Valid[_Scalars] | |
330 | -- then | |
331 | -- return False; | |
332 | -- end if; | |
333 | -- end loop; | |
334 | -- end loop; | |
335 | ||
336 | return | |
337 | Make_Implicit_Loop_Statement (Attr, | |
338 | Identifier => Empty, | |
99fc068e RD |
339 | Iteration_Scheme => |
340 | Make_Iteration_Scheme (Loc, | |
341 | Loop_Parameter_Specification => | |
342 | Make_Loop_Parameter_Specification (Loc, | |
f16cb8df | 343 | Defining_Identifier => Index, |
99fc068e RD |
344 | Discrete_Subtype_Definition => |
345 | Make_Attribute_Reference (Loc, | |
f16cb8df HK |
346 | Prefix => |
347 | Unchecked_Convert_To (Array_Typ, | |
348 | New_Occurrence_Of (Obj_Id, Loc)), | |
99fc068e RD |
349 | Attribute_Name => Name_Range, |
350 | Expressions => New_List ( | |
f16cb8df HK |
351 | Make_Integer_Literal (Loc, Dim))))), |
352 | Statements => New_List ( | |
353 | Validate_Dimension (Obj_Id, Dim + 1, Indexes))); | |
99fc068e | 354 | end if; |
f16cb8df HK |
355 | end Validate_Dimension; |
356 | ||
357 | -- Local variables | |
358 | ||
359 | Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V'); | |
360 | Indexes : constant List_Id := New_List; | |
361 | Obj_Id : constant Entity_Id := Make_Temporary (Loc, 'A'); | |
362 | Stmts : List_Id; | |
99fc068e RD |
363 | |
364 | -- Start of processing for Build_Array_VS_Func | |
365 | ||
366 | begin | |
f16cb8df | 367 | Stmts := New_List (Validate_Dimension (Obj_Id, 1, Indexes)); |
99fc068e | 368 | |
f16cb8df HK |
369 | -- Generate: |
370 | -- return True; | |
99fc068e | 371 | |
f16cb8df HK |
372 | Append_To (Stmts, |
373 | Make_Simple_Return_Statement (Loc, | |
374 | Expression => New_Occurrence_Of (Standard_True, Loc))); | |
99fc068e | 375 | |
f16cb8df HK |
376 | -- Generate: |
377 | -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is | |
378 | -- begin | |
379 | -- Stmts | |
380 | -- end Func_Id; | |
99fc068e RD |
381 | |
382 | Set_Ekind (Func_Id, E_Function); | |
383 | Set_Is_Internal (Func_Id); | |
f16cb8df HK |
384 | Set_Is_Pure (Func_Id); |
385 | ||
386 | if not Debug_Generated_Code then | |
387 | Set_Debug_Info_Off (Func_Id); | |
388 | end if; | |
99fc068e | 389 | |
f16cb8df | 390 | Insert_Action (Attr, |
99fc068e RD |
391 | Make_Subprogram_Body (Loc, |
392 | Specification => | |
393 | Make_Function_Specification (Loc, | |
394 | Defining_Unit_Name => Func_Id, | |
f16cb8df HK |
395 | Parameter_Specifications => New_List ( |
396 | Make_Parameter_Specification (Loc, | |
397 | Defining_Identifier => Obj_Id, | |
398 | In_Present => True, | |
399 | Out_Present => False, | |
400 | Parameter_Type => New_Occurrence_Of (Formal_Typ, Loc))), | |
401 | Result_Definition => | |
402 | New_Occurrence_Of (Standard_Boolean, Loc)), | |
99fc068e RD |
403 | Declarations => New_List, |
404 | Handled_Statement_Sequence => | |
405 | Make_Handled_Sequence_Of_Statements (Loc, | |
f16cb8df | 406 | Statements => Stmts))); |
99fc068e | 407 | |
99fc068e RD |
408 | return Func_Id; |
409 | end Build_Array_VS_Func; | |
410 | ||
99bba92c AC |
411 | --------------------------------- |
412 | -- Build_Disp_Get_Task_Id_Call -- | |
413 | --------------------------------- | |
414 | ||
415 | function Build_Disp_Get_Task_Id_Call (Actual : Node_Id) return Node_Id is | |
c0e938d0 AC |
416 | Loc : constant Source_Ptr := Sloc (Actual); |
417 | Typ : constant Entity_Id := Etype (Actual); | |
418 | Subp : constant Entity_Id := Find_Prim_Op (Typ, Name_uDisp_Get_Task_Id); | |
419 | ||
99bba92c | 420 | begin |
c0e938d0 AC |
421 | -- Generate: |
422 | -- _Disp_Get_Task_Id (Actual) | |
423 | ||
424 | return | |
425 | Make_Function_Call (Loc, | |
426 | Name => New_Occurrence_Of (Subp, Loc), | |
427 | Parameter_Associations => New_List (Actual)); | |
99bba92c AC |
428 | end Build_Disp_Get_Task_Id_Call; |
429 | ||
45ec05e1 RD |
430 | -------------------------- |
431 | -- Build_Record_VS_Func -- | |
432 | -------------------------- | |
433 | ||
45ec05e1 | 434 | function Build_Record_VS_Func |
f16cb8df HK |
435 | (Attr : Node_Id; |
436 | Formal_Typ : Entity_Id; | |
437 | Rec_Typ : Entity_Id) return Entity_Id | |
45ec05e1 | 438 | is |
f16cb8df HK |
439 | -- NOTE: The logic of Build_Record_VS_Func is intentionally passive. |
440 | -- It generates code only when there are components, discriminants, | |
441 | -- or variant parts to validate. | |
442 | ||
443 | -- NOTE: The routines within Build_Record_VS_Func are intentionally | |
444 | -- unnested to avoid deep indentation of code. | |
445 | ||
446 | Loc : constant Source_Ptr := Sloc (Attr); | |
447 | ||
448 | procedure Validate_Component_List | |
449 | (Obj_Id : Entity_Id; | |
450 | Comp_List : Node_Id; | |
451 | Stmts : in out List_Id); | |
452 | -- Process all components and variant parts of component list Comp_List. | |
453 | -- Obj_Id denotes the entity of the validation parameter. All new code | |
454 | -- is added to list Stmts. | |
455 | ||
456 | procedure Validate_Field | |
457 | (Obj_Id : Entity_Id; | |
458 | Field : Node_Id; | |
459 | Cond : in out Node_Id); | |
460 | -- Process component declaration or discriminant specification Field. | |
461 | -- Obj_Id denotes the entity of the validation parameter. Cond denotes | |
462 | -- an "or else" conditional expression which contains the new code (if | |
463 | -- any). | |
464 | ||
465 | procedure Validate_Fields | |
466 | (Obj_Id : Entity_Id; | |
467 | Fields : List_Id; | |
468 | Stmts : in out List_Id); | |
469 | -- Process component declarations or discriminant specifications in list | |
470 | -- Fields. Obj_Id denotes the entity of the validation parameter. All | |
471 | -- new code is added to list Stmts. | |
472 | ||
473 | procedure Validate_Variant | |
474 | (Obj_Id : Entity_Id; | |
475 | Var : Node_Id; | |
476 | Alts : in out List_Id); | |
477 | -- Process variant Var. Obj_Id denotes the entity of the validation | |
478 | -- parameter. Alts denotes a list of case statement alternatives which | |
479 | -- contains the new code (if any). | |
480 | ||
481 | procedure Validate_Variant_Part | |
482 | (Obj_Id : Entity_Id; | |
483 | Var_Part : Node_Id; | |
484 | Stmts : in out List_Id); | |
485 | -- Process variant part Var_Part. Obj_Id denotes the entity of the | |
486 | -- validation parameter. All new code is added to list Stmts. | |
45ec05e1 | 487 | |
f16cb8df HK |
488 | ----------------------------- |
489 | -- Validate_Component_List -- | |
490 | ----------------------------- | |
45ec05e1 | 491 | |
f16cb8df HK |
492 | procedure Validate_Component_List |
493 | (Obj_Id : Entity_Id; | |
494 | Comp_List : Node_Id; | |
495 | Stmts : in out List_Id) | |
496 | is | |
497 | Var_Part : constant Node_Id := Variant_Part (Comp_List); | |
45ec05e1 | 498 | |
f16cb8df HK |
499 | begin |
500 | -- Validate all components | |
501 | ||
502 | Validate_Fields | |
503 | (Obj_Id => Obj_Id, | |
504 | Fields => Component_Items (Comp_List), | |
505 | Stmts => Stmts); | |
506 | ||
507 | -- Validate the variant part | |
508 | ||
509 | if Present (Var_Part) then | |
510 | Validate_Variant_Part | |
511 | (Obj_Id => Obj_Id, | |
512 | Var_Part => Var_Part, | |
513 | Stmts => Stmts); | |
514 | end if; | |
515 | end Validate_Component_List; | |
45ec05e1 | 516 | |
f16cb8df HK |
517 | -------------------- |
518 | -- Validate_Field -- | |
519 | -------------------- | |
520 | ||
521 | procedure Validate_Field | |
522 | (Obj_Id : Entity_Id; | |
523 | Field : Node_Id; | |
524 | Cond : in out Node_Id) | |
45ec05e1 | 525 | is |
f16cb8df HK |
526 | Field_Id : constant Entity_Id := Defining_Entity (Field); |
527 | Field_Nam : constant Name_Id := Chars (Field_Id); | |
528 | Field_Typ : constant Entity_Id := Validated_View (Etype (Field_Id)); | |
529 | Attr_Nam : Name_Id; | |
45ec05e1 RD |
530 | |
531 | begin | |
f16cb8df HK |
532 | -- Do not process internally-generated fields. Note that checking for |
533 | -- Comes_From_Source is not correct because this will eliminate the | |
534 | -- components within the corresponding record of a protected type. | |
45ec05e1 | 535 | |
f16cb8df HK |
536 | if Nam_In (Field_Nam, Name_uObject, |
537 | Name_uParent, | |
538 | Name_uTag) | |
c468e1fb | 539 | then |
f16cb8df | 540 | null; |
45ec05e1 | 541 | |
f16cb8df | 542 | -- Do not process fields without any scalar components |
45ec05e1 | 543 | |
f16cb8df HK |
544 | elsif not Scalar_Part_Present (Field_Typ) then |
545 | null; | |
546 | ||
547 | -- Otherwise the field needs to be validated. Use Make_Identifier | |
548 | -- rather than New_Occurrence_Of to identify the field because the | |
549 | -- wrong entity may be picked up when private types are involved. | |
550 | ||
551 | -- Generate: | |
552 | -- [or else] not Rec_Typ (Obj_Id).Item_Nam'Valid[_Scalars] | |
553 | ||
554 | else | |
555 | if Is_Scalar_Type (Field_Typ) then | |
556 | Attr_Nam := Name_Valid; | |
557 | else | |
558 | Attr_Nam := Name_Valid_Scalars; | |
559 | end if; | |
560 | ||
561 | Evolve_Or_Else (Cond, | |
562 | Make_Op_Not (Loc, | |
563 | Right_Opnd => | |
564 | Make_Attribute_Reference (Loc, | |
565 | Prefix => | |
566 | Make_Selected_Component (Loc, | |
567 | Prefix => | |
568 | Unchecked_Convert_To (Rec_Typ, | |
569 | New_Occurrence_Of (Obj_Id, Loc)), | |
570 | Selector_Name => Make_Identifier (Loc, Field_Nam)), | |
571 | Attribute_Name => Attr_Nam))); | |
45ec05e1 | 572 | end if; |
f16cb8df | 573 | end Validate_Field; |
45ec05e1 | 574 | |
f16cb8df HK |
575 | --------------------- |
576 | -- Validate_Fields -- | |
577 | --------------------- | |
45ec05e1 | 578 | |
f16cb8df HK |
579 | procedure Validate_Fields |
580 | (Obj_Id : Entity_Id; | |
581 | Fields : List_Id; | |
582 | Stmts : in out List_Id) | |
583 | is | |
584 | Cond : Node_Id; | |
585 | Field : Node_Id; | |
45ec05e1 | 586 | |
f16cb8df HK |
587 | begin |
588 | -- Assume that none of the fields are eligible for verification | |
45ec05e1 | 589 | |
f16cb8df | 590 | Cond := Empty; |
45ec05e1 | 591 | |
f16cb8df | 592 | -- Validate all fields |
45ec05e1 | 593 | |
f16cb8df HK |
594 | Field := First_Non_Pragma (Fields); |
595 | while Present (Field) loop | |
596 | Validate_Field | |
597 | (Obj_Id => Obj_Id, | |
598 | Field => Field, | |
599 | Cond => Cond); | |
45ec05e1 | 600 | |
f16cb8df HK |
601 | Next_Non_Pragma (Field); |
602 | end loop; | |
45ec05e1 | 603 | |
f16cb8df HK |
604 | -- Generate: |
605 | -- if not Rec_Typ (Obj_Id).Item_Nam_1'Valid[_Scalars] | |
606 | -- or else not Rec_Typ (Obj_Id).Item_Nam_N'Valid[_Scalars] | |
607 | -- then | |
608 | -- return False; | |
609 | -- end if; | |
45ec05e1 | 610 | |
f16cb8df HK |
611 | if Present (Cond) then |
612 | Append_New_To (Stmts, | |
613 | Make_Implicit_If_Statement (Attr, | |
614 | Condition => Cond, | |
615 | Then_Statements => New_List ( | |
616 | Make_Simple_Return_Statement (Loc, | |
617 | Expression => New_Occurrence_Of (Standard_False, Loc))))); | |
618 | end if; | |
619 | end Validate_Fields; | |
45ec05e1 | 620 | |
f16cb8df HK |
621 | ---------------------- |
622 | -- Validate_Variant -- | |
623 | ---------------------- | |
45ec05e1 | 624 | |
f16cb8df HK |
625 | procedure Validate_Variant |
626 | (Obj_Id : Entity_Id; | |
627 | Var : Node_Id; | |
628 | Alts : in out List_Id) | |
629 | is | |
630 | Stmts : List_Id; | |
45ec05e1 | 631 | |
f16cb8df HK |
632 | begin |
633 | -- Assume that none of the components and variants are eligible for | |
634 | -- verification. | |
45ec05e1 | 635 | |
f16cb8df | 636 | Stmts := No_List; |
45ec05e1 | 637 | |
b108c2ed | 638 | -- Validate components |
45ec05e1 | 639 | |
f16cb8df HK |
640 | Validate_Component_List |
641 | (Obj_Id => Obj_Id, | |
642 | Comp_List => Component_List (Var), | |
643 | Stmts => Stmts); | |
644 | ||
645 | -- Generate a null statement in case none of the components were | |
646 | -- verified because this will otherwise eliminate an alternative | |
647 | -- from the variant case statement and render the generated code | |
648 | -- illegal. | |
45ec05e1 | 649 | |
f16cb8df HK |
650 | if No (Stmts) then |
651 | Append_New_To (Stmts, Make_Null_Statement (Loc)); | |
652 | end if; | |
45ec05e1 | 653 | |
f16cb8df HK |
654 | -- Generate: |
655 | -- when Discrete_Choices => | |
656 | -- Stmts | |
657 | ||
658 | Append_New_To (Alts, | |
659 | Make_Case_Statement_Alternative (Loc, | |
660 | Discrete_Choices => | |
661 | New_Copy_List_Tree (Discrete_Choices (Var)), | |
662 | Statements => Stmts)); | |
663 | end Validate_Variant; | |
664 | ||
665 | --------------------------- | |
666 | -- Validate_Variant_Part -- | |
667 | --------------------------- | |
668 | ||
669 | procedure Validate_Variant_Part | |
670 | (Obj_Id : Entity_Id; | |
671 | Var_Part : Node_Id; | |
672 | Stmts : in out List_Id) | |
673 | is | |
674 | Vars : constant List_Id := Variants (Var_Part); | |
675 | Alts : List_Id; | |
676 | Var : Node_Id; | |
45ec05e1 | 677 | |
f16cb8df HK |
678 | begin |
679 | -- Assume that none of the variants are eligible for verification | |
c468e1fb | 680 | |
f16cb8df | 681 | Alts := No_List; |
45ec05e1 | 682 | |
f16cb8df | 683 | -- Validate variants |
45ec05e1 | 684 | |
f16cb8df HK |
685 | Var := First_Non_Pragma (Vars); |
686 | while Present (Var) loop | |
687 | Validate_Variant | |
688 | (Obj_Id => Obj_Id, | |
689 | Var => Var, | |
690 | Alts => Alts); | |
45ec05e1 | 691 | |
f16cb8df HK |
692 | Next_Non_Pragma (Var); |
693 | end loop; | |
45ec05e1 | 694 | |
f16cb8df HK |
695 | -- Even though individual variants may lack eligible components, the |
696 | -- alternatives must still be generated. | |
45ec05e1 | 697 | |
f16cb8df | 698 | pragma Assert (Present (Alts)); |
45ec05e1 | 699 | |
f16cb8df HK |
700 | -- Generate: |
701 | -- case Rec_Typ (Obj_Id).Discriminant is | |
702 | -- when Discrete_Choices_1 => | |
703 | -- Stmts_1 | |
704 | -- when Discrete_Choices_N => | |
705 | -- Stmts_N | |
706 | -- end case; | |
707 | ||
708 | Append_New_To (Stmts, | |
709 | Make_Case_Statement (Loc, | |
710 | Expression => | |
711 | Make_Selected_Component (Loc, | |
712 | Prefix => | |
713 | Unchecked_Convert_To (Rec_Typ, | |
714 | New_Occurrence_Of (Obj_Id, Loc)), | |
715 | Selector_Name => New_Copy_Tree (Name (Var_Part))), | |
716 | Alternatives => Alts)); | |
717 | end Validate_Variant_Part; | |
45ec05e1 | 718 | |
bbe008b6 | 719 | -- Local variables |
45ec05e1 | 720 | |
f16cb8df HK |
721 | Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V'); |
722 | Obj_Id : constant Entity_Id := Make_Temporary (Loc, 'R'); | |
f2f9cdad | 723 | Comps : Node_Id; |
f16cb8df | 724 | Stmts : List_Id; |
f2f9cdad HK |
725 | Typ : Entity_Id; |
726 | Typ_Decl : Node_Id; | |
727 | Typ_Def : Node_Id; | |
728 | Typ_Ext : Node_Id; | |
45ec05e1 | 729 | |
bbe008b6 HK |
730 | -- Start of processing for Build_Record_VS_Func |
731 | ||
45ec05e1 | 732 | begin |
f2f9cdad HK |
733 | Typ := Rec_Typ; |
734 | ||
735 | -- Use the root type when dealing with a class-wide type | |
736 | ||
737 | if Is_Class_Wide_Type (Typ) then | |
66bd7af5 | 738 | Typ := Validated_View (Root_Type (Typ)); |
f2f9cdad HK |
739 | end if; |
740 | ||
741 | Typ_Decl := Declaration_Node (Typ); | |
742 | Typ_Def := Type_Definition (Typ_Decl); | |
743 | ||
744 | -- The components of a derived type are located in the extension part | |
745 | ||
746 | if Nkind (Typ_Def) = N_Derived_Type_Definition then | |
747 | Typ_Ext := Record_Extension_Part (Typ_Def); | |
748 | ||
749 | if Present (Typ_Ext) then | |
750 | Comps := Component_List (Typ_Ext); | |
751 | else | |
752 | Comps := Empty; | |
753 | end if; | |
754 | ||
755 | -- Otherwise the components are available in the definition | |
756 | ||
757 | else | |
758 | Comps := Component_List (Typ_Def); | |
759 | end if; | |
760 | ||
f16cb8df HK |
761 | -- The code generated by this routine is as follows: |
762 | -- | |
763 | -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is | |
764 | -- begin | |
765 | -- if not Rec_Typ (Obj_Id).Discriminant_1'Valid[_Scalars] | |
766 | -- or else not Rec_Typ (Obj_Id).Discriminant_N'Valid[_Scalars] | |
767 | -- then | |
768 | -- return False; | |
769 | -- end if; | |
770 | -- | |
771 | -- if not Rec_Typ (Obj_Id).Component_1'Valid[_Scalars] | |
772 | -- or else not Rec_Typ (Obj_Id).Component_N'Valid[_Scalars] | |
773 | -- then | |
774 | -- return False; | |
775 | -- end if; | |
776 | -- | |
777 | -- case Discriminant_1 is | |
778 | -- when Choice_1 => | |
779 | -- if not Rec_Typ (Obj_Id).Component_1'Valid[_Scalars] | |
780 | -- or else not Rec_Typ (Obj_Id).Component_N'Valid[_Scalars] | |
781 | -- then | |
782 | -- return False; | |
783 | -- end if; | |
784 | -- | |
785 | -- case Discriminant_N is | |
786 | -- ... | |
787 | -- when Choice_N => | |
788 | -- ... | |
789 | -- end case; | |
790 | -- | |
791 | -- return True; | |
792 | -- end Func_Id; | |
793 | ||
794 | -- Assume that the record type lacks eligible components, discriminants, | |
795 | -- and variant parts. | |
796 | ||
797 | Stmts := No_List; | |
798 | ||
799 | -- Validate the discriminants | |
800 | ||
801 | if not Is_Unchecked_Union (Rec_Typ) then | |
802 | Validate_Fields | |
803 | (Obj_Id => Obj_Id, | |
f2f9cdad | 804 | Fields => Discriminant_Specifications (Typ_Decl), |
f16cb8df HK |
805 | Stmts => Stmts); |
806 | end if; | |
45ec05e1 | 807 | |
f16cb8df | 808 | -- Validate the components and variant parts |
45ec05e1 | 809 | |
f16cb8df HK |
810 | Validate_Component_List |
811 | (Obj_Id => Obj_Id, | |
f2f9cdad | 812 | Comp_List => Comps, |
f16cb8df HK |
813 | Stmts => Stmts); |
814 | ||
815 | -- Generate: | |
816 | -- return True; | |
817 | ||
818 | Append_New_To (Stmts, | |
45ec05e1 RD |
819 | Make_Simple_Return_Statement (Loc, |
820 | Expression => New_Occurrence_Of (Standard_True, Loc))); | |
821 | ||
f16cb8df HK |
822 | -- Generate: |
823 | -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is | |
824 | -- begin | |
825 | -- Stmts | |
826 | -- end Func_Id; | |
827 | ||
828 | Set_Ekind (Func_Id, E_Function); | |
829 | Set_Is_Internal (Func_Id); | |
830 | Set_Is_Pure (Func_Id); | |
831 | ||
832 | if not Debug_Generated_Code then | |
833 | Set_Debug_Info_Off (Func_Id); | |
834 | end if; | |
835 | ||
836 | Insert_Action (Attr, | |
45ec05e1 RD |
837 | Make_Subprogram_Body (Loc, |
838 | Specification => | |
839 | Make_Function_Specification (Loc, | |
840 | Defining_Unit_Name => Func_Id, | |
f16cb8df HK |
841 | Parameter_Specifications => New_List ( |
842 | Make_Parameter_Specification (Loc, | |
843 | Defining_Identifier => Obj_Id, | |
844 | Parameter_Type => New_Occurrence_Of (Formal_Typ, Loc))), | |
845 | Result_Definition => | |
846 | New_Occurrence_Of (Standard_Boolean, Loc)), | |
45ec05e1 RD |
847 | Declarations => New_List, |
848 | Handled_Statement_Sequence => | |
f16cb8df HK |
849 | Make_Handled_Sequence_Of_Statements (Loc, |
850 | Statements => Stmts)), | |
45ec05e1 RD |
851 | Suppress => Discriminant_Check); |
852 | ||
45ec05e1 RD |
853 | return Func_Id; |
854 | end Build_Record_VS_Func; | |
855 | ||
70482933 RK |
856 | ---------------------------------- |
857 | -- Compile_Stream_Body_In_Scope -- | |
858 | ---------------------------------- | |
859 | ||
860 | procedure Compile_Stream_Body_In_Scope | |
861 | (N : Node_Id; | |
862 | Decl : Node_Id; | |
863 | Arr : Entity_Id; | |
864 | Check : Boolean) | |
865 | is | |
d6dffa66 HK |
866 | C_Type : constant Entity_Id := Base_Type (Component_Type (Arr)); |
867 | Curr : constant Entity_Id := Current_Scope; | |
868 | Install : Boolean := False; | |
869 | Scop : Entity_Id := Scope (Arr); | |
73f05f9f | 870 | |
70482933 RK |
871 | begin |
872 | if Is_Hidden (Arr) | |
873 | and then not In_Open_Scopes (Scop) | |
874 | and then Ekind (Scop) = E_Package | |
73f05f9f ES |
875 | then |
876 | Install := True; | |
73f05f9f | 877 | |
d6dffa66 HK |
878 | else |
879 | -- The component type may be private, in which case we install its | |
880 | -- full view to compile the subprogram. | |
3dddb11e | 881 | |
e3f94155 AC |
882 | -- The component type may be private, in which case we install its |
883 | -- full view to compile the subprogram. We do not do this if the | |
884 | -- type has a Stream_Convert pragma, which indicates that there are | |
885 | -- special stream-processing operations for that type (for example | |
886 | -- Unbounded_String and its wide varieties). | |
887 | ||
73f05f9f | 888 | Scop := Scope (C_Type); |
3dddb11e | 889 | |
73f05f9f ES |
890 | if Is_Private_Type (C_Type) |
891 | and then Present (Full_View (C_Type)) | |
892 | and then not In_Open_Scopes (Scop) | |
893 | and then Ekind (Scop) = E_Package | |
e3f94155 | 894 | and then No (Get_Stream_Convert_Pragma (C_Type)) |
73f05f9f ES |
895 | then |
896 | Install := True; | |
897 | end if; | |
898 | end if; | |
899 | ||
900 | -- If we are within an instance body, then all visibility has been | |
901 | -- established already and there is no need to install the package. | |
902 | ||
d6dffa66 | 903 | if Install and then not In_Instance_Body then |
31104818 | 904 | Push_Scope (Scop); |
70482933 RK |
905 | Install_Visible_Declarations (Scop); |
906 | Install_Private_Declarations (Scop); | |
70482933 RK |
907 | |
908 | -- The entities in the package are now visible, but the generated | |
909 | -- stream entity must appear in the current scope (usually an | |
910 | -- enclosing stream function) so that itypes all have their proper | |
911 | -- scopes. | |
912 | ||
31104818 | 913 | Push_Scope (Curr); |
73f05f9f ES |
914 | else |
915 | Install := False; | |
70482933 RK |
916 | end if; |
917 | ||
918 | if Check then | |
919 | Insert_Action (N, Decl); | |
920 | else | |
65f01153 | 921 | Insert_Action (N, Decl, Suppress => All_Checks); |
70482933 RK |
922 | end if; |
923 | ||
73f05f9f | 924 | if Install then |
70482933 RK |
925 | |
926 | -- Remove extra copy of current scope, and package itself | |
927 | ||
928 | Pop_Scope; | |
929 | End_Package_Scope (Scop); | |
930 | end if; | |
931 | end Compile_Stream_Body_In_Scope; | |
932 | ||
7ce611e2 ES |
933 | ----------------------------------- |
934 | -- Expand_Access_To_Protected_Op -- | |
935 | ----------------------------------- | |
936 | ||
937 | procedure Expand_Access_To_Protected_Op | |
938 | (N : Node_Id; | |
939 | Pref : Node_Id; | |
940 | Typ : Entity_Id) | |
941 | is | |
942 | -- The value of the attribute_reference is a record containing two | |
943 | -- fields: an access to the protected object, and an access to the | |
944 | -- subprogram itself. The prefix is a selected component. | |
945 | ||
946 | Loc : constant Source_Ptr := Sloc (N); | |
947 | Agg : Node_Id; | |
948 | Btyp : constant Entity_Id := Base_Type (Typ); | |
949 | Sub : Entity_Id; | |
e657b693 | 950 | Sub_Ref : Node_Id; |
7ce611e2 ES |
951 | E_T : constant Entity_Id := Equivalent_Type (Btyp); |
952 | Acc : constant Entity_Id := | |
953 | Etype (Next_Component (First_Component (E_T))); | |
954 | Obj_Ref : Node_Id; | |
955 | Curr : Entity_Id; | |
956 | ||
7ce611e2 ES |
957 | -- Start of processing for Expand_Access_To_Protected_Op |
958 | ||
959 | begin | |
6e1ee5c3 AC |
960 | -- Within the body of the protected type, the prefix designates a local |
961 | -- operation, and the object is the first parameter of the corresponding | |
962 | -- protected body of the current enclosing operation. | |
7ce611e2 ES |
963 | |
964 | if Is_Entity_Name (Pref) then | |
2290a0fe AC |
965 | -- All indirect calls are external calls, so must do locking and |
966 | -- barrier reevaluation, even if the 'Access occurs within the | |
967 | -- protected body. Hence the call to External_Subprogram, as opposed | |
968 | -- to Protected_Body_Subprogram, below. See RM-9.5(5). This means | |
969 | -- that indirect calls from within the same protected body will | |
970 | -- deadlock, as allowed by RM-9.5.1(8,15,17). | |
971 | ||
972 | Sub := New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc); | |
7ce611e2 | 973 | |
21d27997 RD |
974 | -- Don't traverse the scopes when the attribute occurs within an init |
975 | -- proc, because we directly use the _init formal of the init proc in | |
976 | -- that case. | |
977 | ||
7ce611e2 | 978 | Curr := Current_Scope; |
21d27997 RD |
979 | if not Is_Init_Proc (Curr) then |
980 | pragma Assert (In_Open_Scopes (Scope (Entity (Pref)))); | |
981 | ||
982 | while Scope (Curr) /= Scope (Entity (Pref)) loop | |
983 | Curr := Scope (Curr); | |
984 | end loop; | |
985 | end if; | |
7ce611e2 ES |
986 | |
987 | -- In case of protected entries the first formal of its Protected_ | |
988 | -- Body_Subprogram is the address of the object. | |
989 | ||
990 | if Ekind (Curr) = E_Entry then | |
991 | Obj_Ref := | |
992 | New_Occurrence_Of | |
993 | (First_Formal | |
994 | (Protected_Body_Subprogram (Curr)), Loc); | |
995 | ||
21d27997 RD |
996 | -- If the current scope is an init proc, then use the address of the |
997 | -- _init formal as the object reference. | |
998 | ||
999 | elsif Is_Init_Proc (Curr) then | |
1000 | Obj_Ref := | |
1001 | Make_Attribute_Reference (Loc, | |
1002 | Prefix => New_Occurrence_Of (First_Formal (Curr), Loc), | |
1003 | Attribute_Name => Name_Address); | |
1004 | ||
7ce611e2 ES |
1005 | -- In case of protected subprograms the first formal of its |
1006 | -- Protected_Body_Subprogram is the object and we get its address. | |
1007 | ||
1008 | else | |
1009 | Obj_Ref := | |
1010 | Make_Attribute_Reference (Loc, | |
1011 | Prefix => | |
1012 | New_Occurrence_Of | |
1013 | (First_Formal | |
1014 | (Protected_Body_Subprogram (Curr)), Loc), | |
1015 | Attribute_Name => Name_Address); | |
1016 | end if; | |
1017 | ||
1018 | -- Case where the prefix is not an entity name. Find the | |
1019 | -- version of the protected operation to be called from | |
1020 | -- outside the protected object. | |
1021 | ||
1022 | else | |
1023 | Sub := | |
1024 | New_Occurrence_Of | |
1025 | (External_Subprogram | |
1026 | (Entity (Selector_Name (Pref))), Loc); | |
1027 | ||
1028 | Obj_Ref := | |
1029 | Make_Attribute_Reference (Loc, | |
1030 | Prefix => Relocate_Node (Prefix (Pref)), | |
1031 | Attribute_Name => Name_Address); | |
1032 | end if; | |
1033 | ||
e657b693 AC |
1034 | Sub_Ref := |
1035 | Make_Attribute_Reference (Loc, | |
f7e71125 | 1036 | Prefix => Sub, |
e657b693 AC |
1037 | Attribute_Name => Name_Access); |
1038 | ||
1039 | -- We set the type of the access reference to the already generated | |
1040 | -- access_to_subprogram type, and declare the reference analyzed, to | |
1041 | -- prevent further expansion when the enclosing aggregate is analyzed. | |
1042 | ||
1043 | Set_Etype (Sub_Ref, Acc); | |
1044 | Set_Analyzed (Sub_Ref); | |
1045 | ||
7ce611e2 ES |
1046 | Agg := |
1047 | Make_Aggregate (Loc, | |
f7e71125 | 1048 | Expressions => New_List (Obj_Ref, Sub_Ref)); |
7ce611e2 | 1049 | |
1f92d7f2 AC |
1050 | -- Sub_Ref has been marked as analyzed, but we still need to make sure |
1051 | -- Sub is correctly frozen. | |
1052 | ||
6e1ee5c3 | 1053 | Freeze_Before (N, Entity (Sub)); |
1f92d7f2 | 1054 | |
7ce611e2 | 1055 | Rewrite (N, Agg); |
7ce611e2 ES |
1056 | Analyze_And_Resolve (N, E_T); |
1057 | ||
f7e71125 AC |
1058 | -- For subsequent analysis, the node must retain its type. The backend |
1059 | -- will replace it with the equivalent type where needed. | |
7ce611e2 ES |
1060 | |
1061 | Set_Etype (N, Typ); | |
1062 | end Expand_Access_To_Protected_Op; | |
1063 | ||
70482933 RK |
1064 | -------------------------- |
1065 | -- Expand_Fpt_Attribute -- | |
1066 | -------------------------- | |
1067 | ||
1068 | procedure Expand_Fpt_Attribute | |
1069 | (N : Node_Id; | |
65f01153 | 1070 | Pkg : RE_Id; |
fbf5a39b | 1071 | Nam : Name_Id; |
70482933 RK |
1072 | Args : List_Id) |
1073 | is | |
1074 | Loc : constant Source_Ptr := Sloc (N); | |
1075 | Typ : constant Entity_Id := Etype (N); | |
70482933 RK |
1076 | Fnm : Node_Id; |
1077 | ||
1078 | begin | |
65f01153 RD |
1079 | -- The function name is the selected component Attr_xxx.yyy where |
1080 | -- Attr_xxx is the package name, and yyy is the argument Nam. | |
70482933 RK |
1081 | |
1082 | -- Note: it would be more usual to have separate RE entries for each | |
1083 | -- of the entities in the Fat packages, but first they have identical | |
1084 | -- names (so we would have to have lots of renaming declarations to | |
1085 | -- meet the normal RE rule of separate names for all runtime entities), | |
a90bd866 | 1086 | -- and second there would be an awful lot of them. |
70482933 | 1087 | |
70482933 RK |
1088 | Fnm := |
1089 | Make_Selected_Component (Loc, | |
e4494292 | 1090 | Prefix => New_Occurrence_Of (RTE (Pkg), Loc), |
fbf5a39b | 1091 | Selector_Name => Make_Identifier (Loc, Nam)); |
70482933 RK |
1092 | |
1093 | -- The generated call is given the provided set of parameters, and then | |
6a987d78 | 1094 | -- wrapped in a conversion which converts the result to the target type. |
70482933 RK |
1095 | |
1096 | Rewrite (N, | |
6a987d78 | 1097 | Convert_To (Typ, |
70482933 | 1098 | Make_Function_Call (Loc, |
65f01153 | 1099 | Name => Fnm, |
70482933 RK |
1100 | Parameter_Associations => Args))); |
1101 | ||
1102 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
1103 | end Expand_Fpt_Attribute; |
1104 | ||
1105 | ---------------------------- | |
1106 | -- Expand_Fpt_Attribute_R -- | |
1107 | ---------------------------- | |
1108 | ||
1109 | -- The single argument is converted to its root type to call the | |
1110 | -- appropriate runtime function, with the actual call being built | |
1111 | -- by Expand_Fpt_Attribute | |
1112 | ||
1113 | procedure Expand_Fpt_Attribute_R (N : Node_Id) is | |
1114 | E1 : constant Node_Id := First (Expressions (N)); | |
65f01153 RD |
1115 | Ftp : Entity_Id; |
1116 | Pkg : RE_Id; | |
70482933 | 1117 | begin |
65f01153 | 1118 | Find_Fat_Info (Etype (E1), Ftp, Pkg); |
fbf5a39b | 1119 | Expand_Fpt_Attribute |
65f01153 RD |
1120 | (N, Pkg, Attribute_Name (N), |
1121 | New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1)))); | |
70482933 RK |
1122 | end Expand_Fpt_Attribute_R; |
1123 | ||
1124 | ----------------------------- | |
1125 | -- Expand_Fpt_Attribute_RI -- | |
1126 | ----------------------------- | |
1127 | ||
1128 | -- The first argument is converted to its root type and the second | |
1129 | -- argument is converted to standard long long integer to call the | |
1130 | -- appropriate runtime function, with the actual call being built | |
1131 | -- by Expand_Fpt_Attribute | |
1132 | ||
1133 | procedure Expand_Fpt_Attribute_RI (N : Node_Id) is | |
1134 | E1 : constant Node_Id := First (Expressions (N)); | |
65f01153 RD |
1135 | Ftp : Entity_Id; |
1136 | Pkg : RE_Id; | |
70482933 | 1137 | E2 : constant Node_Id := Next (E1); |
70482933 | 1138 | begin |
65f01153 | 1139 | Find_Fat_Info (Etype (E1), Ftp, Pkg); |
fbf5a39b | 1140 | Expand_Fpt_Attribute |
65f01153 | 1141 | (N, Pkg, Attribute_Name (N), |
fbf5a39b | 1142 | New_List ( |
65f01153 | 1143 | Unchecked_Convert_To (Ftp, Relocate_Node (E1)), |
fbf5a39b | 1144 | Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2)))); |
70482933 RK |
1145 | end Expand_Fpt_Attribute_RI; |
1146 | ||
1147 | ----------------------------- | |
1148 | -- Expand_Fpt_Attribute_RR -- | |
1149 | ----------------------------- | |
1150 | ||
0669bebe | 1151 | -- The two arguments are converted to their root types to call the |
70482933 RK |
1152 | -- appropriate runtime function, with the actual call being built |
1153 | -- by Expand_Fpt_Attribute | |
1154 | ||
1155 | procedure Expand_Fpt_Attribute_RR (N : Node_Id) is | |
d436b30d AC |
1156 | E1 : constant Node_Id := First (Expressions (N)); |
1157 | E2 : constant Node_Id := Next (E1); | |
65f01153 RD |
1158 | Ftp : Entity_Id; |
1159 | Pkg : RE_Id; | |
d436b30d | 1160 | |
70482933 | 1161 | begin |
65f01153 | 1162 | Find_Fat_Info (Etype (E1), Ftp, Pkg); |
fbf5a39b | 1163 | Expand_Fpt_Attribute |
65f01153 | 1164 | (N, Pkg, Attribute_Name (N), |
fbf5a39b | 1165 | New_List ( |
65f01153 RD |
1166 | Unchecked_Convert_To (Ftp, Relocate_Node (E1)), |
1167 | Unchecked_Convert_To (Ftp, Relocate_Node (E2)))); | |
70482933 RK |
1168 | end Expand_Fpt_Attribute_RR; |
1169 | ||
d436b30d AC |
1170 | --------------------------------- |
1171 | -- Expand_Loop_Entry_Attribute -- | |
1172 | --------------------------------- | |
1173 | ||
aa9b151a | 1174 | procedure Expand_Loop_Entry_Attribute (N : Node_Id) is |
d436b30d AC |
1175 | procedure Build_Conditional_Block |
1176 | (Loc : Source_Ptr; | |
1177 | Cond : Node_Id; | |
1178 | Loop_Stmt : Node_Id; | |
1179 | If_Stmt : out Node_Id; | |
1180 | Blk_Stmt : out Node_Id); | |
1181 | -- Create a block Blk_Stmt with an empty declarative list and a single | |
1182 | -- loop Loop_Stmt. The block is encased in an if statement If_Stmt with | |
1183 | -- condition Cond. If_Stmt is Empty when there is no condition provided. | |
1184 | ||
1185 | function Is_Array_Iteration (N : Node_Id) return Boolean; | |
1186 | -- Determine whether loop statement N denotes an Ada 2012 iteration over | |
1187 | -- an array object. | |
1188 | ||
1189 | ----------------------------- | |
1190 | -- Build_Conditional_Block -- | |
1191 | ----------------------------- | |
1192 | ||
1193 | procedure Build_Conditional_Block | |
1194 | (Loc : Source_Ptr; | |
1195 | Cond : Node_Id; | |
1196 | Loop_Stmt : Node_Id; | |
1197 | If_Stmt : out Node_Id; | |
1198 | Blk_Stmt : out Node_Id) | |
1199 | is | |
1200 | begin | |
1201 | -- Do not reanalyze the original loop statement because it is simply | |
1202 | -- being relocated. | |
1203 | ||
1204 | Set_Analyzed (Loop_Stmt); | |
1205 | ||
1206 | Blk_Stmt := | |
1207 | Make_Block_Statement (Loc, | |
1208 | Declarations => New_List, | |
1209 | Handled_Statement_Sequence => | |
1210 | Make_Handled_Sequence_Of_Statements (Loc, | |
1211 | Statements => New_List (Loop_Stmt))); | |
1212 | ||
1213 | if Present (Cond) then | |
1214 | If_Stmt := | |
1215 | Make_If_Statement (Loc, | |
1216 | Condition => Cond, | |
1217 | Then_Statements => New_List (Blk_Stmt)); | |
1218 | else | |
1219 | If_Stmt := Empty; | |
1220 | end if; | |
1221 | end Build_Conditional_Block; | |
1222 | ||
1223 | ------------------------ | |
1224 | -- Is_Array_Iteration -- | |
1225 | ------------------------ | |
1226 | ||
1227 | function Is_Array_Iteration (N : Node_Id) return Boolean is | |
1228 | Stmt : constant Node_Id := Original_Node (N); | |
1229 | Iter : Node_Id; | |
1230 | ||
1231 | begin | |
1232 | if Nkind (Stmt) = N_Loop_Statement | |
1233 | and then Present (Iteration_Scheme (Stmt)) | |
1234 | and then Present (Iterator_Specification (Iteration_Scheme (Stmt))) | |
1235 | then | |
1236 | Iter := Iterator_Specification (Iteration_Scheme (Stmt)); | |
1237 | ||
1238 | return | |
1239 | Of_Present (Iter) and then Is_Array_Type (Etype (Name (Iter))); | |
1240 | end if; | |
1241 | ||
1242 | return False; | |
1243 | end Is_Array_Iteration; | |
1244 | ||
1245 | -- Local variables | |
1246 | ||
aa9b151a | 1247 | Pref : constant Node_Id := Prefix (N); |
0f83b044 AC |
1248 | Base_Typ : constant Entity_Id := Base_Type (Etype (Pref)); |
1249 | Exprs : constant List_Id := Expressions (N); | |
1250 | Aux_Decl : Node_Id; | |
dcd5fd67 | 1251 | Blk : Node_Id := Empty; |
d436b30d AC |
1252 | Decls : List_Id; |
1253 | Installed : Boolean; | |
1254 | Loc : Source_Ptr; | |
1255 | Loop_Id : Entity_Id; | |
1256 | Loop_Stmt : Node_Id; | |
31e358e1 | 1257 | Result : Node_Id := Empty; |
d436b30d AC |
1258 | Scheme : Node_Id; |
1259 | Temp_Decl : Node_Id; | |
1260 | Temp_Id : Entity_Id; | |
1261 | ||
1262 | -- Start of processing for Expand_Loop_Entry_Attribute | |
1263 | ||
1264 | begin | |
1265 | -- Step 1: Find the related loop | |
1266 | ||
1267 | -- The loop label variant of attribute 'Loop_Entry already has all the | |
1268 | -- information in its expression. | |
1269 | ||
1270 | if Present (Exprs) then | |
1271 | Loop_Id := Entity (First (Exprs)); | |
1272 | Loop_Stmt := Label_Construct (Parent (Loop_Id)); | |
1273 | ||
0f83b044 AC |
1274 | -- Climb the parent chain to find the nearest enclosing loop. Skip |
1275 | -- all internally generated loops for quantified expressions and for | |
1276 | -- element iterators over multidimensional arrays because the pragma | |
1277 | -- applies to source loop. | |
d436b30d AC |
1278 | |
1279 | else | |
aa9b151a | 1280 | Loop_Stmt := N; |
d436b30d AC |
1281 | while Present (Loop_Stmt) loop |
1282 | if Nkind (Loop_Stmt) = N_Loop_Statement | |
ae5115dd AC |
1283 | and then Nkind (Original_Node (Loop_Stmt)) = N_Loop_Statement |
1284 | and then Comes_From_Source (Original_Node (Loop_Stmt)) | |
d436b30d AC |
1285 | then |
1286 | exit; | |
1287 | end if; | |
1288 | ||
1289 | Loop_Stmt := Parent (Loop_Stmt); | |
1290 | end loop; | |
1291 | ||
1292 | Loop_Id := Entity (Identifier (Loop_Stmt)); | |
1293 | end if; | |
1294 | ||
1295 | Loc := Sloc (Loop_Stmt); | |
1296 | ||
1297 | -- Step 2: Transform the loop | |
1298 | ||
1299 | -- The loop has already been transformed during the expansion of a prior | |
1300 | -- 'Loop_Entry attribute. Retrieve the declarative list of the block. | |
1301 | ||
1302 | if Has_Loop_Entry_Attributes (Loop_Id) then | |
3d67b239 AC |
1303 | |
1304 | -- When the related loop name appears as the argument of attribute | |
1305 | -- Loop_Entry, the corresponding label construct is the generated | |
0d5fbf52 | 1306 | -- block statement. This is because the expander reuses the label. |
3d67b239 | 1307 | |
24778dbb AC |
1308 | if Nkind (Loop_Stmt) = N_Block_Statement then |
1309 | Decls := Declarations (Loop_Stmt); | |
3d67b239 AC |
1310 | |
1311 | -- In all other cases, the loop must appear in the handled sequence | |
1312 | -- of statements of the generated block. | |
1313 | ||
24778dbb | 1314 | else |
3d67b239 AC |
1315 | pragma Assert |
1316 | (Nkind (Parent (Loop_Stmt)) = N_Handled_Sequence_Of_Statements | |
fc999c5d RD |
1317 | and then |
1318 | Nkind (Parent (Parent (Loop_Stmt))) = N_Block_Statement); | |
24778dbb AC |
1319 | |
1320 | Decls := Declarations (Parent (Parent (Loop_Stmt))); | |
1321 | end if; | |
1322 | ||
d436b30d AC |
1323 | -- Transform the loop into a conditional block |
1324 | ||
1325 | else | |
1326 | Set_Has_Loop_Entry_Attributes (Loop_Id); | |
1327 | Scheme := Iteration_Scheme (Loop_Stmt); | |
1328 | ||
3d67b239 AC |
1329 | -- Infinite loops are transformed into: |
1330 | ||
1331 | -- declare | |
1332 | -- Temp1 : constant <type of Pref1> := <Pref1>; | |
1333 | -- . . . | |
1334 | -- TempN : constant <type of PrefN> := <PrefN>; | |
1335 | -- begin | |
1336 | -- loop | |
1337 | -- <original source statements with attribute rewrites> | |
1338 | -- end loop; | |
1339 | -- end; | |
1340 | ||
1341 | if No (Scheme) then | |
1342 | Build_Conditional_Block (Loc, | |
1343 | Cond => Empty, | |
1344 | Loop_Stmt => Relocate_Node (Loop_Stmt), | |
1345 | If_Stmt => Result, | |
1346 | Blk_Stmt => Blk); | |
1347 | ||
1348 | Result := Blk; | |
1349 | ||
d436b30d AC |
1350 | -- While loops are transformed into: |
1351 | ||
fd7215d7 AC |
1352 | -- function Fnn return Boolean is |
1353 | -- begin | |
1354 | -- <condition actions> | |
1355 | -- return <condition>; | |
1356 | -- end Fnn; | |
1357 | ||
1358 | -- if Fnn then | |
d436b30d AC |
1359 | -- declare |
1360 | -- Temp1 : constant <type of Pref1> := <Pref1>; | |
1361 | -- . . . | |
1362 | -- TempN : constant <type of PrefN> := <PrefN>; | |
1363 | -- begin | |
1364 | -- loop | |
1365 | -- <original source statements with attribute rewrites> | |
fd7215d7 | 1366 | -- exit when not Fnn; |
d436b30d AC |
1367 | -- end loop; |
1368 | -- end; | |
1369 | -- end if; | |
1370 | ||
1371 | -- Note that loops over iterators and containers are already | |
1372 | -- converted into while loops. | |
1373 | ||
3d67b239 | 1374 | elsif Present (Condition (Scheme)) then |
d436b30d | 1375 | declare |
fd7215d7 AC |
1376 | Func_Decl : Node_Id; |
1377 | Func_Id : Entity_Id; | |
1378 | Stmts : List_Id; | |
d436b30d AC |
1379 | |
1380 | begin | |
c961d820 EB |
1381 | Func_Id := Make_Temporary (Loc, 'F'); |
1382 | ||
fd7215d7 AC |
1383 | -- Wrap the condition of the while loop in a Boolean function. |
1384 | -- This avoids the duplication of the same code which may lead | |
1385 | -- to gigi issues with respect to multiple declaration of the | |
1386 | -- same entity in the presence of side effects or checks. Note | |
75f6bfce EB |
1387 | -- that the condition actions must also be relocated into the |
1388 | -- wrapping function because they may contain itypes, e.g. in | |
1389 | -- the case of a comparison involving slices. | |
fd7215d7 AC |
1390 | |
1391 | -- Generate: | |
1392 | -- <condition actions> | |
1393 | -- return <condition>; | |
1394 | ||
1395 | if Present (Condition_Actions (Scheme)) then | |
1396 | Stmts := Condition_Actions (Scheme); | |
1397 | else | |
1398 | Stmts := New_List; | |
1399 | end if; | |
1400 | ||
1401 | Append_To (Stmts, | |
1402 | Make_Simple_Return_Statement (Loc, | |
c961d820 EB |
1403 | Expression => |
1404 | New_Copy_Tree (Condition (Scheme), | |
1405 | New_Scope => Func_Id))); | |
fd7215d7 AC |
1406 | |
1407 | -- Generate: | |
1408 | -- function Fnn return Boolean is | |
1409 | -- begin | |
1410 | -- <Stmts> | |
1411 | -- end Fnn; | |
1412 | ||
fd7215d7 AC |
1413 | Func_Decl := |
1414 | Make_Subprogram_Body (Loc, | |
1415 | Specification => | |
1416 | Make_Function_Specification (Loc, | |
1417 | Defining_Unit_Name => Func_Id, | |
1418 | Result_Definition => | |
1419 | New_Occurrence_Of (Standard_Boolean, Loc)), | |
1420 | Declarations => Empty_List, | |
1421 | Handled_Statement_Sequence => | |
1422 | Make_Handled_Sequence_Of_Statements (Loc, | |
1423 | Statements => Stmts)); | |
1424 | ||
1425 | -- The function is inserted before the related loop. Make sure | |
1426 | -- to analyze it in the context of the loop's enclosing scope. | |
1427 | ||
1428 | Push_Scope (Scope (Loop_Id)); | |
1429 | Insert_Action (Loop_Stmt, Func_Decl); | |
1430 | Pop_Scope; | |
1431 | ||
07511699 GD |
1432 | -- The analysis of the condition may have generated entities |
1433 | -- (such as itypes) that are now used within the function. | |
1434 | -- Adjust their scopes accordingly so that their use appears | |
1435 | -- in their scope of definition. | |
b6b011dd ES |
1436 | |
1437 | declare | |
07511699 | 1438 | Ent : Entity_Id; |
b6b011dd ES |
1439 | |
1440 | begin | |
07511699 GD |
1441 | Ent := First_Entity (Loop_Id); |
1442 | ||
1443 | while Present (Ent) loop | |
1444 | -- Various entities that now occur within the function | |
1445 | -- need to have their scope reset, but not all entities | |
1446 | -- associated with Loop_Id are now inside the function. | |
1447 | -- The function entity itself and loop parameters can | |
1448 | -- be outside the function, and there may be others. | |
1449 | -- It's not clear how the determination of what entity | |
1450 | -- scopes need to be adjusted can be made accurately. | |
1451 | -- Perhaps it will be necessary to traverse the function | |
1452 | -- body to find the exact entities whose scopes need to | |
1453 | -- be reset to the function's Entity_Id. ??? | |
1454 | ||
1455 | if Ekind (Ent) /= E_Loop_Parameter | |
1456 | and then Ent /= Func_Id | |
1457 | then | |
1458 | Set_Scope (Ent, Func_Id); | |
b6b011dd | 1459 | end if; |
07511699 GD |
1460 | |
1461 | Next_Entity (Ent); | |
b6b011dd ES |
1462 | end loop; |
1463 | end; | |
1464 | ||
d436b30d AC |
1465 | -- Transform the original while loop into an infinite loop |
1466 | -- where the last statement checks the negated condition. This | |
1467 | -- placement ensures that the condition will not be evaluated | |
1468 | -- twice on the first iteration. | |
1469 | ||
fd7215d7 AC |
1470 | Set_Iteration_Scheme (Loop_Stmt, Empty); |
1471 | Scheme := Empty; | |
1472 | ||
d436b30d | 1473 | -- Generate: |
fd7215d7 | 1474 | -- exit when not Fnn; |
d436b30d AC |
1475 | |
1476 | Append_To (Statements (Loop_Stmt), | |
1477 | Make_Exit_Statement (Loc, | |
fd7215d7 AC |
1478 | Condition => |
1479 | Make_Op_Not (Loc, | |
1480 | Right_Opnd => | |
1481 | Make_Function_Call (Loc, | |
1482 | Name => New_Occurrence_Of (Func_Id, Loc))))); | |
d436b30d AC |
1483 | |
1484 | Build_Conditional_Block (Loc, | |
fd7215d7 AC |
1485 | Cond => |
1486 | Make_Function_Call (Loc, | |
1487 | Name => New_Occurrence_Of (Func_Id, Loc)), | |
d436b30d AC |
1488 | Loop_Stmt => Relocate_Node (Loop_Stmt), |
1489 | If_Stmt => Result, | |
1490 | Blk_Stmt => Blk); | |
1491 | end; | |
1492 | ||
1493 | -- Ada 2012 iteration over an array is transformed into: | |
1494 | ||
1495 | -- if <Array_Nam>'Length (1) > 0 | |
1496 | -- and then <Array_Nam>'Length (N) > 0 | |
1497 | -- then | |
1498 | -- declare | |
1499 | -- Temp1 : constant <type of Pref1> := <Pref1>; | |
1500 | -- . . . | |
1501 | -- TempN : constant <type of PrefN> := <PrefN>; | |
1502 | -- begin | |
1503 | -- for X in ... loop -- multiple loops depending on dims | |
1504 | -- <original source statements with attribute rewrites> | |
1505 | -- end loop; | |
1506 | -- end; | |
1507 | -- end if; | |
1508 | ||
1509 | elsif Is_Array_Iteration (Loop_Stmt) then | |
1510 | declare | |
1511 | Array_Nam : constant Entity_Id := | |
1512 | Entity (Name (Iterator_Specification | |
1513 | (Iteration_Scheme (Original_Node (Loop_Stmt))))); | |
1514 | Num_Dims : constant Pos := | |
1515 | Number_Dimensions (Etype (Array_Nam)); | |
1516 | Cond : Node_Id := Empty; | |
1517 | Check : Node_Id; | |
1518 | ||
1519 | begin | |
1520 | -- Generate a check which determines whether all dimensions of | |
1521 | -- the array are non-null. | |
1522 | ||
1523 | for Dim in 1 .. Num_Dims loop | |
1524 | Check := | |
1525 | Make_Op_Gt (Loc, | |
1526 | Left_Opnd => | |
1527 | Make_Attribute_Reference (Loc, | |
e4494292 | 1528 | Prefix => New_Occurrence_Of (Array_Nam, Loc), |
d436b30d AC |
1529 | Attribute_Name => Name_Length, |
1530 | Expressions => New_List ( | |
1531 | Make_Integer_Literal (Loc, Dim))), | |
1532 | Right_Opnd => | |
1533 | Make_Integer_Literal (Loc, 0)); | |
1534 | ||
1535 | if No (Cond) then | |
1536 | Cond := Check; | |
1537 | else | |
1538 | Cond := | |
1539 | Make_And_Then (Loc, | |
1540 | Left_Opnd => Cond, | |
1541 | Right_Opnd => Check); | |
1542 | end if; | |
1543 | end loop; | |
1544 | ||
1545 | Build_Conditional_Block (Loc, | |
1546 | Cond => Cond, | |
1547 | Loop_Stmt => Relocate_Node (Loop_Stmt), | |
1548 | If_Stmt => Result, | |
1549 | Blk_Stmt => Blk); | |
1550 | end; | |
1551 | ||
1552 | -- For loops are transformed into: | |
1553 | ||
1554 | -- if <Low> <= <High> then | |
1555 | -- declare | |
1556 | -- Temp1 : constant <type of Pref1> := <Pref1>; | |
1557 | -- . . . | |
1558 | -- TempN : constant <type of PrefN> := <PrefN>; | |
1559 | -- begin | |
1560 | -- for <Def_Id> in <Low> .. <High> loop | |
1561 | -- <original source statements with attribute rewrites> | |
1562 | -- end loop; | |
1563 | -- end; | |
1564 | -- end if; | |
1565 | ||
1566 | elsif Present (Loop_Parameter_Specification (Scheme)) then | |
1567 | declare | |
1568 | Loop_Spec : constant Node_Id := | |
1569 | Loop_Parameter_Specification (Scheme); | |
1570 | Cond : Node_Id; | |
1571 | Subt_Def : Node_Id; | |
1572 | ||
1573 | begin | |
1574 | Subt_Def := Discrete_Subtype_Definition (Loop_Spec); | |
1575 | ||
1576 | -- When the loop iterates over a subtype indication with a | |
1577 | -- range, use the low and high bounds of the subtype itself. | |
1578 | ||
1579 | if Nkind (Subt_Def) = N_Subtype_Indication then | |
1580 | Subt_Def := Scalar_Range (Etype (Subt_Def)); | |
1581 | end if; | |
1582 | ||
1583 | pragma Assert (Nkind (Subt_Def) = N_Range); | |
1584 | ||
1585 | -- Generate | |
1586 | -- Low <= High | |
1587 | ||
1588 | Cond := | |
1589 | Make_Op_Le (Loc, | |
1590 | Left_Opnd => New_Copy_Tree (Low_Bound (Subt_Def)), | |
1591 | Right_Opnd => New_Copy_Tree (High_Bound (Subt_Def))); | |
1592 | ||
1593 | Build_Conditional_Block (Loc, | |
1594 | Cond => Cond, | |
1595 | Loop_Stmt => Relocate_Node (Loop_Stmt), | |
1596 | If_Stmt => Result, | |
1597 | Blk_Stmt => Blk); | |
1598 | end; | |
d436b30d AC |
1599 | end if; |
1600 | ||
1601 | Decls := Declarations (Blk); | |
1602 | end if; | |
1603 | ||
1604 | -- Step 3: Create a constant to capture the value of the prefix at the | |
1605 | -- entry point into the loop. | |
1606 | ||
d436b30d AC |
1607 | Temp_Id := Make_Temporary (Loc, 'P'); |
1608 | ||
6c802906 AC |
1609 | -- Preserve the tag of the prefix by offering a specific view of the |
1610 | -- class-wide version of the prefix. | |
1611 | ||
0f83b044 AC |
1612 | if Is_Tagged_Type (Base_Typ) then |
1613 | Tagged_Case : declare | |
1614 | CW_Temp : Entity_Id; | |
1615 | CW_Typ : Entity_Id; | |
1616 | ||
1617 | begin | |
1618 | -- Generate: | |
1619 | -- CW_Temp : constant Base_Typ'Class := Base_Typ'Class (Pref); | |
1620 | ||
1621 | CW_Temp := Make_Temporary (Loc, 'T'); | |
1622 | CW_Typ := Class_Wide_Type (Base_Typ); | |
1623 | ||
1624 | Aux_Decl := | |
1625 | Make_Object_Declaration (Loc, | |
1626 | Defining_Identifier => CW_Temp, | |
1627 | Constant_Present => True, | |
1628 | Object_Definition => New_Occurrence_Of (CW_Typ, Loc), | |
1629 | Expression => | |
1630 | Convert_To (CW_Typ, Relocate_Node (Pref))); | |
1631 | Append_To (Decls, Aux_Decl); | |
1632 | ||
1633 | -- Generate: | |
1634 | -- Temp : Base_Typ renames Base_Typ (CW_Temp); | |
1635 | ||
1636 | Temp_Decl := | |
1637 | Make_Object_Renaming_Declaration (Loc, | |
1638 | Defining_Identifier => Temp_Id, | |
1639 | Subtype_Mark => New_Occurrence_Of (Base_Typ, Loc), | |
1640 | Name => | |
1641 | Convert_To (Base_Typ, New_Occurrence_Of (CW_Temp, Loc))); | |
1642 | Append_To (Decls, Temp_Decl); | |
1643 | end Tagged_Case; | |
1644 | ||
1645 | -- Untagged case | |
6c802906 AC |
1646 | |
1647 | else | |
0f83b044 AC |
1648 | Untagged_Case : declare |
1649 | Temp_Expr : Node_Id; | |
1650 | ||
1651 | begin | |
1652 | Aux_Decl := Empty; | |
1653 | ||
1654 | -- Generate a nominal type for the constant when the prefix is of | |
1655 | -- a constrained type. This is achieved by setting the Etype of | |
1656 | -- the relocated prefix to its base type. Since the prefix is now | |
1657 | -- the initialization expression of the constant, its freezing | |
1658 | -- will produce a proper nominal type. | |
1659 | ||
1660 | Temp_Expr := Relocate_Node (Pref); | |
1661 | Set_Etype (Temp_Expr, Base_Typ); | |
1662 | ||
1663 | -- Generate: | |
1664 | -- Temp : constant Base_Typ := Pref; | |
1665 | ||
1666 | Temp_Decl := | |
1667 | Make_Object_Declaration (Loc, | |
1668 | Defining_Identifier => Temp_Id, | |
1669 | Constant_Present => True, | |
1670 | Object_Definition => New_Occurrence_Of (Base_Typ, Loc), | |
1671 | Expression => Temp_Expr); | |
1672 | Append_To (Decls, Temp_Decl); | |
1673 | end Untagged_Case; | |
6c802906 | 1674 | end if; |
d436b30d AC |
1675 | |
1676 | -- Step 4: Analyze all bits | |
1677 | ||
3d67b239 | 1678 | Installed := Current_Scope = Scope (Loop_Id); |
d436b30d | 1679 | |
327b1ba4 AC |
1680 | -- Depending on the pracement of attribute 'Loop_Entry relative to the |
1681 | -- associated loop, ensure the proper visibility for analysis. | |
1682 | ||
d436b30d AC |
1683 | if not Installed then |
1684 | Push_Scope (Scope (Loop_Id)); | |
1685 | end if; | |
1686 | ||
327b1ba4 AC |
1687 | -- The analysis of the conditional block takes care of the constant |
1688 | -- declaration. | |
1689 | ||
d436b30d AC |
1690 | if Present (Result) then |
1691 | Rewrite (Loop_Stmt, Result); | |
1692 | Analyze (Loop_Stmt); | |
327b1ba4 AC |
1693 | |
1694 | -- The conditional block was analyzed when a previous 'Loop_Entry was | |
1695 | -- expanded. There is no point in reanalyzing the block, simply analyze | |
1696 | -- the declaration of the constant. | |
1697 | ||
d436b30d | 1698 | else |
0f83b044 AC |
1699 | if Present (Aux_Decl) then |
1700 | Analyze (Aux_Decl); | |
6c802906 AC |
1701 | end if; |
1702 | ||
d436b30d AC |
1703 | Analyze (Temp_Decl); |
1704 | end if; | |
1705 | ||
fd7215d7 | 1706 | Rewrite (N, New_Occurrence_Of (Temp_Id, Loc)); |
aa9b151a | 1707 | Analyze (N); |
d436b30d | 1708 | |
d436b30d AC |
1709 | if not Installed then |
1710 | Pop_Scope; | |
1711 | end if; | |
1712 | end Expand_Loop_Entry_Attribute; | |
1713 | ||
e0f63680 AC |
1714 | ------------------------------ |
1715 | -- Expand_Min_Max_Attribute -- | |
1716 | ------------------------------ | |
1717 | ||
1718 | procedure Expand_Min_Max_Attribute (N : Node_Id) is | |
1719 | begin | |
1720 | -- Min and Max are handled by the back end (except that static cases | |
1721 | -- have already been evaluated during semantic processing, although the | |
1722 | -- back end should not count on this). The one bit of special processing | |
1723 | -- required in the normal case is that these two attributes typically | |
1724 | -- generate conditionals in the code, so check the relevant restriction. | |
1725 | ||
1726 | Check_Restriction (No_Implicit_Conditionals, N); | |
e0f63680 AC |
1727 | end Expand_Min_Max_Attribute; |
1728 | ||
70482933 RK |
1729 | ---------------------------------- |
1730 | -- Expand_N_Attribute_Reference -- | |
1731 | ---------------------------------- | |
1732 | ||
1733 | procedure Expand_N_Attribute_Reference (N : Node_Id) is | |
1734 | Loc : constant Source_Ptr := Sloc (N); | |
70482933 RK |
1735 | Pref : constant Node_Id := Prefix (N); |
1736 | Exprs : constant List_Id := Expressions (N); | |
70482933 | 1737 | |
f193b29e EB |
1738 | function Get_Integer_Type (Typ : Entity_Id) return Entity_Id; |
1739 | -- Return a small integer type appropriate for the enumeration type | |
1740 | ||
110d0820 BD |
1741 | procedure Rewrite_Attribute_Proc_Call (Pname : Entity_Id); |
1742 | -- Rewrites an attribute for Read, Write, Output, or Put_Image with a | |
1743 | -- call to the appropriate TSS procedure. Pname is the entity for the | |
1744 | -- procedure to call. | |
70482933 | 1745 | |
f193b29e EB |
1746 | ---------------------- |
1747 | -- Get_Integer_Type -- | |
1748 | ---------------------- | |
1749 | ||
1750 | function Get_Integer_Type (Typ : Entity_Id) return Entity_Id is | |
25a11453 | 1751 | Siz : constant Uint := Esize (Base_Type (Typ)); |
f193b29e EB |
1752 | Int_Typ : Entity_Id; |
1753 | ||
1754 | begin | |
25a11453 EB |
1755 | -- We need to accommodate invalid values of the base type since we |
1756 | -- accept them for Enum_Rep and Pos, so we reason on the Esize. And | |
1757 | -- we use an unsigned type since the enumeration type is unsigned. | |
f193b29e | 1758 | |
25a11453 EB |
1759 | if Siz <= Esize (Standard_Short_Short_Unsigned) then |
1760 | Int_Typ := Standard_Short_Short_Unsigned; | |
f193b29e | 1761 | |
25a11453 EB |
1762 | elsif Siz <= Esize (Standard_Short_Unsigned) then |
1763 | Int_Typ := Standard_Short_Unsigned; | |
f193b29e | 1764 | |
25a11453 EB |
1765 | elsif Siz <= Esize (Standard_Unsigned) then |
1766 | Int_Typ := Standard_Unsigned; | |
f193b29e EB |
1767 | |
1768 | else | |
c5c69d05 | 1769 | Int_Typ := Standard_Long_Long_Unsigned; |
f193b29e EB |
1770 | end if; |
1771 | ||
1772 | return Int_Typ; | |
1773 | end Get_Integer_Type; | |
1774 | ||
110d0820 BD |
1775 | --------------------------------- |
1776 | -- Rewrite_Attribute_Proc_Call -- | |
1777 | --------------------------------- | |
70482933 | 1778 | |
110d0820 | 1779 | procedure Rewrite_Attribute_Proc_Call (Pname : Entity_Id) is |
70482933 | 1780 | Item : constant Node_Id := Next (First (Exprs)); |
ed3fe8cc | 1781 | Item_Typ : constant Entity_Id := Etype (Item); |
fbf5a39b AC |
1782 | Formal : constant Entity_Id := Next_Formal (First_Formal (Pname)); |
1783 | Formal_Typ : constant Entity_Id := Etype (Formal); | |
ed3fe8cc | 1784 | Is_Written : constant Boolean := Ekind (Formal) /= E_In_Parameter; |
70482933 RK |
1785 | |
1786 | begin | |
fbf5a39b AC |
1787 | -- The expansion depends on Item, the second actual, which is |
1788 | -- the object being streamed in or out. | |
1789 | ||
1790 | -- If the item is a component of a packed array type, and | |
1791 | -- a conversion is needed on exit, we introduce a temporary to | |
1792 | -- hold the value, because otherwise the packed reference will | |
1793 | -- not be properly expanded. | |
1794 | ||
1795 | if Nkind (Item) = N_Indexed_Component | |
1796 | and then Is_Packed (Base_Type (Etype (Prefix (Item)))) | |
ed3fe8cc | 1797 | and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ) |
fbf5a39b AC |
1798 | and then Is_Written |
1799 | then | |
1800 | declare | |
191fcb3a | 1801 | Temp : constant Entity_Id := Make_Temporary (Loc, 'V'); |
fbf5a39b AC |
1802 | Decl : Node_Id; |
1803 | Assn : Node_Id; | |
1804 | ||
1805 | begin | |
1806 | Decl := | |
1807 | Make_Object_Declaration (Loc, | |
1808 | Defining_Identifier => Temp, | |
ed3fe8cc | 1809 | Object_Definition => New_Occurrence_Of (Formal_Typ, Loc)); |
fbf5a39b AC |
1810 | Set_Etype (Temp, Formal_Typ); |
1811 | ||
1812 | Assn := | |
1813 | Make_Assignment_Statement (Loc, | |
ed3fe8cc | 1814 | Name => New_Copy_Tree (Item), |
fbf5a39b AC |
1815 | Expression => |
1816 | Unchecked_Convert_To | |
ed3fe8cc | 1817 | (Item_Typ, New_Occurrence_Of (Temp, Loc))); |
fbf5a39b AC |
1818 | |
1819 | Rewrite (Item, New_Occurrence_Of (Temp, Loc)); | |
1820 | Insert_Actions (N, | |
1821 | New_List ( | |
1822 | Decl, | |
1823 | Make_Procedure_Call_Statement (Loc, | |
ed3fe8cc | 1824 | Name => New_Occurrence_Of (Pname, Loc), |
fbf5a39b AC |
1825 | Parameter_Associations => Exprs), |
1826 | Assn)); | |
1827 | ||
1828 | Rewrite (N, Make_Null_Statement (Loc)); | |
1829 | return; | |
1830 | end; | |
1831 | end if; | |
70482933 RK |
1832 | |
1833 | -- For the class-wide dispatching cases, and for cases in which | |
1834 | -- the base type of the second argument matches the base type of | |
fbf5a39b AC |
1835 | -- the corresponding formal parameter (that is to say the stream |
1836 | -- operation is not inherited), we are all set, and can use the | |
1837 | -- argument unchanged. | |
70482933 | 1838 | |
70482933 | 1839 | if not Is_Class_Wide_Type (Entity (Pref)) |
fbf5a39b | 1840 | and then not Is_Class_Wide_Type (Etype (Item)) |
ed3fe8cc | 1841 | and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ) |
70482933 | 1842 | then |
ed3fe8cc AC |
1843 | -- Perform a view conversion when either the argument or the |
1844 | -- formal parameter are of a private type. | |
1845 | ||
b5360737 AC |
1846 | if Is_Private_Type (Base_Type (Formal_Typ)) |
1847 | or else Is_Private_Type (Base_Type (Item_Typ)) | |
ed3fe8cc AC |
1848 | then |
1849 | Rewrite (Item, | |
1850 | Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item))); | |
1851 | ||
1852 | -- Otherwise perform a regular type conversion to ensure that all | |
1853 | -- relevant checks are installed. | |
1854 | ||
1855 | else | |
1856 | Rewrite (Item, Convert_To (Formal_Typ, Relocate_Node (Item))); | |
1857 | end if; | |
70482933 RK |
1858 | |
1859 | -- For untagged derived types set Assignment_OK, to prevent | |
1860 | -- copies from being created when the unchecked conversion | |
1861 | -- is expanded (which would happen in Remove_Side_Effects | |
1862 | -- if Expand_N_Unchecked_Conversion were allowed to call | |
365c8496 RD |
1863 | -- Force_Evaluation). The copy could violate Ada semantics in |
1864 | -- cases such as an actual that is an out parameter. Note that | |
1865 | -- this approach is also used in exp_ch7 for calls to controlled | |
1866 | -- type operations to prevent problems with actuals wrapped in | |
1867 | -- unchecked conversions. | |
70482933 RK |
1868 | |
1869 | if Is_Untagged_Derivation (Etype (Expression (Item))) then | |
1870 | Set_Assignment_OK (Item); | |
1871 | end if; | |
1872 | end if; | |
1873 | ||
110d0820 BD |
1874 | -- The stream operation to call might be a renaming created by an |
1875 | -- attribute definition clause, and might not be frozen yet. Ensure | |
365c8496 | 1876 | -- that it has the necessary extra formals. |
99269cf5 ES |
1877 | |
1878 | if not Is_Frozen (Pname) then | |
1879 | Create_Extra_Formals (Pname); | |
1880 | end if; | |
1881 | ||
70482933 RK |
1882 | -- And now rewrite the call |
1883 | ||
1884 | Rewrite (N, | |
1885 | Make_Procedure_Call_Statement (Loc, | |
ed3fe8cc | 1886 | Name => New_Occurrence_Of (Pname, Loc), |
70482933 RK |
1887 | Parameter_Associations => Exprs)); |
1888 | ||
1889 | Analyze (N); | |
110d0820 BD |
1890 | end Rewrite_Attribute_Proc_Call; |
1891 | ||
1892 | Typ : constant Entity_Id := Etype (N); | |
1893 | Btyp : constant Entity_Id := Base_Type (Typ); | |
1894 | Ptyp : constant Entity_Id := Etype (Pref); | |
1895 | Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N)); | |
70482933 RK |
1896 | |
1897 | -- Start of processing for Expand_N_Attribute_Reference | |
1898 | ||
1899 | begin | |
82c80734 RD |
1900 | -- Do required validity checking, if enabled. Do not apply check to |
1901 | -- output parameters of an Asm instruction, since the value of this | |
1dcdbfab AC |
1902 | -- is not set till after the attribute has been elaborated, and do |
1903 | -- not apply the check to the arguments of a 'Read or 'Input attribute | |
1904 | -- reference since the scalar argument is an OUT scalar. | |
70482933 | 1905 | |
82c80734 RD |
1906 | if Validity_Checks_On and then Validity_Check_Operands |
1907 | and then Id /= Attribute_Asm_Output | |
1dcdbfab AC |
1908 | and then Id /= Attribute_Read |
1909 | and then Id /= Attribute_Input | |
82c80734 | 1910 | then |
70482933 RK |
1911 | declare |
1912 | Expr : Node_Id; | |
70482933 RK |
1913 | begin |
1914 | Expr := First (Expressions (N)); | |
1915 | while Present (Expr) loop | |
1916 | Ensure_Valid (Expr); | |
1917 | Next (Expr); | |
1918 | end loop; | |
1919 | end; | |
1920 | end if; | |
1921 | ||
21d27997 RD |
1922 | -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in- |
1923 | -- place function, then a temporary return object needs to be created | |
d4dfb005 | 1924 | -- and access to it must be passed to the function. |
21d27997 | 1925 | |
d4dfb005 | 1926 | if Is_Build_In_Place_Function_Call (Pref) then |
fb9dd1c7 PMR |
1927 | |
1928 | -- If attribute is 'Old, the context is a postcondition, and | |
1929 | -- the temporary must go in the corresponding subprogram, not | |
1930 | -- the postcondition function or any created blocks, as when | |
1931 | -- the attribute appears in a quantified expression. This is | |
1932 | -- handled below in the expansion of the attribute. | |
1933 | ||
1934 | if Attribute_Name (Parent (Pref)) = Name_Old then | |
1935 | null; | |
fb9dd1c7 PMR |
1936 | else |
1937 | Make_Build_In_Place_Call_In_Anonymous_Context (Pref); | |
1938 | end if; | |
4ac62786 AC |
1939 | |
1940 | -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix | |
1941 | -- containing build-in-place function calls whose returned object covers | |
1942 | -- interface types. | |
1943 | ||
d4dfb005 | 1944 | elsif Present (Unqual_BIP_Iface_Function_Call (Pref)) then |
4ac62786 | 1945 | Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref); |
21d27997 RD |
1946 | end if; |
1947 | ||
5f3f175d AC |
1948 | -- If prefix is a protected type name, this is a reference to the |
1949 | -- current instance of the type. For a component definition, nothing | |
1950 | -- to do (expansion will occur in the init proc). In other contexts, | |
1951 | -- rewrite into reference to current instance. | |
1952 | ||
1953 | if Is_Protected_Self_Reference (Pref) | |
1e4b91fc | 1954 | and then not |
8926d369 AC |
1955 | (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint, |
1956 | N_Discriminant_Association) | |
1957 | and then Nkind (Parent (Parent (Parent (Parent (N))))) = | |
69ba91ed | 1958 | N_Component_Definition) |
1e4b91fc AC |
1959 | |
1960 | -- No action needed for these attributes since the current instance | |
1961 | -- will be rewritten to be the name of the _object parameter | |
1962 | -- associated with the enclosing protected subprogram (see below). | |
1963 | ||
1964 | and then Id /= Attribute_Access | |
1965 | and then Id /= Attribute_Unchecked_Access | |
1966 | and then Id /= Attribute_Unrestricted_Access | |
5f3f175d | 1967 | then |
2d14501c ST |
1968 | Rewrite (Pref, Concurrent_Ref (Pref)); |
1969 | Analyze (Pref); | |
1970 | end if; | |
1971 | ||
70482933 RK |
1972 | -- Remaining processing depends on specific attribute |
1973 | ||
2eef7403 AC |
1974 | -- Note: individual sections of the following case statement are |
1975 | -- allowed to assume there is no code after the case statement, and | |
1976 | -- are legitimately allowed to execute return statements if they have | |
1977 | -- nothing more to do. | |
1978 | ||
70482933 RK |
1979 | case Id is |
1980 | ||
82d4f390 | 1981 | -- Attributes related to Ada 2012 iterators |
0da80d7d | 1982 | |
d8f43ee6 HK |
1983 | when Attribute_Constant_Indexing |
1984 | | Attribute_Default_Iterator | |
1985 | | Attribute_Implicit_Dereference | |
1986 | | Attribute_Iterable | |
1987 | | Attribute_Iterator_Element | |
1988 | | Attribute_Variable_Indexing | |
1989 | => | |
d48f3dca | 1990 | null; |
b98e2969 | 1991 | |
d27f3ff4 AC |
1992 | -- Internal attributes used to deal with Ada 2012 delayed aspects. These |
1993 | -- were already rejected by the parser. Thus they shouldn't appear here. | |
b98e2969 | 1994 | |
c1107fa3 | 1995 | when Internal_Attribute_Id => |
d48f3dca | 1996 | raise Program_Error; |
0da80d7d | 1997 | |
70482933 RK |
1998 | ------------ |
1999 | -- Access -- | |
2000 | ------------ | |
2001 | ||
d8f43ee6 HK |
2002 | when Attribute_Access |
2003 | | Attribute_Unchecked_Access | |
2004 | | Attribute_Unrestricted_Access | |
2005 | => | |
3192631e | 2006 | Access_Cases : declare |
3192631e | 2007 | Ref_Object : constant Node_Id := Get_Referenced_Object (Pref); |
0d4aed99 | 2008 | Btyp_DDT : Entity_Id; |
70482933 | 2009 | |
01aef5ad GD |
2010 | function Enclosing_Object (N : Node_Id) return Node_Id; |
2011 | -- If N denotes a compound name (selected component, indexed | |
69ba91ed AC |
2012 | -- component, or slice), returns the name of the outermost such |
2013 | -- enclosing object. Otherwise returns N. If the object is a | |
2014 | -- renaming, then the renamed object is returned. | |
01aef5ad GD |
2015 | |
2016 | ---------------------- | |
2017 | -- Enclosing_Object -- | |
2018 | ---------------------- | |
2019 | ||
2020 | function Enclosing_Object (N : Node_Id) return Node_Id is | |
2021 | Obj_Name : Node_Id; | |
2022 | ||
2023 | begin | |
2024 | Obj_Name := N; | |
2025 | while Nkind_In (Obj_Name, N_Selected_Component, | |
2026 | N_Indexed_Component, | |
2027 | N_Slice) | |
2028 | loop | |
2029 | Obj_Name := Prefix (Obj_Name); | |
2030 | end loop; | |
2031 | ||
2032 | return Get_Referenced_Object (Obj_Name); | |
2033 | end Enclosing_Object; | |
2034 | ||
2035 | -- Local declarations | |
2036 | ||
2037 | Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object); | |
2038 | ||
2039 | -- Start of processing for Access_Cases | |
2040 | ||
3192631e | 2041 | begin |
0d4aed99 AC |
2042 | Btyp_DDT := Designated_Type (Btyp); |
2043 | ||
2044 | -- Handle designated types that come from the limited view | |
2045 | ||
47346923 AC |
2046 | if From_Limited_With (Btyp_DDT) |
2047 | and then Has_Non_Limited_View (Btyp_DDT) | |
0d4aed99 AC |
2048 | then |
2049 | Btyp_DDT := Non_Limited_View (Btyp_DDT); | |
0d4aed99 AC |
2050 | end if; |
2051 | ||
e10dab7f JM |
2052 | -- In order to improve the text of error messages, the designated |
2053 | -- type of access-to-subprogram itypes is set by the semantics as | |
2054 | -- the associated subprogram entity (see sem_attr). Now we replace | |
2055 | -- such node with the proper E_Subprogram_Type itype. | |
2056 | ||
2057 | if Id = Attribute_Unrestricted_Access | |
2058 | and then Is_Subprogram (Directly_Designated_Type (Typ)) | |
2059 | then | |
21d27997 | 2060 | -- The following conditions ensure that this special management |
e10dab7f JM |
2061 | -- is done only for "Address!(Prim'Unrestricted_Access)" nodes. |
2062 | -- At this stage other cases in which the designated type is | |
2063 | -- still a subprogram (instead of an E_Subprogram_Type) are | |
e14c931f | 2064 | -- wrong because the semantics must have overridden the type of |
e10dab7f JM |
2065 | -- the node with the type imposed by the context. |
2066 | ||
21d27997 RD |
2067 | if Nkind (Parent (N)) = N_Unchecked_Type_Conversion |
2068 | and then Etype (Parent (N)) = RTE (RE_Prim_Ptr) | |
2069 | then | |
2070 | Set_Etype (N, RTE (RE_Prim_Ptr)); | |
e10dab7f | 2071 | |
21d27997 RD |
2072 | else |
2073 | declare | |
2074 | Subp : constant Entity_Id := | |
2075 | Directly_Designated_Type (Typ); | |
2076 | Etyp : Entity_Id; | |
2077 | Extra : Entity_Id := Empty; | |
2078 | New_Formal : Entity_Id; | |
2079 | Old_Formal : Entity_Id := First_Formal (Subp); | |
2080 | Subp_Typ : Entity_Id; | |
e10dab7f | 2081 | |
21d27997 RD |
2082 | begin |
2083 | Subp_Typ := Create_Itype (E_Subprogram_Type, N); | |
2084 | Set_Etype (Subp_Typ, Etype (Subp)); | |
2085 | Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp)); | |
e10dab7f | 2086 | |
21d27997 RD |
2087 | if Present (Old_Formal) then |
2088 | New_Formal := New_Copy (Old_Formal); | |
2089 | Set_First_Entity (Subp_Typ, New_Formal); | |
e10dab7f | 2090 | |
21d27997 RD |
2091 | loop |
2092 | Set_Scope (New_Formal, Subp_Typ); | |
2093 | Etyp := Etype (New_Formal); | |
e10dab7f | 2094 | |
21d27997 RD |
2095 | -- Handle itypes. There is no need to duplicate |
2096 | -- here the itypes associated with record types | |
2097 | -- (i.e the implicit full view of private types). | |
e10dab7f | 2098 | |
21d27997 RD |
2099 | if Is_Itype (Etyp) |
2100 | and then Ekind (Base_Type (Etyp)) /= E_Record_Type | |
e10dab7f | 2101 | then |
21d27997 RD |
2102 | Extra := New_Copy (Etyp); |
2103 | Set_Parent (Extra, New_Formal); | |
2104 | Set_Etype (New_Formal, Extra); | |
2105 | Set_Scope (Extra, Subp_Typ); | |
e10dab7f JM |
2106 | end if; |
2107 | ||
21d27997 RD |
2108 | Extra := New_Formal; |
2109 | Next_Formal (Old_Formal); | |
2110 | exit when No (Old_Formal); | |
e10dab7f | 2111 | |
3f6d1daa JS |
2112 | Link_Entities (New_Formal, New_Copy (Old_Formal)); |
2113 | Next_Entity (New_Formal); | |
21d27997 | 2114 | end loop; |
e10dab7f | 2115 | |
3f6d1daa | 2116 | Unlink_Next_Entity (New_Formal); |
21d27997 RD |
2117 | Set_Last_Entity (Subp_Typ, Extra); |
2118 | end if; | |
e10dab7f | 2119 | |
21d27997 RD |
2120 | -- Now that the explicit formals have been duplicated, |
2121 | -- any extra formals needed by the subprogram must be | |
2122 | -- created. | |
e10dab7f | 2123 | |
21d27997 RD |
2124 | if Present (Extra) then |
2125 | Set_Extra_Formal (Extra, Empty); | |
2126 | end if; | |
e10dab7f | 2127 | |
21d27997 RD |
2128 | Create_Extra_Formals (Subp_Typ); |
2129 | Set_Directly_Designated_Type (Typ, Subp_Typ); | |
2130 | end; | |
2131 | end if; | |
e10dab7f JM |
2132 | end if; |
2133 | ||
3192631e JM |
2134 | if Is_Access_Protected_Subprogram_Type (Btyp) then |
2135 | Expand_Access_To_Protected_Op (N, Pref, Typ); | |
2136 | ||
2137 | -- If prefix is a type name, this is a reference to the current | |
2138 | -- instance of the type, within its initialization procedure. | |
2139 | ||
2140 | elsif Is_Entity_Name (Pref) | |
2141 | and then Is_Type (Entity (Pref)) | |
2142 | then | |
2143 | declare | |
2144 | Par : Node_Id; | |
2145 | Formal : Entity_Id; | |
2146 | ||
2147 | begin | |
2148 | -- If the current instance name denotes a task type, then | |
2149 | -- the access attribute is rewritten to be the name of the | |
2150 | -- "_task" parameter associated with the task type's task | |
2151 | -- procedure. An unchecked conversion is applied to ensure | |
2152 | -- a type match in cases of expander-generated calls (e.g. | |
2153 | -- init procs). | |
2154 | ||
2155 | if Is_Task_Type (Entity (Pref)) then | |
2156 | Formal := | |
2157 | First_Entity (Get_Task_Body_Procedure (Entity (Pref))); | |
2158 | while Present (Formal) loop | |
2159 | exit when Chars (Formal) = Name_uTask; | |
2160 | Next_Entity (Formal); | |
2161 | end loop; | |
2162 | ||
2163 | pragma Assert (Present (Formal)); | |
3e8ee849 | 2164 | |
3192631e JM |
2165 | Rewrite (N, |
2166 | Unchecked_Convert_To (Typ, | |
2167 | New_Occurrence_Of (Formal, Loc))); | |
2168 | Set_Etype (N, Typ); | |
3e8ee849 | 2169 | |
1e4b91fc AC |
2170 | elsif Is_Protected_Type (Entity (Pref)) then |
2171 | ||
2172 | -- No action needed for current instance located in a | |
2173 | -- component definition (expansion will occur in the | |
2174 | -- init proc) | |
2175 | ||
2176 | if Is_Protected_Type (Current_Scope) then | |
2177 | null; | |
2178 | ||
2179 | -- If the current instance reference is located in a | |
2180 | -- protected subprogram or entry then rewrite the access | |
2181 | -- attribute to be the name of the "_object" parameter. | |
2182 | -- An unchecked conversion is applied to ensure a type | |
2183 | -- match in cases of expander-generated calls (e.g. init | |
2184 | -- procs). | |
2185 | ||
289a994b AC |
2186 | -- The code may be nested in a block, so find enclosing |
2187 | -- scope that is a protected operation. | |
2188 | ||
1e4b91fc | 2189 | else |
289a994b AC |
2190 | declare |
2191 | Subp : Entity_Id; | |
2192 | ||
2193 | begin | |
2194 | Subp := Current_Scope; | |
59fad002 | 2195 | while Ekind_In (Subp, E_Loop, E_Block) loop |
289a994b AC |
2196 | Subp := Scope (Subp); |
2197 | end loop; | |
2198 | ||
2199 | Formal := | |
2200 | First_Entity | |
2201 | (Protected_Body_Subprogram (Subp)); | |
2202 | ||
2203 | -- For a protected subprogram the _Object parameter | |
2204 | -- is the protected record, so we create an access | |
2205 | -- to it. The _Object parameter of an entry is an | |
2206 | -- address. | |
2207 | ||
2208 | if Ekind (Subp) = E_Entry then | |
2209 | Rewrite (N, | |
2210 | Unchecked_Convert_To (Typ, | |
2211 | New_Occurrence_Of (Formal, Loc))); | |
2212 | Set_Etype (N, Typ); | |
2213 | ||
2214 | else | |
2215 | Rewrite (N, | |
2216 | Unchecked_Convert_To (Typ, | |
2217 | Make_Attribute_Reference (Loc, | |
2218 | Attribute_Name => Name_Unrestricted_Access, | |
59fad002 AC |
2219 | Prefix => |
2220 | New_Occurrence_Of (Formal, Loc)))); | |
289a994b AC |
2221 | Analyze_And_Resolve (N); |
2222 | end if; | |
2223 | end; | |
1e4b91fc AC |
2224 | end if; |
2225 | ||
2226 | -- The expression must appear in a default expression, | |
2227 | -- (which in the initialization procedure is the right-hand | |
2228 | -- side of an assignment), and not in a discriminant | |
2229 | -- constraint. | |
3e8ee849 | 2230 | |
3192631e JM |
2231 | else |
2232 | Par := Parent (N); | |
2233 | while Present (Par) loop | |
2234 | exit when Nkind (Par) = N_Assignment_Statement; | |
3e8ee849 | 2235 | |
3192631e JM |
2236 | if Nkind (Par) = N_Component_Declaration then |
2237 | return; | |
2238 | end if; | |
3e8ee849 | 2239 | |
3192631e JM |
2240 | Par := Parent (Par); |
2241 | end loop; | |
3e8ee849 | 2242 | |
3192631e JM |
2243 | if Present (Par) then |
2244 | Rewrite (N, | |
2245 | Make_Attribute_Reference (Loc, | |
2246 | Prefix => Make_Identifier (Loc, Name_uInit), | |
2247 | Attribute_Name => Attribute_Name (N))); | |
3e8ee849 | 2248 | |
3192631e JM |
2249 | Analyze_And_Resolve (N, Typ); |
2250 | end if; | |
3e8ee849 | 2251 | end if; |
3192631e JM |
2252 | end; |
2253 | ||
2254 | -- If the prefix of an Access attribute is a dereference of an | |
01aef5ad GD |
2255 | -- access parameter (or a renaming of such a dereference, or a |
2256 | -- subcomponent of such a dereference) and the context is a | |
ae8c7d87 RD |
2257 | -- general access type (including the type of an object or |
2258 | -- component with an access_definition, but not the anonymous | |
2259 | -- type of an access parameter or access discriminant), then | |
01aef5ad GD |
2260 | -- apply an accessibility check to the access parameter. We used |
2261 | -- to rewrite the access parameter as a type conversion, but that | |
2262 | -- could only be done if the immediate prefix of the Access | |
2263 | -- attribute was the dereference, and didn't handle cases where | |
2264 | -- the attribute is applied to a subcomponent of the dereference, | |
2265 | -- since there's generally no available, appropriate access type | |
e84e11ba GD |
2266 | -- to convert to in that case. The attribute is passed as the |
2267 | -- point to insert the check, because the access parameter may | |
2268 | -- come from a renaming, possibly in a different scope, and the | |
2269 | -- check must be associated with the attribute itself. | |
01aef5ad GD |
2270 | |
2271 | elsif Id = Attribute_Access | |
2272 | and then Nkind (Enc_Object) = N_Explicit_Dereference | |
2273 | and then Is_Entity_Name (Prefix (Enc_Object)) | |
ae8c7d87 RD |
2274 | and then (Ekind (Btyp) = E_General_Access_Type |
2275 | or else Is_Local_Anonymous_Access (Btyp)) | |
01aef5ad GD |
2276 | and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind |
2277 | and then Ekind (Etype (Entity (Prefix (Enc_Object)))) | |
3192631e JM |
2278 | = E_Anonymous_Access_Type |
2279 | and then Present (Extra_Accessibility | |
01aef5ad | 2280 | (Entity (Prefix (Enc_Object)))) |
3192631e | 2281 | then |
e84e11ba | 2282 | Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N); |
3192631e JM |
2283 | |
2284 | -- Ada 2005 (AI-251): If the designated type is an interface we | |
2285 | -- add an implicit conversion to force the displacement of the | |
2286 | -- pointer to reference the secondary dispatch table. | |
2287 | ||
2288 | elsif Is_Interface (Btyp_DDT) | |
2289 | and then (Comes_From_Source (N) | |
2290 | or else Comes_From_Source (Ref_Object) | |
2291 | or else (Nkind (Ref_Object) in N_Has_Chars | |
2292 | and then Chars (Ref_Object) = Name_uInit)) | |
2293 | then | |
2294 | if Nkind (Ref_Object) /= N_Explicit_Dereference then | |
2295 | ||
bea993f9 AC |
2296 | -- No implicit conversion required if types match, or if |
2297 | -- the prefix is the class_wide_type of the interface. In | |
2298 | -- either case passing an object of the interface type has | |
2299 | -- already set the pointer correctly. | |
2300 | ||
2301 | if Btyp_DDT = Etype (Ref_Object) | |
2302 | or else (Is_Class_Wide_Type (Etype (Ref_Object)) | |
2303 | and then | |
2304 | Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object)) | |
2305 | then | |
2306 | null; | |
3192631e | 2307 | |
bea993f9 | 2308 | else |
3192631e | 2309 | Rewrite (Prefix (N), |
0d4aed99 | 2310 | Convert_To (Btyp_DDT, |
3192631e JM |
2311 | New_Copy_Tree (Prefix (N)))); |
2312 | ||
0d4aed99 | 2313 | Analyze_And_Resolve (Prefix (N), Btyp_DDT); |
70482933 | 2314 | end if; |
758c442c | 2315 | |
3192631e JM |
2316 | -- When the object is an explicit dereference, convert the |
2317 | -- dereference's prefix. | |
3e8ee849 | 2318 | |
3192631e JM |
2319 | else |
2320 | declare | |
2321 | Obj_DDT : constant Entity_Id := | |
2322 | Base_Type | |
2323 | (Directly_Designated_Type | |
2324 | (Etype (Prefix (Ref_Object)))); | |
2325 | begin | |
2326 | -- No implicit conversion required if designated types | |
904a2ae4 | 2327 | -- match. |
3192631e JM |
2328 | |
2329 | if Obj_DDT /= Btyp_DDT | |
2330 | and then not (Is_Class_Wide_Type (Obj_DDT) | |
3b59004a | 2331 | and then Etype (Obj_DDT) = Btyp_DDT) |
3192631e JM |
2332 | then |
2333 | Rewrite (N, | |
2334 | Convert_To (Typ, | |
2335 | New_Copy_Tree (Prefix (Ref_Object)))); | |
2336 | Analyze_And_Resolve (N, Typ); | |
2337 | end if; | |
2338 | end; | |
70482933 | 2339 | end if; |
3192631e JM |
2340 | end if; |
2341 | end Access_Cases; | |
70482933 RK |
2342 | |
2343 | -------------- | |
2344 | -- Adjacent -- | |
2345 | -------------- | |
2346 | ||
2347 | -- Transforms 'Adjacent into a call to the floating-point attribute | |
2348 | -- function Adjacent in Fat_xxx (where xxx is the root type) | |
2349 | ||
2350 | when Attribute_Adjacent => | |
2351 | Expand_Fpt_Attribute_RR (N); | |
2352 | ||
2353 | ------------- | |
2354 | -- Address -- | |
2355 | ------------- | |
2356 | ||
2357 | when Attribute_Address => Address : declare | |
2358 | Task_Proc : Entity_Id; | |
2359 | ||
47997d25 GD |
2360 | function Is_Unnested_Component_Init (N : Node_Id) return Boolean; |
2361 | -- Returns True if N is being used to initialize a component of | |
2362 | -- an activation record object where the component corresponds to | |
2363 | -- the object denoted by the prefix of the attribute N. | |
2364 | ||
2365 | function Is_Unnested_Component_Init (N : Node_Id) return Boolean is | |
2366 | begin | |
2367 | return Present (Parent (N)) | |
2368 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
2369 | and then Is_Entity_Name (Pref) | |
2370 | and then Present (Activation_Record_Component (Entity (Pref))) | |
2371 | and then Nkind (Name (Parent (N))) = N_Selected_Component | |
2372 | and then Entity (Selector_Name (Name (Parent (N)))) = | |
2373 | Activation_Record_Component (Entity (Pref)); | |
2374 | end Is_Unnested_Component_Init; | |
2375 | ||
2376 | -- Start of processing for Address | |
2377 | ||
70482933 | 2378 | begin |
3e8ee849 RD |
2379 | -- If the prefix is a task or a task type, the useful address is that |
2380 | -- of the procedure for the task body, i.e. the actual program unit. | |
2381 | -- We replace the original entity with that of the procedure. | |
70482933 RK |
2382 | |
2383 | if Is_Entity_Name (Pref) | |
2384 | and then Is_Task_Type (Entity (Pref)) | |
2385 | then | |
21d27997 | 2386 | Task_Proc := Next_Entity (Root_Type (Ptyp)); |
70482933 RK |
2387 | |
2388 | while Present (Task_Proc) loop | |
2389 | exit when Ekind (Task_Proc) = E_Procedure | |
2390 | and then Etype (First_Formal (Task_Proc)) = | |
21d27997 | 2391 | Corresponding_Record_Type (Ptyp); |
70482933 RK |
2392 | Next_Entity (Task_Proc); |
2393 | end loop; | |
2394 | ||
2395 | if Present (Task_Proc) then | |
2396 | Set_Entity (Pref, Task_Proc); | |
2397 | Set_Etype (Pref, Etype (Task_Proc)); | |
2398 | end if; | |
2399 | ||
2400 | -- Similarly, the address of a protected operation is the address | |
2401 | -- of the corresponding protected body, regardless of the protected | |
2402 | -- object from which it is selected. | |
2403 | ||
2404 | elsif Nkind (Pref) = N_Selected_Component | |
2405 | and then Is_Subprogram (Entity (Selector_Name (Pref))) | |
2406 | and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref)))) | |
2407 | then | |
2408 | Rewrite (Pref, | |
2409 | New_Occurrence_Of ( | |
2410 | External_Subprogram (Entity (Selector_Name (Pref))), Loc)); | |
2411 | ||
2412 | elsif Nkind (Pref) = N_Explicit_Dereference | |
21d27997 RD |
2413 | and then Ekind (Ptyp) = E_Subprogram_Type |
2414 | and then Convention (Ptyp) = Convention_Protected | |
70482933 RK |
2415 | then |
2416 | -- The prefix is be a dereference of an access_to_protected_ | |
2417 | -- subprogram. The desired address is the second component of | |
2418 | -- the record that represents the access. | |
2419 | ||
2420 | declare | |
2421 | Addr : constant Entity_Id := Etype (N); | |
2422 | Ptr : constant Node_Id := Prefix (Pref); | |
2423 | T : constant Entity_Id := | |
2424 | Equivalent_Type (Base_Type (Etype (Ptr))); | |
2425 | ||
2426 | begin | |
2427 | Rewrite (N, | |
2428 | Unchecked_Convert_To (Addr, | |
2429 | Make_Selected_Component (Loc, | |
2430 | Prefix => Unchecked_Convert_To (T, Ptr), | |
2431 | Selector_Name => New_Occurrence_Of ( | |
2432 | Next_Entity (First_Entity (T)), Loc)))); | |
2433 | ||
2434 | Analyze_And_Resolve (N, Addr); | |
2435 | end; | |
0669bebe GB |
2436 | |
2437 | -- Ada 2005 (AI-251): Class-wide interface objects are always | |
2438 | -- "displaced" to reference the tag associated with the interface | |
2439 | -- type. In order to obtain the real address of such objects we | |
2440 | -- generate a call to a run-time subprogram that returns the base | |
47997d25 GD |
2441 | -- address of the object. This call is not generated in cases where |
2442 | -- the attribute is being used to initialize a component of an | |
2443 | -- activation record object where the component corresponds to | |
2444 | -- prefix of the attribute (for back ends that require "unnesting" | |
2445 | -- of nested subprograms), since the address needs to be assigned | |
2446 | -- as-is to such components. | |
0669bebe | 2447 | |
21d27997 | 2448 | elsif Is_Class_Wide_Type (Ptyp) |
63a5b3dc | 2449 | and then Is_Interface (Underlying_Type (Ptyp)) |
1f110335 | 2450 | and then Tagged_Type_Expansion |
31104818 HK |
2451 | and then not (Nkind (Pref) in N_Has_Entity |
2452 | and then Is_Subprogram (Entity (Pref))) | |
47997d25 | 2453 | and then not Is_Unnested_Component_Init (N) |
0669bebe GB |
2454 | then |
2455 | Rewrite (N, | |
2456 | Make_Function_Call (Loc, | |
e4494292 | 2457 | Name => New_Occurrence_Of (RTE (RE_Base_Address), Loc), |
0669bebe GB |
2458 | Parameter_Associations => New_List ( |
2459 | Relocate_Node (N)))); | |
2460 | Analyze (N); | |
2461 | return; | |
70482933 RK |
2462 | end if; |
2463 | ||
21d27997 RD |
2464 | -- Deal with packed array reference, other cases are handled by |
2465 | -- the back end. | |
70482933 RK |
2466 | |
2467 | if Involves_Packed_Array_Reference (Pref) then | |
2468 | Expand_Packed_Address_Reference (N); | |
2469 | end if; | |
2470 | end Address; | |
2471 | ||
fbf5a39b AC |
2472 | --------------- |
2473 | -- Alignment -- | |
2474 | --------------- | |
2475 | ||
2476 | when Attribute_Alignment => Alignment : declare | |
fbf5a39b AC |
2477 | New_Node : Node_Id; |
2478 | ||
2479 | begin | |
2480 | -- For class-wide types, X'Class'Alignment is transformed into a | |
2481 | -- direct reference to the Alignment of the class type, so that the | |
2482 | -- back end does not have to deal with the X'Class'Alignment | |
2483 | -- reference. | |
2484 | ||
2485 | if Is_Entity_Name (Pref) | |
2486 | and then Is_Class_Wide_Type (Entity (Pref)) | |
2487 | then | |
2488 | Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc)); | |
2489 | return; | |
2490 | ||
2491 | -- For x'Alignment applied to an object of a class wide type, | |
2492 | -- transform X'Alignment into a call to the predefined primitive | |
2493 | -- operation _Alignment applied to X. | |
2494 | ||
2495 | elsif Is_Class_Wide_Type (Ptyp) then | |
2496 | New_Node := | |
d9937d1b AC |
2497 | Make_Attribute_Reference (Loc, |
2498 | Prefix => Pref, | |
2499 | Attribute_Name => Name_Tag); | |
2500 | ||
535a8637 | 2501 | New_Node := Build_Get_Alignment (Loc, New_Node); |
fbf5a39b | 2502 | |
445514c0 EB |
2503 | -- Case where the context is an unchecked conversion to a specific |
2504 | -- integer type. We directly convert from the alignment's type. | |
2505 | ||
2506 | if Nkind (Parent (N)) = N_Unchecked_Type_Conversion then | |
2507 | Rewrite (N, New_Node); | |
2508 | Analyze_And_Resolve (N); | |
2509 | return; | |
2510 | ||
033eaf85 | 2511 | -- Case where the context is a specific integer type with which |
33b9e989 EB |
2512 | -- the original attribute was compatible. But the alignment has a |
2513 | -- specific type in a-tags.ads (Standard.Natural) so, in order to | |
2514 | -- preserve type compatibility, we must convert explicitly. | |
fbf5a39b | 2515 | |
445514c0 | 2516 | elsif Typ /= Standard_Natural then |
fbf5a39b AC |
2517 | New_Node := Convert_To (Typ, New_Node); |
2518 | end if; | |
2519 | ||
2520 | Rewrite (N, New_Node); | |
2521 | Analyze_And_Resolve (N, Typ); | |
2522 | return; | |
2523 | ||
2524 | -- For all other cases, we just have to deal with the case of | |
2525 | -- the fact that the result can be universal. | |
2526 | ||
2527 | else | |
2528 | Apply_Universal_Integer_Attribute_Checks (N); | |
2529 | end if; | |
2530 | end Alignment; | |
2531 | ||
3874e79d EB |
2532 | --------------------------- |
2533 | -- Asm_Input, Asm_Output -- | |
2534 | --------------------------- | |
2535 | ||
2536 | -- The Asm_Input and Asm_Output attributes are not expanded at this | |
2537 | -- stage, but will be eliminated in the expansion of the Asm call, | |
2538 | -- see Exp_Intr for details. So the back end will never see them. | |
2539 | ||
2540 | when Attribute_Asm_Input | |
2541 | | Attribute_Asm_Output | |
2542 | => | |
2543 | null; | |
2544 | ||
47d3b920 AC |
2545 | --------- |
2546 | -- Bit -- | |
2547 | --------- | |
2548 | ||
2549 | -- We compute this if a packed array reference was present, otherwise we | |
2550 | -- leave the computation up to the back end. | |
2551 | ||
2552 | when Attribute_Bit => | |
2553 | if Involves_Packed_Array_Reference (Pref) then | |
2554 | Expand_Packed_Bit_Reference (N); | |
2555 | else | |
2556 | Apply_Universal_Integer_Attribute_Checks (N); | |
2557 | end if; | |
2558 | ||
70482933 RK |
2559 | ------------------ |
2560 | -- Bit_Position -- | |
2561 | ------------------ | |
2562 | ||
12be130c EB |
2563 | -- We leave the computation up to the back end, since we don't know what |
2564 | -- layout will be chosen if no component clause was specified. | |
70482933 | 2565 | |
12be130c EB |
2566 | when Attribute_Bit_Position => |
2567 | Apply_Universal_Integer_Attribute_Checks (N); | |
70482933 RK |
2568 | |
2569 | ------------------ | |
2570 | -- Body_Version -- | |
2571 | ------------------ | |
2572 | ||
2573 | -- A reference to P'Body_Version or P'Version is expanded to | |
2574 | ||
2575 | -- Vnn : Unsigned; | |
69a0c174 | 2576 | -- pragma Import (C, Vnn, "uuuuT"); |
70482933 RK |
2577 | -- ... |
2578 | -- Get_Version_String (Vnn) | |
2579 | ||
2580 | -- where uuuu is the unit name (dots replaced by double underscore) | |
2581 | -- and T is B for the cases of Body_Version, or Version applied to a | |
2582 | -- subprogram acting as its own spec, and S for Version applied to a | |
2583 | -- subprogram spec or package. This sequence of code references the | |
308e6f3a | 2584 | -- unsigned constant created in the main program by the binder. |
70482933 | 2585 | |
5c52bf3b AC |
2586 | -- A special exception occurs for Standard, where the string returned |
2587 | -- is a copy of the library string in gnatvsn.ads. | |
70482933 | 2588 | |
d8f43ee6 HK |
2589 | when Attribute_Body_Version |
2590 | | Attribute_Version | |
2591 | => | |
2592 | Version : declare | |
2593 | E : constant Entity_Id := Make_Temporary (Loc, 'V'); | |
2594 | Pent : Entity_Id; | |
2595 | S : String_Id; | |
70482933 | 2596 | |
d8f43ee6 HK |
2597 | begin |
2598 | -- If not library unit, get to containing library unit | |
2599 | ||
2600 | Pent := Entity (Pref); | |
2601 | while Pent /= Standard_Standard | |
2602 | and then Scope (Pent) /= Standard_Standard | |
2603 | and then not Is_Child_Unit (Pent) | |
2604 | loop | |
2605 | Pent := Scope (Pent); | |
2606 | end loop; | |
70482933 | 2607 | |
d8f43ee6 | 2608 | -- Special case Standard and Standard.ASCII |
70482933 | 2609 | |
d8f43ee6 HK |
2610 | if Pent = Standard_Standard or else Pent = Standard_ASCII then |
2611 | Rewrite (N, | |
2612 | Make_String_Literal (Loc, | |
2613 | Strval => Verbose_Library_Version)); | |
70482933 | 2614 | |
d8f43ee6 | 2615 | -- All other cases |
70482933 | 2616 | |
d8f43ee6 HK |
2617 | else |
2618 | -- Build required string constant | |
70482933 | 2619 | |
d8f43ee6 | 2620 | Get_Name_String (Get_Unit_Name (Pent)); |
70482933 | 2621 | |
d8f43ee6 HK |
2622 | Start_String; |
2623 | for J in 1 .. Name_Len - 2 loop | |
2624 | if Name_Buffer (J) = '.' then | |
2625 | Store_String_Chars ("__"); | |
2626 | else | |
2627 | Store_String_Char (Get_Char_Code (Name_Buffer (J))); | |
2628 | end if; | |
2629 | end loop; | |
70482933 | 2630 | |
d8f43ee6 | 2631 | -- Case of subprogram acting as its own spec, always use body |
70482933 | 2632 | |
d8f43ee6 HK |
2633 | if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification |
2634 | and then Nkind (Parent (Declaration_Node (Pent))) = | |
2635 | N_Subprogram_Body | |
2636 | and then Acts_As_Spec (Parent (Declaration_Node (Pent))) | |
2637 | then | |
2638 | Store_String_Chars ("B"); | |
70482933 | 2639 | |
d8f43ee6 | 2640 | -- Case of no body present, always use spec |
70482933 | 2641 | |
d8f43ee6 HK |
2642 | elsif not Unit_Requires_Body (Pent) then |
2643 | Store_String_Chars ("S"); | |
70482933 | 2644 | |
d8f43ee6 | 2645 | -- Otherwise use B for Body_Version, S for spec |
70482933 | 2646 | |
d8f43ee6 HK |
2647 | elsif Id = Attribute_Body_Version then |
2648 | Store_String_Chars ("B"); | |
2649 | else | |
2650 | Store_String_Chars ("S"); | |
2651 | end if; | |
70482933 | 2652 | |
d8f43ee6 HK |
2653 | S := End_String; |
2654 | Lib.Version_Referenced (S); | |
70482933 | 2655 | |
d8f43ee6 | 2656 | -- Insert the object declaration |
70482933 | 2657 | |
d8f43ee6 HK |
2658 | Insert_Actions (N, New_List ( |
2659 | Make_Object_Declaration (Loc, | |
2660 | Defining_Identifier => E, | |
2661 | Object_Definition => | |
2662 | New_Occurrence_Of (RTE (RE_Unsigned), Loc)))); | |
70482933 | 2663 | |
d8f43ee6 | 2664 | -- Set entity as imported with correct external name |
70482933 | 2665 | |
d8f43ee6 HK |
2666 | Set_Is_Imported (E); |
2667 | Set_Interface_Name (E, Make_String_Literal (Loc, S)); | |
70482933 | 2668 | |
d8f43ee6 HK |
2669 | -- Set entity as internal to ensure proper Sprint output of its |
2670 | -- implicit importation. | |
3e8ee849 | 2671 | |
d8f43ee6 | 2672 | Set_Is_Internal (E); |
3e8ee849 | 2673 | |
d8f43ee6 | 2674 | -- And now rewrite original reference |
70482933 | 2675 | |
d8f43ee6 HK |
2676 | Rewrite (N, |
2677 | Make_Function_Call (Loc, | |
2678 | Name => | |
2679 | New_Occurrence_Of (RTE (RE_Get_Version_String), Loc), | |
2680 | Parameter_Associations => New_List ( | |
2681 | New_Occurrence_Of (E, Loc)))); | |
2682 | end if; | |
70482933 | 2683 | |
d8f43ee6 HK |
2684 | Analyze_And_Resolve (N, RTE (RE_Version_String)); |
2685 | end Version; | |
70482933 RK |
2686 | |
2687 | ------------- | |
2688 | -- Ceiling -- | |
2689 | ------------- | |
2690 | ||
2691 | -- Transforms 'Ceiling into a call to the floating-point attribute | |
2692 | -- function Ceiling in Fat_xxx (where xxx is the root type) | |
2693 | ||
2694 | when Attribute_Ceiling => | |
2695 | Expand_Fpt_Attribute_R (N); | |
2696 | ||
2697 | -------------- | |
2698 | -- Callable -- | |
2699 | -------------- | |
2700 | ||
758c442c | 2701 | -- Transforms 'Callable attribute into a call to the Callable function |
70482933 | 2702 | |
d8f43ee6 | 2703 | when Attribute_Callable => |
99bba92c | 2704 | |
65f01153 RD |
2705 | -- We have an object of a task interface class-wide type as a prefix |
2706 | -- to Callable. Generate: | |
31104818 | 2707 | -- callable (Task_Id (Pref._disp_get_task_id)); |
65f01153 | 2708 | |
0791fbe9 | 2709 | if Ada_Version >= Ada_2005 |
21d27997 RD |
2710 | and then Ekind (Ptyp) = E_Class_Wide_Type |
2711 | and then Is_Interface (Ptyp) | |
2712 | and then Is_Task_Interface (Ptyp) | |
65f01153 | 2713 | then |
99bba92c AC |
2714 | Rewrite (N, |
2715 | Make_Function_Call (Loc, | |
c0e938d0 | 2716 | Name => |
99bba92c AC |
2717 | New_Occurrence_Of (RTE (RE_Callable), Loc), |
2718 | Parameter_Associations => New_List ( | |
2719 | Make_Unchecked_Type_Conversion (Loc, | |
2720 | Subtype_Mark => | |
2721 | New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc), | |
c0e938d0 | 2722 | Expression => Build_Disp_Get_Task_Id_Call (Pref))))); |
31104818 | 2723 | |
65f01153 | 2724 | else |
99bba92c | 2725 | Rewrite (N, Build_Call_With_Task (Pref, RTE (RE_Callable))); |
65f01153 RD |
2726 | end if; |
2727 | ||
70482933 | 2728 | Analyze_And_Resolve (N, Standard_Boolean); |
70482933 RK |
2729 | |
2730 | ------------ | |
2731 | -- Caller -- | |
2732 | ------------ | |
2733 | ||
2734 | -- Transforms 'Caller attribute into a call to either the | |
2735 | -- Task_Entry_Caller or the Protected_Entry_Caller function. | |
2736 | ||
2737 | when Attribute_Caller => Caller : declare | |
b5e792e2 | 2738 | Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id); |
fbf5a39b AC |
2739 | Ent : constant Entity_Id := Entity (Pref); |
2740 | Conctype : constant Entity_Id := Scope (Ent); | |
2741 | Nest_Depth : Integer := 0; | |
70482933 RK |
2742 | Name : Node_Id; |
2743 | S : Entity_Id; | |
2744 | ||
2745 | begin | |
2746 | -- Protected case | |
2747 | ||
2748 | if Is_Protected_Type (Conctype) then | |
e10dab7f JM |
2749 | case Corresponding_Runtime_Package (Conctype) is |
2750 | when System_Tasking_Protected_Objects_Entries => | |
2751 | Name := | |
e4494292 | 2752 | New_Occurrence_Of |
e10dab7f JM |
2753 | (RTE (RE_Protected_Entry_Caller), Loc); |
2754 | ||
2755 | when System_Tasking_Protected_Objects_Single_Entry => | |
2756 | Name := | |
e4494292 | 2757 | New_Occurrence_Of |
e10dab7f JM |
2758 | (RTE (RE_Protected_Single_Entry_Caller), Loc); |
2759 | ||
2760 | when others => | |
2761 | raise Program_Error; | |
2762 | end case; | |
70482933 RK |
2763 | |
2764 | Rewrite (N, | |
2765 | Unchecked_Convert_To (Id_Kind, | |
2766 | Make_Function_Call (Loc, | |
2767 | Name => Name, | |
21d27997 | 2768 | Parameter_Associations => New_List ( |
e4494292 | 2769 | New_Occurrence_Of |
21d27997 | 2770 | (Find_Protection_Object (Current_Scope), Loc))))); |
70482933 RK |
2771 | |
2772 | -- Task case | |
2773 | ||
2774 | else | |
2775 | -- Determine the nesting depth of the E'Caller attribute, that | |
2776 | -- is, how many accept statements are nested within the accept | |
2777 | -- statement for E at the point of E'Caller. The runtime uses | |
2778 | -- this depth to find the specified entry call. | |
2779 | ||
2780 | for J in reverse 0 .. Scope_Stack.Last loop | |
2781 | S := Scope_Stack.Table (J).Entity; | |
2782 | ||
2783 | -- We should not reach the scope of the entry, as it should | |
2784 | -- already have been checked in Sem_Attr that this attribute | |
2785 | -- reference is within a matching accept statement. | |
2786 | ||
2787 | pragma Assert (S /= Conctype); | |
2788 | ||
2789 | if S = Ent then | |
2790 | exit; | |
2791 | ||
2792 | elsif Is_Entry (S) then | |
2793 | Nest_Depth := Nest_Depth + 1; | |
2794 | end if; | |
2795 | end loop; | |
2796 | ||
2797 | Rewrite (N, | |
2798 | Unchecked_Convert_To (Id_Kind, | |
2799 | Make_Function_Call (Loc, | |
21d27997 | 2800 | Name => |
e4494292 | 2801 | New_Occurrence_Of (RTE (RE_Task_Entry_Caller), Loc), |
70482933 RK |
2802 | Parameter_Associations => New_List ( |
2803 | Make_Integer_Literal (Loc, | |
2804 | Intval => Int (Nest_Depth)))))); | |
2805 | end if; | |
2806 | ||
2807 | Analyze_And_Resolve (N, Id_Kind); | |
2808 | end Caller; | |
2809 | ||
3874e79d EB |
2810 | -------------------- |
2811 | -- Component_Size -- | |
2812 | -------------------- | |
2813 | ||
2814 | -- Component_Size is handled by the back end | |
2815 | ||
2816 | when Attribute_Component_Size => | |
2817 | Apply_Universal_Integer_Attribute_Checks (N); | |
2818 | ||
70482933 RK |
2819 | ------------- |
2820 | -- Compose -- | |
2821 | ------------- | |
2822 | ||
2823 | -- Transforms 'Compose into a call to the floating-point attribute | |
2824 | -- function Compose in Fat_xxx (where xxx is the root type) | |
2825 | ||
2826 | -- Note: we strictly should have special code here to deal with the | |
2827 | -- case of absurdly negative arguments (less than Integer'First) | |
2828 | -- which will return a (signed) zero value, but it hardly seems | |
2829 | -- worth the effort. Absurdly large positive arguments will raise | |
2830 | -- constraint error which is fine. | |
2831 | ||
2832 | when Attribute_Compose => | |
2833 | Expand_Fpt_Attribute_RI (N); | |
2834 | ||
2835 | ----------------- | |
2836 | -- Constrained -- | |
2837 | ----------------- | |
2838 | ||
2839 | when Attribute_Constrained => Constrained : declare | |
2840 | Formal_Ent : constant Entity_Id := Param_Entity (Pref); | |
2841 | ||
7ce611e2 ES |
2842 | -- Start of processing for Constrained |
2843 | ||
70482933 RK |
2844 | begin |
2845 | -- Reference to a parameter where the value is passed as an extra | |
2846 | -- actual, corresponding to the extra formal referenced by the | |
fbf5a39b AC |
2847 | -- Extra_Constrained field of the corresponding formal. If this |
2848 | -- is an entry in-parameter, it is replaced by a constant renaming | |
2849 | -- for which Extra_Constrained is never created. | |
70482933 RK |
2850 | |
2851 | if Present (Formal_Ent) | |
fbf5a39b | 2852 | and then Ekind (Formal_Ent) /= E_Constant |
70482933 RK |
2853 | and then Present (Extra_Constrained (Formal_Ent)) |
2854 | then | |
2855 | Rewrite (N, | |
2856 | New_Occurrence_Of | |
2857 | (Extra_Constrained (Formal_Ent), Sloc (N))); | |
2858 | ||
ed323421 AC |
2859 | -- If the prefix is an access to object, the attribute applies to |
2860 | -- the designated object, so rewrite with an explicit dereference. | |
2861 | ||
10e168cd | 2862 | elsif Is_Access_Type (Ptyp) |
ed323421 AC |
2863 | and then |
2864 | (not Is_Entity_Name (Pref) or else Is_Object (Entity (Pref))) | |
2865 | then | |
2866 | Rewrite (Pref, | |
2867 | Make_Explicit_Dereference (Loc, Relocate_Node (Pref))); | |
2868 | Analyze_And_Resolve (N, Standard_Boolean); | |
2869 | return; | |
2870 | ||
70482933 RK |
2871 | -- For variables with a Extra_Constrained field, we use the |
2872 | -- corresponding entity. | |
2873 | ||
2874 | elsif Nkind (Pref) = N_Identifier | |
2875 | and then Ekind (Entity (Pref)) = E_Variable | |
2876 | and then Present (Extra_Constrained (Entity (Pref))) | |
2877 | then | |
2878 | Rewrite (N, | |
2879 | New_Occurrence_Of | |
2880 | (Extra_Constrained (Entity (Pref)), Sloc (N))); | |
2881 | ||
d2880e69 | 2882 | -- For all other cases, we can tell at compile time |
be42aa71 | 2883 | |
d2880e69 CD |
2884 | else |
2885 | -- For access type, apply access check as needed | |
70482933 | 2886 | |
d2880e69 CD |
2887 | if Is_Entity_Name (Pref) |
2888 | and then not Is_Type (Entity (Pref)) | |
2889 | and then Is_Access_Type (Ptyp) | |
2890 | then | |
2891 | Apply_Access_Check (N); | |
2892 | end if; | |
70482933 | 2893 | |
aa720a54 | 2894 | Rewrite (N, |
d2880e69 CD |
2895 | New_Occurrence_Of |
2896 | (Boolean_Literals | |
2897 | (Exp_Util.Attribute_Constrained_Static_Value | |
2898 | (Pref)), Sloc (N))); | |
70482933 RK |
2899 | end if; |
2900 | ||
2901 | Analyze_And_Resolve (N, Standard_Boolean); | |
2902 | end Constrained; | |
2903 | ||
2904 | --------------- | |
2905 | -- Copy_Sign -- | |
2906 | --------------- | |
2907 | ||
2908 | -- Transforms 'Copy_Sign into a call to the floating-point attribute | |
2909 | -- function Copy_Sign in Fat_xxx (where xxx is the root type) | |
2910 | ||
2911 | when Attribute_Copy_Sign => | |
2912 | Expand_Fpt_Attribute_RR (N); | |
2913 | ||
2914 | ----------- | |
2915 | -- Count -- | |
2916 | ----------- | |
2917 | ||
2918 | -- Transforms 'Count attribute into a call to the Count function | |
2919 | ||
21d27997 RD |
2920 | when Attribute_Count => Count : declare |
2921 | Call : Node_Id; | |
2922 | Conctyp : Entity_Id; | |
2923 | Entnam : Node_Id; | |
2924 | Entry_Id : Entity_Id; | |
2925 | Index : Node_Id; | |
2926 | Name : Node_Id; | |
70482933 RK |
2927 | |
2928 | begin | |
2929 | -- If the prefix is a member of an entry family, retrieve both | |
2930 | -- entry name and index. For a simple entry there is no index. | |
2931 | ||
2932 | if Nkind (Pref) = N_Indexed_Component then | |
2933 | Entnam := Prefix (Pref); | |
2934 | Index := First (Expressions (Pref)); | |
2935 | else | |
2936 | Entnam := Pref; | |
2937 | Index := Empty; | |
2938 | end if; | |
2939 | ||
21d27997 RD |
2940 | Entry_Id := Entity (Entnam); |
2941 | ||
70482933 RK |
2942 | -- Find the concurrent type in which this attribute is referenced |
2943 | -- (there had better be one). | |
2944 | ||
2945 | Conctyp := Current_Scope; | |
2946 | while not Is_Concurrent_Type (Conctyp) loop | |
2947 | Conctyp := Scope (Conctyp); | |
2948 | end loop; | |
2949 | ||
2950 | -- Protected case | |
2951 | ||
2952 | if Is_Protected_Type (Conctyp) then | |
97710dc7 JM |
2953 | |
2954 | -- No need to transform 'Count into a function call if the current | |
2955 | -- scope has been eliminated. In this case such transformation is | |
2956 | -- also not viable because the enclosing protected object is not | |
2957 | -- available. | |
2958 | ||
2959 | if Is_Eliminated (Current_Scope) then | |
2960 | return; | |
2961 | end if; | |
2962 | ||
e10dab7f JM |
2963 | case Corresponding_Runtime_Package (Conctyp) is |
2964 | when System_Tasking_Protected_Objects_Entries => | |
e4494292 | 2965 | Name := New_Occurrence_Of (RTE (RE_Protected_Count), Loc); |
e10dab7f JM |
2966 | |
2967 | Call := | |
2968 | Make_Function_Call (Loc, | |
d8f43ee6 | 2969 | Name => Name, |
e10dab7f | 2970 | Parameter_Associations => New_List ( |
e4494292 | 2971 | New_Occurrence_Of |
21d27997 RD |
2972 | (Find_Protection_Object (Current_Scope), Loc), |
2973 | Entry_Index_Expression | |
2974 | (Loc, Entry_Id, Index, Scope (Entry_Id)))); | |
e10dab7f JM |
2975 | |
2976 | when System_Tasking_Protected_Objects_Single_Entry => | |
21d27997 | 2977 | Name := |
e4494292 | 2978 | New_Occurrence_Of (RTE (RE_Protected_Count_Entry), Loc); |
e10dab7f JM |
2979 | |
2980 | Call := | |
2981 | Make_Function_Call (Loc, | |
d8f43ee6 | 2982 | Name => Name, |
e10dab7f | 2983 | Parameter_Associations => New_List ( |
e4494292 | 2984 | New_Occurrence_Of |
21d27997 RD |
2985 | (Find_Protection_Object (Current_Scope), Loc))); |
2986 | ||
e10dab7f JM |
2987 | when others => |
2988 | raise Program_Error; | |
e10dab7f | 2989 | end case; |
70482933 RK |
2990 | |
2991 | -- Task case | |
2992 | ||
2993 | else | |
2994 | Call := | |
2995 | Make_Function_Call (Loc, | |
e4494292 | 2996 | Name => New_Occurrence_Of (RTE (RE_Task_Count), Loc), |
70482933 | 2997 | Parameter_Associations => New_List ( |
21d27997 RD |
2998 | Entry_Index_Expression (Loc, |
2999 | Entry_Id, Index, Scope (Entry_Id)))); | |
70482933 RK |
3000 | end if; |
3001 | ||
3002 | -- The call returns type Natural but the context is universal integer | |
3003 | -- so any integer type is allowed. The attribute was already resolved | |
3004 | -- so its Etype is the required result type. If the base type of the | |
3005 | -- context type is other than Standard.Integer we put in a conversion | |
3006 | -- to the required type. This can be a normal typed conversion since | |
3007 | -- both input and output types of the conversion are integer types | |
3008 | ||
3009 | if Base_Type (Typ) /= Base_Type (Standard_Integer) then | |
3010 | Rewrite (N, Convert_To (Typ, Call)); | |
3011 | else | |
3012 | Rewrite (N, Call); | |
3013 | end if; | |
3014 | ||
3015 | Analyze_And_Resolve (N, Typ); | |
3016 | end Count; | |
3017 | ||
203ddcea AC |
3018 | --------------------- |
3019 | -- Descriptor_Size -- | |
3020 | --------------------- | |
3021 | ||
3874e79d | 3022 | -- Descriptor_Size is handled by the back end |
cb3d8731 | 3023 | |
12be130c EB |
3024 | when Attribute_Descriptor_Size => |
3025 | Apply_Universal_Integer_Attribute_Checks (N); | |
203ddcea | 3026 | |
70482933 RK |
3027 | --------------- |
3028 | -- Elab_Body -- | |
3029 | --------------- | |
3030 | ||
3031 | -- This processing is shared by Elab_Spec | |
3032 | ||
3033 | -- What we do is to insert the following declarations | |
3034 | ||
3035 | -- procedure tnn; | |
3036 | -- pragma Import (C, enn, "name___elabb/s"); | |
3037 | ||
3038 | -- and then the Elab_Body/Spec attribute is replaced by a reference | |
3039 | -- to this defining identifier. | |
3040 | ||
d8f43ee6 HK |
3041 | when Attribute_Elab_Body |
3042 | | Attribute_Elab_Spec | |
3043 | => | |
3f5a8fee | 3044 | -- Leave attribute unexpanded in CodePeer mode: the gnat2scil |
2c1a2cf3 | 3045 | -- back-end knows how to handle these attributes directly. |
3f5a8fee | 3046 | |
2c1a2cf3 | 3047 | if CodePeer_Mode then |
3f5a8fee AC |
3048 | return; |
3049 | end if; | |
3050 | ||
70482933 | 3051 | Elab_Body : declare |
191fcb3a | 3052 | Ent : constant Entity_Id := Make_Temporary (Loc, 'E'); |
70482933 RK |
3053 | Str : String_Id; |
3054 | Lang : Node_Id; | |
3055 | ||
3056 | procedure Make_Elab_String (Nod : Node_Id); | |
3057 | -- Given Nod, an identifier, or a selected component, put the | |
3058 | -- image into the current string literal, with double underline | |
3059 | -- between components. | |
3060 | ||
7ce611e2 ES |
3061 | ---------------------- |
3062 | -- Make_Elab_String -- | |
3063 | ---------------------- | |
3064 | ||
70482933 RK |
3065 | procedure Make_Elab_String (Nod : Node_Id) is |
3066 | begin | |
3067 | if Nkind (Nod) = N_Selected_Component then | |
3068 | Make_Elab_String (Prefix (Nod)); | |
535a8637 AC |
3069 | Store_String_Char ('_'); |
3070 | Store_String_Char ('_'); | |
70482933 RK |
3071 | Get_Name_String (Chars (Selector_Name (Nod))); |
3072 | ||
3073 | else | |
3074 | pragma Assert (Nkind (Nod) = N_Identifier); | |
3075 | Get_Name_String (Chars (Nod)); | |
3076 | end if; | |
3077 | ||
3078 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
3079 | end Make_Elab_String; | |
3080 | ||
3081 | -- Start of processing for Elab_Body/Elab_Spec | |
3082 | ||
3083 | begin | |
3084 | -- First we need to prepare the string literal for the name of | |
3085 | -- the elaboration routine to be referenced. | |
3086 | ||
3087 | Start_String; | |
3088 | Make_Elab_String (Pref); | |
535a8637 AC |
3089 | Store_String_Chars ("___elab"); |
3090 | Lang := Make_Identifier (Loc, Name_C); | |
70482933 RK |
3091 | |
3092 | if Id = Attribute_Elab_Body then | |
3093 | Store_String_Char ('b'); | |
3094 | else | |
3095 | Store_String_Char ('s'); | |
3096 | end if; | |
3097 | ||
3098 | Str := End_String; | |
3099 | ||
3100 | Insert_Actions (N, New_List ( | |
3101 | Make_Subprogram_Declaration (Loc, | |
3102 | Specification => | |
3103 | Make_Procedure_Specification (Loc, | |
3104 | Defining_Unit_Name => Ent)), | |
3105 | ||
3106 | Make_Pragma (Loc, | |
3860d469 | 3107 | Chars => Name_Import, |
70482933 | 3108 | Pragma_Argument_Associations => New_List ( |
7675ad4f | 3109 | Make_Pragma_Argument_Association (Loc, Expression => Lang), |
70482933 RK |
3110 | |
3111 | Make_Pragma_Argument_Association (Loc, | |
7675ad4f | 3112 | Expression => Make_Identifier (Loc, Chars (Ent))), |
70482933 RK |
3113 | |
3114 | Make_Pragma_Argument_Association (Loc, | |
7675ad4f | 3115 | Expression => Make_String_Literal (Loc, Str)))))); |
70482933 RK |
3116 | |
3117 | Set_Entity (N, Ent); | |
3118 | Rewrite (N, New_Occurrence_Of (Ent, Loc)); | |
3119 | end Elab_Body; | |
3120 | ||
2c1a2cf3 RD |
3121 | -------------------- |
3122 | -- Elab_Subp_Body -- | |
3123 | -------------------- | |
3124 | ||
3125 | -- Always ignored. In CodePeer mode, gnat2scil knows how to handle | |
3126 | -- this attribute directly, and if we are not in CodePeer mode it is | |
3127 | -- entirely ignored ??? | |
3128 | ||
3129 | when Attribute_Elab_Subp_Body => | |
3130 | return; | |
3131 | ||
70482933 RK |
3132 | ---------------- |
3133 | -- Elaborated -- | |
3134 | ---------------- | |
3135 | ||
21d27997 RD |
3136 | -- Elaborated is always True for preelaborated units, predefined units, |
3137 | -- pure units and units which have Elaborate_Body pragmas. These units | |
3138 | -- have no elaboration entity. | |
70482933 | 3139 | |
21d27997 | 3140 | -- Note: The Elaborated attribute is never passed to the back end |
70482933 RK |
3141 | |
3142 | when Attribute_Elaborated => Elaborated : declare | |
7327f5c2 | 3143 | Elab_Id : constant Entity_Id := Elaboration_Entity (Entity (Pref)); |
70482933 RK |
3144 | |
3145 | begin | |
7327f5c2 | 3146 | if Present (Elab_Id) then |
70482933 | 3147 | Rewrite (N, |
824e9320 | 3148 | Make_Op_Ne (Loc, |
7327f5c2 AC |
3149 | Left_Opnd => New_Occurrence_Of (Elab_Id, Loc), |
3150 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))); | |
3151 | ||
824e9320 | 3152 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
3153 | else |
3154 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
3155 | end if; | |
3156 | end Elaborated; | |
3157 | ||
3158 | -------------- | |
3159 | -- Enum_Rep -- | |
3160 | -------------- | |
3161 | ||
1956beb8 BD |
3162 | when Attribute_Enum_Rep => Enum_Rep : declare |
3163 | Expr : Node_Id; | |
75e4e36d | 3164 | |
70482933 | 3165 | begin |
75e4e36d AC |
3166 | -- Get the expression, which is X for Enum_Type'Enum_Rep (X) or |
3167 | -- X'Enum_Rep. | |
70482933 RK |
3168 | |
3169 | if Is_Non_Empty_List (Exprs) then | |
1956beb8 BD |
3170 | Expr := First (Exprs); |
3171 | else | |
3172 | Expr := Pref; | |
3173 | end if; | |
70482933 | 3174 | |
c64ac479 PT |
3175 | -- If not constant-folded, Enum_Type'Enum_Rep (X) or X'Enum_Rep |
3176 | -- expands to | |
fbf5a39b | 3177 | |
1956beb8 BD |
3178 | -- target-type (X) |
3179 | ||
3180 | -- This is simply a direct conversion from the enumeration type to | |
3181 | -- the target integer type, which is treated by the back end as a | |
3182 | -- normal integer conversion, treating the enumeration type as an | |
3183 | -- integer, which is exactly what we want. We set Conversion_OK to | |
3184 | -- make sure that the analyzer does not complain about what otherwise | |
3185 | -- might be an illegal conversion. | |
70482933 | 3186 | |
10e168cd EB |
3187 | -- However the target type is universal integer in most cases, which |
3188 | -- is a very large type, so in the case of an enumeration type, we | |
3189 | -- first convert to a small signed integer type in order not to lose | |
3190 | -- the size information. | |
3191 | ||
c64ac479 | 3192 | if Is_Enumeration_Type (Ptyp) then |
f193b29e | 3193 | Rewrite (N, OK_Convert_To (Get_Integer_Type (Ptyp), Expr)); |
10e168cd EB |
3194 | Convert_To_And_Rewrite (Typ, N); |
3195 | ||
70482933 | 3196 | else |
10e168cd | 3197 | Rewrite (N, OK_Convert_To (Typ, Expr)); |
70482933 RK |
3198 | end if; |
3199 | ||
70482933 | 3200 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
3201 | end Enum_Rep; |
3202 | ||
21d27997 RD |
3203 | -------------- |
3204 | -- Enum_Val -- | |
3205 | -------------- | |
3206 | ||
3207 | when Attribute_Enum_Val => Enum_Val : declare | |
3208 | Expr : Node_Id; | |
3209 | Btyp : constant Entity_Id := Base_Type (Ptyp); | |
3210 | ||
3211 | begin | |
3212 | -- X'Enum_Val (Y) expands to | |
3213 | ||
3214 | -- [constraint_error when _rep_to_pos (Y, False) = -1, msg] | |
3215 | -- X!(Y); | |
3216 | ||
3217 | Expr := Unchecked_Convert_To (Ptyp, First (Exprs)); | |
3218 | ||
fd90c808 EB |
3219 | -- Ensure that the expression is not truncated since the "bad" bits |
3220 | -- are desired. | |
3221 | ||
3222 | if Nkind (Expr) = N_Unchecked_Type_Conversion then | |
3223 | Set_No_Truncation (Expr); | |
3224 | end if; | |
3225 | ||
21d27997 RD |
3226 | Insert_Action (N, |
3227 | Make_Raise_Constraint_Error (Loc, | |
3228 | Condition => | |
3229 | Make_Op_Eq (Loc, | |
3230 | Left_Opnd => | |
3231 | Make_Function_Call (Loc, | |
3232 | Name => | |
e4494292 | 3233 | New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc), |
21d27997 RD |
3234 | Parameter_Associations => New_List ( |
3235 | Relocate_Node (Duplicate_Subexpr (Expr)), | |
3236 | New_Occurrence_Of (Standard_False, Loc))), | |
3237 | ||
3238 | Right_Opnd => Make_Integer_Literal (Loc, -1)), | |
3239 | Reason => CE_Range_Check_Failed)); | |
3240 | ||
3241 | Rewrite (N, Expr); | |
3242 | Analyze_And_Resolve (N, Ptyp); | |
3243 | end Enum_Val; | |
3244 | ||
70482933 RK |
3245 | -------------- |
3246 | -- Exponent -- | |
3247 | -------------- | |
3248 | ||
3249 | -- Transforms 'Exponent into a call to the floating-point attribute | |
3250 | -- function Exponent in Fat_xxx (where xxx is the root type) | |
3251 | ||
3252 | when Attribute_Exponent => | |
3253 | Expand_Fpt_Attribute_R (N); | |
3254 | ||
3255 | ------------------ | |
3256 | -- External_Tag -- | |
3257 | ------------------ | |
3258 | ||
3259 | -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag) | |
3260 | ||
d8f43ee6 | 3261 | when Attribute_External_Tag => |
70482933 RK |
3262 | Rewrite (N, |
3263 | Make_Function_Call (Loc, | |
d8f43ee6 HK |
3264 | Name => |
3265 | New_Occurrence_Of (RTE (RE_External_Tag), Loc), | |
70482933 RK |
3266 | Parameter_Associations => New_List ( |
3267 | Make_Attribute_Reference (Loc, | |
3268 | Attribute_Name => Name_Tag, | |
d8f43ee6 | 3269 | Prefix => Prefix (N))))); |
70482933 RK |
3270 | |
3271 | Analyze_And_Resolve (N, Standard_String); | |
70482933 | 3272 | |
f68d3344 JS |
3273 | ----------------------- |
3274 | -- Finalization_Size -- | |
3275 | ----------------------- | |
3276 | ||
3277 | when Attribute_Finalization_Size => Finalization_Size : declare | |
f68d3344 | 3278 | function Calculate_Header_Size return Node_Id; |
d9c59db4 AC |
3279 | -- Generate a runtime call to calculate the size of the hidden header |
3280 | -- along with any added padding which would precede a heap-allocated | |
3281 | -- object of the prefix type. | |
f68d3344 JS |
3282 | |
3283 | --------------------------- | |
3284 | -- Calculate_Header_Size -- | |
3285 | --------------------------- | |
3286 | ||
3287 | function Calculate_Header_Size return Node_Id is | |
3288 | begin | |
3289 | -- Generate: | |
10e168cd | 3290 | -- Typ (Header_Size_With_Padding (Pref'Alignment)) |
f68d3344 JS |
3291 | |
3292 | return | |
10e168cd | 3293 | Convert_To (Typ, |
f68d3344 JS |
3294 | Make_Function_Call (Loc, |
3295 | Name => | |
d9c59db4 AC |
3296 | New_Occurrence_Of (RTE (RE_Header_Size_With_Padding), Loc), |
3297 | ||
f68d3344 JS |
3298 | Parameter_Associations => New_List ( |
3299 | Make_Attribute_Reference (Loc, | |
d9c59db4 | 3300 | Prefix => New_Copy_Tree (Pref), |
f68d3344 JS |
3301 | Attribute_Name => Name_Alignment)))); |
3302 | end Calculate_Header_Size; | |
3303 | ||
d9c59db4 | 3304 | -- Local variables |
f68d3344 | 3305 | |
d9c59db4 | 3306 | Size : Entity_Id; |
f68d3344 JS |
3307 | |
3308 | -- Start of Finalization_Size | |
3309 | ||
3310 | begin | |
d9c59db4 | 3311 | -- An object of a class-wide type first requires a runtime check to |
f68d3344 JS |
3312 | -- determine whether it is actually controlled or not. Depending on |
3313 | -- the outcome of this check, the Finalization_Size of the object | |
3314 | -- may be zero or some positive value. | |
3315 | -- | |
d9c59db4 | 3316 | -- In this scenario, Pref'Finalization_Size is expanded into |
f68d3344 | 3317 | -- |
d9c59db4 | 3318 | -- Size : Integer := 0; |
f68d3344 | 3319 | -- |
d9c59db4 | 3320 | -- if Needs_Finalization (Pref'Tag) then |
10e168cd | 3321 | -- Size := Integer (Header_Size_With_Padding (Pref'Alignment)); |
d9c59db4 | 3322 | -- end if; |
f68d3344 JS |
3323 | -- |
3324 | -- and the attribute reference is replaced with a reference to Size. | |
3325 | ||
3326 | if Is_Class_Wide_Type (Ptyp) then | |
d9c59db4 AC |
3327 | Size := Make_Temporary (Loc, 'S'); |
3328 | ||
f68d3344 JS |
3329 | Insert_Actions (N, New_List ( |
3330 | ||
3331 | -- Generate: | |
3332 | -- Size : Integer := 0; | |
3333 | ||
3334 | Make_Object_Declaration (Loc, | |
3335 | Defining_Identifier => Size, | |
3336 | Object_Definition => | |
3337 | New_Occurrence_Of (Standard_Integer, Loc), | |
3338 | Expression => Make_Integer_Literal (Loc, 0)), | |
3339 | ||
3340 | -- Generate: | |
3341 | -- if Needs_Finalization (Pref'Tag) then | |
d9c59db4 | 3342 | -- Size := |
10e168cd | 3343 | -- Integer (Header_Size_With_Padding (Pref'Alignment)); |
f68d3344 JS |
3344 | -- end if; |
3345 | ||
3346 | Make_If_Statement (Loc, | |
3347 | Condition => | |
3348 | Make_Function_Call (Loc, | |
3349 | Name => | |
d9c59db4 AC |
3350 | New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc), |
3351 | ||
f68d3344 JS |
3352 | Parameter_Associations => New_List ( |
3353 | Make_Attribute_Reference (Loc, | |
d9c59db4 AC |
3354 | Prefix => New_Copy_Tree (Pref), |
3355 | Attribute_Name => Name_Tag))), | |
3356 | ||
f68d3344 JS |
3357 | Then_Statements => New_List ( |
3358 | Make_Assignment_Statement (Loc, | |
3359 | Name => New_Occurrence_Of (Size, Loc), | |
10e168cd EB |
3360 | Expression => |
3361 | Convert_To | |
3362 | (Standard_Integer, Calculate_Header_Size)))))); | |
f68d3344 JS |
3363 | |
3364 | Rewrite (N, New_Occurrence_Of (Size, Loc)); | |
3365 | ||
d9c59db4 AC |
3366 | -- The prefix is known to be controlled at compile time. Calculate |
3367 | -- Finalization_Size by calling function Header_Size_With_Padding. | |
f68d3344 JS |
3368 | |
3369 | elsif Needs_Finalization (Ptyp) then | |
3370 | Rewrite (N, Calculate_Header_Size); | |
3371 | ||
d9c59db4 AC |
3372 | -- The prefix is not an object with controlled parts, so its |
3373 | -- Finalization_Size is zero. | |
f68d3344 JS |
3374 | |
3375 | else | |
3376 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
3377 | end if; | |
3378 | ||
5f325af2 AC |
3379 | -- Due to cases where the entity type of the attribute is already |
3380 | -- resolved the rewritten N must get re-resolved to its appropriate | |
3381 | -- type. | |
3382 | ||
3383 | Analyze_And_Resolve (N, Typ); | |
f68d3344 JS |
3384 | end Finalization_Size; |
3385 | ||
ac8806c4 EB |
3386 | ----------------- |
3387 | -- First, Last -- | |
3388 | ----------------- | |
70482933 | 3389 | |
ac8806c4 EB |
3390 | when Attribute_First |
3391 | | Attribute_Last | |
3392 | => | |
70482933 | 3393 | -- If the prefix type is a constrained packed array type which |
8ca597af | 3394 | -- already has a Packed_Array_Impl_Type representation defined, then |
ac8806c4 EB |
3395 | -- replace this attribute with a direct reference to the attribute of |
3396 | -- the appropriate index subtype (since otherwise the back end will | |
3397 | -- try to give us the value of 'First for this implementation type). | |
70482933 RK |
3398 | |
3399 | if Is_Constrained_Packed_Array (Ptyp) then | |
3400 | Rewrite (N, | |
3401 | Make_Attribute_Reference (Loc, | |
ac8806c4 | 3402 | Attribute_Name => Attribute_Name (N), |
41a58113 RD |
3403 | Prefix => |
3404 | New_Occurrence_Of (Get_Index_Subtype (N), Loc))); | |
70482933 RK |
3405 | Analyze_And_Resolve (N, Typ); |
3406 | ||
ac8806c4 EB |
3407 | -- For a constrained array type, if the bound is a reference to an |
3408 | -- entity which is not a discriminant, just replace with a direct | |
3409 | -- reference. Note that this must be in keeping with what is done | |
3410 | -- for scalar types in order for range checks to be elided in loops. | |
3411 | ||
d27ec3f6 EB |
3412 | -- However, avoid doing it if the array type is public because, in |
3413 | -- this case, we effectively rely on the back end to create public | |
3414 | -- symbols with consistent names across units for the array bounds. | |
3415 | ||
3416 | elsif Is_Array_Type (Ptyp) | |
3417 | and then Is_Constrained (Ptyp) | |
3418 | and then not Is_Public (Ptyp) | |
3419 | then | |
ac8806c4 EB |
3420 | declare |
3421 | Bnd : Node_Id; | |
3422 | ||
3423 | begin | |
3424 | if Id = Attribute_First then | |
3425 | Bnd := Type_Low_Bound (Get_Index_Subtype (N)); | |
3426 | else | |
3427 | Bnd := Type_High_Bound (Get_Index_Subtype (N)); | |
3428 | end if; | |
3429 | ||
3430 | if Is_Entity_Name (Bnd) | |
3431 | and then Ekind (Entity (Bnd)) /= E_Discriminant | |
3432 | then | |
3433 | Rewrite (N, New_Occurrence_Of (Entity (Bnd), Loc)); | |
3434 | end if; | |
3435 | end; | |
3436 | ||
41a58113 RD |
3437 | -- For access type, apply access check as needed |
3438 | ||
70482933 RK |
3439 | elsif Is_Access_Type (Ptyp) then |
3440 | Apply_Access_Check (N); | |
41a58113 | 3441 | |
ac8806c4 | 3442 | -- For scalar type, if the bound is a reference to an entity, just |
41a58113 RD |
3443 | -- replace with a direct reference. Note that we can only have a |
3444 | -- reference to a constant entity at this stage, anything else would | |
8e888920 | 3445 | -- have already been rewritten. |
41a58113 | 3446 | |
8e888920 | 3447 | elsif Is_Scalar_Type (Ptyp) then |
41a58113 | 3448 | declare |
ac8806c4 EB |
3449 | Bnd : Node_Id; |
3450 | ||
41a58113 | 3451 | begin |
ac8806c4 EB |
3452 | if Id = Attribute_First then |
3453 | Bnd := Type_Low_Bound (Ptyp); | |
3454 | else | |
3455 | Bnd := Type_High_Bound (Ptyp); | |
3456 | end if; | |
3457 | ||
cb52e9fe | 3458 | if Is_Entity_Name (Bnd) then |
ac8806c4 | 3459 | Rewrite (N, New_Occurrence_Of (Entity (Bnd), Loc)); |
41a58113 RD |
3460 | end if; |
3461 | end; | |
70482933 | 3462 | end if; |
70482933 RK |
3463 | |
3464 | --------------- | |
3465 | -- First_Bit -- | |
3466 | --------------- | |
3467 | ||
12be130c EB |
3468 | -- We leave the computation up to the back end, since we don't know what |
3469 | -- layout will be chosen if no component clause was specified. | |
70482933 | 3470 | |
12be130c EB |
3471 | when Attribute_First_Bit => |
3472 | Apply_Universal_Integer_Attribute_Checks (N); | |
70482933 | 3473 | |
304757d2 AC |
3474 | -------------------------------- |
3475 | -- Fixed_Value, Integer_Value -- | |
3476 | -------------------------------- | |
70482933 | 3477 | |
304757d2 | 3478 | -- We transform |
70482933 RK |
3479 | |
3480 | -- fixtype'Fixed_Value (integer-value) | |
8113b0c7 | 3481 | -- inttype'Integer_Value (fixed-value) |
70482933 RK |
3482 | |
3483 | -- into | |
3484 | ||
304757d2 AC |
3485 | -- fixtype (integer-value) |
3486 | -- inttype (fixed-value) | |
3487 | ||
3488 | -- respectively. | |
70482933 | 3489 | |
8113b0c7 EB |
3490 | -- We set Conversion_OK on the conversion because we do not want it |
3491 | -- to go through the fixed-point conversion circuits. | |
70482933 | 3492 | |
304757d2 AC |
3493 | when Attribute_Fixed_Value |
3494 | | Attribute_Integer_Value | |
3495 | => | |
8113b0c7 | 3496 | Rewrite (N, OK_Convert_To (Entity (Pref), First (Exprs))); |
fbf5a39b | 3497 | |
8113b0c7 | 3498 | -- Note that it might appear that a properly analyzed unchecked |
d8f43ee6 | 3499 | -- conversion would be just fine here, but that's not the case, |
8113b0c7 | 3500 | -- since the full range checks performed by the following calls |
d8f43ee6 | 3501 | -- are critical. |
fbf5a39b | 3502 | |
8113b0c7 EB |
3503 | Apply_Type_Conversion_Checks (N); |
3504 | ||
3505 | -- Note that Apply_Type_Conversion_Checks only deals with the | |
3506 | -- overflow checks on conversions involving fixed-point types | |
3507 | -- so we must apply range checks manually on them and expand. | |
3508 | ||
3509 | Apply_Scalar_Range_Check | |
3510 | (Expression (N), Etype (N), Fixed_Int => True); | |
3511 | ||
3512 | Set_Analyzed (N); | |
3513 | Expand (N); | |
70482933 RK |
3514 | |
3515 | ----------- | |
3516 | -- Floor -- | |
3517 | ----------- | |
3518 | ||
3519 | -- Transforms 'Floor into a call to the floating-point attribute | |
3520 | -- function Floor in Fat_xxx (where xxx is the root type) | |
3521 | ||
3522 | when Attribute_Floor => | |
3523 | Expand_Fpt_Attribute_R (N); | |
3524 | ||
3525 | ---------- | |
3526 | -- Fore -- | |
3527 | ---------- | |
3528 | ||
3529 | -- For the fixed-point type Typ: | |
3530 | ||
3531 | -- Typ'Fore | |
3532 | ||
3533 | -- expands into | |
3534 | ||
65f01153 RD |
3535 | -- Result_Type (System.Fore (Universal_Real (Type'First)), |
3536 | -- Universal_Real (Type'Last)) | |
70482933 | 3537 | |
d39f6b24 YM |
3538 | -- Note that we know that the type is a nonstatic subtype, or Fore would |
3539 | -- have itself been computed dynamically in Eval_Attribute. | |
70482933 | 3540 | |
d8f43ee6 | 3541 | when Attribute_Fore => |
70482933 RK |
3542 | Rewrite (N, |
3543 | Convert_To (Typ, | |
3544 | Make_Function_Call (Loc, | |
d8f43ee6 HK |
3545 | Name => |
3546 | New_Occurrence_Of (RTE (RE_Fore), Loc), | |
70482933 RK |
3547 | |
3548 | Parameter_Associations => New_List ( | |
65f01153 | 3549 | Convert_To (Universal_Real, |
70482933 | 3550 | Make_Attribute_Reference (Loc, |
d8f43ee6 | 3551 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
70482933 RK |
3552 | Attribute_Name => Name_First)), |
3553 | ||
65f01153 | 3554 | Convert_To (Universal_Real, |
70482933 | 3555 | Make_Attribute_Reference (Loc, |
d8f43ee6 | 3556 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
70482933 RK |
3557 | Attribute_Name => Name_Last)))))); |
3558 | ||
3559 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
3560 | |
3561 | -------------- | |
3562 | -- Fraction -- | |
3563 | -------------- | |
3564 | ||
3565 | -- Transforms 'Fraction into a call to the floating-point attribute | |
3566 | -- function Fraction in Fat_xxx (where xxx is the root type) | |
3567 | ||
3568 | when Attribute_Fraction => | |
3569 | Expand_Fpt_Attribute_R (N); | |
3570 | ||
54838d1f AC |
3571 | -------------- |
3572 | -- From_Any -- | |
3573 | -------------- | |
3574 | ||
3575 | when Attribute_From_Any => From_Any : declare | |
54838d1f | 3576 | Decls : constant List_Id := New_List; |
d8f43ee6 | 3577 | |
54838d1f AC |
3578 | begin |
3579 | Rewrite (N, | |
10e168cd | 3580 | Build_From_Any_Call (Ptyp, |
54838d1f AC |
3581 | Relocate_Node (First (Exprs)), |
3582 | Decls)); | |
3583 | Insert_Actions (N, Decls); | |
10e168cd | 3584 | Analyze_And_Resolve (N, Ptyp); |
54838d1f AC |
3585 | end From_Any; |
3586 | ||
ea70f3d0 RD |
3587 | ---------------------- |
3588 | -- Has_Same_Storage -- | |
3589 | ---------------------- | |
3590 | ||
3591 | when Attribute_Has_Same_Storage => Has_Same_Storage : declare | |
d8f43ee6 | 3592 | Loc : constant Source_Ptr := Sloc (N); |
ea70f3d0 | 3593 | |
d8f43ee6 HK |
3594 | X : constant Node_Id := Prefix (N); |
3595 | Y : constant Node_Id := First (Expressions (N)); | |
3596 | -- The arguments | |
ea70f3d0 | 3597 | |
d8f43ee6 HK |
3598 | X_Addr : Node_Id; |
3599 | Y_Addr : Node_Id; | |
3600 | -- Rhe expressions for their addresses | |
ea70f3d0 | 3601 | |
d8f43ee6 HK |
3602 | X_Size : Node_Id; |
3603 | Y_Size : Node_Id; | |
3604 | -- Rhe expressions for their sizes | |
ea70f3d0 RD |
3605 | |
3606 | begin | |
3607 | -- The attribute is expanded as: | |
3608 | ||
3609 | -- (X'address = Y'address) | |
3610 | -- and then (X'Size = Y'Size) | |
36cf595c | 3611 | -- and then (X'Size /= 0) (AI12-0077) |
ea70f3d0 RD |
3612 | |
3613 | -- If both arguments have the same Etype the second conjunct can be | |
3614 | -- omitted. | |
3615 | ||
3616 | X_Addr := | |
3617 | Make_Attribute_Reference (Loc, | |
d8f43ee6 HK |
3618 | Attribute_Name => Name_Address, |
3619 | Prefix => New_Copy_Tree (X)); | |
ea70f3d0 RD |
3620 | |
3621 | Y_Addr := | |
3622 | Make_Attribute_Reference (Loc, | |
d8f43ee6 HK |
3623 | Attribute_Name => Name_Address, |
3624 | Prefix => New_Copy_Tree (Y)); | |
ea70f3d0 RD |
3625 | |
3626 | X_Size := | |
3627 | Make_Attribute_Reference (Loc, | |
d8f43ee6 HK |
3628 | Attribute_Name => Name_Size, |
3629 | Prefix => New_Copy_Tree (X)); | |
ea70f3d0 | 3630 | |
ea70f3d0 RD |
3631 | if Etype (X) = Etype (Y) then |
3632 | Rewrite (N, | |
36cf595c EB |
3633 | Make_And_Then (Loc, |
3634 | Left_Opnd => | |
3635 | Make_Op_Eq (Loc, | |
3636 | Left_Opnd => X_Addr, | |
3637 | Right_Opnd => Y_Addr), | |
3638 | Right_Opnd => | |
3639 | Make_Op_Ne (Loc, | |
3640 | Left_Opnd => X_Size, | |
3641 | Right_Opnd => Make_Integer_Literal (Loc, 0)))); | |
ea70f3d0 | 3642 | else |
36cf595c EB |
3643 | Y_Size := |
3644 | Make_Attribute_Reference (Loc, | |
3645 | Attribute_Name => Name_Size, | |
3646 | Prefix => New_Copy_Tree (Y)); | |
3647 | ||
ea70f3d0 | 3648 | Rewrite (N, |
36cf595c | 3649 | Make_And_Then (Loc, |
d8f43ee6 HK |
3650 | Left_Opnd => |
3651 | Make_Op_Eq (Loc, | |
3652 | Left_Opnd => X_Addr, | |
3653 | Right_Opnd => Y_Addr), | |
3654 | Right_Opnd => | |
36cf595c EB |
3655 | Make_And_Then (Loc, |
3656 | Left_Opnd => | |
3657 | Make_Op_Eq (Loc, | |
3658 | Left_Opnd => X_Size, | |
3659 | Right_Opnd => Y_Size), | |
3660 | Right_Opnd => | |
3661 | Make_Op_Ne (Loc, | |
3662 | Left_Opnd => New_Copy_Tree (X_Size), | |
3663 | Right_Opnd => Make_Integer_Literal (Loc, 0))))); | |
ea70f3d0 RD |
3664 | end if; |
3665 | ||
3666 | Analyze_And_Resolve (N, Standard_Boolean); | |
3667 | end Has_Same_Storage; | |
3668 | ||
70482933 RK |
3669 | -------------- |
3670 | -- Identity -- | |
3671 | -------------- | |
3672 | ||
3673 | -- For an exception returns a reference to the exception data: | |
3674 | -- Exception_Id!(Prefix'Reference) | |
3675 | ||
3676 | -- For a task it returns a reference to the _task_id component of | |
3677 | -- corresponding record: | |
3678 | ||
b5e792e2 | 3679 | -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined |
70482933 | 3680 | |
758c442c | 3681 | -- in Ada.Task_Identification |
70482933 RK |
3682 | |
3683 | when Attribute_Identity => Identity : declare | |
3684 | Id_Kind : Entity_Id; | |
3685 | ||
3686 | begin | |
21d27997 | 3687 | if Ptyp = Standard_Exception_Type then |
70482933 RK |
3688 | Id_Kind := RTE (RE_Exception_Id); |
3689 | ||
3690 | if Present (Renamed_Object (Entity (Pref))) then | |
3691 | Set_Entity (Pref, Renamed_Object (Entity (Pref))); | |
3692 | end if; | |
3693 | ||
3694 | Rewrite (N, | |
3695 | Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref))); | |
3696 | else | |
b5e792e2 | 3697 | Id_Kind := RTE (RO_AT_Task_Id); |
70482933 | 3698 | |
470cd9e9 RD |
3699 | -- If the prefix is a task interface, the Task_Id is obtained |
3700 | -- dynamically through a dispatching call, as for other task | |
3701 | -- attributes applied to interfaces. | |
3702 | ||
0791fbe9 | 3703 | if Ada_Version >= Ada_2005 |
21d27997 RD |
3704 | and then Ekind (Ptyp) = E_Class_Wide_Type |
3705 | and then Is_Interface (Ptyp) | |
3706 | and then Is_Task_Interface (Ptyp) | |
470cd9e9 | 3707 | then |
c0e938d0 AC |
3708 | Rewrite (N, |
3709 | Unchecked_Convert_To | |
3710 | (Id_Kind, Build_Disp_Get_Task_Id_Call (Pref))); | |
470cd9e9 RD |
3711 | |
3712 | else | |
3713 | Rewrite (N, | |
3714 | Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref))); | |
3715 | end if; | |
70482933 RK |
3716 | end if; |
3717 | ||
3718 | Analyze_And_Resolve (N, Id_Kind); | |
3719 | end Identity; | |
3720 | ||
3721 | ----------- | |
3722 | -- Image -- | |
3723 | ----------- | |
3724 | ||
70482933 | 3725 | when Attribute_Image => |
643827e9 | 3726 | |
b63d61f7 AC |
3727 | -- Leave attribute unexpanded in CodePeer mode: the gnat2scil |
3728 | -- back-end knows how to handle this attribute directly. | |
3729 | ||
3730 | if CodePeer_Mode then | |
3731 | return; | |
3732 | end if; | |
3733 | ||
e0fd1b9c | 3734 | Exp_Imgv.Expand_Image_Attribute (N); |
70482933 RK |
3735 | |
3736 | --------- | |
3737 | -- Img -- | |
3738 | --------- | |
3739 | ||
3740 | -- X'Img is expanded to typ'Image (X), where typ is the type of X | |
3741 | ||
d8f43ee6 | 3742 | when Attribute_Img => |
e0fd1b9c | 3743 | Exp_Imgv.Expand_Image_Attribute (N); |
70482933 | 3744 | |
e577151d PT |
3745 | ----------------- |
3746 | -- Initialized -- | |
3747 | ----------------- | |
3748 | ||
3749 | -- For execution, we could either implement an approximation of this | |
3750 | -- aspect, or use Valid_Scalars as a first approximation. For now we do | |
3751 | -- the latter. | |
3752 | ||
3753 | when Attribute_Initialized => | |
3754 | Rewrite | |
3755 | (N, | |
3756 | Make_Attribute_Reference | |
3757 | (Sloc => Loc, | |
3758 | Prefix => Pref, | |
3759 | Attribute_Name => Name_Valid_Scalars, | |
3760 | Expressions => Exprs)); | |
3761 | ||
3762 | Analyze_And_Resolve (N); | |
3763 | ||
70482933 RK |
3764 | ----------- |
3765 | -- Input -- | |
3766 | ----------- | |
3767 | ||
3768 | when Attribute_Input => Input : declare | |
3769 | P_Type : constant Entity_Id := Entity (Pref); | |
3770 | B_Type : constant Entity_Id := Base_Type (P_Type); | |
3771 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
3772 | Strm : constant Node_Id := First (Exprs); | |
3773 | Fname : Entity_Id; | |
3774 | Decl : Node_Id; | |
3775 | Call : Node_Id; | |
3776 | Prag : Node_Id; | |
3777 | Arg2 : Node_Id; | |
3778 | Rfunc : Node_Id; | |
3779 | ||
3780 | Cntrl : Node_Id := Empty; | |
3781 | -- Value for controlling argument in call. Always Empty except in | |
3782 | -- the dispatching (class-wide type) case, where it is a reference | |
3783 | -- to the dummy object initialized to the right internal tag. | |
3784 | ||
1c6c6771 ES |
3785 | procedure Freeze_Stream_Subprogram (F : Entity_Id); |
3786 | -- The expansion of the attribute reference may generate a call to | |
3787 | -- a user-defined stream subprogram that is frozen by the call. This | |
3788 | -- can lead to access-before-elaboration problem if the reference | |
3789 | -- appears in an object declaration and the subprogram body has not | |
3790 | -- been seen. The freezing of the subprogram requires special code | |
3791 | -- because it appears in an expanded context where expressions do | |
3792 | -- not freeze their constituents. | |
3793 | ||
3794 | ------------------------------ | |
3795 | -- Freeze_Stream_Subprogram -- | |
3796 | ------------------------------ | |
3797 | ||
3798 | procedure Freeze_Stream_Subprogram (F : Entity_Id) is | |
3799 | Decl : constant Node_Id := Unit_Declaration_Node (F); | |
3800 | Bod : Node_Id; | |
3801 | ||
3802 | begin | |
3803 | -- If this is user-defined subprogram, the corresponding | |
3804 | -- stream function appears as a renaming-as-body, and the | |
3805 | -- user subprogram must be retrieved by tree traversal. | |
3806 | ||
3807 | if Present (Decl) | |
3808 | and then Nkind (Decl) = N_Subprogram_Declaration | |
3809 | and then Present (Corresponding_Body (Decl)) | |
3810 | then | |
3811 | Bod := Corresponding_Body (Decl); | |
3812 | ||
3813 | if Nkind (Unit_Declaration_Node (Bod)) = | |
3814 | N_Subprogram_Renaming_Declaration | |
3815 | then | |
3816 | Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod)))); | |
3817 | end if; | |
3818 | end if; | |
3819 | end Freeze_Stream_Subprogram; | |
3820 | ||
3821 | -- Start of processing for Input | |
3822 | ||
70482933 RK |
3823 | begin |
3824 | -- If no underlying type, we have an error that will be diagnosed | |
3825 | -- elsewhere, so here we just completely ignore the expansion. | |
3826 | ||
3827 | if No (U_Type) then | |
3828 | return; | |
3829 | end if; | |
3830 | ||
baa571ab AC |
3831 | -- Stream operations can appear in user code even if the restriction |
3832 | -- No_Streams is active (for example, when instantiating a predefined | |
3833 | -- container). In that case rewrite the attribute as a Raise to | |
3834 | -- prevent any run-time use. | |
3835 | ||
3836 | if Restriction_Active (No_Streams) then | |
3837 | Rewrite (N, | |
3838 | Make_Raise_Program_Error (Sloc (N), | |
b8b2d982 | 3839 | Reason => PE_Stream_Operation_Not_Allowed)); |
baa571ab AC |
3840 | Set_Etype (N, B_Type); |
3841 | return; | |
3842 | end if; | |
3843 | ||
70482933 RK |
3844 | -- If there is a TSS for Input, just call it |
3845 | ||
fbf5a39b | 3846 | Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input); |
70482933 RK |
3847 | |
3848 | if Present (Fname) then | |
3849 | null; | |
3850 | ||
3851 | else | |
3852 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
3853 | ||
3854 | -- sourcetyp'Input (stream) | |
3855 | ||
3856 | -- as | |
3857 | ||
3858 | -- sourcetyp (streamread (strmtyp'Input (stream))); | |
3859 | ||
f3d0f304 | 3860 | -- where streamread is the given Read function that converts an |
21d27997 RD |
3861 | -- argument of type strmtyp to type sourcetyp or a type from which |
3862 | -- it is derived (extra conversion required for the derived case). | |
70482933 | 3863 | |
1d571f3b | 3864 | Prag := Get_Stream_Convert_Pragma (P_Type); |
70482933 RK |
3865 | |
3866 | if Present (Prag) then | |
3867 | Arg2 := Next (First (Pragma_Argument_Associations (Prag))); | |
3868 | Rfunc := Entity (Expression (Arg2)); | |
3869 | ||
3870 | Rewrite (N, | |
3871 | Convert_To (B_Type, | |
3872 | Make_Function_Call (Loc, | |
3873 | Name => New_Occurrence_Of (Rfunc, Loc), | |
3874 | Parameter_Associations => New_List ( | |
3875 | Make_Attribute_Reference (Loc, | |
3876 | Prefix => | |
3877 | New_Occurrence_Of | |
3878 | (Etype (First_Formal (Rfunc)), Loc), | |
3879 | Attribute_Name => Name_Input, | |
3880 | Expressions => Exprs))))); | |
3881 | ||
3882 | Analyze_And_Resolve (N, B_Type); | |
3883 | return; | |
3884 | ||
3885 | -- Elementary types | |
3886 | ||
3887 | elsif Is_Elementary_Type (U_Type) then | |
3888 | ||
3889 | -- A special case arises if we have a defined _Read routine, | |
3890 | -- since in this case we are required to call this routine. | |
3891 | ||
a3fbecee EB |
3892 | if Present (Find_Inherited_TSS (P_Type, TSS_Stream_Read)) then |
3893 | Build_Record_Or_Elementary_Input_Function | |
3894 | (Loc, P_Type, Decl, Fname); | |
3895 | Insert_Action (N, Decl); | |
70482933 | 3896 | |
a3fbecee | 3897 | -- For normal cases, we call the I_xxx routine directly |
70482933 | 3898 | |
a3fbecee EB |
3899 | else |
3900 | Rewrite (N, Build_Elementary_Input_Call (N)); | |
3901 | Analyze_And_Resolve (N, P_Type); | |
3902 | return; | |
3903 | end if; | |
70482933 RK |
3904 | |
3905 | -- Array type case | |
3906 | ||
3907 | elsif Is_Array_Type (U_Type) then | |
3908 | Build_Array_Input_Function (Loc, U_Type, Decl, Fname); | |
3909 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
3910 | ||
3911 | -- Dispatching case with class-wide type | |
3912 | ||
3913 | elsif Is_Class_Wide_Type (P_Type) then | |
3914 | ||
0669bebe GB |
3915 | -- No need to do anything else compiling under restriction |
3916 | -- No_Dispatching_Calls. During the semantic analysis we | |
3917 | -- already notified such violation. | |
3918 | ||
3919 | if Restriction_Active (No_Dispatching_Calls) then | |
3920 | return; | |
3921 | end if; | |
3922 | ||
70482933 RK |
3923 | declare |
3924 | Rtyp : constant Entity_Id := Root_Type (P_Type); | |
6d0289b1 HK |
3925 | |
3926 | Expr : Node_Id; -- call to Descendant_Tag | |
8e28429a | 3927 | Get_Tag : Node_Id; -- expression to read the 'Tag |
70482933 RK |
3928 | |
3929 | begin | |
3930 | -- Read the internal tag (RM 13.13.2(34)) and use it to | |
8e28429a BD |
3931 | -- initialize a dummy tag value. We used to unconditionally |
3932 | -- generate: | |
6a237c45 | 3933 | -- |
c9d70ab1 | 3934 | -- Descendant_Tag (String'Input (Strm), P_Type); |
6a237c45 AC |
3935 | -- |
3936 | -- which turns into a call to String_Input_Blk_IO. However, | |
3937 | -- if the input is malformed, that could try to read an | |
3938 | -- enormous String, causing chaos. So instead we call | |
3939 | -- String_Input_Tag, which does the same thing as | |
3940 | -- String_Input_Blk_IO, except that if the String is | |
3941 | -- absurdly long, it raises an exception. | |
3942 | -- | |
8e28429a BD |
3943 | -- However, if the No_Stream_Optimizations restriction |
3944 | -- is active, we disable this unnecessary attempt at | |
3945 | -- robustness; we really need to read the string | |
3946 | -- character-by-character. | |
3947 | -- | |
c9d70ab1 | 3948 | -- This value is used only to provide a controlling |
758c442c GD |
3949 | -- argument for the eventual _Input call. Descendant_Tag is |
3950 | -- called rather than Internal_Tag to ensure that we have a | |
3951 | -- tag for a type that is descended from the prefix type and | |
3952 | -- declared at the same accessibility level (the exception | |
3953 | -- Tag_Error will be raised otherwise). The level check is | |
3954 | -- required for Ada 2005 because tagged types can be | |
3955 | -- extended in nested scopes (AI-344). | |
70482933 | 3956 | |
c9d70ab1 AC |
3957 | -- Note: we used to generate an explicit declaration of a |
3958 | -- constant Ada.Tags.Tag object, and use an occurrence of | |
3959 | -- this constant in Cntrl, but this caused a secondary stack | |
3960 | -- leak. | |
3961 | ||
8e28429a BD |
3962 | if Restriction_Active (No_Stream_Optimizations) then |
3963 | Get_Tag := | |
3964 | Make_Attribute_Reference (Loc, | |
3965 | Prefix => | |
3966 | New_Occurrence_Of (Standard_String, Loc), | |
3967 | Attribute_Name => Name_Input, | |
3968 | Expressions => New_List ( | |
3969 | Relocate_Node (Duplicate_Subexpr (Strm)))); | |
3970 | else | |
3971 | Get_Tag := | |
3972 | Make_Function_Call (Loc, | |
3973 | Name => | |
3974 | New_Occurrence_Of | |
3975 | (RTE (RE_String_Input_Tag), Loc), | |
3976 | Parameter_Associations => New_List ( | |
3977 | Relocate_Node (Duplicate_Subexpr (Strm)))); | |
3978 | end if; | |
3979 | ||
191fcb3a RD |
3980 | Expr := |
3981 | Make_Function_Call (Loc, | |
e0c23ac7 | 3982 | Name => |
191fcb3a RD |
3983 | New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc), |
3984 | Parameter_Associations => New_List ( | |
8e28429a | 3985 | Get_Tag, |
191fcb3a | 3986 | Make_Attribute_Reference (Loc, |
e0c23ac7 | 3987 | Prefix => New_Occurrence_Of (P_Type, Loc), |
191fcb3a | 3988 | Attribute_Name => Name_Tag))); |
683af98c | 3989 | |
c9d70ab1 | 3990 | Set_Etype (Expr, RTE (RE_Tag)); |
70482933 RK |
3991 | |
3992 | -- Now we need to get the entity for the call, and construct | |
3993 | -- a function call node, where we preset a reference to Dnn | |
758c442c GD |
3994 | -- as the controlling argument (doing an unchecked convert |
3995 | -- to the class-wide tagged type to make it look like a real | |
3996 | -- tagged object). | |
70482933 | 3997 | |
fbf5a39b | 3998 | Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input); |
c9d70ab1 | 3999 | Cntrl := Unchecked_Convert_To (P_Type, Expr); |
fbf5a39b | 4000 | Set_Etype (Cntrl, P_Type); |
70482933 RK |
4001 | Set_Parent (Cntrl, N); |
4002 | end; | |
4003 | ||
4004 | -- For tagged types, use the primitive Input function | |
4005 | ||
4006 | elsif Is_Tagged_Type (U_Type) then | |
fbf5a39b | 4007 | Fname := Find_Prim_Op (U_Type, TSS_Stream_Input); |
70482933 | 4008 | |
758c442c GD |
4009 | -- All other record type cases, including protected records. The |
4010 | -- latter only arise for expander generated code for handling | |
4011 | -- shared passive partition access. | |
70482933 RK |
4012 | |
4013 | else | |
4014 | pragma Assert | |
4015 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
4016 | ||
21d27997 RD |
4017 | -- Ada 2005 (AI-216): Program_Error is raised executing default |
4018 | -- implementation of the Input attribute of an unchecked union | |
4019 | -- type if the type lacks default discriminant values. | |
5d09245e AC |
4020 | |
4021 | if Is_Unchecked_Union (Base_Type (U_Type)) | |
80d4224f | 4022 | and then No (Discriminant_Constraint (U_Type)) |
5d09245e AC |
4023 | then |
4024 | Insert_Action (N, | |
4025 | Make_Raise_Program_Error (Loc, | |
4026 | Reason => PE_Unchecked_Union_Restriction)); | |
4027 | ||
4028 | return; | |
4029 | end if; | |
4030 | ||
f2404867 AC |
4031 | -- Build the type's Input function, passing the subtype rather |
4032 | -- than its base type, because checks are needed in the case of | |
4033 | -- constrained discriminants (see Ada 2012 AI05-0192). | |
4034 | ||
70482933 | 4035 | Build_Record_Or_Elementary_Input_Function |
f2404867 | 4036 | (Loc, U_Type, Decl, Fname); |
70482933 | 4037 | Insert_Action (N, Decl); |
1c6c6771 ES |
4038 | |
4039 | if Nkind (Parent (N)) = N_Object_Declaration | |
4040 | and then Is_Record_Type (U_Type) | |
4041 | then | |
4042 | -- The stream function may contain calls to user-defined | |
4043 | -- Read procedures for individual components. | |
4044 | ||
4045 | declare | |
4046 | Comp : Entity_Id; | |
4047 | Func : Entity_Id; | |
4048 | ||
4049 | begin | |
4050 | Comp := First_Component (U_Type); | |
4051 | while Present (Comp) loop | |
4052 | Func := | |
4053 | Find_Stream_Subprogram | |
4054 | (Etype (Comp), TSS_Stream_Read); | |
4055 | ||
4056 | if Present (Func) then | |
4057 | Freeze_Stream_Subprogram (Func); | |
4058 | end if; | |
4059 | ||
4060 | Next_Component (Comp); | |
4061 | end loop; | |
4062 | end; | |
4063 | end if; | |
70482933 RK |
4064 | end if; |
4065 | end if; | |
4066 | ||
758c442c GD |
4067 | -- If we fall through, Fname is the function to be called. The result |
4068 | -- is obtained by calling the appropriate function, then converting | |
4069 | -- the result. The conversion does a subtype check. | |
70482933 RK |
4070 | |
4071 | Call := | |
4072 | Make_Function_Call (Loc, | |
4073 | Name => New_Occurrence_Of (Fname, Loc), | |
4074 | Parameter_Associations => New_List ( | |
4075 | Relocate_Node (Strm))); | |
4076 | ||
4077 | Set_Controlling_Argument (Call, Cntrl); | |
4078 | Rewrite (N, Unchecked_Convert_To (P_Type, Call)); | |
4079 | Analyze_And_Resolve (N, P_Type); | |
1c6c6771 ES |
4080 | |
4081 | if Nkind (Parent (N)) = N_Object_Declaration then | |
4082 | Freeze_Stream_Subprogram (Fname); | |
4083 | end if; | |
70482933 RK |
4084 | end Input; |
4085 | ||
21d27997 RD |
4086 | ------------------- |
4087 | -- Invalid_Value -- | |
4088 | ------------------- | |
4089 | ||
4090 | when Attribute_Invalid_Value => | |
4091 | Rewrite (N, Get_Simple_Init_Val (Ptyp, N)); | |
4092 | ||
68f27c97 HK |
4093 | -- The value produced may be a conversion of a literal, which must be |
4094 | -- resolved to establish its proper type. | |
6a04c943 ES |
4095 | |
4096 | Analyze_And_Resolve (N); | |
4097 | ||
70482933 RK |
4098 | -------------- |
4099 | -- Last_Bit -- | |
4100 | -------------- | |
4101 | ||
12be130c EB |
4102 | -- We leave the computation up to the back end, since we don't know what |
4103 | -- layout will be chosen if no component clause was specified. | |
be482a8c | 4104 | |
12be130c EB |
4105 | when Attribute_Last_Bit => |
4106 | Apply_Universal_Integer_Attribute_Checks (N); | |
70482933 RK |
4107 | |
4108 | ------------------ | |
4109 | -- Leading_Part -- | |
4110 | ------------------ | |
4111 | ||
4112 | -- Transforms 'Leading_Part into a call to the floating-point attribute | |
4113 | -- function Leading_Part in Fat_xxx (where xxx is the root type) | |
4114 | ||
21d27997 | 4115 | -- Note: strictly, we should generate special case code to deal with |
758c442c GD |
4116 | -- absurdly large positive arguments (greater than Integer'Last), which |
4117 | -- result in returning the first argument unchanged, but it hardly seems | |
4118 | -- worth the effort. We raise constraint error for absurdly negative | |
4119 | -- arguments which is fine. | |
70482933 RK |
4120 | |
4121 | when Attribute_Leading_Part => | |
4122 | Expand_Fpt_Attribute_RI (N); | |
4123 | ||
4124 | ------------ | |
4125 | -- Length -- | |
4126 | ------------ | |
4127 | ||
150ac76e | 4128 | when Attribute_Length => Length : declare |
70482933 RK |
4129 | Ityp : Entity_Id; |
4130 | Xnum : Uint; | |
4131 | ||
4132 | begin | |
4133 | -- Processing for packed array types | |
4134 | ||
4135 | if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then | |
4136 | Ityp := Get_Index_Subtype (N); | |
4137 | ||
21d27997 RD |
4138 | -- If the index type, Ityp, is an enumeration type with holes, |
4139 | -- then we calculate X'Length explicitly using | |
70482933 RK |
4140 | |
4141 | -- Typ'Max | |
4142 | -- (0, Ityp'Pos (X'Last (N)) - | |
4143 | -- Ityp'Pos (X'First (N)) + 1); | |
4144 | ||
21d27997 RD |
4145 | -- Since the bounds in the template are the representation values |
4146 | -- and the back end would get the wrong value. | |
70482933 RK |
4147 | |
4148 | if Is_Enumeration_Type (Ityp) | |
4149 | and then Present (Enum_Pos_To_Rep (Base_Type (Ityp))) | |
4150 | then | |
4151 | if No (Exprs) then | |
4152 | Xnum := Uint_1; | |
4153 | else | |
4154 | Xnum := Expr_Value (First (Expressions (N))); | |
4155 | end if; | |
4156 | ||
4157 | Rewrite (N, | |
4158 | Make_Attribute_Reference (Loc, | |
4159 | Prefix => New_Occurrence_Of (Typ, Loc), | |
4160 | Attribute_Name => Name_Max, | |
4161 | Expressions => New_List | |
4162 | (Make_Integer_Literal (Loc, 0), | |
4163 | ||
4164 | Make_Op_Add (Loc, | |
4165 | Left_Opnd => | |
4166 | Make_Op_Subtract (Loc, | |
4167 | Left_Opnd => | |
4168 | Make_Attribute_Reference (Loc, | |
4169 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
4170 | Attribute_Name => Name_Pos, | |
4171 | ||
4172 | Expressions => New_List ( | |
4173 | Make_Attribute_Reference (Loc, | |
4174 | Prefix => Duplicate_Subexpr (Pref), | |
4175 | Attribute_Name => Name_Last, | |
4176 | Expressions => New_List ( | |
4177 | Make_Integer_Literal (Loc, Xnum))))), | |
4178 | ||
4179 | Right_Opnd => | |
4180 | Make_Attribute_Reference (Loc, | |
4181 | Prefix => New_Occurrence_Of (Ityp, Loc), | |
4182 | Attribute_Name => Name_Pos, | |
4183 | ||
4184 | Expressions => New_List ( | |
4185 | Make_Attribute_Reference (Loc, | |
fbf5a39b AC |
4186 | Prefix => |
4187 | Duplicate_Subexpr_No_Checks (Pref), | |
70482933 RK |
4188 | Attribute_Name => Name_First, |
4189 | Expressions => New_List ( | |
4190 | Make_Integer_Literal (Loc, Xnum)))))), | |
4191 | ||
4192 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); | |
4193 | ||
4194 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
4195 | return; | |
4196 | ||
4197 | -- If the prefix type is a constrained packed array type which | |
8ca597af RD |
4198 | -- already has a Packed_Array_Impl_Type representation defined, |
4199 | -- then replace this attribute with a reference to 'Range_Length | |
4200 | -- of the appropriate index subtype (since otherwise the | |
4201 | -- back end will try to give us the value of 'Length for | |
4202 | -- this implementation type).s | |
70482933 RK |
4203 | |
4204 | elsif Is_Constrained (Ptyp) then | |
4205 | Rewrite (N, | |
4206 | Make_Attribute_Reference (Loc, | |
4207 | Attribute_Name => Name_Range_Length, | |
e4494292 | 4208 | Prefix => New_Occurrence_Of (Ityp, Loc))); |
70482933 RK |
4209 | Analyze_And_Resolve (N, Typ); |
4210 | end if; | |
4211 | ||
70482933 RK |
4212 | -- Access type case |
4213 | ||
4214 | elsif Is_Access_Type (Ptyp) then | |
4215 | Apply_Access_Check (N); | |
4216 | ||
21d27997 RD |
4217 | -- If the designated type is a packed array type, then we convert |
4218 | -- the reference to: | |
70482933 RK |
4219 | |
4220 | -- typ'Max (0, 1 + | |
4221 | -- xtyp'Pos (Pref'Last (Expr)) - | |
4222 | -- xtyp'Pos (Pref'First (Expr))); | |
4223 | ||
21d27997 RD |
4224 | -- This is a bit complex, but it is the easiest thing to do that |
4225 | -- works in all cases including enum types with holes xtyp here | |
4226 | -- is the appropriate index type. | |
70482933 RK |
4227 | |
4228 | declare | |
4229 | Dtyp : constant Entity_Id := Designated_Type (Ptyp); | |
4230 | Xtyp : Entity_Id; | |
4231 | ||
4232 | begin | |
4233 | if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then | |
4234 | Xtyp := Get_Index_Subtype (N); | |
4235 | ||
4236 | Rewrite (N, | |
4237 | Make_Attribute_Reference (Loc, | |
4238 | Prefix => New_Occurrence_Of (Typ, Loc), | |
4239 | Attribute_Name => Name_Max, | |
4240 | Expressions => New_List ( | |
4241 | Make_Integer_Literal (Loc, 0), | |
4242 | ||
4243 | Make_Op_Add (Loc, | |
4244 | Make_Integer_Literal (Loc, 1), | |
4245 | Make_Op_Subtract (Loc, | |
4246 | Left_Opnd => | |
4247 | Make_Attribute_Reference (Loc, | |
4248 | Prefix => New_Occurrence_Of (Xtyp, Loc), | |
4249 | Attribute_Name => Name_Pos, | |
4250 | Expressions => New_List ( | |
4251 | Make_Attribute_Reference (Loc, | |
4252 | Prefix => Duplicate_Subexpr (Pref), | |
4253 | Attribute_Name => Name_Last, | |
4254 | Expressions => | |
4255 | New_Copy_List (Exprs)))), | |
4256 | ||
4257 | Right_Opnd => | |
4258 | Make_Attribute_Reference (Loc, | |
4259 | Prefix => New_Occurrence_Of (Xtyp, Loc), | |
4260 | Attribute_Name => Name_Pos, | |
4261 | Expressions => New_List ( | |
4262 | Make_Attribute_Reference (Loc, | |
fbf5a39b AC |
4263 | Prefix => |
4264 | Duplicate_Subexpr_No_Checks (Pref), | |
70482933 RK |
4265 | Attribute_Name => Name_First, |
4266 | Expressions => | |
4267 | New_Copy_List (Exprs))))))))); | |
4268 | ||
4269 | Analyze_And_Resolve (N, Typ); | |
4270 | end if; | |
4271 | end; | |
4272 | ||
21d27997 | 4273 | -- Otherwise leave it to the back end |
70482933 RK |
4274 | |
4275 | else | |
4276 | Apply_Universal_Integer_Attribute_Checks (N); | |
4277 | end if; | |
150ac76e AC |
4278 | end Length; |
4279 | ||
d436b30d AC |
4280 | -- Attribute Loop_Entry is replaced with a reference to a constant value |
4281 | -- which captures the prefix at the entry point of the related loop. The | |
4282 | -- loop itself may be transformed into a conditional block. | |
150ac76e AC |
4283 | |
4284 | when Attribute_Loop_Entry => | |
d436b30d | 4285 | Expand_Loop_Entry_Attribute (N); |
70482933 RK |
4286 | |
4287 | ------------- | |
4288 | -- Machine -- | |
4289 | ------------- | |
4290 | ||
4291 | -- Transforms 'Machine into a call to the floating-point attribute | |
24228312 AC |
4292 | -- function Machine in Fat_xxx (where xxx is the root type). |
4293 | -- Expansion is avoided for cases the back end can handle directly. | |
70482933 RK |
4294 | |
4295 | when Attribute_Machine => | |
24228312 AC |
4296 | if not Is_Inline_Floating_Point_Attribute (N) then |
4297 | Expand_Fpt_Attribute_R (N); | |
4298 | end if; | |
70482933 | 4299 | |
65f01153 RD |
4300 | ---------------------- |
4301 | -- Machine_Rounding -- | |
4302 | ---------------------- | |
4303 | ||
4304 | -- Transforms 'Machine_Rounding into a call to the floating-point | |
4305 | -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root | |
0669bebe GB |
4306 | -- type). Expansion is avoided for cases the back end can handle |
4307 | -- directly. | |
65f01153 RD |
4308 | |
4309 | when Attribute_Machine_Rounding => | |
0669bebe GB |
4310 | if not Is_Inline_Floating_Point_Attribute (N) then |
4311 | Expand_Fpt_Attribute_R (N); | |
4312 | end if; | |
65f01153 | 4313 | |
70482933 RK |
4314 | ------------------ |
4315 | -- Machine_Size -- | |
4316 | ------------------ | |
4317 | ||
4318 | -- Machine_Size is equivalent to Object_Size, so transform it into | |
21d27997 | 4319 | -- Object_Size and that way the back end never sees Machine_Size. |
70482933 RK |
4320 | |
4321 | when Attribute_Machine_Size => | |
4322 | Rewrite (N, | |
4323 | Make_Attribute_Reference (Loc, | |
4324 | Prefix => Prefix (N), | |
4325 | Attribute_Name => Name_Object_Size)); | |
4326 | ||
4327 | Analyze_And_Resolve (N, Typ); | |
4328 | ||
4329 | -------------- | |
4330 | -- Mantissa -- | |
4331 | -------------- | |
4332 | ||
758c442c | 4333 | -- The only case that can get this far is the dynamic case of the old |
21d27997 RD |
4334 | -- Ada 83 Mantissa attribute for the fixed-point case. For this case, |
4335 | -- we expand: | |
70482933 RK |
4336 | |
4337 | -- typ'Mantissa | |
4338 | ||
4339 | -- into | |
4340 | ||
4341 | -- ityp (System.Mantissa.Mantissa_Value | |
4342 | -- (Integer'Integer_Value (typ'First), | |
4343 | -- Integer'Integer_Value (typ'Last))); | |
4344 | ||
d8f43ee6 | 4345 | when Attribute_Mantissa => |
70482933 RK |
4346 | Rewrite (N, |
4347 | Convert_To (Typ, | |
4348 | Make_Function_Call (Loc, | |
d8f43ee6 HK |
4349 | Name => |
4350 | New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc), | |
70482933 RK |
4351 | |
4352 | Parameter_Associations => New_List ( | |
70482933 | 4353 | Make_Attribute_Reference (Loc, |
d8f43ee6 | 4354 | Prefix => New_Occurrence_Of (Standard_Integer, Loc), |
70482933 | 4355 | Attribute_Name => Name_Integer_Value, |
d8f43ee6 | 4356 | Expressions => New_List ( |
70482933 | 4357 | Make_Attribute_Reference (Loc, |
d8f43ee6 | 4358 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
70482933 RK |
4359 | Attribute_Name => Name_First))), |
4360 | ||
4361 | Make_Attribute_Reference (Loc, | |
d8f43ee6 | 4362 | Prefix => New_Occurrence_Of (Standard_Integer, Loc), |
70482933 | 4363 | Attribute_Name => Name_Integer_Value, |
d8f43ee6 | 4364 | Expressions => New_List ( |
70482933 | 4365 | Make_Attribute_Reference (Loc, |
d8f43ee6 | 4366 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
70482933 RK |
4367 | Attribute_Name => Name_Last))))))); |
4368 | ||
4369 | Analyze_And_Resolve (N, Typ); | |
70482933 | 4370 | |
aa9b151a AC |
4371 | --------- |
4372 | -- Max -- | |
4373 | --------- | |
4374 | ||
4375 | when Attribute_Max => | |
e0f63680 | 4376 | Expand_Min_Max_Attribute (N); |
aa9b151a | 4377 | |
ca20a08e AC |
4378 | ---------------------------------- |
4379 | -- Max_Size_In_Storage_Elements -- | |
4380 | ---------------------------------- | |
4381 | ||
24cb156d AC |
4382 | when Attribute_Max_Size_In_Storage_Elements => declare |
4383 | Typ : constant Entity_Id := Etype (N); | |
4384 | Attr : Node_Id; | |
10e168cd | 4385 | Atyp : Entity_Id; |
24cb156d AC |
4386 | |
4387 | Conversion_Added : Boolean := False; | |
4388 | -- A flag which tracks whether the original attribute has been | |
4389 | -- wrapped inside a type conversion. | |
4390 | ||
4391 | begin | |
d85badc7 BD |
4392 | -- If the prefix is X'Class, we transform it into a direct reference |
4393 | -- to the class-wide type, because the back end must not see a 'Class | |
4394 | -- reference. See also 'Size. | |
4395 | ||
4396 | if Is_Entity_Name (Pref) | |
4397 | and then Is_Class_Wide_Type (Entity (Pref)) | |
4398 | then | |
4399 | Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc)); | |
4400 | return; | |
4401 | end if; | |
4402 | ||
ca20a08e AC |
4403 | Apply_Universal_Integer_Attribute_Checks (N); |
4404 | ||
24cb156d AC |
4405 | -- The universal integer check may sometimes add a type conversion, |
4406 | -- retrieve the original attribute reference from the expression. | |
4407 | ||
4408 | Attr := N; | |
d18b1548 | 4409 | |
24cb156d AC |
4410 | if Nkind (Attr) = N_Type_Conversion then |
4411 | Attr := Expression (Attr); | |
4412 | Conversion_Added := True; | |
4413 | end if; | |
d18b1548 | 4414 | |
d85badc7 | 4415 | pragma Assert (Nkind (Attr) = N_Attribute_Reference); |
24cb156d | 4416 | |
ca20a08e AC |
4417 | -- Heap-allocated controlled objects contain two extra pointers which |
4418 | -- are not part of the actual type. Transform the attribute reference | |
4419 | -- into a runtime expression to add the size of the hidden header. | |
4420 | ||
535a8637 | 4421 | if Needs_Finalization (Ptyp) |
24cb156d | 4422 | and then not Header_Size_Added (Attr) |
ca20a08e | 4423 | then |
24cb156d | 4424 | Set_Header_Size_Added (Attr); |
ca20a08e | 4425 | |
10e168cd EB |
4426 | Atyp := Etype (Attr); |
4427 | ||
ca20a08e AC |
4428 | -- Generate: |
4429 | -- P'Max_Size_In_Storage_Elements + | |
10e168cd | 4430 | -- Atyp (Header_Size_With_Padding (Ptyp'Alignment)) |
ca20a08e | 4431 | |
24cb156d | 4432 | Rewrite (Attr, |
ca20a08e | 4433 | Make_Op_Add (Loc, |
24cb156d | 4434 | Left_Opnd => Relocate_Node (Attr), |
ca20a08e | 4435 | Right_Opnd => |
10e168cd | 4436 | Convert_To (Atyp, |
ca20a08e AC |
4437 | Make_Function_Call (Loc, |
4438 | Name => | |
e4494292 | 4439 | New_Occurrence_Of |
ca20a08e AC |
4440 | (RTE (RE_Header_Size_With_Padding), Loc), |
4441 | ||
4442 | Parameter_Associations => New_List ( | |
4443 | Make_Attribute_Reference (Loc, | |
4444 | Prefix => | |
e4494292 | 4445 | New_Occurrence_Of (Ptyp, Loc), |
ca20a08e AC |
4446 | Attribute_Name => Name_Alignment)))))); |
4447 | ||
10e168cd EB |
4448 | Analyze_And_Resolve (Attr, Atyp); |
4449 | ||
24cb156d AC |
4450 | -- Add a conversion to the target type |
4451 | ||
4452 | if not Conversion_Added then | |
10e168cd | 4453 | Convert_To_And_Rewrite (Typ, Attr); |
24cb156d AC |
4454 | end if; |
4455 | ||
ca20a08e AC |
4456 | return; |
4457 | end if; | |
24cb156d | 4458 | end; |
ca20a08e | 4459 | |
80d4224f RD |
4460 | -------------------- |
4461 | -- Mechanism_Code -- | |
4462 | -------------------- | |
4463 | ||
4464 | when Attribute_Mechanism_Code => | |
4465 | ||
d8f43ee6 | 4466 | -- We must replace the prefix in the renamed case |
80d4224f RD |
4467 | |
4468 | if Is_Entity_Name (Pref) | |
4469 | and then Present (Alias (Entity (Pref))) | |
4470 | then | |
4471 | Set_Renamed_Subprogram (Pref, Alias (Entity (Pref))); | |
4472 | end if; | |
4473 | ||
aa9b151a AC |
4474 | --------- |
4475 | -- Min -- | |
4476 | --------- | |
4477 | ||
4478 | when Attribute_Min => | |
e0f63680 | 4479 | Expand_Min_Max_Attribute (N); |
aa9b151a | 4480 | |
5f3ab6fb AC |
4481 | --------- |
4482 | -- Mod -- | |
4483 | --------- | |
4484 | ||
4485 | when Attribute_Mod => Mod_Case : declare | |
4486 | Arg : constant Node_Id := Relocate_Node (First (Exprs)); | |
4487 | Hi : constant Node_Id := Type_High_Bound (Etype (Arg)); | |
4488 | Modv : constant Uint := Modulus (Btyp); | |
4489 | ||
4490 | begin | |
4491 | ||
4492 | -- This is not so simple. The issue is what type to use for the | |
4493 | -- computation of the modular value. | |
4494 | ||
4495 | -- The easy case is when the modulus value is within the bounds | |
4496 | -- of the signed integer type of the argument. In this case we can | |
4497 | -- just do the computation in that signed integer type, and then | |
4498 | -- do an ordinary conversion to the target type. | |
4499 | ||
4500 | if Modv <= Expr_Value (Hi) then | |
4501 | Rewrite (N, | |
4502 | Convert_To (Btyp, | |
4503 | Make_Op_Mod (Loc, | |
4504 | Left_Opnd => Arg, | |
4505 | Right_Opnd => Make_Integer_Literal (Loc, Modv)))); | |
4506 | ||
4507 | -- Here we know that the modulus is larger than type'Last of the | |
3b641023 | 4508 | -- integer type. There are two cases to consider: |
5f3ab6fb AC |
4509 | |
4510 | -- a) The integer value is non-negative. In this case, it is | |
4511 | -- returned as the result (since it is less than the modulus). | |
4512 | ||
758c442c GD |
4513 | -- b) The integer value is negative. In this case, we know that the |
4514 | -- result is modulus + value, where the value might be as small as | |
4515 | -- -modulus. The trouble is what type do we use to do the subtract. | |
4516 | -- No type will do, since modulus can be as big as 2**64, and no | |
f3d0f304 | 4517 | -- integer type accommodates this value. Let's do bit of algebra |
5f3ab6fb AC |
4518 | |
4519 | -- modulus + value | |
4520 | -- = modulus - (-value) | |
4521 | -- = (modulus - 1) - (-value - 1) | |
4522 | ||
4523 | -- Now modulus - 1 is certainly in range of the modular type. | |
4524 | -- -value is in the range 1 .. modulus, so -value -1 is in the | |
4525 | -- range 0 .. modulus-1 which is in range of the modular type. | |
4526 | -- Furthermore, (-value - 1) can be expressed as -(value + 1) | |
4527 | -- which we can compute using the integer base type. | |
4528 | ||
9b16cb57 RD |
4529 | -- Once this is done we analyze the if expression without range |
4530 | -- checks, because we know everything is in range, and we want | |
4531 | -- to prevent spurious warnings on either branch. | |
3b641023 | 4532 | |
5f3ab6fb AC |
4533 | else |
4534 | Rewrite (N, | |
9b16cb57 | 4535 | Make_If_Expression (Loc, |
5f3ab6fb AC |
4536 | Expressions => New_List ( |
4537 | Make_Op_Ge (Loc, | |
4538 | Left_Opnd => Duplicate_Subexpr (Arg), | |
4539 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
4540 | ||
4541 | Convert_To (Btyp, | |
4542 | Duplicate_Subexpr_No_Checks (Arg)), | |
4543 | ||
4544 | Make_Op_Subtract (Loc, | |
4545 | Left_Opnd => | |
4546 | Make_Integer_Literal (Loc, | |
4547 | Intval => Modv - 1), | |
4548 | Right_Opnd => | |
4549 | Convert_To (Btyp, | |
4550 | Make_Op_Minus (Loc, | |
4551 | Right_Opnd => | |
4552 | Make_Op_Add (Loc, | |
4553 | Left_Opnd => Duplicate_Subexpr_No_Checks (Arg), | |
4554 | Right_Opnd => | |
4555 | Make_Integer_Literal (Loc, | |
4556 | Intval => 1)))))))); | |
4557 | ||
5f3ab6fb AC |
4558 | end if; |
4559 | ||
65f01153 | 4560 | Analyze_And_Resolve (N, Btyp, Suppress => All_Checks); |
5f3ab6fb AC |
4561 | end Mod_Case; |
4562 | ||
70482933 RK |
4563 | ----------- |
4564 | -- Model -- | |
4565 | ----------- | |
4566 | ||
4567 | -- Transforms 'Model into a call to the floating-point attribute | |
24228312 AC |
4568 | -- function Model in Fat_xxx (where xxx is the root type). |
4569 | -- Expansion is avoided for cases the back end can handle directly. | |
70482933 RK |
4570 | |
4571 | when Attribute_Model => | |
24228312 AC |
4572 | if not Is_Inline_Floating_Point_Attribute (N) then |
4573 | Expand_Fpt_Attribute_R (N); | |
4574 | end if; | |
70482933 RK |
4575 | |
4576 | ----------------- | |
4577 | -- Object_Size -- | |
4578 | ----------------- | |
4579 | ||
4580 | -- The processing for Object_Size shares the processing for Size | |
4581 | ||
e10dab7f JM |
4582 | --------- |
4583 | -- Old -- | |
4584 | --------- | |
4585 | ||
4586 | when Attribute_Old => Old : declare | |
6c802906 AC |
4587 | Typ : constant Entity_Id := Etype (N); |
4588 | CW_Temp : Entity_Id; | |
4589 | CW_Typ : Entity_Id; | |
d48bb126 | 4590 | Decl : Node_Id; |
64f5d139 | 4591 | Ins_Nod : Node_Id; |
8e1e62e3 AC |
4592 | Subp : Node_Id; |
4593 | Temp : Entity_Id; | |
e10dab7f JM |
4594 | |
4595 | begin | |
64f5d139 JM |
4596 | -- Generating C code we don't need to expand this attribute when |
4597 | -- we are analyzing the internally built nested postconditions | |
4598 | -- procedure since it will be expanded inline (and later it will | |
4599 | -- be removed by Expand_N_Subprogram_Body). It this expansion is | |
4600 | -- performed in such case then the compiler generates unreferenced | |
4601 | -- extra temporaries. | |
4602 | ||
4603 | if Modify_Tree_For_C | |
4604 | and then Chars (Current_Scope) = Name_uPostconditions | |
4605 | then | |
4606 | return; | |
4607 | end if; | |
4608 | ||
8e1e62e3 | 4609 | -- Climb the parent chain looking for subprogram _Postconditions |
21d27997 | 4610 | |
e10dab7f | 4611 | Subp := N; |
8e1e62e3 | 4612 | while Present (Subp) loop |
21d27997 | 4613 | exit when Nkind (Subp) = N_Subprogram_Body |
8e1e62e3 AC |
4614 | and then Chars (Defining_Entity (Subp)) = Name_uPostconditions; |
4615 | ||
4de10025 AC |
4616 | -- If assertions are disabled, no need to create the declaration |
4617 | -- that preserves the value. The postcondition pragma in which | |
4618 | -- 'Old appears will be checked or disabled according to the | |
4619 | -- current policy in effect. | |
4620 | ||
890f1954 | 4621 | if Nkind (Subp) = N_Pragma and then not Is_Checked (Subp) then |
4de10025 AC |
4622 | return; |
4623 | end if; | |
4624 | ||
8e1e62e3 | 4625 | Subp := Parent (Subp); |
e10dab7f JM |
4626 | end loop; |
4627 | ||
8e1e62e3 | 4628 | -- 'Old can only appear in a postcondition, the generated body of |
64f5d139 JM |
4629 | -- _Postconditions must be in the tree (or inlined if we are |
4630 | -- generating C code). | |
8e1e62e3 | 4631 | |
fb757f7d AC |
4632 | pragma Assert |
4633 | (Present (Subp) | |
4634 | or else (Modify_Tree_For_C and then In_Inlined_Body)); | |
8e1e62e3 | 4635 | |
6c802906 | 4636 | Temp := Make_Temporary (Loc, 'T', Pref); |
21d27997 | 4637 | |
6c802906 AC |
4638 | -- Set the entity kind now in order to mark the temporary as a |
4639 | -- handler of attribute 'Old's prefix. | |
4640 | ||
4641 | Set_Ekind (Temp, E_Constant); | |
4642 | Set_Stores_Attribute_Old_Prefix (Temp); | |
e10dab7f | 4643 | |
8e1e62e3 AC |
4644 | -- Push the scope of the related subprogram where _Postcondition |
4645 | -- resides as this ensures that the object will be analyzed in the | |
4646 | -- proper context. | |
7425962b | 4647 | |
64f5d139 JM |
4648 | if Present (Subp) then |
4649 | Push_Scope (Scope (Defining_Entity (Subp))); | |
4650 | ||
4651 | -- No need to push the scope when generating C code since the | |
4652 | -- _Postcondition procedure has been inlined. | |
4653 | ||
4654 | else pragma Assert (Modify_Tree_For_C); | |
4655 | pragma Assert (In_Inlined_Body); | |
4656 | null; | |
4657 | end if; | |
4658 | ||
4659 | -- Locate the insertion place of the internal temporary that saves | |
4660 | -- the 'Old value. | |
4661 | ||
4662 | if Present (Subp) then | |
4663 | Ins_Nod := Subp; | |
4664 | ||
4665 | -- Generating C, the postcondition procedure has been inlined and the | |
4666 | -- temporary is added before the first declaration of the enclosing | |
4667 | -- subprogram. | |
4668 | ||
4669 | else pragma Assert (Modify_Tree_For_C); | |
4670 | Ins_Nod := N; | |
4671 | while Nkind (Ins_Nod) /= N_Subprogram_Body loop | |
4672 | Ins_Nod := Parent (Ins_Nod); | |
4673 | end loop; | |
4674 | ||
4675 | Ins_Nod := First (Declarations (Ins_Nod)); | |
4676 | end if; | |
7425962b | 4677 | |
6c802906 AC |
4678 | -- Preserve the tag of the prefix by offering a specific view of the |
4679 | -- class-wide version of the prefix. | |
4680 | ||
4681 | if Is_Tagged_Type (Typ) then | |
4682 | ||
4683 | -- Generate: | |
4684 | -- CW_Temp : constant Typ'Class := Typ'Class (Pref); | |
4685 | ||
4686 | CW_Temp := Make_Temporary (Loc, 'T'); | |
4687 | CW_Typ := Class_Wide_Type (Typ); | |
4688 | ||
d48bb126 | 4689 | Decl := |
6c802906 AC |
4690 | Make_Object_Declaration (Loc, |
4691 | Defining_Identifier => CW_Temp, | |
4692 | Constant_Present => True, | |
4693 | Object_Definition => New_Occurrence_Of (CW_Typ, Loc), | |
4694 | Expression => | |
d48bb126 PT |
4695 | Convert_To (CW_Typ, Relocate_Node (Pref))); |
4696 | ||
4697 | Insert_Before_And_Analyze (Ins_Nod, Decl); | |
6c802906 AC |
4698 | |
4699 | -- Generate: | |
4700 | -- Temp : Typ renames Typ (CW_Temp); | |
4701 | ||
64f5d139 | 4702 | Insert_Before_And_Analyze (Ins_Nod, |
6c802906 AC |
4703 | Make_Object_Renaming_Declaration (Loc, |
4704 | Defining_Identifier => Temp, | |
4705 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
4706 | Name => | |
4707 | Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc)))); | |
4708 | ||
4709 | -- Non-tagged case | |
4710 | ||
4711 | else | |
4712 | -- Generate: | |
4713 | -- Temp : constant Typ := Pref; | |
4714 | ||
d48bb126 | 4715 | Decl := |
6c802906 AC |
4716 | Make_Object_Declaration (Loc, |
4717 | Defining_Identifier => Temp, | |
4718 | Constant_Present => True, | |
4719 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
d48bb126 PT |
4720 | Expression => Relocate_Node (Pref)); |
4721 | ||
4722 | Insert_Before_And_Analyze (Ins_Nod, Decl); | |
4723 | ||
6c802906 | 4724 | end if; |
8e1e62e3 | 4725 | |
64f5d139 JM |
4726 | if Present (Subp) then |
4727 | Pop_Scope; | |
4728 | end if; | |
e10dab7f | 4729 | |
2838fa93 AC |
4730 | -- Ensure that the prefix of attribute 'Old is valid. The check must |
4731 | -- be inserted after the expansion of the attribute has taken place | |
4732 | -- to reflect the new placement of the prefix. | |
4733 | ||
4734 | if Validity_Checks_On and then Validity_Check_Operands then | |
d48bb126 | 4735 | Ensure_Valid (Expression (Decl)); |
2838fa93 AC |
4736 | end if; |
4737 | ||
8e1e62e3 | 4738 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
e10dab7f JM |
4739 | end Old; |
4740 | ||
2d42e881 ES |
4741 | ---------------------- |
4742 | -- Overlaps_Storage -- | |
4743 | ---------------------- | |
4744 | ||
4745 | when Attribute_Overlaps_Storage => Overlaps_Storage : declare | |
4746 | Loc : constant Source_Ptr := Sloc (N); | |
d4fd4637 JS |
4747 | X : constant Node_Id := Prefix (N); |
4748 | Y : constant Node_Id := First (Expressions (N)); | |
2d42e881 | 4749 | |
90b510e4 | 4750 | -- The arguments |
2d42e881 ES |
4751 | |
4752 | X_Addr, Y_Addr : Node_Id; | |
d4fd4637 JS |
4753 | |
4754 | -- The expressions for their integer addresses | |
2d42e881 ES |
4755 | |
4756 | X_Size, Y_Size : Node_Id; | |
d4fd4637 JS |
4757 | |
4758 | -- The expressions for their sizes | |
2d42e881 ES |
4759 | |
4760 | Cond : Node_Id; | |
4761 | ||
4762 | begin | |
4763 | -- Attribute expands into: | |
4764 | ||
d4fd4637 JS |
4765 | -- (if X'Size = 0 or else Y'Size = 0 then |
4766 | -- False | |
4767 | -- else | |
4768 | -- (if X'Address <= Y'Address then | |
4769 | -- (X'Address + X'Size - 1) >= Y'Address | |
4770 | -- else | |
4771 | -- (Y'Address + Y'Size - 1) >= X'Address)) | |
2d42e881 ES |
4772 | |
4773 | -- with the proper address operations. We convert addresses to | |
4774 | -- integer addresses to use predefined arithmetic. The size is | |
90b510e4 AC |
4775 | -- expressed in storage units. We add copies of X_Addr and Y_Addr |
4776 | -- to prevent the appearance of the same node in two places in | |
4777 | -- the tree. | |
2d42e881 ES |
4778 | |
4779 | X_Addr := | |
4780 | Unchecked_Convert_To (RTE (RE_Integer_Address), | |
4781 | Make_Attribute_Reference (Loc, | |
4782 | Attribute_Name => Name_Address, | |
4783 | Prefix => New_Copy_Tree (X))); | |
4784 | ||
4785 | Y_Addr := | |
4786 | Unchecked_Convert_To (RTE (RE_Integer_Address), | |
4787 | Make_Attribute_Reference (Loc, | |
4788 | Attribute_Name => Name_Address, | |
4789 | Prefix => New_Copy_Tree (Y))); | |
4790 | ||
4791 | X_Size := | |
4792 | Make_Op_Divide (Loc, | |
4793 | Left_Opnd => | |
4794 | Make_Attribute_Reference (Loc, | |
4795 | Attribute_Name => Name_Size, | |
4796 | Prefix => New_Copy_Tree (X)), | |
4797 | Right_Opnd => | |
4798 | Make_Integer_Literal (Loc, System_Storage_Unit)); | |
4799 | ||
4800 | Y_Size := | |
4801 | Make_Op_Divide (Loc, | |
4802 | Left_Opnd => | |
4803 | Make_Attribute_Reference (Loc, | |
4804 | Attribute_Name => Name_Size, | |
4805 | Prefix => New_Copy_Tree (Y)), | |
4806 | Right_Opnd => | |
4807 | Make_Integer_Literal (Loc, System_Storage_Unit)); | |
4808 | ||
4809 | Cond := | |
4810 | Make_Op_Le (Loc, | |
4811 | Left_Opnd => X_Addr, | |
4812 | Right_Opnd => Y_Addr); | |
4813 | ||
d4fd4637 JS |
4814 | -- Perform the rewriting |
4815 | ||
2d42e881 | 4816 | Rewrite (N, |
9ba9f4c0 | 4817 | Make_If_Expression (Loc, New_List ( |
d4fd4637 | 4818 | |
c392a001 | 4819 | -- Generate a check for zero-sized things like a null record with |
d4fd4637 JS |
4820 | -- size zero or an array with zero length since they have no |
4821 | -- opportunity of overlapping. | |
4822 | ||
c392a001 GD |
4823 | -- Without this check, a zero-sized object can trigger a false |
4824 | -- runtime result if it's compared against another object in | |
4825 | -- its declarative region, due to the zero-sized object having | |
d4fd4637 JS |
4826 | -- the same address. |
4827 | ||
4828 | Make_Or_Else (Loc, | |
9ba9f4c0 | 4829 | Left_Opnd => |
d4fd4637 JS |
4830 | Make_Op_Eq (Loc, |
4831 | Left_Opnd => | |
4832 | Make_Attribute_Reference (Loc, | |
4833 | Attribute_Name => Name_Size, | |
4834 | Prefix => New_Copy_Tree (X)), | |
4835 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
4836 | Right_Opnd => | |
4837 | Make_Op_Eq (Loc, | |
4838 | Left_Opnd => | |
4839 | Make_Attribute_Reference (Loc, | |
4840 | Attribute_Name => Name_Size, | |
4841 | Prefix => New_Copy_Tree (Y)), | |
4842 | Right_Opnd => Make_Integer_Literal (Loc, 0))), | |
4843 | ||
4844 | New_Occurrence_Of (Standard_False, Loc), | |
4845 | ||
c392a001 | 4846 | -- Non-zero-size overlap check |
d4fd4637 JS |
4847 | |
4848 | Make_If_Expression (Loc, New_List ( | |
4849 | Cond, | |
4850 | ||
4851 | Make_Op_Ge (Loc, | |
4852 | Left_Opnd => | |
4853 | Make_Op_Add (Loc, | |
4854 | Left_Opnd => New_Copy_Tree (X_Addr), | |
4855 | Right_Opnd => | |
4856 | Make_Op_Subtract (Loc, | |
4857 | Left_Opnd => X_Size, | |
4858 | Right_Opnd => Make_Integer_Literal (Loc, 1))), | |
4859 | Right_Opnd => Y_Addr), | |
4860 | ||
4861 | Make_Op_Ge (Loc, | |
4862 | Left_Opnd => | |
4863 | Make_Op_Add (Loc, | |
4864 | Left_Opnd => New_Copy_Tree (Y_Addr), | |
4865 | Right_Opnd => | |
4866 | Make_Op_Subtract (Loc, | |
4867 | Left_Opnd => Y_Size, | |
4868 | Right_Opnd => Make_Integer_Literal (Loc, 1))), | |
4869 | Right_Opnd => X_Addr)))))); | |
2d42e881 ES |
4870 | |
4871 | Analyze_And_Resolve (N, Standard_Boolean); | |
4872 | end Overlaps_Storage; | |
4873 | ||
70482933 RK |
4874 | ------------ |
4875 | -- Output -- | |
4876 | ------------ | |
4877 | ||
4878 | when Attribute_Output => Output : declare | |
4879 | P_Type : constant Entity_Id := Entity (Pref); | |
70482933 RK |
4880 | U_Type : constant Entity_Id := Underlying_Type (P_Type); |
4881 | Pname : Entity_Id; | |
4882 | Decl : Node_Id; | |
4883 | Prag : Node_Id; | |
4884 | Arg3 : Node_Id; | |
4885 | Wfunc : Node_Id; | |
4886 | ||
4887 | begin | |
4888 | -- If no underlying type, we have an error that will be diagnosed | |
4889 | -- elsewhere, so here we just completely ignore the expansion. | |
4890 | ||
4891 | if No (U_Type) then | |
4892 | return; | |
4893 | end if; | |
4894 | ||
baa571ab AC |
4895 | -- Stream operations can appear in user code even if the restriction |
4896 | -- No_Streams is active (for example, when instantiating a predefined | |
4897 | -- container). In that case rewrite the attribute as a Raise to | |
4898 | -- prevent any run-time use. | |
4899 | ||
4900 | if Restriction_Active (No_Streams) then | |
4901 | Rewrite (N, | |
4902 | Make_Raise_Program_Error (Sloc (N), | |
b8b2d982 | 4903 | Reason => PE_Stream_Operation_Not_Allowed)); |
baa571ab AC |
4904 | Set_Etype (N, Standard_Void_Type); |
4905 | return; | |
4906 | end if; | |
4907 | ||
70482933 RK |
4908 | -- If TSS for Output is present, just call it |
4909 | ||
fbf5a39b | 4910 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output); |
70482933 RK |
4911 | |
4912 | if Present (Pname) then | |
4913 | null; | |
4914 | ||
4915 | else | |
4916 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
4917 | ||
4918 | -- sourcetyp'Output (stream, Item) | |
4919 | ||
4920 | -- as | |
4921 | ||
4922 | -- strmtyp'Output (Stream, strmwrite (acttyp (Item))); | |
4923 | ||
758c442c GD |
4924 | -- where strmwrite is the given Write function that converts an |
4925 | -- argument of type sourcetyp or a type acctyp, from which it is | |
4926 | -- derived to type strmtyp. The conversion to acttyp is required | |
4927 | -- for the derived case. | |
70482933 | 4928 | |
1d571f3b | 4929 | Prag := Get_Stream_Convert_Pragma (P_Type); |
70482933 RK |
4930 | |
4931 | if Present (Prag) then | |
4932 | Arg3 := | |
4933 | Next (Next (First (Pragma_Argument_Associations (Prag)))); | |
4934 | Wfunc := Entity (Expression (Arg3)); | |
4935 | ||
4936 | Rewrite (N, | |
4937 | Make_Attribute_Reference (Loc, | |
4938 | Prefix => New_Occurrence_Of (Etype (Wfunc), Loc), | |
4939 | Attribute_Name => Name_Output, | |
4940 | Expressions => New_List ( | |
4941 | Relocate_Node (First (Exprs)), | |
4942 | Make_Function_Call (Loc, | |
4943 | Name => New_Occurrence_Of (Wfunc, Loc), | |
4944 | Parameter_Associations => New_List ( | |
31104818 | 4945 | OK_Convert_To (Etype (First_Formal (Wfunc)), |
70482933 RK |
4946 | Relocate_Node (Next (First (Exprs))))))))); |
4947 | ||
4948 | Analyze (N); | |
4949 | return; | |
4950 | ||
890f1954 RD |
4951 | -- For elementary types, we call the W_xxx routine directly. Note |
4952 | -- that the effect of Write and Output is identical for the case | |
4953 | -- of an elementary type (there are no discriminants or bounds). | |
70482933 RK |
4954 | |
4955 | elsif Is_Elementary_Type (U_Type) then | |
4956 | ||
4957 | -- A special case arises if we have a defined _Write routine, | |
4958 | -- since in this case we are required to call this routine. | |
4959 | ||
a3fbecee EB |
4960 | if Present (Find_Inherited_TSS (P_Type, TSS_Stream_Write)) then |
4961 | Build_Record_Or_Elementary_Output_Procedure | |
4962 | (Loc, P_Type, Decl, Pname); | |
4963 | Insert_Action (N, Decl); | |
70482933 | 4964 | |
a3fbecee | 4965 | -- For normal cases, we call the W_xxx routine directly |
70482933 | 4966 | |
a3fbecee EB |
4967 | else |
4968 | Rewrite (N, Build_Elementary_Write_Call (N)); | |
4969 | Analyze (N); | |
4970 | return; | |
4971 | end if; | |
70482933 RK |
4972 | |
4973 | -- Array type case | |
4974 | ||
4975 | elsif Is_Array_Type (U_Type) then | |
4976 | Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname); | |
4977 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
4978 | ||
4979 | -- Class-wide case, first output external tag, then dispatch | |
4980 | -- to the appropriate primitive Output function (RM 13.13.2(31)). | |
4981 | ||
4982 | elsif Is_Class_Wide_Type (P_Type) then | |
0669bebe GB |
4983 | |
4984 | -- No need to do anything else compiling under restriction | |
4985 | -- No_Dispatching_Calls. During the semantic analysis we | |
4986 | -- already notified such violation. | |
4987 | ||
4988 | if Restriction_Active (No_Dispatching_Calls) then | |
4989 | return; | |
4990 | end if; | |
4991 | ||
70482933 RK |
4992 | Tag_Write : declare |
4993 | Strm : constant Node_Id := First (Exprs); | |
4994 | Item : constant Node_Id := Next (Strm); | |
4995 | ||
4996 | begin | |
31104818 HK |
4997 | -- Ada 2005 (AI-344): Check that the accessibility level |
4998 | -- of the type of the output object is not deeper than | |
4999 | -- that of the attribute's prefix type. | |
5000 | ||
758c442c GD |
5001 | -- if Get_Access_Level (Item'Tag) |
5002 | -- /= Get_Access_Level (P_Type'Tag) | |
5003 | -- then | |
5004 | -- raise Tag_Error; | |
5005 | -- end if; | |
31104818 | 5006 | |
758c442c GD |
5007 | -- String'Output (Strm, External_Tag (Item'Tag)); |
5008 | ||
31104818 HK |
5009 | -- We cannot figure out a practical way to implement this |
5010 | -- accessibility check on virtual machines, so we omit it. | |
758c442c | 5011 | |
0791fbe9 | 5012 | if Ada_Version >= Ada_2005 |
1f110335 | 5013 | and then Tagged_Type_Expansion |
31104818 | 5014 | then |
758c442c GD |
5015 | Insert_Action (N, |
5016 | Make_Implicit_If_Statement (N, | |
5017 | Condition => | |
5018 | Make_Op_Ne (Loc, | |
5019 | Left_Opnd => | |
0669bebe GB |
5020 | Build_Get_Access_Level (Loc, |
5021 | Make_Attribute_Reference (Loc, | |
5022 | Prefix => | |
5023 | Relocate_Node ( | |
5024 | Duplicate_Subexpr (Item, | |
5025 | Name_Req => True)), | |
5026 | Attribute_Name => Name_Tag)), | |
5027 | ||
758c442c | 5028 | Right_Opnd => |
0669bebe GB |
5029 | Make_Integer_Literal (Loc, |
5030 | Type_Access_Level (P_Type))), | |
5031 | ||
758c442c GD |
5032 | Then_Statements => |
5033 | New_List (Make_Raise_Statement (Loc, | |
5034 | New_Occurrence_Of ( | |
5035 | RTE (RE_Tag_Error), Loc))))); | |
5036 | end if; | |
70482933 RK |
5037 | |
5038 | Insert_Action (N, | |
5039 | Make_Attribute_Reference (Loc, | |
5040 | Prefix => New_Occurrence_Of (Standard_String, Loc), | |
5041 | Attribute_Name => Name_Output, | |
5042 | Expressions => New_List ( | |
5043 | Relocate_Node (Duplicate_Subexpr (Strm)), | |
5044 | Make_Function_Call (Loc, | |
5045 | Name => | |
5046 | New_Occurrence_Of (RTE (RE_External_Tag), Loc), | |
5047 | Parameter_Associations => New_List ( | |
5048 | Make_Attribute_Reference (Loc, | |
5049 | Prefix => | |
5050 | Relocate_Node | |
5051 | (Duplicate_Subexpr (Item, Name_Req => True)), | |
5052 | Attribute_Name => Name_Tag)))))); | |
5053 | end Tag_Write; | |
5054 | ||
fbf5a39b | 5055 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Output); |
70482933 RK |
5056 | |
5057 | -- Tagged type case, use the primitive Output function | |
5058 | ||
5059 | elsif Is_Tagged_Type (U_Type) then | |
fbf5a39b | 5060 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Output); |
70482933 | 5061 | |
0669bebe GB |
5062 | -- All other record type cases, including protected records. |
5063 | -- The latter only arise for expander generated code for | |
5064 | -- handling shared passive partition access. | |
70482933 RK |
5065 | |
5066 | else | |
5067 | pragma Assert | |
5068 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
5069 | ||
5d09245e AC |
5070 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
5071 | -- the default implementation of the Output attribute of an | |
5072 | -- unchecked union type if the type lacks default discriminant | |
5073 | -- values. | |
5074 | ||
5075 | if Is_Unchecked_Union (Base_Type (U_Type)) | |
80d4224f | 5076 | and then No (Discriminant_Constraint (U_Type)) |
5d09245e AC |
5077 | then |
5078 | Insert_Action (N, | |
5079 | Make_Raise_Program_Error (Loc, | |
5080 | Reason => PE_Unchecked_Union_Restriction)); | |
5081 | ||
5082 | return; | |
5083 | end if; | |
5084 | ||
70482933 RK |
5085 | Build_Record_Or_Elementary_Output_Procedure |
5086 | (Loc, Base_Type (U_Type), Decl, Pname); | |
5087 | Insert_Action (N, Decl); | |
5088 | end if; | |
5089 | end if; | |
5090 | ||
5091 | -- If we fall through, Pname is the name of the procedure to call | |
5092 | ||
110d0820 | 5093 | Rewrite_Attribute_Proc_Call (Pname); |
70482933 RK |
5094 | end Output; |
5095 | ||
5096 | --------- | |
5097 | -- Pos -- | |
5098 | --------- | |
5099 | ||
47d3b920 AC |
5100 | -- For enumeration types, with a non-standard representation we generate |
5101 | -- a call to the _Rep_To_Pos function created when the type was frozen. | |
10e168cd | 5102 | -- The call has the form: |
70482933 | 5103 | |
fbf5a39b | 5104 | -- _rep_to_pos (expr, flag) |
70482933 | 5105 | |
fbf5a39b AC |
5106 | -- The parameter flag is True if range checks are enabled, causing |
5107 | -- Program_Error to be raised if the expression has an invalid | |
5108 | -- representation, and False if range checks are suppressed. | |
70482933 | 5109 | |
f193b29e EB |
5110 | -- For enumeration types with a standard representation, Pos can be |
5111 | -- rewritten as a simple conversion with Conversion_OK set. | |
5112 | ||
10e168cd EB |
5113 | -- For integer types, Pos is equivalent to a simple integer conversion |
5114 | -- and we rewrite it as such. | |
70482933 | 5115 | |
d8f43ee6 | 5116 | when Attribute_Pos => Pos : declare |
f193b29e | 5117 | Expr : constant Node_Id := First (Exprs); |
10e168cd | 5118 | Etyp : Entity_Id := Base_Type (Ptyp); |
70482933 RK |
5119 | |
5120 | begin | |
5121 | -- Deal with zero/non-zero boolean values | |
5122 | ||
5123 | if Is_Boolean_Type (Etyp) then | |
f193b29e | 5124 | Adjust_Condition (Expr); |
70482933 RK |
5125 | Etyp := Standard_Boolean; |
5126 | Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc)); | |
5127 | end if; | |
5128 | ||
5129 | -- Case of enumeration type | |
5130 | ||
5131 | if Is_Enumeration_Type (Etyp) then | |
5132 | ||
5133 | -- Non-standard enumeration type (generate call) | |
5134 | ||
5135 | if Present (Enum_Pos_To_Rep (Etyp)) then | |
fbf5a39b | 5136 | Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc)); |
70482933 RK |
5137 | Rewrite (N, |
5138 | Convert_To (Typ, | |
5139 | Make_Function_Call (Loc, | |
5140 | Name => | |
e4494292 | 5141 | New_Occurrence_Of (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
70482933 RK |
5142 | Parameter_Associations => Exprs))); |
5143 | ||
f193b29e | 5144 | -- Standard enumeration type (replace by conversion) |
70482933 | 5145 | |
f193b29e EB |
5146 | -- This is simply a direct conversion from the enumeration type to |
5147 | -- the target integer type, which is treated by the back end as a | |
5148 | -- normal integer conversion, treating the enumeration type as an | |
5149 | -- integer, which is exactly what we want. We set Conversion_OK to | |
5150 | -- make sure that the analyzer does not complain about what might | |
5151 | -- be an illegal conversion. | |
5152 | ||
5153 | -- However the target type is universal integer in most cases, | |
5154 | -- which is a very large type, so we first convert to a small | |
5155 | -- signed integer type in order not to lose the size information. | |
70482933 RK |
5156 | |
5157 | else | |
f193b29e EB |
5158 | Rewrite (N, OK_Convert_To (Get_Integer_Type (Ptyp), Expr)); |
5159 | Convert_To_And_Rewrite (Typ, N); | |
5160 | ||
70482933 RK |
5161 | end if; |
5162 | ||
5163 | -- Deal with integer types (replace by conversion) | |
5164 | ||
5165 | elsif Is_Integer_Type (Etyp) then | |
f193b29e | 5166 | Rewrite (N, Convert_To (Typ, Expr)); |
70482933 RK |
5167 | end if; |
5168 | ||
f193b29e | 5169 | Analyze_And_Resolve (N, Typ); |
70482933 RK |
5170 | end Pos; |
5171 | ||
5172 | -------------- | |
5173 | -- Position -- | |
5174 | -------------- | |
5175 | ||
12be130c EB |
5176 | -- We leave the computation up to the back end, since we don't know what |
5177 | -- layout will be chosen if no component clause was specified. | |
70482933 | 5178 | |
12be130c EB |
5179 | when Attribute_Position => |
5180 | Apply_Universal_Integer_Attribute_Checks (N); | |
70482933 RK |
5181 | |
5182 | ---------- | |
5183 | -- Pred -- | |
5184 | ---------- | |
5185 | ||
29049f0b AC |
5186 | -- 1. Deal with enumeration types with holes. |
5187 | -- 2. For floating-point, generate call to attribute function. | |
5188 | -- 3. For other cases, deal with constraint checking. | |
70482933 | 5189 | |
d8f43ee6 | 5190 | when Attribute_Pred => Pred : declare |
21d27997 | 5191 | Etyp : constant Entity_Id := Base_Type (Ptyp); |
79e267f9 | 5192 | Ityp : Entity_Id; |
70482933 RK |
5193 | |
5194 | begin | |
5195 | -- For enumeration types with non-standard representations, we | |
79e267f9 | 5196 | -- expand typ'Pred (x) into: |
70482933 RK |
5197 | |
5198 | -- Pos_To_Rep (Rep_To_Pos (x) - 1) | |
5199 | ||
79e267f9 EB |
5200 | -- if the representation is non-contiguous, and just x - 1 if it is |
5201 | -- after having dealt with constraint checking. | |
fbf5a39b | 5202 | |
79e267f9 | 5203 | if Is_Enumeration_Type (Etyp) |
21d27997 | 5204 | and then Present (Enum_Pos_To_Rep (Etyp)) |
70482933 | 5205 | then |
21d27997 | 5206 | if Has_Contiguous_Rep (Etyp) then |
79e267f9 EB |
5207 | if not Range_Checks_Suppressed (Ptyp) then |
5208 | Set_Do_Range_Check (First (Exprs), False); | |
5209 | Expand_Pred_Succ_Attribute (N); | |
5210 | end if; | |
5211 | ||
5212 | if Is_Unsigned_Type (Etyp) then | |
5213 | if Esize (Typ) <= Standard_Integer_Size then | |
5214 | Ityp := RTE (RE_Unsigned); | |
5215 | else | |
5216 | Ityp := RTE (RE_Long_Long_Unsigned); | |
5217 | end if; | |
fbf5a39b | 5218 | |
79e267f9 EB |
5219 | else |
5220 | if Esize (Etyp) <= Standard_Integer_Size then | |
5221 | Ityp := Standard_Integer; | |
5222 | else | |
5223 | Ityp := Standard_Long_Long_Integer; | |
5224 | end if; | |
5225 | end if; | |
5226 | ||
5227 | Rewrite (N, | |
5228 | Unchecked_Convert_To (Etyp, | |
5229 | Make_Op_Subtract (Loc, | |
5230 | Left_Opnd => | |
5231 | Unchecked_Convert_To (Ityp, First (Exprs)), | |
5232 | Right_Opnd => | |
5233 | Make_Integer_Literal (Loc, 1)))); | |
70482933 | 5234 | |
fbf5a39b | 5235 | else |
16b54914 | 5236 | -- Add Boolean parameter True, to request program error if |
fbf5a39b AC |
5237 | -- we have a bad representation on our hands. If checks are |
5238 | -- suppressed, then add False instead | |
70482933 | 5239 | |
fbf5a39b AC |
5240 | Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc)); |
5241 | Rewrite (N, | |
5242 | Make_Indexed_Component (Loc, | |
21d27997 | 5243 | Prefix => |
e4494292 | 5244 | New_Occurrence_Of |
21d27997 | 5245 | (Enum_Pos_To_Rep (Etyp), Loc), |
fbf5a39b AC |
5246 | Expressions => New_List ( |
5247 | Make_Op_Subtract (Loc, | |
70482933 RK |
5248 | Left_Opnd => |
5249 | Make_Function_Call (Loc, | |
5250 | Name => | |
e4494292 | 5251 | New_Occurrence_Of |
21d27997 | 5252 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
fbf5a39b | 5253 | Parameter_Associations => Exprs), |
70482933 | 5254 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); |
fbf5a39b | 5255 | end if; |
70482933 | 5256 | |
79e267f9 EB |
5257 | -- Suppress checks since they have all been done above |
5258 | ||
5259 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
70482933 RK |
5260 | |
5261 | -- For floating-point, we transform 'Pred into a call to the Pred | |
0083dd66 | 5262 | -- floating-point attribute function in Fat_xxx (xxx is root type). |
29049f0b | 5263 | -- Note that this function takes care of the overflow case. |
70482933 RK |
5264 | |
5265 | elsif Is_Floating_Point_Type (Ptyp) then | |
5266 | Expand_Fpt_Attribute_R (N); | |
5267 | Analyze_And_Resolve (N, Typ); | |
5268 | ||
5269 | -- For modular types, nothing to do (no overflow, since wraps) | |
5270 | ||
5271 | elsif Is_Modular_Integer_Type (Ptyp) then | |
5272 | null; | |
5273 | ||
d79e621a GD |
5274 | -- For other types, if argument is marked as needing a range check or |
5275 | -- overflow checking is enabled, we must generate a check. | |
70482933 | 5276 | |
d79e621a GD |
5277 | elsif not Overflow_Checks_Suppressed (Ptyp) |
5278 | or else Do_Range_Check (First (Exprs)) | |
5279 | then | |
5280 | Set_Do_Range_Check (First (Exprs), False); | |
aa9b151a | 5281 | Expand_Pred_Succ_Attribute (N); |
70482933 | 5282 | end if; |
70482933 RK |
5283 | end Pred; |
5284 | ||
7ce611e2 ES |
5285 | -------------- |
5286 | -- Priority -- | |
5287 | -------------- | |
5288 | ||
5289 | -- Ada 2005 (AI-327): Dynamic ceiling priorities | |
5290 | ||
5291 | -- We rewrite X'Priority as the following run-time call: | |
5292 | ||
5293 | -- Get_Ceiling (X._Object) | |
5294 | ||
5295 | -- Note that although X'Priority is notionally an object, it is quite | |
5296 | -- deliberately not defined as an aliased object in the RM. This means | |
5297 | -- that it works fine to rewrite it as a call, without having to worry | |
5298 | -- about complications that would other arise from X'Priority'Access, | |
5299 | -- which is illegal, because of the lack of aliasing. | |
5300 | ||
d8f43ee6 HK |
5301 | when Attribute_Priority => Priority : declare |
5302 | Call : Node_Id; | |
5303 | Conctyp : Entity_Id; | |
5304 | New_Itype : Entity_Id; | |
5305 | Object_Parm : Node_Id; | |
5306 | Subprg : Entity_Id; | |
5307 | RT_Subprg_Name : Node_Id; | |
7ce611e2 | 5308 | |
d8f43ee6 HK |
5309 | begin |
5310 | -- Look for the enclosing concurrent type | |
7ce611e2 | 5311 | |
d8f43ee6 HK |
5312 | Conctyp := Current_Scope; |
5313 | while not Is_Concurrent_Type (Conctyp) loop | |
5314 | Conctyp := Scope (Conctyp); | |
5315 | end loop; | |
7ce611e2 | 5316 | |
d8f43ee6 | 5317 | pragma Assert (Is_Protected_Type (Conctyp)); |
7ce611e2 | 5318 | |
d8f43ee6 | 5319 | -- Generate the actual of the call |
7ce611e2 | 5320 | |
d8f43ee6 HK |
5321 | Subprg := Current_Scope; |
5322 | while not Present (Protected_Body_Subprogram (Subprg)) loop | |
5323 | Subprg := Scope (Subprg); | |
5324 | end loop; | |
16f67b79 | 5325 | |
d8f43ee6 HK |
5326 | -- Use of 'Priority inside protected entries and barriers (in both |
5327 | -- cases the type of the first formal of their expanded subprogram | |
5328 | -- is Address) | |
16f67b79 | 5329 | |
d8f43ee6 HK |
5330 | if Etype (First_Entity (Protected_Body_Subprogram (Subprg))) = |
5331 | RTE (RE_Address) | |
5332 | then | |
5333 | -- In the expansion of protected entries the type of the first | |
5334 | -- formal of the Protected_Body_Subprogram is an Address. In order | |
5335 | -- to reference the _object component we generate: | |
16f67b79 | 5336 | |
d8f43ee6 HK |
5337 | -- type T is access p__ptTV; |
5338 | -- freeze T [] | |
16f67b79 | 5339 | |
d8f43ee6 HK |
5340 | New_Itype := Create_Itype (E_Access_Type, N); |
5341 | Set_Etype (New_Itype, New_Itype); | |
5342 | Set_Directly_Designated_Type (New_Itype, | |
5343 | Corresponding_Record_Type (Conctyp)); | |
5344 | Freeze_Itype (New_Itype, N); | |
16f67b79 | 5345 | |
d8f43ee6 HK |
5346 | -- Generate: |
5347 | -- T!(O)._object'unchecked_access | |
16f67b79 | 5348 | |
d8f43ee6 HK |
5349 | Object_Parm := |
5350 | Make_Attribute_Reference (Loc, | |
5351 | Prefix => | |
5352 | Make_Selected_Component (Loc, | |
5353 | Prefix => | |
5354 | Unchecked_Convert_To (New_Itype, | |
5355 | New_Occurrence_Of | |
5356 | (First_Entity (Protected_Body_Subprogram (Subprg)), | |
5357 | Loc)), | |
5358 | Selector_Name => Make_Identifier (Loc, Name_uObject)), | |
5359 | Attribute_Name => Name_Unchecked_Access); | |
16f67b79 | 5360 | |
d8f43ee6 | 5361 | -- Use of 'Priority inside a protected subprogram |
16f67b79 | 5362 | |
d8f43ee6 HK |
5363 | else |
5364 | Object_Parm := | |
5365 | Make_Attribute_Reference (Loc, | |
5366 | Prefix => | |
5367 | Make_Selected_Component (Loc, | |
5368 | Prefix => | |
5369 | New_Occurrence_Of | |
5370 | (First_Entity (Protected_Body_Subprogram (Subprg)), | |
5371 | Loc), | |
5372 | Selector_Name => Make_Identifier (Loc, Name_uObject)), | |
5373 | Attribute_Name => Name_Unchecked_Access); | |
5374 | end if; | |
7ce611e2 | 5375 | |
d8f43ee6 | 5376 | -- Select the appropriate run-time subprogram |
7ce611e2 | 5377 | |
d8f43ee6 HK |
5378 | if Number_Entries (Conctyp) = 0 then |
5379 | RT_Subprg_Name := New_Occurrence_Of (RTE (RE_Get_Ceiling), Loc); | |
5380 | else | |
5381 | RT_Subprg_Name := New_Occurrence_Of (RTE (RO_PE_Get_Ceiling), Loc); | |
5382 | end if; | |
7ce611e2 | 5383 | |
d8f43ee6 HK |
5384 | Call := |
5385 | Make_Function_Call (Loc, | |
5386 | Name => RT_Subprg_Name, | |
5387 | Parameter_Associations => New_List (Object_Parm)); | |
7ce611e2 | 5388 | |
d8f43ee6 | 5389 | Rewrite (N, Call); |
16f67b79 | 5390 | |
d8f43ee6 HK |
5391 | -- Avoid the generation of extra checks on the pointer to the |
5392 | -- protected object. | |
16f67b79 | 5393 | |
d8f43ee6 HK |
5394 | Analyze_And_Resolve (N, Typ, Suppress => Access_Check); |
5395 | end Priority; | |
7ce611e2 | 5396 | |
110d0820 BD |
5397 | --------------- |
5398 | -- Put_Image -- | |
5399 | --------------- | |
5400 | ||
5401 | when Attribute_Put_Image => Put_Image : declare | |
5402 | use Exp_Put_Image; | |
5403 | U_Type : constant Entity_Id := Underlying_Type (Entity (Pref)); | |
5404 | Pname : Entity_Id; | |
5405 | Decl : Node_Id; | |
5406 | ||
5407 | begin | |
5408 | -- If no underlying type, we have an error that will be diagnosed | |
5409 | -- elsewhere, so here we just completely ignore the expansion. | |
5410 | ||
5411 | if No (U_Type) then | |
5412 | return; | |
5413 | end if; | |
5414 | ||
c324c77e BD |
5415 | -- If there is a TSS for Put_Image, just call it. This is true for |
5416 | -- tagged types (if enabled) and if there is a user-specified | |
5417 | -- Put_Image. | |
110d0820 BD |
5418 | |
5419 | Pname := TSS (U_Type, TSS_Put_Image); | |
5420 | if No (Pname) then | |
5421 | if Is_Tagged_Type (U_Type) and then Is_Derived_Type (U_Type) then | |
5422 | Pname := Find_Optional_Prim_Op (U_Type, TSS_Put_Image); | |
110d0820 BD |
5423 | else |
5424 | Pname := Find_Inherited_TSS (U_Type, TSS_Put_Image); | |
5425 | end if; | |
5426 | end if; | |
5427 | ||
5428 | if No (Pname) then | |
c324c77e BD |
5429 | -- If Put_Image is disabled, call the "unknown" version |
5430 | ||
5431 | if not Enable_Put_Image (U_Type) then | |
5432 | Rewrite (N, Build_Unknown_Put_Image_Call (N)); | |
5433 | Analyze (N); | |
5434 | return; | |
5435 | ||
110d0820 BD |
5436 | -- For elementary types, we call the routine in System.Put_Images |
5437 | -- directly. | |
5438 | ||
c324c77e | 5439 | elsif Is_Elementary_Type (U_Type) then |
110d0820 BD |
5440 | Rewrite (N, Build_Elementary_Put_Image_Call (N)); |
5441 | Analyze (N); | |
5442 | return; | |
5443 | ||
eb725219 | 5444 | elsif Is_Standard_String_Type (U_Type) then |
110d0820 BD |
5445 | Rewrite (N, Build_String_Put_Image_Call (N)); |
5446 | Analyze (N); | |
5447 | return; | |
5448 | ||
5449 | elsif Is_Array_Type (U_Type) then | |
5450 | Build_Array_Put_Image_Procedure (N, U_Type, Decl, Pname); | |
5451 | Insert_Action (N, Decl); | |
5452 | ||
5453 | -- Tagged type case, use the primitive Put_Image function. Note | |
5454 | -- that this will dispatch in the class-wide case which is what we | |
5455 | -- want. | |
5456 | ||
5457 | elsif Is_Tagged_Type (U_Type) then | |
5458 | Pname := Find_Optional_Prim_Op (U_Type, TSS_Put_Image); | |
5459 | ||
5460 | -- ????Need Find_Optional_Prim_Op instead of Find_Prim_Op, | |
5461 | -- because we might be deriving from a predefined type, which | |
5462 | -- currently has Enable_Put_Image False. | |
5463 | ||
5464 | if No (Pname) then | |
5465 | Rewrite (N, Build_Unknown_Put_Image_Call (N)); | |
5466 | Analyze (N); | |
5467 | return; | |
5468 | end if; | |
5469 | ||
5470 | elsif Is_Protected_Type (U_Type) then | |
5471 | Rewrite (N, Build_Protected_Put_Image_Call (N)); | |
5472 | Analyze (N); | |
5473 | return; | |
5474 | ||
5475 | elsif Is_Task_Type (U_Type) then | |
5476 | Rewrite (N, Build_Task_Put_Image_Call (N)); | |
5477 | Analyze (N); | |
5478 | return; | |
5479 | ||
c324c77e | 5480 | -- All other record type cases |
110d0820 BD |
5481 | |
5482 | else | |
5483 | pragma Assert (Is_Record_Type (U_Type)); | |
110d0820 BD |
5484 | Build_Record_Put_Image_Procedure |
5485 | (Loc, Full_Base (U_Type), Decl, Pname); | |
5486 | Insert_Action (N, Decl); | |
5487 | end if; | |
5488 | end if; | |
5489 | ||
5490 | -- If we fall through, Pname is the procedure to be called | |
5491 | ||
5492 | Rewrite_Attribute_Proc_Call (Pname); | |
5493 | end Put_Image; | |
5494 | ||
70482933 RK |
5495 | ------------------ |
5496 | -- Range_Length -- | |
5497 | ------------------ | |
5498 | ||
d8f43ee6 | 5499 | when Attribute_Range_Length => |
47d3b920 | 5500 | |
70482933 RK |
5501 | -- The only special processing required is for the case where |
5502 | -- Range_Length is applied to an enumeration type with holes. | |
5503 | -- In this case we transform | |
5504 | ||
5505 | -- X'Range_Length | |
5506 | ||
5507 | -- to | |
5508 | ||
5509 | -- X'Pos (X'Last) - X'Pos (X'First) + 1 | |
5510 | ||
5511 | -- So that the result reflects the proper Pos values instead | |
5512 | -- of the underlying representations. | |
5513 | ||
21d27997 RD |
5514 | if Is_Enumeration_Type (Ptyp) |
5515 | and then Has_Non_Standard_Rep (Ptyp) | |
70482933 RK |
5516 | then |
5517 | Rewrite (N, | |
5518 | Make_Op_Add (Loc, | |
d8f43ee6 | 5519 | Left_Opnd => |
70482933 | 5520 | Make_Op_Subtract (Loc, |
d8f43ee6 | 5521 | Left_Opnd => |
70482933 RK |
5522 | Make_Attribute_Reference (Loc, |
5523 | Attribute_Name => Name_Pos, | |
d8f43ee6 HK |
5524 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
5525 | Expressions => New_List ( | |
70482933 RK |
5526 | Make_Attribute_Reference (Loc, |
5527 | Attribute_Name => Name_Last, | |
d8f43ee6 HK |
5528 | Prefix => |
5529 | New_Occurrence_Of (Ptyp, Loc)))), | |
70482933 RK |
5530 | |
5531 | Right_Opnd => | |
5532 | Make_Attribute_Reference (Loc, | |
5533 | Attribute_Name => Name_Pos, | |
d8f43ee6 HK |
5534 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
5535 | Expressions => New_List ( | |
70482933 RK |
5536 | Make_Attribute_Reference (Loc, |
5537 | Attribute_Name => Name_First, | |
d8f43ee6 HK |
5538 | Prefix => |
5539 | New_Occurrence_Of (Ptyp, Loc))))), | |
70482933 | 5540 | |
49d140bb | 5541 | Right_Opnd => Make_Integer_Literal (Loc, 1))); |
70482933 RK |
5542 | |
5543 | Analyze_And_Resolve (N, Typ); | |
5544 | ||
21d27997 RD |
5545 | -- For all other cases, the attribute is handled by the back end, but |
5546 | -- we need to deal with the case of the range check on a universal | |
5547 | -- integer. | |
70482933 RK |
5548 | |
5549 | else | |
5550 | Apply_Universal_Integer_Attribute_Checks (N); | |
5551 | end if; | |
70482933 | 5552 | |
3c08de34 ES |
5553 | ------------ |
5554 | -- Reduce -- | |
5555 | ------------ | |
5556 | ||
5557 | when Attribute_Reduce => | |
5558 | declare | |
5559 | Loc : constant Source_Ptr := Sloc (N); | |
5560 | E1 : constant Node_Id := First (Expressions (N)); | |
5561 | E2 : constant Node_Id := Next (E1); | |
5562 | Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N); | |
5563 | Typ : constant Entity_Id := Etype (N); | |
5564 | New_Loop : Node_Id; | |
5565 | ||
dab8e608 GD |
5566 | -- If the prefix is an aggregate, its unique component is an |
5567 | -- Iterated_Element, and we create a loop out of its iterator. | |
3c08de34 ES |
5568 | |
5569 | begin | |
5570 | if Nkind (Prefix (N)) = N_Aggregate then | |
5571 | declare | |
5572 | Stream : constant Node_Id := | |
dab8e608 | 5573 | First (Component_Associations (Prefix (N))); |
3c08de34 ES |
5574 | Id : constant Node_Id := Defining_Identifier (Stream); |
5575 | Expr : constant Node_Id := Expression (Stream); | |
5576 | Ch : constant Node_Id := | |
dab8e608 | 5577 | First (Discrete_Choices (Stream)); |
3c08de34 ES |
5578 | begin |
5579 | New_Loop := Make_Loop_Statement (Loc, | |
5580 | Iteration_Scheme => | |
5581 | Make_Iteration_Scheme (Loc, | |
5582 | Iterator_Specification => Empty, | |
5583 | Loop_Parameter_Specification => | |
5584 | Make_Loop_Parameter_Specification (Loc, | |
5585 | Defining_Identifier => New_Copy (Id), | |
5586 | Discrete_Subtype_Definition => | |
5587 | Relocate_Node (Ch))), | |
5588 | End_Label => Empty, | |
5589 | Statements => New_List ( | |
5590 | Make_Assignment_Statement (Loc, | |
5591 | Name => New_Occurrence_Of (Bnn, Loc), | |
5592 | Expression => Make_Function_Call (Loc, | |
5593 | Name => New_Occurrence_Of (Entity (E1), Loc), | |
5594 | Parameter_Associations => New_List ( | |
5595 | New_Occurrence_Of (Bnn, Loc), | |
5596 | Relocate_Node (Expr)))))); | |
5597 | end; | |
5598 | else | |
dab8e608 GD |
5599 | -- If the prefix is a name, we construct an element iterator |
5600 | -- over it. Its expansion will verify that it is an array or | |
5601 | -- a container with the proper aspects. | |
3c08de34 ES |
5602 | |
5603 | declare | |
5604 | Iter : Node_Id; | |
5605 | Elem : constant Entity_Id := Make_Temporary (Loc, 'E', N); | |
5606 | ||
5607 | begin | |
5608 | Iter := | |
5609 | Make_Iterator_Specification (Loc, | |
5610 | Defining_Identifier => Elem, | |
5611 | Name => Relocate_Node (Prefix (N)), | |
5612 | Subtype_Indication => Empty); | |
5613 | Set_Of_Present (Iter); | |
5614 | ||
5615 | New_Loop := Make_Loop_Statement (Loc, | |
5616 | Iteration_Scheme => | |
5617 | Make_Iteration_Scheme (Loc, | |
5618 | Iterator_Specification => Iter, | |
5619 | Loop_Parameter_Specification => Empty), | |
5620 | End_Label => Empty, | |
5621 | Statements => New_List ( | |
5622 | Make_Assignment_Statement (Loc, | |
5623 | Name => New_Occurrence_Of (Bnn, Loc), | |
5624 | Expression => Make_Function_Call (Loc, | |
5625 | Name => New_Occurrence_Of (Entity (E1), Loc), | |
5626 | Parameter_Associations => New_List ( | |
5627 | New_Occurrence_Of (Bnn, Loc), | |
5628 | New_Occurrence_Of (Elem, Loc)))))); | |
5629 | end; | |
5630 | end if; | |
5631 | ||
5632 | Rewrite (N, | |
5633 | Make_Expression_With_Actions (Loc, | |
5634 | Actions => New_List ( | |
5635 | Make_Object_Declaration (Loc, | |
5636 | Defining_Identifier => Bnn, | |
5637 | Object_Definition => | |
5638 | New_Occurrence_Of (Typ, Loc), | |
5639 | Expression => Relocate_Node (E2)), New_Loop), | |
5640 | Expression => New_Occurrence_Of (Bnn, Loc))); | |
5641 | Analyze_And_Resolve (N, Typ); | |
5642 | end; | |
5643 | ||
70482933 RK |
5644 | ---------- |
5645 | -- Read -- | |
5646 | ---------- | |
5647 | ||
5648 | when Attribute_Read => Read : declare | |
5649 | P_Type : constant Entity_Id := Entity (Pref); | |
5650 | B_Type : constant Entity_Id := Base_Type (P_Type); | |
5651 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
5652 | Pname : Entity_Id; | |
5653 | Decl : Node_Id; | |
5654 | Prag : Node_Id; | |
5655 | Arg2 : Node_Id; | |
5656 | Rfunc : Node_Id; | |
5657 | Lhs : Node_Id; | |
5658 | Rhs : Node_Id; | |
5659 | ||
5660 | begin | |
5661 | -- If no underlying type, we have an error that will be diagnosed | |
5662 | -- elsewhere, so here we just completely ignore the expansion. | |
5663 | ||
5664 | if No (U_Type) then | |
5665 | return; | |
5666 | end if; | |
5667 | ||
baa571ab AC |
5668 | -- Stream operations can appear in user code even if the restriction |
5669 | -- No_Streams is active (for example, when instantiating a predefined | |
5670 | -- container). In that case rewrite the attribute as a Raise to | |
5671 | -- prevent any run-time use. | |
5672 | ||
5673 | if Restriction_Active (No_Streams) then | |
5674 | Rewrite (N, | |
5675 | Make_Raise_Program_Error (Sloc (N), | |
b8b2d982 | 5676 | Reason => PE_Stream_Operation_Not_Allowed)); |
baa571ab AC |
5677 | Set_Etype (N, B_Type); |
5678 | return; | |
5679 | end if; | |
5680 | ||
70482933 RK |
5681 | -- The simple case, if there is a TSS for Read, just call it |
5682 | ||
fbf5a39b | 5683 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read); |
70482933 RK |
5684 | |
5685 | if Present (Pname) then | |
5686 | null; | |
5687 | ||
5688 | else | |
5689 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
5690 | ||
5691 | -- sourcetyp'Read (stream, Item) | |
5692 | ||
5693 | -- as | |
5694 | ||
5695 | -- Item := sourcetyp (strmread (strmtyp'Input (Stream))); | |
5696 | ||
758c442c GD |
5697 | -- where strmread is the given Read function that converts an |
5698 | -- argument of type strmtyp to type sourcetyp or a type from which | |
5699 | -- it is derived. The conversion to sourcetyp is required in the | |
5700 | -- latter case. | |
70482933 RK |
5701 | |
5702 | -- A special case arises if Item is a type conversion in which | |
5703 | -- case, we have to expand to: | |
5704 | ||
5705 | -- Itemx := typex (strmread (strmtyp'Input (Stream))); | |
5706 | ||
5707 | -- where Itemx is the expression of the type conversion (i.e. | |
5708 | -- the actual object), and typex is the type of Itemx. | |
5709 | ||
1d571f3b | 5710 | Prag := Get_Stream_Convert_Pragma (P_Type); |
70482933 RK |
5711 | |
5712 | if Present (Prag) then | |
5713 | Arg2 := Next (First (Pragma_Argument_Associations (Prag))); | |
5714 | Rfunc := Entity (Expression (Arg2)); | |
5715 | Lhs := Relocate_Node (Next (First (Exprs))); | |
5716 | Rhs := | |
31104818 | 5717 | OK_Convert_To (B_Type, |
70482933 RK |
5718 | Make_Function_Call (Loc, |
5719 | Name => New_Occurrence_Of (Rfunc, Loc), | |
5720 | Parameter_Associations => New_List ( | |
5721 | Make_Attribute_Reference (Loc, | |
5722 | Prefix => | |
5723 | New_Occurrence_Of | |
5724 | (Etype (First_Formal (Rfunc)), Loc), | |
5725 | Attribute_Name => Name_Input, | |
5726 | Expressions => New_List ( | |
5727 | Relocate_Node (First (Exprs))))))); | |
5728 | ||
5729 | if Nkind (Lhs) = N_Type_Conversion then | |
5730 | Lhs := Expression (Lhs); | |
5731 | Rhs := Convert_To (Etype (Lhs), Rhs); | |
5732 | end if; | |
5733 | ||
5734 | Rewrite (N, | |
5735 | Make_Assignment_Statement (Loc, | |
fbf5a39b | 5736 | Name => Lhs, |
70482933 RK |
5737 | Expression => Rhs)); |
5738 | Set_Assignment_OK (Lhs); | |
5739 | Analyze (N); | |
5740 | return; | |
5741 | ||
5742 | -- For elementary types, we call the I_xxx routine using the first | |
5743 | -- parameter and then assign the result into the second parameter. | |
5744 | -- We set Assignment_OK to deal with the conversion case. | |
5745 | ||
5746 | elsif Is_Elementary_Type (U_Type) then | |
5747 | declare | |
5748 | Lhs : Node_Id; | |
5749 | Rhs : Node_Id; | |
5750 | ||
5751 | begin | |
5752 | Lhs := Relocate_Node (Next (First (Exprs))); | |
5753 | Rhs := Build_Elementary_Input_Call (N); | |
5754 | ||
5755 | if Nkind (Lhs) = N_Type_Conversion then | |
5756 | Lhs := Expression (Lhs); | |
5757 | Rhs := Convert_To (Etype (Lhs), Rhs); | |
5758 | end if; | |
5759 | ||
5760 | Set_Assignment_OK (Lhs); | |
5761 | ||
5762 | Rewrite (N, | |
5763 | Make_Assignment_Statement (Loc, | |
49d140bb | 5764 | Name => Lhs, |
70482933 RK |
5765 | Expression => Rhs)); |
5766 | ||
5767 | Analyze (N); | |
5768 | return; | |
5769 | end; | |
5770 | ||
5771 | -- Array type case | |
5772 | ||
5773 | elsif Is_Array_Type (U_Type) then | |
5774 | Build_Array_Read_Procedure (N, U_Type, Decl, Pname); | |
5775 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
5776 | ||
5777 | -- Tagged type case, use the primitive Read function. Note that | |
5778 | -- this will dispatch in the class-wide case which is what we want | |
5779 | ||
5780 | elsif Is_Tagged_Type (U_Type) then | |
fbf5a39b | 5781 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Read); |
70482933 | 5782 | |
758c442c GD |
5783 | -- All other record type cases, including protected records. The |
5784 | -- latter only arise for expander generated code for handling | |
5785 | -- shared passive partition access. | |
70482933 RK |
5786 | |
5787 | else | |
5788 | pragma Assert | |
5789 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
5790 | ||
5d09245e AC |
5791 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
5792 | -- the default implementation of the Read attribute of an | |
1f70c47f AC |
5793 | -- Unchecked_Union type. We replace the attribute with a |
5794 | -- raise statement (rather than inserting it before) to handle | |
5795 | -- properly the case of an unchecked union that is a record | |
5796 | -- component. | |
5d09245e AC |
5797 | |
5798 | if Is_Unchecked_Union (Base_Type (U_Type)) then | |
1f70c47f | 5799 | Rewrite (N, |
5d09245e AC |
5800 | Make_Raise_Program_Error (Loc, |
5801 | Reason => PE_Unchecked_Union_Restriction)); | |
1f70c47f AC |
5802 | Set_Etype (N, B_Type); |
5803 | return; | |
5d09245e AC |
5804 | end if; |
5805 | ||
70482933 RK |
5806 | if Has_Discriminants (U_Type) |
5807 | and then Present | |
5808 | (Discriminant_Default_Value (First_Discriminant (U_Type))) | |
5809 | then | |
5810 | Build_Mutable_Record_Read_Procedure | |
96d2756f | 5811 | (Loc, Full_Base (U_Type), Decl, Pname); |
70482933 RK |
5812 | else |
5813 | Build_Record_Read_Procedure | |
96d2756f | 5814 | (Loc, Full_Base (U_Type), Decl, Pname); |
70482933 RK |
5815 | end if; |
5816 | ||
5817 | -- Suppress checks, uninitialized or otherwise invalid | |
5818 | -- data does not cause constraint errors to be raised for | |
5819 | -- a complete record read. | |
5820 | ||
5821 | Insert_Action (N, Decl, All_Checks); | |
5822 | end if; | |
5823 | end if; | |
5824 | ||
110d0820 | 5825 | Rewrite_Attribute_Proc_Call (Pname); |
70482933 RK |
5826 | end Read; |
5827 | ||
1b0b0f18 AC |
5828 | --------- |
5829 | -- Ref -- | |
5830 | --------- | |
5831 | ||
5832 | -- Ref is identical to To_Address, see To_Address for processing | |
5833 | ||
70482933 RK |
5834 | --------------- |
5835 | -- Remainder -- | |
5836 | --------------- | |
5837 | ||
5838 | -- Transforms 'Remainder into a call to the floating-point attribute | |
5839 | -- function Remainder in Fat_xxx (where xxx is the root type) | |
5840 | ||
5841 | when Attribute_Remainder => | |
5842 | Expand_Fpt_Attribute_RR (N); | |
5843 | ||
21d27997 RD |
5844 | ------------ |
5845 | -- Result -- | |
5846 | ------------ | |
5847 | ||
5848 | -- Transform 'Result into reference to _Result formal. At the point | |
5849 | -- where a legal 'Result attribute is expanded, we know that we are in | |
5850 | -- the context of a _Postcondition function with a _Result parameter. | |
5851 | ||
5852 | when Attribute_Result => | |
49d140bb | 5853 | Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult)); |
21d27997 RD |
5854 | Analyze_And_Resolve (N, Typ); |
5855 | ||
70482933 RK |
5856 | ----------- |
5857 | -- Round -- | |
5858 | ----------- | |
5859 | ||
758c442c GD |
5860 | -- The handling of the Round attribute is quite delicate. The processing |
5861 | -- in Sem_Attr introduced a conversion to universal real, reflecting the | |
5862 | -- semantics of Round, but we do not want anything to do with universal | |
5863 | -- real at runtime, since this corresponds to using floating-point | |
5864 | -- arithmetic. | |
5865 | ||
5866 | -- What we have now is that the Etype of the Round attribute correctly | |
5867 | -- indicates the final result type. The operand of the Round is the | |
5868 | -- conversion to universal real, described above, and the operand of | |
5869 | -- this conversion is the actual operand of Round, which may be the | |
5870 | -- special case of a fixed point multiplication or division (Etype = | |
5871 | -- universal fixed) | |
5872 | ||
5873 | -- The exapander will expand first the operand of the conversion, then | |
5874 | -- the conversion, and finally the round attribute itself, since we | |
5875 | -- always work inside out. But we cannot simply process naively in this | |
5876 | -- order. In the semantic world where universal fixed and real really | |
5877 | -- exist and have infinite precision, there is no problem, but in the | |
5878 | -- implementation world, where universal real is a floating-point type, | |
5879 | -- we would get the wrong result. | |
5880 | ||
5881 | -- So the approach is as follows. First, when expanding a multiply or | |
5882 | -- divide whose type is universal fixed, we do nothing at all, instead | |
5883 | -- deferring the operation till later. | |
70482933 RK |
5884 | |
5885 | -- The actual processing is done in Expand_N_Type_Conversion which | |
758c442c GD |
5886 | -- handles the special case of Round by looking at its parent to see if |
5887 | -- it is a Round attribute, and if it is, handling the conversion (or | |
5888 | -- its fixed multiply/divide child) in an appropriate manner. | |
70482933 RK |
5889 | |
5890 | -- This means that by the time we get to expanding the Round attribute | |
5891 | -- itself, the Round is nothing more than a type conversion (and will | |
5892 | -- often be a null type conversion), so we just replace it with the | |
5893 | -- appropriate conversion operation. | |
5894 | ||
5895 | when Attribute_Round => | |
5896 | Rewrite (N, | |
5897 | Convert_To (Etype (N), Relocate_Node (First (Exprs)))); | |
5898 | Analyze_And_Resolve (N); | |
5899 | ||
5900 | -------------- | |
5901 | -- Rounding -- | |
5902 | -------------- | |
5903 | ||
5904 | -- Transforms 'Rounding into a call to the floating-point attribute | |
5905 | -- function Rounding in Fat_xxx (where xxx is the root type) | |
24228312 | 5906 | -- Expansion is avoided for cases the back end can handle directly. |
70482933 RK |
5907 | |
5908 | when Attribute_Rounding => | |
24228312 AC |
5909 | if not Is_Inline_Floating_Point_Attribute (N) then |
5910 | Expand_Fpt_Attribute_R (N); | |
5911 | end if; | |
70482933 RK |
5912 | |
5913 | ------------- | |
5914 | -- Scaling -- | |
5915 | ------------- | |
5916 | ||
5917 | -- Transforms 'Scaling into a call to the floating-point attribute | |
5918 | -- function Scaling in Fat_xxx (where xxx is the root type) | |
5919 | ||
5920 | when Attribute_Scaling => | |
5921 | Expand_Fpt_Attribute_RI (N); | |
5922 | ||
a8551b5f AC |
5923 | ------------------------- |
5924 | -- Simple_Storage_Pool -- | |
5925 | ------------------------- | |
5926 | ||
5927 | when Attribute_Simple_Storage_Pool => | |
5928 | Rewrite (N, | |
5929 | Make_Type_Conversion (Loc, | |
e4494292 RD |
5930 | Subtype_Mark => New_Occurrence_Of (Etype (N), Loc), |
5931 | Expression => New_Occurrence_Of (Entity (N), Loc))); | |
a8551b5f AC |
5932 | Analyze_And_Resolve (N, Typ); |
5933 | ||
70482933 RK |
5934 | ---------- |
5935 | -- Size -- | |
5936 | ---------- | |
5937 | ||
d8f43ee6 HK |
5938 | when Attribute_Object_Size |
5939 | | Attribute_Size | |
5940 | | Attribute_Value_Size | |
5941 | | Attribute_VADS_Size | |
5942 | => | |
5943 | Size : declare | |
d8f43ee6 | 5944 | New_Node : Node_Id; |
70482933 | 5945 | |
d8f43ee6 HK |
5946 | begin |
5947 | -- Processing for VADS_Size case. Note that this processing | |
5948 | -- removes all traces of VADS_Size from the tree, and completes | |
5949 | -- all required processing for VADS_Size by translating the | |
5950 | -- attribute reference to an appropriate Size or Object_Size | |
5951 | -- reference. | |
5952 | ||
5953 | if Id = Attribute_VADS_Size | |
5954 | or else (Use_VADS_Size and then Id = Attribute_Size) | |
70482933 | 5955 | then |
d8f43ee6 HK |
5956 | -- If the size is specified, then we simply use the specified |
5957 | -- size. This applies to both types and objects. The size of an | |
5958 | -- object can be specified in the following ways: | |
5959 | ||
5960 | -- An explicit size object is given for an object | |
5961 | -- A component size is specified for an indexed component | |
5962 | -- A component clause is specified for a selected component | |
5963 | -- The object is a component of a packed composite object | |
5964 | ||
5965 | -- If the size is specified, then VADS_Size of an object | |
5966 | ||
5967 | if (Is_Entity_Name (Pref) | |
5968 | and then Present (Size_Clause (Entity (Pref)))) | |
5969 | or else | |
5970 | (Nkind (Pref) = N_Component_Clause | |
5971 | and then (Present (Component_Clause | |
5972 | (Entity (Selector_Name (Pref)))) | |
5973 | or else Is_Packed (Etype (Prefix (Pref))))) | |
5974 | or else | |
5975 | (Nkind (Pref) = N_Indexed_Component | |
5976 | and then (Component_Size (Etype (Prefix (Pref))) /= 0 | |
5977 | or else Is_Packed (Etype (Prefix (Pref))))) | |
5978 | then | |
5979 | Set_Attribute_Name (N, Name_Size); | |
70482933 | 5980 | |
d8f43ee6 HK |
5981 | -- Otherwise if we have an object rather than a type, then |
5982 | -- the VADS_Size attribute applies to the type of the object, | |
5983 | -- rather than the object itself. This is one of the respects | |
5984 | -- in which VADS_Size differs from Size. | |
70482933 | 5985 | |
d8f43ee6 HK |
5986 | else |
5987 | if (not Is_Entity_Name (Pref) | |
5988 | or else not Is_Type (Entity (Pref))) | |
5989 | and then (Is_Scalar_Type (Ptyp) | |
5990 | or else Is_Constrained (Ptyp)) | |
5991 | then | |
5992 | Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc)); | |
5993 | end if; | |
70482933 | 5994 | |
d8f43ee6 HK |
5995 | -- For a scalar type for which no size was explicitly given, |
5996 | -- VADS_Size means Object_Size. This is the other respect in | |
5997 | -- which VADS_Size differs from Size. | |
70482933 | 5998 | |
d8f43ee6 HK |
5999 | if Is_Scalar_Type (Ptyp) |
6000 | and then No (Size_Clause (Ptyp)) | |
6001 | then | |
6002 | Set_Attribute_Name (N, Name_Object_Size); | |
70482933 | 6003 | |
d8f43ee6 | 6004 | -- In all other cases, Size and VADS_Size are the sane |
70482933 | 6005 | |
d8f43ee6 HK |
6006 | else |
6007 | Set_Attribute_Name (N, Name_Size); | |
6008 | end if; | |
70482933 RK |
6009 | end if; |
6010 | end if; | |
70482933 | 6011 | |
d8f43ee6 HK |
6012 | -- If the prefix is X'Class, transform it into a direct reference |
6013 | -- to the class-wide type, because the back end must not see a | |
6014 | -- 'Class reference. | |
70482933 | 6015 | |
d8f43ee6 HK |
6016 | if Is_Entity_Name (Pref) |
6017 | and then Is_Class_Wide_Type (Entity (Pref)) | |
6018 | then | |
6019 | Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc)); | |
6020 | return; | |
fbf5a39b | 6021 | |
d8f43ee6 HK |
6022 | -- For X'Size applied to an object of a class-wide type, transform |
6023 | -- X'Size into a call to the primitive operation _Size applied to | |
6024 | -- X. | |
fbf5a39b | 6025 | |
d8f43ee6 | 6026 | elsif Is_Class_Wide_Type (Ptyp) then |
e23e04db | 6027 | |
d8f43ee6 HK |
6028 | -- No need to do anything else compiling under restriction |
6029 | -- No_Dispatching_Calls. During the semantic analysis we | |
6030 | -- already noted this restriction violation. | |
0669bebe | 6031 | |
d8f43ee6 HK |
6032 | if Restriction_Active (No_Dispatching_Calls) then |
6033 | return; | |
6034 | end if; | |
0669bebe | 6035 | |
d8f43ee6 HK |
6036 | New_Node := |
6037 | Make_Function_Call (Loc, | |
6038 | Name => | |
6039 | New_Occurrence_Of (Find_Prim_Op (Ptyp, Name_uSize), Loc), | |
6040 | Parameter_Associations => New_List (Pref)); | |
70482933 | 6041 | |
d8f43ee6 | 6042 | if Typ /= Standard_Long_Long_Integer then |
70482933 | 6043 | |
d8f43ee6 HK |
6044 | -- The context is a specific integer type with which the |
6045 | -- original attribute was compatible. The function has a | |
6046 | -- specific type as well, so to preserve the compatibility | |
6047 | -- we must convert explicitly. | |
70482933 | 6048 | |
d8f43ee6 HK |
6049 | New_Node := Convert_To (Typ, New_Node); |
6050 | end if; | |
70482933 | 6051 | |
d8f43ee6 HK |
6052 | Rewrite (N, New_Node); |
6053 | Analyze_And_Resolve (N, Typ); | |
6054 | return; | |
d8f43ee6 | 6055 | end if; |
70482933 | 6056 | |
d39f6b24 YM |
6057 | -- Call Expand_Size_Attribute to do the final part of the |
6058 | -- expansion which is shared with GNATprove expansion. | |
70482933 | 6059 | |
d39f6b24 | 6060 | Expand_Size_Attribute (N); |
d8f43ee6 | 6061 | end Size; |
70482933 RK |
6062 | |
6063 | ------------------ | |
6064 | -- Storage_Pool -- | |
6065 | ------------------ | |
6066 | ||
6067 | when Attribute_Storage_Pool => | |
6068 | Rewrite (N, | |
6069 | Make_Type_Conversion (Loc, | |
e4494292 RD |
6070 | Subtype_Mark => New_Occurrence_Of (Etype (N), Loc), |
6071 | Expression => New_Occurrence_Of (Entity (N), Loc))); | |
70482933 RK |
6072 | Analyze_And_Resolve (N, Typ); |
6073 | ||
6074 | ------------------ | |
6075 | -- Storage_Size -- | |
6076 | ------------------ | |
6077 | ||
a8551b5f AC |
6078 | when Attribute_Storage_Size => Storage_Size : declare |
6079 | Alloc_Op : Entity_Id := Empty; | |
6080 | ||
6081 | begin | |
70482933 | 6082 | |
70482933 RK |
6083 | -- Access type case, always go to the root type |
6084 | ||
6085 | -- The case of access types results in a value of zero for the case | |
6086 | -- where no storage size attribute clause has been given. If a | |
6087 | -- storage size has been given, then the attribute is converted | |
6088 | -- to a reference to the variable used to hold this value. | |
6089 | ||
6090 | if Is_Access_Type (Ptyp) then | |
6091 | if Present (Storage_Size_Variable (Root_Type (Ptyp))) then | |
6092 | Rewrite (N, | |
6a987d78 EB |
6093 | Convert_To (Typ, |
6094 | Make_Attribute_Reference (Loc, | |
6095 | Prefix => New_Occurrence_Of | |
6096 | (Etype (Storage_Size_Variable (Root_Type (Ptyp))), Loc), | |
6097 | Attribute_Name => Name_Max, | |
6098 | Expressions => New_List ( | |
6099 | Make_Integer_Literal (Loc, 0), | |
e4494292 | 6100 | New_Occurrence_Of |
70482933 RK |
6101 | (Storage_Size_Variable (Root_Type (Ptyp)), Loc))))); |
6102 | ||
6103 | elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then | |
7ce611e2 | 6104 | |
a8551b5f AC |
6105 | -- If the access type is associated with a simple storage pool |
6106 | -- object, then attempt to locate the optional Storage_Size | |
6107 | -- function of the simple storage pool type. If not found, | |
6108 | -- then the result will default to zero. | |
6109 | ||
6110 | if Present (Get_Rep_Pragma (Root_Type (Ptyp), | |
f6205414 | 6111 | Name_Simple_Storage_Pool_Type)) |
a8551b5f AC |
6112 | then |
6113 | declare | |
6114 | Pool_Type : constant Entity_Id := | |
6115 | Base_Type (Etype (Entity (N))); | |
6116 | ||
6117 | begin | |
6118 | Alloc_Op := Get_Name_Entity_Id (Name_Storage_Size); | |
6119 | while Present (Alloc_Op) loop | |
6120 | if Scope (Alloc_Op) = Scope (Pool_Type) | |
6121 | and then Present (First_Formal (Alloc_Op)) | |
6122 | and then Etype (First_Formal (Alloc_Op)) = Pool_Type | |
6123 | then | |
6124 | exit; | |
6125 | end if; | |
6126 | ||
6127 | Alloc_Op := Homonym (Alloc_Op); | |
6128 | end loop; | |
6129 | end; | |
6130 | ||
6131 | -- In the normal Storage_Pool case, retrieve the primitive | |
6132 | -- function associated with the pool type. | |
6133 | ||
6134 | else | |
6135 | Alloc_Op := | |
6136 | Find_Prim_Op | |
6137 | (Etype (Associated_Storage_Pool (Root_Type (Ptyp))), | |
6138 | Attribute_Name (N)); | |
6139 | end if; | |
6140 | ||
6141 | -- If Storage_Size wasn't found (can only occur in the simple | |
6142 | -- storage pool case), then simply use zero for the result. | |
6143 | ||
6144 | if not Present (Alloc_Op) then | |
6145 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
6146 | ||
6147 | -- Otherwise, rewrite the allocator as a call to pool type's | |
6148 | -- Storage_Size function. | |
6149 | ||
6150 | else | |
6151 | Rewrite (N, | |
6a987d78 | 6152 | Convert_To (Typ, |
a8551b5f AC |
6153 | Make_Function_Call (Loc, |
6154 | Name => | |
e4494292 | 6155 | New_Occurrence_Of (Alloc_Op, Loc), |
a8551b5f AC |
6156 | |
6157 | Parameter_Associations => New_List ( | |
e4494292 | 6158 | New_Occurrence_Of |
a8551b5f AC |
6159 | (Associated_Storage_Pool |
6160 | (Root_Type (Ptyp)), Loc))))); | |
6161 | end if; | |
70482933 | 6162 | |
70482933 RK |
6163 | else |
6164 | Rewrite (N, Make_Integer_Literal (Loc, 0)); | |
6165 | end if; | |
6166 | ||
6167 | Analyze_And_Resolve (N, Typ); | |
6168 | ||
7ce611e2 ES |
6169 | -- For tasks, we retrieve the size directly from the TCB. The |
6170 | -- size may depend on a discriminant of the type, and therefore | |
6171 | -- can be a per-object expression, so type-level information is | |
6172 | -- not sufficient in general. There are four cases to consider: | |
70482933 | 6173 | |
7ce611e2 ES |
6174 | -- a) If the attribute appears within a task body, the designated |
6175 | -- TCB is obtained by a call to Self. | |
70482933 | 6176 | |
7ce611e2 ES |
6177 | -- b) If the prefix of the attribute is the name of a task object, |
6178 | -- the designated TCB is the one stored in the corresponding record. | |
70482933 | 6179 | |
7ce611e2 ES |
6180 | -- c) If the prefix is a task type, the size is obtained from the |
6181 | -- size variable created for each task type | |
70482933 | 6182 | |
f145ece7 | 6183 | -- d) If no Storage_Size was specified for the type, there is no |
7ce611e2 | 6184 | -- size variable, and the value is a system-specific default. |
70482933 RK |
6185 | |
6186 | else | |
7ce611e2 ES |
6187 | if In_Open_Scopes (Ptyp) then |
6188 | ||
6189 | -- Storage_Size (Self) | |
6190 | ||
70482933 RK |
6191 | Rewrite (N, |
6192 | Convert_To (Typ, | |
6193 | Make_Function_Call (Loc, | |
6194 | Name => | |
7ce611e2 ES |
6195 | New_Occurrence_Of (RTE (RE_Storage_Size), Loc), |
6196 | Parameter_Associations => | |
6197 | New_List ( | |
6198 | Make_Function_Call (Loc, | |
6199 | Name => | |
e4494292 | 6200 | New_Occurrence_Of (RTE (RE_Self), Loc)))))); |
70482933 | 6201 | |
7ce611e2 ES |
6202 | elsif not Is_Entity_Name (Pref) |
6203 | or else not Is_Type (Entity (Pref)) | |
6204 | then | |
6205 | -- Storage_Size (Rec (Obj).Size) | |
6206 | ||
6207 | Rewrite (N, | |
6208 | Convert_To (Typ, | |
6209 | Make_Function_Call (Loc, | |
6210 | Name => | |
6211 | New_Occurrence_Of (RTE (RE_Storage_Size), Loc), | |
6212 | Parameter_Associations => | |
70482933 RK |
6213 | New_List ( |
6214 | Make_Selected_Component (Loc, | |
6215 | Prefix => | |
6216 | Unchecked_Convert_To ( | |
6217 | Corresponding_Record_Type (Ptyp), | |
7ce611e2 | 6218 | New_Copy_Tree (Pref)), |
70482933 | 6219 | Selector_Name => |
7ce611e2 | 6220 | Make_Identifier (Loc, Name_uTask_Id)))))); |
70482933 | 6221 | |
7ce611e2 | 6222 | elsif Present (Storage_Size_Variable (Ptyp)) then |
70482933 | 6223 | |
f145ece7 | 6224 | -- Static Storage_Size pragma given for type: retrieve value |
7ce611e2 | 6225 | -- from its allocated storage variable. |
70482933 | 6226 | |
7ce611e2 ES |
6227 | Rewrite (N, |
6228 | Convert_To (Typ, | |
6229 | Make_Function_Call (Loc, | |
6230 | Name => New_Occurrence_Of ( | |
6231 | RTE (RE_Adjust_Storage_Size), Loc), | |
6232 | Parameter_Associations => | |
6233 | New_List ( | |
e4494292 | 6234 | New_Occurrence_Of ( |
7ce611e2 ES |
6235 | Storage_Size_Variable (Ptyp), Loc))))); |
6236 | else | |
6237 | -- Get system default | |
6238 | ||
6239 | Rewrite (N, | |
6240 | Convert_To (Typ, | |
6241 | Make_Function_Call (Loc, | |
6242 | Name => | |
6243 | New_Occurrence_Of ( | |
6244 | RTE (RE_Default_Stack_Size), Loc)))); | |
70482933 | 6245 | end if; |
7ce611e2 ES |
6246 | |
6247 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
6248 | end if; |
6249 | end Storage_Size; | |
6250 | ||
82c80734 RD |
6251 | ----------------- |
6252 | -- Stream_Size -- | |
6253 | ----------------- | |
6254 | ||
9eea4346 GB |
6255 | when Attribute_Stream_Size => |
6256 | Rewrite (N, | |
6257 | Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp))); | |
82c80734 | 6258 | Analyze_And_Resolve (N, Typ); |
82c80734 | 6259 | |
70482933 RK |
6260 | ---------- |
6261 | -- Succ -- | |
6262 | ---------- | |
6263 | ||
29049f0b AC |
6264 | -- 1. Deal with enumeration types with holes. |
6265 | -- 2. For floating-point, generate call to attribute function. | |
6266 | -- 3. For other cases, deal with constraint checking. | |
70482933 | 6267 | |
47d3b920 | 6268 | when Attribute_Succ => Succ : declare |
21d27997 | 6269 | Etyp : constant Entity_Id := Base_Type (Ptyp); |
79e267f9 | 6270 | Ityp : Entity_Id; |
70482933 RK |
6271 | |
6272 | begin | |
6273 | -- For enumeration types with non-standard representations, we | |
79e267f9 | 6274 | -- expand typ'Pred (x) into: |
70482933 RK |
6275 | |
6276 | -- Pos_To_Rep (Rep_To_Pos (x) + 1) | |
6277 | ||
79e267f9 EB |
6278 | -- if the representation is non-contiguous, and just x + 1 if it is |
6279 | -- after having dealt with constraint checking. | |
fbf5a39b | 6280 | |
79e267f9 | 6281 | if Is_Enumeration_Type (Etyp) |
21d27997 | 6282 | and then Present (Enum_Pos_To_Rep (Etyp)) |
70482933 | 6283 | then |
21d27997 | 6284 | if Has_Contiguous_Rep (Etyp) then |
79e267f9 EB |
6285 | if not Range_Checks_Suppressed (Ptyp) then |
6286 | Set_Do_Range_Check (First (Exprs), False); | |
6287 | Expand_Pred_Succ_Attribute (N); | |
6288 | end if; | |
6289 | ||
6290 | if Is_Unsigned_Type (Etyp) then | |
6291 | if Esize (Typ) <= Standard_Integer_Size then | |
6292 | Ityp := RTE (RE_Unsigned); | |
6293 | else | |
6294 | Ityp := RTE (RE_Long_Long_Unsigned); | |
6295 | end if; | |
6296 | ||
6297 | else | |
6298 | if Esize (Etyp) <= Standard_Integer_Size then | |
6299 | Ityp := Standard_Integer; | |
6300 | else | |
6301 | Ityp := Standard_Long_Long_Integer; | |
6302 | end if; | |
6303 | end if; | |
6304 | ||
fbf5a39b | 6305 | Rewrite (N, |
79e267f9 EB |
6306 | Unchecked_Convert_To (Etyp, |
6307 | Make_Op_Add (Loc, | |
6308 | Left_Opnd => | |
6309 | Unchecked_Convert_To (Ityp, First (Exprs)), | |
6310 | Right_Opnd => | |
6311 | Make_Integer_Literal (Loc, 1)))); | |
fbf5a39b | 6312 | |
fbf5a39b | 6313 | else |
16b54914 | 6314 | -- Add Boolean parameter True, to request program error if |
fbf5a39b AC |
6315 | -- we have a bad representation on our hands. Add False if |
6316 | -- checks are suppressed. | |
70482933 | 6317 | |
fbf5a39b AC |
6318 | Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc)); |
6319 | Rewrite (N, | |
6320 | Make_Indexed_Component (Loc, | |
21d27997 | 6321 | Prefix => |
e4494292 | 6322 | New_Occurrence_Of |
21d27997 | 6323 | (Enum_Pos_To_Rep (Etyp), Loc), |
fbf5a39b AC |
6324 | Expressions => New_List ( |
6325 | Make_Op_Add (Loc, | |
6326 | Left_Opnd => | |
6327 | Make_Function_Call (Loc, | |
6328 | Name => | |
e4494292 | 6329 | New_Occurrence_Of |
21d27997 | 6330 | (TSS (Etyp, TSS_Rep_To_Pos), Loc), |
fbf5a39b AC |
6331 | Parameter_Associations => Exprs), |
6332 | Right_Opnd => Make_Integer_Literal (Loc, 1))))); | |
6333 | end if; | |
70482933 | 6334 | |
79e267f9 EB |
6335 | -- Suppress checks since they have all been done above |
6336 | ||
6337 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
70482933 RK |
6338 | |
6339 | -- For floating-point, we transform 'Succ into a call to the Succ | |
6340 | -- floating-point attribute function in Fat_xxx (xxx is root type) | |
6341 | ||
6342 | elsif Is_Floating_Point_Type (Ptyp) then | |
6343 | Expand_Fpt_Attribute_R (N); | |
6344 | Analyze_And_Resolve (N, Typ); | |
6345 | ||
6346 | -- For modular types, nothing to do (no overflow, since wraps) | |
6347 | ||
6348 | elsif Is_Modular_Integer_Type (Ptyp) then | |
6349 | null; | |
6350 | ||
d79e621a GD |
6351 | -- For other types, if argument is marked as needing a range check or |
6352 | -- overflow checking is enabled, we must generate a check. | |
70482933 | 6353 | |
d79e621a GD |
6354 | elsif not Overflow_Checks_Suppressed (Ptyp) |
6355 | or else Do_Range_Check (First (Exprs)) | |
6356 | then | |
6357 | Set_Do_Range_Check (First (Exprs), False); | |
aa9b151a | 6358 | Expand_Pred_Succ_Attribute (N); |
70482933 RK |
6359 | end if; |
6360 | end Succ; | |
6361 | ||
6362 | --------- | |
6363 | -- Tag -- | |
6364 | --------- | |
6365 | ||
6366 | -- Transforms X'Tag into a direct reference to the tag of X | |
6367 | ||
47d3b920 | 6368 | when Attribute_Tag => Tag : declare |
70482933 RK |
6369 | Ttyp : Entity_Id; |
6370 | Prefix_Is_Type : Boolean; | |
6371 | ||
6372 | begin | |
6373 | if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then | |
6374 | Ttyp := Entity (Pref); | |
6375 | Prefix_Is_Type := True; | |
6376 | else | |
21d27997 | 6377 | Ttyp := Ptyp; |
70482933 RK |
6378 | Prefix_Is_Type := False; |
6379 | end if; | |
6380 | ||
6381 | if Is_Class_Wide_Type (Ttyp) then | |
6382 | Ttyp := Root_Type (Ttyp); | |
6383 | end if; | |
6384 | ||
6385 | Ttyp := Underlying_Type (Ttyp); | |
6386 | ||
8a78c50d AC |
6387 | -- Ada 2005: The type may be a synchronized tagged type, in which |
6388 | -- case the tag information is stored in the corresponding record. | |
6389 | ||
6390 | if Is_Concurrent_Type (Ttyp) then | |
6391 | Ttyp := Corresponding_Record_Type (Ttyp); | |
6392 | end if; | |
6393 | ||
70482933 | 6394 | if Prefix_Is_Type then |
3a77b68d | 6395 | |
31104818 | 6396 | -- For VMs we leave the type attribute unexpanded because |
3a77b68d GB |
6397 | -- there's not a dispatching table to reference. |
6398 | ||
1f110335 | 6399 | if Tagged_Type_Expansion then |
3a77b68d GB |
6400 | Rewrite (N, |
6401 | Unchecked_Convert_To (RTE (RE_Tag), | |
e4494292 | 6402 | New_Occurrence_Of |
a9d8907c | 6403 | (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc))); |
3a77b68d GB |
6404 | Analyze_And_Resolve (N, RTE (RE_Tag)); |
6405 | end if; | |
70482933 | 6406 | |
934a3a25 | 6407 | -- Ada 2005 (AI-251): The use of 'Tag in the sources always |
31104818 HK |
6408 | -- references the primary tag of the actual object. If 'Tag is |
6409 | -- applied to class-wide interface objects we generate code that | |
6410 | -- displaces "this" to reference the base of the object. | |
6411 | ||
6412 | elsif Comes_From_Source (N) | |
6413 | and then Is_Class_Wide_Type (Etype (Prefix (N))) | |
63a5b3dc | 6414 | and then Is_Interface (Underlying_Type (Etype (Prefix (N)))) |
31104818 HK |
6415 | then |
6416 | -- Generate: | |
6417 | -- (To_Tag_Ptr (Prefix'Address)).all | |
6418 | ||
6419 | -- Note that Prefix'Address is recursively expanded into a call | |
6420 | -- to Base_Address (Obj.Tag) | |
6421 | ||
470cd9e9 RD |
6422 | -- Not needed for VM targets, since all handled by the VM |
6423 | ||
1f110335 | 6424 | if Tagged_Type_Expansion then |
470cd9e9 RD |
6425 | Rewrite (N, |
6426 | Make_Explicit_Dereference (Loc, | |
6427 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
6428 | Make_Attribute_Reference (Loc, | |
6429 | Prefix => Relocate_Node (Pref), | |
6430 | Attribute_Name => Name_Address)))); | |
6431 | Analyze_And_Resolve (N, RTE (RE_Tag)); | |
6432 | end if; | |
31104818 | 6433 | |
70482933 RK |
6434 | else |
6435 | Rewrite (N, | |
6436 | Make_Selected_Component (Loc, | |
6437 | Prefix => Relocate_Node (Pref), | |
6438 | Selector_Name => | |
e4494292 | 6439 | New_Occurrence_Of (First_Tag_Component (Ttyp), Loc))); |
3a77b68d | 6440 | Analyze_And_Resolve (N, RTE (RE_Tag)); |
70482933 | 6441 | end if; |
70482933 RK |
6442 | end Tag; |
6443 | ||
6444 | ---------------- | |
6445 | -- Terminated -- | |
6446 | ---------------- | |
6447 | ||
758c442c | 6448 | -- Transforms 'Terminated attribute into a call to Terminated function |
70482933 | 6449 | |
d8f43ee6 HK |
6450 | when Attribute_Terminated => Terminated : begin |
6451 | ||
65f01153 RD |
6452 | -- The prefix of Terminated is of a task interface class-wide type. |
6453 | -- Generate: | |
31e358e1 | 6454 | -- terminated (Task_Id (_disp_get_task_id (Pref))); |
65f01153 | 6455 | |
0791fbe9 | 6456 | if Ada_Version >= Ada_2005 |
21d27997 RD |
6457 | and then Ekind (Ptyp) = E_Class_Wide_Type |
6458 | and then Is_Interface (Ptyp) | |
6459 | and then Is_Task_Interface (Ptyp) | |
65f01153 | 6460 | then |
99bba92c AC |
6461 | Rewrite (N, |
6462 | Make_Function_Call (Loc, | |
c0e938d0 | 6463 | Name => |
99bba92c AC |
6464 | New_Occurrence_Of (RTE (RE_Terminated), Loc), |
6465 | Parameter_Associations => New_List ( | |
6466 | Make_Unchecked_Type_Conversion (Loc, | |
6467 | Subtype_Mark => | |
6468 | New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc), | |
c0e938d0 | 6469 | Expression => Build_Disp_Get_Task_Id_Call (Pref))))); |
65f01153 RD |
6470 | |
6471 | elsif Restricted_Profile then | |
70482933 RK |
6472 | Rewrite (N, |
6473 | Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated))); | |
6474 | ||
6475 | else | |
6476 | Rewrite (N, | |
6477 | Build_Call_With_Task (Pref, RTE (RE_Terminated))); | |
6478 | end if; | |
6479 | ||
6480 | Analyze_And_Resolve (N, Standard_Boolean); | |
6481 | end Terminated; | |
6482 | ||
6483 | ---------------- | |
6484 | -- To_Address -- | |
6485 | ---------------- | |
6486 | ||
1b0b0f18 | 6487 | -- Transforms System'To_Address (X) and System.Address'Ref (X) into |
1e3c434f BD |
6488 | -- unchecked conversion from (integral) type of X to type address. If |
6489 | -- the To_Address is a static expression, the transformed expression | |
6490 | -- also needs to be static, because we do some legality checks (e.g. | |
6491 | -- for Thread_Local_Storage) after this transformation. | |
70482933 | 6492 | |
89beb653 HK |
6493 | when Attribute_Ref |
6494 | | Attribute_To_Address | |
6495 | => | |
6496 | To_Address : declare | |
6497 | Is_Static : constant Boolean := Is_Static_Expression (N); | |
6498 | ||
6499 | begin | |
6500 | Rewrite (N, | |
6501 | Unchecked_Convert_To (RTE (RE_Address), | |
6502 | Relocate_Node (First (Exprs)))); | |
6503 | Set_Is_Static_Expression (N, Is_Static); | |
6504 | ||
6505 | Analyze_And_Resolve (N, RTE (RE_Address)); | |
6506 | end To_Address; | |
70482933 | 6507 | |
54838d1f AC |
6508 | ------------ |
6509 | -- To_Any -- | |
6510 | ------------ | |
6511 | ||
6512 | when Attribute_To_Any => To_Any : declare | |
54838d1f AC |
6513 | Decls : constant List_Id := New_List; |
6514 | begin | |
6515 | Rewrite (N, | |
6516 | Build_To_Any_Call | |
30ebb114 | 6517 | (Loc, |
10e168cd | 6518 | Convert_To (Ptyp, |
54838d1f AC |
6519 | Relocate_Node (First (Exprs))), Decls)); |
6520 | Insert_Actions (N, Decls); | |
6521 | Analyze_And_Resolve (N, RTE (RE_Any)); | |
6522 | end To_Any; | |
6523 | ||
70482933 RK |
6524 | ---------------- |
6525 | -- Truncation -- | |
6526 | ---------------- | |
6527 | ||
6528 | -- Transforms 'Truncation into a call to the floating-point attribute | |
0669bebe GB |
6529 | -- function Truncation in Fat_xxx (where xxx is the root type). |
6530 | -- Expansion is avoided for cases the back end can handle directly. | |
70482933 RK |
6531 | |
6532 | when Attribute_Truncation => | |
0669bebe GB |
6533 | if not Is_Inline_Floating_Point_Attribute (N) then |
6534 | Expand_Fpt_Attribute_R (N); | |
6535 | end if; | |
70482933 | 6536 | |
54838d1f AC |
6537 | -------------- |
6538 | -- TypeCode -- | |
6539 | -------------- | |
6540 | ||
6541 | when Attribute_TypeCode => TypeCode : declare | |
54838d1f AC |
6542 | Decls : constant List_Id := New_List; |
6543 | begin | |
10e168cd | 6544 | Rewrite (N, Build_TypeCode_Call (Loc, Ptyp, Decls)); |
54838d1f AC |
6545 | Insert_Actions (N, Decls); |
6546 | Analyze_And_Resolve (N, RTE (RE_TypeCode)); | |
6547 | end TypeCode; | |
6548 | ||
70482933 RK |
6549 | ----------------------- |
6550 | -- Unbiased_Rounding -- | |
6551 | ----------------------- | |
6552 | ||
6553 | -- Transforms 'Unbiased_Rounding into a call to the floating-point | |
6554 | -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the | |
0669bebe GB |
6555 | -- root type). Expansion is avoided for cases the back end can handle |
6556 | -- directly. | |
70482933 RK |
6557 | |
6558 | when Attribute_Unbiased_Rounding => | |
0669bebe GB |
6559 | if not Is_Inline_Floating_Point_Attribute (N) then |
6560 | Expand_Fpt_Attribute_R (N); | |
6561 | end if; | |
70482933 | 6562 | |
18a2ad5d AC |
6563 | ------------ |
6564 | -- Update -- | |
6565 | ------------ | |
6566 | ||
6567 | when Attribute_Update => | |
6568 | Expand_Update_Attribute (N); | |
6569 | ||
70482933 RK |
6570 | --------------- |
6571 | -- VADS_Size -- | |
6572 | --------------- | |
6573 | ||
6574 | -- The processing for VADS_Size is shared with Size | |
6575 | ||
6576 | --------- | |
6577 | -- Val -- | |
6578 | --------- | |
6579 | ||
10e168cd EB |
6580 | -- For enumeration types with a non-standard representation we use the |
6581 | -- _Pos_To_Rep array that was created when the type was frozen, unless | |
6582 | -- the representation is contiguous in which case we use an addition. | |
6583 | ||
f193b29e EB |
6584 | -- For enumeration types with a standard representation, Val can be |
6585 | -- rewritten as a simple conversion with Conversion_OK set. | |
6586 | ||
10e168cd EB |
6587 | -- For integer types, Val is equivalent to a simple integer conversion |
6588 | -- and we rewrite it as such. | |
70482933 | 6589 | |
47d3b920 | 6590 | when Attribute_Val => Val : declare |
10e168cd EB |
6591 | Etyp : constant Entity_Id := Base_Type (Ptyp); |
6592 | Expr : constant Node_Id := First (Exprs); | |
79e267f9 EB |
6593 | Ityp : Entity_Id; |
6594 | Rtyp : Entity_Id; | |
70482933 RK |
6595 | |
6596 | begin | |
10e168cd | 6597 | -- Case of enumeration type |
fbf5a39b | 6598 | |
10e168cd EB |
6599 | if Is_Enumeration_Type (Etyp) then |
6600 | ||
79e267f9 | 6601 | -- Non-contiguous non-standard enumeration type |
10e168cd | 6602 | |
79e267f9 EB |
6603 | if Present (Enum_Pos_To_Rep (Etyp)) |
6604 | and then not Has_Contiguous_Rep (Etyp) | |
6605 | then | |
6606 | Rewrite (N, | |
6607 | Make_Indexed_Component (Loc, | |
6608 | Prefix => | |
6609 | New_Occurrence_Of (Enum_Pos_To_Rep (Etyp), Loc), | |
6610 | Expressions => New_List ( | |
6611 | Convert_To (Standard_Integer, Expr)))); | |
10e168cd | 6612 | |
79e267f9 | 6613 | Analyze_And_Resolve (N, Typ); |
10e168cd | 6614 | |
79e267f9 | 6615 | -- Standard or contiguous non-standard enumeration type |
fbf5a39b | 6616 | |
79e267f9 EB |
6617 | else |
6618 | -- If the argument is marked as requiring a range check then | |
6619 | -- generate it here, after looking through a conversion to | |
6620 | -- universal integer, if any. | |
6621 | ||
6622 | if Do_Range_Check (Expr) then | |
6623 | if Present (Enum_Pos_To_Rep (Etyp)) then | |
6624 | Rtyp := Enum_Pos_To_Rep (Etyp); | |
6625 | else | |
6626 | Rtyp := Etyp; | |
6627 | end if; | |
70482933 | 6628 | |
79e267f9 EB |
6629 | if Nkind (Expr) = N_Type_Conversion |
6630 | and then Entity (Subtype_Mark (Expr)) = Universal_Integer | |
6631 | then | |
6632 | Generate_Range_Check | |
6633 | (Expression (Expr), Rtyp, CE_Range_Check_Failed); | |
10e168cd | 6634 | |
79e267f9 EB |
6635 | else |
6636 | Generate_Range_Check (Expr, Rtyp, CE_Range_Check_Failed); | |
6637 | end if; | |
10e168cd | 6638 | |
10e168cd | 6639 | Set_Do_Range_Check (Expr, False); |
79e267f9 | 6640 | end if; |
d79e621a | 6641 | |
79e267f9 EB |
6642 | -- Contiguous non-standard enumeration type |
6643 | ||
6644 | if Present (Enum_Pos_To_Rep (Etyp)) then | |
6645 | if Is_Unsigned_Type (Etyp) then | |
6646 | if Esize (Typ) <= Standard_Integer_Size then | |
6647 | Ityp := RTE (RE_Unsigned); | |
6648 | else | |
6649 | Ityp := RTE (RE_Long_Long_Unsigned); | |
6650 | end if; | |
6651 | ||
6652 | else | |
6653 | if Esize (Etyp) <= Standard_Integer_Size then | |
6654 | Ityp := Standard_Integer; | |
6655 | else | |
6656 | Ityp := Standard_Long_Long_Integer; | |
6657 | end if; | |
6658 | end if; | |
6659 | ||
6660 | Rewrite (N, | |
6661 | Unchecked_Convert_To (Etyp, | |
6662 | Make_Op_Add (Loc, | |
6663 | Left_Opnd => | |
6664 | Make_Integer_Literal (Loc, | |
6665 | Enumeration_Rep (First_Literal (Etyp))), | |
6666 | Right_Opnd => | |
6667 | Convert_To (Ityp, Expr)))); | |
6668 | ||
f193b29e | 6669 | -- Standard enumeration type |
79e267f9 | 6670 | |
f193b29e EB |
6671 | else |
6672 | Rewrite (N, OK_Convert_To (Typ, Expr)); | |
10e168cd | 6673 | end if; |
f193b29e EB |
6674 | |
6675 | -- Suppress checks since the range check was done above | |
6676 | -- and it guarantees that the addition cannot overflow. | |
6677 | ||
6678 | Analyze_And_Resolve (N, Typ, Suppress => All_Checks); | |
10e168cd | 6679 | end if; |
d79e621a | 6680 | |
10e168cd EB |
6681 | -- Deal with integer types |
6682 | ||
6683 | elsif Is_Integer_Type (Etyp) then | |
6684 | Rewrite (N, Convert_To (Typ, Expr)); | |
6685 | Analyze_And_Resolve (N, Typ); | |
70482933 RK |
6686 | end if; |
6687 | end Val; | |
6688 | ||
6689 | ----------- | |
6690 | -- Valid -- | |
6691 | ----------- | |
6692 | ||
6693 | -- The code for valid is dependent on the particular types involved. | |
6694 | -- See separate sections below for the generated code in each case. | |
6695 | ||
47d3b920 | 6696 | when Attribute_Valid => Valid : declare |
382b0e97 | 6697 | PBtyp : Entity_Id := Base_Type (Ptyp); |
70482933 | 6698 | |
fbf5a39b AC |
6699 | Save_Validity_Checks_On : constant Boolean := Validity_Checks_On; |
6700 | -- Save the validity checking mode. We always turn off validity | |
6701 | -- checking during process of 'Valid since this is one place | |
9e40de1d | 6702 | -- where we do not want the implicit validity checks to interfere |
fbf5a39b AC |
6703 | -- with the explicit validity check that the programmer is doing. |
6704 | ||
70482933 RK |
6705 | function Make_Range_Test return Node_Id; |
6706 | -- Build the code for a range test of the form | |
382b0e97 | 6707 | -- PBtyp!(Pref) in PBtyp!(Ptyp'First) .. PBtyp!(Ptyp'Last) |
70482933 | 6708 | |
fbf5a39b AC |
6709 | --------------------- |
6710 | -- Make_Range_Test -- | |
6711 | --------------------- | |
6712 | ||
70482933 | 6713 | function Make_Range_Test return Node_Id is |
89b6c83e | 6714 | Temp : Node_Id; |
dbf04430 | 6715 | |
70482933 | 6716 | begin |
89b6c83e AC |
6717 | -- The prefix of attribute 'Valid should always denote an object |
6718 | -- reference. The reference is either coming directly from source | |
5168a9b3 PMR |
6719 | -- or is produced by validity check expansion. The object may be |
6720 | -- wrapped in a conversion in which case the call to Unqual_Conv | |
6721 | -- will yield it. | |
dbf04430 | 6722 | |
89b6c83e AC |
6723 | -- If the prefix denotes a variable which captures the value of |
6724 | -- an object for validation purposes, use the variable in the | |
6725 | -- range test. This ensures that no extra copies or extra reads | |
6726 | -- are produced as part of the test. Generate: | |
6727 | ||
6728 | -- Temp : ... := Object; | |
6729 | -- if not Temp in ... then | |
6730 | ||
6731 | if Is_Validation_Variable_Reference (Pref) then | |
5168a9b3 | 6732 | Temp := New_Occurrence_Of (Entity (Unqual_Conv (Pref)), Loc); |
89b6c83e AC |
6733 | |
6734 | -- Otherwise the prefix is either a source object or a constant | |
6735 | -- produced by validity check expansion. Generate: | |
6736 | ||
6737 | -- Temp : constant ... := Pref; | |
6738 | -- if not Temp in ... then | |
6739 | ||
6740 | else | |
6741 | Temp := Duplicate_Subexpr (Pref); | |
dbf04430 AC |
6742 | end if; |
6743 | ||
70482933 | 6744 | return |
ea034236 | 6745 | Make_In (Loc, |
382b0e97 | 6746 | Left_Opnd => Unchecked_Convert_To (PBtyp, Temp), |
ea034236 AC |
6747 | Right_Opnd => |
6748 | Make_Range (Loc, | |
89b6c83e | 6749 | Low_Bound => |
382b0e97 | 6750 | Unchecked_Convert_To (PBtyp, |
70482933 | 6751 | Make_Attribute_Reference (Loc, |
89b6c83e | 6752 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
ea034236 AC |
6753 | Attribute_Name => Name_First)), |
6754 | High_Bound => | |
382b0e97 | 6755 | Unchecked_Convert_To (PBtyp, |
70482933 | 6756 | Make_Attribute_Reference (Loc, |
89b6c83e | 6757 | Prefix => New_Occurrence_Of (Ptyp, Loc), |
70482933 RK |
6758 | Attribute_Name => Name_Last)))); |
6759 | end Make_Range_Test; | |
6760 | ||
f16cb8df HK |
6761 | -- Local variables |
6762 | ||
6763 | Tst : Node_Id; | |
6764 | ||
70482933 RK |
6765 | -- Start of processing for Attribute_Valid |
6766 | ||
6767 | begin | |
1d57c04f AC |
6768 | -- Do not expand sourced code 'Valid reference in CodePeer mode, |
6769 | -- will be handled by the back-end directly. | |
6770 | ||
6771 | if CodePeer_Mode and then Comes_From_Source (N) then | |
6772 | return; | |
6773 | end if; | |
6774 | ||
fbf5a39b AC |
6775 | -- Turn off validity checks. We do not want any implicit validity |
6776 | -- checks to intefere with the explicit check from the attribute | |
6777 | ||
6778 | Validity_Checks_On := False; | |
6779 | ||
d7a44b14 AC |
6780 | -- Retrieve the base type. Handle the case where the base type is a |
6781 | -- private enumeration type. | |
6782 | ||
382b0e97 BD |
6783 | if Is_Private_Type (PBtyp) and then Present (Full_View (PBtyp)) then |
6784 | PBtyp := Full_View (PBtyp); | |
d7a44b14 AC |
6785 | end if; |
6786 | ||
70482933 RK |
6787 | -- Floating-point case. This case is handled by the Valid attribute |
6788 | -- code in the floating-point attribute run-time library. | |
6789 | ||
6790 | if Is_Floating_Point_Type (Ptyp) then | |
dfaff97b | 6791 | Float_Valid : declare |
65f01153 RD |
6792 | Pkg : RE_Id; |
6793 | Ftp : Entity_Id; | |
70482933 | 6794 | |
8575023c AC |
6795 | function Get_Fat_Entity (Nam : Name_Id) return Entity_Id; |
6796 | -- Return entity for Pkg.Nam | |
6797 | ||
6798 | -------------------- | |
6799 | -- Get_Fat_Entity -- | |
6800 | -------------------- | |
6801 | ||
6802 | function Get_Fat_Entity (Nam : Name_Id) return Entity_Id is | |
6803 | Exp_Name : constant Node_Id := | |
6804 | Make_Selected_Component (Loc, | |
6805 | Prefix => New_Occurrence_Of (RTE (Pkg), Loc), | |
6806 | Selector_Name => Make_Identifier (Loc, Nam)); | |
6807 | begin | |
6808 | Find_Selected_Component (Exp_Name); | |
6809 | return Entity (Exp_Name); | |
6810 | end Get_Fat_Entity; | |
6811 | ||
dfaff97b RD |
6812 | -- Start of processing for Float_Valid |
6813 | ||
70482933 | 6814 | begin |
88438c0e | 6815 | -- The C and AAMP back-ends handle Valid for fpt types |
8575023c | 6816 | |
382b0e97 | 6817 | if Modify_Tree_For_C or else Float_Rep (PBtyp) = AAMP then |
88438c0e AC |
6818 | Analyze_And_Resolve (Pref, Ptyp); |
6819 | Set_Etype (N, Standard_Boolean); | |
6820 | Set_Analyzed (N); | |
8575023c | 6821 | |
88438c0e AC |
6822 | else |
6823 | Find_Fat_Info (Ptyp, Ftp, Pkg); | |
6824 | ||
6825 | -- If the prefix is a reverse SSO component, or is possibly | |
6826 | -- unaligned, first create a temporary copy that is in | |
6827 | -- native SSO, and properly aligned. Make it Volatile to | |
6828 | -- prevent folding in the back-end. Note that we use an | |
6829 | -- intermediate constrained string type to initialize the | |
6830 | -- temporary, as the value at hand might be invalid, and in | |
6831 | -- that case it cannot be copied using a floating point | |
6832 | -- register. | |
6833 | ||
6834 | if In_Reverse_Storage_Order_Object (Pref) | |
6835 | or else Is_Possibly_Unaligned_Object (Pref) | |
6836 | then | |
6837 | declare | |
6838 | Temp : constant Entity_Id := | |
6839 | Make_Temporary (Loc, 'F'); | |
6840 | ||
6841 | Fat_S : constant Entity_Id := | |
6842 | Get_Fat_Entity (Name_S); | |
6843 | -- Constrained string subtype of appropriate size | |
6844 | ||
6845 | Fat_P : constant Entity_Id := | |
6846 | Get_Fat_Entity (Name_P); | |
6847 | -- Access to Fat_S | |
6848 | ||
6849 | Decl : constant Node_Id := | |
6850 | Make_Object_Declaration (Loc, | |
6851 | Defining_Identifier => Temp, | |
6852 | Aliased_Present => True, | |
6853 | Object_Definition => | |
6854 | New_Occurrence_Of (Ptyp, Loc)); | |
6855 | ||
6856 | begin | |
6857 | Set_Aspect_Specifications (Decl, New_List ( | |
6858 | Make_Aspect_Specification (Loc, | |
6859 | Identifier => | |
6860 | Make_Identifier (Loc, Name_Volatile)))); | |
6861 | ||
6862 | Insert_Actions (N, | |
6863 | New_List ( | |
6864 | Decl, | |
6865 | ||
6866 | Make_Assignment_Statement (Loc, | |
6867 | Name => | |
6868 | Make_Explicit_Dereference (Loc, | |
6869 | Prefix => | |
6870 | Unchecked_Convert_To (Fat_P, | |
6871 | Make_Attribute_Reference (Loc, | |
6872 | Prefix => | |
6873 | New_Occurrence_Of (Temp, Loc), | |
6874 | Attribute_Name => | |
6875 | Name_Unrestricted_Access))), | |
6876 | Expression => | |
6877 | Unchecked_Convert_To (Fat_S, | |
6878 | Relocate_Node (Pref)))), | |
6879 | ||
6880 | Suppress => All_Checks); | |
6881 | ||
6882 | Rewrite (Pref, New_Occurrence_Of (Temp, Loc)); | |
6883 | end; | |
6884 | end if; | |
8575023c | 6885 | |
88438c0e AC |
6886 | -- We now have an object of the proper endianness and |
6887 | -- alignment, and can construct a Valid attribute. | |
74014283 | 6888 | |
88438c0e AC |
6889 | -- We make sure the prefix of this valid attribute is |
6890 | -- marked as not coming from source, to avoid losing | |
6891 | -- warnings from 'Valid looking like a possible update. | |
74014283 | 6892 | |
88438c0e | 6893 | Set_Comes_From_Source (Pref, False); |
8575023c | 6894 | |
88438c0e AC |
6895 | Expand_Fpt_Attribute |
6896 | (N, Pkg, Name_Valid, | |
6897 | New_List ( | |
6898 | Make_Attribute_Reference (Loc, | |
6899 | Prefix => Unchecked_Convert_To (Ftp, Pref), | |
6900 | Attribute_Name => Name_Unrestricted_Access))); | |
6901 | end if; | |
70482933 RK |
6902 | |
6903 | -- One more task, we still need a range check. Required | |
6904 | -- only if we have a constraint, since the Valid routine | |
6905 | -- catches infinities properly (infinities are never valid). | |
6906 | ||
6907 | -- The way we do the range check is simply to create the | |
6908 | -- expression: Valid (N) and then Base_Type(Pref) in Typ. | |
6909 | ||
382b0e97 | 6910 | if not Subtypes_Statically_Match (Ptyp, PBtyp) then |
70482933 RK |
6911 | Rewrite (N, |
6912 | Make_And_Then (Loc, | |
6913 | Left_Opnd => Relocate_Node (N), | |
6914 | Right_Opnd => | |
6915 | Make_In (Loc, | |
382b0e97 | 6916 | Left_Opnd => Convert_To (PBtyp, Pref), |
70482933 RK |
6917 | Right_Opnd => New_Occurrence_Of (Ptyp, Loc)))); |
6918 | end if; | |
dfaff97b | 6919 | end Float_Valid; |
70482933 RK |
6920 | |
6921 | -- Enumeration type with holes | |
6922 | ||
6923 | -- For enumeration types with holes, the Pos value constructed by | |
6924 | -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a | |
6925 | -- second argument of False returns minus one for an invalid value, | |
6926 | -- and the non-negative pos value for a valid value, so the | |
6927 | -- expansion of X'Valid is simply: | |
6928 | ||
6929 | -- type(X)'Pos (X) >= 0 | |
6930 | ||
6931 | -- We can't quite generate it that way because of the requirement | |
7324bf49 AC |
6932 | -- for the non-standard second argument of False in the resulting |
6933 | -- rep_to_pos call, so we have to explicitly create: | |
70482933 RK |
6934 | |
6935 | -- _rep_to_pos (X, False) >= 0 | |
6936 | ||
6937 | -- If we have an enumeration subtype, we also check that the | |
6938 | -- value is in range: | |
6939 | ||
6940 | -- _rep_to_pos (X, False) >= 0 | |
6941 | -- and then | |
7324bf49 | 6942 | -- (X >= type(X)'First and then type(X)'Last <= X) |
70482933 RK |
6943 | |
6944 | elsif Is_Enumeration_Type (Ptyp) | |
382b0e97 | 6945 | and then Present (Enum_Pos_To_Rep (PBtyp)) |
70482933 RK |
6946 | then |
6947 | Tst := | |
6948 | Make_Op_Ge (Loc, | |
6949 | Left_Opnd => | |
6950 | Make_Function_Call (Loc, | |
6951 | Name => | |
382b0e97 | 6952 | New_Occurrence_Of (TSS (PBtyp, TSS_Rep_To_Pos), Loc), |
70482933 RK |
6953 | Parameter_Associations => New_List ( |
6954 | Pref, | |
6955 | New_Occurrence_Of (Standard_False, Loc))), | |
6956 | Right_Opnd => Make_Integer_Literal (Loc, 0)); | |
6957 | ||
382b0e97 | 6958 | if Ptyp /= PBtyp |
70482933 | 6959 | and then |
382b0e97 | 6960 | (Type_Low_Bound (Ptyp) /= Type_Low_Bound (PBtyp) |
70482933 | 6961 | or else |
382b0e97 | 6962 | Type_High_Bound (Ptyp) /= Type_High_Bound (PBtyp)) |
70482933 RK |
6963 | then |
6964 | -- The call to Make_Range_Test will create declarations | |
6965 | -- that need a proper insertion point, but Pref is now | |
6966 | -- attached to a node with no ancestor. Attach to tree | |
6967 | -- even if it is to be rewritten below. | |
6968 | ||
6969 | Set_Parent (Tst, Parent (N)); | |
6970 | ||
6971 | Tst := | |
6972 | Make_And_Then (Loc, | |
6973 | Left_Opnd => Make_Range_Test, | |
6974 | Right_Opnd => Tst); | |
6975 | end if; | |
6976 | ||
6977 | Rewrite (N, Tst); | |
6978 | ||
6979 | -- Fortran convention booleans | |
6980 | ||
6981 | -- For the very special case of Fortran convention booleans, the | |
6982 | -- value is always valid, since it is an integer with the semantics | |
6983 | -- that non-zero is true, and any value is permissible. | |
6984 | ||
6985 | elsif Is_Boolean_Type (Ptyp) | |
6986 | and then Convention (Ptyp) = Convention_Fortran | |
6987 | then | |
6988 | Rewrite (N, New_Occurrence_Of (Standard_True, Loc)); | |
6989 | ||
6990 | -- For biased representations, we will be doing an unchecked | |
758c442c GD |
6991 | -- conversion without unbiasing the result. That means that the range |
6992 | -- test has to take this into account, and the proper form of the | |
6993 | -- test is: | |
70482933 | 6994 | |
382b0e97 | 6995 | -- PBtyp!(Pref) < PBtyp!(Ptyp'Range_Length) |
70482933 RK |
6996 | |
6997 | elsif Has_Biased_Representation (Ptyp) then | |
382b0e97 | 6998 | PBtyp := RTE (RE_Unsigned_32); |
70482933 RK |
6999 | Rewrite (N, |
7000 | Make_Op_Lt (Loc, | |
7001 | Left_Opnd => | |
382b0e97 | 7002 | Unchecked_Convert_To (PBtyp, Duplicate_Subexpr (Pref)), |
70482933 | 7003 | Right_Opnd => |
382b0e97 | 7004 | Unchecked_Convert_To (PBtyp, |
70482933 RK |
7005 | Make_Attribute_Reference (Loc, |
7006 | Prefix => New_Occurrence_Of (Ptyp, Loc), | |
7007 | Attribute_Name => Name_Range_Length)))); | |
7008 | ||
7009 | -- For all other scalar types, what we want logically is a | |
7010 | -- range test: | |
7011 | ||
7012 | -- X in type(X)'First .. type(X)'Last | |
7013 | ||
7014 | -- But that's precisely what won't work because of possible | |
7015 | -- unwanted optimization (and indeed the basic motivation for | |
a90bd866 | 7016 | -- the Valid attribute is exactly that this test does not work). |
70482933 RK |
7017 | -- What will work is: |
7018 | ||
382b0e97 | 7019 | -- PBtyp!(X) >= PBtyp!(type(X)'First) |
70482933 | 7020 | -- and then |
382b0e97 | 7021 | -- PBtyp!(X) <= PBtyp!(type(X)'Last) |
70482933 | 7022 | |
382b0e97 | 7023 | -- where PBtyp is an integer type large enough to cover the full |
70482933 RK |
7024 | -- range of possible stored values (i.e. it is chosen on the basis |
7025 | -- of the size of the type, not the range of the values). We write | |
7026 | -- this as two tests, rather than a range check, so that static | |
7027 | -- evaluation will easily remove either or both of the checks if | |
7028 | -- they can be -statically determined to be true (this happens | |
7029 | -- when the type of X is static and the range extends to the full | |
7030 | -- range of stored values). | |
7031 | ||
7032 | -- Unsigned types. Note: it is safe to consider only whether the | |
7033 | -- subtype is unsigned, since we will in that case be doing all | |
758c442c GD |
7034 | -- unsigned comparisons based on the subtype range. Since we use the |
7035 | -- actual subtype object size, this is appropriate. | |
70482933 RK |
7036 | |
7037 | -- For example, if we have | |
7038 | ||
7039 | -- subtype x is integer range 1 .. 200; | |
7040 | -- for x'Object_Size use 8; | |
7041 | ||
758c442c GD |
7042 | -- Now the base type is signed, but objects of this type are bits |
7043 | -- unsigned, and doing an unsigned test of the range 1 to 200 is | |
7044 | -- correct, even though a value greater than 127 looks signed to a | |
7045 | -- signed comparison. | |
70482933 | 7046 | |
382b0e97 BD |
7047 | elsif Is_Unsigned_Type (Ptyp) |
7048 | or else (Is_Private_Type (Ptyp) and then Is_Unsigned_Type (Btyp)) | |
7049 | then | |
70482933 | 7050 | if Esize (Ptyp) <= 32 then |
382b0e97 | 7051 | PBtyp := RTE (RE_Unsigned_32); |
70482933 | 7052 | else |
382b0e97 | 7053 | PBtyp := RTE (RE_Unsigned_64); |
70482933 RK |
7054 | end if; |
7055 | ||
7056 | Rewrite (N, Make_Range_Test); | |
7057 | ||
7058 | -- Signed types | |
7059 | ||
7060 | else | |
7061 | if Esize (Ptyp) <= Esize (Standard_Integer) then | |
382b0e97 | 7062 | PBtyp := Standard_Integer; |
70482933 | 7063 | else |
fe2684ad | 7064 | PBtyp := Standard_Long_Long_Integer; |
70482933 RK |
7065 | end if; |
7066 | ||
7067 | Rewrite (N, Make_Range_Test); | |
7068 | end if; | |
7069 | ||
3d6db7f8 GD |
7070 | -- If a predicate is present, then we do the predicate test, even if |
7071 | -- within the predicate function (infinite recursion is warned about | |
97948f41 | 7072 | -- in Sem_Attr in that case). |
3d6db7f8 GD |
7073 | |
7074 | declare | |
7075 | Pred_Func : constant Entity_Id := Predicate_Function (Ptyp); | |
7076 | ||
7077 | begin | |
7078 | if Present (Pred_Func) then | |
7079 | Rewrite (N, | |
7080 | Make_And_Then (Loc, | |
7081 | Left_Opnd => Relocate_Node (N), | |
7082 | Right_Opnd => Make_Predicate_Call (Ptyp, Pref))); | |
3d6db7f8 GD |
7083 | end if; |
7084 | end; | |
7085 | ||
70482933 | 7086 | Analyze_And_Resolve (N, Standard_Boolean); |
fbf5a39b | 7087 | Validity_Checks_On := Save_Validity_Checks_On; |
70482933 RK |
7088 | end Valid; |
7089 | ||
2a1f6a1f AC |
7090 | ------------------- |
7091 | -- Valid_Scalars -- | |
7092 | ------------------- | |
7093 | ||
7094 | when Attribute_Valid_Scalars => Valid_Scalars : declare | |
a4dafb8b PT |
7095 | Val_Typ : constant Entity_Id := Validated_View (Ptyp); |
7096 | Expr : Node_Id; | |
99fc068e | 7097 | |
2a1f6a1f | 7098 | begin |
f16cb8df | 7099 | -- Assume that the prefix does not need validation |
99fc068e | 7100 | |
f16cb8df | 7101 | Expr := Empty; |
45ec05e1 | 7102 | |
d57f8e40 PT |
7103 | -- Attribute 'Valid_Scalars is not supported on private tagged types; |
7104 | -- see a detailed explanation where this attribute is analyzed. | |
99fc068e | 7105 | |
f16cb8df HK |
7106 | if Is_Private_Type (Ptyp) and then Is_Tagged_Type (Ptyp) then |
7107 | null; | |
99fc068e | 7108 | |
f16cb8df HK |
7109 | -- Attribute 'Valid_Scalars evaluates to True when the type lacks |
7110 | -- scalars. | |
99fc068e | 7111 | |
f16cb8df HK |
7112 | elsif not Scalar_Part_Present (Val_Typ) then |
7113 | null; | |
99fc068e | 7114 | |
f16cb8df HK |
7115 | -- Attribute 'Valid_Scalars is the same as attribute 'Valid when the |
7116 | -- validated type is a scalar type. Generate: | |
45ec05e1 | 7117 | |
f16cb8df | 7118 | -- Val_Typ (Pref)'Valid |
45ec05e1 | 7119 | |
f16cb8df HK |
7120 | elsif Is_Scalar_Type (Val_Typ) then |
7121 | Expr := | |
7122 | Make_Attribute_Reference (Loc, | |
7123 | Prefix => | |
7124 | Unchecked_Convert_To (Val_Typ, New_Copy_Tree (Pref)), | |
7125 | Attribute_Name => Name_Valid); | |
45ec05e1 | 7126 | |
efd0843d EB |
7127 | -- Required by LLVM although the sizes are the same??? |
7128 | ||
7129 | if Nkind (Prefix (Expr)) = N_Unchecked_Type_Conversion then | |
7130 | Set_No_Truncation (Prefix (Expr)); | |
7131 | end if; | |
7132 | ||
f16cb8df HK |
7133 | -- Validate the scalar components of an array by iterating over all |
7134 | -- dimensions of the array while checking individual components. | |
45ec05e1 | 7135 | |
f16cb8df | 7136 | elsif Is_Array_Type (Val_Typ) then |
a4dafb8b PT |
7137 | Expr := |
7138 | Make_Function_Call (Loc, | |
7139 | Name => | |
7140 | New_Occurrence_Of | |
7141 | (Build_Array_VS_Func | |
7142 | (Attr => N, | |
7143 | Formal_Typ => Ptyp, | |
7144 | Array_Typ => Val_Typ), | |
7145 | Loc), | |
7146 | Parameter_Associations => New_List (Pref)); | |
99fc068e | 7147 | |
f16cb8df HK |
7148 | -- Validate the scalar components, discriminants of a record type by |
7149 | -- examining the structure of a record type. | |
99fc068e | 7150 | |
f16cb8df HK |
7151 | elsif Is_Record_Type (Val_Typ) then |
7152 | Expr := | |
7153 | Make_Function_Call (Loc, | |
7154 | Name => | |
7155 | New_Occurrence_Of | |
7156 | (Build_Record_VS_Func | |
7157 | (Attr => N, | |
7158 | Formal_Typ => Ptyp, | |
7159 | Rec_Typ => Val_Typ), | |
7160 | Loc), | |
7161 | Parameter_Associations => New_List (Pref)); | |
7162 | end if; | |
99fc068e | 7163 | |
f16cb8df HK |
7164 | -- Default the attribute to True when the type of the prefix does not |
7165 | -- need validation. | |
99fc068e | 7166 | |
f16cb8df HK |
7167 | if No (Expr) then |
7168 | Expr := New_Occurrence_Of (Standard_True, Loc); | |
99fc068e | 7169 | end if; |
45ec05e1 | 7170 | |
f16cb8df | 7171 | Rewrite (N, Expr); |
45ec05e1 RD |
7172 | Analyze_And_Resolve (N, Standard_Boolean); |
7173 | Set_Is_Static_Expression (N, False); | |
2a1f6a1f AC |
7174 | end Valid_Scalars; |
7175 | ||
70482933 RK |
7176 | ----------- |
7177 | -- Value -- | |
7178 | ----------- | |
7179 | ||
70482933 RK |
7180 | when Attribute_Value => |
7181 | Exp_Imgv.Expand_Value_Attribute (N); | |
7182 | ||
7183 | ----------------- | |
7184 | -- Value_Size -- | |
7185 | ----------------- | |
7186 | ||
7187 | -- The processing for Value_Size shares the processing for Size | |
7188 | ||
7189 | ------------- | |
7190 | -- Version -- | |
7191 | ------------- | |
7192 | ||
7193 | -- The processing for Version shares the processing for Body_Version | |
7194 | ||
7195 | ---------------- | |
7196 | -- Wide_Image -- | |
7197 | ---------------- | |
7198 | ||
470cd9e9 | 7199 | when Attribute_Wide_Image => |
b63d61f7 AC |
7200 | -- Leave attribute unexpanded in CodePeer mode: the gnat2scil |
7201 | -- back-end knows how to handle this attribute directly. | |
7202 | ||
7203 | if CodePeer_Mode then | |
7204 | return; | |
7205 | end if; | |
7206 | ||
470cd9e9 | 7207 | Exp_Imgv.Expand_Wide_Image_Attribute (N); |
70482933 | 7208 | |
82c80734 RD |
7209 | --------------------- |
7210 | -- Wide_Wide_Image -- | |
7211 | --------------------- | |
7212 | ||
470cd9e9 | 7213 | when Attribute_Wide_Wide_Image => |
b63d61f7 AC |
7214 | -- Leave attribute unexpanded in CodePeer mode: the gnat2scil |
7215 | -- back-end knows how to handle this attribute directly. | |
7216 | ||
7217 | if CodePeer_Mode then | |
7218 | return; | |
7219 | end if; | |
7220 | ||
470cd9e9 | 7221 | Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N); |
82c80734 | 7222 | |
70482933 RK |
7223 | ---------------- |
7224 | -- Wide_Value -- | |
7225 | ---------------- | |
7226 | ||
7227 | -- We expand typ'Wide_Value (X) into | |
7228 | ||
7229 | -- typ'Value | |
7230 | -- (Wide_String_To_String (X, Wide_Character_Encoding_Method)) | |
7231 | ||
7232 | -- Wide_String_To_String is a runtime function that converts its wide | |
7233 | -- string argument to String, converting any non-translatable characters | |
7234 | -- into appropriate escape sequences. This preserves the required | |
7235 | -- semantics of Wide_Value in all cases, and results in a very simple | |
7236 | -- implementation approach. | |
7237 | ||
7ce611e2 ES |
7238 | -- Note: for this approach to be fully standard compliant for the cases |
7239 | -- where typ is Wide_Character and Wide_Wide_Character, the encoding | |
7240 | -- method must cover the entire character range (e.g. UTF-8). But that | |
7241 | -- is a reasonable requirement when dealing with encoded character | |
7242 | -- sequences. Presumably if one of the restrictive encoding mechanisms | |
7243 | -- is in use such as Shift-JIS, then characters that cannot be | |
7244 | -- represented using this encoding will not appear in any case. | |
70482933 | 7245 | |
d8f43ee6 | 7246 | when Attribute_Wide_Value => |
70482933 RK |
7247 | Rewrite (N, |
7248 | Make_Attribute_Reference (Loc, | |
7249 | Prefix => Pref, | |
7250 | Attribute_Name => Name_Value, | |
7251 | ||
7252 | Expressions => New_List ( | |
7253 | Make_Function_Call (Loc, | |
7254 | Name => | |
e4494292 | 7255 | New_Occurrence_Of (RTE (RE_Wide_String_To_String), Loc), |
70482933 RK |
7256 | |
7257 | Parameter_Associations => New_List ( | |
7258 | Relocate_Node (First (Exprs)), | |
7259 | Make_Integer_Literal (Loc, | |
7260 | Intval => Int (Wide_Character_Encoding_Method))))))); | |
7261 | ||
7262 | Analyze_And_Resolve (N, Typ); | |
70482933 | 7263 | |
82c80734 RD |
7264 | --------------------- |
7265 | -- Wide_Wide_Value -- | |
7266 | --------------------- | |
7267 | ||
7268 | -- We expand typ'Wide_Value_Value (X) into | |
7269 | ||
7270 | -- typ'Value | |
7271 | -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method)) | |
7272 | ||
7273 | -- Wide_Wide_String_To_String is a runtime function that converts its | |
7274 | -- wide string argument to String, converting any non-translatable | |
7275 | -- characters into appropriate escape sequences. This preserves the | |
7276 | -- required semantics of Wide_Wide_Value in all cases, and results in a | |
7277 | -- very simple implementation approach. | |
7278 | ||
7279 | -- It's not quite right where typ = Wide_Wide_Character, because the | |
7280 | -- encoding method may not cover the whole character type ??? | |
7281 | ||
d8f43ee6 | 7282 | when Attribute_Wide_Wide_Value => |
82c80734 RD |
7283 | Rewrite (N, |
7284 | Make_Attribute_Reference (Loc, | |
7285 | Prefix => Pref, | |
7286 | Attribute_Name => Name_Value, | |
7287 | ||
7288 | Expressions => New_List ( | |
7289 | Make_Function_Call (Loc, | |
d8f43ee6 | 7290 | Name => |
e4494292 RD |
7291 | New_Occurrence_Of |
7292 | (RTE (RE_Wide_Wide_String_To_String), Loc), | |
82c80734 RD |
7293 | |
7294 | Parameter_Associations => New_List ( | |
7295 | Relocate_Node (First (Exprs)), | |
7296 | Make_Integer_Literal (Loc, | |
7297 | Intval => Int (Wide_Character_Encoding_Method))))))); | |
7298 | ||
7299 | Analyze_And_Resolve (N, Typ); | |
82c80734 RD |
7300 | |
7301 | --------------------- | |
7302 | -- Wide_Wide_Width -- | |
7303 | --------------------- | |
7304 | ||
82c80734 RD |
7305 | when Attribute_Wide_Wide_Width => |
7306 | Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide); | |
7307 | ||
70482933 RK |
7308 | ---------------- |
7309 | -- Wide_Width -- | |
7310 | ---------------- | |
7311 | ||
70482933 | 7312 | when Attribute_Wide_Width => |
82c80734 | 7313 | Exp_Imgv.Expand_Width_Attribute (N, Wide); |
70482933 RK |
7314 | |
7315 | ----------- | |
7316 | -- Width -- | |
7317 | ----------- | |
7318 | ||
70482933 | 7319 | when Attribute_Width => |
82c80734 | 7320 | Exp_Imgv.Expand_Width_Attribute (N, Normal); |
70482933 RK |
7321 | |
7322 | ----------- | |
7323 | -- Write -- | |
7324 | ----------- | |
7325 | ||
7326 | when Attribute_Write => Write : declare | |
7327 | P_Type : constant Entity_Id := Entity (Pref); | |
7328 | U_Type : constant Entity_Id := Underlying_Type (P_Type); | |
7329 | Pname : Entity_Id; | |
7330 | Decl : Node_Id; | |
7331 | Prag : Node_Id; | |
7332 | Arg3 : Node_Id; | |
7333 | Wfunc : Node_Id; | |
7334 | ||
7335 | begin | |
7336 | -- If no underlying type, we have an error that will be diagnosed | |
7337 | -- elsewhere, so here we just completely ignore the expansion. | |
7338 | ||
7339 | if No (U_Type) then | |
7340 | return; | |
7341 | end if; | |
7342 | ||
baa571ab AC |
7343 | -- Stream operations can appear in user code even if the restriction |
7344 | -- No_Streams is active (for example, when instantiating a predefined | |
7345 | -- container). In that case rewrite the attribute as a Raise to | |
7346 | -- prevent any run-time use. | |
7347 | ||
7348 | if Restriction_Active (No_Streams) then | |
7349 | Rewrite (N, | |
7350 | Make_Raise_Program_Error (Sloc (N), | |
b8b2d982 | 7351 | Reason => PE_Stream_Operation_Not_Allowed)); |
baa571ab AC |
7352 | Set_Etype (N, U_Type); |
7353 | return; | |
7354 | end if; | |
7355 | ||
70482933 RK |
7356 | -- The simple case, if there is a TSS for Write, just call it |
7357 | ||
fbf5a39b | 7358 | Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write); |
70482933 RK |
7359 | |
7360 | if Present (Pname) then | |
7361 | null; | |
7362 | ||
7363 | else | |
7364 | -- If there is a Stream_Convert pragma, use it, we rewrite | |
7365 | ||
7366 | -- sourcetyp'Output (stream, Item) | |
7367 | ||
7368 | -- as | |
7369 | ||
7370 | -- strmtyp'Output (Stream, strmwrite (acttyp (Item))); | |
7371 | ||
758c442c GD |
7372 | -- where strmwrite is the given Write function that converts an |
7373 | -- argument of type sourcetyp or a type acctyp, from which it is | |
7374 | -- derived to type strmtyp. The conversion to acttyp is required | |
7375 | -- for the derived case. | |
70482933 | 7376 | |
1d571f3b | 7377 | Prag := Get_Stream_Convert_Pragma (P_Type); |
70482933 RK |
7378 | |
7379 | if Present (Prag) then | |
7380 | Arg3 := | |
7381 | Next (Next (First (Pragma_Argument_Associations (Prag)))); | |
7382 | Wfunc := Entity (Expression (Arg3)); | |
7383 | ||
7384 | Rewrite (N, | |
7385 | Make_Attribute_Reference (Loc, | |
7386 | Prefix => New_Occurrence_Of (Etype (Wfunc), Loc), | |
7387 | Attribute_Name => Name_Output, | |
7388 | Expressions => New_List ( | |
7389 | Relocate_Node (First (Exprs)), | |
7390 | Make_Function_Call (Loc, | |
7391 | Name => New_Occurrence_Of (Wfunc, Loc), | |
7392 | Parameter_Associations => New_List ( | |
31104818 | 7393 | OK_Convert_To (Etype (First_Formal (Wfunc)), |
70482933 RK |
7394 | Relocate_Node (Next (First (Exprs))))))))); |
7395 | ||
7396 | Analyze (N); | |
7397 | return; | |
7398 | ||
7399 | -- For elementary types, we call the W_xxx routine directly | |
7400 | ||
7401 | elsif Is_Elementary_Type (U_Type) then | |
7402 | Rewrite (N, Build_Elementary_Write_Call (N)); | |
7403 | Analyze (N); | |
7404 | return; | |
7405 | ||
7406 | -- Array type case | |
7407 | ||
7408 | elsif Is_Array_Type (U_Type) then | |
7409 | Build_Array_Write_Procedure (N, U_Type, Decl, Pname); | |
7410 | Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False); | |
7411 | ||
7412 | -- Tagged type case, use the primitive Write function. Note that | |
7413 | -- this will dispatch in the class-wide case which is what we want | |
7414 | ||
7415 | elsif Is_Tagged_Type (U_Type) then | |
fbf5a39b | 7416 | Pname := Find_Prim_Op (U_Type, TSS_Stream_Write); |
70482933 RK |
7417 | |
7418 | -- All other record type cases, including protected records. | |
7419 | -- The latter only arise for expander generated code for | |
7420 | -- handling shared passive partition access. | |
7421 | ||
7422 | else | |
7423 | pragma Assert | |
7424 | (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type)); | |
7425 | ||
5d09245e AC |
7426 | -- Ada 2005 (AI-216): Program_Error is raised when executing |
7427 | -- the default implementation of the Write attribute of an | |
0669bebe GB |
7428 | -- Unchecked_Union type. However, if the 'Write reference is |
7429 | -- within the generated Output stream procedure, Write outputs | |
7430 | -- the components, and the default values of the discriminant | |
1f70c47f AC |
7431 | -- are streamed by the Output procedure itself. If there are |
7432 | -- no default values this is also erroneous. | |
5d09245e | 7433 | |
1f70c47f AC |
7434 | if Is_Unchecked_Union (Base_Type (U_Type)) then |
7435 | if (not Is_TSS (Current_Scope, TSS_Stream_Output) | |
7436 | and not Is_TSS (Current_Scope, TSS_Stream_Write)) | |
7437 | or else No (Discriminant_Default_Value | |
7438 | (First_Discriminant (U_Type))) | |
7439 | then | |
7440 | Rewrite (N, | |
7441 | Make_Raise_Program_Error (Loc, | |
7442 | Reason => PE_Unchecked_Union_Restriction)); | |
7443 | Set_Etype (N, U_Type); | |
7444 | return; | |
7445 | end if; | |
5d09245e AC |
7446 | end if; |
7447 | ||
70482933 RK |
7448 | if Has_Discriminants (U_Type) |
7449 | and then Present | |
7450 | (Discriminant_Default_Value (First_Discriminant (U_Type))) | |
7451 | then | |
7452 | Build_Mutable_Record_Write_Procedure | |
96d2756f | 7453 | (Loc, Full_Base (U_Type), Decl, Pname); |
70482933 RK |
7454 | else |
7455 | Build_Record_Write_Procedure | |
96d2756f | 7456 | (Loc, Full_Base (U_Type), Decl, Pname); |
70482933 RK |
7457 | end if; |
7458 | ||
7459 | Insert_Action (N, Decl); | |
7460 | end if; | |
7461 | end if; | |
7462 | ||
7463 | -- If we fall through, Pname is the procedure to be called | |
7464 | ||
110d0820 | 7465 | Rewrite_Attribute_Proc_Call (Pname); |
70482933 RK |
7466 | end Write; |
7467 | ||
80d4224f RD |
7468 | -- The following attributes are handled by the back end (except that |
7469 | -- static cases have already been evaluated during semantic processing, | |
7470 | -- but in any case the back end should not count on this). | |
70482933 | 7471 | |
3874e79d | 7472 | when Attribute_Code_Address |
d8f43ee6 HK |
7473 | | Attribute_Deref |
7474 | | Attribute_Null_Parameter | |
7475 | | Attribute_Passed_By_Reference | |
7476 | | Attribute_Pool_Address | |
d8f43ee6 | 7477 | => |
70482933 RK |
7478 | null; |
7479 | ||
70482933 RK |
7480 | -- The following attributes should not appear at this stage, since they |
7481 | -- have already been handled by the analyzer (and properly rewritten | |
7482 | -- with corresponding values or entities to represent the right values) | |
7483 | ||
d8f43ee6 HK |
7484 | when Attribute_Abort_Signal |
7485 | | Attribute_Address_Size | |
3874e79d | 7486 | | Attribute_Aft |
d8f43ee6 HK |
7487 | | Attribute_Atomic_Always_Lock_Free |
7488 | | Attribute_Base | |
3874e79d | 7489 | | Attribute_Bit_Order |
d8f43ee6 HK |
7490 | | Attribute_Class |
7491 | | Attribute_Compiler_Version | |
7492 | | Attribute_Default_Bit_Order | |
7493 | | Attribute_Default_Scalar_Storage_Order | |
3874e79d | 7494 | | Attribute_Definite |
d8f43ee6 HK |
7495 | | Attribute_Delta |
7496 | | Attribute_Denorm | |
7497 | | Attribute_Digits | |
7498 | | Attribute_Emax | |
7499 | | Attribute_Enabled | |
7500 | | Attribute_Epsilon | |
7501 | | Attribute_Fast_Math | |
7502 | | Attribute_First_Valid | |
7503 | | Attribute_Has_Access_Values | |
7504 | | Attribute_Has_Discriminants | |
7505 | | Attribute_Has_Tagged_Values | |
7506 | | Attribute_Large | |
7507 | | Attribute_Last_Valid | |
7508 | | Attribute_Library_Level | |
7509 | | Attribute_Lock_Free | |
7510 | | Attribute_Machine_Emax | |
7511 | | Attribute_Machine_Emin | |
7512 | | Attribute_Machine_Mantissa | |
7513 | | Attribute_Machine_Overflows | |
7514 | | Attribute_Machine_Radix | |
7515 | | Attribute_Machine_Rounds | |
3874e79d | 7516 | | Attribute_Max_Alignment_For_Allocation |
d8f43ee6 HK |
7517 | | Attribute_Maximum_Alignment |
7518 | | Attribute_Model_Emin | |
7519 | | Attribute_Model_Epsilon | |
7520 | | Attribute_Model_Mantissa | |
7521 | | Attribute_Model_Small | |
7522 | | Attribute_Modulus | |
7523 | | Attribute_Partition_ID | |
7524 | | Attribute_Range | |
7525 | | Attribute_Restriction_Set | |
7526 | | Attribute_Safe_Emax | |
7527 | | Attribute_Safe_First | |
7528 | | Attribute_Safe_Large | |
7529 | | Attribute_Safe_Last | |
7530 | | Attribute_Safe_Small | |
3874e79d | 7531 | | Attribute_Scalar_Storage_Order |
d8f43ee6 HK |
7532 | | Attribute_Scale |
7533 | | Attribute_Signed_Zeros | |
7534 | | Attribute_Small | |
7535 | | Attribute_Storage_Unit | |
7536 | | Attribute_Stub_Type | |
7537 | | Attribute_System_Allocator_Alignment | |
7538 | | Attribute_Target_Name | |
7539 | | Attribute_Type_Class | |
7540 | | Attribute_Type_Key | |
7541 | | Attribute_Unconstrained_Array | |
7542 | | Attribute_Universal_Literal_String | |
7543 | | Attribute_Wchar_T_Size | |
7544 | | Attribute_Word_Size | |
7545 | => | |
70482933 | 7546 | raise Program_Error; |
70482933 RK |
7547 | end case; |
7548 | ||
2eef7403 AC |
7549 | -- Note: as mentioned earlier, individual sections of the above case |
7550 | -- statement assume there is no code after the case statement, and are | |
7551 | -- legitimately allowed to execute return statements if they have nothing | |
7552 | -- more to do, so DO NOT add code at this point. | |
7553 | ||
fbf5a39b AC |
7554 | exception |
7555 | when RE_Not_Available => | |
7556 | return; | |
70482933 RK |
7557 | end Expand_N_Attribute_Reference; |
7558 | ||
aa9b151a AC |
7559 | -------------------------------- |
7560 | -- Expand_Pred_Succ_Attribute -- | |
7561 | -------------------------------- | |
70482933 RK |
7562 | |
7563 | -- For typ'Pred (exp), we generate the check | |
7564 | ||
7565 | -- [constraint_error when exp = typ'Base'First] | |
7566 | ||
7567 | -- Similarly, for typ'Succ (exp), we generate the check | |
7568 | ||
7569 | -- [constraint_error when exp = typ'Base'Last] | |
7570 | ||
7571 | -- These checks are not generated for modular types, since the proper | |
7572 | -- semantics for Succ and Pred on modular types is to wrap, not raise CE. | |
c7532b2d AC |
7573 | -- We also suppress these checks if we are the right side of an assignment |
7574 | -- statement or the expression of an object declaration, where the flag | |
7575 | -- Suppress_Assignment_Checks is set for the assignment/declaration. | |
70482933 | 7576 | |
aa9b151a | 7577 | procedure Expand_Pred_Succ_Attribute (N : Node_Id) is |
70482933 | 7578 | Loc : constant Source_Ptr := Sloc (N); |
c7532b2d | 7579 | P : constant Node_Id := Parent (N); |
70482933 RK |
7580 | Cnam : Name_Id; |
7581 | ||
7582 | begin | |
7583 | if Attribute_Name (N) = Name_Pred then | |
7584 | Cnam := Name_First; | |
7585 | else | |
7586 | Cnam := Name_Last; | |
7587 | end if; | |
7588 | ||
c7532b2d AC |
7589 | if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration) |
7590 | or else not Suppress_Assignment_Checks (P) | |
7591 | then | |
7592 | Insert_Action (N, | |
7593 | Make_Raise_Constraint_Error (Loc, | |
7594 | Condition => | |
7595 | Make_Op_Eq (Loc, | |
7596 | Left_Opnd => | |
7597 | Duplicate_Subexpr_Move_Checks (First (Expressions (N))), | |
7598 | Right_Opnd => | |
7599 | Make_Attribute_Reference (Loc, | |
7600 | Prefix => | |
e4494292 | 7601 | New_Occurrence_Of (Base_Type (Etype (Prefix (N))), Loc), |
c7532b2d AC |
7602 | Attribute_Name => Cnam)), |
7603 | Reason => CE_Overflow_Check_Failed)); | |
7604 | end if; | |
aa9b151a | 7605 | end Expand_Pred_Succ_Attribute; |
70482933 | 7606 | |
d39f6b24 YM |
7607 | --------------------------- |
7608 | -- Expand_Size_Attribute -- | |
7609 | --------------------------- | |
7610 | ||
7611 | procedure Expand_Size_Attribute (N : Node_Id) is | |
7612 | Loc : constant Source_Ptr := Sloc (N); | |
7613 | Typ : constant Entity_Id := Etype (N); | |
7614 | Pref : constant Node_Id := Prefix (N); | |
7615 | Ptyp : constant Entity_Id := Etype (Pref); | |
7616 | Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N)); | |
7617 | Siz : Uint; | |
7618 | ||
7619 | begin | |
7620 | -- Case of known RM_Size of a type | |
7621 | ||
7622 | if (Id = Attribute_Size or else Id = Attribute_Value_Size) | |
7623 | and then Is_Entity_Name (Pref) | |
7624 | and then Is_Type (Entity (Pref)) | |
7625 | and then Known_Static_RM_Size (Entity (Pref)) | |
7626 | then | |
7627 | Siz := RM_Size (Entity (Pref)); | |
7628 | ||
7629 | -- Case of known Esize of a type | |
7630 | ||
7631 | elsif Id = Attribute_Object_Size | |
7632 | and then Is_Entity_Name (Pref) | |
7633 | and then Is_Type (Entity (Pref)) | |
7634 | and then Known_Static_Esize (Entity (Pref)) | |
7635 | then | |
7636 | Siz := Esize (Entity (Pref)); | |
7637 | ||
7638 | -- Case of known size of object | |
7639 | ||
7640 | elsif Id = Attribute_Size | |
7641 | and then Is_Entity_Name (Pref) | |
7642 | and then Is_Object (Entity (Pref)) | |
7643 | and then Known_Esize (Entity (Pref)) | |
7644 | and then Known_Static_Esize (Entity (Pref)) | |
7645 | then | |
7646 | Siz := Esize (Entity (Pref)); | |
7647 | ||
7648 | -- For an array component, we can do Size in the front end if the | |
7649 | -- component_size of the array is set. | |
7650 | ||
7651 | elsif Nkind (Pref) = N_Indexed_Component then | |
7652 | Siz := Component_Size (Etype (Prefix (Pref))); | |
7653 | ||
7654 | -- For a record component, we can do Size in the front end if there is a | |
7655 | -- component clause, or if the record is packed and the component's size | |
7656 | -- is known at compile time. | |
7657 | ||
7658 | elsif Nkind (Pref) = N_Selected_Component then | |
7659 | declare | |
7660 | Rec : constant Entity_Id := Etype (Prefix (Pref)); | |
7661 | Comp : constant Entity_Id := Entity (Selector_Name (Pref)); | |
7662 | ||
7663 | begin | |
7664 | if Present (Component_Clause (Comp)) then | |
7665 | Siz := Esize (Comp); | |
7666 | ||
7667 | elsif Is_Packed (Rec) then | |
7668 | Siz := RM_Size (Ptyp); | |
7669 | ||
7670 | else | |
7671 | Apply_Universal_Integer_Attribute_Checks (N); | |
7672 | return; | |
7673 | end if; | |
7674 | end; | |
7675 | ||
7676 | -- All other cases are handled by the back end | |
7677 | ||
7678 | else | |
d39f6b24 YM |
7679 | -- If Size is applied to a formal parameter that is of a packed |
7680 | -- array subtype, then apply Size to the actual subtype. | |
7681 | ||
7682 | if Is_Entity_Name (Pref) | |
7683 | and then Is_Formal (Entity (Pref)) | |
7684 | and then Is_Array_Type (Ptyp) | |
7685 | and then Is_Packed (Ptyp) | |
7686 | then | |
7687 | Rewrite (N, | |
7688 | Make_Attribute_Reference (Loc, | |
7689 | Prefix => | |
7690 | New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc), | |
7691 | Attribute_Name => Name_Size)); | |
7692 | Analyze_And_Resolve (N, Typ); | |
d39f6b24 | 7693 | |
37915d02 | 7694 | -- If Size is applied to a dereference of an access to unconstrained |
d39f6b24 YM |
7695 | -- packed array, the back end needs to see its unconstrained nominal |
7696 | -- type, but also a hint to the actual constrained type. | |
7697 | ||
37915d02 | 7698 | elsif Nkind (Pref) = N_Explicit_Dereference |
d39f6b24 YM |
7699 | and then Is_Array_Type (Ptyp) |
7700 | and then not Is_Constrained (Ptyp) | |
7701 | and then Is_Packed (Ptyp) | |
7702 | then | |
7703 | Set_Actual_Designated_Subtype (Pref, Get_Actual_Subtype (Pref)); | |
37915d02 EB |
7704 | |
7705 | -- If Size was applied to a slice of a bit-packed array, we rewrite | |
7706 | -- it into the product of Length and Component_Size. We need to do so | |
7707 | -- because bit-packed arrays are represented internally as arrays of | |
7708 | -- System.Unsigned_Types.Packed_Byte for code generation purposes so | |
7709 | -- the size is always rounded up in the back end. | |
7710 | ||
955379e4 | 7711 | elsif Nkind (Pref) = N_Slice and then Is_Bit_Packed_Array (Ptyp) then |
37915d02 EB |
7712 | Rewrite (N, |
7713 | Make_Op_Multiply (Loc, | |
7714 | Make_Attribute_Reference (Loc, | |
7715 | Prefix => Duplicate_Subexpr (Pref, True), | |
7716 | Attribute_Name => Name_Length), | |
7717 | Make_Attribute_Reference (Loc, | |
7718 | Prefix => Duplicate_Subexpr (Pref, True), | |
7719 | Attribute_Name => Name_Component_Size))); | |
7720 | Analyze_And_Resolve (N, Typ); | |
d39f6b24 YM |
7721 | end if; |
7722 | ||
955379e4 EB |
7723 | -- Apply the required checks last, after rewriting has taken place |
7724 | ||
7725 | Apply_Universal_Integer_Attribute_Checks (N); | |
d39f6b24 YM |
7726 | return; |
7727 | end if; | |
7728 | ||
7729 | -- Common processing for record and array component case | |
7730 | ||
7731 | if Siz /= No_Uint and then Siz /= 0 then | |
7732 | declare | |
7733 | CS : constant Boolean := Comes_From_Source (N); | |
7734 | ||
7735 | begin | |
7736 | Rewrite (N, Make_Integer_Literal (Loc, Siz)); | |
7737 | ||
7738 | -- This integer literal is not a static expression. We do not | |
7739 | -- call Analyze_And_Resolve here, because this would activate | |
7740 | -- the circuit for deciding that a static value was out of range, | |
7741 | -- and we don't want that. | |
7742 | ||
7743 | -- So just manually set the type, mark the expression as | |
7744 | -- nonstatic, and then ensure that the result is checked | |
7745 | -- properly if the attribute comes from source (if it was | |
7746 | -- internally generated, we never need a constraint check). | |
7747 | ||
7748 | Set_Etype (N, Typ); | |
7749 | Set_Is_Static_Expression (N, False); | |
7750 | ||
7751 | if CS then | |
7752 | Apply_Constraint_Check (N, Typ); | |
7753 | end if; | |
7754 | end; | |
7755 | end if; | |
7756 | end Expand_Size_Attribute; | |
7757 | ||
18a2ad5d AC |
7758 | ----------------------------- |
7759 | -- Expand_Update_Attribute -- | |
7760 | ----------------------------- | |
7761 | ||
7762 | procedure Expand_Update_Attribute (N : Node_Id) is | |
7763 | procedure Process_Component_Or_Element_Update | |
7764 | (Temp : Entity_Id; | |
7765 | Comp : Node_Id; | |
7766 | Expr : Node_Id; | |
7767 | Typ : Entity_Id); | |
7768 | -- Generate the statements necessary to update a single component or an | |
7769 | -- element of the prefix. The code is inserted before the attribute N. | |
7770 | -- Temp denotes the entity of the anonymous object created to reflect | |
7771 | -- the changes in values. Comp is the component/index expression to be | |
7772 | -- updated. Expr is an expression yielding the new value of Comp. Typ | |
7773 | -- is the type of the prefix of attribute Update. | |
7774 | ||
7775 | procedure Process_Range_Update | |
7776 | (Temp : Entity_Id; | |
7777 | Comp : Node_Id; | |
d12b19fa AC |
7778 | Expr : Node_Id; |
7779 | Typ : Entity_Id); | |
18a2ad5d AC |
7780 | -- Generate the statements necessary to update a slice of the prefix. |
7781 | -- The code is inserted before the attribute N. Temp denotes the entity | |
7782 | -- of the anonymous object created to reflect the changes in values. | |
7783 | -- Comp is range of the slice to be updated. Expr is an expression | |
d12b19fa AC |
7784 | -- yielding the new value of Comp. Typ is the type of the prefix of |
7785 | -- attribute Update. | |
18a2ad5d AC |
7786 | |
7787 | ----------------------------------------- | |
7788 | -- Process_Component_Or_Element_Update -- | |
7789 | ----------------------------------------- | |
7790 | ||
7791 | procedure Process_Component_Or_Element_Update | |
7792 | (Temp : Entity_Id; | |
7793 | Comp : Node_Id; | |
7794 | Expr : Node_Id; | |
7795 | Typ : Entity_Id) | |
7796 | is | |
7797 | Loc : constant Source_Ptr := Sloc (Comp); | |
7798 | Exprs : List_Id; | |
7799 | LHS : Node_Id; | |
7800 | ||
7801 | begin | |
7802 | -- An array element may be modified by the following relations | |
7803 | -- depending on the number of dimensions: | |
7804 | ||
7805 | -- 1 => Expr -- one dimensional update | |
7806 | -- (1, ..., N) => Expr -- multi dimensional update | |
7807 | ||
7808 | -- The above forms are converted in assignment statements where the | |
7809 | -- left hand side is an indexed component: | |
7810 | ||
7811 | -- Temp (1) := Expr; -- one dimensional update | |
7812 | -- Temp (1, ..., N) := Expr; -- multi dimensional update | |
7813 | ||
7814 | if Is_Array_Type (Typ) then | |
7815 | ||
7816 | -- The index expressions of a multi dimensional array update | |
7817 | -- appear as an aggregate. | |
7818 | ||
7819 | if Nkind (Comp) = N_Aggregate then | |
7820 | Exprs := New_Copy_List_Tree (Expressions (Comp)); | |
7821 | else | |
7822 | Exprs := New_List (Relocate_Node (Comp)); | |
7823 | end if; | |
7824 | ||
7825 | LHS := | |
7826 | Make_Indexed_Component (Loc, | |
e4494292 | 7827 | Prefix => New_Occurrence_Of (Temp, Loc), |
18a2ad5d AC |
7828 | Expressions => Exprs); |
7829 | ||
7830 | -- A record component update appears in the following form: | |
7831 | ||
7832 | -- Comp => Expr | |
7833 | ||
7834 | -- The above relation is transformed into an assignment statement | |
7835 | -- where the left hand side is a selected component: | |
7836 | ||
7837 | -- Temp.Comp := Expr; | |
7838 | ||
7839 | else pragma Assert (Is_Record_Type (Typ)); | |
7840 | LHS := | |
7841 | Make_Selected_Component (Loc, | |
e4494292 | 7842 | Prefix => New_Occurrence_Of (Temp, Loc), |
18a2ad5d AC |
7843 | Selector_Name => Relocate_Node (Comp)); |
7844 | end if; | |
7845 | ||
7846 | Insert_Action (N, | |
7847 | Make_Assignment_Statement (Loc, | |
7848 | Name => LHS, | |
7849 | Expression => Relocate_Node (Expr))); | |
7850 | end Process_Component_Or_Element_Update; | |
7851 | ||
7852 | -------------------------- | |
7853 | -- Process_Range_Update -- | |
7854 | -------------------------- | |
7855 | ||
7856 | procedure Process_Range_Update | |
7857 | (Temp : Entity_Id; | |
7858 | Comp : Node_Id; | |
d12b19fa AC |
7859 | Expr : Node_Id; |
7860 | Typ : Entity_Id) | |
18a2ad5d | 7861 | is |
d12b19fa AC |
7862 | Index_Typ : constant Entity_Id := Etype (First_Index (Typ)); |
7863 | Loc : constant Source_Ptr := Sloc (Comp); | |
7864 | Index : Entity_Id; | |
18a2ad5d AC |
7865 | |
7866 | begin | |
7867 | -- A range update appears as | |
7868 | ||
7869 | -- (Low .. High => Expr) | |
7870 | ||
7871 | -- The above construct is transformed into a loop that iterates over | |
7872 | -- the given range and modifies the corresponding array values to the | |
7873 | -- value of Expr: | |
7874 | ||
7875 | -- for Index in Low .. High loop | |
d12b19fa | 7876 | -- Temp (<Index_Typ> (Index)) := Expr; |
18a2ad5d AC |
7877 | -- end loop; |
7878 | ||
7879 | Index := Make_Temporary (Loc, 'I'); | |
7880 | ||
7881 | Insert_Action (N, | |
7882 | Make_Loop_Statement (Loc, | |
7883 | Iteration_Scheme => | |
7884 | Make_Iteration_Scheme (Loc, | |
7885 | Loop_Parameter_Specification => | |
7886 | Make_Loop_Parameter_Specification (Loc, | |
7887 | Defining_Identifier => Index, | |
7888 | Discrete_Subtype_Definition => Relocate_Node (Comp))), | |
7889 | ||
7890 | Statements => New_List ( | |
7891 | Make_Assignment_Statement (Loc, | |
7892 | Name => | |
7893 | Make_Indexed_Component (Loc, | |
e4494292 | 7894 | Prefix => New_Occurrence_Of (Temp, Loc), |
d12b19fa | 7895 | Expressions => New_List ( |
e4494292 RD |
7896 | Convert_To (Index_Typ, |
7897 | New_Occurrence_Of (Index, Loc)))), | |
18a2ad5d AC |
7898 | Expression => Relocate_Node (Expr))), |
7899 | ||
7900 | End_Label => Empty)); | |
7901 | end Process_Range_Update; | |
7902 | ||
7903 | -- Local variables | |
7904 | ||
6c802906 AC |
7905 | Aggr : constant Node_Id := First (Expressions (N)); |
7906 | Loc : constant Source_Ptr := Sloc (N); | |
7907 | Pref : constant Node_Id := Prefix (N); | |
7908 | Typ : constant Entity_Id := Etype (Pref); | |
7909 | Assoc : Node_Id; | |
7910 | Comp : Node_Id; | |
7911 | CW_Temp : Entity_Id; | |
7912 | CW_Typ : Entity_Id; | |
7913 | Expr : Node_Id; | |
7914 | Temp : Entity_Id; | |
18a2ad5d AC |
7915 | |
7916 | -- Start of processing for Expand_Update_Attribute | |
7917 | ||
7918 | begin | |
6c802906 AC |
7919 | -- Create the anonymous object to store the value of the prefix and |
7920 | -- capture subsequent changes in value. | |
7921 | ||
7922 | Temp := Make_Temporary (Loc, 'T', Pref); | |
18a2ad5d | 7923 | |
6c802906 AC |
7924 | -- Preserve the tag of the prefix by offering a specific view of the |
7925 | -- class-wide version of the prefix. | |
18a2ad5d | 7926 | |
6c802906 | 7927 | if Is_Tagged_Type (Typ) then |
18a2ad5d | 7928 | |
6c802906 AC |
7929 | -- Generate: |
7930 | -- CW_Temp : Typ'Class := Typ'Class (Pref); | |
7931 | ||
7932 | CW_Temp := Make_Temporary (Loc, 'T'); | |
7933 | CW_Typ := Class_Wide_Type (Typ); | |
7934 | ||
7935 | Insert_Action (N, | |
7936 | Make_Object_Declaration (Loc, | |
7937 | Defining_Identifier => CW_Temp, | |
7938 | Object_Definition => New_Occurrence_Of (CW_Typ, Loc), | |
7939 | Expression => | |
7940 | Convert_To (CW_Typ, Relocate_Node (Pref)))); | |
7941 | ||
7942 | -- Generate: | |
7943 | -- Temp : Typ renames Typ (CW_Temp); | |
7944 | ||
7945 | Insert_Action (N, | |
7946 | Make_Object_Renaming_Declaration (Loc, | |
7947 | Defining_Identifier => Temp, | |
7948 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
7949 | Name => | |
7950 | Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc)))); | |
7951 | ||
7952 | -- Non-tagged case | |
7953 | ||
7954 | else | |
7955 | -- Generate: | |
7956 | -- Temp : Typ := Pref; | |
7957 | ||
7958 | Insert_Action (N, | |
7959 | Make_Object_Declaration (Loc, | |
7960 | Defining_Identifier => Temp, | |
7961 | Object_Definition => New_Occurrence_Of (Typ, Loc), | |
7962 | Expression => Relocate_Node (Pref))); | |
7963 | end if; | |
18a2ad5d AC |
7964 | |
7965 | -- Process the update aggregate | |
7966 | ||
7967 | Assoc := First (Component_Associations (Aggr)); | |
7968 | while Present (Assoc) loop | |
7969 | Comp := First (Choices (Assoc)); | |
7970 | Expr := Expression (Assoc); | |
7971 | while Present (Comp) loop | |
7972 | if Nkind (Comp) = N_Range then | |
d12b19fa | 7973 | Process_Range_Update (Temp, Comp, Expr, Typ); |
18a2ad5d AC |
7974 | else |
7975 | Process_Component_Or_Element_Update (Temp, Comp, Expr, Typ); | |
7976 | end if; | |
7977 | ||
7978 | Next (Comp); | |
7979 | end loop; | |
7980 | ||
7981 | Next (Assoc); | |
7982 | end loop; | |
7983 | ||
7984 | -- The attribute is replaced by a reference to the anonymous object | |
7985 | ||
e4494292 | 7986 | Rewrite (N, New_Occurrence_Of (Temp, Loc)); |
18a2ad5d AC |
7987 | Analyze (N); |
7988 | end Expand_Update_Attribute; | |
7989 | ||
65f01153 RD |
7990 | ------------------- |
7991 | -- Find_Fat_Info -- | |
7992 | ------------------- | |
7993 | ||
7994 | procedure Find_Fat_Info | |
7995 | (T : Entity_Id; | |
7996 | Fat_Type : out Entity_Id; | |
7997 | Fat_Pkg : out RE_Id) | |
7998 | is | |
65f01153 | 7999 | Rtyp : constant Entity_Id := Root_Type (T); |
65f01153 RD |
8000 | |
8001 | begin | |
80211802 AC |
8002 | -- All we do is use the root type (historically this dealt with |
8003 | -- VAX-float .. to be cleaned up further later ???) | |
65f01153 | 8004 | |
80211802 | 8005 | Fat_Type := Rtyp; |
65f01153 | 8006 | |
80211802 AC |
8007 | if Fat_Type = Standard_Short_Float then |
8008 | Fat_Pkg := RE_Attr_Short_Float; | |
65f01153 | 8009 | |
80211802 AC |
8010 | elsif Fat_Type = Standard_Float then |
8011 | Fat_Pkg := RE_Attr_Float; | |
65f01153 | 8012 | |
80211802 AC |
8013 | elsif Fat_Type = Standard_Long_Float then |
8014 | Fat_Pkg := RE_Attr_Long_Float; | |
7ce611e2 | 8015 | |
80211802 AC |
8016 | elsif Fat_Type = Standard_Long_Long_Float then |
8017 | Fat_Pkg := RE_Attr_Long_Long_Float; | |
7ce611e2 ES |
8018 | |
8019 | -- Universal real (which is its own root type) is treated as being | |
8020 | -- equivalent to Standard.Long_Long_Float, since it is defined to | |
8021 | -- have the same precision as the longest Float type. | |
8022 | ||
80211802 AC |
8023 | elsif Fat_Type = Universal_Real then |
8024 | Fat_Type := Standard_Long_Long_Float; | |
8025 | Fat_Pkg := RE_Attr_Long_Long_Float; | |
7ce611e2 | 8026 | |
80211802 AC |
8027 | else |
8028 | raise Program_Error; | |
65f01153 RD |
8029 | end if; |
8030 | end Find_Fat_Info; | |
8031 | ||
fbf5a39b AC |
8032 | ---------------------------- |
8033 | -- Find_Stream_Subprogram -- | |
8034 | ---------------------------- | |
8035 | ||
8036 | function Find_Stream_Subprogram | |
8037 | (Typ : Entity_Id; | |
758c442c GD |
8038 | Nam : TSS_Name_Type) return Entity_Id |
8039 | is | |
b2c6b35f HK |
8040 | Base_Typ : constant Entity_Id := Base_Type (Typ); |
8041 | Ent : constant Entity_Id := TSS (Typ, Nam); | |
fbf5a39b | 8042 | begin |
758c442c GD |
8043 | if Present (Ent) then |
8044 | return Ent; | |
8045 | end if; | |
8046 | ||
21d27997 RD |
8047 | -- Stream attributes for strings are expanded into library calls. The |
8048 | -- following checks are disabled when the run-time is not available or | |
8049 | -- when compiling predefined types due to bootstrap issues. As a result, | |
8050 | -- the compiler will generate in-place stream routines for string types | |
8051 | -- that appear in GNAT's library, but will generate calls via rtsfind | |
8052 | -- to library routines for user code. | |
f4b049db | 8053 | |
a20f4389 | 8054 | -- Note: In the case of using a configurable run time, it is very likely |
90878b12 AC |
8055 | -- that stream routines for string types are not present (they require |
8056 | -- file system support). In this case, the specific stream routines for | |
8057 | -- strings are not used, relying on the regular stream mechanism | |
6c9e4a1d | 8058 | -- instead. That is why we include the test RTE_Available when dealing |
a20f4389 | 8059 | -- with these cases. |
90878b12 | 8060 | |
8ab31c0c | 8061 | if not Is_Predefined_Unit (Current_Sem_Unit) then |
161c5cc5 AC |
8062 | -- Storage_Array as defined in package System.Storage_Elements |
8063 | ||
8064 | if Is_RTE (Base_Typ, RE_Storage_Array) then | |
8065 | ||
8066 | -- Case of No_Stream_Optimizations restriction active | |
8067 | ||
8068 | if Restriction_Active (No_Stream_Optimizations) then | |
8069 | if Nam = TSS_Stream_Input | |
6c9e4a1d | 8070 | and then RTE_Available (RE_Storage_Array_Input) |
161c5cc5 AC |
8071 | then |
8072 | return RTE (RE_Storage_Array_Input); | |
8073 | ||
8074 | elsif Nam = TSS_Stream_Output | |
6c9e4a1d | 8075 | and then RTE_Available (RE_Storage_Array_Output) |
161c5cc5 AC |
8076 | then |
8077 | return RTE (RE_Storage_Array_Output); | |
8078 | ||
8079 | elsif Nam = TSS_Stream_Read | |
6c9e4a1d | 8080 | and then RTE_Available (RE_Storage_Array_Read) |
161c5cc5 AC |
8081 | then |
8082 | return RTE (RE_Storage_Array_Read); | |
8083 | ||
8084 | elsif Nam = TSS_Stream_Write | |
6c9e4a1d | 8085 | and then RTE_Available (RE_Storage_Array_Write) |
161c5cc5 AC |
8086 | then |
8087 | return RTE (RE_Storage_Array_Write); | |
8088 | ||
8089 | elsif Nam /= TSS_Stream_Input and then | |
8090 | Nam /= TSS_Stream_Output and then | |
8091 | Nam /= TSS_Stream_Read and then | |
8092 | Nam /= TSS_Stream_Write | |
8093 | then | |
8094 | raise Program_Error; | |
8095 | end if; | |
8096 | ||
8097 | -- Restriction No_Stream_Optimizations is not set, so we can go | |
8098 | -- ahead and optimize using the block IO forms of the routines. | |
8099 | ||
8100 | else | |
8101 | if Nam = TSS_Stream_Input | |
6c9e4a1d | 8102 | and then RTE_Available (RE_Storage_Array_Input_Blk_IO) |
161c5cc5 AC |
8103 | then |
8104 | return RTE (RE_Storage_Array_Input_Blk_IO); | |
8105 | ||
8106 | elsif Nam = TSS_Stream_Output | |
6c9e4a1d | 8107 | and then RTE_Available (RE_Storage_Array_Output_Blk_IO) |
161c5cc5 AC |
8108 | then |
8109 | return RTE (RE_Storage_Array_Output_Blk_IO); | |
8110 | ||
8111 | elsif Nam = TSS_Stream_Read | |
6c9e4a1d | 8112 | and then RTE_Available (RE_Storage_Array_Read_Blk_IO) |
161c5cc5 AC |
8113 | then |
8114 | return RTE (RE_Storage_Array_Read_Blk_IO); | |
8115 | ||
8116 | elsif Nam = TSS_Stream_Write | |
6c9e4a1d | 8117 | and then RTE_Available (RE_Storage_Array_Write_Blk_IO) |
161c5cc5 AC |
8118 | then |
8119 | return RTE (RE_Storage_Array_Write_Blk_IO); | |
8120 | ||
8121 | elsif Nam /= TSS_Stream_Input and then | |
8122 | Nam /= TSS_Stream_Output and then | |
8123 | Nam /= TSS_Stream_Read and then | |
8124 | Nam /= TSS_Stream_Write | |
8125 | then | |
8126 | raise Program_Error; | |
8127 | end if; | |
8128 | end if; | |
8129 | ||
8130 | -- Stream_Element_Array as defined in package Ada.Streams | |
8131 | ||
8132 | elsif Is_RTE (Base_Typ, RE_Stream_Element_Array) then | |
8133 | ||
8134 | -- Case of No_Stream_Optimizations restriction active | |
8135 | ||
8136 | if Restriction_Active (No_Stream_Optimizations) then | |
8137 | if Nam = TSS_Stream_Input | |
6c9e4a1d | 8138 | and then RTE_Available (RE_Stream_Element_Array_Input) |
161c5cc5 AC |
8139 | then |
8140 | return RTE (RE_Stream_Element_Array_Input); | |
8141 | ||
8142 | elsif Nam = TSS_Stream_Output | |
6c9e4a1d | 8143 | and then RTE_Available (RE_Stream_Element_Array_Output) |
161c5cc5 AC |
8144 | then |
8145 | return RTE (RE_Stream_Element_Array_Output); | |
8146 | ||
8147 | elsif Nam = TSS_Stream_Read | |
6c9e4a1d | 8148 | and then RTE_Available (RE_Stream_Element_Array_Read) |
161c5cc5 AC |
8149 | then |
8150 | return RTE (RE_Stream_Element_Array_Read); | |
8151 | ||
8152 | elsif Nam = TSS_Stream_Write | |
6c9e4a1d | 8153 | and then RTE_Available (RE_Stream_Element_Array_Write) |
161c5cc5 AC |
8154 | then |
8155 | return RTE (RE_Stream_Element_Array_Write); | |
8156 | ||
8157 | elsif Nam /= TSS_Stream_Input and then | |
8158 | Nam /= TSS_Stream_Output and then | |
8159 | Nam /= TSS_Stream_Read and then | |
8160 | Nam /= TSS_Stream_Write | |
8161 | then | |
8162 | raise Program_Error; | |
8163 | end if; | |
8164 | ||
8165 | -- Restriction No_Stream_Optimizations is not set, so we can go | |
8166 | -- ahead and optimize using the block IO forms of the routines. | |
8167 | ||
8168 | else | |
8169 | if Nam = TSS_Stream_Input | |
6c9e4a1d | 8170 | and then RTE_Available (RE_Stream_Element_Array_Input_Blk_IO) |
161c5cc5 AC |
8171 | then |
8172 | return RTE (RE_Stream_Element_Array_Input_Blk_IO); | |
8173 | ||
8174 | elsif Nam = TSS_Stream_Output | |
6c9e4a1d | 8175 | and then RTE_Available (RE_Stream_Element_Array_Output_Blk_IO) |
161c5cc5 AC |
8176 | then |
8177 | return RTE (RE_Stream_Element_Array_Output_Blk_IO); | |
8178 | ||
8179 | elsif Nam = TSS_Stream_Read | |
6c9e4a1d | 8180 | and then RTE_Available (RE_Stream_Element_Array_Read_Blk_IO) |
161c5cc5 AC |
8181 | then |
8182 | return RTE (RE_Stream_Element_Array_Read_Blk_IO); | |
8183 | ||
8184 | elsif Nam = TSS_Stream_Write | |
6c9e4a1d | 8185 | and then RTE_Available (RE_Stream_Element_Array_Write_Blk_IO) |
161c5cc5 AC |
8186 | then |
8187 | return RTE (RE_Stream_Element_Array_Write_Blk_IO); | |
8188 | ||
8189 | elsif Nam /= TSS_Stream_Input and then | |
8190 | Nam /= TSS_Stream_Output and then | |
8191 | Nam /= TSS_Stream_Read and then | |
8192 | Nam /= TSS_Stream_Write | |
8193 | then | |
8194 | raise Program_Error; | |
8195 | end if; | |
8196 | end if; | |
8197 | ||
21d27997 RD |
8198 | -- String as defined in package Ada |
8199 | ||
161c5cc5 AC |
8200 | elsif Base_Typ = Standard_String then |
8201 | ||
8202 | -- Case of No_Stream_Optimizations restriction active | |
8203 | ||
585df50b | 8204 | if Restriction_Active (No_Stream_Optimizations) then |
90878b12 | 8205 | if Nam = TSS_Stream_Input |
6c9e4a1d | 8206 | and then RTE_Available (RE_String_Input) |
90878b12 | 8207 | then |
585df50b AC |
8208 | return RTE (RE_String_Input); |
8209 | ||
90878b12 | 8210 | elsif Nam = TSS_Stream_Output |
6c9e4a1d | 8211 | and then RTE_Available (RE_String_Output) |
90878b12 | 8212 | then |
585df50b AC |
8213 | return RTE (RE_String_Output); |
8214 | ||
90878b12 | 8215 | elsif Nam = TSS_Stream_Read |
6c9e4a1d | 8216 | and then RTE_Available (RE_String_Read) |
90878b12 | 8217 | then |
585df50b | 8218 | return RTE (RE_String_Read); |
21d27997 | 8219 | |
90878b12 | 8220 | elsif Nam = TSS_Stream_Write |
6c9e4a1d | 8221 | and then RTE_Available (RE_String_Write) |
90878b12 | 8222 | then |
585df50b | 8223 | return RTE (RE_String_Write); |
90878b12 AC |
8224 | |
8225 | elsif Nam /= TSS_Stream_Input and then | |
8226 | Nam /= TSS_Stream_Output and then | |
8227 | Nam /= TSS_Stream_Read and then | |
8228 | Nam /= TSS_Stream_Write | |
8229 | then | |
8230 | raise Program_Error; | |
585df50b AC |
8231 | end if; |
8232 | ||
161c5cc5 AC |
8233 | -- Restriction No_Stream_Optimizations is not set, so we can go |
8234 | -- ahead and optimize using the block IO forms of the routines. | |
8235 | ||
585df50b | 8236 | else |
90878b12 | 8237 | if Nam = TSS_Stream_Input |
6c9e4a1d | 8238 | and then RTE_Available (RE_String_Input_Blk_IO) |
90878b12 | 8239 | then |
585df50b | 8240 | return RTE (RE_String_Input_Blk_IO); |
21d27997 | 8241 | |
90878b12 | 8242 | elsif Nam = TSS_Stream_Output |
6c9e4a1d | 8243 | and then RTE_Available (RE_String_Output_Blk_IO) |
90878b12 | 8244 | then |
585df50b | 8245 | return RTE (RE_String_Output_Blk_IO); |
21d27997 | 8246 | |
90878b12 | 8247 | elsif Nam = TSS_Stream_Read |
6c9e4a1d | 8248 | and then RTE_Available (RE_String_Read_Blk_IO) |
90878b12 | 8249 | then |
585df50b AC |
8250 | return RTE (RE_String_Read_Blk_IO); |
8251 | ||
90878b12 | 8252 | elsif Nam = TSS_Stream_Write |
6c9e4a1d | 8253 | and then RTE_Available (RE_String_Write_Blk_IO) |
90878b12 | 8254 | then |
585df50b | 8255 | return RTE (RE_String_Write_Blk_IO); |
90878b12 | 8256 | |
161c5cc5 | 8257 | elsif Nam /= TSS_Stream_Input and then |
90878b12 | 8258 | Nam /= TSS_Stream_Output and then |
161c5cc5 | 8259 | Nam /= TSS_Stream_Read and then |
90878b12 AC |
8260 | Nam /= TSS_Stream_Write |
8261 | then | |
8262 | raise Program_Error; | |
585df50b | 8263 | end if; |
21d27997 RD |
8264 | end if; |
8265 | ||
8266 | -- Wide_String as defined in package Ada | |
8267 | ||
b2c6b35f | 8268 | elsif Base_Typ = Standard_Wide_String then |
161c5cc5 AC |
8269 | |
8270 | -- Case of No_Stream_Optimizations restriction active | |
8271 | ||
585df50b | 8272 | if Restriction_Active (No_Stream_Optimizations) then |
90878b12 | 8273 | if Nam = TSS_Stream_Input |
6c9e4a1d | 8274 | and then RTE_Available (RE_Wide_String_Input) |
90878b12 | 8275 | then |
585df50b AC |
8276 | return RTE (RE_Wide_String_Input); |
8277 | ||
90878b12 | 8278 | elsif Nam = TSS_Stream_Output |
6c9e4a1d | 8279 | and then RTE_Available (RE_Wide_String_Output) |
90878b12 | 8280 | then |
585df50b AC |
8281 | return RTE (RE_Wide_String_Output); |
8282 | ||
90878b12 | 8283 | elsif Nam = TSS_Stream_Read |
6c9e4a1d | 8284 | and then RTE_Available (RE_Wide_String_Read) |
90878b12 | 8285 | then |
585df50b AC |
8286 | return RTE (RE_Wide_String_Read); |
8287 | ||
90878b12 | 8288 | elsif Nam = TSS_Stream_Write |
6c9e4a1d | 8289 | and then RTE_Available (RE_Wide_String_Write) |
90878b12 | 8290 | then |
585df50b | 8291 | return RTE (RE_Wide_String_Write); |
90878b12 | 8292 | |
161c5cc5 | 8293 | elsif Nam /= TSS_Stream_Input and then |
90878b12 | 8294 | Nam /= TSS_Stream_Output and then |
161c5cc5 | 8295 | Nam /= TSS_Stream_Read and then |
90878b12 AC |
8296 | Nam /= TSS_Stream_Write |
8297 | then | |
8298 | raise Program_Error; | |
585df50b AC |
8299 | end if; |
8300 | ||
161c5cc5 AC |
8301 | -- Restriction No_Stream_Optimizations is not set, so we can go |
8302 | -- ahead and optimize using the block IO forms of the routines. | |
8303 | ||
585df50b | 8304 | else |
90878b12 | 8305 | if Nam = TSS_Stream_Input |
6c9e4a1d | 8306 | and then RTE_Available (RE_Wide_String_Input_Blk_IO) |
90878b12 | 8307 | then |
585df50b | 8308 | return RTE (RE_Wide_String_Input_Blk_IO); |
21d27997 | 8309 | |
90878b12 | 8310 | elsif Nam = TSS_Stream_Output |
6c9e4a1d | 8311 | and then RTE_Available (RE_Wide_String_Output_Blk_IO) |
90878b12 | 8312 | then |
585df50b | 8313 | return RTE (RE_Wide_String_Output_Blk_IO); |
21d27997 | 8314 | |
90878b12 | 8315 | elsif Nam = TSS_Stream_Read |
6c9e4a1d | 8316 | and then RTE_Available (RE_Wide_String_Read_Blk_IO) |
90878b12 | 8317 | then |
585df50b | 8318 | return RTE (RE_Wide_String_Read_Blk_IO); |
21d27997 | 8319 | |
90878b12 | 8320 | elsif Nam = TSS_Stream_Write |
6c9e4a1d | 8321 | and then RTE_Available (RE_Wide_String_Write_Blk_IO) |
90878b12 | 8322 | then |
585df50b | 8323 | return RTE (RE_Wide_String_Write_Blk_IO); |
90878b12 | 8324 | |
161c5cc5 | 8325 | elsif Nam /= TSS_Stream_Input and then |
90878b12 | 8326 | Nam /= TSS_Stream_Output and then |
161c5cc5 | 8327 | Nam /= TSS_Stream_Read and then |
90878b12 AC |
8328 | Nam /= TSS_Stream_Write |
8329 | then | |
8330 | raise Program_Error; | |
585df50b | 8331 | end if; |
21d27997 RD |
8332 | end if; |
8333 | ||
8334 | -- Wide_Wide_String as defined in package Ada | |
8335 | ||
b2c6b35f | 8336 | elsif Base_Typ = Standard_Wide_Wide_String then |
161c5cc5 AC |
8337 | |
8338 | -- Case of No_Stream_Optimizations restriction active | |
8339 | ||
585df50b | 8340 | if Restriction_Active (No_Stream_Optimizations) then |
90878b12 | 8341 | if Nam = TSS_Stream_Input |
6c9e4a1d | 8342 | and then RTE_Available (RE_Wide_Wide_String_Input) |
90878b12 | 8343 | then |
585df50b AC |
8344 | return RTE (RE_Wide_Wide_String_Input); |
8345 | ||
90878b12 | 8346 | elsif Nam = TSS_Stream_Output |
6c9e4a1d | 8347 | and then RTE_Available (RE_Wide_Wide_String_Output) |
90878b12 | 8348 | then |
585df50b | 8349 | return RTE (RE_Wide_Wide_String_Output); |
21d27997 | 8350 | |
90878b12 | 8351 | elsif Nam = TSS_Stream_Read |
6c9e4a1d | 8352 | and then RTE_Available (RE_Wide_Wide_String_Read) |
90878b12 | 8353 | then |
585df50b | 8354 | return RTE (RE_Wide_Wide_String_Read); |
21d27997 | 8355 | |
90878b12 | 8356 | elsif Nam = TSS_Stream_Write |
6c9e4a1d | 8357 | and then RTE_Available (RE_Wide_Wide_String_Write) |
90878b12 | 8358 | then |
585df50b | 8359 | return RTE (RE_Wide_Wide_String_Write); |
90878b12 | 8360 | |
161c5cc5 | 8361 | elsif Nam /= TSS_Stream_Input and then |
90878b12 | 8362 | Nam /= TSS_Stream_Output and then |
161c5cc5 | 8363 | Nam /= TSS_Stream_Read and then |
90878b12 AC |
8364 | Nam /= TSS_Stream_Write |
8365 | then | |
8366 | raise Program_Error; | |
585df50b | 8367 | end if; |
21d27997 | 8368 | |
161c5cc5 AC |
8369 | -- Restriction No_Stream_Optimizations is not set, so we can go |
8370 | -- ahead and optimize using the block IO forms of the routines. | |
8371 | ||
585df50b | 8372 | else |
90878b12 | 8373 | if Nam = TSS_Stream_Input |
6c9e4a1d | 8374 | and then RTE_Available (RE_Wide_Wide_String_Input_Blk_IO) |
90878b12 | 8375 | then |
585df50b AC |
8376 | return RTE (RE_Wide_Wide_String_Input_Blk_IO); |
8377 | ||
90878b12 | 8378 | elsif Nam = TSS_Stream_Output |
6c9e4a1d | 8379 | and then RTE_Available (RE_Wide_Wide_String_Output_Blk_IO) |
90878b12 | 8380 | then |
585df50b AC |
8381 | return RTE (RE_Wide_Wide_String_Output_Blk_IO); |
8382 | ||
90878b12 | 8383 | elsif Nam = TSS_Stream_Read |
6c9e4a1d | 8384 | and then RTE_Available (RE_Wide_Wide_String_Read_Blk_IO) |
90878b12 | 8385 | then |
585df50b AC |
8386 | return RTE (RE_Wide_Wide_String_Read_Blk_IO); |
8387 | ||
90878b12 | 8388 | elsif Nam = TSS_Stream_Write |
6c9e4a1d | 8389 | and then RTE_Available (RE_Wide_Wide_String_Write_Blk_IO) |
90878b12 | 8390 | then |
585df50b | 8391 | return RTE (RE_Wide_Wide_String_Write_Blk_IO); |
90878b12 | 8392 | |
161c5cc5 | 8393 | elsif Nam /= TSS_Stream_Input and then |
90878b12 | 8394 | Nam /= TSS_Stream_Output and then |
161c5cc5 | 8395 | Nam /= TSS_Stream_Read and then |
90878b12 AC |
8396 | Nam /= TSS_Stream_Write |
8397 | then | |
8398 | raise Program_Error; | |
585df50b | 8399 | end if; |
21d27997 RD |
8400 | end if; |
8401 | end if; | |
8402 | end if; | |
8403 | ||
161c5cc5 | 8404 | if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then |
fbf5a39b AC |
8405 | return Find_Prim_Op (Typ, Nam); |
8406 | else | |
8407 | return Find_Inherited_TSS (Typ, Nam); | |
8408 | end if; | |
8409 | end Find_Stream_Subprogram; | |
8410 | ||
96d2756f AC |
8411 | --------------- |
8412 | -- Full_Base -- | |
8413 | --------------- | |
8414 | ||
8415 | function Full_Base (T : Entity_Id) return Entity_Id is | |
8416 | BT : Entity_Id; | |
8417 | ||
8418 | begin | |
8419 | BT := Base_Type (T); | |
8420 | ||
8421 | if Is_Private_Type (BT) | |
8422 | and then Present (Full_View (BT)) | |
8423 | then | |
8424 | BT := Full_View (BT); | |
8425 | end if; | |
8426 | ||
8427 | return BT; | |
8428 | end Full_Base; | |
8429 | ||
1d571f3b AC |
8430 | ------------------------------- |
8431 | -- Get_Stream_Convert_Pragma -- | |
8432 | ------------------------------- | |
8433 | ||
8434 | function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is | |
8435 | Typ : Entity_Id; | |
8436 | N : Node_Id; | |
8437 | ||
8438 | begin | |
8439 | -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity | |
8440 | -- that a stream convert pragma for a tagged type is not inherited from | |
8441 | -- its parent. Probably what is wrong here is that it is basically | |
8442 | -- incorrect to consider a stream convert pragma to be a representation | |
8443 | -- pragma at all ??? | |
8444 | ||
8445 | N := First_Rep_Item (Implementation_Base_Type (T)); | |
8446 | while Present (N) loop | |
e10dab7f | 8447 | if Nkind (N) = N_Pragma |
6e759c2a | 8448 | and then Pragma_Name (N) = Name_Stream_Convert |
e10dab7f | 8449 | then |
1d571f3b AC |
8450 | -- For tagged types this pragma is not inherited, so we |
8451 | -- must verify that it is defined for the given type and | |
8452 | -- not an ancestor. | |
8453 | ||
8454 | Typ := | |
8455 | Entity (Expression (First (Pragma_Argument_Associations (N)))); | |
8456 | ||
8457 | if not Is_Tagged_Type (T) | |
8458 | or else T = Typ | |
8459 | or else (Is_Private_Type (Typ) and then T = Full_View (Typ)) | |
8460 | then | |
8461 | return N; | |
8462 | end if; | |
8463 | end if; | |
8464 | ||
8465 | Next_Rep_Item (N); | |
8466 | end loop; | |
8467 | ||
8468 | return Empty; | |
8469 | end Get_Stream_Convert_Pragma; | |
8470 | ||
70482933 RK |
8471 | --------------------------------- |
8472 | -- Is_Constrained_Packed_Array -- | |
8473 | --------------------------------- | |
8474 | ||
8475 | function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is | |
8476 | Arr : Entity_Id := Typ; | |
8477 | ||
8478 | begin | |
8479 | if Is_Access_Type (Arr) then | |
8480 | Arr := Designated_Type (Arr); | |
8481 | end if; | |
8482 | ||
8483 | return Is_Array_Type (Arr) | |
8484 | and then Is_Constrained (Arr) | |
8ca597af | 8485 | and then Present (Packed_Array_Impl_Type (Arr)); |
70482933 RK |
8486 | end Is_Constrained_Packed_Array; |
8487 | ||
0669bebe GB |
8488 | ---------------------------------------- |
8489 | -- Is_Inline_Floating_Point_Attribute -- | |
8490 | ---------------------------------------- | |
8491 | ||
8492 | function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is | |
8493 | Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N)); | |
8494 | ||
d18bbd25 AC |
8495 | function Is_GCC_Target return Boolean; |
8496 | -- Return True if we are using a GCC target/back-end | |
8497 | -- ??? Note: the implementation is kludgy/fragile | |
8498 | ||
8499 | ------------------- | |
8500 | -- Is_GCC_Target -- | |
8501 | ------------------- | |
8502 | ||
8503 | function Is_GCC_Target return Boolean is | |
8504 | begin | |
9a476d75 | 8505 | return not CodePeer_Mode |
c63a2ad6 | 8506 | and then not Modify_Tree_For_C; |
d18bbd25 AC |
8507 | end Is_GCC_Target; |
8508 | ||
b943a971 | 8509 | -- Start of processing for Is_Inline_Floating_Point_Attribute |
d18bbd25 | 8510 | |
0669bebe | 8511 | begin |
56af8688 | 8512 | -- Machine and Model can be expanded by the GCC back end only |
78433fec | 8513 | |
24228312 | 8514 | if Id = Attribute_Machine or else Id = Attribute_Model then |
f8f50235 | 8515 | return Is_GCC_Target; |
78433fec | 8516 | |
d18bbd25 | 8517 | -- Remaining cases handled by all back ends are Rounding and Truncation |
78433fec | 8518 | -- when appearing as the operand of a conversion to some integer type. |
24228312 AC |
8519 | |
8520 | elsif Nkind (Parent (N)) /= N_Type_Conversion | |
0669bebe GB |
8521 | or else not Is_Integer_Type (Etype (Parent (N))) |
8522 | then | |
8523 | return False; | |
8524 | end if; | |
8525 | ||
d8ec2787 EB |
8526 | -- Here we are in the integer conversion context. We reuse Rounding for |
8527 | -- Machine_Rounding as System.Fat_Gen, which is a permissible behavior. | |
78433fec | 8528 | |
d8ec2787 EB |
8529 | return |
8530 | Id = Attribute_Rounding | |
8531 | or else Id = Attribute_Machine_Rounding | |
8532 | or else Id = Attribute_Truncation; | |
0669bebe GB |
8533 | end Is_Inline_Floating_Point_Attribute; |
8534 | ||
70482933 | 8535 | end Exp_Attr; |