OSDN Git Service

2007-08-14 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-stratt.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --             S Y S T E M . S T R E A M _ A T T R I B U T E S              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, 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 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Ada.IO_Exceptions;
35 with Ada.Streams; use Ada.Streams;
36 with Ada.Unchecked_Conversion;
37
38 package body System.Stream_Attributes is
39
40    Err : exception renames Ada.IO_Exceptions.End_Error;
41    --  Exception raised if insufficient data read (note that the RM implies
42    --  that Data_Error might be the appropriate choice, but AI95-00132
43    --  decides with a binding interpretation that End_Error is preferred).
44
45    SU : constant := System.Storage_Unit;
46
47    subtype SEA is Ada.Streams.Stream_Element_Array;
48    subtype SEO is Ada.Streams.Stream_Element_Offset;
49
50    generic function UC renames Ada.Unchecked_Conversion;
51
52    --  Subtypes used to define Stream_Element_Array values that map
53    --  into the elementary types, using unchecked conversion.
54
55    Thin_Pointer_Size : constant := System.Address'Size;
56    Fat_Pointer_Size  : constant := System.Address'Size * 2;
57
58    subtype S_AD  is SEA (1 .. (Fat_Pointer_Size              + SU - 1) / SU);
59    subtype S_AS  is SEA (1 .. (Thin_Pointer_Size             + SU - 1) / SU);
60    subtype S_B   is SEA (1 .. (Boolean'Size                  + SU - 1) / SU);
61    subtype S_C   is SEA (1 .. (Character'Size                + SU - 1) / SU);
62    subtype S_F   is SEA (1 .. (Float'Size                    + SU - 1) / SU);
63    subtype S_I   is SEA (1 .. (Integer'Size                  + SU - 1) / SU);
64    subtype S_LF  is SEA (1 .. (Long_Float'Size               + SU - 1) / SU);
65    subtype S_LI  is SEA (1 .. (Long_Integer'Size             + SU - 1) / SU);
66    subtype S_LLF is SEA (1 .. (Long_Long_Float'Size          + SU - 1) / SU);
67    subtype S_LLI is SEA (1 .. (Long_Long_Integer'Size        + SU - 1) / SU);
68    subtype S_LLU is SEA (1 .. (UST.Long_Long_Unsigned'Size   + SU - 1) / SU);
69    subtype S_LU  is SEA (1 .. (UST.Long_Unsigned'Size        + SU - 1) / SU);
70    subtype S_SF  is SEA (1 .. (Short_Float'Size              + SU - 1) / SU);
71    subtype S_SI  is SEA (1 .. (Short_Integer'Size            + SU - 1) / SU);
72    subtype S_SSI is SEA (1 .. (Short_Short_Integer'Size      + SU - 1) / SU);
73    subtype S_SSU is SEA (1 .. (UST.Short_Short_Unsigned'Size + SU - 1) / SU);
74    subtype S_SU  is SEA (1 .. (UST.Short_Unsigned'Size       + SU - 1) / SU);
75    subtype S_U   is SEA (1 .. (UST.Unsigned'Size             + SU - 1) / SU);
76    subtype S_WC  is SEA (1 .. (Wide_Character'Size           + SU - 1) / SU);
77
78    --  Unchecked conversions from the elementary type to the stream type
79
80    function From_AD  is new UC (Fat_Pointer,              S_AD);
81    function From_AS  is new UC (Thin_Pointer,             S_AS);
82    function From_F   is new UC (Float,                    S_F);
83    function From_I   is new UC (Integer,                  S_I);
84    function From_LF  is new UC (Long_Float,               S_LF);
85    function From_LI  is new UC (Long_Integer,             S_LI);
86    function From_LLF is new UC (Long_Long_Float,          S_LLF);
87    function From_LLI is new UC (Long_Long_Integer,        S_LLI);
88    function From_LLU is new UC (UST.Long_Long_Unsigned,   S_LLU);
89    function From_LU  is new UC (UST.Long_Unsigned,        S_LU);
90    function From_SF  is new UC (Short_Float,              S_SF);
91    function From_SI  is new UC (Short_Integer,            S_SI);
92    function From_SSI is new UC (Short_Short_Integer,      S_SSI);
93    function From_SSU is new UC (UST.Short_Short_Unsigned, S_SSU);
94    function From_SU  is new UC (UST.Short_Unsigned,       S_SU);
95    function From_U   is new UC (UST.Unsigned,             S_U);
96    function From_WC  is new UC (Wide_Character,           S_WC);
97
98    --  Unchecked conversions from the stream type to elementary type
99
100    function To_AD  is new UC (S_AD,  Fat_Pointer);
101    function To_AS  is new UC (S_AS,  Thin_Pointer);
102    function To_F   is new UC (S_F,   Float);
103    function To_I   is new UC (S_I,   Integer);
104    function To_LF  is new UC (S_LF,  Long_Float);
105    function To_LI  is new UC (S_LI,  Long_Integer);
106    function To_LLF is new UC (S_LLF, Long_Long_Float);
107    function To_LLI is new UC (S_LLI, Long_Long_Integer);
108    function To_LLU is new UC (S_LLU, UST.Long_Long_Unsigned);
109    function To_LU  is new UC (S_LU,  UST.Long_Unsigned);
110    function To_SF  is new UC (S_SF,  Short_Float);
111    function To_SI  is new UC (S_SI,  Short_Integer);
112    function To_SSI is new UC (S_SSI, Short_Short_Integer);
113    function To_SSU is new UC (S_SSU, UST.Short_Short_Unsigned);
114    function To_SU  is new UC (S_SU,  UST.Short_Unsigned);
115    function To_U   is new UC (S_U,   UST.Unsigned);
116    function To_WC  is new UC (S_WC,  Wide_Character);
117
118    ----------
119    -- I_AD --
120    ----------
121
122    function I_AD (Stream : not null access RST) return Fat_Pointer is
123       T : S_AD;
124       L : SEO;
125
126    begin
127       Ada.Streams.Read (Stream.all, T, L);
128
129       if L < T'Last then
130          raise Err;
131       else
132          return To_AD (T);
133       end if;
134    end I_AD;
135
136    ----------
137    -- I_AS --
138    ----------
139
140    function I_AS (Stream : not null access RST) return Thin_Pointer is
141       T : S_AS;
142       L : SEO;
143
144    begin
145       Ada.Streams.Read (Stream.all, T, L);
146
147       if L < T'Last then
148          raise Err;
149       else
150          return To_AS (T);
151       end if;
152    end I_AS;
153
154    ---------
155    -- I_B --
156    ---------
157
158    function I_B (Stream : not null access RST) return Boolean is
159       T : S_B;
160       L : SEO;
161
162    begin
163       Ada.Streams.Read (Stream.all, T, L);
164
165       if L < T'Last then
166          raise Err;
167       else
168          return Boolean'Val (T (1));
169       end if;
170    end I_B;
171
172    ---------
173    -- I_C --
174    ---------
175
176    function I_C (Stream : not null access RST) return Character is
177       T : S_C;
178       L : SEO;
179
180    begin
181       Ada.Streams.Read (Stream.all, T, L);
182
183       if L < T'Last then
184          raise Err;
185       else
186          return Character'Val (T (1));
187       end if;
188    end I_C;
189
190    ---------
191    -- I_F --
192    ---------
193
194    function I_F (Stream : not null access RST) return Float is
195       T : S_F;
196       L : SEO;
197
198    begin
199       Ada.Streams.Read (Stream.all, T, L);
200
201       if L < T'Last then
202          raise Err;
203       else
204          return To_F (T);
205       end if;
206    end I_F;
207
208    ---------
209    -- I_I --
210    ---------
211
212    function I_I (Stream : not null access RST) return Integer is
213       T : S_I;
214       L : SEO;
215
216    begin
217       Ada.Streams.Read (Stream.all, T, L);
218
219       if L < T'Last then
220          raise Err;
221       else
222          return To_I (T);
223       end if;
224    end I_I;
225
226    ----------
227    -- I_LF --
228    ----------
229
230    function I_LF (Stream : not null access RST) return Long_Float is
231       T : S_LF;
232       L : SEO;
233
234    begin
235       Ada.Streams.Read (Stream.all, T, L);
236
237       if L < T'Last then
238          raise Err;
239       else
240          return To_LF (T);
241       end if;
242    end I_LF;
243
244    ----------
245    -- I_LI --
246    ----------
247
248    function I_LI (Stream : not null access RST) return Long_Integer is
249       T : S_LI;
250       L : SEO;
251
252    begin
253       Ada.Streams.Read (Stream.all, T, L);
254
255       if L < T'Last then
256          raise Err;
257       else
258          return To_LI (T);
259       end if;
260    end I_LI;
261
262    -----------
263    -- I_LLF --
264    -----------
265
266    function I_LLF (Stream : not null access RST) return Long_Long_Float is
267       T : S_LLF;
268       L : SEO;
269
270    begin
271       Ada.Streams.Read (Stream.all, T, L);
272
273       if L < T'Last then
274          raise Err;
275       else
276          return To_LLF (T);
277       end if;
278    end I_LLF;
279
280    -----------
281    -- I_LLI --
282    -----------
283
284    function I_LLI (Stream : not null access RST) return Long_Long_Integer is
285       T : S_LLI;
286       L : SEO;
287
288    begin
289       Ada.Streams.Read (Stream.all, T, L);
290
291       if L < T'Last then
292          raise Err;
293       else
294          return To_LLI (T);
295       end if;
296    end I_LLI;
297
298    -----------
299    -- I_LLU --
300    -----------
301
302    function I_LLU
303      (Stream : not null access RST) return UST.Long_Long_Unsigned
304    is
305       T : S_LLU;
306       L : SEO;
307
308    begin
309       Ada.Streams.Read (Stream.all, T, L);
310
311       if L < T'Last then
312          raise Err;
313       else
314          return To_LLU (T);
315       end if;
316    end I_LLU;
317
318    ----------
319    -- I_LU --
320    ----------
321
322    function I_LU (Stream : not null access RST) return UST.Long_Unsigned is
323       T : S_LU;
324       L : SEO;
325
326    begin
327       Ada.Streams.Read (Stream.all, T, L);
328
329       if L < T'Last then
330          raise Err;
331       else
332          return To_LU (T);
333       end if;
334    end I_LU;
335
336    ----------
337    -- I_SF --
338    ----------
339
340    function I_SF (Stream : not null access RST) return Short_Float is
341       T : S_SF;
342       L : SEO;
343
344    begin
345       Ada.Streams.Read (Stream.all, T, L);
346
347       if L < T'Last then
348          raise Err;
349       else
350          return To_SF (T);
351       end if;
352    end I_SF;
353
354    ----------
355    -- I_SI --
356    ----------
357
358    function I_SI (Stream : not null access RST) return Short_Integer is
359       T : S_SI;
360       L : SEO;
361
362    begin
363       Ada.Streams.Read (Stream.all, T, L);
364
365       if L < T'Last then
366          raise Err;
367       else
368          return To_SI (T);
369       end if;
370    end I_SI;
371
372    -----------
373    -- I_SSI --
374    -----------
375
376    function I_SSI (Stream : not null access RST) return Short_Short_Integer is
377       T : S_SSI;
378       L : SEO;
379
380    begin
381       Ada.Streams.Read (Stream.all, T, L);
382
383       if L < T'Last then
384          raise Err;
385       else
386          return To_SSI (T);
387       end if;
388    end I_SSI;
389
390    -----------
391    -- I_SSU --
392    -----------
393
394    function I_SSU
395      (Stream : not null access RST) return UST.Short_Short_Unsigned
396    is
397       T : S_SSU;
398       L : SEO;
399
400    begin
401       Ada.Streams.Read (Stream.all, T, L);
402
403       if L < T'Last then
404          raise Err;
405       else
406          return To_SSU (T);
407       end if;
408    end I_SSU;
409
410    ----------
411    -- I_SU --
412    ----------
413
414    function I_SU (Stream : not null access RST) return UST.Short_Unsigned is
415       T : S_SU;
416       L : SEO;
417
418    begin
419       Ada.Streams.Read (Stream.all, T, L);
420
421       if L < T'Last then
422          raise Err;
423       else
424          return To_SU (T);
425       end if;
426    end I_SU;
427
428    ---------
429    -- I_U --
430    ---------
431
432    function I_U (Stream : not null access RST) return UST.Unsigned is
433       T : S_U;
434       L : SEO;
435
436    begin
437       Ada.Streams.Read (Stream.all, T, L);
438
439       if L < T'Last then
440          raise Err;
441       else
442          return To_U (T);
443       end if;
444    end I_U;
445
446    ----------
447    -- I_WC --
448    ----------
449
450    function I_WC (Stream : not null access RST) return Wide_Character is
451       T : S_WC;
452       L : SEO;
453
454    begin
455       Ada.Streams.Read (Stream.all, T, L);
456
457       if L < T'Last then
458          raise Err;
459       else
460          return To_WC (T);
461       end if;
462    end I_WC;
463
464    ----------
465    -- W_AD --
466    ----------
467
468    procedure W_AD (Stream : not null access RST; Item : Fat_Pointer) is
469       T : constant S_AD := From_AD (Item);
470    begin
471       Ada.Streams.Write (Stream.all, T);
472    end W_AD;
473
474    ----------
475    -- W_AS --
476    ----------
477
478    procedure W_AS (Stream : not null access RST; Item : Thin_Pointer) is
479       T : constant S_AS := From_AS (Item);
480    begin
481       Ada.Streams.Write (Stream.all, T);
482    end W_AS;
483
484    ---------
485    -- W_B --
486    ---------
487
488    procedure W_B (Stream : not null access RST; Item : Boolean) is
489       T : S_B;
490    begin
491       T (1) := Boolean'Pos (Item);
492       Ada.Streams.Write (Stream.all, T);
493    end W_B;
494
495    ---------
496    -- W_C --
497    ---------
498
499    procedure W_C (Stream : not null access RST; Item : Character) is
500       T : S_C;
501    begin
502       T (1) := Character'Pos (Item);
503       Ada.Streams.Write (Stream.all, T);
504    end W_C;
505
506    ---------
507    -- W_F --
508    ---------
509
510    procedure W_F (Stream : not null access RST; Item : Float) is
511       T : constant S_F := From_F (Item);
512    begin
513       Ada.Streams.Write (Stream.all, T);
514    end W_F;
515
516    ---------
517    -- W_I --
518    ---------
519
520    procedure W_I (Stream : not null access RST; Item : Integer) is
521       T : constant S_I := From_I (Item);
522    begin
523       Ada.Streams.Write (Stream.all, T);
524    end W_I;
525
526    ----------
527    -- W_LF --
528    ----------
529
530    procedure W_LF (Stream : not null access RST; Item : Long_Float) is
531       T : constant S_LF := From_LF (Item);
532    begin
533       Ada.Streams.Write (Stream.all, T);
534    end W_LF;
535
536    ----------
537    -- W_LI --
538    ----------
539
540    procedure W_LI (Stream : not null access RST; Item : Long_Integer) is
541       T : constant S_LI := From_LI (Item);
542    begin
543       Ada.Streams.Write (Stream.all, T);
544    end W_LI;
545
546    -----------
547    -- W_LLF --
548    -----------
549
550    procedure W_LLF (Stream : not null access RST; Item : Long_Long_Float) is
551       T : constant S_LLF := From_LLF (Item);
552    begin
553       Ada.Streams.Write (Stream.all, T);
554    end W_LLF;
555
556    -----------
557    -- W_LLI --
558    -----------
559
560    procedure W_LLI
561      (Stream : not null access RST; Item : Long_Long_Integer)
562    is
563       T : constant S_LLI := From_LLI (Item);
564    begin
565       Ada.Streams.Write (Stream.all, T);
566    end W_LLI;
567
568    -----------
569    -- W_LLU --
570    -----------
571
572    procedure W_LLU
573      (Stream : not null access RST; Item : UST.Long_Long_Unsigned)
574    is
575       T : constant S_LLU := From_LLU (Item);
576    begin
577       Ada.Streams.Write (Stream.all, T);
578    end W_LLU;
579
580    ----------
581    -- W_LU --
582    ----------
583
584    procedure W_LU
585      (Stream : not null access RST; Item : UST.Long_Unsigned)
586    is
587       T : constant S_LU := From_LU (Item);
588    begin
589       Ada.Streams.Write (Stream.all, T);
590    end W_LU;
591
592    ----------
593    -- W_SF --
594    ----------
595
596    procedure W_SF (Stream : not null access RST; Item : Short_Float) is
597       T : constant S_SF := From_SF (Item);
598    begin
599       Ada.Streams.Write (Stream.all, T);
600    end W_SF;
601
602    ----------
603    -- W_SI --
604    ----------
605
606    procedure W_SI (Stream : not null access RST; Item : Short_Integer) is
607       T : constant S_SI := From_SI (Item);
608    begin
609       Ada.Streams.Write (Stream.all, T);
610    end W_SI;
611
612    -----------
613    -- W_SSI --
614    -----------
615
616    procedure W_SSI
617      (Stream : not null access RST; Item : Short_Short_Integer)
618    is
619       T : constant S_SSI := From_SSI (Item);
620    begin
621       Ada.Streams.Write (Stream.all, T);
622    end W_SSI;
623
624    -----------
625    -- W_SSU --
626    -----------
627
628    procedure W_SSU
629      (Stream : not null access RST; Item : UST.Short_Short_Unsigned)
630    is
631       T : constant S_SSU := From_SSU (Item);
632    begin
633       Ada.Streams.Write (Stream.all, T);
634    end W_SSU;
635
636    ----------
637    -- W_SU --
638    ----------
639
640    procedure W_SU
641      (Stream : not null access RST; Item : UST.Short_Unsigned)
642    is
643       T : constant S_SU := From_SU (Item);
644    begin
645       Ada.Streams.Write (Stream.all, T);
646    end W_SU;
647
648    ---------
649    -- W_U --
650    ---------
651
652    procedure W_U (Stream : not null access RST; Item : UST.Unsigned) is
653       T : constant S_U := From_U (Item);
654    begin
655       Ada.Streams.Write (Stream.all, T);
656    end W_U;
657
658    ----------
659    -- W_WC --
660    ----------
661
662    procedure W_WC (Stream : not null access RST; Item : Wide_Character) is
663       T : constant S_WC := From_WC (Item);
664    begin
665       Ada.Streams.Write (Stream.all, T);
666    end W_WC;
667
668 end System.Stream_Attributes;