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