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