1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Einfo
; use Einfo
;
28 with Einfo
.Entities
; use Einfo
.Entities
;
29 with Einfo
.Utils
; use Einfo
.Utils
;
30 with Elists
; use Elists
;
31 with Exp_Util
; use Exp_Util
;
32 with Namet
; use Namet
;
33 with Nlists
; use Nlists
;
34 with Nmake
; use Nmake
;
35 with Rtsfind
; use Rtsfind
;
36 with Sem_Aux
; use Sem_Aux
;
37 with Sem_Util
; use Sem_Util
;
38 with Sinfo
; use Sinfo
;
39 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
40 with Sinfo
.Utils
; use Sinfo
.Utils
;
41 with Snames
; use Snames
;
42 with Stand
; use Stand
;
43 with Tbuild
; use Tbuild
;
44 with Ttypes
; use Ttypes
;
45 with Uintp
; use Uintp
;
47 package body Exp_Strm
is
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 procedure Build_Array_Read_Write_Procedure
58 -- Common routine shared to build either an array Read procedure or an
59 -- array Write procedure, Nam is Name_Read or Name_Write to select which.
60 -- Pnam is the defining identifier for the constructed procedure. The
61 -- other parameters are as for Build_Array_Read_Procedure.
63 procedure Build_Record_Read_Write_Procedure
68 -- Common routine shared to build a record Read Write procedure, Nam
69 -- is Name_Read or Name_Write to select which. Pnam is the defining
70 -- identifier for the constructed procedure. The other parameters are
71 -- as for Build_Record_Read_Procedure.
73 procedure Build_Stream_Function
79 -- Called to build an array or record stream function. The first three
80 -- arguments are the same as Build_Record_Or_Elementary_Input_Function.
81 -- Decls and Stms are the declarations and statements for the body and
82 -- The parameter Fnam is the name of the constructed function.
84 function Has_Stream_Standard_Rep
(U_Type
: Entity_Id
) return Boolean;
85 -- This function is used to test the type U_Type, to determine if it has
86 -- a standard representation from a streaming point of view. Standard means
87 -- that it has a standard representation (e.g. no enumeration rep clause),
88 -- and the size of the root type is the same as the streaming size (which
89 -- is defined as value specified by a Stream_Size clause if present, or
90 -- the Esize of U_Type if not).
92 function Make_Stream_Subprogram_Name
95 Nam
: TSS_Name_Type
) return Entity_Id
;
96 -- Return the entity that identifies the stream subprogram for type Typ
97 -- that is identified by the given Nam. This procedure deals with the
98 -- difference between tagged types (where a single subprogram associated
99 -- with the type is generated) and all other cases (where a subprogram
100 -- is generated at the point of the stream attribute reference). The
101 -- Loc parameter is used as the Sloc of the created entity.
103 function Stream_Base_Type
(E
: Entity_Id
) return Entity_Id
;
104 -- Stream attributes work on the basis of the base type except for the
105 -- array case. For the array case, we do not go to the base type, but
106 -- to the first subtype if it is constrained. This avoids problems with
107 -- incorrect conversions in the packed array case. Stream_Base_Type is
108 -- exactly this function (returns the base type, unless we have an array
109 -- type whose first subtype is constrained, in which case it returns the
112 --------------------------------
113 -- Build_Array_Input_Function --
114 --------------------------------
116 -- The function we build looks like
118 -- function typSI[_nnn] (S : access RST) return Typ is
119 -- L1 : constant Index_Type_1 := Index_Type_1'Input (S);
120 -- H1 : constant Index_Type_1 := Index_Type_1'Input (S);
121 -- L2 : constant Index_Type_2 := Index_Type_2'Input (S);
122 -- H2 : constant Index_Type_2 := Index_Type_2'Input (S);
124 -- Ln : constant Index_Type_n := Index_Type_n'Input (S);
125 -- Hn : constant Index_Type_n := Index_Type_n'Input (S);
127 -- V : Typ'Base (L1 .. H1, L2 .. H2, ... Ln .. Hn)
134 -- Note: the suffix [_nnn] is present for untagged types, where we generate
135 -- a local subprogram at the point of the occurrence of the attribute
136 -- reference, so the name must be unique.
138 procedure Build_Array_Input_Function
141 Fnam
: out Entity_Id
)
143 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
144 Dim
: constant Pos
:= Number_Dimensions
(Typ
);
157 Indx
:= First_Index
(Typ
);
158 for J
in 1 .. Dim
loop
159 Lnam
:= New_External_Name
('L', J
);
160 Hnam
:= New_External_Name
('H', J
);
163 Make_Object_Declaration
(Loc
,
164 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Lnam
),
165 Constant_Present
=> True,
166 Object_Definition
=> New_Occurrence_Of
(Etype
(Indx
), Loc
),
168 Make_Attribute_Reference
(Loc
,
170 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
171 Attribute_Name
=> Name_Input
,
172 Expressions
=> New_List
(Make_Identifier
(Loc
, Name_S
)))));
175 Make_Object_Declaration
(Loc
,
176 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Hnam
),
177 Constant_Present
=> True,
179 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
181 Make_Attribute_Reference
(Loc
,
183 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
184 Attribute_Name
=> Name_Input
,
185 Expressions
=> New_List
(Make_Identifier
(Loc
, Name_S
)))));
189 Low_Bound
=> Make_Identifier
(Loc
, Lnam
),
190 High_Bound
=> Make_Identifier
(Loc
, Hnam
)));
195 -- If the type is constrained, use it directly. Otherwise build a
196 -- subtype indication with the proper bounds.
198 if Is_Constrained
(Typ
) then
200 Make_Object_Declaration
(Loc
,
201 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
202 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
206 Make_Object_Declaration
(Loc
,
207 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
209 Make_Subtype_Indication
(Loc
,
211 New_Occurrence_Of
(Stream_Base_Type
(Typ
), Loc
),
213 Make_Index_Or_Discriminant_Constraint
(Loc
, Ranges
)));
217 Make_Attribute_Reference
(Loc
,
218 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
219 Attribute_Name
=> Name_Read
,
220 Expressions
=> New_List
(
221 Make_Identifier
(Loc
, Name_S
),
222 Make_Identifier
(Loc
, Name_V
)));
225 Make_Extended_Return_Statement
(Loc
,
226 Return_Object_Declarations
=> New_List
(Odecl
),
227 Handled_Statement_Sequence
=>
228 Make_Handled_Sequence_Of_Statements
(Loc
, New_List
(Rstmt
))));
231 Make_Defining_Identifier
(Loc
,
232 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Input
));
234 Build_Stream_Function
(Typ
, Decl
, Fnam
, Decls
, Stms
);
235 end Build_Array_Input_Function
;
237 ----------------------------------
238 -- Build_Array_Output_Procedure --
239 ----------------------------------
241 procedure Build_Array_Output_Procedure
244 Pnam
: out Entity_Id
)
246 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
251 -- Build series of statements to output bounds
253 Indx
:= First_Index
(Typ
);
256 for J
in 1 .. Number_Dimensions
(Typ
) loop
258 Make_Attribute_Reference
(Loc
,
260 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
261 Attribute_Name
=> Name_Write
,
262 Expressions
=> New_List
(
263 Make_Identifier
(Loc
, Name_S
),
264 Make_Attribute_Reference
(Loc
,
265 Prefix
=> Make_Identifier
(Loc
, Name_V
),
266 Attribute_Name
=> Name_First
,
267 Expressions
=> New_List
(
268 Make_Integer_Literal
(Loc
, J
))))));
271 Make_Attribute_Reference
(Loc
,
273 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Indx
)), Loc
),
274 Attribute_Name
=> Name_Write
,
275 Expressions
=> New_List
(
276 Make_Identifier
(Loc
, Name_S
),
277 Make_Attribute_Reference
(Loc
,
278 Prefix
=> Make_Identifier
(Loc
, Name_V
),
279 Attribute_Name
=> Name_Last
,
280 Expressions
=> New_List
(
281 Make_Integer_Literal
(Loc
, J
))))));
286 -- Append Write attribute to write array elements
289 Make_Attribute_Reference
(Loc
,
290 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
291 Attribute_Name
=> Name_Write
,
292 Expressions
=> New_List
(
293 Make_Identifier
(Loc
, Name_S
),
294 Make_Identifier
(Loc
, Name_V
))));
297 Make_Defining_Identifier
(Loc
,
298 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Output
));
300 Build_Stream_Procedure
(Typ
, Decl
, Pnam
, Stms
, Outp
=> False);
301 end Build_Array_Output_Procedure
;
303 --------------------------------
304 -- Build_Array_Read_Procedure --
305 --------------------------------
307 procedure Build_Array_Read_Procedure
310 Pnam
: out Entity_Id
)
312 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
316 Make_Defining_Identifier
(Loc
,
317 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Read
));
318 Build_Array_Read_Write_Procedure
(Typ
, Decl
, Pnam
, Name_Read
);
319 end Build_Array_Read_Procedure
;
321 --------------------------------------
322 -- Build_Array_Read_Write_Procedure --
323 --------------------------------------
325 -- The form of the array read/write procedure is as follows:
327 -- procedure pnam (S : access RST, V : [out] Typ) is
329 -- for L1 in V'Range (1) loop
330 -- for L2 in V'Range (2) loop
332 -- for Ln in V'Range (n) loop
333 -- Component_Type'Read/Write (S, V (L1, L2, .. Ln));
340 -- The out keyword for V is supplied in the Read case
342 procedure Build_Array_Read_Write_Procedure
348 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
349 Ndim
: constant Pos
:= Number_Dimensions
(Typ
);
350 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
357 -- First build the inner attribute call
361 for J
in 1 .. Ndim
loop
362 Append_To
(Exl
, Make_Identifier
(Loc
, New_External_Name
('L', J
)));
366 Make_Attribute_Reference
(Loc
,
367 Prefix
=> New_Occurrence_Of
(Stream_Base_Type
(Ctyp
), Loc
),
368 Attribute_Name
=> Nam
,
369 Expressions
=> New_List
(
370 Make_Identifier
(Loc
, Name_S
),
371 Make_Indexed_Component
(Loc
,
372 Prefix
=> Make_Identifier
(Loc
, Name_V
),
373 Expressions
=> Exl
)));
375 -- The corresponding stream attribute for the component type of the
376 -- array may be user-defined, and be frozen after the type for which
377 -- we are generating the stream subprogram. In that case, freeze the
378 -- stream attribute of the component type, whose declaration could not
379 -- generate any additional freezing actions in any case.
381 if Nam
= Name_Read
then
382 RW
:= TSS
(Base_Type
(Ctyp
), TSS_Stream_Read
);
384 RW
:= TSS
(Base_Type
(Ctyp
), TSS_Stream_Write
);
388 and then not Is_Frozen
(RW
)
393 -- Now this is the big loop to wrap that statement up in a sequence
394 -- of loops. The first time around, Stm is the attribute call. The
395 -- second and subsequent times, Stm is an inner loop.
397 for J
in 1 .. Ndim
loop
399 Make_Implicit_Loop_Statement
(Typ
,
401 Make_Iteration_Scheme
(Loc
,
402 Loop_Parameter_Specification
=>
403 Make_Loop_Parameter_Specification
(Loc
,
404 Defining_Identifier
=>
405 Make_Defining_Identifier
(Loc
,
406 Chars
=> New_External_Name
('L', Ndim
- J
+ 1)),
408 Discrete_Subtype_Definition
=>
409 Make_Attribute_Reference
(Loc
,
410 Prefix
=> Make_Identifier
(Loc
, Name_V
),
411 Attribute_Name
=> Name_Range
,
413 Expressions
=> New_List
(
414 Make_Integer_Literal
(Loc
, Ndim
- J
+ 1))))),
416 Statements
=> New_List
(Stm
));
420 Build_Stream_Procedure
421 (Typ
, Decl
, Pnam
, New_List
(Stm
), Outp
=> Nam
= Name_Read
);
422 end Build_Array_Read_Write_Procedure
;
424 ---------------------------------
425 -- Build_Array_Write_Procedure --
426 ---------------------------------
428 procedure Build_Array_Write_Procedure
431 Pnam
: out Entity_Id
)
433 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
436 Make_Defining_Identifier
(Loc
,
437 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Write
));
438 Build_Array_Read_Write_Procedure
(Typ
, Decl
, Pnam
, Name_Write
);
439 end Build_Array_Write_Procedure
;
441 ---------------------------------
442 -- Build_Elementary_Input_Call --
443 ---------------------------------
445 function Build_Elementary_Input_Call
(N
: Node_Id
) return Node_Id
is
446 Loc
: constant Source_Ptr
:= Sloc
(N
);
447 P_Type
: constant Entity_Id
:= Entity
(Prefix
(N
));
448 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
449 Rt_Type
: constant Entity_Id
:= Root_Type
(U_Type
);
450 FST
: constant Entity_Id
:= First_Subtype
(U_Type
);
451 Strm
: constant Node_Id
:= First
(Expressions
(N
));
452 Targ
: constant Node_Id
:= Next
(Strm
);
453 P_Size
: constant Uint
:= Get_Stream_Size
(FST
);
459 -- Check first for Boolean and Character. These are enumeration types,
460 -- but we treat them specially, since they may require special handling
461 -- in the transfer protocol. However, this special handling only applies
462 -- if they have standard representation, otherwise they are treated like
463 -- any other enumeration type.
465 if Rt_Type
= Standard_Boolean
466 and then Has_Stream_Standard_Rep
(U_Type
)
470 elsif Rt_Type
= Standard_Character
471 and then Has_Stream_Standard_Rep
(U_Type
)
475 elsif Rt_Type
= Standard_Wide_Character
476 and then Has_Stream_Standard_Rep
(U_Type
)
480 elsif Rt_Type
= Standard_Wide_Wide_Character
481 and then Has_Stream_Standard_Rep
(U_Type
)
485 -- Floating point types
487 elsif Is_Floating_Point_Type
(U_Type
) then
489 -- Question: should we use P_Size or Rt_Type to distinguish between
490 -- possible floating point types? If a non-standard size or a stream
491 -- size is specified, then we should certainly use the size. But if
492 -- we have two types the same (notably Short_Float_Size = Float_Size
493 -- which is close to universally true, and Long_Long_Float_Size =
494 -- Long_Float_Size, true on most targets except the x86), then we
495 -- would really rather use the root type, so that if people want to
496 -- fiddle with System.Stream_Attributes to get inter-target portable
497 -- streams, they get the size they expect. Consider in particular the
498 -- case of a stream written on an x86, with 96-bit Long_Long_Float
499 -- being read into a non-x86 target with 64 bit Long_Long_Float. A
500 -- special version of System.Stream_Attributes can deal with this
501 -- provided the proper type is always used.
503 -- To deal with these two requirements we add the special checks
504 -- on equal sizes and use the root type to distinguish.
506 if P_Size
<= Standard_Short_Float_Size
507 and then (Standard_Short_Float_Size
/= Standard_Float_Size
508 or else Rt_Type
= Standard_Short_Float
)
512 elsif P_Size
<= Standard_Float_Size
then
515 elsif P_Size
<= Standard_Long_Float_Size
516 and then (Standard_Long_Float_Size
/= Standard_Long_Long_Float_Size
517 or else Rt_Type
= Standard_Long_Float
)
525 -- Signed integer types. Also includes signed fixed-point types and
526 -- enumeration types with a signed representation.
528 -- Note on signed integer types. We do not consider types as signed for
529 -- this purpose if they have no negative numbers, or if they have biased
530 -- representation. The reason is that the value in either case basically
531 -- represents an unsigned value.
533 -- For example, consider:
535 -- type W is range 0 .. 2**32 - 1;
536 -- for W'Size use 32;
538 -- This is a signed type, but the representation is unsigned, and may
539 -- be outside the range of a 32-bit signed integer, so this must be
540 -- treated as 32-bit unsigned.
542 -- Similarly, if we have
544 -- type W is range -1 .. +254;
547 -- then the representation is unsigned
549 elsif not Is_Unsigned_Type
(FST
)
551 -- The following set of tests gets repeated many times, we should
552 -- have an abstraction defined ???
555 (Is_Fixed_Point_Type
(U_Type
)
557 Is_Enumeration_Type
(U_Type
)
559 (Is_Signed_Integer_Type
(U_Type
)
560 and then not Has_Biased_Representation
(FST
)))
563 if P_Size
<= Standard_Short_Short_Integer_Size
then
566 elsif P_Size
<= Standard_Short_Integer_Size
then
569 elsif P_Size
= 24 then
572 elsif P_Size
<= Standard_Integer_Size
then
575 elsif P_Size
<= Standard_Long_Integer_Size
then
578 elsif P_Size
<= Standard_Long_Long_Integer_Size
then
585 -- Unsigned integer types, also includes unsigned fixed-point types
586 -- and enumeration types with an unsigned representation (note that
587 -- we know they are unsigned because we already tested for signed).
589 -- Also includes signed integer types that are unsigned in the sense
590 -- that they do not include negative numbers. See above for details.
592 elsif Is_Modular_Integer_Type
(U_Type
)
593 or else Is_Fixed_Point_Type
(U_Type
)
594 or else Is_Enumeration_Type
(U_Type
)
595 or else Is_Signed_Integer_Type
(U_Type
)
597 if P_Size
<= Standard_Short_Short_Integer_Size
then
600 elsif P_Size
<= Standard_Short_Integer_Size
then
603 elsif P_Size
= 24 then
606 elsif P_Size
<= Standard_Integer_Size
then
609 elsif P_Size
<= Standard_Long_Integer_Size
then
612 elsif P_Size
<= Standard_Long_Long_Integer_Size
then
619 else pragma Assert
(Is_Access_Type
(U_Type
));
620 if Present
(P_Size
) and then P_Size
> System_Address_Size
then
627 -- Call the function, and do an unchecked conversion of the result
628 -- to the actual type of the prefix. If the target is a discriminant,
629 -- and we are in the body of the default implementation of a 'Read
630 -- attribute, set target type to force a constraint check (13.13.2(35)).
631 -- If the type of the discriminant is currently private, add another
632 -- unchecked conversion from the full view.
634 if Nkind
(Targ
) = N_Identifier
635 and then Is_Internal_Name
(Chars
(Targ
))
636 and then Is_TSS
(Scope
(Entity
(Targ
)), TSS_Stream_Read
)
639 Unchecked_Convert_To
(Base_Type
(U_Type
),
640 Make_Function_Call
(Loc
,
641 Name
=> New_Occurrence_Of
(RTE
(Lib_RE
), Loc
),
642 Parameter_Associations
=> New_List
(
643 Relocate_Node
(Strm
))));
645 Set_Do_Range_Check
(Res
);
647 if Base_Type
(P_Type
) /= Base_Type
(U_Type
) then
648 Res
:= Unchecked_Convert_To
(Base_Type
(P_Type
), Res
);
655 Make_Function_Call
(Loc
,
656 Name
=> New_Occurrence_Of
(RTE
(Lib_RE
), Loc
),
657 Parameter_Associations
=> New_List
(
658 Relocate_Node
(Strm
)));
660 -- Now convert to the base type if we do not have a biased type. Note
661 -- that we did not do this in some older versions, and the result was
662 -- losing a required range check in the case where 'Input is being
663 -- called from 'Read.
665 if not Has_Biased_Representation
(P_Type
) then
666 return Unchecked_Convert_To
(Base_Type
(P_Type
), Res
);
668 -- For the biased case, the conversion to the base type loses the
669 -- biasing, so just convert to Ptype. This is not quite right, and
670 -- for example may lose a corner case CE test, but it is such a
671 -- rare case that for now we ignore it ???
674 return Unchecked_Convert_To
(P_Type
, Res
);
677 end Build_Elementary_Input_Call
;
679 ---------------------------------
680 -- Build_Elementary_Write_Call --
681 ---------------------------------
683 function Build_Elementary_Write_Call
(N
: Node_Id
) return Node_Id
is
684 Loc
: constant Source_Ptr
:= Sloc
(N
);
685 P_Type
: constant Entity_Id
:= Entity
(Prefix
(N
));
686 U_Type
: constant Entity_Id
:= Underlying_Type
(P_Type
);
687 Rt_Type
: constant Entity_Id
:= Root_Type
(U_Type
);
688 FST
: constant Entity_Id
:= First_Subtype
(U_Type
);
689 Strm
: constant Node_Id
:= First
(Expressions
(N
));
690 Item
: constant Node_Id
:= Next
(Strm
);
696 -- Compute the size of the stream element. This is either the size of
697 -- the first subtype or if given the size of the Stream_Size attribute.
699 if Has_Stream_Size_Clause
(FST
) then
700 P_Size
:= Static_Integer
(Expression
(Stream_Size_Clause
(FST
)));
702 P_Size
:= Esize
(FST
);
705 -- Find the routine to be called
707 -- Check for First Boolean and Character. These are enumeration types,
708 -- but we treat them specially, since they may require special handling
709 -- in the transfer protocol. However, this special handling only applies
710 -- if they have standard representation, otherwise they are treated like
711 -- any other enumeration type.
713 if Rt_Type
= Standard_Boolean
714 and then Has_Stream_Standard_Rep
(U_Type
)
718 elsif Rt_Type
= Standard_Character
719 and then Has_Stream_Standard_Rep
(U_Type
)
723 elsif Rt_Type
= Standard_Wide_Character
724 and then Has_Stream_Standard_Rep
(U_Type
)
728 elsif Rt_Type
= Standard_Wide_Wide_Character
729 and then Has_Stream_Standard_Rep
(U_Type
)
733 -- Floating point types
735 elsif Is_Floating_Point_Type
(U_Type
) then
737 -- Question: should we use P_Size or Rt_Type to distinguish between
738 -- possible floating point types? If a non-standard size or a stream
739 -- size is specified, then we should certainly use the size. But if
740 -- we have two types the same (notably Short_Float_Size = Float_Size
741 -- which is close to universally true, and Long_Long_Float_Size =
742 -- Long_Float_Size, true on most targets except the x86), then we
743 -- would really rather use the root type, so that if people want to
744 -- fiddle with System.Stream_Attributes to get inter-target portable
745 -- streams, they get the size they expect. Consider in particular the
746 -- case of a stream written on an x86, with 96-bit Long_Long_Float
747 -- being read into a non-x86 target with 64 bit Long_Long_Float. A
748 -- special version of System.Stream_Attributes can deal with this
749 -- provided the proper type is always used.
751 -- To deal with these two requirements we add the special checks
752 -- on equal sizes and use the root type to distinguish.
754 if P_Size
<= Standard_Short_Float_Size
755 and then (Standard_Short_Float_Size
/= Standard_Float_Size
756 or else Rt_Type
= Standard_Short_Float
)
760 elsif P_Size
<= Standard_Float_Size
then
763 elsif P_Size
<= Standard_Long_Float_Size
764 and then (Standard_Long_Float_Size
/= Standard_Long_Long_Float_Size
765 or else Rt_Type
= Standard_Long_Float
)
773 -- Signed integer types. Also includes signed fixed-point types and
774 -- signed enumeration types share this circuitry.
776 -- Note on signed integer types. We do not consider types as signed for
777 -- this purpose if they have no negative numbers, or if they have biased
778 -- representation. The reason is that the value in either case basically
779 -- represents an unsigned value.
781 -- For example, consider:
783 -- type W is range 0 .. 2**32 - 1;
784 -- for W'Size use 32;
786 -- This is a signed type, but the representation is unsigned, and may
787 -- be outside the range of a 32-bit signed integer, so this must be
788 -- treated as 32-bit unsigned.
790 -- Similarly, the representation is also unsigned if we have:
792 -- type W is range -1 .. +254;
795 -- forcing a biased and unsigned representation
797 elsif not Is_Unsigned_Type
(FST
)
799 (Is_Fixed_Point_Type
(U_Type
)
801 Is_Enumeration_Type
(U_Type
)
803 (Is_Signed_Integer_Type
(U_Type
)
804 and then not Has_Biased_Representation
(FST
)))
806 if P_Size
<= Standard_Short_Short_Integer_Size
then
809 elsif P_Size
<= Standard_Short_Integer_Size
then
812 elsif P_Size
= 24 then
815 elsif P_Size
<= Standard_Integer_Size
then
818 elsif P_Size
<= Standard_Long_Integer_Size
then
821 elsif P_Size
<= Standard_Long_Long_Integer_Size
then
828 -- Unsigned integer types, also includes unsigned fixed-point types
829 -- and unsigned enumeration types (note we know they are unsigned
830 -- because we already tested for signed above).
832 -- Also includes signed integer types that are unsigned in the sense
833 -- that they do not include negative numbers. See above for details.
835 elsif Is_Modular_Integer_Type
(U_Type
)
836 or else Is_Fixed_Point_Type
(U_Type
)
837 or else Is_Enumeration_Type
(U_Type
)
838 or else Is_Signed_Integer_Type
(U_Type
)
840 if P_Size
<= Standard_Short_Short_Integer_Size
then
843 elsif P_Size
<= Standard_Short_Integer_Size
then
846 elsif P_Size
= 24 then
849 elsif P_Size
<= Standard_Integer_Size
then
852 elsif P_Size
<= Standard_Long_Integer_Size
then
855 elsif P_Size
<= Standard_Long_Long_Integer_Size
then
862 else pragma Assert
(Is_Access_Type
(U_Type
));
864 if Present
(P_Size
) and then P_Size
> System_Address_Size
then
871 -- Unchecked-convert parameter to the required type (i.e. the type of
872 -- the corresponding parameter, and call the appropriate routine.
874 Libent
:= RTE
(Lib_RE
);
877 Make_Procedure_Call_Statement
(Loc
,
878 Name
=> New_Occurrence_Of
(Libent
, Loc
),
879 Parameter_Associations
=> New_List
(
880 Relocate_Node
(Strm
),
881 Unchecked_Convert_To
(Etype
(Next_Formal
(First_Formal
(Libent
))),
882 Relocate_Node
(Item
))));
883 end Build_Elementary_Write_Call
;
885 -----------------------------------------
886 -- Build_Mutable_Record_Read_Procedure --
887 -----------------------------------------
889 procedure Build_Mutable_Record_Read_Procedure
892 Pnam
: out Entity_Id
)
894 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
896 Out_Formal
: Node_Id
;
897 -- Expression denoting the out formal parameter
899 Dcls
: constant List_Id
:= New_List
;
900 -- Declarations for the 'Read body
902 Stms
: constant List_Id
:= New_List
;
903 -- Statements for the 'Read body
906 -- Entity of the discriminant being processed
908 Tmp_For_Disc
: Entity_Id
;
909 -- Temporary object used to read the value of Disc
911 Tmps_For_Discs
: constant List_Id
:= New_List
;
912 -- List of object declarations for temporaries holding the read values
913 -- for the discriminants.
915 Cstr
: constant List_Id
:= New_List
;
916 -- List of constraints to be applied on temporary record
918 Discriminant_Checks
: constant List_Id
:= New_List
;
919 -- List of discriminant checks to be performed if the actual object
922 Tmp
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_V
);
923 -- Temporary record must hide formal (assignments to components of the
924 -- record are always generated with V as the identifier for the record).
926 Constrained_Stms
: List_Id
:= New_List
;
927 -- Statements within the block where we have the constrained temporary
930 -- A mutable type cannot be a tagged type, so we generate a new name
931 -- for the stream procedure.
934 Make_Defining_Identifier
(Loc
,
935 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Read
));
937 if Is_Unchecked_Union
(Typ
) then
939 -- If this is an unchecked union, the stream procedure is erroneous,
940 -- because there are no discriminants to read.
942 -- This should generate a warning ???
945 Make_Raise_Program_Error
(Loc
,
946 Reason
=> PE_Unchecked_Union_Restriction
));
948 Build_Stream_Procedure
(Typ
, Decl
, Pnam
, Stms
, Outp
=> True);
952 Disc
:= First_Discriminant
(Typ
);
955 Make_Selected_Component
(Loc
,
956 Prefix
=> New_Occurrence_Of
(Pnam
, Loc
),
957 Selector_Name
=> Make_Identifier
(Loc
, Name_V
));
959 -- Generate Reads for the discriminants of the type. The discriminants
960 -- need to be read before the rest of the components, so that variants
961 -- are initialized correctly. The discriminants must be read into temp
962 -- variables so an incomplete Read (interrupted by an exception, for
963 -- example) does not alter the passed object.
965 while Present
(Disc
) loop
966 Tmp_For_Disc
:= Make_Defining_Identifier
(Loc
,
967 New_External_Name
(Chars
(Disc
), "D"));
969 Append_To
(Tmps_For_Discs
,
970 Make_Object_Declaration
(Loc
,
971 Defining_Identifier
=> Tmp_For_Disc
,
972 Object_Definition
=> New_Occurrence_Of
(Etype
(Disc
), Loc
)));
973 Set_No_Initialization
(Last
(Tmps_For_Discs
));
976 Make_Attribute_Reference
(Loc
,
977 Prefix
=> New_Occurrence_Of
(Etype
(Disc
), Loc
),
978 Attribute_Name
=> Name_Read
,
979 Expressions
=> New_List
(
980 Make_Identifier
(Loc
, Name_S
),
981 New_Occurrence_Of
(Tmp_For_Disc
, Loc
))));
984 Make_Discriminant_Association
(Loc
,
985 Selector_Names
=> New_List
(New_Occurrence_Of
(Disc
, Loc
)),
986 Expression
=> New_Occurrence_Of
(Tmp_For_Disc
, Loc
)));
988 Append_To
(Discriminant_Checks
,
989 Make_Raise_Constraint_Error
(Loc
,
992 Left_Opnd
=> New_Occurrence_Of
(Tmp_For_Disc
, Loc
),
994 Make_Selected_Component
(Loc
,
995 Prefix
=> New_Copy_Tree
(Out_Formal
),
996 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
))),
997 Reason
=> CE_Discriminant_Check_Failed
));
998 Next_Discriminant
(Disc
);
1001 -- Generate reads for the components of the record (including those
1002 -- that depend on discriminants).
1004 Build_Record_Read_Write_Procedure
(Typ
, Decl
, Pnam
, Name_Read
);
1006 -- Save original statement sequence for component assignments, and
1007 -- replace it with Stms.
1009 Constrained_Stms
:= Statements
(Handled_Statement_Sequence
(Decl
));
1010 Set_Handled_Statement_Sequence
(Decl
,
1011 Make_Handled_Sequence_Of_Statements
(Loc
,
1012 Statements
=> Stms
));
1014 -- If Typ has controlled components (i.e. if it is classwide or
1015 -- Has_Controlled), or components constrained using the discriminants
1016 -- of Typ, then we need to ensure that all component assignments are
1017 -- performed on an object that has been appropriately constrained
1018 -- prior to being initialized. To this effect, we wrap the component
1019 -- assignments in a block where V is a constrained temporary.
1022 Make_Object_Declaration
(Loc
,
1023 Defining_Identifier
=> Tmp
,
1024 Object_Definition
=>
1025 Make_Subtype_Indication
(Loc
,
1026 Subtype_Mark
=> New_Occurrence_Of
(Base_Type
(Typ
), Loc
),
1028 Make_Index_Or_Discriminant_Constraint
(Loc
,
1029 Constraints
=> Cstr
))));
1031 -- AI05-023-1: Insert discriminant check prior to initialization of the
1032 -- constrained temporary.
1035 Make_Implicit_If_Statement
(Pnam
,
1037 Make_Attribute_Reference
(Loc
,
1038 Prefix
=> New_Copy_Tree
(Out_Formal
),
1039 Attribute_Name
=> Name_Constrained
),
1040 Then_Statements
=> Discriminant_Checks
));
1042 -- Now insert back original component assignments, wrapped in a block
1043 -- in which V is the constrained temporary.
1046 Make_Block_Statement
(Loc
,
1047 Declarations
=> Dcls
,
1048 Handled_Statement_Sequence
=> Parent
(Constrained_Stms
)));
1050 Append_To
(Constrained_Stms
,
1051 Make_Assignment_Statement
(Loc
,
1053 Expression
=> Make_Identifier
(Loc
, Name_V
)));
1055 Set_Declarations
(Decl
, Tmps_For_Discs
);
1056 end Build_Mutable_Record_Read_Procedure
;
1058 ------------------------------------------
1059 -- Build_Mutable_Record_Write_Procedure --
1060 ------------------------------------------
1062 procedure Build_Mutable_Record_Write_Procedure
1065 Pnam
: out Entity_Id
)
1067 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1074 Disc
:= First_Discriminant
(Typ
);
1076 -- Generate Writes for the discriminants of the type
1077 -- If the type is an unchecked union, use the default values of
1078 -- the discriminants, because they are not stored.
1080 while Present
(Disc
) loop
1081 if Is_Unchecked_Union
(Typ
) then
1083 New_Copy_Tree
(Discriminant_Default_Value
(Disc
));
1086 Make_Selected_Component
(Loc
,
1087 Prefix
=> Make_Identifier
(Loc
, Name_V
),
1088 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
));
1092 Make_Attribute_Reference
(Loc
,
1093 Prefix
=> New_Occurrence_Of
(Etype
(Disc
), Loc
),
1094 Attribute_Name
=> Name_Write
,
1095 Expressions
=> New_List
(
1096 Make_Identifier
(Loc
, Name_S
),
1099 Next_Discriminant
(Disc
);
1102 -- A mutable type cannot be a tagged type, so we generate a new name
1103 -- for the stream procedure.
1106 Make_Defining_Identifier
(Loc
,
1107 Chars
=> Make_TSS_Name_Local
(Typ
, TSS_Stream_Write
));
1108 Build_Record_Read_Write_Procedure
(Typ
, Decl
, Pnam
, Name_Write
);
1110 -- Write the discriminants before the rest of the components, so
1111 -- that discriminant values are properly set of variants, etc.
1113 if Is_Non_Empty_List
(
1114 Statements
(Handled_Statement_Sequence
(Decl
)))
1117 (First
(Statements
(Handled_Statement_Sequence
(Decl
))), Stms
);
1119 Set_Statements
(Handled_Statement_Sequence
(Decl
), Stms
);
1121 end Build_Mutable_Record_Write_Procedure
;
1123 -----------------------------------------------
1124 -- Build_Record_Or_Elementary_Input_Function --
1125 -----------------------------------------------
1127 -- The function we build looks like
1129 -- function InputN (S : access RST) return Typ is
1130 -- C1 : constant Disc_Type_1;
1131 -- Discr_Type_1'Read (S, C1);
1132 -- C2 : constant Disc_Type_2;
1133 -- Discr_Type_2'Read (S, C2);
1135 -- Cn : constant Disc_Type_n;
1136 -- Discr_Type_n'Read (S, Cn);
1137 -- V : Typ (C1, C2, .. Cn)
1144 -- The discriminants are of course only present in the case of a record
1145 -- with discriminants. In the case of a record with no discriminants, or
1146 -- an elementary type, then no Cn constants are defined.
1148 procedure Build_Record_Or_Elementary_Input_Function
1151 Fnam
: out Entity_Id
)
1153 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1154 B_Typ
: constant Entity_Id
:= Underlying_Type
(Base_Type
(Typ
));
1159 Discr_Elmt
: Elmt_Id
:= No_Elmt
;
1171 -- In the presence of multiple instantiations (as in uses of the Booch
1172 -- components) the base type may be private, and the underlying type
1173 -- already constrained, in which case there's no discriminant constraint
1176 if Has_Discriminants
(Typ
)
1177 and then No
(Discriminant_Default_Value
(First_Discriminant
(Typ
)))
1178 and then not Is_Constrained
(Underlying_Type
(B_Typ
))
1180 Discr
:= First_Discriminant
(B_Typ
);
1182 -- If the prefix subtype is constrained, then retrieve the first
1183 -- element of its constraint.
1185 if Is_Constrained
(Typ
) then
1186 Discr_Elmt
:= First_Elmt
(Discriminant_Constraint
(Typ
));
1189 while Present
(Discr
) loop
1190 Cn
:= New_External_Name
('C', J
);
1193 Make_Object_Declaration
(Loc
,
1194 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Cn
),
1195 Object_Definition
=>
1196 New_Occurrence_Of
(Etype
(Discr
), Loc
));
1198 -- If this is an access discriminant, do not perform default
1199 -- initialization. The discriminant is about to get its value
1200 -- from Read, and if the type is null excluding we do not want
1201 -- spurious warnings on an initial null value.
1203 if Is_Access_Type
(Etype
(Discr
)) then
1204 Set_No_Initialization
(Decl
);
1207 Append_To
(Decls
, Decl
);
1209 Make_Attribute_Reference
(Loc
,
1210 Prefix
=> New_Occurrence_Of
(Etype
(Discr
), Loc
),
1211 Attribute_Name
=> Name_Read
,
1212 Expressions
=> New_List
(
1213 Make_Identifier
(Loc
, Name_S
),
1214 Make_Identifier
(Loc
, Cn
))));
1216 Append_To
(Constr
, Make_Identifier
(Loc
, Cn
));
1218 -- If the prefix subtype imposes a discriminant constraint, then
1219 -- check that each discriminant value equals the value read.
1221 if Present
(Discr_Elmt
) then
1223 Make_Raise_Constraint_Error
(Loc
,
1224 Condition
=> Make_Op_Ne
(Loc
,
1227 (Defining_Identifier
(Decl
), Loc
),
1229 New_Copy_Tree
(Node
(Discr_Elmt
))),
1230 Reason
=> CE_Discriminant_Check_Failed
));
1232 Next_Elmt
(Discr_Elmt
);
1235 Next_Discriminant
(Discr
);
1240 Make_Subtype_Indication
(Loc
,
1241 Subtype_Mark
=> New_Occurrence_Of
(B_Typ
, Loc
),
1243 Make_Index_Or_Discriminant_Constraint
(Loc
,
1244 Constraints
=> Constr
));
1246 -- If no discriminants, then just use the type with no constraint
1249 Odef
:= New_Occurrence_Of
(B_Typ
, Loc
);
1252 -- Create an extended return statement encapsulating the result object
1253 -- and 'Read call, which is needed in general for proper handling of
1254 -- build-in-place results (such as when the result type is inherently
1258 Make_Object_Declaration
(Loc
,
1259 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
1260 Object_Definition
=> Odef
);
1262 -- If the type is an access type, do not perform default initialization.
1263 -- The object is about to get its value from Read, and if the type is
1264 -- null excluding we do not want spurious warnings on an initial null.
1266 if Is_Access_Type
(B_Typ
) then
1267 Set_No_Initialization
(Obj_Decl
);
1271 Make_Extended_Return_Statement
(Loc
,
1272 Return_Object_Declarations
=> New_List
(Obj_Decl
),
1273 Handled_Statement_Sequence
=>
1274 Make_Handled_Sequence_Of_Statements
(Loc
,
1275 Statements
=> New_List
(
1276 Make_Attribute_Reference
(Loc
,
1277 Prefix
=> New_Occurrence_Of
(B_Typ
, Loc
),
1278 Attribute_Name
=> Name_Read
,
1279 Expressions
=> New_List
(
1280 Make_Identifier
(Loc
, Name_S
),
1281 Make_Identifier
(Loc
, Name_V
)))))));
1283 Fnam
:= Make_Stream_Subprogram_Name
(Loc
, B_Typ
, TSS_Stream_Input
);
1285 Build_Stream_Function
(B_Typ
, Decl
, Fnam
, Decls
, Stms
);
1286 end Build_Record_Or_Elementary_Input_Function
;
1288 -------------------------------------------------
1289 -- Build_Record_Or_Elementary_Output_Procedure --
1290 -------------------------------------------------
1292 procedure Build_Record_Or_Elementary_Output_Procedure
1295 Pnam
: out Entity_Id
)
1297 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1305 -- Note that of course there will be no discriminants for the elementary
1306 -- type case, so Has_Discriminants will be False. Note that the language
1307 -- rules do not allow writing the discriminants in the defaulted case,
1308 -- because those are written by 'Write.
1310 if Has_Discriminants
(Typ
)
1311 and then No
(Discriminant_Default_Value
(First_Discriminant
(Typ
)))
1313 Disc
:= First_Discriminant
(Typ
);
1314 while Present
(Disc
) loop
1316 -- If the type is an unchecked union, it must have default
1317 -- discriminants (this is checked earlier), and those defaults
1318 -- are written out to the stream.
1320 if Is_Unchecked_Union
(Typ
) then
1321 Disc_Ref
:= New_Copy_Tree
(Discriminant_Default_Value
(Disc
));
1325 Make_Selected_Component
(Loc
,
1326 Prefix
=> Make_Identifier
(Loc
, Name_V
),
1327 Selector_Name
=> New_Occurrence_Of
(Disc
, Loc
));
1331 Make_Attribute_Reference
(Loc
,
1333 New_Occurrence_Of
(Stream_Base_Type
(Etype
(Disc
)), Loc
),
1334 Attribute_Name
=> Name_Write
,
1335 Expressions
=> New_List
(
1336 Make_Identifier
(Loc
, Name_S
),
1339 Next_Discriminant
(Disc
);
1344 Make_Attribute_Reference
(Loc
,
1345 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1346 Attribute_Name
=> Name_Write
,
1347 Expressions
=> New_List
(
1348 Make_Identifier
(Loc
, Name_S
),
1349 Make_Identifier
(Loc
, Name_V
))));
1351 Pnam
:= Make_Stream_Subprogram_Name
(Loc
, Typ
, TSS_Stream_Output
);
1353 Build_Stream_Procedure
(Typ
, Decl
, Pnam
, Stms
, Outp
=> False);
1354 end Build_Record_Or_Elementary_Output_Procedure
;
1356 ---------------------------------
1357 -- Build_Record_Read_Procedure --
1358 ---------------------------------
1360 procedure Build_Record_Read_Procedure
1363 Pnam
: out Entity_Id
)
1365 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1367 Pnam
:= Make_Stream_Subprogram_Name
(Loc
, Typ
, TSS_Stream_Read
);
1368 Build_Record_Read_Write_Procedure
(Typ
, Decl
, Pnam
, Name_Read
);
1369 end Build_Record_Read_Procedure
;
1371 ---------------------------------------
1372 -- Build_Record_Read_Write_Procedure --
1373 ---------------------------------------
1375 -- The form of the record read/write procedure is as shown by the
1376 -- following example for a case with one discriminant case variant:
1378 -- procedure pnam (S : access RST, V : [out] Typ) is
1380 -- Component_Type'Read/Write (S, V.component);
1381 -- Component_Type'Read/Write (S, V.component);
1383 -- Component_Type'Read/Write (S, V.component);
1385 -- case V.discriminant is
1387 -- Component_Type'Read/Write (S, V.component);
1388 -- Component_Type'Read/Write (S, V.component);
1390 -- Component_Type'Read/Write (S, V.component);
1393 -- Component_Type'Read/Write (S, V.component);
1394 -- Component_Type'Read/Write (S, V.component);
1396 -- Component_Type'Read/Write (S, V.component);
1401 -- The out keyword for V is supplied in the Read case
1403 procedure Build_Record_Read_Write_Procedure
1409 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1414 In_Limited_Extension
: Boolean := False;
1415 -- Set to True while processing the record extension definition
1416 -- for an extension of a limited type (for which an ancestor type
1417 -- has an explicit Nam attribute definition).
1419 function Make_Component_List_Attributes
(CL
: Node_Id
) return List_Id
;
1420 -- Returns a sequence of attributes to process the components that
1421 -- are referenced in the given component list.
1423 function Make_Field_Attribute
(C
: Entity_Id
) return Node_Id
;
1424 -- Given C, the entity for a discriminant or component, build
1425 -- an attribute for the corresponding field values.
1427 function Make_Field_Attributes
(Clist
: List_Id
) return List_Id
;
1428 -- Given Clist, a component items list, construct series of attributes
1429 -- for fieldwise processing of the corresponding components.
1431 ------------------------------------
1432 -- Make_Component_List_Attributes --
1433 ------------------------------------
1435 function Make_Component_List_Attributes
(CL
: Node_Id
) return List_Id
is
1436 CI
: constant List_Id
:= Component_Items
(CL
);
1437 VP
: constant Node_Id
:= Variant_Part
(CL
);
1447 Result
:= Make_Field_Attributes
(CI
);
1449 if Present
(VP
) then
1452 V
:= First_Non_Pragma
(Variants
(VP
));
1453 while Present
(V
) loop
1456 DC
:= First
(Discrete_Choices
(V
));
1457 while Present
(DC
) loop
1458 Append_To
(DCH
, New_Copy_Tree
(DC
));
1463 Make_Case_Statement_Alternative
(Loc
,
1464 Discrete_Choices
=> DCH
,
1466 Make_Component_List_Attributes
(Component_List
(V
))));
1467 Next_Non_Pragma
(V
);
1470 -- Note: in the following, we make sure that we use new occurrence
1471 -- of for the selector, since there are cases in which we make a
1472 -- reference to a hidden discriminant that is not visible.
1474 -- If the enclosing record is an unchecked_union, we use the
1475 -- default expressions for the discriminant (it must exist)
1476 -- because we cannot generate a reference to it, given that
1477 -- it is not stored.
1479 if Is_Unchecked_Union
(Scope
(Entity
(Name
(VP
)))) then
1482 (Discriminant_Default_Value
(Entity
(Name
(VP
))));
1485 Make_Selected_Component
(Loc
,
1486 Prefix
=> Make_Identifier
(Loc
, Name_V
),
1488 New_Occurrence_Of
(Entity
(Name
(VP
)), Loc
));
1492 Make_Case_Statement
(Loc
,
1493 Expression
=> D_Ref
,
1494 Alternatives
=> Alts
));
1498 end Make_Component_List_Attributes
;
1500 --------------------------
1501 -- Make_Field_Attribute --
1502 --------------------------
1504 function Make_Field_Attribute
(C
: Entity_Id
) return Node_Id
is
1505 Field_Typ
: constant Entity_Id
:= Stream_Base_Type
(Etype
(C
));
1507 TSS_Names
: constant array (Name_Input
.. Name_Write
) of
1509 (Name_Read
=> TSS_Stream_Read
,
1510 Name_Write
=> TSS_Stream_Write
,
1511 Name_Input
=> TSS_Stream_Input
,
1512 Name_Output
=> TSS_Stream_Output
,
1513 others => TSS_Null
);
1514 pragma Assert
(TSS_Names
(Nam
) /= TSS_Null
);
1517 if In_Limited_Extension
1518 and then Is_Limited_Type
(Field_Typ
)
1519 and then No
(Find_Inherited_TSS
(Field_Typ
, TSS_Names
(Nam
)))
1521 -- The declaration is illegal per 13.13.2(9/1), and this is
1522 -- enforced in Exp_Ch3.Check_Stream_Attributes. Keep the caller
1523 -- happy by returning a null statement.
1525 return Make_Null_Statement
(Loc
);
1529 Make_Attribute_Reference
(Loc
,
1530 Prefix
=> New_Occurrence_Of
(Field_Typ
, Loc
),
1531 Attribute_Name
=> Nam
,
1532 Expressions
=> New_List
(
1533 Make_Identifier
(Loc
, Name_S
),
1534 Make_Selected_Component
(Loc
,
1535 Prefix
=> Make_Identifier
(Loc
, Name_V
),
1536 Selector_Name
=> New_Occurrence_Of
(C
, Loc
))));
1537 end Make_Field_Attribute
;
1539 ---------------------------
1540 -- Make_Field_Attributes --
1541 ---------------------------
1543 function Make_Field_Attributes
(Clist
: List_Id
) return List_Id
is
1545 Result
: constant List_Id
:= New_List
;
1548 -- Loop through components, skipping all internal components, which
1549 -- are not part of the value (e.g. _Tag), except that we don't skip
1550 -- the _Parent, since we do want to process that recursively. If
1551 -- _Parent is an interface type, being abstract with no components
1552 -- there is no need to handle it.
1554 Item
:= First
(Clist
);
1555 while Present
(Item
) loop
1556 if Nkind
(Item
) = N_Component_Declaration
1558 ((Chars
(Defining_Identifier
(Item
)) = Name_uParent
1559 and then not Is_Interface
1560 (Etype
(Defining_Identifier
(Item
))))
1562 not Is_Internal_Name
(Chars
(Defining_Identifier
(Item
))))
1566 Make_Field_Attribute
(Defining_Identifier
(Item
)));
1573 end Make_Field_Attributes
;
1575 -- Start of processing for Build_Record_Read_Write_Procedure
1578 -- For the protected type case, use corresponding record
1580 if Is_Protected_Type
(Typ
) then
1581 Typt
:= Corresponding_Record_Type
(Typ
);
1586 -- Note that we do nothing with the discriminants, since Read and
1587 -- Write do not read or write the discriminant values. All handling
1588 -- of discriminants occurs in the Input and Output subprograms.
1590 Rdef
:= Type_Definition
1591 (Declaration_Node
(Base_Type
(Underlying_Type
(Typt
))));
1594 -- In record extension case, the fields we want, including the _Parent
1595 -- field representing the parent type, are to be found in the extension.
1596 -- Note that we will naturally process the _Parent field using the type
1597 -- of the parent, and hence its stream attributes, which is appropriate.
1599 if Nkind
(Rdef
) = N_Derived_Type_Definition
then
1600 Rdef
:= Record_Extension_Part
(Rdef
);
1602 if Is_Limited_Type
(Typt
) then
1603 In_Limited_Extension
:= True;
1607 if Present
(Component_List
(Rdef
)) then
1608 Append_List_To
(Stms
,
1609 Make_Component_List_Attributes
(Component_List
(Rdef
)));
1612 Build_Stream_Procedure
1613 (Typ
, Decl
, Pnam
, Stms
, Outp
=> Nam
= Name_Read
);
1614 end Build_Record_Read_Write_Procedure
;
1616 ----------------------------------
1617 -- Build_Record_Write_Procedure --
1618 ----------------------------------
1620 procedure Build_Record_Write_Procedure
1623 Pnam
: out Entity_Id
)
1625 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1627 Pnam
:= Make_Stream_Subprogram_Name
(Loc
, Typ
, TSS_Stream_Write
);
1628 Build_Record_Read_Write_Procedure
(Typ
, Decl
, Pnam
, Name_Write
);
1629 end Build_Record_Write_Procedure
;
1631 -------------------------------
1632 -- Build_Stream_Attr_Profile --
1633 -------------------------------
1635 function Build_Stream_Attr_Profile
1638 Nam
: TSS_Name_Type
) return List_Id
1643 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1644 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1646 Profile
:= New_List
(
1647 Make_Parameter_Specification
(Loc
,
1648 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_S
),
1650 Make_Access_Definition
(Loc
,
1651 Null_Exclusion_Present
=> True,
1652 Subtype_Mark
=> New_Occurrence_Of
(
1653 Class_Wide_Type
(RTE
(RE_Root_Stream_Type
)), Loc
))));
1655 if Nam
/= TSS_Stream_Input
then
1657 Make_Parameter_Specification
(Loc
,
1658 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
1659 Out_Present
=> (Nam
= TSS_Stream_Read
),
1660 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
1664 end Build_Stream_Attr_Profile
;
1666 ---------------------------
1667 -- Build_Stream_Function --
1668 ---------------------------
1670 procedure Build_Stream_Function
1677 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1681 -- Construct function specification
1683 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1684 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1687 Make_Function_Specification
(Loc
,
1688 Defining_Unit_Name
=> Fnam
,
1690 Parameter_Specifications
=> New_List
(
1691 Make_Parameter_Specification
(Loc
,
1692 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_S
),
1694 Make_Access_Definition
(Loc
,
1695 Null_Exclusion_Present
=> True,
1698 (Class_Wide_Type
(RTE
(RE_Root_Stream_Type
)), Loc
)))),
1700 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
1703 Make_Subprogram_Body
(Loc
,
1704 Specification
=> Spec
,
1705 Declarations
=> Decls
,
1706 Handled_Statement_Sequence
=>
1707 Make_Handled_Sequence_Of_Statements
(Loc
,
1708 Statements
=> Stms
));
1709 end Build_Stream_Function
;
1711 ----------------------------
1712 -- Build_Stream_Procedure --
1713 ----------------------------
1715 procedure Build_Stream_Procedure
1722 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
1726 -- Construct procedure specification
1728 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1729 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1732 Make_Procedure_Specification
(Loc
,
1733 Defining_Unit_Name
=> Pnam
,
1735 Parameter_Specifications
=> New_List
(
1736 Make_Parameter_Specification
(Loc
,
1737 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_S
),
1739 Make_Access_Definition
(Loc
,
1740 Null_Exclusion_Present
=> True,
1743 (Class_Wide_Type
(RTE
(RE_Root_Stream_Type
)), Loc
))),
1745 Make_Parameter_Specification
(Loc
,
1746 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_V
),
1747 Out_Present
=> Outp
,
1748 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))));
1751 Make_Subprogram_Body
(Loc
,
1752 Specification
=> Spec
,
1753 Declarations
=> Empty_List
,
1754 Handled_Statement_Sequence
=>
1755 Make_Handled_Sequence_Of_Statements
(Loc
,
1756 Statements
=> Stms
));
1757 end Build_Stream_Procedure
;
1759 -----------------------------
1760 -- Has_Stream_Standard_Rep --
1761 -----------------------------
1763 function Has_Stream_Standard_Rep
(U_Type
: Entity_Id
) return Boolean is
1767 if Has_Non_Standard_Rep
(U_Type
) then
1771 if Has_Stream_Size_Clause
(U_Type
) then
1772 Siz
:= Static_Integer
(Expression
(Stream_Size_Clause
(U_Type
)));
1774 Siz
:= Esize
(First_Subtype
(U_Type
));
1777 return Siz
= Esize
(Root_Type
(U_Type
));
1778 end Has_Stream_Standard_Rep
;
1780 ---------------------------------
1781 -- Make_Stream_Subprogram_Name --
1782 ---------------------------------
1784 function Make_Stream_Subprogram_Name
1787 Nam
: TSS_Name_Type
) return Entity_Id
1792 -- For tagged types, we are dealing with a TSS associated with the
1793 -- declaration, so we use the standard primitive function name. For
1794 -- other types, generate a local TSS name since we are generating
1795 -- the subprogram at the point of use.
1797 if Is_Tagged_Type
(Typ
) then
1798 Sname
:= Make_TSS_Name
(Typ
, Nam
);
1800 Sname
:= Make_TSS_Name_Local
(Typ
, Nam
);
1803 return Make_Defining_Identifier
(Loc
, Sname
);
1804 end Make_Stream_Subprogram_Name
;
1806 ----------------------
1807 -- Stream_Base_Type --
1808 ----------------------
1810 function Stream_Base_Type
(E
: Entity_Id
) return Entity_Id
is
1812 if Is_Array_Type
(E
)
1813 and then Is_First_Subtype
(E
)
1817 return Base_Type
(E
);
1819 end Stream_Base_Type
;