aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/libgnat/s-putima.adb
blob: a3fd415c4aa56770ffb881a5cc5bb6a409ca2d73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT RUN-TIME COMPONENTS                         --
--                                                                          --
--                             SYSTEM.PUT_IMAGES                            --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--            Copyright (C) 2020-2024, Free Software Foundation, Inc.       --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Strings.Text_Buffers.Utils;
use Ada.Strings.Text_Buffers;
use Ada.Strings.Text_Buffers.Utils;
with System.Storage_Elements; use System.Storage_Elements;

package body System.Put_Images is

   generic
      type Integer_Type is range <>;
      type Unsigned_Type is mod <>;
      Base : Unsigned_Type;
   package Generic_Integer_Images is
      pragma Assert (Integer_Type'Size = Unsigned_Type'Size);
      pragma Assert (Base in 2 .. 36);
      procedure Put_Image (S : in out Sink'Class; X : Integer_Type);
      procedure Put_Image (S : in out Sink'Class; X : Unsigned_Type);
   private
      subtype Digit is Unsigned_Type range 0 .. Base - 1;
   end Generic_Integer_Images;

   package body Generic_Integer_Images is

      A : constant := Character'Pos ('a');
      Z : constant := Character'Pos ('0');
      function Digit_To_Character (X : Digit) return Character is
        (Character'Val (if X < 10 then X + Z else X + A - 10));

      procedure Put_Digits (S : in out Sink'Class; X : Unsigned_Type);
      --  Put just the digits of X, without any leading minus sign or space.

      procedure Put_Digits (S : in out Sink'Class; X : Unsigned_Type) is
      begin
         if X >= Base then
            Put_Digits (S, X / Base); -- recurse
            Put_7bit (S, Digit_To_Character (X mod Base));
         else
            Put_7bit (S, Digit_To_Character (X));
         end if;
      end Put_Digits;

      procedure Put_Image (S : in out Sink'Class; X : Integer_Type) is
      begin
         --  Put the space or the minus sign, then pass the absolute value to
         --  Put_Digits.

         if X >= 0 then
            Put_7bit (S, ' ');
            Put_Digits (S, Unsigned_Type (X));
         else
            Put_7bit (S, '-');
            Put_Digits (S, -Unsigned_Type'Mod (X));
            --  Convert to Unsigned_Type before negating, to avoid overflow
            --  on Integer_Type'First.
         end if;
      end Put_Image;

      procedure Put_Image (S : in out Sink'Class; X : Unsigned_Type) is
      begin
         Put_7bit (S, ' ');
         Put_Digits (S, X);
      end Put_Image;

   end Generic_Integer_Images;

   package Integer_Images is new Generic_Integer_Images
     (Integer, Unsigned, Base => 10);
   package LL_Integer_Images is new Generic_Integer_Images
     (Long_Long_Integer, Long_Long_Unsigned, Base => 10);
   package LLL_Integer_Images is new Generic_Integer_Images
     (Long_Long_Long_Integer, Long_Long_Long_Unsigned, Base => 10);

   procedure Put_Image_Integer (S : in out Sink'Class; X : Integer)
     renames Integer_Images.Put_Image;
   procedure Put_Image_Long_Long_Integer
     (S : in out Sink'Class; X : Long_Long_Integer)
     renames LL_Integer_Images.Put_Image;
   procedure Put_Image_Long_Long_Long_Integer
     (S : in out Sink'Class; X : Long_Long_Long_Integer)
     renames LLL_Integer_Images.Put_Image;

   procedure Put_Image_Unsigned (S : in out Sink'Class; X : Unsigned)
     renames Integer_Images.Put_Image;
   procedure Put_Image_Long_Long_Unsigned
     (S : in out Sink'Class; X : Long_Long_Unsigned)
     renames LL_Integer_Images.Put_Image;
   procedure Put_Image_Long_Long_Long_Unsigned
     (S : in out Sink'Class; X : Long_Long_Long_Unsigned)
     renames LLL_Integer_Images.Put_Image;

   type Signed_Address is range -Memory_Size / 2 .. Memory_Size / 2 - 1;
   type Unsigned_Address is mod Memory_Size;
   package Hex is new Generic_Integer_Images
     (Signed_Address, Unsigned_Address, Base => 16);

   generic
      type Designated (<>) is private;
      type Pointer is access all Designated;
   procedure Put_Image_Pointer
     (S : in out Sink'Class; X : Pointer; Type_Kind : String);

   procedure Put_Image_Pointer
     (S : in out Sink'Class; X : Pointer; Type_Kind : String)
   is
   begin
      if X = null then
         Put_UTF_8 (S, "null");
      else
         Put_UTF_8 (S, "(");
         Put_UTF_8 (S, Type_Kind);
         Hex.Put_Image (S, Unsigned_Address (To_Integer (X.all'Address)));
         Put_UTF_8 (S, ")");
      end if;
   end Put_Image_Pointer;

   procedure Thin_Instance is new Put_Image_Pointer (Byte, Thin_Pointer);
   procedure Put_Image_Thin_Pointer
     (S : in out Sink'Class; X : Thin_Pointer)
   is
   begin
      Thin_Instance (S, X, "access");
   end Put_Image_Thin_Pointer;

   procedure Fat_Instance is new Put_Image_Pointer (Byte_String, Fat_Pointer);
   procedure Put_Image_Fat_Pointer
     (S : in out Sink'Class; X : Fat_Pointer)
   is
   begin
      Fat_Instance (S, X, "access");
   end Put_Image_Fat_Pointer;

   procedure Put_Image_Access_Subp (S : in out Sink'Class; X : Thin_Pointer) is
   begin
      Thin_Instance (S, X, "access subprogram");
   end Put_Image_Access_Subp;

   procedure Put_Image_Access_Prot_Subp
     (S : in out Sink'Class; X : Thin_Pointer)
   is
   begin
      Thin_Instance (S, X, "access protected subprogram");
   end Put_Image_Access_Prot_Subp;

   procedure Put_Image_String
     (S               : in out Sink'Class;
      X               : String;
      With_Delimiters : Boolean := True) is
   begin
      if With_Delimiters then
         Put_UTF_8 (S, """");
      end if;

      for C of X loop
         if C = '"' and then With_Delimiters then
            Put_UTF_8 (S, """");
         end if;
         Put_Character (S, C);
      end loop;

      if With_Delimiters then
         Put_UTF_8 (S, """");
      end if;
   end Put_Image_String;

   procedure Put_Image_Wide_String
     (S               : in out Sink'Class;
      X               : Wide_String;
      With_Delimiters : Boolean := True) is
   begin
      if With_Delimiters then
         Put_UTF_8 (S, """");
      end if;

      for C of X loop
         if C = '"' and then With_Delimiters then
            Put_UTF_8 (S, """");
         end if;
         Put_Wide_Character (S, C);
      end loop;

      if With_Delimiters then
         Put_UTF_8 (S, """");
      end if;
   end Put_Image_Wide_String;

   procedure Put_Image_Wide_Wide_String
     (S               : in out Sink'Class;
      X               : Wide_Wide_String;
      With_Delimiters : Boolean := True) is
   begin
      if With_Delimiters then
         Put_UTF_8 (S, """");
      end if;

      for C of X loop
         if C = '"' and then With_Delimiters then
            Put_UTF_8 (S, """");
         end if;
         Put_Wide_Wide_Character (S, C);
      end loop;

      if With_Delimiters then
         Put_UTF_8 (S, """");
      end if;
   end Put_Image_Wide_Wide_String;

   procedure Array_Before (S : in out Sink'Class) is
   begin
      New_Line (S);
      Put_7bit (S, '[');
      Increase_Indent (S, 1);
   end Array_Before;

   procedure Array_Between (S : in out Sink'Class) is
   begin
      Put_7bit (S, ',');
      New_Line (S);
   end Array_Between;

   procedure Array_After (S : in out Sink'Class) is
   begin
      Decrease_Indent (S, 1);
      Put_7bit (S, ']');
   end Array_After;

   procedure Simple_Array_Between (S : in out Sink'Class) is
   begin
      Put_7bit (S, ',');
      if Column (S) > 60 then
         New_Line (S);
      else
         Put_7bit (S, ' ');
      end if;
   end Simple_Array_Between;

   procedure Record_Before (S : in out Sink'Class) is
   begin
      New_Line (S);
      Put_7bit (S, '(');
      Increase_Indent (S, 1);
   end Record_Before;

   procedure Record_Between (S : in out Sink'Class) is
   begin
      Put_7bit (S, ',');
      New_Line (S);
   end Record_Between;

   procedure Record_After (S : in out Sink'Class) is
   begin
      Decrease_Indent (S, 1);
      Put_7bit (S, ')');
   end Record_After;

   procedure Put_Arrow (S : in out Sink'Class) is
   begin
      Put_UTF_8 (S, " => ");
   end Put_Arrow;

   procedure Put_Image_Unknown (S : in out Sink'Class; Type_Name : String) is
   begin
      Put_UTF_8 (S, "{");
      Put (S, Type_Name);
      Put_UTF_8 (S, " object}");
   end Put_Image_Unknown;

end System.Put_Images;