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