Debian Codegraph @

gcc/ada/exp_strm.adb

raw
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-2025, 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 ------------------------------------------------------------------------------
25
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;
46
47 package body Exp_Strm is
48
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
52
53 procedure Build_Array_Read_Write_Procedure
54 (Typ : Entity_Id;
55 Decl : out Node_Id;
56 Pnam : Entity_Id;
57 Nam : Name_Id);
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.
62
63 procedure Build_Record_Read_Write_Procedure
64 (Typ : Entity_Id;
65 Decl : out Node_Id;
66 Pnam : Entity_Id;
67 Nam : Name_Id);
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.
72
73 procedure Build_Stream_Function
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.
83
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).
91
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.
102
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).
111
112 --------------------------------
113 -- Build_Array_Input_Function --
114 --------------------------------
115
116 -- The function we build looks like
117
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);
126 --
127 -- V : Typ'Base (L1 .. H1, L2 .. H2, ... Ln .. Hn)
128
129 -- begin
130 -- Typ'Read (S, V);
131 -- return V;
132 -- end typSI[_nnn]
133
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.
137
138 procedure Build_Array_Input_Function
139 (Typ : Entity_Id;
140 Decl : out Node_Id;
141 Fnam : out Entity_Id)
142 is
143 Loc : constant Source_Ptr := Sloc (Typ);
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;
153
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);
161
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)))));
173
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)))));
186
187 Append_To (Ranges,
188 Make_Range (Loc,
189 Low_Bound => Make_Identifier (Loc, Lnam),
190 High_Bound => Make_Identifier (Loc, Hnam)));
191
192 Next_Index (Indx);
193 end loop;
194
195 -- If the type is constrained, use it directly. Otherwise build a
196 -- subtype indication with the proper bounds.
197
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));
203
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;
215
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)));
223
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))));
229
230 Fnam :=
231 Make_Defining_Identifier (Loc,
232 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Input));
233
234 Build_Stream_Function (Typ, Decl, Fnam, Decls, Stms);
235 end Build_Array_Input_Function;
236
237 ----------------------------------
238 -- Build_Array_Output_Procedure --
239 ----------------------------------
240
241 procedure Build_Array_Output_Procedure
242 (Typ : Entity_Id;
243 Decl : out Node_Id;
244 Pnam : out Entity_Id)
245 is
246 Loc : constant Source_Ptr := Sloc (Typ);
247 Stms : List_Id;
248 Indx : Node_Id;
249
250 begin
251 -- Build series of statements to output bounds
252
253 Indx := First_Index (Typ);
254 Stms := New_List;
255
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))))));
269
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))))));
282
283 Next_Index (Indx);
284 end loop;
285
286 -- Append Write attribute to write array elements
287
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))));
295
296 Pnam :=
297 Make_Defining_Identifier (Loc,
298 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Output));
299
300 Build_Stream_Procedure (Typ, Decl, Pnam, Stms, Outp => False);
301 end Build_Array_Output_Procedure;
302
303 --------------------------------
304 -- Build_Array_Read_Procedure --
305 --------------------------------
306
307 procedure Build_Array_Read_Procedure
308 (Typ : Entity_Id;
309 Decl : out Node_Id;
310 Pnam : out Entity_Id)
311 is
312 Loc : constant Source_Ptr := Sloc (Typ);
313
314 begin
315 Pnam :=
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;
320
321 --------------------------------------
322 -- Build_Array_Read_Write_Procedure --
323 --------------------------------------
324
325 -- The form of the array read/write procedure is as follows:
326
327 -- procedure pnam (S : access RST, V : [out] Typ) is
328 -- begin
329 -- for L1 in V'Range (1) loop
330 -- for L2 in V'Range (2) loop
331 -- ...
332 -- for Ln in V'Range (n) loop
333 -- Component_Type'Read/Write (S, V (L1, L2, .. Ln));
334 -- end loop;
335 -- ..
336 -- end loop;
337 -- end loop
338 -- end pnam;
339
340 -- The out keyword for V is supplied in the Read case
341
342 procedure Build_Array_Read_Write_Procedure
343 (Typ : Entity_Id;
344 Decl : out Node_Id;
345 Pnam : Entity_Id;
346 Nam : Name_Id)
347 is
348 Loc : constant Source_Ptr := Sloc (Typ);
349 Ndim : constant Pos := Number_Dimensions (Typ);
350 Ctyp : constant Entity_Id := Component_Type (Typ);
351
352 Stm : Node_Id;
353 Exl : List_Id;
354 RW : Entity_Id;
355
356 begin
357 -- First build the inner attribute call
358
359 Exl := New_List;
360
361 for J in 1 .. Ndim loop
362 Append_To (Exl, Make_Identifier (Loc, New_External_Name ('L', J)));
363 end loop;
364
365 Stm :=
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)));
374
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.
380
381 if Nam = Name_Read then
382 RW := TSS (Base_Type (Ctyp), TSS_Stream_Read);
383 else
384 RW := TSS (Base_Type (Ctyp), TSS_Stream_Write);
385 end if;
386
387 if Present (RW)
388 and then not Is_Frozen (RW)
389 then
390 Set_Is_Frozen (RW);
391 end if;
392
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.
396
397 for J in 1 .. Ndim loop
398 Stm :=
399 Make_Implicit_Loop_Statement (Typ,
400 Iteration_Scheme =>
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)),
407
408 Discrete_Subtype_Definition =>
409 Make_Attribute_Reference (Loc,
410 Prefix => Make_Identifier (Loc, Name_V),
411 Attribute_Name => Name_Range,
412
413 Expressions => New_List (
414 Make_Integer_Literal (Loc, Ndim - J + 1))))),
415
416 Statements => New_List (Stm));
417
418 end loop;
419
420 Build_Stream_Procedure
421 (Typ, Decl, Pnam, New_List (Stm), Outp => Nam = Name_Read);
422 end Build_Array_Read_Write_Procedure;
423
424 ---------------------------------
425 -- Build_Array_Write_Procedure --
426 ---------------------------------
427
428 procedure Build_Array_Write_Procedure
429 (Typ : Entity_Id;
430 Decl : out Node_Id;
431 Pnam : out Entity_Id)
432 is
433 Loc : constant Source_Ptr := Sloc (Typ);
434 begin
435 Pnam :=
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;
440
441 ---------------------------------
442 -- Build_Elementary_Input_Call --
443 ---------------------------------
444
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);
454 Res : Node_Id;
455 Lib_RE : RE_Id;
456
457 begin
458 -- Check first for Boolean and Character. These are enumeration types,
459 -- but we treat them specially, since they may require special handling
460 -- in the transfer protocol. However, this special handling only applies
461 -- if they have standard representation, otherwise they are treated like
462 -- any other enumeration type.
463
464 if Rt_Type = Standard_Boolean
465 and then Has_Stream_Standard_Rep (U_Type)
466 then
467 Lib_RE := RE_I_B;
468
469 elsif Rt_Type = Standard_Character
470 and then Has_Stream_Standard_Rep (U_Type)
471 then
472 Lib_RE := RE_I_C;
473
474 elsif Rt_Type = Standard_Wide_Character
475 and then Has_Stream_Standard_Rep (U_Type)
476 then
477 Lib_RE := RE_I_WC;
478
479 elsif Rt_Type = Standard_Wide_Wide_Character
480 and then Has_Stream_Standard_Rep (U_Type)
481 then
482 Lib_RE := RE_I_WWC;
483
484 -- Floating point types
485
486 elsif Is_Floating_Point_Type (U_Type) then
487
488 -- Question: should we use P_Size or Rt_Type to distinguish between
489 -- possible floating point types? If a non-standard size or a stream
490 -- size is specified, then we should certainly use the size. But if
491 -- we have two types the same (notably Short_Float_Size = Float_Size
492 -- which is close to universally true, and Long_Long_Float_Size =
493 -- Long_Float_Size, true on most targets except the x86), then we
494 -- would really rather use the root type, so that if people want to
495 -- fiddle with System.Stream_Attributes to get inter-target portable
496 -- streams, they get the size they expect. Consider in particular the
497 -- case of a stream written on an x86, with 96-bit Long_Long_Float
498 -- being read into a non-x86 target with 64 bit Long_Long_Float. A
499 -- special version of System.Stream_Attributes can deal with this
500 -- provided the proper type is always used.
501
502 -- To deal with these two requirements we add the special checks
503 -- on equal sizes and use the root type to distinguish.
504
505 if P_Size <= Standard_Short_Float_Size
506 and then (Standard_Short_Float_Size /= Standard_Float_Size
507 or else Rt_Type = Standard_Short_Float)
508 then
509 Lib_RE := RE_I_SF;
510
511 elsif P_Size <= Standard_Float_Size then
512 Lib_RE := RE_I_F;
513
514 elsif P_Size <= Standard_Long_Float_Size
515 and then (Standard_Long_Float_Size /= Standard_Long_Long_Float_Size
516 or else Rt_Type = Standard_Long_Float)
517 then
518 Lib_RE := RE_I_LF;
519
520 else
521 Lib_RE := RE_I_LLF;
522 end if;
523
524 -- Signed integer types. Also includes signed fixed-point types and
525 -- enumeration types with a signed representation.
526
527 -- Note on signed integer types. We do not consider types as signed for
528 -- this purpose if they have no negative numbers, or if they have biased
529 -- representation. The reason is that the value in either case basically
530 -- represents an unsigned value.
531
532 -- For example, consider:
533
534 -- type W is range 0 .. 2**32 - 1;
535 -- for W'Size use 32;
536
537 -- This is a signed type, but the representation is unsigned, and may
538 -- be outside the range of a 32-bit signed integer, so this must be
539 -- treated as 32-bit unsigned.
540
541 -- Similarly, if we have
542
543 -- type W is range -1 .. +254;
544 -- for W'Size use 8;
545
546 -- then the representation is unsigned
547
548 elsif not Is_Unsigned_Type (FST)
549
550 -- The following set of tests gets repeated many times, we should
551 -- have an abstraction defined ???
552
553 and then
554 (Is_Fixed_Point_Type (U_Type)
555 or else
556 Is_Enumeration_Type (U_Type)
557 or else
558 (Is_Signed_Integer_Type (U_Type)
559 and then not Has_Biased_Representation (FST)))
560
561 then
562 if P_Size <= Standard_Short_Short_Integer_Size then
563 Lib_RE := RE_I_SSI;
564
565 elsif P_Size <= Standard_Short_Integer_Size then
566 Lib_RE := RE_I_SI;
567
568 elsif P_Size = 24 then
569 Lib_RE := RE_I_I24;
570
571 elsif P_Size <= Standard_Integer_Size then
572 Lib_RE := RE_I_I;
573
574 elsif P_Size <= Standard_Long_Integer_Size then
575 Lib_RE := RE_I_LI;
576
577 elsif P_Size <= Standard_Long_Long_Integer_Size then
578 Lib_RE := RE_I_LLI;
579
580 else
581 Lib_RE := RE_I_LLLI;
582 end if;
583
584 -- Unsigned integer types, also includes unsigned fixed-point types
585 -- and enumeration types with an unsigned representation (note that
586 -- we know they are unsigned because we already tested for signed).
587
588 -- Also includes signed integer types that are unsigned in the sense
589 -- that they do not include negative numbers. See above for details.
590
591 elsif Is_Modular_Integer_Type (U_Type)
592 or else Is_Fixed_Point_Type (U_Type)
593 or else Is_Enumeration_Type (U_Type)
594 or else Is_Signed_Integer_Type (U_Type)
595 then
596 if P_Size <= Standard_Short_Short_Integer_Size then
597 Lib_RE := RE_I_SSU;
598
599 elsif P_Size <= Standard_Short_Integer_Size then
600 Lib_RE := RE_I_SU;
601
602 elsif P_Size = 24 then
603 Lib_RE := RE_I_U24;
604
605 elsif P_Size <= Standard_Integer_Size then
606 Lib_RE := RE_I_U;
607
608 elsif P_Size <= Standard_Long_Integer_Size then
609 Lib_RE := RE_I_LU;
610
611 elsif P_Size <= Standard_Long_Long_Integer_Size then
612 Lib_RE := RE_I_LLU;
613
614 else
615 Lib_RE := RE_I_LLLU;
616 end if;
617
618 else pragma Assert (Is_Access_Type (U_Type));
619 if Present (P_Size) and then P_Size > System_Address_Size then
620 Lib_RE := RE_I_AD;
621 else
622 Lib_RE := RE_I_AS;
623 end if;
624 end if;
625
626 -- Call the function, and do an unchecked conversion of the result
627 -- to the actual type of the prefix. If the target is a discriminant,
628 -- and we are in the body of the default implementation of a 'Read
629 -- attribute, set target type to force a constraint check (13.13.2(35)).
630 -- If the type of the discriminant is currently private, add another
631 -- unchecked conversion from the full view.
632
633 if Nkind (Targ) = N_Identifier
634 and then Is_Internal_Name (Chars (Targ))
635 and then Is_TSS (Scope (Entity (Targ)), TSS_Stream_Read)
636 then
637 Res :=
638 Unchecked_Convert_To (Base_Type (U_Type),
639 Make_Function_Call (Loc,
640 Name => New_Occurrence_Of (RTE (Lib_RE), Loc),
641 Parameter_Associations => New_List (
642 Relocate_Node (Strm))));
643
644 Set_Do_Range_Check (Res);
645
646 if Base_Type (P_Type) /= Base_Type (U_Type) then
647 Res := Unchecked_Convert_To (Base_Type (P_Type), Res);
648 end if;
649
650 return Res;
651
652 else
653 Res :=
654 Make_Function_Call (Loc,
655 Name => New_Occurrence_Of (RTE (Lib_RE), Loc),
656 Parameter_Associations => New_List (
657 Relocate_Node (Strm)));
658
659 -- Now convert to the base type if we do not have a biased type. Note
660 -- that we did not do this in some older versions, and the result was
661 -- losing a required range check in the case where 'Input is being
662 -- called from 'Read.
663
664 if not Has_Biased_Representation (P_Type) then
665 return Unchecked_Convert_To (Base_Type (P_Type), Res);
666
667 -- For the biased case, the conversion to the base type loses the
668 -- biasing, so just convert to Ptype. This is not quite right, and
669 -- for example may lose a corner case CE test, but it is such a
670 -- rare case that for now we ignore it ???
671
672 else
673 return Unchecked_Convert_To (P_Type, Res);
674 end if;
675 end if;
676 end Build_Elementary_Input_Call;
677
678 ---------------------------------
679 -- Build_Elementary_Write_Call --
680 ---------------------------------
681
682 function Build_Elementary_Write_Call (N : Node_Id) return Node_Id is
683 Loc : constant Source_Ptr := Sloc (N);
684 P_Type : constant Entity_Id := Entity (Prefix (N));
685 U_Type : constant Entity_Id := Underlying_Type (P_Type);
686 Rt_Type : constant Entity_Id := Root_Type (U_Type);
687 FST : constant Entity_Id := First_Subtype (U_Type);
688 Strm : constant Node_Id := First (Expressions (N));
689 Item : constant Node_Id := Next (Strm);
690 P_Size : Uint;
691 Lib_RE : RE_Id;
692 Libent : Entity_Id;
693
694 begin
695 -- Compute the size of the stream element. This is either the size of
696 -- the first subtype or if given the size of the Stream_Size attribute.
697
698 if Has_Stream_Size_Clause (FST) then
699 P_Size := Static_Integer (Expression (Stream_Size_Clause (FST)));
700 else
701 P_Size := Esize (FST);
702 end if;
703
704 -- Find the routine to be called
705
706 -- Check for First Boolean and Character. These are enumeration types,
707 -- but we treat them specially, since they may require special handling
708 -- in the transfer protocol. However, this special handling only applies
709 -- if they have standard representation, otherwise they are treated like
710 -- any other enumeration type.
711
712 if Rt_Type = Standard_Boolean
713 and then Has_Stream_Standard_Rep (U_Type)
714 then
715 Lib_RE := RE_W_B;
716
717 elsif Rt_Type = Standard_Character
718 and then Has_Stream_Standard_Rep (U_Type)
719 then
720 Lib_RE := RE_W_C;
721
722 elsif Rt_Type = Standard_Wide_Character
723 and then Has_Stream_Standard_Rep (U_Type)
724 then
725 Lib_RE := RE_W_WC;
726
727 elsif Rt_Type = Standard_Wide_Wide_Character
728 and then Has_Stream_Standard_Rep (U_Type)
729 then
730 Lib_RE := RE_W_WWC;
731
732 -- Floating point types
733
734 elsif Is_Floating_Point_Type (U_Type) then
735
736 -- Question: should we use P_Size or Rt_Type to distinguish between
737 -- possible floating point types? If a non-standard size or a stream
738 -- size is specified, then we should certainly use the size. But if
739 -- we have two types the same (notably Short_Float_Size = Float_Size
740 -- which is close to universally true, and Long_Long_Float_Size =
741 -- Long_Float_Size, true on most targets except the x86), then we
742 -- would really rather use the root type, so that if people want to
743 -- fiddle with System.Stream_Attributes to get inter-target portable
744 -- streams, they get the size they expect. Consider in particular the
745 -- case of a stream written on an x86, with 96-bit Long_Long_Float
746 -- being read into a non-x86 target with 64 bit Long_Long_Float. A
747 -- special version of System.Stream_Attributes can deal with this
748 -- provided the proper type is always used.
749
750 -- To deal with these two requirements we add the special checks
751 -- on equal sizes and use the root type to distinguish.
752
753 if P_Size <= Standard_Short_Float_Size
754 and then (Standard_Short_Float_Size /= Standard_Float_Size
755 or else Rt_Type = Standard_Short_Float)
756 then
757 Lib_RE := RE_W_SF;
758
759 elsif P_Size <= Standard_Float_Size then
760 Lib_RE := RE_W_F;
761
762 elsif P_Size <= Standard_Long_Float_Size
763 and then (Standard_Long_Float_Size /= Standard_Long_Long_Float_Size
764 or else Rt_Type = Standard_Long_Float)
765 then
766 Lib_RE := RE_W_LF;
767
768 else
769 Lib_RE := RE_W_LLF;
770 end if;
771
772 -- Signed integer types. Also includes signed fixed-point types and
773 -- signed enumeration types share this circuitry.
774
775 -- Note on signed integer types. We do not consider types as signed for
776 -- this purpose if they have no negative numbers, or if they have biased
777 -- representation. The reason is that the value in either case basically
778 -- represents an unsigned value.
779
780 -- For example, consider:
781
782 -- type W is range 0 .. 2**32 - 1;
783 -- for W'Size use 32;
784
785 -- This is a signed type, but the representation is unsigned, and may
786 -- be outside the range of a 32-bit signed integer, so this must be
787 -- treated as 32-bit unsigned.
788
789 -- Similarly, the representation is also unsigned if we have:
790
791 -- type W is range -1 .. +254;
792 -- for W'Size use 8;
793
794 -- forcing a biased and unsigned representation
795
796 elsif not Is_Unsigned_Type (FST)
797 and then
798 (Is_Fixed_Point_Type (U_Type)
799 or else
800 Is_Enumeration_Type (U_Type)
801 or else
802 (Is_Signed_Integer_Type (U_Type)
803 and then not Has_Biased_Representation (FST)))
804 then
805 if P_Size <= Standard_Short_Short_Integer_Size then
806 Lib_RE := RE_W_SSI;
807
808 elsif P_Size <= Standard_Short_Integer_Size then
809 Lib_RE := RE_W_SI;
810
811 elsif P_Size = 24 then
812 Lib_RE := RE_W_I24;
813
814 elsif P_Size <= Standard_Integer_Size then
815 Lib_RE := RE_W_I;
816
817 elsif P_Size <= Standard_Long_Integer_Size then
818 Lib_RE := RE_W_LI;
819
820 elsif P_Size <= Standard_Long_Long_Integer_Size then
821 Lib_RE := RE_W_LLI;
822
823 else
824 Lib_RE := RE_W_LLLI;
825 end if;
826
827 -- Unsigned integer types, also includes unsigned fixed-point types
828 -- and unsigned enumeration types (note we know they are unsigned
829 -- because we already tested for signed above).
830
831 -- Also includes signed integer types that are unsigned in the sense
832 -- that they do not include negative numbers. See above for details.
833
834 elsif Is_Modular_Integer_Type (U_Type)
835 or else Is_Fixed_Point_Type (U_Type)
836 or else Is_Enumeration_Type (U_Type)
837 or else Is_Signed_Integer_Type (U_Type)
838 then
839 if P_Size <= Standard_Short_Short_Integer_Size then
840 Lib_RE := RE_W_SSU;
841
842 elsif P_Size <= Standard_Short_Integer_Size then
843 Lib_RE := RE_W_SU;
844
845 elsif P_Size = 24 then
846 Lib_RE := RE_W_U24;
847
848 elsif P_Size <= Standard_Integer_Size then
849 Lib_RE := RE_W_U;
850
851 elsif P_Size <= Standard_Long_Integer_Size then
852 Lib_RE := RE_W_LU;
853
854 elsif P_Size <= Standard_Long_Long_Integer_Size then
855 Lib_RE := RE_W_LLU;
856
857 else
858 Lib_RE := RE_W_LLLU;
859 end if;
860
861 else pragma Assert (Is_Access_Type (U_Type));
862
863 if Present (P_Size) and then P_Size > System_Address_Size then
864 Lib_RE := RE_W_AD;
865 else
866 Lib_RE := RE_W_AS;
867 end if;
868 end if;
869
870 -- Unchecked-convert parameter to the required type (i.e. the type of
871 -- the corresponding parameter, and call the appropriate routine.
872
873 Libent := RTE (Lib_RE);
874
875 return
876 Make_Procedure_Call_Statement (Loc,
877 Name => New_Occurrence_Of (Libent, Loc),
878 Parameter_Associations => New_List (
879 Relocate_Node (Strm),
880 Unchecked_Convert_To (Etype (Next_Formal (First_Formal (Libent))),
881 Relocate_Node (Item))));
882 end Build_Elementary_Write_Call;
883
884 -----------------------------------------
885 -- Build_Mutable_Record_Read_Procedure --
886 -----------------------------------------
887
888 procedure Build_Mutable_Record_Read_Procedure
889 (Typ : Entity_Id;
890 Decl : out Node_Id;
891 Pnam : out Entity_Id)
892 is
893 Loc : constant Source_Ptr := Sloc (Typ);
894
895 Out_Formal : Node_Id;
896 -- Expression denoting the out formal parameter
897
898 Dcls : constant List_Id := New_List;
899 -- Declarations for the 'Read body
900
901 Stms : constant List_Id := New_List;
902 -- Statements for the 'Read body
903
904 Disc : Entity_Id;
905 -- Entity of the discriminant being processed
906
907 Tmp_For_Disc : Entity_Id;
908 -- Temporary object used to read the value of Disc
909
910 Tmps_For_Discs : constant List_Id := New_List;
911 -- List of object declarations for temporaries holding the read values
912 -- for the discriminants.
913
914 Cstr : constant List_Id := New_List;
915 -- List of constraints to be applied on temporary record
916
917 Discriminant_Checks : constant List_Id := New_List;
918 -- List of discriminant checks to be performed if the actual object
919 -- is constrained.
920
921 Tmp : constant Entity_Id := Make_Defining_Identifier (Loc, Name_V);
922 -- Temporary record must hide formal (assignments to components of the
923 -- record are always generated with V as the identifier for the record).
924
925 Constrained_Stms : List_Id := New_List;
926 -- Statements within the block where we have the constrained temporary
927
928 begin
929 -- A mutable type cannot be a tagged type, so we generate a new name
930 -- for the stream procedure.
931
932 Pnam :=
933 Make_Defining_Identifier (Loc,
934 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Read));
935
936 if Is_Unchecked_Union (Typ) then
937
938 -- If this is an unchecked union, the stream procedure is erroneous,
939 -- because there are no discriminants to read.
940
941 -- This should generate a warning ???
942
943 Append_To (Stms,
944 Make_Raise_Program_Error (Loc,
945 Reason => PE_Unchecked_Union_Restriction));
946
947 Build_Stream_Procedure (Typ, Decl, Pnam, Stms, Outp => True);
948 return;
949 end if;
950
951 Disc := First_Discriminant (Typ);
952
953 Out_Formal :=
954 Make_Selected_Component (Loc,
955 Prefix => New_Occurrence_Of (Pnam, Loc),
956 Selector_Name => Make_Identifier (Loc, Name_V));
957
958 -- Generate Reads for the discriminants of the type. The discriminants
959 -- need to be read before the rest of the components, so that variants
960 -- are initialized correctly. The discriminants must be read into temp
961 -- variables so an incomplete Read (interrupted by an exception, for
962 -- example) does not alter the passed object.
963
964 while Present (Disc) loop
965 Tmp_For_Disc := Make_Defining_Identifier (Loc,
966 New_External_Name (Chars (Disc), "D"));
967
968 Append_To (Tmps_For_Discs,
969 Make_Object_Declaration (Loc,
970 Defining_Identifier => Tmp_For_Disc,
971 Object_Definition => New_Occurrence_Of (Etype (Disc), Loc)));
972 Set_No_Initialization (Last (Tmps_For_Discs));
973
974 Append_To (Stms,
975 Make_Attribute_Reference (Loc,
976 Prefix => New_Occurrence_Of (Etype (Disc), Loc),
977 Attribute_Name => Name_Read,
978 Expressions => New_List (
979 Make_Identifier (Loc, Name_S),
980 New_Occurrence_Of (Tmp_For_Disc, Loc))));
981
982 Append_To (Cstr,
983 Make_Discriminant_Association (Loc,
984 Selector_Names => New_List (New_Occurrence_Of (Disc, Loc)),
985 Expression => New_Occurrence_Of (Tmp_For_Disc, Loc)));
986
987 Append_To (Discriminant_Checks,
988 Make_Raise_Constraint_Error (Loc,
989 Condition =>
990 Make_Op_Ne (Loc,
991 Left_Opnd => New_Occurrence_Of (Tmp_For_Disc, Loc),
992 Right_Opnd =>
993 Make_Selected_Component (Loc,
994 Prefix => New_Copy_Tree (Out_Formal),
995 Selector_Name => New_Occurrence_Of (Disc, Loc))),
996 Reason => CE_Discriminant_Check_Failed));
997 Next_Discriminant (Disc);
998 end loop;
999
1000 -- Generate reads for the components of the record (including those
1001 -- that depend on discriminants).
1002
1003 Build_Record_Read_Write_Procedure (Typ, Decl, Pnam, Name_Read);
1004
1005 -- Save original statement sequence for component assignments, and
1006 -- replace it with Stms.
1007
1008 Constrained_Stms := Statements (Handled_Statement_Sequence (Decl));
1009 Set_Handled_Statement_Sequence (Decl,
1010 Make_Handled_Sequence_Of_Statements (Loc,
1011 Statements => Stms));
1012
1013 -- If Typ has controlled components (i.e. if it is classwide or
1014 -- Has_Controlled), or components constrained using the discriminants
1015 -- of Typ, then we need to ensure that all component assignments are
1016 -- performed on an object that has been appropriately constrained
1017 -- prior to being initialized. To this effect, we wrap the component
1018 -- assignments in a block where V is a constrained temporary.
1019
1020 Append_To (Dcls,
1021 Make_Object_Declaration (Loc,
1022 Defining_Identifier => Tmp,
1023 Object_Definition =>
1024 Make_Subtype_Indication (Loc,
1025 Subtype_Mark => New_Occurrence_Of (Base_Type (Typ), Loc),
1026 Constraint =>
1027 Make_Index_Or_Discriminant_Constraint (Loc,
1028 Constraints => Cstr))));
1029
1030 -- AI05-023-1: Insert discriminant check prior to initialization of the
1031 -- constrained temporary.
1032
1033 Append_To (Stms,
1034 Make_Implicit_If_Statement (Pnam,
1035 Condition =>
1036 Make_Attribute_Reference (Loc,
1037 Prefix => New_Copy_Tree (Out_Formal),
1038 Attribute_Name => Name_Constrained),
1039 Then_Statements => Discriminant_Checks));
1040
1041 -- Now insert back original component assignments, wrapped in a block
1042 -- in which V is the constrained temporary.
1043
1044 Append_To (Stms,
1045 Make_Block_Statement (Loc,
1046 Declarations => Dcls,
1047 Handled_Statement_Sequence => Parent (Constrained_Stms)));
1048
1049 Append_To (Constrained_Stms,
1050 Make_Assignment_Statement (Loc,
1051 Name => Out_Formal,
1052 Expression => Make_Identifier (Loc, Name_V)));
1053
1054 Set_Declarations (Decl, Tmps_For_Discs);
1055 end Build_Mutable_Record_Read_Procedure;
1056
1057 ------------------------------------------
1058 -- Build_Mutable_Record_Write_Procedure --
1059 ------------------------------------------
1060
1061 procedure Build_Mutable_Record_Write_Procedure
1062 (Typ : Entity_Id;
1063 Decl : out Node_Id;
1064 Pnam : out Entity_Id)
1065 is
1066 Loc : constant Source_Ptr := Sloc (Typ);
1067 Stms : List_Id;
1068 Disc : Entity_Id;
1069 D_Ref : Node_Id;
1070
1071 begin
1072 Stms := New_List;
1073 Disc := First_Discriminant (Typ);
1074
1075 -- Generate Writes for the discriminants of the type
1076 -- If the type is an unchecked union, use the default values of
1077 -- the discriminants, because they are not stored.
1078
1079 while Present (Disc) loop
1080 if Is_Unchecked_Union (Typ) then
1081 D_Ref :=
1082 New_Copy_Tree (Discriminant_Default_Value (Disc));
1083 else
1084 D_Ref :=
1085 Make_Selected_Component (Loc,
1086 Prefix => Make_Identifier (Loc, Name_V),
1087 Selector_Name => New_Occurrence_Of (Disc, Loc));
1088 end if;
1089
1090 Append_To (Stms,
1091 Make_Attribute_Reference (Loc,
1092 Prefix => New_Occurrence_Of (Etype (Disc), Loc),
1093 Attribute_Name => Name_Write,
1094 Expressions => New_List (
1095 Make_Identifier (Loc, Name_S),
1096 D_Ref)));
1097
1098 Next_Discriminant (Disc);
1099 end loop;
1100
1101 -- A mutable type cannot be a tagged type, so we generate a new name
1102 -- for the stream procedure.
1103
1104 Pnam :=
1105 Make_Defining_Identifier (Loc,
1106 Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Write));
1107 Build_Record_Read_Write_Procedure (Typ, Decl, Pnam, Name_Write);
1108
1109 -- Write the discriminants before the rest of the components, so
1110 -- that discriminant values are properly set of variants, etc.
1111
1112 if Is_Non_Empty_List (
1113 Statements (Handled_Statement_Sequence (Decl)))
1114 then
1115 Insert_List_Before
1116 (First (Statements (Handled_Statement_Sequence (Decl))), Stms);
1117 else
1118 Set_Statements (Handled_Statement_Sequence (Decl), Stms);
1119 end if;
1120 end Build_Mutable_Record_Write_Procedure;
1121
1122 -----------------------------------------------
1123 -- Build_Record_Or_Elementary_Input_Function --
1124 -----------------------------------------------
1125
1126 -- The function we build looks like
1127
1128 -- function InputN (S : access RST) return Typ is
1129 -- C1 : constant Disc_Type_1;
1130 -- Discr_Type_1'Read (S, C1);
1131 -- C2 : constant Disc_Type_2;
1132 -- Discr_Type_2'Read (S, C2);
1133 -- ...
1134 -- Cn : constant Disc_Type_n;
1135 -- Discr_Type_n'Read (S, Cn);
1136 -- V : Typ (C1, C2, .. Cn)
1137
1138 -- begin
1139 -- Typ'Read (S, V);
1140 -- return V;
1141 -- end InputN
1142
1143 -- The discriminants are of course only present in the case of a record
1144 -- with discriminants. In the case of a record with no discriminants, or
1145 -- an elementary type, then no Cn constants are defined.
1146
1147 procedure Build_Record_Or_Elementary_Input_Function
1148 (Typ : Entity_Id;
1149 Decl : out Node_Id;
1150 Fnam : out Entity_Id)
1151 is
1152 Loc : constant Source_Ptr := Sloc (Typ);
1153 B_Typ : constant Entity_Id := Underlying_Type (Base_Type (Typ));
1154 Cn : Name_Id;
1155 Constr : List_Id;
1156 Decls : List_Id;
1157 Discr : Entity_Id;
1158 Discr_Elmt : Elmt_Id := No_Elmt;
1159 J : Pos;
1160 Obj_Decl : Node_Id;
1161 Odef : Node_Id;
1162 Stms : List_Id;
1163
1164 begin
1165 Decls := New_List;
1166 Constr := New_List;
1167
1168 J := 1;
1169
1170 -- In the presence of multiple instantiations (as in uses of the Booch
1171 -- components) the base type may be private, and the underlying type
1172 -- already constrained, in which case there's no discriminant constraint
1173 -- to construct.
1174
1175 if Has_Discriminants (Typ)
1176 and then No (Discriminant_Default_Value (First_Discriminant (Typ)))
1177 and then not Is_Constrained (Underlying_Type (B_Typ))
1178 then
1179 Discr := First_Discriminant (B_Typ);
1180
1181 -- If the prefix subtype is constrained, then retrieve the first
1182 -- element of its constraint.
1183
1184 if Is_Constrained (Typ) then
1185 Discr_Elmt := First_Elmt (Discriminant_Constraint (Typ));
1186 end if;
1187
1188 while Present (Discr) loop
1189 Cn := New_External_Name ('C', J);
1190
1191 Decl :=
1192 Make_Object_Declaration (Loc,
1193 Defining_Identifier => Make_Defining_Identifier (Loc, Cn),
1194 Object_Definition =>
1195 New_Occurrence_Of (Etype (Discr), Loc));
1196
1197 -- If this is an access discriminant, do not perform default
1198 -- initialization. The discriminant is about to get its value
1199 -- from Read, and if the type is null excluding we do not want
1200 -- spurious warnings on an initial null value.
1201
1202 if Is_Access_Type (Etype (Discr)) then
1203 Set_No_Initialization (Decl);
1204 end if;
1205
1206 Append_To (Decls, Decl);
1207 Append_To (Decls,
1208 Make_Attribute_Reference (Loc,
1209 Prefix => New_Occurrence_Of (Etype (Discr), Loc),
1210 Attribute_Name => Name_Read,
1211 Expressions => New_List (
1212 Make_Identifier (Loc, Name_S),
1213 Make_Identifier (Loc, Cn))));
1214
1215 Append_To (Constr, Make_Identifier (Loc, Cn));
1216
1217 -- If the prefix subtype imposes a discriminant constraint, then
1218 -- check that each discriminant value equals the value read.
1219
1220 if Present (Discr_Elmt) then
1221 Append_To (Decls,
1222 Make_Raise_Constraint_Error (Loc,
1223 Condition => Make_Op_Ne (Loc,
1224 Left_Opnd =>
1225 New_Occurrence_Of
1226 (Defining_Identifier (Decl), Loc),
1227 Right_Opnd =>
1228 New_Copy_Tree (Node (Discr_Elmt))),
1229 Reason => CE_Discriminant_Check_Failed));
1230
1231 Next_Elmt (Discr_Elmt);
1232 end if;
1233
1234 Next_Discriminant (Discr);
1235 J := J + 1;
1236 end loop;
1237
1238 Odef :=
1239 Make_Subtype_Indication (Loc,
1240 Subtype_Mark => New_Occurrence_Of (B_Typ, Loc),
1241 Constraint =>
1242 Make_Index_Or_Discriminant_Constraint (Loc,
1243 Constraints => Constr));
1244
1245 -- If no discriminants, then just use the type with no constraint
1246
1247 else
1248 Odef := New_Occurrence_Of (B_Typ, Loc);
1249 end if;
1250
1251 -- Create an extended return statement encapsulating the result object
1252 -- and 'Read call, which is needed in general for proper handling of
1253 -- build-in-place results (such as when the result type is inherently
1254 -- limited).
1255
1256 Obj_Decl :=
1257 Make_Object_Declaration (Loc,
1258 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
1259 Object_Definition => Odef);
1260
1261 -- If the type is an access type, do not perform default initialization.
1262 -- The object is about to get its value from Read, and if the type is
1263 -- null excluding we do not want spurious warnings on an initial null.
1264
1265 if Is_Access_Type (B_Typ) then
1266 Set_No_Initialization (Obj_Decl);
1267 end if;
1268
1269 Stms := New_List (
1270 Make_Extended_Return_Statement (Loc,
1271 Return_Object_Declarations => New_List (Obj_Decl),
1272 Handled_Statement_Sequence =>
1273 Make_Handled_Sequence_Of_Statements (Loc,
1274 Statements => New_List (
1275 Make_Attribute_Reference (Loc,
1276 Prefix => New_Occurrence_Of (B_Typ, Loc),
1277 Attribute_Name => Name_Read,
1278 Expressions => New_List (
1279 Make_Identifier (Loc, Name_S),
1280 Make_Identifier (Loc, Name_V)))))));
1281
1282 Fnam := Make_Stream_Subprogram_Name (Loc, B_Typ, TSS_Stream_Input);
1283
1284 Build_Stream_Function (B_Typ, Decl, Fnam, Decls, Stms);
1285 end Build_Record_Or_Elementary_Input_Function;
1286
1287 -------------------------------------------------
1288 -- Build_Record_Or_Elementary_Output_Procedure --
1289 -------------------------------------------------
1290
1291 procedure Build_Record_Or_Elementary_Output_Procedure
1292 (Typ : Entity_Id;
1293 Decl : out Node_Id;
1294 Pnam : out Entity_Id)
1295 is
1296 Loc : constant Source_Ptr := Sloc (Typ);
1297 Stms : List_Id;
1298 Disc : Entity_Id;
1299 Disc_Ref : Node_Id;
1300
1301 begin
1302 Stms := New_List;
1303
1304 -- Note that of course there will be no discriminants for the elementary
1305 -- type case, so Has_Discriminants will be False. Note that the language
1306 -- rules do not allow writing the discriminants in the defaulted case,
1307 -- because those are written by 'Write.
1308
1309 if Has_Discriminants (Typ)
1310 and then No (Discriminant_Default_Value (First_Discriminant (Typ)))
1311 then
1312 Disc := First_Discriminant (Typ);
1313 while Present (Disc) loop
1314
1315 -- If the type is an unchecked union, it must have default
1316 -- discriminants (this is checked earlier), and those defaults
1317 -- are written out to the stream.
1318
1319 if Is_Unchecked_Union (Typ) then
1320 Disc_Ref := New_Copy_Tree (Discriminant_Default_Value (Disc));
1321
1322 else
1323 Disc_Ref :=
1324 Make_Selected_Component (Loc,
1325 Prefix => Make_Identifier (Loc, Name_V),
1326 Selector_Name => New_Occurrence_Of (Disc, Loc));
1327 end if;
1328
1329 Append_To (Stms,
1330 Make_Attribute_Reference (Loc,
1331 Prefix =>
1332 New_Occurrence_Of (Stream_Base_Type (Etype (Disc)), Loc),
1333 Attribute_Name => Name_Write,
1334 Expressions => New_List (
1335 Make_Identifier (Loc, Name_S),
1336 Disc_Ref)));
1337
1338 Next_Discriminant (Disc);
1339 end loop;
1340 end if;
1341
1342 Append_To (Stms,
1343 Make_Attribute_Reference (Loc,
1344 Prefix => New_Occurrence_Of (Typ, Loc),
1345 Attribute_Name => Name_Write,
1346 Expressions => New_List (
1347 Make_Identifier (Loc, Name_S),
1348 Make_Identifier (Loc, Name_V))));
1349
1350 Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Output);
1351
1352 Build_Stream_Procedure (Typ, Decl, Pnam, Stms, Outp => False);
1353 end Build_Record_Or_Elementary_Output_Procedure;
1354
1355 ---------------------------------
1356 -- Build_Record_Read_Procedure --
1357 ---------------------------------
1358
1359 procedure Build_Record_Read_Procedure
1360 (Typ : Entity_Id;
1361 Decl : out Node_Id;
1362 Pnam : out Entity_Id)
1363 is
1364 Loc : constant Source_Ptr := Sloc (Typ);
1365 begin
1366 Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Read);
1367 Build_Record_Read_Write_Procedure (Typ, Decl, Pnam, Name_Read);
1368 end Build_Record_Read_Procedure;
1369
1370 ---------------------------------------
1371 -- Build_Record_Read_Write_Procedure --
1372 ---------------------------------------
1373
1374 -- The form of the record read/write procedure is as shown by the
1375 -- following example for a case with one discriminant case variant:
1376
1377 -- procedure pnam (S : access RST, V : [out] Typ) is
1378 -- begin
1379 -- Component_Type'Read/Write (S, V.component);
1380 -- Component_Type'Read/Write (S, V.component);
1381 -- ...
1382 -- Component_Type'Read/Write (S, V.component);
1383 --
1384 -- case V.discriminant is
1385 -- when choices =>
1386 -- Component_Type'Read/Write (S, V.component);
1387 -- Component_Type'Read/Write (S, V.component);
1388 -- ...
1389 -- Component_Type'Read/Write (S, V.component);
1390 --
1391 -- when choices =>
1392 -- Component_Type'Read/Write (S, V.component);
1393 -- Component_Type'Read/Write (S, V.component);
1394 -- ...
1395 -- Component_Type'Read/Write (S, V.component);
1396 -- ...
1397 -- end case;
1398 -- end pnam;
1399
1400 -- The out keyword for V is supplied in the Read case
1401
1402 procedure Build_Record_Read_Write_Procedure
1403 (Typ : Entity_Id;
1404 Decl : out Node_Id;
1405 Pnam : Entity_Id;
1406 Nam : Name_Id)
1407 is
1408 Loc : constant Source_Ptr := Sloc (Typ);
1409 Rdef : Node_Id;
1410 Stms : List_Id;
1411 Typt : Entity_Id;
1412
1413 In_Limited_Extension : Boolean := False;
1414 -- Set to True while processing the record extension definition
1415 -- for an extension of a limited type (for which an ancestor type
1416 -- has an explicit Nam attribute definition).
1417
1418 function Make_Component_List_Attributes (CL : Node_Id) return List_Id;
1419 -- Returns a sequence of attributes to process the components that
1420 -- are referenced in the given component list.
1421
1422 function Make_Field_Attribute (C : Entity_Id) return Node_Id;
1423 -- Given C, the entity for a discriminant or component, build
1424 -- an attribute for the corresponding field values.
1425
1426 function Make_Field_Attributes (Clist : List_Id) return List_Id;
1427 -- Given Clist, a component items list, construct series of attributes
1428 -- for fieldwise processing of the corresponding components.
1429
1430 ------------------------------------
1431 -- Make_Component_List_Attributes --
1432 ------------------------------------
1433
1434 function Make_Component_List_Attributes (CL : Node_Id) return List_Id is
1435 CI : constant List_Id := Component_Items (CL);
1436 VP : constant Node_Id := Variant_Part (CL);
1437
1438 Result : List_Id;
1439 Alts : List_Id;
1440 V : Node_Id;
1441 DC : Node_Id;
1442 DCH : List_Id;
1443 D_Ref : Node_Id;
1444
1445 begin
1446 Result := Make_Field_Attributes (CI);
1447
1448 if Present (VP) then
1449 Alts := New_List;
1450
1451 V := First_Non_Pragma (Variants (VP));
1452 while Present (V) loop
1453 DCH := New_List;
1454
1455 DC := First (Discrete_Choices (V));
1456 while Present (DC) loop
1457 Append_To (DCH, New_Copy_Tree (DC));
1458 Next (DC);
1459 end loop;
1460
1461 Append_To (Alts,
1462 Make_Case_Statement_Alternative (Loc,
1463 Discrete_Choices => DCH,
1464 Statements =>
1465 Make_Component_List_Attributes (Component_List (V))));
1466 Next_Non_Pragma (V);
1467 end loop;
1468
1469 -- Note: in the following, we make sure that we use new occurrence
1470 -- of for the selector, since there are cases in which we make a
1471 -- reference to a hidden discriminant that is not visible.
1472
1473 -- If the enclosing record is an unchecked_union, we use the
1474 -- default expressions for the discriminant (it must exist)
1475 -- because we cannot generate a reference to it, given that
1476 -- it is not stored.
1477
1478 if Is_Unchecked_Union (Scope (Entity (Name (VP)))) then
1479 D_Ref :=
1480 New_Copy_Tree
1481 (Discriminant_Default_Value (Entity (Name (VP))));
1482 else
1483 D_Ref :=
1484 Make_Selected_Component (Loc,
1485 Prefix => Make_Identifier (Loc, Name_V),
1486 Selector_Name =>
1487 New_Occurrence_Of (Entity (Name (VP)), Loc));
1488 end if;
1489
1490 Append_To (Result,
1491 Make_Case_Statement (Loc,
1492 Expression => D_Ref,
1493 Alternatives => Alts));
1494 end if;
1495
1496 return Result;
1497 end Make_Component_List_Attributes;
1498
1499 --------------------------
1500 -- Make_Field_Attribute --
1501 --------------------------
1502
1503 function Make_Field_Attribute (C : Entity_Id) return Node_Id is
1504 Field_Typ : constant Entity_Id := Stream_Base_Type (Etype (C));
1505
1506 TSS_Names : constant array (Name_Input .. Name_Write) of
1507 TSS_Name_Type :=
1508 (Name_Read => TSS_Stream_Read,
1509 Name_Write => TSS_Stream_Write,
1510 Name_Input => TSS_Stream_Input,
1511 Name_Output => TSS_Stream_Output,
1512 others => TSS_Null);
1513 pragma Assert (TSS_Names (Nam) /= TSS_Null);
1514
1515 begin
1516 if In_Limited_Extension
1517 and then Is_Limited_Type (Field_Typ)
1518 and then No (Find_Inherited_TSS (Field_Typ, TSS_Names (Nam)))
1519 then
1520 -- The declaration is illegal per 13.13.2(9/1), and this is
1521 -- enforced in Exp_Ch3.Check_Stream_Attributes. Keep the caller
1522 -- happy by returning a null statement.
1523
1524 return Make_Null_Statement (Loc);
1525 end if;
1526
1527 return
1528 Make_Attribute_Reference (Loc,
1529 Prefix => New_Occurrence_Of (Field_Typ, Loc),
1530 Attribute_Name => Nam,
1531 Expressions => New_List (
1532 Make_Identifier (Loc, Name_S),
1533 Make_Selected_Component (Loc,
1534 Prefix => Make_Identifier (Loc, Name_V),
1535 Selector_Name => New_Occurrence_Of (C, Loc))));
1536 end Make_Field_Attribute;
1537
1538 ---------------------------
1539 -- Make_Field_Attributes --
1540 ---------------------------
1541
1542 function Make_Field_Attributes (Clist : List_Id) return List_Id is
1543 Item : Node_Id;
1544 Result : constant List_Id := New_List;
1545
1546 begin
1547 -- Loop through components, skipping all internal components, which
1548 -- are not part of the value (e.g. _Tag), except that we don't skip
1549 -- the _Parent, since we do want to process that recursively. If
1550 -- _Parent is an interface type, being abstract with no components
1551 -- there is no need to handle it.
1552
1553 Item := First (Clist);
1554 while Present (Item) loop
1555 if Nkind (Item) = N_Component_Declaration
1556 and then
1557 ((Chars (Defining_Identifier (Item)) = Name_uParent
1558 and then not Is_Interface
1559 (Etype (Defining_Identifier (Item))))
1560 or else
1561 not Is_Internal_Name (Chars (Defining_Identifier (Item))))
1562 then
1563 Append_To
1564 (Result,
1565 Make_Field_Attribute (Defining_Identifier (Item)));
1566 end if;
1567
1568 Next (Item);
1569 end loop;
1570
1571 return Result;
1572 end Make_Field_Attributes;
1573
1574 -- Start of processing for Build_Record_Read_Write_Procedure
1575
1576 begin
1577 -- For the protected type case, use corresponding record
1578
1579 if Is_Protected_Type (Typ) then
1580 Typt := Corresponding_Record_Type (Typ);
1581 else
1582 Typt := Typ;
1583 end if;
1584
1585 -- Note that we do nothing with the discriminants, since Read and
1586 -- Write do not read or write the discriminant values. All handling
1587 -- of discriminants occurs in the Input and Output subprograms.
1588
1589 Rdef := Type_Definition
1590 (Declaration_Node (Base_Type (Underlying_Type (Typt))));
1591 Stms := Empty_List;
1592
1593 -- In record extension case, the fields we want, including the _Parent
1594 -- field representing the parent type, are to be found in the extension.
1595 -- Note that we will naturally process the _Parent field using the type
1596 -- of the parent, and hence its stream attributes, which is appropriate.
1597
1598 if Nkind (Rdef) = N_Derived_Type_Definition then
1599 Rdef := Record_Extension_Part (Rdef);
1600
1601 if Is_Limited_Type (Typt) then
1602 In_Limited_Extension := True;
1603 end if;
1604 end if;
1605
1606 if Present (Component_List (Rdef)) then
1607 Append_List_To (Stms,
1608 Make_Component_List_Attributes (Component_List (Rdef)));
1609 end if;
1610
1611 Build_Stream_Procedure
1612 (Typ, Decl, Pnam, Stms, Outp => Nam = Name_Read);
1613 end Build_Record_Read_Write_Procedure;
1614
1615 ----------------------------------
1616 -- Build_Record_Write_Procedure --
1617 ----------------------------------
1618
1619 procedure Build_Record_Write_Procedure
1620 (Typ : Entity_Id;
1621 Decl : out Node_Id;
1622 Pnam : out Entity_Id)
1623 is
1624 Loc : constant Source_Ptr := Sloc (Typ);
1625 begin
1626 Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Write);
1627 Build_Record_Read_Write_Procedure (Typ, Decl, Pnam, Name_Write);
1628 end Build_Record_Write_Procedure;
1629
1630 -------------------------------
1631 -- Build_Stream_Attr_Profile --
1632 -------------------------------
1633
1634 function Build_Stream_Attr_Profile
1635 (Loc : Source_Ptr;
1636 Typ : Entity_Id;
1637 Nam : TSS_Name_Type) return List_Id
1638 is
1639 Profile : List_Id;
1640
1641 begin
1642 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1643 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1644
1645 Profile := New_List (
1646 Make_Parameter_Specification (Loc,
1647 Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
1648 Parameter_Type =>
1649 Make_Access_Definition (Loc,
1650 Null_Exclusion_Present => True,
1651 Subtype_Mark => New_Occurrence_Of (
1652 Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc))));
1653
1654 if Nam /= TSS_Stream_Input then
1655 Append_To (Profile,
1656 Make_Parameter_Specification (Loc,
1657 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
1658 Out_Present => (Nam = TSS_Stream_Read),
1659 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
1660 end if;
1661
1662 return Profile;
1663 end Build_Stream_Attr_Profile;
1664
1665 ---------------------------
1666 -- Build_Stream_Function --
1667 ---------------------------
1668
1669 procedure Build_Stream_Function
1670 (Typ : Entity_Id;
1671 Decl : out Node_Id;
1672 Fnam : Entity_Id;
1673 Decls : List_Id;
1674 Stms : List_Id)
1675 is
1676 Loc : constant Source_Ptr := Sloc (Typ);
1677 Spec : Node_Id;
1678
1679 begin
1680 -- Construct function specification
1681
1682 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1683 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1684
1685 Spec :=
1686 Make_Function_Specification (Loc,
1687 Defining_Unit_Name => Fnam,
1688
1689 Parameter_Specifications => New_List (
1690 Make_Parameter_Specification (Loc,
1691 Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
1692 Parameter_Type =>
1693 Make_Access_Definition (Loc,
1694 Null_Exclusion_Present => True,
1695 Subtype_Mark =>
1696 New_Occurrence_Of
1697 (Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc)))),
1698
1699 Result_Definition => New_Occurrence_Of (Typ, Loc));
1700
1701 Decl :=
1702 Make_Subprogram_Body (Loc,
1703 Specification => Spec,
1704 Declarations => Decls,
1705 Handled_Statement_Sequence =>
1706 Make_Handled_Sequence_Of_Statements (Loc,
1707 Statements => Stms));
1708 end Build_Stream_Function;
1709
1710 ----------------------------
1711 -- Build_Stream_Procedure --
1712 ----------------------------
1713
1714 procedure Build_Stream_Procedure
1715 (Typ : Entity_Id;
1716 Decl : out Node_Id;
1717 Pnam : Entity_Id;
1718 Stms : List_Id;
1719 Outp : Boolean)
1720 is
1721 Loc : constant Source_Ptr := Sloc (Typ);
1722 Spec : Node_Id;
1723
1724 begin
1725 -- Construct procedure specification
1726
1727 -- (Ada 2005: AI-441): Set the null-excluding attribute because it has
1728 -- no semantic meaning in Ada 95 but it is a requirement in Ada 2005.
1729
1730 Spec :=
1731 Make_Procedure_Specification (Loc,
1732 Defining_Unit_Name => Pnam,
1733
1734 Parameter_Specifications => New_List (
1735 Make_Parameter_Specification (Loc,
1736 Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
1737 Parameter_Type =>
1738 Make_Access_Definition (Loc,
1739 Null_Exclusion_Present => True,
1740 Subtype_Mark =>
1741 New_Occurrence_Of
1742 (Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc))),
1743
1744 Make_Parameter_Specification (Loc,
1745 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
1746 Out_Present => Outp,
1747 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
1748
1749 Decl :=
1750 Make_Subprogram_Body (Loc,
1751 Specification => Spec,
1752 Declarations => Empty_List,
1753 Handled_Statement_Sequence =>
1754 Make_Handled_Sequence_Of_Statements (Loc,
1755 Statements => Stms));
1756 end Build_Stream_Procedure;
1757
1758 -----------------------------
1759 -- Has_Stream_Standard_Rep --
1760 -----------------------------
1761
1762 function Has_Stream_Standard_Rep (U_Type : Entity_Id) return Boolean is
1763 Siz : Uint;
1764
1765 begin
1766 if Has_Non_Standard_Rep (U_Type) then
1767 return False;
1768 end if;
1769
1770 if Has_Stream_Size_Clause (U_Type) then
1771 Siz := Static_Integer (Expression (Stream_Size_Clause (U_Type)));
1772 else
1773 Siz := Esize (First_Subtype (U_Type));
1774 end if;
1775
1776 return Siz = Esize (Root_Type (U_Type));
1777 end Has_Stream_Standard_Rep;
1778
1779 ---------------------------------
1780 -- Make_Stream_Subprogram_Name --
1781 ---------------------------------
1782
1783 function Make_Stream_Subprogram_Name
1784 (Loc : Source_Ptr;
1785 Typ : Entity_Id;
1786 Nam : TSS_Name_Type) return Entity_Id
1787 is
1788 Sname : Name_Id;
1789
1790 begin
1791 -- For tagged types, we are dealing with a TSS associated with the
1792 -- declaration, so we use the standard primitive function name. For
1793 -- other types, generate a local TSS name since we are generating
1794 -- the subprogram at the point of use.
1795
1796 if Is_Tagged_Type (Typ) then
1797 Sname := Make_TSS_Name (Typ, Nam);
1798 else
1799 Sname := Make_TSS_Name_Local (Typ, Nam);
1800 end if;
1801
1802 return Make_Defining_Identifier (Loc, Sname);
1803 end Make_Stream_Subprogram_Name;
1804
1805 ----------------------
1806 -- Stream_Base_Type --
1807 ----------------------
1808
1809 function Stream_Base_Type (E : Entity_Id) return Entity_Id is
1810 begin
1811 if Is_Array_Type (E)
1812 and then Is_First_Subtype (E)
1813 then
1814 return E;
1815 else
1816 return Base_Type (E);
1817 end if;
1818 end Stream_Base_Type;
1819
1820 end Exp_Strm;