1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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. --
18 -- You should have received a copy of the GNU General Public License along --
19 -- with this program; see file COPYING3. If not see --
20 -- <http://www.gnu.org/licenses/>. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
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 Exp_VFpt; use Exp_VFpt;
43 with Fname; use Fname;
44 with Freeze; use Freeze;
45 with Gnatvsn; use Gnatvsn;
46 with Itypes; use Itypes;
48 with Namet; use Namet;
49 with Nmake; use Nmake;
50 with Nlists; use Nlists;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Res; use Sem_Res;
62 with Sem_Util; use Sem_Util;
63 with Sinfo; use Sinfo;
64 with Snames; use Snames;
65 with Stand; use Stand;
66 with Stringt; use Stringt;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Uname; use Uname;
72 with Validsw; use Validsw;
74 package body Exp_Attr is
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Compile_Stream_Body_In_Scope
85 -- The body for a stream subprogram may be generated outside of the scope
86 -- of the type. If the type is fully private, it may depend on the full
87 -- view of other types (e.g. indices) that are currently private as well.
88 -- We install the declarations of the package in which the type is declared
89 -- before compiling the body in what is its proper environment. The Check
90 -- parameter indicates if checks are to be suppressed for the stream body.
91 -- We suppress checks for array/record reads, since the rule is that these
92 -- are like assignments, out of range values due to uninitialized storage,
93 -- or other invalid values do NOT cause a Constraint_Error to be raised.
95 procedure Expand_Access_To_Protected_Op
99 -- An attribute reference to a protected subprogram is transformed into
100 -- a pair of pointers: one to the object, and one to the operations.
101 -- This expansion is performed for 'Access and for 'Unrestricted_Access.
103 procedure Expand_Fpt_Attribute
108 -- This procedure expands a call to a floating-point attribute function.
109 -- N is the attribute reference node, and Args is a list of arguments to
110 -- be passed to the function call. Pkg identifies the package containing
111 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
112 -- have already been converted to the floating-point type for which Pkg was
113 -- instantiated. The Nam argument is the relevant attribute processing
114 -- routine to be called. This is the same as the attribute name, except in
115 -- the Unaligned_Valid case.
117 procedure Expand_Fpt_Attribute_R (N : Node_Id);
118 -- This procedure expands a call to a floating-point attribute function
119 -- that takes a single floating-point argument. The function to be called
120 -- is always the same as the attribute name.
122 procedure Expand_Fpt_Attribute_RI (N : Node_Id);
123 -- This procedure expands a call to a floating-point attribute function
124 -- that takes one floating-point argument and one integer argument. The
125 -- function to be called is always the same as the attribute name.
127 procedure Expand_Fpt_Attribute_RR (N : Node_Id);
128 -- This procedure expands a call to a floating-point attribute function
129 -- that takes two floating-point arguments. The function to be called
130 -- is always the same as the attribute name.
132 procedure Expand_Pred_Succ (N : Node_Id);
133 -- Handles expansion of Pred or Succ attributes for case of non-real
134 -- operand with overflow checking required.
136 function Get_Index_Subtype (N : Node_Id) return Entity_Id;
137 -- Used for Last, Last, and Length, when the prefix is an array type.
138 -- Obtains the corresponding index subtype.
140 procedure Find_Fat_Info
142 Fat_Type : out Entity_Id;
143 Fat_Pkg : out RE_Id);
144 -- Given a floating-point type T, identifies the package containing the
145 -- attributes for this type (returned in Fat_Pkg), and the corresponding
146 -- type for which this package was instantiated from Fat_Gen. Error if T
147 -- is not a floating-point type.
149 function Find_Stream_Subprogram
151 Nam : TSS_Name_Type) return Entity_Id;
152 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
153 -- types, the corresponding primitive operation is looked up, else the
154 -- appropriate TSS from the type itself, or from its closest ancestor
155 -- defining it, is returned. In both cases, inheritance of representation
156 -- aspects is thus taken into account.
158 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id;
159 -- Given a type, find a corresponding stream convert pragma that applies to
160 -- the implementation base type of this type (Typ). If found, return the
161 -- pragma node, otherwise return Empty if no pragma is found.
163 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean;
164 -- Utility for array attributes, returns true on packed constrained
165 -- arrays, and on access to same.
167 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean;
168 -- Returns true iff the given node refers to an attribute call that
169 -- can be expanded directly by the back end and does not need front end
170 -- expansion. Typically used for rounding and truncation attributes that
171 -- appear directly inside a conversion to integer.
173 ----------------------------------
174 -- Compile_Stream_Body_In_Scope --
175 ----------------------------------
177 procedure Compile_Stream_Body_In_Scope
183 Installed : Boolean := False;
184 Scop : constant Entity_Id := Scope (Arr);
185 Curr : constant Entity_Id := Current_Scope;
189 and then not In_Open_Scopes (Scop)
190 and then Ekind (Scop) = E_Package
193 Install_Visible_Declarations (Scop);
194 Install_Private_Declarations (Scop);
197 -- The entities in the package are now visible, but the generated
198 -- stream entity must appear in the current scope (usually an
199 -- enclosing stream function) so that itypes all have their proper
206 Insert_Action (N, Decl);
208 Insert_Action (N, Decl, Suppress => All_Checks);
213 -- Remove extra copy of current scope, and package itself
216 End_Package_Scope (Scop);
218 end Compile_Stream_Body_In_Scope;
220 -----------------------------------
221 -- Expand_Access_To_Protected_Op --
222 -----------------------------------
224 procedure Expand_Access_To_Protected_Op
229 -- The value of the attribute_reference is a record containing two
230 -- fields: an access to the protected object, and an access to the
231 -- subprogram itself. The prefix is a selected component.
233 Loc : constant Source_Ptr := Sloc (N);
235 Btyp : constant Entity_Id := Base_Type (Typ);
238 E_T : constant Entity_Id := Equivalent_Type (Btyp);
239 Acc : constant Entity_Id :=
240 Etype (Next_Component (First_Component (E_T)));
244 function May_Be_External_Call return Boolean;
245 -- If the 'Access is to a local operation, but appears in a context
246 -- where it may lead to a call from outside the object, we must treat
247 -- this as an external call. Clearly we cannot tell without full
248 -- flow analysis, and a subsequent call that uses this 'Access may
249 -- lead to a bounded error (trying to seize locks twice, e.g.). For
250 -- now we treat 'Access as a potential external call if it is an actual
251 -- in a call to an outside subprogram.
253 --------------------------
254 -- May_Be_External_Call --
255 --------------------------
257 function May_Be_External_Call return Boolean is
259 Par : Node_Id := Parent (N);
262 -- Account for the case where the Access attribute is part of a
263 -- named parameter association.
265 if Nkind (Par) = N_Parameter_Association then
269 if Nkind_In (Par, N_Procedure_Call_Statement, N_Function_Call)
270 and then Is_Entity_Name (Name (Par))
272 Subp := Entity (Name (Par));
273 return not In_Open_Scopes (Scope (Subp));
277 end May_Be_External_Call;
279 -- Start of processing for Expand_Access_To_Protected_Op
282 -- Within the body of the protected type, the prefix designates a local
283 -- operation, and the object is the first parameter of the corresponding
284 -- protected body of the current enclosing operation.
286 if Is_Entity_Name (Pref) then
287 if May_Be_External_Call then
289 New_Occurrence_Of (External_Subprogram (Entity (Pref)), Loc);
293 (Protected_Body_Subprogram (Entity (Pref)), Loc);
296 -- Don't traverse the scopes when the attribute occurs within an init
297 -- proc, because we directly use the _init formal of the init proc in
300 Curr := Current_Scope;
301 if not Is_Init_Proc (Curr) then
302 pragma Assert (In_Open_Scopes (Scope (Entity (Pref))));
304 while Scope (Curr) /= Scope (Entity (Pref)) loop
305 Curr := Scope (Curr);
309 -- In case of protected entries the first formal of its Protected_
310 -- Body_Subprogram is the address of the object.
312 if Ekind (Curr) = E_Entry then
316 (Protected_Body_Subprogram (Curr)), Loc);
318 -- If the current scope is an init proc, then use the address of the
319 -- _init formal as the object reference.
321 elsif Is_Init_Proc (Curr) then
323 Make_Attribute_Reference (Loc,
324 Prefix => New_Occurrence_Of (First_Formal (Curr), Loc),
325 Attribute_Name => Name_Address);
327 -- In case of protected subprograms the first formal of its
328 -- Protected_Body_Subprogram is the object and we get its address.
332 Make_Attribute_Reference (Loc,
336 (Protected_Body_Subprogram (Curr)), Loc),
337 Attribute_Name => Name_Address);
340 -- Case where the prefix is not an entity name. Find the
341 -- version of the protected operation to be called from
342 -- outside the protected object.
348 (Entity (Selector_Name (Pref))), Loc);
351 Make_Attribute_Reference (Loc,
352 Prefix => Relocate_Node (Prefix (Pref)),
353 Attribute_Name => Name_Address);
357 Make_Attribute_Reference (Loc,
359 Attribute_Name => Name_Access);
361 -- We set the type of the access reference to the already generated
362 -- access_to_subprogram type, and declare the reference analyzed, to
363 -- prevent further expansion when the enclosing aggregate is analyzed.
365 Set_Etype (Sub_Ref, Acc);
366 Set_Analyzed (Sub_Ref);
370 Expressions => New_List (Obj_Ref, Sub_Ref));
372 -- Sub_Ref has been marked as analyzed, but we still need to make sure
373 -- Sub is correctly frozen.
375 Freeze_Before (N, Entity (Sub));
378 Analyze_And_Resolve (N, E_T);
380 -- For subsequent analysis, the node must retain its type. The backend
381 -- will replace it with the equivalent type where needed.
384 end Expand_Access_To_Protected_Op;
386 --------------------------
387 -- Expand_Fpt_Attribute --
388 --------------------------
390 procedure Expand_Fpt_Attribute
396 Loc : constant Source_Ptr := Sloc (N);
397 Typ : constant Entity_Id := Etype (N);
401 -- The function name is the selected component Attr_xxx.yyy where
402 -- Attr_xxx is the package name, and yyy is the argument Nam.
404 -- Note: it would be more usual to have separate RE entries for each
405 -- of the entities in the Fat packages, but first they have identical
406 -- names (so we would have to have lots of renaming declarations to
407 -- meet the normal RE rule of separate names for all runtime entities),
408 -- and second there would be an awful lot of them!
411 Make_Selected_Component (Loc,
412 Prefix => New_Reference_To (RTE (Pkg), Loc),
413 Selector_Name => Make_Identifier (Loc, Nam));
415 -- The generated call is given the provided set of parameters, and then
416 -- wrapped in a conversion which converts the result to the target type
417 -- We use the base type as the target because a range check may be
421 Unchecked_Convert_To (Base_Type (Etype (N)),
422 Make_Function_Call (Loc,
424 Parameter_Associations => Args)));
426 Analyze_And_Resolve (N, Typ);
427 end Expand_Fpt_Attribute;
429 ----------------------------
430 -- Expand_Fpt_Attribute_R --
431 ----------------------------
433 -- The single argument is converted to its root type to call the
434 -- appropriate runtime function, with the actual call being built
435 -- by Expand_Fpt_Attribute
437 procedure Expand_Fpt_Attribute_R (N : Node_Id) is
438 E1 : constant Node_Id := First (Expressions (N));
442 Find_Fat_Info (Etype (E1), Ftp, Pkg);
444 (N, Pkg, Attribute_Name (N),
445 New_List (Unchecked_Convert_To (Ftp, Relocate_Node (E1))));
446 end Expand_Fpt_Attribute_R;
448 -----------------------------
449 -- Expand_Fpt_Attribute_RI --
450 -----------------------------
452 -- The first argument is converted to its root type and the second
453 -- argument is converted to standard long long integer to call the
454 -- appropriate runtime function, with the actual call being built
455 -- by Expand_Fpt_Attribute
457 procedure Expand_Fpt_Attribute_RI (N : Node_Id) is
458 E1 : constant Node_Id := First (Expressions (N));
461 E2 : constant Node_Id := Next (E1);
463 Find_Fat_Info (Etype (E1), Ftp, Pkg);
465 (N, Pkg, Attribute_Name (N),
467 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
468 Unchecked_Convert_To (Standard_Integer, Relocate_Node (E2))));
469 end Expand_Fpt_Attribute_RI;
471 -----------------------------
472 -- Expand_Fpt_Attribute_RR --
473 -----------------------------
475 -- The two arguments are converted to their root types to call the
476 -- appropriate runtime function, with the actual call being built
477 -- by Expand_Fpt_Attribute
479 procedure Expand_Fpt_Attribute_RR (N : Node_Id) is
480 E1 : constant Node_Id := First (Expressions (N));
483 E2 : constant Node_Id := Next (E1);
485 Find_Fat_Info (Etype (E1), Ftp, Pkg);
487 (N, Pkg, Attribute_Name (N),
489 Unchecked_Convert_To (Ftp, Relocate_Node (E1)),
490 Unchecked_Convert_To (Ftp, Relocate_Node (E2))));
491 end Expand_Fpt_Attribute_RR;
493 ----------------------------------
494 -- Expand_N_Attribute_Reference --
495 ----------------------------------
497 procedure Expand_N_Attribute_Reference (N : Node_Id) is
498 Loc : constant Source_Ptr := Sloc (N);
499 Typ : constant Entity_Id := Etype (N);
500 Btyp : constant Entity_Id := Base_Type (Typ);
501 Pref : constant Node_Id := Prefix (N);
502 Ptyp : constant Entity_Id := Etype (Pref);
503 Exprs : constant List_Id := Expressions (N);
504 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
506 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id);
507 -- Rewrites a stream attribute for Read, Write or Output with the
508 -- procedure call. Pname is the entity for the procedure to call.
510 ------------------------------
511 -- Rewrite_Stream_Proc_Call --
512 ------------------------------
514 procedure Rewrite_Stream_Proc_Call (Pname : Entity_Id) is
515 Item : constant Node_Id := Next (First (Exprs));
516 Formal : constant Entity_Id := Next_Formal (First_Formal (Pname));
517 Formal_Typ : constant Entity_Id := Etype (Formal);
518 Is_Written : constant Boolean := (Ekind (Formal) /= E_In_Parameter);
521 -- The expansion depends on Item, the second actual, which is
522 -- the object being streamed in or out.
524 -- If the item is a component of a packed array type, and
525 -- a conversion is needed on exit, we introduce a temporary to
526 -- hold the value, because otherwise the packed reference will
527 -- not be properly expanded.
529 if Nkind (Item) = N_Indexed_Component
530 and then Is_Packed (Base_Type (Etype (Prefix (Item))))
531 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
535 Temp : constant Entity_Id := Make_Temporary (Loc, 'V');
541 Make_Object_Declaration (Loc,
542 Defining_Identifier => Temp,
544 New_Occurrence_Of (Formal_Typ, Loc));
545 Set_Etype (Temp, Formal_Typ);
548 Make_Assignment_Statement (Loc,
549 Name => New_Copy_Tree (Item),
552 (Etype (Item), New_Occurrence_Of (Temp, Loc)));
554 Rewrite (Item, New_Occurrence_Of (Temp, Loc));
558 Make_Procedure_Call_Statement (Loc,
559 Name => New_Occurrence_Of (Pname, Loc),
560 Parameter_Associations => Exprs),
563 Rewrite (N, Make_Null_Statement (Loc));
568 -- For the class-wide dispatching cases, and for cases in which
569 -- the base type of the second argument matches the base type of
570 -- the corresponding formal parameter (that is to say the stream
571 -- operation is not inherited), we are all set, and can use the
572 -- argument unchanged.
574 -- For all other cases we do an unchecked conversion of the second
575 -- parameter to the type of the formal of the procedure we are
576 -- calling. This deals with the private type cases, and with going
577 -- to the root type as required in elementary type case.
579 if not Is_Class_Wide_Type (Entity (Pref))
580 and then not Is_Class_Wide_Type (Etype (Item))
581 and then Base_Type (Etype (Item)) /= Base_Type (Formal_Typ)
584 Unchecked_Convert_To (Formal_Typ, Relocate_Node (Item)));
586 -- For untagged derived types set Assignment_OK, to prevent
587 -- copies from being created when the unchecked conversion
588 -- is expanded (which would happen in Remove_Side_Effects
589 -- if Expand_N_Unchecked_Conversion were allowed to call
590 -- Force_Evaluation). The copy could violate Ada semantics
591 -- in cases such as an actual that is an out parameter.
592 -- Note that this approach is also used in exp_ch7 for calls
593 -- to controlled type operations to prevent problems with
594 -- actuals wrapped in unchecked conversions.
596 if Is_Untagged_Derivation (Etype (Expression (Item))) then
597 Set_Assignment_OK (Item);
601 -- The stream operation to call maybe a renaming created by
602 -- an attribute definition clause, and may not be frozen yet.
603 -- Ensure that it has the necessary extra formals.
605 if not Is_Frozen (Pname) then
606 Create_Extra_Formals (Pname);
609 -- And now rewrite the call
612 Make_Procedure_Call_Statement (Loc,
613 Name => New_Occurrence_Of (Pname, Loc),
614 Parameter_Associations => Exprs));
617 end Rewrite_Stream_Proc_Call;
619 -- Start of processing for Expand_N_Attribute_Reference
622 -- Do required validity checking, if enabled. Do not apply check to
623 -- output parameters of an Asm instruction, since the value of this
624 -- is not set till after the attribute has been elaborated, and do
625 -- not apply the check to the arguments of a 'Read or 'Input attribute
626 -- reference since the scalar argument is an OUT scalar.
628 if Validity_Checks_On and then Validity_Check_Operands
629 and then Id /= Attribute_Asm_Output
630 and then Id /= Attribute_Read
631 and then Id /= Attribute_Input
636 Expr := First (Expressions (N));
637 while Present (Expr) loop
644 -- Ada 2005 (AI-318-02): If attribute prefix is a call to a build-in-
645 -- place function, then a temporary return object needs to be created
646 -- and access to it must be passed to the function. Currently we limit
647 -- such functions to those with inherently limited result subtypes, but
648 -- eventually we plan to expand the functions that are treated as
649 -- build-in-place to include other composite result types.
651 if Ada_Version >= Ada_05
652 and then Is_Build_In_Place_Function_Call (Pref)
654 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
657 -- If prefix is a protected type name, this is a reference to the
658 -- current instance of the type. For a component definition, nothing
659 -- to do (expansion will occur in the init proc). In other contexts,
660 -- rewrite into reference to current instance.
662 if Is_Protected_Self_Reference (Pref)
664 (Nkind_In (Parent (N), N_Index_Or_Discriminant_Constraint,
665 N_Discriminant_Association)
666 and then Nkind (Parent (Parent (Parent (Parent (N))))) =
667 N_Component_Definition)
669 Rewrite (Pref, Concurrent_Ref (Pref));
673 -- Remaining processing depends on specific attribute
681 when Attribute_Access |
682 Attribute_Unchecked_Access |
683 Attribute_Unrestricted_Access =>
685 Access_Cases : declare
686 Ref_Object : constant Node_Id := Get_Referenced_Object (Pref);
687 Btyp_DDT : Entity_Id;
689 function Enclosing_Object (N : Node_Id) return Node_Id;
690 -- If N denotes a compound name (selected component, indexed
691 -- component, or slice), returns the name of the outermost such
692 -- enclosing object. Otherwise returns N. If the object is a
693 -- renaming, then the renamed object is returned.
695 ----------------------
696 -- Enclosing_Object --
697 ----------------------
699 function Enclosing_Object (N : Node_Id) return Node_Id is
704 while Nkind_In (Obj_Name, N_Selected_Component,
708 Obj_Name := Prefix (Obj_Name);
711 return Get_Referenced_Object (Obj_Name);
712 end Enclosing_Object;
714 -- Local declarations
716 Enc_Object : constant Node_Id := Enclosing_Object (Ref_Object);
718 -- Start of processing for Access_Cases
721 Btyp_DDT := Designated_Type (Btyp);
723 -- Handle designated types that come from the limited view
725 if Ekind (Btyp_DDT) = E_Incomplete_Type
726 and then From_With_Type (Btyp_DDT)
727 and then Present (Non_Limited_View (Btyp_DDT))
729 Btyp_DDT := Non_Limited_View (Btyp_DDT);
731 elsif Is_Class_Wide_Type (Btyp_DDT)
732 and then Ekind (Etype (Btyp_DDT)) = E_Incomplete_Type
733 and then From_With_Type (Etype (Btyp_DDT))
734 and then Present (Non_Limited_View (Etype (Btyp_DDT)))
735 and then Present (Class_Wide_Type
736 (Non_Limited_View (Etype (Btyp_DDT))))
739 Class_Wide_Type (Non_Limited_View (Etype (Btyp_DDT)));
742 -- In order to improve the text of error messages, the designated
743 -- type of access-to-subprogram itypes is set by the semantics as
744 -- the associated subprogram entity (see sem_attr). Now we replace
745 -- such node with the proper E_Subprogram_Type itype.
747 if Id = Attribute_Unrestricted_Access
748 and then Is_Subprogram (Directly_Designated_Type (Typ))
750 -- The following conditions ensure that this special management
751 -- is done only for "Address!(Prim'Unrestricted_Access)" nodes.
752 -- At this stage other cases in which the designated type is
753 -- still a subprogram (instead of an E_Subprogram_Type) are
754 -- wrong because the semantics must have overridden the type of
755 -- the node with the type imposed by the context.
757 if Nkind (Parent (N)) = N_Unchecked_Type_Conversion
758 and then Etype (Parent (N)) = RTE (RE_Prim_Ptr)
760 Set_Etype (N, RTE (RE_Prim_Ptr));
764 Subp : constant Entity_Id :=
765 Directly_Designated_Type (Typ);
767 Extra : Entity_Id := Empty;
768 New_Formal : Entity_Id;
769 Old_Formal : Entity_Id := First_Formal (Subp);
770 Subp_Typ : Entity_Id;
773 Subp_Typ := Create_Itype (E_Subprogram_Type, N);
774 Set_Etype (Subp_Typ, Etype (Subp));
775 Set_Returns_By_Ref (Subp_Typ, Returns_By_Ref (Subp));
777 if Present (Old_Formal) then
778 New_Formal := New_Copy (Old_Formal);
779 Set_First_Entity (Subp_Typ, New_Formal);
782 Set_Scope (New_Formal, Subp_Typ);
783 Etyp := Etype (New_Formal);
785 -- Handle itypes. There is no need to duplicate
786 -- here the itypes associated with record types
787 -- (i.e the implicit full view of private types).
790 and then Ekind (Base_Type (Etyp)) /= E_Record_Type
792 Extra := New_Copy (Etyp);
793 Set_Parent (Extra, New_Formal);
794 Set_Etype (New_Formal, Extra);
795 Set_Scope (Extra, Subp_Typ);
799 Next_Formal (Old_Formal);
800 exit when No (Old_Formal);
802 Set_Next_Entity (New_Formal,
803 New_Copy (Old_Formal));
804 Next_Entity (New_Formal);
807 Set_Next_Entity (New_Formal, Empty);
808 Set_Last_Entity (Subp_Typ, Extra);
811 -- Now that the explicit formals have been duplicated,
812 -- any extra formals needed by the subprogram must be
815 if Present (Extra) then
816 Set_Extra_Formal (Extra, Empty);
819 Create_Extra_Formals (Subp_Typ);
820 Set_Directly_Designated_Type (Typ, Subp_Typ);
825 if Is_Access_Protected_Subprogram_Type (Btyp) then
826 Expand_Access_To_Protected_Op (N, Pref, Typ);
828 -- If prefix is a type name, this is a reference to the current
829 -- instance of the type, within its initialization procedure.
831 elsif Is_Entity_Name (Pref)
832 and then Is_Type (Entity (Pref))
839 -- If the current instance name denotes a task type, then
840 -- the access attribute is rewritten to be the name of the
841 -- "_task" parameter associated with the task type's task
842 -- procedure. An unchecked conversion is applied to ensure
843 -- a type match in cases of expander-generated calls (e.g.
846 if Is_Task_Type (Entity (Pref)) then
848 First_Entity (Get_Task_Body_Procedure (Entity (Pref)));
849 while Present (Formal) loop
850 exit when Chars (Formal) = Name_uTask;
851 Next_Entity (Formal);
854 pragma Assert (Present (Formal));
857 Unchecked_Convert_To (Typ,
858 New_Occurrence_Of (Formal, Loc)));
861 -- The expression must appear in a default expression,
862 -- (which in the initialization procedure is the
863 -- right-hand side of an assignment), and not in a
864 -- discriminant constraint.
868 while Present (Par) loop
869 exit when Nkind (Par) = N_Assignment_Statement;
871 if Nkind (Par) = N_Component_Declaration then
878 if Present (Par) then
880 Make_Attribute_Reference (Loc,
881 Prefix => Make_Identifier (Loc, Name_uInit),
882 Attribute_Name => Attribute_Name (N)));
884 Analyze_And_Resolve (N, Typ);
889 -- If the prefix of an Access attribute is a dereference of an
890 -- access parameter (or a renaming of such a dereference, or a
891 -- subcomponent of such a dereference) and the context is a
892 -- general access type (including the type of an object or
893 -- component with an access_definition, but not the anonymous
894 -- type of an access parameter or access discriminant), then
895 -- apply an accessibility check to the access parameter. We used
896 -- to rewrite the access parameter as a type conversion, but that
897 -- could only be done if the immediate prefix of the Access
898 -- attribute was the dereference, and didn't handle cases where
899 -- the attribute is applied to a subcomponent of the dereference,
900 -- since there's generally no available, appropriate access type
901 -- to convert to in that case. The attribute is passed as the
902 -- point to insert the check, because the access parameter may
903 -- come from a renaming, possibly in a different scope, and the
904 -- check must be associated with the attribute itself.
906 elsif Id = Attribute_Access
907 and then Nkind (Enc_Object) = N_Explicit_Dereference
908 and then Is_Entity_Name (Prefix (Enc_Object))
909 and then (Ekind (Btyp) = E_General_Access_Type
910 or else Is_Local_Anonymous_Access (Btyp))
911 and then Ekind (Entity (Prefix (Enc_Object))) in Formal_Kind
912 and then Ekind (Etype (Entity (Prefix (Enc_Object))))
913 = E_Anonymous_Access_Type
914 and then Present (Extra_Accessibility
915 (Entity (Prefix (Enc_Object))))
917 Apply_Accessibility_Check (Prefix (Enc_Object), Typ, N);
919 -- Ada 2005 (AI-251): If the designated type is an interface we
920 -- add an implicit conversion to force the displacement of the
921 -- pointer to reference the secondary dispatch table.
923 elsif Is_Interface (Btyp_DDT)
924 and then (Comes_From_Source (N)
925 or else Comes_From_Source (Ref_Object)
926 or else (Nkind (Ref_Object) in N_Has_Chars
927 and then Chars (Ref_Object) = Name_uInit))
929 if Nkind (Ref_Object) /= N_Explicit_Dereference then
931 -- No implicit conversion required if types match, or if
932 -- the prefix is the class_wide_type of the interface. In
933 -- either case passing an object of the interface type has
934 -- already set the pointer correctly.
936 if Btyp_DDT = Etype (Ref_Object)
937 or else (Is_Class_Wide_Type (Etype (Ref_Object))
939 Class_Wide_Type (Btyp_DDT) = Etype (Ref_Object))
945 Convert_To (Btyp_DDT,
946 New_Copy_Tree (Prefix (N))));
948 Analyze_And_Resolve (Prefix (N), Btyp_DDT);
951 -- When the object is an explicit dereference, convert the
952 -- dereference's prefix.
956 Obj_DDT : constant Entity_Id :=
958 (Directly_Designated_Type
959 (Etype (Prefix (Ref_Object))));
961 -- No implicit conversion required if designated types
964 if Obj_DDT /= Btyp_DDT
965 and then not (Is_Class_Wide_Type (Obj_DDT)
966 and then Etype (Obj_DDT) = Btyp_DDT)
970 New_Copy_Tree (Prefix (Ref_Object))));
971 Analyze_And_Resolve (N, Typ);
982 -- Transforms 'Adjacent into a call to the floating-point attribute
983 -- function Adjacent in Fat_xxx (where xxx is the root type)
985 when Attribute_Adjacent =>
986 Expand_Fpt_Attribute_RR (N);
992 when Attribute_Address => Address : declare
993 Task_Proc : Entity_Id;
996 -- If the prefix is a task or a task type, the useful address is that
997 -- of the procedure for the task body, i.e. the actual program unit.
998 -- We replace the original entity with that of the procedure.
1000 if Is_Entity_Name (Pref)
1001 and then Is_Task_Type (Entity (Pref))
1003 Task_Proc := Next_Entity (Root_Type (Ptyp));
1005 while Present (Task_Proc) loop
1006 exit when Ekind (Task_Proc) = E_Procedure
1007 and then Etype (First_Formal (Task_Proc)) =
1008 Corresponding_Record_Type (Ptyp);
1009 Next_Entity (Task_Proc);
1012 if Present (Task_Proc) then
1013 Set_Entity (Pref, Task_Proc);
1014 Set_Etype (Pref, Etype (Task_Proc));
1017 -- Similarly, the address of a protected operation is the address
1018 -- of the corresponding protected body, regardless of the protected
1019 -- object from which it is selected.
1021 elsif Nkind (Pref) = N_Selected_Component
1022 and then Is_Subprogram (Entity (Selector_Name (Pref)))
1023 and then Is_Protected_Type (Scope (Entity (Selector_Name (Pref))))
1027 External_Subprogram (Entity (Selector_Name (Pref))), Loc));
1029 elsif Nkind (Pref) = N_Explicit_Dereference
1030 and then Ekind (Ptyp) = E_Subprogram_Type
1031 and then Convention (Ptyp) = Convention_Protected
1033 -- The prefix is be a dereference of an access_to_protected_
1034 -- subprogram. The desired address is the second component of
1035 -- the record that represents the access.
1038 Addr : constant Entity_Id := Etype (N);
1039 Ptr : constant Node_Id := Prefix (Pref);
1040 T : constant Entity_Id :=
1041 Equivalent_Type (Base_Type (Etype (Ptr)));
1045 Unchecked_Convert_To (Addr,
1046 Make_Selected_Component (Loc,
1047 Prefix => Unchecked_Convert_To (T, Ptr),
1048 Selector_Name => New_Occurrence_Of (
1049 Next_Entity (First_Entity (T)), Loc))));
1051 Analyze_And_Resolve (N, Addr);
1054 -- Ada 2005 (AI-251): Class-wide interface objects are always
1055 -- "displaced" to reference the tag associated with the interface
1056 -- type. In order to obtain the real address of such objects we
1057 -- generate a call to a run-time subprogram that returns the base
1058 -- address of the object.
1060 -- This processing is not needed in the VM case, where dispatching
1061 -- issues are taken care of by the virtual machine.
1063 elsif Is_Class_Wide_Type (Ptyp)
1064 and then Is_Interface (Ptyp)
1065 and then Tagged_Type_Expansion
1066 and then not (Nkind (Pref) in N_Has_Entity
1067 and then Is_Subprogram (Entity (Pref)))
1070 Make_Function_Call (Loc,
1071 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1072 Parameter_Associations => New_List (
1073 Relocate_Node (N))));
1078 -- Deal with packed array reference, other cases are handled by
1081 if Involves_Packed_Array_Reference (Pref) then
1082 Expand_Packed_Address_Reference (N);
1090 when Attribute_Alignment => Alignment : declare
1094 -- For class-wide types, X'Class'Alignment is transformed into a
1095 -- direct reference to the Alignment of the class type, so that the
1096 -- back end does not have to deal with the X'Class'Alignment
1099 if Is_Entity_Name (Pref)
1100 and then Is_Class_Wide_Type (Entity (Pref))
1102 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
1105 -- For x'Alignment applied to an object of a class wide type,
1106 -- transform X'Alignment into a call to the predefined primitive
1107 -- operation _Alignment applied to X.
1109 elsif Is_Class_Wide_Type (Ptyp) then
1111 -- No need to do anything else compiling under restriction
1112 -- No_Dispatching_Calls. During the semantic analysis we
1113 -- already notified such violation.
1115 if Restriction_Active (No_Dispatching_Calls) then
1120 Make_Function_Call (Loc,
1121 Name => New_Reference_To
1122 (Find_Prim_Op (Ptyp, Name_uAlignment), Loc),
1123 Parameter_Associations => New_List (Pref));
1125 if Typ /= Standard_Integer then
1127 -- The context is a specific integer type with which the
1128 -- original attribute was compatible. The function has a
1129 -- specific type as well, so to preserve the compatibility
1130 -- we must convert explicitly.
1132 New_Node := Convert_To (Typ, New_Node);
1135 Rewrite (N, New_Node);
1136 Analyze_And_Resolve (N, Typ);
1139 -- For all other cases, we just have to deal with the case of
1140 -- the fact that the result can be universal.
1143 Apply_Universal_Integer_Attribute_Checks (N);
1151 when Attribute_AST_Entry => AST_Entry : declare
1156 Entry_Ref : Node_Id;
1157 -- The reference to the entry or entry family
1160 -- The index expression for an entry family reference, or
1161 -- the Empty if Entry_Ref references a simple entry.
1164 if Nkind (Pref) = N_Indexed_Component then
1165 Entry_Ref := Prefix (Pref);
1166 Index := First (Expressions (Pref));
1172 -- Get expression for Task_Id and the entry entity
1174 if Nkind (Entry_Ref) = N_Selected_Component then
1176 Make_Attribute_Reference (Loc,
1177 Attribute_Name => Name_Identity,
1178 Prefix => Prefix (Entry_Ref));
1180 Ttyp := Etype (Prefix (Entry_Ref));
1181 Eent := Entity (Selector_Name (Entry_Ref));
1185 Make_Function_Call (Loc,
1186 Name => New_Occurrence_Of (RTE (RE_Current_Task), Loc));
1188 Eent := Entity (Entry_Ref);
1190 -- We have to find the enclosing task to get the task type
1191 -- There must be one, since we already validated this earlier
1193 Ttyp := Current_Scope;
1194 while not Is_Task_Type (Ttyp) loop
1195 Ttyp := Scope (Ttyp);
1199 -- Now rewrite the attribute with a call to Create_AST_Handler
1202 Make_Function_Call (Loc,
1203 Name => New_Occurrence_Of (RTE (RE_Create_AST_Handler), Loc),
1204 Parameter_Associations => New_List (
1206 Entry_Index_Expression (Loc, Eent, Index, Ttyp))));
1208 Analyze_And_Resolve (N, RTE (RE_AST_Handler));
1215 -- We compute this if a packed array reference was present, otherwise we
1216 -- leave the computation up to the back end.
1218 when Attribute_Bit =>
1219 if Involves_Packed_Array_Reference (Pref) then
1220 Expand_Packed_Bit_Reference (N);
1222 Apply_Universal_Integer_Attribute_Checks (N);
1229 -- We compute this if a component clause was present, otherwise we leave
1230 -- the computation up to the back end, since we don't know what layout
1233 -- Note that the attribute can apply to a naked record component
1234 -- in generated code (i.e. the prefix is an identifier that
1235 -- references the component or discriminant entity).
1237 when Attribute_Bit_Position => Bit_Position : declare
1241 if Nkind (Pref) = N_Identifier then
1242 CE := Entity (Pref);
1244 CE := Entity (Selector_Name (Pref));
1247 if Known_Static_Component_Bit_Offset (CE) then
1249 Make_Integer_Literal (Loc,
1250 Intval => Component_Bit_Offset (CE)));
1251 Analyze_And_Resolve (N, Typ);
1254 Apply_Universal_Integer_Attribute_Checks (N);
1262 -- A reference to P'Body_Version or P'Version is expanded to
1265 -- pragma Import (C, Vnn, "uuuuT");
1267 -- Get_Version_String (Vnn)
1269 -- where uuuu is the unit name (dots replaced by double underscore)
1270 -- and T is B for the cases of Body_Version, or Version applied to a
1271 -- subprogram acting as its own spec, and S for Version applied to a
1272 -- subprogram spec or package. This sequence of code references the
1273 -- the unsigned constant created in the main program by the binder.
1275 -- A special exception occurs for Standard, where the string returned
1276 -- is a copy of the library string in gnatvsn.ads.
1278 when Attribute_Body_Version | Attribute_Version => Version : declare
1279 E : constant Entity_Id := Make_Temporary (Loc, 'V');
1284 -- If not library unit, get to containing library unit
1286 Pent := Entity (Pref);
1287 while Pent /= Standard_Standard
1288 and then Scope (Pent) /= Standard_Standard
1289 and then not Is_Child_Unit (Pent)
1291 Pent := Scope (Pent);
1294 -- Special case Standard and Standard.ASCII
1296 if Pent = Standard_Standard or else Pent = Standard_ASCII then
1298 Make_String_Literal (Loc,
1299 Strval => Verbose_Library_Version));
1304 -- Build required string constant
1306 Get_Name_String (Get_Unit_Name (Pent));
1309 for J in 1 .. Name_Len - 2 loop
1310 if Name_Buffer (J) = '.' then
1311 Store_String_Chars ("__");
1313 Store_String_Char (Get_Char_Code (Name_Buffer (J)));
1317 -- Case of subprogram acting as its own spec, always use body
1319 if Nkind (Declaration_Node (Pent)) in N_Subprogram_Specification
1320 and then Nkind (Parent (Declaration_Node (Pent))) =
1322 and then Acts_As_Spec (Parent (Declaration_Node (Pent)))
1324 Store_String_Chars ("B");
1326 -- Case of no body present, always use spec
1328 elsif not Unit_Requires_Body (Pent) then
1329 Store_String_Chars ("S");
1331 -- Otherwise use B for Body_Version, S for spec
1333 elsif Id = Attribute_Body_Version then
1334 Store_String_Chars ("B");
1336 Store_String_Chars ("S");
1340 Lib.Version_Referenced (S);
1342 -- Insert the object declaration
1344 Insert_Actions (N, New_List (
1345 Make_Object_Declaration (Loc,
1346 Defining_Identifier => E,
1347 Object_Definition =>
1348 New_Occurrence_Of (RTE (RE_Unsigned), Loc))));
1350 -- Set entity as imported with correct external name
1352 Set_Is_Imported (E);
1353 Set_Interface_Name (E, Make_String_Literal (Loc, S));
1355 -- Set entity as internal to ensure proper Sprint output of its
1356 -- implicit importation.
1358 Set_Is_Internal (E);
1360 -- And now rewrite original reference
1363 Make_Function_Call (Loc,
1364 Name => New_Reference_To (RTE (RE_Get_Version_String), Loc),
1365 Parameter_Associations => New_List (
1366 New_Occurrence_Of (E, Loc))));
1369 Analyze_And_Resolve (N, RTE (RE_Version_String));
1376 -- Transforms 'Ceiling into a call to the floating-point attribute
1377 -- function Ceiling in Fat_xxx (where xxx is the root type)
1379 when Attribute_Ceiling =>
1380 Expand_Fpt_Attribute_R (N);
1386 -- Transforms 'Callable attribute into a call to the Callable function
1388 when Attribute_Callable => Callable :
1390 -- We have an object of a task interface class-wide type as a prefix
1391 -- to Callable. Generate:
1392 -- callable (Task_Id (Pref._disp_get_task_id));
1394 if Ada_Version >= Ada_05
1395 and then Ekind (Ptyp) = E_Class_Wide_Type
1396 and then Is_Interface (Ptyp)
1397 and then Is_Task_Interface (Ptyp)
1400 Make_Function_Call (Loc,
1402 New_Reference_To (RTE (RE_Callable), Loc),
1403 Parameter_Associations => New_List (
1404 Make_Unchecked_Type_Conversion (Loc,
1406 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
1408 Make_Selected_Component (Loc,
1410 New_Copy_Tree (Pref),
1412 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
1416 Build_Call_With_Task (Pref, RTE (RE_Callable)));
1419 Analyze_And_Resolve (N, Standard_Boolean);
1426 -- Transforms 'Caller attribute into a call to either the
1427 -- Task_Entry_Caller or the Protected_Entry_Caller function.
1429 when Attribute_Caller => Caller : declare
1430 Id_Kind : constant Entity_Id := RTE (RO_AT_Task_Id);
1431 Ent : constant Entity_Id := Entity (Pref);
1432 Conctype : constant Entity_Id := Scope (Ent);
1433 Nest_Depth : Integer := 0;
1440 if Is_Protected_Type (Conctype) then
1441 case Corresponding_Runtime_Package (Conctype) is
1442 when System_Tasking_Protected_Objects_Entries =>
1445 (RTE (RE_Protected_Entry_Caller), Loc);
1447 when System_Tasking_Protected_Objects_Single_Entry =>
1450 (RTE (RE_Protected_Single_Entry_Caller), Loc);
1453 raise Program_Error;
1457 Unchecked_Convert_To (Id_Kind,
1458 Make_Function_Call (Loc,
1460 Parameter_Associations => New_List (
1462 (Find_Protection_Object (Current_Scope), Loc)))));
1467 -- Determine the nesting depth of the E'Caller attribute, that
1468 -- is, how many accept statements are nested within the accept
1469 -- statement for E at the point of E'Caller. The runtime uses
1470 -- this depth to find the specified entry call.
1472 for J in reverse 0 .. Scope_Stack.Last loop
1473 S := Scope_Stack.Table (J).Entity;
1475 -- We should not reach the scope of the entry, as it should
1476 -- already have been checked in Sem_Attr that this attribute
1477 -- reference is within a matching accept statement.
1479 pragma Assert (S /= Conctype);
1484 elsif Is_Entry (S) then
1485 Nest_Depth := Nest_Depth + 1;
1490 Unchecked_Convert_To (Id_Kind,
1491 Make_Function_Call (Loc,
1493 New_Reference_To (RTE (RE_Task_Entry_Caller), Loc),
1494 Parameter_Associations => New_List (
1495 Make_Integer_Literal (Loc,
1496 Intval => Int (Nest_Depth))))));
1499 Analyze_And_Resolve (N, Id_Kind);
1506 -- Transforms 'Compose into a call to the floating-point attribute
1507 -- function Compose in Fat_xxx (where xxx is the root type)
1509 -- Note: we strictly should have special code here to deal with the
1510 -- case of absurdly negative arguments (less than Integer'First)
1511 -- which will return a (signed) zero value, but it hardly seems
1512 -- worth the effort. Absurdly large positive arguments will raise
1513 -- constraint error which is fine.
1515 when Attribute_Compose =>
1516 Expand_Fpt_Attribute_RI (N);
1522 when Attribute_Constrained => Constrained : declare
1523 Formal_Ent : constant Entity_Id := Param_Entity (Pref);
1525 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean;
1526 -- Ada 2005 (AI-363): Returns True if the object name Obj denotes a
1527 -- view of an aliased object whose subtype is constrained.
1529 ---------------------------------
1530 -- Is_Constrained_Aliased_View --
1531 ---------------------------------
1533 function Is_Constrained_Aliased_View (Obj : Node_Id) return Boolean is
1537 if Is_Entity_Name (Obj) then
1540 if Present (Renamed_Object (E)) then
1541 return Is_Constrained_Aliased_View (Renamed_Object (E));
1543 return Is_Aliased (E) and then Is_Constrained (Etype (E));
1547 return Is_Aliased_View (Obj)
1549 (Is_Constrained (Etype (Obj))
1550 or else (Nkind (Obj) = N_Explicit_Dereference
1552 not Has_Constrained_Partial_View
1553 (Base_Type (Etype (Obj)))));
1555 end Is_Constrained_Aliased_View;
1557 -- Start of processing for Constrained
1560 -- Reference to a parameter where the value is passed as an extra
1561 -- actual, corresponding to the extra formal referenced by the
1562 -- Extra_Constrained field of the corresponding formal. If this
1563 -- is an entry in-parameter, it is replaced by a constant renaming
1564 -- for which Extra_Constrained is never created.
1566 if Present (Formal_Ent)
1567 and then Ekind (Formal_Ent) /= E_Constant
1568 and then Present (Extra_Constrained (Formal_Ent))
1572 (Extra_Constrained (Formal_Ent), Sloc (N)));
1574 -- For variables with a Extra_Constrained field, we use the
1575 -- corresponding entity.
1577 elsif Nkind (Pref) = N_Identifier
1578 and then Ekind (Entity (Pref)) = E_Variable
1579 and then Present (Extra_Constrained (Entity (Pref)))
1583 (Extra_Constrained (Entity (Pref)), Sloc (N)));
1585 -- For all other entity names, we can tell at compile time
1587 elsif Is_Entity_Name (Pref) then
1589 Ent : constant Entity_Id := Entity (Pref);
1593 -- (RM J.4) obsolescent cases
1595 if Is_Type (Ent) then
1599 if Is_Private_Type (Ent) then
1600 Res := not Has_Discriminants (Ent)
1601 or else Is_Constrained (Ent);
1603 -- It not a private type, must be a generic actual type
1604 -- that corresponded to a private type. We know that this
1605 -- correspondence holds, since otherwise the reference
1606 -- within the generic template would have been illegal.
1609 if Is_Composite_Type (Underlying_Type (Ent)) then
1610 Res := Is_Constrained (Ent);
1616 -- If the prefix is not a variable or is aliased, then
1617 -- definitely true; if it's a formal parameter without an
1618 -- associated extra formal, then treat it as constrained.
1620 -- Ada 2005 (AI-363): An aliased prefix must be known to be
1621 -- constrained in order to set the attribute to True.
1623 elsif not Is_Variable (Pref)
1624 or else Present (Formal_Ent)
1625 or else (Ada_Version < Ada_05
1626 and then Is_Aliased_View (Pref))
1627 or else (Ada_Version >= Ada_05
1628 and then Is_Constrained_Aliased_View (Pref))
1632 -- Variable case, look at type to see if it is constrained.
1633 -- Note that the one case where this is not accurate (the
1634 -- procedure formal case), has been handled above.
1636 -- We use the Underlying_Type here (and below) in case the
1637 -- type is private without discriminants, but the full type
1638 -- has discriminants. This case is illegal, but we generate it
1639 -- internally for passing to the Extra_Constrained parameter.
1642 Res := Is_Constrained (Underlying_Type (Etype (Ent)));
1646 New_Reference_To (Boolean_Literals (Res), Loc));
1649 -- Prefix is not an entity name. These are also cases where we can
1650 -- always tell at compile time by looking at the form and type of the
1651 -- prefix. If an explicit dereference of an object with constrained
1652 -- partial view, this is unconstrained (Ada 2005 AI-363).
1658 not Is_Variable (Pref)
1660 (Nkind (Pref) = N_Explicit_Dereference
1662 not Has_Constrained_Partial_View (Base_Type (Ptyp)))
1663 or else Is_Constrained (Underlying_Type (Ptyp))),
1667 Analyze_And_Resolve (N, Standard_Boolean);
1674 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1675 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1677 when Attribute_Copy_Sign =>
1678 Expand_Fpt_Attribute_RR (N);
1684 -- Transforms 'Count attribute into a call to the Count function
1686 when Attribute_Count => Count : declare
1688 Conctyp : Entity_Id;
1690 Entry_Id : Entity_Id;
1695 -- If the prefix is a member of an entry family, retrieve both
1696 -- entry name and index. For a simple entry there is no index.
1698 if Nkind (Pref) = N_Indexed_Component then
1699 Entnam := Prefix (Pref);
1700 Index := First (Expressions (Pref));
1706 Entry_Id := Entity (Entnam);
1708 -- Find the concurrent type in which this attribute is referenced
1709 -- (there had better be one).
1711 Conctyp := Current_Scope;
1712 while not Is_Concurrent_Type (Conctyp) loop
1713 Conctyp := Scope (Conctyp);
1718 if Is_Protected_Type (Conctyp) then
1719 case Corresponding_Runtime_Package (Conctyp) is
1720 when System_Tasking_Protected_Objects_Entries =>
1721 Name := New_Reference_To (RTE (RE_Protected_Count), Loc);
1724 Make_Function_Call (Loc,
1726 Parameter_Associations => New_List (
1728 (Find_Protection_Object (Current_Scope), Loc),
1729 Entry_Index_Expression
1730 (Loc, Entry_Id, Index, Scope (Entry_Id))));
1732 when System_Tasking_Protected_Objects_Single_Entry =>
1734 New_Reference_To (RTE (RE_Protected_Count_Entry), Loc);
1737 Make_Function_Call (Loc,
1739 Parameter_Associations => New_List (
1741 (Find_Protection_Object (Current_Scope), Loc)));
1744 raise Program_Error;
1751 Make_Function_Call (Loc,
1752 Name => New_Reference_To (RTE (RE_Task_Count), Loc),
1753 Parameter_Associations => New_List (
1754 Entry_Index_Expression (Loc,
1755 Entry_Id, Index, Scope (Entry_Id))));
1758 -- The call returns type Natural but the context is universal integer
1759 -- so any integer type is allowed. The attribute was already resolved
1760 -- so its Etype is the required result type. If the base type of the
1761 -- context type is other than Standard.Integer we put in a conversion
1762 -- to the required type. This can be a normal typed conversion since
1763 -- both input and output types of the conversion are integer types
1765 if Base_Type (Typ) /= Base_Type (Standard_Integer) then
1766 Rewrite (N, Convert_To (Typ, Call));
1771 Analyze_And_Resolve (N, Typ);
1778 -- This processing is shared by Elab_Spec
1780 -- What we do is to insert the following declarations
1783 -- pragma Import (C, enn, "name___elabb/s");
1785 -- and then the Elab_Body/Spec attribute is replaced by a reference
1786 -- to this defining identifier.
1788 when Attribute_Elab_Body |
1789 Attribute_Elab_Spec =>
1792 Ent : constant Entity_Id := Make_Temporary (Loc, 'E');
1796 procedure Make_Elab_String (Nod : Node_Id);
1797 -- Given Nod, an identifier, or a selected component, put the
1798 -- image into the current string literal, with double underline
1799 -- between components.
1801 ----------------------
1802 -- Make_Elab_String --
1803 ----------------------
1805 procedure Make_Elab_String (Nod : Node_Id) is
1807 if Nkind (Nod) = N_Selected_Component then
1808 Make_Elab_String (Prefix (Nod));
1812 Store_String_Char ('$');
1814 Store_String_Char ('.');
1816 Store_String_Char ('_');
1817 Store_String_Char ('_');
1820 Get_Name_String (Chars (Selector_Name (Nod)));
1823 pragma Assert (Nkind (Nod) = N_Identifier);
1824 Get_Name_String (Chars (Nod));
1827 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1828 end Make_Elab_String;
1830 -- Start of processing for Elab_Body/Elab_Spec
1833 -- First we need to prepare the string literal for the name of
1834 -- the elaboration routine to be referenced.
1837 Make_Elab_String (Pref);
1839 if VM_Target = No_VM then
1840 Store_String_Chars ("___elab");
1841 Lang := Make_Identifier (Loc, Name_C);
1843 Store_String_Chars ("._elab");
1844 Lang := Make_Identifier (Loc, Name_Ada);
1847 if Id = Attribute_Elab_Body then
1848 Store_String_Char ('b');
1850 Store_String_Char ('s');
1855 Insert_Actions (N, New_List (
1856 Make_Subprogram_Declaration (Loc,
1858 Make_Procedure_Specification (Loc,
1859 Defining_Unit_Name => Ent)),
1862 Chars => Name_Import,
1863 Pragma_Argument_Associations => New_List (
1864 Make_Pragma_Argument_Association (Loc,
1865 Expression => Lang),
1867 Make_Pragma_Argument_Association (Loc,
1869 Make_Identifier (Loc, Chars (Ent))),
1871 Make_Pragma_Argument_Association (Loc,
1873 Make_String_Literal (Loc, Str))))));
1875 Set_Entity (N, Ent);
1876 Rewrite (N, New_Occurrence_Of (Ent, Loc));
1883 -- Elaborated is always True for preelaborated units, predefined units,
1884 -- pure units and units which have Elaborate_Body pragmas. These units
1885 -- have no elaboration entity.
1887 -- Note: The Elaborated attribute is never passed to the back end
1889 when Attribute_Elaborated => Elaborated : declare
1890 Ent : constant Entity_Id := Entity (Pref);
1893 if Present (Elaboration_Entity (Ent)) then
1895 New_Occurrence_Of (Elaboration_Entity (Ent), Loc));
1897 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
1905 when Attribute_Enum_Rep => Enum_Rep :
1907 -- X'Enum_Rep (Y) expands to
1911 -- This is simply a direct conversion from the enumeration type to
1912 -- the target integer type, which is treated by the back end as a
1913 -- normal integer conversion, treating the enumeration type as an
1914 -- integer, which is exactly what we want! We set Conversion_OK to
1915 -- make sure that the analyzer does not complain about what otherwise
1916 -- might be an illegal conversion.
1918 if Is_Non_Empty_List (Exprs) then
1920 OK_Convert_To (Typ, Relocate_Node (First (Exprs))));
1922 -- X'Enum_Rep where X is an enumeration literal is replaced by
1923 -- the literal value.
1925 elsif Ekind (Entity (Pref)) = E_Enumeration_Literal then
1927 Make_Integer_Literal (Loc, Enumeration_Rep (Entity (Pref))));
1929 -- If this is a renaming of a literal, recover the representation
1932 elsif Ekind (Entity (Pref)) = E_Constant
1933 and then Present (Renamed_Object (Entity (Pref)))
1935 Ekind (Entity (Renamed_Object (Entity (Pref))))
1936 = E_Enumeration_Literal
1939 Make_Integer_Literal (Loc,
1940 Enumeration_Rep (Entity (Renamed_Object (Entity (Pref))))));
1942 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1943 -- of the object value, as described for the type case above.
1947 OK_Convert_To (Typ, Relocate_Node (Pref)));
1951 Analyze_And_Resolve (N, Typ);
1958 when Attribute_Enum_Val => Enum_Val : declare
1960 Btyp : constant Entity_Id := Base_Type (Ptyp);
1963 -- X'Enum_Val (Y) expands to
1965 -- [constraint_error when _rep_to_pos (Y, False) = -1, msg]
1968 Expr := Unchecked_Convert_To (Ptyp, First (Exprs));
1971 Make_Raise_Constraint_Error (Loc,
1975 Make_Function_Call (Loc,
1977 New_Reference_To (TSS (Btyp, TSS_Rep_To_Pos), Loc),
1978 Parameter_Associations => New_List (
1979 Relocate_Node (Duplicate_Subexpr (Expr)),
1980 New_Occurrence_Of (Standard_False, Loc))),
1982 Right_Opnd => Make_Integer_Literal (Loc, -1)),
1983 Reason => CE_Range_Check_Failed));
1986 Analyze_And_Resolve (N, Ptyp);
1993 -- Transforms 'Exponent into a call to the floating-point attribute
1994 -- function Exponent in Fat_xxx (where xxx is the root type)
1996 when Attribute_Exponent =>
1997 Expand_Fpt_Attribute_R (N);
2003 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
2005 when Attribute_External_Tag => External_Tag :
2008 Make_Function_Call (Loc,
2009 Name => New_Reference_To (RTE (RE_External_Tag), Loc),
2010 Parameter_Associations => New_List (
2011 Make_Attribute_Reference (Loc,
2012 Attribute_Name => Name_Tag,
2013 Prefix => Prefix (N)))));
2015 Analyze_And_Resolve (N, Standard_String);
2022 when Attribute_First =>
2024 -- If the prefix type is a constrained packed array type which
2025 -- already has a Packed_Array_Type representation defined, then
2026 -- replace this attribute with a direct reference to 'First of the
2027 -- appropriate index subtype (since otherwise the back end will try
2028 -- to give us the value of 'First for this implementation type).
2030 if Is_Constrained_Packed_Array (Ptyp) then
2032 Make_Attribute_Reference (Loc,
2033 Attribute_Name => Name_First,
2034 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2035 Analyze_And_Resolve (N, Typ);
2037 elsif Is_Access_Type (Ptyp) then
2038 Apply_Access_Check (N);
2045 -- Compute this if component clause was present, otherwise we leave the
2046 -- computation to be completed in the back-end, since we don't know what
2047 -- layout will be chosen.
2049 when Attribute_First_Bit => First_Bit : declare
2050 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2053 if Known_Static_Component_Bit_Offset (CE) then
2055 Make_Integer_Literal (Loc,
2056 Component_Bit_Offset (CE) mod System_Storage_Unit));
2058 Analyze_And_Resolve (N, Typ);
2061 Apply_Universal_Integer_Attribute_Checks (N);
2071 -- fixtype'Fixed_Value (integer-value)
2075 -- fixtype(integer-value)
2077 -- We do all the required analysis of the conversion here, because we do
2078 -- not want this to go through the fixed-point conversion circuits. Note
2079 -- that the back end always treats fixed-point as equivalent to the
2080 -- corresponding integer type anyway.
2082 when Attribute_Fixed_Value => Fixed_Value :
2085 Make_Type_Conversion (Loc,
2086 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2087 Expression => Relocate_Node (First (Exprs))));
2088 Set_Etype (N, Entity (Pref));
2091 -- Note: it might appear that a properly analyzed unchecked conversion
2092 -- would be just fine here, but that's not the case, since the full
2093 -- range checks performed by the following call are critical!
2095 Apply_Type_Conversion_Checks (N);
2102 -- Transforms 'Floor into a call to the floating-point attribute
2103 -- function Floor in Fat_xxx (where xxx is the root type)
2105 when Attribute_Floor =>
2106 Expand_Fpt_Attribute_R (N);
2112 -- For the fixed-point type Typ:
2118 -- Result_Type (System.Fore (Universal_Real (Type'First)),
2119 -- Universal_Real (Type'Last))
2121 -- Note that we know that the type is a non-static subtype, or Fore
2122 -- would have itself been computed dynamically in Eval_Attribute.
2124 when Attribute_Fore => Fore : begin
2127 Make_Function_Call (Loc,
2128 Name => New_Reference_To (RTE (RE_Fore), Loc),
2130 Parameter_Associations => New_List (
2131 Convert_To (Universal_Real,
2132 Make_Attribute_Reference (Loc,
2133 Prefix => New_Reference_To (Ptyp, Loc),
2134 Attribute_Name => Name_First)),
2136 Convert_To (Universal_Real,
2137 Make_Attribute_Reference (Loc,
2138 Prefix => New_Reference_To (Ptyp, Loc),
2139 Attribute_Name => Name_Last))))));
2141 Analyze_And_Resolve (N, Typ);
2148 -- Transforms 'Fraction into a call to the floating-point attribute
2149 -- function Fraction in Fat_xxx (where xxx is the root type)
2151 when Attribute_Fraction =>
2152 Expand_Fpt_Attribute_R (N);
2158 when Attribute_From_Any => From_Any : declare
2159 P_Type : constant Entity_Id := Etype (Pref);
2160 Decls : constant List_Id := New_List;
2163 Build_From_Any_Call (P_Type,
2164 Relocate_Node (First (Exprs)),
2166 Insert_Actions (N, Decls);
2167 Analyze_And_Resolve (N, P_Type);
2174 -- For an exception returns a reference to the exception data:
2175 -- Exception_Id!(Prefix'Reference)
2177 -- For a task it returns a reference to the _task_id component of
2178 -- corresponding record:
2180 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
2182 -- in Ada.Task_Identification
2184 when Attribute_Identity => Identity : declare
2185 Id_Kind : Entity_Id;
2188 if Ptyp = Standard_Exception_Type then
2189 Id_Kind := RTE (RE_Exception_Id);
2191 if Present (Renamed_Object (Entity (Pref))) then
2192 Set_Entity (Pref, Renamed_Object (Entity (Pref)));
2196 Unchecked_Convert_To (Id_Kind, Make_Reference (Loc, Pref)));
2198 Id_Kind := RTE (RO_AT_Task_Id);
2200 -- If the prefix is a task interface, the Task_Id is obtained
2201 -- dynamically through a dispatching call, as for other task
2202 -- attributes applied to interfaces.
2204 if Ada_Version >= Ada_05
2205 and then Ekind (Ptyp) = E_Class_Wide_Type
2206 and then Is_Interface (Ptyp)
2207 and then Is_Task_Interface (Ptyp)
2210 Unchecked_Convert_To (Id_Kind,
2211 Make_Selected_Component (Loc,
2213 New_Copy_Tree (Pref),
2215 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))));
2219 Unchecked_Convert_To (Id_Kind, Concurrent_Ref (Pref)));
2223 Analyze_And_Resolve (N, Id_Kind);
2230 -- Image attribute is handled in separate unit Exp_Imgv
2232 when Attribute_Image =>
2233 Exp_Imgv.Expand_Image_Attribute (N);
2239 -- X'Img is expanded to typ'Image (X), where typ is the type of X
2241 when Attribute_Img => Img :
2244 Make_Attribute_Reference (Loc,
2245 Prefix => New_Reference_To (Ptyp, Loc),
2246 Attribute_Name => Name_Image,
2247 Expressions => New_List (Relocate_Node (Pref))));
2249 Analyze_And_Resolve (N, Standard_String);
2256 when Attribute_Input => Input : declare
2257 P_Type : constant Entity_Id := Entity (Pref);
2258 B_Type : constant Entity_Id := Base_Type (P_Type);
2259 U_Type : constant Entity_Id := Underlying_Type (P_Type);
2260 Strm : constant Node_Id := First (Exprs);
2268 Cntrl : Node_Id := Empty;
2269 -- Value for controlling argument in call. Always Empty except in
2270 -- the dispatching (class-wide type) case, where it is a reference
2271 -- to the dummy object initialized to the right internal tag.
2273 procedure Freeze_Stream_Subprogram (F : Entity_Id);
2274 -- The expansion of the attribute reference may generate a call to
2275 -- a user-defined stream subprogram that is frozen by the call. This
2276 -- can lead to access-before-elaboration problem if the reference
2277 -- appears in an object declaration and the subprogram body has not
2278 -- been seen. The freezing of the subprogram requires special code
2279 -- because it appears in an expanded context where expressions do
2280 -- not freeze their constituents.
2282 ------------------------------
2283 -- Freeze_Stream_Subprogram --
2284 ------------------------------
2286 procedure Freeze_Stream_Subprogram (F : Entity_Id) is
2287 Decl : constant Node_Id := Unit_Declaration_Node (F);
2291 -- If this is user-defined subprogram, the corresponding
2292 -- stream function appears as a renaming-as-body, and the
2293 -- user subprogram must be retrieved by tree traversal.
2296 and then Nkind (Decl) = N_Subprogram_Declaration
2297 and then Present (Corresponding_Body (Decl))
2299 Bod := Corresponding_Body (Decl);
2301 if Nkind (Unit_Declaration_Node (Bod)) =
2302 N_Subprogram_Renaming_Declaration
2304 Set_Is_Frozen (Entity (Name (Unit_Declaration_Node (Bod))));
2307 end Freeze_Stream_Subprogram;
2309 -- Start of processing for Input
2312 -- If no underlying type, we have an error that will be diagnosed
2313 -- elsewhere, so here we just completely ignore the expansion.
2319 -- If there is a TSS for Input, just call it
2321 Fname := Find_Stream_Subprogram (P_Type, TSS_Stream_Input);
2323 if Present (Fname) then
2327 -- If there is a Stream_Convert pragma, use it, we rewrite
2329 -- sourcetyp'Input (stream)
2333 -- sourcetyp (streamread (strmtyp'Input (stream)));
2335 -- where streamread is the given Read function that converts an
2336 -- argument of type strmtyp to type sourcetyp or a type from which
2337 -- it is derived (extra conversion required for the derived case).
2339 Prag := Get_Stream_Convert_Pragma (P_Type);
2341 if Present (Prag) then
2342 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
2343 Rfunc := Entity (Expression (Arg2));
2347 Make_Function_Call (Loc,
2348 Name => New_Occurrence_Of (Rfunc, Loc),
2349 Parameter_Associations => New_List (
2350 Make_Attribute_Reference (Loc,
2353 (Etype (First_Formal (Rfunc)), Loc),
2354 Attribute_Name => Name_Input,
2355 Expressions => Exprs)))));
2357 Analyze_And_Resolve (N, B_Type);
2362 elsif Is_Elementary_Type (U_Type) then
2364 -- A special case arises if we have a defined _Read routine,
2365 -- since in this case we are required to call this routine.
2367 if Present (TSS (Base_Type (U_Type), TSS_Stream_Read)) then
2368 Build_Record_Or_Elementary_Input_Function
2369 (Loc, U_Type, Decl, Fname);
2370 Insert_Action (N, Decl);
2372 -- For normal cases, we call the I_xxx routine directly
2375 Rewrite (N, Build_Elementary_Input_Call (N));
2376 Analyze_And_Resolve (N, P_Type);
2382 elsif Is_Array_Type (U_Type) then
2383 Build_Array_Input_Function (Loc, U_Type, Decl, Fname);
2384 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
2386 -- Dispatching case with class-wide type
2388 elsif Is_Class_Wide_Type (P_Type) then
2390 -- No need to do anything else compiling under restriction
2391 -- No_Dispatching_Calls. During the semantic analysis we
2392 -- already notified such violation.
2394 if Restriction_Active (No_Dispatching_Calls) then
2399 Rtyp : constant Entity_Id := Root_Type (P_Type);
2405 -- Read the internal tag (RM 13.13.2(34)) and use it to
2406 -- initialize a dummy tag object:
2408 -- Dnn : Ada.Tags.Tag :=
2409 -- Descendant_Tag (String'Input (Strm), P_Type);
2411 -- This dummy object is used only to provide a controlling
2412 -- argument for the eventual _Input call. Descendant_Tag is
2413 -- called rather than Internal_Tag to ensure that we have a
2414 -- tag for a type that is descended from the prefix type and
2415 -- declared at the same accessibility level (the exception
2416 -- Tag_Error will be raised otherwise). The level check is
2417 -- required for Ada 2005 because tagged types can be
2418 -- extended in nested scopes (AI-344).
2421 Make_Function_Call (Loc,
2423 New_Occurrence_Of (RTE (RE_Descendant_Tag), Loc),
2424 Parameter_Associations => New_List (
2425 Make_Attribute_Reference (Loc,
2426 Prefix => New_Occurrence_Of (Standard_String, Loc),
2427 Attribute_Name => Name_Input,
2428 Expressions => New_List (
2429 Relocate_Node (Duplicate_Subexpr (Strm)))),
2430 Make_Attribute_Reference (Loc,
2431 Prefix => New_Reference_To (P_Type, Loc),
2432 Attribute_Name => Name_Tag)));
2434 Dnn := Make_Temporary (Loc, 'D', Expr);
2437 Make_Object_Declaration (Loc,
2438 Defining_Identifier => Dnn,
2439 Object_Definition =>
2440 New_Occurrence_Of (RTE (RE_Tag), Loc),
2441 Expression => Expr);
2443 Insert_Action (N, Decl);
2445 -- Now we need to get the entity for the call, and construct
2446 -- a function call node, where we preset a reference to Dnn
2447 -- as the controlling argument (doing an unchecked convert
2448 -- to the class-wide tagged type to make it look like a real
2451 Fname := Find_Prim_Op (Rtyp, TSS_Stream_Input);
2453 Unchecked_Convert_To (P_Type,
2454 New_Occurrence_Of (Dnn, Loc));
2455 Set_Etype (Cntrl, P_Type);
2456 Set_Parent (Cntrl, N);
2459 -- For tagged types, use the primitive Input function
2461 elsif Is_Tagged_Type (U_Type) then
2462 Fname := Find_Prim_Op (U_Type, TSS_Stream_Input);
2464 -- All other record type cases, including protected records. The
2465 -- latter only arise for expander generated code for handling
2466 -- shared passive partition access.
2470 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
2472 -- Ada 2005 (AI-216): Program_Error is raised executing default
2473 -- implementation of the Input attribute of an unchecked union
2474 -- type if the type lacks default discriminant values.
2476 if Is_Unchecked_Union (Base_Type (U_Type))
2477 and then No (Discriminant_Constraint (U_Type))
2480 Make_Raise_Program_Error (Loc,
2481 Reason => PE_Unchecked_Union_Restriction));
2486 Build_Record_Or_Elementary_Input_Function
2487 (Loc, Base_Type (U_Type), Decl, Fname);
2488 Insert_Action (N, Decl);
2490 if Nkind (Parent (N)) = N_Object_Declaration
2491 and then Is_Record_Type (U_Type)
2493 -- The stream function may contain calls to user-defined
2494 -- Read procedures for individual components.
2501 Comp := First_Component (U_Type);
2502 while Present (Comp) loop
2504 Find_Stream_Subprogram
2505 (Etype (Comp), TSS_Stream_Read);
2507 if Present (Func) then
2508 Freeze_Stream_Subprogram (Func);
2511 Next_Component (Comp);
2518 -- If we fall through, Fname is the function to be called. The result
2519 -- is obtained by calling the appropriate function, then converting
2520 -- the result. The conversion does a subtype check.
2523 Make_Function_Call (Loc,
2524 Name => New_Occurrence_Of (Fname, Loc),
2525 Parameter_Associations => New_List (
2526 Relocate_Node (Strm)));
2528 Set_Controlling_Argument (Call, Cntrl);
2529 Rewrite (N, Unchecked_Convert_To (P_Type, Call));
2530 Analyze_And_Resolve (N, P_Type);
2532 if Nkind (Parent (N)) = N_Object_Declaration then
2533 Freeze_Stream_Subprogram (Fname);
2543 -- inttype'Fixed_Value (fixed-value)
2547 -- inttype(integer-value))
2549 -- we do all the required analysis of the conversion here, because we do
2550 -- not want this to go through the fixed-point conversion circuits. Note
2551 -- that the back end always treats fixed-point as equivalent to the
2552 -- corresponding integer type anyway.
2554 when Attribute_Integer_Value => Integer_Value :
2557 Make_Type_Conversion (Loc,
2558 Subtype_Mark => New_Occurrence_Of (Entity (Pref), Loc),
2559 Expression => Relocate_Node (First (Exprs))));
2560 Set_Etype (N, Entity (Pref));
2563 -- Note: it might appear that a properly analyzed unchecked conversion
2564 -- would be just fine here, but that's not the case, since the full
2565 -- range checks performed by the following call are critical!
2567 Apply_Type_Conversion_Checks (N);
2574 when Attribute_Invalid_Value =>
2575 Rewrite (N, Get_Simple_Init_Val (Ptyp, N));
2581 when Attribute_Last =>
2583 -- If the prefix type is a constrained packed array type which
2584 -- already has a Packed_Array_Type representation defined, then
2585 -- replace this attribute with a direct reference to 'Last of the
2586 -- appropriate index subtype (since otherwise the back end will try
2587 -- to give us the value of 'Last for this implementation type).
2589 if Is_Constrained_Packed_Array (Ptyp) then
2591 Make_Attribute_Reference (Loc,
2592 Attribute_Name => Name_Last,
2593 Prefix => New_Reference_To (Get_Index_Subtype (N), Loc)));
2594 Analyze_And_Resolve (N, Typ);
2596 elsif Is_Access_Type (Ptyp) then
2597 Apply_Access_Check (N);
2604 -- We compute this if a component clause was present, otherwise we leave
2605 -- the computation up to the back end, since we don't know what layout
2608 when Attribute_Last_Bit => Last_Bit : declare
2609 CE : constant Entity_Id := Entity (Selector_Name (Pref));
2612 if Known_Static_Component_Bit_Offset (CE)
2613 and then Known_Static_Esize (CE)
2616 Make_Integer_Literal (Loc,
2617 Intval => (Component_Bit_Offset (CE) mod System_Storage_Unit)
2620 Analyze_And_Resolve (N, Typ);
2623 Apply_Universal_Integer_Attribute_Checks (N);
2631 -- Transforms 'Leading_Part into a call to the floating-point attribute
2632 -- function Leading_Part in Fat_xxx (where xxx is the root type)
2634 -- Note: strictly, we should generate special case code to deal with
2635 -- absurdly large positive arguments (greater than Integer'Last), which
2636 -- result in returning the first argument unchanged, but it hardly seems
2637 -- worth the effort. We raise constraint error for absurdly negative
2638 -- arguments which is fine.
2640 when Attribute_Leading_Part =>
2641 Expand_Fpt_Attribute_RI (N);
2647 when Attribute_Length => declare
2652 -- Processing for packed array types
2654 if Is_Array_Type (Ptyp) and then Is_Packed (Ptyp) then
2655 Ityp := Get_Index_Subtype (N);
2657 -- If the index type, Ityp, is an enumeration type with holes,
2658 -- then we calculate X'Length explicitly using
2661 -- (0, Ityp'Pos (X'Last (N)) -
2662 -- Ityp'Pos (X'First (N)) + 1);
2664 -- Since the bounds in the template are the representation values
2665 -- and the back end would get the wrong value.
2667 if Is_Enumeration_Type (Ityp)
2668 and then Present (Enum_Pos_To_Rep (Base_Type (Ityp)))
2673 Xnum := Expr_Value (First (Expressions (N)));
2677 Make_Attribute_Reference (Loc,
2678 Prefix => New_Occurrence_Of (Typ, Loc),
2679 Attribute_Name => Name_Max,
2680 Expressions => New_List
2681 (Make_Integer_Literal (Loc, 0),
2685 Make_Op_Subtract (Loc,
2687 Make_Attribute_Reference (Loc,
2688 Prefix => New_Occurrence_Of (Ityp, Loc),
2689 Attribute_Name => Name_Pos,
2691 Expressions => New_List (
2692 Make_Attribute_Reference (Loc,
2693 Prefix => Duplicate_Subexpr (Pref),
2694 Attribute_Name => Name_Last,
2695 Expressions => New_List (
2696 Make_Integer_Literal (Loc, Xnum))))),
2699 Make_Attribute_Reference (Loc,
2700 Prefix => New_Occurrence_Of (Ityp, Loc),
2701 Attribute_Name => Name_Pos,
2703 Expressions => New_List (
2704 Make_Attribute_Reference (Loc,
2706 Duplicate_Subexpr_No_Checks (Pref),
2707 Attribute_Name => Name_First,
2708 Expressions => New_List (
2709 Make_Integer_Literal (Loc, Xnum)))))),
2711 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
2713 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2716 -- If the prefix type is a constrained packed array type which
2717 -- already has a Packed_Array_Type representation defined, then
2718 -- replace this attribute with a direct reference to 'Range_Length
2719 -- of the appropriate index subtype (since otherwise the back end
2720 -- will try to give us the value of 'Length for this
2721 -- implementation type).
2723 elsif Is_Constrained (Ptyp) then
2725 Make_Attribute_Reference (Loc,
2726 Attribute_Name => Name_Range_Length,
2727 Prefix => New_Reference_To (Ityp, Loc)));
2728 Analyze_And_Resolve (N, Typ);
2733 elsif Is_Access_Type (Ptyp) then
2734 Apply_Access_Check (N);
2736 -- If the designated type is a packed array type, then we convert
2737 -- the reference to:
2740 -- xtyp'Pos (Pref'Last (Expr)) -
2741 -- xtyp'Pos (Pref'First (Expr)));
2743 -- This is a bit complex, but it is the easiest thing to do that
2744 -- works in all cases including enum types with holes xtyp here
2745 -- is the appropriate index type.
2748 Dtyp : constant Entity_Id := Designated_Type (Ptyp);
2752 if Is_Array_Type (Dtyp) and then Is_Packed (Dtyp) then
2753 Xtyp := Get_Index_Subtype (N);
2756 Make_Attribute_Reference (Loc,
2757 Prefix => New_Occurrence_Of (Typ, Loc),
2758 Attribute_Name => Name_Max,
2759 Expressions => New_List (
2760 Make_Integer_Literal (Loc, 0),
2763 Make_Integer_Literal (Loc, 1),
2764 Make_Op_Subtract (Loc,
2766 Make_Attribute_Reference (Loc,
2767 Prefix => New_Occurrence_Of (Xtyp, Loc),
2768 Attribute_Name => Name_Pos,
2769 Expressions => New_List (
2770 Make_Attribute_Reference (Loc,
2771 Prefix => Duplicate_Subexpr (Pref),
2772 Attribute_Name => Name_Last,
2774 New_Copy_List (Exprs)))),
2777 Make_Attribute_Reference (Loc,
2778 Prefix => New_Occurrence_Of (Xtyp, Loc),
2779 Attribute_Name => Name_Pos,
2780 Expressions => New_List (
2781 Make_Attribute_Reference (Loc,
2783 Duplicate_Subexpr_No_Checks (Pref),
2784 Attribute_Name => Name_First,
2786 New_Copy_List (Exprs)))))))));
2788 Analyze_And_Resolve (N, Typ);
2792 -- Otherwise leave it to the back end
2795 Apply_Universal_Integer_Attribute_Checks (N);
2803 -- Transforms 'Machine into a call to the floating-point attribute
2804 -- function Machine in Fat_xxx (where xxx is the root type)
2806 when Attribute_Machine =>
2807 Expand_Fpt_Attribute_R (N);
2809 ----------------------
2810 -- Machine_Rounding --
2811 ----------------------
2813 -- Transforms 'Machine_Rounding into a call to the floating-point
2814 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
2815 -- type). Expansion is avoided for cases the back end can handle
2818 when Attribute_Machine_Rounding =>
2819 if not Is_Inline_Floating_Point_Attribute (N) then
2820 Expand_Fpt_Attribute_R (N);
2827 -- Machine_Size is equivalent to Object_Size, so transform it into
2828 -- Object_Size and that way the back end never sees Machine_Size.
2830 when Attribute_Machine_Size =>
2832 Make_Attribute_Reference (Loc,
2833 Prefix => Prefix (N),
2834 Attribute_Name => Name_Object_Size));
2836 Analyze_And_Resolve (N, Typ);
2842 -- The only case that can get this far is the dynamic case of the old
2843 -- Ada 83 Mantissa attribute for the fixed-point case. For this case,
2850 -- ityp (System.Mantissa.Mantissa_Value
2851 -- (Integer'Integer_Value (typ'First),
2852 -- Integer'Integer_Value (typ'Last)));
2854 when Attribute_Mantissa => Mantissa : begin
2857 Make_Function_Call (Loc,
2858 Name => New_Occurrence_Of (RTE (RE_Mantissa_Value), Loc),
2860 Parameter_Associations => New_List (
2862 Make_Attribute_Reference (Loc,
2863 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2864 Attribute_Name => Name_Integer_Value,
2865 Expressions => New_List (
2867 Make_Attribute_Reference (Loc,
2868 Prefix => New_Occurrence_Of (Ptyp, Loc),
2869 Attribute_Name => Name_First))),
2871 Make_Attribute_Reference (Loc,
2872 Prefix => New_Occurrence_Of (Standard_Integer, Loc),
2873 Attribute_Name => Name_Integer_Value,
2874 Expressions => New_List (
2876 Make_Attribute_Reference (Loc,
2877 Prefix => New_Occurrence_Of (Ptyp, Loc),
2878 Attribute_Name => Name_Last)))))));
2880 Analyze_And_Resolve (N, Typ);
2883 --------------------
2884 -- Mechanism_Code --
2885 --------------------
2887 when Attribute_Mechanism_Code =>
2889 -- We must replace the prefix in the renamed case
2891 if Is_Entity_Name (Pref)
2892 and then Present (Alias (Entity (Pref)))
2894 Set_Renamed_Subprogram (Pref, Alias (Entity (Pref)));
2901 when Attribute_Mod => Mod_Case : declare
2902 Arg : constant Node_Id := Relocate_Node (First (Exprs));
2903 Hi : constant Node_Id := Type_High_Bound (Etype (Arg));
2904 Modv : constant Uint := Modulus (Btyp);
2908 -- This is not so simple. The issue is what type to use for the
2909 -- computation of the modular value.
2911 -- The easy case is when the modulus value is within the bounds
2912 -- of the signed integer type of the argument. In this case we can
2913 -- just do the computation in that signed integer type, and then
2914 -- do an ordinary conversion to the target type.
2916 if Modv <= Expr_Value (Hi) then
2921 Right_Opnd => Make_Integer_Literal (Loc, Modv))));
2923 -- Here we know that the modulus is larger than type'Last of the
2924 -- integer type. There are two cases to consider:
2926 -- a) The integer value is non-negative. In this case, it is
2927 -- returned as the result (since it is less than the modulus).
2929 -- b) The integer value is negative. In this case, we know that the
2930 -- result is modulus + value, where the value might be as small as
2931 -- -modulus. The trouble is what type do we use to do the subtract.
2932 -- No type will do, since modulus can be as big as 2**64, and no
2933 -- integer type accommodates this value. Let's do bit of algebra
2936 -- = modulus - (-value)
2937 -- = (modulus - 1) - (-value - 1)
2939 -- Now modulus - 1 is certainly in range of the modular type.
2940 -- -value is in the range 1 .. modulus, so -value -1 is in the
2941 -- range 0 .. modulus-1 which is in range of the modular type.
2942 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
2943 -- which we can compute using the integer base type.
2945 -- Once this is done we analyze the conditional expression without
2946 -- range checks, because we know everything is in range, and we
2947 -- want to prevent spurious warnings on either branch.
2951 Make_Conditional_Expression (Loc,
2952 Expressions => New_List (
2954 Left_Opnd => Duplicate_Subexpr (Arg),
2955 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2958 Duplicate_Subexpr_No_Checks (Arg)),
2960 Make_Op_Subtract (Loc,
2962 Make_Integer_Literal (Loc,
2963 Intval => Modv - 1),
2969 Left_Opnd => Duplicate_Subexpr_No_Checks (Arg),
2971 Make_Integer_Literal (Loc,
2972 Intval => 1))))))));
2976 Analyze_And_Resolve (N, Btyp, Suppress => All_Checks);
2983 -- Transforms 'Model into a call to the floating-point attribute
2984 -- function Model in Fat_xxx (where xxx is the root type)
2986 when Attribute_Model =>
2987 Expand_Fpt_Attribute_R (N);
2993 -- The processing for Object_Size shares the processing for Size
2999 when Attribute_Old => Old : declare
3000 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', Pref);
3005 -- Find the nearest subprogram body, ignoring _Preconditions
3009 Subp := Parent (Subp);
3010 exit when Nkind (Subp) = N_Subprogram_Body
3011 and then Chars (Defining_Entity (Subp)) /= Name_uPostconditions;
3014 -- Insert the assignment at the start of the declarations
3017 Make_Object_Declaration (Loc,
3018 Defining_Identifier => Tnn,
3019 Constant_Present => True,
3020 Object_Definition => New_Occurrence_Of (Etype (N), Loc),
3021 Expression => Pref);
3023 if Is_Empty_List (Declarations (Subp)) then
3024 Set_Declarations (Subp, New_List (Asn_Stm));
3027 Insert_Action (First (Declarations (Subp)), Asn_Stm);
3030 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
3037 when Attribute_Output => Output : declare
3038 P_Type : constant Entity_Id := Entity (Pref);
3039 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3047 -- If no underlying type, we have an error that will be diagnosed
3048 -- elsewhere, so here we just completely ignore the expansion.
3054 -- If TSS for Output is present, just call it
3056 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Output);
3058 if Present (Pname) then
3062 -- If there is a Stream_Convert pragma, use it, we rewrite
3064 -- sourcetyp'Output (stream, Item)
3068 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3070 -- where strmwrite is the given Write function that converts an
3071 -- argument of type sourcetyp or a type acctyp, from which it is
3072 -- derived to type strmtyp. The conversion to acttyp is required
3073 -- for the derived case.
3075 Prag := Get_Stream_Convert_Pragma (P_Type);
3077 if Present (Prag) then
3079 Next (Next (First (Pragma_Argument_Associations (Prag))));
3080 Wfunc := Entity (Expression (Arg3));
3083 Make_Attribute_Reference (Loc,
3084 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
3085 Attribute_Name => Name_Output,
3086 Expressions => New_List (
3087 Relocate_Node (First (Exprs)),
3088 Make_Function_Call (Loc,
3089 Name => New_Occurrence_Of (Wfunc, Loc),
3090 Parameter_Associations => New_List (
3091 OK_Convert_To (Etype (First_Formal (Wfunc)),
3092 Relocate_Node (Next (First (Exprs)))))))));
3097 -- For elementary types, we call the W_xxx routine directly.
3098 -- Note that the effect of Write and Output is identical for
3099 -- the case of an elementary type, since there are no
3100 -- discriminants or bounds.
3102 elsif Is_Elementary_Type (U_Type) then
3104 -- A special case arises if we have a defined _Write routine,
3105 -- since in this case we are required to call this routine.
3107 if Present (TSS (Base_Type (U_Type), TSS_Stream_Write)) then
3108 Build_Record_Or_Elementary_Output_Procedure
3109 (Loc, U_Type, Decl, Pname);
3110 Insert_Action (N, Decl);
3112 -- For normal cases, we call the W_xxx routine directly
3115 Rewrite (N, Build_Elementary_Write_Call (N));
3122 elsif Is_Array_Type (U_Type) then
3123 Build_Array_Output_Procedure (Loc, U_Type, Decl, Pname);
3124 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3126 -- Class-wide case, first output external tag, then dispatch
3127 -- to the appropriate primitive Output function (RM 13.13.2(31)).
3129 elsif Is_Class_Wide_Type (P_Type) then
3131 -- No need to do anything else compiling under restriction
3132 -- No_Dispatching_Calls. During the semantic analysis we
3133 -- already notified such violation.
3135 if Restriction_Active (No_Dispatching_Calls) then
3140 Strm : constant Node_Id := First (Exprs);
3141 Item : constant Node_Id := Next (Strm);
3144 -- Ada 2005 (AI-344): Check that the accessibility level
3145 -- of the type of the output object is not deeper than
3146 -- that of the attribute's prefix type.
3148 -- if Get_Access_Level (Item'Tag)
3149 -- /= Get_Access_Level (P_Type'Tag)
3154 -- String'Output (Strm, External_Tag (Item'Tag));
3156 -- We cannot figure out a practical way to implement this
3157 -- accessibility check on virtual machines, so we omit it.
3159 if Ada_Version >= Ada_05
3160 and then Tagged_Type_Expansion
3163 Make_Implicit_If_Statement (N,
3167 Build_Get_Access_Level (Loc,
3168 Make_Attribute_Reference (Loc,
3171 Duplicate_Subexpr (Item,
3173 Attribute_Name => Name_Tag)),
3176 Make_Integer_Literal (Loc,
3177 Type_Access_Level (P_Type))),
3180 New_List (Make_Raise_Statement (Loc,
3182 RTE (RE_Tag_Error), Loc)))));
3186 Make_Attribute_Reference (Loc,
3187 Prefix => New_Occurrence_Of (Standard_String, Loc),
3188 Attribute_Name => Name_Output,
3189 Expressions => New_List (
3190 Relocate_Node (Duplicate_Subexpr (Strm)),
3191 Make_Function_Call (Loc,
3193 New_Occurrence_Of (RTE (RE_External_Tag), Loc),
3194 Parameter_Associations => New_List (
3195 Make_Attribute_Reference (Loc,
3198 (Duplicate_Subexpr (Item, Name_Req => True)),
3199 Attribute_Name => Name_Tag))))));
3202 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3204 -- Tagged type case, use the primitive Output function
3206 elsif Is_Tagged_Type (U_Type) then
3207 Pname := Find_Prim_Op (U_Type, TSS_Stream_Output);
3209 -- All other record type cases, including protected records.
3210 -- The latter only arise for expander generated code for
3211 -- handling shared passive partition access.
3215 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3217 -- Ada 2005 (AI-216): Program_Error is raised when executing
3218 -- the default implementation of the Output attribute of an
3219 -- unchecked union type if the type lacks default discriminant
3222 if Is_Unchecked_Union (Base_Type (U_Type))
3223 and then No (Discriminant_Constraint (U_Type))
3226 Make_Raise_Program_Error (Loc,
3227 Reason => PE_Unchecked_Union_Restriction));
3232 Build_Record_Or_Elementary_Output_Procedure
3233 (Loc, Base_Type (U_Type), Decl, Pname);
3234 Insert_Action (N, Decl);
3238 -- If we fall through, Pname is the name of the procedure to call
3240 Rewrite_Stream_Proc_Call (Pname);
3247 -- For enumeration types with a standard representation, Pos is
3248 -- handled by the back end.
3250 -- For enumeration types, with a non-standard representation we generate
3251 -- a call to the _Rep_To_Pos function created when the type was frozen.
3252 -- The call has the form
3254 -- _rep_to_pos (expr, flag)
3256 -- The parameter flag is True if range checks are enabled, causing
3257 -- Program_Error to be raised if the expression has an invalid
3258 -- representation, and False if range checks are suppressed.
3260 -- For integer types, Pos is equivalent to a simple integer
3261 -- conversion and we rewrite it as such
3263 when Attribute_Pos => Pos :
3265 Etyp : Entity_Id := Base_Type (Entity (Pref));
3268 -- Deal with zero/non-zero boolean values
3270 if Is_Boolean_Type (Etyp) then
3271 Adjust_Condition (First (Exprs));
3272 Etyp := Standard_Boolean;
3273 Set_Prefix (N, New_Occurrence_Of (Standard_Boolean, Loc));
3276 -- Case of enumeration type
3278 if Is_Enumeration_Type (Etyp) then
3280 -- Non-standard enumeration type (generate call)
3282 if Present (Enum_Pos_To_Rep (Etyp)) then
3283 Append_To (Exprs, Rep_To_Pos_Flag (Etyp, Loc));
3286 Make_Function_Call (Loc,
3288 New_Reference_To (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3289 Parameter_Associations => Exprs)));
3291 Analyze_And_Resolve (N, Typ);
3293 -- Standard enumeration type (do universal integer check)
3296 Apply_Universal_Integer_Attribute_Checks (N);
3299 -- Deal with integer types (replace by conversion)
3301 elsif Is_Integer_Type (Etyp) then
3302 Rewrite (N, Convert_To (Typ, First (Exprs)));
3303 Analyze_And_Resolve (N, Typ);
3312 -- We compute this if a component clause was present, otherwise we leave
3313 -- the computation up to the back end, since we don't know what layout
3316 when Attribute_Position => Position :
3318 CE : constant Entity_Id := Entity (Selector_Name (Pref));
3321 if Present (Component_Clause (CE)) then
3323 Make_Integer_Literal (Loc,
3324 Intval => Component_Bit_Offset (CE) / System_Storage_Unit));
3325 Analyze_And_Resolve (N, Typ);
3328 Apply_Universal_Integer_Attribute_Checks (N);
3336 -- 1. Deal with enumeration types with holes
3337 -- 2. For floating-point, generate call to attribute function
3338 -- 3. For other cases, deal with constraint checking
3340 when Attribute_Pred => Pred :
3342 Etyp : constant Entity_Id := Base_Type (Ptyp);
3346 -- For enumeration types with non-standard representations, we
3347 -- expand typ'Pred (x) into
3349 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
3351 -- If the representation is contiguous, we compute instead
3352 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
3353 -- The conversion function Enum_Pos_To_Rep is defined on the
3354 -- base type, not the subtype, so we have to use the base type
3355 -- explicitly for this and other enumeration attributes.
3357 if Is_Enumeration_Type (Ptyp)
3358 and then Present (Enum_Pos_To_Rep (Etyp))
3360 if Has_Contiguous_Rep (Etyp) then
3362 Unchecked_Convert_To (Ptyp,
3365 Make_Integer_Literal (Loc,
3366 Enumeration_Rep (First_Literal (Ptyp))),
3368 Make_Function_Call (Loc,
3371 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3373 Parameter_Associations =>
3375 Unchecked_Convert_To (Ptyp,
3376 Make_Op_Subtract (Loc,
3378 Unchecked_Convert_To (Standard_Integer,
3379 Relocate_Node (First (Exprs))),
3381 Make_Integer_Literal (Loc, 1))),
3382 Rep_To_Pos_Flag (Ptyp, Loc))))));
3385 -- Add Boolean parameter True, to request program errror if
3386 -- we have a bad representation on our hands. If checks are
3387 -- suppressed, then add False instead
3389 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
3391 Make_Indexed_Component (Loc,
3394 (Enum_Pos_To_Rep (Etyp), Loc),
3395 Expressions => New_List (
3396 Make_Op_Subtract (Loc,
3398 Make_Function_Call (Loc,
3401 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
3402 Parameter_Associations => Exprs),
3403 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
3406 Analyze_And_Resolve (N, Typ);
3408 -- For floating-point, we transform 'Pred into a call to the Pred
3409 -- floating-point attribute function in Fat_xxx (xxx is root type)
3411 elsif Is_Floating_Point_Type (Ptyp) then
3412 Expand_Fpt_Attribute_R (N);
3413 Analyze_And_Resolve (N, Typ);
3415 -- For modular types, nothing to do (no overflow, since wraps)
3417 elsif Is_Modular_Integer_Type (Ptyp) then
3420 -- For other types, if argument is marked as needing a range check or
3421 -- overflow checking is enabled, we must generate a check.
3423 elsif not Overflow_Checks_Suppressed (Ptyp)
3424 or else Do_Range_Check (First (Exprs))
3426 Set_Do_Range_Check (First (Exprs), False);
3427 Expand_Pred_Succ (N);
3435 -- Ada 2005 (AI-327): Dynamic ceiling priorities
3437 -- We rewrite X'Priority as the following run-time call:
3439 -- Get_Ceiling (X._Object)
3441 -- Note that although X'Priority is notionally an object, it is quite
3442 -- deliberately not defined as an aliased object in the RM. This means
3443 -- that it works fine to rewrite it as a call, without having to worry
3444 -- about complications that would other arise from X'Priority'Access,
3445 -- which is illegal, because of the lack of aliasing.
3447 when Attribute_Priority =>
3450 Conctyp : Entity_Id;
3451 Object_Parm : Node_Id;
3453 RT_Subprg_Name : Node_Id;
3456 -- Look for the enclosing concurrent type
3458 Conctyp := Current_Scope;
3459 while not Is_Concurrent_Type (Conctyp) loop
3460 Conctyp := Scope (Conctyp);
3463 pragma Assert (Is_Protected_Type (Conctyp));
3465 -- Generate the actual of the call
3467 Subprg := Current_Scope;
3468 while not Present (Protected_Body_Subprogram (Subprg)) loop
3469 Subprg := Scope (Subprg);
3472 -- Use of 'Priority inside protected entries and barriers (in
3473 -- both cases the type of the first formal of their expanded
3474 -- subprogram is Address)
3476 if Etype (First_Entity (Protected_Body_Subprogram (Subprg)))
3480 New_Itype : Entity_Id;
3483 -- In the expansion of protected entries the type of the
3484 -- first formal of the Protected_Body_Subprogram is an
3485 -- Address. In order to reference the _object component
3488 -- type T is access p__ptTV;
3491 New_Itype := Create_Itype (E_Access_Type, N);
3492 Set_Etype (New_Itype, New_Itype);
3493 Set_Directly_Designated_Type (New_Itype,
3494 Corresponding_Record_Type (Conctyp));
3495 Freeze_Itype (New_Itype, N);
3498 -- T!(O)._object'unchecked_access
3501 Make_Attribute_Reference (Loc,
3503 Make_Selected_Component (Loc,
3505 Unchecked_Convert_To (New_Itype,
3508 (Protected_Body_Subprogram (Subprg)),
3511 Make_Identifier (Loc, Name_uObject)),
3512 Attribute_Name => Name_Unchecked_Access);
3515 -- Use of 'Priority inside a protected subprogram
3519 Make_Attribute_Reference (Loc,
3521 Make_Selected_Component (Loc,
3522 Prefix => New_Reference_To
3524 (Protected_Body_Subprogram (Subprg)),
3527 Make_Identifier (Loc, Name_uObject)),
3528 Attribute_Name => Name_Unchecked_Access);
3531 -- Select the appropriate run-time subprogram
3533 if Number_Entries (Conctyp) = 0 then
3535 New_Reference_To (RTE (RE_Get_Ceiling), Loc);
3538 New_Reference_To (RTE (RO_PE_Get_Ceiling), Loc);
3542 Make_Function_Call (Loc,
3543 Name => RT_Subprg_Name,
3544 Parameter_Associations => New_List (Object_Parm));
3548 -- Avoid the generation of extra checks on the pointer to the
3549 -- protected object.
3551 Analyze_And_Resolve (N, Typ, Suppress => Access_Check);
3558 when Attribute_Range_Length => Range_Length : begin
3560 -- The only special processing required is for the case where
3561 -- Range_Length is applied to an enumeration type with holes.
3562 -- In this case we transform
3568 -- X'Pos (X'Last) - X'Pos (X'First) + 1
3570 -- So that the result reflects the proper Pos values instead
3571 -- of the underlying representations.
3573 if Is_Enumeration_Type (Ptyp)
3574 and then Has_Non_Standard_Rep (Ptyp)
3579 Make_Op_Subtract (Loc,
3581 Make_Attribute_Reference (Loc,
3582 Attribute_Name => Name_Pos,
3583 Prefix => New_Occurrence_Of (Ptyp, Loc),
3584 Expressions => New_List (
3585 Make_Attribute_Reference (Loc,
3586 Attribute_Name => Name_Last,
3587 Prefix => New_Occurrence_Of (Ptyp, Loc)))),
3590 Make_Attribute_Reference (Loc,
3591 Attribute_Name => Name_Pos,
3592 Prefix => New_Occurrence_Of (Ptyp, Loc),
3593 Expressions => New_List (
3594 Make_Attribute_Reference (Loc,
3595 Attribute_Name => Name_First,
3596 Prefix => New_Occurrence_Of (Ptyp, Loc))))),
3598 Right_Opnd => Make_Integer_Literal (Loc, 1)));
3600 Analyze_And_Resolve (N, Typ);
3602 -- For all other cases, the attribute is handled by the back end, but
3603 -- we need to deal with the case of the range check on a universal
3607 Apply_Universal_Integer_Attribute_Checks (N);
3615 when Attribute_Read => Read : declare
3616 P_Type : constant Entity_Id := Entity (Pref);
3617 B_Type : constant Entity_Id := Base_Type (P_Type);
3618 U_Type : constant Entity_Id := Underlying_Type (P_Type);
3628 -- If no underlying type, we have an error that will be diagnosed
3629 -- elsewhere, so here we just completely ignore the expansion.
3635 -- The simple case, if there is a TSS for Read, just call it
3637 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Read);
3639 if Present (Pname) then
3643 -- If there is a Stream_Convert pragma, use it, we rewrite
3645 -- sourcetyp'Read (stream, Item)
3649 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
3651 -- where strmread is the given Read function that converts an
3652 -- argument of type strmtyp to type sourcetyp or a type from which
3653 -- it is derived. The conversion to sourcetyp is required in the
3656 -- A special case arises if Item is a type conversion in which
3657 -- case, we have to expand to:
3659 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
3661 -- where Itemx is the expression of the type conversion (i.e.
3662 -- the actual object), and typex is the type of Itemx.
3664 Prag := Get_Stream_Convert_Pragma (P_Type);
3666 if Present (Prag) then
3667 Arg2 := Next (First (Pragma_Argument_Associations (Prag)));
3668 Rfunc := Entity (Expression (Arg2));
3669 Lhs := Relocate_Node (Next (First (Exprs)));
3671 OK_Convert_To (B_Type,
3672 Make_Function_Call (Loc,
3673 Name => New_Occurrence_Of (Rfunc, Loc),
3674 Parameter_Associations => New_List (
3675 Make_Attribute_Reference (Loc,
3678 (Etype (First_Formal (Rfunc)), Loc),
3679 Attribute_Name => Name_Input,
3680 Expressions => New_List (
3681 Relocate_Node (First (Exprs)))))));
3683 if Nkind (Lhs) = N_Type_Conversion then
3684 Lhs := Expression (Lhs);
3685 Rhs := Convert_To (Etype (Lhs), Rhs);
3689 Make_Assignment_Statement (Loc,
3691 Expression => Rhs));
3692 Set_Assignment_OK (Lhs);
3696 -- For elementary types, we call the I_xxx routine using the first
3697 -- parameter and then assign the result into the second parameter.
3698 -- We set Assignment_OK to deal with the conversion case.
3700 elsif Is_Elementary_Type (U_Type) then
3706 Lhs := Relocate_Node (Next (First (Exprs)));
3707 Rhs := Build_Elementary_Input_Call (N);
3709 if Nkind (Lhs) = N_Type_Conversion then
3710 Lhs := Expression (Lhs);
3711 Rhs := Convert_To (Etype (Lhs), Rhs);
3714 Set_Assignment_OK (Lhs);
3717 Make_Assignment_Statement (Loc,
3719 Expression => Rhs));
3727 elsif Is_Array_Type (U_Type) then
3728 Build_Array_Read_Procedure (N, U_Type, Decl, Pname);
3729 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
3731 -- Tagged type case, use the primitive Read function. Note that
3732 -- this will dispatch in the class-wide case which is what we want
3734 elsif Is_Tagged_Type (U_Type) then
3735 Pname := Find_Prim_Op (U_Type, TSS_Stream_Read);
3737 -- All other record type cases, including protected records. The
3738 -- latter only arise for expander generated code for handling
3739 -- shared passive partition access.
3743 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
3745 -- Ada 2005 (AI-216): Program_Error is raised when executing
3746 -- the default implementation of the Read attribute of an
3747 -- Unchecked_Union type.
3749 if Is_Unchecked_Union (Base_Type (U_Type)) then
3751 Make_Raise_Program_Error (Loc,
3752 Reason => PE_Unchecked_Union_Restriction));
3755 if Has_Discriminants (U_Type)
3757 (Discriminant_Default_Value (First_Discriminant (U_Type)))
3759 Build_Mutable_Record_Read_Procedure
3760 (Loc, Base_Type (U_Type), Decl, Pname);
3762 Build_Record_Read_Procedure
3763 (Loc, Base_Type (U_Type), Decl, Pname);
3766 -- Suppress checks, uninitialized or otherwise invalid
3767 -- data does not cause constraint errors to be raised for
3768 -- a complete record read.
3770 Insert_Action (N, Decl, All_Checks);
3774 Rewrite_Stream_Proc_Call (Pname);
3781 -- Transforms 'Remainder into a call to the floating-point attribute
3782 -- function Remainder in Fat_xxx (where xxx is the root type)
3784 when Attribute_Remainder =>
3785 Expand_Fpt_Attribute_RR (N);
3791 -- Transform 'Result into reference to _Result formal. At the point
3792 -- where a legal 'Result attribute is expanded, we know that we are in
3793 -- the context of a _Postcondition function with a _Result parameter.
3795 when Attribute_Result =>
3796 Rewrite (N, Make_Identifier (Loc, Chars => Name_uResult));
3797 Analyze_And_Resolve (N, Typ);
3803 -- The handling of the Round attribute is quite delicate. The processing
3804 -- in Sem_Attr introduced a conversion to universal real, reflecting the
3805 -- semantics of Round, but we do not want anything to do with universal
3806 -- real at runtime, since this corresponds to using floating-point
3809 -- What we have now is that the Etype of the Round attribute correctly
3810 -- indicates the final result type. The operand of the Round is the
3811 -- conversion to universal real, described above, and the operand of
3812 -- this conversion is the actual operand of Round, which may be the
3813 -- special case of a fixed point multiplication or division (Etype =
3816 -- The exapander will expand first the operand of the conversion, then
3817 -- the conversion, and finally the round attribute itself, since we
3818 -- always work inside out. But we cannot simply process naively in this
3819 -- order. In the semantic world where universal fixed and real really
3820 -- exist and have infinite precision, there is no problem, but in the
3821 -- implementation world, where universal real is a floating-point type,
3822 -- we would get the wrong result.
3824 -- So the approach is as follows. First, when expanding a multiply or
3825 -- divide whose type is universal fixed, we do nothing at all, instead
3826 -- deferring the operation till later.
3828 -- The actual processing is done in Expand_N_Type_Conversion which
3829 -- handles the special case of Round by looking at its parent to see if
3830 -- it is a Round attribute, and if it is, handling the conversion (or
3831 -- its fixed multiply/divide child) in an appropriate manner.
3833 -- This means that by the time we get to expanding the Round attribute
3834 -- itself, the Round is nothing more than a type conversion (and will
3835 -- often be a null type conversion), so we just replace it with the
3836 -- appropriate conversion operation.
3838 when Attribute_Round =>
3840 Convert_To (Etype (N), Relocate_Node (First (Exprs))));
3841 Analyze_And_Resolve (N);
3847 -- Transforms 'Rounding into a call to the floating-point attribute
3848 -- function Rounding in Fat_xxx (where xxx is the root type)
3850 when Attribute_Rounding =>
3851 Expand_Fpt_Attribute_R (N);
3857 -- Transforms 'Scaling into a call to the floating-point attribute
3858 -- function Scaling in Fat_xxx (where xxx is the root type)
3860 when Attribute_Scaling =>
3861 Expand_Fpt_Attribute_RI (N);
3867 when Attribute_Size |
3868 Attribute_Object_Size |
3869 Attribute_Value_Size |
3870 Attribute_VADS_Size => Size :
3877 -- Processing for VADS_Size case. Note that this processing removes
3878 -- all traces of VADS_Size from the tree, and completes all required
3879 -- processing for VADS_Size by translating the attribute reference
3880 -- to an appropriate Size or Object_Size reference.
3882 if Id = Attribute_VADS_Size
3883 or else (Use_VADS_Size and then Id = Attribute_Size)
3885 -- If the size is specified, then we simply use the specified
3886 -- size. This applies to both types and objects. The size of an
3887 -- object can be specified in the following ways:
3889 -- An explicit size object is given for an object
3890 -- A component size is specified for an indexed component
3891 -- A component clause is specified for a selected component
3892 -- The object is a component of a packed composite object
3894 -- If the size is specified, then VADS_Size of an object
3896 if (Is_Entity_Name (Pref)
3897 and then Present (Size_Clause (Entity (Pref))))
3899 (Nkind (Pref) = N_Component_Clause
3900 and then (Present (Component_Clause
3901 (Entity (Selector_Name (Pref))))
3902 or else Is_Packed (Etype (Prefix (Pref)))))
3904 (Nkind (Pref) = N_Indexed_Component
3905 and then (Component_Size (Etype (Prefix (Pref))) /= 0
3906 or else Is_Packed (Etype (Prefix (Pref)))))
3908 Set_Attribute_Name (N, Name_Size);
3910 -- Otherwise if we have an object rather than a type, then the
3911 -- VADS_Size attribute applies to the type of the object, rather
3912 -- than the object itself. This is one of the respects in which
3913 -- VADS_Size differs from Size.
3916 if (not Is_Entity_Name (Pref)
3917 or else not Is_Type (Entity (Pref)))
3918 and then (Is_Scalar_Type (Ptyp) or else Is_Constrained (Ptyp))
3920 Rewrite (Pref, New_Occurrence_Of (Ptyp, Loc));
3923 -- For a scalar type for which no size was explicitly given,
3924 -- VADS_Size means Object_Size. This is the other respect in
3925 -- which VADS_Size differs from Size.
3927 if Is_Scalar_Type (Ptyp) and then No (Size_Clause (Ptyp)) then
3928 Set_Attribute_Name (N, Name_Object_Size);
3930 -- In all other cases, Size and VADS_Size are the sane
3933 Set_Attribute_Name (N, Name_Size);
3938 -- For class-wide types, X'Class'Size is transformed into a direct
3939 -- reference to the Size of the class type, so that the back end does
3940 -- not have to deal with the X'Class'Size reference.
3942 if Is_Entity_Name (Pref)
3943 and then Is_Class_Wide_Type (Entity (Pref))
3945 Rewrite (Prefix (N), New_Occurrence_Of (Entity (Pref), Loc));
3948 -- For X'Size applied to an object of a class-wide type, transform
3949 -- X'Size into a call to the primitive operation _Size applied to X.
3951 elsif Is_Class_Wide_Type (Ptyp)
3952 or else (Id = Attribute_Size
3953 and then Is_Tagged_Type (Ptyp)
3954 and then Has_Unknown_Discriminants (Ptyp))
3956 -- No need to do anything else compiling under restriction
3957 -- No_Dispatching_Calls. During the semantic analysis we
3958 -- already notified such violation.
3960 if Restriction_Active (No_Dispatching_Calls) then
3965 Make_Function_Call (Loc,
3966 Name => New_Reference_To
3967 (Find_Prim_Op (Ptyp, Name_uSize), Loc),
3968 Parameter_Associations => New_List (Pref));
3970 if Typ /= Standard_Long_Long_Integer then
3972 -- The context is a specific integer type with which the
3973 -- original attribute was compatible. The function has a
3974 -- specific type as well, so to preserve the compatibility
3975 -- we must convert explicitly.
3977 New_Node := Convert_To (Typ, New_Node);
3980 Rewrite (N, New_Node);
3981 Analyze_And_Resolve (N, Typ);
3984 -- Case of known RM_Size of a type
3986 elsif (Id = Attribute_Size or else Id = Attribute_Value_Size)
3987 and then Is_Entity_Name (Pref)
3988 and then Is_Type (Entity (Pref))
3989 and then Known_Static_RM_Size (Entity (Pref))
3991 Siz := RM_Size (Entity (Pref));
3993 -- Case of known Esize of a type
3995 elsif Id = Attribute_Object_Size
3996 and then Is_Entity_Name (Pref)
3997 and then Is_Type (Entity (Pref))
3998 and then Known_Static_Esize (Entity (Pref))
4000 Siz := Esize (Entity (Pref));
4002 -- Case of known size of object
4004 elsif Id = Attribute_Size
4005 and then Is_Entity_Name (Pref)
4006 and then Is_Object (Entity (Pref))
4007 and then Known_Esize (Entity (Pref))
4008 and then Known_Static_Esize (Entity (Pref))
4010 Siz := Esize (Entity (Pref));
4012 -- For an array component, we can do Size in the front end
4013 -- if the component_size of the array is set.
4015 elsif Nkind (Pref) = N_Indexed_Component then
4016 Siz := Component_Size (Etype (Prefix (Pref)));
4018 -- For a record component, we can do Size in the front end if there
4019 -- is a component clause, or if the record is packed and the
4020 -- component's size is known at compile time.
4022 elsif Nkind (Pref) = N_Selected_Component then
4024 Rec : constant Entity_Id := Etype (Prefix (Pref));
4025 Comp : constant Entity_Id := Entity (Selector_Name (Pref));
4028 if Present (Component_Clause (Comp)) then
4029 Siz := Esize (Comp);
4031 elsif Is_Packed (Rec) then
4032 Siz := RM_Size (Ptyp);
4035 Apply_Universal_Integer_Attribute_Checks (N);
4040 -- All other cases are handled by the back end
4043 Apply_Universal_Integer_Attribute_Checks (N);
4045 -- If Size is applied to a formal parameter that is of a packed
4046 -- array subtype, then apply Size to the actual subtype.
4048 if Is_Entity_Name (Pref)
4049 and then Is_Formal (Entity (Pref))
4050 and then Is_Array_Type (Ptyp)
4051 and then Is_Packed (Ptyp)
4054 Make_Attribute_Reference (Loc,
4056 New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc),
4057 Attribute_Name => Name_Size));
4058 Analyze_And_Resolve (N, Typ);
4061 -- If Size applies to a dereference of an access to unconstrained
4062 -- packed array, the back end needs to see its unconstrained
4063 -- nominal type, but also a hint to the actual constrained type.
4065 if Nkind (Pref) = N_Explicit_Dereference
4066 and then Is_Array_Type (Ptyp)
4067 and then not Is_Constrained (Ptyp)
4068 and then Is_Packed (Ptyp)
4070 Set_Actual_Designated_Subtype (Pref,
4071 Get_Actual_Subtype (Pref));
4077 -- Common processing for record and array component case
4079 if Siz /= No_Uint and then Siz /= 0 then
4081 CS : constant Boolean := Comes_From_Source (N);
4084 Rewrite (N, Make_Integer_Literal (Loc, Siz));
4086 -- This integer literal is not a static expression. We do not
4087 -- call Analyze_And_Resolve here, because this would activate
4088 -- the circuit for deciding that a static value was out of
4089 -- range, and we don't want that.
4091 -- So just manually set the type, mark the expression as non-
4092 -- static, and then ensure that the result is checked properly
4093 -- if the attribute comes from source (if it was internally
4094 -- generated, we never need a constraint check).
4097 Set_Is_Static_Expression (N, False);
4100 Apply_Constraint_Check (N, Typ);
4110 when Attribute_Storage_Pool =>
4112 Make_Type_Conversion (Loc,
4113 Subtype_Mark => New_Reference_To (Etype (N), Loc),
4114 Expression => New_Reference_To (Entity (N), Loc)));
4115 Analyze_And_Resolve (N, Typ);
4121 when Attribute_Storage_Size => Storage_Size : begin
4123 -- Access type case, always go to the root type
4125 -- The case of access types results in a value of zero for the case
4126 -- where no storage size attribute clause has been given. If a
4127 -- storage size has been given, then the attribute is converted
4128 -- to a reference to the variable used to hold this value.
4130 if Is_Access_Type (Ptyp) then
4131 if Present (Storage_Size_Variable (Root_Type (Ptyp))) then
4133 Make_Attribute_Reference (Loc,
4134 Prefix => New_Reference_To (Typ, Loc),
4135 Attribute_Name => Name_Max,
4136 Expressions => New_List (
4137 Make_Integer_Literal (Loc, 0),
4140 (Storage_Size_Variable (Root_Type (Ptyp)), Loc)))));
4142 elsif Present (Associated_Storage_Pool (Root_Type (Ptyp))) then
4145 Make_Function_Call (Loc,
4149 (Etype (Associated_Storage_Pool (Root_Type (Ptyp))),
4150 Attribute_Name (N)),
4153 Parameter_Associations => New_List (
4155 (Associated_Storage_Pool (Root_Type (Ptyp)), Loc)))));
4158 Rewrite (N, Make_Integer_Literal (Loc, 0));
4161 Analyze_And_Resolve (N, Typ);
4163 -- For tasks, we retrieve the size directly from the TCB. The
4164 -- size may depend on a discriminant of the type, and therefore
4165 -- can be a per-object expression, so type-level information is
4166 -- not sufficient in general. There are four cases to consider:
4168 -- a) If the attribute appears within a task body, the designated
4169 -- TCB is obtained by a call to Self.
4171 -- b) If the prefix of the attribute is the name of a task object,
4172 -- the designated TCB is the one stored in the corresponding record.
4174 -- c) If the prefix is a task type, the size is obtained from the
4175 -- size variable created for each task type
4177 -- d) If no storage_size was specified for the type , there is no
4178 -- size variable, and the value is a system-specific default.
4181 if In_Open_Scopes (Ptyp) then
4183 -- Storage_Size (Self)
4187 Make_Function_Call (Loc,
4189 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4190 Parameter_Associations =>
4192 Make_Function_Call (Loc,
4194 New_Reference_To (RTE (RE_Self), Loc))))));
4196 elsif not Is_Entity_Name (Pref)
4197 or else not Is_Type (Entity (Pref))
4199 -- Storage_Size (Rec (Obj).Size)
4203 Make_Function_Call (Loc,
4205 New_Occurrence_Of (RTE (RE_Storage_Size), Loc),
4206 Parameter_Associations =>
4208 Make_Selected_Component (Loc,
4210 Unchecked_Convert_To (
4211 Corresponding_Record_Type (Ptyp),
4212 New_Copy_Tree (Pref)),
4214 Make_Identifier (Loc, Name_uTask_Id))))));
4216 elsif Present (Storage_Size_Variable (Ptyp)) then
4218 -- Static storage size pragma given for type: retrieve value
4219 -- from its allocated storage variable.
4223 Make_Function_Call (Loc,
4224 Name => New_Occurrence_Of (
4225 RTE (RE_Adjust_Storage_Size), Loc),
4226 Parameter_Associations =>
4229 Storage_Size_Variable (Ptyp), Loc)))));
4231 -- Get system default
4235 Make_Function_Call (Loc,
4238 RTE (RE_Default_Stack_Size), Loc))));
4241 Analyze_And_Resolve (N, Typ);
4249 when Attribute_Stream_Size => Stream_Size : declare
4253 -- If we have a Stream_Size clause for this type use it, otherwise
4254 -- the Stream_Size if the size of the type.
4256 if Has_Stream_Size_Clause (Ptyp) then
4259 (Static_Integer (Expression (Stream_Size_Clause (Ptyp))));
4261 Size := UI_To_Int (Esize (Ptyp));
4264 Rewrite (N, Make_Integer_Literal (Loc, Intval => Size));
4265 Analyze_And_Resolve (N, Typ);
4272 -- 1. Deal with enumeration types with holes
4273 -- 2. For floating-point, generate call to attribute function
4274 -- 3. For other cases, deal with constraint checking
4276 when Attribute_Succ => Succ : declare
4277 Etyp : constant Entity_Id := Base_Type (Ptyp);
4281 -- For enumeration types with non-standard representations, we
4282 -- expand typ'Succ (x) into
4284 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
4286 -- If the representation is contiguous, we compute instead
4287 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
4289 if Is_Enumeration_Type (Ptyp)
4290 and then Present (Enum_Pos_To_Rep (Etyp))
4292 if Has_Contiguous_Rep (Etyp) then
4294 Unchecked_Convert_To (Ptyp,
4297 Make_Integer_Literal (Loc,
4298 Enumeration_Rep (First_Literal (Ptyp))),
4300 Make_Function_Call (Loc,
4303 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4305 Parameter_Associations =>
4307 Unchecked_Convert_To (Ptyp,
4310 Unchecked_Convert_To (Standard_Integer,
4311 Relocate_Node (First (Exprs))),
4313 Make_Integer_Literal (Loc, 1))),
4314 Rep_To_Pos_Flag (Ptyp, Loc))))));
4316 -- Add Boolean parameter True, to request program errror if
4317 -- we have a bad representation on our hands. Add False if
4318 -- checks are suppressed.
4320 Append_To (Exprs, Rep_To_Pos_Flag (Ptyp, Loc));
4322 Make_Indexed_Component (Loc,
4325 (Enum_Pos_To_Rep (Etyp), Loc),
4326 Expressions => New_List (
4329 Make_Function_Call (Loc,
4332 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4333 Parameter_Associations => Exprs),
4334 Right_Opnd => Make_Integer_Literal (Loc, 1)))));
4337 Analyze_And_Resolve (N, Typ);
4339 -- For floating-point, we transform 'Succ into a call to the Succ
4340 -- floating-point attribute function in Fat_xxx (xxx is root type)
4342 elsif Is_Floating_Point_Type (Ptyp) then
4343 Expand_Fpt_Attribute_R (N);
4344 Analyze_And_Resolve (N, Typ);
4346 -- For modular types, nothing to do (no overflow, since wraps)
4348 elsif Is_Modular_Integer_Type (Ptyp) then
4351 -- For other types, if argument is marked as needing a range check or
4352 -- overflow checking is enabled, we must generate a check.
4354 elsif not Overflow_Checks_Suppressed (Ptyp)
4355 or else Do_Range_Check (First (Exprs))
4357 Set_Do_Range_Check (First (Exprs), False);
4358 Expand_Pred_Succ (N);
4366 -- Transforms X'Tag into a direct reference to the tag of X
4368 when Attribute_Tag => Tag : declare
4370 Prefix_Is_Type : Boolean;
4373 if Is_Entity_Name (Pref) and then Is_Type (Entity (Pref)) then
4374 Ttyp := Entity (Pref);
4375 Prefix_Is_Type := True;
4378 Prefix_Is_Type := False;
4381 if Is_Class_Wide_Type (Ttyp) then
4382 Ttyp := Root_Type (Ttyp);
4385 Ttyp := Underlying_Type (Ttyp);
4387 -- Ada 2005: The type may be a synchronized tagged type, in which
4388 -- case the tag information is stored in the corresponding record.
4390 if Is_Concurrent_Type (Ttyp) then
4391 Ttyp := Corresponding_Record_Type (Ttyp);
4394 if Prefix_Is_Type then
4396 -- For VMs we leave the type attribute unexpanded because
4397 -- there's not a dispatching table to reference.
4399 if Tagged_Type_Expansion then
4401 Unchecked_Convert_To (RTE (RE_Tag),
4403 (Node (First_Elmt (Access_Disp_Table (Ttyp))), Loc)));
4404 Analyze_And_Resolve (N, RTE (RE_Tag));
4407 -- Ada 2005 (AI-251): The use of 'Tag in the sources always
4408 -- references the primary tag of the actual object. If 'Tag is
4409 -- applied to class-wide interface objects we generate code that
4410 -- displaces "this" to reference the base of the object.
4412 elsif Comes_From_Source (N)
4413 and then Is_Class_Wide_Type (Etype (Prefix (N)))
4414 and then Is_Interface (Etype (Prefix (N)))
4417 -- (To_Tag_Ptr (Prefix'Address)).all
4419 -- Note that Prefix'Address is recursively expanded into a call
4420 -- to Base_Address (Obj.Tag)
4422 -- Not needed for VM targets, since all handled by the VM
4424 if Tagged_Type_Expansion then
4426 Make_Explicit_Dereference (Loc,
4427 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4428 Make_Attribute_Reference (Loc,
4429 Prefix => Relocate_Node (Pref),
4430 Attribute_Name => Name_Address))));
4431 Analyze_And_Resolve (N, RTE (RE_Tag));
4436 Make_Selected_Component (Loc,
4437 Prefix => Relocate_Node (Pref),
4439 New_Reference_To (First_Tag_Component (Ttyp), Loc)));
4440 Analyze_And_Resolve (N, RTE (RE_Tag));
4448 -- Transforms 'Terminated attribute into a call to Terminated function
4450 when Attribute_Terminated => Terminated :
4452 -- The prefix of Terminated is of a task interface class-wide type.
4454 -- terminated (Task_Id (Pref._disp_get_task_id));
4456 if Ada_Version >= Ada_05
4457 and then Ekind (Ptyp) = E_Class_Wide_Type
4458 and then Is_Interface (Ptyp)
4459 and then Is_Task_Interface (Ptyp)
4462 Make_Function_Call (Loc,
4464 New_Reference_To (RTE (RE_Terminated), Loc),
4465 Parameter_Associations => New_List (
4466 Make_Unchecked_Type_Conversion (Loc,
4468 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
4470 Make_Selected_Component (Loc,
4472 New_Copy_Tree (Pref),
4474 Make_Identifier (Loc, Name_uDisp_Get_Task_Id))))));
4476 elsif Restricted_Profile then
4478 Build_Call_With_Task (Pref, RTE (RE_Restricted_Terminated)));
4482 Build_Call_With_Task (Pref, RTE (RE_Terminated)));
4485 Analyze_And_Resolve (N, Standard_Boolean);
4492 -- Transforms System'To_Address (X) into unchecked conversion
4493 -- from (integral) type of X to type address.
4495 when Attribute_To_Address =>
4497 Unchecked_Convert_To (RTE (RE_Address),
4498 Relocate_Node (First (Exprs))));
4499 Analyze_And_Resolve (N, RTE (RE_Address));
4505 when Attribute_To_Any => To_Any : declare
4506 P_Type : constant Entity_Id := Etype (Pref);
4507 Decls : constant List_Id := New_List;
4511 (Convert_To (P_Type,
4512 Relocate_Node (First (Exprs))), Decls));
4513 Insert_Actions (N, Decls);
4514 Analyze_And_Resolve (N, RTE (RE_Any));
4521 -- Transforms 'Truncation into a call to the floating-point attribute
4522 -- function Truncation in Fat_xxx (where xxx is the root type).
4523 -- Expansion is avoided for cases the back end can handle directly.
4525 when Attribute_Truncation =>
4526 if not Is_Inline_Floating_Point_Attribute (N) then
4527 Expand_Fpt_Attribute_R (N);
4534 when Attribute_TypeCode => TypeCode : declare
4535 P_Type : constant Entity_Id := Etype (Pref);
4536 Decls : constant List_Id := New_List;
4538 Rewrite (N, Build_TypeCode_Call (Loc, P_Type, Decls));
4539 Insert_Actions (N, Decls);
4540 Analyze_And_Resolve (N, RTE (RE_TypeCode));
4543 -----------------------
4544 -- Unbiased_Rounding --
4545 -----------------------
4547 -- Transforms 'Unbiased_Rounding into a call to the floating-point
4548 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
4549 -- root type). Expansion is avoided for cases the back end can handle
4552 when Attribute_Unbiased_Rounding =>
4553 if not Is_Inline_Floating_Point_Attribute (N) then
4554 Expand_Fpt_Attribute_R (N);
4561 when Attribute_UET_Address => UET_Address : declare
4562 Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
4566 Make_Object_Declaration (Loc,
4567 Defining_Identifier => Ent,
4568 Aliased_Present => True,
4569 Object_Definition =>
4570 New_Occurrence_Of (RTE (RE_Address), Loc)));
4572 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
4573 -- in normal external form.
4575 Get_External_Unit_Name_String (Get_Unit_Name (Pref));
4576 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
4577 Name_Len := Name_Len + 7;
4578 Name_Buffer (1 .. 7) := "__gnat_";
4579 Name_Buffer (Name_Len + 1 .. Name_Len + 5) := "__SDP";
4580 Name_Len := Name_Len + 5;
4582 Set_Is_Imported (Ent);
4583 Set_Interface_Name (Ent,
4584 Make_String_Literal (Loc,
4585 Strval => String_From_Name_Buffer));
4587 -- Set entity as internal to ensure proper Sprint output of its
4588 -- implicit importation.
4590 Set_Is_Internal (Ent);
4593 Make_Attribute_Reference (Loc,
4594 Prefix => New_Occurrence_Of (Ent, Loc),
4595 Attribute_Name => Name_Address));
4597 Analyze_And_Resolve (N, Typ);
4604 -- The processing for VADS_Size is shared with Size
4610 -- For enumeration types with a standard representation, and for all
4611 -- other types, Val is handled by the back end. For enumeration types
4612 -- with a non-standard representation we use the _Pos_To_Rep array that
4613 -- was created when the type was frozen.
4615 when Attribute_Val => Val : declare
4616 Etyp : constant Entity_Id := Base_Type (Entity (Pref));
4619 if Is_Enumeration_Type (Etyp)
4620 and then Present (Enum_Pos_To_Rep (Etyp))
4622 if Has_Contiguous_Rep (Etyp) then
4624 Rep_Node : constant Node_Id :=
4625 Unchecked_Convert_To (Etyp,
4628 Make_Integer_Literal (Loc,
4629 Enumeration_Rep (First_Literal (Etyp))),
4631 (Convert_To (Standard_Integer,
4632 Relocate_Node (First (Exprs))))));
4636 Unchecked_Convert_To (Etyp,
4639 Make_Integer_Literal (Loc,
4640 Enumeration_Rep (First_Literal (Etyp))),
4642 Make_Function_Call (Loc,
4645 (TSS (Etyp, TSS_Rep_To_Pos), Loc),
4646 Parameter_Associations => New_List (
4648 Rep_To_Pos_Flag (Etyp, Loc))))));
4653 Make_Indexed_Component (Loc,
4654 Prefix => New_Reference_To (Enum_Pos_To_Rep (Etyp), Loc),
4655 Expressions => New_List (
4656 Convert_To (Standard_Integer,
4657 Relocate_Node (First (Exprs))))));
4660 Analyze_And_Resolve (N, Typ);
4662 -- If the argument is marked as requiring a range check then generate
4665 elsif Do_Range_Check (First (Exprs)) then
4666 Set_Do_Range_Check (First (Exprs), False);
4667 Generate_Range_Check (First (Exprs), Etyp, CE_Range_Check_Failed);
4675 -- The code for valid is dependent on the particular types involved.
4676 -- See separate sections below for the generated code in each case.
4678 when Attribute_Valid => Valid : declare
4679 Btyp : Entity_Id := Base_Type (Ptyp);
4682 Save_Validity_Checks_On : constant Boolean := Validity_Checks_On;
4683 -- Save the validity checking mode. We always turn off validity
4684 -- checking during process of 'Valid since this is one place
4685 -- where we do not want the implicit validity checks to intefere
4686 -- with the explicit validity check that the programmer is doing.
4688 function Make_Range_Test return Node_Id;
4689 -- Build the code for a range test of the form
4690 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
4692 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
4694 ---------------------
4695 -- Make_Range_Test --
4696 ---------------------
4698 function Make_Range_Test return Node_Id is
4699 Temp : constant Node_Id := Duplicate_Subexpr (Pref);
4702 -- The value whose validity is being checked has been captured in
4703 -- an object declaration. We certainly don't want this object to
4704 -- appear valid because the declaration initializes it!
4706 if Is_Entity_Name (Temp) then
4707 Set_Is_Known_Valid (Entity (Temp), False);
4715 Unchecked_Convert_To (Btyp, Temp),
4718 Unchecked_Convert_To (Btyp,
4719 Make_Attribute_Reference (Loc,
4720 Prefix => New_Occurrence_Of (Ptyp, Loc),
4721 Attribute_Name => Name_First))),
4726 Unchecked_Convert_To (Btyp, Temp),
4729 Unchecked_Convert_To (Btyp,
4730 Make_Attribute_Reference (Loc,
4731 Prefix => New_Occurrence_Of (Ptyp, Loc),
4732 Attribute_Name => Name_Last))));
4733 end Make_Range_Test;
4735 -- Start of processing for Attribute_Valid
4738 -- Do not expand sourced code 'Valid reference in CodePeer mode,
4739 -- will be handled by the back-end directly.
4741 if CodePeer_Mode and then Comes_From_Source (N) then
4745 -- Turn off validity checks. We do not want any implicit validity
4746 -- checks to intefere with the explicit check from the attribute
4748 Validity_Checks_On := False;
4750 -- Floating-point case. This case is handled by the Valid attribute
4751 -- code in the floating-point attribute run-time library.
4753 if Is_Floating_Point_Type (Ptyp) then
4759 -- For vax fpt types, call appropriate routine in special vax
4760 -- floating point unit. We do not have to worry about loads in
4761 -- this case, since these types have no signalling NaN's.
4763 if Vax_Float (Btyp) then
4764 Expand_Vax_Valid (N);
4766 -- The AAMP back end handles Valid for floating-point types
4768 elsif Is_AAMP_Float (Btyp) then
4769 Analyze_And_Resolve (Pref, Ptyp);
4770 Set_Etype (N, Standard_Boolean);
4773 -- Non VAX float case
4776 Find_Fat_Info (Ptyp, Ftp, Pkg);
4778 -- If the floating-point object might be unaligned, we need
4779 -- to call the special routine Unaligned_Valid, which makes
4780 -- the needed copy, being careful not to load the value into
4781 -- any floating-point register. The argument in this case is
4782 -- obj'Address (see Unaligned_Valid routine in Fat_Gen).
4784 if Is_Possibly_Unaligned_Object (Pref) then
4785 Expand_Fpt_Attribute
4786 (N, Pkg, Name_Unaligned_Valid,
4788 Make_Attribute_Reference (Loc,
4789 Prefix => Relocate_Node (Pref),
4790 Attribute_Name => Name_Address)));
4792 -- In the normal case where we are sure the object is
4793 -- aligned, we generate a call to Valid, and the argument in
4794 -- this case is obj'Unrestricted_Access (after converting
4795 -- obj to the right floating-point type).
4798 Expand_Fpt_Attribute
4799 (N, Pkg, Name_Valid,
4801 Make_Attribute_Reference (Loc,
4802 Prefix => Unchecked_Convert_To (Ftp, Pref),
4803 Attribute_Name => Name_Unrestricted_Access)));
4807 -- One more task, we still need a range check. Required
4808 -- only if we have a constraint, since the Valid routine
4809 -- catches infinities properly (infinities are never valid).
4811 -- The way we do the range check is simply to create the
4812 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
4814 if not Subtypes_Statically_Match (Ptyp, Btyp) then
4817 Left_Opnd => Relocate_Node (N),
4820 Left_Opnd => Convert_To (Btyp, Pref),
4821 Right_Opnd => New_Occurrence_Of (Ptyp, Loc))));
4825 -- Enumeration type with holes
4827 -- For enumeration types with holes, the Pos value constructed by
4828 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
4829 -- second argument of False returns minus one for an invalid value,
4830 -- and the non-negative pos value for a valid value, so the
4831 -- expansion of X'Valid is simply:
4833 -- type(X)'Pos (X) >= 0
4835 -- We can't quite generate it that way because of the requirement
4836 -- for the non-standard second argument of False in the resulting
4837 -- rep_to_pos call, so we have to explicitly create:
4839 -- _rep_to_pos (X, False) >= 0
4841 -- If we have an enumeration subtype, we also check that the
4842 -- value is in range:
4844 -- _rep_to_pos (X, False) >= 0
4846 -- (X >= type(X)'First and then type(X)'Last <= X)
4848 elsif Is_Enumeration_Type (Ptyp)
4849 and then Present (Enum_Pos_To_Rep (Base_Type (Ptyp)))
4854 Make_Function_Call (Loc,
4857 (TSS (Base_Type (Ptyp), TSS_Rep_To_Pos), Loc),
4858 Parameter_Associations => New_List (
4860 New_Occurrence_Of (Standard_False, Loc))),
4861 Right_Opnd => Make_Integer_Literal (Loc, 0));
4865 (Type_Low_Bound (Ptyp) /= Type_Low_Bound (Btyp)
4867 Type_High_Bound (Ptyp) /= Type_High_Bound (Btyp))
4869 -- The call to Make_Range_Test will create declarations
4870 -- that need a proper insertion point, but Pref is now
4871 -- attached to a node with no ancestor. Attach to tree
4872 -- even if it is to be rewritten below.
4874 Set_Parent (Tst, Parent (N));
4878 Left_Opnd => Make_Range_Test,
4884 -- Fortran convention booleans
4886 -- For the very special case of Fortran convention booleans, the
4887 -- value is always valid, since it is an integer with the semantics
4888 -- that non-zero is true, and any value is permissible.
4890 elsif Is_Boolean_Type (Ptyp)
4891 and then Convention (Ptyp) = Convention_Fortran
4893 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
4895 -- For biased representations, we will be doing an unchecked
4896 -- conversion without unbiasing the result. That means that the range
4897 -- test has to take this into account, and the proper form of the
4900 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
4902 elsif Has_Biased_Representation (Ptyp) then
4903 Btyp := RTE (RE_Unsigned_32);
4907 Unchecked_Convert_To (Btyp, Duplicate_Subexpr (Pref)),
4909 Unchecked_Convert_To (Btyp,
4910 Make_Attribute_Reference (Loc,
4911 Prefix => New_Occurrence_Of (Ptyp, Loc),
4912 Attribute_Name => Name_Range_Length))));
4914 -- For all other scalar types, what we want logically is a
4917 -- X in type(X)'First .. type(X)'Last
4919 -- But that's precisely what won't work because of possible
4920 -- unwanted optimization (and indeed the basic motivation for
4921 -- the Valid attribute is exactly that this test does not work!)
4922 -- What will work is:
4924 -- Btyp!(X) >= Btyp!(type(X)'First)
4926 -- Btyp!(X) <= Btyp!(type(X)'Last)
4928 -- where Btyp is an integer type large enough to cover the full
4929 -- range of possible stored values (i.e. it is chosen on the basis
4930 -- of the size of the type, not the range of the values). We write
4931 -- this as two tests, rather than a range check, so that static
4932 -- evaluation will easily remove either or both of the checks if
4933 -- they can be -statically determined to be true (this happens
4934 -- when the type of X is static and the range extends to the full
4935 -- range of stored values).
4937 -- Unsigned types. Note: it is safe to consider only whether the
4938 -- subtype is unsigned, since we will in that case be doing all
4939 -- unsigned comparisons based on the subtype range. Since we use the
4940 -- actual subtype object size, this is appropriate.
4942 -- For example, if we have
4944 -- subtype x is integer range 1 .. 200;
4945 -- for x'Object_Size use 8;
4947 -- Now the base type is signed, but objects of this type are bits
4948 -- unsigned, and doing an unsigned test of the range 1 to 200 is
4949 -- correct, even though a value greater than 127 looks signed to a
4950 -- signed comparison.
4952 elsif Is_Unsigned_Type (Ptyp) then
4953 if Esize (Ptyp) <= 32 then
4954 Btyp := RTE (RE_Unsigned_32);
4956 Btyp := RTE (RE_Unsigned_64);
4959 Rewrite (N, Make_Range_Test);
4964 if Esize (Ptyp) <= Esize (Standard_Integer) then
4965 Btyp := Standard_Integer;
4967 Btyp := Universal_Integer;
4970 Rewrite (N, Make_Range_Test);
4973 Analyze_And_Resolve (N, Standard_Boolean);
4974 Validity_Checks_On := Save_Validity_Checks_On;
4981 -- Value attribute is handled in separate unti Exp_Imgv
4983 when Attribute_Value =>
4984 Exp_Imgv.Expand_Value_Attribute (N);
4990 -- The processing for Value_Size shares the processing for Size
4996 -- The processing for Version shares the processing for Body_Version
5002 -- Wide_Image attribute is handled in separate unit Exp_Imgv
5004 when Attribute_Wide_Image =>
5005 Exp_Imgv.Expand_Wide_Image_Attribute (N);
5007 ---------------------
5008 -- Wide_Wide_Image --
5009 ---------------------
5011 -- Wide_Wide_Image attribute is handled in separate unit Exp_Imgv
5013 when Attribute_Wide_Wide_Image =>
5014 Exp_Imgv.Expand_Wide_Wide_Image_Attribute (N);
5020 -- We expand typ'Wide_Value (X) into
5023 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
5025 -- Wide_String_To_String is a runtime function that converts its wide
5026 -- string argument to String, converting any non-translatable characters
5027 -- into appropriate escape sequences. This preserves the required
5028 -- semantics of Wide_Value in all cases, and results in a very simple
5029 -- implementation approach.
5031 -- Note: for this approach to be fully standard compliant for the cases
5032 -- where typ is Wide_Character and Wide_Wide_Character, the encoding
5033 -- method must cover the entire character range (e.g. UTF-8). But that
5034 -- is a reasonable requirement when dealing with encoded character
5035 -- sequences. Presumably if one of the restrictive encoding mechanisms
5036 -- is in use such as Shift-JIS, then characters that cannot be
5037 -- represented using this encoding will not appear in any case.
5039 when Attribute_Wide_Value => Wide_Value :
5042 Make_Attribute_Reference (Loc,
5044 Attribute_Name => Name_Value,
5046 Expressions => New_List (
5047 Make_Function_Call (Loc,
5049 New_Reference_To (RTE (RE_Wide_String_To_String), Loc),
5051 Parameter_Associations => New_List (
5052 Relocate_Node (First (Exprs)),
5053 Make_Integer_Literal (Loc,
5054 Intval => Int (Wide_Character_Encoding_Method)))))));
5056 Analyze_And_Resolve (N, Typ);
5059 ---------------------
5060 -- Wide_Wide_Value --
5061 ---------------------
5063 -- We expand typ'Wide_Value_Value (X) into
5066 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
5068 -- Wide_Wide_String_To_String is a runtime function that converts its
5069 -- wide string argument to String, converting any non-translatable
5070 -- characters into appropriate escape sequences. This preserves the
5071 -- required semantics of Wide_Wide_Value in all cases, and results in a
5072 -- very simple implementation approach.
5074 -- It's not quite right where typ = Wide_Wide_Character, because the
5075 -- encoding method may not cover the whole character type ???
5077 when Attribute_Wide_Wide_Value => Wide_Wide_Value :
5080 Make_Attribute_Reference (Loc,
5082 Attribute_Name => Name_Value,
5084 Expressions => New_List (
5085 Make_Function_Call (Loc,
5087 New_Reference_To (RTE (RE_Wide_Wide_String_To_String), Loc),
5089 Parameter_Associations => New_List (
5090 Relocate_Node (First (Exprs)),
5091 Make_Integer_Literal (Loc,
5092 Intval => Int (Wide_Character_Encoding_Method)))))));
5094 Analyze_And_Resolve (N, Typ);
5095 end Wide_Wide_Value;
5097 ---------------------
5098 -- Wide_Wide_Width --
5099 ---------------------
5101 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
5103 when Attribute_Wide_Wide_Width =>
5104 Exp_Imgv.Expand_Width_Attribute (N, Wide_Wide);
5110 -- Wide_Width attribute is handled in separate unit Exp_Imgv
5112 when Attribute_Wide_Width =>
5113 Exp_Imgv.Expand_Width_Attribute (N, Wide);
5119 -- Width attribute is handled in separate unit Exp_Imgv
5121 when Attribute_Width =>
5122 Exp_Imgv.Expand_Width_Attribute (N, Normal);
5128 when Attribute_Write => Write : declare
5129 P_Type : constant Entity_Id := Entity (Pref);
5130 U_Type : constant Entity_Id := Underlying_Type (P_Type);
5138 -- If no underlying type, we have an error that will be diagnosed
5139 -- elsewhere, so here we just completely ignore the expansion.
5145 -- The simple case, if there is a TSS for Write, just call it
5147 Pname := Find_Stream_Subprogram (P_Type, TSS_Stream_Write);
5149 if Present (Pname) then
5153 -- If there is a Stream_Convert pragma, use it, we rewrite
5155 -- sourcetyp'Output (stream, Item)
5159 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
5161 -- where strmwrite is the given Write function that converts an
5162 -- argument of type sourcetyp or a type acctyp, from which it is
5163 -- derived to type strmtyp. The conversion to acttyp is required
5164 -- for the derived case.
5166 Prag := Get_Stream_Convert_Pragma (P_Type);
5168 if Present (Prag) then
5170 Next (Next (First (Pragma_Argument_Associations (Prag))));
5171 Wfunc := Entity (Expression (Arg3));
5174 Make_Attribute_Reference (Loc,
5175 Prefix => New_Occurrence_Of (Etype (Wfunc), Loc),
5176 Attribute_Name => Name_Output,
5177 Expressions => New_List (
5178 Relocate_Node (First (Exprs)),
5179 Make_Function_Call (Loc,
5180 Name => New_Occurrence_Of (Wfunc, Loc),
5181 Parameter_Associations => New_List (
5182 OK_Convert_To (Etype (First_Formal (Wfunc)),
5183 Relocate_Node (Next (First (Exprs)))))))));
5188 -- For elementary types, we call the W_xxx routine directly
5190 elsif Is_Elementary_Type (U_Type) then
5191 Rewrite (N, Build_Elementary_Write_Call (N));
5197 elsif Is_Array_Type (U_Type) then
5198 Build_Array_Write_Procedure (N, U_Type, Decl, Pname);
5199 Compile_Stream_Body_In_Scope (N, Decl, U_Type, Check => False);
5201 -- Tagged type case, use the primitive Write function. Note that
5202 -- this will dispatch in the class-wide case which is what we want
5204 elsif Is_Tagged_Type (U_Type) then
5205 Pname := Find_Prim_Op (U_Type, TSS_Stream_Write);
5207 -- All other record type cases, including protected records.
5208 -- The latter only arise for expander generated code for
5209 -- handling shared passive partition access.
5213 (Is_Record_Type (U_Type) or else Is_Protected_Type (U_Type));
5215 -- Ada 2005 (AI-216): Program_Error is raised when executing
5216 -- the default implementation of the Write attribute of an
5217 -- Unchecked_Union type. However, if the 'Write reference is
5218 -- within the generated Output stream procedure, Write outputs
5219 -- the components, and the default values of the discriminant
5220 -- are streamed by the Output procedure itself.
5222 if Is_Unchecked_Union (Base_Type (U_Type))
5223 and not Is_TSS (Current_Scope, TSS_Stream_Output)
5226 Make_Raise_Program_Error (Loc,
5227 Reason => PE_Unchecked_Union_Restriction));
5230 if Has_Discriminants (U_Type)
5232 (Discriminant_Default_Value (First_Discriminant (U_Type)))
5234 Build_Mutable_Record_Write_Procedure
5235 (Loc, Base_Type (U_Type), Decl, Pname);
5237 Build_Record_Write_Procedure
5238 (Loc, Base_Type (U_Type), Decl, Pname);
5241 Insert_Action (N, Decl);
5245 -- If we fall through, Pname is the procedure to be called
5247 Rewrite_Stream_Proc_Call (Pname);
5250 -- Component_Size is handled by the back end, unless the component size
5251 -- is known at compile time, which is always true in the packed array
5252 -- case. It is important that the packed array case is handled in the
5253 -- front end (see Eval_Attribute) since the back end would otherwise get
5254 -- confused by the equivalent packed array type.
5256 when Attribute_Component_Size =>
5259 -- The following attributes are handled by the back end (except that
5260 -- static cases have already been evaluated during semantic processing,
5261 -- but in any case the back end should not count on this). The one bit
5262 -- of special processing required is that these attributes typically
5263 -- generate conditionals in the code, so we need to check the relevant
5266 when Attribute_Max |
5268 Check_Restriction (No_Implicit_Conditionals, N);
5270 -- The following attributes are handled by the back end (except that
5271 -- static cases have already been evaluated during semantic processing,
5272 -- but in any case the back end should not count on this).
5274 -- The back end also handles the non-class-wide cases of Size
5276 when Attribute_Bit_Order |
5277 Attribute_Code_Address |
5278 Attribute_Definite |
5279 Attribute_Null_Parameter |
5280 Attribute_Passed_By_Reference |
5281 Attribute_Pool_Address =>
5284 -- The following attributes are also handled by the back end, but return
5285 -- a universal integer result, so may need a conversion for checking
5286 -- that the result is in range.
5288 when Attribute_Aft |
5289 Attribute_Max_Size_In_Storage_Elements
5291 Apply_Universal_Integer_Attribute_Checks (N);
5293 -- The following attributes should not appear at this stage, since they
5294 -- have already been handled by the analyzer (and properly rewritten
5295 -- with corresponding values or entities to represent the right values)
5297 when Attribute_Abort_Signal |
5298 Attribute_Address_Size |
5301 Attribute_Compiler_Version |
5302 Attribute_Default_Bit_Order |
5309 Attribute_Fast_Math |
5310 Attribute_Has_Access_Values |
5311 Attribute_Has_Discriminants |
5312 Attribute_Has_Tagged_Values |
5314 Attribute_Machine_Emax |
5315 Attribute_Machine_Emin |
5316 Attribute_Machine_Mantissa |
5317 Attribute_Machine_Overflows |
5318 Attribute_Machine_Radix |
5319 Attribute_Machine_Rounds |
5320 Attribute_Maximum_Alignment |
5321 Attribute_Model_Emin |
5322 Attribute_Model_Epsilon |
5323 Attribute_Model_Mantissa |
5324 Attribute_Model_Small |
5326 Attribute_Partition_ID |
5328 Attribute_Safe_Emax |
5329 Attribute_Safe_First |
5330 Attribute_Safe_Large |
5331 Attribute_Safe_Last |
5332 Attribute_Safe_Small |
5334 Attribute_Signed_Zeros |
5336 Attribute_Storage_Unit |
5337 Attribute_Stub_Type |
5338 Attribute_Target_Name |
5339 Attribute_Type_Class |
5340 Attribute_Unconstrained_Array |
5341 Attribute_Universal_Literal_String |
5342 Attribute_Wchar_T_Size |
5343 Attribute_Word_Size =>
5345 raise Program_Error;
5347 -- The Asm_Input and Asm_Output attributes are not expanded at this
5348 -- stage, but will be eliminated in the expansion of the Asm call, see
5349 -- Exp_Intr for details. So the back end will never see these either.
5351 when Attribute_Asm_Input |
5352 Attribute_Asm_Output =>
5359 when RE_Not_Available =>
5361 end Expand_N_Attribute_Reference;
5363 ----------------------
5364 -- Expand_Pred_Succ --
5365 ----------------------
5367 -- For typ'Pred (exp), we generate the check
5369 -- [constraint_error when exp = typ'Base'First]
5371 -- Similarly, for typ'Succ (exp), we generate the check
5373 -- [constraint_error when exp = typ'Base'Last]
5375 -- These checks are not generated for modular types, since the proper
5376 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
5378 procedure Expand_Pred_Succ (N : Node_Id) is
5379 Loc : constant Source_Ptr := Sloc (N);
5383 if Attribute_Name (N) = Name_Pred then
5390 Make_Raise_Constraint_Error (Loc,
5394 Duplicate_Subexpr_Move_Checks (First (Expressions (N))),
5396 Make_Attribute_Reference (Loc,
5398 New_Reference_To (Base_Type (Etype (Prefix (N))), Loc),
5399 Attribute_Name => Cnam)),
5400 Reason => CE_Overflow_Check_Failed));
5401 end Expand_Pred_Succ;
5407 procedure Find_Fat_Info
5409 Fat_Type : out Entity_Id;
5410 Fat_Pkg : out RE_Id)
5412 Btyp : constant Entity_Id := Base_Type (T);
5413 Rtyp : constant Entity_Id := Root_Type (T);
5414 Digs : constant Nat := UI_To_Int (Digits_Value (Btyp));
5417 -- If the base type is VAX float, then get appropriate VAX float type
5419 if Vax_Float (Btyp) then
5422 Fat_Type := RTE (RE_Fat_VAX_F);
5423 Fat_Pkg := RE_Attr_VAX_F_Float;
5426 Fat_Type := RTE (RE_Fat_VAX_D);
5427 Fat_Pkg := RE_Attr_VAX_D_Float;
5430 Fat_Type := RTE (RE_Fat_VAX_G);
5431 Fat_Pkg := RE_Attr_VAX_G_Float;
5434 raise Program_Error;
5437 -- If root type is VAX float, this is the case where the library has
5438 -- been recompiled in VAX float mode, and we have an IEEE float type.
5439 -- This is when we use the special IEEE Fat packages.
5441 elsif Vax_Float (Rtyp) then
5444 Fat_Type := RTE (RE_Fat_IEEE_Short);
5445 Fat_Pkg := RE_Attr_IEEE_Short;
5448 Fat_Type := RTE (RE_Fat_IEEE_Long);
5449 Fat_Pkg := RE_Attr_IEEE_Long;
5452 raise Program_Error;
5455 -- If neither the base type nor the root type is VAX_Float then VAX
5456 -- float is out of the picture, and we can just use the root type.
5461 if Fat_Type = Standard_Short_Float then
5462 Fat_Pkg := RE_Attr_Short_Float;
5464 elsif Fat_Type = Standard_Float then
5465 Fat_Pkg := RE_Attr_Float;
5467 elsif Fat_Type = Standard_Long_Float then
5468 Fat_Pkg := RE_Attr_Long_Float;
5470 elsif Fat_Type = Standard_Long_Long_Float then
5471 Fat_Pkg := RE_Attr_Long_Long_Float;
5473 -- Universal real (which is its own root type) is treated as being
5474 -- equivalent to Standard.Long_Long_Float, since it is defined to
5475 -- have the same precision as the longest Float type.
5477 elsif Fat_Type = Universal_Real then
5478 Fat_Type := Standard_Long_Long_Float;
5479 Fat_Pkg := RE_Attr_Long_Long_Float;
5482 raise Program_Error;
5487 ----------------------------
5488 -- Find_Stream_Subprogram --
5489 ----------------------------
5491 function Find_Stream_Subprogram
5493 Nam : TSS_Name_Type) return Entity_Id
5495 Base_Typ : constant Entity_Id := Base_Type (Typ);
5496 Ent : constant Entity_Id := TSS (Typ, Nam);
5499 if Present (Ent) then
5503 -- Stream attributes for strings are expanded into library calls. The
5504 -- following checks are disabled when the run-time is not available or
5505 -- when compiling predefined types due to bootstrap issues. As a result,
5506 -- the compiler will generate in-place stream routines for string types
5507 -- that appear in GNAT's library, but will generate calls via rtsfind
5508 -- to library routines for user code.
5509 -- ??? For now, disable this code for JVM, since this generates a
5510 -- VerifyError exception at run-time on e.g. c330001.
5511 -- This is disabled for AAMP, to avoid making dependences on files not
5512 -- supported in the AAMP library (such as s-fileio.adb).
5514 if VM_Target /= JVM_Target
5515 and then not AAMP_On_Target
5517 not Is_Predefined_File_Name (Unit_File_Name (Current_Sem_Unit))
5519 -- String as defined in package Ada
5521 if Base_Typ = Standard_String then
5522 if Restriction_Active (No_Stream_Optimizations) then
5523 if Nam = TSS_Stream_Input then
5524 return RTE (RE_String_Input);
5526 elsif Nam = TSS_Stream_Output then
5527 return RTE (RE_String_Output);
5529 elsif Nam = TSS_Stream_Read then
5530 return RTE (RE_String_Read);
5532 else pragma Assert (Nam = TSS_Stream_Write);
5533 return RTE (RE_String_Write);
5537 if Nam = TSS_Stream_Input then
5538 return RTE (RE_String_Input_Blk_IO);
5540 elsif Nam = TSS_Stream_Output then
5541 return RTE (RE_String_Output_Blk_IO);
5543 elsif Nam = TSS_Stream_Read then
5544 return RTE (RE_String_Read_Blk_IO);
5546 else pragma Assert (Nam = TSS_Stream_Write);
5547 return RTE (RE_String_Write_Blk_IO);
5551 -- Wide_String as defined in package Ada
5553 elsif Base_Typ = Standard_Wide_String then
5554 if Restriction_Active (No_Stream_Optimizations) then
5555 if Nam = TSS_Stream_Input then
5556 return RTE (RE_Wide_String_Input);
5558 elsif Nam = TSS_Stream_Output then
5559 return RTE (RE_Wide_String_Output);
5561 elsif Nam = TSS_Stream_Read then
5562 return RTE (RE_Wide_String_Read);
5564 else pragma Assert (Nam = TSS_Stream_Write);
5565 return RTE (RE_Wide_String_Write);
5569 if Nam = TSS_Stream_Input then
5570 return RTE (RE_Wide_String_Input_Blk_IO);
5572 elsif Nam = TSS_Stream_Output then
5573 return RTE (RE_Wide_String_Output_Blk_IO);
5575 elsif Nam = TSS_Stream_Read then
5576 return RTE (RE_Wide_String_Read_Blk_IO);
5578 else pragma Assert (Nam = TSS_Stream_Write);
5579 return RTE (RE_Wide_String_Write_Blk_IO);
5583 -- Wide_Wide_String as defined in package Ada
5585 elsif Base_Typ = Standard_Wide_Wide_String then
5586 if Restriction_Active (No_Stream_Optimizations) then
5587 if Nam = TSS_Stream_Input then
5588 return RTE (RE_Wide_Wide_String_Input);
5590 elsif Nam = TSS_Stream_Output then
5591 return RTE (RE_Wide_Wide_String_Output);
5593 elsif Nam = TSS_Stream_Read then
5594 return RTE (RE_Wide_Wide_String_Read);
5596 else pragma Assert (Nam = TSS_Stream_Write);
5597 return RTE (RE_Wide_Wide_String_Write);
5601 if Nam = TSS_Stream_Input then
5602 return RTE (RE_Wide_Wide_String_Input_Blk_IO);
5604 elsif Nam = TSS_Stream_Output then
5605 return RTE (RE_Wide_Wide_String_Output_Blk_IO);
5607 elsif Nam = TSS_Stream_Read then
5608 return RTE (RE_Wide_Wide_String_Read_Blk_IO);
5610 else pragma Assert (Nam = TSS_Stream_Write);
5611 return RTE (RE_Wide_Wide_String_Write_Blk_IO);
5617 if Is_Tagged_Type (Typ)
5618 and then Is_Derived_Type (Typ)
5620 return Find_Prim_Op (Typ, Nam);
5622 return Find_Inherited_TSS (Typ, Nam);
5624 end Find_Stream_Subprogram;
5626 -----------------------
5627 -- Get_Index_Subtype --
5628 -----------------------
5630 function Get_Index_Subtype (N : Node_Id) return Node_Id is
5631 P_Type : Entity_Id := Etype (Prefix (N));
5636 if Is_Access_Type (P_Type) then
5637 P_Type := Designated_Type (P_Type);
5640 if No (Expressions (N)) then
5643 J := UI_To_Int (Expr_Value (First (Expressions (N))));
5646 Indx := First_Index (P_Type);
5652 return Etype (Indx);
5653 end Get_Index_Subtype;
5655 -------------------------------
5656 -- Get_Stream_Convert_Pragma --
5657 -------------------------------
5659 function Get_Stream_Convert_Pragma (T : Entity_Id) return Node_Id is
5664 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
5665 -- that a stream convert pragma for a tagged type is not inherited from
5666 -- its parent. Probably what is wrong here is that it is basically
5667 -- incorrect to consider a stream convert pragma to be a representation
5668 -- pragma at all ???
5670 N := First_Rep_Item (Implementation_Base_Type (T));
5671 while Present (N) loop
5672 if Nkind (N) = N_Pragma
5673 and then Pragma_Name (N) = Name_Stream_Convert
5675 -- For tagged types this pragma is not inherited, so we
5676 -- must verify that it is defined for the given type and
5680 Entity (Expression (First (Pragma_Argument_Associations (N))));
5682 if not Is_Tagged_Type (T)
5684 or else (Is_Private_Type (Typ) and then T = Full_View (Typ))
5694 end Get_Stream_Convert_Pragma;
5696 ---------------------------------
5697 -- Is_Constrained_Packed_Array --
5698 ---------------------------------
5700 function Is_Constrained_Packed_Array (Typ : Entity_Id) return Boolean is
5701 Arr : Entity_Id := Typ;
5704 if Is_Access_Type (Arr) then
5705 Arr := Designated_Type (Arr);
5708 return Is_Array_Type (Arr)
5709 and then Is_Constrained (Arr)
5710 and then Present (Packed_Array_Type (Arr));
5711 end Is_Constrained_Packed_Array;
5713 ----------------------------------------
5714 -- Is_Inline_Floating_Point_Attribute --
5715 ----------------------------------------
5717 function Is_Inline_Floating_Point_Attribute (N : Node_Id) return Boolean is
5718 Id : constant Attribute_Id := Get_Attribute_Id (Attribute_Name (N));
5721 if Nkind (Parent (N)) /= N_Type_Conversion
5722 or else not Is_Integer_Type (Etype (Parent (N)))
5727 -- Should also support 'Machine_Rounding and 'Unbiased_Rounding, but
5728 -- required back end support has not been implemented yet ???
5730 return Id = Attribute_Truncation;
5731 end Is_Inline_Floating_Point_Attribute;