1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Einfo
; use Einfo
;
29 with Namet
; use Namet
;
30 with Nlists
; use Nlists
;
31 with Nmake
; use Nmake
;
32 with Rtsfind
; use Rtsfind
;
33 with Sinfo
; use Sinfo
;
34 with Snames
; use Snames
;
35 with Stand
; use Stand
;
36 with Tbuild
; use Tbuild
;
37 with Ttypes
; use Ttypes
;
38 with Exp_Tss
; use Exp_Tss
;
39 with Uintp
; use Uintp
;
41 package body Exp_Strm
is
43 -----------------------
44 -- Local Subprograms --
45 -----------------------
47 procedure Build_Array_Read_Write_Procedure
53 -- Common routine shared to build either an array Read procedure or an
54 -- array Write procedure, Nam is Name_Read or Name_Write to select which.
55 -- Pnam is the defining identifier for the constructed procedure. The
56 -- other parameters are as for Build_Array_Read_Procedure except that
57 -- the first parameter Nod supplies the Sloc to be used to generate code.
59 procedure Build_Record_Read_Write_Procedure
65 -- Common routine shared to build a record Read Write procedure, Nam
66 -- is Name_Read or Name_Write to select which. Pnam is the defining
67 -- identifier for the constructed procedure. The other parameters are
68 -- as for Build_Record_Read_Procedure.
70 procedure Build_Stream_Function
77 -- Called to build an array or record stream function. The first three
78 -- arguments are the same as Build_Record_Or_Elementary_Input_Function.
79 -- Decls and Stms are the declarations and statements for the body and
80 -- The parameter Fnam is the name of the constructed function.
82 function Has_Stream_Standard_Rep
(U_Type
: Entity_Id
) return Boolean;
83 -- This function is used to test U_Type, which is a type
84 -- Returns True if U_Type has a standard representation for stream
85 -- purposes, i.e. there is no non-standard enumeration representation
86 -- clause, and the size of the first subtype is the same as the size
89 function Make_Stream_Subprogram_Name
92 Nam
: TSS_Name_Type
) return Entity_Id
;
93 -- Return the entity that identifies the stream subprogram for type Typ
94 -- that is identified by the given Nam. This procedure deals with the
95 -- difference between tagged types (where a single subprogram associated
96 -- with the type is generated) and all other cases (where a subprogram
97 -- is generated at the point of the stream attribute reference). The
98 -- Loc parameter is used as the Sloc of the created entity.
100 function Stream_Base_Type
(E
: Entity_Id
) return Entity_Id
;
101 -- Stream attributes work on the basis of the base type except for the
102 -- array case. For the array case, we do not go to the base type, but
103 -- to the first subtype if it is constrained. This avoids problems with
104 -- incorrect conversions in the packed array case. Stream_Base_Type is
105 -- exactly this function (returns the base type, unless we have an array
106 -- type whose first subtype is constrained, in which case it returns the
109 --------------------------------
110 -- Build_Array_Input_Function --
111 --------------------------------
113 -- The function we build looks like
115 -- function typSI[_nnn] (S : access RST) return Typ is
116 -- L1 : constant Index_Type_1 := Index_Type_1'Input (S);
117 -- H1 : constant Index_Type_1 := Index_Type_1'Input (S);
118 -- L2 : constant Index_Type_2 := Index_Type_2'Input (S);
119 -- H2 : constant Index_Type_2 := Index_Type_2'Input (S);
121 -- Ln : constant Index_Type_n := Index_Type_n'Input (S);
122 -- Hn : constant Index_Type_n := Index_Type_n'Input (S);
124 -- V : Typ'Base (L1 .. H1, L2 .. H2, ... Ln .. Hn)
131 -- Note: the suffix [_nnn] is present for non-tagged types, where we
132 -- generate a local subprogram at the point of the occurrence of the
133 -- attribute reference, so the name must be unique.
135 procedure Build_Array_Input_Function
139 Fnam
: out Entity_Id
)
141 Dim
: constant Pos
:= Number_Dimensions
(Typ
);
152 Indx
:= First_Index
(Typ
);
154 for J
in 1 .. Dim
loop
155 Lnam
:= New_External_Name
('L', J
);
156 Hnam
:= New_External_Name
('H', J
);
159 Make_Object_Declaration
(Loc
,
160 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Lnam
),
161 Constant_Present
=> True,
162 Object_Definition
=> New_Occurrence_Of
(Etype
(Indx
), Loc
),
164 Make_Attribute_Reference
(Loc
,
166 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
167 Attribute_Name
=> Name_Input
,
168 Expressions
=> New_List
(Make_Identifier
(Loc
, Name_S
)))));
171 Make_Object_Declaration
(Loc
,
172 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Hnam
),
173 Constant_Present
=> True,
175 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
177 Make_Attribute_Reference
(Loc
,
179 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
180 Attribute_Name
=> Name_Input
,
181 Expressions
=> New_List
(Make_Identifier
(Loc
, Name_S
)))));
185 Low_Bound
=> Make_Identifier
(Loc
, Lnam
),
186 High_Bound
=> Make_Identifier
(Loc
, Hnam
)));
191 -- If the first subtype is constrained, use it directly. Otherwise
192 -- build a subtype indication with the proper bounds.
194 if Is_Constrained
(Stream_Base_Type
(Typ
)) then
196 Make_Object_Declaration
(Loc
,
197 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
199 New_Occurrence_Of
(Stream_Base_Type
(Typ
), Loc
)));
202 Make_Object_Declaration
(Loc
,
203 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
205 Make_Subtype_Indication
(Loc
,
207 New_Occurrence_Of
(Stream_Base_Type
(Typ
), Loc
),
209 Make_Index_Or_Discriminant_Constraint
(Loc
,
210 Constraints
=> Ranges
))));
214 Make_Attribute_Reference
(Loc
,
215 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
216 Attribute_Name
=> Name_Read
,
217 Expressions
=> New_List
(
218 Make_Identifier
(Loc
, Name_S
),
219 Make_Identifier
(Loc
, Name_V
))),
221 Make_Return_Statement
(Loc
,
222 Expression
=> Make_Identifier
(Loc
, Name_V
)));
225 Make_Defining_Identifier
(Loc
,
226 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Input
));
228 Build_Stream_Function
(Loc
, Typ
, Decl
, Fnam
, Decls
, Stms
);
229 end Build_Array_Input_Function
;
231 ----------------------------------
232 -- Build_Array_Output_Procedure --
233 ----------------------------------
235 procedure Build_Array_Output_Procedure
239 Pnam
: out Entity_Id
)
245 -- Build series of statements to output bounds
247 Indx
:= First_Index
(Typ
);
250 for J
in 1 .. Number_Dimensions
(Typ
) loop
252 Make_Attribute_Reference
(Loc
,
254 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
255 Attribute_Name
=> Name_Write
,
256 Expressions
=> New_List
(
257 Make_Identifier
(Loc
, Name_S
),
258 Make_Attribute_Reference
(Loc
,
259 Prefix
=> Make_Identifier
(Loc
, Name_V
),
260 Attribute_Name
=> Name_First
,
261 Expressions
=> New_List
(
262 Make_Integer_Literal
(Loc
, J
))))));
265 Make_Attribute_Reference
(Loc
,
267 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
268 Attribute_Name
=> Name_Write
,
269 Expressions
=> New_List
(
270 Make_Identifier
(Loc
, Name_S
),
271 Make_Attribute_Reference
(Loc
,
272 Prefix
=> Make_Identifier
(Loc
, Name_V
),
273 Attribute_Name
=> Name_Last
,
274 Expressions
=> New_List
(
275 Make_Integer_Literal
(Loc
, J
))))));
280 -- Append Write attribute to write array elements
283 Make_Attribute_Reference
(Loc
,
284 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
285 Attribute_Name
=> Name_Write
,
286 Expressions
=> New_List
(
287 Make_Identifier
(Loc
, Name_S
),
288 Make_Identifier
(Loc
, Name_V
))));
291 Make_Defining_Identifier
(Loc
,
292 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Output
));
294 Build_Stream_Procedure
(Loc
, Typ
, Decl
, Pnam
, Stms
, False);
295 end Build_Array_Output_Procedure
;
297 --------------------------------
298 -- Build_Array_Read_Procedure --
299 --------------------------------
301 procedure Build_Array_Read_Procedure
305 Pnam
: out Entity_Id
)
307 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
311 Make_Defining_Identifier
(Loc
,
312 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Read
));
313 Build_Array_Read_Write_Procedure
(Nod
, Typ
, Decl
, Pnam
, Name_Read
);
314 end Build_Array_Read_Procedure
;
316 --------------------------------------
317 -- Build_Array_Read_Write_Procedure --
318 --------------------------------------
320 -- The form of the array read/write procedure is as follows:
322 -- procedure pnam (S : access RST, V : [out] Typ) is
324 -- for L1 in V'Range (1) loop
325 -- for L2 in V'Range (2) loop
327 -- for Ln in V'Range (n) loop
328 -- Component_Type'Read/Write (S, V (L1, L2, .. Ln));
335 -- The out keyword for V is supplied in the Read case
337 procedure Build_Array_Read_Write_Procedure
344 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
345 Ndim
: constant Pos
:= Number_Dimensions
(Typ
);
346 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
353 -- First build the inner attribute call
357 for J
in 1 .. Ndim
loop
358 Append_To
(Exl
, Make_Identifier
(Loc
, New_External_Name
('L', J
)));
362 Make_Attribute_Reference
(Loc
,
363 Prefix
=> New_Occurrence_Of
(Stream_Base_Type
(Ctyp
), Loc
),
364 Attribute_Name
=> Nam
,
365 Expressions
=> New_List
(
366 Make_Identifier
(Loc
, Name_S
),
367 Make_Indexed_Component
(Loc
,
368 Prefix
=> Make_Identifier
(Loc
, Name_V
),
369 Expressions
=> Exl
)));
371 -- The corresponding stream attribute for the component type of the
372 -- array may be user-defined, and be frozen after the type for which
373 -- we are generating the stream subprogram. In that case, freeze the
374 -- stream attribute of the component type, whose declaration could not
375 -- generate any additional freezing actions in any case. See 5509-003.
377 if Nam
= Name_Read
then
378 RW
:= TSS
(Base_Type
(Ctyp
), TSS_Stream_Read
);
380 RW
:= TSS
(Base_Type
(Ctyp
), TSS_Stream_Write
);
384 and then not Is_Frozen
(RW
)
389 -- Now this is the big loop to wrap that statement up in a sequence
390 -- of loops. The first time around, Stm is the attribute call. The
391 -- second and subsequent times, Stm is an inner loop.
393 for J
in 1 .. Ndim
loop
395 Make_Implicit_Loop_Statement
(Nod
,
397 Make_Iteration_Scheme
(Loc
,
398 Loop_Parameter_Specification
=>
399 Make_Loop_Parameter_Specification
(Loc
,
400 Defining_Identifier
=>
401 Make_Defining_Identifier
(Loc
,
402 Chars
=> New_External_Name
('L', Ndim
- J
+ 1)),
404 Discrete_Subtype_Definition
=>
405 Make_Attribute_Reference
(Loc
,
406 Prefix
=> Make_Identifier
(Loc
, Name_V
),
407 Attribute_Name
=> Name_Range
,
409 Expressions
=> New_List
(
410 Make_Integer_Literal
(Loc
, Ndim
- J
+ 1))))),
412 Statements
=> New_List
(Stm
));
416 Build_Stream_Procedure
417 (Loc
, Typ
, Decl
, Pnam
, New_List
(Stm
), Nam
= Name_Read
);
418 end Build_Array_Read_Write_Procedure
;
420 ---------------------------------
421 -- Build_Array_Write_Procedure --
422 ---------------------------------
424 procedure Build_Array_Write_Procedure
428 Pnam
: out Entity_Id
)
430 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
434 Make_Defining_Identifier
(Loc
,
435 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Write
));
436 Build_Array_Read_Write_Procedure
(Nod
, Typ
, Decl
, Pnam
, Name_Write
);
437 end Build_Array_Write_Procedure
;
439 ---------------------------------
440 -- Build_Elementary_Input_Call --
441 ---------------------------------
443 function Build_Elementary_Input_Call
(N
: Node_Id
) return Node_Id
is
444 Loc
: constant Source_Ptr
:= Sloc
(N
);
445 P_Type
: constant Entity_Id
:= Entity
(Prefix
(N
));
446 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
447 Rt_Type
: constant Entity_Id
:= Root_Type
(U_Type
);
448 FST
: constant Entity_Id
:= First_Subtype
(U_Type
);
449 P_Size
: constant Uint
:= Esize
(FST
);
451 Strm
: constant Node_Id
:= First
(Expressions
(N
));
452 Targ
: constant Node_Id
:= Next
(Strm
);
456 -- Check first for Boolean and Character. These are enumeration types,
457 -- but we treat them specially, since they may require special handling
458 -- in the transfer protocol. However, this special handling only applies
459 -- if they have standard representation, otherwise they are treated like
460 -- any other enumeration type.
462 if Rt_Type
= Standard_Boolean
463 and then Has_Stream_Standard_Rep
(U_Type
)
467 elsif Rt_Type
= Standard_Character
468 and then Has_Stream_Standard_Rep
(U_Type
)
472 elsif Rt_Type
= Standard_Wide_Character
473 and then Has_Stream_Standard_Rep
(U_Type
)
477 -- Floating point types
479 elsif Is_Floating_Point_Type
(U_Type
) then
481 if Rt_Type
= Standard_Short_Float
then
484 elsif Rt_Type
= Standard_Float
then
487 elsif Rt_Type
= Standard_Long_Float
then
490 else pragma Assert
(Rt_Type
= Standard_Long_Long_Float
);
494 -- Signed integer types. Also includes signed fixed-point types and
495 -- enumeration types with a signed representation.
497 -- Note on signed integer types. We do not consider types as signed for
498 -- this purpose if they have no negative numbers, or if they have biased
499 -- representation. The reason is that the value in either case basically
500 -- represents an unsigned value.
502 -- For example, consider:
504 -- type W is range 0 .. 2**32 - 1;
505 -- for W'Size use 32;
507 -- This is a signed type, but the representation is unsigned, and may
508 -- be outside the range of a 32-bit signed integer, so this must be
509 -- treated as 32-bit unsigned.
511 -- Similarly, if we have
513 -- type W is range -1 .. +254;
516 -- then the representation is unsigned
518 elsif not Is_Unsigned_Type
(FST
)
520 (Is_Fixed_Point_Type
(U_Type
)
522 Is_Enumeration_Type
(U_Type
)
524 (Is_Signed_Integer_Type
(U_Type
)
525 and then not Has_Biased_Representation
(FST
)))
527 if P_Size
<= Standard_Short_Short_Integer_Size
then
530 elsif P_Size
<= Standard_Short_Integer_Size
then
533 elsif P_Size
<= Standard_Integer_Size
then
536 elsif P_Size
<= Standard_Long_Integer_Size
then
543 -- Unsigned integer types, also includes unsigned fixed-point types
544 -- and enumeration types with an unsigned representation (note that
545 -- we know they are unsigned because we already tested for signed).
547 -- Also includes signed integer types that are unsigned in the sense
548 -- that they do not include negative numbers. See above for details.
550 elsif Is_Modular_Integer_Type
(U_Type
)
551 or else Is_Fixed_Point_Type
(U_Type
)
552 or else Is_Enumeration_Type
(U_Type
)
553 or else Is_Signed_Integer_Type
(U_Type
)
555 if P_Size
<= Standard_Short_Short_Integer_Size
then
558 elsif P_Size
<= Standard_Short_Integer_Size
then
561 elsif P_Size
<= Standard_Integer_Size
then
564 elsif P_Size
<= Standard_Long_Integer_Size
then
571 else pragma Assert
(Is_Access_Type
(U_Type
));
572 if P_Size
> System_Address_Size
then
579 -- Call the function, and do an unchecked conversion of the result
580 -- to the actual type of the prefix. If the target is a discriminant,
581 -- set target type to force a constraint check (13.13.2 (35)).
583 if Nkind
(Targ
) = N_Selected_Component
584 and then Present
(Entity
(Selector_Name
(Targ
)))
585 and then Ekind
(Entity
(Selector_Name
(Targ
)))
589 Unchecked_Convert_To
(Base_Type
(P_Type
),
590 Make_Function_Call
(Loc
,
591 Name
=> New_Occurrence_Of
(RTE
(Lib_RE
), Loc
),
592 Parameter_Associations
=> New_List
(
593 Relocate_Node
(Strm
))));
595 Set_Do_Range_Check
(Res
);
600 Unchecked_Convert_To
(P_Type
,
601 Make_Function_Call
(Loc
,
602 Name
=> New_Occurrence_Of
(RTE
(Lib_RE
), Loc
),
603 Parameter_Associations
=> New_List
(
604 Relocate_Node
(Strm
))));
606 end Build_Elementary_Input_Call
;
608 ---------------------------------
609 -- Build_Elementary_Write_Call --
610 ---------------------------------
612 function Build_Elementary_Write_Call
(N
: Node_Id
) return Node_Id
is
613 Loc
: constant Source_Ptr
:= Sloc
(N
);
614 P_Type
: constant Entity_Id
:= Entity
(Prefix
(N
));
615 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
616 Rt_Type
: constant Entity_Id
:= Root_Type
(U_Type
);
617 FST
: constant Entity_Id
:= First_Subtype
(U_Type
);
618 P_Size
: constant Uint
:= Esize
(FST
);
619 Strm
: constant Node_Id
:= First
(Expressions
(N
));
620 Item
: constant Node_Id
:= Next
(Strm
);
625 -- Find the routine to be called
627 -- Check for First Boolean and Character. These are enumeration types,
628 -- but we treat them specially, since they may require special handling
629 -- in the transfer protocol. However, this special handling only applies
630 -- if they have standard representation, otherwise they are treated like
631 -- any other enumeration type.
633 if Rt_Type
= Standard_Boolean
634 and then Has_Stream_Standard_Rep
(U_Type
)
638 elsif Rt_Type
= Standard_Character
639 and then Has_Stream_Standard_Rep
(U_Type
)
643 elsif Rt_Type
= Standard_Wide_Character
644 and then Has_Stream_Standard_Rep
(U_Type
)
648 -- Floating point types
650 elsif Is_Floating_Point_Type
(U_Type
) then
652 if Rt_Type
= Standard_Short_Float
then
655 elsif Rt_Type
= Standard_Float
then
658 elsif Rt_Type
= Standard_Long_Float
then
661 else pragma Assert
(Rt_Type
= Standard_Long_Long_Float
);
665 -- Signed integer types. Also includes signed fixed-point types and
666 -- signed enumeration types share this circuitry.
668 -- Note on signed integer types. We do not consider types as signed for
669 -- this purpose if they have no negative numbers, or if they have biased
670 -- representation. The reason is that the value in either case basically
671 -- represents an unsigned value.
673 -- For example, consider:
675 -- type W is range 0 .. 2**32 - 1;
676 -- for W'Size use 32;
678 -- This is a signed type, but the representation is unsigned, and may
679 -- be outside the range of a 32-bit signed integer, so this must be
680 -- treated as 32-bit unsigned.
682 -- Similarly, the representation is also unsigned if we have:
684 -- type W is range -1 .. +254;
687 elsif not Is_Unsigned_Type
(FST
)
689 (Is_Fixed_Point_Type
(U_Type
)
691 Is_Enumeration_Type
(U_Type
)
693 (Is_Signed_Integer_Type
(U_Type
)
694 and then not Has_Biased_Representation
(FST
)))
696 if P_Size
<= Standard_Short_Short_Integer_Size
then
699 elsif P_Size
<= Standard_Short_Integer_Size
then
702 elsif P_Size
<= Standard_Integer_Size
then
705 elsif P_Size
<= Standard_Long_Integer_Size
then
712 -- Unsigned integer types, also includes unsigned fixed-point types
713 -- and unsigned enumeration types (note we know they are unsigned
714 -- because we already tested for signed above).
716 -- Also includes signed integer types that are unsigned in the sense
717 -- that they do not include negative numbers. See above for details.
719 elsif Is_Modular_Integer_Type
(U_Type
)
720 or else Is_Fixed_Point_Type
(U_Type
)
721 or else Is_Enumeration_Type
(U_Type
)
722 or else Is_Signed_Integer_Type
(U_Type
)
724 if P_Size
<= Standard_Short_Short_Integer_Size
then
727 elsif P_Size
<= Standard_Short_Integer_Size
then
730 elsif P_Size
<= Standard_Integer_Size
then
733 elsif P_Size
<= Standard_Long_Integer_Size
then
740 else pragma Assert
(Is_Access_Type
(U_Type
));
742 if P_Size
> System_Address_Size
then
749 -- Unchecked-convert parameter to the required type (i.e. the type of
750 -- the corresponding parameter, and call the appropriate routine.
752 Libent
:= RTE
(Lib_RE
);
755 Make_Procedure_Call_Statement
(Loc
,
756 Name
=> New_Occurrence_Of
(Libent
, Loc
),
757 Parameter_Associations
=> New_List
(
758 Relocate_Node
(Strm
),
759 Unchecked_Convert_To
(Etype
(Next_Formal
(First_Formal
(Libent
))),
760 Relocate_Node
(Item
))));
761 end Build_Elementary_Write_Call
;
763 -----------------------------------------
764 -- Build_Mutable_Record_Read_Procedure --
765 -----------------------------------------
767 procedure Build_Mutable_Record_Read_Procedure
771 Pnam
: out Entity_Id
)
774 -- Statements for the 'Read body
776 Tmp
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_V
);
777 -- Temporary, must hide formal (assignments to components of the
778 -- record are always generated with V as the identifier for the record).
781 -- List of constraints to be applied on temporary
790 Disc
:= First_Discriminant
(Typ
);
792 -- A mutable type cannot be a tagged type, so we generate a new name
793 -- for the stream procedure.
796 Make_Defining_Identifier
(Loc
,
797 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Read
));
799 -- Generate Reads for the discriminants of the type. The discriminants
800 -- need to be read before the rest of the components, so that
801 -- variants are initialized correctly.
803 while Present
(Disc
) loop
805 Make_Selected_Component
(Loc
,
806 Prefix
=> Make_Selected_Component
(Loc
,
807 Prefix
=> New_Occurrence_Of
(Pnam
, Loc
),
809 Make_Identifier
(Loc
, Name_V
)),
810 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
));
812 Set_Assignment_OK
(Disc_Ref
);
815 Make_Attribute_Reference
(Loc
,
816 Prefix
=> New_Occurrence_Of
(Etype
(Disc
), Loc
),
817 Attribute_Name
=> Name_Read
,
818 Expressions
=> New_List
(
819 Make_Identifier
(Loc
, Name_S
),
823 Make_Discriminant_Association
(Loc
,
824 Selector_Names
=> New_List
(New_Occurrence_Of
(Disc
, Loc
)),
825 Expression
=> New_Copy_Tree
(Disc_Ref
)));
826 Next_Discriminant
(Disc
);
829 -- Generate reads for the components of the record (including
830 -- those that depend on discriminants).
832 Build_Record_Read_Write_Procedure
(Loc
, Typ
, Decl
, Pnam
, Name_Read
);
834 -- If Typ has controlled components (i.e. if it is classwide
835 -- or Has_Controlled), or components constrained using the discriminants
836 -- of Typ, then we need to ensure that all component assignments
837 -- are performed on an object that has been appropriately constrained
838 -- prior to being initialized. To this effect, we wrap the component
839 -- assignments in a block where V is a constrained temporary.
842 Make_Block_Statement
(Loc
,
843 Declarations
=> New_List
(
844 Make_Object_Declaration
(Loc
,
845 Defining_Identifier
=> Tmp
,
847 Make_Subtype_Indication
(Loc
,
848 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
850 Make_Index_Or_Discriminant_Constraint
(Loc
,
851 Constraints
=> Cstr
)))),
852 Handled_Statement_Sequence
=>
853 Handled_Statement_Sequence
(Decl
));
855 Append_To
(Stms
, Block
);
857 Append_To
(Statements
(Handled_Statement_Sequence
(Block
)),
858 Make_Assignment_Statement
(Loc
,
859 Name
=> Make_Selected_Component
(Loc
,
860 Prefix
=> New_Occurrence_Of
(Pnam
, Loc
),
861 Selector_Name
=> Make_Identifier
(Loc
, Name_V
)),
862 Expression
=> Make_Identifier
(Loc
, Name_V
)));
864 if Is_Unchecked_Union
(Typ
) then
866 -- If this is an unchecked union, the stream procedure is erroneous,
867 -- because there are no discriminants to read.
869 -- This should generate a warning ???
873 Make_Raise_Program_Error
(Loc
,
874 Reason
=> PE_Unchecked_Union_Restriction
));
877 Set_Handled_Statement_Sequence
(Decl
,
878 Make_Handled_Sequence_Of_Statements
(Loc
,
879 Statements
=> Stms
));
880 end Build_Mutable_Record_Read_Procedure
;
882 ------------------------------------------
883 -- Build_Mutable_Record_Write_Procedure --
884 ------------------------------------------
886 procedure Build_Mutable_Record_Write_Procedure
890 Pnam
: out Entity_Id
)
897 Disc
:= First_Discriminant
(Typ
);
899 -- Generate Writes for the discriminants of the type
901 while Present
(Disc
) loop
904 Make_Attribute_Reference
(Loc
,
905 Prefix
=> New_Occurrence_Of
(Etype
(Disc
), Loc
),
906 Attribute_Name
=> Name_Write
,
907 Expressions
=> New_List
(
908 Make_Identifier
(Loc
, Name_S
),
909 Make_Selected_Component
(Loc
,
910 Prefix
=> Make_Identifier
(Loc
, Name_V
),
911 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
)))));
913 Next_Discriminant
(Disc
);
916 -- A mutable type cannot be a tagged type, so we generate a new name
917 -- for the stream procedure.
920 Make_Defining_Identifier
(Loc
,
921 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Write
));
922 Build_Record_Read_Write_Procedure
(Loc
, Typ
, Decl
, Pnam
, Name_Write
);
924 -- Write the discriminants before the rest of the components, so
925 -- that discriminant values are properly set of variants, etc.
926 -- If this is an unchecked union, the stream procedure is erroneous
927 -- because there are no discriminants to write.
929 if Is_Unchecked_Union
(Typ
) then
932 Make_Raise_Program_Error
(Loc
,
933 Reason
=> PE_Unchecked_Union_Restriction
));
936 if Is_Non_Empty_List
(
937 Statements
(Handled_Statement_Sequence
(Decl
)))
940 (First
(Statements
(Handled_Statement_Sequence
(Decl
))), Stms
);
942 Set_Statements
(Handled_Statement_Sequence
(Decl
), Stms
);
944 end Build_Mutable_Record_Write_Procedure
;
946 -----------------------------------------------
947 -- Build_Record_Or_Elementary_Input_Function --
948 -----------------------------------------------
950 -- The function we build looks like
952 -- function InputN (S : access RST) return Typ is
953 -- C1 : constant Disc_Type_1;
954 -- Discr_Type_1'Read (S, C1);
955 -- C2 : constant Disc_Type_2;
956 -- Discr_Type_2'Read (S, C2);
958 -- Cn : constant Disc_Type_n;
959 -- Discr_Type_n'Read (S, Cn);
960 -- V : Typ (C1, C2, .. Cn)
967 -- The discriminants are of course only present in the case of a record
968 -- with discriminants. In the case of a record with no discriminants, or
969 -- an elementary type, then no Cn constants are defined.
971 procedure Build_Record_Or_Elementary_Input_Function
975 Fnam
: out Entity_Id
)
991 if Has_Discriminants
(Typ
) then
992 Discr
:= First_Discriminant
(Typ
);
994 while Present
(Discr
) loop
995 Cn
:= New_External_Name
('C', J
);
998 Make_Object_Declaration
(Loc
,
999 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Cn
),
1000 Object_Definition
=>
1001 New_Occurrence_Of
(Etype
(Discr
), Loc
)));
1004 Make_Attribute_Reference
(Loc
,
1005 Prefix
=> New_Occurrence_Of
(Etype
(Discr
), Loc
),
1006 Attribute_Name
=> Name_Read
,
1007 Expressions
=> New_List
(
1008 Make_Identifier
(Loc
, Name_S
),
1009 Make_Identifier
(Loc
, Cn
))));
1011 Append_To
(Constr
, Make_Identifier
(Loc
, Cn
));
1013 Next_Discriminant
(Discr
);
1018 Make_Subtype_Indication
(Loc
,
1019 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
1021 Make_Index_Or_Discriminant_Constraint
(Loc
,
1022 Constraints
=> Constr
));
1024 -- If no discriminants, then just use the type with no constraint
1027 Odef
:= New_Occurrence_Of
(Typ
, Loc
);
1031 Make_Object_Declaration
(Loc
,
1032 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
1033 Object_Definition
=> Odef
));
1036 Make_Attribute_Reference
(Loc
,
1037 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1038 Attribute_Name
=> Name_Read
,
1039 Expressions
=> New_List
(
1040 Make_Identifier
(Loc
, Name_S
),
1041 Make_Identifier
(Loc
, Name_V
))),
1043 Make_Return_Statement
(Loc
,
1044 Expression
=> Make_Identifier
(Loc
, Name_V
)));
1046 Fnam
:= Make_Stream_Subprogram_Name
(Loc
, Typ
, TSS_Stream_Input
);
1048 Build_Stream_Function
(Loc
, Typ
, Decl
, Fnam
, Decls
, Stms
);
1049 end Build_Record_Or_Elementary_Input_Function
;
1051 -------------------------------------------------
1052 -- Build_Record_Or_Elementary_Output_Procedure --
1053 -------------------------------------------------
1055 procedure Build_Record_Or_Elementary_Output_Procedure
1059 Pnam
: out Entity_Id
)
1067 -- Note that of course there will be no discriminants for the
1068 -- elementary type case, so Has_Discriminants will be False.
1070 if Has_Discriminants
(Typ
) then
1071 Disc
:= First_Discriminant
(Typ
);
1073 while Present
(Disc
) loop
1075 Make_Attribute_Reference
(Loc
,
1077 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Disc
)), Loc
),
1078 Attribute_Name
=> Name_Write
,
1079 Expressions
=> New_List
(
1080 Make_Identifier
(Loc
, Name_S
),
1081 Make_Selected_Component
(Loc
,
1082 Prefix
=> Make_Identifier
(Loc
, Name_V
),
1083 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
)))));
1085 Next_Discriminant
(Disc
);
1090 Make_Attribute_Reference
(Loc
,
1091 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1092 Attribute_Name
=> Name_Write
,
1093 Expressions
=> New_List
(
1094 Make_Identifier
(Loc
, Name_S
),
1095 Make_Identifier
(Loc
, Name_V
))));
1097 Pnam
:= Make_Stream_Subprogram_Name
(Loc
, Typ
, TSS_Stream_Output
);
1099 Build_Stream_Procedure
(Loc
, Typ
, Decl
, Pnam
, Stms
, False);
1100 end Build_Record_Or_Elementary_Output_Procedure
;
1102 ---------------------------------
1103 -- Build_Record_Read_Procedure --
1104 ---------------------------------
1106 procedure Build_Record_Read_Procedure
1110 Pnam
: out Entity_Id
)
1113 Pnam
:= Make_Stream_Subprogram_Name
(Loc
, Typ
, TSS_Stream_Read
);
1114 Build_Record_Read_Write_Procedure
(Loc
, Typ
, Decl
, Pnam
, Name_Read
);
1115 end Build_Record_Read_Procedure
;
1117 ---------------------------------------
1118 -- Build_Record_Read_Write_Procedure --
1119 ---------------------------------------
1121 -- The form of the record read/write procedure is as shown by the
1122 -- following example for a case with one discriminant case variant:
1124 -- procedure pnam (S : access RST, V : [out] Typ) is
1126 -- Component_Type'Read/Write (S, V.component);
1127 -- Component_Type'Read/Write (S, V.component);
1129 -- Component_Type'Read/Write (S, V.component);
1131 -- case V.discriminant is
1133 -- Component_Type'Read/Write (S, V.component);
1134 -- Component_Type'Read/Write (S, V.component);
1136 -- Component_Type'Read/Write (S, V.component);
1139 -- Component_Type'Read/Write (S, V.component);
1140 -- Component_Type'Read/Write (S, V.component);
1142 -- Component_Type'Read/Write (S, V.component);
1147 -- The out keyword for V is supplied in the Read case
1149 procedure Build_Record_Read_Write_Procedure
1160 function Make_Component_List_Attributes
(CL
: Node_Id
) return List_Id
;
1161 -- Returns a sequence of attributes to process the components that
1162 -- are referenced in the given component list.
1164 function Make_Field_Attribute
(C
: Entity_Id
) return Node_Id
;
1165 -- Given C, the entity for a discriminant or component, build
1166 -- an attribute for the corresponding field values.
1168 function Make_Field_Attributes
(Clist
: List_Id
) return List_Id
;
1169 -- Given Clist, a component items list, construct series of attributes
1170 -- for fieldwise processing of the corresponding components.
1172 ------------------------------------
1173 -- Make_Component_List_Attributes --
1174 ------------------------------------
1176 function Make_Component_List_Attributes
(CL
: Node_Id
) return List_Id
is
1177 CI
: constant List_Id
:= Component_Items
(CL
);
1178 VP
: constant Node_Id
:= Variant_Part
(CL
);
1187 Result
:= Make_Field_Attributes
(CI
);
1189 -- If a component is an unchecked union, there is no discriminant
1190 -- and we cannot generate a read/write procedure for it.
1192 if Present
(VP
) then
1193 if Is_Unchecked_Union
(Scope
(Entity
(Name
(VP
)))) then
1195 Make_Raise_Program_Error
(Sloc
(VP
),
1196 Reason
=> PE_Unchecked_Union_Restriction
));
1199 V
:= First_Non_Pragma
(Variants
(VP
));
1201 while Present
(V
) loop
1204 DC
:= First
(Discrete_Choices
(V
));
1205 while Present
(DC
) loop
1206 Append_To
(DCH
, New_Copy_Tree
(DC
));
1211 Make_Case_Statement_Alternative
(Loc
,
1212 Discrete_Choices
=> DCH
,
1214 Make_Component_List_Attributes
(Component_List
(V
))));
1215 Next_Non_Pragma
(V
);
1218 -- Note: in the following, we make sure that we use new occurrence
1219 -- of for the selector, since there are cases in which we make a
1220 -- reference to a hidden discriminant that is not visible.
1223 Make_Case_Statement
(Loc
,
1225 Make_Selected_Component
(Loc
,
1226 Prefix
=> Make_Identifier
(Loc
, Name_V
),
1228 New_Occurrence_Of
(Entity
(Name
(VP
)), Loc
)),
1229 Alternatives
=> Alts
));
1234 end Make_Component_List_Attributes
;
1236 --------------------------
1237 -- Make_Field_Attribute --
1238 --------------------------
1240 function Make_Field_Attribute
(C
: Entity_Id
) return Node_Id
is
1243 Make_Attribute_Reference
(Loc
,
1245 New_Occurrence_Of
(Stream_Base_Type
(Etype
(C
)), Loc
),
1246 Attribute_Name
=> Nam
,
1247 Expressions
=> New_List
(
1248 Make_Identifier
(Loc
, Name_S
),
1249 Make_Selected_Component
(Loc
,
1250 Prefix
=> Make_Identifier
(Loc
, Name_V
),
1251 Selector_Name
=> New_Occurrence_Of
(C
, Loc
))));
1252 end Make_Field_Attribute
;
1254 ---------------------------
1255 -- Make_Field_Attributes --
1256 ---------------------------
1258 function Make_Field_Attributes
(Clist
: List_Id
) return List_Id
is
1265 if Present
(Clist
) then
1266 Item
:= First
(Clist
);
1268 -- Loop through components, skipping all internal components,
1269 -- which are not part of the value (e.g. _Tag), except that we
1270 -- don't skip the _Parent, since we do want to process that
1273 while Present
(Item
) loop
1274 if Nkind
(Item
) = N_Component_Declaration
1276 (Chars
(Defining_Identifier
(Item
)) = Name_uParent
1278 not Is_Internal_Name
(Chars
(Defining_Identifier
(Item
))))
1282 Make_Field_Attribute
(Defining_Identifier
(Item
)));
1290 end Make_Field_Attributes
;
1292 -- Start of processing for Build_Record_Read_Write_Procedure
1295 -- For the protected type case, use corresponding record
1297 if Is_Protected_Type
(Typ
) then
1298 Typt
:= Corresponding_Record_Type
(Typ
);
1303 -- Note that we do nothing with the discriminants, since Read and
1304 -- Write do not read or write the discriminant values. All handling
1305 -- of discriminants occurs in the Input and Output subprograms.
1307 Rdef
:= Type_Definition
1308 (Declaration_Node
(Base_Type
(Underlying_Type
(Typt
))));
1311 -- In record extension case, the fields we want, including the _Parent
1312 -- field representing the parent type, are to be found in the extension.
1313 -- Note that we will naturally process the _Parent field using the type
1314 -- of the parent, and hence its stream attributes, which is appropriate.
1316 if Nkind
(Rdef
) = N_Derived_Type_Definition
then
1317 Rdef
:= Record_Extension_Part
(Rdef
);
1320 if Present
(Component_List
(Rdef
)) then
1321 Append_List_To
(Stms
,
1322 Make_Component_List_Attributes
(Component_List
(Rdef
)));
1325 Build_Stream_Procedure
1326 (Loc
, Typ
, Decl
, Pnam
, Stms
, Nam
= Name_Read
);
1327 end Build_Record_Read_Write_Procedure
;
1329 ----------------------------------
1330 -- Build_Record_Write_Procedure --
1331 ----------------------------------
1333 procedure Build_Record_Write_Procedure
1337 Pnam
: out Entity_Id
)
1340 Pnam
:= Make_Stream_Subprogram_Name
(Loc
, Typ
, TSS_Stream_Write
);
1341 Build_Record_Read_Write_Procedure
(Loc
, Typ
, Decl
, Pnam
, Name_Write
);
1342 end Build_Record_Write_Procedure
;
1344 -------------------------------
1345 -- Build_Stream_Attr_Profile --
1346 -------------------------------
1348 function Build_Stream_Attr_Profile
1351 Nam
: TSS_Name_Type
) return List_Id
1356 Profile
:= New_List
(
1357 Make_Parameter_Specification
(Loc
,
1358 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_S
),
1360 Make_Access_Definition
(Loc
,
1361 Subtype_Mark
=> New_Reference_To
(
1362 Class_Wide_Type
(RTE
(RE_Root_Stream_Type
)), Loc
))));
1364 if Nam
/= TSS_Stream_Input
then
1366 Make_Parameter_Specification
(Loc
,
1367 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
1368 Out_Present
=> (Nam
= TSS_Stream_Read
),
1369 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)));
1373 end Build_Stream_Attr_Profile
;
1375 ---------------------------
1376 -- Build_Stream_Function --
1377 ---------------------------
1379 procedure Build_Stream_Function
1390 -- Construct function specification
1393 Make_Function_Specification
(Loc
,
1394 Defining_Unit_Name
=> Fnam
,
1396 Parameter_Specifications
=> New_List
(
1397 Make_Parameter_Specification
(Loc
,
1398 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_S
),
1400 Make_Access_Definition
(Loc
,
1401 Subtype_Mark
=> New_Reference_To
(
1402 Class_Wide_Type
(RTE
(RE_Root_Stream_Type
)), Loc
)))),
1404 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
));
1407 Make_Subprogram_Body
(Loc
,
1408 Specification
=> Spec
,
1409 Declarations
=> Decls
,
1410 Handled_Statement_Sequence
=>
1411 Make_Handled_Sequence_Of_Statements
(Loc
,
1412 Statements
=> Stms
));
1413 end Build_Stream_Function
;
1415 ----------------------------
1416 -- Build_Stream_Procedure --
1417 ----------------------------
1419 procedure Build_Stream_Procedure
1430 -- Construct procedure specification
1433 Make_Procedure_Specification
(Loc
,
1434 Defining_Unit_Name
=> Pnam
,
1436 Parameter_Specifications
=> New_List
(
1437 Make_Parameter_Specification
(Loc
,
1438 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_S
),
1440 Make_Access_Definition
(Loc
,
1441 Subtype_Mark
=> New_Reference_To
(
1442 Class_Wide_Type
(RTE
(RE_Root_Stream_Type
)), Loc
))),
1444 Make_Parameter_Specification
(Loc
,
1445 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
1446 Out_Present
=> Outp
,
1447 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))));
1450 Make_Subprogram_Body
(Loc
,
1451 Specification
=> Spec
,
1452 Declarations
=> Empty_List
,
1453 Handled_Statement_Sequence
=>
1454 Make_Handled_Sequence_Of_Statements
(Loc
,
1455 Statements
=> Stms
));
1456 end Build_Stream_Procedure
;
1458 -----------------------------
1459 -- Has_Stream_Standard_Rep --
1460 -----------------------------
1462 function Has_Stream_Standard_Rep
(U_Type
: Entity_Id
) return Boolean is
1464 if Has_Non_Standard_Rep
(U_Type
) then
1468 Esize
(First_Subtype
(U_Type
)) = Esize
(Root_Type
(U_Type
));
1470 end Has_Stream_Standard_Rep
;
1472 ---------------------------------
1473 -- Make_Stream_Subprogram_Name --
1474 ---------------------------------
1476 function Make_Stream_Subprogram_Name
1479 Nam
: TSS_Name_Type
) return Entity_Id
1484 -- For tagged types, we are dealing with a TSS associated with the
1485 -- declaration, so we use the standard primitive function name. For
1486 -- other types, generate a local TSS name since we are generating
1487 -- the subprogram at the point of use.
1489 if Is_Tagged_Type
(Typ
) then
1490 Sname
:= Make_TSS_Name
(Typ
, Nam
);
1492 Sname
:= Make_TSS_Name_Local
(Typ
, Nam
);
1495 return Make_Defining_Identifier
(Loc
, Sname
);
1496 end Make_Stream_Subprogram_Name
;
1498 ----------------------
1499 -- Stream_Base_Type --
1500 ----------------------
1502 function Stream_Base_Type
(E
: Entity_Id
) return Entity_Id
is
1504 if Is_Array_Type
(E
)
1505 and then Is_First_Subtype
(E
)
1509 return Base_Type
(E
);
1511 end Stream_Base_Type
;