2014-10-31 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / exp_strm.adb
blobdfb5f0dd2e093583b674d04ed09615ecf7ccf1a3
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ S T R M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Einfo; use Einfo;
28 with Elists; use Elists;
29 with Exp_Util; use Exp_Util;
30 with Namet; use Namet;
31 with Nlists; use Nlists;
32 with Nmake; use Nmake;
33 with Rtsfind; use Rtsfind;
34 with Sem_Aux; use Sem_Aux;
35 with Sem_Util; use Sem_Util;
36 with Sinfo; use Sinfo;
37 with Snames; use Snames;
38 with Stand; use Stand;
39 with Tbuild; use Tbuild;
40 with Ttypes; use Ttypes;
41 with Uintp; use Uintp;
43 package body Exp_Strm is
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 procedure Build_Array_Read_Write_Procedure
50 (Nod : Node_Id;
51 Typ : Entity_Id;
52 Decl : out Node_Id;
53 Pnam : Entity_Id;
54 Nam : Name_Id);
55 -- Common routine shared to build either an array Read procedure or an
56 -- array Write procedure, Nam is Name_Read or Name_Write to select which.
57 -- Pnam is the defining identifier for the constructed procedure. The
58 -- other parameters are as for Build_Array_Read_Procedure except that
59 -- the first parameter Nod supplies the Sloc to be used to generate code.
61 procedure Build_Record_Read_Write_Procedure
62 (Loc : Source_Ptr;
63 Typ : Entity_Id;
64 Decl : out Node_Id;
65 Pnam : Entity_Id;
66 Nam : Name_Id);
67 -- Common routine shared to build a record Read Write procedure, Nam
68 -- is Name_Read or Name_Write to select which. Pnam is the defining
69 -- identifier for the constructed procedure. The other parameters are
70 -- as for Build_Record_Read_Procedure.
72 procedure Build_Stream_Function
73 (Loc : Source_Ptr;
74 Typ : Entity_Id;
75 Decl : out Node_Id;
76 Fnam : Entity_Id;
77 Decls : List_Id;
78 Stms : List_Id);
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
93 (Loc : Source_Ptr;
94 Typ : Entity_Id;
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
110 -- first subtype).
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);
123 -- ..
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)
129 -- begin
130 -- Typ'Read (S, V);
131 -- return V;
132 -- end typSI[_nnn]
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
139 (Loc : Source_Ptr;
140 Typ : Entity_Id;
141 Decl : out Node_Id;
142 Fnam : out Entity_Id)
144 Dim : constant Pos := Number_Dimensions (Typ);
145 Lnam : Name_Id;
146 Hnam : Name_Id;
147 Decls : List_Id;
148 Ranges : List_Id;
149 Stms : List_Id;
150 Rstmt : Node_Id;
151 Indx : Node_Id;
152 Odecl : Node_Id;
154 begin
155 Decls := New_List;
156 Ranges := New_List;
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);
162 Append_To (Decls,
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),
167 Expression =>
168 Make_Attribute_Reference (Loc,
169 Prefix =>
170 New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
171 Attribute_Name => Name_Input,
172 Expressions => New_List (Make_Identifier (Loc, Name_S)))));
174 Append_To (Decls,
175 Make_Object_Declaration (Loc,
176 Defining_Identifier => Make_Defining_Identifier (Loc, Hnam),
177 Constant_Present => True,
178 Object_Definition =>
179 New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
180 Expression =>
181 Make_Attribute_Reference (Loc,
182 Prefix =>
183 New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
184 Attribute_Name => Name_Input,
185 Expressions => New_List (Make_Identifier (Loc, Name_S)))));
187 Append_To (Ranges,
188 Make_Range (Loc,
189 Low_Bound => Make_Identifier (Loc, Lnam),
190 High_Bound => Make_Identifier (Loc, Hnam)));
192 Next_Index (Indx);
193 end loop;
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
199 Odecl :=
200 Make_Object_Declaration (Loc,
201 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
202 Object_Definition => New_Occurrence_Of (Typ, Loc));
204 else
205 Odecl :=
206 Make_Object_Declaration (Loc,
207 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
208 Object_Definition =>
209 Make_Subtype_Indication (Loc,
210 Subtype_Mark =>
211 New_Occurrence_Of (Stream_Base_Type (Typ), Loc),
212 Constraint =>
213 Make_Index_Or_Discriminant_Constraint (Loc, Ranges)));
214 end if;
216 Rstmt :=
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)));
224 Stms := New_List (
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))));
230 Fnam :=
231 Make_Defining_Identifier (Loc,
232 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Input));
234 Build_Stream_Function (Loc, Typ, Decl, Fnam, Decls, Stms);
235 end Build_Array_Input_Function;
237 ----------------------------------
238 -- Build_Array_Output_Procedure --
239 ----------------------------------
241 procedure Build_Array_Output_Procedure
242 (Loc : Source_Ptr;
243 Typ : Entity_Id;
244 Decl : out Node_Id;
245 Pnam : out Entity_Id)
247 Stms : List_Id;
248 Indx : Node_Id;
250 begin
251 -- Build series of statements to output bounds
253 Indx := First_Index (Typ);
254 Stms := New_List;
256 for J in 1 .. Number_Dimensions (Typ) loop
257 Append_To (Stms,
258 Make_Attribute_Reference (Loc,
259 Prefix =>
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))))));
270 Append_To (Stms,
271 Make_Attribute_Reference (Loc,
272 Prefix =>
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))))));
283 Next_Index (Indx);
284 end loop;
286 -- Append Write attribute to write array elements
288 Append_To (Stms,
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))));
296 Pnam :=
297 Make_Defining_Identifier (Loc,
298 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Output));
300 Build_Stream_Procedure (Loc, Typ, Decl, Pnam, Stms, False);
301 end Build_Array_Output_Procedure;
303 --------------------------------
304 -- Build_Array_Read_Procedure --
305 --------------------------------
307 procedure Build_Array_Read_Procedure
308 (Nod : Node_Id;
309 Typ : Entity_Id;
310 Decl : out Node_Id;
311 Pnam : out Entity_Id)
313 Loc : constant Source_Ptr := Sloc (Nod);
315 begin
316 Pnam :=
317 Make_Defining_Identifier (Loc,
318 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Read));
319 Build_Array_Read_Write_Procedure (Nod, Typ, Decl, Pnam, Name_Read);
320 end Build_Array_Read_Procedure;
322 --------------------------------------
323 -- Build_Array_Read_Write_Procedure --
324 --------------------------------------
326 -- The form of the array read/write procedure is as follows:
328 -- procedure pnam (S : access RST, V : [out] Typ) is
329 -- begin
330 -- for L1 in V'Range (1) loop
331 -- for L2 in V'Range (2) loop
332 -- ...
333 -- for Ln in V'Range (n) loop
334 -- Component_Type'Read/Write (S, V (L1, L2, .. Ln));
335 -- end loop;
336 -- ..
337 -- end loop;
338 -- end loop
339 -- end pnam;
341 -- The out keyword for V is supplied in the Read case
343 procedure Build_Array_Read_Write_Procedure
344 (Nod : Node_Id;
345 Typ : Entity_Id;
346 Decl : out Node_Id;
347 Pnam : Entity_Id;
348 Nam : Name_Id)
350 Loc : constant Source_Ptr := Sloc (Nod);
351 Ndim : constant Pos := Number_Dimensions (Typ);
352 Ctyp : constant Entity_Id := Component_Type (Typ);
354 Stm : Node_Id;
355 Exl : List_Id;
356 RW : Entity_Id;
358 begin
359 -- First build the inner attribute call
361 Exl := New_List;
363 for J in 1 .. Ndim loop
364 Append_To (Exl, Make_Identifier (Loc, New_External_Name ('L', J)));
365 end loop;
367 Stm :=
368 Make_Attribute_Reference (Loc,
369 Prefix => New_Occurrence_Of (Stream_Base_Type (Ctyp), Loc),
370 Attribute_Name => Nam,
371 Expressions => New_List (
372 Make_Identifier (Loc, Name_S),
373 Make_Indexed_Component (Loc,
374 Prefix => Make_Identifier (Loc, Name_V),
375 Expressions => Exl)));
377 -- The corresponding stream attribute for the component type of the
378 -- array may be user-defined, and be frozen after the type for which
379 -- we are generating the stream subprogram. In that case, freeze the
380 -- stream attribute of the component type, whose declaration could not
381 -- generate any additional freezing actions in any case.
383 if Nam = Name_Read then
384 RW := TSS (Base_Type (Ctyp), TSS_Stream_Read);
385 else
386 RW := TSS (Base_Type (Ctyp), TSS_Stream_Write);
387 end if;
389 if Present (RW)
390 and then not Is_Frozen (RW)
391 then
392 Set_Is_Frozen (RW);
393 end if;
395 -- Now this is the big loop to wrap that statement up in a sequence
396 -- of loops. The first time around, Stm is the attribute call. The
397 -- second and subsequent times, Stm is an inner loop.
399 for J in 1 .. Ndim loop
400 Stm :=
401 Make_Implicit_Loop_Statement (Nod,
402 Iteration_Scheme =>
403 Make_Iteration_Scheme (Loc,
404 Loop_Parameter_Specification =>
405 Make_Loop_Parameter_Specification (Loc,
406 Defining_Identifier =>
407 Make_Defining_Identifier (Loc,
408 Chars => New_External_Name ('L', Ndim - J + 1)),
410 Discrete_Subtype_Definition =>
411 Make_Attribute_Reference (Loc,
412 Prefix => Make_Identifier (Loc, Name_V),
413 Attribute_Name => Name_Range,
415 Expressions => New_List (
416 Make_Integer_Literal (Loc, Ndim - J + 1))))),
418 Statements => New_List (Stm));
420 end loop;
422 Build_Stream_Procedure
423 (Loc, Typ, Decl, Pnam, New_List (Stm), Nam = Name_Read);
424 end Build_Array_Read_Write_Procedure;
426 ---------------------------------
427 -- Build_Array_Write_Procedure --
428 ---------------------------------
430 procedure Build_Array_Write_Procedure
431 (Nod : Node_Id;
432 Typ : Entity_Id;
433 Decl : out Node_Id;
434 Pnam : out Entity_Id)
436 Loc : constant Source_Ptr := Sloc (Nod);
437 begin
438 Pnam :=
439 Make_Defining_Identifier (Loc,
440 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Write));
441 Build_Array_Read_Write_Procedure (Nod, Typ, Decl, Pnam, Name_Write);
442 end Build_Array_Write_Procedure;
444 ---------------------------------
445 -- Build_Elementary_Input_Call --
446 ---------------------------------
448 function Build_Elementary_Input_Call (N : Node_Id) return Node_Id is
449 Loc : constant Source_Ptr := Sloc (N);
450 P_Type : constant Entity_Id := Entity (Prefix (N));
451 U_Type : constant Entity_Id := Underlying_Type (P_Type);
452 Rt_Type : constant Entity_Id := Root_Type (U_Type);
453 FST : constant Entity_Id := First_Subtype (U_Type);
454 Strm : constant Node_Id := First (Expressions (N));
455 Targ : constant Node_Id := Next (Strm);
456 P_Size : constant Uint := Get_Stream_Size (FST);
457 Res : Node_Id;
458 Lib_RE : RE_Id;
460 begin
462 -- Check first for Boolean and Character. These are enumeration types,
463 -- but we treat them specially, since they may require special handling
464 -- in the transfer protocol. However, this special handling only applies
465 -- if they have standard representation, otherwise they are treated like
466 -- any other enumeration type.
468 if Rt_Type = Standard_Boolean
469 and then Has_Stream_Standard_Rep (U_Type)
470 then
471 Lib_RE := RE_I_B;
473 elsif Rt_Type = Standard_Character
474 and then Has_Stream_Standard_Rep (U_Type)
475 then
476 Lib_RE := RE_I_C;
478 elsif Rt_Type = Standard_Wide_Character
479 and then Has_Stream_Standard_Rep (U_Type)
480 then
481 Lib_RE := RE_I_WC;
483 elsif Rt_Type = Standard_Wide_Wide_Character
484 and then Has_Stream_Standard_Rep (U_Type)
485 then
486 Lib_RE := RE_I_WWC;
488 -- Floating point types
490 elsif Is_Floating_Point_Type (U_Type) then
492 -- Question: should we use P_Size or Rt_Type to distinguish between
493 -- possible floating point types? If a non-standard size or a stream
494 -- size is specified, then we should certainly use the size. But if
495 -- we have two types the same (notably Short_Float_Size = Float_Size
496 -- which is close to universally true, and Long_Long_Float_Size =
497 -- Long_Float_Size, true on most targets except the x86), then we
498 -- would really rather use the root type, so that if people want to
499 -- fiddle with System.Stream_Attributes to get inter-target portable
500 -- streams, they get the size they expect. Consider in particular the
501 -- case of a stream written on an x86, with 96-bit Long_Long_Float
502 -- being read into a non-x86 target with 64 bit Long_Long_Float. A
503 -- special version of System.Stream_Attributes can deal with this
504 -- provided the proper type is always used.
506 -- To deal with these two requirements we add the special checks
507 -- on equal sizes and use the root type to distinguish.
509 if P_Size <= Standard_Short_Float_Size
510 and then (Standard_Short_Float_Size /= Standard_Float_Size
511 or else Rt_Type = Standard_Short_Float)
512 then
513 Lib_RE := RE_I_SF;
515 elsif P_Size <= Standard_Float_Size then
516 Lib_RE := RE_I_F;
518 elsif P_Size <= Standard_Long_Float_Size
519 and then (Standard_Long_Float_Size /= Standard_Long_Long_Float_Size
520 or else Rt_Type = Standard_Long_Float)
521 then
522 Lib_RE := RE_I_LF;
524 else
525 Lib_RE := RE_I_LLF;
526 end if;
528 -- Signed integer types. Also includes signed fixed-point types and
529 -- enumeration types with a signed representation.
531 -- Note on signed integer types. We do not consider types as signed for
532 -- this purpose if they have no negative numbers, or if they have biased
533 -- representation. The reason is that the value in either case basically
534 -- represents an unsigned value.
536 -- For example, consider:
538 -- type W is range 0 .. 2**32 - 1;
539 -- for W'Size use 32;
541 -- This is a signed type, but the representation is unsigned, and may
542 -- be outside the range of a 32-bit signed integer, so this must be
543 -- treated as 32-bit unsigned.
545 -- Similarly, if we have
547 -- type W is range -1 .. +254;
548 -- for W'Size use 8;
550 -- then the representation is unsigned
552 elsif not Is_Unsigned_Type (FST)
554 -- The following set of tests gets repeated many times, we should
555 -- have an abstraction defined ???
557 and then
558 (Is_Fixed_Point_Type (U_Type)
559 or else
560 Is_Enumeration_Type (U_Type)
561 or else
562 (Is_Signed_Integer_Type (U_Type)
563 and then not Has_Biased_Representation (FST)))
565 then
566 if P_Size <= Standard_Short_Short_Integer_Size then
567 Lib_RE := RE_I_SSI;
569 elsif P_Size <= Standard_Short_Integer_Size then
570 Lib_RE := RE_I_SI;
572 elsif P_Size <= Standard_Integer_Size then
573 Lib_RE := RE_I_I;
575 elsif P_Size <= Standard_Long_Integer_Size then
576 Lib_RE := RE_I_LI;
578 else
579 Lib_RE := RE_I_LLI;
580 end if;
582 -- Unsigned integer types, also includes unsigned fixed-point types
583 -- and enumeration types with an unsigned representation (note that
584 -- we know they are unsigned because we already tested for signed).
586 -- Also includes signed integer types that are unsigned in the sense
587 -- that they do not include negative numbers. See above for details.
589 elsif Is_Modular_Integer_Type (U_Type)
590 or else Is_Fixed_Point_Type (U_Type)
591 or else Is_Enumeration_Type (U_Type)
592 or else Is_Signed_Integer_Type (U_Type)
593 then
594 if P_Size <= Standard_Short_Short_Integer_Size then
595 Lib_RE := RE_I_SSU;
597 elsif P_Size <= Standard_Short_Integer_Size then
598 Lib_RE := RE_I_SU;
600 elsif P_Size <= Standard_Integer_Size then
601 Lib_RE := RE_I_U;
603 elsif P_Size <= Standard_Long_Integer_Size then
604 Lib_RE := RE_I_LU;
606 else
607 Lib_RE := RE_I_LLU;
608 end if;
610 else pragma Assert (Is_Access_Type (U_Type));
611 if P_Size > System_Address_Size then
612 Lib_RE := RE_I_AD;
613 else
614 Lib_RE := RE_I_AS;
615 end if;
616 end if;
618 -- Call the function, and do an unchecked conversion of the result
619 -- to the actual type of the prefix. If the target is a discriminant,
620 -- and we are in the body of the default implementation of a 'Read
621 -- attribute, set target type to force a constraint check (13.13.2(35)).
622 -- If the type of the discriminant is currently private, add another
623 -- unchecked conversion from the full view.
625 if Nkind (Targ) = N_Identifier
626 and then Is_Internal_Name (Chars (Targ))
627 and then Is_TSS (Scope (Entity (Targ)), TSS_Stream_Read)
628 then
629 Res :=
630 Unchecked_Convert_To (Base_Type (U_Type),
631 Make_Function_Call (Loc,
632 Name => New_Occurrence_Of (RTE (Lib_RE), Loc),
633 Parameter_Associations => New_List (
634 Relocate_Node (Strm))));
636 Set_Do_Range_Check (Res);
638 if Base_Type (P_Type) /= Base_Type (U_Type) then
639 Res := Unchecked_Convert_To (Base_Type (P_Type), Res);
640 end if;
642 return Res;
644 else
645 return
646 Unchecked_Convert_To (P_Type,
647 Make_Function_Call (Loc,
648 Name => New_Occurrence_Of (RTE (Lib_RE), Loc),
649 Parameter_Associations => New_List (
650 Relocate_Node (Strm))));
651 end if;
652 end Build_Elementary_Input_Call;
654 ---------------------------------
655 -- Build_Elementary_Write_Call --
656 ---------------------------------
658 function Build_Elementary_Write_Call (N : Node_Id) return Node_Id is
659 Loc : constant Source_Ptr := Sloc (N);
660 P_Type : constant Entity_Id := Entity (Prefix (N));
661 U_Type : constant Entity_Id := Underlying_Type (P_Type);
662 Rt_Type : constant Entity_Id := Root_Type (U_Type);
663 FST : constant Entity_Id := First_Subtype (U_Type);
664 Strm : constant Node_Id := First (Expressions (N));
665 Item : constant Node_Id := Next (Strm);
666 P_Size : Uint;
667 Lib_RE : RE_Id;
668 Libent : Entity_Id;
670 begin
672 -- Compute the size of the stream element. This is either the size of
673 -- the first subtype or if given the size of the Stream_Size attribute.
675 if Has_Stream_Size_Clause (FST) then
676 P_Size := Static_Integer (Expression (Stream_Size_Clause (FST)));
677 else
678 P_Size := Esize (FST);
679 end if;
681 -- Find the routine to be called
683 -- Check for First Boolean and Character. These are enumeration types,
684 -- but we treat them specially, since they may require special handling
685 -- in the transfer protocol. However, this special handling only applies
686 -- if they have standard representation, otherwise they are treated like
687 -- any other enumeration type.
689 if Rt_Type = Standard_Boolean
690 and then Has_Stream_Standard_Rep (U_Type)
691 then
692 Lib_RE := RE_W_B;
694 elsif Rt_Type = Standard_Character
695 and then Has_Stream_Standard_Rep (U_Type)
696 then
697 Lib_RE := RE_W_C;
699 elsif Rt_Type = Standard_Wide_Character
700 and then Has_Stream_Standard_Rep (U_Type)
701 then
702 Lib_RE := RE_W_WC;
704 elsif Rt_Type = Standard_Wide_Wide_Character
705 and then Has_Stream_Standard_Rep (U_Type)
706 then
707 Lib_RE := RE_W_WWC;
709 -- Floating point types
711 elsif Is_Floating_Point_Type (U_Type) then
713 -- Question: should we use P_Size or Rt_Type to distinguish between
714 -- possible floating point types? If a non-standard size or a stream
715 -- size is specified, then we should certainly use the size. But if
716 -- we have two types the same (notably Short_Float_Size = Float_Size
717 -- which is close to universally true, and Long_Long_Float_Size =
718 -- Long_Float_Size, true on most targets except the x86), then we
719 -- would really rather use the root type, so that if people want to
720 -- fiddle with System.Stream_Attributes to get inter-target portable
721 -- streams, they get the size they expect. Consider in particular the
722 -- case of a stream written on an x86, with 96-bit Long_Long_Float
723 -- being read into a non-x86 target with 64 bit Long_Long_Float. A
724 -- special version of System.Stream_Attributes can deal with this
725 -- provided the proper type is always used.
727 -- To deal with these two requirements we add the special checks
728 -- on equal sizes and use the root type to distinguish.
730 if P_Size <= Standard_Short_Float_Size
731 and then (Standard_Short_Float_Size /= Standard_Float_Size
732 or else Rt_Type = Standard_Short_Float)
733 then
734 Lib_RE := RE_W_SF;
736 elsif P_Size <= Standard_Float_Size then
737 Lib_RE := RE_W_F;
739 elsif P_Size <= Standard_Long_Float_Size
740 and then (Standard_Long_Float_Size /= Standard_Long_Long_Float_Size
741 or else Rt_Type = Standard_Long_Float)
742 then
743 Lib_RE := RE_W_LF;
745 else
746 Lib_RE := RE_W_LLF;
747 end if;
749 -- Signed integer types. Also includes signed fixed-point types and
750 -- signed enumeration types share this circuitry.
752 -- Note on signed integer types. We do not consider types as signed for
753 -- this purpose if they have no negative numbers, or if they have biased
754 -- representation. The reason is that the value in either case basically
755 -- represents an unsigned value.
757 -- For example, consider:
759 -- type W is range 0 .. 2**32 - 1;
760 -- for W'Size use 32;
762 -- This is a signed type, but the representation is unsigned, and may
763 -- be outside the range of a 32-bit signed integer, so this must be
764 -- treated as 32-bit unsigned.
766 -- Similarly, the representation is also unsigned if we have:
768 -- type W is range -1 .. +254;
769 -- for W'Size use 8;
771 -- forcing a biased and unsigned representation
773 elsif not Is_Unsigned_Type (FST)
774 and then
775 (Is_Fixed_Point_Type (U_Type)
776 or else
777 Is_Enumeration_Type (U_Type)
778 or else
779 (Is_Signed_Integer_Type (U_Type)
780 and then not Has_Biased_Representation (FST)))
781 then
782 if P_Size <= Standard_Short_Short_Integer_Size then
783 Lib_RE := RE_W_SSI;
784 elsif P_Size <= Standard_Short_Integer_Size then
785 Lib_RE := RE_W_SI;
786 elsif P_Size <= Standard_Integer_Size then
787 Lib_RE := RE_W_I;
788 elsif P_Size <= Standard_Long_Integer_Size then
789 Lib_RE := RE_W_LI;
790 else
791 Lib_RE := RE_W_LLI;
792 end if;
794 -- Unsigned integer types, also includes unsigned fixed-point types
795 -- and unsigned enumeration types (note we know they are unsigned
796 -- because we already tested for signed above).
798 -- Also includes signed integer types that are unsigned in the sense
799 -- that they do not include negative numbers. See above for details.
801 elsif Is_Modular_Integer_Type (U_Type)
802 or else Is_Fixed_Point_Type (U_Type)
803 or else Is_Enumeration_Type (U_Type)
804 or else Is_Signed_Integer_Type (U_Type)
805 then
806 if P_Size <= Standard_Short_Short_Integer_Size then
807 Lib_RE := RE_W_SSU;
808 elsif P_Size <= Standard_Short_Integer_Size then
809 Lib_RE := RE_W_SU;
810 elsif P_Size <= Standard_Integer_Size then
811 Lib_RE := RE_W_U;
812 elsif P_Size <= Standard_Long_Integer_Size then
813 Lib_RE := RE_W_LU;
814 else
815 Lib_RE := RE_W_LLU;
816 end if;
818 else pragma Assert (Is_Access_Type (U_Type));
820 if P_Size > System_Address_Size then
821 Lib_RE := RE_W_AD;
822 else
823 Lib_RE := RE_W_AS;
824 end if;
825 end if;
827 -- Unchecked-convert parameter to the required type (i.e. the type of
828 -- the corresponding parameter, and call the appropriate routine.
830 Libent := RTE (Lib_RE);
832 return
833 Make_Procedure_Call_Statement (Loc,
834 Name => New_Occurrence_Of (Libent, Loc),
835 Parameter_Associations => New_List (
836 Relocate_Node (Strm),
837 Unchecked_Convert_To (Etype (Next_Formal (First_Formal (Libent))),
838 Relocate_Node (Item))));
839 end Build_Elementary_Write_Call;
841 -----------------------------------------
842 -- Build_Mutable_Record_Read_Procedure --
843 -----------------------------------------
845 procedure Build_Mutable_Record_Read_Procedure
846 (Loc : Source_Ptr;
847 Typ : Entity_Id;
848 Decl : out Node_Id;
849 Pnam : out Entity_Id)
851 Out_Formal : Node_Id;
852 -- Expression denoting the out formal parameter
854 Dcls : constant List_Id := New_List;
855 -- Declarations for the 'Read body
857 Stms : constant List_Id := New_List;
858 -- Statements for the 'Read body
860 Disc : Entity_Id;
861 -- Entity of the discriminant being processed
863 Tmp_For_Disc : Entity_Id;
864 -- Temporary object used to read the value of Disc
866 Tmps_For_Discs : constant List_Id := New_List;
867 -- List of object declarations for temporaries holding the read values
868 -- for the discriminants.
870 Cstr : constant List_Id := New_List;
871 -- List of constraints to be applied on temporary record
873 Discriminant_Checks : constant List_Id := New_List;
874 -- List of discriminant checks to be performed if the actual object
875 -- is constrained.
877 Tmp : constant Entity_Id := Make_Defining_Identifier (Loc, Name_V);
878 -- Temporary record must hide formal (assignments to components of the
879 -- record are always generated with V as the identifier for the record).
881 Constrained_Stms : List_Id := New_List;
882 -- Statements within the block where we have the constrained temporary
884 begin
885 -- A mutable type cannot be a tagged type, so we generate a new name
886 -- for the stream procedure.
888 Pnam :=
889 Make_Defining_Identifier (Loc,
890 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Read));
892 if Is_Unchecked_Union (Typ) then
894 -- If this is an unchecked union, the stream procedure is erroneous,
895 -- because there are no discriminants to read.
897 -- This should generate a warning ???
899 Append_To (Stms,
900 Make_Raise_Program_Error (Loc,
901 Reason => PE_Unchecked_Union_Restriction));
903 Build_Stream_Procedure (Loc, Typ, Decl, Pnam, Stms, Outp => True);
904 return;
905 end if;
907 Disc := First_Discriminant (Typ);
909 Out_Formal :=
910 Make_Selected_Component (Loc,
911 Prefix => New_Occurrence_Of (Pnam, Loc),
912 Selector_Name => Make_Identifier (Loc, Name_V));
914 -- Generate Reads for the discriminants of the type. The discriminants
915 -- need to be read before the rest of the components, so that variants
916 -- are initialized correctly. The discriminants must be read into temp
917 -- variables so an incomplete Read (interrupted by an exception, for
918 -- example) does not alter the passed object.
920 while Present (Disc) loop
921 Tmp_For_Disc := Make_Defining_Identifier (Loc,
922 New_External_Name (Chars (Disc), "D"));
924 Append_To (Tmps_For_Discs,
925 Make_Object_Declaration (Loc,
926 Defining_Identifier => Tmp_For_Disc,
927 Object_Definition => New_Occurrence_Of (Etype (Disc), Loc)));
928 Set_No_Initialization (Last (Tmps_For_Discs));
930 Append_To (Stms,
931 Make_Attribute_Reference (Loc,
932 Prefix => New_Occurrence_Of (Etype (Disc), Loc),
933 Attribute_Name => Name_Read,
934 Expressions => New_List (
935 Make_Identifier (Loc, Name_S),
936 New_Occurrence_Of (Tmp_For_Disc, Loc))));
938 Append_To (Cstr,
939 Make_Discriminant_Association (Loc,
940 Selector_Names => New_List (New_Occurrence_Of (Disc, Loc)),
941 Expression => New_Occurrence_Of (Tmp_For_Disc, Loc)));
943 Append_To (Discriminant_Checks,
944 Make_Raise_Constraint_Error (Loc,
945 Condition =>
946 Make_Op_Ne (Loc,
947 Left_Opnd => New_Occurrence_Of (Tmp_For_Disc, Loc),
948 Right_Opnd =>
949 Make_Selected_Component (Loc,
950 Prefix => New_Copy_Tree (Out_Formal),
951 Selector_Name => New_Occurrence_Of (Disc, Loc))),
952 Reason => CE_Discriminant_Check_Failed));
953 Next_Discriminant (Disc);
954 end loop;
956 -- Generate reads for the components of the record (including those
957 -- that depend on discriminants).
959 Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Read);
961 -- Save original statement sequence for component assignments, and
962 -- replace it with Stms.
964 Constrained_Stms := Statements (Handled_Statement_Sequence (Decl));
965 Set_Handled_Statement_Sequence (Decl,
966 Make_Handled_Sequence_Of_Statements (Loc,
967 Statements => Stms));
969 -- If Typ has controlled components (i.e. if it is classwide
970 -- or Has_Controlled), or components constrained using the discriminants
971 -- of Typ, then we need to ensure that all component assignments
972 -- are performed on an object that has been appropriately constrained
973 -- prior to being initialized. To this effect, we wrap the component
974 -- assignments in a block where V is a constrained temporary.
976 Append_To (Dcls,
977 Make_Object_Declaration (Loc,
978 Defining_Identifier => Tmp,
979 Object_Definition =>
980 Make_Subtype_Indication (Loc,
981 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
982 Constraint =>
983 Make_Index_Or_Discriminant_Constraint (Loc,
984 Constraints => Cstr))));
986 -- AI05-023-1: Insert discriminant check prior to initialization of the
987 -- constrained temporary.
989 Append_To (Stms,
990 Make_Implicit_If_Statement (Pnam,
991 Condition =>
992 Make_Attribute_Reference (Loc,
993 Prefix => New_Copy_Tree (Out_Formal),
994 Attribute_Name => Name_Constrained),
995 Then_Statements => Discriminant_Checks));
997 -- Now insert back original component assignments, wrapped in a block
998 -- in which V is the constrained temporary.
1000 Append_To (Stms,
1001 Make_Block_Statement (Loc,
1002 Declarations => Dcls,
1003 Handled_Statement_Sequence => Parent (Constrained_Stms)));
1005 Append_To (Constrained_Stms,
1006 Make_Assignment_Statement (Loc,
1007 Name => Out_Formal,
1008 Expression => Make_Identifier (Loc, Name_V)));
1010 Set_Declarations (Decl, Tmps_For_Discs);
1011 end Build_Mutable_Record_Read_Procedure;
1013 ------------------------------------------
1014 -- Build_Mutable_Record_Write_Procedure --
1015 ------------------------------------------
1017 procedure Build_Mutable_Record_Write_Procedure
1018 (Loc : Source_Ptr;
1019 Typ : Entity_Id;
1020 Decl : out Node_Id;
1021 Pnam : out Entity_Id)
1023 Stms : List_Id;
1024 Disc : Entity_Id;
1025 D_Ref : Node_Id;
1027 begin
1028 Stms := New_List;
1029 Disc := First_Discriminant (Typ);
1031 -- Generate Writes for the discriminants of the type
1032 -- If the type is an unchecked union, use the default values of
1033 -- the discriminants, because they are not stored.
1035 while Present (Disc) loop
1036 if Is_Unchecked_Union (Typ) then
1037 D_Ref :=
1038 New_Copy_Tree (Discriminant_Default_Value (Disc));
1039 else
1040 D_Ref :=
1041 Make_Selected_Component (Loc,
1042 Prefix => Make_Identifier (Loc, Name_V),
1043 Selector_Name => New_Occurrence_Of (Disc, Loc));
1044 end if;
1046 Append_To (Stms,
1047 Make_Attribute_Reference (Loc,
1048 Prefix => New_Occurrence_Of (Etype (Disc), Loc),
1049 Attribute_Name => Name_Write,
1050 Expressions => New_List (
1051 Make_Identifier (Loc, Name_S),
1052 D_Ref)));
1054 Next_Discriminant (Disc);
1055 end loop;
1057 -- A mutable type cannot be a tagged type, so we generate a new name
1058 -- for the stream procedure.
1060 Pnam :=
1061 Make_Defining_Identifier (Loc,
1062 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Write));
1063 Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Write);
1065 -- Write the discriminants before the rest of the components, so
1066 -- that discriminant values are properly set of variants, etc.
1068 if Is_Non_Empty_List (
1069 Statements (Handled_Statement_Sequence (Decl)))
1070 then
1071 Insert_List_Before
1072 (First (Statements (Handled_Statement_Sequence (Decl))), Stms);
1073 else
1074 Set_Statements (Handled_Statement_Sequence (Decl), Stms);
1075 end if;
1076 end Build_Mutable_Record_Write_Procedure;
1078 -----------------------------------------------
1079 -- Build_Record_Or_Elementary_Input_Function --
1080 -----------------------------------------------
1082 -- The function we build looks like
1084 -- function InputN (S : access RST) return Typ is
1085 -- C1 : constant Disc_Type_1;
1086 -- Discr_Type_1'Read (S, C1);
1087 -- C2 : constant Disc_Type_2;
1088 -- Discr_Type_2'Read (S, C2);
1089 -- ...
1090 -- Cn : constant Disc_Type_n;
1091 -- Discr_Type_n'Read (S, Cn);
1092 -- V : Typ (C1, C2, .. Cn)
1094 -- begin
1095 -- Typ'Read (S, V);
1096 -- return V;
1097 -- end InputN
1099 -- The discriminants are of course only present in the case of a record
1100 -- with discriminants. In the case of a record with no discriminants, or
1101 -- an elementary type, then no Cn constants are defined.
1103 procedure Build_Record_Or_Elementary_Input_Function
1104 (Loc : Source_Ptr;
1105 Typ : Entity_Id;
1106 Decl : out Node_Id;
1107 Fnam : out Entity_Id)
1109 B_Typ : constant Entity_Id := Base_Type (Typ);
1110 Cn : Name_Id;
1111 Constr : List_Id;
1112 Decls : List_Id;
1113 Discr : Entity_Id;
1114 Discr_Elmt : Elmt_Id := No_Elmt;
1115 J : Pos;
1116 Obj_Decl : Node_Id;
1117 Odef : Node_Id;
1118 Stms : List_Id;
1120 begin
1121 Decls := New_List;
1122 Constr := New_List;
1124 J := 1;
1126 if Has_Discriminants (Typ)
1127 and then
1128 No (Discriminant_Default_Value (First_Discriminant (Typ)))
1129 then
1130 Discr := First_Discriminant (B_Typ);
1132 -- If the prefix subtype is constrained, then retrieve the first
1133 -- element of its constraint.
1135 if Is_Constrained (Typ) then
1136 Discr_Elmt := First_Elmt (Discriminant_Constraint (Typ));
1137 end if;
1139 while Present (Discr) loop
1140 Cn := New_External_Name ('C', J);
1142 Decl :=
1143 Make_Object_Declaration (Loc,
1144 Defining_Identifier => Make_Defining_Identifier (Loc, Cn),
1145 Object_Definition =>
1146 New_Occurrence_Of (Etype (Discr), Loc));
1148 -- If this is an access discriminant, do not perform default
1149 -- initialization. The discriminant is about to get its value
1150 -- from Read, and if the type is null excluding we do not want
1151 -- spurious warnings on an initial null value.
1153 if Is_Access_Type (Etype (Discr)) then
1154 Set_No_Initialization (Decl);
1155 end if;
1157 Append_To (Decls, Decl);
1158 Append_To (Decls,
1159 Make_Attribute_Reference (Loc,
1160 Prefix => New_Occurrence_Of (Etype (Discr), Loc),
1161 Attribute_Name => Name_Read,
1162 Expressions => New_List (
1163 Make_Identifier (Loc, Name_S),
1164 Make_Identifier (Loc, Cn))));
1166 Append_To (Constr, Make_Identifier (Loc, Cn));
1168 -- If the prefix subtype imposes a discriminant constraint, then
1169 -- check that each discriminant value equals the value read.
1171 if Present (Discr_Elmt) then
1172 Append_To (Decls,
1173 Make_Raise_Constraint_Error (Loc,
1174 Condition => Make_Op_Ne (Loc,
1175 Left_Opnd =>
1176 New_Occurrence_Of
1177 (Defining_Identifier (Decl), Loc),
1178 Right_Opnd =>
1179 New_Copy_Tree (Node (Discr_Elmt))),
1180 Reason => CE_Discriminant_Check_Failed));
1182 Next_Elmt (Discr_Elmt);
1183 end if;
1185 Next_Discriminant (Discr);
1186 J := J + 1;
1187 end loop;
1189 Odef :=
1190 Make_Subtype_Indication (Loc,
1191 Subtype_Mark => New_Occurrence_Of (B_Typ, Loc),
1192 Constraint =>
1193 Make_Index_Or_Discriminant_Constraint (Loc,
1194 Constraints => Constr));
1196 -- If no discriminants, then just use the type with no constraint
1198 else
1199 Odef := New_Occurrence_Of (B_Typ, Loc);
1200 end if;
1202 -- Create an extended return statement encapsulating the result object
1203 -- and 'Read call, which is needed in general for proper handling of
1204 -- build-in-place results (such as when the result type is inherently
1205 -- limited).
1207 Obj_Decl :=
1208 Make_Object_Declaration (Loc,
1209 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
1210 Object_Definition => Odef);
1212 -- If the type is an access type, do not perform default initialization.
1213 -- The object is about to get its value from Read, and if the type is
1214 -- null excluding we do not want spurious warnings on an initial null.
1216 if Is_Access_Type (B_Typ) then
1217 Set_No_Initialization (Obj_Decl);
1218 end if;
1220 Stms := New_List (
1221 Make_Extended_Return_Statement (Loc,
1222 Return_Object_Declarations => New_List (Obj_Decl),
1223 Handled_Statement_Sequence =>
1224 Make_Handled_Sequence_Of_Statements (Loc,
1225 Statements => New_List (
1226 Make_Attribute_Reference (Loc,
1227 Prefix => New_Occurrence_Of (B_Typ, Loc),
1228 Attribute_Name => Name_Read,
1229 Expressions => New_List (
1230 Make_Identifier (Loc, Name_S),
1231 Make_Identifier (Loc, Name_V)))))));
1233 Fnam := Make_Stream_Subprogram_Name (Loc, B_Typ, TSS_Stream_Input);
1235 Build_Stream_Function (Loc, B_Typ, Decl, Fnam, Decls, Stms);
1236 end Build_Record_Or_Elementary_Input_Function;
1238 -------------------------------------------------
1239 -- Build_Record_Or_Elementary_Output_Procedure --
1240 -------------------------------------------------
1242 procedure Build_Record_Or_Elementary_Output_Procedure
1243 (Loc : Source_Ptr;
1244 Typ : Entity_Id;
1245 Decl : out Node_Id;
1246 Pnam : out Entity_Id)
1248 Stms : List_Id;
1249 Disc : Entity_Id;
1250 Disc_Ref : Node_Id;
1252 begin
1253 Stms := New_List;
1255 -- Note that of course there will be no discriminants for the elementary
1256 -- type case, so Has_Discriminants will be False. Note that the language
1257 -- rules do not allow writing the discriminants in the defaulted case,
1258 -- because those are written by 'Write.
1260 if Has_Discriminants (Typ)
1261 and then
1262 No (Discriminant_Default_Value (First_Discriminant (Typ)))
1263 then
1264 Disc := First_Discriminant (Typ);
1266 while Present (Disc) loop
1268 -- If the type is an unchecked union, it must have default
1269 -- discriminants (this is checked earlier), and those defaults
1270 -- are written out to the stream.
1272 if Is_Unchecked_Union (Typ) then
1273 Disc_Ref := New_Copy_Tree (Discriminant_Default_Value (Disc));
1275 else
1276 Disc_Ref :=
1277 Make_Selected_Component (Loc,
1278 Prefix => Make_Identifier (Loc, Name_V),
1279 Selector_Name => New_Occurrence_Of (Disc, Loc));
1280 end if;
1282 Append_To (Stms,
1283 Make_Attribute_Reference (Loc,
1284 Prefix =>
1285 New_Occurrence_Of (Stream_Base_Type (Etype (Disc)), Loc),
1286 Attribute_Name => Name_Write,
1287 Expressions => New_List (
1288 Make_Identifier (Loc, Name_S),
1289 Disc_Ref)));
1291 Next_Discriminant (Disc);
1292 end loop;
1293 end if;
1295 Append_To (Stms,
1296 Make_Attribute_Reference (Loc,
1297 Prefix => New_Occurrence_Of (Typ, Loc),
1298 Attribute_Name => Name_Write,
1299 Expressions => New_List (
1300 Make_Identifier (Loc, Name_S),
1301 Make_Identifier (Loc, Name_V))));
1303 Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Output);
1305 Build_Stream_Procedure (Loc, Typ, Decl, Pnam, Stms, False);
1306 end Build_Record_Or_Elementary_Output_Procedure;
1308 ---------------------------------
1309 -- Build_Record_Read_Procedure --
1310 ---------------------------------
1312 procedure Build_Record_Read_Procedure
1313 (Loc : Source_Ptr;
1314 Typ : Entity_Id;
1315 Decl : out Node_Id;
1316 Pnam : out Entity_Id)
1318 begin
1319 Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Read);
1320 Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Read);
1321 end Build_Record_Read_Procedure;
1323 ---------------------------------------
1324 -- Build_Record_Read_Write_Procedure --
1325 ---------------------------------------
1327 -- The form of the record read/write procedure is as shown by the
1328 -- following example for a case with one discriminant case variant:
1330 -- procedure pnam (S : access RST, V : [out] Typ) is
1331 -- begin
1332 -- Component_Type'Read/Write (S, V.component);
1333 -- Component_Type'Read/Write (S, V.component);
1334 -- ...
1335 -- Component_Type'Read/Write (S, V.component);
1337 -- case V.discriminant is
1338 -- when choices =>
1339 -- Component_Type'Read/Write (S, V.component);
1340 -- Component_Type'Read/Write (S, V.component);
1341 -- ...
1342 -- Component_Type'Read/Write (S, V.component);
1344 -- when choices =>
1345 -- Component_Type'Read/Write (S, V.component);
1346 -- Component_Type'Read/Write (S, V.component);
1347 -- ...
1348 -- Component_Type'Read/Write (S, V.component);
1349 -- ...
1350 -- end case;
1351 -- end pnam;
1353 -- The out keyword for V is supplied in the Read case
1355 procedure Build_Record_Read_Write_Procedure
1356 (Loc : Source_Ptr;
1357 Typ : Entity_Id;
1358 Decl : out Node_Id;
1359 Pnam : Entity_Id;
1360 Nam : Name_Id)
1362 Rdef : Node_Id;
1363 Stms : List_Id;
1364 Typt : Entity_Id;
1366 In_Limited_Extension : Boolean := False;
1367 -- Set to True while processing the record extension definition
1368 -- for an extension of a limited type (for which an ancestor type
1369 -- has an explicit Nam attribute definition).
1371 function Make_Component_List_Attributes (CL : Node_Id) return List_Id;
1372 -- Returns a sequence of attributes to process the components that
1373 -- are referenced in the given component list.
1375 function Make_Field_Attribute (C : Entity_Id) return Node_Id;
1376 -- Given C, the entity for a discriminant or component, build
1377 -- an attribute for the corresponding field values.
1379 function Make_Field_Attributes (Clist : List_Id) return List_Id;
1380 -- Given Clist, a component items list, construct series of attributes
1381 -- for fieldwise processing of the corresponding components.
1383 ------------------------------------
1384 -- Make_Component_List_Attributes --
1385 ------------------------------------
1387 function Make_Component_List_Attributes (CL : Node_Id) return List_Id is
1388 CI : constant List_Id := Component_Items (CL);
1389 VP : constant Node_Id := Variant_Part (CL);
1391 Result : List_Id;
1392 Alts : List_Id;
1393 V : Node_Id;
1394 DC : Node_Id;
1395 DCH : List_Id;
1396 D_Ref : Node_Id;
1398 begin
1399 Result := Make_Field_Attributes (CI);
1401 if Present (VP) then
1402 Alts := New_List;
1404 V := First_Non_Pragma (Variants (VP));
1405 while Present (V) loop
1406 DCH := New_List;
1408 DC := First (Discrete_Choices (V));
1409 while Present (DC) loop
1410 Append_To (DCH, New_Copy_Tree (DC));
1411 Next (DC);
1412 end loop;
1414 Append_To (Alts,
1415 Make_Case_Statement_Alternative (Loc,
1416 Discrete_Choices => DCH,
1417 Statements =>
1418 Make_Component_List_Attributes (Component_List (V))));
1419 Next_Non_Pragma (V);
1420 end loop;
1422 -- Note: in the following, we make sure that we use new occurrence
1423 -- of for the selector, since there are cases in which we make a
1424 -- reference to a hidden discriminant that is not visible.
1426 -- If the enclosing record is an unchecked_union, we use the
1427 -- default expressions for the discriminant (it must exist)
1428 -- because we cannot generate a reference to it, given that
1429 -- it is not stored.
1431 if Is_Unchecked_Union (Scope (Entity (Name (VP)))) then
1432 D_Ref :=
1433 New_Copy_Tree
1434 (Discriminant_Default_Value (Entity (Name (VP))));
1435 else
1436 D_Ref :=
1437 Make_Selected_Component (Loc,
1438 Prefix => Make_Identifier (Loc, Name_V),
1439 Selector_Name =>
1440 New_Occurrence_Of (Entity (Name (VP)), Loc));
1441 end if;
1443 Append_To (Result,
1444 Make_Case_Statement (Loc,
1445 Expression => D_Ref,
1446 Alternatives => Alts));
1447 end if;
1449 return Result;
1450 end Make_Component_List_Attributes;
1452 --------------------------
1453 -- Make_Field_Attribute --
1454 --------------------------
1456 function Make_Field_Attribute (C : Entity_Id) return Node_Id is
1457 Field_Typ : constant Entity_Id := Stream_Base_Type (Etype (C));
1459 TSS_Names : constant array (Name_Input .. Name_Write) of
1460 TSS_Name_Type :=
1461 (Name_Read => TSS_Stream_Read,
1462 Name_Write => TSS_Stream_Write,
1463 Name_Input => TSS_Stream_Input,
1464 Name_Output => TSS_Stream_Output,
1465 others => TSS_Null);
1466 pragma Assert (TSS_Names (Nam) /= TSS_Null);
1468 begin
1469 if In_Limited_Extension
1470 and then Is_Limited_Type (Field_Typ)
1471 and then No (Find_Inherited_TSS (Field_Typ, TSS_Names (Nam)))
1472 then
1473 -- The declaration is illegal per 13.13.2(9/1), and this is
1474 -- enforced in Exp_Ch3.Check_Stream_Attributes. Keep the caller
1475 -- happy by returning a null statement.
1477 return Make_Null_Statement (Loc);
1478 end if;
1480 return
1481 Make_Attribute_Reference (Loc,
1482 Prefix =>
1483 New_Occurrence_Of (Field_Typ, Loc),
1484 Attribute_Name => Nam,
1485 Expressions => New_List (
1486 Make_Identifier (Loc, Name_S),
1487 Make_Selected_Component (Loc,
1488 Prefix => Make_Identifier (Loc, Name_V),
1489 Selector_Name => New_Occurrence_Of (C, Loc))));
1490 end Make_Field_Attribute;
1492 ---------------------------
1493 -- Make_Field_Attributes --
1494 ---------------------------
1496 function Make_Field_Attributes (Clist : List_Id) return List_Id is
1497 Item : Node_Id;
1498 Result : List_Id;
1500 begin
1501 Result := New_List;
1503 if Present (Clist) then
1504 Item := First (Clist);
1506 -- Loop through components, skipping all internal components,
1507 -- which are not part of the value (e.g. _Tag), except that we
1508 -- don't skip the _Parent, since we do want to process that
1509 -- recursively. If _Parent is an interface type, being abstract
1510 -- with no components there is no need to handle it.
1512 while Present (Item) loop
1513 if Nkind (Item) = N_Component_Declaration
1514 and then
1515 ((Chars (Defining_Identifier (Item)) = Name_uParent
1516 and then not Is_Interface
1517 (Etype (Defining_Identifier (Item))))
1518 or else
1519 not Is_Internal_Name (Chars (Defining_Identifier (Item))))
1520 then
1521 Append_To
1522 (Result,
1523 Make_Field_Attribute (Defining_Identifier (Item)));
1524 end if;
1526 Next (Item);
1527 end loop;
1528 end if;
1530 return Result;
1531 end Make_Field_Attributes;
1533 -- Start of processing for Build_Record_Read_Write_Procedure
1535 begin
1536 -- For the protected type case, use corresponding record
1538 if Is_Protected_Type (Typ) then
1539 Typt := Corresponding_Record_Type (Typ);
1540 else
1541 Typt := Typ;
1542 end if;
1544 -- Note that we do nothing with the discriminants, since Read and
1545 -- Write do not read or write the discriminant values. All handling
1546 -- of discriminants occurs in the Input and Output subprograms.
1548 Rdef := Type_Definition
1549 (Declaration_Node (Base_Type (Underlying_Type (Typt))));
1550 Stms := Empty_List;
1552 -- In record extension case, the fields we want, including the _Parent
1553 -- field representing the parent type, are to be found in the extension.
1554 -- Note that we will naturally process the _Parent field using the type
1555 -- of the parent, and hence its stream attributes, which is appropriate.
1557 if Nkind (Rdef) = N_Derived_Type_Definition then
1558 Rdef := Record_Extension_Part (Rdef);
1560 if Is_Limited_Type (Typt) then
1561 In_Limited_Extension := True;
1562 end if;
1563 end if;
1565 if Present (Component_List (Rdef)) then
1566 Append_List_To (Stms,
1567 Make_Component_List_Attributes (Component_List (Rdef)));
1568 end if;
1570 Build_Stream_Procedure
1571 (Loc, Typ, Decl, Pnam, Stms, Nam = Name_Read);
1572 end Build_Record_Read_Write_Procedure;
1574 ----------------------------------
1575 -- Build_Record_Write_Procedure --
1576 ----------------------------------
1578 procedure Build_Record_Write_Procedure
1579 (Loc : Source_Ptr;
1580 Typ : Entity_Id;
1581 Decl : out Node_Id;
1582 Pnam : out Entity_Id)
1584 begin
1585 Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Write);
1586 Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Write);
1587 end Build_Record_Write_Procedure;
1589 -------------------------------
1590 -- Build_Stream_Attr_Profile --
1591 -------------------------------
1593 function Build_Stream_Attr_Profile
1594 (Loc : Source_Ptr;
1595 Typ : Entity_Id;
1596 Nam : TSS_Name_Type) return List_Id
1598 Profile : List_Id;
1600 begin
1601 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1602 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1604 Profile := New_List (
1605 Make_Parameter_Specification (Loc,
1606 Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
1607 Parameter_Type =>
1608 Make_Access_Definition (Loc,
1609 Null_Exclusion_Present => True,
1610 Subtype_Mark => New_Occurrence_Of (
1611 Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc))));
1613 if Nam /= TSS_Stream_Input then
1614 Append_To (Profile,
1615 Make_Parameter_Specification (Loc,
1616 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
1617 Out_Present => (Nam = TSS_Stream_Read),
1618 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
1619 end if;
1621 return Profile;
1622 end Build_Stream_Attr_Profile;
1624 ---------------------------
1625 -- Build_Stream_Function --
1626 ---------------------------
1628 procedure Build_Stream_Function
1629 (Loc : Source_Ptr;
1630 Typ : Entity_Id;
1631 Decl : out Node_Id;
1632 Fnam : Entity_Id;
1633 Decls : List_Id;
1634 Stms : List_Id)
1636 Spec : Node_Id;
1638 begin
1639 -- Construct function specification
1641 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1642 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1644 Spec :=
1645 Make_Function_Specification (Loc,
1646 Defining_Unit_Name => Fnam,
1648 Parameter_Specifications => New_List (
1649 Make_Parameter_Specification (Loc,
1650 Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
1651 Parameter_Type =>
1652 Make_Access_Definition (Loc,
1653 Null_Exclusion_Present => True,
1654 Subtype_Mark => New_Occurrence_Of (
1655 Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc)))),
1657 Result_Definition => New_Occurrence_Of (Typ, Loc));
1659 Decl :=
1660 Make_Subprogram_Body (Loc,
1661 Specification => Spec,
1662 Declarations => Decls,
1663 Handled_Statement_Sequence =>
1664 Make_Handled_Sequence_Of_Statements (Loc,
1665 Statements => Stms));
1666 end Build_Stream_Function;
1668 ----------------------------
1669 -- Build_Stream_Procedure --
1670 ----------------------------
1672 procedure Build_Stream_Procedure
1673 (Loc : Source_Ptr;
1674 Typ : Entity_Id;
1675 Decl : out Node_Id;
1676 Pnam : Entity_Id;
1677 Stms : List_Id;
1678 Outp : Boolean)
1680 Spec : Node_Id;
1682 begin
1683 -- Construct procedure specification
1685 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1686 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1688 Spec :=
1689 Make_Procedure_Specification (Loc,
1690 Defining_Unit_Name => Pnam,
1692 Parameter_Specifications => New_List (
1693 Make_Parameter_Specification (Loc,
1694 Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
1695 Parameter_Type =>
1696 Make_Access_Definition (Loc,
1697 Null_Exclusion_Present => True,
1698 Subtype_Mark => New_Occurrence_Of (
1699 Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc))),
1701 Make_Parameter_Specification (Loc,
1702 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
1703 Out_Present => Outp,
1704 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
1706 Decl :=
1707 Make_Subprogram_Body (Loc,
1708 Specification => Spec,
1709 Declarations => Empty_List,
1710 Handled_Statement_Sequence =>
1711 Make_Handled_Sequence_Of_Statements (Loc,
1712 Statements => Stms));
1713 end Build_Stream_Procedure;
1715 -----------------------------
1716 -- Has_Stream_Standard_Rep --
1717 -----------------------------
1719 function Has_Stream_Standard_Rep (U_Type : Entity_Id) return Boolean is
1720 Siz : Uint;
1722 begin
1723 if Has_Non_Standard_Rep (U_Type) then
1724 return False;
1725 end if;
1727 if Has_Stream_Size_Clause (U_Type) then
1728 Siz := Static_Integer (Expression (Stream_Size_Clause (U_Type)));
1729 else
1730 Siz := Esize (First_Subtype (U_Type));
1731 end if;
1733 return Siz = Esize (Root_Type (U_Type));
1734 end Has_Stream_Standard_Rep;
1736 ---------------------------------
1737 -- Make_Stream_Subprogram_Name --
1738 ---------------------------------
1740 function Make_Stream_Subprogram_Name
1741 (Loc : Source_Ptr;
1742 Typ : Entity_Id;
1743 Nam : TSS_Name_Type) return Entity_Id
1745 Sname : Name_Id;
1747 begin
1748 -- For tagged types, we are dealing with a TSS associated with the
1749 -- declaration, so we use the standard primitive function name. For
1750 -- other types, generate a local TSS name since we are generating
1751 -- the subprogram at the point of use.
1753 if Is_Tagged_Type (Typ) then
1754 Sname := Make_TSS_Name (Typ, Nam);
1755 else
1756 Sname := Make_TSS_Name_Local (Typ, Nam);
1757 end if;
1759 return Make_Defining_Identifier (Loc, Sname);
1760 end Make_Stream_Subprogram_Name;
1762 ----------------------
1763 -- Stream_Base_Type --
1764 ----------------------
1766 function Stream_Base_Type (E : Entity_Id) return Entity_Id is
1767 begin
1768 if Is_Array_Type (E)
1769 and then Is_First_Subtype (E)
1770 then
1771 return E;
1772 else
1773 return Base_Type (E);
1774 end if;
1775 end Stream_Base_Type;
1777 end Exp_Strm;