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