| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- E X P _ P U T _ I M A G E -- |
| 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. 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 Aspects; use Aspects; |
| 27 |
with Atree; use Atree; |
| 28 |
with Csets; use Csets; |
| 29 |
with Debug; use Debug; |
| 30 |
with Einfo; use Einfo; |
| 31 |
with Einfo.Entities; use Einfo.Entities; |
| 32 |
with Einfo.Utils; use Einfo.Utils; |
| 33 |
with Exp_Tss; use Exp_Tss; |
| 34 |
with Exp_Util; use Exp_Util; |
| 35 |
with Lib; use Lib; |
| 36 |
with Mutably_Tagged; use Mutably_Tagged; |
| 37 |
with Namet; use Namet; |
| 38 |
with Nlists; use Nlists; |
| 39 |
with Nmake; use Nmake; |
| 40 |
with Opt; use Opt; |
| 41 |
with Output; use Output; |
| 42 |
with Rtsfind; use Rtsfind; |
| 43 |
with Sem_Aux; use Sem_Aux; |
| 44 |
with Sem_Util; use Sem_Util; |
| 45 |
with Sinfo; use Sinfo; |
| 46 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 47 |
with Sinfo.Utils; use Sinfo.Utils; |
| 48 |
with Snames; use Snames; |
| 49 |
with Stand; |
| 50 |
with Stringt; use Stringt; |
| 51 |
with Tbuild; use Tbuild; |
| 52 |
with Ttypes; use Ttypes; |
| 53 |
with Uintp; use Uintp; |
| 54 |
|
| 55 |
package body Exp_Put_Image is |
| 56 |
|
| 57 |
----------------------- |
| 58 |
-- Local Subprograms -- |
| 59 |
----------------------- |
| 60 |
|
| 61 |
procedure Build_Put_Image_Proc |
| 62 |
(Loc : Source_Ptr; |
| 63 |
Typ : Entity_Id; |
| 64 |
Decl : out Node_Id; |
| 65 |
Pnam : Entity_Id; |
| 66 |
Stms : List_Id); |
| 67 |
-- Build an array or record Put_Image procedure. Stms is the list of |
| 68 |
-- statements for the body and Pnam is the name of the constructed |
| 69 |
-- procedure. (The declaration list is always null.) |
| 70 |
|
| 71 |
function Make_Put_Image_Name |
| 72 |
(Loc : Source_Ptr; Typ : Entity_Id) return Entity_Id; |
| 73 |
-- Return the entity that identifies the Put_Image subprogram for Typ. This |
| 74 |
-- procedure deals with the difference between tagged types (where a single |
| 75 |
-- subprogram associated with the type is generated) and all other cases |
| 76 |
-- (where a subprogram is generated at the point of the attribute |
| 77 |
-- reference). The Loc parameter is used as the Sloc of the created entity. |
| 78 |
|
| 79 |
function Put_Image_Base_Type (E : Entity_Id) return Entity_Id; |
| 80 |
-- Returns the base type, except for an array type whose whose first |
| 81 |
-- subtype is constrained, in which case it returns the first subtype. |
| 82 |
|
| 83 |
------------------------------------- |
| 84 |
-- Build_Array_Put_Image_Procedure -- |
| 85 |
------------------------------------- |
| 86 |
|
| 87 |
procedure Build_Array_Put_Image_Procedure |
| 88 |
(Typ : Entity_Id; |
| 89 |
Decl : out Node_Id; |
| 90 |
Pnam : out Entity_Id) |
| 91 |
is |
| 92 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 93 |
|
| 94 |
function Wrap_In_Loop |
| 95 |
(Stms : List_Id; |
| 96 |
Dim : Pos; |
| 97 |
Index_Subtype : Entity_Id; |
| 98 |
Between_Proc : RE_Id) return Node_Id; |
| 99 |
-- Wrap Stms in a loop and if statement of the form: |
| 100 |
-- |
| 101 |
-- if V'First (Dim) <= V'Last (Dim) then -- nonempty range? |
| 102 |
-- declare |
| 103 |
-- LDim : Index_Type_For_Dim := V'First (Dim); |
| 104 |
-- begin |
| 105 |
-- loop |
| 106 |
-- Stms; |
| 107 |
-- exit when LDim = V'Last (Dim); |
| 108 |
-- Between_Proc (S); |
| 109 |
-- LDim := Index_Type_For_Dim'Succ (LDim); |
| 110 |
-- end loop; |
| 111 |
-- end; |
| 112 |
-- end if; |
| 113 |
-- |
| 114 |
-- This is called once per dimension, from inner to outer. |
| 115 |
|
| 116 |
function Wrap_In_Loop |
| 117 |
(Stms : List_Id; |
| 118 |
Dim : Pos; |
| 119 |
Index_Subtype : Entity_Id; |
| 120 |
Between_Proc : RE_Id) return Node_Id |
| 121 |
is |
| 122 |
Index : constant Entity_Id := |
| 123 |
Make_Defining_Identifier |
| 124 |
(Loc, Chars => New_External_Name ('L', Dim)); |
| 125 |
Decl : constant Node_Id := |
| 126 |
Make_Object_Declaration (Loc, |
| 127 |
Defining_Identifier => Index, |
| 128 |
Object_Definition => |
| 129 |
New_Occurrence_Of (Index_Subtype, Loc), |
| 130 |
Expression => |
| 131 |
Make_Attribute_Reference (Loc, |
| 132 |
Prefix => Make_Identifier (Loc, Name_V), |
| 133 |
Attribute_Name => Name_First, |
| 134 |
Expressions => New_List ( |
| 135 |
Make_Integer_Literal (Loc, Dim)))); |
| 136 |
Loop_Stm : constant Node_Id := |
| 137 |
Make_Implicit_Loop_Statement (Typ, Statements => Stms); |
| 138 |
Exit_Stm : constant Node_Id := |
| 139 |
Make_Exit_Statement (Loc, |
| 140 |
Condition => |
| 141 |
Make_Op_Eq (Loc, |
| 142 |
Left_Opnd => New_Occurrence_Of (Index, Loc), |
| 143 |
Right_Opnd => |
| 144 |
Make_Attribute_Reference (Loc, |
| 145 |
Prefix => |
| 146 |
Make_Identifier (Loc, Name_V), |
| 147 |
Attribute_Name => Name_Last, |
| 148 |
Expressions => New_List ( |
| 149 |
Make_Integer_Literal (Loc, Dim))))); |
| 150 |
Increment : constant Node_Id := |
| 151 |
Make_Increment (Loc, Index, Index_Subtype); |
| 152 |
Between : constant Node_Id := |
| 153 |
Make_Procedure_Call_Statement (Loc, |
| 154 |
Name => |
| 155 |
New_Occurrence_Of (RTE (Between_Proc), Loc), |
| 156 |
Parameter_Associations => New_List |
| 157 |
(Make_Identifier (Loc, Name_S))); |
| 158 |
Block : constant Node_Id := |
| 159 |
Make_Block_Statement (Loc, |
| 160 |
Declarations => New_List (Decl), |
| 161 |
Handled_Statement_Sequence => |
| 162 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 163 |
Statements => New_List (Loop_Stm))); |
| 164 |
begin |
| 165 |
Append_To (Stms, Exit_Stm); |
| 166 |
Append_To (Stms, Between); |
| 167 |
Append_To (Stms, Increment); |
| 168 |
-- Note that we're appending to the Stms list passed in |
| 169 |
|
| 170 |
return |
| 171 |
Make_If_Statement (Loc, |
| 172 |
Condition => |
| 173 |
Make_Op_Le (Loc, |
| 174 |
Left_Opnd => |
| 175 |
Make_Attribute_Reference (Loc, |
| 176 |
Prefix => Make_Identifier (Loc, Name_V), |
| 177 |
Attribute_Name => Name_First, |
| 178 |
Expressions => New_List ( |
| 179 |
Make_Integer_Literal (Loc, Dim))), |
| 180 |
Right_Opnd => |
| 181 |
Make_Attribute_Reference (Loc, |
| 182 |
Prefix => Make_Identifier (Loc, Name_V), |
| 183 |
Attribute_Name => Name_Last, |
| 184 |
Expressions => New_List ( |
| 185 |
Make_Integer_Literal (Loc, Dim)))), |
| 186 |
Then_Statements => New_List (Block)); |
| 187 |
end Wrap_In_Loop; |
| 188 |
|
| 189 |
Ndim : constant Pos := Number_Dimensions (Typ); |
| 190 |
Ctyp : constant Entity_Id := Component_Type (Typ); |
| 191 |
|
| 192 |
Stm : Node_Id; |
| 193 |
Exl : constant List_Id := New_List; |
| 194 |
PI_Entity : Entity_Id; |
| 195 |
|
| 196 |
Indices : array (1 .. Ndim) of Entity_Id; |
| 197 |
|
| 198 |
-- Start of processing for Build_Array_Put_Image_Procedure |
| 199 |
|
| 200 |
begin |
| 201 |
Pnam := |
| 202 |
Make_Defining_Identifier (Loc, |
| 203 |
Chars => Make_TSS_Name_Local (Typ, TSS_Put_Image)); |
| 204 |
|
| 205 |
-- Get the Indices |
| 206 |
|
| 207 |
declare |
| 208 |
Index_Subtype : Node_Id := First_Index (Typ); |
| 209 |
begin |
| 210 |
for Dim in 1 .. Ndim loop |
| 211 |
Indices (Dim) := Etype (Index_Subtype); |
| 212 |
Next_Index (Index_Subtype); |
| 213 |
end loop; |
| 214 |
pragma Assert (No (Index_Subtype)); |
| 215 |
end; |
| 216 |
|
| 217 |
-- Build the inner attribute call |
| 218 |
|
| 219 |
for Dim in 1 .. Ndim loop |
| 220 |
Append_To (Exl, Make_Identifier (Loc, New_External_Name ('L', Dim))); |
| 221 |
end loop; |
| 222 |
|
| 223 |
Stm := |
| 224 |
Make_Attribute_Reference (Loc, |
| 225 |
Prefix => New_Occurrence_Of (Put_Image_Base_Type (Ctyp), Loc), |
| 226 |
Attribute_Name => Name_Put_Image, |
| 227 |
Expressions => New_List ( |
| 228 |
Make_Identifier (Loc, Name_S), |
| 229 |
Make_Indexed_Component (Loc, |
| 230 |
Prefix => Make_Identifier (Loc, Name_V), |
| 231 |
Expressions => Exl))); |
| 232 |
|
| 233 |
-- The corresponding attribute for the component type of the array might |
| 234 |
-- be user-defined, and frozen after the array type. In that case, |
| 235 |
-- freeze the Put_Image attribute of the component type, whose |
| 236 |
-- declaration could not generate any additional freezing actions in any |
| 237 |
-- case. |
| 238 |
|
| 239 |
PI_Entity := TSS (Base_Type (Ctyp), TSS_Put_Image); |
| 240 |
|
| 241 |
if Present (PI_Entity) and then not Is_Frozen (PI_Entity) then |
| 242 |
Set_Is_Frozen (PI_Entity); |
| 243 |
end if; |
| 244 |
|
| 245 |
-- Loop through the dimensions, innermost first, generating a loop for |
| 246 |
-- each dimension. |
| 247 |
|
| 248 |
declare |
| 249 |
Stms : List_Id := New_List (Stm); |
| 250 |
begin |
| 251 |
for Dim in reverse 1 .. Ndim loop |
| 252 |
declare |
| 253 |
New_Stms : constant List_Id := New_List; |
| 254 |
Between_Proc : RE_Id; |
| 255 |
begin |
| 256 |
-- For a one-dimensional array of elementary type, use |
| 257 |
-- RE_Simple_Array_Between. The same applies to the last |
| 258 |
-- dimension of a multidimensional array. |
| 259 |
|
| 260 |
if Is_Elementary_Type (Ctyp) and then Dim = Ndim then |
| 261 |
Between_Proc := RE_Simple_Array_Between; |
| 262 |
else |
| 263 |
Between_Proc := RE_Array_Between; |
| 264 |
end if; |
| 265 |
|
| 266 |
Append_To (New_Stms, |
| 267 |
Make_Procedure_Call_Statement (Loc, |
| 268 |
Name => New_Occurrence_Of (RTE (RE_Array_Before), Loc), |
| 269 |
Parameter_Associations => New_List |
| 270 |
(Make_Identifier (Loc, Name_S)))); |
| 271 |
|
| 272 |
Append_To |
| 273 |
(New_Stms, |
| 274 |
Wrap_In_Loop (Stms, Dim, Indices (Dim), Between_Proc)); |
| 275 |
|
| 276 |
Append_To (New_Stms, |
| 277 |
Make_Procedure_Call_Statement (Loc, |
| 278 |
Name => New_Occurrence_Of (RTE (RE_Array_After), Loc), |
| 279 |
Parameter_Associations => New_List |
| 280 |
(Make_Identifier (Loc, Name_S)))); |
| 281 |
|
| 282 |
Stms := New_Stms; |
| 283 |
end; |
| 284 |
end loop; |
| 285 |
|
| 286 |
Build_Put_Image_Proc (Loc, Typ, Decl, Pnam, Stms); |
| 287 |
end; |
| 288 |
end Build_Array_Put_Image_Procedure; |
| 289 |
|
| 290 |
------------------------------------- |
| 291 |
-- Build_Elementary_Put_Image_Call -- |
| 292 |
------------------------------------- |
| 293 |
|
| 294 |
function Build_Elementary_Put_Image_Call (N : Node_Id) return Node_Id is |
| 295 |
Loc : constant Source_Ptr := Sloc (N); |
| 296 |
P_Type : constant Entity_Id := Entity (Prefix (N)); |
| 297 |
U_Type : constant Entity_Id := Underlying_Type (P_Type); |
| 298 |
Sink : constant Node_Id := First (Expressions (N)); |
| 299 |
Item : constant Node_Id := Next (Sink); |
| 300 |
P_Size : constant Uint := Esize (U_Type); |
| 301 |
Lib_RE : RE_Id; |
| 302 |
|
| 303 |
begin |
| 304 |
if Is_Signed_Integer_Type (U_Type) then |
| 305 |
if P_Size <= Standard_Integer_Size then |
| 306 |
Lib_RE := RE_Put_Image_Integer; |
| 307 |
elsif P_Size <= Standard_Long_Long_Integer_Size then |
| 308 |
Lib_RE := RE_Put_Image_Long_Long_Integer; |
| 309 |
else |
| 310 |
pragma Assert (P_Size <= Standard_Long_Long_Long_Integer_Size); |
| 311 |
Lib_RE := RE_Put_Image_Long_Long_Long_Integer; |
| 312 |
end if; |
| 313 |
|
| 314 |
elsif Is_Modular_Integer_Type (U_Type) then |
| 315 |
if P_Size <= Standard_Integer_Size then -- Yes, Integer |
| 316 |
Lib_RE := RE_Put_Image_Unsigned; |
| 317 |
elsif P_Size <= Standard_Long_Long_Integer_Size then |
| 318 |
Lib_RE := RE_Put_Image_Long_Long_Unsigned; |
| 319 |
else |
| 320 |
pragma Assert (P_Size <= Standard_Long_Long_Long_Integer_Size); |
| 321 |
Lib_RE := RE_Put_Image_Long_Long_Long_Unsigned; |
| 322 |
end if; |
| 323 |
|
| 324 |
elsif Is_Access_Type (U_Type) then |
| 325 |
if Is_Access_Protected_Subprogram_Type (Base_Type (U_Type)) then |
| 326 |
Lib_RE := RE_Put_Image_Access_Prot_Subp; |
| 327 |
elsif Is_Access_Subprogram_Type (Base_Type (U_Type)) then |
| 328 |
Lib_RE := RE_Put_Image_Access_Subp; |
| 329 |
elsif P_Size = System_Address_Size then |
| 330 |
Lib_RE := RE_Put_Image_Thin_Pointer; |
| 331 |
else |
| 332 |
pragma Assert (P_Size = 2 * System_Address_Size); |
| 333 |
Lib_RE := RE_Put_Image_Fat_Pointer; |
| 334 |
end if; |
| 335 |
|
| 336 |
else |
| 337 |
pragma Assert |
| 338 |
(Is_Enumeration_Type (U_Type) or else Is_Real_Type (U_Type)); |
| 339 |
|
| 340 |
-- For other elementary types, generate: |
| 341 |
-- |
| 342 |
-- Wide_Wide_Put (Root_Buffer_Type'Class (Sink), |
| 343 |
-- U_Type'Wide_Wide_Image (Item)); |
| 344 |
-- |
| 345 |
-- It would be more elegant to do it the other way around (define |
| 346 |
-- '[[Wide_]Wide_]Image in terms of 'Put_Image). But this is easier |
| 347 |
-- to implement, because we already have support for |
| 348 |
-- 'Wide_Wide_Image. Furthermore, we don't want to remove the |
| 349 |
-- existing support for '[[Wide_]Wide_]Image, because we don't |
| 350 |
-- currently plan to support 'Put_Image on restricted runtimes. |
| 351 |
|
| 352 |
-- We can't do this: |
| 353 |
-- |
| 354 |
-- Put_UTF_8 (Sink, U_Type'Image (Item)); |
| 355 |
-- |
| 356 |
-- because we need to generate UTF-8, but 'Image for enumeration |
| 357 |
-- types uses the character encoding of the source file. |
| 358 |
-- |
| 359 |
-- Note that this is putting a leading space for reals. |
| 360 |
|
| 361 |
declare |
| 362 |
Image : constant Node_Id := |
| 363 |
Make_Attribute_Reference (Loc, |
| 364 |
Prefix => New_Occurrence_Of (U_Type, Loc), |
| 365 |
Attribute_Name => Name_Wide_Wide_Image, |
| 366 |
Expressions => New_List (Relocate_Node (Item))); |
| 367 |
Sink_Exp : constant Node_Id := |
| 368 |
Make_Type_Conversion (Loc, |
| 369 |
Subtype_Mark => |
| 370 |
New_Occurrence_Of |
| 371 |
(Class_Wide_Type (RTE (RE_Root_Buffer_Type)), Loc), |
| 372 |
Expression => Relocate_Node (Sink)); |
| 373 |
Put_Call : constant Node_Id := |
| 374 |
Make_Procedure_Call_Statement (Loc, |
| 375 |
Name => |
| 376 |
New_Occurrence_Of (RTE (RE_Wide_Wide_Put), Loc), |
| 377 |
Parameter_Associations => New_List |
| 378 |
(Sink_Exp, Image)); |
| 379 |
begin |
| 380 |
-- We have built a dispatching call to handle calls to |
| 381 |
-- descendants (since they are not available through rtsfind). |
| 382 |
-- Further details available in the body of Put_String_Exp. |
| 383 |
|
| 384 |
return Put_Call; |
| 385 |
end; |
| 386 |
end if; |
| 387 |
|
| 388 |
-- Unchecked-convert parameter to the required type (i.e. the type of |
| 389 |
-- the corresponding parameter), and call the appropriate routine. |
| 390 |
-- We could use a normal type conversion for scalars, but the |
| 391 |
-- "unchecked" is needed for access and private types. |
| 392 |
|
| 393 |
declare |
| 394 |
Libent : constant Entity_Id := RTE (Lib_RE); |
| 395 |
begin |
| 396 |
return |
| 397 |
Make_Procedure_Call_Statement (Loc, |
| 398 |
Name => New_Occurrence_Of (Libent, Loc), |
| 399 |
Parameter_Associations => New_List ( |
| 400 |
Relocate_Node (Sink), |
| 401 |
Unchecked_Convert_To |
| 402 |
(Etype (Next_Formal (First_Formal (Libent))), |
| 403 |
Relocate_Node (Item)))); |
| 404 |
end; |
| 405 |
end Build_Elementary_Put_Image_Call; |
| 406 |
|
| 407 |
--------------------------------- |
| 408 |
-- Build_String_Put_Image_Call -- |
| 409 |
--------------------------------- |
| 410 |
|
| 411 |
function Build_String_Put_Image_Call (N : Node_Id) return Node_Id is |
| 412 |
Loc : constant Source_Ptr := Sloc (N); |
| 413 |
P_Type : constant Entity_Id := Entity (Prefix (N)); |
| 414 |
U_Type : constant Entity_Id := Underlying_Type (P_Type); |
| 415 |
R : constant Entity_Id := Root_Type (U_Type); |
| 416 |
Sink : constant Node_Id := First (Expressions (N)); |
| 417 |
Item : constant Node_Id := Next (Sink); |
| 418 |
Lib_RE : RE_Id; |
| 419 |
use Stand; |
| 420 |
begin |
| 421 |
pragma Assert (Is_String_Type (U_Type)); |
| 422 |
pragma Assert (not RTU_Loaded (Interfaces_C) |
| 423 |
or else Enclosing_Lib_Unit_Entity (U_Type) |
| 424 |
/= RTU_Entity (Interfaces_C)); |
| 425 |
|
| 426 |
if R = Standard_String then |
| 427 |
Lib_RE := RE_Put_Image_String; |
| 428 |
elsif R = Standard_Wide_String then |
| 429 |
Lib_RE := RE_Put_Image_Wide_String; |
| 430 |
elsif R = Standard_Wide_Wide_String then |
| 431 |
Lib_RE := RE_Put_Image_Wide_Wide_String; |
| 432 |
|
| 433 |
else |
| 434 |
-- Handle custom string types. For example: |
| 435 |
|
| 436 |
-- type T is array (1 .. 10) of Character; |
| 437 |
-- Obj : T := (others => 'A'); |
| 438 |
-- ... |
| 439 |
-- Put (Obj'Image); |
| 440 |
|
| 441 |
declare |
| 442 |
C_Type : Entity_Id; |
| 443 |
|
| 444 |
begin |
| 445 |
if Is_Private_Type (R) then |
| 446 |
C_Type := Component_Type (Full_View (R)); |
| 447 |
else |
| 448 |
C_Type := Component_Type (R); |
| 449 |
end if; |
| 450 |
|
| 451 |
C_Type := Root_Type (Underlying_Type (C_Type)); |
| 452 |
|
| 453 |
if C_Type = Standard_Character then |
| 454 |
Lib_RE := RE_Put_Image_String; |
| 455 |
elsif C_Type = Standard_Wide_Character then |
| 456 |
Lib_RE := RE_Put_Image_Wide_String; |
| 457 |
elsif C_Type = Standard_Wide_Wide_Character then |
| 458 |
Lib_RE := RE_Put_Image_Wide_Wide_String; |
| 459 |
else |
| 460 |
raise Program_Error; |
| 461 |
end if; |
| 462 |
end; |
| 463 |
end if; |
| 464 |
|
| 465 |
-- Convert parameter to the required type (i.e. the type of the |
| 466 |
-- corresponding parameter), and call the appropriate routine. |
| 467 |
-- We set the Conversion_OK flag in case the type is private. |
| 468 |
|
| 469 |
declare |
| 470 |
Libent : constant Entity_Id := RTE (Lib_RE); |
| 471 |
Conv : constant Node_Id := |
| 472 |
OK_Convert_To |
| 473 |
(Etype (Next_Formal (First_Formal (Libent))), |
| 474 |
Relocate_Node (Item)); |
| 475 |
begin |
| 476 |
-- Do not output string delimiters if this is part of an |
| 477 |
-- interpolated string literal. |
| 478 |
|
| 479 |
if Nkind (Parent (N)) = N_Expression_With_Actions |
| 480 |
and then Nkind (Original_Node (Parent (N))) |
| 481 |
= N_Interpolated_String_Literal |
| 482 |
then |
| 483 |
return |
| 484 |
Make_Procedure_Call_Statement (Loc, |
| 485 |
Name => New_Occurrence_Of (Libent, Loc), |
| 486 |
Parameter_Associations => New_List ( |
| 487 |
Relocate_Node (Sink), |
| 488 |
Conv, |
| 489 |
New_Occurrence_Of (Stand.Standard_False, Loc))); |
| 490 |
else |
| 491 |
return |
| 492 |
Make_Procedure_Call_Statement (Loc, |
| 493 |
Name => New_Occurrence_Of (Libent, Loc), |
| 494 |
Parameter_Associations => New_List ( |
| 495 |
Relocate_Node (Sink), |
| 496 |
Conv)); |
| 497 |
end if; |
| 498 |
end; |
| 499 |
end Build_String_Put_Image_Call; |
| 500 |
|
| 501 |
------------------------------------ |
| 502 |
-- Build_Protected_Put_Image_Call -- |
| 503 |
------------------------------------ |
| 504 |
|
| 505 |
-- For "Protected_Type'Put_Image (S, Protected_Object)", build: |
| 506 |
-- |
| 507 |
-- Put_Image_Protected (S); |
| 508 |
-- |
| 509 |
-- The protected object is not passed. |
| 510 |
|
| 511 |
function Build_Protected_Put_Image_Call (N : Node_Id) return Node_Id is |
| 512 |
Loc : constant Source_Ptr := Sloc (N); |
| 513 |
Sink : constant Node_Id := First (Expressions (N)); |
| 514 |
Lib_RE : constant RE_Id := RE_Put_Image_Protected; |
| 515 |
Libent : constant Entity_Id := RTE (Lib_RE); |
| 516 |
begin |
| 517 |
return |
| 518 |
Make_Procedure_Call_Statement (Loc, |
| 519 |
Name => New_Occurrence_Of (Libent, Loc), |
| 520 |
Parameter_Associations => New_List ( |
| 521 |
Relocate_Node (Sink))); |
| 522 |
end Build_Protected_Put_Image_Call; |
| 523 |
|
| 524 |
------------------------------- |
| 525 |
-- Build_Task_Put_Image_Call -- |
| 526 |
------------------------------- |
| 527 |
|
| 528 |
-- For "Task_Type'Put_Image (S, Task_Object)", build: |
| 529 |
-- |
| 530 |
-- Put_Image_Task (S, Task_Object'Identity); |
| 531 |
-- |
| 532 |
-- The task object is not passed; its Task_Id is. |
| 533 |
|
| 534 |
function Build_Task_Put_Image_Call (N : Node_Id) return Node_Id is |
| 535 |
Loc : constant Source_Ptr := Sloc (N); |
| 536 |
Sink : constant Node_Id := First (Expressions (N)); |
| 537 |
Item : constant Node_Id := Next (Sink); |
| 538 |
Lib_RE : constant RE_Id := RE_Put_Image_Task; |
| 539 |
Libent : constant Entity_Id := RTE (Lib_RE); |
| 540 |
|
| 541 |
Task_Id : constant Node_Id := |
| 542 |
Make_Attribute_Reference (Loc, |
| 543 |
Prefix => Relocate_Node (Item), |
| 544 |
Attribute_Name => Name_Identity, |
| 545 |
Expressions => No_List); |
| 546 |
|
| 547 |
begin |
| 548 |
return |
| 549 |
Make_Procedure_Call_Statement (Loc, |
| 550 |
Name => New_Occurrence_Of (Libent, Loc), |
| 551 |
Parameter_Associations => New_List ( |
| 552 |
Relocate_Node (Sink), |
| 553 |
Task_Id)); |
| 554 |
end Build_Task_Put_Image_Call; |
| 555 |
|
| 556 |
-------------------------------------- |
| 557 |
-- Build_Record_Put_Image_Procedure -- |
| 558 |
-------------------------------------- |
| 559 |
|
| 560 |
-- The form of the record Put_Image procedure is as shown by the |
| 561 |
-- following example: |
| 562 |
|
| 563 |
-- procedure Put_Image (S : in out Sink'Class; V : Typ) is |
| 564 |
-- begin |
| 565 |
-- Component_Type'Put_Image (S, V.component); |
| 566 |
-- Component_Type'Put_Image (S, V.component); |
| 567 |
-- ... |
| 568 |
-- Component_Type'Put_Image (S, V.component); |
| 569 |
-- |
| 570 |
-- case V.discriminant is |
| 571 |
-- when choices => |
| 572 |
-- Component_Type'Put_Image (S, V.component); |
| 573 |
-- Component_Type'Put_Image (S, V.component); |
| 574 |
-- ... |
| 575 |
-- Component_Type'Put_Image (S, V.component); |
| 576 |
-- |
| 577 |
-- when choices => |
| 578 |
-- Component_Type'Put_Image (S, V.component); |
| 579 |
-- Component_Type'Put_Image (S, V.component); |
| 580 |
-- ... |
| 581 |
-- Component_Type'Put_Image (S, V.component); |
| 582 |
-- ... |
| 583 |
-- end case; |
| 584 |
-- end Put_Image; |
| 585 |
|
| 586 |
procedure Build_Record_Put_Image_Procedure |
| 587 |
(Typ : Entity_Id; |
| 588 |
Decl : out Node_Id; |
| 589 |
Pnam : out Entity_Id) |
| 590 |
is |
| 591 |
Loc : constant Source_Ptr := Sloc (Typ); |
| 592 |
Btyp : constant Entity_Id := Base_Type (Typ); |
| 593 |
pragma Assert (not Is_Class_Wide_Type (Btyp)); |
| 594 |
pragma Assert (not Is_Unchecked_Union (Btyp)); |
| 595 |
|
| 596 |
First_Time : Boolean := True; |
| 597 |
|
| 598 |
function Make_Component_List_Attributes (CL : Node_Id) return List_Id; |
| 599 |
-- Returns a sequence of Component_Type'Put_Image attribute_references |
| 600 |
-- to process the components that are referenced in the given component |
| 601 |
-- list. Called for the main component list, and then recursively for |
| 602 |
-- variants. |
| 603 |
|
| 604 |
function Make_Component_Attributes (Clist : List_Id) return List_Id; |
| 605 |
-- Given Clist, a component items list, construct series of |
| 606 |
-- Component_Type'Put_Image attribute_references for componentwise |
| 607 |
-- processing of the corresponding components. Called for the |
| 608 |
-- discriminants, and then from Make_Component_List_Attributes for each |
| 609 |
-- list (including in variants). |
| 610 |
|
| 611 |
procedure Append_Component_Attr (Clist : List_Id; C : Entity_Id); |
| 612 |
-- Given C, the entity for a discriminant or component, build a call to |
| 613 |
-- Component_Type'Put_Image for the corresponding component value, and |
| 614 |
-- append it onto Clist. Called from Make_Component_Attributes. |
| 615 |
|
| 616 |
function Make_Component_Name (C : Entity_Id) return Node_Id; |
| 617 |
-- Create a call that prints "Comp_Name => " |
| 618 |
|
| 619 |
function Null_Record_Default_Implementation_OK |
| 620 |
(Null_Record_Type : Entity_Id) return Boolean |
| 621 |
is |
| 622 |
(if Has_Aspect (Null_Record_Type, Aspect_Put_Image) |
| 623 |
then False |
| 624 |
elsif not Is_Derived_Type |
| 625 |
(Implementation_Base_Type (Null_Record_Type)) |
| 626 |
then True |
| 627 |
else Null_Record_Default_Implementation_OK |
| 628 |
(Implementation_Base_Type (Etype (Null_Record_Type)))); |
| 629 |
-- return True iff ok to emit "(NULL RECORD)" for given null record type |
| 630 |
|
| 631 |
------------------------------------ |
| 632 |
-- Make_Component_List_Attributes -- |
| 633 |
------------------------------------ |
| 634 |
|
| 635 |
function Make_Component_List_Attributes (CL : Node_Id) return List_Id is |
| 636 |
CI : constant List_Id := Component_Items (CL); |
| 637 |
VP : constant Node_Id := Variant_Part (CL); |
| 638 |
|
| 639 |
Result : List_Id; |
| 640 |
Alts : List_Id; |
| 641 |
V : Node_Id; |
| 642 |
DC : Node_Id; |
| 643 |
DCH : List_Id; |
| 644 |
D_Ref : Node_Id; |
| 645 |
|
| 646 |
begin |
| 647 |
Result := Make_Component_Attributes (CI); |
| 648 |
|
| 649 |
if Present (VP) then |
| 650 |
Alts := New_List; |
| 651 |
|
| 652 |
V := First_Non_Pragma (Variants (VP)); |
| 653 |
while Present (V) loop |
| 654 |
DCH := New_List; |
| 655 |
|
| 656 |
DC := First (Discrete_Choices (V)); |
| 657 |
while Present (DC) loop |
| 658 |
Append_To (DCH, New_Copy_Tree (DC)); |
| 659 |
Next (DC); |
| 660 |
end loop; |
| 661 |
|
| 662 |
Append_To (Alts, |
| 663 |
Make_Case_Statement_Alternative (Loc, |
| 664 |
Discrete_Choices => DCH, |
| 665 |
Statements => |
| 666 |
Make_Component_List_Attributes (Component_List (V)))); |
| 667 |
Next_Non_Pragma (V); |
| 668 |
end loop; |
| 669 |
|
| 670 |
-- Note: in the following, we use New_Occurrence_Of for the |
| 671 |
-- selector, since there are cases in which we make a reference |
| 672 |
-- to a hidden discriminant that is not visible. |
| 673 |
|
| 674 |
D_Ref := |
| 675 |
Make_Selected_Component (Loc, |
| 676 |
Prefix => Make_Identifier (Loc, Name_V), |
| 677 |
Selector_Name => |
| 678 |
New_Occurrence_Of (Entity (Name (VP)), Loc)); |
| 679 |
|
| 680 |
Append_To (Result, |
| 681 |
Make_Case_Statement (Loc, |
| 682 |
Expression => D_Ref, |
| 683 |
Alternatives => Alts)); |
| 684 |
end if; |
| 685 |
|
| 686 |
return Result; |
| 687 |
end Make_Component_List_Attributes; |
| 688 |
|
| 689 |
--------------------------- |
| 690 |
-- Append_Component_Attr -- |
| 691 |
--------------------------- |
| 692 |
|
| 693 |
procedure Append_Component_Attr (Clist : List_Id; C : Entity_Id) is |
| 694 |
Component_Typ : constant Entity_Id := |
| 695 |
Put_Image_Base_Type |
| 696 |
(Get_Corresponding_Mutably_Tagged_Type_If_Present (Etype (C))); |
| 697 |
begin |
| 698 |
Append_To (Clist, |
| 699 |
Make_Attribute_Reference (Loc, |
| 700 |
Prefix => New_Occurrence_Of (Component_Typ, Loc), |
| 701 |
Attribute_Name => Name_Put_Image, |
| 702 |
Expressions => New_List ( |
| 703 |
Make_Identifier (Loc, Name_S), |
| 704 |
Make_Selected_Component (Loc, |
| 705 |
Prefix => Make_Identifier (Loc, Name_V), |
| 706 |
Selector_Name => New_Occurrence_Of (C, Loc))))); |
| 707 |
end Append_Component_Attr; |
| 708 |
|
| 709 |
------------------------------- |
| 710 |
-- Make_Component_Attributes -- |
| 711 |
------------------------------- |
| 712 |
|
| 713 |
function Make_Component_Attributes (Clist : List_Id) return List_Id is |
| 714 |
Item : Node_Id; |
| 715 |
Result : List_Id; |
| 716 |
|
| 717 |
begin |
| 718 |
Result := New_List; |
| 719 |
|
| 720 |
if Present (Clist) then |
| 721 |
Item := First (Clist); |
| 722 |
|
| 723 |
-- Loop through components, skipping all internal components, |
| 724 |
-- which are not part of the value (e.g. _Tag), except that we |
| 725 |
-- don't skip the _Parent, since we do want to process that |
| 726 |
-- recursively. |
| 727 |
|
| 728 |
while Present (Item) loop |
| 729 |
if Nkind (Item) in |
| 730 |
N_Component_Declaration | N_Discriminant_Specification |
| 731 |
then |
| 732 |
if Chars (Defining_Identifier (Item)) = Name_uParent then |
| 733 |
declare |
| 734 |
Parent_Type : constant Entity_Id := |
| 735 |
Implementation_Base_Type |
| 736 |
(Etype (Defining_Identifier (Item))); |
| 737 |
|
| 738 |
Parent_Aspect_Spec : constant Node_Id := |
| 739 |
Find_Aspect (Parent_Type, Aspect_Put_Image); |
| 740 |
|
| 741 |
Parent_Type_Decl : constant Node_Id := |
| 742 |
Declaration_Node (Parent_Type); |
| 743 |
|
| 744 |
Parent_Rdef : Node_Id := |
| 745 |
Type_Definition (Parent_Type_Decl); |
| 746 |
begin |
| 747 |
-- If parent type has an noninherited |
| 748 |
-- explicitly-specified Put_Image aspect spec, then |
| 749 |
-- display parent part by calling specified procedure, |
| 750 |
-- and then use extension-aggregate syntax for the |
| 751 |
-- remaining components as per RM 4.10(15/5); |
| 752 |
-- otherwise, "look through" the parent component |
| 753 |
-- to its components - we don't want the image text |
| 754 |
-- to include mention of an "_parent" component. |
| 755 |
|
| 756 |
if Present (Parent_Aspect_Spec) and then |
| 757 |
Entity (Parent_Aspect_Spec) = Parent_Type |
| 758 |
then |
| 759 |
Append_Component_Attr |
| 760 |
(Result, Defining_Identifier (Item)); |
| 761 |
|
| 762 |
-- Omit the " with " if no subsequent components. |
| 763 |
|
| 764 |
if not Is_Null_Extension_Of |
| 765 |
(Descendant => Typ, |
| 766 |
Ancestor => Parent_Type) |
| 767 |
then |
| 768 |
Append_To (Result, |
| 769 |
Make_Procedure_Call_Statement (Loc, |
| 770 |
Name => |
| 771 |
New_Occurrence_Of |
| 772 |
(RTE (RE_Put_UTF_8), Loc), |
| 773 |
Parameter_Associations => New_List |
| 774 |
(Make_Identifier (Loc, Name_S), |
| 775 |
Make_String_Literal (Loc, " with ")))); |
| 776 |
end if; |
| 777 |
else |
| 778 |
if Nkind (Parent_Rdef) = N_Derived_Type_Definition |
| 779 |
then |
| 780 |
Parent_Rdef := |
| 781 |
Record_Extension_Part (Parent_Rdef); |
| 782 |
end if; |
| 783 |
|
| 784 |
if Present (Component_List (Parent_Rdef)) then |
| 785 |
Append_List_To (Result, |
| 786 |
Make_Component_List_Attributes |
| 787 |
(Component_List (Parent_Rdef))); |
| 788 |
end if; |
| 789 |
end if; |
| 790 |
end; |
| 791 |
|
| 792 |
elsif not Is_Internal_Name |
| 793 |
(Chars (Defining_Identifier (Item))) |
| 794 |
then |
| 795 |
if First_Time then |
| 796 |
First_Time := False; |
| 797 |
else |
| 798 |
Append_To (Result, |
| 799 |
Make_Procedure_Call_Statement (Loc, |
| 800 |
Name => |
| 801 |
New_Occurrence_Of (RTE (RE_Record_Between), Loc), |
| 802 |
Parameter_Associations => New_List |
| 803 |
(Make_Identifier (Loc, Name_S)))); |
| 804 |
end if; |
| 805 |
|
| 806 |
Append_To (Result, Make_Component_Name (Item)); |
| 807 |
Append_Component_Attr |
| 808 |
(Result, Defining_Identifier (Item)); |
| 809 |
end if; |
| 810 |
end if; |
| 811 |
|
| 812 |
Next (Item); |
| 813 |
end loop; |
| 814 |
end if; |
| 815 |
|
| 816 |
return Result; |
| 817 |
end Make_Component_Attributes; |
| 818 |
|
| 819 |
------------------------- |
| 820 |
-- Make_Component_Name -- |
| 821 |
------------------------- |
| 822 |
|
| 823 |
function Make_Component_Name (C : Entity_Id) return Node_Id is |
| 824 |
Name : constant Name_Id := Chars (Defining_Identifier (C)); |
| 825 |
pragma Assert (Name /= Name_uParent); |
| 826 |
|
| 827 |
function To_Upper (S : String) return String; |
| 828 |
-- Same as Ada.Characters.Handling.To_Upper, but withing |
| 829 |
-- Ada.Characters.Handling seems to cause mailserver problems. |
| 830 |
|
| 831 |
-------------- |
| 832 |
-- To_Upper -- |
| 833 |
-------------- |
| 834 |
|
| 835 |
function To_Upper (S : String) return String is |
| 836 |
begin |
| 837 |
return Result : String := S do |
| 838 |
for Char of Result loop |
| 839 |
Char := Fold_Upper (Char); |
| 840 |
end loop; |
| 841 |
end return; |
| 842 |
end To_Upper; |
| 843 |
|
| 844 |
-- Start of processing for Make_Component_Name |
| 845 |
|
| 846 |
begin |
| 847 |
return |
| 848 |
Make_Procedure_Call_Statement (Loc, |
| 849 |
Name => New_Occurrence_Of (RTE (RE_Put_UTF_8), Loc), |
| 850 |
Parameter_Associations => New_List |
| 851 |
(Make_Identifier (Loc, Name_S), |
| 852 |
Make_String_Literal (Loc, |
| 853 |
To_Upper (Get_Name_String (Name)) & " => "))); |
| 854 |
end Make_Component_Name; |
| 855 |
|
| 856 |
Stms : constant List_Id := New_List; |
| 857 |
Rdef : Node_Id; |
| 858 |
Type_Decl : constant Node_Id := |
| 859 |
Declaration_Node (Base_Type (Underlying_Type (Btyp))); |
| 860 |
|
| 861 |
-- Start of processing for Build_Record_Put_Image_Procedure |
| 862 |
|
| 863 |
begin |
| 864 |
if Ada_Version < Ada_2022 |
| 865 |
or else not Put_Image_Enabled (Btyp) |
| 866 |
then |
| 867 |
-- generate a very simple Put_Image implementation |
| 868 |
|
| 869 |
if Is_RTE (Typ, RE_Root_Buffer_Type) then |
| 870 |
-- Avoid introducing a cyclic dependency between |
| 871 |
-- Ada.Strings.Text_Buffers and System.Put_Images. |
| 872 |
|
| 873 |
Append_To (Stms, |
| 874 |
Make_Raise_Program_Error (Loc, |
| 875 |
Reason => PE_Explicit_Raise)); |
| 876 |
else |
| 877 |
declare |
| 878 |
Type_Name : String_Id; |
| 879 |
begin |
| 880 |
-- If aspect Discard_Names is enabled the intention is to |
| 881 |
-- prevent type names from leaking into object file. Instead, |
| 882 |
-- we emit string that is different from the ones from the |
| 883 |
-- default implementations of the Put_Image attribute. |
| 884 |
|
| 885 |
if Global_Discard_Names or else Discard_Names (Typ) then |
| 886 |
Start_String; |
| 887 |
Store_String_Chars ("(DISCARDED TYPE NAME)"); |
| 888 |
Type_Name := End_String; |
| 889 |
else |
| 890 |
Type_Name := |
| 891 |
Fully_Qualified_Name_String (Btyp, Append_NUL => False); |
| 892 |
end if; |
| 893 |
|
| 894 |
Append_To (Stms, |
| 895 |
Make_Procedure_Call_Statement (Loc, |
| 896 |
Name => New_Occurrence_Of (RTE (RE_Put_Image_Unknown), Loc), |
| 897 |
Parameter_Associations => New_List |
| 898 |
(Make_Identifier (Loc, Name_S), |
| 899 |
Make_String_Literal (Loc, |
| 900 |
Type_Name)))); |
| 901 |
end; |
| 902 |
end if; |
| 903 |
|
| 904 |
elsif Is_Null_Record_Type (Btyp, Ignore_Privacy => True) |
| 905 |
and then Null_Record_Default_Implementation_OK (Btyp) |
| 906 |
then |
| 907 |
|
| 908 |
-- Interface types take this path. |
| 909 |
|
| 910 |
Append_To (Stms, |
| 911 |
Make_Procedure_Call_Statement (Loc, |
| 912 |
Name => New_Occurrence_Of (RTE (RE_Put_UTF_8), Loc), |
| 913 |
Parameter_Associations => New_List |
| 914 |
(Make_Identifier (Loc, Name_S), |
| 915 |
Make_String_Literal (Loc, "(NULL RECORD)")))); |
| 916 |
|
| 917 |
elsif Is_Derived_Type (Btyp) |
| 918 |
and then (not Is_Tagged_Type (Btyp) or else Is_Null_Extension (Btyp)) |
| 919 |
then |
| 920 |
declare |
| 921 |
Parent_Type : constant Entity_Id := Base_Type (Etype (Btyp)); |
| 922 |
begin |
| 923 |
Append_To (Stms, |
| 924 |
Make_Attribute_Reference (Loc, |
| 925 |
Prefix => New_Occurrence_Of (Parent_Type, Loc), |
| 926 |
Attribute_Name => Name_Put_Image, |
| 927 |
Expressions => New_List ( |
| 928 |
Make_Identifier (Loc, Name_S), |
| 929 |
Make_Type_Conversion (Loc, |
| 930 |
Subtype_Mark => New_Occurrence_Of |
| 931 |
(Parent_Type, Loc), |
| 932 |
Expression => Make_Identifier |
| 933 |
(Loc, Name_V))))); |
| 934 |
end; |
| 935 |
|
| 936 |
else |
| 937 |
Append_To (Stms, |
| 938 |
Make_Procedure_Call_Statement (Loc, |
| 939 |
Name => New_Occurrence_Of (RTE (RE_Record_Before), Loc), |
| 940 |
Parameter_Associations => New_List |
| 941 |
(Make_Identifier (Loc, Name_S)))); |
| 942 |
|
| 943 |
-- Generate Put_Images for the discriminants of the type |
| 944 |
|
| 945 |
declare |
| 946 |
Discrim_Specs : List_Id := Discriminant_Specifications (Type_Decl); |
| 947 |
Partial_View : Entity_Id; |
| 948 |
begin |
| 949 |
if Present (First (Discrim_Specs)) |
| 950 |
and then Ekind (Defining_Identifier (First (Discrim_Specs))) = |
| 951 |
E_Void |
| 952 |
then |
| 953 |
-- If the known discriminant part is repeated for the |
| 954 |
-- completion of a private type declaration, then the |
| 955 |
-- second copy is (by design) not analyzed. So we'd better |
| 956 |
-- use the first copy instead. |
| 957 |
|
| 958 |
Partial_View := Incomplete_Or_Partial_View |
| 959 |
(Defining_Identifier (Type_Decl), |
| 960 |
Partial_Only => True); |
| 961 |
|
| 962 |
pragma Assert (Ekind (Partial_View) in |
| 963 |
E_Private_Type |
| 964 |
| E_Limited_Private_Type |
| 965 |
| E_Record_Type_With_Private); |
| 966 |
|
| 967 |
Discrim_Specs := |
| 968 |
Discriminant_Specifications (Parent (Partial_View)); |
| 969 |
|
| 970 |
pragma Assert (Present (First (Discrim_Specs))); |
| 971 |
pragma Assert |
| 972 |
(Ekind (Defining_Identifier (First (Discrim_Specs))) /= |
| 973 |
E_Void); |
| 974 |
end if; |
| 975 |
|
| 976 |
Append_List_To (Stms, Make_Component_Attributes (Discrim_Specs)); |
| 977 |
end; |
| 978 |
|
| 979 |
Rdef := Type_Definition (Type_Decl); |
| 980 |
|
| 981 |
-- In the record extension case, the components we want are to be |
| 982 |
-- found in the extension (although we have to process the |
| 983 |
-- _Parent component to find inherited components). |
| 984 |
|
| 985 |
if Nkind (Rdef) = N_Derived_Type_Definition then |
| 986 |
Rdef := Record_Extension_Part (Rdef); |
| 987 |
end if; |
| 988 |
|
| 989 |
if Present (Component_List (Rdef)) then |
| 990 |
Append_List_To (Stms, |
| 991 |
Make_Component_List_Attributes (Component_List (Rdef))); |
| 992 |
end if; |
| 993 |
|
| 994 |
Append_To (Stms, |
| 995 |
Make_Procedure_Call_Statement (Loc, |
| 996 |
Name => New_Occurrence_Of (RTE (RE_Record_After), Loc), |
| 997 |
Parameter_Associations => New_List |
| 998 |
(Make_Identifier (Loc, Name_S)))); |
| 999 |
end if; |
| 1000 |
|
| 1001 |
Pnam := Make_Put_Image_Name (Loc, Btyp); |
| 1002 |
Build_Put_Image_Proc (Loc, Btyp, Decl, Pnam, Stms); |
| 1003 |
end Build_Record_Put_Image_Procedure; |
| 1004 |
|
| 1005 |
----------------------------- |
| 1006 |
-- Build_Put_Image_Profile -- |
| 1007 |
----------------------------- |
| 1008 |
|
| 1009 |
function Build_Put_Image_Profile |
| 1010 |
(Loc : Source_Ptr; Typ : Entity_Id) return List_Id |
| 1011 |
is |
| 1012 |
begin |
| 1013 |
return New_List ( |
| 1014 |
Make_Parameter_Specification (Loc, |
| 1015 |
Defining_Identifier => Make_Defining_Identifier (Loc, Name_S), |
| 1016 |
In_Present => True, |
| 1017 |
Out_Present => True, |
| 1018 |
Parameter_Type => |
| 1019 |
New_Occurrence_Of |
| 1020 |
(Class_Wide_Type (RTE (RE_Root_Buffer_Type)), Loc)), |
| 1021 |
|
| 1022 |
Make_Parameter_Specification (Loc, |
| 1023 |
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V), |
| 1024 |
Parameter_Type => New_Occurrence_Of (Typ, Loc))); |
| 1025 |
end Build_Put_Image_Profile; |
| 1026 |
|
| 1027 |
-------------------------- |
| 1028 |
-- Build_Put_Image_Proc -- |
| 1029 |
-------------------------- |
| 1030 |
|
| 1031 |
procedure Build_Put_Image_Proc |
| 1032 |
(Loc : Source_Ptr; |
| 1033 |
Typ : Entity_Id; |
| 1034 |
Decl : out Node_Id; |
| 1035 |
Pnam : Entity_Id; |
| 1036 |
Stms : List_Id) |
| 1037 |
is |
| 1038 |
Spec : constant Node_Id := |
| 1039 |
Make_Procedure_Specification (Loc, |
| 1040 |
Defining_Unit_Name => Pnam, |
| 1041 |
Parameter_Specifications => Build_Put_Image_Profile (Loc, Typ)); |
| 1042 |
begin |
| 1043 |
Decl := |
| 1044 |
Make_Subprogram_Body (Loc, |
| 1045 |
Specification => Spec, |
| 1046 |
Declarations => Empty_List, |
| 1047 |
Handled_Statement_Sequence => |
| 1048 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 1049 |
Statements => Stms)); |
| 1050 |
end Build_Put_Image_Proc; |
| 1051 |
|
| 1052 |
---------------------------------- |
| 1053 |
-- Build_Unknown_Put_Image_Call -- |
| 1054 |
---------------------------------- |
| 1055 |
|
| 1056 |
function Build_Unknown_Put_Image_Call (N : Node_Id) return Node_Id is |
| 1057 |
Loc : constant Source_Ptr := Sloc (N); |
| 1058 |
Sink : constant Node_Id := First (Expressions (N)); |
| 1059 |
Lib_RE : constant RE_Id := RE_Put_Image_Unknown; |
| 1060 |
Libent : constant Entity_Id := RTE (Lib_RE); |
| 1061 |
begin |
| 1062 |
return |
| 1063 |
Make_Procedure_Call_Statement (Loc, |
| 1064 |
Name => New_Occurrence_Of (Libent, Loc), |
| 1065 |
Parameter_Associations => New_List ( |
| 1066 |
Relocate_Node (Sink), |
| 1067 |
Make_String_Literal (Loc, |
| 1068 |
Exp_Util.Fully_Qualified_Name_String ( |
| 1069 |
Entity (Prefix (N)), Append_NUL => False)))); |
| 1070 |
end Build_Unknown_Put_Image_Call; |
| 1071 |
|
| 1072 |
----------------------- |
| 1073 |
-- Put_Image_Enabled -- |
| 1074 |
----------------------- |
| 1075 |
|
| 1076 |
function Put_Image_Enabled (Typ : Entity_Id) return Boolean is |
| 1077 |
begin |
| 1078 |
-- If this function returns False for a non-scalar type Typ, then |
| 1079 |
-- a) calls to Typ'Image will result in calls to |
| 1080 |
-- System.Put_Images.Put_Image_Unknown to generate the image. |
| 1081 |
-- b) If Typ is a tagged type, then similarly the implementation |
| 1082 |
-- of Typ's Put_Image procedure will call Put_Image_Unknown |
| 1083 |
-- and will ignore its formal parameter of type Typ. |
| 1084 |
-- Note that Typ will still have a Put_Image procedure |
| 1085 |
-- in this case, albeit one with a simplified implementation. |
| 1086 |
-- |
| 1087 |
-- The name "Sink" here is a short nickname for |
| 1088 |
-- "Ada.Strings.Text_Buffers.Root_Buffer_Type". |
| 1089 |
-- |
| 1090 |
|
| 1091 |
-- Put_Image does not work for Remote_Types. We check the containing |
| 1092 |
-- package, rather than the type itself, because we want to include |
| 1093 |
-- types in the private part of a Remote_Types package. |
| 1094 |
|
| 1095 |
if Is_Remote_Types (Scope (Typ)) |
| 1096 |
or else Is_Remote_Call_Interface (Typ) |
| 1097 |
then |
| 1098 |
return False; |
| 1099 |
end if; |
| 1100 |
|
| 1101 |
-- No sense in generating code for Put_Image if there are errors. This |
| 1102 |
-- avoids certain cascade errors. |
| 1103 |
|
| 1104 |
if Total_Errors_Detected > 0 then |
| 1105 |
return False; |
| 1106 |
end if; |
| 1107 |
|
| 1108 |
-- If type Sink is unavailable in this runtime, disable Put_Image |
| 1109 |
-- altogether. |
| 1110 |
|
| 1111 |
if No_Run_Time_Mode or else not RTE_Available (RE_Root_Buffer_Type) then |
| 1112 |
return False; |
| 1113 |
end if; |
| 1114 |
|
| 1115 |
if Is_Tagged_Type (Typ) then |
| 1116 |
if Is_Class_Wide_Type (Typ) then |
| 1117 |
return Put_Image_Enabled (Find_Specific_Type (Base_Type (Typ))); |
| 1118 |
elsif Present (Find_Aspect (Typ, Aspect_Put_Image, |
| 1119 |
Or_Rep_Item => True)) |
| 1120 |
then |
| 1121 |
null; |
| 1122 |
elsif Is_Derived_Type (Typ) then |
| 1123 |
return Put_Image_Enabled (Etype (Base_Type (Typ))); |
| 1124 |
elsif Is_Predefined_Unit (Get_Code_Unit (Typ)) then |
| 1125 |
return False; |
| 1126 |
end if; |
| 1127 |
end if; |
| 1128 |
|
| 1129 |
-- ???Disable Put_Image on type Root_Buffer_Type declared in |
| 1130 |
-- Ada.Strings.Text_Buffers. Note that we can't call Is_RTU on |
| 1131 |
-- Ada_Strings_Text_Buffers, because it's not known yet (we might be |
| 1132 |
-- compiling it). But this is insufficient to allow support for tagged |
| 1133 |
-- predefined types. |
| 1134 |
|
| 1135 |
declare |
| 1136 |
Parent_Scope : constant Entity_Id := Scope (Scope (Typ)); |
| 1137 |
begin |
| 1138 |
if Present (Parent_Scope) |
| 1139 |
and then Is_RTU (Parent_Scope, Ada_Strings) |
| 1140 |
and then Chars (Scope (Typ)) = Name_Find ("text_buffers") |
| 1141 |
then |
| 1142 |
return False; |
| 1143 |
end if; |
| 1144 |
end; |
| 1145 |
|
| 1146 |
-- Disable for CPP types, because the components are unavailable on the |
| 1147 |
-- Ada side. |
| 1148 |
|
| 1149 |
if Is_Tagged_Type (Typ) |
| 1150 |
and then Convention (Typ) = Convention_CPP |
| 1151 |
and then Is_CPP_Class (Root_Type (Typ)) |
| 1152 |
then |
| 1153 |
return False; |
| 1154 |
end if; |
| 1155 |
|
| 1156 |
-- Disable for unchecked unions, because there is no way to know the |
| 1157 |
-- discriminant value, and therefore no way to know which components |
| 1158 |
-- should be printed. |
| 1159 |
|
| 1160 |
if Is_Unchecked_Union (Typ) then |
| 1161 |
return False; |
| 1162 |
end if; |
| 1163 |
|
| 1164 |
return True; |
| 1165 |
end Put_Image_Enabled; |
| 1166 |
|
| 1167 |
------------------------- |
| 1168 |
-- Make_Put_Image_Name -- |
| 1169 |
------------------------- |
| 1170 |
|
| 1171 |
function Make_Put_Image_Name |
| 1172 |
(Loc : Source_Ptr; Typ : Entity_Id) return Entity_Id |
| 1173 |
is |
| 1174 |
Sname : Name_Id; |
| 1175 |
begin |
| 1176 |
-- For tagged types, we are dealing with a TSS associated with the |
| 1177 |
-- declaration, so we use the standard primitive function name. For |
| 1178 |
-- other types, generate a local TSS name since we are generating |
| 1179 |
-- the subprogram at the point of use. |
| 1180 |
|
| 1181 |
if Is_Tagged_Type (Typ) then |
| 1182 |
Sname := Make_TSS_Name (Typ, TSS_Put_Image); |
| 1183 |
else |
| 1184 |
Sname := Make_TSS_Name_Local (Typ, TSS_Put_Image); |
| 1185 |
end if; |
| 1186 |
|
| 1187 |
return Make_Defining_Identifier (Loc, Sname); |
| 1188 |
end Make_Put_Image_Name; |
| 1189 |
|
| 1190 |
--------------------------------- |
| 1191 |
-- Image_Should_Call_Put_Image -- |
| 1192 |
--------------------------------- |
| 1193 |
|
| 1194 |
function Image_Should_Call_Put_Image (N : Node_Id) return Boolean is |
| 1195 |
begin |
| 1196 |
if Ada_Version < Ada_2022 then |
| 1197 |
return False; |
| 1198 |
end if; |
| 1199 |
|
| 1200 |
-- In Ada 2022, T'Image calls T'Put_Image if there is an explicit |
| 1201 |
-- (or inherited) aspect_specification for Put_Image, or if |
| 1202 |
-- U_Type'Image is illegal in pre-2022 versions of Ada. |
| 1203 |
|
| 1204 |
declare |
| 1205 |
U_Type : constant Entity_Id := Underlying_Type (Entity (Prefix (N))); |
| 1206 |
begin |
| 1207 |
if Has_Aspect (U_Type, Aspect_Put_Image) |
| 1208 |
or else not Is_Scalar_Type (U_Type) |
| 1209 |
then |
| 1210 |
return True; |
| 1211 |
end if; |
| 1212 |
|
| 1213 |
-- Deal with Itypes. One case where this is needed is for a |
| 1214 |
-- fixed-point type with a Put_Image aspect specification. |
| 1215 |
|
| 1216 |
-- ??? Should we be checking for Itype case here, or in Has_Aspect? |
| 1217 |
-- In other words, do we want to do what we are doing here for all |
| 1218 |
-- aspects, not just for Put_Image? |
| 1219 |
|
| 1220 |
if Is_Itype (U_Type) |
| 1221 |
and then Nkind (Associated_Node_For_Itype (U_Type)) in |
| 1222 |
N_Full_Type_Declaration | N_Subtype_Declaration |
| 1223 |
and then Has_Aspect (Defining_Identifier |
| 1224 |
(Associated_Node_For_Itype (U_Type)), |
| 1225 |
Aspect_Put_Image) |
| 1226 |
then |
| 1227 |
return True; |
| 1228 |
end if; |
| 1229 |
|
| 1230 |
return False; |
| 1231 |
end; |
| 1232 |
end Image_Should_Call_Put_Image; |
| 1233 |
|
| 1234 |
---------------------- |
| 1235 |
-- Build_Image_Call -- |
| 1236 |
---------------------- |
| 1237 |
|
| 1238 |
function Build_Image_Call (N : Node_Id) return Node_Id is |
| 1239 |
-- For Typ'[[Wide_]Wide_]Image (X) generate: |
| 1240 |
-- |
| 1241 |
-- S : Buffer_Type; |
| 1242 |
-- U_Typ'Put_Image (S, X); |
| 1243 |
-- [[Wide_[Wide_]]Get (S) |
| 1244 |
-- |
| 1245 |
-- where U_Typ is the underlying type, as needed to bypass privacy. |
| 1246 |
|
| 1247 |
Loc : constant Source_Ptr := Sloc (N); |
| 1248 |
U_Typ : constant Entity_Id := Underlying_Type (Entity (Prefix (N))); |
| 1249 |
|
| 1250 |
Sink_Entity : constant Entity_Id := Make_Temporary (Loc, 'S'); |
| 1251 |
|
| 1252 |
Sink_Decl : constant Node_Id := |
| 1253 |
Make_Object_Declaration (Loc, |
| 1254 |
Defining_Identifier => Sink_Entity, |
| 1255 |
Object_Definition => |
| 1256 |
New_Occurrence_Of (RTE (RE_Buffer_Type), Loc)); |
| 1257 |
|
| 1258 |
Image_Prefix : constant Node_Id := |
| 1259 |
Duplicate_Subexpr (First (Expressions (N))); |
| 1260 |
|
| 1261 |
Put_Im : constant Node_Id := |
| 1262 |
Make_Attribute_Reference (Loc, |
| 1263 |
Prefix => New_Occurrence_Of (U_Typ, Loc), |
| 1264 |
Attribute_Name => Name_Put_Image, |
| 1265 |
Expressions => New_List ( |
| 1266 |
New_Occurrence_Of (Sink_Entity, Loc), |
| 1267 |
Image_Prefix)); |
| 1268 |
|
| 1269 |
Get_Func_Id : constant RE_Id := |
| 1270 |
(case Get_Attribute_Id (Attribute_Name (N)) is |
| 1271 |
when Attribute_Img => RE_Get, |
| 1272 |
when Attribute_Image => RE_Get, |
| 1273 |
when Attribute_Wide_Image => RE_Wide_Get, |
| 1274 |
when Attribute_Wide_Wide_Image => RE_Wide_Wide_Get, |
| 1275 |
when others => raise Program_Error); |
| 1276 |
|
| 1277 |
Actions : List_Id; |
| 1278 |
|
| 1279 |
function Put_String_Exp (String_Exp : Node_Id; |
| 1280 |
Wide_Wide : Boolean := False) return Node_Id; |
| 1281 |
-- Generate a call to evaluate a String (or Wide_Wide_String, depending |
| 1282 |
-- on the Wide_Wide Boolean parameter) expression and output it into |
| 1283 |
-- the buffer. |
| 1284 |
|
| 1285 |
-------------------- |
| 1286 |
-- Put_String_Exp -- |
| 1287 |
-------------------- |
| 1288 |
|
| 1289 |
function Put_String_Exp (String_Exp : Node_Id; |
| 1290 |
Wide_Wide : Boolean := False) return Node_Id is |
| 1291 |
Put_Id : constant RE_Id := |
| 1292 |
(if Wide_Wide then RE_Wide_Wide_Put else RE_Put_UTF_8); |
| 1293 |
|
| 1294 |
-- We could build a nondispatching call here, but to make |
| 1295 |
-- that work we'd have to change Rtsfind spec to make available |
| 1296 |
-- corresponding callees out of Ada.Strings.Text_Buffers.Unbounded |
| 1297 |
-- (as opposed to from Ada.Strings.Text_Buffers). Seems simpler to |
| 1298 |
-- introduce a type conversion and leave it to the optimizer to |
| 1299 |
-- eliminate the dispatching. This does not *introduce* any problems |
| 1300 |
-- if a no-dispatching-allowed restriction is in effect, since we |
| 1301 |
-- are already in the middle of generating a call to T'Class'Image. |
| 1302 |
|
| 1303 |
Sink_Exp : constant Node_Id := |
| 1304 |
Make_Type_Conversion (Loc, |
| 1305 |
Subtype_Mark => |
| 1306 |
New_Occurrence_Of |
| 1307 |
(Class_Wide_Type (RTE (RE_Root_Buffer_Type)), Loc), |
| 1308 |
Expression => New_Occurrence_Of (Sink_Entity, Loc)); |
| 1309 |
begin |
| 1310 |
return |
| 1311 |
Make_Procedure_Call_Statement (Loc, |
| 1312 |
Name => New_Occurrence_Of (RTE (Put_Id), Loc), |
| 1313 |
Parameter_Associations => New_List (Sink_Exp, String_Exp)); |
| 1314 |
end Put_String_Exp; |
| 1315 |
|
| 1316 |
-- Local variables |
| 1317 |
|
| 1318 |
Tag_Node : Node_Id; |
| 1319 |
|
| 1320 |
-- Start of processing for Build_Image_Call |
| 1321 |
|
| 1322 |
begin |
| 1323 |
if Is_Class_Wide_Type (U_Typ) then |
| 1324 |
|
| 1325 |
-- For interface types we must generate code to displace the pointer |
| 1326 |
-- to the object to reference the base of the underlying object. |
| 1327 |
|
| 1328 |
-- Generate: |
| 1329 |
-- To_Tag_Ptr (Image_Prefix'Address).all |
| 1330 |
|
| 1331 |
-- Note that Image_Prefix'Address is recursively expanded into a |
| 1332 |
-- call to Ada.Tags.Base_Address (Image_Prefix'Address). |
| 1333 |
|
| 1334 |
if Is_Interface (U_Typ) then |
| 1335 |
Tag_Node := |
| 1336 |
Make_Explicit_Dereference (Loc, |
| 1337 |
Unchecked_Convert_To (RTE (RE_Tag_Ptr), |
| 1338 |
Make_Attribute_Reference (Loc, |
| 1339 |
Prefix => Duplicate_Subexpr (Image_Prefix), |
| 1340 |
Attribute_Name => Name_Address))); |
| 1341 |
|
| 1342 |
-- Common case |
| 1343 |
|
| 1344 |
else |
| 1345 |
Tag_Node := |
| 1346 |
Make_Attribute_Reference (Loc, |
| 1347 |
Prefix => Duplicate_Subexpr (Image_Prefix), |
| 1348 |
Attribute_Name => Name_Tag); |
| 1349 |
end if; |
| 1350 |
|
| 1351 |
-- Generate qualified-expression syntax; qualification name comes |
| 1352 |
-- from calling Ada.Tags.Wide_Wide_Expanded_Name. |
| 1353 |
|
| 1354 |
declare |
| 1355 |
-- The copy of Image_Prefix will be evaluated before the |
| 1356 |
-- original, which is ok if no side effects are involved. |
| 1357 |
|
| 1358 |
pragma Assert (Side_Effect_Free (Image_Prefix)); |
| 1359 |
|
| 1360 |
Specific_Type_Name : constant Node_Id := |
| 1361 |
Put_String_Exp |
| 1362 |
(Make_Function_Call (Loc, |
| 1363 |
Name => New_Occurrence_Of |
| 1364 |
(RTE (RE_Wide_Wide_Expanded_Name), Loc), |
| 1365 |
Parameter_Associations => New_List (Tag_Node)), |
| 1366 |
Wide_Wide => True); |
| 1367 |
|
| 1368 |
Qualification : constant Node_Id := |
| 1369 |
Put_String_Exp (Make_String_Literal (Loc, "'")); |
| 1370 |
begin |
| 1371 |
Actions := New_List |
| 1372 |
(Sink_Decl, |
| 1373 |
Specific_Type_Name, |
| 1374 |
Qualification, |
| 1375 |
Put_Im); |
| 1376 |
end; |
| 1377 |
|
| 1378 |
else |
| 1379 |
Actions := New_List (Sink_Decl, Put_Im); |
| 1380 |
end if; |
| 1381 |
|
| 1382 |
Insert_Actions (N, Actions); |
| 1383 |
|
| 1384 |
return |
| 1385 |
Make_Function_Call (Loc, |
| 1386 |
Name => New_Occurrence_Of (RTE (Get_Func_Id), Loc), |
| 1387 |
Parameter_Associations => New_List ( |
| 1388 |
New_Occurrence_Of (Sink_Entity, Loc))); |
| 1389 |
end Build_Image_Call; |
| 1390 |
|
| 1391 |
------------------------------ |
| 1392 |
-- Preload_Root_Buffer_Type -- |
| 1393 |
------------------------------ |
| 1394 |
|
| 1395 |
Preload_Root_Buffer_Type_Done : Boolean := False; |
| 1396 |
-- True if Preload_Root_Buffer_Type has already done its work; |
| 1397 |
-- no need to do it again in that case. |
| 1398 |
|
| 1399 |
Debug_Unit_Walk : Boolean renames Debug_Flag_Dot_WW; |
| 1400 |
|
| 1401 |
procedure Preload_Root_Buffer_Type (Compilation_Unit : Node_Id) is |
| 1402 |
Ignore : Entity_Id; |
| 1403 |
begin |
| 1404 |
if Preload_Root_Buffer_Type_Done then |
| 1405 |
return; |
| 1406 |
end if; |
| 1407 |
|
| 1408 |
-- We can't call RTE (RE_Root_Buffer_Type) for at least some |
| 1409 |
-- predefined units, because it would introduce cyclic dependences. |
| 1410 |
-- The package where Root_Buffer_Type is declared, for example, and |
| 1411 |
-- things it depends on. |
| 1412 |
-- |
| 1413 |
-- It's only needed for tagged types, so don't do it unless Put_Image is |
| 1414 |
-- enabled for tagged types, and we've seen a tagged type. Note that |
| 1415 |
-- Tagged_Seen is set True by the parser if the "tagged" reserved word |
| 1416 |
-- is seen; this flag tells us whether we have any tagged types. |
| 1417 |
-- It's unfortunate to have this Tagged_Seen processing so scattered |
| 1418 |
-- about, but we need to know if there are tagged types where this is |
| 1419 |
-- called in Analyze_Compilation_Unit, before we have analyzed any type |
| 1420 |
-- declarations. This mechanism also prevents doing |
| 1421 |
-- RTE (RE_Root_Buffer_Type) when compiling the compiler itself. |
| 1422 |
-- Packages Ada.Strings.Buffer_Types and friends are not included |
| 1423 |
-- in the compiler. |
| 1424 |
|
| 1425 |
if not In_Predefined_Unit (Compilation_Unit) |
| 1426 |
and then Tagged_Seen |
| 1427 |
and then not No_Run_Time_Mode |
| 1428 |
then |
| 1429 |
Preload_Root_Buffer_Type_Done := True; |
| 1430 |
|
| 1431 |
-- Don't do it if type Root_Buffer_Type is unavailable in the |
| 1432 |
-- runtime. |
| 1433 |
|
| 1434 |
if RTE_Available (RE_Root_Buffer_Type) then |
| 1435 |
if Debug_Unit_Walk then |
| 1436 |
Write_Line ("Preload_Root_Buffer_Type: "); |
| 1437 |
Write_Unit_Info |
| 1438 |
(Get_Cunit_Unit_Number (Compilation_Unit), |
| 1439 |
Unit (Compilation_Unit)); |
| 1440 |
end if; |
| 1441 |
|
| 1442 |
Ignore := RTE (RE_Root_Buffer_Type); |
| 1443 |
end if; |
| 1444 |
end if; |
| 1445 |
end Preload_Root_Buffer_Type; |
| 1446 |
|
| 1447 |
------------------------- |
| 1448 |
-- Put_Image_Base_Type -- |
| 1449 |
------------------------- |
| 1450 |
|
| 1451 |
function Put_Image_Base_Type (E : Entity_Id) return Entity_Id is |
| 1452 |
begin |
| 1453 |
if Is_Array_Type (E) and then Is_First_Subtype (E) then |
| 1454 |
return E; |
| 1455 |
elsif Is_Private_Type (Base_Type (E)) and not Is_Private_Type (E) then |
| 1456 |
return Implementation_Base_Type (E); |
| 1457 |
else |
| 1458 |
return Base_Type (E); |
| 1459 |
end if; |
| 1460 |
end Put_Image_Base_Type; |
| 1461 |
|
| 1462 |
end Exp_Put_Image; |