| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- SYSTEM.PUT_IMAGES -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2020-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. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
with Ada.Strings.Text_Buffers.Utils; |
| 33 |
use Ada.Strings.Text_Buffers; |
| 34 |
use Ada.Strings.Text_Buffers.Utils; |
| 35 |
with System.Storage_Elements; use System.Storage_Elements; |
| 36 |
|
| 37 |
package body System.Put_Images is |
| 38 |
|
| 39 |
generic |
| 40 |
type Integer_Type is range <>; |
| 41 |
type Unsigned_Type is mod <>; |
| 42 |
Base : Unsigned_Type; |
| 43 |
package Generic_Integer_Images is |
| 44 |
pragma Assert (Integer_Type'Size = Unsigned_Type'Size); |
| 45 |
pragma Assert (Base in 2 .. 36); |
| 46 |
procedure Put_Image (S : in out Sink'Class; X : Integer_Type); |
| 47 |
procedure Put_Image (S : in out Sink'Class; X : Unsigned_Type); |
| 48 |
private |
| 49 |
subtype Digit is Unsigned_Type range 0 .. Base - 1; |
| 50 |
end Generic_Integer_Images; |
| 51 |
|
| 52 |
package body Generic_Integer_Images is |
| 53 |
|
| 54 |
A : constant := Character'Pos ('a'); |
| 55 |
Z : constant := Character'Pos ('0'); |
| 56 |
function Digit_To_Character (X : Digit) return Character is |
| 57 |
(Character'Val (if X < 10 then X + Z else X + A - 10)); |
| 58 |
|
| 59 |
procedure Put_Digits (S : in out Sink'Class; X : Unsigned_Type); |
| 60 |
-- Put just the digits of X, without any leading minus sign or space. |
| 61 |
|
| 62 |
procedure Put_Digits (S : in out Sink'Class; X : Unsigned_Type) is |
| 63 |
begin |
| 64 |
if X >= Base then |
| 65 |
Put_Digits (S, X / Base); -- recurse |
| 66 |
Put_7bit (S, Digit_To_Character (X mod Base)); |
| 67 |
else |
| 68 |
Put_7bit (S, Digit_To_Character (X)); |
| 69 |
end if; |
| 70 |
end Put_Digits; |
| 71 |
|
| 72 |
procedure Put_Image (S : in out Sink'Class; X : Integer_Type) is |
| 73 |
begin |
| 74 |
-- Put the space or the minus sign, then pass the absolute value to |
| 75 |
-- Put_Digits. |
| 76 |
|
| 77 |
if X >= 0 then |
| 78 |
Put_7bit (S, ' '); |
| 79 |
Put_Digits (S, Unsigned_Type (X)); |
| 80 |
else |
| 81 |
Put_7bit (S, '-'); |
| 82 |
Put_Digits (S, -Unsigned_Type'Mod (X)); |
| 83 |
-- Convert to Unsigned_Type before negating, to avoid overflow |
| 84 |
-- on Integer_Type'First. |
| 85 |
end if; |
| 86 |
end Put_Image; |
| 87 |
|
| 88 |
procedure Put_Image (S : in out Sink'Class; X : Unsigned_Type) is |
| 89 |
begin |
| 90 |
Put_7bit (S, ' '); |
| 91 |
Put_Digits (S, X); |
| 92 |
end Put_Image; |
| 93 |
|
| 94 |
end Generic_Integer_Images; |
| 95 |
|
| 96 |
package Integer_Images is new Generic_Integer_Images |
| 97 |
(Integer, Unsigned, Base => 10); |
| 98 |
package LL_Integer_Images is new Generic_Integer_Images |
| 99 |
(Long_Long_Integer, Long_Long_Unsigned, Base => 10); |
| 100 |
package LLL_Integer_Images is new Generic_Integer_Images |
| 101 |
(Long_Long_Long_Integer, Long_Long_Long_Unsigned, Base => 10); |
| 102 |
|
| 103 |
procedure Put_Image_Integer (S : in out Sink'Class; X : Integer) |
| 104 |
renames Integer_Images.Put_Image; |
| 105 |
procedure Put_Image_Long_Long_Integer |
| 106 |
(S : in out Sink'Class; X : Long_Long_Integer) |
| 107 |
renames LL_Integer_Images.Put_Image; |
| 108 |
procedure Put_Image_Long_Long_Long_Integer |
| 109 |
(S : in out Sink'Class; X : Long_Long_Long_Integer) |
| 110 |
renames LLL_Integer_Images.Put_Image; |
| 111 |
|
| 112 |
procedure Put_Image_Unsigned (S : in out Sink'Class; X : Unsigned) |
| 113 |
renames Integer_Images.Put_Image; |
| 114 |
procedure Put_Image_Long_Long_Unsigned |
| 115 |
(S : in out Sink'Class; X : Long_Long_Unsigned) |
| 116 |
renames LL_Integer_Images.Put_Image; |
| 117 |
procedure Put_Image_Long_Long_Long_Unsigned |
| 118 |
(S : in out Sink'Class; X : Long_Long_Long_Unsigned) |
| 119 |
renames LLL_Integer_Images.Put_Image; |
| 120 |
|
| 121 |
type Signed_Address is range -Memory_Size / 2 .. Memory_Size / 2 - 1; |
| 122 |
type Unsigned_Address is mod Memory_Size; |
| 123 |
package Hex is new Generic_Integer_Images |
| 124 |
(Signed_Address, Unsigned_Address, Base => 16); |
| 125 |
|
| 126 |
generic |
| 127 |
type Designated (<>) is private; |
| 128 |
type Pointer is access all Designated; |
| 129 |
procedure Put_Image_Pointer |
| 130 |
(S : in out Sink'Class; X : Pointer; Type_Kind : String); |
| 131 |
|
| 132 |
procedure Put_Image_Pointer |
| 133 |
(S : in out Sink'Class; X : Pointer; Type_Kind : String) |
| 134 |
is |
| 135 |
begin |
| 136 |
if X = null then |
| 137 |
Put_UTF_8 (S, "null"); |
| 138 |
else |
| 139 |
Put_UTF_8 (S, "("); |
| 140 |
Put_UTF_8 (S, Type_Kind); |
| 141 |
Hex.Put_Image (S, Unsigned_Address (To_Integer (X.all'Address))); |
| 142 |
Put_UTF_8 (S, ")"); |
| 143 |
end if; |
| 144 |
end Put_Image_Pointer; |
| 145 |
|
| 146 |
procedure Thin_Instance is new Put_Image_Pointer (Byte, Thin_Pointer); |
| 147 |
procedure Put_Image_Thin_Pointer |
| 148 |
(S : in out Sink'Class; X : Thin_Pointer) |
| 149 |
is |
| 150 |
begin |
| 151 |
Thin_Instance (S, X, "access"); |
| 152 |
end Put_Image_Thin_Pointer; |
| 153 |
|
| 154 |
procedure Fat_Instance is new Put_Image_Pointer (Byte_String, Fat_Pointer); |
| 155 |
procedure Put_Image_Fat_Pointer |
| 156 |
(S : in out Sink'Class; X : Fat_Pointer) |
| 157 |
is |
| 158 |
begin |
| 159 |
Fat_Instance (S, X, "access"); |
| 160 |
end Put_Image_Fat_Pointer; |
| 161 |
|
| 162 |
procedure Put_Image_Access_Subp (S : in out Sink'Class; X : Thin_Pointer) is |
| 163 |
begin |
| 164 |
Thin_Instance (S, X, "access subprogram"); |
| 165 |
end Put_Image_Access_Subp; |
| 166 |
|
| 167 |
procedure Put_Image_Access_Prot_Subp |
| 168 |
(S : in out Sink'Class; X : Thin_Pointer) |
| 169 |
is |
| 170 |
begin |
| 171 |
Thin_Instance (S, X, "access protected subprogram"); |
| 172 |
end Put_Image_Access_Prot_Subp; |
| 173 |
|
| 174 |
procedure Put_Image_String |
| 175 |
(S : in out Sink'Class; |
| 176 |
X : String; |
| 177 |
With_Delimiters : Boolean := True) is |
| 178 |
begin |
| 179 |
if With_Delimiters then |
| 180 |
Put_UTF_8 (S, """"); |
| 181 |
end if; |
| 182 |
|
| 183 |
for C of X loop |
| 184 |
if C = '"' and then With_Delimiters then |
| 185 |
Put_UTF_8 (S, """"); |
| 186 |
end if; |
| 187 |
Put_Character (S, C); |
| 188 |
end loop; |
| 189 |
|
| 190 |
if With_Delimiters then |
| 191 |
Put_UTF_8 (S, """"); |
| 192 |
end if; |
| 193 |
end Put_Image_String; |
| 194 |
|
| 195 |
procedure Put_Image_Wide_String |
| 196 |
(S : in out Sink'Class; |
| 197 |
X : Wide_String; |
| 198 |
With_Delimiters : Boolean := True) is |
| 199 |
begin |
| 200 |
if With_Delimiters then |
| 201 |
Put_UTF_8 (S, """"); |
| 202 |
end if; |
| 203 |
|
| 204 |
for C of X loop |
| 205 |
if C = '"' and then With_Delimiters then |
| 206 |
Put_UTF_8 (S, """"); |
| 207 |
end if; |
| 208 |
Put_Wide_Character (S, C); |
| 209 |
end loop; |
| 210 |
|
| 211 |
if With_Delimiters then |
| 212 |
Put_UTF_8 (S, """"); |
| 213 |
end if; |
| 214 |
end Put_Image_Wide_String; |
| 215 |
|
| 216 |
procedure Put_Image_Wide_Wide_String |
| 217 |
(S : in out Sink'Class; |
| 218 |
X : Wide_Wide_String; |
| 219 |
With_Delimiters : Boolean := True) is |
| 220 |
begin |
| 221 |
if With_Delimiters then |
| 222 |
Put_UTF_8 (S, """"); |
| 223 |
end if; |
| 224 |
|
| 225 |
for C of X loop |
| 226 |
if C = '"' and then With_Delimiters then |
| 227 |
Put_UTF_8 (S, """"); |
| 228 |
end if; |
| 229 |
Put_Wide_Wide_Character (S, C); |
| 230 |
end loop; |
| 231 |
|
| 232 |
if With_Delimiters then |
| 233 |
Put_UTF_8 (S, """"); |
| 234 |
end if; |
| 235 |
end Put_Image_Wide_Wide_String; |
| 236 |
|
| 237 |
procedure Array_Before (S : in out Sink'Class) is |
| 238 |
begin |
| 239 |
New_Line (S); |
| 240 |
Put_7bit (S, '['); |
| 241 |
Increase_Indent (S, 1); |
| 242 |
end Array_Before; |
| 243 |
|
| 244 |
procedure Array_Between (S : in out Sink'Class) is |
| 245 |
begin |
| 246 |
Put_7bit (S, ','); |
| 247 |
New_Line (S); |
| 248 |
end Array_Between; |
| 249 |
|
| 250 |
procedure Array_After (S : in out Sink'Class) is |
| 251 |
begin |
| 252 |
Decrease_Indent (S, 1); |
| 253 |
Put_7bit (S, ']'); |
| 254 |
end Array_After; |
| 255 |
|
| 256 |
procedure Simple_Array_Between (S : in out Sink'Class) is |
| 257 |
begin |
| 258 |
Put_7bit (S, ','); |
| 259 |
if Column (S) > 60 then |
| 260 |
New_Line (S); |
| 261 |
else |
| 262 |
Put_7bit (S, ' '); |
| 263 |
end if; |
| 264 |
end Simple_Array_Between; |
| 265 |
|
| 266 |
procedure Record_Before (S : in out Sink'Class) is |
| 267 |
begin |
| 268 |
New_Line (S); |
| 269 |
Put_7bit (S, '('); |
| 270 |
Increase_Indent (S, 1); |
| 271 |
end Record_Before; |
| 272 |
|
| 273 |
procedure Record_Between (S : in out Sink'Class) is |
| 274 |
begin |
| 275 |
Put_7bit (S, ','); |
| 276 |
New_Line (S); |
| 277 |
end Record_Between; |
| 278 |
|
| 279 |
procedure Record_After (S : in out Sink'Class) is |
| 280 |
begin |
| 281 |
Decrease_Indent (S, 1); |
| 282 |
Put_7bit (S, ')'); |
| 283 |
end Record_After; |
| 284 |
|
| 285 |
procedure Put_Arrow (S : in out Sink'Class) is |
| 286 |
begin |
| 287 |
Put_UTF_8 (S, " => "); |
| 288 |
end Put_Arrow; |
| 289 |
|
| 290 |
procedure Put_Image_Unknown (S : in out Sink'Class; Type_Name : String) is |
| 291 |
begin |
| 292 |
Put_UTF_8 (S, "{"); |
| 293 |
Put (S, Type_Name); |
| 294 |
Put_UTF_8 (S, " object}"); |
| 295 |
end Put_Image_Unknown; |
| 296 |
|
| 297 |
end System.Put_Images; |