1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, 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 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
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_Ch2
; use Exp_Ch2
;
32 with Exp_Ch9
; use Exp_Ch9
;
33 with Exp_Imgv
; use Exp_Imgv
;
34 with Exp_Pakd
; use Exp_Pakd
;
35 with Exp_Strm
; use Exp_Strm
;
36 with Exp_Tss
; use Exp_Tss
;
37 with Exp_Util
; use Exp_Util
;
38 with Gnatvsn
; use Gnatvsn
;
39 with Hostparm
; use Hostparm
;
41 with Namet
; use Namet
;
42 with Nmake
; use Nmake
;
43 with Nlists
; use Nlists
;
45 with Restrict
; use Restrict
;
46 with Rident
; use Rident
;
47 with Rtsfind
; use Rtsfind
;
49 with Sem_Ch7
; use Sem_Ch7
;
50 with Sem_Ch8
; use Sem_Ch8
;
51 with Sem_Eval
; use Sem_Eval
;
52 with Sem_Res
; use Sem_Res
;
53 with Sem_Util
; use Sem_Util
;
54 with Sinfo
; use Sinfo
;
55 with Snames
; use Snames
;
56 with Stand
; use Stand
;
57 with Stringt
; use Stringt
;
58 with Tbuild
; use Tbuild
;
59 with Ttypes
; use Ttypes
;
60 with Uintp
; use Uintp
;
61 with Uname
; use Uname
;
62 with Validsw
; use Validsw
;
64 package body Exp_Attr
is
66 -----------------------
67 -- Local Subprograms --
68 -----------------------
70 procedure Compile_Stream_Body_In_Scope
75 -- The body for a stream subprogram may be generated outside of the scope
76 -- of the type. If the type is fully private, it may depend on the full
77 -- view of other types (e.g. indices) that are currently private as well.
78 -- We install the declarations of the package in which the type is declared
79 -- before compiling the body in what is its proper environment. The Check
80 -- parameter indicates if checks are to be suppressed for the stream body.
81 -- We suppress checks for array/record reads, since the rule is that these
82 -- are like assignments, out of range values due to uninitialized storage,
83 -- or other invalid values do NOT cause a Constraint_Error to be raised.
85 procedure Expand_Fpt_Attribute
90 -- This procedure expands a call to a floating-point attribute function.
91 -- N is the attribute reference node, and Args is a list of arguments to
92 -- be passed to the function call. Rtp is the root type of the floating
93 -- point type involved (used to select the proper generic instantiation
94 -- of the package containing the attribute routines). The Nam argument
95 -- is the attribute processing routine to be called. This is normally
96 -- the same as the attribute name, except in the Unaligned_Valid case.
98 procedure Expand_Fpt_Attribute_R
(N
: Node_Id
);
99 -- This procedure expands a call to a floating-point attribute function
100 -- that takes a single floating-point argument. The function to be called
101 -- is always the same as the attribute name.
103 procedure Expand_Fpt_Attribute_RI
(N
: Node_Id
);
104 -- This procedure expands a call to a floating-point attribute function
105 -- that takes one floating-point argument and one integer argument. The
106 -- function to be called is always the same as the attribute name.
108 procedure Expand_Fpt_Attribute_RR
(N
: Node_Id
);
109 -- This procedure expands a call to a floating-point attribute function
110 -- that takes two floating-point arguments. The function to be called
111 -- is always the same as the attribute name.
113 procedure Expand_Pred_Succ
(N
: Node_Id
);
114 -- Handles expansion of Pred or Succ attributes for case of non-real
115 -- operand with overflow checking required.
117 function Get_Index_Subtype
(N
: Node_Id
) return Entity_Id
;
118 -- Used for Last, Last, and Length, when the prefix is an array type,
119 -- Obtains the corresponding index subtype.
121 procedure Expand_Access_To_Type
(N
: Node_Id
);
122 -- A reference to a type within its own scope is resolved to a reference
123 -- to the current instance of the type in its initialization procedure.
125 function Find_Inherited_TSS
127 Nam
: TSS_Name_Type
) return Entity_Id
;
128 -- Returns the TSS of name Nam of Typ, or of its closest ancestor defining
129 -- such a TSS. Empty is returned is neither Typ nor any of its ancestors
132 function Find_Stream_Subprogram
134 Nam
: TSS_Name_Type
) return Entity_Id
;
135 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
136 -- types, the corresponding primitive operation is looked up, else the
137 -- appropriate TSS from the type itself, or from its closest ancestor
138 -- defining it, is returned. In both cases, inheritance of representation
139 -- aspects is thus taken into account.
141 function Get_Stream_Convert_Pragma
(T
: Entity_Id
) return Node_Id
;
142 -- Given a type, find a corresponding stream convert pragma that applies to
143 -- the implementation base type of this type (Typ). If found, return the
144 -- pragma node, otherwise return Empty if no pragma is found.
146 function Is_Constrained_Packed_Array
(Typ
: Entity_Id
) return Boolean;
147 -- Utility for array attributes, returns true on packed constrained
148 -- arrays, and on access to same.
150 ----------------------------------
151 -- Compile_Stream_Body_In_Scope --
152 ----------------------------------
154 procedure Compile_Stream_Body_In_Scope
160 Installed
: Boolean := False;
161 Scop
: constant Entity_Id
:= Scope
(Arr
);
162 Curr
: constant Entity_Id
:= Current_Scope
;
166 and then not In_Open_Scopes
(Scop
)
167 and then Ekind
(Scop
) = E_Package
170 Install_Visible_Declarations
(Scop
);
171 Install_Private_Declarations
(Scop
);
174 -- The entities in the package are now visible, but the generated
175 -- stream entity must appear in the current scope (usually an
176 -- enclosing stream function) so that itypes all have their proper
183 Insert_Action
(N
, Decl
);
185 Insert_Action
(N
, Decl
, All_Checks
);
190 -- Remove extra copy of current scope, and package itself
193 End_Package_Scope
(Scop
);
195 end Compile_Stream_Body_In_Scope
;
197 ---------------------------
198 -- Expand_Access_To_Type --
199 ---------------------------
201 procedure Expand_Access_To_Type
(N
: Node_Id
) is
202 Loc
: constant Source_Ptr
:= Sloc
(N
);
203 Typ
: constant Entity_Id
:= Etype
(N
);
204 Pref
: constant Node_Id
:= Prefix
(N
);
209 if Is_Entity_Name
(Pref
)
210 and then Is_Type
(Entity
(Pref
))
212 -- If the current instance name denotes a task type,
213 -- then the access attribute is rewritten to be the
214 -- name of the "_task" parameter associated with the
215 -- task type's task body procedure. An unchecked
216 -- conversion is applied to ensure a type match in
217 -- cases of expander-generated calls (e.g., init procs).
219 if Is_Task_Type
(Entity
(Pref
)) then
221 First_Entity
(Get_Task_Body_Procedure
(Entity
(Pref
)));
223 while Present
(Formal
) loop
224 exit when Chars
(Formal
) = Name_uTask
;
225 Next_Entity
(Formal
);
228 pragma Assert
(Present
(Formal
));
231 Unchecked_Convert_To
(Typ
, New_Occurrence_Of
(Formal
, Loc
)));
234 -- The expression must appear in a default expression,
235 -- (which in the initialization procedure is the rhs of
236 -- an assignment), and not in a discriminant constraint.
241 while Present
(Par
) loop
242 exit when Nkind
(Par
) = N_Assignment_Statement
;
244 if Nkind
(Par
) = N_Component_Declaration
then
251 if Present
(Par
) then
253 Make_Attribute_Reference
(Loc
,
254 Prefix
=> Make_Identifier
(Loc
, Name_uInit
),
255 Attribute_Name
=> Attribute_Name
(N
)));
257 Analyze_And_Resolve
(N
, Typ
);
261 end Expand_Access_To_Type
;
263 --------------------------
264 -- Expand_Fpt_Attribute --
265 --------------------------
267 procedure Expand_Fpt_Attribute
273 Loc
: constant Source_Ptr
:= Sloc
(N
);
274 Typ
: constant Entity_Id
:= Etype
(N
);
279 -- The function name is the selected component Fat_xxx.yyy where xxx
280 -- is the floating-point root type, and yyy is the argument Nam.
282 -- Note: it would be more usual to have separate RE entries for each
283 -- of the entities in the Fat packages, but first they have identical
284 -- names (so we would have to have lots of renaming declarations to
285 -- meet the normal RE rule of separate names for all runtime entities),
286 -- and second there would be an awful lot of them!
288 if Rtp
= Standard_Short_Float
then
289 Pkg
:= RE_Fat_Short_Float
;
290 elsif Rtp
= Standard_Float
then
292 elsif Rtp
= Standard_Long_Float
then
293 Pkg
:= RE_Fat_Long_Float
;
295 Pkg
:= RE_Fat_Long_Long_Float
;
299 Make_Selected_Component
(Loc
,
300 Prefix
=> New_Reference_To
(RTE
(Pkg
), Loc
),
301 Selector_Name
=> Make_Identifier
(Loc
, Nam
));
303 -- The generated call is given the provided set of parameters, and then
304 -- wrapped in a conversion which converts the result to the target type
305 -- We use the base type as the target because a range check may be
309 Unchecked_Convert_To
(Base_Type
(Etype
(N
)),
310 Make_Function_Call
(Loc
,
312 Parameter_Associations
=> Args
)));
314 Analyze_And_Resolve
(N
, Typ
);
315 end Expand_Fpt_Attribute
;
317 ----------------------------
318 -- Expand_Fpt_Attribute_R --
319 ----------------------------
321 -- The single argument is converted to its root type to call the
322 -- appropriate runtime function, with the actual call being built
323 -- by Expand_Fpt_Attribute
325 procedure Expand_Fpt_Attribute_R
(N
: Node_Id
) is
326 E1
: constant Node_Id
:= First
(Expressions
(N
));
327 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(E1
));
331 (N
, Rtp
, Attribute_Name
(N
),
332 New_List
(Unchecked_Convert_To
(Rtp
, Relocate_Node
(E1
))));
333 end Expand_Fpt_Attribute_R
;
335 -----------------------------
336 -- Expand_Fpt_Attribute_RI --
337 -----------------------------
339 -- The first argument is converted to its root type and the second
340 -- argument is converted to standard long long integer to call the
341 -- appropriate runtime function, with the actual call being built
342 -- by Expand_Fpt_Attribute
344 procedure Expand_Fpt_Attribute_RI
(N
: Node_Id
) is
345 E1
: constant Node_Id
:= First
(Expressions
(N
));
346 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(E1
));
347 E2
: constant Node_Id
:= Next
(E1
);
351 (N
, Rtp
, Attribute_Name
(N
),
353 Unchecked_Convert_To
(Rtp
, Relocate_Node
(E1
)),
354 Unchecked_Convert_To
(Standard_Integer
, Relocate_Node
(E2
))));
355 end Expand_Fpt_Attribute_RI
;
357 -----------------------------
358 -- Expand_Fpt_Attribute_RR --
359 -----------------------------
361 -- The two arguments is converted to their root types to call the
362 -- appropriate runtime function, with the actual call being built
363 -- by Expand_Fpt_Attribute
365 procedure Expand_Fpt_Attribute_RR
(N
: Node_Id
) is
366 E1
: constant Node_Id
:= First
(Expressions
(N
));
367 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(E1
));
368 E2
: constant Node_Id
:= Next
(E1
);
372 (N
, Rtp
, Attribute_Name
(N
),
374 Unchecked_Convert_To
(Rtp
, Relocate_Node
(E1
)),
375 Unchecked_Convert_To
(Rtp
, Relocate_Node
(E2
))));
376 end Expand_Fpt_Attribute_RR
;
378 ----------------------------------
379 -- Expand_N_Attribute_Reference --
380 ----------------------------------
382 procedure Expand_N_Attribute_Reference
(N
: Node_Id
) is
383 Loc
: constant Source_Ptr
:= Sloc
(N
);
384 Typ
: constant Entity_Id
:= Etype
(N
);
385 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
386 Pref
: constant Node_Id
:= Prefix
(N
);
387 Exprs
: constant List_Id
:= Expressions
(N
);
388 Id
: constant Attribute_Id
:= Get_Attribute_Id
(Attribute_Name
(N
));
390 procedure Rewrite_Stream_Proc_Call
(Pname
: Entity_Id
);
391 -- Rewrites a stream attribute for Read, Write or Output with the
392 -- procedure call. Pname is the entity for the procedure to call.
394 ------------------------------
395 -- Rewrite_Stream_Proc_Call --
396 ------------------------------
398 procedure Rewrite_Stream_Proc_Call
(Pname
: Entity_Id
) is
399 Item
: constant Node_Id
:= Next
(First
(Exprs
));
400 Formal
: constant Entity_Id
:= Next_Formal
(First_Formal
(Pname
));
401 Formal_Typ
: constant Entity_Id
:= Etype
(Formal
);
402 Is_Written
: constant Boolean := (Ekind
(Formal
) /= E_In_Parameter
);
405 -- The expansion depends on Item, the second actual, which is
406 -- the object being streamed in or out.
408 -- If the item is a component of a packed array type, and
409 -- a conversion is needed on exit, we introduce a temporary to
410 -- hold the value, because otherwise the packed reference will
411 -- not be properly expanded.
413 if Nkind
(Item
) = N_Indexed_Component
414 and then Is_Packed
(Base_Type
(Etype
(Prefix
(Item
))))
415 and then Base_Type
(Etype
(Item
)) /= Base_Type
(Formal_Typ
)
419 Temp
: constant Entity_Id
:=
420 Make_Defining_Identifier
421 (Loc
, New_Internal_Name
('V'));
427 Make_Object_Declaration
(Loc
,
428 Defining_Identifier
=> Temp
,
430 New_Occurrence_Of
(Formal_Typ
, Loc
));
431 Set_Etype
(Temp
, Formal_Typ
);
434 Make_Assignment_Statement
(Loc
,
435 Name
=> New_Copy_Tree
(Item
),
438 (Etype
(Item
), New_Occurrence_Of
(Temp
, Loc
)));
440 Rewrite
(Item
, New_Occurrence_Of
(Temp
, Loc
));
444 Make_Procedure_Call_Statement
(Loc
,
445 Name
=> New_Occurrence_Of
(Pname
, Loc
),
446 Parameter_Associations
=> Exprs
),
449 Rewrite
(N
, Make_Null_Statement
(Loc
));
454 -- For the class-wide dispatching cases, and for cases in which
455 -- the base type of the second argument matches the base type of
456 -- the corresponding formal parameter (that is to say the stream
457 -- operation is not inherited), we are all set, and can use the
458 -- argument unchanged.
460 -- For all other cases we do an unchecked conversion of the second
461 -- parameter to the type of the formal of the procedure we are
462 -- calling. This deals with the private type cases, and with going
463 -- to the root type as required in elementary type case.
465 if not Is_Class_Wide_Type
(Entity
(Pref
))
466 and then not Is_Class_Wide_Type
(Etype
(Item
))
467 and then Base_Type
(Etype
(Item
)) /= Base_Type
(Formal_Typ
)
470 Unchecked_Convert_To
(Formal_Typ
, Relocate_Node
(Item
)));
472 -- For untagged derived types set Assignment_OK, to prevent
473 -- copies from being created when the unchecked conversion
474 -- is expanded (which would happen in Remove_Side_Effects
475 -- if Expand_N_Unchecked_Conversion were allowed to call
476 -- Force_Evaluation). The copy could violate Ada semantics
477 -- in cases such as an actual that is an out parameter.
478 -- Note that this approach is also used in exp_ch7 for calls
479 -- to controlled type operations to prevent problems with
480 -- actuals wrapped in unchecked conversions.
482 if Is_Untagged_Derivation
(Etype
(Expression
(Item
))) then
483 Set_Assignment_OK
(Item
);
487 -- And now rewrite the call
490 Make_Procedure_Call_Statement
(Loc
,
491 Name
=> New_Occurrence_Of
(Pname
, Loc
),
492 Parameter_Associations
=> Exprs
));
495 end Rewrite_Stream_Proc_Call
;
497 -- Start of processing for Expand_N_Attribute_Reference
500 -- Do required validity checking, if enabled. Do not apply check to
501 -- output parameters of an Asm instruction, since the value of this
502 -- is not set till after the attribute has been elaborated.
504 if Validity_Checks_On
and then Validity_Check_Operands
505 and then Id
/= Attribute_Asm_Output
510 Expr
:= First
(Expressions
(N
));
511 while Present
(Expr
) loop
518 -- Remaining processing depends on specific attribute
526 when Attribute_Access
=>
528 if Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
then
530 -- The value of the attribute_reference is a record containing
531 -- two fields: an access to the protected object, and an access
532 -- to the subprogram itself. The prefix is a selected component.
537 E_T
: constant Entity_Id
:= Equivalent_Type
(Btyp
);
538 Acc
: constant Entity_Id
:=
539 Etype
(Next_Component
(First_Component
(E_T
)));
544 -- Within the body of the protected type, the prefix
545 -- designates a local operation, and the object is the first
546 -- parameter of the corresponding protected body of the
547 -- current enclosing operation.
549 if Is_Entity_Name
(Pref
) then
550 pragma Assert
(In_Open_Scopes
(Scope
(Entity
(Pref
))));
553 (Protected_Body_Subprogram
(Entity
(Pref
)), Loc
);
554 Curr
:= Current_Scope
;
556 while Scope
(Curr
) /= Scope
(Entity
(Pref
)) loop
557 Curr
:= Scope
(Curr
);
561 Make_Attribute_Reference
(Loc
,
565 (Protected_Body_Subprogram
(Curr
)), Loc
),
566 Attribute_Name
=> Name_Address
);
568 -- Case where the prefix is not an entity name. Find the
569 -- version of the protected operation to be called from
570 -- outside the protected object.
576 (Entity
(Selector_Name
(Pref
))), Loc
);
579 Make_Attribute_Reference
(Loc
,
580 Prefix
=> Relocate_Node
(Prefix
(Pref
)),
581 Attribute_Name
=> Name_Address
);
589 Unchecked_Convert_To
(Acc
,
590 Make_Attribute_Reference
(Loc
,
592 Attribute_Name
=> Name_Address
))));
596 Analyze_And_Resolve
(N
, E_T
);
598 -- For subsequent analysis, the node must retain its type.
599 -- The backend will replace it with the equivalent type where
605 elsif Ekind
(Btyp
) = E_General_Access_Type
then
607 Ref_Object
: constant Node_Id
:= Get_Referenced_Object
(Pref
);
608 Parm_Ent
: Entity_Id
;
609 Conversion
: Node_Id
;
612 -- If the prefix of an Access attribute is a dereference of an
613 -- access parameter (or a renaming of such a dereference) and
614 -- the context is a general access type (but not an anonymous
615 -- access type), then rewrite the attribute as a conversion of
616 -- the access parameter to the context access type. This will
617 -- result in an accessibility check being performed, if needed.
619 -- (X.all'Access => Acc_Type (X))
621 if Nkind
(Ref_Object
) = N_Explicit_Dereference
622 and then Is_Entity_Name
(Prefix
(Ref_Object
))
624 Parm_Ent
:= Entity
(Prefix
(Ref_Object
));
626 if Ekind
(Parm_Ent
) in Formal_Kind
627 and then Ekind
(Etype
(Parm_Ent
)) = E_Anonymous_Access_Type
628 and then Present
(Extra_Accessibility
(Parm_Ent
))
631 Convert_To
(Typ
, New_Copy_Tree
(Prefix
(Ref_Object
)));
633 Rewrite
(N
, Conversion
);
634 Analyze_And_Resolve
(N
, Typ
);
639 -- If the prefix is a type name, this is a reference to the current
640 -- instance of the type, within its initialization procedure.
643 Expand_Access_To_Type
(N
);
650 -- Transforms 'Adjacent into a call to the floating-point attribute
651 -- function Adjacent in Fat_xxx (where xxx is the root type)
653 when Attribute_Adjacent
=>
654 Expand_Fpt_Attribute_RR
(N
);
660 when Attribute_Address
=> Address
: declare
661 Task_Proc
: Entity_Id
;
664 -- If the prefix is a task or a task type, the useful address
665 -- is that of the procedure for the task body, i.e. the actual
666 -- program unit. We replace the original entity with that of
669 if Is_Entity_Name
(Pref
)
670 and then Is_Task_Type
(Entity
(Pref
))
672 Task_Proc
:= Next_Entity
(Root_Type
(Etype
(Pref
)));
674 while Present
(Task_Proc
) loop
675 exit when Ekind
(Task_Proc
) = E_Procedure
676 and then Etype
(First_Formal
(Task_Proc
)) =
677 Corresponding_Record_Type
(Etype
(Pref
));
678 Next_Entity
(Task_Proc
);
681 if Present
(Task_Proc
) then
682 Set_Entity
(Pref
, Task_Proc
);
683 Set_Etype
(Pref
, Etype
(Task_Proc
));
686 -- Similarly, the address of a protected operation is the address
687 -- of the corresponding protected body, regardless of the protected
688 -- object from which it is selected.
690 elsif Nkind
(Pref
) = N_Selected_Component
691 and then Is_Subprogram
(Entity
(Selector_Name
(Pref
)))
692 and then Is_Protected_Type
(Scope
(Entity
(Selector_Name
(Pref
))))
696 External_Subprogram
(Entity
(Selector_Name
(Pref
))), Loc
));
698 elsif Nkind
(Pref
) = N_Explicit_Dereference
699 and then Ekind
(Etype
(Pref
)) = E_Subprogram_Type
700 and then Convention
(Etype
(Pref
)) = Convention_Protected
702 -- The prefix is be a dereference of an access_to_protected_
703 -- subprogram. The desired address is the second component of
704 -- the record that represents the access.
707 Addr
: constant Entity_Id
:= Etype
(N
);
708 Ptr
: constant Node_Id
:= Prefix
(Pref
);
709 T
: constant Entity_Id
:=
710 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
714 Unchecked_Convert_To
(Addr
,
715 Make_Selected_Component
(Loc
,
716 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
717 Selector_Name
=> New_Occurrence_Of
(
718 Next_Entity
(First_Entity
(T
)), Loc
))));
720 Analyze_And_Resolve
(N
, Addr
);
724 -- Deal with packed array reference, other cases are handled by gigi
726 if Involves_Packed_Array_Reference
(Pref
) then
727 Expand_Packed_Address_Reference
(N
);
735 when Attribute_Alignment
=> Alignment
: declare
736 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
740 -- For class-wide types, X'Class'Alignment is transformed into a
741 -- direct reference to the Alignment of the class type, so that the
742 -- back end does not have to deal with the X'Class'Alignment
745 if Is_Entity_Name
(Pref
)
746 and then Is_Class_Wide_Type
(Entity
(Pref
))
748 Rewrite
(Prefix
(N
), New_Occurrence_Of
(Entity
(Pref
), Loc
));
751 -- For x'Alignment applied to an object of a class wide type,
752 -- transform X'Alignment into a call to the predefined primitive
753 -- operation _Alignment applied to X.
755 elsif Is_Class_Wide_Type
(Ptyp
) then
757 Make_Function_Call
(Loc
,
758 Name
=> New_Reference_To
759 (Find_Prim_Op
(Ptyp
, Name_uAlignment
), Loc
),
760 Parameter_Associations
=> New_List
(Pref
));
762 if Typ
/= Standard_Integer
then
764 -- The context is a specific integer type with which the
765 -- original attribute was compatible. The function has a
766 -- specific type as well, so to preserve the compatibility
767 -- we must convert explicitly.
769 New_Node
:= Convert_To
(Typ
, New_Node
);
772 Rewrite
(N
, New_Node
);
773 Analyze_And_Resolve
(N
, Typ
);
776 -- For all other cases, we just have to deal with the case of
777 -- the fact that the result can be universal.
780 Apply_Universal_Integer_Attribute_Checks
(N
);
788 when Attribute_AST_Entry
=> AST_Entry
: declare
794 -- The reference to the entry or entry family
797 -- The index expression for an entry family reference, or
798 -- the Empty if Entry_Ref references a simple entry.
801 if Nkind
(Pref
) = N_Indexed_Component
then
802 Entry_Ref
:= Prefix
(Pref
);
803 Index
:= First
(Expressions
(Pref
));
809 -- Get expression for Task_Id and the entry entity
811 if Nkind
(Entry_Ref
) = N_Selected_Component
then
813 Make_Attribute_Reference
(Loc
,
814 Attribute_Name
=> Name_Identity
,
815 Prefix
=> Prefix
(Entry_Ref
));
817 Ttyp
:= Etype
(Prefix
(Entry_Ref
));
818 Eent
:= Entity
(Selector_Name
(Entry_Ref
));
822 Make_Function_Call
(Loc
,
823 Name
=> New_Occurrence_Of
(RTE
(RE_Current_Task
), Loc
));
825 Eent
:= Entity
(Entry_Ref
);
827 -- We have to find the enclosing task to get the task type
828 -- There must be one, since we already validated this earlier
830 Ttyp
:= Current_Scope
;
831 while not Is_Task_Type
(Ttyp
) loop
832 Ttyp
:= Scope
(Ttyp
);
836 -- Now rewrite the attribute with a call to Create_AST_Handler
839 Make_Function_Call
(Loc
,
840 Name
=> New_Occurrence_Of
(RTE
(RE_Create_AST_Handler
), Loc
),
841 Parameter_Associations
=> New_List
(
843 Entry_Index_Expression
(Loc
, Eent
, Index
, Ttyp
))));
845 Analyze_And_Resolve
(N
, RTE
(RE_AST_Handler
));
852 -- We compute this if a component clause was present, otherwise
853 -- we leave the computation up to Gigi, since we don't know what
854 -- layout will be chosen.
856 -- Note that the attribute can apply to a naked record component
857 -- in generated code (i.e. the prefix is an identifier that
858 -- references the component or discriminant entity).
860 when Attribute_Bit_Position
=> Bit_Position
:
865 if Nkind
(Pref
) = N_Identifier
then
868 CE
:= Entity
(Selector_Name
(Pref
));
871 if Known_Static_Component_Bit_Offset
(CE
) then
873 Make_Integer_Literal
(Loc
,
874 Intval
=> Component_Bit_Offset
(CE
)));
875 Analyze_And_Resolve
(N
, Typ
);
878 Apply_Universal_Integer_Attribute_Checks
(N
);
886 -- A reference to P'Body_Version or P'Version is expanded to
889 -- pragma Import (C, Vnn, "uuuuT";
891 -- Get_Version_String (Vnn)
893 -- where uuuu is the unit name (dots replaced by double underscore)
894 -- and T is B for the cases of Body_Version, or Version applied to a
895 -- subprogram acting as its own spec, and S for Version applied to a
896 -- subprogram spec or package. This sequence of code references the
897 -- the unsigned constant created in the main program by the binder.
899 -- A special exception occurs for Standard, where the string
900 -- returned is a copy of the library string in gnatvsn.ads.
902 when Attribute_Body_Version | Attribute_Version
=> Version
: declare
903 E
: constant Entity_Id
:=
904 Make_Defining_Identifier
(Loc
, New_Internal_Name
('V'));
905 Pent
: Entity_Id
:= Entity
(Pref
);
909 -- If not library unit, get to containing library unit
911 while Pent
/= Standard_Standard
912 and then Scope
(Pent
) /= Standard_Standard
914 Pent
:= Scope
(Pent
);
917 -- Special case Standard
919 if Pent
= Standard_Standard
920 or else Pent
= Standard_ASCII
923 Make_String_Literal
(Loc
,
924 Strval
=> Verbose_Library_Version
));
929 -- Build required string constant
931 Get_Name_String
(Get_Unit_Name
(Pent
));
934 for J
in 1 .. Name_Len
- 2 loop
935 if Name_Buffer
(J
) = '.' then
936 Store_String_Chars
("__");
938 Store_String_Char
(Get_Char_Code
(Name_Buffer
(J
)));
942 -- Case of subprogram acting as its own spec, always use body
944 if Nkind
(Declaration_Node
(Pent
)) in N_Subprogram_Specification
945 and then Nkind
(Parent
(Declaration_Node
(Pent
))) =
947 and then Acts_As_Spec
(Parent
(Declaration_Node
(Pent
)))
949 Store_String_Chars
("B");
951 -- Case of no body present, always use spec
953 elsif not Unit_Requires_Body
(Pent
) then
954 Store_String_Chars
("S");
956 -- Otherwise use B for Body_Version, S for spec
958 elsif Id
= Attribute_Body_Version
then
959 Store_String_Chars
("B");
961 Store_String_Chars
("S");
965 Lib
.Version_Referenced
(S
);
967 -- Insert the object declaration
969 Insert_Actions
(N
, New_List
(
970 Make_Object_Declaration
(Loc
,
971 Defining_Identifier
=> E
,
973 New_Occurrence_Of
(RTE
(RE_Unsigned
), Loc
))));
975 -- Set entity as imported with correct external name
978 Set_Interface_Name
(E
, Make_String_Literal
(Loc
, S
));
980 -- And now rewrite original reference
983 Make_Function_Call
(Loc
,
984 Name
=> New_Reference_To
(RTE
(RE_Get_Version_String
), Loc
),
985 Parameter_Associations
=> New_List
(
986 New_Occurrence_Of
(E
, Loc
))));
989 Analyze_And_Resolve
(N
, RTE
(RE_Version_String
));
996 -- Transforms 'Ceiling into a call to the floating-point attribute
997 -- function Ceiling in Fat_xxx (where xxx is the root type)
999 when Attribute_Ceiling
=>
1000 Expand_Fpt_Attribute_R
(N
);
1006 -- Transforms 'Callable attribute into a call to the Callable function.
1008 when Attribute_Callable
=> Callable
:
1011 Build_Call_With_Task
(Pref
, RTE
(RE_Callable
)));
1012 Analyze_And_Resolve
(N
, Standard_Boolean
);
1019 -- Transforms 'Caller attribute into a call to either the
1020 -- Task_Entry_Caller or the Protected_Entry_Caller function.
1022 when Attribute_Caller
=> Caller
: declare
1023 Id_Kind
: constant Entity_Id
:= RTE
(RO_AT_Task_Id
);
1024 Ent
: constant Entity_Id
:= Entity
(Pref
);
1025 Conctype
: constant Entity_Id
:= Scope
(Ent
);
1026 Nest_Depth
: Integer := 0;
1033 if Is_Protected_Type
(Conctype
) then
1035 or else Restriction_Active
(No_Entry_Queue
) = False
1036 or else Number_Entries
(Conctype
) > 1
1040 (RTE
(RE_Protected_Entry_Caller
), Loc
);
1044 (RTE
(RE_Protected_Single_Entry_Caller
), Loc
);
1048 Unchecked_Convert_To
(Id_Kind
,
1049 Make_Function_Call
(Loc
,
1051 Parameter_Associations
=> New_List
1054 (Corresponding_Body
(Parent
(Conctype
))), Loc
)))));
1059 -- Determine the nesting depth of the E'Caller attribute, that
1060 -- is, how many accept statements are nested within the accept
1061 -- statement for E at the point of E'Caller. The runtime uses
1062 -- this depth to find the specified entry call.
1064 for J
in reverse 0 .. Scope_Stack
.Last
loop
1065 S
:= Scope_Stack
.Table
(J
).Entity
;
1067 -- We should not reach the scope of the entry, as it should
1068 -- already have been checked in Sem_Attr that this attribute
1069 -- reference is within a matching accept statement.
1071 pragma Assert
(S
/= Conctype
);
1076 elsif Is_Entry
(S
) then
1077 Nest_Depth
:= Nest_Depth
+ 1;
1082 Unchecked_Convert_To
(Id_Kind
,
1083 Make_Function_Call
(Loc
,
1084 Name
=> New_Reference_To
(
1085 RTE
(RE_Task_Entry_Caller
), Loc
),
1086 Parameter_Associations
=> New_List
(
1087 Make_Integer_Literal
(Loc
,
1088 Intval
=> Int
(Nest_Depth
))))));
1091 Analyze_And_Resolve
(N
, Id_Kind
);
1098 -- Transforms 'Compose into a call to the floating-point attribute
1099 -- function Compose in Fat_xxx (where xxx is the root type)
1101 -- Note: we strictly should have special code here to deal with the
1102 -- case of absurdly negative arguments (less than Integer'First)
1103 -- which will return a (signed) zero value, but it hardly seems
1104 -- worth the effort. Absurdly large positive arguments will raise
1105 -- constraint error which is fine.
1107 when Attribute_Compose
=>
1108 Expand_Fpt_Attribute_RI
(N
);
1114 when Attribute_Constrained
=> Constrained
: declare
1115 Formal_Ent
: constant Entity_Id
:= Param_Entity
(Pref
);
1118 -- Reference to a parameter where the value is passed as an extra
1119 -- actual, corresponding to the extra formal referenced by the
1120 -- Extra_Constrained field of the corresponding formal. If this
1121 -- is an entry in-parameter, it is replaced by a constant renaming
1122 -- for which Extra_Constrained is never created.
1124 if Present
(Formal_Ent
)
1125 and then Ekind
(Formal_Ent
) /= E_Constant
1126 and then Present
(Extra_Constrained
(Formal_Ent
))
1130 (Extra_Constrained
(Formal_Ent
), Sloc
(N
)));
1132 -- For variables with a Extra_Constrained field, we use the
1133 -- corresponding entity.
1135 elsif Nkind
(Pref
) = N_Identifier
1136 and then Ekind
(Entity
(Pref
)) = E_Variable
1137 and then Present
(Extra_Constrained
(Entity
(Pref
)))
1141 (Extra_Constrained
(Entity
(Pref
)), Sloc
(N
)));
1143 -- For all other entity names, we can tell at compile time
1145 elsif Is_Entity_Name
(Pref
) then
1147 Ent
: constant Entity_Id
:= Entity
(Pref
);
1151 -- (RM J.4) obsolescent cases
1153 if Is_Type
(Ent
) then
1157 if Is_Private_Type
(Ent
) then
1158 Res
:= not Has_Discriminants
(Ent
)
1159 or else Is_Constrained
(Ent
);
1161 -- It not a private type, must be a generic actual type
1162 -- that corresponded to a private type. We know that this
1163 -- correspondence holds, since otherwise the reference
1164 -- within the generic template would have been illegal.
1167 if Is_Composite_Type
(Underlying_Type
(Ent
)) then
1168 Res
:= Is_Constrained
(Ent
);
1174 -- If the prefix is not a variable or is aliased, then
1175 -- definitely true; if it's a formal parameter without
1176 -- an associated extra formal, then treat it as constrained.
1178 elsif not Is_Variable
(Pref
)
1179 or else Present
(Formal_Ent
)
1180 or else Is_Aliased_View
(Pref
)
1184 -- Variable case, just look at type to see if it is
1185 -- constrained. Note that the one case where this is
1186 -- not accurate (the procedure formal case), has been
1190 Res
:= Is_Constrained
(Etype
(Ent
));
1194 New_Reference_To
(Boolean_Literals
(Res
), Loc
));
1197 -- Prefix is not an entity name. These are also cases where
1198 -- we can always tell at compile time by looking at the form
1199 -- and type of the prefix.
1205 not Is_Variable
(Pref
)
1206 or else Nkind
(Pref
) = N_Explicit_Dereference
1207 or else Is_Constrained
(Etype
(Pref
))),
1211 Analyze_And_Resolve
(N
, Standard_Boolean
);
1218 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1219 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1221 when Attribute_Copy_Sign
=>
1222 Expand_Fpt_Attribute_RR
(N
);
1228 -- Transforms 'Count attribute into a call to the Count function
1230 when Attribute_Count
=> Count
:
1236 Conctyp
: Entity_Id
;
1239 -- If the prefix is a member of an entry family, retrieve both
1240 -- entry name and index. For a simple entry there is no index.
1242 if Nkind
(Pref
) = N_Indexed_Component
then
1243 Entnam
:= Prefix
(Pref
);
1244 Index
:= First
(Expressions
(Pref
));
1250 -- Find the concurrent type in which this attribute is referenced
1251 -- (there had better be one).
1253 Conctyp
:= Current_Scope
;
1254 while not Is_Concurrent_Type
(Conctyp
) loop
1255 Conctyp
:= Scope
(Conctyp
);
1260 if Is_Protected_Type
(Conctyp
) then
1263 or else Restriction_Active
(No_Entry_Queue
) = False
1264 or else Number_Entries
(Conctyp
) > 1
1266 Name
:= New_Reference_To
(RTE
(RE_Protected_Count
), Loc
);
1269 Make_Function_Call
(Loc
,
1271 Parameter_Associations
=> New_List
(
1274 Corresponding_Body
(Parent
(Conctyp
))), Loc
),
1275 Entry_Index_Expression
(
1276 Loc
, Entity
(Entnam
), Index
, Scope
(Entity
(Entnam
)))));
1278 Name
:= New_Reference_To
(RTE
(RE_Protected_Count_Entry
), Loc
);
1280 Call
:= Make_Function_Call
(Loc
,
1282 Parameter_Associations
=> New_List
(
1285 Corresponding_Body
(Parent
(Conctyp
))), Loc
)));
1292 Make_Function_Call
(Loc
,
1293 Name
=> New_Reference_To
(RTE
(RE_Task_Count
), Loc
),
1294 Parameter_Associations
=> New_List
(
1295 Entry_Index_Expression
1296 (Loc
, Entity
(Entnam
), Index
, Scope
(Entity
(Entnam
)))));
1299 -- The call returns type Natural but the context is universal integer
1300 -- so any integer type is allowed. The attribute was already resolved
1301 -- so its Etype is the required result type. If the base type of the
1302 -- context type is other than Standard.Integer we put in a conversion
1303 -- to the required type. This can be a normal typed conversion since
1304 -- both input and output types of the conversion are integer types
1306 if Base_Type
(Typ
) /= Base_Type
(Standard_Integer
) then
1307 Rewrite
(N
, Convert_To
(Typ
, Call
));
1312 Analyze_And_Resolve
(N
, Typ
);
1319 -- This processing is shared by Elab_Spec
1321 -- What we do is to insert the following declarations
1324 -- pragma Import (C, enn, "name___elabb/s");
1326 -- and then the Elab_Body/Spec attribute is replaced by a reference
1327 -- to this defining identifier.
1329 when Attribute_Elab_Body |
1330 Attribute_Elab_Spec
=>
1333 Ent
: constant Entity_Id
:=
1334 Make_Defining_Identifier
(Loc
,
1335 New_Internal_Name
('E'));
1339 procedure Make_Elab_String
(Nod
: Node_Id
);
1340 -- Given Nod, an identifier, or a selected component, put the
1341 -- image into the current string literal, with double underline
1342 -- between components.
1344 procedure Make_Elab_String
(Nod
: Node_Id
) is
1346 if Nkind
(Nod
) = N_Selected_Component
then
1347 Make_Elab_String
(Prefix
(Nod
));
1349 Store_String_Char
('$');
1351 Store_String_Char
('_');
1352 Store_String_Char
('_');
1355 Get_Name_String
(Chars
(Selector_Name
(Nod
)));
1358 pragma Assert
(Nkind
(Nod
) = N_Identifier
);
1359 Get_Name_String
(Chars
(Nod
));
1362 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
1363 end Make_Elab_String
;
1365 -- Start of processing for Elab_Body/Elab_Spec
1368 -- First we need to prepare the string literal for the name of
1369 -- the elaboration routine to be referenced.
1372 Make_Elab_String
(Pref
);
1375 Store_String_Chars
("._elab");
1376 Lang
:= Make_Identifier
(Loc
, Name_Ada
);
1378 Store_String_Chars
("___elab");
1379 Lang
:= Make_Identifier
(Loc
, Name_C
);
1382 if Id
= Attribute_Elab_Body
then
1383 Store_String_Char
('b');
1385 Store_String_Char
('s');
1390 Insert_Actions
(N
, New_List
(
1391 Make_Subprogram_Declaration
(Loc
,
1393 Make_Procedure_Specification
(Loc
,
1394 Defining_Unit_Name
=> Ent
)),
1397 Chars
=> Name_Import
,
1398 Pragma_Argument_Associations
=> New_List
(
1399 Make_Pragma_Argument_Association
(Loc
,
1400 Expression
=> Lang
),
1402 Make_Pragma_Argument_Association
(Loc
,
1404 Make_Identifier
(Loc
, Chars
(Ent
))),
1406 Make_Pragma_Argument_Association
(Loc
,
1408 Make_String_Literal
(Loc
, Str
))))));
1410 Set_Entity
(N
, Ent
);
1411 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
1418 -- Elaborated is always True for preelaborated units, predefined
1419 -- units, pure units and units which have Elaborate_Body pragmas.
1420 -- These units have no elaboration entity.
1422 -- Note: The Elaborated attribute is never passed through to Gigi
1424 when Attribute_Elaborated
=> Elaborated
: declare
1425 Ent
: constant Entity_Id
:= Entity
(Pref
);
1428 if Present
(Elaboration_Entity
(Ent
)) then
1430 New_Occurrence_Of
(Elaboration_Entity
(Ent
), Loc
));
1432 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
1440 when Attribute_Enum_Rep
=> Enum_Rep
:
1442 -- X'Enum_Rep (Y) expands to
1446 -- This is simply a direct conversion from the enumeration type
1447 -- to the target integer type, which is treated by Gigi as a normal
1448 -- integer conversion, treating the enumeration type as an integer,
1449 -- which is exactly what we want! We set Conversion_OK to make sure
1450 -- that the analyzer does not complain about what otherwise might
1451 -- be an illegal conversion.
1453 if Is_Non_Empty_List
(Exprs
) then
1455 OK_Convert_To
(Typ
, Relocate_Node
(First
(Exprs
))));
1457 -- X'Enum_Rep where X is an enumeration literal is replaced by
1458 -- the literal value.
1460 elsif Ekind
(Entity
(Pref
)) = E_Enumeration_Literal
then
1462 Make_Integer_Literal
(Loc
, Enumeration_Rep
(Entity
(Pref
))));
1464 -- If this is a renaming of a literal, recover the representation
1467 elsif Ekind
(Entity
(Pref
)) = E_Constant
1468 and then Present
(Renamed_Object
(Entity
(Pref
)))
1470 Ekind
(Entity
(Renamed_Object
(Entity
(Pref
))))
1471 = E_Enumeration_Literal
1474 Make_Integer_Literal
(Loc
,
1475 Enumeration_Rep
(Entity
(Renamed_Object
(Entity
(Pref
))))));
1477 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1478 -- of the object value, as described for the type case above.
1482 OK_Convert_To
(Typ
, Relocate_Node
(Pref
)));
1486 Analyze_And_Resolve
(N
, Typ
);
1494 -- Transforms 'Exponent into a call to the floating-point attribute
1495 -- function Exponent in Fat_xxx (where xxx is the root type)
1497 when Attribute_Exponent
=>
1498 Expand_Fpt_Attribute_R
(N
);
1504 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1506 when Attribute_External_Tag
=> External_Tag
:
1509 Make_Function_Call
(Loc
,
1510 Name
=> New_Reference_To
(RTE
(RE_External_Tag
), Loc
),
1511 Parameter_Associations
=> New_List
(
1512 Make_Attribute_Reference
(Loc
,
1513 Attribute_Name
=> Name_Tag
,
1514 Prefix
=> Prefix
(N
)))));
1516 Analyze_And_Resolve
(N
, Standard_String
);
1523 when Attribute_First
=> declare
1524 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1527 -- If the prefix type is a constrained packed array type which
1528 -- already has a Packed_Array_Type representation defined, then
1529 -- replace this attribute with a direct reference to 'First of the
1530 -- appropriate index subtype (since otherwise Gigi will try to give
1531 -- us the value of 'First for this implementation type).
1533 if Is_Constrained_Packed_Array
(Ptyp
) then
1535 Make_Attribute_Reference
(Loc
,
1536 Attribute_Name
=> Name_First
,
1537 Prefix
=> New_Reference_To
(Get_Index_Subtype
(N
), Loc
)));
1538 Analyze_And_Resolve
(N
, Typ
);
1540 elsif Is_Access_Type
(Ptyp
) then
1541 Apply_Access_Check
(N
);
1549 -- We compute this if a component clause was present, otherwise
1550 -- we leave the computation up to Gigi, since we don't know what
1551 -- layout will be chosen.
1553 when Attribute_First_Bit
=> First_Bit
:
1555 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
1558 if Known_Static_Component_Bit_Offset
(CE
) then
1560 Make_Integer_Literal
(Loc
,
1561 Component_Bit_Offset
(CE
) mod System_Storage_Unit
));
1563 Analyze_And_Resolve
(N
, Typ
);
1566 Apply_Universal_Integer_Attribute_Checks
(N
);
1576 -- fixtype'Fixed_Value (integer-value)
1580 -- fixtype(integer-value)
1582 -- we do all the required analysis of the conversion here, because
1583 -- we do not want this to go through the fixed-point conversion
1584 -- circuits. Note that gigi always treats fixed-point as equivalent
1585 -- to the corresponding integer type anyway.
1587 when Attribute_Fixed_Value
=> Fixed_Value
:
1590 Make_Type_Conversion
(Loc
,
1591 Subtype_Mark
=> New_Occurrence_Of
(Entity
(Pref
), Loc
),
1592 Expression
=> Relocate_Node
(First
(Exprs
))));
1593 Set_Etype
(N
, Entity
(Pref
));
1596 -- Note: it might appear that a properly analyzed unchecked conversion
1597 -- would be just fine here, but that's not the case, since the full
1598 -- range checks performed by the following call are critical!
1600 Apply_Type_Conversion_Checks
(N
);
1607 -- Transforms 'Floor into a call to the floating-point attribute
1608 -- function Floor in Fat_xxx (where xxx is the root type)
1610 when Attribute_Floor
=>
1611 Expand_Fpt_Attribute_R
(N
);
1617 -- For the fixed-point type Typ:
1623 -- Result_Type (System.Fore (Long_Long_Float (Type'First)),
1624 -- Long_Long_Float (Type'Last))
1626 -- Note that we know that the type is a non-static subtype, or Fore
1627 -- would have itself been computed dynamically in Eval_Attribute.
1629 when Attribute_Fore
=> Fore
:
1631 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1636 Make_Function_Call
(Loc
,
1637 Name
=> New_Reference_To
(RTE
(RE_Fore
), Loc
),
1639 Parameter_Associations
=> New_List
(
1640 Convert_To
(Standard_Long_Long_Float
,
1641 Make_Attribute_Reference
(Loc
,
1642 Prefix
=> New_Reference_To
(Ptyp
, Loc
),
1643 Attribute_Name
=> Name_First
)),
1645 Convert_To
(Standard_Long_Long_Float
,
1646 Make_Attribute_Reference
(Loc
,
1647 Prefix
=> New_Reference_To
(Ptyp
, Loc
),
1648 Attribute_Name
=> Name_Last
))))));
1650 Analyze_And_Resolve
(N
, Typ
);
1657 -- Transforms 'Fraction into a call to the floating-point attribute
1658 -- function Fraction in Fat_xxx (where xxx is the root type)
1660 when Attribute_Fraction
=>
1661 Expand_Fpt_Attribute_R
(N
);
1667 -- For an exception returns a reference to the exception data:
1668 -- Exception_Id!(Prefix'Reference)
1670 -- For a task it returns a reference to the _task_id component of
1671 -- corresponding record:
1673 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
1675 -- in Ada.Task_Identification.
1677 when Attribute_Identity
=> Identity
: declare
1678 Id_Kind
: Entity_Id
;
1681 if Etype
(Pref
) = Standard_Exception_Type
then
1682 Id_Kind
:= RTE
(RE_Exception_Id
);
1684 if Present
(Renamed_Object
(Entity
(Pref
))) then
1685 Set_Entity
(Pref
, Renamed_Object
(Entity
(Pref
)));
1689 Unchecked_Convert_To
(Id_Kind
, Make_Reference
(Loc
, Pref
)));
1691 Id_Kind
:= RTE
(RO_AT_Task_Id
);
1694 Unchecked_Convert_To
(Id_Kind
, Concurrent_Ref
(Pref
)));
1697 Analyze_And_Resolve
(N
, Id_Kind
);
1704 -- Image attribute is handled in separate unit Exp_Imgv
1706 when Attribute_Image
=>
1707 Exp_Imgv
.Expand_Image_Attribute
(N
);
1713 -- X'Img is expanded to typ'Image (X), where typ is the type of X
1715 when Attribute_Img
=> Img
:
1718 Make_Attribute_Reference
(Loc
,
1719 Prefix
=> New_Reference_To
(Etype
(Pref
), Loc
),
1720 Attribute_Name
=> Name_Image
,
1721 Expressions
=> New_List
(Relocate_Node
(Pref
))));
1723 Analyze_And_Resolve
(N
, Standard_String
);
1730 when Attribute_Input
=> Input
: declare
1731 P_Type
: constant Entity_Id
:= Entity
(Pref
);
1732 B_Type
: constant Entity_Id
:= Base_Type
(P_Type
);
1733 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
1734 Strm
: constant Node_Id
:= First
(Exprs
);
1742 Cntrl
: Node_Id
:= Empty
;
1743 -- Value for controlling argument in call. Always Empty except in
1744 -- the dispatching (class-wide type) case, where it is a reference
1745 -- to the dummy object initialized to the right internal tag.
1747 procedure Freeze_Stream_Subprogram
(F
: Entity_Id
);
1748 -- The expansion of the attribute reference may generate a call to
1749 -- a user-defined stream subprogram that is frozen by the call. This
1750 -- can lead to access-before-elaboration problem if the reference
1751 -- appears in an object declaration and the subprogram body has not
1752 -- been seen. The freezing of the subprogram requires special code
1753 -- because it appears in an expanded context where expressions do
1754 -- not freeze their constituents.
1756 ------------------------------
1757 -- Freeze_Stream_Subprogram --
1758 ------------------------------
1760 procedure Freeze_Stream_Subprogram
(F
: Entity_Id
) is
1761 Decl
: constant Node_Id
:= Unit_Declaration_Node
(F
);
1765 -- If this is user-defined subprogram, the corresponding
1766 -- stream function appears as a renaming-as-body, and the
1767 -- user subprogram must be retrieved by tree traversal.
1770 and then Nkind
(Decl
) = N_Subprogram_Declaration
1771 and then Present
(Corresponding_Body
(Decl
))
1773 Bod
:= Corresponding_Body
(Decl
);
1775 if Nkind
(Unit_Declaration_Node
(Bod
)) =
1776 N_Subprogram_Renaming_Declaration
1778 Set_Is_Frozen
(Entity
(Name
(Unit_Declaration_Node
(Bod
))));
1781 end Freeze_Stream_Subprogram
;
1783 -- Start of processing for Input
1786 -- If no underlying type, we have an error that will be diagnosed
1787 -- elsewhere, so here we just completely ignore the expansion.
1793 -- If there is a TSS for Input, just call it
1795 Fname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Input
);
1797 if Present
(Fname
) then
1801 -- If there is a Stream_Convert pragma, use it, we rewrite
1803 -- sourcetyp'Input (stream)
1807 -- sourcetyp (streamread (strmtyp'Input (stream)));
1809 -- where stmrearead is the given Read function that converts
1810 -- an argument of type strmtyp to type sourcetyp or a type
1811 -- from which it is derived. The extra conversion is required
1812 -- for the derived case.
1814 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
1816 if Present
(Prag
) then
1817 Arg2
:= Next
(First
(Pragma_Argument_Associations
(Prag
)));
1818 Rfunc
:= Entity
(Expression
(Arg2
));
1822 Make_Function_Call
(Loc
,
1823 Name
=> New_Occurrence_Of
(Rfunc
, Loc
),
1824 Parameter_Associations
=> New_List
(
1825 Make_Attribute_Reference
(Loc
,
1828 (Etype
(First_Formal
(Rfunc
)), Loc
),
1829 Attribute_Name
=> Name_Input
,
1830 Expressions
=> Exprs
)))));
1832 Analyze_And_Resolve
(N
, B_Type
);
1837 elsif Is_Elementary_Type
(U_Type
) then
1839 -- A special case arises if we have a defined _Read routine,
1840 -- since in this case we are required to call this routine.
1842 if Present
(TSS
(Base_Type
(U_Type
), TSS_Stream_Read
)) then
1843 Build_Record_Or_Elementary_Input_Function
1844 (Loc
, U_Type
, Decl
, Fname
);
1845 Insert_Action
(N
, Decl
);
1847 -- For normal cases, we call the I_xxx routine directly
1850 Rewrite
(N
, Build_Elementary_Input_Call
(N
));
1851 Analyze_And_Resolve
(N
, P_Type
);
1857 elsif Is_Array_Type
(U_Type
) then
1858 Build_Array_Input_Function
(Loc
, U_Type
, Decl
, Fname
);
1859 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
1861 -- Dispatching case with class-wide type
1863 elsif Is_Class_Wide_Type
(P_Type
) then
1866 Rtyp
: constant Entity_Id
:= Root_Type
(P_Type
);
1871 -- Read the internal tag (RM 13.13.2(34)) and use it to
1872 -- initialize a dummy tag object:
1874 -- Dnn : Ada.Tags.Tag
1875 -- := Internal_Tag (String'Input (Strm));
1877 -- This dummy object is used only to provide a controlling
1878 -- argument for the eventual _Input call.
1881 Make_Defining_Identifier
(Loc
,
1882 Chars
=> New_Internal_Name
('D'));
1885 Make_Object_Declaration
(Loc
,
1886 Defining_Identifier
=> Dnn
,
1887 Object_Definition
=>
1888 New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
1890 Make_Function_Call
(Loc
,
1892 New_Occurrence_Of
(RTE
(RE_Internal_Tag
), Loc
),
1893 Parameter_Associations
=> New_List
(
1894 Make_Attribute_Reference
(Loc
,
1896 New_Occurrence_Of
(Standard_String
, Loc
),
1897 Attribute_Name
=> Name_Input
,
1898 Expressions
=> New_List
(
1900 (Duplicate_Subexpr
(Strm
)))))));
1902 Insert_Action
(N
, Decl
);
1904 -- Now we need to get the entity for the call, and construct
1905 -- a function call node, where we preset a reference to Dnn
1906 -- as the controlling argument (doing an unchecked
1907 -- conversion to the class-wide tagged type to make it
1908 -- look like a real tagged object).
1910 Fname
:= Find_Prim_Op
(Rtyp
, TSS_Stream_Input
);
1911 Cntrl
:= Unchecked_Convert_To
(P_Type
,
1912 New_Occurrence_Of
(Dnn
, Loc
));
1913 Set_Etype
(Cntrl
, P_Type
);
1914 Set_Parent
(Cntrl
, N
);
1917 -- For tagged types, use the primitive Input function
1919 elsif Is_Tagged_Type
(U_Type
) then
1920 Fname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Input
);
1922 -- All other record type cases, including protected records.
1923 -- The latter only arise for expander generated code for
1924 -- handling shared passive partition access.
1928 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
1930 -- Ada 2005 (AI-216): Program_Error is raised when executing
1931 -- the default implementation of the Input attribute of an
1932 -- unchecked union type if the type lacks default discriminant
1935 if Is_Unchecked_Union
(Base_Type
(U_Type
))
1936 and then not Present
(Discriminant_Constraint
(U_Type
))
1939 Make_Raise_Program_Error
(Loc
,
1940 Reason
=> PE_Unchecked_Union_Restriction
));
1945 Build_Record_Or_Elementary_Input_Function
1946 (Loc
, Base_Type
(U_Type
), Decl
, Fname
);
1947 Insert_Action
(N
, Decl
);
1949 if Nkind
(Parent
(N
)) = N_Object_Declaration
1950 and then Is_Record_Type
(U_Type
)
1952 -- The stream function may contain calls to user-defined
1953 -- Read procedures for individual components.
1960 Comp
:= First_Component
(U_Type
);
1961 while Present
(Comp
) loop
1963 Find_Stream_Subprogram
1964 (Etype
(Comp
), TSS_Stream_Read
);
1966 if Present
(Func
) then
1967 Freeze_Stream_Subprogram
(Func
);
1970 Next_Component
(Comp
);
1977 -- If we fall through, Fname is the function to be called. The
1978 -- result is obtained by calling the appropriate function, then
1979 -- converting the result. The conversion does a subtype check.
1982 Make_Function_Call
(Loc
,
1983 Name
=> New_Occurrence_Of
(Fname
, Loc
),
1984 Parameter_Associations
=> New_List
(
1985 Relocate_Node
(Strm
)));
1987 Set_Controlling_Argument
(Call
, Cntrl
);
1988 Rewrite
(N
, Unchecked_Convert_To
(P_Type
, Call
));
1989 Analyze_And_Resolve
(N
, P_Type
);
1991 if Nkind
(Parent
(N
)) = N_Object_Declaration
then
1992 Freeze_Stream_Subprogram
(Fname
);
2002 -- inttype'Fixed_Value (fixed-value)
2006 -- inttype(integer-value))
2008 -- we do all the required analysis of the conversion here, because
2009 -- we do not want this to go through the fixed-point conversion
2010 -- circuits. Note that gigi always treats fixed-point as equivalent
2011 -- to the corresponding integer type anyway.
2013 when Attribute_Integer_Value
=> Integer_Value
:
2016 Make_Type_Conversion
(Loc
,
2017 Subtype_Mark
=> New_Occurrence_Of
(Entity
(Pref
), Loc
),
2018 Expression
=> Relocate_Node
(First
(Exprs
))));
2019 Set_Etype
(N
, Entity
(Pref
));
2022 -- Note: it might appear that a properly analyzed unchecked conversion
2023 -- would be just fine here, but that's not the case, since the full
2024 -- range checks performed by the following call are critical!
2026 Apply_Type_Conversion_Checks
(N
);
2033 when Attribute_Last
=> declare
2034 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2037 -- If the prefix type is a constrained packed array type which
2038 -- already has a Packed_Array_Type representation defined, then
2039 -- replace this attribute with a direct reference to 'Last of the
2040 -- appropriate index subtype (since otherwise Gigi will try to give
2041 -- us the value of 'Last for this implementation type).
2043 if Is_Constrained_Packed_Array
(Ptyp
) then
2045 Make_Attribute_Reference
(Loc
,
2046 Attribute_Name
=> Name_Last
,
2047 Prefix
=> New_Reference_To
(Get_Index_Subtype
(N
), Loc
)));
2048 Analyze_And_Resolve
(N
, Typ
);
2050 elsif Is_Access_Type
(Ptyp
) then
2051 Apply_Access_Check
(N
);
2059 -- We compute this if a component clause was present, otherwise
2060 -- we leave the computation up to Gigi, since we don't know what
2061 -- layout will be chosen.
2063 when Attribute_Last_Bit
=> Last_Bit
:
2065 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
2068 if Known_Static_Component_Bit_Offset
(CE
)
2069 and then Known_Static_Esize
(CE
)
2072 Make_Integer_Literal
(Loc
,
2073 Intval
=> (Component_Bit_Offset
(CE
) mod System_Storage_Unit
)
2076 Analyze_And_Resolve
(N
, Typ
);
2079 Apply_Universal_Integer_Attribute_Checks
(N
);
2087 -- Transforms 'Leading_Part into a call to the floating-point attribute
2088 -- function Leading_Part in Fat_xxx (where xxx is the root type)
2090 -- Note: strictly, we should have special case code to deal with
2091 -- absurdly large positive arguments (greater than Integer'Last),
2092 -- which result in returning the first argument unchanged, but it
2093 -- hardly seems worth the effort. We raise constraint error for
2094 -- absurdly negative arguments which is fine.
2096 when Attribute_Leading_Part
=>
2097 Expand_Fpt_Attribute_RI
(N
);
2103 when Attribute_Length
=> declare
2104 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2109 -- Processing for packed array types
2111 if Is_Array_Type
(Ptyp
) and then Is_Packed
(Ptyp
) then
2112 Ityp
:= Get_Index_Subtype
(N
);
2114 -- If the index type, Ityp, is an enumeration type with
2115 -- holes, then we calculate X'Length explicitly using
2118 -- (0, Ityp'Pos (X'Last (N)) -
2119 -- Ityp'Pos (X'First (N)) + 1);
2121 -- Since the bounds in the template are the representation
2122 -- values and gigi would get the wrong value.
2124 if Is_Enumeration_Type
(Ityp
)
2125 and then Present
(Enum_Pos_To_Rep
(Base_Type
(Ityp
)))
2130 Xnum
:= Expr_Value
(First
(Expressions
(N
)));
2134 Make_Attribute_Reference
(Loc
,
2135 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
2136 Attribute_Name
=> Name_Max
,
2137 Expressions
=> New_List
2138 (Make_Integer_Literal
(Loc
, 0),
2142 Make_Op_Subtract
(Loc
,
2144 Make_Attribute_Reference
(Loc
,
2145 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2146 Attribute_Name
=> Name_Pos
,
2148 Expressions
=> New_List
(
2149 Make_Attribute_Reference
(Loc
,
2150 Prefix
=> Duplicate_Subexpr
(Pref
),
2151 Attribute_Name
=> Name_Last
,
2152 Expressions
=> New_List
(
2153 Make_Integer_Literal
(Loc
, Xnum
))))),
2156 Make_Attribute_Reference
(Loc
,
2157 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2158 Attribute_Name
=> Name_Pos
,
2160 Expressions
=> New_List
(
2161 Make_Attribute_Reference
(Loc
,
2163 Duplicate_Subexpr_No_Checks
(Pref
),
2164 Attribute_Name
=> Name_First
,
2165 Expressions
=> New_List
(
2166 Make_Integer_Literal
(Loc
, Xnum
)))))),
2168 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
2170 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2173 -- If the prefix type is a constrained packed array type which
2174 -- already has a Packed_Array_Type representation defined, then
2175 -- replace this attribute with a direct reference to 'Range_Length
2176 -- of the appropriate index subtype (since otherwise Gigi will try
2177 -- to give us the value of 'Length for this implementation type).
2179 elsif Is_Constrained
(Ptyp
) then
2181 Make_Attribute_Reference
(Loc
,
2182 Attribute_Name
=> Name_Range_Length
,
2183 Prefix
=> New_Reference_To
(Ityp
, Loc
)));
2184 Analyze_And_Resolve
(N
, Typ
);
2187 -- If we have a packed array that is not bit packed, which was
2191 elsif Is_Access_Type
(Ptyp
) then
2192 Apply_Access_Check
(N
);
2194 -- If the designated type is a packed array type, then we
2195 -- convert the reference to:
2198 -- xtyp'Pos (Pref'Last (Expr)) -
2199 -- xtyp'Pos (Pref'First (Expr)));
2201 -- This is a bit complex, but it is the easiest thing to do
2202 -- that works in all cases including enum types with holes
2203 -- xtyp here is the appropriate index type.
2206 Dtyp
: constant Entity_Id
:= Designated_Type
(Ptyp
);
2210 if Is_Array_Type
(Dtyp
) and then Is_Packed
(Dtyp
) then
2211 Xtyp
:= Get_Index_Subtype
(N
);
2214 Make_Attribute_Reference
(Loc
,
2215 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
2216 Attribute_Name
=> Name_Max
,
2217 Expressions
=> New_List
(
2218 Make_Integer_Literal
(Loc
, 0),
2221 Make_Integer_Literal
(Loc
, 1),
2222 Make_Op_Subtract
(Loc
,
2224 Make_Attribute_Reference
(Loc
,
2225 Prefix
=> New_Occurrence_Of
(Xtyp
, Loc
),
2226 Attribute_Name
=> Name_Pos
,
2227 Expressions
=> New_List
(
2228 Make_Attribute_Reference
(Loc
,
2229 Prefix
=> Duplicate_Subexpr
(Pref
),
2230 Attribute_Name
=> Name_Last
,
2232 New_Copy_List
(Exprs
)))),
2235 Make_Attribute_Reference
(Loc
,
2236 Prefix
=> New_Occurrence_Of
(Xtyp
, Loc
),
2237 Attribute_Name
=> Name_Pos
,
2238 Expressions
=> New_List
(
2239 Make_Attribute_Reference
(Loc
,
2241 Duplicate_Subexpr_No_Checks
(Pref
),
2242 Attribute_Name
=> Name_First
,
2244 New_Copy_List
(Exprs
)))))))));
2246 Analyze_And_Resolve
(N
, Typ
);
2250 -- Otherwise leave it to gigi
2253 Apply_Universal_Integer_Attribute_Checks
(N
);
2261 -- Transforms 'Machine into a call to the floating-point attribute
2262 -- function Machine in Fat_xxx (where xxx is the root type)
2264 when Attribute_Machine
=>
2265 Expand_Fpt_Attribute_R
(N
);
2271 -- Machine_Size is equivalent to Object_Size, so transform it into
2272 -- Object_Size and that way Gigi never sees Machine_Size.
2274 when Attribute_Machine_Size
=>
2276 Make_Attribute_Reference
(Loc
,
2277 Prefix
=> Prefix
(N
),
2278 Attribute_Name
=> Name_Object_Size
));
2280 Analyze_And_Resolve
(N
, Typ
);
2286 -- The only case that can get this far is the dynamic case of the
2287 -- old Ada 83 Mantissa attribute for the fixed-point case. For this
2294 -- ityp (System.Mantissa.Mantissa_Value
2295 -- (Integer'Integer_Value (typ'First),
2296 -- Integer'Integer_Value (typ'Last)));
2298 when Attribute_Mantissa
=> Mantissa
: declare
2299 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2304 Make_Function_Call
(Loc
,
2305 Name
=> New_Occurrence_Of
(RTE
(RE_Mantissa_Value
), Loc
),
2307 Parameter_Associations
=> New_List
(
2309 Make_Attribute_Reference
(Loc
,
2310 Prefix
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
2311 Attribute_Name
=> Name_Integer_Value
,
2312 Expressions
=> New_List
(
2314 Make_Attribute_Reference
(Loc
,
2315 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
2316 Attribute_Name
=> Name_First
))),
2318 Make_Attribute_Reference
(Loc
,
2319 Prefix
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
2320 Attribute_Name
=> Name_Integer_Value
,
2321 Expressions
=> New_List
(
2323 Make_Attribute_Reference
(Loc
,
2324 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
2325 Attribute_Name
=> Name_Last
)))))));
2327 Analyze_And_Resolve
(N
, Typ
);
2334 when Attribute_Mod
=> Mod_Case
: declare
2335 Arg
: constant Node_Id
:= Relocate_Node
(First
(Exprs
));
2336 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Arg
));
2337 Modv
: constant Uint
:= Modulus
(Btyp
);
2341 -- This is not so simple. The issue is what type to use for the
2342 -- computation of the modular value.
2344 -- The easy case is when the modulus value is within the bounds
2345 -- of the signed integer type of the argument. In this case we can
2346 -- just do the computation in that signed integer type, and then
2347 -- do an ordinary conversion to the target type.
2349 if Modv
<= Expr_Value
(Hi
) then
2354 Right_Opnd
=> Make_Integer_Literal
(Loc
, Modv
))));
2356 -- Here we know that the modulus is larger than type'Last of the
2357 -- integer type. There are three possible cases to consider:
2359 -- a) The integer value is non-negative. In this case, it is
2360 -- returned as the result (since it is less than the modulus).
2362 -- b) The integer value is negative. In this case, we know that
2363 -- the result is modulus + value, where the value might be as
2364 -- small as -modulus. The trouble is what type do we use to do
2365 -- this subtraction. No type will do, since modulus can be as
2366 -- big as 2**64, and no integer type accomodates this value.
2367 -- Let's do a bit of algebra
2370 -- = modulus - (-value)
2371 -- = (modulus - 1) - (-value - 1)
2373 -- Now modulus - 1 is certainly in range of the modular type.
2374 -- -value is in the range 1 .. modulus, so -value -1 is in the
2375 -- range 0 .. modulus-1 which is in range of the modular type.
2376 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
2377 -- which we can compute using the integer base type.
2381 Make_Conditional_Expression
(Loc
,
2382 Expressions
=> New_List
(
2384 Left_Opnd
=> Duplicate_Subexpr
(Arg
),
2385 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
2388 Duplicate_Subexpr_No_Checks
(Arg
)),
2390 Make_Op_Subtract
(Loc
,
2392 Make_Integer_Literal
(Loc
,
2393 Intval
=> Modv
- 1),
2399 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Arg
),
2401 Make_Integer_Literal
(Loc
,
2402 Intval
=> 1))))))));
2406 Analyze_And_Resolve
(N
, Btyp
);
2413 -- Transforms 'Model into a call to the floating-point attribute
2414 -- function Model in Fat_xxx (where xxx is the root type)
2416 when Attribute_Model
=>
2417 Expand_Fpt_Attribute_R
(N
);
2423 -- The processing for Object_Size shares the processing for Size
2429 when Attribute_Output
=> Output
: declare
2430 P_Type
: constant Entity_Id
:= Entity
(Pref
);
2431 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
2439 -- If no underlying type, we have an error that will be diagnosed
2440 -- elsewhere, so here we just completely ignore the expansion.
2446 -- If TSS for Output is present, just call it
2448 Pname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Output
);
2450 if Present
(Pname
) then
2454 -- If there is a Stream_Convert pragma, use it, we rewrite
2456 -- sourcetyp'Output (stream, Item)
2460 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
2462 -- where strmwrite is the given Write function that converts
2463 -- an argument of type sourcetyp or a type acctyp, from which
2464 -- it is derived to type strmtyp. The conversion to acttyp is
2465 -- required for the derived case.
2467 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
2469 if Present
(Prag
) then
2471 Next
(Next
(First
(Pragma_Argument_Associations
(Prag
))));
2472 Wfunc
:= Entity
(Expression
(Arg3
));
2475 Make_Attribute_Reference
(Loc
,
2476 Prefix
=> New_Occurrence_Of
(Etype
(Wfunc
), Loc
),
2477 Attribute_Name
=> Name_Output
,
2478 Expressions
=> New_List
(
2479 Relocate_Node
(First
(Exprs
)),
2480 Make_Function_Call
(Loc
,
2481 Name
=> New_Occurrence_Of
(Wfunc
, Loc
),
2482 Parameter_Associations
=> New_List
(
2483 Convert_To
(Etype
(First_Formal
(Wfunc
)),
2484 Relocate_Node
(Next
(First
(Exprs
)))))))));
2489 -- For elementary types, we call the W_xxx routine directly.
2490 -- Note that the effect of Write and Output is identical for
2491 -- the case of an elementary type, since there are no
2492 -- discriminants or bounds.
2494 elsif Is_Elementary_Type
(U_Type
) then
2496 -- A special case arises if we have a defined _Write routine,
2497 -- since in this case we are required to call this routine.
2499 if Present
(TSS
(Base_Type
(U_Type
), TSS_Stream_Write
)) then
2500 Build_Record_Or_Elementary_Output_Procedure
2501 (Loc
, U_Type
, Decl
, Pname
);
2502 Insert_Action
(N
, Decl
);
2504 -- For normal cases, we call the W_xxx routine directly
2507 Rewrite
(N
, Build_Elementary_Write_Call
(N
));
2514 elsif Is_Array_Type
(U_Type
) then
2515 Build_Array_Output_Procedure
(Loc
, U_Type
, Decl
, Pname
);
2516 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
2518 -- Class-wide case, first output external tag, then dispatch
2519 -- to the appropriate primitive Output function (RM 13.13.2(31)).
2521 elsif Is_Class_Wide_Type
(P_Type
) then
2523 Strm
: constant Node_Id
:= First
(Exprs
);
2524 Item
: constant Node_Id
:= Next
(Strm
);
2528 -- String'Output (Strm, External_Tag (Item'Tag))
2531 Make_Attribute_Reference
(Loc
,
2532 Prefix
=> New_Occurrence_Of
(Standard_String
, Loc
),
2533 Attribute_Name
=> Name_Output
,
2534 Expressions
=> New_List
(
2535 Relocate_Node
(Duplicate_Subexpr
(Strm
)),
2536 Make_Function_Call
(Loc
,
2538 New_Occurrence_Of
(RTE
(RE_External_Tag
), Loc
),
2539 Parameter_Associations
=> New_List
(
2540 Make_Attribute_Reference
(Loc
,
2543 (Duplicate_Subexpr
(Item
, Name_Req
=> True)),
2544 Attribute_Name
=> Name_Tag
))))));
2547 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Output
);
2549 -- Tagged type case, use the primitive Output function
2551 elsif Is_Tagged_Type
(U_Type
) then
2552 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Output
);
2554 -- All other record type cases, including protected records.
2555 -- The latter only arise for expander generated code for
2556 -- handling shared passive partition access.
2560 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
2562 -- Ada 2005 (AI-216): Program_Error is raised when executing
2563 -- the default implementation of the Output attribute of an
2564 -- unchecked union type if the type lacks default discriminant
2567 if Is_Unchecked_Union
(Base_Type
(U_Type
))
2568 and then not Present
(Discriminant_Constraint
(U_Type
))
2571 Make_Raise_Program_Error
(Loc
,
2572 Reason
=> PE_Unchecked_Union_Restriction
));
2577 Build_Record_Or_Elementary_Output_Procedure
2578 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
2579 Insert_Action
(N
, Decl
);
2583 -- If we fall through, Pname is the name of the procedure to call
2585 Rewrite_Stream_Proc_Call
(Pname
);
2592 -- For enumeration types with a standard representation, Pos is
2595 -- For enumeration types, with a non-standard representation we
2596 -- generate a call to the _Rep_To_Pos function created when the
2597 -- type was frozen. The call has the form
2599 -- _rep_to_pos (expr, flag)
2601 -- The parameter flag is True if range checks are enabled, causing
2602 -- Program_Error to be raised if the expression has an invalid
2603 -- representation, and False if range checks are suppressed.
2605 -- For integer types, Pos is equivalent to a simple integer
2606 -- conversion and we rewrite it as such
2608 when Attribute_Pos
=> Pos
:
2610 Etyp
: Entity_Id
:= Base_Type
(Entity
(Pref
));
2613 -- Deal with zero/non-zero boolean values
2615 if Is_Boolean_Type
(Etyp
) then
2616 Adjust_Condition
(First
(Exprs
));
2617 Etyp
:= Standard_Boolean
;
2618 Set_Prefix
(N
, New_Occurrence_Of
(Standard_Boolean
, Loc
));
2621 -- Case of enumeration type
2623 if Is_Enumeration_Type
(Etyp
) then
2625 -- Non-standard enumeration type (generate call)
2627 if Present
(Enum_Pos_To_Rep
(Etyp
)) then
2628 Append_To
(Exprs
, Rep_To_Pos_Flag
(Etyp
, Loc
));
2631 Make_Function_Call
(Loc
,
2633 New_Reference_To
(TSS
(Etyp
, TSS_Rep_To_Pos
), Loc
),
2634 Parameter_Associations
=> Exprs
)));
2636 Analyze_And_Resolve
(N
, Typ
);
2638 -- Standard enumeration type (do universal integer check)
2641 Apply_Universal_Integer_Attribute_Checks
(N
);
2644 -- Deal with integer types (replace by conversion)
2646 elsif Is_Integer_Type
(Etyp
) then
2647 Rewrite
(N
, Convert_To
(Typ
, First
(Exprs
)));
2648 Analyze_And_Resolve
(N
, Typ
);
2657 -- We compute this if a component clause was present, otherwise
2658 -- we leave the computation up to Gigi, since we don't know what
2659 -- layout will be chosen.
2661 when Attribute_Position
=> Position
:
2663 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
2666 if Present
(Component_Clause
(CE
)) then
2668 Make_Integer_Literal
(Loc
,
2669 Intval
=> Component_Bit_Offset
(CE
) / System_Storage_Unit
));
2670 Analyze_And_Resolve
(N
, Typ
);
2673 Apply_Universal_Integer_Attribute_Checks
(N
);
2681 -- 1. Deal with enumeration types with holes
2682 -- 2. For floating-point, generate call to attribute function
2683 -- 3. For other cases, deal with constraint checking
2685 when Attribute_Pred
=> Pred
:
2687 Ptyp
: constant Entity_Id
:= Base_Type
(Etype
(Pref
));
2690 -- For enumeration types with non-standard representations, we
2691 -- expand typ'Pred (x) into
2693 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
2695 -- If the representation is contiguous, we compute instead
2696 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
2698 if Is_Enumeration_Type
(Ptyp
)
2699 and then Present
(Enum_Pos_To_Rep
(Ptyp
))
2701 if Has_Contiguous_Rep
(Ptyp
) then
2703 Unchecked_Convert_To
(Ptyp
,
2706 Make_Integer_Literal
(Loc
,
2707 Enumeration_Rep
(First_Literal
(Ptyp
))),
2709 Make_Function_Call
(Loc
,
2712 (TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
2714 Parameter_Associations
=>
2716 Unchecked_Convert_To
(Ptyp
,
2717 Make_Op_Subtract
(Loc
,
2719 Unchecked_Convert_To
(Standard_Integer
,
2720 Relocate_Node
(First
(Exprs
))),
2722 Make_Integer_Literal
(Loc
, 1))),
2723 Rep_To_Pos_Flag
(Ptyp
, Loc
))))));
2726 -- Add Boolean parameter True, to request program errror if
2727 -- we have a bad representation on our hands. If checks are
2728 -- suppressed, then add False instead
2730 Append_To
(Exprs
, Rep_To_Pos_Flag
(Ptyp
, Loc
));
2732 Make_Indexed_Component
(Loc
,
2733 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Ptyp
), Loc
),
2734 Expressions
=> New_List
(
2735 Make_Op_Subtract
(Loc
,
2737 Make_Function_Call
(Loc
,
2739 New_Reference_To
(TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
2740 Parameter_Associations
=> Exprs
),
2741 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
2744 Analyze_And_Resolve
(N
, Typ
);
2746 -- For floating-point, we transform 'Pred into a call to the Pred
2747 -- floating-point attribute function in Fat_xxx (xxx is root type)
2749 elsif Is_Floating_Point_Type
(Ptyp
) then
2750 Expand_Fpt_Attribute_R
(N
);
2751 Analyze_And_Resolve
(N
, Typ
);
2753 -- For modular types, nothing to do (no overflow, since wraps)
2755 elsif Is_Modular_Integer_Type
(Ptyp
) then
2758 -- For other types, if range checking is enabled, we must generate
2759 -- a check if overflow checking is enabled.
2761 elsif not Overflow_Checks_Suppressed
(Ptyp
) then
2762 Expand_Pred_Succ
(N
);
2771 when Attribute_Range_Length
=> Range_Length
: declare
2772 P_Type
: constant Entity_Id
:= Etype
(Pref
);
2775 -- The only special processing required is for the case where
2776 -- Range_Length is applied to an enumeration type with holes.
2777 -- In this case we transform
2783 -- X'Pos (X'Last) - X'Pos (X'First) + 1
2785 -- So that the result reflects the proper Pos values instead
2786 -- of the underlying representations.
2788 if Is_Enumeration_Type
(P_Type
)
2789 and then Has_Non_Standard_Rep
(P_Type
)
2794 Make_Op_Subtract
(Loc
,
2796 Make_Attribute_Reference
(Loc
,
2797 Attribute_Name
=> Name_Pos
,
2798 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
),
2799 Expressions
=> New_List
(
2800 Make_Attribute_Reference
(Loc
,
2801 Attribute_Name
=> Name_Last
,
2802 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
)))),
2805 Make_Attribute_Reference
(Loc
,
2806 Attribute_Name
=> Name_Pos
,
2807 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
),
2808 Expressions
=> New_List
(
2809 Make_Attribute_Reference
(Loc
,
2810 Attribute_Name
=> Name_First
,
2811 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
))))),
2814 Make_Integer_Literal
(Loc
, 1)));
2816 Analyze_And_Resolve
(N
, Typ
);
2818 -- For all other cases, attribute is handled by Gigi, but we need
2819 -- to deal with the case of the range check on a universal integer.
2822 Apply_Universal_Integer_Attribute_Checks
(N
);
2831 when Attribute_Read
=> Read
: declare
2832 P_Type
: constant Entity_Id
:= Entity
(Pref
);
2833 B_Type
: constant Entity_Id
:= Base_Type
(P_Type
);
2834 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
2844 -- If no underlying type, we have an error that will be diagnosed
2845 -- elsewhere, so here we just completely ignore the expansion.
2851 -- The simple case, if there is a TSS for Read, just call it
2853 Pname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Read
);
2855 if Present
(Pname
) then
2859 -- If there is a Stream_Convert pragma, use it, we rewrite
2861 -- sourcetyp'Read (stream, Item)
2865 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
2867 -- where strmread is the given Read function that converts
2868 -- an argument of type strmtyp to type sourcetyp or a type
2869 -- from which it is derived. The conversion to sourcetyp
2870 -- is required in the latter case.
2872 -- A special case arises if Item is a type conversion in which
2873 -- case, we have to expand to:
2875 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
2877 -- where Itemx is the expression of the type conversion (i.e.
2878 -- the actual object), and typex is the type of Itemx.
2880 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
2882 if Present
(Prag
) then
2883 Arg2
:= Next
(First
(Pragma_Argument_Associations
(Prag
)));
2884 Rfunc
:= Entity
(Expression
(Arg2
));
2885 Lhs
:= Relocate_Node
(Next
(First
(Exprs
)));
2888 Make_Function_Call
(Loc
,
2889 Name
=> New_Occurrence_Of
(Rfunc
, Loc
),
2890 Parameter_Associations
=> New_List
(
2891 Make_Attribute_Reference
(Loc
,
2894 (Etype
(First_Formal
(Rfunc
)), Loc
),
2895 Attribute_Name
=> Name_Input
,
2896 Expressions
=> New_List
(
2897 Relocate_Node
(First
(Exprs
)))))));
2899 if Nkind
(Lhs
) = N_Type_Conversion
then
2900 Lhs
:= Expression
(Lhs
);
2901 Rhs
:= Convert_To
(Etype
(Lhs
), Rhs
);
2905 Make_Assignment_Statement
(Loc
,
2907 Expression
=> Rhs
));
2908 Set_Assignment_OK
(Lhs
);
2912 -- For elementary types, we call the I_xxx routine using the first
2913 -- parameter and then assign the result into the second parameter.
2914 -- We set Assignment_OK to deal with the conversion case.
2916 elsif Is_Elementary_Type
(U_Type
) then
2922 Lhs
:= Relocate_Node
(Next
(First
(Exprs
)));
2923 Rhs
:= Build_Elementary_Input_Call
(N
);
2925 if Nkind
(Lhs
) = N_Type_Conversion
then
2926 Lhs
:= Expression
(Lhs
);
2927 Rhs
:= Convert_To
(Etype
(Lhs
), Rhs
);
2930 Set_Assignment_OK
(Lhs
);
2933 Make_Assignment_Statement
(Loc
,
2935 Expression
=> Rhs
));
2943 elsif Is_Array_Type
(U_Type
) then
2944 Build_Array_Read_Procedure
(N
, U_Type
, Decl
, Pname
);
2945 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
2947 -- Tagged type case, use the primitive Read function. Note that
2948 -- this will dispatch in the class-wide case which is what we want
2950 elsif Is_Tagged_Type
(U_Type
) then
2951 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Read
);
2953 -- All other record type cases, including protected records.
2954 -- The latter only arise for expander generated code for
2955 -- handling shared passive partition access.
2959 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
2961 -- Ada 2005 (AI-216): Program_Error is raised when executing
2962 -- the default implementation of the Read attribute of an
2963 -- Unchecked_Union type.
2965 if Is_Unchecked_Union
(Base_Type
(U_Type
)) then
2967 Make_Raise_Program_Error
(Loc
,
2968 Reason
=> PE_Unchecked_Union_Restriction
));
2971 if Has_Discriminants
(U_Type
)
2973 (Discriminant_Default_Value
(First_Discriminant
(U_Type
)))
2975 Build_Mutable_Record_Read_Procedure
2976 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
2978 Build_Record_Read_Procedure
2979 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
2982 -- Suppress checks, uninitialized or otherwise invalid
2983 -- data does not cause constraint errors to be raised for
2984 -- a complete record read.
2986 Insert_Action
(N
, Decl
, All_Checks
);
2990 Rewrite_Stream_Proc_Call
(Pname
);
2997 -- Transforms 'Remainder into a call to the floating-point attribute
2998 -- function Remainder in Fat_xxx (where xxx is the root type)
3000 when Attribute_Remainder
=>
3001 Expand_Fpt_Attribute_RR
(N
);
3007 -- The handling of the Round attribute is quite delicate. The
3008 -- processing in Sem_Attr introduced a conversion to universal
3009 -- real, reflecting the semantics of Round, but we do not want
3010 -- anything to do with universal real at runtime, since this
3011 -- corresponds to using floating-point arithmetic.
3013 -- What we have now is that the Etype of the Round attribute
3014 -- correctly indicates the final result type. The operand of
3015 -- the Round is the conversion to universal real, described
3016 -- above, and the operand of this conversion is the actual
3017 -- operand of Round, which may be the special case of a fixed
3018 -- point multiplication or division (Etype = universal fixed)
3020 -- The exapander will expand first the operand of the conversion,
3021 -- then the conversion, and finally the round attribute itself,
3022 -- since we always work inside out. But we cannot simply process
3023 -- naively in this order. In the semantic world where universal
3024 -- fixed and real really exist and have infinite precision, there
3025 -- is no problem, but in the implementation world, where universal
3026 -- real is a floating-point type, we would get the wrong result.
3028 -- So the approach is as follows. First, when expanding a multiply
3029 -- or divide whose type is universal fixed, we do nothing at all,
3030 -- instead deferring the operation till later.
3032 -- The actual processing is done in Expand_N_Type_Conversion which
3033 -- handles the special case of Round by looking at its parent to
3034 -- see if it is a Round attribute, and if it is, handling the
3035 -- conversion (or its fixed multiply/divide child) in an appropriate
3038 -- This means that by the time we get to expanding the Round attribute
3039 -- itself, the Round is nothing more than a type conversion (and will
3040 -- often be a null type conversion), so we just replace it with the
3041 -- appropriate conversion operation.
3043 when Attribute_Round
=>
3045 Convert_To
(Etype
(N
), Relocate_Node
(First
(Exprs
))));
3046 Analyze_And_Resolve
(N
);
3052 -- Transforms 'Rounding into a call to the floating-point attribute
3053 -- function Rounding in Fat_xxx (where xxx is the root type)
3055 when Attribute_Rounding
=>
3056 Expand_Fpt_Attribute_R
(N
);
3062 -- Transforms 'Scaling into a call to the floating-point attribute
3063 -- function Scaling in Fat_xxx (where xxx is the root type)
3065 when Attribute_Scaling
=>
3066 Expand_Fpt_Attribute_RI
(N
);
3072 when Attribute_Size |
3073 Attribute_Object_Size |
3074 Attribute_Value_Size |
3075 Attribute_VADS_Size
=> Size
:
3078 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3083 -- Processing for VADS_Size case. Note that this processing removes
3084 -- all traces of VADS_Size from the tree, and completes all required
3085 -- processing for VADS_Size by translating the attribute reference
3086 -- to an appropriate Size or Object_Size reference.
3088 if Id
= Attribute_VADS_Size
3089 or else (Use_VADS_Size
and then Id
= Attribute_Size
)
3091 -- If the size is specified, then we simply use the specified
3092 -- size. This applies to both types and objects. The size of an
3093 -- object can be specified in the following ways:
3095 -- An explicit size object is given for an object
3096 -- A component size is specified for an indexed component
3097 -- A component clause is specified for a selected component
3098 -- The object is a component of a packed composite object
3100 -- If the size is specified, then VADS_Size of an object
3102 if (Is_Entity_Name
(Pref
)
3103 and then Present
(Size_Clause
(Entity
(Pref
))))
3105 (Nkind
(Pref
) = N_Component_Clause
3106 and then (Present
(Component_Clause
3107 (Entity
(Selector_Name
(Pref
))))
3108 or else Is_Packed
(Etype
(Prefix
(Pref
)))))
3110 (Nkind
(Pref
) = N_Indexed_Component
3111 and then (Component_Size
(Etype
(Prefix
(Pref
))) /= 0
3112 or else Is_Packed
(Etype
(Prefix
(Pref
)))))
3114 Set_Attribute_Name
(N
, Name_Size
);
3116 -- Otherwise if we have an object rather than a type, then the
3117 -- VADS_Size attribute applies to the type of the object, rather
3118 -- than the object itself. This is one of the respects in which
3119 -- VADS_Size differs from Size.
3122 if (not Is_Entity_Name
(Pref
)
3123 or else not Is_Type
(Entity
(Pref
)))
3124 and then (Is_Scalar_Type
(Etype
(Pref
))
3125 or else Is_Constrained
(Etype
(Pref
)))
3127 Rewrite
(Pref
, New_Occurrence_Of
(Etype
(Pref
), Loc
));
3130 -- For a scalar type for which no size was
3131 -- explicitly given, VADS_Size means Object_Size. This is the
3132 -- other respect in which VADS_Size differs from Size.
3134 if Is_Scalar_Type
(Etype
(Pref
))
3135 and then No
(Size_Clause
(Etype
(Pref
)))
3137 Set_Attribute_Name
(N
, Name_Object_Size
);
3139 -- In all other cases, Size and VADS_Size are the sane
3142 Set_Attribute_Name
(N
, Name_Size
);
3147 -- For class-wide types, X'Class'Size is transformed into a
3148 -- direct reference to the Size of the class type, so that gigi
3149 -- does not have to deal with the X'Class'Size reference.
3151 if Is_Entity_Name
(Pref
)
3152 and then Is_Class_Wide_Type
(Entity
(Pref
))
3154 Rewrite
(Prefix
(N
), New_Occurrence_Of
(Entity
(Pref
), Loc
));
3157 -- For x'Size applied to an object of a class-wide type, transform
3158 -- X'Size into a call to the primitive operation _Size applied to X.
3160 elsif Is_Class_Wide_Type
(Ptyp
) then
3162 Make_Function_Call
(Loc
,
3163 Name
=> New_Reference_To
3164 (Find_Prim_Op
(Ptyp
, Name_uSize
), Loc
),
3165 Parameter_Associations
=> New_List
(Pref
));
3167 if Typ
/= Standard_Long_Long_Integer
then
3169 -- The context is a specific integer type with which the
3170 -- original attribute was compatible. The function has a
3171 -- specific type as well, so to preserve the compatibility
3172 -- we must convert explicitly.
3174 New_Node
:= Convert_To
(Typ
, New_Node
);
3177 Rewrite
(N
, New_Node
);
3178 Analyze_And_Resolve
(N
, Typ
);
3181 -- For an array component, we can do Size in the front end
3182 -- if the component_size of the array is set.
3184 elsif Nkind
(Pref
) = N_Indexed_Component
then
3185 Siz
:= Component_Size
(Etype
(Prefix
(Pref
)));
3187 -- For a record component, we can do Size in the front end
3188 -- if there is a component clause, or if the record is packed
3189 -- and the component's size is known at compile time.
3191 elsif Nkind
(Pref
) = N_Selected_Component
then
3193 Rec
: constant Entity_Id
:= Etype
(Prefix
(Pref
));
3194 Comp
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
3197 if Present
(Component_Clause
(Comp
)) then
3198 Siz
:= Esize
(Comp
);
3200 elsif Is_Packed
(Rec
) then
3201 Siz
:= RM_Size
(Ptyp
);
3204 Apply_Universal_Integer_Attribute_Checks
(N
);
3209 -- All other cases are handled by Gigi
3212 Apply_Universal_Integer_Attribute_Checks
(N
);
3214 -- If we have Size applied to a formal parameter, that is a
3215 -- packed array subtype, then apply size to the actual subtype.
3217 if Is_Entity_Name
(Pref
)
3218 and then Is_Formal
(Entity
(Pref
))
3219 and then Is_Array_Type
(Etype
(Pref
))
3220 and then Is_Packed
(Etype
(Pref
))
3223 Make_Attribute_Reference
(Loc
,
3225 New_Occurrence_Of
(Get_Actual_Subtype
(Pref
), Loc
),
3226 Attribute_Name
=> Name_Size
));
3227 Analyze_And_Resolve
(N
, Typ
);
3233 -- Common processing for record and array component case
3236 Rewrite
(N
, Make_Integer_Literal
(Loc
, Siz
));
3238 Analyze_And_Resolve
(N
, Typ
);
3240 -- The result is not a static expression
3242 Set_Is_Static_Expression
(N
, False);
3250 when Attribute_Storage_Pool
=>
3252 Make_Type_Conversion
(Loc
,
3253 Subtype_Mark
=> New_Reference_To
(Etype
(N
), Loc
),
3254 Expression
=> New_Reference_To
(Entity
(N
), Loc
)));
3255 Analyze_And_Resolve
(N
, Typ
);
3261 when Attribute_Storage_Size
=> Storage_Size
:
3263 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3266 -- Access type case, always go to the root type
3268 -- The case of access types results in a value of zero for the case
3269 -- where no storage size attribute clause has been given. If a
3270 -- storage size has been given, then the attribute is converted
3271 -- to a reference to the variable used to hold this value.
3273 if Is_Access_Type
(Ptyp
) then
3274 if Present
(Storage_Size_Variable
(Root_Type
(Ptyp
))) then
3276 Make_Attribute_Reference
(Loc
,
3277 Prefix
=> New_Reference_To
(Typ
, Loc
),
3278 Attribute_Name
=> Name_Max
,
3279 Expressions
=> New_List
(
3280 Make_Integer_Literal
(Loc
, 0),
3283 (Storage_Size_Variable
(Root_Type
(Ptyp
)), Loc
)))));
3285 elsif Present
(Associated_Storage_Pool
(Root_Type
(Ptyp
))) then
3288 Make_Function_Call
(Loc
,
3292 (Etype
(Associated_Storage_Pool
(Root_Type
(Ptyp
))),
3293 Attribute_Name
(N
)),
3296 Parameter_Associations
=> New_List
(New_Reference_To
(
3297 Associated_Storage_Pool
(Root_Type
(Ptyp
)), Loc
)))));
3299 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
3302 Analyze_And_Resolve
(N
, Typ
);
3304 -- The case of a task type (an obsolescent feature) is handled the
3305 -- same way, seems as reasonable as anything, and it is what the
3306 -- ACVC tests (e.g. CD1009K) seem to expect.
3308 -- If there is no Storage_Size variable, then we return the default
3309 -- task stack size, otherwise, expand a Storage_Size attribute as
3312 -- Typ (Adjust_Storage_Size (taskZ))
3314 -- except for the case of a task object which has a Storage_Size
3317 -- Typ (Adjust_Storage_Size (taskV!(name)._Size))
3320 if not Present
(Storage_Size_Variable
(Ptyp
)) then
3323 Make_Function_Call
(Loc
,
3325 New_Occurrence_Of
(RTE
(RE_Default_Stack_Size
), Loc
))));
3328 if not (Is_Entity_Name
(Pref
) and then
3329 Is_Task_Type
(Entity
(Pref
))) and then
3330 Chars
(Last_Entity
(Corresponding_Record_Type
(Ptyp
))) =
3335 Make_Function_Call
(Loc
,
3336 Name
=> New_Occurrence_Of
(
3337 RTE
(RE_Adjust_Storage_Size
), Loc
),
3338 Parameter_Associations
=>
3340 Make_Selected_Component
(Loc
,
3342 Unchecked_Convert_To
(
3343 Corresponding_Record_Type
(Ptyp
),
3344 New_Copy_Tree
(Pref
)),
3346 Make_Identifier
(Loc
, Name_uSize
))))));
3348 -- Task not having Storage_Size pragma
3353 Make_Function_Call
(Loc
,
3354 Name
=> New_Occurrence_Of
(
3355 RTE
(RE_Adjust_Storage_Size
), Loc
),
3356 Parameter_Associations
=>
3359 Storage_Size_Variable
(Ptyp
), Loc
)))));
3362 Analyze_And_Resolve
(N
, Typ
);
3371 when Attribute_Stream_Size
=> Stream_Size
: declare
3372 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3376 -- If we have a Stream_Size clause for this type use it, otherwise
3377 -- the Stream_Size if the size of the type.
3379 if Has_Stream_Size_Clause
(Ptyp
) then
3381 (Static_Integer
(Expression
(Stream_Size_Clause
(Ptyp
))));
3383 Size
:= UI_To_Int
(Esize
(Ptyp
));
3386 Rewrite
(N
, Make_Integer_Literal
(Loc
, Intval
=> Size
));
3387 Analyze_And_Resolve
(N
, Typ
);
3394 -- 1. Deal with enumeration types with holes
3395 -- 2. For floating-point, generate call to attribute function
3396 -- 3. For other cases, deal with constraint checking
3398 when Attribute_Succ
=> Succ
:
3400 Ptyp
: constant Entity_Id
:= Base_Type
(Etype
(Pref
));
3403 -- For enumeration types with non-standard representations, we
3404 -- expand typ'Succ (x) into
3406 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
3408 -- If the representation is contiguous, we compute instead
3409 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
3411 if Is_Enumeration_Type
(Ptyp
)
3412 and then Present
(Enum_Pos_To_Rep
(Ptyp
))
3414 if Has_Contiguous_Rep
(Ptyp
) then
3416 Unchecked_Convert_To
(Ptyp
,
3419 Make_Integer_Literal
(Loc
,
3420 Enumeration_Rep
(First_Literal
(Ptyp
))),
3422 Make_Function_Call
(Loc
,
3425 (TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
3427 Parameter_Associations
=>
3429 Unchecked_Convert_To
(Ptyp
,
3432 Unchecked_Convert_To
(Standard_Integer
,
3433 Relocate_Node
(First
(Exprs
))),
3435 Make_Integer_Literal
(Loc
, 1))),
3436 Rep_To_Pos_Flag
(Ptyp
, Loc
))))));
3438 -- Add Boolean parameter True, to request program errror if
3439 -- we have a bad representation on our hands. Add False if
3440 -- checks are suppressed.
3442 Append_To
(Exprs
, Rep_To_Pos_Flag
(Ptyp
, Loc
));
3444 Make_Indexed_Component
(Loc
,
3445 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Ptyp
), Loc
),
3446 Expressions
=> New_List
(
3449 Make_Function_Call
(Loc
,
3452 (TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
3453 Parameter_Associations
=> Exprs
),
3454 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
3457 Analyze_And_Resolve
(N
, Typ
);
3459 -- For floating-point, we transform 'Succ into a call to the Succ
3460 -- floating-point attribute function in Fat_xxx (xxx is root type)
3462 elsif Is_Floating_Point_Type
(Ptyp
) then
3463 Expand_Fpt_Attribute_R
(N
);
3464 Analyze_And_Resolve
(N
, Typ
);
3466 -- For modular types, nothing to do (no overflow, since wraps)
3468 elsif Is_Modular_Integer_Type
(Ptyp
) then
3471 -- For other types, if range checking is enabled, we must generate
3472 -- a check if overflow checking is enabled.
3474 elsif not Overflow_Checks_Suppressed
(Ptyp
) then
3475 Expand_Pred_Succ
(N
);
3483 -- Transforms X'Tag into a direct reference to the tag of X
3485 when Attribute_Tag
=> Tag
:
3488 Prefix_Is_Type
: Boolean;
3491 if Is_Entity_Name
(Pref
) and then Is_Type
(Entity
(Pref
)) then
3492 Ttyp
:= Entity
(Pref
);
3493 Prefix_Is_Type
:= True;
3495 Ttyp
:= Etype
(Pref
);
3496 Prefix_Is_Type
:= False;
3499 if Is_Class_Wide_Type
(Ttyp
) then
3500 Ttyp
:= Root_Type
(Ttyp
);
3503 Ttyp
:= Underlying_Type
(Ttyp
);
3505 if Prefix_Is_Type
then
3507 -- For JGNAT we leave the type attribute unexpanded because
3508 -- there's not a dispatching table to reference.
3512 Unchecked_Convert_To
(RTE
(RE_Tag
),
3513 New_Reference_To
(Access_Disp_Table
(Ttyp
), Loc
)));
3514 Analyze_And_Resolve
(N
, RTE
(RE_Tag
));
3519 Make_Selected_Component
(Loc
,
3520 Prefix
=> Relocate_Node
(Pref
),
3522 New_Reference_To
(Tag_Component
(Ttyp
), Loc
)));
3523 Analyze_And_Resolve
(N
, RTE
(RE_Tag
));
3531 -- Transforms 'Terminated attribute into a call to Terminated function.
3533 when Attribute_Terminated
=> Terminated
:
3535 if Restricted_Profile
then
3537 Build_Call_With_Task
(Pref
, RTE
(RE_Restricted_Terminated
)));
3541 Build_Call_With_Task
(Pref
, RTE
(RE_Terminated
)));
3544 Analyze_And_Resolve
(N
, Standard_Boolean
);
3551 -- Transforms System'To_Address (X) into unchecked conversion
3552 -- from (integral) type of X to type address.
3554 when Attribute_To_Address
=>
3556 Unchecked_Convert_To
(RTE
(RE_Address
),
3557 Relocate_Node
(First
(Exprs
))));
3558 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
3564 -- Transforms 'Truncation into a call to the floating-point attribute
3565 -- function Truncation in Fat_xxx (where xxx is the root type)
3567 when Attribute_Truncation
=>
3568 Expand_Fpt_Attribute_R
(N
);
3570 -----------------------
3571 -- Unbiased_Rounding --
3572 -----------------------
3574 -- Transforms 'Unbiased_Rounding into a call to the floating-point
3575 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
3578 when Attribute_Unbiased_Rounding
=>
3579 Expand_Fpt_Attribute_R
(N
);
3581 ----------------------
3582 -- Unchecked_Access --
3583 ----------------------
3585 when Attribute_Unchecked_Access
=>
3586 Expand_Access_To_Type
(N
);
3592 when Attribute_UET_Address
=> UET_Address
: declare
3593 Ent
: constant Entity_Id
:=
3594 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
3598 Make_Object_Declaration
(Loc
,
3599 Defining_Identifier
=> Ent
,
3600 Aliased_Present
=> True,
3601 Object_Definition
=>
3602 New_Occurrence_Of
(RTE
(RE_Address
), Loc
)));
3604 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
3605 -- in normal external form.
3607 Get_External_Unit_Name_String
(Get_Unit_Name
(Pref
));
3608 Name_Buffer
(1 + 7 .. Name_Len
+ 7) := Name_Buffer
(1 .. Name_Len
);
3609 Name_Len
:= Name_Len
+ 7;
3610 Name_Buffer
(1 .. 7) := "__gnat_";
3611 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 5) := "__SDP";
3612 Name_Len
:= Name_Len
+ 5;
3614 Set_Is_Imported
(Ent
);
3615 Set_Interface_Name
(Ent
,
3616 Make_String_Literal
(Loc
,
3617 Strval
=> String_From_Name_Buffer
));
3620 Make_Attribute_Reference
(Loc
,
3621 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3622 Attribute_Name
=> Name_Address
));
3624 Analyze_And_Resolve
(N
, Typ
);
3627 -------------------------
3628 -- Unrestricted_Access --
3629 -------------------------
3631 when Attribute_Unrestricted_Access
=>
3632 Expand_Access_To_Type
(N
);
3638 -- The processing for VADS_Size is shared with Size
3644 -- For enumeration types with a standard representation, and for all
3645 -- other types, Val is handled by Gigi. For enumeration types with
3646 -- a non-standard representation we use the _Pos_To_Rep array that
3647 -- was created when the type was frozen.
3649 when Attribute_Val
=> Val
:
3651 Etyp
: constant Entity_Id
:= Base_Type
(Entity
(Pref
));
3654 if Is_Enumeration_Type
(Etyp
)
3655 and then Present
(Enum_Pos_To_Rep
(Etyp
))
3657 if Has_Contiguous_Rep
(Etyp
) then
3659 Rep_Node
: constant Node_Id
:=
3660 Unchecked_Convert_To
(Etyp
,
3663 Make_Integer_Literal
(Loc
,
3664 Enumeration_Rep
(First_Literal
(Etyp
))),
3666 (Convert_To
(Standard_Integer
,
3667 Relocate_Node
(First
(Exprs
))))));
3671 Unchecked_Convert_To
(Etyp
,
3674 Make_Integer_Literal
(Loc
,
3675 Enumeration_Rep
(First_Literal
(Etyp
))),
3677 Make_Function_Call
(Loc
,
3680 (TSS
(Etyp
, TSS_Rep_To_Pos
), Loc
),
3681 Parameter_Associations
=> New_List
(
3683 Rep_To_Pos_Flag
(Etyp
, Loc
))))));
3688 Make_Indexed_Component
(Loc
,
3689 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Etyp
), Loc
),
3690 Expressions
=> New_List
(
3691 Convert_To
(Standard_Integer
,
3692 Relocate_Node
(First
(Exprs
))))));
3695 Analyze_And_Resolve
(N
, Typ
);
3703 -- The code for valid is dependent on the particular types involved.
3704 -- See separate sections below for the generated code in each case.
3706 when Attribute_Valid
=> Valid
:
3708 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3709 Btyp
: Entity_Id
:= Base_Type
(Ptyp
);
3712 Save_Validity_Checks_On
: constant Boolean := Validity_Checks_On
;
3713 -- Save the validity checking mode. We always turn off validity
3714 -- checking during process of 'Valid since this is one place
3715 -- where we do not want the implicit validity checks to intefere
3716 -- with the explicit validity check that the programmer is doing.
3718 function Make_Range_Test
return Node_Id
;
3719 -- Build the code for a range test of the form
3720 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
3722 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
3724 ---------------------
3725 -- Make_Range_Test --
3726 ---------------------
3728 function Make_Range_Test
return Node_Id
is
3735 Unchecked_Convert_To
(Btyp
, Duplicate_Subexpr
(Pref
)),
3738 Unchecked_Convert_To
(Btyp
,
3739 Make_Attribute_Reference
(Loc
,
3740 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3741 Attribute_Name
=> Name_First
))),
3746 Unchecked_Convert_To
(Btyp
,
3747 Duplicate_Subexpr_No_Checks
(Pref
)),
3750 Unchecked_Convert_To
(Btyp
,
3751 Make_Attribute_Reference
(Loc
,
3752 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3753 Attribute_Name
=> Name_Last
))));
3754 end Make_Range_Test
;
3756 -- Start of processing for Attribute_Valid
3759 -- Turn off validity checks. We do not want any implicit validity
3760 -- checks to intefere with the explicit check from the attribute
3762 Validity_Checks_On
:= False;
3764 -- Floating-point case. This case is handled by the Valid attribute
3765 -- code in the floating-point attribute run-time library.
3767 if Is_Floating_Point_Type
(Ptyp
) then
3769 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(Pref
));
3772 -- If the floating-point object might be unaligned, we need
3773 -- to call the special routine Unaligned_Valid, which makes
3774 -- the needed copy, being careful not to load the value into
3775 -- any floating-point register. The argument in this case is
3776 -- obj'Address (see Unchecked_Valid routine in s-fatgen.ads).
3778 if Is_Possibly_Unaligned_Object
(Pref
) then
3779 Set_Attribute_Name
(N
, Name_Unaligned_Valid
);
3780 Expand_Fpt_Attribute
3781 (N
, Rtp
, Name_Unaligned_Valid
,
3783 Make_Attribute_Reference
(Loc
,
3784 Prefix
=> Relocate_Node
(Pref
),
3785 Attribute_Name
=> Name_Address
)));
3787 -- In the normal case where we are sure the object is aligned,
3788 -- we generate a caqll to Valid, and the argument in this case
3789 -- is obj'Unrestricted_Access (after converting obj to the
3790 -- right floating-point type).
3793 Expand_Fpt_Attribute
3794 (N
, Rtp
, Name_Valid
,
3796 Make_Attribute_Reference
(Loc
,
3797 Prefix
=> Unchecked_Convert_To
(Rtp
, Pref
),
3798 Attribute_Name
=> Name_Unrestricted_Access
)));
3801 -- One more task, we still need a range check. Required
3802 -- only if we have a constraint, since the Valid routine
3803 -- catches infinities properly (infinities are never valid).
3805 -- The way we do the range check is simply to create the
3806 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
3808 if not Subtypes_Statically_Match
(Ptyp
, Btyp
) then
3811 Left_Opnd
=> Relocate_Node
(N
),
3814 Left_Opnd
=> Convert_To
(Btyp
, Pref
),
3815 Right_Opnd
=> New_Occurrence_Of
(Ptyp
, Loc
))));
3819 -- Enumeration type with holes
3821 -- For enumeration types with holes, the Pos value constructed by
3822 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
3823 -- second argument of False returns minus one for an invalid value,
3824 -- and the non-negative pos value for a valid value, so the
3825 -- expansion of X'Valid is simply:
3827 -- type(X)'Pos (X) >= 0
3829 -- We can't quite generate it that way because of the requirement
3830 -- for the non-standard second argument of False in the resulting
3831 -- rep_to_pos call, so we have to explicitly create:
3833 -- _rep_to_pos (X, False) >= 0
3835 -- If we have an enumeration subtype, we also check that the
3836 -- value is in range:
3838 -- _rep_to_pos (X, False) >= 0
3840 -- (X >= type(X)'First and then type(X)'Last <= X)
3842 elsif Is_Enumeration_Type
(Ptyp
)
3843 and then Present
(Enum_Pos_To_Rep
(Base_Type
(Ptyp
)))
3848 Make_Function_Call
(Loc
,
3851 (TSS
(Base_Type
(Ptyp
), TSS_Rep_To_Pos
), Loc
),
3852 Parameter_Associations
=> New_List
(
3854 New_Occurrence_Of
(Standard_False
, Loc
))),
3855 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
3859 (Type_Low_Bound
(Ptyp
) /= Type_Low_Bound
(Btyp
)
3861 Type_High_Bound
(Ptyp
) /= Type_High_Bound
(Btyp
))
3863 -- The call to Make_Range_Test will create declarations
3864 -- that need a proper insertion point, but Pref is now
3865 -- attached to a node with no ancestor. Attach to tree
3866 -- even if it is to be rewritten below.
3868 Set_Parent
(Tst
, Parent
(N
));
3872 Left_Opnd
=> Make_Range_Test
,
3878 -- Fortran convention booleans
3880 -- For the very special case of Fortran convention booleans, the
3881 -- value is always valid, since it is an integer with the semantics
3882 -- that non-zero is true, and any value is permissible.
3884 elsif Is_Boolean_Type
(Ptyp
)
3885 and then Convention
(Ptyp
) = Convention_Fortran
3887 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
3889 -- For biased representations, we will be doing an unchecked
3890 -- conversion without unbiasing the result. That means that
3891 -- the range test has to take this into account, and the
3892 -- proper form of the test is:
3894 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
3896 elsif Has_Biased_Representation
(Ptyp
) then
3897 Btyp
:= RTE
(RE_Unsigned_32
);
3901 Unchecked_Convert_To
(Btyp
, Duplicate_Subexpr
(Pref
)),
3903 Unchecked_Convert_To
(Btyp
,
3904 Make_Attribute_Reference
(Loc
,
3905 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3906 Attribute_Name
=> Name_Range_Length
))));
3908 -- For all other scalar types, what we want logically is a
3911 -- X in type(X)'First .. type(X)'Last
3913 -- But that's precisely what won't work because of possible
3914 -- unwanted optimization (and indeed the basic motivation for
3915 -- the Valid attribute is exactly that this test does not work!)
3916 -- What will work is:
3918 -- Btyp!(X) >= Btyp!(type(X)'First)
3920 -- Btyp!(X) <= Btyp!(type(X)'Last)
3922 -- where Btyp is an integer type large enough to cover the full
3923 -- range of possible stored values (i.e. it is chosen on the basis
3924 -- of the size of the type, not the range of the values). We write
3925 -- this as two tests, rather than a range check, so that static
3926 -- evaluation will easily remove either or both of the checks if
3927 -- they can be -statically determined to be true (this happens
3928 -- when the type of X is static and the range extends to the full
3929 -- range of stored values).
3931 -- Unsigned types. Note: it is safe to consider only whether the
3932 -- subtype is unsigned, since we will in that case be doing all
3933 -- unsigned comparisons based on the subtype range. Since we use
3934 -- the actual subtype object size, this is appropriate.
3936 -- For example, if we have
3938 -- subtype x is integer range 1 .. 200;
3939 -- for x'Object_Size use 8;
3941 -- Now the base type is signed, but objects of this type are 8
3942 -- bits unsigned, and doing an unsigned test of the range 1 to
3943 -- 200 is correct, even though a value greater than 127 looks
3944 -- signed to a signed comparison.
3946 elsif Is_Unsigned_Type
(Ptyp
) then
3947 if Esize
(Ptyp
) <= 32 then
3948 Btyp
:= RTE
(RE_Unsigned_32
);
3950 Btyp
:= RTE
(RE_Unsigned_64
);
3953 Rewrite
(N
, Make_Range_Test
);
3958 if Esize
(Ptyp
) <= Esize
(Standard_Integer
) then
3959 Btyp
:= Standard_Integer
;
3961 Btyp
:= Universal_Integer
;
3964 Rewrite
(N
, Make_Range_Test
);
3967 Analyze_And_Resolve
(N
, Standard_Boolean
);
3968 Validity_Checks_On
:= Save_Validity_Checks_On
;
3975 -- Value attribute is handled in separate unti Exp_Imgv
3977 when Attribute_Value
=>
3978 Exp_Imgv
.Expand_Value_Attribute
(N
);
3984 -- The processing for Value_Size shares the processing for Size
3990 -- The processing for Version shares the processing for Body_Version
3996 -- We expand typ'Wide_Image (X) into
3998 -- String_To_Wide_String
3999 -- (typ'Image (X), Wide_Character_Encoding_Method)
4001 -- This works in all cases because String_To_Wide_String converts any
4002 -- wide character escape sequences resulting from the Image call to the
4003 -- proper Wide_Character equivalent
4005 -- not quite right for typ = Wide_Character ???
4007 when Attribute_Wide_Image
=> Wide_Image
:
4010 Make_Function_Call
(Loc
,
4011 Name
=> New_Reference_To
(RTE
(RE_String_To_Wide_String
), Loc
),
4012 Parameter_Associations
=> New_List
(
4013 Make_Attribute_Reference
(Loc
,
4015 Attribute_Name
=> Name_Image
,
4016 Expressions
=> Exprs
),
4018 Make_Integer_Literal
(Loc
,
4019 Intval
=> Int
(Wide_Character_Encoding_Method
)))));
4021 Analyze_And_Resolve
(N
, Standard_Wide_String
);
4024 ---------------------
4025 -- Wide_Wide_Image --
4026 ---------------------
4028 -- We expand typ'Wide_Wide_Image (X) into
4030 -- String_To_Wide_Wide_String
4031 -- (typ'Image (X), Wide_Character_Encoding_Method)
4033 -- This works in all cases because String_To_Wide_Wide_String converts
4034 -- any wide character escape sequences resulting from the Image call to
4035 -- the proper Wide_Character equivalent
4037 -- not quite right for typ = Wide_Wide_Character ???
4039 when Attribute_Wide_Wide_Image
=> Wide_Wide_Image
:
4042 Make_Function_Call
(Loc
,
4043 Name
=> New_Reference_To
4044 (RTE
(RE_String_To_Wide_Wide_String
), Loc
),
4045 Parameter_Associations
=> New_List
(
4046 Make_Attribute_Reference
(Loc
,
4048 Attribute_Name
=> Name_Image
,
4049 Expressions
=> Exprs
),
4051 Make_Integer_Literal
(Loc
,
4052 Intval
=> Int
(Wide_Character_Encoding_Method
)))));
4054 Analyze_And_Resolve
(N
, Standard_Wide_Wide_String
);
4055 end Wide_Wide_Image
;
4061 -- We expand typ'Wide_Value (X) into
4064 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
4066 -- Wide_String_To_String is a runtime function that converts its wide
4067 -- string argument to String, converting any non-translatable characters
4068 -- into appropriate escape sequences. This preserves the required
4069 -- semantics of Wide_Value in all cases, and results in a very simple
4070 -- implementation approach.
4072 -- It's not quite right where typ = Wide_Character, because the encoding
4073 -- method may not cover the whole character type ???
4075 when Attribute_Wide_Value
=> Wide_Value
:
4078 Make_Attribute_Reference
(Loc
,
4080 Attribute_Name
=> Name_Value
,
4082 Expressions
=> New_List
(
4083 Make_Function_Call
(Loc
,
4085 New_Reference_To
(RTE
(RE_Wide_String_To_String
), Loc
),
4087 Parameter_Associations
=> New_List
(
4088 Relocate_Node
(First
(Exprs
)),
4089 Make_Integer_Literal
(Loc
,
4090 Intval
=> Int
(Wide_Character_Encoding_Method
)))))));
4092 Analyze_And_Resolve
(N
, Typ
);
4095 ---------------------
4096 -- Wide_Wide_Value --
4097 ---------------------
4099 -- We expand typ'Wide_Value_Value (X) into
4102 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
4104 -- Wide_Wide_String_To_String is a runtime function that converts its
4105 -- wide string argument to String, converting any non-translatable
4106 -- characters into appropriate escape sequences. This preserves the
4107 -- required semantics of Wide_Wide_Value in all cases, and results in a
4108 -- very simple implementation approach.
4110 -- It's not quite right where typ = Wide_Wide_Character, because the
4111 -- encoding method may not cover the whole character type ???
4113 when Attribute_Wide_Wide_Value
=> Wide_Wide_Value
:
4116 Make_Attribute_Reference
(Loc
,
4118 Attribute_Name
=> Name_Value
,
4120 Expressions
=> New_List
(
4121 Make_Function_Call
(Loc
,
4123 New_Reference_To
(RTE
(RE_Wide_Wide_String_To_String
), Loc
),
4125 Parameter_Associations
=> New_List
(
4126 Relocate_Node
(First
(Exprs
)),
4127 Make_Integer_Literal
(Loc
,
4128 Intval
=> Int
(Wide_Character_Encoding_Method
)))))));
4130 Analyze_And_Resolve
(N
, Typ
);
4131 end Wide_Wide_Value
;
4133 ---------------------
4134 -- Wide_Wide_Width --
4135 ---------------------
4137 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
4139 when Attribute_Wide_Wide_Width
=>
4140 Exp_Imgv
.Expand_Width_Attribute
(N
, Wide_Wide
);
4146 -- Wide_Width attribute is handled in separate unit Exp_Imgv
4148 when Attribute_Wide_Width
=>
4149 Exp_Imgv
.Expand_Width_Attribute
(N
, Wide
);
4155 -- Width attribute is handled in separate unit Exp_Imgv
4157 when Attribute_Width
=>
4158 Exp_Imgv
.Expand_Width_Attribute
(N
, Normal
);
4164 when Attribute_Write
=> Write
: declare
4165 P_Type
: constant Entity_Id
:= Entity
(Pref
);
4166 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
4174 -- If no underlying type, we have an error that will be diagnosed
4175 -- elsewhere, so here we just completely ignore the expansion.
4181 -- The simple case, if there is a TSS for Write, just call it
4183 Pname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Write
);
4185 if Present
(Pname
) then
4189 -- If there is a Stream_Convert pragma, use it, we rewrite
4191 -- sourcetyp'Output (stream, Item)
4195 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
4197 -- where strmwrite is the given Write function that converts
4198 -- an argument of type sourcetyp or a type acctyp, from which
4199 -- it is derived to type strmtyp. The conversion to acttyp is
4200 -- required for the derived case.
4202 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
4204 if Present
(Prag
) then
4206 Next
(Next
(First
(Pragma_Argument_Associations
(Prag
))));
4207 Wfunc
:= Entity
(Expression
(Arg3
));
4210 Make_Attribute_Reference
(Loc
,
4211 Prefix
=> New_Occurrence_Of
(Etype
(Wfunc
), Loc
),
4212 Attribute_Name
=> Name_Output
,
4213 Expressions
=> New_List
(
4214 Relocate_Node
(First
(Exprs
)),
4215 Make_Function_Call
(Loc
,
4216 Name
=> New_Occurrence_Of
(Wfunc
, Loc
),
4217 Parameter_Associations
=> New_List
(
4218 Convert_To
(Etype
(First_Formal
(Wfunc
)),
4219 Relocate_Node
(Next
(First
(Exprs
)))))))));
4224 -- For elementary types, we call the W_xxx routine directly
4226 elsif Is_Elementary_Type
(U_Type
) then
4227 Rewrite
(N
, Build_Elementary_Write_Call
(N
));
4233 elsif Is_Array_Type
(U_Type
) then
4234 Build_Array_Write_Procedure
(N
, U_Type
, Decl
, Pname
);
4235 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
4237 -- Tagged type case, use the primitive Write function. Note that
4238 -- this will dispatch in the class-wide case which is what we want
4240 elsif Is_Tagged_Type
(U_Type
) then
4241 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Write
);
4243 -- All other record type cases, including protected records.
4244 -- The latter only arise for expander generated code for
4245 -- handling shared passive partition access.
4249 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
4251 -- Ada 2005 (AI-216): Program_Error is raised when executing
4252 -- the default implementation of the Write attribute of an
4253 -- Unchecked_Union type.
4255 if Is_Unchecked_Union
(Base_Type
(U_Type
)) then
4257 Make_Raise_Program_Error
(Loc
,
4258 Reason
=> PE_Unchecked_Union_Restriction
));
4261 if Has_Discriminants
(U_Type
)
4263 (Discriminant_Default_Value
(First_Discriminant
(U_Type
)))
4265 Build_Mutable_Record_Write_Procedure
4266 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
4268 Build_Record_Write_Procedure
4269 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
4272 Insert_Action
(N
, Decl
);
4276 -- If we fall through, Pname is the procedure to be called
4278 Rewrite_Stream_Proc_Call
(Pname
);
4281 -- Component_Size is handled by Gigi, unless the component size is
4282 -- known at compile time, which is always true in the packed array
4283 -- case. It is important that the packed array case is handled in
4284 -- the front end (see Eval_Attribute) since Gigi would otherwise
4285 -- get confused by the equivalent packed array type.
4287 when Attribute_Component_Size
=>
4290 -- The following attributes are handled by Gigi (except that static
4291 -- cases have already been evaluated by the semantics, but in any
4292 -- case Gigi should not count on that).
4294 -- In addition Gigi handles the non-floating-point cases of Pred
4295 -- and Succ (including the fixed-point cases, which can just be
4296 -- treated as integer increment/decrement operations)
4298 -- Gigi also handles the non-class-wide cases of Size
4300 when Attribute_Bit_Order |
4301 Attribute_Code_Address |
4302 Attribute_Definite |
4304 Attribute_Mechanism_Code |
4306 Attribute_Null_Parameter |
4307 Attribute_Passed_By_Reference |
4308 Attribute_Pool_Address
=>
4311 -- The following attributes are also handled by Gigi, but return a
4312 -- universal integer result, so may need a conversion for checking
4313 -- that the result is in range.
4315 when Attribute_Aft |
4317 Attribute_Max_Size_In_Storage_Elements
4319 Apply_Universal_Integer_Attribute_Checks
(N
);
4321 -- The following attributes should not appear at this stage, since they
4322 -- have already been handled by the analyzer (and properly rewritten
4323 -- with corresponding values or entities to represent the right values)
4325 when Attribute_Abort_Signal |
4326 Attribute_Address_Size |
4329 Attribute_Default_Bit_Order |
4335 Attribute_Has_Access_Values |
4336 Attribute_Has_Discriminants |
4338 Attribute_Machine_Emax |
4339 Attribute_Machine_Emin |
4340 Attribute_Machine_Mantissa |
4341 Attribute_Machine_Overflows |
4342 Attribute_Machine_Radix |
4343 Attribute_Machine_Rounds |
4344 Attribute_Maximum_Alignment |
4345 Attribute_Model_Emin |
4346 Attribute_Model_Epsilon |
4347 Attribute_Model_Mantissa |
4348 Attribute_Model_Small |
4350 Attribute_Partition_ID |
4352 Attribute_Safe_Emax |
4353 Attribute_Safe_First |
4354 Attribute_Safe_Large |
4355 Attribute_Safe_Last |
4356 Attribute_Safe_Small |
4358 Attribute_Signed_Zeros |
4360 Attribute_Storage_Unit |
4361 Attribute_Target_Name |
4362 Attribute_Type_Class |
4363 Attribute_Unconstrained_Array |
4364 Attribute_Universal_Literal_String |
4365 Attribute_Wchar_T_Size |
4366 Attribute_Word_Size
=>
4368 raise Program_Error
;
4370 -- The Asm_Input and Asm_Output attributes are not expanded at this
4371 -- stage, but will be eliminated in the expansion of the Asm call,
4372 -- see Exp_Intr for details. So Gigi will never see these either.
4374 when Attribute_Asm_Input |
4375 Attribute_Asm_Output
=>
4382 when RE_Not_Available
=>
4384 end Expand_N_Attribute_Reference
;
4386 ----------------------
4387 -- Expand_Pred_Succ --
4388 ----------------------
4390 -- For typ'Pred (exp), we generate the check
4392 -- [constraint_error when exp = typ'Base'First]
4394 -- Similarly, for typ'Succ (exp), we generate the check
4396 -- [constraint_error when exp = typ'Base'Last]
4398 -- These checks are not generated for modular types, since the proper
4399 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
4401 procedure Expand_Pred_Succ
(N
: Node_Id
) is
4402 Loc
: constant Source_Ptr
:= Sloc
(N
);
4406 if Attribute_Name
(N
) = Name_Pred
then
4413 Make_Raise_Constraint_Error
(Loc
,
4417 Duplicate_Subexpr_Move_Checks
(First
(Expressions
(N
))),
4419 Make_Attribute_Reference
(Loc
,
4421 New_Reference_To
(Base_Type
(Etype
(Prefix
(N
))), Loc
),
4422 Attribute_Name
=> Cnam
)),
4423 Reason
=> CE_Overflow_Check_Failed
));
4424 end Expand_Pred_Succ
;
4426 ------------------------
4427 -- Find_Inherited_TSS --
4428 ------------------------
4430 function Find_Inherited_TSS
4432 Nam
: TSS_Name_Type
) return Entity_Id
4434 Btyp
: Entity_Id
:= Typ
;
4439 Btyp
:= Base_Type
(Btyp
);
4440 Proc
:= TSS
(Btyp
, Nam
);
4442 exit when Present
(Proc
)
4443 or else not Is_Derived_Type
(Btyp
);
4445 -- If Typ is a derived type, it may inherit attributes from
4448 Btyp
:= Etype
(Btyp
);
4453 -- If nothing else, use the TSS of the root type
4455 Proc
:= TSS
(Base_Type
(Underlying_Type
(Typ
)), Nam
);
4459 end Find_Inherited_TSS
;
4461 ----------------------------
4462 -- Find_Stream_Subprogram --
4463 ----------------------------
4465 function Find_Stream_Subprogram
4467 Nam
: TSS_Name_Type
) return Entity_Id
is
4469 if Is_Tagged_Type
(Typ
)
4470 and then Is_Derived_Type
(Typ
)
4472 return Find_Prim_Op
(Typ
, Nam
);
4474 return Find_Inherited_TSS
(Typ
, Nam
);
4476 end Find_Stream_Subprogram
;
4478 -----------------------
4479 -- Get_Index_Subtype --
4480 -----------------------
4482 function Get_Index_Subtype
(N
: Node_Id
) return Node_Id
is
4483 P_Type
: Entity_Id
:= Etype
(Prefix
(N
));
4488 if Is_Access_Type
(P_Type
) then
4489 P_Type
:= Designated_Type
(P_Type
);
4492 if No
(Expressions
(N
)) then
4495 J
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
4498 Indx
:= First_Index
(P_Type
);
4504 return Etype
(Indx
);
4505 end Get_Index_Subtype
;
4507 -------------------------------
4508 -- Get_Stream_Convert_Pragma --
4509 -------------------------------
4511 function Get_Stream_Convert_Pragma
(T
: Entity_Id
) return Node_Id
is
4516 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
4517 -- that a stream convert pragma for a tagged type is not inherited from
4518 -- its parent. Probably what is wrong here is that it is basically
4519 -- incorrect to consider a stream convert pragma to be a representation
4520 -- pragma at all ???
4522 N
:= First_Rep_Item
(Implementation_Base_Type
(T
));
4523 while Present
(N
) loop
4524 if Nkind
(N
) = N_Pragma
and then Chars
(N
) = Name_Stream_Convert
then
4526 -- For tagged types this pragma is not inherited, so we
4527 -- must verify that it is defined for the given type and
4531 Entity
(Expression
(First
(Pragma_Argument_Associations
(N
))));
4533 if not Is_Tagged_Type
(T
)
4535 or else (Is_Private_Type
(Typ
) and then T
= Full_View
(Typ
))
4545 end Get_Stream_Convert_Pragma
;
4547 ---------------------------------
4548 -- Is_Constrained_Packed_Array --
4549 ---------------------------------
4551 function Is_Constrained_Packed_Array
(Typ
: Entity_Id
) return Boolean is
4552 Arr
: Entity_Id
:= Typ
;
4555 if Is_Access_Type
(Arr
) then
4556 Arr
:= Designated_Type
(Arr
);
4559 return Is_Array_Type
(Arr
)
4560 and then Is_Constrained
(Arr
)
4561 and then Present
(Packed_Array_Type
(Arr
));
4562 end Is_Constrained_Packed_Array
;