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