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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Exp_VFpt
; use Exp_VFpt
;
39 with Gnatvsn
; use Gnatvsn
;
40 with Hostparm
; use Hostparm
;
42 with Namet
; use Namet
;
43 with Nmake
; use Nmake
;
44 with Nlists
; use Nlists
;
46 with Restrict
; use Restrict
;
47 with Rident
; use Rident
;
48 with Rtsfind
; use Rtsfind
;
50 with Sem_Ch7
; use Sem_Ch7
;
51 with Sem_Ch8
; use Sem_Ch8
;
52 with Sem_Eval
; use Sem_Eval
;
53 with Sem_Res
; use Sem_Res
;
54 with Sem_Util
; use Sem_Util
;
55 with Sinfo
; use Sinfo
;
56 with Snames
; use Snames
;
57 with Stand
; use Stand
;
58 with Stringt
; use Stringt
;
59 with Tbuild
; use Tbuild
;
60 with Ttypes
; use Ttypes
;
61 with Uintp
; use Uintp
;
62 with Uname
; use Uname
;
63 with Validsw
; use Validsw
;
65 package body Exp_Attr
is
67 -----------------------
68 -- Local Subprograms --
69 -----------------------
71 procedure Compile_Stream_Body_In_Scope
76 -- The body for a stream subprogram may be generated outside of the scope
77 -- of the type. If the type is fully private, it may depend on the full
78 -- view of other types (e.g. indices) that are currently private as well.
79 -- We install the declarations of the package in which the type is declared
80 -- before compiling the body in what is its proper environment. The Check
81 -- parameter indicates if checks are to be suppressed for the stream body.
82 -- We suppress checks for array/record reads, since the rule is that these
83 -- are like assignments, out of range values due to uninitialized storage,
84 -- or other invalid values do NOT cause a Constraint_Error to be raised.
86 procedure Expand_Fpt_Attribute
91 -- This procedure expands a call to a floating-point attribute function.
92 -- N is the attribute reference node, and Args is a list of arguments to
93 -- be passed to the function call. Pkg identifies the package containing
94 -- the appropriate instantiation of System.Fat_Gen. Float arguments in Args
95 -- have already been converted to the floating-point type for which Pkg was
96 -- instantiated. The Nam argument is the relevant attribute processing
97 -- routine to be called. This is the same as the attribute name, except in
98 -- the Unaligned_Valid case.
100 procedure Expand_Fpt_Attribute_R
(N
: Node_Id
);
101 -- This procedure expands a call to a floating-point attribute function
102 -- that takes a single floating-point argument. The function to be called
103 -- is always the same as the attribute name.
105 procedure Expand_Fpt_Attribute_RI
(N
: Node_Id
);
106 -- This procedure expands a call to a floating-point attribute function
107 -- that takes one floating-point argument and one integer argument. The
108 -- function to be called is always the same as the attribute name.
110 procedure Expand_Fpt_Attribute_RR
(N
: Node_Id
);
111 -- This procedure expands a call to a floating-point attribute function
112 -- that takes two floating-point arguments. The function to be called
113 -- is always the same as the attribute name.
115 procedure Expand_Pred_Succ
(N
: Node_Id
);
116 -- Handles expansion of Pred or Succ attributes for case of non-real
117 -- operand with overflow checking required.
119 function Get_Index_Subtype
(N
: Node_Id
) return Entity_Id
;
120 -- Used for Last, Last, and Length, when the prefix is an array type,
121 -- Obtains the corresponding index subtype.
123 procedure Expand_Access_To_Type
(N
: Node_Id
);
124 -- A reference to a type within its own scope is resolved to a reference
125 -- to the current instance of the type in its initialization procedure.
127 procedure Find_Fat_Info
129 Fat_Type
: out Entity_Id
;
130 Fat_Pkg
: out RE_Id
);
131 -- Given a floating-point type T, identifies the package containing the
132 -- attributes for this type (returned in Fat_Pkg), and the corresponding
133 -- type for which this package was instantiated from Fat_Gen. Error if T
134 -- is not a floating-point type.
136 function Find_Stream_Subprogram
138 Nam
: TSS_Name_Type
) return Entity_Id
;
139 -- Returns the stream-oriented subprogram attribute for Typ. For tagged
140 -- types, the corresponding primitive operation is looked up, else the
141 -- appropriate TSS from the type itself, or from its closest ancestor
142 -- defining it, is returned. In both cases, inheritance of representation
143 -- aspects is thus taken into account.
145 function Get_Stream_Convert_Pragma
(T
: Entity_Id
) return Node_Id
;
146 -- Given a type, find a corresponding stream convert pragma that applies to
147 -- the implementation base type of this type (Typ). If found, return the
148 -- pragma node, otherwise return Empty if no pragma is found.
150 function Is_Constrained_Packed_Array
(Typ
: Entity_Id
) return Boolean;
151 -- Utility for array attributes, returns true on packed constrained
152 -- arrays, and on access to same.
154 ----------------------------------
155 -- Compile_Stream_Body_In_Scope --
156 ----------------------------------
158 procedure Compile_Stream_Body_In_Scope
164 Installed
: Boolean := False;
165 Scop
: constant Entity_Id
:= Scope
(Arr
);
166 Curr
: constant Entity_Id
:= Current_Scope
;
170 and then not In_Open_Scopes
(Scop
)
171 and then Ekind
(Scop
) = E_Package
174 Install_Visible_Declarations
(Scop
);
175 Install_Private_Declarations
(Scop
);
178 -- The entities in the package are now visible, but the generated
179 -- stream entity must appear in the current scope (usually an
180 -- enclosing stream function) so that itypes all have their proper
187 Insert_Action
(N
, Decl
);
189 Insert_Action
(N
, Decl
, Suppress
=> All_Checks
);
194 -- Remove extra copy of current scope, and package itself
197 End_Package_Scope
(Scop
);
199 end Compile_Stream_Body_In_Scope
;
201 ---------------------------
202 -- Expand_Access_To_Type --
203 ---------------------------
205 procedure Expand_Access_To_Type
(N
: Node_Id
) is
206 Loc
: constant Source_Ptr
:= Sloc
(N
);
207 Typ
: constant Entity_Id
:= Etype
(N
);
208 Pref
: constant Node_Id
:= Prefix
(N
);
213 if Is_Entity_Name
(Pref
)
214 and then Is_Type
(Entity
(Pref
))
216 -- If the current instance name denotes a task type,
217 -- then the access attribute is rewritten to be the
218 -- name of the "_task" parameter associated with the
219 -- task type's task body procedure. An unchecked
220 -- conversion is applied to ensure a type match in
221 -- cases of expander-generated calls (e.g., init procs).
223 if Is_Task_Type
(Entity
(Pref
)) then
225 First_Entity
(Get_Task_Body_Procedure
(Entity
(Pref
)));
227 while Present
(Formal
) loop
228 exit when Chars
(Formal
) = Name_uTask
;
229 Next_Entity
(Formal
);
232 pragma Assert
(Present
(Formal
));
235 Unchecked_Convert_To
(Typ
, New_Occurrence_Of
(Formal
, Loc
)));
238 -- The expression must appear in a default expression,
239 -- (which in the initialization procedure is the rhs of
240 -- an assignment), and not in a discriminant constraint.
245 while Present
(Par
) loop
246 exit when Nkind
(Par
) = N_Assignment_Statement
;
248 if Nkind
(Par
) = N_Component_Declaration
then
255 if Present
(Par
) then
257 Make_Attribute_Reference
(Loc
,
258 Prefix
=> Make_Identifier
(Loc
, Name_uInit
),
259 Attribute_Name
=> Attribute_Name
(N
)));
261 Analyze_And_Resolve
(N
, Typ
);
265 end Expand_Access_To_Type
;
267 --------------------------
268 -- Expand_Fpt_Attribute --
269 --------------------------
271 procedure Expand_Fpt_Attribute
277 Loc
: constant Source_Ptr
:= Sloc
(N
);
278 Typ
: constant Entity_Id
:= Etype
(N
);
282 -- The function name is the selected component Attr_xxx.yyy where
283 -- Attr_xxx is the package name, and yyy is the argument Nam.
285 -- Note: it would be more usual to have separate RE entries for each
286 -- of the entities in the Fat packages, but first they have identical
287 -- names (so we would have to have lots of renaming declarations to
288 -- meet the normal RE rule of separate names for all runtime entities),
289 -- and second there would be an awful lot of them!
292 Make_Selected_Component
(Loc
,
293 Prefix
=> New_Reference_To
(RTE
(Pkg
), Loc
),
294 Selector_Name
=> Make_Identifier
(Loc
, Nam
));
296 -- The generated call is given the provided set of parameters, and then
297 -- wrapped in a conversion which converts the result to the target type
298 -- We use the base type as the target because a range check may be
302 Unchecked_Convert_To
(Base_Type
(Etype
(N
)),
303 Make_Function_Call
(Loc
,
305 Parameter_Associations
=> Args
)));
307 Analyze_And_Resolve
(N
, Typ
);
308 end Expand_Fpt_Attribute
;
310 ----------------------------
311 -- Expand_Fpt_Attribute_R --
312 ----------------------------
314 -- The single argument is converted to its root type to call the
315 -- appropriate runtime function, with the actual call being built
316 -- by Expand_Fpt_Attribute
318 procedure Expand_Fpt_Attribute_R
(N
: Node_Id
) is
319 E1
: constant Node_Id
:= First
(Expressions
(N
));
323 Find_Fat_Info
(Etype
(E1
), Ftp
, Pkg
);
325 (N
, Pkg
, Attribute_Name
(N
),
326 New_List
(Unchecked_Convert_To
(Ftp
, Relocate_Node
(E1
))));
327 end Expand_Fpt_Attribute_R
;
329 -----------------------------
330 -- Expand_Fpt_Attribute_RI --
331 -----------------------------
333 -- The first argument is converted to its root type and the second
334 -- argument is converted to standard long long integer to call the
335 -- appropriate runtime function, with the actual call being built
336 -- by Expand_Fpt_Attribute
338 procedure Expand_Fpt_Attribute_RI
(N
: Node_Id
) is
339 E1
: constant Node_Id
:= First
(Expressions
(N
));
342 E2
: constant Node_Id
:= Next
(E1
);
344 Find_Fat_Info
(Etype
(E1
), Ftp
, Pkg
);
346 (N
, Pkg
, Attribute_Name
(N
),
348 Unchecked_Convert_To
(Ftp
, Relocate_Node
(E1
)),
349 Unchecked_Convert_To
(Standard_Integer
, Relocate_Node
(E2
))));
350 end Expand_Fpt_Attribute_RI
;
352 -----------------------------
353 -- Expand_Fpt_Attribute_RR --
354 -----------------------------
356 -- The two arguments is converted to their root types to call the
357 -- appropriate runtime function, with the actual call being built
358 -- by Expand_Fpt_Attribute
360 procedure Expand_Fpt_Attribute_RR
(N
: Node_Id
) is
361 E1
: constant Node_Id
:= First
(Expressions
(N
));
364 E2
: constant Node_Id
:= Next
(E1
);
366 Find_Fat_Info
(Etype
(E1
), Ftp
, Pkg
);
368 (N
, Pkg
, Attribute_Name
(N
),
370 Unchecked_Convert_To
(Ftp
, Relocate_Node
(E1
)),
371 Unchecked_Convert_To
(Ftp
, Relocate_Node
(E2
))));
372 end Expand_Fpt_Attribute_RR
;
374 ----------------------------------
375 -- Expand_N_Attribute_Reference --
376 ----------------------------------
378 procedure Expand_N_Attribute_Reference
(N
: Node_Id
) is
379 Loc
: constant Source_Ptr
:= Sloc
(N
);
380 Typ
: constant Entity_Id
:= Etype
(N
);
381 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
382 Pref
: constant Node_Id
:= Prefix
(N
);
383 Exprs
: constant List_Id
:= Expressions
(N
);
384 Id
: constant Attribute_Id
:= Get_Attribute_Id
(Attribute_Name
(N
));
386 procedure Rewrite_Stream_Proc_Call
(Pname
: Entity_Id
);
387 -- Rewrites a stream attribute for Read, Write or Output with the
388 -- procedure call. Pname is the entity for the procedure to call.
390 ------------------------------
391 -- Rewrite_Stream_Proc_Call --
392 ------------------------------
394 procedure Rewrite_Stream_Proc_Call
(Pname
: Entity_Id
) is
395 Item
: constant Node_Id
:= Next
(First
(Exprs
));
396 Formal
: constant Entity_Id
:= Next_Formal
(First_Formal
(Pname
));
397 Formal_Typ
: constant Entity_Id
:= Etype
(Formal
);
398 Is_Written
: constant Boolean := (Ekind
(Formal
) /= E_In_Parameter
);
401 -- The expansion depends on Item, the second actual, which is
402 -- the object being streamed in or out.
404 -- If the item is a component of a packed array type, and
405 -- a conversion is needed on exit, we introduce a temporary to
406 -- hold the value, because otherwise the packed reference will
407 -- not be properly expanded.
409 if Nkind
(Item
) = N_Indexed_Component
410 and then Is_Packed
(Base_Type
(Etype
(Prefix
(Item
))))
411 and then Base_Type
(Etype
(Item
)) /= Base_Type
(Formal_Typ
)
415 Temp
: constant Entity_Id
:=
416 Make_Defining_Identifier
417 (Loc
, New_Internal_Name
('V'));
423 Make_Object_Declaration
(Loc
,
424 Defining_Identifier
=> Temp
,
426 New_Occurrence_Of
(Formal_Typ
, Loc
));
427 Set_Etype
(Temp
, Formal_Typ
);
430 Make_Assignment_Statement
(Loc
,
431 Name
=> New_Copy_Tree
(Item
),
434 (Etype
(Item
), New_Occurrence_Of
(Temp
, Loc
)));
436 Rewrite
(Item
, New_Occurrence_Of
(Temp
, Loc
));
440 Make_Procedure_Call_Statement
(Loc
,
441 Name
=> New_Occurrence_Of
(Pname
, Loc
),
442 Parameter_Associations
=> Exprs
),
445 Rewrite
(N
, Make_Null_Statement
(Loc
));
450 -- For the class-wide dispatching cases, and for cases in which
451 -- the base type of the second argument matches the base type of
452 -- the corresponding formal parameter (that is to say the stream
453 -- operation is not inherited), we are all set, and can use the
454 -- argument unchanged.
456 -- For all other cases we do an unchecked conversion of the second
457 -- parameter to the type of the formal of the procedure we are
458 -- calling. This deals with the private type cases, and with going
459 -- to the root type as required in elementary type case.
461 if not Is_Class_Wide_Type
(Entity
(Pref
))
462 and then not Is_Class_Wide_Type
(Etype
(Item
))
463 and then Base_Type
(Etype
(Item
)) /= Base_Type
(Formal_Typ
)
466 Unchecked_Convert_To
(Formal_Typ
, Relocate_Node
(Item
)));
468 -- For untagged derived types set Assignment_OK, to prevent
469 -- copies from being created when the unchecked conversion
470 -- is expanded (which would happen in Remove_Side_Effects
471 -- if Expand_N_Unchecked_Conversion were allowed to call
472 -- Force_Evaluation). The copy could violate Ada semantics
473 -- in cases such as an actual that is an out parameter.
474 -- Note that this approach is also used in exp_ch7 for calls
475 -- to controlled type operations to prevent problems with
476 -- actuals wrapped in unchecked conversions.
478 if Is_Untagged_Derivation
(Etype
(Expression
(Item
))) then
479 Set_Assignment_OK
(Item
);
483 -- And now rewrite the call
486 Make_Procedure_Call_Statement
(Loc
,
487 Name
=> New_Occurrence_Of
(Pname
, Loc
),
488 Parameter_Associations
=> Exprs
));
491 end Rewrite_Stream_Proc_Call
;
493 -- Start of processing for Expand_N_Attribute_Reference
496 -- Do required validity checking, if enabled. Do not apply check to
497 -- output parameters of an Asm instruction, since the value of this
498 -- is not set till after the attribute has been elaborated.
500 if Validity_Checks_On
and then Validity_Check_Operands
501 and then Id
/= Attribute_Asm_Output
506 Expr
:= First
(Expressions
(N
));
507 while Present
(Expr
) loop
514 -- Remaining processing depends on specific attribute
522 when Attribute_Access
=>
524 if Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
then
526 -- The value of the attribute_reference is a record containing
527 -- two fields: an access to the protected object, and an access
528 -- to the subprogram itself. The prefix is a selected component.
533 E_T
: constant Entity_Id
:= Equivalent_Type
(Btyp
);
534 Acc
: constant Entity_Id
:=
535 Etype
(Next_Component
(First_Component
(E_T
)));
540 -- Within the body of the protected type, the prefix
541 -- designates a local operation, and the object is the first
542 -- parameter of the corresponding protected body of the
543 -- current enclosing operation.
545 if Is_Entity_Name
(Pref
) then
546 pragma Assert
(In_Open_Scopes
(Scope
(Entity
(Pref
))));
549 (Protected_Body_Subprogram
(Entity
(Pref
)), Loc
);
550 Curr
:= Current_Scope
;
552 while Scope
(Curr
) /= Scope
(Entity
(Pref
)) loop
553 Curr
:= Scope
(Curr
);
557 Make_Attribute_Reference
(Loc
,
561 (Protected_Body_Subprogram
(Curr
)), Loc
),
562 Attribute_Name
=> Name_Address
);
564 -- Case where the prefix is not an entity name. Find the
565 -- version of the protected operation to be called from
566 -- outside the protected object.
572 (Entity
(Selector_Name
(Pref
))), Loc
);
575 Make_Attribute_Reference
(Loc
,
576 Prefix
=> Relocate_Node
(Prefix
(Pref
)),
577 Attribute_Name
=> Name_Address
);
585 Unchecked_Convert_To
(Acc
,
586 Make_Attribute_Reference
(Loc
,
588 Attribute_Name
=> Name_Address
))));
592 Analyze_And_Resolve
(N
, E_T
);
594 -- For subsequent analysis, the node must retain its type.
595 -- The backend will replace it with the equivalent type where
601 elsif Ekind
(Btyp
) = E_General_Access_Type
then
603 Ref_Object
: constant Node_Id
:= Get_Referenced_Object
(Pref
);
604 Parm_Ent
: Entity_Id
;
605 Conversion
: Node_Id
;
608 -- If the prefix of an Access attribute is a dereference of an
609 -- access parameter (or a renaming of such a dereference) and
610 -- the context is a general access type (but not an anonymous
611 -- access type), then rewrite the attribute as a conversion of
612 -- the access parameter to the context access type. This will
613 -- result in an accessibility check being performed, if needed.
615 -- (X.all'Access => Acc_Type (X))
617 if Nkind
(Ref_Object
) = N_Explicit_Dereference
618 and then Is_Entity_Name
(Prefix
(Ref_Object
))
620 Parm_Ent
:= Entity
(Prefix
(Ref_Object
));
622 if Ekind
(Parm_Ent
) in Formal_Kind
623 and then Ekind
(Etype
(Parm_Ent
)) = E_Anonymous_Access_Type
624 and then Present
(Extra_Accessibility
(Parm_Ent
))
627 Convert_To
(Typ
, New_Copy_Tree
(Prefix
(Ref_Object
)));
629 Rewrite
(N
, Conversion
);
630 Analyze_And_Resolve
(N
, Typ
);
633 -- Ada 2005 (AI-251): If the designated type is an interface,
634 -- then rewrite the referenced object as a conversion to force
635 -- the displacement of the pointer to the secondary dispatch
638 elsif Is_Interface
(Directly_Designated_Type
(Btyp
)) then
639 Conversion
:= Convert_To
(Typ
, New_Copy_Tree
(Ref_Object
));
640 Rewrite
(N
, Conversion
);
641 Analyze_And_Resolve
(N
, Typ
);
645 -- If the prefix is a type name, this is a reference to the current
646 -- instance of the type, within its initialization procedure.
649 Expand_Access_To_Type
(N
);
656 -- Transforms 'Adjacent into a call to the floating-point attribute
657 -- function Adjacent in Fat_xxx (where xxx is the root type)
659 when Attribute_Adjacent
=>
660 Expand_Fpt_Attribute_RR
(N
);
666 when Attribute_Address
=> Address
: declare
667 Task_Proc
: Entity_Id
;
670 -- If the prefix is a task or a task type, the useful address
671 -- is that of the procedure for the task body, i.e. the actual
672 -- program unit. We replace the original entity with that of
675 if Is_Entity_Name
(Pref
)
676 and then Is_Task_Type
(Entity
(Pref
))
678 Task_Proc
:= Next_Entity
(Root_Type
(Etype
(Pref
)));
680 while Present
(Task_Proc
) loop
681 exit when Ekind
(Task_Proc
) = E_Procedure
682 and then Etype
(First_Formal
(Task_Proc
)) =
683 Corresponding_Record_Type
(Etype
(Pref
));
684 Next_Entity
(Task_Proc
);
687 if Present
(Task_Proc
) then
688 Set_Entity
(Pref
, Task_Proc
);
689 Set_Etype
(Pref
, Etype
(Task_Proc
));
692 -- Similarly, the address of a protected operation is the address
693 -- of the corresponding protected body, regardless of the protected
694 -- object from which it is selected.
696 elsif Nkind
(Pref
) = N_Selected_Component
697 and then Is_Subprogram
(Entity
(Selector_Name
(Pref
)))
698 and then Is_Protected_Type
(Scope
(Entity
(Selector_Name
(Pref
))))
702 External_Subprogram
(Entity
(Selector_Name
(Pref
))), Loc
));
704 elsif Nkind
(Pref
) = N_Explicit_Dereference
705 and then Ekind
(Etype
(Pref
)) = E_Subprogram_Type
706 and then Convention
(Etype
(Pref
)) = Convention_Protected
708 -- The prefix is be a dereference of an access_to_protected_
709 -- subprogram. The desired address is the second component of
710 -- the record that represents the access.
713 Addr
: constant Entity_Id
:= Etype
(N
);
714 Ptr
: constant Node_Id
:= Prefix
(Pref
);
715 T
: constant Entity_Id
:=
716 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
720 Unchecked_Convert_To
(Addr
,
721 Make_Selected_Component
(Loc
,
722 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
723 Selector_Name
=> New_Occurrence_Of
(
724 Next_Entity
(First_Entity
(T
)), Loc
))));
726 Analyze_And_Resolve
(N
, Addr
);
730 -- Deal with packed array reference, other cases are handled by gigi
732 if Involves_Packed_Array_Reference
(Pref
) then
733 Expand_Packed_Address_Reference
(N
);
741 when Attribute_Alignment
=> Alignment
: declare
742 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
746 -- For class-wide types, X'Class'Alignment is transformed into a
747 -- direct reference to the Alignment of the class type, so that the
748 -- back end does not have to deal with the X'Class'Alignment
751 if Is_Entity_Name
(Pref
)
752 and then Is_Class_Wide_Type
(Entity
(Pref
))
754 Rewrite
(Prefix
(N
), New_Occurrence_Of
(Entity
(Pref
), Loc
));
757 -- For x'Alignment applied to an object of a class wide type,
758 -- transform X'Alignment into a call to the predefined primitive
759 -- operation _Alignment applied to X.
761 elsif Is_Class_Wide_Type
(Ptyp
) then
763 Make_Function_Call
(Loc
,
764 Name
=> New_Reference_To
765 (Find_Prim_Op
(Ptyp
, Name_uAlignment
), Loc
),
766 Parameter_Associations
=> New_List
(Pref
));
768 if Typ
/= Standard_Integer
then
770 -- The context is a specific integer type with which the
771 -- original attribute was compatible. The function has a
772 -- specific type as well, so to preserve the compatibility
773 -- we must convert explicitly.
775 New_Node
:= Convert_To
(Typ
, New_Node
);
778 Rewrite
(N
, New_Node
);
779 Analyze_And_Resolve
(N
, Typ
);
782 -- For all other cases, we just have to deal with the case of
783 -- the fact that the result can be universal.
786 Apply_Universal_Integer_Attribute_Checks
(N
);
794 when Attribute_AST_Entry
=> AST_Entry
: declare
800 -- The reference to the entry or entry family
803 -- The index expression for an entry family reference, or
804 -- the Empty if Entry_Ref references a simple entry.
807 if Nkind
(Pref
) = N_Indexed_Component
then
808 Entry_Ref
:= Prefix
(Pref
);
809 Index
:= First
(Expressions
(Pref
));
815 -- Get expression for Task_Id and the entry entity
817 if Nkind
(Entry_Ref
) = N_Selected_Component
then
819 Make_Attribute_Reference
(Loc
,
820 Attribute_Name
=> Name_Identity
,
821 Prefix
=> Prefix
(Entry_Ref
));
823 Ttyp
:= Etype
(Prefix
(Entry_Ref
));
824 Eent
:= Entity
(Selector_Name
(Entry_Ref
));
828 Make_Function_Call
(Loc
,
829 Name
=> New_Occurrence_Of
(RTE
(RE_Current_Task
), Loc
));
831 Eent
:= Entity
(Entry_Ref
);
833 -- We have to find the enclosing task to get the task type
834 -- There must be one, since we already validated this earlier
836 Ttyp
:= Current_Scope
;
837 while not Is_Task_Type
(Ttyp
) loop
838 Ttyp
:= Scope
(Ttyp
);
842 -- Now rewrite the attribute with a call to Create_AST_Handler
845 Make_Function_Call
(Loc
,
846 Name
=> New_Occurrence_Of
(RTE
(RE_Create_AST_Handler
), Loc
),
847 Parameter_Associations
=> New_List
(
849 Entry_Index_Expression
(Loc
, Eent
, Index
, Ttyp
))));
851 Analyze_And_Resolve
(N
, RTE
(RE_AST_Handler
));
858 -- We compute this if a component clause was present, otherwise
859 -- we leave the computation up to Gigi, since we don't know what
860 -- layout will be chosen.
862 -- Note that the attribute can apply to a naked record component
863 -- in generated code (i.e. the prefix is an identifier that
864 -- references the component or discriminant entity).
866 when Attribute_Bit_Position
=> Bit_Position
:
871 if Nkind
(Pref
) = N_Identifier
then
874 CE
:= Entity
(Selector_Name
(Pref
));
877 if Known_Static_Component_Bit_Offset
(CE
) then
879 Make_Integer_Literal
(Loc
,
880 Intval
=> Component_Bit_Offset
(CE
)));
881 Analyze_And_Resolve
(N
, Typ
);
884 Apply_Universal_Integer_Attribute_Checks
(N
);
892 -- A reference to P'Body_Version or P'Version is expanded to
895 -- pragma Import (C, Vnn, "uuuuT";
897 -- Get_Version_String (Vnn)
899 -- where uuuu is the unit name (dots replaced by double underscore)
900 -- and T is B for the cases of Body_Version, or Version applied to a
901 -- subprogram acting as its own spec, and S for Version applied to a
902 -- subprogram spec or package. This sequence of code references the
903 -- the unsigned constant created in the main program by the binder.
905 -- A special exception occurs for Standard, where the string
906 -- returned is a copy of the library string in gnatvsn.ads.
908 when Attribute_Body_Version | Attribute_Version
=> Version
: declare
909 E
: constant Entity_Id
:=
910 Make_Defining_Identifier
(Loc
, New_Internal_Name
('V'));
911 Pent
: Entity_Id
:= Entity
(Pref
);
915 -- If not library unit, get to containing library unit
917 while Pent
/= Standard_Standard
918 and then Scope
(Pent
) /= Standard_Standard
920 Pent
:= Scope
(Pent
);
923 -- Special case Standard
925 if Pent
= Standard_Standard
926 or else Pent
= Standard_ASCII
929 Make_String_Literal
(Loc
,
930 Strval
=> Verbose_Library_Version
));
935 -- Build required string constant
937 Get_Name_String
(Get_Unit_Name
(Pent
));
940 for J
in 1 .. Name_Len
- 2 loop
941 if Name_Buffer
(J
) = '.' then
942 Store_String_Chars
("__");
944 Store_String_Char
(Get_Char_Code
(Name_Buffer
(J
)));
948 -- Case of subprogram acting as its own spec, always use body
950 if Nkind
(Declaration_Node
(Pent
)) in N_Subprogram_Specification
951 and then Nkind
(Parent
(Declaration_Node
(Pent
))) =
953 and then Acts_As_Spec
(Parent
(Declaration_Node
(Pent
)))
955 Store_String_Chars
("B");
957 -- Case of no body present, always use spec
959 elsif not Unit_Requires_Body
(Pent
) then
960 Store_String_Chars
("S");
962 -- Otherwise use B for Body_Version, S for spec
964 elsif Id
= Attribute_Body_Version
then
965 Store_String_Chars
("B");
967 Store_String_Chars
("S");
971 Lib
.Version_Referenced
(S
);
973 -- Insert the object declaration
975 Insert_Actions
(N
, New_List
(
976 Make_Object_Declaration
(Loc
,
977 Defining_Identifier
=> E
,
979 New_Occurrence_Of
(RTE
(RE_Unsigned
), Loc
))));
981 -- Set entity as imported with correct external name
984 Set_Interface_Name
(E
, Make_String_Literal
(Loc
, S
));
986 -- And now rewrite original reference
989 Make_Function_Call
(Loc
,
990 Name
=> New_Reference_To
(RTE
(RE_Get_Version_String
), Loc
),
991 Parameter_Associations
=> New_List
(
992 New_Occurrence_Of
(E
, Loc
))));
995 Analyze_And_Resolve
(N
, RTE
(RE_Version_String
));
1002 -- Transforms 'Ceiling into a call to the floating-point attribute
1003 -- function Ceiling in Fat_xxx (where xxx is the root type)
1005 when Attribute_Ceiling
=>
1006 Expand_Fpt_Attribute_R
(N
);
1012 -- Transforms 'Callable attribute into a call to the Callable function
1014 when Attribute_Callable
=> Callable
:
1016 -- We have an object of a task interface class-wide type as a prefix
1017 -- to Callable. Generate:
1019 -- callable (Pref._disp_get_task_id);
1021 if Ada_Version
>= Ada_05
1022 and then Ekind
(Etype
(Pref
)) = E_Class_Wide_Type
1023 and then Is_Interface
(Etype
(Pref
))
1024 and then Is_Task_Interface
(Etype
(Pref
))
1027 Make_Function_Call
(Loc
,
1029 New_Reference_To
(RTE
(RE_Callable
), Loc
),
1030 Parameter_Associations
=> New_List
(
1031 Make_Selected_Component
(Loc
,
1033 New_Copy_Tree
(Pref
),
1035 Make_Identifier
(Loc
, Name_uDisp_Get_Task_Id
)))));
1038 Build_Call_With_Task
(Pref
, RTE
(RE_Callable
)));
1041 Analyze_And_Resolve
(N
, Standard_Boolean
);
1048 -- Transforms 'Caller attribute into a call to either the
1049 -- Task_Entry_Caller or the Protected_Entry_Caller function.
1051 when Attribute_Caller
=> Caller
: declare
1052 Id_Kind
: constant Entity_Id
:= RTE
(RO_AT_Task_Id
);
1053 Ent
: constant Entity_Id
:= Entity
(Pref
);
1054 Conctype
: constant Entity_Id
:= Scope
(Ent
);
1055 Nest_Depth
: Integer := 0;
1062 if Is_Protected_Type
(Conctype
) then
1064 or else Restriction_Active
(No_Entry_Queue
) = False
1065 or else Number_Entries
(Conctype
) > 1
1069 (RTE
(RE_Protected_Entry_Caller
), Loc
);
1073 (RTE
(RE_Protected_Single_Entry_Caller
), Loc
);
1077 Unchecked_Convert_To
(Id_Kind
,
1078 Make_Function_Call
(Loc
,
1080 Parameter_Associations
=> New_List
1083 (Corresponding_Body
(Parent
(Conctype
))), Loc
)))));
1088 -- Determine the nesting depth of the E'Caller attribute, that
1089 -- is, how many accept statements are nested within the accept
1090 -- statement for E at the point of E'Caller. The runtime uses
1091 -- this depth to find the specified entry call.
1093 for J
in reverse 0 .. Scope_Stack
.Last
loop
1094 S
:= Scope_Stack
.Table
(J
).Entity
;
1096 -- We should not reach the scope of the entry, as it should
1097 -- already have been checked in Sem_Attr that this attribute
1098 -- reference is within a matching accept statement.
1100 pragma Assert
(S
/= Conctype
);
1105 elsif Is_Entry
(S
) then
1106 Nest_Depth
:= Nest_Depth
+ 1;
1111 Unchecked_Convert_To
(Id_Kind
,
1112 Make_Function_Call
(Loc
,
1113 Name
=> New_Reference_To
(
1114 RTE
(RE_Task_Entry_Caller
), Loc
),
1115 Parameter_Associations
=> New_List
(
1116 Make_Integer_Literal
(Loc
,
1117 Intval
=> Int
(Nest_Depth
))))));
1120 Analyze_And_Resolve
(N
, Id_Kind
);
1127 -- Transforms 'Compose into a call to the floating-point attribute
1128 -- function Compose in Fat_xxx (where xxx is the root type)
1130 -- Note: we strictly should have special code here to deal with the
1131 -- case of absurdly negative arguments (less than Integer'First)
1132 -- which will return a (signed) zero value, but it hardly seems
1133 -- worth the effort. Absurdly large positive arguments will raise
1134 -- constraint error which is fine.
1136 when Attribute_Compose
=>
1137 Expand_Fpt_Attribute_RI
(N
);
1143 when Attribute_Constrained
=> Constrained
: declare
1144 Formal_Ent
: constant Entity_Id
:= Param_Entity
(Pref
);
1145 Typ
: constant Entity_Id
:= Etype
(Pref
);
1148 -- Reference to a parameter where the value is passed as an extra
1149 -- actual, corresponding to the extra formal referenced by the
1150 -- Extra_Constrained field of the corresponding formal. If this
1151 -- is an entry in-parameter, it is replaced by a constant renaming
1152 -- for which Extra_Constrained is never created.
1154 if Present
(Formal_Ent
)
1155 and then Ekind
(Formal_Ent
) /= E_Constant
1156 and then Present
(Extra_Constrained
(Formal_Ent
))
1160 (Extra_Constrained
(Formal_Ent
), Sloc
(N
)));
1162 -- For variables with a Extra_Constrained field, we use the
1163 -- corresponding entity.
1165 elsif Nkind
(Pref
) = N_Identifier
1166 and then Ekind
(Entity
(Pref
)) = E_Variable
1167 and then Present
(Extra_Constrained
(Entity
(Pref
)))
1171 (Extra_Constrained
(Entity
(Pref
)), Sloc
(N
)));
1173 -- For all other entity names, we can tell at compile time
1175 elsif Is_Entity_Name
(Pref
) then
1177 Ent
: constant Entity_Id
:= Entity
(Pref
);
1181 -- (RM J.4) obsolescent cases
1183 if Is_Type
(Ent
) then
1187 if Is_Private_Type
(Ent
) then
1188 Res
:= not Has_Discriminants
(Ent
)
1189 or else Is_Constrained
(Ent
);
1191 -- It not a private type, must be a generic actual type
1192 -- that corresponded to a private type. We know that this
1193 -- correspondence holds, since otherwise the reference
1194 -- within the generic template would have been illegal.
1197 if Is_Composite_Type
(Underlying_Type
(Ent
)) then
1198 Res
:= Is_Constrained
(Ent
);
1204 -- If the prefix is not a variable or is aliased, then
1205 -- definitely true; if it's a formal parameter without
1206 -- an associated extra formal, then treat it as constrained.
1208 elsif not Is_Variable
(Pref
)
1209 or else Present
(Formal_Ent
)
1210 or else Is_Aliased_View
(Pref
)
1214 -- Variable case, just look at type to see if it is
1215 -- constrained. Note that the one case where this is
1216 -- not accurate (the procedure formal case), has been
1220 Res
:= Is_Constrained
(Etype
(Ent
));
1224 New_Reference_To
(Boolean_Literals
(Res
), Loc
));
1227 -- Prefix is not an entity name. These are also cases where
1228 -- we can always tell at compile time by looking at the form
1229 -- and type of the prefix. If an explicit dereference of an
1230 -- object with constrained partial view, this is unconstrained
1231 -- (Ada 2005 AI-363).
1237 not Is_Variable
(Pref
)
1239 (Nkind
(Pref
) = N_Explicit_Dereference
1241 not Has_Constrained_Partial_View
(Base_Type
(Typ
)))
1242 or else Is_Constrained
(Typ
)),
1246 Analyze_And_Resolve
(N
, Standard_Boolean
);
1253 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1254 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1256 when Attribute_Copy_Sign
=>
1257 Expand_Fpt_Attribute_RR
(N
);
1263 -- Transforms 'Count attribute into a call to the Count function
1265 when Attribute_Count
=> Count
:
1271 Conctyp
: Entity_Id
;
1274 -- If the prefix is a member of an entry family, retrieve both
1275 -- entry name and index. For a simple entry there is no index.
1277 if Nkind
(Pref
) = N_Indexed_Component
then
1278 Entnam
:= Prefix
(Pref
);
1279 Index
:= First
(Expressions
(Pref
));
1285 -- Find the concurrent type in which this attribute is referenced
1286 -- (there had better be one).
1288 Conctyp
:= Current_Scope
;
1289 while not Is_Concurrent_Type
(Conctyp
) loop
1290 Conctyp
:= Scope
(Conctyp
);
1295 if Is_Protected_Type
(Conctyp
) then
1298 or else Restriction_Active
(No_Entry_Queue
) = False
1299 or else Number_Entries
(Conctyp
) > 1
1301 Name
:= New_Reference_To
(RTE
(RE_Protected_Count
), Loc
);
1304 Make_Function_Call
(Loc
,
1306 Parameter_Associations
=> New_List
(
1309 Corresponding_Body
(Parent
(Conctyp
))), Loc
),
1310 Entry_Index_Expression
(
1311 Loc
, Entity
(Entnam
), Index
, Scope
(Entity
(Entnam
)))));
1313 Name
:= New_Reference_To
(RTE
(RE_Protected_Count_Entry
), Loc
);
1315 Call
:= Make_Function_Call
(Loc
,
1317 Parameter_Associations
=> New_List
(
1320 Corresponding_Body
(Parent
(Conctyp
))), Loc
)));
1327 Make_Function_Call
(Loc
,
1328 Name
=> New_Reference_To
(RTE
(RE_Task_Count
), Loc
),
1329 Parameter_Associations
=> New_List
(
1330 Entry_Index_Expression
1331 (Loc
, Entity
(Entnam
), Index
, Scope
(Entity
(Entnam
)))));
1334 -- The call returns type Natural but the context is universal integer
1335 -- so any integer type is allowed. The attribute was already resolved
1336 -- so its Etype is the required result type. If the base type of the
1337 -- context type is other than Standard.Integer we put in a conversion
1338 -- to the required type. This can be a normal typed conversion since
1339 -- both input and output types of the conversion are integer types
1341 if Base_Type
(Typ
) /= Base_Type
(Standard_Integer
) then
1342 Rewrite
(N
, Convert_To
(Typ
, Call
));
1347 Analyze_And_Resolve
(N
, Typ
);
1354 -- This processing is shared by Elab_Spec
1356 -- What we do is to insert the following declarations
1359 -- pragma Import (C, enn, "name___elabb/s");
1361 -- and then the Elab_Body/Spec attribute is replaced by a reference
1362 -- to this defining identifier.
1364 when Attribute_Elab_Body |
1365 Attribute_Elab_Spec
=>
1368 Ent
: constant Entity_Id
:=
1369 Make_Defining_Identifier
(Loc
,
1370 New_Internal_Name
('E'));
1374 procedure Make_Elab_String
(Nod
: Node_Id
);
1375 -- Given Nod, an identifier, or a selected component, put the
1376 -- image into the current string literal, with double underline
1377 -- between components.
1379 procedure Make_Elab_String
(Nod
: Node_Id
) is
1381 if Nkind
(Nod
) = N_Selected_Component
then
1382 Make_Elab_String
(Prefix
(Nod
));
1384 Store_String_Char
('$');
1386 Store_String_Char
('_');
1387 Store_String_Char
('_');
1390 Get_Name_String
(Chars
(Selector_Name
(Nod
)));
1393 pragma Assert
(Nkind
(Nod
) = N_Identifier
);
1394 Get_Name_String
(Chars
(Nod
));
1397 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
1398 end Make_Elab_String
;
1400 -- Start of processing for Elab_Body/Elab_Spec
1403 -- First we need to prepare the string literal for the name of
1404 -- the elaboration routine to be referenced.
1407 Make_Elab_String
(Pref
);
1410 Store_String_Chars
("._elab");
1411 Lang
:= Make_Identifier
(Loc
, Name_Ada
);
1413 Store_String_Chars
("___elab");
1414 Lang
:= Make_Identifier
(Loc
, Name_C
);
1417 if Id
= Attribute_Elab_Body
then
1418 Store_String_Char
('b');
1420 Store_String_Char
('s');
1425 Insert_Actions
(N
, New_List
(
1426 Make_Subprogram_Declaration
(Loc
,
1428 Make_Procedure_Specification
(Loc
,
1429 Defining_Unit_Name
=> Ent
)),
1432 Chars
=> Name_Import
,
1433 Pragma_Argument_Associations
=> New_List
(
1434 Make_Pragma_Argument_Association
(Loc
,
1435 Expression
=> Lang
),
1437 Make_Pragma_Argument_Association
(Loc
,
1439 Make_Identifier
(Loc
, Chars
(Ent
))),
1441 Make_Pragma_Argument_Association
(Loc
,
1443 Make_String_Literal
(Loc
, Str
))))));
1445 Set_Entity
(N
, Ent
);
1446 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
1453 -- Elaborated is always True for preelaborated units, predefined
1454 -- units, pure units and units which have Elaborate_Body pragmas.
1455 -- These units have no elaboration entity.
1457 -- Note: The Elaborated attribute is never passed through to Gigi
1459 when Attribute_Elaborated
=> Elaborated
: declare
1460 Ent
: constant Entity_Id
:= Entity
(Pref
);
1463 if Present
(Elaboration_Entity
(Ent
)) then
1465 New_Occurrence_Of
(Elaboration_Entity
(Ent
), Loc
));
1467 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
1475 when Attribute_Enum_Rep
=> Enum_Rep
:
1477 -- X'Enum_Rep (Y) expands to
1481 -- This is simply a direct conversion from the enumeration type
1482 -- to the target integer type, which is treated by Gigi as a normal
1483 -- integer conversion, treating the enumeration type as an integer,
1484 -- which is exactly what we want! We set Conversion_OK to make sure
1485 -- that the analyzer does not complain about what otherwise might
1486 -- be an illegal conversion.
1488 if Is_Non_Empty_List
(Exprs
) then
1490 OK_Convert_To
(Typ
, Relocate_Node
(First
(Exprs
))));
1492 -- X'Enum_Rep where X is an enumeration literal is replaced by
1493 -- the literal value.
1495 elsif Ekind
(Entity
(Pref
)) = E_Enumeration_Literal
then
1497 Make_Integer_Literal
(Loc
, Enumeration_Rep
(Entity
(Pref
))));
1499 -- If this is a renaming of a literal, recover the representation
1502 elsif Ekind
(Entity
(Pref
)) = E_Constant
1503 and then Present
(Renamed_Object
(Entity
(Pref
)))
1505 Ekind
(Entity
(Renamed_Object
(Entity
(Pref
))))
1506 = E_Enumeration_Literal
1509 Make_Integer_Literal
(Loc
,
1510 Enumeration_Rep
(Entity
(Renamed_Object
(Entity
(Pref
))))));
1512 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1513 -- of the object value, as described for the type case above.
1517 OK_Convert_To
(Typ
, Relocate_Node
(Pref
)));
1521 Analyze_And_Resolve
(N
, Typ
);
1529 -- Transforms 'Exponent into a call to the floating-point attribute
1530 -- function Exponent in Fat_xxx (where xxx is the root type)
1532 when Attribute_Exponent
=>
1533 Expand_Fpt_Attribute_R
(N
);
1539 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1541 when Attribute_External_Tag
=> External_Tag
:
1544 Make_Function_Call
(Loc
,
1545 Name
=> New_Reference_To
(RTE
(RE_External_Tag
), Loc
),
1546 Parameter_Associations
=> New_List
(
1547 Make_Attribute_Reference
(Loc
,
1548 Attribute_Name
=> Name_Tag
,
1549 Prefix
=> Prefix
(N
)))));
1551 Analyze_And_Resolve
(N
, Standard_String
);
1558 when Attribute_First
=> declare
1559 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1562 -- If the prefix type is a constrained packed array type which
1563 -- already has a Packed_Array_Type representation defined, then
1564 -- replace this attribute with a direct reference to 'First of the
1565 -- appropriate index subtype (since otherwise Gigi will try to give
1566 -- us the value of 'First for this implementation type).
1568 if Is_Constrained_Packed_Array
(Ptyp
) then
1570 Make_Attribute_Reference
(Loc
,
1571 Attribute_Name
=> Name_First
,
1572 Prefix
=> New_Reference_To
(Get_Index_Subtype
(N
), Loc
)));
1573 Analyze_And_Resolve
(N
, Typ
);
1575 elsif Is_Access_Type
(Ptyp
) then
1576 Apply_Access_Check
(N
);
1584 -- We compute this if a component clause was present, otherwise
1585 -- we leave the computation up to Gigi, since we don't know what
1586 -- layout will be chosen.
1588 when Attribute_First_Bit
=> First_Bit
:
1590 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
1593 if Known_Static_Component_Bit_Offset
(CE
) then
1595 Make_Integer_Literal
(Loc
,
1596 Component_Bit_Offset
(CE
) mod System_Storage_Unit
));
1598 Analyze_And_Resolve
(N
, Typ
);
1601 Apply_Universal_Integer_Attribute_Checks
(N
);
1611 -- fixtype'Fixed_Value (integer-value)
1615 -- fixtype(integer-value)
1617 -- we do all the required analysis of the conversion here, because
1618 -- we do not want this to go through the fixed-point conversion
1619 -- circuits. Note that gigi always treats fixed-point as equivalent
1620 -- to the corresponding integer type anyway.
1622 when Attribute_Fixed_Value
=> Fixed_Value
:
1625 Make_Type_Conversion
(Loc
,
1626 Subtype_Mark
=> New_Occurrence_Of
(Entity
(Pref
), Loc
),
1627 Expression
=> Relocate_Node
(First
(Exprs
))));
1628 Set_Etype
(N
, Entity
(Pref
));
1631 -- Note: it might appear that a properly analyzed unchecked conversion
1632 -- would be just fine here, but that's not the case, since the full
1633 -- range checks performed by the following call are critical!
1635 Apply_Type_Conversion_Checks
(N
);
1642 -- Transforms 'Floor into a call to the floating-point attribute
1643 -- function Floor in Fat_xxx (where xxx is the root type)
1645 when Attribute_Floor
=>
1646 Expand_Fpt_Attribute_R
(N
);
1652 -- For the fixed-point type Typ:
1658 -- Result_Type (System.Fore (Universal_Real (Type'First)),
1659 -- Universal_Real (Type'Last))
1661 -- Note that we know that the type is a non-static subtype, or Fore
1662 -- would have itself been computed dynamically in Eval_Attribute.
1664 when Attribute_Fore
=> Fore
:
1666 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1671 Make_Function_Call
(Loc
,
1672 Name
=> New_Reference_To
(RTE
(RE_Fore
), Loc
),
1674 Parameter_Associations
=> New_List
(
1675 Convert_To
(Universal_Real
,
1676 Make_Attribute_Reference
(Loc
,
1677 Prefix
=> New_Reference_To
(Ptyp
, Loc
),
1678 Attribute_Name
=> Name_First
)),
1680 Convert_To
(Universal_Real
,
1681 Make_Attribute_Reference
(Loc
,
1682 Prefix
=> New_Reference_To
(Ptyp
, Loc
),
1683 Attribute_Name
=> Name_Last
))))));
1685 Analyze_And_Resolve
(N
, Typ
);
1692 -- Transforms 'Fraction into a call to the floating-point attribute
1693 -- function Fraction in Fat_xxx (where xxx is the root type)
1695 when Attribute_Fraction
=>
1696 Expand_Fpt_Attribute_R
(N
);
1702 -- For an exception returns a reference to the exception data:
1703 -- Exception_Id!(Prefix'Reference)
1705 -- For a task it returns a reference to the _task_id component of
1706 -- corresponding record:
1708 -- taskV!(Prefix)._Task_Id, converted to the type Task_Id defined
1710 -- in Ada.Task_Identification
1712 when Attribute_Identity
=> Identity
: declare
1713 Id_Kind
: Entity_Id
;
1716 if Etype
(Pref
) = Standard_Exception_Type
then
1717 Id_Kind
:= RTE
(RE_Exception_Id
);
1719 if Present
(Renamed_Object
(Entity
(Pref
))) then
1720 Set_Entity
(Pref
, Renamed_Object
(Entity
(Pref
)));
1724 Unchecked_Convert_To
(Id_Kind
, Make_Reference
(Loc
, Pref
)));
1726 Id_Kind
:= RTE
(RO_AT_Task_Id
);
1729 Unchecked_Convert_To
(Id_Kind
, Concurrent_Ref
(Pref
)));
1732 Analyze_And_Resolve
(N
, Id_Kind
);
1739 -- Image attribute is handled in separate unit Exp_Imgv
1741 when Attribute_Image
=>
1742 Exp_Imgv
.Expand_Image_Attribute
(N
);
1748 -- X'Img is expanded to typ'Image (X), where typ is the type of X
1750 when Attribute_Img
=> Img
:
1753 Make_Attribute_Reference
(Loc
,
1754 Prefix
=> New_Reference_To
(Etype
(Pref
), Loc
),
1755 Attribute_Name
=> Name_Image
,
1756 Expressions
=> New_List
(Relocate_Node
(Pref
))));
1758 Analyze_And_Resolve
(N
, Standard_String
);
1765 when Attribute_Input
=> Input
: declare
1766 P_Type
: constant Entity_Id
:= Entity
(Pref
);
1767 B_Type
: constant Entity_Id
:= Base_Type
(P_Type
);
1768 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
1769 Strm
: constant Node_Id
:= First
(Exprs
);
1777 Cntrl
: Node_Id
:= Empty
;
1778 -- Value for controlling argument in call. Always Empty except in
1779 -- the dispatching (class-wide type) case, where it is a reference
1780 -- to the dummy object initialized to the right internal tag.
1782 procedure Freeze_Stream_Subprogram
(F
: Entity_Id
);
1783 -- The expansion of the attribute reference may generate a call to
1784 -- a user-defined stream subprogram that is frozen by the call. This
1785 -- can lead to access-before-elaboration problem if the reference
1786 -- appears in an object declaration and the subprogram body has not
1787 -- been seen. The freezing of the subprogram requires special code
1788 -- because it appears in an expanded context where expressions do
1789 -- not freeze their constituents.
1791 ------------------------------
1792 -- Freeze_Stream_Subprogram --
1793 ------------------------------
1795 procedure Freeze_Stream_Subprogram
(F
: Entity_Id
) is
1796 Decl
: constant Node_Id
:= Unit_Declaration_Node
(F
);
1800 -- If this is user-defined subprogram, the corresponding
1801 -- stream function appears as a renaming-as-body, and the
1802 -- user subprogram must be retrieved by tree traversal.
1805 and then Nkind
(Decl
) = N_Subprogram_Declaration
1806 and then Present
(Corresponding_Body
(Decl
))
1808 Bod
:= Corresponding_Body
(Decl
);
1810 if Nkind
(Unit_Declaration_Node
(Bod
)) =
1811 N_Subprogram_Renaming_Declaration
1813 Set_Is_Frozen
(Entity
(Name
(Unit_Declaration_Node
(Bod
))));
1816 end Freeze_Stream_Subprogram
;
1818 -- Start of processing for Input
1821 -- If no underlying type, we have an error that will be diagnosed
1822 -- elsewhere, so here we just completely ignore the expansion.
1828 -- If there is a TSS for Input, just call it
1830 Fname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Input
);
1832 if Present
(Fname
) then
1836 -- If there is a Stream_Convert pragma, use it, we rewrite
1838 -- sourcetyp'Input (stream)
1842 -- sourcetyp (streamread (strmtyp'Input (stream)));
1844 -- where stmrearead is the given Read function that converts
1845 -- an argument of type strmtyp to type sourcetyp or a type
1846 -- from which it is derived. The extra conversion is required
1847 -- for the derived case.
1849 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
1851 if Present
(Prag
) then
1852 Arg2
:= Next
(First
(Pragma_Argument_Associations
(Prag
)));
1853 Rfunc
:= Entity
(Expression
(Arg2
));
1857 Make_Function_Call
(Loc
,
1858 Name
=> New_Occurrence_Of
(Rfunc
, Loc
),
1859 Parameter_Associations
=> New_List
(
1860 Make_Attribute_Reference
(Loc
,
1863 (Etype
(First_Formal
(Rfunc
)), Loc
),
1864 Attribute_Name
=> Name_Input
,
1865 Expressions
=> Exprs
)))));
1867 Analyze_And_Resolve
(N
, B_Type
);
1872 elsif Is_Elementary_Type
(U_Type
) then
1874 -- A special case arises if we have a defined _Read routine,
1875 -- since in this case we are required to call this routine.
1877 if Present
(TSS
(Base_Type
(U_Type
), TSS_Stream_Read
)) then
1878 Build_Record_Or_Elementary_Input_Function
1879 (Loc
, U_Type
, Decl
, Fname
);
1880 Insert_Action
(N
, Decl
);
1882 -- For normal cases, we call the I_xxx routine directly
1885 Rewrite
(N
, Build_Elementary_Input_Call
(N
));
1886 Analyze_And_Resolve
(N
, P_Type
);
1892 elsif Is_Array_Type
(U_Type
) then
1893 Build_Array_Input_Function
(Loc
, U_Type
, Decl
, Fname
);
1894 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
1896 -- Dispatching case with class-wide type
1898 elsif Is_Class_Wide_Type
(P_Type
) then
1901 Rtyp
: constant Entity_Id
:= Root_Type
(P_Type
);
1906 -- Read the internal tag (RM 13.13.2(34)) and use it to
1907 -- initialize a dummy tag object:
1909 -- Dnn : Ada.Tags.Tag
1910 -- := Descendant_Tag (String'Input (Strm), P_Type);
1912 -- This dummy object is used only to provide a controlling
1913 -- argument for the eventual _Input call. Descendant_Tag is
1914 -- called rather than Internal_Tag to ensure that we have a
1915 -- tag for a type that is descended from the prefix type and
1916 -- declared at the same accessibility level (the exception
1917 -- Tag_Error will be raised otherwise). The level check is
1918 -- required for Ada 2005 because tagged types can be
1919 -- extended in nested scopes (AI-344).
1922 Make_Defining_Identifier
(Loc
,
1923 Chars
=> New_Internal_Name
('D'));
1926 Make_Object_Declaration
(Loc
,
1927 Defining_Identifier
=> Dnn
,
1928 Object_Definition
=>
1929 New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
1931 Make_Function_Call
(Loc
,
1933 New_Occurrence_Of
(RTE
(RE_Descendant_Tag
), Loc
),
1934 Parameter_Associations
=> New_List
(
1935 Make_Attribute_Reference
(Loc
,
1937 New_Occurrence_Of
(Standard_String
, Loc
),
1938 Attribute_Name
=> Name_Input
,
1939 Expressions
=> New_List
(
1941 (Duplicate_Subexpr
(Strm
)))),
1942 Make_Attribute_Reference
(Loc
,
1943 Prefix
=> New_Reference_To
(P_Type
, Loc
),
1944 Attribute_Name
=> Name_Tag
))));
1946 Insert_Action
(N
, Decl
);
1948 -- Now we need to get the entity for the call, and construct
1949 -- a function call node, where we preset a reference to Dnn
1950 -- as the controlling argument (doing an unchecked convert
1951 -- to the class-wide tagged type to make it look like a real
1954 Fname
:= Find_Prim_Op
(Rtyp
, TSS_Stream_Input
);
1955 Cntrl
:= Unchecked_Convert_To
(P_Type
,
1956 New_Occurrence_Of
(Dnn
, Loc
));
1957 Set_Etype
(Cntrl
, P_Type
);
1958 Set_Parent
(Cntrl
, N
);
1961 -- For tagged types, use the primitive Input function
1963 elsif Is_Tagged_Type
(U_Type
) then
1964 Fname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Input
);
1966 -- All other record type cases, including protected records. The
1967 -- latter only arise for expander generated code for handling
1968 -- shared passive partition access.
1972 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
1974 -- Ada 2005 (AI-216): Program_Error is raised when executing
1975 -- the default implementation of the Input attribute of an
1976 -- unchecked union type if the type lacks default discriminant
1979 if Is_Unchecked_Union
(Base_Type
(U_Type
))
1980 and then not Present
(Discriminant_Constraint
(U_Type
))
1983 Make_Raise_Program_Error
(Loc
,
1984 Reason
=> PE_Unchecked_Union_Restriction
));
1989 Build_Record_Or_Elementary_Input_Function
1990 (Loc
, Base_Type
(U_Type
), Decl
, Fname
);
1991 Insert_Action
(N
, Decl
);
1993 if Nkind
(Parent
(N
)) = N_Object_Declaration
1994 and then Is_Record_Type
(U_Type
)
1996 -- The stream function may contain calls to user-defined
1997 -- Read procedures for individual components.
2004 Comp
:= First_Component
(U_Type
);
2005 while Present
(Comp
) loop
2007 Find_Stream_Subprogram
2008 (Etype
(Comp
), TSS_Stream_Read
);
2010 if Present
(Func
) then
2011 Freeze_Stream_Subprogram
(Func
);
2014 Next_Component
(Comp
);
2021 -- If we fall through, Fname is the function to be called. The result
2022 -- is obtained by calling the appropriate function, then converting
2023 -- the result. The conversion does a subtype check.
2026 Make_Function_Call
(Loc
,
2027 Name
=> New_Occurrence_Of
(Fname
, Loc
),
2028 Parameter_Associations
=> New_List
(
2029 Relocate_Node
(Strm
)));
2031 Set_Controlling_Argument
(Call
, Cntrl
);
2032 Rewrite
(N
, Unchecked_Convert_To
(P_Type
, Call
));
2033 Analyze_And_Resolve
(N
, P_Type
);
2035 if Nkind
(Parent
(N
)) = N_Object_Declaration
then
2036 Freeze_Stream_Subprogram
(Fname
);
2046 -- inttype'Fixed_Value (fixed-value)
2050 -- inttype(integer-value))
2052 -- we do all the required analysis of the conversion here, because
2053 -- we do not want this to go through the fixed-point conversion
2054 -- circuits. Note that gigi always treats fixed-point as equivalent
2055 -- to the corresponding integer type anyway.
2057 when Attribute_Integer_Value
=> Integer_Value
:
2060 Make_Type_Conversion
(Loc
,
2061 Subtype_Mark
=> New_Occurrence_Of
(Entity
(Pref
), Loc
),
2062 Expression
=> Relocate_Node
(First
(Exprs
))));
2063 Set_Etype
(N
, Entity
(Pref
));
2066 -- Note: it might appear that a properly analyzed unchecked conversion
2067 -- would be just fine here, but that's not the case, since the full
2068 -- range checks performed by the following call are critical!
2070 Apply_Type_Conversion_Checks
(N
);
2077 when Attribute_Last
=> declare
2078 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2081 -- If the prefix type is a constrained packed array type which
2082 -- already has a Packed_Array_Type representation defined, then
2083 -- replace this attribute with a direct reference to 'Last of the
2084 -- appropriate index subtype (since otherwise Gigi will try to give
2085 -- us the value of 'Last for this implementation type).
2087 if Is_Constrained_Packed_Array
(Ptyp
) then
2089 Make_Attribute_Reference
(Loc
,
2090 Attribute_Name
=> Name_Last
,
2091 Prefix
=> New_Reference_To
(Get_Index_Subtype
(N
), Loc
)));
2092 Analyze_And_Resolve
(N
, Typ
);
2094 elsif Is_Access_Type
(Ptyp
) then
2095 Apply_Access_Check
(N
);
2103 -- We compute this if a component clause was present, otherwise
2104 -- we leave the computation up to Gigi, since we don't know what
2105 -- layout will be chosen.
2107 when Attribute_Last_Bit
=> Last_Bit
:
2109 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
2112 if Known_Static_Component_Bit_Offset
(CE
)
2113 and then Known_Static_Esize
(CE
)
2116 Make_Integer_Literal
(Loc
,
2117 Intval
=> (Component_Bit_Offset
(CE
) mod System_Storage_Unit
)
2120 Analyze_And_Resolve
(N
, Typ
);
2123 Apply_Universal_Integer_Attribute_Checks
(N
);
2131 -- Transforms 'Leading_Part into a call to the floating-point attribute
2132 -- function Leading_Part in Fat_xxx (where xxx is the root type)
2134 -- Note: strictly, we should have special case code to deal with
2135 -- absurdly large positive arguments (greater than Integer'Last), which
2136 -- result in returning the first argument unchanged, but it hardly seems
2137 -- worth the effort. We raise constraint error for absurdly negative
2138 -- arguments which is fine.
2140 when Attribute_Leading_Part
=>
2141 Expand_Fpt_Attribute_RI
(N
);
2147 when Attribute_Length
=> declare
2148 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2153 -- Processing for packed array types
2155 if Is_Array_Type
(Ptyp
) and then Is_Packed
(Ptyp
) then
2156 Ityp
:= Get_Index_Subtype
(N
);
2158 -- If the index type, Ityp, is an enumeration type with
2159 -- holes, then we calculate X'Length explicitly using
2162 -- (0, Ityp'Pos (X'Last (N)) -
2163 -- Ityp'Pos (X'First (N)) + 1);
2165 -- Since the bounds in the template are the representation
2166 -- values and gigi would get the wrong value.
2168 if Is_Enumeration_Type
(Ityp
)
2169 and then Present
(Enum_Pos_To_Rep
(Base_Type
(Ityp
)))
2174 Xnum
:= Expr_Value
(First
(Expressions
(N
)));
2178 Make_Attribute_Reference
(Loc
,
2179 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
2180 Attribute_Name
=> Name_Max
,
2181 Expressions
=> New_List
2182 (Make_Integer_Literal
(Loc
, 0),
2186 Make_Op_Subtract
(Loc
,
2188 Make_Attribute_Reference
(Loc
,
2189 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2190 Attribute_Name
=> Name_Pos
,
2192 Expressions
=> New_List
(
2193 Make_Attribute_Reference
(Loc
,
2194 Prefix
=> Duplicate_Subexpr
(Pref
),
2195 Attribute_Name
=> Name_Last
,
2196 Expressions
=> New_List
(
2197 Make_Integer_Literal
(Loc
, Xnum
))))),
2200 Make_Attribute_Reference
(Loc
,
2201 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2202 Attribute_Name
=> Name_Pos
,
2204 Expressions
=> New_List
(
2205 Make_Attribute_Reference
(Loc
,
2207 Duplicate_Subexpr_No_Checks
(Pref
),
2208 Attribute_Name
=> Name_First
,
2209 Expressions
=> New_List
(
2210 Make_Integer_Literal
(Loc
, Xnum
)))))),
2212 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
2214 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2217 -- If the prefix type is a constrained packed array type which
2218 -- already has a Packed_Array_Type representation defined, then
2219 -- replace this attribute with a direct reference to 'Range_Length
2220 -- of the appropriate index subtype (since otherwise Gigi will try
2221 -- to give us the value of 'Length for this implementation type).
2223 elsif Is_Constrained
(Ptyp
) then
2225 Make_Attribute_Reference
(Loc
,
2226 Attribute_Name
=> Name_Range_Length
,
2227 Prefix
=> New_Reference_To
(Ityp
, Loc
)));
2228 Analyze_And_Resolve
(N
, Typ
);
2231 -- If we have a packed array that is not bit packed, which was
2235 elsif Is_Access_Type
(Ptyp
) then
2236 Apply_Access_Check
(N
);
2238 -- If the designated type is a packed array type, then we
2239 -- convert the reference to:
2242 -- xtyp'Pos (Pref'Last (Expr)) -
2243 -- xtyp'Pos (Pref'First (Expr)));
2245 -- This is a bit complex, but it is the easiest thing to do
2246 -- that works in all cases including enum types with holes
2247 -- xtyp here is the appropriate index type.
2250 Dtyp
: constant Entity_Id
:= Designated_Type
(Ptyp
);
2254 if Is_Array_Type
(Dtyp
) and then Is_Packed
(Dtyp
) then
2255 Xtyp
:= Get_Index_Subtype
(N
);
2258 Make_Attribute_Reference
(Loc
,
2259 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
2260 Attribute_Name
=> Name_Max
,
2261 Expressions
=> New_List
(
2262 Make_Integer_Literal
(Loc
, 0),
2265 Make_Integer_Literal
(Loc
, 1),
2266 Make_Op_Subtract
(Loc
,
2268 Make_Attribute_Reference
(Loc
,
2269 Prefix
=> New_Occurrence_Of
(Xtyp
, Loc
),
2270 Attribute_Name
=> Name_Pos
,
2271 Expressions
=> New_List
(
2272 Make_Attribute_Reference
(Loc
,
2273 Prefix
=> Duplicate_Subexpr
(Pref
),
2274 Attribute_Name
=> Name_Last
,
2276 New_Copy_List
(Exprs
)))),
2279 Make_Attribute_Reference
(Loc
,
2280 Prefix
=> New_Occurrence_Of
(Xtyp
, Loc
),
2281 Attribute_Name
=> Name_Pos
,
2282 Expressions
=> New_List
(
2283 Make_Attribute_Reference
(Loc
,
2285 Duplicate_Subexpr_No_Checks
(Pref
),
2286 Attribute_Name
=> Name_First
,
2288 New_Copy_List
(Exprs
)))))))));
2290 Analyze_And_Resolve
(N
, Typ
);
2294 -- Otherwise leave it to gigi
2297 Apply_Universal_Integer_Attribute_Checks
(N
);
2305 -- Transforms 'Machine into a call to the floating-point attribute
2306 -- function Machine in Fat_xxx (where xxx is the root type)
2308 when Attribute_Machine
=>
2309 Expand_Fpt_Attribute_R
(N
);
2311 ----------------------
2312 -- Machine_Rounding --
2313 ----------------------
2315 -- Transforms 'Machine_Rounding into a call to the floating-point
2316 -- attribute function Machine_Rounding in Fat_xxx (where xxx is the root
2319 when Attribute_Machine_Rounding
=>
2320 Expand_Fpt_Attribute_R
(N
);
2326 -- Machine_Size is equivalent to Object_Size, so transform it into
2327 -- Object_Size and that way Gigi never sees Machine_Size.
2329 when Attribute_Machine_Size
=>
2331 Make_Attribute_Reference
(Loc
,
2332 Prefix
=> Prefix
(N
),
2333 Attribute_Name
=> Name_Object_Size
));
2335 Analyze_And_Resolve
(N
, Typ
);
2341 -- The only case that can get this far is the dynamic case of the old
2342 -- Ada 83 Mantissa attribute for the fixed-point case. For this case, we
2349 -- ityp (System.Mantissa.Mantissa_Value
2350 -- (Integer'Integer_Value (typ'First),
2351 -- Integer'Integer_Value (typ'Last)));
2353 when Attribute_Mantissa
=> Mantissa
: declare
2354 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2359 Make_Function_Call
(Loc
,
2360 Name
=> New_Occurrence_Of
(RTE
(RE_Mantissa_Value
), Loc
),
2362 Parameter_Associations
=> New_List
(
2364 Make_Attribute_Reference
(Loc
,
2365 Prefix
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
2366 Attribute_Name
=> Name_Integer_Value
,
2367 Expressions
=> New_List
(
2369 Make_Attribute_Reference
(Loc
,
2370 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
2371 Attribute_Name
=> Name_First
))),
2373 Make_Attribute_Reference
(Loc
,
2374 Prefix
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
2375 Attribute_Name
=> Name_Integer_Value
,
2376 Expressions
=> New_List
(
2378 Make_Attribute_Reference
(Loc
,
2379 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
2380 Attribute_Name
=> Name_Last
)))))));
2382 Analyze_And_Resolve
(N
, Typ
);
2389 when Attribute_Mod
=> Mod_Case
: declare
2390 Arg
: constant Node_Id
:= Relocate_Node
(First
(Exprs
));
2391 Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Arg
));
2392 Modv
: constant Uint
:= Modulus
(Btyp
);
2396 -- This is not so simple. The issue is what type to use for the
2397 -- computation of the modular value.
2399 -- The easy case is when the modulus value is within the bounds
2400 -- of the signed integer type of the argument. In this case we can
2401 -- just do the computation in that signed integer type, and then
2402 -- do an ordinary conversion to the target type.
2404 if Modv
<= Expr_Value
(Hi
) then
2409 Right_Opnd
=> Make_Integer_Literal
(Loc
, Modv
))));
2411 -- Here we know that the modulus is larger than type'Last of the
2412 -- integer type. There are two cases to consider:
2414 -- a) The integer value is non-negative. In this case, it is
2415 -- returned as the result (since it is less than the modulus).
2417 -- b) The integer value is negative. In this case, we know that the
2418 -- result is modulus + value, where the value might be as small as
2419 -- -modulus. The trouble is what type do we use to do the subtract.
2420 -- No type will do, since modulus can be as big as 2**64, and no
2421 -- integer type accomodates this value. Let's do bit of algebra
2424 -- = modulus - (-value)
2425 -- = (modulus - 1) - (-value - 1)
2427 -- Now modulus - 1 is certainly in range of the modular type.
2428 -- -value is in the range 1 .. modulus, so -value -1 is in the
2429 -- range 0 .. modulus-1 which is in range of the modular type.
2430 -- Furthermore, (-value - 1) can be expressed as -(value + 1)
2431 -- which we can compute using the integer base type.
2433 -- Once this is done we analyze the conditional expression without
2434 -- range checks, because we know everything is in range, and we
2435 -- want to prevent spurious warnings on either branch.
2439 Make_Conditional_Expression
(Loc
,
2440 Expressions
=> New_List
(
2442 Left_Opnd
=> Duplicate_Subexpr
(Arg
),
2443 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
2446 Duplicate_Subexpr_No_Checks
(Arg
)),
2448 Make_Op_Subtract
(Loc
,
2450 Make_Integer_Literal
(Loc
,
2451 Intval
=> Modv
- 1),
2457 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Arg
),
2459 Make_Integer_Literal
(Loc
,
2460 Intval
=> 1))))))));
2464 Analyze_And_Resolve
(N
, Btyp
, Suppress
=> All_Checks
);
2471 -- Transforms 'Model into a call to the floating-point attribute
2472 -- function Model in Fat_xxx (where xxx is the root type)
2474 when Attribute_Model
=>
2475 Expand_Fpt_Attribute_R
(N
);
2481 -- The processing for Object_Size shares the processing for Size
2487 when Attribute_Output
=> Output
: declare
2488 P_Type
: constant Entity_Id
:= Entity
(Pref
);
2489 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
2497 -- If no underlying type, we have an error that will be diagnosed
2498 -- elsewhere, so here we just completely ignore the expansion.
2504 -- If TSS for Output is present, just call it
2506 Pname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Output
);
2508 if Present
(Pname
) then
2512 -- If there is a Stream_Convert pragma, use it, we rewrite
2514 -- sourcetyp'Output (stream, Item)
2518 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
2520 -- where strmwrite is the given Write function that converts an
2521 -- argument of type sourcetyp or a type acctyp, from which it is
2522 -- derived to type strmtyp. The conversion to acttyp is required
2523 -- for the derived case.
2525 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
2527 if Present
(Prag
) then
2529 Next
(Next
(First
(Pragma_Argument_Associations
(Prag
))));
2530 Wfunc
:= Entity
(Expression
(Arg3
));
2533 Make_Attribute_Reference
(Loc
,
2534 Prefix
=> New_Occurrence_Of
(Etype
(Wfunc
), Loc
),
2535 Attribute_Name
=> Name_Output
,
2536 Expressions
=> New_List
(
2537 Relocate_Node
(First
(Exprs
)),
2538 Make_Function_Call
(Loc
,
2539 Name
=> New_Occurrence_Of
(Wfunc
, Loc
),
2540 Parameter_Associations
=> New_List
(
2541 Convert_To
(Etype
(First_Formal
(Wfunc
)),
2542 Relocate_Node
(Next
(First
(Exprs
)))))))));
2547 -- For elementary types, we call the W_xxx routine directly.
2548 -- Note that the effect of Write and Output is identical for
2549 -- the case of an elementary type, since there are no
2550 -- discriminants or bounds.
2552 elsif Is_Elementary_Type
(U_Type
) then
2554 -- A special case arises if we have a defined _Write routine,
2555 -- since in this case we are required to call this routine.
2557 if Present
(TSS
(Base_Type
(U_Type
), TSS_Stream_Write
)) then
2558 Build_Record_Or_Elementary_Output_Procedure
2559 (Loc
, U_Type
, Decl
, Pname
);
2560 Insert_Action
(N
, Decl
);
2562 -- For normal cases, we call the W_xxx routine directly
2565 Rewrite
(N
, Build_Elementary_Write_Call
(N
));
2572 elsif Is_Array_Type
(U_Type
) then
2573 Build_Array_Output_Procedure
(Loc
, U_Type
, Decl
, Pname
);
2574 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
2576 -- Class-wide case, first output external tag, then dispatch
2577 -- to the appropriate primitive Output function (RM 13.13.2(31)).
2579 elsif Is_Class_Wide_Type
(P_Type
) then
2581 Strm
: constant Node_Id
:= First
(Exprs
);
2582 Item
: constant Node_Id
:= Next
(Strm
);
2586 -- if Get_Access_Level (Item'Tag)
2587 -- /= Get_Access_Level (P_Type'Tag)
2591 -- String'Output (Strm, External_Tag (Item'Tag));
2593 -- Ada 2005 (AI-344): Check that the accessibility level
2594 -- of the type of the output object is not deeper than
2595 -- that of the attribute's prefix type.
2597 if Ada_Version
>= Ada_05
then
2599 Make_Implicit_If_Statement
(N
,
2603 Make_Function_Call
(Loc
,
2606 (RTE
(RE_Get_Access_Level
), Loc
),
2607 Parameter_Associations
=>
2608 New_List
(Make_Attribute_Reference
(Loc
,
2611 Duplicate_Subexpr
(Item
,
2616 Make_Integer_Literal
2617 (Loc
, Type_Access_Level
(P_Type
))),
2619 New_List
(Make_Raise_Statement
(Loc
,
2621 RTE
(RE_Tag_Error
), Loc
)))));
2625 Make_Attribute_Reference
(Loc
,
2626 Prefix
=> New_Occurrence_Of
(Standard_String
, Loc
),
2627 Attribute_Name
=> Name_Output
,
2628 Expressions
=> New_List
(
2629 Relocate_Node
(Duplicate_Subexpr
(Strm
)),
2630 Make_Function_Call
(Loc
,
2632 New_Occurrence_Of
(RTE
(RE_External_Tag
), Loc
),
2633 Parameter_Associations
=> New_List
(
2634 Make_Attribute_Reference
(Loc
,
2637 (Duplicate_Subexpr
(Item
, Name_Req
=> True)),
2638 Attribute_Name
=> Name_Tag
))))));
2641 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Output
);
2643 -- Tagged type case, use the primitive Output function
2645 elsif Is_Tagged_Type
(U_Type
) then
2646 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Output
);
2648 -- -- All other record type cases, including protected records.
2649 -- -- The latter only arise for expander generated code for
2650 -- -- handling shared passive partition access.
2654 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
2656 -- Ada 2005 (AI-216): Program_Error is raised when executing
2657 -- the default implementation of the Output attribute of an
2658 -- unchecked union type if the type lacks default discriminant
2661 if Is_Unchecked_Union
(Base_Type
(U_Type
))
2662 and then not Present
(Discriminant_Constraint
(U_Type
))
2665 Make_Raise_Program_Error
(Loc
,
2666 Reason
=> PE_Unchecked_Union_Restriction
));
2671 Build_Record_Or_Elementary_Output_Procedure
2672 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
2673 Insert_Action
(N
, Decl
);
2677 -- If we fall through, Pname is the name of the procedure to call
2679 Rewrite_Stream_Proc_Call
(Pname
);
2686 -- For enumeration types with a standard representation, Pos is
2689 -- For enumeration types, with a non-standard representation we
2690 -- generate a call to the _Rep_To_Pos function created when the
2691 -- type was frozen. The call has the form
2693 -- _rep_to_pos (expr, flag)
2695 -- The parameter flag is True if range checks are enabled, causing
2696 -- Program_Error to be raised if the expression has an invalid
2697 -- representation, and False if range checks are suppressed.
2699 -- For integer types, Pos is equivalent to a simple integer
2700 -- conversion and we rewrite it as such
2702 when Attribute_Pos
=> Pos
:
2704 Etyp
: Entity_Id
:= Base_Type
(Entity
(Pref
));
2707 -- Deal with zero/non-zero boolean values
2709 if Is_Boolean_Type
(Etyp
) then
2710 Adjust_Condition
(First
(Exprs
));
2711 Etyp
:= Standard_Boolean
;
2712 Set_Prefix
(N
, New_Occurrence_Of
(Standard_Boolean
, Loc
));
2715 -- Case of enumeration type
2717 if Is_Enumeration_Type
(Etyp
) then
2719 -- Non-standard enumeration type (generate call)
2721 if Present
(Enum_Pos_To_Rep
(Etyp
)) then
2722 Append_To
(Exprs
, Rep_To_Pos_Flag
(Etyp
, Loc
));
2725 Make_Function_Call
(Loc
,
2727 New_Reference_To
(TSS
(Etyp
, TSS_Rep_To_Pos
), Loc
),
2728 Parameter_Associations
=> Exprs
)));
2730 Analyze_And_Resolve
(N
, Typ
);
2732 -- Standard enumeration type (do universal integer check)
2735 Apply_Universal_Integer_Attribute_Checks
(N
);
2738 -- Deal with integer types (replace by conversion)
2740 elsif Is_Integer_Type
(Etyp
) then
2741 Rewrite
(N
, Convert_To
(Typ
, First
(Exprs
)));
2742 Analyze_And_Resolve
(N
, Typ
);
2751 -- We compute this if a component clause was present, otherwise
2752 -- we leave the computation up to Gigi, since we don't know what
2753 -- layout will be chosen.
2755 when Attribute_Position
=> Position
:
2757 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
2760 if Present
(Component_Clause
(CE
)) then
2762 Make_Integer_Literal
(Loc
,
2763 Intval
=> Component_Bit_Offset
(CE
) / System_Storage_Unit
));
2764 Analyze_And_Resolve
(N
, Typ
);
2767 Apply_Universal_Integer_Attribute_Checks
(N
);
2775 -- 1. Deal with enumeration types with holes
2776 -- 2. For floating-point, generate call to attribute function
2777 -- 3. For other cases, deal with constraint checking
2779 when Attribute_Pred
=> Pred
:
2781 Ptyp
: constant Entity_Id
:= Base_Type
(Etype
(Pref
));
2784 -- For enumeration types with non-standard representations, we
2785 -- expand typ'Pred (x) into
2787 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
2789 -- If the representation is contiguous, we compute instead
2790 -- Lit1 + Rep_to_Pos (x -1), to catch invalid representations.
2792 if Is_Enumeration_Type
(Ptyp
)
2793 and then Present
(Enum_Pos_To_Rep
(Ptyp
))
2795 if Has_Contiguous_Rep
(Ptyp
) then
2797 Unchecked_Convert_To
(Ptyp
,
2800 Make_Integer_Literal
(Loc
,
2801 Enumeration_Rep
(First_Literal
(Ptyp
))),
2803 Make_Function_Call
(Loc
,
2806 (TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
2808 Parameter_Associations
=>
2810 Unchecked_Convert_To
(Ptyp
,
2811 Make_Op_Subtract
(Loc
,
2813 Unchecked_Convert_To
(Standard_Integer
,
2814 Relocate_Node
(First
(Exprs
))),
2816 Make_Integer_Literal
(Loc
, 1))),
2817 Rep_To_Pos_Flag
(Ptyp
, Loc
))))));
2820 -- Add Boolean parameter True, to request program errror if
2821 -- we have a bad representation on our hands. If checks are
2822 -- suppressed, then add False instead
2824 Append_To
(Exprs
, Rep_To_Pos_Flag
(Ptyp
, Loc
));
2826 Make_Indexed_Component
(Loc
,
2827 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Ptyp
), Loc
),
2828 Expressions
=> New_List
(
2829 Make_Op_Subtract
(Loc
,
2831 Make_Function_Call
(Loc
,
2833 New_Reference_To
(TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
2834 Parameter_Associations
=> Exprs
),
2835 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
2838 Analyze_And_Resolve
(N
, Typ
);
2840 -- For floating-point, we transform 'Pred into a call to the Pred
2841 -- floating-point attribute function in Fat_xxx (xxx is root type)
2843 elsif Is_Floating_Point_Type
(Ptyp
) then
2844 Expand_Fpt_Attribute_R
(N
);
2845 Analyze_And_Resolve
(N
, Typ
);
2847 -- For modular types, nothing to do (no overflow, since wraps)
2849 elsif Is_Modular_Integer_Type
(Ptyp
) then
2852 -- For other types, if range checking is enabled, we must generate
2853 -- a check if overflow checking is enabled.
2855 elsif not Overflow_Checks_Suppressed
(Ptyp
) then
2856 Expand_Pred_Succ
(N
);
2865 when Attribute_Range_Length
=> Range_Length
: declare
2866 P_Type
: constant Entity_Id
:= Etype
(Pref
);
2869 -- The only special processing required is for the case where
2870 -- Range_Length is applied to an enumeration type with holes.
2871 -- In this case we transform
2877 -- X'Pos (X'Last) - X'Pos (X'First) + 1
2879 -- So that the result reflects the proper Pos values instead
2880 -- of the underlying representations.
2882 if Is_Enumeration_Type
(P_Type
)
2883 and then Has_Non_Standard_Rep
(P_Type
)
2888 Make_Op_Subtract
(Loc
,
2890 Make_Attribute_Reference
(Loc
,
2891 Attribute_Name
=> Name_Pos
,
2892 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
),
2893 Expressions
=> New_List
(
2894 Make_Attribute_Reference
(Loc
,
2895 Attribute_Name
=> Name_Last
,
2896 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
)))),
2899 Make_Attribute_Reference
(Loc
,
2900 Attribute_Name
=> Name_Pos
,
2901 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
),
2902 Expressions
=> New_List
(
2903 Make_Attribute_Reference
(Loc
,
2904 Attribute_Name
=> Name_First
,
2905 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
))))),
2908 Make_Integer_Literal
(Loc
, 1)));
2910 Analyze_And_Resolve
(N
, Typ
);
2912 -- For all other cases, attribute is handled by Gigi, but we need
2913 -- to deal with the case of the range check on a universal integer.
2916 Apply_Universal_Integer_Attribute_Checks
(N
);
2925 when Attribute_Read
=> Read
: declare
2926 P_Type
: constant Entity_Id
:= Entity
(Pref
);
2927 B_Type
: constant Entity_Id
:= Base_Type
(P_Type
);
2928 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
2938 -- If no underlying type, we have an error that will be diagnosed
2939 -- elsewhere, so here we just completely ignore the expansion.
2945 -- The simple case, if there is a TSS for Read, just call it
2947 Pname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Read
);
2949 if Present
(Pname
) then
2953 -- If there is a Stream_Convert pragma, use it, we rewrite
2955 -- sourcetyp'Read (stream, Item)
2959 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
2961 -- where strmread is the given Read function that converts an
2962 -- argument of type strmtyp to type sourcetyp or a type from which
2963 -- it is derived. The conversion to sourcetyp is required in the
2966 -- A special case arises if Item is a type conversion in which
2967 -- case, we have to expand to:
2969 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
2971 -- where Itemx is the expression of the type conversion (i.e.
2972 -- the actual object), and typex is the type of Itemx.
2974 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
2976 if Present
(Prag
) then
2977 Arg2
:= Next
(First
(Pragma_Argument_Associations
(Prag
)));
2978 Rfunc
:= Entity
(Expression
(Arg2
));
2979 Lhs
:= Relocate_Node
(Next
(First
(Exprs
)));
2982 Make_Function_Call
(Loc
,
2983 Name
=> New_Occurrence_Of
(Rfunc
, Loc
),
2984 Parameter_Associations
=> New_List
(
2985 Make_Attribute_Reference
(Loc
,
2988 (Etype
(First_Formal
(Rfunc
)), Loc
),
2989 Attribute_Name
=> Name_Input
,
2990 Expressions
=> New_List
(
2991 Relocate_Node
(First
(Exprs
)))))));
2993 if Nkind
(Lhs
) = N_Type_Conversion
then
2994 Lhs
:= Expression
(Lhs
);
2995 Rhs
:= Convert_To
(Etype
(Lhs
), Rhs
);
2999 Make_Assignment_Statement
(Loc
,
3001 Expression
=> Rhs
));
3002 Set_Assignment_OK
(Lhs
);
3006 -- For elementary types, we call the I_xxx routine using the first
3007 -- parameter and then assign the result into the second parameter.
3008 -- We set Assignment_OK to deal with the conversion case.
3010 elsif Is_Elementary_Type
(U_Type
) then
3016 Lhs
:= Relocate_Node
(Next
(First
(Exprs
)));
3017 Rhs
:= Build_Elementary_Input_Call
(N
);
3019 if Nkind
(Lhs
) = N_Type_Conversion
then
3020 Lhs
:= Expression
(Lhs
);
3021 Rhs
:= Convert_To
(Etype
(Lhs
), Rhs
);
3024 Set_Assignment_OK
(Lhs
);
3027 Make_Assignment_Statement
(Loc
,
3029 Expression
=> Rhs
));
3037 elsif Is_Array_Type
(U_Type
) then
3038 Build_Array_Read_Procedure
(N
, U_Type
, Decl
, Pname
);
3039 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
3041 -- Tagged type case, use the primitive Read function. Note that
3042 -- this will dispatch in the class-wide case which is what we want
3044 elsif Is_Tagged_Type
(U_Type
) then
3045 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Read
);
3047 -- All other record type cases, including protected records. The
3048 -- latter only arise for expander generated code for handling
3049 -- shared passive partition access.
3053 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
3055 -- Ada 2005 (AI-216): Program_Error is raised when executing
3056 -- the default implementation of the Read attribute of an
3057 -- Unchecked_Union type.
3059 if Is_Unchecked_Union
(Base_Type
(U_Type
)) then
3061 Make_Raise_Program_Error
(Loc
,
3062 Reason
=> PE_Unchecked_Union_Restriction
));
3065 if Has_Discriminants
(U_Type
)
3067 (Discriminant_Default_Value
(First_Discriminant
(U_Type
)))
3069 Build_Mutable_Record_Read_Procedure
3070 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
3072 Build_Record_Read_Procedure
3073 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
3076 -- Suppress checks, uninitialized or otherwise invalid
3077 -- data does not cause constraint errors to be raised for
3078 -- a complete record read.
3080 Insert_Action
(N
, Decl
, All_Checks
);
3084 Rewrite_Stream_Proc_Call
(Pname
);
3091 -- Transforms 'Remainder into a call to the floating-point attribute
3092 -- function Remainder in Fat_xxx (where xxx is the root type)
3094 when Attribute_Remainder
=>
3095 Expand_Fpt_Attribute_RR
(N
);
3101 -- The handling of the Round attribute is quite delicate. The processing
3102 -- in Sem_Attr introduced a conversion to universal real, reflecting the
3103 -- semantics of Round, but we do not want anything to do with universal
3104 -- real at runtime, since this corresponds to using floating-point
3107 -- What we have now is that the Etype of the Round attribute correctly
3108 -- indicates the final result type. The operand of the Round is the
3109 -- conversion to universal real, described above, and the operand of
3110 -- this conversion is the actual operand of Round, which may be the
3111 -- special case of a fixed point multiplication or division (Etype =
3114 -- The exapander will expand first the operand of the conversion, then
3115 -- the conversion, and finally the round attribute itself, since we
3116 -- always work inside out. But we cannot simply process naively in this
3117 -- order. In the semantic world where universal fixed and real really
3118 -- exist and have infinite precision, there is no problem, but in the
3119 -- implementation world, where universal real is a floating-point type,
3120 -- we would get the wrong result.
3122 -- So the approach is as follows. First, when expanding a multiply or
3123 -- divide whose type is universal fixed, we do nothing at all, instead
3124 -- deferring the operation till later.
3126 -- The actual processing is done in Expand_N_Type_Conversion which
3127 -- handles the special case of Round by looking at its parent to see if
3128 -- it is a Round attribute, and if it is, handling the conversion (or
3129 -- its fixed multiply/divide child) in an appropriate manner.
3131 -- This means that by the time we get to expanding the Round attribute
3132 -- itself, the Round is nothing more than a type conversion (and will
3133 -- often be a null type conversion), so we just replace it with the
3134 -- appropriate conversion operation.
3136 when Attribute_Round
=>
3138 Convert_To
(Etype
(N
), Relocate_Node
(First
(Exprs
))));
3139 Analyze_And_Resolve
(N
);
3145 -- Transforms 'Rounding into a call to the floating-point attribute
3146 -- function Rounding in Fat_xxx (where xxx is the root type)
3148 when Attribute_Rounding
=>
3149 Expand_Fpt_Attribute_R
(N
);
3155 -- Transforms 'Scaling into a call to the floating-point attribute
3156 -- function Scaling in Fat_xxx (where xxx is the root type)
3158 when Attribute_Scaling
=>
3159 Expand_Fpt_Attribute_RI
(N
);
3165 when Attribute_Size |
3166 Attribute_Object_Size |
3167 Attribute_Value_Size |
3168 Attribute_VADS_Size
=> Size
:
3171 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3176 -- Processing for VADS_Size case. Note that this processing removes
3177 -- all traces of VADS_Size from the tree, and completes all required
3178 -- processing for VADS_Size by translating the attribute reference
3179 -- to an appropriate Size or Object_Size reference.
3181 if Id
= Attribute_VADS_Size
3182 or else (Use_VADS_Size
and then Id
= Attribute_Size
)
3184 -- If the size is specified, then we simply use the specified
3185 -- size. This applies to both types and objects. The size of an
3186 -- object can be specified in the following ways:
3188 -- An explicit size object is given for an object
3189 -- A component size is specified for an indexed component
3190 -- A component clause is specified for a selected component
3191 -- The object is a component of a packed composite object
3193 -- If the size is specified, then VADS_Size of an object
3195 if (Is_Entity_Name
(Pref
)
3196 and then Present
(Size_Clause
(Entity
(Pref
))))
3198 (Nkind
(Pref
) = N_Component_Clause
3199 and then (Present
(Component_Clause
3200 (Entity
(Selector_Name
(Pref
))))
3201 or else Is_Packed
(Etype
(Prefix
(Pref
)))))
3203 (Nkind
(Pref
) = N_Indexed_Component
3204 and then (Component_Size
(Etype
(Prefix
(Pref
))) /= 0
3205 or else Is_Packed
(Etype
(Prefix
(Pref
)))))
3207 Set_Attribute_Name
(N
, Name_Size
);
3209 -- Otherwise if we have an object rather than a type, then the
3210 -- VADS_Size attribute applies to the type of the object, rather
3211 -- than the object itself. This is one of the respects in which
3212 -- VADS_Size differs from Size.
3215 if (not Is_Entity_Name
(Pref
)
3216 or else not Is_Type
(Entity
(Pref
)))
3217 and then (Is_Scalar_Type
(Etype
(Pref
))
3218 or else Is_Constrained
(Etype
(Pref
)))
3220 Rewrite
(Pref
, New_Occurrence_Of
(Etype
(Pref
), Loc
));
3223 -- For a scalar type for which no size was explicitly given,
3224 -- VADS_Size means Object_Size. This is the other respect in
3225 -- which VADS_Size differs from Size.
3227 if Is_Scalar_Type
(Etype
(Pref
))
3228 and then No
(Size_Clause
(Etype
(Pref
)))
3230 Set_Attribute_Name
(N
, Name_Object_Size
);
3232 -- In all other cases, Size and VADS_Size are the sane
3235 Set_Attribute_Name
(N
, Name_Size
);
3240 -- For class-wide types, X'Class'Size is transformed into a
3241 -- direct reference to the Size of the class type, so that gigi
3242 -- does not have to deal with the X'Class'Size reference.
3244 if Is_Entity_Name
(Pref
)
3245 and then Is_Class_Wide_Type
(Entity
(Pref
))
3247 Rewrite
(Prefix
(N
), New_Occurrence_Of
(Entity
(Pref
), Loc
));
3250 -- For X'Size applied to an object of a class-wide type, transform
3251 -- X'Size into a call to the primitive operation _Size applied to X.
3253 elsif Is_Class_Wide_Type
(Ptyp
) then
3255 Make_Function_Call
(Loc
,
3256 Name
=> New_Reference_To
3257 (Find_Prim_Op
(Ptyp
, Name_uSize
), Loc
),
3258 Parameter_Associations
=> New_List
(Pref
));
3260 if Typ
/= Standard_Long_Long_Integer
then
3262 -- The context is a specific integer type with which the
3263 -- original attribute was compatible. The function has a
3264 -- specific type as well, so to preserve the compatibility
3265 -- we must convert explicitly.
3267 New_Node
:= Convert_To
(Typ
, New_Node
);
3270 Rewrite
(N
, New_Node
);
3271 Analyze_And_Resolve
(N
, Typ
);
3274 -- For an array component, we can do Size in the front end
3275 -- if the component_size of the array is set.
3277 elsif Nkind
(Pref
) = N_Indexed_Component
then
3278 Siz
:= Component_Size
(Etype
(Prefix
(Pref
)));
3280 -- For a record component, we can do Size in the front end if there
3281 -- is a component clause, or if the record is packed and the
3282 -- component's size is known at compile time.
3284 elsif Nkind
(Pref
) = N_Selected_Component
then
3286 Rec
: constant Entity_Id
:= Etype
(Prefix
(Pref
));
3287 Comp
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
3290 if Present
(Component_Clause
(Comp
)) then
3291 Siz
:= Esize
(Comp
);
3293 elsif Is_Packed
(Rec
) then
3294 Siz
:= RM_Size
(Ptyp
);
3297 Apply_Universal_Integer_Attribute_Checks
(N
);
3302 -- All other cases are handled by Gigi
3305 Apply_Universal_Integer_Attribute_Checks
(N
);
3307 -- If Size is applied to a formal parameter that is of a packed
3308 -- array subtype, then apply Size to the actual subtype.
3310 if Is_Entity_Name
(Pref
)
3311 and then Is_Formal
(Entity
(Pref
))
3312 and then Is_Array_Type
(Etype
(Pref
))
3313 and then Is_Packed
(Etype
(Pref
))
3316 Make_Attribute_Reference
(Loc
,
3318 New_Occurrence_Of
(Get_Actual_Subtype
(Pref
), Loc
),
3319 Attribute_Name
=> Name_Size
));
3320 Analyze_And_Resolve
(N
, Typ
);
3323 -- If Size is applied to a dereference of an access to
3324 -- unconstrained packed array, GIGI needs to see its
3325 -- unconstrained nominal type, but also a hint to the actual
3326 -- constrained type.
3328 if Nkind
(Pref
) = N_Explicit_Dereference
3329 and then Is_Array_Type
(Etype
(Pref
))
3330 and then not Is_Constrained
(Etype
(Pref
))
3331 and then Is_Packed
(Etype
(Pref
))
3333 Set_Actual_Designated_Subtype
(Pref
,
3334 Get_Actual_Subtype
(Pref
));
3340 -- Common processing for record and array component case
3343 Rewrite
(N
, Make_Integer_Literal
(Loc
, Siz
));
3345 Analyze_And_Resolve
(N
, Typ
);
3347 -- The result is not a static expression
3349 Set_Is_Static_Expression
(N
, False);
3357 when Attribute_Storage_Pool
=>
3359 Make_Type_Conversion
(Loc
,
3360 Subtype_Mark
=> New_Reference_To
(Etype
(N
), Loc
),
3361 Expression
=> New_Reference_To
(Entity
(N
), Loc
)));
3362 Analyze_And_Resolve
(N
, Typ
);
3368 when Attribute_Storage_Size
=> Storage_Size
:
3370 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3373 -- Access type case, always go to the root type
3375 -- The case of access types results in a value of zero for the case
3376 -- where no storage size attribute clause has been given. If a
3377 -- storage size has been given, then the attribute is converted
3378 -- to a reference to the variable used to hold this value.
3380 if Is_Access_Type
(Ptyp
) then
3381 if Present
(Storage_Size_Variable
(Root_Type
(Ptyp
))) then
3383 Make_Attribute_Reference
(Loc
,
3384 Prefix
=> New_Reference_To
(Typ
, Loc
),
3385 Attribute_Name
=> Name_Max
,
3386 Expressions
=> New_List
(
3387 Make_Integer_Literal
(Loc
, 0),
3390 (Storage_Size_Variable
(Root_Type
(Ptyp
)), Loc
)))));
3392 elsif Present
(Associated_Storage_Pool
(Root_Type
(Ptyp
))) then
3395 Make_Function_Call
(Loc
,
3399 (Etype
(Associated_Storage_Pool
(Root_Type
(Ptyp
))),
3400 Attribute_Name
(N
)),
3403 Parameter_Associations
=> New_List
(New_Reference_To
(
3404 Associated_Storage_Pool
(Root_Type
(Ptyp
)), Loc
)))));
3406 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
3409 Analyze_And_Resolve
(N
, Typ
);
3411 -- The case of a task type (an obsolescent feature) is handled the
3412 -- same way, seems as reasonable as anything, and it is what the
3413 -- ACVC tests (e.g. CD1009K) seem to expect.
3415 -- If there is no Storage_Size variable, then we return the default
3416 -- task stack size, otherwise, expand a Storage_Size attribute as
3419 -- Typ (Adjust_Storage_Size (taskZ))
3421 -- except for the case of a task object which has a Storage_Size
3424 -- Typ (Adjust_Storage_Size (taskV!(name)._Size))
3427 if not Present
(Storage_Size_Variable
(Ptyp
)) then
3430 Make_Function_Call
(Loc
,
3432 New_Occurrence_Of
(RTE
(RE_Default_Stack_Size
), Loc
))));
3435 if not (Is_Entity_Name
(Pref
) and then
3436 Is_Task_Type
(Entity
(Pref
))) and then
3437 Chars
(Last_Entity
(Corresponding_Record_Type
(Ptyp
))) =
3442 Make_Function_Call
(Loc
,
3443 Name
=> New_Occurrence_Of
(
3444 RTE
(RE_Adjust_Storage_Size
), Loc
),
3445 Parameter_Associations
=>
3447 Make_Selected_Component
(Loc
,
3449 Unchecked_Convert_To
(
3450 Corresponding_Record_Type
(Ptyp
),
3451 New_Copy_Tree
(Pref
)),
3453 Make_Identifier
(Loc
, Name_uSize
))))));
3455 -- Task not having Storage_Size pragma
3460 Make_Function_Call
(Loc
,
3461 Name
=> New_Occurrence_Of
(
3462 RTE
(RE_Adjust_Storage_Size
), Loc
),
3463 Parameter_Associations
=>
3466 Storage_Size_Variable
(Ptyp
), Loc
)))));
3469 Analyze_And_Resolve
(N
, Typ
);
3478 when Attribute_Stream_Size
=> Stream_Size
: declare
3479 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3483 -- If we have a Stream_Size clause for this type use it, otherwise
3484 -- the Stream_Size if the size of the type.
3486 if Has_Stream_Size_Clause
(Ptyp
) then
3488 (Static_Integer
(Expression
(Stream_Size_Clause
(Ptyp
))));
3490 Size
:= UI_To_Int
(Esize
(Ptyp
));
3493 Rewrite
(N
, Make_Integer_Literal
(Loc
, Intval
=> Size
));
3494 Analyze_And_Resolve
(N
, Typ
);
3501 -- 1. Deal with enumeration types with holes
3502 -- 2. For floating-point, generate call to attribute function
3503 -- 3. For other cases, deal with constraint checking
3505 when Attribute_Succ
=> Succ
:
3507 Ptyp
: constant Entity_Id
:= Base_Type
(Etype
(Pref
));
3510 -- For enumeration types with non-standard representations, we
3511 -- expand typ'Succ (x) into
3513 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
3515 -- If the representation is contiguous, we compute instead
3516 -- Lit1 + Rep_to_Pos (x+1), to catch invalid representations.
3518 if Is_Enumeration_Type
(Ptyp
)
3519 and then Present
(Enum_Pos_To_Rep
(Ptyp
))
3521 if Has_Contiguous_Rep
(Ptyp
) then
3523 Unchecked_Convert_To
(Ptyp
,
3526 Make_Integer_Literal
(Loc
,
3527 Enumeration_Rep
(First_Literal
(Ptyp
))),
3529 Make_Function_Call
(Loc
,
3532 (TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
3534 Parameter_Associations
=>
3536 Unchecked_Convert_To
(Ptyp
,
3539 Unchecked_Convert_To
(Standard_Integer
,
3540 Relocate_Node
(First
(Exprs
))),
3542 Make_Integer_Literal
(Loc
, 1))),
3543 Rep_To_Pos_Flag
(Ptyp
, Loc
))))));
3545 -- Add Boolean parameter True, to request program errror if
3546 -- we have a bad representation on our hands. Add False if
3547 -- checks are suppressed.
3549 Append_To
(Exprs
, Rep_To_Pos_Flag
(Ptyp
, Loc
));
3551 Make_Indexed_Component
(Loc
,
3552 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Ptyp
), Loc
),
3553 Expressions
=> New_List
(
3556 Make_Function_Call
(Loc
,
3559 (TSS
(Ptyp
, TSS_Rep_To_Pos
), Loc
),
3560 Parameter_Associations
=> Exprs
),
3561 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
3564 Analyze_And_Resolve
(N
, Typ
);
3566 -- For floating-point, we transform 'Succ into a call to the Succ
3567 -- floating-point attribute function in Fat_xxx (xxx is root type)
3569 elsif Is_Floating_Point_Type
(Ptyp
) then
3570 Expand_Fpt_Attribute_R
(N
);
3571 Analyze_And_Resolve
(N
, Typ
);
3573 -- For modular types, nothing to do (no overflow, since wraps)
3575 elsif Is_Modular_Integer_Type
(Ptyp
) then
3578 -- For other types, if range checking is enabled, we must generate
3579 -- a check if overflow checking is enabled.
3581 elsif not Overflow_Checks_Suppressed
(Ptyp
) then
3582 Expand_Pred_Succ
(N
);
3590 -- Transforms X'Tag into a direct reference to the tag of X
3592 when Attribute_Tag
=> Tag
:
3595 Prefix_Is_Type
: Boolean;
3598 if Is_Entity_Name
(Pref
) and then Is_Type
(Entity
(Pref
)) then
3599 Ttyp
:= Entity
(Pref
);
3600 Prefix_Is_Type
:= True;
3602 Ttyp
:= Etype
(Pref
);
3603 Prefix_Is_Type
:= False;
3606 if Is_Class_Wide_Type
(Ttyp
) then
3607 Ttyp
:= Root_Type
(Ttyp
);
3610 Ttyp
:= Underlying_Type
(Ttyp
);
3612 if Prefix_Is_Type
then
3614 -- For JGNAT we leave the type attribute unexpanded because
3615 -- there's not a dispatching table to reference.
3619 Unchecked_Convert_To
(RTE
(RE_Tag
),
3621 (Node
(First_Elmt
(Access_Disp_Table
(Ttyp
))), Loc
)));
3622 Analyze_And_Resolve
(N
, RTE
(RE_Tag
));
3627 Make_Selected_Component
(Loc
,
3628 Prefix
=> Relocate_Node
(Pref
),
3630 New_Reference_To
(First_Tag_Component
(Ttyp
), Loc
)));
3631 Analyze_And_Resolve
(N
, RTE
(RE_Tag
));
3639 -- Transforms 'Terminated attribute into a call to Terminated function
3641 when Attribute_Terminated
=> Terminated
:
3643 -- The prefix of Terminated is of a task interface class-wide type.
3646 -- terminated (Pref._disp_get_task_id);
3648 if Ada_Version
>= Ada_05
3649 and then Ekind
(Etype
(Pref
)) = E_Class_Wide_Type
3650 and then Is_Interface
(Etype
(Pref
))
3651 and then Is_Task_Interface
(Etype
(Pref
))
3654 Make_Function_Call
(Loc
,
3656 New_Reference_To
(RTE
(RE_Terminated
), Loc
),
3657 Parameter_Associations
=> New_List
(
3658 Make_Selected_Component
(Loc
,
3660 New_Copy_Tree
(Pref
),
3662 Make_Identifier
(Loc
, Name_uDisp_Get_Task_Id
)))));
3664 elsif Restricted_Profile
then
3666 Build_Call_With_Task
(Pref
, RTE
(RE_Restricted_Terminated
)));
3670 Build_Call_With_Task
(Pref
, RTE
(RE_Terminated
)));
3673 Analyze_And_Resolve
(N
, Standard_Boolean
);
3680 -- Transforms System'To_Address (X) into unchecked conversion
3681 -- from (integral) type of X to type address.
3683 when Attribute_To_Address
=>
3685 Unchecked_Convert_To
(RTE
(RE_Address
),
3686 Relocate_Node
(First
(Exprs
))));
3687 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
3693 -- Transforms 'Truncation into a call to the floating-point attribute
3694 -- function Truncation in Fat_xxx (where xxx is the root type)
3696 when Attribute_Truncation
=>
3697 Expand_Fpt_Attribute_R
(N
);
3699 -----------------------
3700 -- Unbiased_Rounding --
3701 -----------------------
3703 -- Transforms 'Unbiased_Rounding into a call to the floating-point
3704 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
3707 when Attribute_Unbiased_Rounding
=>
3708 Expand_Fpt_Attribute_R
(N
);
3710 ----------------------
3711 -- Unchecked_Access --
3712 ----------------------
3714 when Attribute_Unchecked_Access
=>
3716 -- Ada 2005 (AI-251): If the designated type is an interface, then
3717 -- rewrite the referenced object as a conversion to force the
3718 -- displacement of the pointer to the secondary dispatch table.
3720 if Is_Interface
(Directly_Designated_Type
(Btyp
)) then
3722 Ref_Object
: constant Node_Id
:= Get_Referenced_Object
(Pref
);
3723 Conversion
: Node_Id
;
3725 Conversion
:= Convert_To
(Typ
, New_Copy_Tree
(Ref_Object
));
3726 Rewrite
(N
, Conversion
);
3727 Analyze_And_Resolve
(N
, Typ
);
3730 -- Otherwise this is like normal Access without a check
3733 Expand_Access_To_Type
(N
);
3740 when Attribute_UET_Address
=> UET_Address
: declare
3741 Ent
: constant Entity_Id
:=
3742 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
3746 Make_Object_Declaration
(Loc
,
3747 Defining_Identifier
=> Ent
,
3748 Aliased_Present
=> True,
3749 Object_Definition
=>
3750 New_Occurrence_Of
(RTE
(RE_Address
), Loc
)));
3752 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
3753 -- in normal external form.
3755 Get_External_Unit_Name_String
(Get_Unit_Name
(Pref
));
3756 Name_Buffer
(1 + 7 .. Name_Len
+ 7) := Name_Buffer
(1 .. Name_Len
);
3757 Name_Len
:= Name_Len
+ 7;
3758 Name_Buffer
(1 .. 7) := "__gnat_";
3759 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 5) := "__SDP";
3760 Name_Len
:= Name_Len
+ 5;
3762 Set_Is_Imported
(Ent
);
3763 Set_Interface_Name
(Ent
,
3764 Make_String_Literal
(Loc
,
3765 Strval
=> String_From_Name_Buffer
));
3768 Make_Attribute_Reference
(Loc
,
3769 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3770 Attribute_Name
=> Name_Address
));
3772 Analyze_And_Resolve
(N
, Typ
);
3775 -------------------------
3776 -- Unrestricted_Access --
3777 -------------------------
3779 when Attribute_Unrestricted_Access
=>
3781 -- Ada 2005 (AI-251): If the designated type is an interface, then
3782 -- rewrite the referenced object as a conversion to force the
3783 -- displacement of the pointer to the secondary dispatch table.
3785 if Is_Interface
(Directly_Designated_Type
(Btyp
)) then
3787 Ref_Object
: constant Node_Id
:= Get_Referenced_Object
(Pref
);
3788 Conversion
: Node_Id
;
3790 Conversion
:= Convert_To
(Typ
, New_Copy_Tree
(Ref_Object
));
3791 Rewrite
(N
, Conversion
);
3792 Analyze_And_Resolve
(N
, Typ
);
3795 -- Otherwise this is like Access without a check
3798 Expand_Access_To_Type
(N
);
3805 -- The processing for VADS_Size is shared with Size
3811 -- For enumeration types with a standard representation, and for all
3812 -- other types, Val is handled by Gigi. For enumeration types with
3813 -- a non-standard representation we use the _Pos_To_Rep array that
3814 -- was created when the type was frozen.
3816 when Attribute_Val
=> Val
:
3818 Etyp
: constant Entity_Id
:= Base_Type
(Entity
(Pref
));
3821 if Is_Enumeration_Type
(Etyp
)
3822 and then Present
(Enum_Pos_To_Rep
(Etyp
))
3824 if Has_Contiguous_Rep
(Etyp
) then
3826 Rep_Node
: constant Node_Id
:=
3827 Unchecked_Convert_To
(Etyp
,
3830 Make_Integer_Literal
(Loc
,
3831 Enumeration_Rep
(First_Literal
(Etyp
))),
3833 (Convert_To
(Standard_Integer
,
3834 Relocate_Node
(First
(Exprs
))))));
3838 Unchecked_Convert_To
(Etyp
,
3841 Make_Integer_Literal
(Loc
,
3842 Enumeration_Rep
(First_Literal
(Etyp
))),
3844 Make_Function_Call
(Loc
,
3847 (TSS
(Etyp
, TSS_Rep_To_Pos
), Loc
),
3848 Parameter_Associations
=> New_List
(
3850 Rep_To_Pos_Flag
(Etyp
, Loc
))))));
3855 Make_Indexed_Component
(Loc
,
3856 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Etyp
), Loc
),
3857 Expressions
=> New_List
(
3858 Convert_To
(Standard_Integer
,
3859 Relocate_Node
(First
(Exprs
))))));
3862 Analyze_And_Resolve
(N
, Typ
);
3870 -- The code for valid is dependent on the particular types involved.
3871 -- See separate sections below for the generated code in each case.
3873 when Attribute_Valid
=> Valid
:
3875 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3876 Btyp
: Entity_Id
:= Base_Type
(Ptyp
);
3879 Save_Validity_Checks_On
: constant Boolean := Validity_Checks_On
;
3880 -- Save the validity checking mode. We always turn off validity
3881 -- checking during process of 'Valid since this is one place
3882 -- where we do not want the implicit validity checks to intefere
3883 -- with the explicit validity check that the programmer is doing.
3885 function Make_Range_Test
return Node_Id
;
3886 -- Build the code for a range test of the form
3887 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
3889 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
3891 ---------------------
3892 -- Make_Range_Test --
3893 ---------------------
3895 function Make_Range_Test
return Node_Id
is
3902 Unchecked_Convert_To
(Btyp
, Duplicate_Subexpr
(Pref
)),
3905 Unchecked_Convert_To
(Btyp
,
3906 Make_Attribute_Reference
(Loc
,
3907 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3908 Attribute_Name
=> Name_First
))),
3913 Unchecked_Convert_To
(Btyp
,
3914 Duplicate_Subexpr_No_Checks
(Pref
)),
3917 Unchecked_Convert_To
(Btyp
,
3918 Make_Attribute_Reference
(Loc
,
3919 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3920 Attribute_Name
=> Name_Last
))));
3921 end Make_Range_Test
;
3923 -- Start of processing for Attribute_Valid
3926 -- Turn off validity checks. We do not want any implicit validity
3927 -- checks to intefere with the explicit check from the attribute
3929 Validity_Checks_On
:= False;
3931 -- Floating-point case. This case is handled by the Valid attribute
3932 -- code in the floating-point attribute run-time library.
3934 if Is_Floating_Point_Type
(Ptyp
) then
3940 -- For vax fpt types, call appropriate routine in special vax
3941 -- floating point unit. We do not have to worry about loads in
3942 -- this case, since these types have no signalling NaN's.
3944 if Vax_Float
(Btyp
) then
3945 Expand_Vax_Valid
(N
);
3947 -- Non VAX float case
3950 Find_Fat_Info
(Etype
(Pref
), Ftp
, Pkg
);
3952 -- If the floating-point object might be unaligned, we need
3953 -- to call the special routine Unaligned_Valid, which makes
3954 -- the needed copy, being careful not to load the value into
3955 -- any floating-point register. The argument in this case is
3956 -- obj'Address (see Unchecked_Valid routine in Fat_Gen).
3958 if Is_Possibly_Unaligned_Object
(Pref
) then
3959 Set_Attribute_Name
(N
, Name_Unaligned_Valid
);
3960 Expand_Fpt_Attribute
3961 (N
, Pkg
, Name_Unaligned_Valid
,
3963 Make_Attribute_Reference
(Loc
,
3964 Prefix
=> Relocate_Node
(Pref
),
3965 Attribute_Name
=> Name_Address
)));
3967 -- In the normal case where we are sure the object is
3968 -- aligned, we generate a call to Valid, and the argument in
3969 -- this case is obj'Unrestricted_Access (after converting
3970 -- obj to the right floating-point type).
3973 Expand_Fpt_Attribute
3974 (N
, Pkg
, Name_Valid
,
3976 Make_Attribute_Reference
(Loc
,
3977 Prefix
=> Unchecked_Convert_To
(Ftp
, Pref
),
3978 Attribute_Name
=> Name_Unrestricted_Access
)));
3982 -- One more task, we still need a range check. Required
3983 -- only if we have a constraint, since the Valid routine
3984 -- catches infinities properly (infinities are never valid).
3986 -- The way we do the range check is simply to create the
3987 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
3989 if not Subtypes_Statically_Match
(Ptyp
, Btyp
) then
3992 Left_Opnd
=> Relocate_Node
(N
),
3995 Left_Opnd
=> Convert_To
(Btyp
, Pref
),
3996 Right_Opnd
=> New_Occurrence_Of
(Ptyp
, Loc
))));
4000 -- Enumeration type with holes
4002 -- For enumeration types with holes, the Pos value constructed by
4003 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
4004 -- second argument of False returns minus one for an invalid value,
4005 -- and the non-negative pos value for a valid value, so the
4006 -- expansion of X'Valid is simply:
4008 -- type(X)'Pos (X) >= 0
4010 -- We can't quite generate it that way because of the requirement
4011 -- for the non-standard second argument of False in the resulting
4012 -- rep_to_pos call, so we have to explicitly create:
4014 -- _rep_to_pos (X, False) >= 0
4016 -- If we have an enumeration subtype, we also check that the
4017 -- value is in range:
4019 -- _rep_to_pos (X, False) >= 0
4021 -- (X >= type(X)'First and then type(X)'Last <= X)
4023 elsif Is_Enumeration_Type
(Ptyp
)
4024 and then Present
(Enum_Pos_To_Rep
(Base_Type
(Ptyp
)))
4029 Make_Function_Call
(Loc
,
4032 (TSS
(Base_Type
(Ptyp
), TSS_Rep_To_Pos
), Loc
),
4033 Parameter_Associations
=> New_List
(
4035 New_Occurrence_Of
(Standard_False
, Loc
))),
4036 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
4040 (Type_Low_Bound
(Ptyp
) /= Type_Low_Bound
(Btyp
)
4042 Type_High_Bound
(Ptyp
) /= Type_High_Bound
(Btyp
))
4044 -- The call to Make_Range_Test will create declarations
4045 -- that need a proper insertion point, but Pref is now
4046 -- attached to a node with no ancestor. Attach to tree
4047 -- even if it is to be rewritten below.
4049 Set_Parent
(Tst
, Parent
(N
));
4053 Left_Opnd
=> Make_Range_Test
,
4059 -- Fortran convention booleans
4061 -- For the very special case of Fortran convention booleans, the
4062 -- value is always valid, since it is an integer with the semantics
4063 -- that non-zero is true, and any value is permissible.
4065 elsif Is_Boolean_Type
(Ptyp
)
4066 and then Convention
(Ptyp
) = Convention_Fortran
4068 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
4070 -- For biased representations, we will be doing an unchecked
4071 -- conversion without unbiasing the result. That means that the range
4072 -- test has to take this into account, and the proper form of the
4075 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
4077 elsif Has_Biased_Representation
(Ptyp
) then
4078 Btyp
:= RTE
(RE_Unsigned_32
);
4082 Unchecked_Convert_To
(Btyp
, Duplicate_Subexpr
(Pref
)),
4084 Unchecked_Convert_To
(Btyp
,
4085 Make_Attribute_Reference
(Loc
,
4086 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
4087 Attribute_Name
=> Name_Range_Length
))));
4089 -- For all other scalar types, what we want logically is a
4092 -- X in type(X)'First .. type(X)'Last
4094 -- But that's precisely what won't work because of possible
4095 -- unwanted optimization (and indeed the basic motivation for
4096 -- the Valid attribute is exactly that this test does not work!)
4097 -- What will work is:
4099 -- Btyp!(X) >= Btyp!(type(X)'First)
4101 -- Btyp!(X) <= Btyp!(type(X)'Last)
4103 -- where Btyp is an integer type large enough to cover the full
4104 -- range of possible stored values (i.e. it is chosen on the basis
4105 -- of the size of the type, not the range of the values). We write
4106 -- this as two tests, rather than a range check, so that static
4107 -- evaluation will easily remove either or both of the checks if
4108 -- they can be -statically determined to be true (this happens
4109 -- when the type of X is static and the range extends to the full
4110 -- range of stored values).
4112 -- Unsigned types. Note: it is safe to consider only whether the
4113 -- subtype is unsigned, since we will in that case be doing all
4114 -- unsigned comparisons based on the subtype range. Since we use the
4115 -- actual subtype object size, this is appropriate.
4117 -- For example, if we have
4119 -- subtype x is integer range 1 .. 200;
4120 -- for x'Object_Size use 8;
4122 -- Now the base type is signed, but objects of this type are bits
4123 -- unsigned, and doing an unsigned test of the range 1 to 200 is
4124 -- correct, even though a value greater than 127 looks signed to a
4125 -- signed comparison.
4127 elsif Is_Unsigned_Type
(Ptyp
) then
4128 if Esize
(Ptyp
) <= 32 then
4129 Btyp
:= RTE
(RE_Unsigned_32
);
4131 Btyp
:= RTE
(RE_Unsigned_64
);
4134 Rewrite
(N
, Make_Range_Test
);
4139 if Esize
(Ptyp
) <= Esize
(Standard_Integer
) then
4140 Btyp
:= Standard_Integer
;
4142 Btyp
:= Universal_Integer
;
4145 Rewrite
(N
, Make_Range_Test
);
4148 Analyze_And_Resolve
(N
, Standard_Boolean
);
4149 Validity_Checks_On
:= Save_Validity_Checks_On
;
4156 -- Value attribute is handled in separate unti Exp_Imgv
4158 when Attribute_Value
=>
4159 Exp_Imgv
.Expand_Value_Attribute
(N
);
4165 -- The processing for Value_Size shares the processing for Size
4171 -- The processing for Version shares the processing for Body_Version
4177 -- We expand typ'Wide_Image (X) into
4179 -- String_To_Wide_String
4180 -- (typ'Image (X), Wide_Character_Encoding_Method)
4182 -- This works in all cases because String_To_Wide_String converts any
4183 -- wide character escape sequences resulting from the Image call to the
4184 -- proper Wide_Character equivalent
4186 -- not quite right for typ = Wide_Character ???
4188 when Attribute_Wide_Image
=> Wide_Image
:
4191 Make_Function_Call
(Loc
,
4192 Name
=> New_Reference_To
(RTE
(RE_String_To_Wide_String
), Loc
),
4193 Parameter_Associations
=> New_List
(
4194 Make_Attribute_Reference
(Loc
,
4196 Attribute_Name
=> Name_Image
,
4197 Expressions
=> Exprs
),
4199 Make_Integer_Literal
(Loc
,
4200 Intval
=> Int
(Wide_Character_Encoding_Method
)))));
4202 Analyze_And_Resolve
(N
, Standard_Wide_String
);
4205 ---------------------
4206 -- Wide_Wide_Image --
4207 ---------------------
4209 -- We expand typ'Wide_Wide_Image (X) into
4211 -- String_To_Wide_Wide_String
4212 -- (typ'Image (X), Wide_Character_Encoding_Method)
4214 -- This works in all cases because String_To_Wide_Wide_String converts
4215 -- any wide character escape sequences resulting from the Image call to
4216 -- the proper Wide_Character equivalent
4218 -- not quite right for typ = Wide_Wide_Character ???
4220 when Attribute_Wide_Wide_Image
=> Wide_Wide_Image
:
4223 Make_Function_Call
(Loc
,
4224 Name
=> New_Reference_To
4225 (RTE
(RE_String_To_Wide_Wide_String
), Loc
),
4226 Parameter_Associations
=> New_List
(
4227 Make_Attribute_Reference
(Loc
,
4229 Attribute_Name
=> Name_Image
,
4230 Expressions
=> Exprs
),
4232 Make_Integer_Literal
(Loc
,
4233 Intval
=> Int
(Wide_Character_Encoding_Method
)))));
4235 Analyze_And_Resolve
(N
, Standard_Wide_Wide_String
);
4236 end Wide_Wide_Image
;
4242 -- We expand typ'Wide_Value (X) into
4245 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
4247 -- Wide_String_To_String is a runtime function that converts its wide
4248 -- string argument to String, converting any non-translatable characters
4249 -- into appropriate escape sequences. This preserves the required
4250 -- semantics of Wide_Value in all cases, and results in a very simple
4251 -- implementation approach.
4253 -- It's not quite right where typ = Wide_Character, because the encoding
4254 -- method may not cover the whole character type ???
4256 when Attribute_Wide_Value
=> Wide_Value
:
4259 Make_Attribute_Reference
(Loc
,
4261 Attribute_Name
=> Name_Value
,
4263 Expressions
=> New_List
(
4264 Make_Function_Call
(Loc
,
4266 New_Reference_To
(RTE
(RE_Wide_String_To_String
), Loc
),
4268 Parameter_Associations
=> New_List
(
4269 Relocate_Node
(First
(Exprs
)),
4270 Make_Integer_Literal
(Loc
,
4271 Intval
=> Int
(Wide_Character_Encoding_Method
)))))));
4273 Analyze_And_Resolve
(N
, Typ
);
4276 ---------------------
4277 -- Wide_Wide_Value --
4278 ---------------------
4280 -- We expand typ'Wide_Value_Value (X) into
4283 -- (Wide_Wide_String_To_String (X, Wide_Character_Encoding_Method))
4285 -- Wide_Wide_String_To_String is a runtime function that converts its
4286 -- wide string argument to String, converting any non-translatable
4287 -- characters into appropriate escape sequences. This preserves the
4288 -- required semantics of Wide_Wide_Value in all cases, and results in a
4289 -- very simple implementation approach.
4291 -- It's not quite right where typ = Wide_Wide_Character, because the
4292 -- encoding method may not cover the whole character type ???
4294 when Attribute_Wide_Wide_Value
=> Wide_Wide_Value
:
4297 Make_Attribute_Reference
(Loc
,
4299 Attribute_Name
=> Name_Value
,
4301 Expressions
=> New_List
(
4302 Make_Function_Call
(Loc
,
4304 New_Reference_To
(RTE
(RE_Wide_Wide_String_To_String
), Loc
),
4306 Parameter_Associations
=> New_List
(
4307 Relocate_Node
(First
(Exprs
)),
4308 Make_Integer_Literal
(Loc
,
4309 Intval
=> Int
(Wide_Character_Encoding_Method
)))))));
4311 Analyze_And_Resolve
(N
, Typ
);
4312 end Wide_Wide_Value
;
4314 ---------------------
4315 -- Wide_Wide_Width --
4316 ---------------------
4318 -- Wide_Wide_Width attribute is handled in separate unit Exp_Imgv
4320 when Attribute_Wide_Wide_Width
=>
4321 Exp_Imgv
.Expand_Width_Attribute
(N
, Wide_Wide
);
4327 -- Wide_Width attribute is handled in separate unit Exp_Imgv
4329 when Attribute_Wide_Width
=>
4330 Exp_Imgv
.Expand_Width_Attribute
(N
, Wide
);
4336 -- Width attribute is handled in separate unit Exp_Imgv
4338 when Attribute_Width
=>
4339 Exp_Imgv
.Expand_Width_Attribute
(N
, Normal
);
4345 when Attribute_Write
=> Write
: declare
4346 P_Type
: constant Entity_Id
:= Entity
(Pref
);
4347 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
4355 -- If no underlying type, we have an error that will be diagnosed
4356 -- elsewhere, so here we just completely ignore the expansion.
4362 -- The simple case, if there is a TSS for Write, just call it
4364 Pname
:= Find_Stream_Subprogram
(P_Type
, TSS_Stream_Write
);
4366 if Present
(Pname
) then
4370 -- If there is a Stream_Convert pragma, use it, we rewrite
4372 -- sourcetyp'Output (stream, Item)
4376 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
4378 -- where strmwrite is the given Write function that converts an
4379 -- argument of type sourcetyp or a type acctyp, from which it is
4380 -- derived to type strmtyp. The conversion to acttyp is required
4381 -- for the derived case.
4383 Prag
:= Get_Stream_Convert_Pragma
(P_Type
);
4385 if Present
(Prag
) then
4387 Next
(Next
(First
(Pragma_Argument_Associations
(Prag
))));
4388 Wfunc
:= Entity
(Expression
(Arg3
));
4391 Make_Attribute_Reference
(Loc
,
4392 Prefix
=> New_Occurrence_Of
(Etype
(Wfunc
), Loc
),
4393 Attribute_Name
=> Name_Output
,
4394 Expressions
=> New_List
(
4395 Relocate_Node
(First
(Exprs
)),
4396 Make_Function_Call
(Loc
,
4397 Name
=> New_Occurrence_Of
(Wfunc
, Loc
),
4398 Parameter_Associations
=> New_List
(
4399 Convert_To
(Etype
(First_Formal
(Wfunc
)),
4400 Relocate_Node
(Next
(First
(Exprs
)))))))));
4405 -- For elementary types, we call the W_xxx routine directly
4407 elsif Is_Elementary_Type
(U_Type
) then
4408 Rewrite
(N
, Build_Elementary_Write_Call
(N
));
4414 elsif Is_Array_Type
(U_Type
) then
4415 Build_Array_Write_Procedure
(N
, U_Type
, Decl
, Pname
);
4416 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
4418 -- Tagged type case, use the primitive Write function. Note that
4419 -- this will dispatch in the class-wide case which is what we want
4421 elsif Is_Tagged_Type
(U_Type
) then
4422 Pname
:= Find_Prim_Op
(U_Type
, TSS_Stream_Write
);
4424 -- All other record type cases, including protected records.
4425 -- The latter only arise for expander generated code for
4426 -- handling shared passive partition access.
4430 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
4432 -- Ada 2005 (AI-216): Program_Error is raised when executing
4433 -- the default implementation of the Write attribute of an
4434 -- Unchecked_Union type.
4436 if Is_Unchecked_Union
(Base_Type
(U_Type
)) then
4438 Make_Raise_Program_Error
(Loc
,
4439 Reason
=> PE_Unchecked_Union_Restriction
));
4442 if Has_Discriminants
(U_Type
)
4444 (Discriminant_Default_Value
(First_Discriminant
(U_Type
)))
4446 Build_Mutable_Record_Write_Procedure
4447 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
4449 Build_Record_Write_Procedure
4450 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
4453 Insert_Action
(N
, Decl
);
4457 -- If we fall through, Pname is the procedure to be called
4459 Rewrite_Stream_Proc_Call
(Pname
);
4462 -- Component_Size is handled by Gigi, unless the component size is known
4463 -- at compile time, which is always true in the packed array case. It is
4464 -- important that the packed array case is handled in the front end (see
4465 -- Eval_Attribute) since Gigi would otherwise get confused by the
4466 -- equivalent packed array type.
4468 when Attribute_Component_Size
=>
4471 -- The following attributes are handled by Gigi (except that static
4472 -- cases have already been evaluated by the semantics, but in any case
4473 -- Gigi should not count on that).
4475 -- In addition Gigi handles the non-floating-point cases of Pred and
4476 -- Succ (including the fixed-point cases, which can just be treated as
4477 -- integer increment/decrement operations)
4479 -- Gigi also handles the non-class-wide cases of Size
4481 when Attribute_Bit_Order |
4482 Attribute_Code_Address |
4483 Attribute_Definite |
4485 Attribute_Mechanism_Code |
4487 Attribute_Null_Parameter |
4488 Attribute_Passed_By_Reference |
4489 Attribute_Pool_Address
=>
4492 -- The following attributes are also handled by Gigi, but return a
4493 -- universal integer result, so may need a conversion for checking
4494 -- that the result is in range.
4496 when Attribute_Aft |
4498 Attribute_Max_Size_In_Storage_Elements
4500 Apply_Universal_Integer_Attribute_Checks
(N
);
4502 -- The following attributes should not appear at this stage, since they
4503 -- have already been handled by the analyzer (and properly rewritten
4504 -- with corresponding values or entities to represent the right values)
4506 when Attribute_Abort_Signal |
4507 Attribute_Address_Size |
4510 Attribute_Default_Bit_Order |
4516 Attribute_Has_Access_Values |
4517 Attribute_Has_Discriminants |
4519 Attribute_Machine_Emax |
4520 Attribute_Machine_Emin |
4521 Attribute_Machine_Mantissa |
4522 Attribute_Machine_Overflows |
4523 Attribute_Machine_Radix |
4524 Attribute_Machine_Rounds |
4525 Attribute_Maximum_Alignment |
4526 Attribute_Model_Emin |
4527 Attribute_Model_Epsilon |
4528 Attribute_Model_Mantissa |
4529 Attribute_Model_Small |
4531 Attribute_Partition_ID |
4533 Attribute_Safe_Emax |
4534 Attribute_Safe_First |
4535 Attribute_Safe_Large |
4536 Attribute_Safe_Last |
4537 Attribute_Safe_Small |
4539 Attribute_Signed_Zeros |
4541 Attribute_Storage_Unit |
4542 Attribute_Target_Name |
4543 Attribute_Type_Class |
4544 Attribute_Unconstrained_Array |
4545 Attribute_Universal_Literal_String |
4546 Attribute_Wchar_T_Size |
4547 Attribute_Word_Size
=>
4549 raise Program_Error
;
4551 -- The Asm_Input and Asm_Output attributes are not expanded at this
4552 -- stage, but will be eliminated in the expansion of the Asm call,
4553 -- see Exp_Intr for details. So Gigi will never see these either.
4555 when Attribute_Asm_Input |
4556 Attribute_Asm_Output
=>
4563 when RE_Not_Available
=>
4565 end Expand_N_Attribute_Reference
;
4567 ----------------------
4568 -- Expand_Pred_Succ --
4569 ----------------------
4571 -- For typ'Pred (exp), we generate the check
4573 -- [constraint_error when exp = typ'Base'First]
4575 -- Similarly, for typ'Succ (exp), we generate the check
4577 -- [constraint_error when exp = typ'Base'Last]
4579 -- These checks are not generated for modular types, since the proper
4580 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
4582 procedure Expand_Pred_Succ
(N
: Node_Id
) is
4583 Loc
: constant Source_Ptr
:= Sloc
(N
);
4587 if Attribute_Name
(N
) = Name_Pred
then
4594 Make_Raise_Constraint_Error
(Loc
,
4598 Duplicate_Subexpr_Move_Checks
(First
(Expressions
(N
))),
4600 Make_Attribute_Reference
(Loc
,
4602 New_Reference_To
(Base_Type
(Etype
(Prefix
(N
))), Loc
),
4603 Attribute_Name
=> Cnam
)),
4604 Reason
=> CE_Overflow_Check_Failed
));
4605 end Expand_Pred_Succ
;
4611 procedure Find_Fat_Info
4613 Fat_Type
: out Entity_Id
;
4614 Fat_Pkg
: out RE_Id
)
4616 Btyp
: constant Entity_Id
:= Base_Type
(T
);
4617 Rtyp
: constant Entity_Id
:= Root_Type
(T
);
4618 Digs
: constant Nat
:= UI_To_Int
(Digits_Value
(Btyp
));
4621 -- If the base type is VAX float, then get appropriate VAX float type
4623 if Vax_Float
(Btyp
) then
4626 Fat_Type
:= RTE
(RE_Fat_VAX_F
);
4627 Fat_Pkg
:= RE_Attr_VAX_F_Float
;
4630 Fat_Type
:= RTE
(RE_Fat_VAX_D
);
4631 Fat_Pkg
:= RE_Attr_VAX_D_Float
;
4634 Fat_Type
:= RTE
(RE_Fat_VAX_G
);
4635 Fat_Pkg
:= RE_Attr_VAX_G_Float
;
4638 raise Program_Error
;
4641 -- If root type is VAX float, this is the case where the library has
4642 -- been recompiled in VAX float mode, and we have an IEEE float type.
4643 -- This is when we use the special IEEE Fat packages.
4645 elsif Vax_Float
(Rtyp
) then
4648 Fat_Type
:= RTE
(RE_Fat_IEEE_Short
);
4649 Fat_Pkg
:= RE_Attr_IEEE_Short
;
4652 Fat_Type
:= RTE
(RE_Fat_IEEE_Long
);
4653 Fat_Pkg
:= RE_Attr_IEEE_Long
;
4656 raise Program_Error
;
4659 -- If neither the base type nor the root type is VAX_Float then VAX
4660 -- float is out of the picture, and we can just use the root type.
4665 if Fat_Type
= Standard_Short_Float
then
4666 Fat_Pkg
:= RE_Attr_Short_Float
;
4667 elsif Fat_Type
= Standard_Float
then
4668 Fat_Pkg
:= RE_Attr_Float
;
4669 elsif Fat_Type
= Standard_Long_Float
then
4670 Fat_Pkg
:= RE_Attr_Long_Float
;
4671 elsif Fat_Type
= Standard_Long_Long_Float
then
4672 Fat_Pkg
:= RE_Attr_Long_Long_Float
;
4674 raise Program_Error
;
4679 ----------------------------
4680 -- Find_Stream_Subprogram --
4681 ----------------------------
4683 function Find_Stream_Subprogram
4685 Nam
: TSS_Name_Type
) return Entity_Id
4687 Ent
: constant Entity_Id
:= TSS
(Typ
, Nam
);
4689 if Present
(Ent
) then
4693 if Is_Tagged_Type
(Typ
)
4694 and then Is_Derived_Type
(Typ
)
4696 return Find_Prim_Op
(Typ
, Nam
);
4698 return Find_Inherited_TSS
(Typ
, Nam
);
4700 end Find_Stream_Subprogram
;
4702 -----------------------
4703 -- Get_Index_Subtype --
4704 -----------------------
4706 function Get_Index_Subtype
(N
: Node_Id
) return Node_Id
is
4707 P_Type
: Entity_Id
:= Etype
(Prefix
(N
));
4712 if Is_Access_Type
(P_Type
) then
4713 P_Type
:= Designated_Type
(P_Type
);
4716 if No
(Expressions
(N
)) then
4719 J
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
4722 Indx
:= First_Index
(P_Type
);
4728 return Etype
(Indx
);
4729 end Get_Index_Subtype
;
4731 -------------------------------
4732 -- Get_Stream_Convert_Pragma --
4733 -------------------------------
4735 function Get_Stream_Convert_Pragma
(T
: Entity_Id
) return Node_Id
is
4740 -- Note: we cannot use Get_Rep_Pragma here because of the peculiarity
4741 -- that a stream convert pragma for a tagged type is not inherited from
4742 -- its parent. Probably what is wrong here is that it is basically
4743 -- incorrect to consider a stream convert pragma to be a representation
4744 -- pragma at all ???
4746 N
:= First_Rep_Item
(Implementation_Base_Type
(T
));
4747 while Present
(N
) loop
4748 if Nkind
(N
) = N_Pragma
and then Chars
(N
) = Name_Stream_Convert
then
4750 -- For tagged types this pragma is not inherited, so we
4751 -- must verify that it is defined for the given type and
4755 Entity
(Expression
(First
(Pragma_Argument_Associations
(N
))));
4757 if not Is_Tagged_Type
(T
)
4759 or else (Is_Private_Type
(Typ
) and then T
= Full_View
(Typ
))
4769 end Get_Stream_Convert_Pragma
;
4771 ---------------------------------
4772 -- Is_Constrained_Packed_Array --
4773 ---------------------------------
4775 function Is_Constrained_Packed_Array
(Typ
: Entity_Id
) return Boolean is
4776 Arr
: Entity_Id
:= Typ
;
4779 if Is_Access_Type
(Arr
) then
4780 Arr
:= Designated_Type
(Arr
);
4783 return Is_Array_Type
(Arr
)
4784 and then Is_Constrained
(Arr
)
4785 and then Present
(Packed_Array_Type
(Arr
));
4786 end Is_Constrained_Packed_Array
;