1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree
; use Atree
;
30 with Checks
; use Checks
;
31 with Einfo
; use Einfo
;
32 with Exp_Ch2
; use Exp_Ch2
;
33 with Exp_Ch9
; use Exp_Ch9
;
34 with Exp_Imgv
; use Exp_Imgv
;
35 with Exp_Pakd
; use Exp_Pakd
;
36 with Exp_Strm
; use Exp_Strm
;
37 with Exp_Tss
; use Exp_Tss
;
38 with Exp_Util
; use Exp_Util
;
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 Rtsfind
; use Rtsfind
;
49 with Sem_Ch7
; use Sem_Ch7
;
50 with Sem_Ch8
; use Sem_Ch8
;
51 with Sem_Ch13
; use Sem_Ch13
;
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
90 -- This procedure expands a call to a floating-point attribute function.
91 -- N is the attribute reference node, and Args is a list of arguments to
92 -- be passed to the function call. Rtp is the root type of the floating
93 -- point type involved (used to select the proper generic instantiation
94 -- of the package containing the attribute routines).
96 procedure Expand_Fpt_Attribute_R
(N
: Node_Id
);
97 -- This procedure expands a call to a floating-point attribute function
98 -- that takes a single floating-point argument.
100 procedure Expand_Fpt_Attribute_RI
(N
: Node_Id
);
101 -- This procedure expands a call to a floating-point attribute function
102 -- that takes one floating-point argument and one integer argument.
104 procedure Expand_Fpt_Attribute_RR
(N
: Node_Id
);
105 -- This procedure expands a call to a floating-point attribute function
106 -- that takes two floating-point arguments.
108 procedure Expand_Pred_Succ
(N
: Node_Id
);
109 -- Handles expansion of Pred or Succ attributes for case of non-real
110 -- operand with overflow checking required.
112 function Get_Index_Subtype
(N
: Node_Id
) return Entity_Id
;
113 -- Used for Last, Last, and Length, when the prefix is an array type,
114 -- Obtains the corresponding index subtype.
116 procedure Expand_Access_To_Type
(N
: Node_Id
);
117 -- A reference to a type within its own scope is resolved to a reference
118 -- to the current instance of the type in its initialization procedure.
120 function Find_Inherited_TSS
122 Nam
: Name_Id
) return Entity_Id
;
124 function Is_Constrained_Packed_Array
(Typ
: Entity_Id
) return Boolean;
125 -- Utility for array attributes, returns true on packed constrained
126 -- arrays, and on access to same.
128 ----------------------------------
129 -- Compile_Stream_Body_In_Scope --
130 ----------------------------------
132 procedure Compile_Stream_Body_In_Scope
138 Installed
: Boolean := False;
139 Scop
: constant Entity_Id
:= Scope
(Arr
);
140 Curr
: constant Entity_Id
:= Current_Scope
;
144 and then not In_Open_Scopes
(Scop
)
145 and then Ekind
(Scop
) = E_Package
148 Install_Visible_Declarations
(Scop
);
149 Install_Private_Declarations
(Scop
);
152 -- The entities in the package are now visible, but the generated
153 -- stream entity must appear in the current scope (usually an
154 -- enclosing stream function) so that itypes all have their proper
161 Insert_Action
(N
, Decl
);
163 Insert_Action
(N
, Decl
, All_Checks
);
168 -- Remove extra copy of current scope, and package itself
171 End_Package_Scope
(Scop
);
173 end Compile_Stream_Body_In_Scope
;
175 ---------------------------
176 -- Expand_Access_To_Type --
177 ---------------------------
179 procedure Expand_Access_To_Type
(N
: Node_Id
) is
180 Loc
: constant Source_Ptr
:= Sloc
(N
);
181 Typ
: constant Entity_Id
:= Etype
(N
);
182 Pref
: constant Node_Id
:= Prefix
(N
);
187 if Is_Entity_Name
(Pref
)
188 and then Is_Type
(Entity
(Pref
))
190 -- If the current instance name denotes a task type,
191 -- then the access attribute is rewritten to be the
192 -- name of the "_task" parameter associated with the
193 -- task type's task body procedure. An unchecked
194 -- conversion is applied to ensure a type match in
195 -- cases of expander-generated calls (e.g., init procs).
197 if Is_Task_Type
(Entity
(Pref
)) then
199 First_Entity
(Get_Task_Body_Procedure
(Entity
(Pref
)));
201 while Present
(Formal
) loop
202 exit when Chars
(Formal
) = Name_uTask
;
203 Next_Entity
(Formal
);
206 pragma Assert
(Present
(Formal
));
209 Unchecked_Convert_To
(Typ
, New_Occurrence_Of
(Formal
, Loc
)));
212 -- The expression must appear in a default expression,
213 -- (which in the initialization procedure is the rhs of
214 -- an assignment), and not in a discriminant constraint.
219 while Present
(Par
) loop
220 exit when Nkind
(Par
) = N_Assignment_Statement
;
222 if Nkind
(Par
) = N_Component_Declaration
then
229 if Present
(Par
) then
231 Make_Attribute_Reference
(Loc
,
232 Prefix
=> Make_Identifier
(Loc
, Name_uInit
),
233 Attribute_Name
=> Attribute_Name
(N
)));
235 Analyze_And_Resolve
(N
, Typ
);
239 end Expand_Access_To_Type
;
241 --------------------------
242 -- Expand_Fpt_Attribute --
243 --------------------------
245 procedure Expand_Fpt_Attribute
250 Loc
: constant Source_Ptr
:= Sloc
(N
);
251 Typ
: constant Entity_Id
:= Etype
(N
);
256 -- The function name is the selected component Fat_xxx.yyy where xxx
257 -- is the floating-point root type, and yyy is the attribute name
259 -- Note: it would be more usual to have separate RE entries for each
260 -- of the entities in the Fat packages, but first they have identical
261 -- names (so we would have to have lots of renaming declarations to
262 -- meet the normal RE rule of separate names for all runtime entities),
263 -- and second there would be an awful lot of them!
265 if Rtp
= Standard_Short_Float
then
266 Pkg
:= RE_Fat_Short_Float
;
267 elsif Rtp
= Standard_Float
then
269 elsif Rtp
= Standard_Long_Float
then
270 Pkg
:= RE_Fat_Long_Float
;
272 Pkg
:= RE_Fat_Long_Long_Float
;
276 Make_Selected_Component
(Loc
,
277 Prefix
=> New_Reference_To
(RTE
(Pkg
), Loc
),
278 Selector_Name
=> Make_Identifier
(Loc
, Attribute_Name
(N
)));
280 -- The generated call is given the provided set of parameters, and then
281 -- wrapped in a conversion which converts the result to the target type
284 Unchecked_Convert_To
(Etype
(N
),
285 Make_Function_Call
(Loc
,
287 Parameter_Associations
=> Args
)));
289 Analyze_And_Resolve
(N
, Typ
);
291 end Expand_Fpt_Attribute
;
293 ----------------------------
294 -- Expand_Fpt_Attribute_R --
295 ----------------------------
297 -- The single argument is converted to its root type to call the
298 -- appropriate runtime function, with the actual call being built
299 -- by Expand_Fpt_Attribute
301 procedure Expand_Fpt_Attribute_R
(N
: Node_Id
) is
302 E1
: constant Node_Id
:= First
(Expressions
(N
));
303 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(E1
));
306 Expand_Fpt_Attribute
(N
, Rtp
, New_List
(
307 Unchecked_Convert_To
(Rtp
, Relocate_Node
(E1
))));
308 end Expand_Fpt_Attribute_R
;
310 -----------------------------
311 -- Expand_Fpt_Attribute_RI --
312 -----------------------------
314 -- The first argument is converted to its root type and the second
315 -- argument is converted to standard long long integer to call the
316 -- appropriate runtime function, with the actual call being built
317 -- by Expand_Fpt_Attribute
319 procedure Expand_Fpt_Attribute_RI
(N
: Node_Id
) is
320 E1
: constant Node_Id
:= First
(Expressions
(N
));
321 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(E1
));
322 E2
: constant Node_Id
:= Next
(E1
);
325 Expand_Fpt_Attribute
(N
, Rtp
, New_List
(
326 Unchecked_Convert_To
(Rtp
, Relocate_Node
(E1
)),
327 Unchecked_Convert_To
(Standard_Integer
, Relocate_Node
(E2
))));
328 end Expand_Fpt_Attribute_RI
;
330 -----------------------------
331 -- Expand_Fpt_Attribute_RR --
332 -----------------------------
334 -- The two arguments is converted to their root types to call the
335 -- appropriate runtime function, with the actual call being built
336 -- by Expand_Fpt_Attribute
338 procedure Expand_Fpt_Attribute_RR
(N
: Node_Id
) is
339 E1
: constant Node_Id
:= First
(Expressions
(N
));
340 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(E1
));
341 E2
: constant Node_Id
:= Next
(E1
);
344 Expand_Fpt_Attribute
(N
, Rtp
, New_List
(
345 Unchecked_Convert_To
(Rtp
, Relocate_Node
(E1
)),
346 Unchecked_Convert_To
(Rtp
, Relocate_Node
(E2
))));
347 end Expand_Fpt_Attribute_RR
;
349 ----------------------------------
350 -- Expand_N_Attribute_Reference --
351 ----------------------------------
353 procedure Expand_N_Attribute_Reference
(N
: Node_Id
) is
354 Loc
: constant Source_Ptr
:= Sloc
(N
);
355 Typ
: constant Entity_Id
:= Etype
(N
);
356 Btyp
: constant Entity_Id
:= Base_Type
(Typ
);
357 Pref
: constant Node_Id
:= Prefix
(N
);
358 Exprs
: constant List_Id
:= Expressions
(N
);
359 Id
: constant Attribute_Id
:= Get_Attribute_Id
(Attribute_Name
(N
));
361 procedure Rewrite_Stream_Proc_Call
(Pname
: Entity_Id
);
362 -- Rewrites a stream attribute for Read, Write or Output with the
363 -- procedure call. Pname is the entity for the procedure to call.
365 ------------------------------
366 -- Rewrite_Stream_Proc_Call --
367 ------------------------------
369 procedure Rewrite_Stream_Proc_Call
(Pname
: Entity_Id
) is
370 Item
: constant Node_Id
:= Next
(First
(Exprs
));
371 Formal_Typ
: constant Entity_Id
:=
372 Etype
(Next_Formal
(First_Formal
(Pname
)));
375 -- We have to worry about the type of the second argument
377 -- For the class-wide dispatching cases, and for cases in which
378 -- the base type of the second argument matches the base type of
379 -- the corresponding formal parameter, we are all set, and can use
380 -- the argument unchanged.
382 -- For all other cases we do an unchecked conversion of the second
383 -- parameter to the type of the formal of the procedure we are
384 -- calling. This deals with the private type cases, and with going
385 -- to the root type as required in elementary type case.
387 if not Is_Class_Wide_Type
(Entity
(Pref
))
388 and then Base_Type
(Etype
(Item
)) /= Base_Type
(Formal_Typ
)
391 Unchecked_Convert_To
(Formal_Typ
, Relocate_Node
(Item
)));
393 -- For untagged derived types set Assignment_OK, to prevent
394 -- copies from being created when the unchecked conversion
395 -- is expanded (which would happen in Remove_Side_Effects
396 -- if Expand_N_Unchecked_Conversion were allowed to call
397 -- Force_Evaluation). The copy could violate Ada semantics
398 -- in cases such as an actual that is an out parameter.
399 -- Note that this approach is also used in exp_ch7 for calls
400 -- to controlled type operations to prevent problems with
401 -- actuals wrapped in unchecked conversions.
403 if Is_Untagged_Derivation
(Etype
(Expression
(Item
))) then
404 Set_Assignment_OK
(Item
);
408 -- And now rewrite the call
411 Make_Procedure_Call_Statement
(Loc
,
412 Name
=> New_Occurrence_Of
(Pname
, Loc
),
413 Parameter_Associations
=> Exprs
));
416 end Rewrite_Stream_Proc_Call
;
418 -- Start of processing for Expand_N_Attribute_Reference
421 -- Do required validity checking
423 if Validity_Checks_On
and Validity_Check_Operands
then
428 Expr
:= First
(Expressions
(N
));
429 while Present
(Expr
) loop
436 -- Remaining processing depends on specific attribute
444 when Attribute_Access
=>
446 if Ekind
(Btyp
) = E_Access_Protected_Subprogram_Type
then
448 -- The value of the attribute_reference is a record containing
449 -- two fields: an access to the protected object, and an access
450 -- to the subprogram itself. The prefix is a selected component.
455 E_T
: constant Entity_Id
:= Equivalent_Type
(Typ
);
456 Acc
: constant Entity_Id
:=
457 Etype
(Next_Component
(First_Component
(E_T
)));
462 -- Within the body of the protected type, the prefix
463 -- designates a local operation, and the object is the first
464 -- parameter of the corresponding protected body of the
465 -- current enclosing operation.
467 if Is_Entity_Name
(Pref
) then
468 pragma Assert
(In_Open_Scopes
(Scope
(Entity
(Pref
))));
471 (Protected_Body_Subprogram
(Entity
(Pref
)), Loc
);
472 Curr
:= Current_Scope
;
474 while Scope
(Curr
) /= Scope
(Entity
(Pref
)) loop
475 Curr
:= Scope
(Curr
);
479 Make_Attribute_Reference
(Loc
,
483 (Protected_Body_Subprogram
(Curr
)), Loc
),
484 Attribute_Name
=> Name_Address
);
486 -- Case where the prefix is not an entity name. Find the
487 -- version of the protected operation to be called from
488 -- outside the protected object.
494 (Entity
(Selector_Name
(Pref
))), Loc
);
497 Make_Attribute_Reference
(Loc
,
498 Prefix
=> Relocate_Node
(Prefix
(Pref
)),
499 Attribute_Name
=> Name_Address
);
507 Unchecked_Convert_To
(Acc
,
508 Make_Attribute_Reference
(Loc
,
510 Attribute_Name
=> Name_Address
))));
514 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
516 -- For subsequent analysis, the node must retain its type.
517 -- The backend will replace it with the equivalent type where
523 elsif Ekind
(Btyp
) = E_General_Access_Type
then
525 Ref_Object
: constant Node_Id
:= Get_Referenced_Object
(Pref
);
526 Parm_Ent
: Entity_Id
;
527 Conversion
: Node_Id
;
530 -- If the prefix of an Access attribute is a dereference of an
531 -- access parameter (or a renaming of such a dereference) and
532 -- the context is a general access type (but not an anonymous
533 -- access type), then rewrite the attribute as a conversion of
534 -- the access parameter to the context access type. This will
535 -- result in an accessibility check being performed, if needed.
537 -- (X.all'Access => Acc_Type (X))
539 if Nkind
(Ref_Object
) = N_Explicit_Dereference
540 and then Is_Entity_Name
(Prefix
(Ref_Object
))
542 Parm_Ent
:= Entity
(Prefix
(Ref_Object
));
544 if Ekind
(Parm_Ent
) in Formal_Kind
545 and then Ekind
(Etype
(Parm_Ent
)) = E_Anonymous_Access_Type
546 and then Present
(Extra_Accessibility
(Parm_Ent
))
549 Convert_To
(Typ
, New_Copy_Tree
(Prefix
(Ref_Object
)));
551 Rewrite
(N
, Conversion
);
552 Analyze_And_Resolve
(N
, Typ
);
557 -- If the prefix is a type name, this is a reference to the current
558 -- instance of the type, within its initialization procedure.
561 Expand_Access_To_Type
(N
);
568 -- Transforms 'Adjacent into a call to the floating-point attribute
569 -- function Adjacent in Fat_xxx (where xxx is the root type)
571 when Attribute_Adjacent
=>
572 Expand_Fpt_Attribute_RR
(N
);
578 when Attribute_Address
=> Address
: declare
579 Task_Proc
: Entity_Id
;
582 -- If the prefix is a task or a task type, the useful address
583 -- is that of the procedure for the task body, i.e. the actual
584 -- program unit. We replace the original entity with that of
587 if Is_Entity_Name
(Pref
)
588 and then Is_Task_Type
(Entity
(Pref
))
590 Task_Proc
:= Next_Entity
(Root_Type
(Etype
(Pref
)));
592 while Present
(Task_Proc
) loop
593 exit when Ekind
(Task_Proc
) = E_Procedure
594 and then Etype
(First_Formal
(Task_Proc
)) =
595 Corresponding_Record_Type
(Etype
(Pref
));
596 Next_Entity
(Task_Proc
);
599 if Present
(Task_Proc
) then
600 Set_Entity
(Pref
, Task_Proc
);
601 Set_Etype
(Pref
, Etype
(Task_Proc
));
604 -- Similarly, the address of a protected operation is the address
605 -- of the corresponding protected body, regardless of the protected
606 -- object from which it is selected.
608 elsif Nkind
(Pref
) = N_Selected_Component
609 and then Is_Subprogram
(Entity
(Selector_Name
(Pref
)))
610 and then Is_Protected_Type
(Scope
(Entity
(Selector_Name
(Pref
))))
614 External_Subprogram
(Entity
(Selector_Name
(Pref
))), Loc
));
616 elsif Nkind
(Pref
) = N_Explicit_Dereference
617 and then Ekind
(Etype
(Pref
)) = E_Subprogram_Type
618 and then Convention
(Etype
(Pref
)) = Convention_Protected
620 -- The prefix is be a dereference of an access_to_protected_
621 -- subprogram. The desired address is the second component of
622 -- the record that represents the access.
625 Addr
: constant Entity_Id
:= Etype
(N
);
626 Ptr
: constant Node_Id
:= Prefix
(Pref
);
627 T
: constant Entity_Id
:=
628 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
632 Unchecked_Convert_To
(Addr
,
633 Make_Selected_Component
(Loc
,
634 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
635 Selector_Name
=> New_Occurrence_Of
(
636 Next_Entity
(First_Entity
(T
)), Loc
))));
638 Analyze_And_Resolve
(N
, Addr
);
642 -- Deal with packed array reference, other cases are handled by gigi
644 if Involves_Packed_Array_Reference
(Pref
) then
645 Expand_Packed_Address_Reference
(N
);
653 when Attribute_AST_Entry
=> AST_Entry
: declare
659 -- The reference to the entry or entry family
662 -- The index expression for an entry family reference, or
663 -- the Empty if Entry_Ref references a simple entry.
666 if Nkind
(Pref
) = N_Indexed_Component
then
667 Entry_Ref
:= Prefix
(Pref
);
668 Index
:= First
(Expressions
(Pref
));
674 -- Get expression for Task_Id and the entry entity
676 if Nkind
(Entry_Ref
) = N_Selected_Component
then
678 Make_Attribute_Reference
(Loc
,
679 Attribute_Name
=> Name_Identity
,
680 Prefix
=> Prefix
(Entry_Ref
));
682 Ttyp
:= Etype
(Prefix
(Entry_Ref
));
683 Eent
:= Entity
(Selector_Name
(Entry_Ref
));
687 Make_Function_Call
(Loc
,
688 Name
=> New_Occurrence_Of
(RTE
(RE_Current_Task
), Loc
));
690 Eent
:= Entity
(Entry_Ref
);
692 -- We have to find the enclosing task to get the task type
693 -- There must be one, since we already validated this earlier
695 Ttyp
:= Current_Scope
;
696 while not Is_Task_Type
(Ttyp
) loop
697 Ttyp
:= Scope
(Ttyp
);
701 -- Now rewrite the attribute with a call to Create_AST_Handler
704 Make_Function_Call
(Loc
,
705 Name
=> New_Occurrence_Of
(RTE
(RE_Create_AST_Handler
), Loc
),
706 Parameter_Associations
=> New_List
(
708 Entry_Index_Expression
(Loc
, Eent
, Index
, Ttyp
))));
710 Analyze_And_Resolve
(N
, RTE
(RE_AST_Handler
));
717 -- We compute this if a component clause was present, otherwise
718 -- we leave the computation up to Gigi, since we don't know what
719 -- layout will be chosen.
721 -- Note that the attribute can apply to a naked record component
722 -- in generated code (i.e. the prefix is an identifier that
723 -- references the component or discriminant entity).
725 when Attribute_Bit_Position
=> Bit_Position
:
730 if Nkind
(Pref
) = N_Identifier
then
733 CE
:= Entity
(Selector_Name
(Pref
));
736 if Known_Static_Component_Bit_Offset
(CE
) then
738 Make_Integer_Literal
(Loc
,
739 Intval
=> Component_Bit_Offset
(CE
)));
740 Analyze_And_Resolve
(N
, Typ
);
743 Apply_Universal_Integer_Attribute_Checks
(N
);
751 -- A reference to P'Body_Version or P'Version is expanded to
754 -- pragma Import (C, Vnn, "uuuuT";
756 -- Get_Version_String (Vnn)
758 -- where uuuu is the unit name (dots replaced by double underscore)
759 -- and T is B for the cases of Body_Version, or Version applied to a
760 -- subprogram acting as its own spec, and S for Version applied to a
761 -- subprogram spec or package. This sequence of code references the
762 -- the unsigned constant created in the main program by the binder.
764 -- A special exception occurs for Standard, where the string
765 -- returned is a copy of the library string in gnatvsn.ads.
767 when Attribute_Body_Version | Attribute_Version
=> Version
: declare
768 E
: constant Entity_Id
:=
769 Make_Defining_Identifier
(Loc
, New_Internal_Name
('V'));
770 Pent
: Entity_Id
:= Entity
(Pref
);
774 -- If not library unit, get to containing library unit
776 while Pent
/= Standard_Standard
777 and then Scope
(Pent
) /= Standard_Standard
779 Pent
:= Scope
(Pent
);
782 -- Special case Standard
784 if Pent
= Standard_Standard
785 or else Pent
= Standard_ASCII
787 Name_Buffer
(1 .. Library_Version
'Length) := Library_Version
;
788 Name_Len
:= Library_Version
'Length;
790 Make_String_Literal
(Loc
,
791 Strval
=> String_From_Name_Buffer
));
796 -- Build required string constant
798 Get_Name_String
(Get_Unit_Name
(Pent
));
801 for J
in 1 .. Name_Len
- 2 loop
802 if Name_Buffer
(J
) = '.' then
803 Store_String_Chars
("__");
805 Store_String_Char
(Get_Char_Code
(Name_Buffer
(J
)));
809 -- Case of subprogram acting as its own spec, always use body
811 if Nkind
(Declaration_Node
(Pent
)) in N_Subprogram_Specification
812 and then Nkind
(Parent
(Declaration_Node
(Pent
))) =
814 and then Acts_As_Spec
(Parent
(Declaration_Node
(Pent
)))
816 Store_String_Chars
("B");
818 -- Case of no body present, always use spec
820 elsif not Unit_Requires_Body
(Pent
) then
821 Store_String_Chars
("S");
823 -- Otherwise use B for Body_Version, S for spec
825 elsif Id
= Attribute_Body_Version
then
826 Store_String_Chars
("B");
828 Store_String_Chars
("S");
832 Lib
.Version_Referenced
(S
);
834 -- Insert the object declaration
836 Insert_Actions
(N
, New_List
(
837 Make_Object_Declaration
(Loc
,
838 Defining_Identifier
=> E
,
840 New_Occurrence_Of
(RTE
(RE_Unsigned
), Loc
))));
842 -- Set entity as imported with correct external name
845 Set_Interface_Name
(E
, Make_String_Literal
(Loc
, S
));
847 -- And now rewrite original reference
850 Make_Function_Call
(Loc
,
851 Name
=> New_Reference_To
(RTE
(RE_Get_Version_String
), Loc
),
852 Parameter_Associations
=> New_List
(
853 New_Occurrence_Of
(E
, Loc
))));
856 Analyze_And_Resolve
(N
, RTE
(RE_Version_String
));
863 -- Transforms 'Ceiling into a call to the floating-point attribute
864 -- function Ceiling in Fat_xxx (where xxx is the root type)
866 when Attribute_Ceiling
=>
867 Expand_Fpt_Attribute_R
(N
);
873 -- Transforms 'Callable attribute into a call to the Callable function.
875 when Attribute_Callable
=> Callable
:
878 Build_Call_With_Task
(Pref
, RTE
(RE_Callable
)));
879 Analyze_And_Resolve
(N
, Standard_Boolean
);
886 -- Transforms 'Caller attribute into a call to either the
887 -- Task_Entry_Caller or the Protected_Entry_Caller function.
889 when Attribute_Caller
=> Caller
: declare
890 Id_Kind
: Entity_Id
:= RTE
(RO_AT_Task_ID
);
891 Ent
: Entity_Id
:= Entity
(Pref
);
892 Conctype
: Entity_Id
:= Scope
(Ent
);
893 Nest_Depth
: Integer := 0;
900 if Is_Protected_Type
(Conctype
) then
902 or else Restrictions
(No_Entry_Queue
) = False
903 or else Number_Entries
(Conctype
) > 1
907 (RTE
(RE_Protected_Entry_Caller
), Loc
);
911 (RTE
(RE_Protected_Single_Entry_Caller
), Loc
);
915 Unchecked_Convert_To
(Id_Kind
,
916 Make_Function_Call
(Loc
,
918 Parameter_Associations
=> New_List
921 (Corresponding_Body
(Parent
(Conctype
))), Loc
)))));
926 -- Determine the nesting depth of the E'Caller attribute, that
927 -- is, how many accept statements are nested within the accept
928 -- statement for E at the point of E'Caller. The runtime uses
929 -- this depth to find the specified entry call.
931 for J
in reverse 0 .. Scope_Stack
.Last
loop
932 S
:= Scope_Stack
.Table
(J
).Entity
;
934 -- We should not reach the scope of the entry, as it should
935 -- already have been checked in Sem_Attr that this attribute
936 -- reference is within a matching accept statement.
938 pragma Assert
(S
/= Conctype
);
943 elsif Is_Entry
(S
) then
944 Nest_Depth
:= Nest_Depth
+ 1;
949 Unchecked_Convert_To
(Id_Kind
,
950 Make_Function_Call
(Loc
,
951 Name
=> New_Reference_To
(
952 RTE
(RE_Task_Entry_Caller
), Loc
),
953 Parameter_Associations
=> New_List
(
954 Make_Integer_Literal
(Loc
,
955 Intval
=> Int
(Nest_Depth
))))));
958 Analyze_And_Resolve
(N
, Id_Kind
);
965 -- Transforms 'Compose into a call to the floating-point attribute
966 -- function Compose in Fat_xxx (where xxx is the root type)
968 -- Note: we strictly should have special code here to deal with the
969 -- case of absurdly negative arguments (less than Integer'First)
970 -- which will return a (signed) zero value, but it hardly seems
971 -- worth the effort. Absurdly large positive arguments will raise
972 -- constraint error which is fine.
974 when Attribute_Compose
=>
975 Expand_Fpt_Attribute_RI
(N
);
981 when Attribute_Constrained
=> Constrained
: declare
982 Formal_Ent
: constant Entity_Id
:= Param_Entity
(Pref
);
985 -- Reference to a parameter where the value is passed as an extra
986 -- actual, corresponding to the extra formal referenced by the
987 -- Extra_Constrained field of the corresponding formal.
989 if Present
(Formal_Ent
)
990 and then Present
(Extra_Constrained
(Formal_Ent
))
994 (Extra_Constrained
(Formal_Ent
), Sloc
(N
)));
996 -- For variables with a Extra_Constrained field, we use the
997 -- corresponding entity.
999 elsif Nkind
(Pref
) = N_Identifier
1000 and then Ekind
(Entity
(Pref
)) = E_Variable
1001 and then Present
(Extra_Constrained
(Entity
(Pref
)))
1005 (Extra_Constrained
(Entity
(Pref
)), Sloc
(N
)));
1007 -- For all other entity names, we can tell at compile time
1009 elsif Is_Entity_Name
(Pref
) then
1011 Ent
: constant Entity_Id
:= Entity
(Pref
);
1015 -- (RM J.4) obsolescent cases
1017 if Is_Type
(Ent
) then
1021 if Is_Private_Type
(Ent
) then
1022 Res
:= not Has_Discriminants
(Ent
)
1023 or else Is_Constrained
(Ent
);
1025 -- It not a private type, must be a generic actual type
1026 -- that corresponded to a private type. We know that this
1027 -- correspondence holds, since otherwise the reference
1028 -- within the generic template would have been illegal.
1032 UT
: Entity_Id
:= Underlying_Type
(Ent
);
1035 if Is_Composite_Type
(UT
) then
1036 Res
:= Is_Constrained
(Ent
);
1043 -- If the prefix is not a variable or is aliased, then
1044 -- definitely true; if it's a formal parameter without
1045 -- an associated extra formal, then treat it as constrained.
1047 elsif not Is_Variable
(Pref
)
1048 or else Present
(Formal_Ent
)
1049 or else Is_Aliased_View
(Pref
)
1053 -- Variable case, just look at type to see if it is
1054 -- constrained. Note that the one case where this is
1055 -- not accurate (the procedure formal case), has been
1059 Res
:= Is_Constrained
(Etype
(Ent
));
1064 New_Reference_To
(Standard_True
, Loc
));
1067 New_Reference_To
(Standard_False
, Loc
));
1071 -- Prefix is not an entity name. These are also cases where
1072 -- we can always tell at compile time by looking at the form
1073 -- and type of the prefix.
1076 if not Is_Variable
(Pref
)
1077 or else Nkind
(Pref
) = N_Explicit_Dereference
1078 or else Is_Constrained
(Etype
(Pref
))
1081 New_Reference_To
(Standard_True
, Loc
));
1084 New_Reference_To
(Standard_False
, Loc
));
1088 Analyze_And_Resolve
(N
, Standard_Boolean
);
1095 -- Transforms 'Copy_Sign into a call to the floating-point attribute
1096 -- function Copy_Sign in Fat_xxx (where xxx is the root type)
1098 when Attribute_Copy_Sign
=>
1099 Expand_Fpt_Attribute_RR
(N
);
1105 -- Transforms 'Count attribute into a call to the Count function
1107 when Attribute_Count
=> Count
:
1113 Conctyp
: Entity_Id
;
1116 -- If the prefix is a member of an entry family, retrieve both
1117 -- entry name and index. For a simple entry there is no index.
1119 if Nkind
(Pref
) = N_Indexed_Component
then
1120 Entnam
:= Prefix
(Pref
);
1121 Index
:= First
(Expressions
(Pref
));
1127 -- Find the concurrent type in which this attribute is referenced
1128 -- (there had better be one).
1130 Conctyp
:= Current_Scope
;
1131 while not Is_Concurrent_Type
(Conctyp
) loop
1132 Conctyp
:= Scope
(Conctyp
);
1137 if Is_Protected_Type
(Conctyp
) then
1140 or else Restrictions
(No_Entry_Queue
) = False
1141 or else Number_Entries
(Conctyp
) > 1
1143 Name
:= New_Reference_To
(RTE
(RE_Protected_Count
), Loc
);
1146 Make_Function_Call
(Loc
,
1148 Parameter_Associations
=> New_List
(
1151 Corresponding_Body
(Parent
(Conctyp
))), Loc
),
1152 Entry_Index_Expression
(
1153 Loc
, Entity
(Entnam
), Index
, Scope
(Entity
(Entnam
)))));
1155 Name
:= New_Reference_To
(RTE
(RE_Protected_Count_Entry
), Loc
);
1157 Call
:= Make_Function_Call
(Loc
,
1159 Parameter_Associations
=> New_List
(
1162 Corresponding_Body
(Parent
(Conctyp
))), Loc
)));
1169 Make_Function_Call
(Loc
,
1170 Name
=> New_Reference_To
(RTE
(RE_Task_Count
), Loc
),
1171 Parameter_Associations
=> New_List
(
1172 Entry_Index_Expression
1173 (Loc
, Entity
(Entnam
), Index
, Scope
(Entity
(Entnam
)))));
1176 -- The call returns type Natural but the context is universal integer
1177 -- so any integer type is allowed. The attribute was already resolved
1178 -- so its Etype is the required result type. If the base type of the
1179 -- context type is other than Standard.Integer we put in a conversion
1180 -- to the required type. This can be a normal typed conversion since
1181 -- both input and output types of the conversion are integer types
1183 if Base_Type
(Typ
) /= Base_Type
(Standard_Integer
) then
1184 Rewrite
(N
, Convert_To
(Typ
, Call
));
1189 Analyze_And_Resolve
(N
, Typ
);
1196 -- This processing is shared by Elab_Spec
1198 -- What we do is to insert the following declarations
1201 -- pragma Import (C, enn, "name___elabb/s");
1203 -- and then the Elab_Body/Spec attribute is replaced by a reference
1204 -- to this defining identifier.
1206 when Attribute_Elab_Body |
1207 Attribute_Elab_Spec
=>
1210 Ent
: constant Entity_Id
:=
1211 Make_Defining_Identifier
(Loc
,
1212 New_Internal_Name
('E'));
1216 procedure Make_Elab_String
(Nod
: Node_Id
);
1217 -- Given Nod, an identifier, or a selected component, put the
1218 -- image into the current string literal, with double underline
1219 -- between components.
1221 procedure Make_Elab_String
(Nod
: Node_Id
) is
1223 if Nkind
(Nod
) = N_Selected_Component
then
1224 Make_Elab_String
(Prefix
(Nod
));
1226 Store_String_Char
('$');
1228 Store_String_Char
('_');
1229 Store_String_Char
('_');
1232 Get_Name_String
(Chars
(Selector_Name
(Nod
)));
1235 pragma Assert
(Nkind
(Nod
) = N_Identifier
);
1236 Get_Name_String
(Chars
(Nod
));
1239 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
1240 end Make_Elab_String
;
1242 -- Start of processing for Elab_Body/Elab_Spec
1245 -- First we need to prepare the string literal for the name of
1246 -- the elaboration routine to be referenced.
1249 Make_Elab_String
(Pref
);
1252 Store_String_Chars
("._elab");
1253 Lang
:= Make_Identifier
(Loc
, Name_Ada
);
1255 Store_String_Chars
("___elab");
1256 Lang
:= Make_Identifier
(Loc
, Name_C
);
1259 if Id
= Attribute_Elab_Body
then
1260 Store_String_Char
('b');
1262 Store_String_Char
('s');
1267 Insert_Actions
(N
, New_List
(
1268 Make_Subprogram_Declaration
(Loc
,
1270 Make_Procedure_Specification
(Loc
,
1271 Defining_Unit_Name
=> Ent
)),
1274 Chars
=> Name_Import
,
1275 Pragma_Argument_Associations
=> New_List
(
1276 Make_Pragma_Argument_Association
(Loc
,
1277 Expression
=> Lang
),
1279 Make_Pragma_Argument_Association
(Loc
,
1281 Make_Identifier
(Loc
, Chars
(Ent
))),
1283 Make_Pragma_Argument_Association
(Loc
,
1285 Make_String_Literal
(Loc
, Str
))))));
1287 Set_Entity
(N
, Ent
);
1288 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
1295 -- Elaborated is always True for preelaborated units, predefined
1296 -- units, pure units and units which have Elaborate_Body pragmas.
1297 -- These units have no elaboration entity.
1299 -- Note: The Elaborated attribute is never passed through to Gigi
1301 when Attribute_Elaborated
=> Elaborated
: declare
1302 Ent
: constant Entity_Id
:= Entity
(Pref
);
1305 if Present
(Elaboration_Entity
(Ent
)) then
1307 New_Occurrence_Of
(Elaboration_Entity
(Ent
), Loc
));
1309 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
1317 when Attribute_Enum_Rep
=> Enum_Rep
:
1319 -- X'Enum_Rep (Y) expands to
1323 -- This is simply a direct conversion from the enumeration type
1324 -- to the target integer type, which is treated by Gigi as a normal
1325 -- integer conversion, treating the enumeration type as an integer,
1326 -- which is exactly what we want! We set Conversion_OK to make sure
1327 -- that the analyzer does not complain about what otherwise might
1328 -- be an illegal conversion.
1330 if Is_Non_Empty_List
(Exprs
) then
1332 OK_Convert_To
(Typ
, Relocate_Node
(First
(Exprs
))));
1334 -- X'Enum_Rep where X is an enumeration literal is replaced by
1335 -- the literal value.
1337 elsif Ekind
(Entity
(Pref
)) = E_Enumeration_Literal
then
1339 Make_Integer_Literal
(Loc
, Enumeration_Rep
(Entity
(Pref
))));
1341 -- X'Enum_Rep where X is an object does a direct unchecked conversion
1342 -- of the object value, as described for the type case above.
1346 OK_Convert_To
(Typ
, Relocate_Node
(Pref
)));
1350 Analyze_And_Resolve
(N
, Typ
);
1358 -- Transforms 'Exponent into a call to the floating-point attribute
1359 -- function Exponent in Fat_xxx (where xxx is the root type)
1361 when Attribute_Exponent
=>
1362 Expand_Fpt_Attribute_R
(N
);
1368 -- transforme X'External_Tag into Ada.Tags.External_Tag (X'tag)
1370 when Attribute_External_Tag
=> External_Tag
:
1373 Make_Function_Call
(Loc
,
1374 Name
=> New_Reference_To
(RTE
(RE_External_Tag
), Loc
),
1375 Parameter_Associations
=> New_List
(
1376 Make_Attribute_Reference
(Loc
,
1377 Attribute_Name
=> Name_Tag
,
1378 Prefix
=> Prefix
(N
)))));
1380 Analyze_And_Resolve
(N
, Standard_String
);
1387 when Attribute_First
=> declare
1388 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1391 -- If the prefix type is a constrained packed array type which
1392 -- already has a Packed_Array_Type representation defined, then
1393 -- replace this attribute with a direct reference to 'First of the
1394 -- appropriate index subtype (since otherwise Gigi will try to give
1395 -- us the value of 'First for this implementation type).
1397 if Is_Constrained_Packed_Array
(Ptyp
) then
1399 Make_Attribute_Reference
(Loc
,
1400 Attribute_Name
=> Name_First
,
1401 Prefix
=> New_Reference_To
(Get_Index_Subtype
(N
), Loc
)));
1402 Analyze_And_Resolve
(N
, Typ
);
1404 elsif Is_Access_Type
(Ptyp
) then
1405 Apply_Access_Check
(N
);
1413 -- We compute this if a component clause was present, otherwise
1414 -- we leave the computation up to Gigi, since we don't know what
1415 -- layout will be chosen.
1417 when Attribute_First_Bit
=> First_Bit
:
1419 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
1422 if Known_Static_Component_Bit_Offset
(CE
) then
1424 Make_Integer_Literal
(Loc
,
1425 Component_Bit_Offset
(CE
) mod System_Storage_Unit
));
1427 Analyze_And_Resolve
(N
, Typ
);
1430 Apply_Universal_Integer_Attribute_Checks
(N
);
1440 -- fixtype'Fixed_Value (integer-value)
1444 -- fixtype(integer-value)
1446 -- we do all the required analysis of the conversion here, because
1447 -- we do not want this to go through the fixed-point conversion
1448 -- circuits. Note that gigi always treats fixed-point as equivalent
1449 -- to the corresponding integer type anyway.
1451 when Attribute_Fixed_Value
=> Fixed_Value
:
1454 Make_Type_Conversion
(Loc
,
1455 Subtype_Mark
=> New_Occurrence_Of
(Entity
(Pref
), Loc
),
1456 Expression
=> Relocate_Node
(First
(Exprs
))));
1457 Set_Etype
(N
, Entity
(Pref
));
1459 Apply_Type_Conversion_Checks
(N
);
1466 -- Transforms 'Floor into a call to the floating-point attribute
1467 -- function Floor in Fat_xxx (where xxx is the root type)
1469 when Attribute_Floor
=>
1470 Expand_Fpt_Attribute_R
(N
);
1476 -- For the fixed-point type Typ:
1482 -- Result_Type (System.Fore (Long_Long_Float (Type'First)),
1483 -- Long_Long_Float (Type'Last))
1485 -- Note that we know that the type is a non-static subtype, or Fore
1486 -- would have itself been computed dynamically in Eval_Attribute.
1488 when Attribute_Fore
=> Fore
:
1490 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1495 Make_Function_Call
(Loc
,
1496 Name
=> New_Reference_To
(RTE
(RE_Fore
), Loc
),
1498 Parameter_Associations
=> New_List
(
1499 Convert_To
(Standard_Long_Long_Float
,
1500 Make_Attribute_Reference
(Loc
,
1501 Prefix
=> New_Reference_To
(Ptyp
, Loc
),
1502 Attribute_Name
=> Name_First
)),
1504 Convert_To
(Standard_Long_Long_Float
,
1505 Make_Attribute_Reference
(Loc
,
1506 Prefix
=> New_Reference_To
(Ptyp
, Loc
),
1507 Attribute_Name
=> Name_Last
))))));
1509 Analyze_And_Resolve
(N
, Typ
);
1516 -- Transforms 'Fraction into a call to the floating-point attribute
1517 -- function Fraction in Fat_xxx (where xxx is the root type)
1519 when Attribute_Fraction
=>
1520 Expand_Fpt_Attribute_R
(N
);
1526 -- For an exception returns a reference to the exception data:
1527 -- Exception_Id!(Prefix'Reference)
1529 -- For a task it returns a reference to the _task_id component of
1530 -- corresponding record:
1532 -- taskV!(Prefix)._Task_Id, converted to the type Task_ID defined
1534 -- in Ada.Task_Identification.
1536 when Attribute_Identity
=> Identity
: declare
1537 Id_Kind
: Entity_Id
;
1540 if Etype
(Pref
) = Standard_Exception_Type
then
1541 Id_Kind
:= RTE
(RE_Exception_Id
);
1543 if Present
(Renamed_Object
(Entity
(Pref
))) then
1544 Set_Entity
(Pref
, Renamed_Object
(Entity
(Pref
)));
1548 Unchecked_Convert_To
(Id_Kind
, Make_Reference
(Loc
, Pref
)));
1550 Id_Kind
:= RTE
(RO_AT_Task_ID
);
1553 Unchecked_Convert_To
(Id_Kind
, Concurrent_Ref
(Pref
)));
1556 Analyze_And_Resolve
(N
, Id_Kind
);
1563 -- Image attribute is handled in separate unit Exp_Imgv
1565 when Attribute_Image
=>
1566 Exp_Imgv
.Expand_Image_Attribute
(N
);
1572 -- X'Img is expanded to typ'Image (X), where typ is the type of X
1574 when Attribute_Img
=> Img
:
1577 Make_Attribute_Reference
(Loc
,
1578 Prefix
=> New_Reference_To
(Etype
(Pref
), Loc
),
1579 Attribute_Name
=> Name_Image
,
1580 Expressions
=> New_List
(Relocate_Node
(Pref
))));
1582 Analyze_And_Resolve
(N
, Standard_String
);
1589 when Attribute_Input
=> Input
: declare
1590 P_Type
: constant Entity_Id
:= Entity
(Pref
);
1591 B_Type
: constant Entity_Id
:= Base_Type
(P_Type
);
1592 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
1593 Strm
: constant Node_Id
:= First
(Exprs
);
1601 Cntrl
: Node_Id
:= Empty
;
1602 -- Value for controlling argument in call. Always Empty except in
1603 -- the dispatching (class-wide type) case, where it is a reference
1604 -- to the dummy object initialized to the right internal tag.
1607 -- If no underlying type, we have an error that will be diagnosed
1608 -- elsewhere, so here we just completely ignore the expansion.
1614 -- If there is a TSS for Input, just call it
1616 Fname
:= Find_Inherited_TSS
(P_Type
, Name_uInput
);
1618 if Present
(Fname
) then
1622 -- If there is a Stream_Convert pragma, use it, we rewrite
1624 -- sourcetyp'Input (stream)
1628 -- sourcetyp (streamread (strmtyp'Input (stream)));
1630 -- where stmrearead is the given Read function that converts
1631 -- an argument of type strmtyp to type sourcetyp or a type
1632 -- from which it is derived. The extra conversion is required
1633 -- for the derived case.
1637 (Implementation_Base_Type
(P_Type
), Name_Stream_Convert
);
1639 if Present
(Prag
) then
1640 Arg2
:= Next
(First
(Pragma_Argument_Associations
(Prag
)));
1641 Rfunc
:= Entity
(Expression
(Arg2
));
1645 Make_Function_Call
(Loc
,
1646 Name
=> New_Occurrence_Of
(Rfunc
, Loc
),
1647 Parameter_Associations
=> New_List
(
1648 Make_Attribute_Reference
(Loc
,
1651 (Etype
(First_Formal
(Rfunc
)), Loc
),
1652 Attribute_Name
=> Name_Input
,
1653 Expressions
=> Exprs
)))));
1655 Analyze_And_Resolve
(N
, B_Type
);
1660 elsif Is_Elementary_Type
(U_Type
) then
1662 -- A special case arises if we have a defined _Read routine,
1663 -- since in this case we are required to call this routine.
1665 if Present
(TSS
(B_Type
, Name_uRead
)) then
1666 Build_Record_Or_Elementary_Input_Function
1667 (Loc
, U_Type
, Decl
, Fname
);
1668 Insert_Action
(N
, Decl
);
1670 -- For normal cases, we call the I_xxx routine directly
1673 Rewrite
(N
, Build_Elementary_Input_Call
(N
));
1674 Analyze_And_Resolve
(N
, P_Type
);
1680 elsif Is_Array_Type
(U_Type
) then
1681 Build_Array_Input_Function
(Loc
, U_Type
, Decl
, Fname
);
1682 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
1684 -- Dispatching case with class-wide type
1686 elsif Is_Class_Wide_Type
(P_Type
) then
1689 Rtyp
: constant Entity_Id
:= Root_Type
(P_Type
);
1694 -- Read the internal tag (RM 13.13.2(34)) and use it to
1695 -- initialize a dummy tag object:
1697 -- Dnn : Ada.Tags.Tag
1698 -- := Internal_Tag (String'Input (Strm));
1700 -- This dummy object is used only to provide a controlling
1701 -- argument for the eventual _Input call.
1704 Make_Defining_Identifier
(Loc
,
1705 Chars
=> New_Internal_Name
('D'));
1708 Make_Object_Declaration
(Loc
,
1709 Defining_Identifier
=> Dnn
,
1710 Object_Definition
=>
1711 New_Occurrence_Of
(RTE
(RE_Tag
), Loc
),
1713 Make_Function_Call
(Loc
,
1715 New_Occurrence_Of
(RTE
(RE_Internal_Tag
), Loc
),
1716 Parameter_Associations
=> New_List
(
1717 Make_Attribute_Reference
(Loc
,
1719 New_Occurrence_Of
(Standard_String
, Loc
),
1720 Attribute_Name
=> Name_Input
,
1721 Expressions
=> New_List
(
1723 (Duplicate_Subexpr
(Strm
)))))));
1725 Insert_Action
(N
, Decl
);
1727 -- Now we need to get the entity for the call, and construct
1728 -- a function call node, where we preset a reference to Dnn
1729 -- as the controlling argument (doing an unchecked
1730 -- conversion to the tagged type to make it look like
1731 -- a real tagged object).
1733 Fname
:= Find_Prim_Op
(Rtyp
, Name_uInput
);
1734 Cntrl
:= Unchecked_Convert_To
(Rtyp
,
1735 New_Occurrence_Of
(Dnn
, Loc
));
1736 Set_Etype
(Cntrl
, Rtyp
);
1737 Set_Parent
(Cntrl
, N
);
1740 -- For tagged types, use the primitive Input function
1742 elsif Is_Tagged_Type
(U_Type
) then
1743 Fname
:= Find_Prim_Op
(U_Type
, Name_uInput
);
1745 -- All other record type cases, including protected records.
1746 -- The latter only arise for expander generated code for
1747 -- handling shared passive partition access.
1751 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
1753 Build_Record_Or_Elementary_Input_Function
1754 (Loc
, Base_Type
(U_Type
), Decl
, Fname
);
1755 Insert_Action
(N
, Decl
);
1759 -- If we fall through, Fname is the function to be called. The
1760 -- result is obtained by calling the appropriate function, then
1761 -- converting the result. The conversion does a subtype check.
1764 Make_Function_Call
(Loc
,
1765 Name
=> New_Occurrence_Of
(Fname
, Loc
),
1766 Parameter_Associations
=> New_List
(
1767 Relocate_Node
(Strm
)));
1769 Set_Controlling_Argument
(Call
, Cntrl
);
1770 Rewrite
(N
, Unchecked_Convert_To
(P_Type
, Call
));
1771 Analyze_And_Resolve
(N
, P_Type
);
1780 -- inttype'Fixed_Value (fixed-value)
1784 -- inttype(integer-value))
1786 -- we do all the required analysis of the conversion here, because
1787 -- we do not want this to go through the fixed-point conversion
1788 -- circuits. Note that gigi always treats fixed-point as equivalent
1789 -- to the corresponding integer type anyway.
1791 when Attribute_Integer_Value
=> Integer_Value
:
1794 Make_Type_Conversion
(Loc
,
1795 Subtype_Mark
=> New_Occurrence_Of
(Entity
(Pref
), Loc
),
1796 Expression
=> Relocate_Node
(First
(Exprs
))));
1797 Set_Etype
(N
, Entity
(Pref
));
1799 Apply_Type_Conversion_Checks
(N
);
1806 when Attribute_Last
=> declare
1807 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1810 -- If the prefix type is a constrained packed array type which
1811 -- already has a Packed_Array_Type representation defined, then
1812 -- replace this attribute with a direct reference to 'Last of the
1813 -- appropriate index subtype (since otherwise Gigi will try to give
1814 -- us the value of 'Last for this implementation type).
1816 if Is_Constrained_Packed_Array
(Ptyp
) then
1818 Make_Attribute_Reference
(Loc
,
1819 Attribute_Name
=> Name_Last
,
1820 Prefix
=> New_Reference_To
(Get_Index_Subtype
(N
), Loc
)));
1821 Analyze_And_Resolve
(N
, Typ
);
1823 elsif Is_Access_Type
(Ptyp
) then
1824 Apply_Access_Check
(N
);
1832 -- We compute this if a component clause was present, otherwise
1833 -- we leave the computation up to Gigi, since we don't know what
1834 -- layout will be chosen.
1836 when Attribute_Last_Bit
=> Last_Bit
:
1838 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
1841 if Known_Static_Component_Bit_Offset
(CE
)
1842 and then Known_Static_Esize
(CE
)
1845 Make_Integer_Literal
(Loc
,
1846 Intval
=> (Component_Bit_Offset
(CE
) mod System_Storage_Unit
)
1849 Analyze_And_Resolve
(N
, Typ
);
1852 Apply_Universal_Integer_Attribute_Checks
(N
);
1860 -- Transforms 'Leading_Part into a call to the floating-point attribute
1861 -- function Leading_Part in Fat_xxx (where xxx is the root type)
1863 -- Note: strictly, we should have special case code to deal with
1864 -- absurdly large positive arguments (greater than Integer'Last),
1865 -- which result in returning the first argument unchanged, but it
1866 -- hardly seems worth the effort. We raise constraint error for
1867 -- absurdly negative arguments which is fine.
1869 when Attribute_Leading_Part
=>
1870 Expand_Fpt_Attribute_RI
(N
);
1876 when Attribute_Length
=> declare
1877 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
1882 -- Processing for packed array types
1884 if Is_Array_Type
(Ptyp
) and then Is_Packed
(Ptyp
) then
1885 Ityp
:= Get_Index_Subtype
(N
);
1887 -- If the index type, Ityp, is an enumeration type with
1888 -- holes, then we calculate X'Length explicitly using
1891 -- (0, Ityp'Pos (X'Last (N)) -
1892 -- Ityp'Pos (X'First (N)) + 1);
1894 -- Since the bounds in the template are the representation
1895 -- values and gigi would get the wrong value.
1897 if Is_Enumeration_Type
(Ityp
)
1898 and then Present
(Enum_Pos_To_Rep
(Base_Type
(Ityp
)))
1903 Xnum
:= Expr_Value
(First
(Expressions
(N
)));
1907 Make_Attribute_Reference
(Loc
,
1908 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1909 Attribute_Name
=> Name_Max
,
1910 Expressions
=> New_List
1911 (Make_Integer_Literal
(Loc
, 0),
1915 Make_Op_Subtract
(Loc
,
1917 Make_Attribute_Reference
(Loc
,
1918 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
1919 Attribute_Name
=> Name_Pos
,
1921 Expressions
=> New_List
(
1922 Make_Attribute_Reference
(Loc
,
1923 Prefix
=> Duplicate_Subexpr
(Pref
),
1924 Attribute_Name
=> Name_Last
,
1925 Expressions
=> New_List
(
1926 Make_Integer_Literal
(Loc
, Xnum
))))),
1929 Make_Attribute_Reference
(Loc
,
1930 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
1931 Attribute_Name
=> Name_Pos
,
1933 Expressions
=> New_List
(
1934 Make_Attribute_Reference
(Loc
,
1935 Prefix
=> Duplicate_Subexpr
(Pref
),
1936 Attribute_Name
=> Name_First
,
1937 Expressions
=> New_List
(
1938 Make_Integer_Literal
(Loc
, Xnum
)))))),
1940 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
1942 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
1945 -- If the prefix type is a constrained packed array type which
1946 -- already has a Packed_Array_Type representation defined, then
1947 -- replace this attribute with a direct reference to 'Range_Length
1948 -- of the appropriate index subtype (since otherwise Gigi will try
1949 -- to give us the value of 'Length for this implementation type).
1951 elsif Is_Constrained
(Ptyp
) then
1953 Make_Attribute_Reference
(Loc
,
1954 Attribute_Name
=> Name_Range_Length
,
1955 Prefix
=> New_Reference_To
(Ityp
, Loc
)));
1956 Analyze_And_Resolve
(N
, Typ
);
1959 -- If we have a packed array that is not bit packed, which was
1963 elsif Is_Access_Type
(Ptyp
) then
1964 Apply_Access_Check
(N
);
1966 -- If the designated type is a packed array type, then we
1967 -- convert the reference to:
1970 -- xtyp'Pos (Pref'Last (Expr)) -
1971 -- xtyp'Pos (Pref'First (Expr)));
1973 -- This is a bit complex, but it is the easiest thing to do
1974 -- that works in all cases including enum types with holes
1975 -- xtyp here is the appropriate index type.
1978 Dtyp
: constant Entity_Id
:= Designated_Type
(Ptyp
);
1982 if Is_Array_Type
(Dtyp
) and then Is_Packed
(Dtyp
) then
1983 Xtyp
:= Get_Index_Subtype
(N
);
1986 Make_Attribute_Reference
(Loc
,
1987 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1988 Attribute_Name
=> Name_Max
,
1989 Expressions
=> New_List
(
1990 Make_Integer_Literal
(Loc
, 0),
1993 Make_Integer_Literal
(Loc
, 1),
1994 Make_Op_Subtract
(Loc
,
1996 Make_Attribute_Reference
(Loc
,
1997 Prefix
=> New_Occurrence_Of
(Xtyp
, Loc
),
1998 Attribute_Name
=> Name_Pos
,
1999 Expressions
=> New_List
(
2000 Make_Attribute_Reference
(Loc
,
2001 Prefix
=> Duplicate_Subexpr
(Pref
),
2002 Attribute_Name
=> Name_Last
,
2004 New_Copy_List
(Exprs
)))),
2007 Make_Attribute_Reference
(Loc
,
2008 Prefix
=> New_Occurrence_Of
(Xtyp
, Loc
),
2009 Attribute_Name
=> Name_Pos
,
2010 Expressions
=> New_List
(
2011 Make_Attribute_Reference
(Loc
,
2012 Prefix
=> Duplicate_Subexpr
(Pref
),
2013 Attribute_Name
=> Name_First
,
2015 New_Copy_List
(Exprs
)))))))));
2017 Analyze_And_Resolve
(N
, Typ
);
2021 -- Otherwise leave it to gigi
2024 Apply_Universal_Integer_Attribute_Checks
(N
);
2032 -- Transforms 'Machine into a call to the floating-point attribute
2033 -- function Machine in Fat_xxx (where xxx is the root type)
2035 when Attribute_Machine
=>
2036 Expand_Fpt_Attribute_R
(N
);
2042 -- Machine_Size is equivalent to Object_Size, so transform it into
2043 -- Object_Size and that way Gigi never sees Machine_Size.
2045 when Attribute_Machine_Size
=>
2047 Make_Attribute_Reference
(Loc
,
2048 Prefix
=> Prefix
(N
),
2049 Attribute_Name
=> Name_Object_Size
));
2051 Analyze_And_Resolve
(N
, Typ
);
2057 -- The only case that can get this far is the dynamic case of the
2058 -- old Ada 83 Mantissa attribute for the fixed-point case. For this
2065 -- ityp (System.Mantissa.Mantissa_Value
2066 -- (Integer'Integer_Value (typ'First),
2067 -- Integer'Integer_Value (typ'Last)));
2069 when Attribute_Mantissa
=> Mantissa
: declare
2070 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2075 Make_Function_Call
(Loc
,
2076 Name
=> New_Occurrence_Of
(RTE
(RE_Mantissa_Value
), Loc
),
2078 Parameter_Associations
=> New_List
(
2080 Make_Attribute_Reference
(Loc
,
2081 Prefix
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
2082 Attribute_Name
=> Name_Integer_Value
,
2083 Expressions
=> New_List
(
2085 Make_Attribute_Reference
(Loc
,
2086 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
2087 Attribute_Name
=> Name_First
))),
2089 Make_Attribute_Reference
(Loc
,
2090 Prefix
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
2091 Attribute_Name
=> Name_Integer_Value
,
2092 Expressions
=> New_List
(
2094 Make_Attribute_Reference
(Loc
,
2095 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
2096 Attribute_Name
=> Name_Last
)))))));
2098 Analyze_And_Resolve
(N
, Typ
);
2105 -- Transforms 'Model into a call to the floating-point attribute
2106 -- function Model in Fat_xxx (where xxx is the root type)
2108 when Attribute_Model
=>
2109 Expand_Fpt_Attribute_R
(N
);
2115 -- The processing for Object_Size shares the processing for Size
2121 when Attribute_Output
=> Output
: declare
2122 P_Type
: constant Entity_Id
:= Entity
(Pref
);
2123 B_Type
: constant Entity_Id
:= Base_Type
(P_Type
);
2124 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
2132 -- If no underlying type, we have an error that will be diagnosed
2133 -- elsewhere, so here we just completely ignore the expansion.
2139 -- If TSS for Output is present, just call it
2141 Pname
:= Find_Inherited_TSS
(P_Type
, Name_uOutput
);
2143 if Present
(Pname
) then
2147 -- If there is a Stream_Convert pragma, use it, we rewrite
2149 -- sourcetyp'Output (stream, Item)
2153 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
2155 -- where strmwrite is the given Write function that converts
2156 -- an argument of type sourcetyp or a type acctyp, from which
2157 -- it is derived to type strmtyp. The conversion to acttyp is
2158 -- required for the derived case.
2162 (Implementation_Base_Type
(P_Type
), Name_Stream_Convert
);
2164 if Present
(Prag
) then
2166 Next
(Next
(First
(Pragma_Argument_Associations
(Prag
))));
2167 Wfunc
:= Entity
(Expression
(Arg3
));
2170 Make_Attribute_Reference
(Loc
,
2171 Prefix
=> New_Occurrence_Of
(Etype
(Wfunc
), Loc
),
2172 Attribute_Name
=> Name_Output
,
2173 Expressions
=> New_List
(
2174 Relocate_Node
(First
(Exprs
)),
2175 Make_Function_Call
(Loc
,
2176 Name
=> New_Occurrence_Of
(Wfunc
, Loc
),
2177 Parameter_Associations
=> New_List
(
2178 Convert_To
(Etype
(First_Formal
(Wfunc
)),
2179 Relocate_Node
(Next
(First
(Exprs
)))))))));
2184 -- For elementary types, we call the W_xxx routine directly.
2185 -- Note that the effect of Write and Output is identical for
2186 -- the case of an elementary type, since there are no
2187 -- discriminants or bounds.
2189 elsif Is_Elementary_Type
(U_Type
) then
2191 -- A special case arises if we have a defined _Write routine,
2192 -- since in this case we are required to call this routine.
2194 if Present
(TSS
(B_Type
, Name_uWrite
)) then
2195 Build_Record_Or_Elementary_Output_Procedure
2196 (Loc
, U_Type
, Decl
, Pname
);
2197 Insert_Action
(N
, Decl
);
2199 -- For normal cases, we call the W_xxx routine directly
2202 Rewrite
(N
, Build_Elementary_Write_Call
(N
));
2209 elsif Is_Array_Type
(U_Type
) then
2210 Build_Array_Output_Procedure
(Loc
, U_Type
, Decl
, Pname
);
2211 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
2213 -- Class-wide case, first output external tag, then dispatch
2214 -- to the appropriate primitive Output function (RM 13.13.2(31)).
2216 elsif Is_Class_Wide_Type
(P_Type
) then
2218 Strm
: constant Node_Id
:= First
(Exprs
);
2219 Item
: constant Node_Id
:= Next
(Strm
);
2223 -- String'Output (Strm, External_Tag (Item'Tag))
2226 Make_Attribute_Reference
(Loc
,
2227 Prefix
=> New_Occurrence_Of
(Standard_String
, Loc
),
2228 Attribute_Name
=> Name_Output
,
2229 Expressions
=> New_List
(
2230 Relocate_Node
(Duplicate_Subexpr
(Strm
)),
2231 Make_Function_Call
(Loc
,
2233 New_Occurrence_Of
(RTE
(RE_External_Tag
), Loc
),
2234 Parameter_Associations
=> New_List
(
2235 Make_Attribute_Reference
(Loc
,
2238 (Duplicate_Subexpr
(Item
, Name_Req
=> True)),
2239 Attribute_Name
=> Name_Tag
))))));
2242 Pname
:= Find_Prim_Op
(U_Type
, Name_uOutput
);
2244 -- Tagged type case, use the primitive Output function
2246 elsif Is_Tagged_Type
(U_Type
) then
2247 Pname
:= Find_Prim_Op
(U_Type
, Name_uOutput
);
2249 -- All other record type cases, including protected records.
2250 -- The latter only arise for expander generated code for
2251 -- handling shared passive partition access.
2255 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
2257 Build_Record_Or_Elementary_Output_Procedure
2258 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
2259 Insert_Action
(N
, Decl
);
2263 -- If we fall through, Pname is the name of the procedure to call
2265 Rewrite_Stream_Proc_Call
(Pname
);
2272 -- For enumeration types with a standard representation, Pos is
2275 -- For enumeration types, with a non-standard representation we
2276 -- generate a call to the _Rep_To_Pos function created when the
2277 -- type was frozen. The call has the form
2279 -- _rep_to_pos (expr, True)
2281 -- The parameter True causes Program_Error to be raised if the
2282 -- expression has an invalid representation.
2284 -- For integer types, Pos is equivalent to a simple integer
2285 -- conversion and we rewrite it as such
2287 when Attribute_Pos
=> Pos
:
2289 Etyp
: Entity_Id
:= Base_Type
(Entity
(Pref
));
2292 -- Deal with zero/non-zero boolean values
2294 if Is_Boolean_Type
(Etyp
) then
2295 Adjust_Condition
(First
(Exprs
));
2296 Etyp
:= Standard_Boolean
;
2297 Set_Prefix
(N
, New_Occurrence_Of
(Standard_Boolean
, Loc
));
2300 -- Case of enumeration type
2302 if Is_Enumeration_Type
(Etyp
) then
2304 -- Non-standard enumeration type (generate call)
2306 if Present
(Enum_Pos_To_Rep
(Etyp
)) then
2307 Append_To
(Exprs
, New_Occurrence_Of
(Standard_True
, Loc
));
2311 Make_Function_Call
(Loc
,
2313 New_Reference_To
(TSS
(Etyp
, Name_uRep_To_Pos
), Loc
),
2314 Parameter_Associations
=> Exprs
)));
2316 Analyze_And_Resolve
(N
, Typ
);
2318 -- Standard enumeration type (do universal integer check)
2321 Apply_Universal_Integer_Attribute_Checks
(N
);
2324 -- Deal with integer types (replace by conversion)
2326 elsif Is_Integer_Type
(Etyp
) then
2327 Rewrite
(N
, Convert_To
(Typ
, First
(Exprs
)));
2328 Analyze_And_Resolve
(N
, Typ
);
2337 -- We compute this if a component clause was present, otherwise
2338 -- we leave the computation up to Gigi, since we don't know what
2339 -- layout will be chosen.
2341 when Attribute_Position
=> Position
:
2343 CE
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
2346 if Present
(Component_Clause
(CE
)) then
2348 Make_Integer_Literal
(Loc
,
2349 Intval
=> Component_Bit_Offset
(CE
) / System_Storage_Unit
));
2350 Analyze_And_Resolve
(N
, Typ
);
2353 Apply_Universal_Integer_Attribute_Checks
(N
);
2361 -- 1. Deal with enumeration types with holes
2362 -- 2. For floating-point, generate call to attribute function
2363 -- 3. For other cases, deal with constraint checking
2365 when Attribute_Pred
=> Pred
:
2367 Ptyp
: constant Entity_Id
:= Base_Type
(Etype
(Pref
));
2370 -- For enumeration types with non-standard representations, we
2371 -- expand typ'Pred (x) into
2373 -- Pos_To_Rep (Rep_To_Pos (x) - 1)
2375 if Is_Enumeration_Type
(Ptyp
)
2376 and then Present
(Enum_Pos_To_Rep
(Ptyp
))
2378 -- Add Boolean parameter True, to request program errror if
2379 -- we have a bad representation on our hands.
2381 Append_To
(Exprs
, New_Occurrence_Of
(Standard_True
, Loc
));
2384 Make_Indexed_Component
(Loc
,
2385 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Ptyp
), Loc
),
2386 Expressions
=> New_List
(
2387 Make_Op_Subtract
(Loc
,
2389 Make_Function_Call
(Loc
,
2391 New_Reference_To
(TSS
(Ptyp
, Name_uRep_To_Pos
), Loc
),
2392 Parameter_Associations
=> Exprs
),
2393 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
2395 Analyze_And_Resolve
(N
, Typ
);
2397 -- For floating-point, we transform 'Pred into a call to the Pred
2398 -- floating-point attribute function in Fat_xxx (xxx is root type)
2400 elsif Is_Floating_Point_Type
(Ptyp
) then
2401 Expand_Fpt_Attribute_R
(N
);
2402 Analyze_And_Resolve
(N
, Typ
);
2404 -- For modular types, nothing to do (no overflow, since wraps)
2406 elsif Is_Modular_Integer_Type
(Ptyp
) then
2409 -- For other types, if range checking is enabled, we must generate
2410 -- a check if overflow checking is enabled.
2412 elsif not Overflow_Checks_Suppressed
(Ptyp
) then
2413 Expand_Pred_Succ
(N
);
2422 when Attribute_Range_Length
=> Range_Length
: declare
2423 P_Type
: constant Entity_Id
:= Etype
(Pref
);
2426 -- The only special processing required is for the case where
2427 -- Range_Length is applied to an enumeration type with holes.
2428 -- In this case we transform
2434 -- X'Pos (X'Last) - X'Pos (X'First) + 1
2436 -- So that the result reflects the proper Pos values instead
2437 -- of the underlying representations.
2439 if Is_Enumeration_Type
(P_Type
)
2440 and then Has_Non_Standard_Rep
(P_Type
)
2445 Make_Op_Subtract
(Loc
,
2447 Make_Attribute_Reference
(Loc
,
2448 Attribute_Name
=> Name_Pos
,
2449 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
),
2450 Expressions
=> New_List
(
2451 Make_Attribute_Reference
(Loc
,
2452 Attribute_Name
=> Name_Last
,
2453 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
)))),
2456 Make_Attribute_Reference
(Loc
,
2457 Attribute_Name
=> Name_Pos
,
2458 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
),
2459 Expressions
=> New_List
(
2460 Make_Attribute_Reference
(Loc
,
2461 Attribute_Name
=> Name_First
,
2462 Prefix
=> New_Occurrence_Of
(P_Type
, Loc
))))),
2465 Make_Integer_Literal
(Loc
, 1)));
2467 Analyze_And_Resolve
(N
, Typ
);
2469 -- For all other cases, attribute is handled by Gigi, but we need
2470 -- to deal with the case of the range check on a universal integer.
2473 Apply_Universal_Integer_Attribute_Checks
(N
);
2482 when Attribute_Read
=> Read
: declare
2483 P_Type
: constant Entity_Id
:= Entity
(Pref
);
2484 B_Type
: constant Entity_Id
:= Base_Type
(P_Type
);
2485 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
2495 -- If no underlying type, we have an error that will be diagnosed
2496 -- elsewhere, so here we just completely ignore the expansion.
2502 -- The simple case, if there is a TSS for Read, just call it
2504 Pname
:= Find_Inherited_TSS
(P_Type
, Name_uRead
);
2506 if Present
(Pname
) then
2510 -- If there is a Stream_Convert pragma, use it, we rewrite
2512 -- sourcetyp'Read (stream, Item)
2516 -- Item := sourcetyp (strmread (strmtyp'Input (Stream)));
2518 -- where strmread is the given Read function that converts
2519 -- an argument of type strmtyp to type sourcetyp or a type
2520 -- from which it is derived. The conversion to sourcetyp
2521 -- is required in the latter case.
2523 -- A special case arises if Item is a type conversion in which
2524 -- case, we have to expand to:
2526 -- Itemx := typex (strmread (strmtyp'Input (Stream)));
2528 -- where Itemx is the expression of the type conversion (i.e.
2529 -- the actual object), and typex is the type of Itemx.
2533 (Implementation_Base_Type
(P_Type
), Name_Stream_Convert
);
2535 if Present
(Prag
) then
2536 Arg2
:= Next
(First
(Pragma_Argument_Associations
(Prag
)));
2537 Rfunc
:= Entity
(Expression
(Arg2
));
2538 Lhs
:= Relocate_Node
(Next
(First
(Exprs
)));
2541 Make_Function_Call
(Loc
,
2542 Name
=> New_Occurrence_Of
(Rfunc
, Loc
),
2543 Parameter_Associations
=> New_List
(
2544 Make_Attribute_Reference
(Loc
,
2547 (Etype
(First_Formal
(Rfunc
)), Loc
),
2548 Attribute_Name
=> Name_Input
,
2549 Expressions
=> New_List
(
2550 Relocate_Node
(First
(Exprs
)))))));
2552 if Nkind
(Lhs
) = N_Type_Conversion
then
2553 Lhs
:= Expression
(Lhs
);
2554 Rhs
:= Convert_To
(Etype
(Lhs
), Rhs
);
2558 Make_Assignment_Statement
(Loc
,
2560 Expression
=> Rhs
));
2561 Set_Assignment_OK
(Lhs
);
2565 -- For elementary types, we call the I_xxx routine using the first
2566 -- parameter and then assign the result into the second parameter.
2567 -- We set Assignment_OK to deal with the conversion case.
2569 elsif Is_Elementary_Type
(U_Type
) then
2575 Lhs
:= Relocate_Node
(Next
(First
(Exprs
)));
2576 Rhs
:= Build_Elementary_Input_Call
(N
);
2578 if Nkind
(Lhs
) = N_Type_Conversion
then
2579 Lhs
:= Expression
(Lhs
);
2580 Rhs
:= Convert_To
(Etype
(Lhs
), Rhs
);
2583 Set_Assignment_OK
(Lhs
);
2586 Make_Assignment_Statement
(Loc
,
2588 Expression
=> Rhs
));
2596 elsif Is_Array_Type
(U_Type
) then
2597 Build_Array_Read_Procedure
(N
, U_Type
, Decl
, Pname
);
2598 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
2600 -- Tagged type case, use the primitive Read function. Note that
2601 -- this will dispatch in the class-wide case which is what we want
2603 elsif Is_Tagged_Type
(U_Type
) then
2604 Pname
:= Find_Prim_Op
(U_Type
, Name_uRead
);
2606 -- All other record type cases, including protected records.
2607 -- The latter only arise for expander generated code for
2608 -- handling shared passive partition access.
2612 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
2614 if Has_Discriminants
(U_Type
)
2616 (Discriminant_Default_Value
(First_Discriminant
(U_Type
)))
2618 Build_Mutable_Record_Read_Procedure
2619 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
2622 Build_Record_Read_Procedure
2623 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
2626 -- Suppress checks, uninitialized or otherwise invalid
2627 -- data does not cause constraint errors to be raised for
2628 -- a complete record read.
2630 Insert_Action
(N
, Decl
, All_Checks
);
2634 Rewrite_Stream_Proc_Call
(Pname
);
2641 -- Transforms 'Remainder into a call to the floating-point attribute
2642 -- function Remainder in Fat_xxx (where xxx is the root type)
2644 when Attribute_Remainder
=>
2645 Expand_Fpt_Attribute_RR
(N
);
2651 -- The handling of the Round attribute is quite delicate. The
2652 -- processing in Sem_Attr introduced a conversion to universal
2653 -- real, reflecting the semantics of Round, but we do not want
2654 -- anything to do with universal real at runtime, since this
2655 -- corresponds to using floating-point arithmetic.
2657 -- What we have now is that the Etype of the Round attribute
2658 -- correctly indicates the final result type. The operand of
2659 -- the Round is the conversion to universal real, described
2660 -- above, and the operand of this conversion is the actual
2661 -- operand of Round, which may be the special case of a fixed
2662 -- point multiplication or division (Etype = universal fixed)
2664 -- The exapander will expand first the operand of the conversion,
2665 -- then the conversion, and finally the round attribute itself,
2666 -- since we always work inside out. But we cannot simply process
2667 -- naively in this order. In the semantic world where universal
2668 -- fixed and real really exist and have infinite precision, there
2669 -- is no problem, but in the implementation world, where universal
2670 -- real is a floating-point type, we would get the wrong result.
2672 -- So the approach is as follows. First, when expanding a multiply
2673 -- or divide whose type is universal fixed, we do nothing at all,
2674 -- instead deferring the operation till later.
2676 -- The actual processing is done in Expand_N_Type_Conversion which
2677 -- handles the special case of Round by looking at its parent to
2678 -- see if it is a Round attribute, and if it is, handling the
2679 -- conversion (or its fixed multiply/divide child) in an appropriate
2682 -- This means that by the time we get to expanding the Round attribute
2683 -- itself, the Round is nothing more than a type conversion (and will
2684 -- often be a null type conversion), so we just replace it with the
2685 -- appropriate conversion operation.
2687 when Attribute_Round
=>
2689 Convert_To
(Etype
(N
), Relocate_Node
(First
(Exprs
))));
2690 Analyze_And_Resolve
(N
);
2696 -- Transforms 'Rounding into a call to the floating-point attribute
2697 -- function Rounding in Fat_xxx (where xxx is the root type)
2699 when Attribute_Rounding
=>
2700 Expand_Fpt_Attribute_R
(N
);
2706 -- Transforms 'Scaling into a call to the floating-point attribute
2707 -- function Scaling in Fat_xxx (where xxx is the root type)
2709 when Attribute_Scaling
=>
2710 Expand_Fpt_Attribute_RI
(N
);
2716 when Attribute_Size |
2717 Attribute_Object_Size |
2718 Attribute_Value_Size |
2719 Attribute_VADS_Size
=> Size
:
2722 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2727 -- Processing for VADS_Size case. Note that this processing removes
2728 -- all traces of VADS_Size from the tree, and completes all required
2729 -- processing for VADS_Size by translating the attribute reference
2730 -- to an appropriate Size or Object_Size reference.
2732 if Id
= Attribute_VADS_Size
2733 or else (Use_VADS_Size
and then Id
= Attribute_Size
)
2735 -- If the size is specified, then we simply use the specified
2736 -- size. This applies to both types and objects. The size of an
2737 -- object can be specified in the following ways:
2739 -- An explicit size object is given for an object
2740 -- A component size is specified for an indexed component
2741 -- A component clause is specified for a selected component
2742 -- The object is a component of a packed composite object
2744 -- If the size is specified, then VADS_Size of an object
2746 if (Is_Entity_Name
(Pref
)
2747 and then Present
(Size_Clause
(Entity
(Pref
))))
2749 (Nkind
(Pref
) = N_Component_Clause
2750 and then (Present
(Component_Clause
2751 (Entity
(Selector_Name
(Pref
))))
2752 or else Is_Packed
(Etype
(Prefix
(Pref
)))))
2754 (Nkind
(Pref
) = N_Indexed_Component
2755 and then (Component_Size
(Etype
(Prefix
(Pref
))) /= 0
2756 or else Is_Packed
(Etype
(Prefix
(Pref
)))))
2758 Set_Attribute_Name
(N
, Name_Size
);
2760 -- Otherwise if we have an object rather than a type, then the
2761 -- VADS_Size attribute applies to the type of the object, rather
2762 -- than the object itself. This is one of the respects in which
2763 -- VADS_Size differs from Size.
2766 if (not Is_Entity_Name
(Pref
)
2767 or else not Is_Type
(Entity
(Pref
)))
2768 and then (Is_Scalar_Type
(Etype
(Pref
))
2769 or else Is_Constrained
(Etype
(Pref
)))
2771 Rewrite
(Pref
, New_Occurrence_Of
(Etype
(Pref
), Loc
));
2774 -- For a scalar type for which no size was
2775 -- explicitly given, VADS_Size means Object_Size. This is the
2776 -- other respect in which VADS_Size differs from Size.
2778 if Is_Scalar_Type
(Etype
(Pref
))
2779 and then No
(Size_Clause
(Etype
(Pref
)))
2781 Set_Attribute_Name
(N
, Name_Object_Size
);
2783 -- In all other cases, Size and VADS_Size are the sane
2786 Set_Attribute_Name
(N
, Name_Size
);
2791 -- For class-wide types, transform X'Size into a call to
2792 -- the primitive operation _Size
2794 if Is_Class_Wide_Type
(Ptyp
) then
2796 Make_Function_Call
(Loc
,
2797 Name
=> New_Reference_To
2798 (Find_Prim_Op
(Ptyp
, Name_uSize
), Loc
),
2799 Parameter_Associations
=> New_List
(Pref
));
2801 if Typ
/= Standard_Long_Long_Integer
then
2803 -- The context is a specific integer type with which the
2804 -- original attribute was compatible. The function has a
2805 -- specific type as well, so to preserve the compatibility
2806 -- we must convert explicitly.
2808 New_Node
:= Convert_To
(Typ
, New_Node
);
2811 Rewrite
(N
, New_Node
);
2812 Analyze_And_Resolve
(N
, Typ
);
2815 -- For an array component, we can do Size in the front end
2816 -- if the component_size of the array is set.
2818 elsif Nkind
(Pref
) = N_Indexed_Component
then
2819 Siz
:= Component_Size
(Etype
(Prefix
(Pref
)));
2821 -- For a record component, we can do Size in the front end
2822 -- if there is a component clause, or if the record is packed
2823 -- and the component's size is known at compile time.
2825 elsif Nkind
(Pref
) = N_Selected_Component
then
2827 Rec
: constant Entity_Id
:= Etype
(Prefix
(Pref
));
2828 Comp
: constant Entity_Id
:= Entity
(Selector_Name
(Pref
));
2831 if Present
(Component_Clause
(Comp
)) then
2832 Siz
:= Esize
(Comp
);
2834 elsif Is_Packed
(Rec
) then
2835 Siz
:= RM_Size
(Ptyp
);
2838 Apply_Universal_Integer_Attribute_Checks
(N
);
2843 -- All other cases are handled by Gigi
2846 Apply_Universal_Integer_Attribute_Checks
(N
);
2848 -- If we have Size applied to a formal parameter, that is a
2849 -- packed array subtype, then apply size to the actual subtype.
2851 if Is_Entity_Name
(Pref
)
2852 and then Is_Formal
(Entity
(Pref
))
2853 and then Is_Array_Type
(Etype
(Pref
))
2854 and then Is_Packed
(Etype
(Pref
))
2857 Make_Attribute_Reference
(Loc
,
2859 New_Occurrence_Of
(Get_Actual_Subtype
(Pref
), Loc
),
2860 Attribute_Name
=> Name_Size
));
2861 Analyze_And_Resolve
(N
, Typ
);
2867 -- Common processing for record and array component case
2871 Make_Integer_Literal
(Loc
, Siz
));
2873 Analyze_And_Resolve
(N
, Typ
);
2875 -- The result is not a static expression
2877 Set_Is_Static_Expression
(N
, False);
2885 when Attribute_Storage_Pool
=>
2887 Make_Type_Conversion
(Loc
,
2888 Subtype_Mark
=> New_Reference_To
(Etype
(N
), Loc
),
2889 Expression
=> New_Reference_To
(Entity
(N
), Loc
)));
2890 Analyze_And_Resolve
(N
, Typ
);
2896 when Attribute_Storage_Size
=> Storage_Size
:
2898 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
2901 -- Access type case, always go to the root type
2903 -- The case of access types results in a value of zero for the case
2904 -- where no storage size attribute clause has been given. If a
2905 -- storage size has been given, then the attribute is converted
2906 -- to a reference to the variable used to hold this value.
2908 if Is_Access_Type
(Ptyp
) then
2909 if Present
(Storage_Size_Variable
(Root_Type
(Ptyp
))) then
2911 Make_Attribute_Reference
(Loc
,
2912 Prefix
=> New_Reference_To
(Typ
, Loc
),
2913 Attribute_Name
=> Name_Max
,
2914 Expressions
=> New_List
(
2915 Make_Integer_Literal
(Loc
, 0),
2918 (Storage_Size_Variable
(Root_Type
(Ptyp
)), Loc
)))));
2920 elsif Present
(Associated_Storage_Pool
(Root_Type
(Ptyp
))) then
2923 Make_Function_Call
(Loc
,
2924 Name
=> New_Reference_To
(Find_Prim_Op
(Etype
(
2925 Associated_Storage_Pool
(Root_Type
(Ptyp
))),
2926 Attribute_Name
(N
)), Loc
),
2928 Parameter_Associations
=> New_List
(New_Reference_To
(
2929 Associated_Storage_Pool
(Root_Type
(Ptyp
)), Loc
)))));
2931 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
2934 Analyze_And_Resolve
(N
, Typ
);
2936 -- The case of a task type (an obsolescent feature) is handled the
2937 -- same way, seems as reasonable as anything, and it is what the
2938 -- ACVC tests (e.g. CD1009K) seem to expect.
2940 -- If there is no Storage_Size variable, then we return the default
2941 -- task stack size, otherwise, expand a Storage_Size attribute as
2944 -- Typ (Adjust_Storage_Size (taskZ))
2946 -- except for the case of a task object which has a Storage_Size
2949 -- Typ (Adjust_Storage_Size (taskV!(name)._Size))
2952 if not Present
(Storage_Size_Variable
(Ptyp
)) then
2955 Make_Function_Call
(Loc
,
2957 New_Occurrence_Of
(RTE
(RE_Default_Stack_Size
), Loc
))));
2960 if not (Is_Entity_Name
(Pref
) and then
2961 Is_Task_Type
(Entity
(Pref
))) and then
2962 Chars
(Last_Entity
(Corresponding_Record_Type
(Ptyp
))) =
2967 Make_Function_Call
(Loc
,
2968 Name
=> New_Occurrence_Of
(
2969 RTE
(RE_Adjust_Storage_Size
), Loc
),
2970 Parameter_Associations
=>
2972 Make_Selected_Component
(Loc
,
2974 Unchecked_Convert_To
(
2975 Corresponding_Record_Type
(Ptyp
),
2976 New_Copy_Tree
(Pref
)),
2978 Make_Identifier
(Loc
, Name_uSize
))))));
2980 -- Task not having Storage_Size pragma
2985 Make_Function_Call
(Loc
,
2986 Name
=> New_Occurrence_Of
(
2987 RTE
(RE_Adjust_Storage_Size
), Loc
),
2988 Parameter_Associations
=>
2991 Storage_Size_Variable
(Ptyp
), Loc
)))));
2994 Analyze_And_Resolve
(N
, Typ
);
3003 -- 1. Deal with enumeration types with holes
3004 -- 2. For floating-point, generate call to attribute function
3005 -- 3. For other cases, deal with constraint checking
3007 when Attribute_Succ
=> Succ
:
3009 Ptyp
: constant Entity_Id
:= Base_Type
(Etype
(Pref
));
3012 -- For enumeration types with non-standard representations, we
3013 -- expand typ'Succ (x) into
3015 -- Pos_To_Rep (Rep_To_Pos (x) + 1)
3017 if Is_Enumeration_Type
(Ptyp
)
3018 and then Present
(Enum_Pos_To_Rep
(Ptyp
))
3020 -- Add Boolean parameter True, to request program errror if
3021 -- we have a bad representation on our hands.
3023 Append_To
(Exprs
, New_Occurrence_Of
(Standard_True
, Loc
));
3026 Make_Indexed_Component
(Loc
,
3027 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Ptyp
), Loc
),
3028 Expressions
=> New_List
(
3031 Make_Function_Call
(Loc
,
3033 New_Reference_To
(TSS
(Ptyp
, Name_uRep_To_Pos
), Loc
),
3034 Parameter_Associations
=> Exprs
),
3035 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)))));
3037 Analyze_And_Resolve
(N
, Typ
);
3039 -- For floating-point, we transform 'Succ into a call to the Succ
3040 -- floating-point attribute function in Fat_xxx (xxx is root type)
3042 elsif Is_Floating_Point_Type
(Ptyp
) then
3043 Expand_Fpt_Attribute_R
(N
);
3044 Analyze_And_Resolve
(N
, Typ
);
3046 -- For modular types, nothing to do (no overflow, since wraps)
3048 elsif Is_Modular_Integer_Type
(Ptyp
) then
3051 -- For other types, if range checking is enabled, we must generate
3052 -- a check if overflow checking is enabled.
3054 elsif not Overflow_Checks_Suppressed
(Ptyp
) then
3055 Expand_Pred_Succ
(N
);
3063 -- Transforms X'Tag into a direct reference to the tag of X
3065 when Attribute_Tag
=> Tag
:
3068 Prefix_Is_Type
: Boolean;
3071 if Is_Entity_Name
(Pref
) and then Is_Type
(Entity
(Pref
)) then
3072 Ttyp
:= Entity
(Pref
);
3073 Prefix_Is_Type
:= True;
3075 Ttyp
:= Etype
(Pref
);
3076 Prefix_Is_Type
:= False;
3079 if Is_Class_Wide_Type
(Ttyp
) then
3080 Ttyp
:= Root_Type
(Ttyp
);
3083 Ttyp
:= Underlying_Type
(Ttyp
);
3085 if Prefix_Is_Type
then
3087 Unchecked_Convert_To
(RTE
(RE_Tag
),
3088 New_Reference_To
(Access_Disp_Table
(Ttyp
), Loc
)));
3092 Make_Selected_Component
(Loc
,
3093 Prefix
=> Relocate_Node
(Pref
),
3095 New_Reference_To
(Tag_Component
(Ttyp
), Loc
)));
3098 Analyze_And_Resolve
(N
, RTE
(RE_Tag
));
3105 -- Transforms 'Terminated attribute into a call to Terminated function.
3107 when Attribute_Terminated
=> Terminated
:
3109 if Restricted_Profile
then
3111 Build_Call_With_Task
(Pref
, RTE
(RE_Restricted_Terminated
)));
3115 Build_Call_With_Task
(Pref
, RTE
(RE_Terminated
)));
3118 Analyze_And_Resolve
(N
, Standard_Boolean
);
3125 -- Transforms System'To_Address (X) into unchecked conversion
3126 -- from (integral) type of X to type address.
3128 when Attribute_To_Address
=>
3130 Unchecked_Convert_To
(RTE
(RE_Address
),
3131 Relocate_Node
(First
(Exprs
))));
3132 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
3138 -- Transforms 'Truncation into a call to the floating-point attribute
3139 -- function Truncation in Fat_xxx (where xxx is the root type)
3141 when Attribute_Truncation
=>
3142 Expand_Fpt_Attribute_R
(N
);
3144 -----------------------
3145 -- Unbiased_Rounding --
3146 -----------------------
3148 -- Transforms 'Unbiased_Rounding into a call to the floating-point
3149 -- attribute function Unbiased_Rounding in Fat_xxx (where xxx is the
3152 when Attribute_Unbiased_Rounding
=>
3153 Expand_Fpt_Attribute_R
(N
);
3155 ----------------------
3156 -- Unchecked_Access --
3157 ----------------------
3159 when Attribute_Unchecked_Access
=>
3160 Expand_Access_To_Type
(N
);
3166 when Attribute_UET_Address
=> UET_Address
: declare
3167 Ent
: constant Entity_Id
:=
3168 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
3172 Make_Object_Declaration
(Loc
,
3173 Defining_Identifier
=> Ent
,
3174 Aliased_Present
=> True,
3175 Object_Definition
=>
3176 New_Occurrence_Of
(RTE
(RE_Address
), Loc
)));
3178 -- Construct name __gnat_xxx__SDP, where xxx is the unit name
3179 -- in normal external form.
3181 Get_External_Unit_Name_String
(Get_Unit_Name
(Pref
));
3182 Name_Buffer
(1 + 7 .. Name_Len
+ 7) := Name_Buffer
(1 .. Name_Len
);
3183 Name_Len
:= Name_Len
+ 7;
3184 Name_Buffer
(1 .. 7) := "__gnat_";
3185 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 5) := "__SDP";
3186 Name_Len
:= Name_Len
+ 5;
3188 Set_Is_Imported
(Ent
);
3189 Set_Interface_Name
(Ent
,
3190 Make_String_Literal
(Loc
,
3191 Strval
=> String_From_Name_Buffer
));
3194 Make_Attribute_Reference
(Loc
,
3195 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3196 Attribute_Name
=> Name_Address
));
3198 Analyze_And_Resolve
(N
, Typ
);
3201 -------------------------
3202 -- Unrestricted_Access --
3203 -------------------------
3205 when Attribute_Unrestricted_Access
=>
3206 Expand_Access_To_Type
(N
);
3212 -- The processing for VADS_Size is shared with Size
3218 -- For enumeration types with a standard representation, and for all
3219 -- other types, Val is handled by Gigi. For enumeration types with
3220 -- a non-standard representation we use the _Pos_To_Rep array that
3221 -- was created when the type was frozen.
3223 when Attribute_Val
=> Val
:
3225 Etyp
: constant Entity_Id
:= Base_Type
(Entity
(Pref
));
3228 if Is_Enumeration_Type
(Etyp
)
3229 and then Present
(Enum_Pos_To_Rep
(Etyp
))
3232 Make_Indexed_Component
(Loc
,
3233 Prefix
=> New_Reference_To
(Enum_Pos_To_Rep
(Etyp
), Loc
),
3234 Expressions
=> New_List
(
3235 Convert_To
(Standard_Integer
,
3236 Relocate_Node
(First
(Exprs
))))));
3238 Analyze_And_Resolve
(N
, Typ
);
3246 -- The code for valid is dependent on the particular types involved.
3247 -- See separate sections below for the generated code in each case.
3249 when Attribute_Valid
=> Valid
:
3251 Ptyp
: constant Entity_Id
:= Etype
(Pref
);
3252 Btyp
: Entity_Id
:= Base_Type
(Ptyp
);
3255 function Make_Range_Test
return Node_Id
;
3256 -- Build the code for a range test of the form
3257 -- Btyp!(Pref) >= Btyp!(Ptyp'First)
3259 -- Btyp!(Pref) <= Btyp!(Ptyp'Last)
3261 function Make_Range_Test
return Node_Id
is
3268 Unchecked_Convert_To
(Btyp
, Duplicate_Subexpr
(Pref
)),
3271 Unchecked_Convert_To
(Btyp
,
3272 Make_Attribute_Reference
(Loc
,
3273 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3274 Attribute_Name
=> Name_First
))),
3279 Unchecked_Convert_To
(Btyp
, Duplicate_Subexpr
(Pref
)),
3282 Unchecked_Convert_To
(Btyp
,
3283 Make_Attribute_Reference
(Loc
,
3284 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3285 Attribute_Name
=> Name_Last
))));
3286 end Make_Range_Test
;
3288 -- Start of processing for Attribute_Valid
3291 -- Floating-point case. This case is handled by the Valid attribute
3292 -- code in the floating-point attribute run-time library.
3294 if Is_Floating_Point_Type
(Ptyp
) then
3296 Rtp
: constant Entity_Id
:= Root_Type
(Etype
(Pref
));
3299 Expand_Fpt_Attribute
(N
, Rtp
, New_List
(
3300 Make_Attribute_Reference
(Loc
,
3301 Prefix
=> Unchecked_Convert_To
(Rtp
, Pref
),
3302 Attribute_Name
=> Name_Unrestricted_Access
)));
3304 -- One more task, we still need a range check. Required
3305 -- only if we have a constraint, since the Valid routine
3306 -- catches infinities properly (infinities are never valid).
3308 -- The way we do the range check is simply to create the
3309 -- expression: Valid (N) and then Base_Type(Pref) in Typ.
3311 if not Subtypes_Statically_Match
(Ptyp
, Btyp
) then
3314 Left_Opnd
=> Relocate_Node
(N
),
3317 Left_Opnd
=> Convert_To
(Btyp
, Pref
),
3318 Right_Opnd
=> New_Occurrence_Of
(Ptyp
, Loc
))));
3322 -- Enumeration type with holes
3324 -- For enumeration types with holes, the Pos value constructed by
3325 -- the Enum_Rep_To_Pos function built in Exp_Ch3 called with a
3326 -- second argument of False returns minus one for an invalid value,
3327 -- and the non-negative pos value for a valid value, so the
3328 -- expansion of X'Valid is simply:
3330 -- type(X)'Pos (X) >= 0
3332 -- We can't quite generate it that way because of the requirement
3333 -- for the non-standard second argument of False, so we have to
3334 -- explicitly create:
3336 -- _rep_to_pos (X, False) >= 0
3338 -- If we have an enumeration subtype, we also check that the
3339 -- value is in range:
3341 -- _rep_to_pos (X, False) >= 0
3343 -- (X >= type(X)'First and then type(X)'Last <= X)
3345 elsif Is_Enumeration_Type
(Ptyp
)
3346 and then Present
(Enum_Pos_To_Rep
(Base_Type
(Ptyp
)))
3351 Make_Function_Call
(Loc
,
3354 (TSS
(Base_Type
(Ptyp
), Name_uRep_To_Pos
), Loc
),
3355 Parameter_Associations
=> New_List
(
3357 New_Occurrence_Of
(Standard_False
, Loc
))),
3358 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
3362 (Type_Low_Bound
(Ptyp
) /= Type_Low_Bound
(Btyp
)
3364 Type_High_Bound
(Ptyp
) /= Type_High_Bound
(Btyp
))
3366 -- The call to Make_Range_Test will create declarations
3367 -- that need a proper insertion point, but Pref is now
3368 -- attached to a node with no ancestor. Attach to tree
3369 -- even if it is to be rewritten below.
3371 Set_Parent
(Tst
, Parent
(N
));
3375 Left_Opnd
=> Make_Range_Test
,
3381 -- Fortran convention booleans
3383 -- For the very special case of Fortran convention booleans, the
3384 -- value is always valid, since it is an integer with the semantics
3385 -- that non-zero is true, and any value is permissible.
3387 elsif Is_Boolean_Type
(Ptyp
)
3388 and then Convention
(Ptyp
) = Convention_Fortran
3390 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
3392 -- For biased representations, we will be doing an unchecked
3393 -- conversion without unbiasing the result. That means that
3394 -- the range test has to take this into account, and the
3395 -- proper form of the test is:
3397 -- Btyp!(Pref) < Btyp!(Ptyp'Range_Length)
3399 elsif Has_Biased_Representation
(Ptyp
) then
3400 Btyp
:= RTE
(RE_Unsigned_32
);
3404 Unchecked_Convert_To
(Btyp
, Duplicate_Subexpr
(Pref
)),
3406 Unchecked_Convert_To
(Btyp
,
3407 Make_Attribute_Reference
(Loc
,
3408 Prefix
=> New_Occurrence_Of
(Ptyp
, Loc
),
3409 Attribute_Name
=> Name_Range_Length
))));
3411 -- For all other scalar types, what we want logically is a
3414 -- X in type(X)'First .. type(X)'Last
3416 -- But that's precisely what won't work because of possible
3417 -- unwanted optimization (and indeed the basic motivation for
3418 -- the Valid attribute -is exactly that this test does not work.
3419 -- What will work is:
3421 -- Btyp!(X) >= Btyp!(type(X)'First)
3423 -- Btyp!(X) <= Btyp!(type(X)'Last)
3425 -- where Btyp is an integer type large enough to cover the full
3426 -- range of possible stored values (i.e. it is chosen on the basis
3427 -- of the size of the type, not the range of the values). We write
3428 -- this as two tests, rather than a range check, so that static
3429 -- evaluation will easily remove either or both of the checks if
3430 -- they can be -statically determined to be true (this happens
3431 -- when the type of X is static and the range extends to the full
3432 -- range of stored values).
3434 -- Unsigned types. Note: it is safe to consider only whether the
3435 -- subtype is unsigned, since we will in that case be doing all
3436 -- unsigned comparisons based on the subtype range. Since we use
3437 -- the actual subtype object size, this is appropriate.
3439 -- For example, if we have
3441 -- subtype x is integer range 1 .. 200;
3442 -- for x'Object_Size use 8;
3444 -- Now the base type is signed, but objects of this type are 8
3445 -- bits unsigned, and doing an unsigned test of the range 1 to
3446 -- 200 is correct, even though a value greater than 127 looks
3447 -- signed to a signed comparison.
3449 elsif Is_Unsigned_Type
(Ptyp
) then
3450 if Esize
(Ptyp
) <= 32 then
3451 Btyp
:= RTE
(RE_Unsigned_32
);
3453 Btyp
:= RTE
(RE_Unsigned_64
);
3456 Rewrite
(N
, Make_Range_Test
);
3461 if Esize
(Ptyp
) <= Esize
(Standard_Integer
) then
3462 Btyp
:= Standard_Integer
;
3464 Btyp
:= Universal_Integer
;
3467 Rewrite
(N
, Make_Range_Test
);
3470 Analyze_And_Resolve
(N
, Standard_Boolean
);
3477 -- Value attribute is handled in separate unti Exp_Imgv
3479 when Attribute_Value
=>
3480 Exp_Imgv
.Expand_Value_Attribute
(N
);
3486 -- The processing for Value_Size shares the processing for Size
3492 -- The processing for Version shares the processing for Body_Version
3498 -- We expand typ'Wide_Image (X) into
3500 -- String_To_Wide_String
3501 -- (typ'Image (X), Wide_Character_Encoding_Method)
3503 -- This works in all cases because String_To_Wide_String converts any
3504 -- wide character escape sequences resulting from the Image call to the
3505 -- proper Wide_Character equivalent
3507 -- not quite right for typ = Wide_Character ???
3509 when Attribute_Wide_Image
=> Wide_Image
:
3512 Make_Function_Call
(Loc
,
3513 Name
=> New_Reference_To
(RTE
(RE_String_To_Wide_String
), Loc
),
3514 Parameter_Associations
=> New_List
(
3515 Make_Attribute_Reference
(Loc
,
3517 Attribute_Name
=> Name_Image
,
3518 Expressions
=> Exprs
),
3520 Make_Integer_Literal
(Loc
,
3521 Intval
=> Int
(Wide_Character_Encoding_Method
)))));
3523 Analyze_And_Resolve
(N
, Standard_Wide_String
);
3530 -- We expand typ'Wide_Value (X) into
3533 -- (Wide_String_To_String (X, Wide_Character_Encoding_Method))
3535 -- Wide_String_To_String is a runtime function that converts its wide
3536 -- string argument to String, converting any non-translatable characters
3537 -- into appropriate escape sequences. This preserves the required
3538 -- semantics of Wide_Value in all cases, and results in a very simple
3539 -- implementation approach.
3541 -- It's not quite right where typ = Wide_Character, because the encoding
3542 -- method may not cover the whole character type ???
3544 when Attribute_Wide_Value
=> Wide_Value
:
3547 Make_Attribute_Reference
(Loc
,
3549 Attribute_Name
=> Name_Value
,
3551 Expressions
=> New_List
(
3552 Make_Function_Call
(Loc
,
3554 New_Reference_To
(RTE
(RE_Wide_String_To_String
), Loc
),
3556 Parameter_Associations
=> New_List
(
3557 Relocate_Node
(First
(Exprs
)),
3558 Make_Integer_Literal
(Loc
,
3559 Intval
=> Int
(Wide_Character_Encoding_Method
)))))));
3561 Analyze_And_Resolve
(N
, Typ
);
3568 -- Wide_Width attribute is handled in separate unit Exp_Imgv
3570 when Attribute_Wide_Width
=>
3571 Exp_Imgv
.Expand_Width_Attribute
(N
, Wide
=> True);
3577 -- Width attribute is handled in separate unit Exp_Imgv
3579 when Attribute_Width
=>
3580 Exp_Imgv
.Expand_Width_Attribute
(N
, Wide
=> False);
3586 when Attribute_Write
=> Write
: declare
3587 P_Type
: constant Entity_Id
:= Entity
(Pref
);
3588 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
3596 -- If no underlying type, we have an error that will be diagnosed
3597 -- elsewhere, so here we just completely ignore the expansion.
3603 -- The simple case, if there is a TSS for Write, just call it
3605 Pname
:= Find_Inherited_TSS
(P_Type
, Name_uWrite
);
3607 if Present
(Pname
) then
3611 -- If there is a Stream_Convert pragma, use it, we rewrite
3613 -- sourcetyp'Output (stream, Item)
3617 -- strmtyp'Output (Stream, strmwrite (acttyp (Item)));
3619 -- where strmwrite is the given Write function that converts
3620 -- an argument of type sourcetyp or a type acctyp, from which
3621 -- it is derived to type strmtyp. The conversion to acttyp is
3622 -- required for the derived case.
3626 (Implementation_Base_Type
(P_Type
), Name_Stream_Convert
);
3628 if Present
(Prag
) then
3630 Next
(Next
(First
(Pragma_Argument_Associations
(Prag
))));
3631 Wfunc
:= Entity
(Expression
(Arg3
));
3634 Make_Attribute_Reference
(Loc
,
3635 Prefix
=> New_Occurrence_Of
(Etype
(Wfunc
), Loc
),
3636 Attribute_Name
=> Name_Output
,
3637 Expressions
=> New_List
(
3638 Relocate_Node
(First
(Exprs
)),
3639 Make_Function_Call
(Loc
,
3640 Name
=> New_Occurrence_Of
(Wfunc
, Loc
),
3641 Parameter_Associations
=> New_List
(
3642 Convert_To
(Etype
(First_Formal
(Wfunc
)),
3643 Relocate_Node
(Next
(First
(Exprs
)))))))));
3648 -- For elementary types, we call the W_xxx routine directly
3650 elsif Is_Elementary_Type
(U_Type
) then
3651 Rewrite
(N
, Build_Elementary_Write_Call
(N
));
3657 elsif Is_Array_Type
(U_Type
) then
3658 Build_Array_Write_Procedure
(N
, U_Type
, Decl
, Pname
);
3659 Compile_Stream_Body_In_Scope
(N
, Decl
, U_Type
, Check
=> False);
3661 -- Tagged type case, use the primitive Write function. Note that
3662 -- this will dispatch in the class-wide case which is what we want
3664 elsif Is_Tagged_Type
(U_Type
) then
3665 Pname
:= Find_Prim_Op
(U_Type
, Name_uWrite
);
3667 -- All other record type cases, including protected records.
3668 -- The latter only arise for expander generated code for
3669 -- handling shared passive partition access.
3673 (Is_Record_Type
(U_Type
) or else Is_Protected_Type
(U_Type
));
3675 if Has_Discriminants
(U_Type
)
3677 (Discriminant_Default_Value
(First_Discriminant
(U_Type
)))
3679 Build_Mutable_Record_Write_Procedure
3680 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
3683 Build_Record_Write_Procedure
3684 (Loc
, Base_Type
(U_Type
), Decl
, Pname
);
3687 Insert_Action
(N
, Decl
);
3691 -- If we fall through, Pname is the procedure to be called
3693 Rewrite_Stream_Proc_Call
(Pname
);
3696 -- Component_Size is handled by Gigi, unless the component size is
3697 -- known at compile time, which is always true in the packed array
3698 -- case. It is important that the packed array case is handled in
3699 -- the front end (see Eval_Attribute) since Gigi would otherwise
3700 -- get confused by the equivalent packed array type.
3702 when Attribute_Component_Size
=>
3705 -- The following attributes are handled by Gigi (except that static
3706 -- cases have already been evaluated by the semantics, but in any
3707 -- case Gigi should not count on that).
3709 -- In addition Gigi handles the non-floating-point cases of Pred
3710 -- and Succ (including the fixed-point cases, which can just be
3711 -- treated as integer increment/decrement operations)
3713 -- Gigi also handles the non-class-wide cases of Size
3715 when Attribute_Bit_Order |
3716 Attribute_Code_Address |
3717 Attribute_Definite |
3719 Attribute_Mechanism_Code |
3721 Attribute_Null_Parameter |
3722 Attribute_Passed_By_Reference
=>
3725 -- The following attributes are also handled by Gigi, but return a
3726 -- universal integer result, so may need a conversion for checking
3727 -- that the result is in range.
3729 when Attribute_Aft |
3730 Attribute_Alignment |
3732 Attribute_Max_Size_In_Storage_Elements
3734 Apply_Universal_Integer_Attribute_Checks
(N
);
3736 -- The following attributes should not appear at this stage, since they
3737 -- have already been handled by the analyzer (and properly rewritten
3738 -- with corresponding values or entities to represent the right values)
3740 when Attribute_Abort_Signal |
3741 Attribute_Address_Size |
3744 Attribute_Default_Bit_Order |
3750 Attribute_Has_Discriminants |
3752 Attribute_Machine_Emax |
3753 Attribute_Machine_Emin |
3754 Attribute_Machine_Mantissa |
3755 Attribute_Machine_Overflows |
3756 Attribute_Machine_Radix |
3757 Attribute_Machine_Rounds |
3758 Attribute_Max_Interrupt_Priority |
3759 Attribute_Max_Priority |
3760 Attribute_Maximum_Alignment |
3761 Attribute_Model_Emin |
3762 Attribute_Model_Epsilon |
3763 Attribute_Model_Mantissa |
3764 Attribute_Model_Small |
3766 Attribute_Partition_ID |
3768 Attribute_Safe_Emax |
3769 Attribute_Safe_First |
3770 Attribute_Safe_Large |
3771 Attribute_Safe_Last |
3772 Attribute_Safe_Small |
3774 Attribute_Signed_Zeros |
3776 Attribute_Storage_Unit |
3778 Attribute_Type_Class |
3779 Attribute_Universal_Literal_String |
3780 Attribute_Wchar_T_Size |
3781 Attribute_Word_Size
=>
3783 raise Program_Error
;
3785 -- The Asm_Input and Asm_Output attributes are not expanded at this
3786 -- stage, but will be eliminated in the expansion of the Asm call,
3787 -- see Exp_Intr for details. So Gigi will never see these either.
3789 when Attribute_Asm_Input |
3790 Attribute_Asm_Output
=>
3796 end Expand_N_Attribute_Reference
;
3798 ----------------------
3799 -- Expand_Pred_Succ --
3800 ----------------------
3802 -- For typ'Pred (exp), we generate the check
3804 -- [constraint_error when exp = typ'Base'First]
3806 -- Similarly, for typ'Succ (exp), we generate the check
3808 -- [constraint_error when exp = typ'Base'Last]
3810 -- These checks are not generated for modular types, since the proper
3811 -- semantics for Succ and Pred on modular types is to wrap, not raise CE.
3813 procedure Expand_Pred_Succ
(N
: Node_Id
) is
3814 Loc
: constant Source_Ptr
:= Sloc
(N
);
3818 if Attribute_Name
(N
) = Name_Pred
then
3825 Make_Raise_Constraint_Error
(Loc
,
3828 Left_Opnd
=> Duplicate_Subexpr
(First
(Expressions
(N
))),
3830 Make_Attribute_Reference
(Loc
,
3832 New_Reference_To
(Base_Type
(Etype
(Prefix
(N
))), Loc
),
3833 Attribute_Name
=> Cnam
))));
3835 end Expand_Pred_Succ
;
3837 ------------------------
3838 -- Find_Inherited_TSS --
3839 ------------------------
3841 function Find_Inherited_TSS
3843 Nam
: Name_Id
) return Entity_Id
3845 P_Type
: Entity_Id
:= Typ
;
3849 Proc
:= TSS
(Base_Type
(Typ
), Nam
);
3851 -- Check first if there is a TSS given for the type itself.
3853 if Present
(Proc
) then
3857 -- If Typ is a derived type, it may inherit attributes from some
3858 -- ancestor which is not the ultimate underlying one.
3860 if Is_Derived_Type
(P_Type
) then
3862 while Is_Derived_Type
(P_Type
) loop
3863 Proc
:= TSS
(Base_Type
(Etype
(Typ
)), Nam
);
3865 if Present
(Proc
) then
3868 P_Type
:= Base_Type
(Etype
(P_Type
));
3873 -- If nothing else, use the TSS of the root type.
3875 return TSS
(Base_Type
(Underlying_Type
(Typ
)), Nam
);
3876 end Find_Inherited_TSS
;
3878 -----------------------
3879 -- Get_Index_Subtype --
3880 -----------------------
3882 function Get_Index_Subtype
(N
: Node_Id
) return Node_Id
is
3883 P_Type
: Entity_Id
:= Etype
(Prefix
(N
));
3888 if Is_Access_Type
(P_Type
) then
3889 P_Type
:= Designated_Type
(P_Type
);
3892 if No
(Expressions
(N
)) then
3895 J
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
3898 Indx
:= First_Index
(P_Type
);
3904 return Etype
(Indx
);
3905 end Get_Index_Subtype
;
3907 ---------------------------------
3908 -- Is_Constrained_Packed_Array --
3909 ---------------------------------
3911 function Is_Constrained_Packed_Array
(Typ
: Entity_Id
) return Boolean is
3912 Arr
: Entity_Id
:= Typ
;
3915 if Is_Access_Type
(Arr
) then
3916 Arr
:= Designated_Type
(Arr
);
3919 return Is_Array_Type
(Arr
)
3920 and then Is_Constrained
(Arr
)
3921 and then Present
(Packed_Array_Type
(Arr
));
3922 end Is_Constrained_Packed_Array
;