1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "c-pretty-print.h"
28 static void pp_c_char PARAMS ((c_pretty_print_info *, int));
29 static void pp_c_character_literal PARAMS ((c_pretty_print_info *, tree));
30 static void pp_c_bool_literal PARAMS ((c_pretty_print_info *, tree));
31 static bool pp_c_enumerator PARAMS ((c_pretty_print_info *, tree));
32 static void pp_c_integer_literal PARAMS ((c_pretty_print_info *, tree));
33 static void pp_c_real_literal PARAMS ((c_pretty_print_info *, tree));
34 static void pp_c_string_literal PARAMS ((c_pretty_print_info *, tree));
36 static void pp_c_primary_expression PARAMS ((c_pretty_print_info *, tree));
38 /* postfix-expression */
39 static void pp_c_initializer_list PARAMS ((c_pretty_print_info *, tree));
41 static void pp_c_unary_expression PARAMS ((c_pretty_print_info *, tree));
42 static void pp_c_multiplicative_expression PARAMS ((c_pretty_print_info *,
44 static void pp_c_additive_expression PARAMS ((c_pretty_print_info *, tree));
45 static void pp_c_shift_expression PARAMS ((c_pretty_print_info *, tree));
46 static void pp_c_relational_expression PARAMS ((c_pretty_print_info *, tree));
47 static void pp_c_equality_expression PARAMS ((c_pretty_print_info *, tree));
48 static void pp_c_and_expression PARAMS ((c_pretty_print_info *, tree));
49 static void pp_c_exclusive_or_expression PARAMS ((c_pretty_print_info *,
51 static void pp_c_inclusive_or_expression PARAMS ((c_pretty_print_info *,
53 static void pp_c_logical_and_expression PARAMS ((c_pretty_print_info *, tree));
54 static void pp_c_conditional_expression PARAMS ((c_pretty_print_info *, tree));
55 static void pp_c_assignment_expression PARAMS ((c_pretty_print_info *, tree));
59 /* Print out CV-qualifiers. Take care of possible extension. */
61 pp_c_cv_qualifier (ppi, cv)
62 c_pretty_print_info *ppi;
65 if (cv & TYPE_QUAL_CONST)
66 pp_c_identifier (ppi, "const");
67 if (cv & TYPE_QUAL_VOLATILE)
68 pp_c_identifier (ppi, "volatile");
69 if (cv & TYPE_QUAL_RESTRICT)
70 pp_c_identifier (ppi, flag_isoc99 ? "restrict" : "__restrict__");
79 /* Print out a c-char. */
82 c_pretty_print_info *ppi;
88 pp_identifier (ppi, "\\n");
91 pp_identifier (ppi, "\\t");
94 pp_identifier (ppi, "\\v");
97 pp_identifier (ppi, "\\b");
100 pp_identifier (ppi, "\\r");
103 pp_identifier (ppi, "\\f");
106 pp_identifier (ppi, "\\a");
109 pp_identifier (ppi, "\\\\");
112 pp_identifier (ppi, "\\'");
115 pp_identifier (ppi, "\\\"");
119 pp_character (ppi, c);
121 pp_format_integer (ppi, "\\%03o", (unsigned) c);
126 /* Print out a STRING literal. */
128 pp_c_string_literal (ppi, s)
129 c_pretty_print_info *ppi;
132 const char *p = TREE_STRING_POINTER (s);
133 int n = TREE_STRING_LENGTH (s) - 1;
135 pp_doublequote (ppi);
136 for (i = 0; i < n; ++i)
137 pp_c_char (ppi, p[i]);
138 pp_doublequote (ppi);
141 /* Print out a CHARACTER literal. */
143 pp_c_character_literal (ppi, c)
144 c_pretty_print_info *ppi;
148 pp_c_char (ppi, tree_low_cst (c, 0));
152 /* Print out a BOOLEAN literal. */
154 pp_c_bool_literal (ppi, b)
155 c_pretty_print_info *ppi;
158 if (b == boolean_false_node || integer_zerop (b))
160 if (c_language == clk_cplusplus)
161 pp_c_identifier (ppi, "false");
162 else if (c_language == clk_c && flag_isoc99)
163 pp_c_identifier (ppi, "_False");
165 pp_unsupported_tree (ppi, b);
167 else if (b == boolean_true_node)
169 if (c_language == clk_cplusplus)
170 pp_c_identifier (ppi, "true");
171 else if (c_language == clk_c && flag_isoc99)
172 pp_c_identifier (ppi, "_True");
174 pp_unsupported_tree (ppi, b);
177 pp_unsupported_tree (ppi, b);
180 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
181 false; that means the value was obtained by a cast, in which case
182 print out the type-id part of the cast-expression -- the casted value
183 is then printed by pp_c_integer_literal. */
185 pp_c_enumerator (ppi, e)
186 c_pretty_print_info *ppi;
189 tree type = TREE_TYPE (e);
192 /* Find the name of this constant. */
193 for (value = TYPE_VALUES (type);
194 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
195 value = TREE_CHAIN (value))
198 if (value != NULL_TREE)
199 pp_c_tree_identifier (ppi, TREE_PURPOSE (value));
202 /* Value must have been cast. */
203 pp_c_left_paren (ppi);
204 pp_type_id (ppi, type);
205 pp_c_right_paren (ppi);
212 /* Print out an INTEGER constant value. */
214 pp_c_integer_literal (ppi, i)
215 c_pretty_print_info *ppi;
218 tree type = TREE_TYPE (i);
220 if (type == boolean_type_node)
221 pp_c_bool_literal (ppi, i);
222 else if (type == char_type_node)
223 pp_c_character_literal (ppi, i);
224 else if (TREE_CODE (type) == ENUMERAL_TYPE
225 && pp_c_enumerator (ppi, i))
229 if (host_integerp (i, 0))
230 pp_wide_integer (ppi, TREE_INT_CST_LOW (i));
233 if (tree_int_cst_sgn (i) < 0)
235 static char format[10]; /* "%x%09999x\0" */
237 sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
239 pp_c_char (ppi, '-');
240 i = build_int_2 (-TREE_INT_CST_LOW (i),
241 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
242 sprintf (pp_buffer (ppi)->digit_buffer, format,
243 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
244 pp_identifier (ppi, pp_buffer (ppi)->digit_buffer);
251 /* Print out a REAL value. */
253 pp_c_real_literal (ppi, r)
254 c_pretty_print_info *ppi;
257 REAL_VALUE_TO_DECIMAL (TREE_REAL_CST (r), "%.16g",
258 pp_buffer (ppi)->digit_buffer);
259 pp_identifier (ppi, pp_buffer(ppi)->digit_buffer);
264 pp_c_literal (ppi, e)
265 c_pretty_print_info *ppi;
268 switch (TREE_CODE (e))
271 pp_c_integer_literal (ppi, e);
275 pp_c_real_literal (ppi, e);
279 pp_c_string_literal (ppi, e);
283 pp_unsupported_tree (ppi, e);
288 /* Pretty-print a C primary-expression. */
290 pp_c_primary_expression (ppi, e)
291 c_pretty_print_info *ppi;
294 switch (TREE_CODE (e))
304 case IDENTIFIER_NODE:
305 pp_c_tree_identifier (ppi, e);
309 pp_c_identifier (ppi, "<erroneous-expression>");
313 pp_c_identifier (ppi, "<return-value>");
319 pp_c_literal (ppi, e);
323 pp_c_left_paren (ppi);
324 pp_c_identifier (ppi, "__builtin_memcpy");
325 pp_c_left_paren (ppi);
327 pp_c_primary_expression (ppi, TREE_OPERAND (e, 0));
328 pp_separate_with (ppi, ',');
330 pp_initializer (ppi, TREE_OPERAND (e, 1));
331 if (TREE_OPERAND (e, 2))
333 pp_separate_with (ppi, ',');
334 pp_c_expression (ppi, TREE_OPERAND (e, 2));
336 pp_c_right_paren (ppi);
339 /* Make sure this call won't cause any infinite loop. */
340 pp_c_left_paren (ppi);
341 pp_c_expression (ppi, e);
342 pp_c_right_paren (ppi);
347 /* Print out a C initializer -- also support C compound-literals. */
349 pp_c_initializer (ppi, e)
350 c_pretty_print_info *ppi;
353 if (TREE_CODE (e) == CONSTRUCTOR)
355 enum tree_code code = TREE_CODE (TREE_TYPE (e));
356 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
359 pp_c_initializer_list (ppi, e);
360 pp_right_brace (ppi);
363 pp_unsupported_tree (ppi, TREE_OPERAND (e, 1));
366 pp_assignment_expression (ppi, e);
370 pp_c_initializer_list (ppi, e)
371 c_pretty_print_info *ppi;
374 tree type = TREE_TYPE (e);
375 const enum tree_code code = TREE_CODE (type);
377 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
379 tree init = TREE_OPERAND (e, 1);
380 for (; init != NULL_TREE; init = TREE_CHAIN (init))
382 if (code == RECORD_TYPE || code == UNION_TYPE)
385 pp_c_primary_expression (ppi, TREE_PURPOSE (init));
389 pp_c_left_bracket (ppi);
390 if (TREE_PURPOSE (init))
391 pp_c_literal (ppi, TREE_PURPOSE (init));
392 pp_c_right_bracket (ppi);
394 pp_c_whitespace (ppi);
396 pp_c_whitespace (ppi);
397 pp_initializer (ppi, TREE_VALUE (init));
398 if (TREE_CHAIN (init))
399 pp_separate_with (ppi, ',');
403 pp_unsupported_tree (ppi, type);
407 pp_c_postfix_expression (ppi, e)
408 c_pretty_print_info *ppi;
411 enum tree_code code = TREE_CODE (e);
414 case POSTINCREMENT_EXPR:
415 case POSTDECREMENT_EXPR:
416 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
417 pp_identifier (ppi, code == POSTINCREMENT_EXPR ? "++" : "--");
421 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
422 pp_c_left_bracket (ppi);
423 pp_c_expression (ppi, TREE_OPERAND (e, 1));
424 pp_c_right_bracket (ppi);
428 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
429 pp_c_left_paren (ppi);
430 pp_c_expression_list (ppi, TREE_OPERAND (e, 1));
431 pp_c_right_paren (ppi);
435 pp_c_identifier (ppi, "abs");
436 pp_c_left_paren (ppi);
437 pp_c_expression (ppi, TREE_OPERAND (e, 0));
438 pp_c_right_paren (ppi);
443 tree object = TREE_OPERAND (e, 0);
444 if (TREE_CODE (object) == INDIRECT_REF)
446 pp_postfix_expression (ppi, TREE_OPERAND (object, 0));
451 pp_postfix_expression (ppi, object);
454 pp_c_expression (ppi, TREE_OPERAND (e, 1));
461 pp_c_left_paren (ppi);
462 pp_type_id (ppi, TREE_TYPE (e));
463 pp_c_right_paren (ppi);
466 if (code == COMPLEX_CST)
468 pp_c_expression (ppi, TREE_REALPART (e));
469 pp_separate_with (ppi, ',');
470 pp_c_expression (ppi, TREE_IMAGPART (e));
472 else if (code == VECTOR_CST)
473 pp_c_expression_list (ppi, TREE_VECTOR_CST_ELTS (e));
474 else if (code == COMPLEX_EXPR)
476 pp_c_expression (ppi, TREE_OPERAND (e, 0));
477 pp_separate_with (ppi, ',');
478 pp_c_expression (ppi, TREE_OPERAND (e, 1));
481 pp_right_brace (ppi);
485 pp_initializer (ppi, e);
489 pp_primary_expression (ppi, e);
494 /* Print out an expession-list; E is expected to be a TREE_LIST */
496 pp_c_expression_list (ppi, e)
497 c_pretty_print_info *ppi;
500 for (; e != NULL_TREE; e = TREE_CHAIN (e))
502 pp_c_assignment_expression (ppi, TREE_VALUE (e));
504 pp_separate_with (ppi, ',');
509 pp_c_unary_expression (ppi, e)
510 c_pretty_print_info *ppi;
513 enum tree_code code = TREE_CODE (e);
516 case PREINCREMENT_EXPR:
517 case PREDECREMENT_EXPR:
518 pp_identifier (ppi, code == PREINCREMENT_EXPR ? "++" : "--");
519 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
529 if (code == ADDR_EXPR)
531 else if (code == INDIRECT_REF)
533 else if (code == NEGATE_EXPR)
535 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
537 else if (code == TRUTH_NOT_EXPR)
538 pp_exclamation (ppi);
539 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
544 pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
545 pp_c_whitespace (ppi);
546 if (TYPE_P (TREE_OPERAND (e, 0)))
548 pp_c_left_paren (ppi);
549 pp_type_id (ppi, TREE_OPERAND (e, 0));
550 pp_c_right_paren (ppi);
553 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
558 pp_c_identifier (ppi, code == REALPART_EXPR ? "__real__" : "__imag__");
559 pp_c_whitespace (ppi);
560 pp_unary_expression (ppi, TREE_OPERAND (e, 0));
564 pp_postfix_expression (ppi, e);
570 pp_c_cast_expression (ppi, e)
571 c_pretty_print_info *ppi;
574 if (TREE_CODE (e) == CONVERT_EXPR || TREE_CODE (e) == FLOAT_EXPR)
576 pp_c_left_paren (ppi);
577 pp_type_id (ppi, TREE_TYPE (e));
578 pp_c_right_paren (ppi);
579 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
582 pp_unary_expression (ppi, e);
586 pp_c_multiplicative_expression (ppi, e)
587 c_pretty_print_info *ppi;
590 enum tree_code code = TREE_CODE (e);
596 pp_c_multiplicative_expression (ppi, TREE_OPERAND (e, 0));
597 pp_c_whitespace (ppi);
598 if (code == MULT_EXPR)
600 else if (code == TRUNC_DIV_EXPR)
604 pp_c_whitespace (ppi);
605 pp_c_cast_expression (ppi, TREE_OPERAND (e, 1));
609 pp_c_cast_expression (ppi, e);
615 pp_c_additive_expression (ppi, e)
616 c_pretty_print_info *ppi;
619 enum tree_code code = TREE_CODE (e);
624 pp_c_additive_expression (ppi, TREE_OPERAND (e, 0));
625 pp_c_whitespace (ppi);
626 if (code == PLUS_EXPR)
630 pp_c_whitespace (ppi);
631 pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1));
635 pp_multiplicative_expression (ppi, e);
641 pp_c_shift_expression (ppi, e)
642 c_pretty_print_info *ppi;
645 enum tree_code code = TREE_CODE (e);
650 pp_c_shift_expression (ppi, TREE_OPERAND (e, 0));
651 pp_c_whitespace (ppi);
652 pp_identifier (ppi, code == LSHIFT_EXPR ? "<<" : ">>");
653 pp_c_whitespace (ppi);
654 pp_c_additive_expression (ppi, TREE_OPERAND (e, 1));
658 pp_c_additive_expression (ppi, e);
663 pp_c_relational_expression (ppi, e)
664 c_pretty_print_info *ppi;
667 enum tree_code code = TREE_CODE (e);
674 pp_c_relational_expression (ppi, TREE_OPERAND (e, 0));
675 pp_c_whitespace (ppi);
678 else if (code == GT_EXPR)
680 else if (code == LE_EXPR)
681 pp_identifier (ppi, "<=");
682 else if (code == GE_EXPR)
683 pp_identifier (ppi, ">=");
684 pp_c_whitespace (ppi);
685 pp_c_shift_expression (ppi, TREE_OPERAND (e, 1));
689 pp_c_shift_expression (ppi, e);
695 pp_c_equality_expression (ppi, e)
696 c_pretty_print_info *ppi;
699 enum tree_code code = TREE_CODE (e);
704 pp_c_equality_expression (ppi, TREE_OPERAND (e, 0));
705 pp_c_maybe_whitespace (ppi);
706 pp_identifier (ppi, code == EQ_EXPR ? "==" : "!=");
707 pp_c_whitespace (ppi);
708 pp_c_relational_expression (ppi, TREE_OPERAND (e, 1));
712 pp_c_relational_expression (ppi, e);
718 pp_c_and_expression (ppi, e)
719 c_pretty_print_info *ppi;
722 if (TREE_CODE (e) == BIT_AND_EXPR)
724 pp_c_and_expression (ppi, TREE_OPERAND (e, 0));
725 pp_c_maybe_whitespace (ppi);
727 pp_c_whitespace (ppi);
728 pp_c_equality_expression (ppi, TREE_OPERAND (e, 1));
731 pp_c_equality_expression (ppi, e);
735 pp_c_exclusive_or_expression (ppi, e)
736 c_pretty_print_info *ppi;
739 if (TREE_CODE (e) == BIT_XOR_EXPR)
741 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
742 pp_c_maybe_whitespace (ppi);
744 pp_c_whitespace (ppi);
745 pp_c_and_expression (ppi, TREE_OPERAND (e, 1));
748 pp_c_and_expression (ppi, e);
752 pp_c_inclusive_or_expression (ppi, e)
753 c_pretty_print_info *ppi;
756 if (TREE_CODE (e) == BIT_IOR_EXPR)
758 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
759 pp_c_maybe_whitespace (ppi);
761 pp_c_whitespace (ppi);
762 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1));
765 pp_c_exclusive_or_expression (ppi, e);
769 pp_c_logical_and_expression (ppi, e)
770 c_pretty_print_info *ppi;
773 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
775 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0));
776 pp_c_maybe_whitespace (ppi);
777 pp_identifier (ppi, "&&");
778 pp_c_whitespace (ppi);
779 pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1));
782 pp_c_inclusive_or_expression (ppi, e);
786 pp_c_logical_or_expression (ppi, e)
787 c_pretty_print_info *ppi;
790 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
792 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
793 pp_c_maybe_whitespace (ppi);
794 pp_identifier (ppi, "||");
795 pp_c_whitespace (ppi);
796 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1));
799 pp_c_logical_and_expression (ppi, e);
803 pp_c_conditional_expression (ppi, e)
804 c_pretty_print_info *ppi;
807 if (TREE_CODE (e) == COND_EXPR)
809 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
810 pp_c_maybe_whitespace (ppi);
812 pp_c_whitespace (ppi);
813 pp_c_expression (ppi, TREE_OPERAND (e, 1));
814 pp_c_maybe_whitespace (ppi);
816 pp_c_whitespace (ppi);
817 pp_c_conditional_expression (ppi, TREE_OPERAND (e, 2));
820 pp_c_logical_or_expression (ppi, e);
824 /* Pretty-print a C assignment-expression. */
826 pp_c_assignment_expression (ppi, e)
827 c_pretty_print_info *ppi;
830 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
832 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
833 pp_c_maybe_whitespace (ppi);
836 pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1));
839 pp_c_conditional_expression (ppi, e);
842 /* Pretty-print an expression. */
844 pp_c_expression (ppi, e)
845 c_pretty_print_info *ppi;
848 switch (TREE_CODE (e))
851 pp_c_integer_literal (ppi, e);
855 pp_c_real_literal (ppi, e);
859 pp_c_string_literal (ppi, e);
871 pp_c_primary_expression (ppi, e);
874 case POSTINCREMENT_EXPR:
875 case POSTDECREMENT_EXPR:
884 pp_c_postfix_expression (ppi, e);
893 case PREINCREMENT_EXPR:
894 case PREDECREMENT_EXPR:
899 pp_c_unary_expression (ppi, e);
904 pp_c_cast_expression (ppi, e);
910 pp_c_multiplicative_expression (ppi, e);
915 pp_c_shift_expression (ppi, e);
922 pp_c_relational_expression (ppi, e);
926 pp_c_and_expression (ppi, e);
930 pp_c_exclusive_or_expression (ppi, e);
934 pp_c_inclusive_or_expression (ppi, e);
937 case TRUTH_ANDIF_EXPR:
938 pp_c_logical_and_expression (ppi, e);
941 case TRUTH_ORIF_EXPR:
942 pp_c_logical_or_expression (ppi, e);
946 pp_c_conditional_expression (ppi, e);
951 pp_c_assignment_expression (ppi, e);
955 pp_c_expression (ppi, TREE_OPERAND (e, 0));
959 pp_c_left_paren (ppi);
960 pp_c_expression (ppi, TREE_OPERAND (e, 0));
961 pp_separate_with (ppi, ',');
962 pp_assignment_expression (ppi, TREE_OPERAND (e, 1));
963 pp_c_right_paren (ppi);
968 pp_unsupported_tree (ppi, e);