1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
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;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
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;
72 package body Exp_Attr is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 function Build_Array_VS_Func
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:
86 -- * Attr - the 'Valid_Scalars attribute for which the function is
89 -- * Formal_Typ - the type of the generated function's only formal
92 -- * Array_Typ - the array type whose components are to be validated
94 -- * Comp_Typ - the component type of the array
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
99 function Build_Record_VS_Func
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:
107 -- * Attr - the 'Valid_Scalars attribute for which the function is
110 -- * Formal_Typ - the type of the generated function's only formal
113 -- * Rec_Typ - the record type whose internals are to be validated
115 procedure Compile_Stream_Body_In_Scope
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.
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.
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.
142 procedure Expand_Access_To_Protected_Op
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.
150 procedure Expand_Fpt_Attribute
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.
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.
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.
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.
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.
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.
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.
191 procedure Expand_Update_Attribute (N : Node_Id);
192 -- Handle the expansion of attribute Update
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.
198 procedure Find_Fat_Info
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.
207 function Find_Stream_Subprogram
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.
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.
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.
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.
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.
236 -------------------------
237 -- Build_Array_VS_Func --
238 -------------------------
240 function Build_Array_VS_Func
242 Formal_Typ : Entity_Id;
243 Array_Typ : Entity_Id;
244 Comp_Typ : Entity_Id) return Entity_Id
246 Loc : constant Source_Ptr := Sloc (Attr);
248 function Validate_Component
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.
255 function Validate_Dimension
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.
264 ------------------------
265 -- Validate_Component --
266 ------------------------
268 function Validate_Component
270 Indexes : List_Id) return Node_Id
275 if Is_Scalar_Type (Comp_Typ) then
276 Attr_Nam := Name_Valid;
278 Attr_Nam := Name_Valid_Scalars;
282 -- if not Array_Typ (Obj_Id) (Indexes)'Valid[_Scalars] then
287 Make_If_Statement (Loc,
291 Make_Attribute_Reference (Loc,
293 Make_Indexed_Component (Loc,
295 Unchecked_Convert_To (Array_Typ,
296 New_Occurrence_Of (Obj_Id, Loc)),
297 Expressions => Indexes),
298 Attribute_Name => Attr_Nam)),
300 Then_Statements => New_List (
301 Make_Simple_Return_Statement (Loc,
302 Expression => New_Occurrence_Of (Standard_False, Loc))));
303 end Validate_Component;
305 ------------------------
306 -- Validate_Dimension --
307 ------------------------
309 function Validate_Dimension
312 Indexes : List_Id) return Node_Id
317 -- Validate the component once all dimensions have produced their
320 if Dim > Number_Dimensions (Array_Typ) then
321 return Validate_Component (Obj_Id, Indexes);
323 -- Process the current dimension
327 Make_Defining_Identifier (Loc, New_External_Name ('J', Dim));
329 Append_To (Indexes, New_Occurrence_Of (Index, Loc));
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]
342 Make_Implicit_Loop_Statement (Attr,
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,
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)));
360 end Validate_Dimension;
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');
369 -- Start of processing for Build_Array_VS_Func
372 Stmts := New_List (Validate_Dimension (Obj_Id, 1, Indexes));
378 Make_Simple_Return_Statement (Loc,
379 Expression => New_Occurrence_Of (Standard_True, Loc)));
382 -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is
387 Set_Ekind (Func_Id, E_Function);
388 Set_Is_Internal (Func_Id);
389 Set_Is_Pure (Func_Id);
391 if not Debug_Generated_Code then
392 Set_Debug_Info_Off (Func_Id);
396 Make_Subprogram_Body (Loc,
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,
404 Out_Present => False,
405 Parameter_Type => New_Occurrence_Of (Formal_Typ, Loc))),
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)));
414 end Build_Array_VS_Func;
416 ---------------------------------
417 -- Build_Disp_Get_Task_Id_Call --
418 ---------------------------------
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);
427 -- _Disp_Get_Task_Id (Actual)
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;
435 --------------------------
436 -- Build_Record_VS_Func --
437 --------------------------
439 function Build_Record_VS_Func
441 Formal_Typ : Entity_Id;
442 Rec_Typ : Entity_Id) return Entity_Id
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.
448 -- NOTE: The routines within Build_Record_VS_Func are intentionally
449 -- unnested to avoid deep indentation of code.
451 Loc : constant Source_Ptr := Sloc (Attr);
453 procedure Validate_Component_List
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.
461 procedure Validate_Field
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
470 procedure Validate_Fields
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.
478 procedure Validate_Variant
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).
486 procedure Validate_Variant_Part
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.
493 -----------------------------
494 -- Validate_Component_List --
495 -----------------------------
497 procedure Validate_Component_List
500 Stmts : in out List_Id)
502 Var_Part : constant Node_Id := Variant_Part (Comp_List);
505 -- Validate all components
509 Fields => Component_Items (Comp_List),
512 -- Validate the variant part
514 if Present (Var_Part) then
515 Validate_Variant_Part
517 Var_Part => Var_Part,
520 end Validate_Component_List;
526 procedure Validate_Field
529 Cond : in out Node_Id)
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));
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.
541 if Nam_In (Field_Nam, Name_uObject,
547 -- Do not process fields without any scalar components
549 elsif not Scalar_Part_Present (Field_Typ) then
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.
557 -- [or else] not Rec_Typ (Obj_Id).Item_Nam'Valid[_Scalars]
560 if Is_Scalar_Type (Field_Typ) then
561 Attr_Nam := Name_Valid;
563 Attr_Nam := Name_Valid_Scalars;
566 Evolve_Or_Else (Cond,
569 Make_Attribute_Reference (Loc,
571 Make_Selected_Component (Loc,
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)));
580 ---------------------
581 -- Validate_Fields --
582 ---------------------
584 procedure Validate_Fields
587 Stmts : in out List_Id)
593 -- Assume that none of the fields are eligible for verification
597 -- Validate all fields
599 Field := First_Non_Pragma (Fields);
600 while Present (Field) loop
606 Next_Non_Pragma (Field);
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]
616 if Present (Cond) then
617 Append_New_To (Stmts,
618 Make_Implicit_If_Statement (Attr,
620 Then_Statements => New_List (
621 Make_Simple_Return_Statement (Loc,
622 Expression => New_Occurrence_Of (Standard_False, Loc)))));
626 ----------------------
627 -- Validate_Variant --
628 ----------------------
630 procedure Validate_Variant
633 Alts : in out List_Id)
638 -- Assume that none of the components and variants are eligible for
643 -- Validate components
645 Validate_Component_List
647 Comp_List => Component_List (Var),
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
656 Append_New_To (Stmts, Make_Null_Statement (Loc));
660 -- when Discrete_Choices =>
664 Make_Case_Statement_Alternative (Loc,
666 New_Copy_List_Tree (Discrete_Choices (Var)),
667 Statements => Stmts));
668 end Validate_Variant;
670 ---------------------------
671 -- Validate_Variant_Part --
672 ---------------------------
674 procedure Validate_Variant_Part
677 Stmts : in out List_Id)
679 Vars : constant List_Id := Variants (Var_Part);
684 -- Assume that none of the variants are eligible for verification
690 Var := First_Non_Pragma (Vars);
691 while Present (Var) loop
697 Next_Non_Pragma (Var);
700 -- Even though individual variants may lack eligible components, the
701 -- alternatives must still be generated.
703 pragma Assert (Present (Alts));
706 -- case Rec_Typ (Obj_Id).Discriminant is
707 -- when Discrete_Choices_1 =>
709 -- when Discrete_Choices_N =>
713 Append_New_To (Stmts,
714 Make_Case_Statement (Loc,
716 Make_Selected_Component (Loc,
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;
726 Func_Id : constant Entity_Id := Make_Temporary (Loc, 'V');
727 Obj_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
735 -- Start of processing for Build_Record_VS_Func
740 -- Use the root type when dealing with a class-wide type
742 if Is_Class_Wide_Type (Typ) then
743 Typ := Root_Type (Typ);
746 Typ_Decl := Declaration_Node (Typ);
747 Typ_Def := Type_Definition (Typ_Decl);
749 -- The components of a derived type are located in the extension part
751 if Nkind (Typ_Def) = N_Derived_Type_Definition then
752 Typ_Ext := Record_Extension_Part (Typ_Def);
754 if Present (Typ_Ext) then
755 Comps := Component_List (Typ_Ext);
760 -- Otherwise the components are available in the definition
763 Comps := Component_List (Typ_Def);
766 -- The code generated by this routine is as follows:
768 -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is
770 -- if not Rec_Typ (Obj_Id).Discriminant_1'Valid[_Scalars]
771 -- or else not Rec_Typ (Obj_Id).Discriminant_N'Valid[_Scalars]
776 -- if not Rec_Typ (Obj_Id).Component_1'Valid[_Scalars]
777 -- or else not Rec_Typ (Obj_Id).Component_N'Valid[_Scalars]
782 -- case Discriminant_1 is
784 -- if not Rec_Typ (Obj_Id).Component_1'Valid[_Scalars]
785 -- or else not Rec_Typ (Obj_Id).Component_N'Valid[_Scalars]
790 -- case Discriminant_N is
799 -- Assume that the record type lacks eligible components, discriminants,
800 -- and variant parts.
804 -- Validate the discriminants
806 if not Is_Unchecked_Union (Rec_Typ) then
809 Fields => Discriminant_Specifications (Typ_Decl),
813 -- Validate the components and variant parts
815 Validate_Component_List
823 Append_New_To (Stmts,
824 Make_Simple_Return_Statement (Loc,
825 Expression => New_Occurrence_Of (Standard_True, Loc)));
828 -- function Func_Id (Obj_Id : Formal_Typ) return Boolean is
833 Set_Ekind (Func_Id, E_Function);
834 Set_Is_Internal (Func_Id);
835 Set_Is_Pure (Func_Id);
837 if not Debug_Generated_Code then
838 Set_Debug_Info_Off (Func_Id);
842 Make_Subprogram_Body (Loc,
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))),
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);
859 end Build_Record_VS_Func;
861 ----------------------------------
862 -- Compile_Stream_Body_In_Scope --
863 ----------------------------------
865 procedure Compile_Stream_Body_In_Scope
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);
878 and then not In_Open_Scopes (Scop)
879 and then Ekind (Scop) = E_Package
884 -- The component type may be private, in which case we install its
885 -- full view to compile the subprogram.
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).
893 Scop := Scope (C_Type);
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))
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.
908 if Install and then not In_Instance_Body then
910 Install_Visible_Declarations (Scop);
911 Install_Private_Declarations (Scop);
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
924 Insert_Action (N, Decl);
926 Insert_Action (N, Decl, Suppress => All_Checks);
931 -- Remove extra copy of current scope, and package itself
934 End_Package_Scope (Scop);
936 end Compile_Stream_Body_In_Scope;
938 -----------------------------------
939 -- Expand_Access_To_Protected_Op --
940 -----------------------------------
942 procedure Expand_Access_To_Protected_Op
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.
951 Loc : constant Source_Ptr := Sloc (N);
953 Btyp : constant Entity_Id := Base_Type (Typ);
956 E_T : constant Entity_Id := Equivalent_Type (Btyp);
957 Acc : constant Entity_Id :=
958 Etype (Next_Component (First_Component (E_T)));
962 -- Start of processing for Expand_Access_To_Protected_Op
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.
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).
977 Sub := New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
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
983 Curr := Current_Scope;
984 if not Is_Init_Proc (Curr) then
985 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
987 while Scope (Curr) /= Scope (Entity (Pref)) loop
988 Curr := Scope (Curr);
992 -- In case of protected entries the first formal of its Protected_
993 -- Body_Subprogram is the address of the object.
995 if Ekind (Curr) = E_Entry then
999 (Protected_Body_Subprogram (Curr)), Loc);
1001 -- If the current scope is an init proc, then use the address of the
1002 -- _init formal as the object reference.
1004 elsif Is_Init_Proc (Curr) then
1006 Make_Attribute_Reference (Loc,
1007 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
1008 Attribute_Name => Name_Address);
1010 -- In case of protected subprograms the first formal of its
1011 -- Protected_Body_Subprogram is the object and we get its address.
1015 Make_Attribute_Reference (Loc,
1019 (Protected_Body_Subprogram (Curr)), Loc),
1020 Attribute_Name => Name_Address);
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.
1030 (External_Subprogram
1031 (Entity (Selector_Name (Pref))), Loc);
1034 Make_Attribute_Reference (Loc,
1035 Prefix => Relocate_Node (Prefix (Pref)),
1036 Attribute_Name => Name_Address);
1040 Make_Attribute_Reference (Loc,
1042 Attribute_Name => Name_Access);
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.
1048 Set_Etype (Sub_Ref, Acc);
1049 Set_Analyzed (Sub_Ref);
1052 Make_Aggregate (Loc,
1053 Expressions => New_List (Obj_Ref, Sub_Ref));
1055 -- Sub_Ref has been marked as analyzed, but we still need to make sure
1056 -- Sub is correctly frozen.
1058 Freeze_Before (N, Entity (Sub));
1061 Analyze_And_Resolve (N, E_T);
1063 -- For subsequent analysis, the node must retain its type. The backend
1064 -- will replace it with the equivalent type where needed.
1067 end Expand_Access_To_Protected_Op;
1069 --------------------------
1070 -- Expand_Fpt_Attribute --
1071 --------------------------
1073 procedure Expand_Fpt_Attribute
1079 Loc : constant Source_Ptr := Sloc (N);
1080 Typ : constant Entity_Id := Etype (N);
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.
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.
1094 Make_Selected_Component (Loc,
1095 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
1096 Selector_Name => Make_Identifier (Loc, Nam));
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
1104 Unchecked_Convert_To (Base_Type (Etype (N)),
1105 Make_Function_Call (Loc,
1107 Parameter_Associations => Args)));
1109 Analyze_And_Resolve (N, Typ);
1110 end Expand_Fpt_Attribute;
1112 ----------------------------
1113 -- Expand_Fpt_Attribute_R --
1114 ----------------------------
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
1120 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
1121 E1 : constant Node_Id := First (Expressions (N));
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;
1131 -----------------------------
1132 -- Expand_Fpt_Attribute_RI --
1133 -----------------------------
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
1140 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
1141 E1 : constant Node_Id := First (Expressions (N));
1144 E2 : constant Node_Id := Next (E1);
1146 Find_Fat_Info (Etype (E1), Ftp, Pkg);
1147 Expand_Fpt_Attribute
1148 (N, Pkg, Attribute_Name (N),
1150 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
1151 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
1152 end Expand_Fpt_Attribute_RI;
1154 -----------------------------
1155 -- Expand_Fpt_Attribute_RR --
1156 -----------------------------
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
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);
1169 Find_Fat_Info (Etype (E1), Ftp, Pkg);
1170 Expand_Fpt_Attribute
1171 (N, Pkg, Attribute_Name (N),
1173 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
1174 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
1175 end Expand_Fpt_Attribute_RR;
1177 ---------------------------------
1178 -- Expand_Loop_Entry_Attribute --
1179 ---------------------------------
1181 procedure Expand_Loop_Entry_Attribute (N : Node_Id) is
1182 procedure Build_Conditional_Block
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.
1192 function Is_Array_Iteration (N : Node_Id) return Boolean;
1193 -- Determine whether loop statement N denotes an Ada 2012 iteration over
1196 -----------------------------
1197 -- Build_Conditional_Block --
1198 -----------------------------
1200 procedure Build_Conditional_Block
1203 Loop_Stmt : Node_Id;
1204 If_Stmt : out Node_Id;
1205 Blk_Stmt : out Node_Id)
1208 -- Do not reanalyze the original loop statement because it is simply
1211 Set_Analyzed (Loop_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)));
1220 if Present (Cond) then
1222 Make_If_Statement (Loc,
1224 Then_Statements => New_List (Blk_Stmt));
1228 end Build_Conditional_Block;
1230 ------------------------
1231 -- Is_Array_Iteration --
1232 ------------------------
1234 function Is_Array_Iteration (N : Node_Id) return Boolean is
1235 Stmt : constant Node_Id := Original_Node (N);
1239 if Nkind (Stmt) = N_Loop_Statement
1240 and then Present (Iteration_Scheme (Stmt))
1241 and then Present (Iterator_Specification (Iteration_Scheme (Stmt)))
1243 Iter := Iterator_Specification (Iteration_Scheme (Stmt));
1246 Of_Present (Iter) and then Is_Array_Type (Etype (Name (Iter)));
1250 end Is_Array_Iteration;
1254 Pref : constant Node_Id := Prefix (N);
1255 Base_Typ : constant Entity_Id := Base_Type (Etype (Pref));
1256 Exprs : constant List_Id := Expressions (N);
1258 Blk : Node_Id := Empty;
1260 Installed : Boolean;
1262 Loop_Id : Entity_Id;
1263 Loop_Stmt : Node_Id;
1264 Result : Node_Id := Empty;
1266 Temp_Decl : Node_Id;
1267 Temp_Id : Entity_Id;
1269 -- Start of processing for Expand_Loop_Entry_Attribute
1272 -- Step 1: Find the related loop
1274 -- The loop label variant of attribute 'Loop_Entry already has all the
1275 -- information in its expression.
1277 if Present (Exprs) then
1278 Loop_Id := Entity (First (Exprs));
1279 Loop_Stmt := Label_Construct (Parent (Loop_Id));
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.
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))
1296 Loop_Stmt := Parent (Loop_Stmt);
1299 Loop_Id := Entity (Identifier (Loop_Stmt));
1302 Loc := Sloc (Loop_Stmt);
1304 -- Step 2: Transform the loop
1306 -- The loop has already been transformed during the expansion of a prior
1307 -- 'Loop_Entry attribute. Retrieve the declarative list of the block.
1309 if Has_Loop_Entry_Attributes (Loop_Id) then
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.
1315 if Nkind (Loop_Stmt) = N_Block_Statement then
1316 Decls := Declarations (Loop_Stmt);
1318 -- In all other cases, the loop must appear in the handled sequence
1319 -- of statements of the generated block.
1323 (Nkind (Parent (Loop_Stmt)) = N_Handled_Sequence_Of_Statements
1325 Nkind (Parent (Parent (Loop_Stmt))) = N_Block_Statement);
1327 Decls := Declarations (Parent (Parent (Loop_Stmt)));
1330 -- Transform the loop into a conditional block
1333 Set_Has_Loop_Entry_Attributes (Loop_Id);
1334 Scheme := Iteration_Scheme (Loop_Stmt);
1336 -- Infinite loops are transformed into:
1339 -- Temp1 : constant <type of Pref1> := <Pref1>;
1341 -- TempN : constant <type of PrefN> := <PrefN>;
1344 -- <original source statements with attribute rewrites>
1349 Build_Conditional_Block (Loc,
1351 Loop_Stmt => Relocate_Node (Loop_Stmt),
1357 -- While loops are transformed into:
1359 -- function Fnn return Boolean is
1361 -- <condition actions>
1362 -- return <condition>;
1367 -- Temp1 : constant <type of Pref1> := <Pref1>;
1369 -- TempN : constant <type of PrefN> := <PrefN>;
1372 -- <original source statements with attribute rewrites>
1373 -- exit when not Fnn;
1378 -- Note that loops over iterators and containers are already
1379 -- converted into while loops.
1381 elsif Present (Condition (Scheme)) then
1383 Func_Decl : Node_Id;
1384 Func_Id : Entity_Id;
1388 Func_Id := Make_Temporary (Loc, 'F');
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.
1399 -- <condition actions>
1400 -- return <condition>;
1402 if Present (Condition_Actions (Scheme)) then
1403 Stmts := Condition_Actions (Scheme);
1409 Make_Simple_Return_Statement (Loc,
1411 New_Copy_Tree (Condition (Scheme),
1412 New_Scope => Func_Id)));
1415 -- function Fnn return Boolean is
1421 Make_Subprogram_Body (Loc,
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));
1432 -- The function is inserted before the related loop. Make sure
1433 -- to analyze it in the context of the loop's enclosing scope.
1435 Push_Scope (Scope (Loop_Id));
1436 Insert_Action (Loop_Stmt, Func_Decl);
1439 -- The analysis of the condition may have generated itypes
1440 -- that are now used within the function: Adjust their
1441 -- scopes accordingly so that their use appears in their
1442 -- scope of definition.
1448 Ityp := First_Entity (Loop_Id);
1450 while Present (Ityp) loop
1451 if Is_Itype (Ityp) then
1452 Set_Scope (Ityp, Func_Id);
1458 -- Transform the original while loop into an infinite loop
1459 -- where the last statement checks the negated condition. This
1460 -- placement ensures that the condition will not be evaluated
1461 -- twice on the first iteration.
1463 Set_Iteration_Scheme (Loop_Stmt, Empty);
1467 -- exit when not Fnn;
1469 Append_To (Statements (Loop_Stmt),
1470 Make_Exit_Statement (Loc,
1474 Make_Function_Call (Loc,
1475 Name => New_Occurrence_Of (Func_Id, Loc)))));
1477 Build_Conditional_Block (Loc,
1479 Make_Function_Call (Loc,
1480 Name => New_Occurrence_Of (Func_Id, Loc)),
1481 Loop_Stmt => Relocate_Node (Loop_Stmt),
1486 -- Ada 2012 iteration over an array is transformed into:
1488 -- if <Array_Nam>'Length (1) > 0
1489 -- and then <Array_Nam>'Length (N) > 0
1492 -- Temp1 : constant <type of Pref1> := <Pref1>;
1494 -- TempN : constant <type of PrefN> := <PrefN>;
1496 -- for X in ... loop -- multiple loops depending on dims
1497 -- <original source statements with attribute rewrites>
1502 elsif Is_Array_Iteration (Loop_Stmt) then
1504 Array_Nam : constant Entity_Id :=
1505 Entity (Name (Iterator_Specification
1506 (Iteration_Scheme (Original_Node (Loop_Stmt)))));
1507 Num_Dims : constant Pos :=
1508 Number_Dimensions (Etype (Array_Nam));
1509 Cond : Node_Id := Empty;
1513 -- Generate a check which determines whether all dimensions of
1514 -- the array are non-null.
1516 for Dim in 1 .. Num_Dims loop
1520 Make_Attribute_Reference (Loc,
1521 Prefix => New_Occurrence_Of (Array_Nam, Loc),
1522 Attribute_Name => Name_Length,
1523 Expressions => New_List (
1524 Make_Integer_Literal (Loc, Dim))),
1526 Make_Integer_Literal (Loc, 0));
1534 Right_Opnd => Check);
1538 Build_Conditional_Block (Loc,
1540 Loop_Stmt => Relocate_Node (Loop_Stmt),
1545 -- For loops are transformed into:
1547 -- if <Low> <= <High> then
1549 -- Temp1 : constant <type of Pref1> := <Pref1>;
1551 -- TempN : constant <type of PrefN> := <PrefN>;
1553 -- for <Def_Id> in <Low> .. <High> loop
1554 -- <original source statements with attribute rewrites>
1559 elsif Present (Loop_Parameter_Specification (Scheme)) then
1561 Loop_Spec : constant Node_Id :=
1562 Loop_Parameter_Specification (Scheme);
1567 Subt_Def := Discrete_Subtype_Definition (Loop_Spec);
1569 -- When the loop iterates over a subtype indication with a
1570 -- range, use the low and high bounds of the subtype itself.
1572 if Nkind (Subt_Def) = N_Subtype_Indication then
1573 Subt_Def := Scalar_Range (Etype (Subt_Def));
1576 pragma Assert (Nkind (Subt_Def) = N_Range);
1583 Left_Opnd => New_Copy_Tree (Low_Bound (Subt_Def)),
1584 Right_Opnd => New_Copy_Tree (High_Bound (Subt_Def)));
1586 Build_Conditional_Block (Loc,
1588 Loop_Stmt => Relocate_Node (Loop_Stmt),
1594 Decls := Declarations (Blk);
1597 -- Step 3: Create a constant to capture the value of the prefix at the
1598 -- entry point into the loop.
1600 Temp_Id := Make_Temporary (Loc, 'P');
1602 -- Preserve the tag of the prefix by offering a specific view of the
1603 -- class-wide version of the prefix.
1605 if Is_Tagged_Type (Base_Typ) then
1606 Tagged_Case : declare
1607 CW_Temp : Entity_Id;
1612 -- CW_Temp : constant Base_Typ'Class := Base_Typ'Class (Pref);
1614 CW_Temp := Make_Temporary (Loc, 'T');
1615 CW_Typ := Class_Wide_Type (Base_Typ);
1618 Make_Object_Declaration (Loc,
1619 Defining_Identifier => CW_Temp,
1620 Constant_Present => True,
1621 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
1623 Convert_To (CW_Typ, Relocate_Node (Pref)));
1624 Append_To (Decls, Aux_Decl);
1627 -- Temp : Base_Typ renames Base_Typ (CW_Temp);
1630 Make_Object_Renaming_Declaration (Loc,
1631 Defining_Identifier => Temp_Id,
1632 Subtype_Mark => New_Occurrence_Of (Base_Typ, Loc),
1634 Convert_To (Base_Typ, New_Occurrence_Of (CW_Temp, Loc)));
1635 Append_To (Decls, Temp_Decl);
1641 Untagged_Case : declare
1642 Temp_Expr : Node_Id;
1647 -- Generate a nominal type for the constant when the prefix is of
1648 -- a constrained type. This is achieved by setting the Etype of
1649 -- the relocated prefix to its base type. Since the prefix is now
1650 -- the initialization expression of the constant, its freezing
1651 -- will produce a proper nominal type.
1653 Temp_Expr := Relocate_Node (Pref);
1654 Set_Etype (Temp_Expr, Base_Typ);
1657 -- Temp : constant Base_Typ := Pref;
1660 Make_Object_Declaration (Loc,
1661 Defining_Identifier => Temp_Id,
1662 Constant_Present => True,
1663 Object_Definition => New_Occurrence_Of (Base_Typ, Loc),
1664 Expression => Temp_Expr);
1665 Append_To (Decls, Temp_Decl);
1669 -- Step 4: Analyze all bits
1671 Installed := Current_Scope = Scope (Loop_Id);
1673 -- Depending on the pracement of attribute 'Loop_Entry relative to the
1674 -- associated loop, ensure the proper visibility for analysis.
1676 if not Installed then
1677 Push_Scope (Scope (Loop_Id));
1680 -- The analysis of the conditional block takes care of the constant
1683 if Present (Result) then
1684 Rewrite (Loop_Stmt, Result);
1685 Analyze (Loop_Stmt);
1687 -- The conditional block was analyzed when a previous 'Loop_Entry was
1688 -- expanded. There is no point in reanalyzing the block, simply analyze
1689 -- the declaration of the constant.
1692 if Present (Aux_Decl) then
1696 Analyze (Temp_Decl);
1699 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
1702 if not Installed then
1705 end Expand_Loop_Entry_Attribute;
1707 ------------------------------
1708 -- Expand_Min_Max_Attribute --
1709 ------------------------------
1711 procedure Expand_Min_Max_Attribute (N : Node_Id) is
1713 -- Min and Max are handled by the back end (except that static cases
1714 -- have already been evaluated during semantic processing, although the
1715 -- back end should not count on this). The one bit of special processing
1716 -- required in the normal case is that these two attributes typically
1717 -- generate conditionals in the code, so check the relevant restriction.
1719 Check_Restriction (No_Implicit_Conditionals, N);
1720 end Expand_Min_Max_Attribute;
1722 ----------------------------------
1723 -- Expand_N_Attribute_Reference --
1724 ----------------------------------
1726 procedure Expand_N_Attribute_Reference (N : Node_Id) is
1727 Loc : constant Source_Ptr := Sloc (N);
1728 Typ : constant Entity_Id := Etype (N);
1729 Btyp : constant Entity_Id := Base_Type (Typ);
1730 Pref : constant Node_Id := Prefix (N);
1731 Ptyp : constant Entity_Id := Etype (Pref);
1732 Exprs : constant List_Id := Expressions (N);
1733 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
1735 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
1736 -- Rewrites a stream attribute for Read, Write or Output with the
1737 -- procedure call. Pname is the entity for the procedure to call.
1739 ------------------------------
1740 -- Rewrite_Stream_Proc_Call --
1741 ------------------------------
1743 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
1744 Item : constant Node_Id := Next (First (Exprs));
1745 Item_Typ : constant Entity_Id := Etype (Item);
1746 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
1747 Formal_Typ : constant Entity_Id := Etype (Formal);
1748 Is_Written : constant Boolean := Ekind (Formal) /= E_In_Parameter;
1751 -- The expansion depends on Item, the second actual, which is
1752 -- the object being streamed in or out.
1754 -- If the item is a component of a packed array type, and
1755 -- a conversion is needed on exit, we introduce a temporary to
1756 -- hold the value, because otherwise the packed reference will
1757 -- not be properly expanded.
1759 if Nkind (Item) = N_Indexed_Component
1760 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
1761 and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ)
1765 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
1771 Make_Object_Declaration (Loc,
1772 Defining_Identifier => Temp,
1773 Object_Definition => New_Occurrence_Of (Formal_Typ, Loc));
1774 Set_Etype (Temp, Formal_Typ);
1777 Make_Assignment_Statement (Loc,
1778 Name => New_Copy_Tree (Item),
1780 Unchecked_Convert_To
1781 (Item_Typ, New_Occurrence_Of (Temp, Loc)));
1783 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
1787 Make_Procedure_Call_Statement (Loc,
1788 Name => New_Occurrence_Of (Pname, Loc),
1789 Parameter_Associations => Exprs),
1792 Rewrite (N, Make_Null_Statement (Loc));
1797 -- For the class-wide dispatching cases, and for cases in which
1798 -- the base type of the second argument matches the base type of
1799 -- the corresponding formal parameter (that is to say the stream
1800 -- operation is not inherited), we are all set, and can use the
1801 -- argument unchanged.
1803 if not Is_Class_Wide_Type (Entity (Pref))
1804 and then not Is_Class_Wide_Type (Etype (Item))
1805 and then Base_Type (Item_Typ) /= Base_Type (Formal_Typ)
1807 -- Perform a view conversion when either the argument or the
1808 -- formal parameter are of a private type.
1810 if Is_Private_Type (Base_Type (Formal_Typ))
1811 or else Is_Private_Type (Base_Type (Item_Typ))
1814 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
1816 -- Otherwise perform a regular type conversion to ensure that all
1817 -- relevant checks are installed.
1820 Rewrite (Item, Convert_To (Formal_Typ, Relocate_Node (Item)));
1823 -- For untagged derived types set Assignment_OK, to prevent
1824 -- copies from being created when the unchecked conversion
1825 -- is expanded (which would happen in Remove_Side_Effects
1826 -- if Expand_N_Unchecked_Conversion were allowed to call
1827 -- Force_Evaluation). The copy could violate Ada semantics in
1828 -- cases such as an actual that is an out parameter. Note that
1829 -- this approach is also used in exp_ch7 for calls to controlled
1830 -- type operations to prevent problems with actuals wrapped in
1831 -- unchecked conversions.
1833 if Is_Untagged_Derivation (Etype (Expression (Item))) then
1834 Set_Assignment_OK (Item);
1838 -- The stream operation to call may be a renaming created by an
1839 -- attribute definition clause, and may not be frozen yet. Ensure
1840 -- that it has the necessary extra formals.
1842 if not Is_Frozen (Pname) then
1843 Create_Extra_Formals (Pname);
1846 -- And now rewrite the call
1849 Make_Procedure_Call_Statement (Loc,
1850 Name => New_Occurrence_Of (Pname, Loc),
1851 Parameter_Associations => Exprs));
1854 end Rewrite_Stream_Proc_Call;
1856 -- Start of processing for Expand_N_Attribute_Reference
1859 -- Do required validity checking, if enabled. Do not apply check to
1860 -- output parameters of an Asm instruction, since the value of this
1861 -- is not set till after the attribute has been elaborated, and do
1862 -- not apply the check to the arguments of a 'Read or 'Input attribute
1863 -- reference since the scalar argument is an OUT scalar.
1865 if Validity_Checks_On and then Validity_Check_Operands
1866 and then Id /= Attribute_Asm_Output
1867 and then Id /= Attribute_Read
1868 and then Id /= Attribute_Input
1873 Expr := First (Expressions (N));
1874 while Present (Expr) loop
1875 Ensure_Valid (Expr);
1881 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
1882 -- place function, then a temporary return object needs to be created
1883 -- and access to it must be passed to the function.
1885 if Is_Build_In_Place_Function_Call (Pref) then
1887 -- If attribute is 'Old, the context is a postcondition, and
1888 -- the temporary must go in the corresponding subprogram, not
1889 -- the postcondition function or any created blocks, as when
1890 -- the attribute appears in a quantified expression. This is
1891 -- handled below in the expansion of the attribute.
1893 if Attribute_Name (Parent (Pref)) = Name_Old then
1896 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
1899 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
1900 -- containing build-in-place function calls whose returned object covers
1903 elsif Present (Unqual_BIP_Iface_Function_Call (Pref)) then
1904 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref);
1907 -- If prefix is a protected type name, this is a reference to the
1908 -- current instance of the type. For a component definition, nothing
1909 -- to do (expansion will occur in the init proc). In other contexts,
1910 -- rewrite into reference to current instance.
1912 if Is_Protected_Self_Reference (Pref)
1914 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
1915 N_Discriminant_Association)
1916 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
1917 N_Component_Definition)
1919 -- No action needed for these attributes since the current instance
1920 -- will be rewritten to be the name of the _object parameter
1921 -- associated with the enclosing protected subprogram (see below).
1923 and then Id /= Attribute_Access
1924 and then Id /= Attribute_Unchecked_Access
1925 and then Id /= Attribute_Unrestricted_Access
1927 Rewrite (Pref, Concurrent_Ref (Pref));
1931 -- Remaining processing depends on specific attribute
1933 -- Note: individual sections of the following case statement are
1934 -- allowed to assume there is no code after the case statement, and
1935 -- are legitimately allowed to execute return statements if they have
1936 -- nothing more to do.
1940 -- Attributes related to Ada 2012 iterators
1942 when Attribute_Constant_Indexing
1943 | Attribute_Default_Iterator
1944 | Attribute_Implicit_Dereference
1945 | Attribute_Iterable
1946 | Attribute_Iterator_Element
1947 | Attribute_Variable_Indexing
1951 -- Internal attributes used to deal with Ada 2012 delayed aspects. These
1952 -- were already rejected by the parser. Thus they shouldn't appear here.
1954 when Internal_Attribute_Id =>
1955 raise Program_Error;
1961 when Attribute_Access
1962 | Attribute_Unchecked_Access
1963 | Attribute_Unrestricted_Access
1965 Access_Cases : declare
1966 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
1967 Btyp_DDT : Entity_Id;
1969 function Enclosing_Object (N : Node_Id) return Node_Id;
1970 -- If N denotes a compound name (selected component, indexed
1971 -- component, or slice), returns the name of the outermost such
1972 -- enclosing object. Otherwise returns N. If the object is a
1973 -- renaming, then the renamed object is returned.
1975 ----------------------
1976 -- Enclosing_Object --
1977 ----------------------
1979 function Enclosing_Object (N : Node_Id) return Node_Id is
1984 while Nkind_In (Obj_Name, N_Selected_Component,
1985 N_Indexed_Component,
1988 Obj_Name := Prefix (Obj_Name);
1991 return Get_Referenced_Object (Obj_Name);
1992 end Enclosing_Object;
1994 -- Local declarations
1996 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
1998 -- Start of processing for Access_Cases
2001 Btyp_DDT := Designated_Type (Btyp);
2003 -- Handle designated types that come from the limited view
2005 if From_Limited_With (Btyp_DDT)
2006 and then Has_Non_Limited_View (Btyp_DDT)
2008 Btyp_DDT := Non_Limited_View (Btyp_DDT);
2011 -- In order to improve the text of error messages, the designated
2012 -- type of access-to-subprogram itypes is set by the semantics as
2013 -- the associated subprogram entity (see sem_attr). Now we replace
2014 -- such node with the proper E_Subprogram_Type itype.
2016 if Id = Attribute_Unrestricted_Access
2017 and then Is_Subprogram (Directly_Designated_Type (Typ))
2019 -- The following conditions ensure that this special management
2020 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
2021 -- At this stage other cases in which the designated type is
2022 -- still a subprogram (instead of an E_Subprogram_Type) are
2023 -- wrong because the semantics must have overridden the type of
2024 -- the node with the type imposed by the context.
2026 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
2027 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
2029 Set_Etype (N, RTE (RE_Prim_Ptr));
2033 Subp : constant Entity_Id :=
2034 Directly_Designated_Type (Typ);
2036 Extra : Entity_Id := Empty;
2037 New_Formal : Entity_Id;
2038 Old_Formal : Entity_Id := First_Formal (Subp);
2039 Subp_Typ : Entity_Id;
2042 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
2043 Set_Etype (Subp_Typ, Etype (Subp));
2044 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
2046 if Present (Old_Formal) then
2047 New_Formal := New_Copy (Old_Formal);
2048 Set_First_Entity (Subp_Typ, New_Formal);
2051 Set_Scope (New_Formal, Subp_Typ);
2052 Etyp := Etype (New_Formal);
2054 -- Handle itypes. There is no need to duplicate
2055 -- here the itypes associated with record types
2056 -- (i.e the implicit full view of private types).
2059 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
2061 Extra := New_Copy (Etyp);
2062 Set_Parent (Extra, New_Formal);
2063 Set_Etype (New_Formal, Extra);
2064 Set_Scope (Extra, Subp_Typ);
2067 Extra := New_Formal;
2068 Next_Formal (Old_Formal);
2069 exit when No (Old_Formal);
2071 Link_Entities (New_Formal, New_Copy (Old_Formal));
2072 Next_Entity (New_Formal);
2075 Unlink_Next_Entity (New_Formal);
2076 Set_Last_Entity (Subp_Typ, Extra);
2079 -- Now that the explicit formals have been duplicated,
2080 -- any extra formals needed by the subprogram must be
2083 if Present (Extra) then
2084 Set_Extra_Formal (Extra, Empty);
2087 Create_Extra_Formals (Subp_Typ);
2088 Set_Directly_Designated_Type (Typ, Subp_Typ);
2093 if Is_Access_Protected_Subprogram_Type (Btyp) then
2094 Expand_Access_To_Protected_Op (N, Pref, Typ);
2096 -- If prefix is a type name, this is a reference to the current
2097 -- instance of the type, within its initialization procedure.
2099 elsif Is_Entity_Name (Pref)
2100 and then Is_Type (Entity (Pref))
2107 -- If the current instance name denotes a task type, then
2108 -- the access attribute is rewritten to be the name of the
2109 -- "_task" parameter associated with the task type's task
2110 -- procedure. An unchecked conversion is applied to ensure
2111 -- a type match in cases of expander-generated calls (e.g.
2114 if Is_Task_Type (Entity (Pref)) then
2116 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
2117 while Present (Formal) loop
2118 exit when Chars (Formal) = Name_uTask;
2119 Next_Entity (Formal);
2122 pragma Assert (Present (Formal));
2125 Unchecked_Convert_To (Typ,
2126 New_Occurrence_Of (Formal, Loc)));
2129 elsif Is_Protected_Type (Entity (Pref)) then
2131 -- No action needed for current instance located in a
2132 -- component definition (expansion will occur in the
2135 if Is_Protected_Type (Current_Scope) then
2138 -- If the current instance reference is located in a
2139 -- protected subprogram or entry then rewrite the access
2140 -- attribute to be the name of the "_object" parameter.
2141 -- An unchecked conversion is applied to ensure a type
2142 -- match in cases of expander-generated calls (e.g. init
2145 -- The code may be nested in a block, so find enclosing
2146 -- scope that is a protected operation.
2153 Subp := Current_Scope;
2154 while Ekind_In (Subp, E_Loop, E_Block) loop
2155 Subp := Scope (Subp);
2160 (Protected_Body_Subprogram (Subp));
2162 -- For a protected subprogram the _Object parameter
2163 -- is the protected record, so we create an access
2164 -- to it. The _Object parameter of an entry is an
2167 if Ekind (Subp) = E_Entry then
2169 Unchecked_Convert_To (Typ,
2170 New_Occurrence_Of (Formal, Loc)));
2175 Unchecked_Convert_To (Typ,
2176 Make_Attribute_Reference (Loc,
2177 Attribute_Name => Name_Unrestricted_Access,
2179 New_Occurrence_Of (Formal, Loc))));
2180 Analyze_And_Resolve (N);
2185 -- The expression must appear in a default expression,
2186 -- (which in the initialization procedure is the right-hand
2187 -- side of an assignment), and not in a discriminant
2192 while Present (Par) loop
2193 exit when Nkind (Par) = N_Assignment_Statement;
2195 if Nkind (Par) = N_Component_Declaration then
2199 Par := Parent (Par);
2202 if Present (Par) then
2204 Make_Attribute_Reference (Loc,
2205 Prefix => Make_Identifier (Loc, Name_uInit),
2206 Attribute_Name => Attribute_Name (N)));
2208 Analyze_And_Resolve (N, Typ);
2213 -- If the prefix of an Access attribute is a dereference of an
2214 -- access parameter (or a renaming of such a dereference, or a
2215 -- subcomponent of such a dereference) and the context is a
2216 -- general access type (including the type of an object or
2217 -- component with an access_definition, but not the anonymous
2218 -- type of an access parameter or access discriminant), then
2219 -- apply an accessibility check to the access parameter. We used
2220 -- to rewrite the access parameter as a type conversion, but that
2221 -- could only be done if the immediate prefix of the Access
2222 -- attribute was the dereference, and didn't handle cases where
2223 -- the attribute is applied to a subcomponent of the dereference,
2224 -- since there's generally no available, appropriate access type
2225 -- to convert to in that case. The attribute is passed as the
2226 -- point to insert the check, because the access parameter may
2227 -- come from a renaming, possibly in a different scope, and the
2228 -- check must be associated with the attribute itself.
2230 elsif Id = Attribute_Access
2231 and then Nkind (Enc_Object) = N_Explicit_Dereference
2232 and then Is_Entity_Name (Prefix (Enc_Object))
2233 and then (Ekind (Btyp) = E_General_Access_Type
2234 or else Is_Local_Anonymous_Access (Btyp))
2235 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
2236 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
2237 = E_Anonymous_Access_Type
2238 and then Present (Extra_Accessibility
2239 (Entity (Prefix (Enc_Object))))
2241 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
2243 -- Ada 2005 (AI-251): If the designated type is an interface we
2244 -- add an implicit conversion to force the displacement of the
2245 -- pointer to reference the secondary dispatch table.
2247 elsif Is_Interface (Btyp_DDT)
2248 and then (Comes_From_Source (N)
2249 or else Comes_From_Source (Ref_Object)
2250 or else (Nkind (Ref_Object) in N_Has_Chars
2251 and then Chars (Ref_Object) = Name_uInit))
2253 if Nkind (Ref_Object) /= N_Explicit_Dereference then
2255 -- No implicit conversion required if types match, or if
2256 -- the prefix is the class_wide_type of the interface. In
2257 -- either case passing an object of the interface type has
2258 -- already set the pointer correctly.
2260 if Btyp_DDT = Etype (Ref_Object)
2261 or else (Is_Class_Wide_Type (Etype (Ref_Object))
2263 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
2268 Rewrite (Prefix (N),
2269 Convert_To (Btyp_DDT,
2270 New_Copy_Tree (Prefix (N))));
2272 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
2275 -- When the object is an explicit dereference, convert the
2276 -- dereference's prefix.
2280 Obj_DDT : constant Entity_Id :=
2282 (Directly_Designated_Type
2283 (Etype (Prefix (Ref_Object))));
2285 -- No implicit conversion required if designated types
2288 if Obj_DDT /= Btyp_DDT
2289 and then not (Is_Class_Wide_Type (Obj_DDT)
2290 and then Etype (Obj_DDT) = Btyp_DDT)
2294 New_Copy_Tree (Prefix (Ref_Object))));
2295 Analyze_And_Resolve (N, Typ);
2306 -- Transforms 'Adjacent into a call to the floating-point attribute
2307 -- function Adjacent in Fat_xxx (where xxx is the root type)
2309 when Attribute_Adjacent =>
2310 Expand_Fpt_Attribute_RR (N);
2316 when Attribute_Address => Address : declare
2317 Task_Proc : Entity_Id;
2320 -- If the prefix is a task or a task type, the useful address is that
2321 -- of the procedure for the task body, i.e. the actual program unit.
2322 -- We replace the original entity with that of the procedure.
2324 if Is_Entity_Name (Pref)
2325 and then Is_Task_Type (Entity (Pref))
2327 Task_Proc := Next_Entity (Root_Type (Ptyp));
2329 while Present (Task_Proc) loop
2330 exit when Ekind (Task_Proc) = E_Procedure
2331 and then Etype (First_Formal (Task_Proc)) =
2332 Corresponding_Record_Type (Ptyp);
2333 Next_Entity (Task_Proc);
2336 if Present (Task_Proc) then
2337 Set_Entity (Pref, Task_Proc);
2338 Set_Etype (Pref, Etype (Task_Proc));
2341 -- Similarly, the address of a protected operation is the address
2342 -- of the corresponding protected body, regardless of the protected
2343 -- object from which it is selected.
2345 elsif Nkind (Pref) = N_Selected_Component
2346 and then Is_Subprogram (Entity (Selector_Name (Pref)))
2347 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
2351 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
2353 elsif Nkind (Pref) = N_Explicit_Dereference
2354 and then Ekind (Ptyp) = E_Subprogram_Type
2355 and then Convention (Ptyp) = Convention_Protected
2357 -- The prefix is be a dereference of an access_to_protected_
2358 -- subprogram. The desired address is the second component of
2359 -- the record that represents the access.
2362 Addr : constant Entity_Id := Etype (N);
2363 Ptr : constant Node_Id := Prefix (Pref);
2364 T : constant Entity_Id :=
2365 Equivalent_Type (Base_Type (Etype (Ptr)));
2369 Unchecked_Convert_To (Addr,
2370 Make_Selected_Component (Loc,
2371 Prefix => Unchecked_Convert_To (T, Ptr),
2372 Selector_Name => New_Occurrence_Of (
2373 Next_Entity (First_Entity (T)), Loc))));
2375 Analyze_And_Resolve (N, Addr);
2378 -- Ada 2005 (AI-251): Class-wide interface objects are always
2379 -- "displaced" to reference the tag associated with the interface
2380 -- type. In order to obtain the real address of such objects we
2381 -- generate a call to a run-time subprogram that returns the base
2382 -- address of the object.
2384 elsif Is_Class_Wide_Type (Ptyp)
2385 and then Is_Interface (Underlying_Type (Ptyp))
2386 and then Tagged_Type_Expansion
2387 and then not (Nkind (Pref) in N_Has_Entity
2388 and then Is_Subprogram (Entity (Pref)))
2391 Make_Function_Call (Loc,
2392 Name => New_Occurrence_Of (RTE (RE_Base_Address), Loc),
2393 Parameter_Associations => New_List (
2394 Relocate_Node (N))));
2399 -- Deal with packed array reference, other cases are handled by
2402 if Involves_Packed_Array_Reference (Pref) then
2403 Expand_Packed_Address_Reference (N);
2411 when Attribute_Alignment => Alignment : declare
2415 -- For class-wide types, X'Class'Alignment is transformed into a
2416 -- direct reference to the Alignment of the class type, so that the
2417 -- back end does not have to deal with the X'Class'Alignment
2420 if Is_Entity_Name (Pref)
2421 and then Is_Class_Wide_Type (Entity (Pref))
2423 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
2426 -- For x'Alignment applied to an object of a class wide type,
2427 -- transform X'Alignment into a call to the predefined primitive
2428 -- operation _Alignment applied to X.
2430 elsif Is_Class_Wide_Type (Ptyp) then
2432 Make_Attribute_Reference (Loc,
2434 Attribute_Name => Name_Tag);
2436 New_Node := Build_Get_Alignment (Loc, New_Node);
2438 -- Case where the context is a specific integer type with which
2439 -- the original attribute was compatible. The function has a
2440 -- specific type as well, so to preserve the compatibility we
2441 -- must convert explicitly.
2443 if Typ /= Standard_Integer then
2444 New_Node := Convert_To (Typ, New_Node);
2447 Rewrite (N, New_Node);
2448 Analyze_And_Resolve (N, Typ);
2451 -- For all other cases, we just have to deal with the case of
2452 -- the fact that the result can be universal.
2455 Apply_Universal_Integer_Attribute_Checks (N);
2463 -- We compute this if a packed array reference was present, otherwise we
2464 -- leave the computation up to the back end.
2466 when Attribute_Bit =>
2467 if Involves_Packed_Array_Reference (Pref) then
2468 Expand_Packed_Bit_Reference (N);
2470 Apply_Universal_Integer_Attribute_Checks (N);
2477 -- We compute this if a component clause was present, otherwise we leave
2478 -- the computation up to the back end, since we don't know what layout
2481 -- Note that the attribute can apply to a naked record component
2482 -- in generated code (i.e. the prefix is an identifier that
2483 -- references the component or discriminant entity).
2485 when Attribute_Bit_Position => Bit_Position : declare
2489 if Nkind (Pref) = N_Identifier then
2490 CE := Entity (Pref);
2492 CE := Entity (Selector_Name (Pref));
2495 if Known_Static_Component_Bit_Offset (CE) then
2497 Make_Integer_Literal (Loc,
2498 Intval => Component_Bit_Offset (CE)));
2499 Analyze_And_Resolve (N, Typ);
2502 Apply_Universal_Integer_Attribute_Checks (N);
2510 -- A reference to P'Body_Version or P'Version is expanded to
2513 -- pragma Import (C, Vnn, "uuuuT");
2515 -- Get_Version_String (Vnn)
2517 -- where uuuu is the unit name (dots replaced by double underscore)
2518 -- and T is B for the cases of Body_Version, or Version applied to a
2519 -- subprogram acting as its own spec, and S for Version applied to a
2520 -- subprogram spec or package. This sequence of code references the
2521 -- unsigned constant created in the main program by the binder.
2523 -- A special exception occurs for Standard, where the string returned
2524 -- is a copy of the library string in gnatvsn.ads.
2526 when Attribute_Body_Version
2530 E : constant Entity_Id := Make_Temporary (Loc, 'V');
2535 -- If not library unit, get to containing library unit
2537 Pent := Entity (Pref);
2538 while Pent /= Standard_Standard
2539 and then Scope (Pent) /= Standard_Standard
2540 and then not Is_Child_Unit (Pent)
2542 Pent := Scope (Pent);
2545 -- Special case Standard and Standard.ASCII
2547 if Pent = Standard_Standard or else Pent = Standard_ASCII then
2549 Make_String_Literal (Loc,
2550 Strval => Verbose_Library_Version));
2555 -- Build required string constant
2557 Get_Name_String (Get_Unit_Name (Pent));
2560 for J in 1 .. Name_Len - 2 loop
2561 if Name_Buffer (J) = '.' then
2562 Store_String_Chars ("__");
2564 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
2568 -- Case of subprogram acting as its own spec, always use body
2570 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
2571 and then Nkind (Parent (Declaration_Node (Pent))) =
2573 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
2575 Store_String_Chars ("B");
2577 -- Case of no body present, always use spec
2579 elsif not Unit_Requires_Body (Pent) then
2580 Store_String_Chars ("S");
2582 -- Otherwise use B for Body_Version, S for spec
2584 elsif Id = Attribute_Body_Version then
2585 Store_String_Chars ("B");
2587 Store_String_Chars ("S");
2591 Lib.Version_Referenced (S);
2593 -- Insert the object declaration
2595 Insert_Actions (N, New_List (
2596 Make_Object_Declaration (Loc,
2597 Defining_Identifier => E,
2598 Object_Definition =>
2599 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
2601 -- Set entity as imported with correct external name
2603 Set_Is_Imported (E);
2604 Set_Interface_Name (E, Make_String_Literal (Loc, S));
2606 -- Set entity as internal to ensure proper Sprint output of its
2607 -- implicit importation.
2609 Set_Is_Internal (E);
2611 -- And now rewrite original reference
2614 Make_Function_Call (Loc,
2616 New_Occurrence_Of (RTE (RE_Get_Version_String), Loc),
2617 Parameter_Associations => New_List (
2618 New_Occurrence_Of (E, Loc))));
2621 Analyze_And_Resolve (N, RTE (RE_Version_String));
2628 -- Transforms 'Ceiling into a call to the floating-point attribute
2629 -- function Ceiling in Fat_xxx (where xxx is the root type)
2631 when Attribute_Ceiling =>
2632 Expand_Fpt_Attribute_R (N);
2638 -- Transforms 'Callable attribute into a call to the Callable function
2640 when Attribute_Callable =>
2642 -- We have an object of a task interface class-wide type as a prefix
2643 -- to Callable. Generate:
2644 -- callable (Task_Id (Pref._disp_get_task_id));
2646 if Ada_Version >= Ada_2005
2647 and then Ekind (Ptyp) = E_Class_Wide_Type
2648 and then Is_Interface (Ptyp)
2649 and then Is_Task_Interface (Ptyp)
2652 Make_Function_Call (Loc,
2654 New_Occurrence_Of (RTE (RE_Callable), Loc),
2655 Parameter_Associations => New_List (
2656 Make_Unchecked_Type_Conversion (Loc,
2658 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
2659 Expression => Build_Disp_Get_Task_Id_Call (Pref)))));
2662 Rewrite (N, Build_Call_With_Task (Pref, RTE (RE_Callable)));
2665 Analyze_And_Resolve (N, Standard_Boolean);
2671 -- Transforms 'Caller attribute into a call to either the
2672 -- Task_Entry_Caller or the Protected_Entry_Caller function.
2674 when Attribute_Caller => Caller : declare
2675 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
2676 Ent : constant Entity_Id := Entity (Pref);
2677 Conctype : constant Entity_Id := Scope (Ent);
2678 Nest_Depth : Integer := 0;
2685 if Is_Protected_Type (Conctype) then
2686 case Corresponding_Runtime_Package (Conctype) is
2687 when System_Tasking_Protected_Objects_Entries =>
2690 (RTE (RE_Protected_Entry_Caller), Loc);
2692 when System_Tasking_Protected_Objects_Single_Entry =>
2695 (RTE (RE_Protected_Single_Entry_Caller), Loc);
2698 raise Program_Error;
2702 Unchecked_Convert_To (Id_Kind,
2703 Make_Function_Call (Loc,
2705 Parameter_Associations => New_List (
2707 (Find_Protection_Object (Current_Scope), Loc)))));
2712 -- Determine the nesting depth of the E'Caller attribute, that
2713 -- is, how many accept statements are nested within the accept
2714 -- statement for E at the point of E'Caller. The runtime uses
2715 -- this depth to find the specified entry call.
2717 for J in reverse 0 .. Scope_Stack.Last loop
2718 S := Scope_Stack.Table (J).Entity;
2720 -- We should not reach the scope of the entry, as it should
2721 -- already have been checked in Sem_Attr that this attribute
2722 -- reference is within a matching accept statement.
2724 pragma Assert (S /= Conctype);
2729 elsif Is_Entry (S) then
2730 Nest_Depth := Nest_Depth + 1;
2735 Unchecked_Convert_To (Id_Kind,
2736 Make_Function_Call (Loc,
2738 New_Occurrence_Of (RTE (RE_Task_Entry_Caller), Loc),
2739 Parameter_Associations => New_List (
2740 Make_Integer_Literal (Loc,
2741 Intval => Int (Nest_Depth))))));
2744 Analyze_And_Resolve (N, Id_Kind);
2751 -- Transforms 'Compose into a call to the floating-point attribute
2752 -- function Compose in Fat_xxx (where xxx is the root type)
2754 -- Note: we strictly should have special code here to deal with the
2755 -- case of absurdly negative arguments (less than Integer'First)
2756 -- which will return a (signed) zero value, but it hardly seems
2757 -- worth the effort. Absurdly large positive arguments will raise
2758 -- constraint error which is fine.
2760 when Attribute_Compose =>
2761 Expand_Fpt_Attribute_RI (N);
2767 when Attribute_Constrained => Constrained : declare
2768 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
2770 -- Start of processing for Constrained
2773 -- Reference to a parameter where the value is passed as an extra
2774 -- actual, corresponding to the extra formal referenced by the
2775 -- Extra_Constrained field of the corresponding formal. If this
2776 -- is an entry in-parameter, it is replaced by a constant renaming
2777 -- for which Extra_Constrained is never created.
2779 if Present (Formal_Ent)
2780 and then Ekind (Formal_Ent) /= E_Constant
2781 and then Present (Extra_Constrained (Formal_Ent))
2785 (Extra_Constrained (Formal_Ent), Sloc (N)));
2787 -- If the prefix is an access to object, the attribute applies to
2788 -- the designated object, so rewrite with an explicit dereference.
2790 elsif Is_Access_Type (Etype (Pref))
2792 (not Is_Entity_Name (Pref) or else Is_Object (Entity (Pref)))
2795 Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
2796 Analyze_And_Resolve (N, Standard_Boolean);
2799 -- For variables with a Extra_Constrained field, we use the
2800 -- corresponding entity.
2802 elsif Nkind (Pref) = N_Identifier
2803 and then Ekind (Entity (Pref)) = E_Variable
2804 and then Present (Extra_Constrained (Entity (Pref)))
2808 (Extra_Constrained (Entity (Pref)), Sloc (N)));
2810 -- For all other cases, we can tell at compile time
2813 -- For access type, apply access check as needed
2815 if Is_Entity_Name (Pref)
2816 and then not Is_Type (Entity (Pref))
2817 and then Is_Access_Type (Ptyp)
2819 Apply_Access_Check (N);
2825 (Exp_Util.Attribute_Constrained_Static_Value
2826 (Pref)), Sloc (N)));
2829 Analyze_And_Resolve (N, Standard_Boolean);
2836 -- Transforms 'Copy_Sign into a call to the floating-point attribute
2837 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
2839 when Attribute_Copy_Sign =>
2840 Expand_Fpt_Attribute_RR (N);
2846 -- Transforms 'Count attribute into a call to the Count function
2848 when Attribute_Count => Count : declare
2850 Conctyp : Entity_Id;
2852 Entry_Id : Entity_Id;
2857 -- If the prefix is a member of an entry family, retrieve both
2858 -- entry name and index. For a simple entry there is no index.
2860 if Nkind (Pref) = N_Indexed_Component then
2861 Entnam := Prefix (Pref);
2862 Index := First (Expressions (Pref));
2868 Entry_Id := Entity (Entnam);
2870 -- Find the concurrent type in which this attribute is referenced
2871 -- (there had better be one).
2873 Conctyp := Current_Scope;
2874 while not Is_Concurrent_Type (Conctyp) loop
2875 Conctyp := Scope (Conctyp);
2880 if Is_Protected_Type (Conctyp) then
2882 -- No need to transform 'Count into a function call if the current
2883 -- scope has been eliminated. In this case such transformation is
2884 -- also not viable because the enclosing protected object is not
2887 if Is_Eliminated (Current_Scope) then
2891 case Corresponding_Runtime_Package (Conctyp) is
2892 when System_Tasking_Protected_Objects_Entries =>
2893 Name := New_Occurrence_Of (RTE (RE_Protected_Count), Loc);
2896 Make_Function_Call (Loc,
2898 Parameter_Associations => New_List (
2900 (Find_Protection_Object (Current_Scope), Loc),
2901 Entry_Index_Expression
2902 (Loc, Entry_Id, Index, Scope (Entry_Id))));
2904 when System_Tasking_Protected_Objects_Single_Entry =>
2906 New_Occurrence_Of (RTE (RE_Protected_Count_Entry), Loc);
2909 Make_Function_Call (Loc,
2911 Parameter_Associations => New_List (
2913 (Find_Protection_Object (Current_Scope), Loc)));
2916 raise Program_Error;
2923 Make_Function_Call (Loc,
2924 Name => New_Occurrence_Of (RTE (RE_Task_Count), Loc),
2925 Parameter_Associations => New_List (
2926 Entry_Index_Expression (Loc,
2927 Entry_Id, Index, Scope (Entry_Id))));
2930 -- The call returns type Natural but the context is universal integer
2931 -- so any integer type is allowed. The attribute was already resolved
2932 -- so its Etype is the required result type. If the base type of the
2933 -- context type is other than Standard.Integer we put in a conversion
2934 -- to the required type. This can be a normal typed conversion since
2935 -- both input and output types of the conversion are integer types
2937 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
2938 Rewrite (N, Convert_To (Typ, Call));
2943 Analyze_And_Resolve (N, Typ);
2946 ---------------------
2947 -- Descriptor_Size --
2948 ---------------------
2950 when Attribute_Descriptor_Size =>
2952 -- Attribute Descriptor_Size is handled by the back end when applied
2953 -- to an unconstrained array type.
2955 if Is_Array_Type (Ptyp)
2956 and then not Is_Constrained (Ptyp)
2958 Apply_Universal_Integer_Attribute_Checks (N);
2960 -- For any other type, the descriptor size is 0 because there is no
2961 -- actual descriptor, but the result is not formally static.
2964 Rewrite (N, Make_Integer_Literal (Loc, 0));
2966 Set_Is_Static_Expression (N, False);
2973 -- This processing is shared by Elab_Spec
2975 -- What we do is to insert the following declarations
2978 -- pragma Import (C, enn, "name___elabb/s");
2980 -- and then the Elab_Body/Spec attribute is replaced by a reference
2981 -- to this defining identifier.
2983 when Attribute_Elab_Body
2984 | Attribute_Elab_Spec
2986 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
2987 -- back-end knows how to handle these attributes directly.
2989 if CodePeer_Mode then
2994 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
2998 procedure Make_Elab_String (Nod : Node_Id);
2999 -- Given Nod, an identifier, or a selected component, put the
3000 -- image into the current string literal, with double underline
3001 -- between components.
3003 ----------------------
3004 -- Make_Elab_String --
3005 ----------------------
3007 procedure Make_Elab_String (Nod : Node_Id) is
3009 if Nkind (Nod) = N_Selected_Component then
3010 Make_Elab_String (Prefix (Nod));
3011 Store_String_Char ('_');
3012 Store_String_Char ('_');
3013 Get_Name_String (Chars (Selector_Name (Nod)));
3016 pragma Assert (Nkind (Nod) = N_Identifier);
3017 Get_Name_String (Chars (Nod));
3020 Store_String_Chars (Name_Buffer (1 .. Name_Len));
3021 end Make_Elab_String;
3023 -- Start of processing for Elab_Body/Elab_Spec
3026 -- First we need to prepare the string literal for the name of
3027 -- the elaboration routine to be referenced.
3030 Make_Elab_String (Pref);
3031 Store_String_Chars ("___elab");
3032 Lang := Make_Identifier (Loc, Name_C);
3034 if Id = Attribute_Elab_Body then
3035 Store_String_Char ('b');
3037 Store_String_Char ('s');
3042 Insert_Actions (N, New_List (
3043 Make_Subprogram_Declaration (Loc,
3045 Make_Procedure_Specification (Loc,
3046 Defining_Unit_Name => Ent)),
3049 Chars => Name_Import,
3050 Pragma_Argument_Associations => New_List (
3051 Make_Pragma_Argument_Association (Loc, Expression => Lang),
3053 Make_Pragma_Argument_Association (Loc,
3054 Expression => Make_Identifier (Loc, Chars (Ent))),
3056 Make_Pragma_Argument_Association (Loc,
3057 Expression => Make_String_Literal (Loc, Str))))));
3059 Set_Entity (N, Ent);
3060 Rewrite (N, New_Occurrence_Of (Ent, Loc));
3063 --------------------
3064 -- Elab_Subp_Body --
3065 --------------------
3067 -- Always ignored. In CodePeer mode, gnat2scil knows how to handle
3068 -- this attribute directly, and if we are not in CodePeer mode it is
3069 -- entirely ignored ???
3071 when Attribute_Elab_Subp_Body =>
3078 -- Elaborated is always True for preelaborated units, predefined units,
3079 -- pure units and units which have Elaborate_Body pragmas. These units
3080 -- have no elaboration entity.
3082 -- Note: The Elaborated attribute is never passed to the back end
3084 when Attribute_Elaborated => Elaborated : declare
3085 Elab_Id : constant Entity_Id := Elaboration_Entity (Entity (Pref));
3088 if Present (Elab_Id) then
3091 Left_Opnd => New_Occurrence_Of (Elab_Id, Loc),
3092 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)));
3094 Analyze_And_Resolve (N, Typ);
3096 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
3104 when Attribute_Enum_Rep => Enum_Rep : declare
3108 -- Get the expression, which is X for Enum_Type'Enum_Rep (X) or
3111 if Is_Non_Empty_List (Exprs) then
3112 Expr := First (Exprs);
3117 -- If the expression is an enumeration literal, it is replaced by the
3120 if Nkind (Expr) in N_Has_Entity
3121 and then Ekind (Entity (Expr)) = E_Enumeration_Literal
3124 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Expr))));
3126 -- If this is a renaming of a literal, recover the representation
3127 -- of the original. If it renames an expression there is nothing to
3130 elsif Nkind (Expr) in N_Has_Entity
3131 and then Ekind (Entity (Expr)) = E_Constant
3132 and then Present (Renamed_Object (Entity (Expr)))
3133 and then Is_Entity_Name (Renamed_Object (Entity (Expr)))
3134 and then Ekind (Entity (Renamed_Object (Entity (Expr)))) =
3135 E_Enumeration_Literal
3138 Make_Integer_Literal (Loc,
3139 Enumeration_Rep (Entity (Renamed_Object (Entity (Expr))))));
3141 -- If not constant-folded above, Enum_Type'Enum_Rep (X) or
3142 -- X'Enum_Rep expands to
3146 -- This is simply a direct conversion from the enumeration type to
3147 -- the target integer type, which is treated by the back end as a
3148 -- normal integer conversion, treating the enumeration type as an
3149 -- integer, which is exactly what we want. We set Conversion_OK to
3150 -- make sure that the analyzer does not complain about what otherwise
3151 -- might be an illegal conversion.
3154 Rewrite (N, OK_Convert_To (Typ, Relocate_Node (Expr)));
3158 Analyze_And_Resolve (N, Typ);
3165 when Attribute_Enum_Val => Enum_Val : declare
3167 Btyp : constant Entity_Id := Base_Type (Ptyp);
3170 -- X'Enum_Val (Y) expands to
3172 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
3175 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
3177 -- Ensure that the expression is not truncated since the "bad" bits
3180 if Nkind (Expr) = N_Unchecked_Type_Conversion then
3181 Set_No_Truncation (Expr);
3185 Make_Raise_Constraint_Error (Loc,
3189 Make_Function_Call (Loc,
3191 New_Occurrence_Of (TSS (Btyp, TSS_Rep_To_Pos), Loc),
3192 Parameter_Associations => New_List (
3193 Relocate_Node (Duplicate_Subexpr (Expr)),
3194 New_Occurrence_Of (Standard_False, Loc))),
3196 Right_Opnd => Make_Integer_Literal (Loc, -1)),
3197 Reason => CE_Range_Check_Failed));
3200 Analyze_And_Resolve (N, Ptyp);
3207 -- Transforms 'Exponent into a call to the floating-point attribute
3208 -- function Exponent in Fat_xxx (where xxx is the root type)
3210 when Attribute_Exponent =>
3211 Expand_Fpt_Attribute_R (N);
3217 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
3219 when Attribute_External_Tag =>
3221 Make_Function_Call (Loc,
3223 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3224 Parameter_Associations => New_List (
3225 Make_Attribute_Reference (Loc,
3226 Attribute_Name => Name_Tag,
3227 Prefix => Prefix (N)))));
3229 Analyze_And_Resolve (N, Standard_String);
3231 -----------------------
3232 -- Finalization_Size --
3233 -----------------------
3235 when Attribute_Finalization_Size => Finalization_Size : declare
3236 function Calculate_Header_Size return Node_Id;
3237 -- Generate a runtime call to calculate the size of the hidden header
3238 -- along with any added padding which would precede a heap-allocated
3239 -- object of the prefix type.
3241 ---------------------------
3242 -- Calculate_Header_Size --
3243 ---------------------------
3245 function Calculate_Header_Size return Node_Id is
3248 -- Universal_Integer
3249 -- (Header_Size_With_Padding (Pref'Alignment))
3252 Convert_To (Universal_Integer,
3253 Make_Function_Call (Loc,
3255 New_Occurrence_Of (RTE (RE_Header_Size_With_Padding), Loc),
3257 Parameter_Associations => New_List (
3258 Make_Attribute_Reference (Loc,
3259 Prefix => New_Copy_Tree (Pref),
3260 Attribute_Name => Name_Alignment))));
3261 end Calculate_Header_Size;
3267 -- Start of Finalization_Size
3270 -- An object of a class-wide type first requires a runtime check to
3271 -- determine whether it is actually controlled or not. Depending on
3272 -- the outcome of this check, the Finalization_Size of the object
3273 -- may be zero or some positive value.
3275 -- In this scenario, Pref'Finalization_Size is expanded into
3277 -- Size : Integer := 0;
3279 -- if Needs_Finalization (Pref'Tag) then
3281 -- Universal_Integer
3282 -- (Header_Size_With_Padding (Pref'Alignment));
3285 -- and the attribute reference is replaced with a reference to Size.
3287 if Is_Class_Wide_Type (Ptyp) then
3288 Size := Make_Temporary (Loc, 'S');
3290 Insert_Actions (N, New_List (
3293 -- Size : Integer := 0;
3295 Make_Object_Declaration (Loc,
3296 Defining_Identifier => Size,
3297 Object_Definition =>
3298 New_Occurrence_Of (Standard_Integer, Loc),
3299 Expression => Make_Integer_Literal (Loc, 0)),
3302 -- if Needs_Finalization (Pref'Tag) then
3304 -- Universal_Integer
3305 -- (Header_Size_With_Padding (Pref'Alignment));
3308 Make_If_Statement (Loc,
3310 Make_Function_Call (Loc,
3312 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
3314 Parameter_Associations => New_List (
3315 Make_Attribute_Reference (Loc,
3316 Prefix => New_Copy_Tree (Pref),
3317 Attribute_Name => Name_Tag))),
3319 Then_Statements => New_List (
3320 Make_Assignment_Statement (Loc,
3321 Name => New_Occurrence_Of (Size, Loc),
3322 Expression => Calculate_Header_Size)))));
3324 Rewrite (N, New_Occurrence_Of (Size, Loc));
3326 -- The prefix is known to be controlled at compile time. Calculate
3327 -- Finalization_Size by calling function Header_Size_With_Padding.
3329 elsif Needs_Finalization (Ptyp) then
3330 Rewrite (N, Calculate_Header_Size);
3332 -- The prefix is not an object with controlled parts, so its
3333 -- Finalization_Size is zero.
3336 Rewrite (N, Make_Integer_Literal (Loc, 0));
3339 -- Due to cases where the entity type of the attribute is already
3340 -- resolved the rewritten N must get re-resolved to its appropriate
3343 Analyze_And_Resolve (N, Typ);
3344 end Finalization_Size;
3350 when Attribute_First =>
3352 -- If the prefix type is a constrained packed array type which
3353 -- already has a Packed_Array_Impl_Type representation defined, then
3354 -- replace this attribute with a direct reference to 'First of the
3355 -- appropriate index subtype (since otherwise the back end will try
3356 -- to give us the value of 'First for this implementation type).
3358 if Is_Constrained_Packed_Array (Ptyp) then
3360 Make_Attribute_Reference (Loc,
3361 Attribute_Name => Name_First,
3363 New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
3364 Analyze_And_Resolve (N, Typ);
3366 -- For access type, apply access check as needed
3368 elsif Is_Access_Type (Ptyp) then
3369 Apply_Access_Check (N);
3371 -- For scalar type, if low bound is a reference to an entity, just
3372 -- replace with a direct reference. Note that we can only have a
3373 -- reference to a constant entity at this stage, anything else would
3374 -- have already been rewritten.
3376 elsif Is_Scalar_Type (Ptyp) then
3378 Lo : constant Node_Id := Type_Low_Bound (Ptyp);
3380 if Is_Entity_Name (Lo) then
3381 Rewrite (N, New_Occurrence_Of (Entity (Lo), Loc));
3390 -- Compute this if component clause was present, otherwise we leave the
3391 -- computation to be completed in the back-end, since we don't know what
3392 -- layout will be chosen.
3394 when Attribute_First_Bit => First_Bit_Attr : declare
3395 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3398 -- In Ada 2005 (or later) if we have the non-default bit order, then
3399 -- we return the original value as given in the component clause
3400 -- (RM 2005 13.5.2(3/2)).
3402 if Present (Component_Clause (CE))
3403 and then Ada_Version >= Ada_2005
3404 and then Reverse_Bit_Order (Scope (CE))
3407 Make_Integer_Literal (Loc,
3408 Intval => Expr_Value (First_Bit (Component_Clause (CE)))));
3409 Analyze_And_Resolve (N, Typ);
3411 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
3412 -- rewrite with normalized value if we know it statically.
3414 elsif Known_Static_Component_Bit_Offset (CE) then
3416 Make_Integer_Literal (Loc,
3417 Component_Bit_Offset (CE) mod System_Storage_Unit));
3418 Analyze_And_Resolve (N, Typ);
3420 -- Otherwise left to back end, just do universal integer checks
3423 Apply_Universal_Integer_Attribute_Checks (N);
3427 --------------------------------
3428 -- Fixed_Value, Integer_Value --
3429 --------------------------------
3433 -- fixtype'Fixed_Value (integer-value)
3434 -- inttype'Integer_Value (fixed-value)
3438 -- fixtype (integer-value)
3439 -- inttype (fixed-value)
3443 -- We set Conversion_OK on the conversion because we do not want it
3444 -- to go through the fixed-point conversion circuits.
3446 when Attribute_Fixed_Value
3447 | Attribute_Integer_Value
3449 Rewrite (N, OK_Convert_To (Entity (Pref), First (Exprs)));
3451 -- Note that it might appear that a properly analyzed unchecked
3452 -- conversion would be just fine here, but that's not the case,
3453 -- since the full range checks performed by the following calls
3456 Apply_Type_Conversion_Checks (N);
3458 -- Note that Apply_Type_Conversion_Checks only deals with the
3459 -- overflow checks on conversions involving fixed-point types
3460 -- so we must apply range checks manually on them and expand.
3462 Apply_Scalar_Range_Check
3463 (Expression (N), Etype (N), Fixed_Int => True);
3472 -- Transforms 'Floor into a call to the floating-point attribute
3473 -- function Floor in Fat_xxx (where xxx is the root type)
3475 when Attribute_Floor =>
3476 Expand_Fpt_Attribute_R (N);
3482 -- For the fixed-point type Typ:
3488 -- Result_Type (System.Fore (Universal_Real (Type'First)),
3489 -- Universal_Real (Type'Last))
3491 -- Note that we know that the type is a nonstatic subtype, or Fore would
3492 -- have itself been computed dynamically in Eval_Attribute.
3494 when Attribute_Fore =>
3497 Make_Function_Call (Loc,
3499 New_Occurrence_Of (RTE (RE_Fore), Loc),
3501 Parameter_Associations => New_List (
3502 Convert_To (Universal_Real,
3503 Make_Attribute_Reference (Loc,
3504 Prefix => New_Occurrence_Of (Ptyp, Loc),
3505 Attribute_Name => Name_First)),
3507 Convert_To (Universal_Real,
3508 Make_Attribute_Reference (Loc,
3509 Prefix => New_Occurrence_Of (Ptyp, Loc),
3510 Attribute_Name => Name_Last))))));
3512 Analyze_And_Resolve (N, Typ);
3518 -- Transforms 'Fraction into a call to the floating-point attribute
3519 -- function Fraction in Fat_xxx (where xxx is the root type)
3521 when Attribute_Fraction =>
3522 Expand_Fpt_Attribute_R (N);
3528 when Attribute_From_Any => From_Any : declare
3529 P_Type : constant Entity_Id := Etype (Pref);
3530 Decls : constant List_Id := New_List;
3534 Build_From_Any_Call (P_Type,
3535 Relocate_Node (First (Exprs)),
3537 Insert_Actions (N, Decls);
3538 Analyze_And_Resolve (N, P_Type);
3541 ----------------------
3542 -- Has_Same_Storage --
3543 ----------------------
3545 when Attribute_Has_Same_Storage => Has_Same_Storage : declare
3546 Loc : constant Source_Ptr := Sloc (N);
3548 X : constant Node_Id := Prefix (N);
3549 Y : constant Node_Id := First (Expressions (N));
3554 -- Rhe expressions for their addresses
3558 -- Rhe expressions for their sizes
3561 -- The attribute is expanded as:
3563 -- (X'address = Y'address)
3564 -- and then (X'Size = Y'Size)
3566 -- If both arguments have the same Etype the second conjunct can be
3570 Make_Attribute_Reference (Loc,
3571 Attribute_Name => Name_Address,
3572 Prefix => New_Copy_Tree (X));
3575 Make_Attribute_Reference (Loc,
3576 Attribute_Name => Name_Address,
3577 Prefix => New_Copy_Tree (Y));
3580 Make_Attribute_Reference (Loc,
3581 Attribute_Name => Name_Size,
3582 Prefix => New_Copy_Tree (X));
3585 Make_Attribute_Reference (Loc,
3586 Attribute_Name => Name_Size,
3587 Prefix => New_Copy_Tree (Y));
3589 if Etype (X) = Etype (Y) then
3592 Left_Opnd => X_Addr,
3593 Right_Opnd => Y_Addr));
3599 Left_Opnd => X_Addr,
3600 Right_Opnd => Y_Addr),
3603 Left_Opnd => X_Size,
3604 Right_Opnd => Y_Size)));
3607 Analyze_And_Resolve (N, Standard_Boolean);
3608 end Has_Same_Storage;
3614 -- For an exception returns a reference to the exception data:
3615 -- Exception_Id!(Prefix'Reference)
3617 -- For a task it returns a reference to the _task_id component of
3618 -- corresponding record:
3620 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
3622 -- in Ada.Task_Identification
3624 when Attribute_Identity => Identity : declare
3625 Id_Kind : Entity_Id;
3628 if Ptyp = Standard_Exception_Type then
3629 Id_Kind := RTE (RE_Exception_Id);
3631 if Present (Renamed_Object (Entity (Pref))) then
3632 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
3636 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
3638 Id_Kind := RTE (RO_AT_Task_Id);
3640 -- If the prefix is a task interface, the Task_Id is obtained
3641 -- dynamically through a dispatching call, as for other task
3642 -- attributes applied to interfaces.
3644 if Ada_Version >= Ada_2005
3645 and then Ekind (Ptyp) = E_Class_Wide_Type
3646 and then Is_Interface (Ptyp)
3647 and then Is_Task_Interface (Ptyp)
3650 Unchecked_Convert_To
3651 (Id_Kind, Build_Disp_Get_Task_Id_Call (Pref)));
3655 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
3659 Analyze_And_Resolve (N, Id_Kind);
3666 -- Image attribute is handled in separate unit Exp_Imgv
3668 when Attribute_Image =>
3670 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
3671 -- back-end knows how to handle this attribute directly.
3673 if CodePeer_Mode then
3677 Expand_Image_Attribute (N);
3683 -- X'Img is expanded to typ'Image (X), where typ is the type of X
3685 when Attribute_Img =>
3686 Expand_Image_Attribute (N);
3692 when Attribute_Input => Input : declare
3693 P_Type : constant Entity_Id := Entity (Pref);
3694 B_Type : constant Entity_Id := Base_Type (P_Type);
3695 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3696 Strm : constant Node_Id := First (Exprs);
3704 Cntrl : Node_Id := Empty;
3705 -- Value for controlling argument in call. Always Empty except in
3706 -- the dispatching (class-wide type) case, where it is a reference
3707 -- to the dummy object initialized to the right internal tag.
3709 procedure Freeze_Stream_Subprogram (F : Entity_Id);
3710 -- The expansion of the attribute reference may generate a call to
3711 -- a user-defined stream subprogram that is frozen by the call. This
3712 -- can lead to access-before-elaboration problem if the reference
3713 -- appears in an object declaration and the subprogram body has not
3714 -- been seen. The freezing of the subprogram requires special code
3715 -- because it appears in an expanded context where expressions do
3716 -- not freeze their constituents.
3718 ------------------------------
3719 -- Freeze_Stream_Subprogram --
3720 ------------------------------
3722 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
3723 Decl : constant Node_Id := Unit_Declaration_Node (F);
3727 -- If this is user-defined subprogram, the corresponding
3728 -- stream function appears as a renaming-as-body, and the
3729 -- user subprogram must be retrieved by tree traversal.
3732 and then Nkind (Decl) = N_Subprogram_Declaration
3733 and then Present (Corresponding_Body (Decl))
3735 Bod := Corresponding_Body (Decl);
3737 if Nkind (Unit_Declaration_Node (Bod)) =
3738 N_Subprogram_Renaming_Declaration
3740 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
3743 end Freeze_Stream_Subprogram;
3745 -- Start of processing for Input
3748 -- If no underlying type, we have an error that will be diagnosed
3749 -- elsewhere, so here we just completely ignore the expansion.
3755 -- Stream operations can appear in user code even if the restriction
3756 -- No_Streams is active (for example, when instantiating a predefined
3757 -- container). In that case rewrite the attribute as a Raise to
3758 -- prevent any run-time use.
3760 if Restriction_Active (No_Streams) then
3762 Make_Raise_Program_Error (Sloc (N),
3763 Reason => PE_Stream_Operation_Not_Allowed));
3764 Set_Etype (N, B_Type);
3768 -- If there is a TSS for Input, just call it
3770 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
3772 if Present (Fname) then
3776 -- If there is a Stream_Convert pragma, use it, we rewrite
3778 -- sourcetyp'Input (stream)
3782 -- sourcetyp (streamread (strmtyp'Input (stream)));
3784 -- where streamread is the given Read function that converts an
3785 -- argument of type strmtyp to type sourcetyp or a type from which
3786 -- it is derived (extra conversion required for the derived case).
3788 Prag := Get_Stream_Convert_Pragma (P_Type);
3790 if Present (Prag) then
3791 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
3792 Rfunc := Entity (Expression (Arg2));
3796 Make_Function_Call (Loc,
3797 Name => New_Occurrence_Of (Rfunc, Loc),
3798 Parameter_Associations => New_List (
3799 Make_Attribute_Reference (Loc,
3802 (Etype (First_Formal (Rfunc)), Loc),
3803 Attribute_Name => Name_Input,
3804 Expressions => Exprs)))));
3806 Analyze_And_Resolve (N, B_Type);
3811 elsif Is_Elementary_Type (U_Type) then
3813 -- A special case arises if we have a defined _Read routine,
3814 -- since in this case we are required to call this routine.
3817 Typ : Entity_Id := P_Type;
3819 if Present (Full_View (Typ)) then
3820 Typ := Full_View (Typ);
3823 if Present (TSS (Base_Type (Typ), TSS_Stream_Read)) then
3824 Build_Record_Or_Elementary_Input_Function
3825 (Loc, Typ, Decl, Fname, Use_Underlying => False);
3826 Insert_Action (N, Decl);
3828 -- For normal cases, we call the I_xxx routine directly
3831 Rewrite (N, Build_Elementary_Input_Call (N));
3832 Analyze_And_Resolve (N, P_Type);
3839 elsif Is_Array_Type (U_Type) then
3840 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
3841 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3843 -- Dispatching case with class-wide type
3845 elsif Is_Class_Wide_Type (P_Type) then
3847 -- No need to do anything else compiling under restriction
3848 -- No_Dispatching_Calls. During the semantic analysis we
3849 -- already notified such violation.
3851 if Restriction_Active (No_Dispatching_Calls) then
3856 Rtyp : constant Entity_Id := Root_Type (P_Type);
3858 Expr : Node_Id; -- call to Descendant_Tag
3859 Get_Tag : Node_Id; -- expression to read the 'Tag
3862 -- Read the internal tag (RM 13.13.2(34)) and use it to
3863 -- initialize a dummy tag value. We used to unconditionally
3866 -- Descendant_Tag (String'Input (Strm), P_Type);
3868 -- which turns into a call to String_Input_Blk_IO. However,
3869 -- if the input is malformed, that could try to read an
3870 -- enormous String, causing chaos. So instead we call
3871 -- String_Input_Tag, which does the same thing as
3872 -- String_Input_Blk_IO, except that if the String is
3873 -- absurdly long, it raises an exception.
3875 -- However, if the No_Stream_Optimizations restriction
3876 -- is active, we disable this unnecessary attempt at
3877 -- robustness; we really need to read the string
3878 -- character-by-character.
3880 -- This value is used only to provide a controlling
3881 -- argument for the eventual _Input call. Descendant_Tag is
3882 -- called rather than Internal_Tag to ensure that we have a
3883 -- tag for a type that is descended from the prefix type and
3884 -- declared at the same accessibility level (the exception
3885 -- Tag_Error will be raised otherwise). The level check is
3886 -- required for Ada 2005 because tagged types can be
3887 -- extended in nested scopes (AI-344).
3889 -- Note: we used to generate an explicit declaration of a
3890 -- constant Ada.Tags.Tag object, and use an occurrence of
3891 -- this constant in Cntrl, but this caused a secondary stack
3894 if Restriction_Active (No_Stream_Optimizations) then
3896 Make_Attribute_Reference (Loc,
3898 New_Occurrence_Of (Standard_String, Loc),
3899 Attribute_Name => Name_Input,
3900 Expressions => New_List (
3901 Relocate_Node (Duplicate_Subexpr (Strm))));
3904 Make_Function_Call (Loc,
3907 (RTE (RE_String_Input_Tag), Loc),
3908 Parameter_Associations => New_List (
3909 Relocate_Node (Duplicate_Subexpr (Strm))));
3913 Make_Function_Call (Loc,
3915 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
3916 Parameter_Associations => New_List (
3918 Make_Attribute_Reference (Loc,
3919 Prefix => New_Occurrence_Of (P_Type, Loc),
3920 Attribute_Name => Name_Tag)));
3922 Set_Etype (Expr, RTE (RE_Tag));
3924 -- Now we need to get the entity for the call, and construct
3925 -- a function call node, where we preset a reference to Dnn
3926 -- as the controlling argument (doing an unchecked convert
3927 -- to the class-wide tagged type to make it look like a real
3930 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
3931 Cntrl := Unchecked_Convert_To (P_Type, Expr);
3932 Set_Etype (Cntrl, P_Type);
3933 Set_Parent (Cntrl, N);
3936 -- For tagged types, use the primitive Input function
3938 elsif Is_Tagged_Type (U_Type) then
3939 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
3941 -- All other record type cases, including protected records. The
3942 -- latter only arise for expander generated code for handling
3943 -- shared passive partition access.
3947 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3949 -- Ada 2005 (AI-216): Program_Error is raised executing default
3950 -- implementation of the Input attribute of an unchecked union
3951 -- type if the type lacks default discriminant values.
3953 if Is_Unchecked_Union (Base_Type (U_Type))
3954 and then No (Discriminant_Constraint (U_Type))
3957 Make_Raise_Program_Error (Loc,
3958 Reason => PE_Unchecked_Union_Restriction));
3963 -- Build the type's Input function, passing the subtype rather
3964 -- than its base type, because checks are needed in the case of
3965 -- constrained discriminants (see Ada 2012 AI05-0192).
3967 Build_Record_Or_Elementary_Input_Function
3968 (Loc, U_Type, Decl, Fname);
3969 Insert_Action (N, Decl);
3971 if Nkind (Parent (N)) = N_Object_Declaration
3972 and then Is_Record_Type (U_Type)
3974 -- The stream function may contain calls to user-defined
3975 -- Read procedures for individual components.
3982 Comp := First_Component (U_Type);
3983 while Present (Comp) loop
3985 Find_Stream_Subprogram
3986 (Etype (Comp), TSS_Stream_Read);
3988 if Present (Func) then
3989 Freeze_Stream_Subprogram (Func);
3992 Next_Component (Comp);
3999 -- If we fall through, Fname is the function to be called. The result
4000 -- is obtained by calling the appropriate function, then converting
4001 -- the result. The conversion does a subtype check.
4004 Make_Function_Call (Loc,
4005 Name => New_Occurrence_Of (Fname, Loc),
4006 Parameter_Associations => New_List (
4007 Relocate_Node (Strm)));
4009 Set_Controlling_Argument (Call, Cntrl);
4010 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
4011 Analyze_And_Resolve (N, P_Type);
4013 if Nkind (Parent (N)) = N_Object_Declaration then
4014 Freeze_Stream_Subprogram (Fname);
4022 when Attribute_Invalid_Value =>
4023 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
4025 -- The value produced may be a conversion of a literal, which must be
4026 -- resolved to establish its proper type.
4028 Analyze_And_Resolve (N);
4034 when Attribute_Last =>
4036 -- If the prefix type is a constrained packed array type which
4037 -- already has a Packed_Array_Impl_Type representation defined, then
4038 -- replace this attribute with a direct reference to 'Last of the
4039 -- appropriate index subtype (since otherwise the back end will try
4040 -- to give us the value of 'Last for this implementation type).
4042 if Is_Constrained_Packed_Array (Ptyp) then
4044 Make_Attribute_Reference (Loc,
4045 Attribute_Name => Name_Last,
4046 Prefix => New_Occurrence_Of (Get_Index_Subtype (N), Loc)));
4047 Analyze_And_Resolve (N, Typ);
4049 -- For access type, apply access check as needed
4051 elsif Is_Access_Type (Ptyp) then
4052 Apply_Access_Check (N);
4054 -- For scalar type, if low bound is a reference to an entity, just
4055 -- replace with a direct reference. Note that we can only have a
4056 -- reference to a constant entity at this stage, anything else would
4057 -- have already been rewritten.
4059 elsif Is_Scalar_Type (Ptyp) then
4061 Hi : constant Node_Id := Type_High_Bound (Ptyp);
4063 if Is_Entity_Name (Hi) then
4064 Rewrite (N, New_Occurrence_Of (Entity (Hi), Loc));
4073 -- We compute this if a component clause was present, otherwise we leave
4074 -- the computation up to the back end, since we don't know what layout
4077 when Attribute_Last_Bit => Last_Bit_Attr : declare
4078 CE : constant Entity_Id := Entity (Selector_Name (Pref));
4081 -- In Ada 2005 (or later) if we have the non-default bit order, then
4082 -- we return the original value as given in the component clause
4083 -- (RM 2005 13.5.2(3/2)).
4085 if Present (Component_Clause (CE))
4086 and then Ada_Version >= Ada_2005
4087 and then Reverse_Bit_Order (Scope (CE))
4090 Make_Integer_Literal (Loc,
4091 Intval => Expr_Value (Last_Bit (Component_Clause (CE)))));
4092 Analyze_And_Resolve (N, Typ);
4094 -- Otherwise (Ada 83/95 or Ada 2005 or later with default bit order),
4095 -- rewrite with normalized value if we know it statically.
4097 elsif Known_Static_Component_Bit_Offset (CE)
4098 and then Known_Static_Esize (CE)
4101 Make_Integer_Literal (Loc,
4102 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
4104 Analyze_And_Resolve (N, Typ);
4106 -- Otherwise leave to back end, just apply universal integer checks
4109 Apply_Universal_Integer_Attribute_Checks (N);
4117 -- Transforms 'Leading_Part into a call to the floating-point attribute
4118 -- function Leading_Part in Fat_xxx (where xxx is the root type)
4120 -- Note: strictly, we should generate special case code to deal with
4121 -- absurdly large positive arguments (greater than Integer'Last), which
4122 -- result in returning the first argument unchanged, but it hardly seems
4123 -- worth the effort. We raise constraint error for absurdly negative
4124 -- arguments which is fine.
4126 when Attribute_Leading_Part =>
4127 Expand_Fpt_Attribute_RI (N);
4133 when Attribute_Length => Length : declare
4138 -- Processing for packed array types
4140 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
4141 Ityp := Get_Index_Subtype (N);
4143 -- If the index type, Ityp, is an enumeration type with holes,
4144 -- then we calculate X'Length explicitly using
4147 -- (0, Ityp'Pos (X'Last (N)) -
4148 -- Ityp'Pos (X'First (N)) + 1);
4150 -- Since the bounds in the template are the representation values
4151 -- and the back end would get the wrong value.
4153 if Is_Enumeration_Type (Ityp)
4154 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
4159 Xnum := Expr_Value (First (Expressions (N)));
4163 Make_Attribute_Reference (Loc,
4164 Prefix => New_Occurrence_Of (Typ, Loc),
4165 Attribute_Name => Name_Max,
4166 Expressions => New_List
4167 (Make_Integer_Literal (Loc, 0),
4171 Make_Op_Subtract (Loc,
4173 Make_Attribute_Reference (Loc,
4174 Prefix => New_Occurrence_Of (Ityp, Loc),
4175 Attribute_Name => Name_Pos,
4177 Expressions => New_List (
4178 Make_Attribute_Reference (Loc,
4179 Prefix => Duplicate_Subexpr (Pref),
4180 Attribute_Name => Name_Last,
4181 Expressions => New_List (
4182 Make_Integer_Literal (Loc, Xnum))))),
4185 Make_Attribute_Reference (Loc,
4186 Prefix => New_Occurrence_Of (Ityp, Loc),
4187 Attribute_Name => Name_Pos,
4189 Expressions => New_List (
4190 Make_Attribute_Reference (Loc,
4192 Duplicate_Subexpr_No_Checks (Pref),
4193 Attribute_Name => Name_First,
4194 Expressions => New_List (
4195 Make_Integer_Literal (Loc, Xnum)))))),
4197 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4199 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
4202 -- If the prefix type is a constrained packed array type which
4203 -- already has a Packed_Array_Impl_Type representation defined,
4204 -- then replace this attribute with a reference to 'Range_Length
4205 -- of the appropriate index subtype (since otherwise the
4206 -- back end will try to give us the value of 'Length for
4207 -- this implementation type).s
4209 elsif Is_Constrained (Ptyp) then
4211 Make_Attribute_Reference (Loc,
4212 Attribute_Name => Name_Range_Length,
4213 Prefix => New_Occurrence_Of (Ityp, Loc)));
4214 Analyze_And_Resolve (N, Typ);
4219 elsif Is_Access_Type (Ptyp) then
4220 Apply_Access_Check (N);
4222 -- If the designated type is a packed array type, then we convert
4223 -- the reference to:
4226 -- xtyp'Pos (Pref'Last (Expr)) -
4227 -- xtyp'Pos (Pref'First (Expr)));
4229 -- This is a bit complex, but it is the easiest thing to do that
4230 -- works in all cases including enum types with holes xtyp here
4231 -- is the appropriate index type.
4234 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
4238 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
4239 Xtyp := Get_Index_Subtype (N);
4242 Make_Attribute_Reference (Loc,
4243 Prefix => New_Occurrence_Of (Typ, Loc),
4244 Attribute_Name => Name_Max,
4245 Expressions => New_List (
4246 Make_Integer_Literal (Loc, 0),
4249 Make_Integer_Literal (Loc, 1),
4250 Make_Op_Subtract (Loc,
4252 Make_Attribute_Reference (Loc,
4253 Prefix => New_Occurrence_Of (Xtyp, Loc),
4254 Attribute_Name => Name_Pos,
4255 Expressions => New_List (
4256 Make_Attribute_Reference (Loc,
4257 Prefix => Duplicate_Subexpr (Pref),
4258 Attribute_Name => Name_Last,
4260 New_Copy_List (Exprs)))),
4263 Make_Attribute_Reference (Loc,
4264 Prefix => New_Occurrence_Of (Xtyp, Loc),
4265 Attribute_Name => Name_Pos,
4266 Expressions => New_List (
4267 Make_Attribute_Reference (Loc,
4269 Duplicate_Subexpr_No_Checks (Pref),
4270 Attribute_Name => Name_First,
4272 New_Copy_List (Exprs)))))))));
4274 Analyze_And_Resolve (N, Typ);
4278 -- Otherwise leave it to the back end
4281 Apply_Universal_Integer_Attribute_Checks (N);
4285 -- Attribute Loop_Entry is replaced with a reference to a constant value
4286 -- which captures the prefix at the entry point of the related loop. The
4287 -- loop itself may be transformed into a conditional block.
4289 when Attribute_Loop_Entry =>
4290 Expand_Loop_Entry_Attribute (N);
4296 -- Transforms 'Machine into a call to the floating-point attribute
4297 -- function Machine in Fat_xxx (where xxx is the root type).
4298 -- Expansion is avoided for cases the back end can handle directly.
4300 when Attribute_Machine =>
4301 if not Is_Inline_Floating_Point_Attribute (N) then
4302 Expand_Fpt_Attribute_R (N);
4305 ----------------------
4306 -- Machine_Rounding --
4307 ----------------------
4309 -- Transforms 'Machine_Rounding into a call to the floating-point
4310 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
4311 -- type). Expansion is avoided for cases the back end can handle
4314 when Attribute_Machine_Rounding =>
4315 if not Is_Inline_Floating_Point_Attribute (N) then
4316 Expand_Fpt_Attribute_R (N);
4323 -- Machine_Size is equivalent to Object_Size, so transform it into
4324 -- Object_Size and that way the back end never sees Machine_Size.
4326 when Attribute_Machine_Size =>
4328 Make_Attribute_Reference (Loc,
4329 Prefix => Prefix (N),
4330 Attribute_Name => Name_Object_Size));
4332 Analyze_And_Resolve (N, Typ);
4338 -- The only case that can get this far is the dynamic case of the old
4339 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
4346 -- ityp (System.Mantissa.Mantissa_Value
4347 -- (Integer'Integer_Value (typ'First),
4348 -- Integer'Integer_Value (typ'Last)));
4350 when Attribute_Mantissa =>
4353 Make_Function_Call (Loc,
4355 New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
4357 Parameter_Associations => New_List (
4358 Make_Attribute_Reference (Loc,
4359 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4360 Attribute_Name => Name_Integer_Value,
4361 Expressions => New_List (
4362 Make_Attribute_Reference (Loc,
4363 Prefix => New_Occurrence_Of (Ptyp, Loc),
4364 Attribute_Name => Name_First))),
4366 Make_Attribute_Reference (Loc,
4367 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
4368 Attribute_Name => Name_Integer_Value,
4369 Expressions => New_List (
4370 Make_Attribute_Reference (Loc,
4371 Prefix => New_Occurrence_Of (Ptyp, Loc),
4372 Attribute_Name => Name_Last)))))));
4374 Analyze_And_Resolve (N, Typ);
4380 when Attribute_Max =>
4381 Expand_Min_Max_Attribute (N);
4383 ----------------------------------
4384 -- Max_Size_In_Storage_Elements --
4385 ----------------------------------
4387 when Attribute_Max_Size_In_Storage_Elements => declare
4388 Typ : constant Entity_Id := Etype (N);
4391 Conversion_Added : Boolean := False;
4392 -- A flag which tracks whether the original attribute has been
4393 -- wrapped inside a type conversion.
4396 -- If the prefix is X'Class, we transform it into a direct reference
4397 -- to the class-wide type, because the back end must not see a 'Class
4398 -- reference. See also 'Size.
4400 if Is_Entity_Name (Pref)
4401 and then Is_Class_Wide_Type (Entity (Pref))
4403 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
4407 Apply_Universal_Integer_Attribute_Checks (N);
4409 -- The universal integer check may sometimes add a type conversion,
4410 -- retrieve the original attribute reference from the expression.
4414 if Nkind (Attr) = N_Type_Conversion then
4415 Attr := Expression (Attr);
4416 Conversion_Added := True;
4419 pragma Assert (Nkind (Attr) = N_Attribute_Reference);
4421 -- Heap-allocated controlled objects contain two extra pointers which
4422 -- are not part of the actual type. Transform the attribute reference
4423 -- into a runtime expression to add the size of the hidden header.
4425 if Needs_Finalization (Ptyp)
4426 and then not Header_Size_Added (Attr)
4428 Set_Header_Size_Added (Attr);
4431 -- P'Max_Size_In_Storage_Elements +
4432 -- Universal_Integer
4433 -- (Header_Size_With_Padding (Ptyp'Alignment))
4437 Left_Opnd => Relocate_Node (Attr),
4439 Convert_To (Universal_Integer,
4440 Make_Function_Call (Loc,
4443 (RTE (RE_Header_Size_With_Padding), Loc),
4445 Parameter_Associations => New_List (
4446 Make_Attribute_Reference (Loc,
4448 New_Occurrence_Of (Ptyp, Loc),
4449 Attribute_Name => Name_Alignment))))));
4451 -- Add a conversion to the target type
4453 if not Conversion_Added then
4455 Make_Type_Conversion (Loc,
4456 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4457 Expression => Relocate_Node (Attr)));
4465 --------------------
4466 -- Mechanism_Code --
4467 --------------------
4469 when Attribute_Mechanism_Code =>
4471 -- We must replace the prefix in the renamed case
4473 if Is_Entity_Name (Pref)
4474 and then Present (Alias (Entity (Pref)))
4476 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
4483 when Attribute_Min =>
4484 Expand_Min_Max_Attribute (N);
4490 when Attribute_Mod => Mod_Case : declare
4491 Arg : constant Node_Id := Relocate_Node (First (Exprs));
4492 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
4493 Modv : constant Uint := Modulus (Btyp);
4497 -- This is not so simple. The issue is what type to use for the
4498 -- computation of the modular value.
4500 -- The easy case is when the modulus value is within the bounds
4501 -- of the signed integer type of the argument. In this case we can
4502 -- just do the computation in that signed integer type, and then
4503 -- do an ordinary conversion to the target type.
4505 if Modv <= Expr_Value (Hi) then
4510 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
4512 -- Here we know that the modulus is larger than type'Last of the
4513 -- integer type. There are two cases to consider:
4515 -- a) The integer value is non-negative. In this case, it is
4516 -- returned as the result (since it is less than the modulus).
4518 -- b) The integer value is negative. In this case, we know that the
4519 -- result is modulus + value, where the value might be as small as
4520 -- -modulus. The trouble is what type do we use to do the subtract.
4521 -- No type will do, since modulus can be as big as 2**64, and no
4522 -- integer type accommodates this value. Let's do bit of algebra
4525 -- = modulus - (-value)
4526 -- = (modulus - 1) - (-value - 1)
4528 -- Now modulus - 1 is certainly in range of the modular type.
4529 -- -value is in the range 1 .. modulus, so -value -1 is in the
4530 -- range 0 .. modulus-1 which is in range of the modular type.
4531 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
4532 -- which we can compute using the integer base type.
4534 -- Once this is done we analyze the if expression without range
4535 -- checks, because we know everything is in range, and we want
4536 -- to prevent spurious warnings on either branch.
4540 Make_If_Expression (Loc,
4541 Expressions => New_List (
4543 Left_Opnd => Duplicate_Subexpr (Arg),
4544 Right_Opnd => Make_Integer_Literal (Loc, 0)),
4547 Duplicate_Subexpr_No_Checks (Arg)),
4549 Make_Op_Subtract (Loc,
4551 Make_Integer_Literal (Loc,
4552 Intval => Modv - 1),
4558 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
4560 Make_Integer_Literal (Loc,
4561 Intval => 1))))))));
4565 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
4572 -- Transforms 'Model into a call to the floating-point attribute
4573 -- function Model in Fat_xxx (where xxx is the root type).
4574 -- Expansion is avoided for cases the back end can handle directly.
4576 when Attribute_Model =>
4577 if not Is_Inline_Floating_Point_Attribute (N) then
4578 Expand_Fpt_Attribute_R (N);
4585 -- The processing for Object_Size shares the processing for Size
4591 when Attribute_Old => Old : declare
4592 Typ : constant Entity_Id := Etype (N);
4593 CW_Temp : Entity_Id;
4600 -- Generating C code we don't need to expand this attribute when
4601 -- we are analyzing the internally built nested postconditions
4602 -- procedure since it will be expanded inline (and later it will
4603 -- be removed by Expand_N_Subprogram_Body). It this expansion is
4604 -- performed in such case then the compiler generates unreferenced
4605 -- extra temporaries.
4607 if Modify_Tree_For_C
4608 and then Chars (Current_Scope) = Name_uPostconditions
4613 -- Climb the parent chain looking for subprogram _Postconditions
4616 while Present (Subp) loop
4617 exit when Nkind (Subp) = N_Subprogram_Body
4618 and then Chars (Defining_Entity (Subp)) = Name_uPostconditions;
4620 -- If assertions are disabled, no need to create the declaration
4621 -- that preserves the value. The postcondition pragma in which
4622 -- 'Old appears will be checked or disabled according to the
4623 -- current policy in effect.
4625 if Nkind (Subp) = N_Pragma and then not Is_Checked (Subp) then
4629 Subp := Parent (Subp);
4632 -- 'Old can only appear in a postcondition, the generated body of
4633 -- _Postconditions must be in the tree (or inlined if we are
4634 -- generating C code).
4638 or else (Modify_Tree_For_C and then In_Inlined_Body));
4640 Temp := Make_Temporary (Loc, 'T', Pref);
4642 -- Set the entity kind now in order to mark the temporary as a
4643 -- handler of attribute 'Old's prefix.
4645 Set_Ekind (Temp, E_Constant);
4646 Set_Stores_Attribute_Old_Prefix (Temp);
4648 -- Push the scope of the related subprogram where _Postcondition
4649 -- resides as this ensures that the object will be analyzed in the
4652 if Present (Subp) then
4653 Push_Scope (Scope (Defining_Entity (Subp)));
4655 -- No need to push the scope when generating C code since the
4656 -- _Postcondition procedure has been inlined.
4658 else pragma Assert (Modify_Tree_For_C);
4659 pragma Assert (In_Inlined_Body);
4663 -- Locate the insertion place of the internal temporary that saves
4666 if Present (Subp) then
4669 -- Generating C, the postcondition procedure has been inlined and the
4670 -- temporary is added before the first declaration of the enclosing
4673 else pragma Assert (Modify_Tree_For_C);
4675 while Nkind (Ins_Nod) /= N_Subprogram_Body loop
4676 Ins_Nod := Parent (Ins_Nod);
4679 Ins_Nod := First (Declarations (Ins_Nod));
4682 -- Preserve the tag of the prefix by offering a specific view of the
4683 -- class-wide version of the prefix.
4685 if Is_Tagged_Type (Typ) then
4688 -- CW_Temp : constant Typ'Class := Typ'Class (Pref);
4690 CW_Temp := Make_Temporary (Loc, 'T');
4691 CW_Typ := Class_Wide_Type (Typ);
4693 Insert_Before_And_Analyze (Ins_Nod,
4694 Make_Object_Declaration (Loc,
4695 Defining_Identifier => CW_Temp,
4696 Constant_Present => True,
4697 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
4699 Convert_To (CW_Typ, Relocate_Node (Pref))));
4702 -- Temp : Typ renames Typ (CW_Temp);
4704 Insert_Before_And_Analyze (Ins_Nod,
4705 Make_Object_Renaming_Declaration (Loc,
4706 Defining_Identifier => Temp,
4707 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4709 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
4715 -- Temp : constant Typ := Pref;
4717 Insert_Before_And_Analyze (Ins_Nod,
4718 Make_Object_Declaration (Loc,
4719 Defining_Identifier => Temp,
4720 Constant_Present => True,
4721 Object_Definition => New_Occurrence_Of (Typ, Loc),
4722 Expression => Relocate_Node (Pref)));
4725 if Present (Subp) then
4729 -- Ensure that the prefix of attribute 'Old is valid. The check must
4730 -- be inserted after the expansion of the attribute has taken place
4731 -- to reflect the new placement of the prefix.
4733 if Validity_Checks_On and then Validity_Check_Operands then
4734 Ensure_Valid (Pref);
4737 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4740 ----------------------
4741 -- Overlaps_Storage --
4742 ----------------------
4744 when Attribute_Overlaps_Storage => Overlaps_Storage : declare
4745 Loc : constant Source_Ptr := Sloc (N);
4747 X : constant Node_Id := Prefix (N);
4748 Y : constant Node_Id := First (Expressions (N));
4751 X_Addr, Y_Addr : Node_Id;
4752 -- the expressions for their integer addresses
4754 X_Size, Y_Size : Node_Id;
4755 -- the expressions for their sizes
4760 -- Attribute expands into:
4762 -- if X'Address < Y'address then
4763 -- (X'address + X'Size - 1) >= Y'address
4765 -- (Y'address + Y'size - 1) >= X'Address
4768 -- with the proper address operations. We convert addresses to
4769 -- integer addresses to use predefined arithmetic. The size is
4770 -- expressed in storage units. We add copies of X_Addr and Y_Addr
4771 -- to prevent the appearance of the same node in two places in
4775 Unchecked_Convert_To (RTE (RE_Integer_Address),
4776 Make_Attribute_Reference (Loc,
4777 Attribute_Name => Name_Address,
4778 Prefix => New_Copy_Tree (X)));
4781 Unchecked_Convert_To (RTE (RE_Integer_Address),
4782 Make_Attribute_Reference (Loc,
4783 Attribute_Name => Name_Address,
4784 Prefix => New_Copy_Tree (Y)));
4787 Make_Op_Divide (Loc,
4789 Make_Attribute_Reference (Loc,
4790 Attribute_Name => Name_Size,
4791 Prefix => New_Copy_Tree (X)),
4793 Make_Integer_Literal (Loc, System_Storage_Unit));
4796 Make_Op_Divide (Loc,
4798 Make_Attribute_Reference (Loc,
4799 Attribute_Name => Name_Size,
4800 Prefix => New_Copy_Tree (Y)),
4802 Make_Integer_Literal (Loc, System_Storage_Unit));
4806 Left_Opnd => X_Addr,
4807 Right_Opnd => Y_Addr);
4810 Make_If_Expression (Loc, New_List (
4816 Left_Opnd => New_Copy_Tree (X_Addr),
4818 Make_Op_Subtract (Loc,
4819 Left_Opnd => X_Size,
4820 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4821 Right_Opnd => Y_Addr),
4826 Left_Opnd => New_Copy_Tree (Y_Addr),
4828 Make_Op_Subtract (Loc,
4829 Left_Opnd => Y_Size,
4830 Right_Opnd => Make_Integer_Literal (Loc, 1))),
4831 Right_Opnd => X_Addr))));
4833 Analyze_And_Resolve (N, Standard_Boolean);
4834 end Overlaps_Storage;
4840 when Attribute_Output => Output : declare
4841 P_Type : constant Entity_Id := Entity (Pref);
4842 U_Type : constant Entity_Id := Underlying_Type (P_Type);
4850 -- If no underlying type, we have an error that will be diagnosed
4851 -- elsewhere, so here we just completely ignore the expansion.
4857 -- Stream operations can appear in user code even if the restriction
4858 -- No_Streams is active (for example, when instantiating a predefined
4859 -- container). In that case rewrite the attribute as a Raise to
4860 -- prevent any run-time use.
4862 if Restriction_Active (No_Streams) then
4864 Make_Raise_Program_Error (Sloc (N),
4865 Reason => PE_Stream_Operation_Not_Allowed));
4866 Set_Etype (N, Standard_Void_Type);
4870 -- If TSS for Output is present, just call it
4872 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
4874 if Present (Pname) then
4878 -- If there is a Stream_Convert pragma, use it, we rewrite
4880 -- sourcetyp'Output (stream, Item)
4884 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
4886 -- where strmwrite is the given Write function that converts an
4887 -- argument of type sourcetyp or a type acctyp, from which it is
4888 -- derived to type strmtyp. The conversion to acttyp is required
4889 -- for the derived case.
4891 Prag := Get_Stream_Convert_Pragma (P_Type);
4893 if Present (Prag) then
4895 Next (Next (First (Pragma_Argument_Associations (Prag))));
4896 Wfunc := Entity (Expression (Arg3));
4899 Make_Attribute_Reference (Loc,
4900 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
4901 Attribute_Name => Name_Output,
4902 Expressions => New_List (
4903 Relocate_Node (First (Exprs)),
4904 Make_Function_Call (Loc,
4905 Name => New_Occurrence_Of (Wfunc, Loc),
4906 Parameter_Associations => New_List (
4907 OK_Convert_To (Etype (First_Formal (Wfunc)),
4908 Relocate_Node (Next (First (Exprs)))))))));
4913 -- For elementary types, we call the W_xxx routine directly. Note
4914 -- that the effect of Write and Output is identical for the case
4915 -- of an elementary type (there are no discriminants or bounds).
4917 elsif Is_Elementary_Type (U_Type) then
4919 -- A special case arises if we have a defined _Write routine,
4920 -- since in this case we are required to call this routine.
4923 Typ : Entity_Id := P_Type;
4925 if Present (Full_View (Typ)) then
4926 Typ := Full_View (Typ);
4929 if Present (TSS (Base_Type (Typ), TSS_Stream_Write)) then
4930 Build_Record_Or_Elementary_Output_Procedure
4931 (Loc, Typ, Decl, Pname);
4932 Insert_Action (N, Decl);
4934 -- For normal cases, we call the W_xxx routine directly
4937 Rewrite (N, Build_Elementary_Write_Call (N));
4945 elsif Is_Array_Type (U_Type) then
4946 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
4947 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
4949 -- Class-wide case, first output external tag, then dispatch
4950 -- to the appropriate primitive Output function (RM 13.13.2(31)).
4952 elsif Is_Class_Wide_Type (P_Type) then
4954 -- No need to do anything else compiling under restriction
4955 -- No_Dispatching_Calls. During the semantic analysis we
4956 -- already notified such violation.
4958 if Restriction_Active (No_Dispatching_Calls) then
4963 Strm : constant Node_Id := First (Exprs);
4964 Item : constant Node_Id := Next (Strm);
4967 -- Ada 2005 (AI-344): Check that the accessibility level
4968 -- of the type of the output object is not deeper than
4969 -- that of the attribute's prefix type.
4971 -- if Get_Access_Level (Item'Tag)
4972 -- /= Get_Access_Level (P_Type'Tag)
4977 -- String'Output (Strm, External_Tag (Item'Tag));
4979 -- We cannot figure out a practical way to implement this
4980 -- accessibility check on virtual machines, so we omit it.
4982 if Ada_Version >= Ada_2005
4983 and then Tagged_Type_Expansion
4986 Make_Implicit_If_Statement (N,
4990 Build_Get_Access_Level (Loc,
4991 Make_Attribute_Reference (Loc,
4994 Duplicate_Subexpr (Item,
4996 Attribute_Name => Name_Tag)),
4999 Make_Integer_Literal (Loc,
5000 Type_Access_Level (P_Type))),
5003 New_List (Make_Raise_Statement (Loc,
5005 RTE (RE_Tag_Error), Loc)))));
5009 Make_Attribute_Reference (Loc,
5010 Prefix => New_Occurrence_Of (Standard_String, Loc),
5011 Attribute_Name => Name_Output,
5012 Expressions => New_List (
5013 Relocate_Node (Duplicate_Subexpr (Strm)),
5014 Make_Function_Call (Loc,
5016 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
5017 Parameter_Associations => New_List (
5018 Make_Attribute_Reference (Loc,
5021 (Duplicate_Subexpr (Item, Name_Req => True)),
5022 Attribute_Name => Name_Tag))))));
5025 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
5027 -- Tagged type case, use the primitive Output function
5029 elsif Is_Tagged_Type (U_Type) then
5030 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
5032 -- All other record type cases, including protected records.
5033 -- The latter only arise for expander generated code for
5034 -- handling shared passive partition access.
5038 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5040 -- Ada 2005 (AI-216): Program_Error is raised when executing
5041 -- the default implementation of the Output attribute of an
5042 -- unchecked union type if the type lacks default discriminant
5045 if Is_Unchecked_Union (Base_Type (U_Type))
5046 and then No (Discriminant_Constraint (U_Type))
5049 Make_Raise_Program_Error (Loc,
5050 Reason => PE_Unchecked_Union_Restriction));
5055 Build_Record_Or_Elementary_Output_Procedure
5056 (Loc, Base_Type (U_Type), Decl, Pname);
5057 Insert_Action (N, Decl);
5061 -- If we fall through, Pname is the name of the procedure to call
5063 Rewrite_Stream_Proc_Call (Pname);
5070 -- For enumeration types with a standard representation, Pos is
5071 -- handled by the back end.
5073 -- For enumeration types, with a non-standard representation we generate
5074 -- a call to the _Rep_To_Pos function created when the type was frozen.
5075 -- The call has the form
5077 -- _rep_to_pos (expr, flag)
5079 -- The parameter flag is True if range checks are enabled, causing
5080 -- Program_Error to be raised if the expression has an invalid
5081 -- representation, and False if range checks are suppressed.
5083 -- For integer types, Pos is equivalent to a simple integer
5084 -- conversion and we rewrite it as such
5086 when Attribute_Pos => Pos : declare
5087 Etyp : Entity_Id := Base_Type (Entity (Pref));
5090 -- Deal with zero/non-zero boolean values
5092 if Is_Boolean_Type (Etyp) then
5093 Adjust_Condition (First (Exprs));
5094 Etyp := Standard_Boolean;
5095 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
5098 -- Case of enumeration type
5100 if Is_Enumeration_Type (Etyp) then
5102 -- Non-standard enumeration type (generate call)
5104 if Present (Enum_Pos_To_Rep (Etyp)) then
5105 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
5108 Make_Function_Call (Loc,
5110 New_Occurrence_Of (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5111 Parameter_Associations => Exprs)));
5113 Analyze_And_Resolve (N, Typ);
5115 -- Standard enumeration type (do universal integer check)
5118 Apply_Universal_Integer_Attribute_Checks (N);
5121 -- Deal with integer types (replace by conversion)
5123 elsif Is_Integer_Type (Etyp) then
5124 Rewrite (N, Convert_To (Typ, First (Exprs)));
5125 Analyze_And_Resolve (N, Typ);
5134 -- We compute this if a component clause was present, otherwise we leave
5135 -- the computation up to the back end, since we don't know what layout
5138 when Attribute_Position => Position_Attr : declare
5139 CE : constant Entity_Id := Entity (Selector_Name (Pref));
5142 if Present (Component_Clause (CE)) then
5144 -- In Ada 2005 (or later) if we have the non-default bit order,
5145 -- then we return the original value as given in the component
5146 -- clause (RM 2005 13.5.2(2/2)).
5148 if Ada_Version >= Ada_2005
5149 and then Reverse_Bit_Order (Scope (CE))
5152 Make_Integer_Literal (Loc,
5153 Intval => Expr_Value (Position (Component_Clause (CE)))));
5155 -- Otherwise (Ada 83 or 95, or default bit order specified in
5156 -- later Ada version), return the normalized value.
5160 Make_Integer_Literal (Loc,
5161 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
5164 Analyze_And_Resolve (N, Typ);
5166 -- If back end is doing things, just apply universal integer checks
5169 Apply_Universal_Integer_Attribute_Checks (N);
5177 -- 1. Deal with enumeration types with holes.
5178 -- 2. For floating-point, generate call to attribute function.
5179 -- 3. For other cases, deal with constraint checking.
5181 when Attribute_Pred => Pred : declare
5182 Etyp : constant Entity_Id := Base_Type (Ptyp);
5186 -- For enumeration types with non-standard representations, we
5187 -- expand typ'Pred (x) into
5189 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
5191 -- If the representation is contiguous, we compute instead
5192 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
5193 -- The conversion function Enum_Pos_To_Rep is defined on the
5194 -- base type, not the subtype, so we have to use the base type
5195 -- explicitly for this and other enumeration attributes.
5197 if Is_Enumeration_Type (Ptyp)
5198 and then Present (Enum_Pos_To_Rep (Etyp))
5200 if Has_Contiguous_Rep (Etyp) then
5202 Unchecked_Convert_To (Ptyp,
5205 Make_Integer_Literal (Loc,
5206 Enumeration_Rep (First_Literal (Ptyp))),
5208 Make_Function_Call (Loc,
5211 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5213 Parameter_Associations =>
5215 Unchecked_Convert_To (Ptyp,
5216 Make_Op_Subtract (Loc,
5218 Unchecked_Convert_To (Standard_Integer,
5219 Relocate_Node (First (Exprs))),
5221 Make_Integer_Literal (Loc, 1))),
5222 Rep_To_Pos_Flag (Ptyp, Loc))))));
5225 -- Add Boolean parameter True, to request program errror if
5226 -- we have a bad representation on our hands. If checks are
5227 -- suppressed, then add False instead
5229 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
5231 Make_Indexed_Component (Loc,
5234 (Enum_Pos_To_Rep (Etyp), Loc),
5235 Expressions => New_List (
5236 Make_Op_Subtract (Loc,
5238 Make_Function_Call (Loc,
5241 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
5242 Parameter_Associations => Exprs),
5243 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
5246 Analyze_And_Resolve (N, Typ);
5248 -- For floating-point, we transform 'Pred into a call to the Pred
5249 -- floating-point attribute function in Fat_xxx (xxx is root type).
5250 -- Note that this function takes care of the overflow case.
5252 elsif Is_Floating_Point_Type (Ptyp) then
5253 Expand_Fpt_Attribute_R (N);
5254 Analyze_And_Resolve (N, Typ);
5256 -- For modular types, nothing to do (no overflow, since wraps)
5258 elsif Is_Modular_Integer_Type (Ptyp) then
5261 -- For other types, if argument is marked as needing a range check or
5262 -- overflow checking is enabled, we must generate a check.
5264 elsif not Overflow_Checks_Suppressed (Ptyp)
5265 or else Do_Range_Check (First (Exprs))
5267 Set_Do_Range_Check (First (Exprs), False);
5268 Expand_Pred_Succ_Attribute (N);
5276 -- Ada 2005 (AI-327): Dynamic ceiling priorities
5278 -- We rewrite X'Priority as the following run-time call:
5280 -- Get_Ceiling (X._Object)
5282 -- Note that although X'Priority is notionally an object, it is quite
5283 -- deliberately not defined as an aliased object in the RM. This means
5284 -- that it works fine to rewrite it as a call, without having to worry
5285 -- about complications that would other arise from X'Priority'Access,
5286 -- which is illegal, because of the lack of aliasing.
5288 when Attribute_Priority => Priority : declare
5290 Conctyp : Entity_Id;
5291 New_Itype : Entity_Id;
5292 Object_Parm : Node_Id;
5294 RT_Subprg_Name : Node_Id;
5297 -- Look for the enclosing concurrent type
5299 Conctyp := Current_Scope;
5300 while not Is_Concurrent_Type (Conctyp) loop
5301 Conctyp := Scope (Conctyp);
5304 pragma Assert (Is_Protected_Type (Conctyp));
5306 -- Generate the actual of the call
5308 Subprg := Current_Scope;
5309 while not Present (Protected_Body_Subprogram (Subprg)) loop
5310 Subprg := Scope (Subprg);
5313 -- Use of 'Priority inside protected entries and barriers (in both
5314 -- cases the type of the first formal of their expanded subprogram
5317 if Etype (First_Entity (Protected_Body_Subprogram (Subprg))) =
5320 -- In the expansion of protected entries the type of the first
5321 -- formal of the Protected_Body_Subprogram is an Address. In order
5322 -- to reference the _object component we generate:
5324 -- type T is access p__ptTV;
5327 New_Itype := Create_Itype (E_Access_Type, N);
5328 Set_Etype (New_Itype, New_Itype);
5329 Set_Directly_Designated_Type (New_Itype,
5330 Corresponding_Record_Type (Conctyp));
5331 Freeze_Itype (New_Itype, N);
5334 -- T!(O)._object'unchecked_access
5337 Make_Attribute_Reference (Loc,
5339 Make_Selected_Component (Loc,
5341 Unchecked_Convert_To (New_Itype,
5343 (First_Entity (Protected_Body_Subprogram (Subprg)),
5345 Selector_Name => Make_Identifier (Loc, Name_uObject)),
5346 Attribute_Name => Name_Unchecked_Access);
5348 -- Use of 'Priority inside a protected subprogram
5352 Make_Attribute_Reference (Loc,
5354 Make_Selected_Component (Loc,
5357 (First_Entity (Protected_Body_Subprogram (Subprg)),
5359 Selector_Name => Make_Identifier (Loc, Name_uObject)),
5360 Attribute_Name => Name_Unchecked_Access);
5363 -- Select the appropriate run-time subprogram
5365 if Number_Entries (Conctyp) = 0 then
5366 RT_Subprg_Name := New_Occurrence_Of (RTE (RE_Get_Ceiling), Loc);
5368 RT_Subprg_Name := New_Occurrence_Of (RTE (RO_PE_Get_Ceiling), Loc);
5372 Make_Function_Call (Loc,
5373 Name => RT_Subprg_Name,
5374 Parameter_Associations => New_List (Object_Parm));
5378 -- Avoid the generation of extra checks on the pointer to the
5379 -- protected object.
5381 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
5388 when Attribute_Range_Length =>
5390 -- The only special processing required is for the case where
5391 -- Range_Length is applied to an enumeration type with holes.
5392 -- In this case we transform
5398 -- X'Pos (X'Last) - X'Pos (X'First) + 1
5400 -- So that the result reflects the proper Pos values instead
5401 -- of the underlying representations.
5403 if Is_Enumeration_Type (Ptyp)
5404 and then Has_Non_Standard_Rep (Ptyp)
5409 Make_Op_Subtract (Loc,
5411 Make_Attribute_Reference (Loc,
5412 Attribute_Name => Name_Pos,
5413 Prefix => New_Occurrence_Of (Ptyp, Loc),
5414 Expressions => New_List (
5415 Make_Attribute_Reference (Loc,
5416 Attribute_Name => Name_Last,
5418 New_Occurrence_Of (Ptyp, Loc)))),
5421 Make_Attribute_Reference (Loc,
5422 Attribute_Name => Name_Pos,
5423 Prefix => New_Occurrence_Of (Ptyp, Loc),
5424 Expressions => New_List (
5425 Make_Attribute_Reference (Loc,
5426 Attribute_Name => Name_First,
5428 New_Occurrence_Of (Ptyp, Loc))))),
5430 Right_Opnd => Make_Integer_Literal (Loc, 1)));
5432 Analyze_And_Resolve (N, Typ);
5434 -- For all other cases, the attribute is handled by the back end, but
5435 -- we need to deal with the case of the range check on a universal
5439 Apply_Universal_Integer_Attribute_Checks (N);
5446 when Attribute_Read => Read : declare
5447 P_Type : constant Entity_Id := Entity (Pref);
5448 B_Type : constant Entity_Id := Base_Type (P_Type);
5449 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5459 -- If no underlying type, we have an error that will be diagnosed
5460 -- elsewhere, so here we just completely ignore the expansion.
5466 -- Stream operations can appear in user code even if the restriction
5467 -- No_Streams is active (for example, when instantiating a predefined
5468 -- container). In that case rewrite the attribute as a Raise to
5469 -- prevent any run-time use.
5471 if Restriction_Active (No_Streams) then
5473 Make_Raise_Program_Error (Sloc (N),
5474 Reason => PE_Stream_Operation_Not_Allowed));
5475 Set_Etype (N, B_Type);
5479 -- The simple case, if there is a TSS for Read, just call it
5481 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
5483 if Present (Pname) then
5487 -- If there is a Stream_Convert pragma, use it, we rewrite
5489 -- sourcetyp'Read (stream, Item)
5493 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
5495 -- where strmread is the given Read function that converts an
5496 -- argument of type strmtyp to type sourcetyp or a type from which
5497 -- it is derived. The conversion to sourcetyp is required in the
5500 -- A special case arises if Item is a type conversion in which
5501 -- case, we have to expand to:
5503 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
5505 -- where Itemx is the expression of the type conversion (i.e.
5506 -- the actual object), and typex is the type of Itemx.
5508 Prag := Get_Stream_Convert_Pragma (P_Type);
5510 if Present (Prag) then
5511 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
5512 Rfunc := Entity (Expression (Arg2));
5513 Lhs := Relocate_Node (Next (First (Exprs)));
5515 OK_Convert_To (B_Type,
5516 Make_Function_Call (Loc,
5517 Name => New_Occurrence_Of (Rfunc, Loc),
5518 Parameter_Associations => New_List (
5519 Make_Attribute_Reference (Loc,
5522 (Etype (First_Formal (Rfunc)), Loc),
5523 Attribute_Name => Name_Input,
5524 Expressions => New_List (
5525 Relocate_Node (First (Exprs)))))));
5527 if Nkind (Lhs) = N_Type_Conversion then
5528 Lhs := Expression (Lhs);
5529 Rhs := Convert_To (Etype (Lhs), Rhs);
5533 Make_Assignment_Statement (Loc,
5535 Expression => Rhs));
5536 Set_Assignment_OK (Lhs);
5540 -- For elementary types, we call the I_xxx routine using the first
5541 -- parameter and then assign the result into the second parameter.
5542 -- We set Assignment_OK to deal with the conversion case.
5544 elsif Is_Elementary_Type (U_Type) then
5550 Lhs := Relocate_Node (Next (First (Exprs)));
5551 Rhs := Build_Elementary_Input_Call (N);
5553 if Nkind (Lhs) = N_Type_Conversion then
5554 Lhs := Expression (Lhs);
5555 Rhs := Convert_To (Etype (Lhs), Rhs);
5558 Set_Assignment_OK (Lhs);
5561 Make_Assignment_Statement (Loc,
5563 Expression => Rhs));
5571 elsif Is_Array_Type (U_Type) then
5572 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
5573 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5575 -- Tagged type case, use the primitive Read function. Note that
5576 -- this will dispatch in the class-wide case which is what we want
5578 elsif Is_Tagged_Type (U_Type) then
5579 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
5581 -- All other record type cases, including protected records. The
5582 -- latter only arise for expander generated code for handling
5583 -- shared passive partition access.
5587 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5589 -- Ada 2005 (AI-216): Program_Error is raised when executing
5590 -- the default implementation of the Read attribute of an
5591 -- Unchecked_Union type. We replace the attribute with a
5592 -- raise statement (rather than inserting it before) to handle
5593 -- properly the case of an unchecked union that is a record
5596 if Is_Unchecked_Union (Base_Type (U_Type)) then
5598 Make_Raise_Program_Error (Loc,
5599 Reason => PE_Unchecked_Union_Restriction));
5600 Set_Etype (N, B_Type);
5604 if Has_Discriminants (U_Type)
5606 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5608 Build_Mutable_Record_Read_Procedure
5609 (Loc, Full_Base (U_Type), Decl, Pname);
5611 Build_Record_Read_Procedure
5612 (Loc, Full_Base (U_Type), Decl, Pname);
5615 -- Suppress checks, uninitialized or otherwise invalid
5616 -- data does not cause constraint errors to be raised for
5617 -- a complete record read.
5619 Insert_Action (N, Decl, All_Checks);
5623 Rewrite_Stream_Proc_Call (Pname);
5630 -- Ref is identical to To_Address, see To_Address for processing
5636 -- Transforms 'Remainder into a call to the floating-point attribute
5637 -- function Remainder in Fat_xxx (where xxx is the root type)
5639 when Attribute_Remainder =>
5640 Expand_Fpt_Attribute_RR (N);
5646 -- Transform 'Result into reference to _Result formal. At the point
5647 -- where a legal 'Result attribute is expanded, we know that we are in
5648 -- the context of a _Postcondition function with a _Result parameter.
5650 when Attribute_Result =>
5651 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
5652 Analyze_And_Resolve (N, Typ);
5658 -- The handling of the Round attribute is quite delicate. The processing
5659 -- in Sem_Attr introduced a conversion to universal real, reflecting the
5660 -- semantics of Round, but we do not want anything to do with universal
5661 -- real at runtime, since this corresponds to using floating-point
5664 -- What we have now is that the Etype of the Round attribute correctly
5665 -- indicates the final result type. The operand of the Round is the
5666 -- conversion to universal real, described above, and the operand of
5667 -- this conversion is the actual operand of Round, which may be the
5668 -- special case of a fixed point multiplication or division (Etype =
5671 -- The exapander will expand first the operand of the conversion, then
5672 -- the conversion, and finally the round attribute itself, since we
5673 -- always work inside out. But we cannot simply process naively in this
5674 -- order. In the semantic world where universal fixed and real really
5675 -- exist and have infinite precision, there is no problem, but in the
5676 -- implementation world, where universal real is a floating-point type,
5677 -- we would get the wrong result.
5679 -- So the approach is as follows. First, when expanding a multiply or
5680 -- divide whose type is universal fixed, we do nothing at all, instead
5681 -- deferring the operation till later.
5683 -- The actual processing is done in Expand_N_Type_Conversion which
5684 -- handles the special case of Round by looking at its parent to see if
5685 -- it is a Round attribute, and if it is, handling the conversion (or
5686 -- its fixed multiply/divide child) in an appropriate manner.
5688 -- This means that by the time we get to expanding the Round attribute
5689 -- itself, the Round is nothing more than a type conversion (and will
5690 -- often be a null type conversion), so we just replace it with the
5691 -- appropriate conversion operation.
5693 when Attribute_Round =>
5695 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
5696 Analyze_And_Resolve (N);
5702 -- Transforms 'Rounding into a call to the floating-point attribute
5703 -- function Rounding in Fat_xxx (where xxx is the root type)
5704 -- Expansion is avoided for cases the back end can handle directly.
5706 when Attribute_Rounding =>
5707 if not Is_Inline_Floating_Point_Attribute (N) then
5708 Expand_Fpt_Attribute_R (N);
5715 -- Transforms 'Scaling into a call to the floating-point attribute
5716 -- function Scaling in Fat_xxx (where xxx is the root type)
5718 when Attribute_Scaling =>
5719 Expand_Fpt_Attribute_RI (N);
5721 -------------------------
5722 -- Simple_Storage_Pool --
5723 -------------------------
5725 when Attribute_Simple_Storage_Pool =>
5727 Make_Type_Conversion (Loc,
5728 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
5729 Expression => New_Occurrence_Of (Entity (N), Loc)));
5730 Analyze_And_Resolve (N, Typ);
5736 when Attribute_Object_Size
5738 | Attribute_Value_Size
5739 | Attribute_VADS_Size
5745 -- Processing for VADS_Size case. Note that this processing
5746 -- removes all traces of VADS_Size from the tree, and completes
5747 -- all required processing for VADS_Size by translating the
5748 -- attribute reference to an appropriate Size or Object_Size
5751 if Id = Attribute_VADS_Size
5752 or else (Use_VADS_Size and then Id = Attribute_Size)
5754 -- If the size is specified, then we simply use the specified
5755 -- size. This applies to both types and objects. The size of an
5756 -- object can be specified in the following ways:
5758 -- An explicit size object is given for an object
5759 -- A component size is specified for an indexed component
5760 -- A component clause is specified for a selected component
5761 -- The object is a component of a packed composite object
5763 -- If the size is specified, then VADS_Size of an object
5765 if (Is_Entity_Name (Pref)
5766 and then Present (Size_Clause (Entity (Pref))))
5768 (Nkind (Pref) = N_Component_Clause
5769 and then (Present (Component_Clause
5770 (Entity (Selector_Name (Pref))))
5771 or else Is_Packed (Etype (Prefix (Pref)))))
5773 (Nkind (Pref) = N_Indexed_Component
5774 and then (Component_Size (Etype (Prefix (Pref))) /= 0
5775 or else Is_Packed (Etype (Prefix (Pref)))))
5777 Set_Attribute_Name (N, Name_Size);
5779 -- Otherwise if we have an object rather than a type, then
5780 -- the VADS_Size attribute applies to the type of the object,
5781 -- rather than the object itself. This is one of the respects
5782 -- in which VADS_Size differs from Size.
5785 if (not Is_Entity_Name (Pref)
5786 or else not Is_Type (Entity (Pref)))
5787 and then (Is_Scalar_Type (Ptyp)
5788 or else Is_Constrained (Ptyp))
5790 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
5793 -- For a scalar type for which no size was explicitly given,
5794 -- VADS_Size means Object_Size. This is the other respect in
5795 -- which VADS_Size differs from Size.
5797 if Is_Scalar_Type (Ptyp)
5798 and then No (Size_Clause (Ptyp))
5800 Set_Attribute_Name (N, Name_Object_Size);
5802 -- In all other cases, Size and VADS_Size are the sane
5805 Set_Attribute_Name (N, Name_Size);
5810 -- If the prefix is X'Class, transform it into a direct reference
5811 -- to the class-wide type, because the back end must not see a
5812 -- 'Class reference.
5814 if Is_Entity_Name (Pref)
5815 and then Is_Class_Wide_Type (Entity (Pref))
5817 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
5820 -- For X'Size applied to an object of a class-wide type, transform
5821 -- X'Size into a call to the primitive operation _Size applied to
5824 elsif Is_Class_Wide_Type (Ptyp) then
5826 -- No need to do anything else compiling under restriction
5827 -- No_Dispatching_Calls. During the semantic analysis we
5828 -- already noted this restriction violation.
5830 if Restriction_Active (No_Dispatching_Calls) then
5835 Make_Function_Call (Loc,
5837 New_Occurrence_Of (Find_Prim_Op (Ptyp, Name_uSize), Loc),
5838 Parameter_Associations => New_List (Pref));
5840 if Typ /= Standard_Long_Long_Integer then
5842 -- The context is a specific integer type with which the
5843 -- original attribute was compatible. The function has a
5844 -- specific type as well, so to preserve the compatibility
5845 -- we must convert explicitly.
5847 New_Node := Convert_To (Typ, New_Node);
5850 Rewrite (N, New_Node);
5851 Analyze_And_Resolve (N, Typ);
5855 -- Call Expand_Size_Attribute to do the final part of the
5856 -- expansion which is shared with GNATprove expansion.
5858 Expand_Size_Attribute (N);
5865 when Attribute_Storage_Pool =>
5867 Make_Type_Conversion (Loc,
5868 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
5869 Expression => New_Occurrence_Of (Entity (N), Loc)));
5870 Analyze_And_Resolve (N, Typ);
5876 when Attribute_Storage_Size => Storage_Size : declare
5877 Alloc_Op : Entity_Id := Empty;
5881 -- Access type case, always go to the root type
5883 -- The case of access types results in a value of zero for the case
5884 -- where no storage size attribute clause has been given. If a
5885 -- storage size has been given, then the attribute is converted
5886 -- to a reference to the variable used to hold this value.
5888 if Is_Access_Type (Ptyp) then
5889 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
5891 Make_Attribute_Reference (Loc,
5892 Prefix => New_Occurrence_Of (Typ, Loc),
5893 Attribute_Name => Name_Max,
5894 Expressions => New_List (
5895 Make_Integer_Literal (Loc, 0),
5898 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
5900 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
5902 -- If the access type is associated with a simple storage pool
5903 -- object, then attempt to locate the optional Storage_Size
5904 -- function of the simple storage pool type. If not found,
5905 -- then the result will default to zero.
5907 if Present (Get_Rep_Pragma (Root_Type (Ptyp),
5908 Name_Simple_Storage_Pool_Type))
5911 Pool_Type : constant Entity_Id :=
5912 Base_Type (Etype (Entity (N)));
5915 Alloc_Op := Get_Name_Entity_Id (Name_Storage_Size);
5916 while Present (Alloc_Op) loop
5917 if Scope (Alloc_Op) = Scope (Pool_Type)
5918 and then Present (First_Formal (Alloc_Op))
5919 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
5924 Alloc_Op := Homonym (Alloc_Op);
5928 -- In the normal Storage_Pool case, retrieve the primitive
5929 -- function associated with the pool type.
5934 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
5935 Attribute_Name (N));
5938 -- If Storage_Size wasn't found (can only occur in the simple
5939 -- storage pool case), then simply use zero for the result.
5941 if not Present (Alloc_Op) then
5942 Rewrite (N, Make_Integer_Literal (Loc, 0));
5944 -- Otherwise, rewrite the allocator as a call to pool type's
5945 -- Storage_Size function.
5950 Make_Function_Call (Loc,
5952 New_Occurrence_Of (Alloc_Op, Loc),
5954 Parameter_Associations => New_List (
5956 (Associated_Storage_Pool
5957 (Root_Type (Ptyp)), Loc)))));
5961 Rewrite (N, Make_Integer_Literal (Loc, 0));
5964 Analyze_And_Resolve (N, Typ);
5966 -- For tasks, we retrieve the size directly from the TCB. The
5967 -- size may depend on a discriminant of the type, and therefore
5968 -- can be a per-object expression, so type-level information is
5969 -- not sufficient in general. There are four cases to consider:
5971 -- a) If the attribute appears within a task body, the designated
5972 -- TCB is obtained by a call to Self.
5974 -- b) If the prefix of the attribute is the name of a task object,
5975 -- the designated TCB is the one stored in the corresponding record.
5977 -- c) If the prefix is a task type, the size is obtained from the
5978 -- size variable created for each task type
5980 -- d) If no Storage_Size was specified for the type, there is no
5981 -- size variable, and the value is a system-specific default.
5984 if In_Open_Scopes (Ptyp) then
5986 -- Storage_Size (Self)
5990 Make_Function_Call (Loc,
5992 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
5993 Parameter_Associations =>
5995 Make_Function_Call (Loc,
5997 New_Occurrence_Of (RTE (RE_Self), Loc))))));
5999 elsif not Is_Entity_Name (Pref)
6000 or else not Is_Type (Entity (Pref))
6002 -- Storage_Size (Rec (Obj).Size)
6006 Make_Function_Call (Loc,
6008 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
6009 Parameter_Associations =>
6011 Make_Selected_Component (Loc,
6013 Unchecked_Convert_To (
6014 Corresponding_Record_Type (Ptyp),
6015 New_Copy_Tree (Pref)),
6017 Make_Identifier (Loc, Name_uTask_Id))))));
6019 elsif Present (Storage_Size_Variable (Ptyp)) then
6021 -- Static Storage_Size pragma given for type: retrieve value
6022 -- from its allocated storage variable.
6026 Make_Function_Call (Loc,
6027 Name => New_Occurrence_Of (
6028 RTE (RE_Adjust_Storage_Size), Loc),
6029 Parameter_Associations =>
6032 Storage_Size_Variable (Ptyp), Loc)))));
6034 -- Get system default
6038 Make_Function_Call (Loc,
6041 RTE (RE_Default_Stack_Size), Loc))));
6044 Analyze_And_Resolve (N, Typ);
6052 when Attribute_Stream_Size =>
6054 Make_Integer_Literal (Loc, Intval => Get_Stream_Size (Ptyp)));
6055 Analyze_And_Resolve (N, Typ);
6061 -- 1. Deal with enumeration types with holes.
6062 -- 2. For floating-point, generate call to attribute function.
6063 -- 3. For other cases, deal with constraint checking.
6065 when Attribute_Succ => Succ : declare
6066 Etyp : constant Entity_Id := Base_Type (Ptyp);
6069 -- For enumeration types with non-standard representations, we
6070 -- expand typ'Succ (x) into
6072 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
6074 -- If the representation is contiguous, we compute instead
6075 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
6077 if Is_Enumeration_Type (Ptyp)
6078 and then Present (Enum_Pos_To_Rep (Etyp))
6080 if Has_Contiguous_Rep (Etyp) then
6082 Unchecked_Convert_To (Ptyp,
6085 Make_Integer_Literal (Loc,
6086 Enumeration_Rep (First_Literal (Ptyp))),
6088 Make_Function_Call (Loc,
6091 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6093 Parameter_Associations =>
6095 Unchecked_Convert_To (Ptyp,
6098 Unchecked_Convert_To (Standard_Integer,
6099 Relocate_Node (First (Exprs))),
6101 Make_Integer_Literal (Loc, 1))),
6102 Rep_To_Pos_Flag (Ptyp, Loc))))));
6104 -- Add Boolean parameter True, to request program errror if
6105 -- we have a bad representation on our hands. Add False if
6106 -- checks are suppressed.
6108 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
6110 Make_Indexed_Component (Loc,
6113 (Enum_Pos_To_Rep (Etyp), Loc),
6114 Expressions => New_List (
6117 Make_Function_Call (Loc,
6120 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6121 Parameter_Associations => Exprs),
6122 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
6125 Analyze_And_Resolve (N, Typ);
6127 -- For floating-point, we transform 'Succ into a call to the Succ
6128 -- floating-point attribute function in Fat_xxx (xxx is root type)
6130 elsif Is_Floating_Point_Type (Ptyp) then
6131 Expand_Fpt_Attribute_R (N);
6132 Analyze_And_Resolve (N, Typ);
6134 -- For modular types, nothing to do (no overflow, since wraps)
6136 elsif Is_Modular_Integer_Type (Ptyp) then
6139 -- For other types, if argument is marked as needing a range check or
6140 -- overflow checking is enabled, we must generate a check.
6142 elsif not Overflow_Checks_Suppressed (Ptyp)
6143 or else Do_Range_Check (First (Exprs))
6145 Set_Do_Range_Check (First (Exprs), False);
6146 Expand_Pred_Succ_Attribute (N);
6154 -- Transforms X'Tag into a direct reference to the tag of X
6156 when Attribute_Tag => Tag : declare
6158 Prefix_Is_Type : Boolean;
6161 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
6162 Ttyp := Entity (Pref);
6163 Prefix_Is_Type := True;
6166 Prefix_Is_Type := False;
6169 if Is_Class_Wide_Type (Ttyp) then
6170 Ttyp := Root_Type (Ttyp);
6173 Ttyp := Underlying_Type (Ttyp);
6175 -- Ada 2005: The type may be a synchronized tagged type, in which
6176 -- case the tag information is stored in the corresponding record.
6178 if Is_Concurrent_Type (Ttyp) then
6179 Ttyp := Corresponding_Record_Type (Ttyp);
6182 if Prefix_Is_Type then
6184 -- For VMs we leave the type attribute unexpanded because
6185 -- there's not a dispatching table to reference.
6187 if Tagged_Type_Expansion then
6189 Unchecked_Convert_To (RTE (RE_Tag),
6191 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
6192 Analyze_And_Resolve (N, RTE (RE_Tag));
6195 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
6196 -- references the primary tag of the actual object. If 'Tag is
6197 -- applied to class-wide interface objects we generate code that
6198 -- displaces "this" to reference the base of the object.
6200 elsif Comes_From_Source (N)
6201 and then Is_Class_Wide_Type (Etype (Prefix (N)))
6202 and then Is_Interface (Underlying_Type (Etype (Prefix (N))))
6205 -- (To_Tag_Ptr (Prefix'Address)).all
6207 -- Note that Prefix'Address is recursively expanded into a call
6208 -- to Base_Address (Obj.Tag)
6210 -- Not needed for VM targets, since all handled by the VM
6212 if Tagged_Type_Expansion then
6214 Make_Explicit_Dereference (Loc,
6215 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6216 Make_Attribute_Reference (Loc,
6217 Prefix => Relocate_Node (Pref),
6218 Attribute_Name => Name_Address))));
6219 Analyze_And_Resolve (N, RTE (RE_Tag));
6224 Make_Selected_Component (Loc,
6225 Prefix => Relocate_Node (Pref),
6227 New_Occurrence_Of (First_Tag_Component (Ttyp), Loc)));
6228 Analyze_And_Resolve (N, RTE (RE_Tag));
6236 -- Transforms 'Terminated attribute into a call to Terminated function
6238 when Attribute_Terminated => Terminated : begin
6240 -- The prefix of Terminated is of a task interface class-wide type.
6242 -- terminated (Task_Id (_disp_get_task_id (Pref)));
6244 if Ada_Version >= Ada_2005
6245 and then Ekind (Ptyp) = E_Class_Wide_Type
6246 and then Is_Interface (Ptyp)
6247 and then Is_Task_Interface (Ptyp)
6250 Make_Function_Call (Loc,
6252 New_Occurrence_Of (RTE (RE_Terminated), Loc),
6253 Parameter_Associations => New_List (
6254 Make_Unchecked_Type_Conversion (Loc,
6256 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6257 Expression => Build_Disp_Get_Task_Id_Call (Pref)))));
6259 elsif Restricted_Profile then
6261 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
6265 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
6268 Analyze_And_Resolve (N, Standard_Boolean);
6275 -- Transforms System'To_Address (X) and System.Address'Ref (X) into
6276 -- unchecked conversion from (integral) type of X to type address. If
6277 -- the To_Address is a static expression, the transformed expression
6278 -- also needs to be static, because we do some legality checks (e.g.
6279 -- for Thread_Local_Storage) after this transformation.
6282 | Attribute_To_Address
6284 To_Address : declare
6285 Is_Static : constant Boolean := Is_Static_Expression (N);
6289 Unchecked_Convert_To (RTE (RE_Address),
6290 Relocate_Node (First (Exprs))));
6291 Set_Is_Static_Expression (N, Is_Static);
6293 Analyze_And_Resolve (N, RTE (RE_Address));
6300 when Attribute_To_Any => To_Any : declare
6301 P_Type : constant Entity_Id := Etype (Pref);
6302 Decls : constant List_Id := New_List;
6308 Relocate_Node (First (Exprs))), Decls));
6309 Insert_Actions (N, Decls);
6310 Analyze_And_Resolve (N, RTE (RE_Any));
6317 -- Transforms 'Truncation into a call to the floating-point attribute
6318 -- function Truncation in Fat_xxx (where xxx is the root type).
6319 -- Expansion is avoided for cases the back end can handle directly.
6321 when Attribute_Truncation =>
6322 if not Is_Inline_Floating_Point_Attribute (N) then
6323 Expand_Fpt_Attribute_R (N);
6330 when Attribute_TypeCode => TypeCode : declare
6331 P_Type : constant Entity_Id := Etype (Pref);
6332 Decls : constant List_Id := New_List;
6334 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
6335 Insert_Actions (N, Decls);
6336 Analyze_And_Resolve (N, RTE (RE_TypeCode));
6339 -----------------------
6340 -- Unbiased_Rounding --
6341 -----------------------
6343 -- Transforms 'Unbiased_Rounding into a call to the floating-point
6344 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
6345 -- root type). Expansion is avoided for cases the back end can handle
6348 when Attribute_Unbiased_Rounding =>
6349 if not Is_Inline_Floating_Point_Attribute (N) then
6350 Expand_Fpt_Attribute_R (N);
6357 when Attribute_Update =>
6358 Expand_Update_Attribute (N);
6364 -- The processing for VADS_Size is shared with Size
6370 -- For enumeration types with a standard representation, and for all
6371 -- other types, Val is handled by the back end. For enumeration types
6372 -- with a non-standard representation we use the _Pos_To_Rep array that
6373 -- was created when the type was frozen.
6375 when Attribute_Val => Val : declare
6376 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
6379 if Is_Enumeration_Type (Etyp)
6380 and then Present (Enum_Pos_To_Rep (Etyp))
6382 if Has_Contiguous_Rep (Etyp) then
6384 Rep_Node : constant Node_Id :=
6385 Unchecked_Convert_To (Etyp,
6388 Make_Integer_Literal (Loc,
6389 Enumeration_Rep (First_Literal (Etyp))),
6391 (Convert_To (Standard_Integer,
6392 Relocate_Node (First (Exprs))))));
6396 Unchecked_Convert_To (Etyp,
6399 Make_Integer_Literal (Loc,
6400 Enumeration_Rep (First_Literal (Etyp))),
6402 Make_Function_Call (Loc,
6405 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
6406 Parameter_Associations => New_List (
6408 Rep_To_Pos_Flag (Etyp, Loc))))));
6413 Make_Indexed_Component (Loc,
6414 Prefix => New_Occurrence_Of (Enum_Pos_To_Rep (Etyp), Loc),
6415 Expressions => New_List (
6416 Convert_To (Standard_Integer,
6417 Relocate_Node (First (Exprs))))));
6420 Analyze_And_Resolve (N, Typ);
6422 -- If the argument is marked as requiring a range check then generate
6425 elsif Do_Range_Check (First (Exprs)) then
6426 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
6434 -- The code for valid is dependent on the particular types involved.
6435 -- See separate sections below for the generated code in each case.
6437 when Attribute_Valid => Valid : declare
6438 PBtyp : Entity_Id := Base_Type (Ptyp);
6440 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
6441 -- Save the validity checking mode. We always turn off validity
6442 -- checking during process of 'Valid since this is one place
6443 -- where we do not want the implicit validity checks to interfere
6444 -- with the explicit validity check that the programmer is doing.
6446 function Make_Range_Test return Node_Id;
6447 -- Build the code for a range test of the form
6448 -- PBtyp!(Pref) in PBtyp!(Ptyp'First) .. PBtyp!(Ptyp'Last)
6450 ---------------------
6451 -- Make_Range_Test --
6452 ---------------------
6454 function Make_Range_Test return Node_Id is
6458 -- The prefix of attribute 'Valid should always denote an object
6459 -- reference. The reference is either coming directly from source
6460 -- or is produced by validity check expansion. The object may be
6461 -- wrapped in a conversion in which case the call to Unqual_Conv
6464 -- If the prefix denotes a variable which captures the value of
6465 -- an object for validation purposes, use the variable in the
6466 -- range test. This ensures that no extra copies or extra reads
6467 -- are produced as part of the test. Generate:
6469 -- Temp : ... := Object;
6470 -- if not Temp in ... then
6472 if Is_Validation_Variable_Reference (Pref) then
6473 Temp := New_Occurrence_Of (Entity (Unqual_Conv (Pref)), Loc);
6475 -- Otherwise the prefix is either a source object or a constant
6476 -- produced by validity check expansion. Generate:
6478 -- Temp : constant ... := Pref;
6479 -- if not Temp in ... then
6482 Temp := Duplicate_Subexpr (Pref);
6487 Left_Opnd => Unchecked_Convert_To (PBtyp, Temp),
6491 Unchecked_Convert_To (PBtyp,
6492 Make_Attribute_Reference (Loc,
6493 Prefix => New_Occurrence_Of (Ptyp, Loc),
6494 Attribute_Name => Name_First)),
6496 Unchecked_Convert_To (PBtyp,
6497 Make_Attribute_Reference (Loc,
6498 Prefix => New_Occurrence_Of (Ptyp, Loc),
6499 Attribute_Name => Name_Last))));
6500 end Make_Range_Test;
6506 -- Start of processing for Attribute_Valid
6509 -- Do not expand sourced code 'Valid reference in CodePeer mode,
6510 -- will be handled by the back-end directly.
6512 if CodePeer_Mode and then Comes_From_Source (N) then
6516 -- Turn off validity checks. We do not want any implicit validity
6517 -- checks to intefere with the explicit check from the attribute
6519 Validity_Checks_On := False;
6521 -- Retrieve the base type. Handle the case where the base type is a
6522 -- private enumeration type.
6524 if Is_Private_Type (PBtyp) and then Present (Full_View (PBtyp)) then
6525 PBtyp := Full_View (PBtyp);
6528 -- Floating-point case. This case is handled by the Valid attribute
6529 -- code in the floating-point attribute run-time library.
6531 if Is_Floating_Point_Type (Ptyp) then
6532 Float_Valid : declare
6536 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id;
6537 -- Return entity for Pkg.Nam
6539 --------------------
6540 -- Get_Fat_Entity --
6541 --------------------
6543 function Get_Fat_Entity (Nam : Name_Id) return Entity_Id is
6544 Exp_Name : constant Node_Id :=
6545 Make_Selected_Component (Loc,
6546 Prefix => New_Occurrence_Of (RTE (Pkg), Loc),
6547 Selector_Name => Make_Identifier (Loc, Nam));
6549 Find_Selected_Component (Exp_Name);
6550 return Entity (Exp_Name);
6553 -- Start of processing for Float_Valid
6556 -- The C and AAMP back-ends handle Valid for fpt types
6558 if Modify_Tree_For_C or else Float_Rep (PBtyp) = AAMP then
6559 Analyze_And_Resolve (Pref, Ptyp);
6560 Set_Etype (N, Standard_Boolean);
6564 Find_Fat_Info (Ptyp, Ftp, Pkg);
6566 -- If the prefix is a reverse SSO component, or is possibly
6567 -- unaligned, first create a temporary copy that is in
6568 -- native SSO, and properly aligned. Make it Volatile to
6569 -- prevent folding in the back-end. Note that we use an
6570 -- intermediate constrained string type to initialize the
6571 -- temporary, as the value at hand might be invalid, and in
6572 -- that case it cannot be copied using a floating point
6575 if In_Reverse_Storage_Order_Object (Pref)
6576 or else Is_Possibly_Unaligned_Object (Pref)
6579 Temp : constant Entity_Id :=
6580 Make_Temporary (Loc, 'F');
6582 Fat_S : constant Entity_Id :=
6583 Get_Fat_Entity (Name_S);
6584 -- Constrained string subtype of appropriate size
6586 Fat_P : constant Entity_Id :=
6587 Get_Fat_Entity (Name_P);
6590 Decl : constant Node_Id :=
6591 Make_Object_Declaration (Loc,
6592 Defining_Identifier => Temp,
6593 Aliased_Present => True,
6594 Object_Definition =>
6595 New_Occurrence_Of (Ptyp, Loc));
6598 Set_Aspect_Specifications (Decl, New_List (
6599 Make_Aspect_Specification (Loc,
6601 Make_Identifier (Loc, Name_Volatile))));
6607 Make_Assignment_Statement (Loc,
6609 Make_Explicit_Dereference (Loc,
6611 Unchecked_Convert_To (Fat_P,
6612 Make_Attribute_Reference (Loc,
6614 New_Occurrence_Of (Temp, Loc),
6616 Name_Unrestricted_Access))),
6618 Unchecked_Convert_To (Fat_S,
6619 Relocate_Node (Pref)))),
6621 Suppress => All_Checks);
6623 Rewrite (Pref, New_Occurrence_Of (Temp, Loc));
6627 -- We now have an object of the proper endianness and
6628 -- alignment, and can construct a Valid attribute.
6630 -- We make sure the prefix of this valid attribute is
6631 -- marked as not coming from source, to avoid losing
6632 -- warnings from 'Valid looking like a possible update.
6634 Set_Comes_From_Source (Pref, False);
6636 Expand_Fpt_Attribute
6637 (N, Pkg, Name_Valid,
6639 Make_Attribute_Reference (Loc,
6640 Prefix => Unchecked_Convert_To (Ftp, Pref),
6641 Attribute_Name => Name_Unrestricted_Access)));
6644 -- One more task, we still need a range check. Required
6645 -- only if we have a constraint, since the Valid routine
6646 -- catches infinities properly (infinities are never valid).
6648 -- The way we do the range check is simply to create the
6649 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
6651 if not Subtypes_Statically_Match (Ptyp, PBtyp) then
6654 Left_Opnd => Relocate_Node (N),
6657 Left_Opnd => Convert_To (PBtyp, Pref),
6658 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
6662 -- Enumeration type with holes
6664 -- For enumeration types with holes, the Pos value constructed by
6665 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
6666 -- second argument of False returns minus one for an invalid value,
6667 -- and the non-negative pos value for a valid value, so the
6668 -- expansion of X'Valid is simply:
6670 -- type(X)'Pos (X) >= 0
6672 -- We can't quite generate it that way because of the requirement
6673 -- for the non-standard second argument of False in the resulting
6674 -- rep_to_pos call, so we have to explicitly create:
6676 -- _rep_to_pos (X, False) >= 0
6678 -- If we have an enumeration subtype, we also check that the
6679 -- value is in range:
6681 -- _rep_to_pos (X, False) >= 0
6683 -- (X >= type(X)'First and then type(X)'Last <= X)
6685 elsif Is_Enumeration_Type (Ptyp)
6686 and then Present (Enum_Pos_To_Rep (PBtyp))
6691 Make_Function_Call (Loc,
6693 New_Occurrence_Of (TSS (PBtyp, TSS_Rep_To_Pos), Loc),
6694 Parameter_Associations => New_List (
6696 New_Occurrence_Of (Standard_False, Loc))),
6697 Right_Opnd => Make_Integer_Literal (Loc, 0));
6701 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (PBtyp)
6703 Type_High_Bound (Ptyp) /= Type_High_Bound (PBtyp))
6705 -- The call to Make_Range_Test will create declarations
6706 -- that need a proper insertion point, but Pref is now
6707 -- attached to a node with no ancestor. Attach to tree
6708 -- even if it is to be rewritten below.
6710 Set_Parent (Tst, Parent (N));
6714 Left_Opnd => Make_Range_Test,
6720 -- Fortran convention booleans
6722 -- For the very special case of Fortran convention booleans, the
6723 -- value is always valid, since it is an integer with the semantics
6724 -- that non-zero is true, and any value is permissible.
6726 elsif Is_Boolean_Type (Ptyp)
6727 and then Convention (Ptyp) = Convention_Fortran
6729 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6731 -- For biased representations, we will be doing an unchecked
6732 -- conversion without unbiasing the result. That means that the range
6733 -- test has to take this into account, and the proper form of the
6736 -- PBtyp!(Pref) < PBtyp!(Ptyp'Range_Length)
6738 elsif Has_Biased_Representation (Ptyp) then
6739 PBtyp := RTE (RE_Unsigned_32);
6743 Unchecked_Convert_To (PBtyp, Duplicate_Subexpr (Pref)),
6745 Unchecked_Convert_To (PBtyp,
6746 Make_Attribute_Reference (Loc,
6747 Prefix => New_Occurrence_Of (Ptyp, Loc),
6748 Attribute_Name => Name_Range_Length))));
6750 -- For all other scalar types, what we want logically is a
6753 -- X in type(X)'First .. type(X)'Last
6755 -- But that's precisely what won't work because of possible
6756 -- unwanted optimization (and indeed the basic motivation for
6757 -- the Valid attribute is exactly that this test does not work).
6758 -- What will work is:
6760 -- PBtyp!(X) >= PBtyp!(type(X)'First)
6762 -- PBtyp!(X) <= PBtyp!(type(X)'Last)
6764 -- where PBtyp is an integer type large enough to cover the full
6765 -- range of possible stored values (i.e. it is chosen on the basis
6766 -- of the size of the type, not the range of the values). We write
6767 -- this as two tests, rather than a range check, so that static
6768 -- evaluation will easily remove either or both of the checks if
6769 -- they can be -statically determined to be true (this happens
6770 -- when the type of X is static and the range extends to the full
6771 -- range of stored values).
6773 -- Unsigned types. Note: it is safe to consider only whether the
6774 -- subtype is unsigned, since we will in that case be doing all
6775 -- unsigned comparisons based on the subtype range. Since we use the
6776 -- actual subtype object size, this is appropriate.
6778 -- For example, if we have
6780 -- subtype x is integer range 1 .. 200;
6781 -- for x'Object_Size use 8;
6783 -- Now the base type is signed, but objects of this type are bits
6784 -- unsigned, and doing an unsigned test of the range 1 to 200 is
6785 -- correct, even though a value greater than 127 looks signed to a
6786 -- signed comparison.
6788 elsif Is_Unsigned_Type (Ptyp)
6789 or else (Is_Private_Type (Ptyp) and then Is_Unsigned_Type (Btyp))
6791 if Esize (Ptyp) <= 32 then
6792 PBtyp := RTE (RE_Unsigned_32);
6794 PBtyp := RTE (RE_Unsigned_64);
6797 Rewrite (N, Make_Range_Test);
6802 if Esize (Ptyp) <= Esize (Standard_Integer) then
6803 PBtyp := Standard_Integer;
6805 PBtyp := Universal_Integer;
6808 Rewrite (N, Make_Range_Test);
6811 -- If a predicate is present, then we do the predicate test, even if
6812 -- within the predicate function (infinite recursion is warned about
6813 -- in Sem_Attr in that case).
6816 Pred_Func : constant Entity_Id := Predicate_Function (Ptyp);
6819 if Present (Pred_Func) then
6822 Left_Opnd => Relocate_Node (N),
6823 Right_Opnd => Make_Predicate_Call (Ptyp, Pref)));
6827 Analyze_And_Resolve (N, Standard_Boolean);
6828 Validity_Checks_On := Save_Validity_Checks_On;
6835 when Attribute_Valid_Scalars => Valid_Scalars : declare
6836 Val_Typ : constant Entity_Id := Validated_View (Ptyp);
6837 Comp_Typ : Entity_Id;
6841 -- Assume that the prefix does not need validation
6845 -- Attribute 'Valid_Scalars is not supported on private tagged types
6847 if Is_Private_Type (Ptyp) and then Is_Tagged_Type (Ptyp) then
6850 -- Attribute 'Valid_Scalars evaluates to True when the type lacks
6853 elsif not Scalar_Part_Present (Val_Typ) then
6856 -- Attribute 'Valid_Scalars is the same as attribute 'Valid when the
6857 -- validated type is a scalar type. Generate:
6859 -- Val_Typ (Pref)'Valid
6861 elsif Is_Scalar_Type (Val_Typ) then
6863 Make_Attribute_Reference (Loc,
6865 Unchecked_Convert_To (Val_Typ, New_Copy_Tree (Pref)),
6866 Attribute_Name => Name_Valid);
6868 -- Validate the scalar components of an array by iterating over all
6869 -- dimensions of the array while checking individual components.
6871 elsif Is_Array_Type (Val_Typ) then
6872 Comp_Typ := Validated_View (Component_Type (Val_Typ));
6874 if Scalar_Part_Present (Comp_Typ) then
6876 Make_Function_Call (Loc,
6879 (Build_Array_VS_Func
6882 Array_Typ => Val_Typ,
6883 Comp_Typ => Comp_Typ),
6885 Parameter_Associations => New_List (Pref));
6888 -- Validate the scalar components, discriminants of a record type by
6889 -- examining the structure of a record type.
6891 elsif Is_Record_Type (Val_Typ) then
6893 Make_Function_Call (Loc,
6896 (Build_Record_VS_Func
6899 Rec_Typ => Val_Typ),
6901 Parameter_Associations => New_List (Pref));
6904 -- Default the attribute to True when the type of the prefix does not
6908 Expr := New_Occurrence_Of (Standard_True, Loc);
6912 Analyze_And_Resolve (N, Standard_Boolean);
6913 Set_Is_Static_Expression (N, False);
6920 -- Value attribute is handled in separate unit Exp_Imgv
6922 when Attribute_Value =>
6923 Exp_Imgv.Expand_Value_Attribute (N);
6929 -- The processing for Value_Size shares the processing for Size
6935 -- The processing for Version shares the processing for Body_Version
6941 -- Wide_Image attribute is handled in separate unit Exp_Imgv
6943 when Attribute_Wide_Image =>
6944 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
6945 -- back-end knows how to handle this attribute directly.
6947 if CodePeer_Mode then
6951 Exp_Imgv.Expand_Wide_Image_Attribute (N);
6953 ---------------------
6954 -- Wide_Wide_Image --
6955 ---------------------
6957 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
6959 when Attribute_Wide_Wide_Image =>
6960 -- Leave attribute unexpanded in CodePeer mode: the gnat2scil
6961 -- back-end knows how to handle this attribute directly.
6963 if CodePeer_Mode then
6967 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
6973 -- We expand typ'Wide_Value (X) into
6976 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
6978 -- Wide_String_To_String is a runtime function that converts its wide
6979 -- string argument to String, converting any non-translatable characters
6980 -- into appropriate escape sequences. This preserves the required
6981 -- semantics of Wide_Value in all cases, and results in a very simple
6982 -- implementation approach.
6984 -- Note: for this approach to be fully standard compliant for the cases
6985 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
6986 -- method must cover the entire character range (e.g. UTF-8). But that
6987 -- is a reasonable requirement when dealing with encoded character
6988 -- sequences. Presumably if one of the restrictive encoding mechanisms
6989 -- is in use such as Shift-JIS, then characters that cannot be
6990 -- represented using this encoding will not appear in any case.
6992 when Attribute_Wide_Value =>
6994 Make_Attribute_Reference (Loc,
6996 Attribute_Name => Name_Value,
6998 Expressions => New_List (
6999 Make_Function_Call (Loc,
7001 New_Occurrence_Of (RTE (RE_Wide_String_To_String), Loc),
7003 Parameter_Associations => New_List (
7004 Relocate_Node (First (Exprs)),
7005 Make_Integer_Literal (Loc,
7006 Intval => Int (Wide_Character_Encoding_Method)))))));
7008 Analyze_And_Resolve (N, Typ);
7010 ---------------------
7011 -- Wide_Wide_Value --
7012 ---------------------
7014 -- We expand typ'Wide_Value_Value (X) into
7017 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
7019 -- Wide_Wide_String_To_String is a runtime function that converts its
7020 -- wide string argument to String, converting any non-translatable
7021 -- characters into appropriate escape sequences. This preserves the
7022 -- required semantics of Wide_Wide_Value in all cases, and results in a
7023 -- very simple implementation approach.
7025 -- It's not quite right where typ = Wide_Wide_Character, because the
7026 -- encoding method may not cover the whole character type ???
7028 when Attribute_Wide_Wide_Value =>
7030 Make_Attribute_Reference (Loc,
7032 Attribute_Name => Name_Value,
7034 Expressions => New_List (
7035 Make_Function_Call (Loc,
7038 (RTE (RE_Wide_Wide_String_To_String), Loc),
7040 Parameter_Associations => New_List (
7041 Relocate_Node (First (Exprs)),
7042 Make_Integer_Literal (Loc,
7043 Intval => Int (Wide_Character_Encoding_Method)))))));
7045 Analyze_And_Resolve (N, Typ);
7047 ---------------------
7048 -- Wide_Wide_Width --
7049 ---------------------
7051 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
7053 when Attribute_Wide_Wide_Width =>
7054 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
7060 -- Wide_Width attribute is handled in separate unit Exp_Imgv
7062 when Attribute_Wide_Width =>
7063 Exp_Imgv.Expand_Width_Attribute (N, Wide);
7069 -- Width attribute is handled in separate unit Exp_Imgv
7071 when Attribute_Width =>
7072 Exp_Imgv.Expand_Width_Attribute (N, Normal);
7078 when Attribute_Write => Write : declare
7079 P_Type : constant Entity_Id := Entity (Pref);
7080 U_Type : constant Entity_Id := Underlying_Type (P_Type);
7088 -- If no underlying type, we have an error that will be diagnosed
7089 -- elsewhere, so here we just completely ignore the expansion.
7095 -- Stream operations can appear in user code even if the restriction
7096 -- No_Streams is active (for example, when instantiating a predefined
7097 -- container). In that case rewrite the attribute as a Raise to
7098 -- prevent any run-time use.
7100 if Restriction_Active (No_Streams) then
7102 Make_Raise_Program_Error (Sloc (N),
7103 Reason => PE_Stream_Operation_Not_Allowed));
7104 Set_Etype (N, U_Type);
7108 -- The simple case, if there is a TSS for Write, just call it
7110 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
7112 if Present (Pname) then
7116 -- If there is a Stream_Convert pragma, use it, we rewrite
7118 -- sourcetyp'Output (stream, Item)
7122 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
7124 -- where strmwrite is the given Write function that converts an
7125 -- argument of type sourcetyp or a type acctyp, from which it is
7126 -- derived to type strmtyp. The conversion to acttyp is required
7127 -- for the derived case.
7129 Prag := Get_Stream_Convert_Pragma (P_Type);
7131 if Present (Prag) then
7133 Next (Next (First (Pragma_Argument_Associations (Prag))));
7134 Wfunc := Entity (Expression (Arg3));
7137 Make_Attribute_Reference (Loc,
7138 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
7139 Attribute_Name => Name_Output,
7140 Expressions => New_List (
7141 Relocate_Node (First (Exprs)),
7142 Make_Function_Call (Loc,
7143 Name => New_Occurrence_Of (Wfunc, Loc),
7144 Parameter_Associations => New_List (
7145 OK_Convert_To (Etype (First_Formal (Wfunc)),
7146 Relocate_Node (Next (First (Exprs)))))))));
7151 -- For elementary types, we call the W_xxx routine directly
7153 elsif Is_Elementary_Type (U_Type) then
7154 Rewrite (N, Build_Elementary_Write_Call (N));
7160 elsif Is_Array_Type (U_Type) then
7161 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
7162 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
7164 -- Tagged type case, use the primitive Write function. Note that
7165 -- this will dispatch in the class-wide case which is what we want
7167 elsif Is_Tagged_Type (U_Type) then
7168 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
7170 -- All other record type cases, including protected records.
7171 -- The latter only arise for expander generated code for
7172 -- handling shared passive partition access.
7176 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
7178 -- Ada 2005 (AI-216): Program_Error is raised when executing
7179 -- the default implementation of the Write attribute of an
7180 -- Unchecked_Union type. However, if the 'Write reference is
7181 -- within the generated Output stream procedure, Write outputs
7182 -- the components, and the default values of the discriminant
7183 -- are streamed by the Output procedure itself. If there are
7184 -- no default values this is also erroneous.
7186 if Is_Unchecked_Union (Base_Type (U_Type)) then
7187 if (not Is_TSS (Current_Scope, TSS_Stream_Output)
7188 and not Is_TSS (Current_Scope, TSS_Stream_Write))
7189 or else No (Discriminant_Default_Value
7190 (First_Discriminant (U_Type)))
7193 Make_Raise_Program_Error (Loc,
7194 Reason => PE_Unchecked_Union_Restriction));
7195 Set_Etype (N, U_Type);
7200 if Has_Discriminants (U_Type)
7202 (Discriminant_Default_Value (First_Discriminant (U_Type)))
7204 Build_Mutable_Record_Write_Procedure
7205 (Loc, Full_Base (U_Type), Decl, Pname);
7207 Build_Record_Write_Procedure
7208 (Loc, Full_Base (U_Type), Decl, Pname);
7211 Insert_Action (N, Decl);
7215 -- If we fall through, Pname is the procedure to be called
7217 Rewrite_Stream_Proc_Call (Pname);
7220 -- Component_Size is handled by the back end, unless the component size
7221 -- is known at compile time, which is always true in the packed array
7222 -- case. It is important that the packed array case is handled in the
7223 -- front end (see Eval_Attribute) since the back end would otherwise get
7224 -- confused by the equivalent packed array type.
7226 when Attribute_Component_Size =>
7229 -- The following attributes are handled by the back end (except that
7230 -- static cases have already been evaluated during semantic processing,
7231 -- but in any case the back end should not count on this).
7233 -- The back end also handles the non-class-wide cases of Size
7235 when Attribute_Bit_Order
7236 | Attribute_Code_Address
7237 | Attribute_Definite
7239 | Attribute_Null_Parameter
7240 | Attribute_Passed_By_Reference
7241 | Attribute_Pool_Address
7242 | Attribute_Scalar_Storage_Order
7246 -- The following attributes are also handled by the back end, but return
7247 -- a universal integer result, so may need a conversion for checking
7248 -- that the result is in range.
7251 | Attribute_Max_Alignment_For_Allocation
7253 Apply_Universal_Integer_Attribute_Checks (N);
7255 -- The following attributes should not appear at this stage, since they
7256 -- have already been handled by the analyzer (and properly rewritten
7257 -- with corresponding values or entities to represent the right values)
7259 when Attribute_Abort_Signal
7260 | Attribute_Address_Size
7261 | Attribute_Atomic_Always_Lock_Free
7264 | Attribute_Compiler_Version
7265 | Attribute_Default_Bit_Order
7266 | Attribute_Default_Scalar_Storage_Order
7273 | Attribute_Fast_Math
7274 | Attribute_First_Valid
7275 | Attribute_Has_Access_Values
7276 | Attribute_Has_Discriminants
7277 | Attribute_Has_Tagged_Values
7279 | Attribute_Last_Valid
7280 | Attribute_Library_Level
7281 | Attribute_Lock_Free
7282 | Attribute_Machine_Emax
7283 | Attribute_Machine_Emin
7284 | Attribute_Machine_Mantissa
7285 | Attribute_Machine_Overflows
7286 | Attribute_Machine_Radix
7287 | Attribute_Machine_Rounds
7288 | Attribute_Maximum_Alignment
7289 | Attribute_Model_Emin
7290 | Attribute_Model_Epsilon
7291 | Attribute_Model_Mantissa
7292 | Attribute_Model_Small
7294 | Attribute_Partition_ID
7296 | Attribute_Restriction_Set
7297 | Attribute_Safe_Emax
7298 | Attribute_Safe_First
7299 | Attribute_Safe_Large
7300 | Attribute_Safe_Last
7301 | Attribute_Safe_Small
7303 | Attribute_Signed_Zeros
7305 | Attribute_Storage_Unit
7306 | Attribute_Stub_Type
7307 | Attribute_System_Allocator_Alignment
7308 | Attribute_Target_Name
7309 | Attribute_Type_Class
7310 | Attribute_Type_Key
7311 | Attribute_Unconstrained_Array
7312 | Attribute_Universal_Literal_String
7313 | Attribute_Wchar_T_Size
7314 | Attribute_Word_Size
7316 raise Program_Error;
7318 -- The Asm_Input and Asm_Output attributes are not expanded at this
7319 -- stage, but will be eliminated in the expansion of the Asm call, see
7320 -- Exp_Intr for details. So the back end will never see these either.
7322 when Attribute_Asm_Input
7323 | Attribute_Asm_Output
7328 -- Note: as mentioned earlier, individual sections of the above case
7329 -- statement assume there is no code after the case statement, and are
7330 -- legitimately allowed to execute return statements if they have nothing
7331 -- more to do, so DO NOT add code at this point.
7334 when RE_Not_Available =>
7336 end Expand_N_Attribute_Reference;
7338 --------------------------------
7339 -- Expand_Pred_Succ_Attribute --
7340 --------------------------------
7342 -- For typ'Pred (exp), we generate the check
7344 -- [constraint_error when exp = typ'Base'First]
7346 -- Similarly, for typ'Succ (exp), we generate the check
7348 -- [constraint_error when exp = typ'Base'Last]
7350 -- These checks are not generated for modular types, since the proper
7351 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
7352 -- We also suppress these checks if we are the right side of an assignment
7353 -- statement or the expression of an object declaration, where the flag
7354 -- Suppress_Assignment_Checks is set for the assignment/declaration.
7356 procedure Expand_Pred_Succ_Attribute (N : Node_Id) is
7357 Loc : constant Source_Ptr := Sloc (N);
7358 P : constant Node_Id := Parent (N);
7362 if Attribute_Name (N) = Name_Pred then
7368 if not Nkind_In (P, N_Assignment_Statement, N_Object_Declaration)
7369 or else not Suppress_Assignment_Checks (P)
7372 Make_Raise_Constraint_Error (Loc,
7376 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
7378 Make_Attribute_Reference (Loc,
7380 New_Occurrence_Of (Base_Type (Etype (Prefix (N))), Loc),
7381 Attribute_Name => Cnam)),
7382 Reason => CE_Overflow_Check_Failed));
7384 end Expand_Pred_Succ_Attribute;
7386 ---------------------------
7387 -- Expand_Size_Attribute --
7388 ---------------------------
7390 procedure Expand_Size_Attribute (N : Node_Id) is
7391 Loc : constant Source_Ptr := Sloc (N);
7392 Typ : constant Entity_Id := Etype (N);
7393 Pref : constant Node_Id := Prefix (N);
7394 Ptyp : constant Entity_Id := Etype (Pref);
7395 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
7399 -- Case of known RM_Size of a type
7401 if (Id = Attribute_Size or else Id = Attribute_Value_Size)
7402 and then Is_Entity_Name (Pref)
7403 and then Is_Type (Entity (Pref))
7404 and then Known_Static_RM_Size (Entity (Pref))
7406 Siz := RM_Size (Entity (Pref));
7408 -- Case of known Esize of a type
7410 elsif Id = Attribute_Object_Size
7411 and then Is_Entity_Name (Pref)
7412 and then Is_Type (Entity (Pref))
7413 and then Known_Static_Esize (Entity (Pref))
7415 Siz := Esize (Entity (Pref));
7417 -- Case of known size of object
7419 elsif Id = Attribute_Size
7420 and then Is_Entity_Name (Pref)
7421 and then Is_Object (Entity (Pref))
7422 and then Known_Esize (Entity (Pref))
7423 and then Known_Static_Esize (Entity (Pref))
7425 Siz := Esize (Entity (Pref));
7427 -- For an array component, we can do Size in the front end if the
7428 -- component_size of the array is set.
7430 elsif Nkind (Pref) = N_Indexed_Component then
7431 Siz := Component_Size (Etype (Prefix (Pref)));
7433 -- For a record component, we can do Size in the front end if there is a
7434 -- component clause, or if the record is packed and the component's size
7435 -- is known at compile time.
7437 elsif Nkind (Pref) = N_Selected_Component then
7439 Rec : constant Entity_Id := Etype (Prefix (Pref));
7440 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
7443 if Present (Component_Clause (Comp)) then
7444 Siz := Esize (Comp);
7446 elsif Is_Packed (Rec) then
7447 Siz := RM_Size (Ptyp);
7450 Apply_Universal_Integer_Attribute_Checks (N);
7455 -- All other cases are handled by the back end
7458 Apply_Universal_Integer_Attribute_Checks (N);
7460 -- If Size is applied to a formal parameter that is of a packed
7461 -- array subtype, then apply Size to the actual subtype.
7463 if Is_Entity_Name (Pref)
7464 and then Is_Formal (Entity (Pref))
7465 and then Is_Array_Type (Ptyp)
7466 and then Is_Packed (Ptyp)
7469 Make_Attribute_Reference (Loc,
7471 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
7472 Attribute_Name => Name_Size));
7473 Analyze_And_Resolve (N, Typ);
7475 -- If Size is applied to a dereference of an access to unconstrained
7476 -- packed array, the back end needs to see its unconstrained nominal
7477 -- type, but also a hint to the actual constrained type.
7479 elsif Nkind (Pref) = N_Explicit_Dereference
7480 and then Is_Array_Type (Ptyp)
7481 and then not Is_Constrained (Ptyp)
7482 and then Is_Packed (Ptyp)
7484 Set_Actual_Designated_Subtype (Pref, Get_Actual_Subtype (Pref));
7486 -- If Size was applied to a slice of a bit-packed array, we rewrite
7487 -- it into the product of Length and Component_Size. We need to do so
7488 -- because bit-packed arrays are represented internally as arrays of
7489 -- System.Unsigned_Types.Packed_Byte for code generation purposes so
7490 -- the size is always rounded up in the back end.
7492 elsif Nkind (Original_Node (Pref)) = N_Slice
7493 and then Is_Bit_Packed_Array (Ptyp)
7496 Make_Op_Multiply (Loc,
7497 Make_Attribute_Reference (Loc,
7498 Prefix => Duplicate_Subexpr (Pref, True),
7499 Attribute_Name => Name_Length),
7500 Make_Attribute_Reference (Loc,
7501 Prefix => Duplicate_Subexpr (Pref, True),
7502 Attribute_Name => Name_Component_Size)));
7503 Analyze_And_Resolve (N, Typ);
7509 -- Common processing for record and array component case
7511 if Siz /= No_Uint and then Siz /= 0 then
7513 CS : constant Boolean := Comes_From_Source (N);
7516 Rewrite (N, Make_Integer_Literal (Loc, Siz));
7518 -- This integer literal is not a static expression. We do not
7519 -- call Analyze_And_Resolve here, because this would activate
7520 -- the circuit for deciding that a static value was out of range,
7521 -- and we don't want that.
7523 -- So just manually set the type, mark the expression as
7524 -- nonstatic, and then ensure that the result is checked
7525 -- properly if the attribute comes from source (if it was
7526 -- internally generated, we never need a constraint check).
7529 Set_Is_Static_Expression (N, False);
7532 Apply_Constraint_Check (N, Typ);
7536 end Expand_Size_Attribute;
7538 -----------------------------
7539 -- Expand_Update_Attribute --
7540 -----------------------------
7542 procedure Expand_Update_Attribute (N : Node_Id) is
7543 procedure Process_Component_Or_Element_Update
7548 -- Generate the statements necessary to update a single component or an
7549 -- element of the prefix. The code is inserted before the attribute N.
7550 -- Temp denotes the entity of the anonymous object created to reflect
7551 -- the changes in values. Comp is the component/index expression to be
7552 -- updated. Expr is an expression yielding the new value of Comp. Typ
7553 -- is the type of the prefix of attribute Update.
7555 procedure Process_Range_Update
7560 -- Generate the statements necessary to update a slice of the prefix.
7561 -- The code is inserted before the attribute N. Temp denotes the entity
7562 -- of the anonymous object created to reflect the changes in values.
7563 -- Comp is range of the slice to be updated. Expr is an expression
7564 -- yielding the new value of Comp. Typ is the type of the prefix of
7565 -- attribute Update.
7567 -----------------------------------------
7568 -- Process_Component_Or_Element_Update --
7569 -----------------------------------------
7571 procedure Process_Component_Or_Element_Update
7577 Loc : constant Source_Ptr := Sloc (Comp);
7582 -- An array element may be modified by the following relations
7583 -- depending on the number of dimensions:
7585 -- 1 => Expr -- one dimensional update
7586 -- (1, ..., N) => Expr -- multi dimensional update
7588 -- The above forms are converted in assignment statements where the
7589 -- left hand side is an indexed component:
7591 -- Temp (1) := Expr; -- one dimensional update
7592 -- Temp (1, ..., N) := Expr; -- multi dimensional update
7594 if Is_Array_Type (Typ) then
7596 -- The index expressions of a multi dimensional array update
7597 -- appear as an aggregate.
7599 if Nkind (Comp) = N_Aggregate then
7600 Exprs := New_Copy_List_Tree (Expressions (Comp));
7602 Exprs := New_List (Relocate_Node (Comp));
7606 Make_Indexed_Component (Loc,
7607 Prefix => New_Occurrence_Of (Temp, Loc),
7608 Expressions => Exprs);
7610 -- A record component update appears in the following form:
7614 -- The above relation is transformed into an assignment statement
7615 -- where the left hand side is a selected component:
7617 -- Temp.Comp := Expr;
7619 else pragma Assert (Is_Record_Type (Typ));
7621 Make_Selected_Component (Loc,
7622 Prefix => New_Occurrence_Of (Temp, Loc),
7623 Selector_Name => Relocate_Node (Comp));
7627 Make_Assignment_Statement (Loc,
7629 Expression => Relocate_Node (Expr)));
7630 end Process_Component_Or_Element_Update;
7632 --------------------------
7633 -- Process_Range_Update --
7634 --------------------------
7636 procedure Process_Range_Update
7642 Index_Typ : constant Entity_Id := Etype (First_Index (Typ));
7643 Loc : constant Source_Ptr := Sloc (Comp);
7647 -- A range update appears as
7649 -- (Low .. High => Expr)
7651 -- The above construct is transformed into a loop that iterates over
7652 -- the given range and modifies the corresponding array values to the
7655 -- for Index in Low .. High loop
7656 -- Temp (<Index_Typ> (Index)) := Expr;
7659 Index := Make_Temporary (Loc, 'I');
7662 Make_Loop_Statement (Loc,
7664 Make_Iteration_Scheme (Loc,
7665 Loop_Parameter_Specification =>
7666 Make_Loop_Parameter_Specification (Loc,
7667 Defining_Identifier => Index,
7668 Discrete_Subtype_Definition => Relocate_Node (Comp))),
7670 Statements => New_List (
7671 Make_Assignment_Statement (Loc,
7673 Make_Indexed_Component (Loc,
7674 Prefix => New_Occurrence_Of (Temp, Loc),
7675 Expressions => New_List (
7676 Convert_To (Index_Typ,
7677 New_Occurrence_Of (Index, Loc)))),
7678 Expression => Relocate_Node (Expr))),
7680 End_Label => Empty));
7681 end Process_Range_Update;
7685 Aggr : constant Node_Id := First (Expressions (N));
7686 Loc : constant Source_Ptr := Sloc (N);
7687 Pref : constant Node_Id := Prefix (N);
7688 Typ : constant Entity_Id := Etype (Pref);
7691 CW_Temp : Entity_Id;
7696 -- Start of processing for Expand_Update_Attribute
7699 -- Create the anonymous object to store the value of the prefix and
7700 -- capture subsequent changes in value.
7702 Temp := Make_Temporary (Loc, 'T', Pref);
7704 -- Preserve the tag of the prefix by offering a specific view of the
7705 -- class-wide version of the prefix.
7707 if Is_Tagged_Type (Typ) then
7710 -- CW_Temp : Typ'Class := Typ'Class (Pref);
7712 CW_Temp := Make_Temporary (Loc, 'T');
7713 CW_Typ := Class_Wide_Type (Typ);
7716 Make_Object_Declaration (Loc,
7717 Defining_Identifier => CW_Temp,
7718 Object_Definition => New_Occurrence_Of (CW_Typ, Loc),
7720 Convert_To (CW_Typ, Relocate_Node (Pref))));
7723 -- Temp : Typ renames Typ (CW_Temp);
7726 Make_Object_Renaming_Declaration (Loc,
7727 Defining_Identifier => Temp,
7728 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
7730 Convert_To (Typ, New_Occurrence_Of (CW_Temp, Loc))));
7736 -- Temp : Typ := Pref;
7739 Make_Object_Declaration (Loc,
7740 Defining_Identifier => Temp,
7741 Object_Definition => New_Occurrence_Of (Typ, Loc),
7742 Expression => Relocate_Node (Pref)));
7745 -- Process the update aggregate
7747 Assoc := First (Component_Associations (Aggr));
7748 while Present (Assoc) loop
7749 Comp := First (Choices (Assoc));
7750 Expr := Expression (Assoc);
7751 while Present (Comp) loop
7752 if Nkind (Comp) = N_Range then
7753 Process_Range_Update (Temp, Comp, Expr, Typ);
7755 Process_Component_Or_Element_Update (Temp, Comp, Expr, Typ);
7764 -- The attribute is replaced by a reference to the anonymous object
7766 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7768 end Expand_Update_Attribute;
7774 procedure Find_Fat_Info
7776 Fat_Type : out Entity_Id;
7777 Fat_Pkg : out RE_Id)
7779 Rtyp : constant Entity_Id := Root_Type (T);
7782 -- All we do is use the root type (historically this dealt with
7783 -- VAX-float .. to be cleaned up further later ???)
7787 if Fat_Type = Standard_Short_Float then
7788 Fat_Pkg := RE_Attr_Short_Float;
7790 elsif Fat_Type = Standard_Float then
7791 Fat_Pkg := RE_Attr_Float;
7793 elsif Fat_Type = Standard_Long_Float then
7794 Fat_Pkg := RE_Attr_Long_Float;
7796 elsif Fat_Type = Standard_Long_Long_Float then
7797 Fat_Pkg := RE_Attr_Long_Long_Float;
7799 -- Universal real (which is its own root type) is treated as being
7800 -- equivalent to Standard.Long_Long_Float, since it is defined to
7801 -- have the same precision as the longest Float type.
7803 elsif Fat_Type = Universal_Real then
7804 Fat_Type := Standard_Long_Long_Float;
7805 Fat_Pkg := RE_Attr_Long_Long_Float;
7808 raise Program_Error;
7812 ----------------------------
7813 -- Find_Stream_Subprogram --
7814 ----------------------------
7816 function Find_Stream_Subprogram
7818 Nam : TSS_Name_Type) return Entity_Id
7820 Base_Typ : constant Entity_Id := Base_Type (Typ);
7821 Ent : constant Entity_Id := TSS (Typ, Nam);
7823 function Is_Available (Entity : RE_Id) return Boolean;
7824 pragma Inline (Is_Available);
7825 -- Function to check whether the specified run-time call is available
7826 -- in the run time used. In the case of a configurable run time, it
7827 -- is normal that some subprograms are not there.
7829 -- I don't understand this routine at all, why is this not just a
7830 -- call to RTE_Available? And if for some reason we need a different
7831 -- routine with different semantics, why is not in Rtsfind ???
7837 function Is_Available (Entity : RE_Id) return Boolean is
7839 -- Assume that the unit will always be available when using a
7840 -- "normal" (not configurable) run time.
7842 return not Configurable_Run_Time_Mode or else RTE_Available (Entity);
7845 -- Start of processing for Find_Stream_Subprogram
7848 if Present (Ent) then
7852 -- Stream attributes for strings are expanded into library calls. The
7853 -- following checks are disabled when the run-time is not available or
7854 -- when compiling predefined types due to bootstrap issues. As a result,
7855 -- the compiler will generate in-place stream routines for string types
7856 -- that appear in GNAT's library, but will generate calls via rtsfind
7857 -- to library routines for user code.
7859 -- Note: In the case of using a configurable run time, it is very likely
7860 -- that stream routines for string types are not present (they require
7861 -- file system support). In this case, the specific stream routines for
7862 -- strings are not used, relying on the regular stream mechanism
7863 -- instead. That is why we include the test Is_Available when dealing
7864 -- with these cases.
7866 if not Is_Predefined_Unit (Current_Sem_Unit) then
7867 -- Storage_Array as defined in package System.Storage_Elements
7869 if Is_RTE (Base_Typ, RE_Storage_Array) then
7871 -- Case of No_Stream_Optimizations restriction active
7873 if Restriction_Active (No_Stream_Optimizations) then
7874 if Nam = TSS_Stream_Input
7875 and then Is_Available (RE_Storage_Array_Input)
7877 return RTE (RE_Storage_Array_Input);
7879 elsif Nam = TSS_Stream_Output
7880 and then Is_Available (RE_Storage_Array_Output)
7882 return RTE (RE_Storage_Array_Output);
7884 elsif Nam = TSS_Stream_Read
7885 and then Is_Available (RE_Storage_Array_Read)
7887 return RTE (RE_Storage_Array_Read);
7889 elsif Nam = TSS_Stream_Write
7890 and then Is_Available (RE_Storage_Array_Write)
7892 return RTE (RE_Storage_Array_Write);
7894 elsif Nam /= TSS_Stream_Input and then
7895 Nam /= TSS_Stream_Output and then
7896 Nam /= TSS_Stream_Read and then
7897 Nam /= TSS_Stream_Write
7899 raise Program_Error;
7902 -- Restriction No_Stream_Optimizations is not set, so we can go
7903 -- ahead and optimize using the block IO forms of the routines.
7906 if Nam = TSS_Stream_Input
7907 and then Is_Available (RE_Storage_Array_Input_Blk_IO)
7909 return RTE (RE_Storage_Array_Input_Blk_IO);
7911 elsif Nam = TSS_Stream_Output
7912 and then Is_Available (RE_Storage_Array_Output_Blk_IO)
7914 return RTE (RE_Storage_Array_Output_Blk_IO);
7916 elsif Nam = TSS_Stream_Read
7917 and then Is_Available (RE_Storage_Array_Read_Blk_IO)
7919 return RTE (RE_Storage_Array_Read_Blk_IO);
7921 elsif Nam = TSS_Stream_Write
7922 and then Is_Available (RE_Storage_Array_Write_Blk_IO)
7924 return RTE (RE_Storage_Array_Write_Blk_IO);
7926 elsif Nam /= TSS_Stream_Input and then
7927 Nam /= TSS_Stream_Output and then
7928 Nam /= TSS_Stream_Read and then
7929 Nam /= TSS_Stream_Write
7931 raise Program_Error;
7935 -- Stream_Element_Array as defined in package Ada.Streams
7937 elsif Is_RTE (Base_Typ, RE_Stream_Element_Array) then
7939 -- Case of No_Stream_Optimizations restriction active
7941 if Restriction_Active (No_Stream_Optimizations) then
7942 if Nam = TSS_Stream_Input
7943 and then Is_Available (RE_Stream_Element_Array_Input)
7945 return RTE (RE_Stream_Element_Array_Input);
7947 elsif Nam = TSS_Stream_Output
7948 and then Is_Available (RE_Stream_Element_Array_Output)
7950 return RTE (RE_Stream_Element_Array_Output);
7952 elsif Nam = TSS_Stream_Read
7953 and then Is_Available (RE_Stream_Element_Array_Read)
7955 return RTE (RE_Stream_Element_Array_Read);
7957 elsif Nam = TSS_Stream_Write
7958 and then Is_Available (RE_Stream_Element_Array_Write)
7960 return RTE (RE_Stream_Element_Array_Write);
7962 elsif Nam /= TSS_Stream_Input and then
7963 Nam /= TSS_Stream_Output and then
7964 Nam /= TSS_Stream_Read and then
7965 Nam /= TSS_Stream_Write
7967 raise Program_Error;
7970 -- Restriction No_Stream_Optimizations is not set, so we can go
7971 -- ahead and optimize using the block IO forms of the routines.
7974 if Nam = TSS_Stream_Input
7975 and then Is_Available (RE_Stream_Element_Array_Input_Blk_IO)
7977 return RTE (RE_Stream_Element_Array_Input_Blk_IO);
7979 elsif Nam = TSS_Stream_Output
7980 and then Is_Available (RE_Stream_Element_Array_Output_Blk_IO)
7982 return RTE (RE_Stream_Element_Array_Output_Blk_IO);
7984 elsif Nam = TSS_Stream_Read
7985 and then Is_Available (RE_Stream_Element_Array_Read_Blk_IO)
7987 return RTE (RE_Stream_Element_Array_Read_Blk_IO);
7989 elsif Nam = TSS_Stream_Write
7990 and then Is_Available (RE_Stream_Element_Array_Write_Blk_IO)
7992 return RTE (RE_Stream_Element_Array_Write_Blk_IO);
7994 elsif Nam /= TSS_Stream_Input and then
7995 Nam /= TSS_Stream_Output and then
7996 Nam /= TSS_Stream_Read and then
7997 Nam /= TSS_Stream_Write
7999 raise Program_Error;
8003 -- String as defined in package Ada
8005 elsif Base_Typ = Standard_String then
8007 -- Case of No_Stream_Optimizations restriction active
8009 if Restriction_Active (No_Stream_Optimizations) then
8010 if Nam = TSS_Stream_Input
8011 and then Is_Available (RE_String_Input)
8013 return RTE (RE_String_Input);
8015 elsif Nam = TSS_Stream_Output
8016 and then Is_Available (RE_String_Output)
8018 return RTE (RE_String_Output);
8020 elsif Nam = TSS_Stream_Read
8021 and then Is_Available (RE_String_Read)
8023 return RTE (RE_String_Read);
8025 elsif Nam = TSS_Stream_Write
8026 and then Is_Available (RE_String_Write)
8028 return RTE (RE_String_Write);
8030 elsif Nam /= TSS_Stream_Input and then
8031 Nam /= TSS_Stream_Output and then
8032 Nam /= TSS_Stream_Read and then
8033 Nam /= TSS_Stream_Write
8035 raise Program_Error;
8038 -- Restriction No_Stream_Optimizations is not set, so we can go
8039 -- ahead and optimize using the block IO forms of the routines.
8042 if Nam = TSS_Stream_Input
8043 and then Is_Available (RE_String_Input_Blk_IO)
8045 return RTE (RE_String_Input_Blk_IO);
8047 elsif Nam = TSS_Stream_Output
8048 and then Is_Available (RE_String_Output_Blk_IO)
8050 return RTE (RE_String_Output_Blk_IO);
8052 elsif Nam = TSS_Stream_Read
8053 and then Is_Available (RE_String_Read_Blk_IO)
8055 return RTE (RE_String_Read_Blk_IO);
8057 elsif Nam = TSS_Stream_Write
8058 and then Is_Available (RE_String_Write_Blk_IO)
8060 return RTE (RE_String_Write_Blk_IO);
8062 elsif Nam /= TSS_Stream_Input and then
8063 Nam /= TSS_Stream_Output and then
8064 Nam /= TSS_Stream_Read and then
8065 Nam /= TSS_Stream_Write
8067 raise Program_Error;
8071 -- Wide_String as defined in package Ada
8073 elsif Base_Typ = Standard_Wide_String then
8075 -- Case of No_Stream_Optimizations restriction active
8077 if Restriction_Active (No_Stream_Optimizations) then
8078 if Nam = TSS_Stream_Input
8079 and then Is_Available (RE_Wide_String_Input)
8081 return RTE (RE_Wide_String_Input);
8083 elsif Nam = TSS_Stream_Output
8084 and then Is_Available (RE_Wide_String_Output)
8086 return RTE (RE_Wide_String_Output);
8088 elsif Nam = TSS_Stream_Read
8089 and then Is_Available (RE_Wide_String_Read)
8091 return RTE (RE_Wide_String_Read);
8093 elsif Nam = TSS_Stream_Write
8094 and then Is_Available (RE_Wide_String_Write)
8096 return RTE (RE_Wide_String_Write);
8098 elsif Nam /= TSS_Stream_Input and then
8099 Nam /= TSS_Stream_Output and then
8100 Nam /= TSS_Stream_Read and then
8101 Nam /= TSS_Stream_Write
8103 raise Program_Error;
8106 -- Restriction No_Stream_Optimizations is not set, so we can go
8107 -- ahead and optimize using the block IO forms of the routines.
8110 if Nam = TSS_Stream_Input
8111 and then Is_Available (RE_Wide_String_Input_Blk_IO)
8113 return RTE (RE_Wide_String_Input_Blk_IO);
8115 elsif Nam = TSS_Stream_Output
8116 and then Is_Available (RE_Wide_String_Output_Blk_IO)
8118 return RTE (RE_Wide_String_Output_Blk_IO);
8120 elsif Nam = TSS_Stream_Read
8121 and then Is_Available (RE_Wide_String_Read_Blk_IO)
8123 return RTE (RE_Wide_String_Read_Blk_IO);
8125 elsif Nam = TSS_Stream_Write
8126 and then Is_Available (RE_Wide_String_Write_Blk_IO)
8128 return RTE (RE_Wide_String_Write_Blk_IO);
8130 elsif Nam /= TSS_Stream_Input and then
8131 Nam /= TSS_Stream_Output and then
8132 Nam /= TSS_Stream_Read and then
8133 Nam /= TSS_Stream_Write
8135 raise Program_Error;
8139 -- Wide_Wide_String as defined in package Ada
8141 elsif Base_Typ = Standard_Wide_Wide_String then
8143 -- Case of No_Stream_Optimizations restriction active
8145 if Restriction_Active (No_Stream_Optimizations) then
8146 if Nam = TSS_Stream_Input
8147 and then Is_Available (RE_Wide_Wide_String_Input)
8149 return RTE (RE_Wide_Wide_String_Input);
8151 elsif Nam = TSS_Stream_Output
8152 and then Is_Available (RE_Wide_Wide_String_Output)
8154 return RTE (RE_Wide_Wide_String_Output);
8156 elsif Nam = TSS_Stream_Read
8157 and then Is_Available (RE_Wide_Wide_String_Read)
8159 return RTE (RE_Wide_Wide_String_Read);
8161 elsif Nam = TSS_Stream_Write
8162 and then Is_Available (RE_Wide_Wide_String_Write)
8164 return RTE (RE_Wide_Wide_String_Write);
8166 elsif Nam /= TSS_Stream_Input and then
8167 Nam /= TSS_Stream_Output and then
8168 Nam /= TSS_Stream_Read and then
8169 Nam /= TSS_Stream_Write
8171 raise Program_Error;
8174 -- Restriction No_Stream_Optimizations is not set, so we can go
8175 -- ahead and optimize using the block IO forms of the routines.
8178 if Nam = TSS_Stream_Input
8179 and then Is_Available (RE_Wide_Wide_String_Input_Blk_IO)
8181 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
8183 elsif Nam = TSS_Stream_Output
8184 and then Is_Available (RE_Wide_Wide_String_Output_Blk_IO)
8186 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
8188 elsif Nam = TSS_Stream_Read
8189 and then Is_Available (RE_Wide_Wide_String_Read_Blk_IO)
8191 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
8193 elsif Nam = TSS_Stream_Write
8194 and then Is_Available (RE_Wide_Wide_String_Write_Blk_IO)
8196 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
8198 elsif Nam /= TSS_Stream_Input and then
8199 Nam /= TSS_Stream_Output and then
8200 Nam /= TSS_Stream_Read and then
8201 Nam /= TSS_Stream_Write
8203 raise Program_Error;
8209 if Is_Tagged_Type (Typ) and then Is_Derived_Type (Typ) then
8210 return Find_Prim_Op (Typ, Nam);
8212 return Find_Inherited_TSS (Typ, Nam);
8214 end Find_Stream_Subprogram;
8220 function Full_Base (T : Entity_Id) return Entity_Id is
8224 BT := Base_Type (T);
8226 if Is_Private_Type (BT)
8227 and then Present (Full_View (BT))
8229 BT := Full_View (BT);
8235 -----------------------
8236 -- Get_Index_Subtype --
8237 -----------------------
8239 function Get_Index_Subtype (N : Node_Id) return Node_Id is
8240 P_Type : Entity_Id := Etype (Prefix (N));
8245 if Is_Access_Type (P_Type) then
8246 P_Type := Designated_Type (P_Type);
8249 if No (Expressions (N)) then
8252 J := UI_To_Int (Expr_Value (First (Expressions (N))));
8255 Indx := First_Index (P_Type);
8261 return Etype (Indx);
8262 end Get_Index_Subtype;
8264 -------------------------------
8265 -- Get_Stream_Convert_Pragma --
8266 -------------------------------
8268 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
8273 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
8274 -- that a stream convert pragma for a tagged type is not inherited from
8275 -- its parent. Probably what is wrong here is that it is basically
8276 -- incorrect to consider a stream convert pragma to be a representation
8277 -- pragma at all ???
8279 N := First_Rep_Item (Implementation_Base_Type (T));
8280 while Present (N) loop
8281 if Nkind (N) = N_Pragma
8282 and then Pragma_Name (N) = Name_Stream_Convert
8284 -- For tagged types this pragma is not inherited, so we
8285 -- must verify that it is defined for the given type and
8289 Entity (Expression (First (Pragma_Argument_Associations (N))));
8291 if not Is_Tagged_Type (T)
8293 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
8303 end Get_Stream_Convert_Pragma;
8305 ---------------------------------
8306 -- Is_Constrained_Packed_Array --
8307 ---------------------------------
8309 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
8310 Arr : Entity_Id := Typ;
8313 if Is_Access_Type (Arr) then
8314 Arr := Designated_Type (Arr);
8317 return Is_Array_Type (Arr)
8318 and then Is_Constrained (Arr)
8319 and then Present (Packed_Array_Impl_Type (Arr));
8320 end Is_Constrained_Packed_Array;
8322 ----------------------------------------
8323 -- Is_Inline_Floating_Point_Attribute --
8324 ----------------------------------------
8326 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
8327 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
8329 function Is_GCC_Target return Boolean;
8330 -- Return True if we are using a GCC target/back-end
8331 -- ??? Note: the implementation is kludgy/fragile
8337 function Is_GCC_Target return Boolean is
8339 return not CodePeer_Mode
8340 and then not Modify_Tree_For_C;
8343 -- Start of processing for Is_Inline_Floating_Point_Attribute
8346 -- Machine and Model can be expanded by the GCC back end only
8348 if Id = Attribute_Machine or else Id = Attribute_Model then
8349 return Is_GCC_Target;
8351 -- Remaining cases handled by all back ends are Rounding and Truncation
8352 -- when appearing as the operand of a conversion to some integer type.
8354 elsif Nkind (Parent (N)) /= N_Type_Conversion
8355 or else not Is_Integer_Type (Etype (Parent (N)))
8360 -- Here we are in the integer conversion context. We reuse Rounding for
8361 -- Machine_Rounding as System.Fat_Gen, which is a permissible behavior.
8364 Id = Attribute_Rounding
8365 or else Id = Attribute_Machine_Rounding
8366 or else Id = Attribute_Truncation;
8367 end Is_Inline_Floating_Point_Attribute;