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