1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2008-2016, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Einfo
; use Einfo
;
28 with Namet
; use Namet
;
29 with Nlists
; use Nlists
;
31 with Sinfo
; use Sinfo
;
32 with Sinput
; use Sinput
;
33 with Snames
; use Snames
;
34 with Uintp
; use Uintp
;
36 package body Pprint
is
38 List_Name_Count
: Integer := 0;
39 -- Counter used to prevent infinite recursion while computing name of
40 -- complex expressions.
42 ----------------------
43 -- Expression_Image --
44 ----------------------
46 function Expression_Image
48 Default
: String) return String
50 From_Source
: constant Boolean :=
51 Comes_From_Source
(Expr
)
52 and then not Opt
.Debug_Generated_Code
;
53 Append_Paren
: Boolean := False;
54 Left
: Node_Id
:= Original_Node
(Expr
);
55 Right
: Node_Id
:= Original_Node
(Expr
);
59 Take_Prefix
: Boolean := True;
60 Expand_Type
: Boolean := True) return String;
61 -- Return string corresponding to Expr. If no string can be extracted,
62 -- return "...". If Take_Prefix is True, go back to prefix when needed,
63 -- otherwise only consider the right-hand side of an expression. If
64 -- Expand_Type is True and Expr is a type, try to expand Expr (an
65 -- internally generated type) into a user understandable name.
67 Max_List
: constant := 3;
68 -- Limit number of list elements to dump
70 Max_Expr_Elements
: constant := 24;
71 -- Limit number of elements in an expression for use by Expr_Name
73 Num_Elements
: Natural := 0;
74 -- Current number of elements processed by Expr_Name
78 Add_Space
: Boolean := True;
79 Add_Paren
: Boolean := True) return String;
80 -- Return a string corresponding to List
88 Add_Space
: Boolean := True;
89 Add_Paren
: Boolean := True) return String
91 function Internal_List_Name
93 First
: Boolean := True;
94 Add_Space
: Boolean := True;
95 Add_Paren
: Boolean := True;
96 Num
: Natural := 1) return String;
97 -- ??? what does this do
99 ------------------------
100 -- Internal_List_Name --
101 ------------------------
103 function Internal_List_Name
105 First
: Boolean := True;
106 Add_Space
: Boolean := True;
107 Add_Paren
: Boolean := True;
108 Num
: Natural := 1) return String
110 function Prepend
(S
: String) return String;
111 -- ??? what does this do
117 function Prepend
(S
: String) return String is
132 -- Start of processing for Internal_List_Name
135 if not Present
(List
) then
136 if First
or else not Add_Paren
then
141 elsif Num
> Max_List
then
149 -- ??? the Internal_List_Name calls can be factored out
152 return Prepend
(Expr_Name
(List
)
154 (List
=> Next
(List
),
156 Add_Paren
=> Add_Paren
,
159 return ", " & Expr_Name
(List
)
161 (List
=> Next
(List
),
163 Add_Paren
=> Add_Paren
,
166 end Internal_List_Name
;
168 -- Start of processing for List_Name
171 -- Prevent infinite recursion by limiting depth to 3
173 if List_Name_Count
> 3 then
177 List_Name_Count
:= List_Name_Count
+ 1;
180 Result
: constant String :=
183 Add_Space
=> Add_Space
,
184 Add_Paren
=> Add_Paren
);
186 List_Name_Count
:= List_Name_Count
- 1;
197 Take_Prefix
: Boolean := True;
198 Expand_Type
: Boolean := True) return String
201 Num_Elements
:= Num_Elements
+ 1;
203 if Num_Elements
> Max_Expr_Elements
then
208 when N_Defining_Identifier
211 return Ident_Image
(Expr
, Expression_Image
.Expr
, Expand_Type
);
213 when N_Character_Literal
=>
215 Char
: constant Int
:=
216 UI_To_Int
(Char_Literal_Value
(Expr
));
218 if Char
in 32 .. 127 then
219 return "'" & Character'Val (Char
) & "'";
221 UI_Image
(Char_Literal_Value
(Expr
));
223 "'\" & UI_Image_Buffer (1 .. UI_Image_Length) & "'";
227 when N_Integer_Literal =>
228 UI_Image (Intval (Expr));
229 return UI_Image_Buffer (1 .. UI_Image_Length);
231 when N_Real_Literal =>
232 return Real_Image (Realval (Expr));
234 when N_String_Literal =>
235 return String_Image (Strval (Expr));
238 return "new " & Expr_Name (Expression (Expr));
241 if Present (Sinfo.Expressions (Expr)) then
244 (List => First (Sinfo.Expressions (Expr)),
247 -- Do not return empty string for (others => <>) aggregate
248 -- of a componentless record type. At least one caller (the
249 -- recursive call below in the N_Qualified_Expression case)
250 -- is not prepared to deal with a zero-length result.
252 elsif Null_Record_Present (Expr)
253 or else not Present (First (Component_Associations (Expr)))
255 return ("(null record)");
260 (List => First (Component_Associations (Expr)),
265 when N_Extension_Aggregate =>
266 return "(" & Expr_Name (Ancestor_Part (Expr)) & " with "
268 (List => First (Sinfo.Expressions (Expr)),
270 Add_Paren => False) & ")";
272 when N_Attribute_Reference =>
275 Id : constant Attribute_Id :=
276 Get_Attribute_Id (Attribute_Name (Expr));
277 Str : constant String :=
278 Expr_Name (Prefix (Expr)) & "'"
279 & Get_Name_String (Attribute_Name (Expr));
284 if (Id = Attribute_First or else Id = Attribute_Last)
285 and then Str (Str'First) = '$'
287 N := Associated_Node_For_Itype (Etype (Prefix (Expr)));
290 if Nkind (N) = N_Full_Type_Declaration then
291 N := Type_Definition (N);
294 if Nkind (N) = N_Subtype_Declaration then
297 (Constraint (Subtype_Indication (N)));
299 if List_Length (Ranges) = 1
304 N_Real_Range_Specification,
305 N_Signed_Integer_Type_Definition)
307 if Id = Attribute_First then
310 (Low_Bound (First (Ranges)), Str);
314 (High_Bound (First (Ranges)), Str);
324 return "'" & Get_Name_String (Attribute_Name (Expr));
327 when N_Explicit_Dereference =>
329 -- Return "Foo" instead of "Parameter_Block.Foo.all"
331 if Hide_Parameter_Blocks
332 and then Nkind (Prefix (Expr)) = N_Selected_Component
333 and then Present (Etype (Prefix (Expr)))
334 and then Is_Access_Type (Etype (Prefix (Expr)))
335 and then Is_Param_Block_Component_Type (Etype (Prefix (Expr)))
337 return Expr_Name (Selector_Name (Prefix (Expr)));
339 elsif Take_Prefix then
340 return Expr_Name (Prefix (Expr)) & ".all";
346 | N_Selected_Component
350 Expr_Name (Prefix (Expr)) & "." &
351 Expr_Name (Selector_Name (Expr));
353 return "." & Expr_Name (Selector_Name (Expr));
356 when N_Component_Association =>
359 (List => First (Choices (Expr)),
362 & " => " & Expr_Name (Expression (Expr)) & ")";
364 when N_If_Expression =>
366 N : constant Node_Id := First (Sinfo.Expressions (Expr));
369 "if " & Expr_Name (N) & " then "
370 & Expr_Name (Next (N)) & " else "
371 & Expr_Name (Next (Next (N)));
374 when N_Qualified_Expression =>
376 Mark : constant String :=
378 (Subtype_Mark (Expr), Expand_Type => False);
379 Str : constant String := Expr_Name (Expression (Expr));
381 if Str (Str'First) = '(' and then Str (Str'Last) = ')' then
382 return Mark & "'" & Str;
384 return Mark & "'(" & Str & ")";
388 when N_Expression_With_Actions
389 | N_Unchecked_Expression
391 return Expr_Name (Expression (Expr));
393 when N_Raise_Constraint_Error =>
394 if Present (Condition (Expr)) then
396 "[constraint_error when "
397 & Expr_Name (Condition (Expr)) & "]";
399 return "[constraint_error]";
402 when N_Raise_Program_Error =>
403 if Present (Condition (Expr)) then
405 "[program_error when "
406 & Expr_Name (Condition (Expr)) & "]";
408 return "[program_error]";
413 Expr_Name (Low_Bound (Expr)) & ".." &
414 Expr_Name (High_Bound (Expr));
418 Expr_Name (Prefix (Expr)) & " (" &
419 Expr_Name (Discrete_Range (Expr)) & ")";
423 Expr_Name (Left_Opnd (Expr)) & " and then " &
424 Expr_Name (Right_Opnd (Expr));
428 Expr_Name (Left_Opnd (Expr)) & " in " &
429 Expr_Name (Right_Opnd (Expr));
433 Expr_Name (Left_Opnd (Expr)) & " not in " &
434 Expr_Name (Right_Opnd (Expr));
438 Expr_Name (Left_Opnd (Expr)) & " or else " &
439 Expr_Name (Right_Opnd (Expr));
443 Expr_Name (Left_Opnd (Expr)) & " and " &
444 Expr_Name (Right_Opnd (Expr));
448 Expr_Name (Left_Opnd (Expr)) & " or " &
449 Expr_Name (Right_Opnd (Expr));
453 Expr_Name (Left_Opnd (Expr)) & " xor " &
454 Expr_Name (Right_Opnd (Expr));
458 Expr_Name (Left_Opnd (Expr)) & " = " &
459 Expr_Name (Right_Opnd (Expr));
463 Expr_Name (Left_Opnd (Expr)) & " /= " &
464 Expr_Name (Right_Opnd (Expr));
468 Expr_Name (Left_Opnd (Expr)) & " < " &
469 Expr_Name (Right_Opnd (Expr));
473 Expr_Name (Left_Opnd (Expr)) & " <= " &
474 Expr_Name (Right_Opnd (Expr));
478 Expr_Name (Left_Opnd (Expr)) & " > " &
479 Expr_Name (Right_Opnd (Expr));
483 Expr_Name (Left_Opnd (Expr)) & " >= " &
484 Expr_Name (Right_Opnd (Expr));
488 Expr_Name (Left_Opnd (Expr)) & " + " &
489 Expr_Name (Right_Opnd (Expr));
491 when N_Op_Subtract =>
493 Expr_Name (Left_Opnd (Expr)) & " - " &
494 Expr_Name (Right_Opnd (Expr));
496 when N_Op_Multiply =>
498 Expr_Name (Left_Opnd (Expr)) & " * " &
499 Expr_Name (Right_Opnd (Expr));
503 Expr_Name (Left_Opnd (Expr)) & " / " &
504 Expr_Name (Right_Opnd (Expr));
508 Expr_Name (Left_Opnd (Expr)) & " mod " &
509 Expr_Name (Right_Opnd (Expr));
513 Expr_Name (Left_Opnd (Expr)) & " rem " &
514 Expr_Name (Right_Opnd (Expr));
518 Expr_Name (Left_Opnd (Expr)) & " ** " &
519 Expr_Name (Right_Opnd (Expr));
521 when N_Op_Shift_Left =>
523 Expr_Name (Left_Opnd (Expr)) & " << " &
524 Expr_Name (Right_Opnd (Expr));
526 when N_Op_Shift_Right | N_Op_Shift_Right_Arithmetic =>
528 Expr_Name (Left_Opnd (Expr)) & " >> " &
529 Expr_Name (Right_Opnd (Expr));
533 Expr_Name (Left_Opnd (Expr)) & " & " &
534 Expr_Name (Right_Opnd (Expr));
537 return "+" & Expr_Name (Right_Opnd (Expr));
540 return "-" & Expr_Name (Right_Opnd (Expr));
543 return "abs " & Expr_Name (Right_Opnd (Expr));
546 return "not (" & Expr_Name (Right_Opnd (Expr)) & ")";
548 when N_Parameter_Association =>
549 return Expr_Name (Explicit_Actual_Parameter (Expr));
551 when N_Type_Conversion =>
553 -- Most conversions are not very interesting (used inside
554 -- expanded checks to convert to larger ranges), so skip them.
556 return Expr_Name (Expression (Expr));
558 when N_Unchecked_Type_Conversion =>
560 -- Only keep the type conversion in complex cases
562 if not Is_Scalar_Type (Etype (Expr))
563 or else not Is_Scalar_Type (Etype (Expression (Expr)))
564 or else Is_Modular_Integer_Type (Etype (Expr)) /=
565 Is_Modular_Integer_Type (Etype (Expression (Expr)))
567 return Expr_Name (Subtype_Mark (Expr)) &
568 "(" & Expr_Name (Expression (Expr)) & ")";
570 return Expr_Name (Expression (Expr));
573 when N_Indexed_Component =>
576 Expr_Name (Prefix (Expr))
577 & List_Name (First (Sinfo.Expressions (Expr)));
579 return List_Name (First (Sinfo.Expressions (Expr)));
582 when N_Function_Call =>
584 -- If Default = "", it means we're expanding the name of
585 -- a gnat temporary (and not really a function call), so add
586 -- parentheses around function call to mark it specially.
590 & Expr_Name (Name (Expr))
591 & List_Name (First (Sinfo.Parameter_Associations (Expr)))
595 Expr_Name (Name (Expr))
597 (First (Sinfo.Parameter_Associations (Expr)));
603 when N_Others_Choice =>
611 -- Start of processing for Expression_Name
614 if not From_Source then
616 S : constant String := Expr_Name (Expr);
626 -- Compute left (start) and right (end) slocs for the expression
627 -- Consider using Sinput.Sloc_Range instead, except that it does not
628 -- work properly currently???
637 Left := Original_Node (Left_Opnd (Left));
639 when N_Attribute_Reference
641 | N_Explicit_Dereference
642 | N_Indexed_Component
644 | N_Selected_Component
647 Left := Original_Node (Prefix (Left));
649 when N_Defining_Program_Unit_Name
653 Left := Original_Node (Name (Left));
656 Left := Original_Node (Low_Bound (Left));
658 when N_Type_Conversion =>
659 Left := Original_Node (Subtype_Mark (Left));
661 -- For any other item, quit loop
669 case Nkind (Right) is
675 Right := Original_Node (Right_Opnd (Right));
678 | N_Selected_Component
680 Right := Original_Node (Selector_Name (Right));
683 Right := Original_Node (Identifier (Right));
685 when N_Defining_Program_Unit_Name =>
686 Right := Original_Node (Defining_Identifier (Right));
689 Right := Original_Node (High_Bound (Right));
691 when N_Parameter_Association =>
692 Right := Original_Node (Explicit_Actual_Parameter (Right));
694 when N_Indexed_Component =>
695 Right := Original_Node (Last (Sinfo.Expressions (Right)));
696 Append_Paren := True;
698 when N_Function_Call =>
699 if Present (Sinfo.Parameter_Associations (Right)) then
705 -- Avoid source position confusion associated with
706 -- parameters for which Comes_From_Source is False.
708 Rover := First (Sinfo.Parameter_Associations (Right));
710 while Present (Rover) loop
711 if Comes_From_Source (Original_Node (Rover)) then
712 Right := Original_Node (Rover);
713 Append_Paren := True;
720 -- Quit loop if no Comes_From_Source parameters
725 -- Quit loop if no parameters
731 when N_Quantified_Expression =>
732 Right := Original_Node (Condition (Right));
734 -- For all other items, quit the loop
742 Scn : Source_Ptr := Original_Location (Sloc (Left));
743 End_Sloc : constant Source_Ptr :=
744 Original_Location (Sloc (Right));
745 Src : constant Source_Buffer_Ptr :=
746 Source_Text (Get_Source_File_Index (Scn));
749 if Scn > End_Sloc then
754 Buffer : String (1 .. Natural (End_Sloc - Scn));
755 Index : Natural := 0;
756 Skipping_Comment : Boolean := False;
757 Underscore : Boolean := False;
760 if Right /= Expr then
761 while Scn < End_Sloc loop
766 if not Skipping_Comment and then not Underscore then
769 Buffer (Index) := ' ';
772 -- CR/LF/FF is the end of any comment
778 Skipping_Comment := False;
783 if not Skipping_Comment then
787 if Src (Scn) = '-' and then Src (Scn + 1) = '-' then
788 Skipping_Comment := True;
792 Buffer (Index) := Src (Scn);
803 S : constant String := Expr_Name (Right);
812 elsif Append_Paren then
813 return Buffer (1 .. Index) & Expr_Name (Right, False) & ')';
816 return Buffer (1 .. Index) & Expr_Name (Right, False);
820 end Expression_Image;