| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- P P R I N T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2008-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- |
| 17 |
-- for more details. You should have received a copy of the GNU General -- |
| 18 |
-- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
with Atree; use Atree; |
| 27 |
with Einfo; use Einfo; |
| 28 |
with Einfo.Entities; use Einfo.Entities; |
| 29 |
with Einfo.Utils; use Einfo.Utils; |
| 30 |
with Errout; use Errout; |
| 31 |
with Namet; use Namet; |
| 32 |
with Nlists; use Nlists; |
| 33 |
with Opt; use Opt; |
| 34 |
with Sinfo; use Sinfo; |
| 35 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 36 |
with Sinfo.Utils; use Sinfo.Utils; |
| 37 |
with Sinput; use Sinput; |
| 38 |
with Snames; use Snames; |
| 39 |
with Uintp; use Uintp; |
| 40 |
|
| 41 |
with System.Case_Util; |
| 42 |
|
| 43 |
package body Pprint is |
| 44 |
|
| 45 |
List_Name_Count : Natural := 0; |
| 46 |
-- Counter used to prevent infinite recursion while computing name of |
| 47 |
-- complex expressions. |
| 48 |
|
| 49 |
---------------------- |
| 50 |
-- Expression_Image -- |
| 51 |
---------------------- |
| 52 |
|
| 53 |
function Expression_Image |
| 54 |
(Expr : Node_Id; |
| 55 |
Default : String) return String |
| 56 |
is |
| 57 |
function Expr_Name |
| 58 |
(Expr : Node_Id; |
| 59 |
Take_Prefix : Boolean := True; |
| 60 |
Expand_Type : Boolean := True; |
| 61 |
No_Parens : Boolean := False) return String; |
| 62 |
-- Return string corresponding to Expr. If no string can be extracted, |
| 63 |
-- return "...". If Take_Prefix is True, go back to prefix when needed, |
| 64 |
-- otherwise only consider the right-hand side of an expression. If |
| 65 |
-- Expand_Type is True and Expr is a type, try to expand Expr (an |
| 66 |
-- internally generated type) into a user understandable name. |
| 67 |
-- If No_Parens is True, then suppress creating parentheses around |
| 68 |
-- expression. If False, check to see whether expression should be |
| 69 |
-- parenthesized. |
| 70 |
|
| 71 |
function Count_Parentheses (S : String; C : Character) return Natural |
| 72 |
with Pre => C in '(' | ')'; |
| 73 |
-- Returns the number of times parenthesis character C should be added |
| 74 |
-- to string S for getting a correctly parenthesized result. For C = '(' |
| 75 |
-- this means prepending the character, for C = ')' this means appending |
| 76 |
-- the character. |
| 77 |
-- In other words, count the number of missing instances of C, |
| 78 |
-- but never return less than zero. |
| 79 |
|
| 80 |
function Fix_Parentheses (S : String) return String; |
| 81 |
-- Counts the number of required opening and closing parentheses in S to |
| 82 |
-- respectively prepend and append for getting correct parentheses. Then |
| 83 |
-- returns S with opening parentheses prepended and closing parentheses |
| 84 |
-- appended so that the result is correctly parenthesized. |
| 85 |
|
| 86 |
Max_List_Depth : constant := 3; |
| 87 |
-- Limit number of nested lists to print |
| 88 |
|
| 89 |
Max_List_Length : constant := 3; |
| 90 |
-- Limit number of list elements to print |
| 91 |
|
| 92 |
Max_Expr_Elements : constant := 24; |
| 93 |
-- Limit number of elements in an expression for use by Expr_Name |
| 94 |
|
| 95 |
Num_Elements : Natural := 0; |
| 96 |
-- Current number of elements processed by Expr_Name |
| 97 |
|
| 98 |
function List_Name (List : List_Id) return String; |
| 99 |
-- Return a string corresponding to List |
| 100 |
|
| 101 |
--------------- |
| 102 |
-- List_Name -- |
| 103 |
--------------- |
| 104 |
|
| 105 |
function List_Name (List : List_Id) return String is |
| 106 |
Buf : Bounded_String; |
| 107 |
Elmt : Node_Id; |
| 108 |
|
| 109 |
Printed_Elmts : Natural := 0; |
| 110 |
List_Len : constant Natural := Natural (List_Length (List)); |
| 111 |
|
| 112 |
begin |
| 113 |
-- Give up if the printed list is too deep |
| 114 |
|
| 115 |
if List_Name_Count > Max_List_Depth then |
| 116 |
return "..."; |
| 117 |
end if; |
| 118 |
|
| 119 |
List_Name_Count := List_Name_Count + 1; |
| 120 |
|
| 121 |
Elmt := First (List); |
| 122 |
while Present (Elmt) loop |
| 123 |
|
| 124 |
-- Print component_association as "x | y | z => 12345" |
| 125 |
|
| 126 |
if Nkind (Elmt) = N_Component_Association then |
| 127 |
declare |
| 128 |
Choice : Node_Id := First (Choices (Elmt)); |
| 129 |
begin |
| 130 |
while Present (Choice) loop |
| 131 |
Append (Buf, Expr_Name (Choice)); |
| 132 |
Next (Choice); |
| 133 |
|
| 134 |
if Present (Choice) then |
| 135 |
Append (Buf, " | "); |
| 136 |
end if; |
| 137 |
end loop; |
| 138 |
end; |
| 139 |
Append (Buf, " => "); |
| 140 |
if Box_Present (Elmt) then |
| 141 |
Append (Buf, "<>"); |
| 142 |
else |
| 143 |
Append (Buf, Expr_Name (Expression (Elmt))); |
| 144 |
end if; |
| 145 |
|
| 146 |
-- Print parameter_association as "x => 12345" |
| 147 |
|
| 148 |
elsif Nkind (Elmt) = N_Parameter_Association then |
| 149 |
Append (Buf, Expr_Name (Selector_Name (Elmt))); |
| 150 |
Append (Buf, " => "); |
| 151 |
Append (Buf, Expr_Name (Explicit_Actual_Parameter (Elmt))); |
| 152 |
|
| 153 |
-- Print expression itself as "12345" |
| 154 |
|
| 155 |
else |
| 156 |
-- Suppress parens if is the only parameter |
| 157 |
Append (Buf, Expr_Name (Elmt, No_Parens => List_Len = 1)); |
| 158 |
end if; |
| 159 |
|
| 160 |
Next (Elmt); |
| 161 |
Printed_Elmts := Printed_Elmts + 1; |
| 162 |
|
| 163 |
-- Separate next element with a comma, if necessary |
| 164 |
|
| 165 |
if Present (Elmt) then |
| 166 |
Append (Buf, ", "); |
| 167 |
|
| 168 |
-- Abbreviate remaining elements as "...", if limit exceeded |
| 169 |
|
| 170 |
if Printed_Elmts = Max_List_Length then |
| 171 |
Append (Buf, "..."); |
| 172 |
exit; |
| 173 |
end if; |
| 174 |
end if; |
| 175 |
end loop; |
| 176 |
|
| 177 |
List_Name_Count := List_Name_Count - 1; |
| 178 |
|
| 179 |
return To_String (Buf); |
| 180 |
end List_Name; |
| 181 |
|
| 182 |
--------------- |
| 183 |
-- Expr_Name -- |
| 184 |
--------------- |
| 185 |
|
| 186 |
function Expr_Name |
| 187 |
(Expr : Node_Id; |
| 188 |
Take_Prefix : Boolean := True; |
| 189 |
Expand_Type : Boolean := True; |
| 190 |
No_Parens : Boolean := False) return String |
| 191 |
is |
| 192 |
-- Define a subtype matching logical operations |
| 193 |
-- and [then], or [else], and xor. |
| 194 |
-- In Ada, these operations are non associative -- they |
| 195 |
-- all have the same precedence, so parentheses |
| 196 |
-- are needed to indicate the association of |
| 197 |
-- operands in a sequence of distinct operations. |
| 198 |
subtype Non_Associative is N_Subexpr |
| 199 |
with Static_Predicate => |
| 200 |
Non_Associative in |
| 201 |
N_Short_Circuit | N_Op_And | N_Op_Or | N_Op_Xor; |
| 202 |
begin |
| 203 |
if not No_Parens |
| 204 |
and then |
| 205 |
(Paren_Count (Expr) > 0 |
| 206 |
or else |
| 207 |
(Nkind (Expr) in Non_Associative |
| 208 |
and then |
| 209 |
Nkind (Parent (Expr)) in Non_Associative |
| 210 |
and then |
| 211 |
Nkind (Parent (Expr)) /= Nkind (Expr))) |
| 212 |
then |
| 213 |
-- Parentheses are needed, either because |
| 214 |
-- Paren_Count is greater than zero, or because |
| 215 |
-- this operation and its parent are non associative, |
| 216 |
-- and are not the same operation. |
| 217 |
return '(' & |
| 218 |
Expr_Name (Expr, Take_Prefix, Expand_Type, No_Parens => True) & |
| 219 |
')'; |
| 220 |
end if; |
| 221 |
|
| 222 |
Num_Elements := Num_Elements + 1; |
| 223 |
|
| 224 |
if Num_Elements > Max_Expr_Elements then |
| 225 |
return "..."; |
| 226 |
end if; |
| 227 |
|
| 228 |
-- Just print pieces of aggregate nodes, even though they are not |
| 229 |
-- expressions. It is too much trouble to handle them any better. |
| 230 |
|
| 231 |
if Nkind (Expr) = N_Component_Association then |
| 232 |
|
| 233 |
pragma Assert (Box_Present (Expr)); |
| 234 |
|
| 235 |
declare |
| 236 |
Buf : Bounded_String; |
| 237 |
Choice : Node_Id := First (Choices (Expr)); |
| 238 |
begin |
| 239 |
while Present (Choice) loop |
| 240 |
Append (Buf, Expr_Name (Choice)); |
| 241 |
Next (Choice); |
| 242 |
|
| 243 |
if Present (Choice) then |
| 244 |
Append (Buf, " | "); |
| 245 |
end if; |
| 246 |
end loop; |
| 247 |
|
| 248 |
Append (Buf, " => <>"); |
| 249 |
|
| 250 |
return To_String (Buf); |
| 251 |
end; |
| 252 |
|
| 253 |
elsif Nkind (Expr) = N_Others_Choice then |
| 254 |
return "others"; |
| 255 |
end if; |
| 256 |
|
| 257 |
case N_Subexpr'(Nkind (Expr)) is |
| 258 |
when N_Identifier => |
| 259 |
return Ident_Image (Expr, Expression_Image.Expr, Expand_Type); |
| 260 |
|
| 261 |
when N_Character_Literal => |
| 262 |
declare |
| 263 |
Char : constant Int := UI_To_Int (Char_Literal_Value (Expr)); |
| 264 |
begin |
| 265 |
if Char in 32 .. 126 then |
| 266 |
return "'" & Character'Val (Char) & "'"; |
| 267 |
else |
| 268 |
UI_Image (Char_Literal_Value (Expr)); |
| 269 |
return |
| 270 |
"'\" & UI_Image_Buffer (1 .. UI_Image_Length) & "'"; |
| 271 |
end if; |
| 272 |
end; |
| 273 |
|
| 274 |
when N_Integer_Literal => |
| 275 |
return UI_Image (Intval (Expr)); |
| 276 |
|
| 277 |
when N_Real_Literal => |
| 278 |
return Real_Image (Realval (Expr)); |
| 279 |
|
| 280 |
when N_String_Literal => |
| 281 |
return String_Image (Strval (Expr)); |
| 282 |
|
| 283 |
when N_Allocator => |
| 284 |
return "new " & Expr_Name (Expression (Expr)); |
| 285 |
|
| 286 |
when N_Aggregate => |
| 287 |
if Present (Expressions (Expr)) then |
| 288 |
return '(' & List_Name (Expressions (Expr)) & ')'; |
| 289 |
|
| 290 |
-- Do not return empty string for (others => <>) aggregate |
| 291 |
-- of a componentless record type. At least one caller (the |
| 292 |
-- recursive call below in the N_Qualified_Expression case) |
| 293 |
-- is not prepared to deal with a zero-length result. |
| 294 |
|
| 295 |
elsif Null_Record_Present (Expr) |
| 296 |
or else No (First (Component_Associations (Expr))) |
| 297 |
then |
| 298 |
return ("(null record)"); |
| 299 |
|
| 300 |
else |
| 301 |
return '(' & List_Name (Component_Associations (Expr)) & ')'; |
| 302 |
end if; |
| 303 |
|
| 304 |
when N_Extension_Aggregate => |
| 305 |
return '(' & Expr_Name (Ancestor_Part (Expr)) |
| 306 |
& " with (" & List_Name (Expressions (Expr)) & ')'; |
| 307 |
|
| 308 |
when N_Attribute_Reference => |
| 309 |
if Take_Prefix then |
| 310 |
declare |
| 311 |
Id : constant Attribute_Id := |
| 312 |
Get_Attribute_Id (Attribute_Name (Expr)); |
| 313 |
|
| 314 |
-- Always use mixed case for attributes |
| 315 |
|
| 316 |
Str : constant String := |
| 317 |
Expr_Name (Prefix (Expr)) |
| 318 |
& "'" |
| 319 |
& System.Case_Util.To_Mixed |
| 320 |
(Get_Name_String (Attribute_Name (Expr))); |
| 321 |
|
| 322 |
N : Node_Id; |
| 323 |
Ranges : List_Id; |
| 324 |
|
| 325 |
begin |
| 326 |
if (Id = Attribute_First or else Id = Attribute_Last) |
| 327 |
and then Str (Str'First) = '$' |
| 328 |
then |
| 329 |
N := Associated_Node_For_Itype (Etype (Prefix (Expr))); |
| 330 |
|
| 331 |
if Present (N) then |
| 332 |
if Nkind (N) = N_Full_Type_Declaration then |
| 333 |
N := Type_Definition (N); |
| 334 |
end if; |
| 335 |
|
| 336 |
if Nkind (N) = N_Subtype_Declaration then |
| 337 |
Ranges := |
| 338 |
Constraints |
| 339 |
(Constraint (Subtype_Indication (N))); |
| 340 |
|
| 341 |
if List_Length (Ranges) = 1 |
| 342 |
and then Nkind (First (Ranges)) in |
| 343 |
N_Range | |
| 344 |
N_Real_Range_Specification | |
| 345 |
N_Signed_Integer_Type_Definition |
| 346 |
then |
| 347 |
if Id = Attribute_First then |
| 348 |
return |
| 349 |
Expression_Image |
| 350 |
(Low_Bound (First (Ranges)), Str); |
| 351 |
else |
| 352 |
return |
| 353 |
Expression_Image |
| 354 |
(High_Bound (First (Ranges)), Str); |
| 355 |
end if; |
| 356 |
end if; |
| 357 |
end if; |
| 358 |
end if; |
| 359 |
end if; |
| 360 |
|
| 361 |
return Str; |
| 362 |
end; |
| 363 |
else |
| 364 |
return ''' & Get_Name_String (Attribute_Name (Expr)); |
| 365 |
end if; |
| 366 |
|
| 367 |
when N_Explicit_Dereference => |
| 368 |
Explicit_Dereference : declare |
| 369 |
function Deref_Suffix return String; |
| 370 |
-- Usually returns ".all", but will return "" if |
| 371 |
-- Hide_Temp_Derefs is true and the prefix is a use of a |
| 372 |
-- not-from-source object declared as |
| 373 |
-- X : constant Some_Access_Type := Some_Expr'Reference; |
| 374 |
-- (as is sometimes done in Exp_Util.Remove_Side_Effects). |
| 375 |
|
| 376 |
------------------ |
| 377 |
-- Deref_Suffix -- |
| 378 |
------------------ |
| 379 |
|
| 380 |
function Deref_Suffix return String is |
| 381 |
Decl : Node_Id; |
| 382 |
|
| 383 |
begin |
| 384 |
if Hide_Temp_Derefs |
| 385 |
and then Nkind (Prefix (Expr)) = N_Identifier |
| 386 |
and then Nkind (Entity (Prefix (Expr))) = |
| 387 |
N_Defining_Identifier |
| 388 |
then |
| 389 |
Decl := Parent (Entity (Prefix (Expr))); |
| 390 |
|
| 391 |
if Present (Decl) |
| 392 |
and then Nkind (Decl) = N_Object_Declaration |
| 393 |
and then not Comes_From_Source (Decl) |
| 394 |
and then Constant_Present (Decl) |
| 395 |
and then Present (Expression (Decl)) |
| 396 |
and then Nkind (Expression (Decl)) = N_Reference |
| 397 |
then |
| 398 |
return ""; |
| 399 |
end if; |
| 400 |
end if; |
| 401 |
|
| 402 |
-- The default case |
| 403 |
|
| 404 |
return ".all"; |
| 405 |
end Deref_Suffix; |
| 406 |
|
| 407 |
-- Start of processing for Explicit_Dereference |
| 408 |
|
| 409 |
begin |
| 410 |
if Hide_Parameter_Blocks |
| 411 |
and then Nkind (Prefix (Expr)) = N_Selected_Component |
| 412 |
and then Present (Etype (Prefix (Expr))) |
| 413 |
and then Is_Access_Type (Etype (Prefix (Expr))) |
| 414 |
and then Is_Param_Block_Component_Type |
| 415 |
(Etype (Prefix (Expr))) |
| 416 |
then |
| 417 |
-- Return "Foo" instead of "Parameter_Block.Foo.all" |
| 418 |
|
| 419 |
return Expr_Name (Selector_Name (Prefix (Expr))); |
| 420 |
|
| 421 |
elsif Take_Prefix then |
| 422 |
return Expr_Name (Prefix (Expr)) & Deref_Suffix; |
| 423 |
else |
| 424 |
return Deref_Suffix; |
| 425 |
end if; |
| 426 |
end Explicit_Dereference; |
| 427 |
|
| 428 |
when N_Expanded_Name |
| 429 |
| N_Selected_Component |
| 430 |
=> |
| 431 |
if Take_Prefix then |
| 432 |
return |
| 433 |
Expr_Name (Prefix (Expr)) & "." & |
| 434 |
Expr_Name (Selector_Name (Expr)); |
| 435 |
else |
| 436 |
return "." & Expr_Name (Selector_Name (Expr)); |
| 437 |
end if; |
| 438 |
|
| 439 |
when N_If_Expression => |
| 440 |
declare |
| 441 |
Cond_Expr : constant Node_Id := First (Expressions (Expr)); |
| 442 |
Then_Expr : constant Node_Id := Next (Cond_Expr); |
| 443 |
Else_Expr : constant Node_Id := Next (Then_Expr); |
| 444 |
begin |
| 445 |
return |
| 446 |
"if " & Expr_Name (Cond_Expr) & " then " |
| 447 |
& Expr_Name (Then_Expr) & " else " |
| 448 |
& Expr_Name (Else_Expr); |
| 449 |
end; |
| 450 |
|
| 451 |
when N_Qualified_Expression => |
| 452 |
declare |
| 453 |
Mark : constant String := |
| 454 |
Expr_Name |
| 455 |
(Subtype_Mark (Expr), Expand_Type => False); |
| 456 |
Str : constant String := Expr_Name (Expression (Expr)); |
| 457 |
begin |
| 458 |
if Str (Str'First) = '(' and then Str (Str'Last) = ')' then |
| 459 |
return Mark & "'" & Str; |
| 460 |
else |
| 461 |
return Mark & "'(" & Str & ")"; |
| 462 |
end if; |
| 463 |
end; |
| 464 |
|
| 465 |
when N_Expression_With_Actions => |
| 466 |
return Expr_Name (Expression (Expr)); |
| 467 |
|
| 468 |
when N_Raise_Constraint_Error => |
| 469 |
if Present (Condition (Expr)) then |
| 470 |
return |
| 471 |
"[constraint_error when " |
| 472 |
& Expr_Name (Condition (Expr)) & "]"; |
| 473 |
else |
| 474 |
return "[constraint_error]"; |
| 475 |
end if; |
| 476 |
|
| 477 |
when N_Raise_Program_Error => |
| 478 |
if Present (Condition (Expr)) then |
| 479 |
return |
| 480 |
"[program_error when " |
| 481 |
& Expr_Name (Condition (Expr)) & "]"; |
| 482 |
else |
| 483 |
return "[program_error]"; |
| 484 |
end if; |
| 485 |
|
| 486 |
when N_Raise_Storage_Error => |
| 487 |
if Present (Condition (Expr)) then |
| 488 |
return |
| 489 |
"[storage_error when " |
| 490 |
& Expr_Name (Condition (Expr)) & "]"; |
| 491 |
else |
| 492 |
return "[storage_error]"; |
| 493 |
end if; |
| 494 |
|
| 495 |
when N_Range => |
| 496 |
return |
| 497 |
Expr_Name (Low_Bound (Expr)) & ".." & |
| 498 |
Expr_Name (High_Bound (Expr)); |
| 499 |
|
| 500 |
when N_Slice => |
| 501 |
return |
| 502 |
Expr_Name (Prefix (Expr)) & " (" & |
| 503 |
Expr_Name (Discrete_Range (Expr)) & ")"; |
| 504 |
|
| 505 |
when N_And_Then => |
| 506 |
return |
| 507 |
Expr_Name (Left_Opnd (Expr)) & " and then " & |
| 508 |
Expr_Name (Right_Opnd (Expr)); |
| 509 |
|
| 510 |
when N_In => |
| 511 |
return |
| 512 |
Expr_Name (Left_Opnd (Expr)) & " in " & |
| 513 |
Expr_Name (Right_Opnd (Expr)); |
| 514 |
|
| 515 |
when N_Not_In => |
| 516 |
return |
| 517 |
Expr_Name (Left_Opnd (Expr)) & " not in " & |
| 518 |
Expr_Name (Right_Opnd (Expr)); |
| 519 |
|
| 520 |
when N_Or_Else => |
| 521 |
return |
| 522 |
Expr_Name (Left_Opnd (Expr)) & " or else " & |
| 523 |
Expr_Name (Right_Opnd (Expr)); |
| 524 |
|
| 525 |
when N_Op_And => |
| 526 |
return |
| 527 |
Expr_Name (Left_Opnd (Expr)) & " and " & |
| 528 |
Expr_Name (Right_Opnd (Expr)); |
| 529 |
|
| 530 |
when N_Op_Or => |
| 531 |
return |
| 532 |
Expr_Name (Left_Opnd (Expr)) & " or " & |
| 533 |
Expr_Name (Right_Opnd (Expr)); |
| 534 |
|
| 535 |
when N_Op_Xor => |
| 536 |
return |
| 537 |
Expr_Name (Left_Opnd (Expr)) & " xor " & |
| 538 |
Expr_Name (Right_Opnd (Expr)); |
| 539 |
|
| 540 |
when N_Op_Eq => |
| 541 |
return |
| 542 |
Expr_Name (Left_Opnd (Expr)) & " = " & |
| 543 |
Expr_Name (Right_Opnd (Expr)); |
| 544 |
|
| 545 |
when N_Op_Ne => |
| 546 |
return |
| 547 |
Expr_Name (Left_Opnd (Expr)) & " /= " & |
| 548 |
Expr_Name (Right_Opnd (Expr)); |
| 549 |
|
| 550 |
when N_Op_Lt => |
| 551 |
return |
| 552 |
Expr_Name (Left_Opnd (Expr)) & " < " & |
| 553 |
Expr_Name (Right_Opnd (Expr)); |
| 554 |
|
| 555 |
when N_Op_Le => |
| 556 |
return |
| 557 |
Expr_Name (Left_Opnd (Expr)) & " <= " & |
| 558 |
Expr_Name (Right_Opnd (Expr)); |
| 559 |
|
| 560 |
when N_Op_Gt => |
| 561 |
return |
| 562 |
Expr_Name (Left_Opnd (Expr)) & " > " & |
| 563 |
Expr_Name (Right_Opnd (Expr)); |
| 564 |
|
| 565 |
when N_Op_Ge => |
| 566 |
return |
| 567 |
Expr_Name (Left_Opnd (Expr)) & " >= " & |
| 568 |
Expr_Name (Right_Opnd (Expr)); |
| 569 |
|
| 570 |
when N_Op_Add => |
| 571 |
return |
| 572 |
Expr_Name (Left_Opnd (Expr)) & " + " & |
| 573 |
Expr_Name (Right_Opnd (Expr)); |
| 574 |
|
| 575 |
when N_Op_Subtract => |
| 576 |
return |
| 577 |
Expr_Name (Left_Opnd (Expr)) & " - " & |
| 578 |
Expr_Name (Right_Opnd (Expr)); |
| 579 |
|
| 580 |
when N_Op_Multiply => |
| 581 |
return |
| 582 |
Expr_Name (Left_Opnd (Expr)) & " * " & |
| 583 |
Expr_Name (Right_Opnd (Expr)); |
| 584 |
|
| 585 |
when N_Op_Divide => |
| 586 |
return |
| 587 |
Expr_Name (Left_Opnd (Expr)) & " / " & |
| 588 |
Expr_Name (Right_Opnd (Expr)); |
| 589 |
|
| 590 |
when N_Op_Mod => |
| 591 |
return |
| 592 |
Expr_Name (Left_Opnd (Expr)) & " mod " & |
| 593 |
Expr_Name (Right_Opnd (Expr)); |
| 594 |
|
| 595 |
when N_Op_Rem => |
| 596 |
return |
| 597 |
Expr_Name (Left_Opnd (Expr)) & " rem " & |
| 598 |
Expr_Name (Right_Opnd (Expr)); |
| 599 |
|
| 600 |
when N_Op_Expon => |
| 601 |
return |
| 602 |
Expr_Name (Left_Opnd (Expr)) & " ** " & |
| 603 |
Expr_Name (Right_Opnd (Expr)); |
| 604 |
|
| 605 |
when N_Op_Shift_Left => |
| 606 |
return |
| 607 |
Expr_Name (Left_Opnd (Expr)) & " << " & |
| 608 |
Expr_Name (Right_Opnd (Expr)); |
| 609 |
|
| 610 |
when N_Op_Shift_Right | N_Op_Shift_Right_Arithmetic => |
| 611 |
return |
| 612 |
Expr_Name (Left_Opnd (Expr)) & " >> " & |
| 613 |
Expr_Name (Right_Opnd (Expr)); |
| 614 |
|
| 615 |
when N_Op_Concat => |
| 616 |
return |
| 617 |
Expr_Name (Left_Opnd (Expr)) & " & " & |
| 618 |
Expr_Name (Right_Opnd (Expr)); |
| 619 |
|
| 620 |
when N_Op_Plus => |
| 621 |
return "+" & Expr_Name (Right_Opnd (Expr)); |
| 622 |
|
| 623 |
when N_Op_Minus => |
| 624 |
return "-" & Expr_Name (Right_Opnd (Expr)); |
| 625 |
|
| 626 |
when N_Op_Abs => |
| 627 |
return "abs " & Expr_Name (Right_Opnd (Expr)); |
| 628 |
|
| 629 |
when N_Op_Not => |
| 630 |
return "not (" & |
| 631 |
Expr_Name (Right_Opnd (Expr), No_Parens => True) & |
| 632 |
")"; |
| 633 |
|
| 634 |
when N_Type_Conversion => |
| 635 |
|
| 636 |
-- Most conversions are not very interesting (used inside |
| 637 |
-- expanded checks to convert to larger ranges), so skip them. |
| 638 |
|
| 639 |
return Expr_Name (Expression (Expr)); |
| 640 |
|
| 641 |
when N_Unchecked_Type_Conversion => |
| 642 |
|
| 643 |
-- Only keep the type conversion in complex cases |
| 644 |
|
| 645 |
if not Is_Scalar_Type (Etype (Expr)) |
| 646 |
or else not Is_Scalar_Type (Etype (Expression (Expr))) |
| 647 |
or else Is_Modular_Integer_Type (Etype (Expr)) /= |
| 648 |
Is_Modular_Integer_Type (Etype (Expression (Expr))) |
| 649 |
then |
| 650 |
return Expr_Name (Subtype_Mark (Expr)) & |
| 651 |
"(" & |
| 652 |
Expr_Name (Expression (Expr), No_Parens => True) & |
| 653 |
")"; |
| 654 |
else |
| 655 |
return Expr_Name (Expression (Expr)); |
| 656 |
end if; |
| 657 |
|
| 658 |
when N_Indexed_Component => |
| 659 |
if Take_Prefix then |
| 660 |
return |
| 661 |
Expr_Name (Prefix (Expr)) |
| 662 |
& " (" & List_Name (Expressions (Expr)) & ')'; |
| 663 |
else |
| 664 |
return List_Name (Expressions (Expr)); |
| 665 |
end if; |
| 666 |
|
| 667 |
when N_Function_Call => |
| 668 |
|
| 669 |
-- If Default = "", it means we're expanding the name of |
| 670 |
-- a gnat temporary (and not really a function call), so add |
| 671 |
-- parentheses around function call to mark it specially. |
| 672 |
|
| 673 |
if Default = "" then |
| 674 |
if Present (Parameter_Associations (Expr)) then |
| 675 |
return '(' |
| 676 |
& Expr_Name (Name (Expr)) |
| 677 |
& " (" |
| 678 |
& List_Name (Parameter_Associations (Expr)) |
| 679 |
& "))"; |
| 680 |
else |
| 681 |
return '(' & |
| 682 |
Expr_Name (Name (Expr), No_Parens => True) & |
| 683 |
')'; |
| 684 |
end if; |
| 685 |
elsif Present (Parameter_Associations (Expr)) then |
| 686 |
return |
| 687 |
Expr_Name (Name (Expr)) |
| 688 |
& " (" & List_Name (Parameter_Associations (Expr)) & ')'; |
| 689 |
else |
| 690 |
return Expr_Name (Name (Expr)); |
| 691 |
end if; |
| 692 |
|
| 693 |
when N_Null => |
| 694 |
return "null"; |
| 695 |
|
| 696 |
when N_Case_Expression |
| 697 |
| N_Delta_Aggregate |
| 698 |
| N_External_Initializer |
| 699 |
| N_Interpolated_String_Literal |
| 700 |
| N_Op_Rotate_Left |
| 701 |
| N_Op_Rotate_Right |
| 702 |
| N_Operator_Symbol |
| 703 |
| N_Procedure_Call_Statement |
| 704 |
| N_Quantified_Expression |
| 705 |
| N_Raise_Expression |
| 706 |
| N_Reference |
| 707 |
| N_Target_Name |
| 708 |
=> |
| 709 |
return "..."; |
| 710 |
end case; |
| 711 |
end Expr_Name; |
| 712 |
|
| 713 |
----------------------- |
| 714 |
-- Count_Parentheses -- |
| 715 |
----------------------- |
| 716 |
|
| 717 |
function Count_Parentheses (S : String; C : Character) return Natural is |
| 718 |
|
| 719 |
procedure Next_Char (Count : in out Natural; C, D, Ch : Character); |
| 720 |
-- Process next character Ch and update the number Count of C |
| 721 |
-- characters to add for correct parenthesizing, where D is the |
| 722 |
-- opposite parenthesis. |
| 723 |
-- In other words, count the number of missing instances of C, |
| 724 |
-- or equivalently, the number of unmatched instances of D. |
| 725 |
|
| 726 |
--------------- |
| 727 |
-- Next_Char -- |
| 728 |
--------------- |
| 729 |
|
| 730 |
procedure Next_Char (Count : in out Natural; C, D, Ch : Character) is |
| 731 |
begin |
| 732 |
if Ch = D then |
| 733 |
Count := Count + 1; |
| 734 |
elsif Ch = C and then Count > 0 then |
| 735 |
Count := Count - 1; |
| 736 |
end if; |
| 737 |
end Next_Char; |
| 738 |
|
| 739 |
-- Local variables |
| 740 |
|
| 741 |
Count : Natural := 0; |
| 742 |
|
| 743 |
-- Start of processing for Count_Parentheses |
| 744 |
|
| 745 |
begin |
| 746 |
if C = '(' then |
| 747 |
for Ch of reverse S loop |
| 748 |
Next_Char (Count, C, ')', Ch); |
| 749 |
end loop; |
| 750 |
else |
| 751 |
for Ch of S loop |
| 752 |
Next_Char (Count, C, '(', Ch); |
| 753 |
end loop; |
| 754 |
end if; |
| 755 |
|
| 756 |
return Count; |
| 757 |
end Count_Parentheses; |
| 758 |
|
| 759 |
--------------------- |
| 760 |
-- Fix_Parentheses -- |
| 761 |
--------------------- |
| 762 |
|
| 763 |
function Fix_Parentheses (S : String) return String is |
| 764 |
Count_Open : constant Natural := Count_Parentheses (S, '('); |
| 765 |
Count_Close : constant Natural := Count_Parentheses (S, ')'); |
| 766 |
begin |
| 767 |
return (1 .. Count_Open => '(') & S & (1 .. Count_Close => ')'); |
| 768 |
end Fix_Parentheses; |
| 769 |
|
| 770 |
-- Local variables |
| 771 |
|
| 772 |
Left, Right : Source_Ptr; |
| 773 |
|
| 774 |
-- Start of processing for Expression_Image |
| 775 |
|
| 776 |
begin |
| 777 |
-- Since this is an expression pretty-printer, it should not be called |
| 778 |
-- for anything but an expression. However, currently CodePeer calls |
| 779 |
-- it for defining identifiers. This should be fixed in the CodePeer |
| 780 |
-- itself, but for now simply return the default (if present) or print |
| 781 |
-- name of the defining identifier. |
| 782 |
|
| 783 |
if Nkind (Expr) = N_Defining_Identifier then |
| 784 |
pragma Assert (CodePeer_Mode); |
| 785 |
if Comes_From_Source (Expr) |
| 786 |
or else Opt.Debug_Generated_Code |
| 787 |
then |
| 788 |
if Default = "" then |
| 789 |
declare |
| 790 |
Nam : constant Name_Id := Chars (Expr); |
| 791 |
Buf : Bounded_String |
| 792 |
(Max_Length => Natural (Length_Of_Name (Nam))); |
| 793 |
begin |
| 794 |
Adjust_Name_Case (Buf, Sloc (Expr)); |
| 795 |
Append (Buf, Nam); |
| 796 |
return To_String (Buf); |
| 797 |
end; |
| 798 |
else |
| 799 |
return Default; |
| 800 |
end if; |
| 801 |
else |
| 802 |
declare |
| 803 |
S : constant String := |
| 804 |
Ident_Image |
| 805 |
(Expr => Expr, Orig_Expr => Expr, Expand_Type => True); |
| 806 |
begin |
| 807 |
if S = "..." then |
| 808 |
return Default; |
| 809 |
else |
| 810 |
return S; |
| 811 |
end if; |
| 812 |
end; |
| 813 |
end if; |
| 814 |
else |
| 815 |
pragma Assert (Nkind (Expr) in N_Subexpr); |
| 816 |
end if; |
| 817 |
|
| 818 |
-- ??? The following should be primarily needed for CodePeer |
| 819 |
|
| 820 |
if not Comes_From_Source (Expr) |
| 821 |
or else Opt.Debug_Generated_Code |
| 822 |
then |
| 823 |
declare |
| 824 |
S : constant String := Expr_Name (Expr, No_Parens => True); |
| 825 |
begin |
| 826 |
if S = "..." then |
| 827 |
return Default; |
| 828 |
else |
| 829 |
return S; |
| 830 |
end if; |
| 831 |
end; |
| 832 |
end if; |
| 833 |
|
| 834 |
-- Reach to the underlying expression for an expression-with-actions |
| 835 |
|
| 836 |
if Nkind (Expr) = N_Expression_With_Actions then |
| 837 |
return Expression_Image (Expression (Expr), Default); |
| 838 |
end if; |
| 839 |
|
| 840 |
-- Compute left (start) and right (end) slocs for the expression |
| 841 |
|
| 842 |
Left := First_Sloc (Expr); |
| 843 |
Right := Last_Sloc (Expr); |
| 844 |
|
| 845 |
if Left > Right then |
| 846 |
return Default; |
| 847 |
end if; |
| 848 |
|
| 849 |
declare |
| 850 |
Scn : Source_Ptr := Left; |
| 851 |
Src : constant not null Source_Buffer_Ptr := |
| 852 |
Source_Text (Get_Source_File_Index (Scn)); |
| 853 |
|
| 854 |
Threshold : constant := 256; |
| 855 |
Buffer : String (1 .. Natural (Right - Left + 1)); |
| 856 |
Index : Natural := 0; |
| 857 |
Skipping_Comment : Boolean := False; |
| 858 |
Underscore : Boolean := False; |
| 859 |
begin |
| 860 |
while Scn <= Right loop |
| 861 |
case Src (Scn) is |
| 862 |
|
| 863 |
-- Give up on non ASCII characters |
| 864 |
|
| 865 |
when Character'Val (128) .. Character'Last => |
| 866 |
Index := 0; |
| 867 |
exit; |
| 868 |
|
| 869 |
when ' ' |
| 870 |
| ASCII.HT |
| 871 |
=> |
| 872 |
if not Skipping_Comment and then not Underscore then |
| 873 |
Underscore := True; |
| 874 |
Index := Index + 1; |
| 875 |
Buffer (Index) := ' '; |
| 876 |
end if; |
| 877 |
|
| 878 |
-- CR/LF/FF is the end of any comment |
| 879 |
|
| 880 |
when ASCII.CR |
| 881 |
| ASCII.FF |
| 882 |
| ASCII.LF |
| 883 |
=> |
| 884 |
Skipping_Comment := False; |
| 885 |
|
| 886 |
when others => |
| 887 |
Underscore := False; |
| 888 |
|
| 889 |
if not Skipping_Comment then |
| 890 |
|
| 891 |
-- Ignore comment |
| 892 |
|
| 893 |
if Src (Scn) = '-' and then Src (Scn + 1) = '-' then |
| 894 |
Skipping_Comment := True; |
| 895 |
else |
| 896 |
Index := Index + 1; |
| 897 |
Buffer (Index) := Src (Scn); |
| 898 |
end if; |
| 899 |
end if; |
| 900 |
end case; |
| 901 |
|
| 902 |
-- Give up on too long strings |
| 903 |
|
| 904 |
if Index >= Threshold then |
| 905 |
return Buffer (1 .. Index) & "..."; |
| 906 |
end if; |
| 907 |
|
| 908 |
Scn := Scn + 1; |
| 909 |
end loop; |
| 910 |
|
| 911 |
return Fix_Parentheses (Buffer (1 .. Index)); |
| 912 |
end; |
| 913 |
end Expression_Image; |
| 914 |
|
| 915 |
end Pprint; |