1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "c-pretty-print.h"
28 #include "tree-pretty-print.h"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* Translate if being used for diagnostics, but not for dump files or
34 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
36 /* The pretty-printer code is primarily designed to closely follow
37 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
38 codes we used to have in the past. Following a structured
39 approach (preferably the official grammars) is believed to make it
40 much easier to add extensions and nifty pretty-printing effects that
41 takes expression or declaration contexts into account. */
44 #define pp_c_maybe_whitespace(PP) \
46 if (pp_base (PP)->padding == pp_before) \
47 pp_c_whitespace (PP); \
51 static void pp_c_char (c_pretty_printer
*, int);
53 /* postfix-expression */
54 static void pp_c_initializer_list (c_pretty_printer
*, tree
);
55 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer
*, tree
);
57 static void pp_c_multiplicative_expression (c_pretty_printer
*, tree
);
58 static void pp_c_additive_expression (c_pretty_printer
*, tree
);
59 static void pp_c_shift_expression (c_pretty_printer
*, tree
);
60 static void pp_c_relational_expression (c_pretty_printer
*, tree
);
61 static void pp_c_equality_expression (c_pretty_printer
*, tree
);
62 static void pp_c_and_expression (c_pretty_printer
*, tree
);
63 static void pp_c_exclusive_or_expression (c_pretty_printer
*, tree
);
64 static void pp_c_inclusive_or_expression (c_pretty_printer
*, tree
);
65 static void pp_c_logical_and_expression (c_pretty_printer
*, tree
);
66 static void pp_c_conditional_expression (c_pretty_printer
*, tree
);
67 static void pp_c_assignment_expression (c_pretty_printer
*, tree
);
72 /* Helper functions. */
75 pp_c_whitespace (c_pretty_printer
*pp
)
78 pp_base (pp
)->padding
= pp_none
;
82 pp_c_left_paren (c_pretty_printer
*pp
)
85 pp_base (pp
)->padding
= pp_none
;
89 pp_c_right_paren (c_pretty_printer
*pp
)
92 pp_base (pp
)->padding
= pp_none
;
96 pp_c_left_brace (c_pretty_printer
*pp
)
99 pp_base (pp
)->padding
= pp_none
;
103 pp_c_right_brace (c_pretty_printer
*pp
)
106 pp_base (pp
)->padding
= pp_none
;
110 pp_c_left_bracket (c_pretty_printer
*pp
)
112 pp_left_bracket (pp
);
113 pp_base (pp
)->padding
= pp_none
;
117 pp_c_right_bracket (c_pretty_printer
*pp
)
119 pp_right_bracket (pp
);
120 pp_base (pp
)->padding
= pp_none
;
124 pp_c_dot (c_pretty_printer
*pp
)
127 pp_base (pp
)->padding
= pp_none
;
131 pp_c_ampersand (c_pretty_printer
*pp
)
134 pp_base (pp
)->padding
= pp_none
;
138 pp_c_star (c_pretty_printer
*pp
)
141 pp_base (pp
)->padding
= pp_none
;
145 pp_c_arrow (c_pretty_printer
*pp
)
148 pp_base (pp
)->padding
= pp_none
;
152 pp_c_semicolon (c_pretty_printer
*pp
)
155 pp_base (pp
)->padding
= pp_none
;
159 pp_c_complement (c_pretty_printer
*pp
)
162 pp_base (pp
)->padding
= pp_none
;
166 pp_c_exclamation (c_pretty_printer
*pp
)
169 pp_base (pp
)->padding
= pp_none
;
172 /* Print out the external representation of QUALIFIERS. */
175 pp_c_cv_qualifiers (c_pretty_printer
*pp
, int qualifiers
, bool func_type
)
177 const char *p
= pp_last_position_in_text (pp
);
178 bool previous
= false;
183 /* The C programming language does not have references, but it is much
184 simpler to handle those here rather than going through the same
185 logic in the C++ pretty-printer. */
186 if (p
!= NULL
&& (*p
== '*' || *p
== '&'))
187 pp_c_whitespace (pp
);
189 if (qualifiers
& TYPE_QUAL_CONST
)
191 pp_c_ws_string (pp
, func_type
? "__attribute__((const))" : "const");
195 if (qualifiers
& TYPE_QUAL_VOLATILE
)
198 pp_c_whitespace (pp
);
199 pp_c_ws_string (pp
, func_type
? "__attribute__((noreturn))" : "volatile");
203 if (qualifiers
& TYPE_QUAL_RESTRICT
)
206 pp_c_whitespace (pp
);
207 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
208 ? "restrict" : "__restrict__"));
212 if (qualifiers
& TYPE_QUAL_RELAXED
)
215 pp_c_whitespace (pp
);
216 pp_c_ws_string (pp
, "relaxed");
220 if (qualifiers
& TYPE_QUAL_STRICT
)
223 pp_c_whitespace (pp
);
224 pp_c_ws_string (pp
, "strict");
228 if (qualifiers
& TYPE_QUAL_SHARED
)
231 pp_c_whitespace (pp
);
232 pp_c_ws_string (pp
, "shared");
238 /* Pretty-print T using the type-cast notation '( type-name )'. */
241 pp_c_type_cast (c_pretty_printer
*pp
, tree t
)
243 pp_c_left_paren (pp
);
245 pp_c_right_paren (pp
);
248 /* We're about to pretty-print a pointer type as indicated by T.
249 Output a whitespace, if needed, preparing for subsequent output. */
252 pp_c_space_for_pointer_operator (c_pretty_printer
*pp
, tree t
)
254 if (POINTER_TYPE_P (t
))
256 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
257 if (TREE_CODE (pointee
) != ARRAY_TYPE
258 && TREE_CODE (pointee
) != FUNCTION_TYPE
)
259 pp_c_whitespace (pp
);
266 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
267 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
268 of its type. Take care of possible extensions.
272 type-qualifier-list type-qualifier
277 __restrict__ -- GNU C
278 address-space-qualifier -- GNU C
281 address-space-qualifier:
282 identifier -- GNU C */
285 pp_c_type_qualifier_list (c_pretty_printer
*pp
, tree t
)
289 if (!t
|| t
== error_mark_node
)
295 qualifiers
= TYPE_QUALS (t
);
296 pp_c_cv_qualifiers (pp
, qualifiers
,
297 TREE_CODE (t
) == FUNCTION_TYPE
);
299 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t
)))
301 const char *as
= c_addr_space_name (TYPE_ADDR_SPACE (t
));
302 pp_c_identifier (pp
, as
);
307 * type-qualifier-list(opt)
308 * type-qualifier-list(opt) pointer */
311 pp_c_pointer (c_pretty_printer
*pp
, tree t
)
313 if (!TYPE_P (t
) && TREE_CODE (t
) != TYPE_DECL
)
315 switch (TREE_CODE (t
))
318 /* It is easier to handle C++ reference types here. */
320 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
321 pp_c_pointer (pp
, TREE_TYPE (t
));
322 if (TREE_CODE (t
) == POINTER_TYPE
)
326 pp_c_type_qualifier_list (pp
, t
);
329 /* ??? This node is now in GENERIC and so shouldn't be here. But
330 we'll fix that later. */
332 pp_declaration (pp
, DECL_EXPR_DECL (t
));
333 pp_needs_newline (pp
) = true;
337 pp_unsupported_tree (pp
, t
);
354 struct-or-union-specifier
359 simple-type-specifier:
364 pp_c_type_specifier (c_pretty_printer
*pp
, tree t
)
366 const enum tree_code code
= TREE_CODE (t
);
370 pp_c_ws_string (pp
, M_("<type-error>"));
373 case IDENTIFIER_NODE
:
374 pp_c_identifier (pp
, IDENTIFIER_POINTER (t
));
381 case FIXED_POINT_TYPE
:
385 pp_c_type_specifier (pp
, t
);
389 int prec
= TYPE_PRECISION (t
);
390 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t
)))
391 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_SATURATING (t
));
393 t
= c_common_type_for_mode (TYPE_MODE (t
), TYPE_UNSIGNED (t
));
396 pp_c_type_specifier (pp
, t
);
397 if (TYPE_PRECISION (t
) != prec
)
400 pp_decimal_int (pp
, prec
);
408 pp_string (pp
, (TYPE_UNSIGNED (t
)
409 ? M_("<unnamed-unsigned:")
410 : M_("<unnamed-signed:")));
413 pp_string (pp
, M_("<unnamed-float:"));
415 case FIXED_POINT_TYPE
:
416 pp_string (pp
, M_("<unnamed-fixed:"));
421 pp_decimal_int (pp
, prec
);
429 pp_id_expression (pp
, t
);
431 pp_c_ws_string (pp
, M_("<typedef-error>"));
437 if (code
== UNION_TYPE
)
438 pp_c_ws_string (pp
, "union");
439 else if (code
== RECORD_TYPE
)
440 pp_c_ws_string (pp
, "struct");
441 else if (code
== ENUMERAL_TYPE
)
442 pp_c_ws_string (pp
, "enum");
444 pp_c_ws_string (pp
, M_("<tag-error>"));
447 pp_id_expression (pp
, TYPE_NAME (t
));
449 pp_c_ws_string (pp
, M_("<anonymous>"));
453 pp_unsupported_tree (pp
, t
);
458 /* specifier-qualifier-list:
459 type-specifier specifier-qualifier-list-opt
460 type-qualifier specifier-qualifier-list-opt
463 Implementation note: Because of the non-linearities in array or
464 function declarations, this routine prints not just the
465 specifier-qualifier-list of such entities or types of such entities,
466 but also the 'pointer' production part of their declarators. The
467 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
470 pp_c_specifier_qualifier_list (c_pretty_printer
*pp
, tree t
)
472 const enum tree_code code
= TREE_CODE (t
);
474 if (!(pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
475 pp_c_type_qualifier_list (pp
, t
);
481 /* Get the types-specifier of this type. */
482 tree pointee
= strip_pointer_operator (TREE_TYPE (t
));
483 pp_c_specifier_qualifier_list (pp
, pointee
);
484 if (TREE_CODE (pointee
) == ARRAY_TYPE
485 || TREE_CODE (pointee
) == FUNCTION_TYPE
)
487 pp_c_whitespace (pp
);
488 pp_c_left_paren (pp
);
489 pp_c_attributes_display (pp
, TYPE_ATTRIBUTES (pointee
));
491 else if (!c_dialect_cxx ())
492 pp_c_whitespace (pp
);
493 pp_ptr_operator (pp
, t
);
499 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
504 if (code
== COMPLEX_TYPE
)
505 pp_c_ws_string (pp
, (flag_isoc99
&& !c_dialect_cxx ()
506 ? "_Complex" : "__complex__"));
507 else if (code
== VECTOR_TYPE
)
509 pp_c_ws_string (pp
, "__vector");
510 pp_c_left_paren (pp
);
511 pp_wide_integer (pp
, TYPE_VECTOR_SUBPARTS (t
));
512 pp_c_right_paren (pp
);
513 pp_c_whitespace (pp
);
515 pp_c_specifier_qualifier_list (pp
, TREE_TYPE (t
));
519 pp_simple_type_specifier (pp
, t
);
522 if ((pp
->flags
& pp_c_flag_gnu_v3
) && code
!= POINTER_TYPE
)
523 pp_c_type_qualifier_list (pp
, t
);
526 /* parameter-type-list:
531 parameter-declaration
532 parameter-list , parameter-declaration
534 parameter-declaration:
535 declaration-specifiers declarator
536 declaration-specifiers abstract-declarator(opt) */
539 pp_c_parameter_type_list (c_pretty_printer
*pp
, tree t
)
541 bool want_parm_decl
= DECL_P (t
) && !(pp
->flags
& pp_c_flag_abstract
);
542 tree parms
= want_parm_decl
? DECL_ARGUMENTS (t
) : TYPE_ARG_TYPES (t
);
543 pp_c_left_paren (pp
);
544 if (parms
== void_list_node
)
545 pp_c_ws_string (pp
, "void");
549 for ( ; parms
&& parms
!= void_list_node
; parms
= TREE_CHAIN (parms
))
552 pp_separate_with (pp
, ',');
554 pp_declaration_specifiers
555 (pp
, want_parm_decl
? parms
: TREE_VALUE (parms
));
557 pp_declarator (pp
, parms
);
559 pp_abstract_declarator (pp
, TREE_VALUE (parms
));
562 pp_c_right_paren (pp
);
565 /* abstract-declarator:
567 pointer(opt) direct-abstract-declarator */
570 pp_c_abstract_declarator (c_pretty_printer
*pp
, tree t
)
572 if (TREE_CODE (t
) == POINTER_TYPE
)
574 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
575 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
576 pp_c_right_paren (pp
);
580 pp_direct_abstract_declarator (pp
, t
);
583 /* direct-abstract-declarator:
584 ( abstract-declarator )
585 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
586 direct-abstract-declarator(opt) [ * ]
587 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
590 pp_c_direct_abstract_declarator (c_pretty_printer
*pp
, tree t
)
592 switch (TREE_CODE (t
))
595 pp_abstract_declarator (pp
, t
);
599 pp_c_parameter_type_list (pp
, t
);
600 pp_direct_abstract_declarator (pp
, TREE_TYPE (t
));
604 pp_c_left_bracket (pp
);
605 if (TYPE_DOMAIN (t
) && TYPE_MAX_VALUE (TYPE_DOMAIN (t
)))
607 tree maxval
= TYPE_MAX_VALUE (TYPE_DOMAIN (t
));
608 tree type
= TREE_TYPE (maxval
);
610 if (host_integerp (maxval
, 0))
611 pp_wide_integer (pp
, tree_low_cst (maxval
, 0) + 1);
613 pp_expression (pp
, fold_build2 (PLUS_EXPR
, type
, maxval
,
614 build_int_cst (type
, 1)));
616 pp_c_right_bracket (pp
);
617 pp_direct_abstract_declarator (pp
, TREE_TYPE (t
));
620 case IDENTIFIER_NODE
:
625 case FIXED_POINT_TYPE
:
635 pp_unsupported_tree (pp
, t
);
641 specifier-qualifier-list abstract-declarator(opt) */
644 pp_c_type_id (c_pretty_printer
*pp
, tree t
)
646 pp_c_specifier_qualifier_list (pp
, t
);
647 pp_abstract_declarator (pp
, t
);
650 /* storage-class-specifier:
658 pp_c_storage_class_specifier (c_pretty_printer
*pp
, tree t
)
660 if (TREE_CODE (t
) == TYPE_DECL
)
661 pp_c_ws_string (pp
, "typedef");
664 if (DECL_REGISTER (t
))
665 pp_c_ws_string (pp
, "register");
666 else if (TREE_STATIC (t
) && TREE_CODE (t
) == VAR_DECL
)
667 pp_c_ws_string (pp
, "static");
671 /* function-specifier:
675 pp_c_function_specifier (c_pretty_printer
*pp
, tree t
)
677 if (TREE_CODE (t
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (t
))
678 pp_c_ws_string (pp
, "inline");
681 /* declaration-specifiers:
682 storage-class-specifier declaration-specifiers(opt)
683 type-specifier declaration-specifiers(opt)
684 type-qualifier declaration-specifiers(opt)
685 function-specifier declaration-specifiers(opt) */
688 pp_c_declaration_specifiers (c_pretty_printer
*pp
, tree t
)
690 pp_storage_class_specifier (pp
, t
);
691 pp_function_specifier (pp
, t
);
692 pp_c_specifier_qualifier_list (pp
, DECL_P (t
) ? TREE_TYPE (t
) : t
);
698 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
699 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
700 direct-declarator [ type-qualifier-list static assignment-expression ]
701 direct-declarator [ type-qualifier-list * ]
702 direct-declarator ( parameter-type-list )
703 direct-declarator ( identifier-list(opt) ) */
706 pp_c_direct_declarator (c_pretty_printer
*pp
, tree t
)
708 switch (TREE_CODE (t
))
715 pp_c_space_for_pointer_operator (pp
, TREE_TYPE (t
));
716 pp_c_tree_decl_identifier (pp
, t
);
721 pp_abstract_declarator (pp
, TREE_TYPE (t
));
725 pp_parameter_list (pp
, t
);
726 pp_abstract_declarator (pp
, TREE_TYPE (t
));
730 pp_c_space_for_pointer_operator (pp
, TREE_TYPE (TREE_TYPE (t
)));
731 pp_c_tree_decl_identifier (pp
, t
);
732 if (pp_c_base (pp
)->flags
& pp_c_flag_abstract
)
733 pp_abstract_declarator (pp
, TREE_TYPE (t
));
736 pp_parameter_list (pp
, t
);
737 pp_abstract_declarator (pp
, TREE_TYPE (TREE_TYPE (t
)));
743 case FIXED_POINT_TYPE
:
750 pp_unsupported_tree (pp
, t
);
757 pointer(opt) direct-declarator */
760 pp_c_declarator (c_pretty_printer
*pp
, tree t
)
762 switch (TREE_CODE (t
))
766 case FIXED_POINT_TYPE
:
779 pp_direct_declarator (pp
, t
);
784 pp_unsupported_tree (pp
, t
);
790 declaration-specifiers init-declarator-list(opt) ; */
793 pp_c_declaration (c_pretty_printer
*pp
, tree t
)
795 pp_declaration_specifiers (pp
, t
);
796 pp_c_init_declarator (pp
, t
);
799 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
802 pp_c_attributes (c_pretty_printer
*pp
, tree attributes
)
804 if (attributes
== NULL_TREE
)
807 pp_c_ws_string (pp
, "__attribute__");
808 pp_c_left_paren (pp
);
809 pp_c_left_paren (pp
);
810 for (; attributes
!= NULL_TREE
; attributes
= TREE_CHAIN (attributes
))
812 pp_tree_identifier (pp
, TREE_PURPOSE (attributes
));
813 if (TREE_VALUE (attributes
))
814 pp_c_call_argument_list (pp
, TREE_VALUE (attributes
));
816 if (TREE_CHAIN (attributes
))
817 pp_separate_with (pp
, ',');
819 pp_c_right_paren (pp
);
820 pp_c_right_paren (pp
);
823 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
824 marked to be displayed on disgnostic. */
827 pp_c_attributes_display (c_pretty_printer
*pp
, tree a
)
829 bool is_first
= true;
834 for (; a
!= NULL_TREE
; a
= TREE_CHAIN (a
))
836 const struct attribute_spec
*as
;
837 as
= lookup_attribute_spec (TREE_PURPOSE (a
));
838 if (!as
|| as
->affects_type_identity
== false)
842 pp_c_ws_string (pp
, "__attribute__");
843 pp_c_left_paren (pp
);
844 pp_c_left_paren (pp
);
849 pp_separate_with (pp
, ',');
851 pp_tree_identifier (pp
, TREE_PURPOSE (a
));
853 pp_c_call_argument_list (pp
, TREE_VALUE (a
));
858 pp_c_right_paren (pp
);
859 pp_c_right_paren (pp
);
860 pp_c_whitespace (pp
);
864 /* function-definition:
865 declaration-specifiers declarator compound-statement */
868 pp_c_function_definition (c_pretty_printer
*pp
, tree t
)
870 pp_declaration_specifiers (pp
, t
);
871 pp_declarator (pp
, t
);
872 pp_needs_newline (pp
) = true;
873 pp_statement (pp
, DECL_SAVED_TREE (t
));
874 pp_newline_and_flush (pp
);
880 /* Print out a c-char. This is called solely for characters which are
881 in the *target* execution character set. We ought to convert them
882 back to the *host* execution character set before printing, but we
883 have no way to do this at present. A decent compromise is to print
884 all characters as if they were in the host execution character set,
885 and not attempt to recover any named escape characters, but render
886 all unprintables as octal escapes. If the host and target character
887 sets are the same, this produces relatively readable output. If they
888 are not the same, strings may appear as gibberish, but that's okay
889 (in fact, it may well be what the reader wants, e.g. if they are looking
890 to see if conversion to the target character set happened correctly).
892 A special case: we need to prefix \, ", and ' with backslashes. It is
893 correct to do so for the *host*'s \, ", and ', because the rest of the
894 file appears in the host character set. */
897 pp_c_char (c_pretty_printer
*pp
, int c
)
903 case '\\': pp_string (pp
, "\\\\"); break;
904 case '\'': pp_string (pp
, "\\\'"); break;
905 case '\"': pp_string (pp
, "\\\""); break;
906 default: pp_character (pp
, c
);
910 pp_scalar (pp
, "\\%03o", (unsigned) c
);
913 /* Print out a STRING literal. */
916 pp_c_string_literal (c_pretty_printer
*pp
, tree s
)
918 const char *p
= TREE_STRING_POINTER (s
);
919 int n
= TREE_STRING_LENGTH (s
) - 1;
922 for (i
= 0; i
< n
; ++i
)
923 pp_c_char (pp
, p
[i
]);
927 /* Pretty-print an INTEGER literal. */
930 pp_c_integer_constant (c_pretty_printer
*pp
, tree i
)
932 /* We are going to compare the type of I to other types using
933 pointer comparison so we need to use its canonical type. */
935 TYPE_CANONICAL (TREE_TYPE (i
))
936 ? TYPE_CANONICAL (TREE_TYPE (i
))
939 if (host_integerp (i
, 0))
940 pp_wide_integer (pp
, TREE_INT_CST_LOW (i
));
941 else if (host_integerp (i
, 1))
942 pp_unsigned_wide_integer (pp
, TREE_INT_CST_LOW (i
));
945 unsigned HOST_WIDE_INT low
= TREE_INT_CST_LOW (i
);
946 HOST_WIDE_INT high
= TREE_INT_CST_HIGH (i
);
947 if (tree_int_cst_sgn (i
) < 0)
949 pp_character (pp
, '-');
953 sprintf (pp_buffer (pp
)->digit_buffer
, HOST_WIDE_INT_PRINT_DOUBLE_HEX
,
954 (unsigned HOST_WIDE_INT
) high
, (unsigned HOST_WIDE_INT
) low
);
955 pp_string (pp
, pp_buffer (pp
)->digit_buffer
);
957 if (TYPE_UNSIGNED (type
))
958 pp_character (pp
, 'u');
959 if (type
== long_integer_type_node
|| type
== long_unsigned_type_node
)
960 pp_character (pp
, 'l');
961 else if (type
== long_long_integer_type_node
962 || type
== long_long_unsigned_type_node
)
963 pp_string (pp
, "ll");
964 else if (type
== int128_integer_type_node
965 || type
== int128_unsigned_type_node
)
966 pp_string (pp
, "I128");
969 /* Print out a CHARACTER literal. */
972 pp_c_character_constant (c_pretty_printer
*pp
, tree c
)
974 tree type
= TREE_TYPE (c
);
975 if (type
== wchar_type_node
)
976 pp_character (pp
, 'L');
978 if (host_integerp (c
, TYPE_UNSIGNED (type
)))
979 pp_c_char (pp
, tree_low_cst (c
, TYPE_UNSIGNED (type
)));
981 pp_scalar (pp
, "\\x%x", (unsigned) TREE_INT_CST_LOW (c
));
985 /* Print out a BOOLEAN literal. */
988 pp_c_bool_constant (c_pretty_printer
*pp
, tree b
)
990 if (b
== boolean_false_node
)
992 if (c_dialect_cxx ())
993 pp_c_ws_string (pp
, "false");
994 else if (flag_isoc99
)
995 pp_c_ws_string (pp
, "_False");
997 pp_unsupported_tree (pp
, b
);
999 else if (b
== boolean_true_node
)
1001 if (c_dialect_cxx ())
1002 pp_c_ws_string (pp
, "true");
1003 else if (flag_isoc99
)
1004 pp_c_ws_string (pp
, "_True");
1006 pp_unsupported_tree (pp
, b
);
1008 else if (TREE_CODE (b
) == INTEGER_CST
)
1009 pp_c_integer_constant (pp
, b
);
1011 pp_unsupported_tree (pp
, b
);
1014 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
1015 false; that means the value was obtained by a cast, in which case
1016 print out the type-id part of the cast-expression -- the casted value
1017 is then printed by pp_c_integer_literal. */
1020 pp_c_enumeration_constant (c_pretty_printer
*pp
, tree e
)
1022 bool value_is_named
= true;
1023 tree type
= TREE_TYPE (e
);
1026 /* Find the name of this constant. */
1027 for (value
= TYPE_VALUES (type
);
1028 value
!= NULL_TREE
&& !tree_int_cst_equal (TREE_VALUE (value
), e
);
1029 value
= TREE_CHAIN (value
))
1032 if (value
!= NULL_TREE
)
1033 pp_id_expression (pp
, TREE_PURPOSE (value
));
1036 /* Value must have been cast. */
1037 pp_c_type_cast (pp
, type
);
1038 value_is_named
= false;
1041 return value_is_named
;
1044 /* Print out a REAL value as a decimal-floating-constant. */
1047 pp_c_floating_constant (c_pretty_printer
*pp
, tree r
)
1049 const struct real_format
*fmt
1050 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r
)));
1052 REAL_VALUE_TYPE floating_cst
= TREE_REAL_CST (r
);
1053 bool is_decimal
= floating_cst
.decimal
;
1055 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1056 log10(2) to 7 significant digits. */
1057 int max_digits10
= 2 + (is_decimal
? fmt
->p
: fmt
->p
* 643L / 2136);
1059 real_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_REAL_CST (r
),
1060 sizeof (pp_buffer (pp
)->digit_buffer
),
1063 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1064 if (TREE_TYPE (r
) == float_type_node
)
1065 pp_character (pp
, 'f');
1066 else if (TREE_TYPE (r
) == long_double_type_node
)
1067 pp_character (pp
, 'l');
1068 else if (TREE_TYPE (r
) == dfloat128_type_node
)
1069 pp_string (pp
, "dl");
1070 else if (TREE_TYPE (r
) == dfloat64_type_node
)
1071 pp_string (pp
, "dd");
1072 else if (TREE_TYPE (r
) == dfloat32_type_node
)
1073 pp_string (pp
, "df");
1076 /* Print out a FIXED value as a decimal-floating-constant. */
1079 pp_c_fixed_constant (c_pretty_printer
*pp
, tree r
)
1081 fixed_to_decimal (pp_buffer (pp
)->digit_buffer
, &TREE_FIXED_CST (r
),
1082 sizeof (pp_buffer (pp
)->digit_buffer
));
1083 pp_string (pp
, pp_buffer(pp
)->digit_buffer
);
1086 /* Pretty-print a compound literal expression. GNU extensions include
1087 vector constants. */
1090 pp_c_compound_literal (c_pretty_printer
*pp
, tree e
)
1092 tree type
= TREE_TYPE (e
);
1093 pp_c_type_cast (pp
, type
);
1095 switch (TREE_CODE (type
))
1102 pp_c_brace_enclosed_initializer_list (pp
, e
);
1106 pp_unsupported_tree (pp
, e
);
1111 /* Pretty-print a COMPLEX_EXPR expression. */
1114 pp_c_complex_expr (c_pretty_printer
*pp
, tree e
)
1116 /* Handle a few common special cases, otherwise fallback
1117 to printing it as compound literal. */
1118 tree type
= TREE_TYPE (e
);
1119 tree realexpr
= TREE_OPERAND (e
, 0);
1120 tree imagexpr
= TREE_OPERAND (e
, 1);
1122 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1123 if (TREE_CODE (realexpr
) == NOP_EXPR
1124 && TREE_CODE (imagexpr
) == NOP_EXPR
1125 && TREE_TYPE (realexpr
) == TREE_TYPE (type
)
1126 && TREE_TYPE (imagexpr
) == TREE_TYPE (type
)
1127 && TREE_CODE (TREE_OPERAND (realexpr
, 0)) == REALPART_EXPR
1128 && TREE_CODE (TREE_OPERAND (imagexpr
, 0)) == IMAGPART_EXPR
1129 && TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0)
1130 == TREE_OPERAND (TREE_OPERAND (imagexpr
, 0), 0))
1132 pp_c_type_cast (pp
, type
);
1133 pp_expression (pp
, TREE_OPERAND (TREE_OPERAND (realexpr
, 0), 0));
1137 /* Cast of an scalar expression to COMPLEX_TYPE. */
1138 if ((integer_zerop (imagexpr
) || real_zerop (imagexpr
))
1139 && TREE_TYPE (realexpr
) == TREE_TYPE (type
))
1141 pp_c_type_cast (pp
, type
);
1142 if (TREE_CODE (realexpr
) == NOP_EXPR
)
1143 realexpr
= TREE_OPERAND (realexpr
, 0);
1144 pp_expression (pp
, realexpr
);
1148 pp_c_compound_literal (pp
, e
);
1154 fixed-point-constant
1155 enumeration-constant
1156 character-constant */
1159 pp_c_constant (c_pretty_printer
*pp
, tree e
)
1161 const enum tree_code code
= TREE_CODE (e
);
1167 tree type
= TREE_TYPE (e
);
1168 if (type
== boolean_type_node
)
1169 pp_c_bool_constant (pp
, e
);
1170 else if (type
== char_type_node
)
1171 pp_c_character_constant (pp
, e
);
1172 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1173 && pp_c_enumeration_constant (pp
, e
))
1176 pp_c_integer_constant (pp
, e
);
1181 pp_c_floating_constant (pp
, e
);
1185 pp_c_fixed_constant (pp
, e
);
1189 pp_c_string_literal (pp
, e
);
1193 /* Sometimes, we are confused and we think a complex literal
1194 is a constant. Such thing is a compound literal which
1195 grammatically belongs to postfix-expr production. */
1196 pp_c_compound_literal (pp
, e
);
1200 pp_unsupported_tree (pp
, e
);
1205 /* Pretty-print a string such as an identifier, without changing its
1206 encoding, preceded by whitespace is necessary. */
1209 pp_c_ws_string (c_pretty_printer
*pp
, const char *str
)
1211 pp_c_maybe_whitespace (pp
);
1212 pp_string (pp
, str
);
1213 pp_base (pp
)->padding
= pp_before
;
1216 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1217 that need converting to the locale encoding, preceded by whitespace
1221 pp_c_identifier (c_pretty_printer
*pp
, const char *id
)
1223 pp_c_maybe_whitespace (pp
);
1224 pp_identifier (pp
, id
);
1225 pp_base (pp
)->padding
= pp_before
;
1228 /* Pretty-print a C primary-expression.
1236 pp_c_primary_expression (c_pretty_printer
*pp
, tree e
)
1238 switch (TREE_CODE (e
))
1246 pp_c_tree_decl_identifier (pp
, e
);
1249 case IDENTIFIER_NODE
:
1250 pp_c_tree_identifier (pp
, e
);
1254 pp_c_ws_string (pp
, M_("<erroneous-expression>"));
1258 pp_c_ws_string (pp
, M_("<return-value>"));
1265 pp_c_constant (pp
, e
);
1269 pp_c_ws_string (pp
, "__builtin_memcpy");
1270 pp_c_left_paren (pp
);
1272 pp_primary_expression (pp
, TREE_OPERAND (e
, 0));
1273 pp_separate_with (pp
, ',');
1275 pp_initializer (pp
, TREE_OPERAND (e
, 1));
1276 if (TREE_OPERAND (e
, 2))
1278 pp_separate_with (pp
, ',');
1279 pp_c_expression (pp
, TREE_OPERAND (e
, 2));
1281 pp_c_right_paren (pp
);
1285 /* FIXME: Make sure we won't get into an infinite loop. */
1286 pp_c_left_paren (pp
);
1287 pp_expression (pp
, e
);
1288 pp_c_right_paren (pp
);
1293 /* Print out a C initializer -- also support C compound-literals.
1295 assignment-expression:
1296 { initializer-list }
1297 { initializer-list , } */
1300 pp_c_initializer (c_pretty_printer
*pp
, tree e
)
1302 if (TREE_CODE (e
) == CONSTRUCTOR
)
1303 pp_c_brace_enclosed_initializer_list (pp
, e
);
1305 pp_expression (pp
, e
);
1310 declarator = initializer */
1313 pp_c_init_declarator (c_pretty_printer
*pp
, tree t
)
1315 pp_declarator (pp
, t
);
1316 /* We don't want to output function definitions here. There are handled
1317 elsewhere (and the syntactic form is bogus anyway). */
1318 if (TREE_CODE (t
) != FUNCTION_DECL
&& DECL_INITIAL (t
))
1320 tree init
= DECL_INITIAL (t
);
1321 /* This C++ bit is handled here because it is easier to do so.
1322 In templates, the C++ parser builds a TREE_LIST for a
1323 direct-initialization; the TREE_PURPOSE is the variable to
1324 initialize and the TREE_VALUE is the initializer. */
1325 if (TREE_CODE (init
) == TREE_LIST
)
1327 pp_c_left_paren (pp
);
1328 pp_expression (pp
, TREE_VALUE (init
));
1329 pp_right_paren (pp
);
1336 pp_c_initializer (pp
, init
);
1341 /* initializer-list:
1342 designation(opt) initializer
1343 initializer-list , designation(opt) initializer
1350 designator-list designator
1353 [ constant-expression ]
1357 pp_c_initializer_list (c_pretty_printer
*pp
, tree e
)
1359 tree type
= TREE_TYPE (e
);
1360 const enum tree_code code
= TREE_CODE (type
);
1362 if (TREE_CODE (e
) == CONSTRUCTOR
)
1364 pp_c_constructor_elts (pp
, CONSTRUCTOR_ELTS (e
));
1374 tree init
= TREE_OPERAND (e
, 0);
1375 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
1377 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1380 pp_c_primary_expression (pp
, TREE_PURPOSE (init
));
1384 pp_c_left_bracket (pp
);
1385 if (TREE_PURPOSE (init
))
1386 pp_c_constant (pp
, TREE_PURPOSE (init
));
1387 pp_c_right_bracket (pp
);
1389 pp_c_whitespace (pp
);
1391 pp_c_whitespace (pp
);
1392 pp_initializer (pp
, TREE_VALUE (init
));
1393 if (TREE_CHAIN (init
))
1394 pp_separate_with (pp
, ',');
1400 if (TREE_CODE (e
) == VECTOR_CST
)
1403 for (i
= 0; i
< VECTOR_CST_NELTS (e
); ++i
)
1406 pp_separate_with (pp
, ',');
1407 pp_expression (pp
, VECTOR_CST_ELT (e
, i
));
1415 if (TREE_CODE (e
) == COMPLEX_CST
|| TREE_CODE (e
) == COMPLEX_EXPR
)
1417 const bool cst
= TREE_CODE (e
) == COMPLEX_CST
;
1418 pp_expression (pp
, cst
? TREE_REALPART (e
) : TREE_OPERAND (e
, 0));
1419 pp_separate_with (pp
, ',');
1420 pp_expression (pp
, cst
? TREE_IMAGPART (e
) : TREE_OPERAND (e
, 1));
1430 pp_unsupported_tree (pp
, type
);
1433 /* Pretty-print a brace-enclosed initializer-list. */
1436 pp_c_brace_enclosed_initializer_list (c_pretty_printer
*pp
, tree l
)
1438 pp_c_left_brace (pp
);
1439 pp_c_initializer_list (pp
, l
);
1440 pp_c_right_brace (pp
);
1444 /* This is a convenient function, used to bridge gap between C and C++
1451 pp_c_id_expression (c_pretty_printer
*pp
, tree t
)
1453 switch (TREE_CODE (t
))
1462 pp_c_tree_decl_identifier (pp
, t
);
1465 case IDENTIFIER_NODE
:
1466 pp_c_tree_identifier (pp
, t
);
1470 pp_unsupported_tree (pp
, t
);
1475 /* postfix-expression:
1477 postfix-expression [ expression ]
1478 postfix-expression ( argument-expression-list(opt) )
1479 postfix-expression . identifier
1480 postfix-expression -> identifier
1481 postfix-expression ++
1482 postfix-expression --
1483 ( type-name ) { initializer-list }
1484 ( type-name ) { initializer-list , } */
1487 pp_c_postfix_expression (c_pretty_printer
*pp
, tree e
)
1489 enum tree_code code
= TREE_CODE (e
);
1492 case POSTINCREMENT_EXPR
:
1493 case POSTDECREMENT_EXPR
:
1494 pp_postfix_expression (pp
, TREE_OPERAND (e
, 0));
1495 pp_string (pp
, code
== POSTINCREMENT_EXPR
? "++" : "--");
1499 pp_postfix_expression (pp
, TREE_OPERAND (e
, 0));
1500 pp_c_left_bracket (pp
);
1501 pp_expression (pp
, TREE_OPERAND (e
, 1));
1502 pp_c_right_bracket (pp
);
1507 call_expr_arg_iterator iter
;
1509 pp_postfix_expression (pp
, CALL_EXPR_FN (e
));
1510 pp_c_left_paren (pp
);
1511 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, e
)
1513 pp_expression (pp
, arg
);
1514 if (more_call_expr_args_p (&iter
))
1515 pp_separate_with (pp
, ',');
1517 pp_c_right_paren (pp
);
1521 case UNORDERED_EXPR
:
1522 pp_c_ws_string (pp
, flag_isoc99
1524 : "__builtin_isunordered");
1528 pp_c_ws_string (pp
, flag_isoc99
1530 : "!__builtin_isunordered");
1534 pp_c_ws_string (pp
, flag_isoc99
1536 : "!__builtin_isgreaterequal");
1540 pp_c_ws_string (pp
, flag_isoc99
1542 : "!__builtin_isgreater");
1546 pp_c_ws_string (pp
, flag_isoc99
1548 : "!__builtin_islessequal");
1552 pp_c_ws_string (pp
, flag_isoc99
1554 : "!__builtin_isless");
1558 pp_c_ws_string (pp
, flag_isoc99
1560 : "!__builtin_islessgreater");
1564 pp_c_ws_string (pp
, flag_isoc99
1566 : "__builtin_islessgreater");
1570 pp_c_left_paren (pp
);
1571 pp_expression (pp
, TREE_OPERAND (e
, 0));
1572 pp_separate_with (pp
, ',');
1573 pp_expression (pp
, TREE_OPERAND (e
, 1));
1574 pp_c_right_paren (pp
);
1578 pp_c_ws_string (pp
, "__builtin_abs");
1579 pp_c_left_paren (pp
);
1580 pp_expression (pp
, TREE_OPERAND (e
, 0));
1581 pp_c_right_paren (pp
);
1586 tree object
= TREE_OPERAND (e
, 0);
1587 if (TREE_CODE (object
) == INDIRECT_REF
)
1589 pp_postfix_expression (pp
, TREE_OPERAND (object
, 0));
1594 pp_postfix_expression (pp
, object
);
1597 pp_expression (pp
, TREE_OPERAND (e
, 1));
1603 tree type
= TREE_TYPE (e
);
1605 type
= signed_or_unsigned_type_for (TYPE_UNSIGNED (type
), type
);
1607 && tree_int_cst_equal (TYPE_SIZE (type
), TREE_OPERAND (e
, 1)))
1609 HOST_WIDE_INT bitpos
= tree_low_cst (TREE_OPERAND (e
, 2), 0);
1610 HOST_WIDE_INT size
= tree_low_cst (TYPE_SIZE (type
), 0);
1611 if ((bitpos
% size
) == 0)
1613 pp_c_left_paren (pp
);
1614 pp_c_left_paren (pp
);
1615 pp_type_id (pp
, type
);
1617 pp_c_right_paren (pp
);
1618 pp_c_ampersand (pp
);
1619 pp_expression (pp
, TREE_OPERAND (e
, 0));
1620 pp_c_right_paren (pp
);
1621 pp_c_left_bracket (pp
);
1622 pp_wide_integer (pp
, bitpos
/ size
);
1623 pp_c_right_bracket (pp
);
1627 pp_unsupported_tree (pp
, e
);
1632 pp_c_expression (pp
, e
);
1637 pp_c_compound_literal (pp
, e
);
1641 pp_c_complex_expr (pp
, e
);
1644 case COMPOUND_LITERAL_EXPR
:
1645 e
= DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e
));
1648 pp_initializer (pp
, e
);
1652 pp_c_ws_string (pp
, "__builtin_va_arg");
1653 pp_c_left_paren (pp
);
1654 pp_assignment_expression (pp
, TREE_OPERAND (e
, 0));
1655 pp_separate_with (pp
, ',');
1656 pp_type_id (pp
, TREE_TYPE (e
));
1657 pp_c_right_paren (pp
);
1661 if (TREE_CODE (TREE_OPERAND (e
, 0)) == FUNCTION_DECL
)
1663 pp_c_id_expression (pp
, TREE_OPERAND (e
, 0));
1666 /* else fall through. */
1669 pp_primary_expression (pp
, e
);
1674 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1677 pp_c_expression_list (c_pretty_printer
*pp
, tree e
)
1679 for (; e
!= NULL_TREE
; e
= TREE_CHAIN (e
))
1681 pp_expression (pp
, TREE_VALUE (e
));
1683 pp_separate_with (pp
, ',');
1687 /* Print out V, which contains the elements of a constructor. */
1690 pp_c_constructor_elts (c_pretty_printer
*pp
, vec
<constructor_elt
, va_gc
> *v
)
1692 unsigned HOST_WIDE_INT ix
;
1695 FOR_EACH_CONSTRUCTOR_VALUE (v
, ix
, value
)
1697 pp_expression (pp
, value
);
1698 if (ix
!= vec_safe_length (v
) - 1)
1699 pp_separate_with (pp
, ',');
1703 /* Print out an expression-list in parens, as if it were the argument
1704 list to a function. */
1707 pp_c_call_argument_list (c_pretty_printer
*pp
, tree t
)
1709 pp_c_left_paren (pp
);
1710 if (t
&& TREE_CODE (t
) == TREE_LIST
)
1711 pp_c_expression_list (pp
, t
);
1712 pp_c_right_paren (pp
);
1715 /* unary-expression:
1719 unary-operator cast-expression
1720 sizeof unary-expression
1723 unary-operator: one of
1728 __alignof__ unary-expression
1729 __alignof__ ( type-id )
1730 __real__ unary-expression
1731 __imag__ unary-expression */
1734 pp_c_unary_expression (c_pretty_printer
*pp
, tree e
)
1736 enum tree_code code
= TREE_CODE (e
);
1739 case PREINCREMENT_EXPR
:
1740 case PREDECREMENT_EXPR
:
1741 pp_string (pp
, code
== PREINCREMENT_EXPR
? "++" : "--");
1742 pp_c_unary_expression (pp
, TREE_OPERAND (e
, 0));
1749 case TRUTH_NOT_EXPR
:
1751 /* String literal are used by address. */
1752 if (code
== ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (e
, 0)) != STRING_CST
)
1754 else if (code
== INDIRECT_REF
)
1756 else if (code
== NEGATE_EXPR
)
1758 else if (code
== BIT_NOT_EXPR
|| code
== CONJ_EXPR
)
1760 else if (code
== TRUTH_NOT_EXPR
)
1761 pp_exclamation (pp
);
1762 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1766 if (TREE_CODE (TREE_OPERAND (e
, 0)) == ADDR_EXPR
1767 && integer_zerop (TREE_OPERAND (e
, 1)))
1768 pp_c_expression (pp
, TREE_OPERAND (TREE_OPERAND (e
, 0), 0));
1772 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1774 pp_c_left_paren (pp
);
1775 if (!integer_onep (TYPE_SIZE_UNIT
1776 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e
, 0))))))
1777 pp_c_type_cast (pp
, ptr_type_node
);
1779 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1780 if (!integer_zerop (TREE_OPERAND (e
, 1)))
1783 pp_c_integer_constant (pp
,
1784 fold_convert (ssizetype
,
1785 TREE_OPERAND (e
, 1)));
1786 pp_c_right_paren (pp
);
1793 pp_c_ws_string (pp
, code
== REALPART_EXPR
? "__real__" : "__imag__");
1794 pp_c_whitespace (pp
);
1795 pp_unary_expression (pp
, TREE_OPERAND (e
, 0));
1799 pp_postfix_expression (pp
, e
);
1806 ( type-name ) cast-expression */
1809 pp_c_cast_expression (c_pretty_printer
*pp
, tree e
)
1811 switch (TREE_CODE (e
))
1814 case FIX_TRUNC_EXPR
:
1816 case VIEW_CONVERT_EXPR
:
1817 pp_c_type_cast (pp
, TREE_TYPE (e
));
1818 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 0));
1822 pp_unary_expression (pp
, e
);
1826 /* multiplicative-expression:
1828 multiplicative-expression * cast-expression
1829 multiplicative-expression / cast-expression
1830 multiplicative-expression % cast-expression */
1833 pp_c_multiplicative_expression (c_pretty_printer
*pp
, tree e
)
1835 enum tree_code code
= TREE_CODE (e
);
1839 case TRUNC_DIV_EXPR
:
1840 case TRUNC_MOD_EXPR
:
1841 pp_multiplicative_expression (pp
, TREE_OPERAND (e
, 0));
1842 pp_c_whitespace (pp
);
1843 if (code
== MULT_EXPR
)
1845 else if (code
== TRUNC_DIV_EXPR
)
1849 pp_c_whitespace (pp
);
1850 pp_c_cast_expression (pp
, TREE_OPERAND (e
, 1));
1854 pp_c_cast_expression (pp
, e
);
1859 /* additive-expression:
1860 multiplicative-expression
1861 additive-expression + multiplicative-expression
1862 additive-expression - multiplicative-expression */
1865 pp_c_additive_expression (c_pretty_printer
*pp
, tree e
)
1867 enum tree_code code
= TREE_CODE (e
);
1870 case POINTER_PLUS_EXPR
:
1873 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 0));
1874 pp_c_whitespace (pp
);
1875 if (code
== PLUS_EXPR
|| code
== POINTER_PLUS_EXPR
)
1879 pp_c_whitespace (pp
);
1880 pp_multiplicative_expression (pp
, TREE_OPERAND (e
, 1));
1884 pp_multiplicative_expression (pp
, e
);
1889 /* additive-expression:
1891 shift-expression << additive-expression
1892 shift-expression >> additive-expression */
1895 pp_c_shift_expression (c_pretty_printer
*pp
, tree e
)
1897 enum tree_code code
= TREE_CODE (e
);
1902 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 0));
1903 pp_c_whitespace (pp
);
1904 pp_string (pp
, code
== LSHIFT_EXPR
? "<<" : ">>");
1905 pp_c_whitespace (pp
);
1906 pp_c_additive_expression (pp
, TREE_OPERAND (e
, 1));
1910 pp_c_additive_expression (pp
, e
);
1914 /* relational-expression:
1916 relational-expression < shift-expression
1917 relational-expression > shift-expression
1918 relational-expression <= shift-expression
1919 relational-expression >= shift-expression */
1922 pp_c_relational_expression (c_pretty_printer
*pp
, tree e
)
1924 enum tree_code code
= TREE_CODE (e
);
1931 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 0));
1932 pp_c_whitespace (pp
);
1933 if (code
== LT_EXPR
)
1935 else if (code
== GT_EXPR
)
1937 else if (code
== LE_EXPR
)
1938 pp_string (pp
, "<=");
1939 else if (code
== GE_EXPR
)
1940 pp_string (pp
, ">=");
1941 pp_c_whitespace (pp
);
1942 pp_c_shift_expression (pp
, TREE_OPERAND (e
, 1));
1946 pp_c_shift_expression (pp
, e
);
1951 /* equality-expression:
1952 relational-expression
1953 equality-expression == relational-expression
1954 equality-equality != relational-expression */
1957 pp_c_equality_expression (c_pretty_printer
*pp
, tree e
)
1959 enum tree_code code
= TREE_CODE (e
);
1964 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 0));
1965 pp_c_whitespace (pp
);
1966 pp_string (pp
, code
== EQ_EXPR
? "==" : "!=");
1967 pp_c_whitespace (pp
);
1968 pp_c_relational_expression (pp
, TREE_OPERAND (e
, 1));
1972 pp_c_relational_expression (pp
, e
);
1979 AND-expression & equality-equality */
1982 pp_c_and_expression (c_pretty_printer
*pp
, tree e
)
1984 if (TREE_CODE (e
) == BIT_AND_EXPR
)
1986 pp_c_and_expression (pp
, TREE_OPERAND (e
, 0));
1987 pp_c_whitespace (pp
);
1989 pp_c_whitespace (pp
);
1990 pp_c_equality_expression (pp
, TREE_OPERAND (e
, 1));
1993 pp_c_equality_expression (pp
, e
);
1996 /* exclusive-OR-expression:
1998 exclusive-OR-expression ^ AND-expression */
2001 pp_c_exclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2003 if (TREE_CODE (e
) == BIT_XOR_EXPR
2004 || TREE_CODE (e
) == TRUTH_XOR_EXPR
)
2006 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2007 if (TREE_CODE (e
) == BIT_XOR_EXPR
)
2008 pp_c_maybe_whitespace (pp
);
2010 pp_c_whitespace (pp
);
2012 pp_c_whitespace (pp
);
2013 pp_c_and_expression (pp
, TREE_OPERAND (e
, 1));
2016 pp_c_and_expression (pp
, e
);
2019 /* inclusive-OR-expression:
2020 exclusive-OR-expression
2021 inclusive-OR-expression | exclusive-OR-expression */
2024 pp_c_inclusive_or_expression (c_pretty_printer
*pp
, tree e
)
2026 if (TREE_CODE (e
) == BIT_IOR_EXPR
)
2028 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 0));
2029 pp_c_whitespace (pp
);
2031 pp_c_whitespace (pp
);
2032 pp_c_exclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2035 pp_c_exclusive_or_expression (pp
, e
);
2038 /* logical-AND-expression:
2039 inclusive-OR-expression
2040 logical-AND-expression && inclusive-OR-expression */
2043 pp_c_logical_and_expression (c_pretty_printer
*pp
, tree e
)
2045 if (TREE_CODE (e
) == TRUTH_ANDIF_EXPR
2046 || TREE_CODE (e
) == TRUTH_AND_EXPR
)
2048 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 0));
2049 pp_c_whitespace (pp
);
2050 pp_string (pp
, "&&");
2051 pp_c_whitespace (pp
);
2052 pp_c_inclusive_or_expression (pp
, TREE_OPERAND (e
, 1));
2055 pp_c_inclusive_or_expression (pp
, e
);
2058 /* logical-OR-expression:
2059 logical-AND-expression
2060 logical-OR-expression || logical-AND-expression */
2063 pp_c_logical_or_expression (c_pretty_printer
*pp
, tree e
)
2065 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
2066 || TREE_CODE (e
) == TRUTH_OR_EXPR
)
2068 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2069 pp_c_whitespace (pp
);
2070 pp_string (pp
, "||");
2071 pp_c_whitespace (pp
);
2072 pp_c_logical_and_expression (pp
, TREE_OPERAND (e
, 1));
2075 pp_c_logical_and_expression (pp
, e
);
2078 /* conditional-expression:
2079 logical-OR-expression
2080 logical-OR-expression ? expression : conditional-expression */
2083 pp_c_conditional_expression (c_pretty_printer
*pp
, tree e
)
2085 if (TREE_CODE (e
) == COND_EXPR
)
2087 pp_c_logical_or_expression (pp
, TREE_OPERAND (e
, 0));
2088 pp_c_whitespace (pp
);
2090 pp_c_whitespace (pp
);
2091 pp_expression (pp
, TREE_OPERAND (e
, 1));
2092 pp_c_whitespace (pp
);
2094 pp_c_whitespace (pp
);
2095 pp_c_conditional_expression (pp
, TREE_OPERAND (e
, 2));
2098 pp_c_logical_or_expression (pp
, e
);
2102 /* assignment-expression:
2103 conditional-expression
2104 unary-expression assignment-operator assignment-expression
2106 assignment-expression: one of
2107 = *= /= %= += -= >>= <<= &= ^= |= */
2110 pp_c_assignment_expression (c_pretty_printer
*pp
, tree e
)
2112 if (TREE_CODE (e
) == MODIFY_EXPR
2113 || TREE_CODE (e
) == INIT_EXPR
)
2115 pp_c_unary_expression (pp
, TREE_OPERAND (e
, 0));
2116 pp_c_whitespace (pp
);
2119 pp_c_expression (pp
, TREE_OPERAND (e
, 1));
2122 pp_c_conditional_expression (pp
, e
);
2126 assignment-expression
2127 expression , assignment-expression
2129 Implementation note: instead of going through the usual recursion
2130 chain, I take the liberty of dispatching nodes to the appropriate
2131 functions. This makes some redundancy, but it worths it. That also
2132 prevents a possible infinite recursion between pp_c_primary_expression ()
2133 and pp_c_expression (). */
2136 pp_c_expression (c_pretty_printer
*pp
, tree e
)
2138 switch (TREE_CODE (e
))
2141 pp_c_integer_constant (pp
, e
);
2145 pp_c_floating_constant (pp
, e
);
2149 pp_c_fixed_constant (pp
, e
);
2153 pp_c_string_literal (pp
, e
);
2156 case IDENTIFIER_NODE
:
2165 pp_primary_expression (pp
, e
);
2169 if (SSA_NAME_VAR (e
)
2170 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e
)))
2171 pp_c_expression (pp
, SSA_NAME_VAR (e
));
2173 pp_c_ws_string (pp
, M_("<unknown>"));
2176 case POSTINCREMENT_EXPR
:
2177 case POSTDECREMENT_EXPR
:
2186 case UNORDERED_EXPR
:
2195 case COMPOUND_LITERAL_EXPR
:
2197 pp_postfix_expression (pp
, e
);
2206 case TRUTH_NOT_EXPR
:
2207 case PREINCREMENT_EXPR
:
2208 case PREDECREMENT_EXPR
:
2211 pp_c_unary_expression (pp
, e
);
2215 case FIX_TRUNC_EXPR
:
2217 case VIEW_CONVERT_EXPR
:
2218 pp_c_cast_expression (pp
, e
);
2222 case TRUNC_MOD_EXPR
:
2223 case TRUNC_DIV_EXPR
:
2224 pp_multiplicative_expression (pp
, e
);
2229 pp_c_shift_expression (pp
, e
);
2236 pp_c_relational_expression (pp
, e
);
2240 pp_c_and_expression (pp
, e
);
2244 case TRUTH_XOR_EXPR
:
2245 pp_c_exclusive_or_expression (pp
, e
);
2249 pp_c_inclusive_or_expression (pp
, e
);
2252 case TRUTH_ANDIF_EXPR
:
2253 case TRUTH_AND_EXPR
:
2254 pp_c_logical_and_expression (pp
, e
);
2257 case TRUTH_ORIF_EXPR
:
2259 pp_c_logical_or_expression (pp
, e
);
2264 pp_c_equality_expression (pp
, e
);
2268 pp_conditional_expression (pp
, e
);
2271 case POINTER_PLUS_EXPR
:
2274 pp_c_additive_expression (pp
, e
);
2279 pp_assignment_expression (pp
, e
);
2283 pp_c_left_paren (pp
);
2284 pp_expression (pp
, TREE_OPERAND (e
, 0));
2285 pp_separate_with (pp
, ',');
2286 pp_assignment_expression (pp
, TREE_OPERAND (e
, 1));
2287 pp_c_right_paren (pp
);
2290 case NON_LVALUE_EXPR
:
2292 pp_expression (pp
, TREE_OPERAND (e
, 0));
2296 pp_postfix_expression (pp
, TREE_OPERAND (e
, 1));
2301 /* We don't yet have a way of dumping statements in a
2302 human-readable format. */
2303 pp_string (pp
, "({...})");
2306 case C_MAYBE_CONST_EXPR
:
2307 pp_c_expression (pp
, C_MAYBE_CONST_EXPR_EXPR (e
));
2311 pp_unsupported_tree (pp
, e
);
2321 pp_c_statement (c_pretty_printer
*pp
, tree stmt
)
2326 if (pp_needs_newline (pp
))
2327 pp_newline_and_indent (pp
, 0);
2329 dump_generic_node (pp_base (pp
), stmt
, pp_indentation (pp
), 0, true);
2333 /* Initialize the PRETTY-PRINTER for handling C codes. */
2336 pp_c_pretty_printer_init (c_pretty_printer
*pp
)
2338 pp
->offset_list
= 0;
2340 pp
->declaration
= pp_c_declaration
;
2341 pp
->declaration_specifiers
= pp_c_declaration_specifiers
;
2342 pp
->declarator
= pp_c_declarator
;
2343 pp
->direct_declarator
= pp_c_direct_declarator
;
2344 pp
->type_specifier_seq
= pp_c_specifier_qualifier_list
;
2345 pp
->abstract_declarator
= pp_c_abstract_declarator
;
2346 pp
->direct_abstract_declarator
= pp_c_direct_abstract_declarator
;
2347 pp
->ptr_operator
= pp_c_pointer
;
2348 pp
->parameter_list
= pp_c_parameter_type_list
;
2349 pp
->type_id
= pp_c_type_id
;
2350 pp
->simple_type_specifier
= pp_c_type_specifier
;
2351 pp
->function_specifier
= pp_c_function_specifier
;
2352 pp
->storage_class_specifier
= pp_c_storage_class_specifier
;
2354 pp
->statement
= pp_c_statement
;
2356 pp
->constant
= pp_c_constant
;
2357 pp
->id_expression
= pp_c_id_expression
;
2358 pp
->primary_expression
= pp_c_primary_expression
;
2359 pp
->postfix_expression
= pp_c_postfix_expression
;
2360 pp
->unary_expression
= pp_c_unary_expression
;
2361 pp
->initializer
= pp_c_initializer
;
2362 pp
->multiplicative_expression
= pp_c_multiplicative_expression
;
2363 pp
->conditional_expression
= pp_c_conditional_expression
;
2364 pp
->assignment_expression
= pp_c_assignment_expression
;
2365 pp
->expression
= pp_c_expression
;
2369 /* Print the tree T in full, on file FILE. */
2372 print_c_tree (FILE *file
, tree t
)
2374 static c_pretty_printer pp_rec
;
2375 static bool initialized
= 0;
2376 c_pretty_printer
*pp
= &pp_rec
;
2381 pp_construct (pp_base (pp
), NULL
, 0);
2382 pp_c_pretty_printer_init (pp
);
2383 pp_needs_newline (pp
) = true;
2385 pp_base (pp
)->buffer
->stream
= file
;
2387 pp_statement (pp
, t
);
2389 pp_newline_and_flush (pp
);
2392 /* Print the tree T in full, on stderr. */
2395 debug_c_tree (tree t
)
2397 print_c_tree (stderr
, t
);
2398 fputc ('\n', stderr
);
2401 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2402 up of T's memory address. */
2405 pp_c_tree_decl_identifier (c_pretty_printer
*pp
, tree t
)
2409 gcc_assert (DECL_P (t
));
2412 name
= IDENTIFIER_POINTER (DECL_NAME (t
));
2415 static char xname
[8];
2416 sprintf (xname
, "<U%4x>", ((unsigned)((uintptr_t)(t
) & 0xffff)));
2420 pp_c_identifier (pp
, name
);