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 pp_c_left_paren (ppi);
340 pp_statement (ppi, STMT_EXPR_STMT (e));
341 pp_c_right_paren (ppi);
345 /* Make sure this call won't cause any infinite loop. */
346 pp_c_left_paren (ppi);
347 pp_c_expression (ppi, e);
348 pp_c_right_paren (ppi);
353 /* Print out a C initializer -- also support C compound-literals. */
355 pp_c_initializer (ppi, e)
356 c_pretty_print_info *ppi;
359 if (TREE_CODE (e) == CONSTRUCTOR)
361 enum tree_code code = TREE_CODE (TREE_TYPE (e));
362 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
365 pp_c_initializer_list (ppi, e);
366 pp_right_brace (ppi);
369 pp_unsupported_tree (ppi, TREE_OPERAND (e, 1));
372 pp_assignment_expression (ppi, e);
376 pp_c_initializer_list (ppi, e)
377 c_pretty_print_info *ppi;
380 tree type = TREE_TYPE (e);
381 const enum tree_code code = TREE_CODE (type);
383 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
385 tree init = TREE_OPERAND (e, 1);
386 for (; init != NULL_TREE; init = TREE_CHAIN (init))
388 if (code == RECORD_TYPE || code == UNION_TYPE)
391 pp_c_primary_expression (ppi, TREE_PURPOSE (init));
395 pp_c_left_bracket (ppi);
396 if (TREE_PURPOSE (init))
397 pp_c_literal (ppi, TREE_PURPOSE (init));
398 pp_c_right_bracket (ppi);
400 pp_c_whitespace (ppi);
402 pp_c_whitespace (ppi);
403 pp_initializer (ppi, TREE_VALUE (init));
404 if (TREE_CHAIN (init))
405 pp_separate_with (ppi, ',');
409 pp_unsupported_tree (ppi, type);
413 pp_c_postfix_expression (ppi, e)
414 c_pretty_print_info *ppi;
417 enum tree_code code = TREE_CODE (e);
420 case POSTINCREMENT_EXPR:
421 case POSTDECREMENT_EXPR:
422 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
423 pp_identifier (ppi, code == POSTINCREMENT_EXPR ? "++" : "--");
427 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
432 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
433 pp_c_left_bracket (ppi);
434 pp_c_expression (ppi, TREE_OPERAND (e, 1));
435 pp_c_right_bracket (ppi);
439 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
440 pp_c_left_paren (ppi);
441 pp_c_expression_list (ppi, TREE_OPERAND (e, 1));
442 pp_c_right_paren (ppi);
447 pp_c_identifier (ppi,
448 code == ABS_EXPR ? "__builtin_abs" : "__builtin_ffs");
449 pp_c_left_paren (ppi);
450 pp_c_expression (ppi, TREE_OPERAND (e, 0));
451 pp_c_right_paren (ppi);
456 tree object = TREE_OPERAND (e, 0);
457 if (TREE_CODE (object) == INDIRECT_REF)
459 pp_postfix_expression (ppi, TREE_OPERAND (object, 0));
464 pp_postfix_expression (ppi, object);
467 pp_c_expression (ppi, TREE_OPERAND (e, 1));
474 pp_c_left_paren (ppi);
475 pp_type_id (ppi, TREE_TYPE (e));
476 pp_c_right_paren (ppi);
479 if (code == COMPLEX_CST)
481 pp_c_expression (ppi, TREE_REALPART (e));
482 pp_separate_with (ppi, ',');
483 pp_c_expression (ppi, TREE_IMAGPART (e));
485 else if (code == VECTOR_CST)
486 pp_c_expression_list (ppi, TREE_VECTOR_CST_ELTS (e));
487 else if (code == COMPLEX_EXPR)
489 pp_c_expression (ppi, TREE_OPERAND (e, 0));
490 pp_separate_with (ppi, ',');
491 pp_c_expression (ppi, TREE_OPERAND (e, 1));
494 pp_right_brace (ppi);
497 case COMPOUND_LITERAL_EXPR:
498 e = DECL_INITIAL (e);
501 pp_initializer (ppi, e);
505 pp_c_identifier (ppi, "__builtin_va_arg");
506 pp_c_left_paren (ppi);
507 pp_assignment_expression (ppi, TREE_OPERAND (e, 0));
508 pp_separate_with (ppi, ',');
509 pp_type_id (ppi, TREE_TYPE (e));
510 pp_c_right_paren (ppi);
514 pp_primary_expression (ppi, e);
519 /* Print out an expession-list; E is expected to be a TREE_LIST */
521 pp_c_expression_list (ppi, e)
522 c_pretty_print_info *ppi;
525 for (; e != NULL_TREE; e = TREE_CHAIN (e))
527 pp_c_assignment_expression (ppi, TREE_VALUE (e));
529 pp_separate_with (ppi, ',');
534 pp_c_unary_expression (ppi, e)
535 c_pretty_print_info *ppi;
538 enum tree_code code = TREE_CODE (e);
541 case PREINCREMENT_EXPR:
542 case PREDECREMENT_EXPR:
543 pp_identifier (ppi, code == PREINCREMENT_EXPR ? "++" : "--");
544 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
554 if (code == ADDR_EXPR)
556 else if (code == INDIRECT_REF)
558 else if (code == NEGATE_EXPR)
560 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
562 else if (code == TRUTH_NOT_EXPR)
563 pp_exclamation (ppi);
564 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
569 pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
570 pp_c_whitespace (ppi);
571 if (TYPE_P (TREE_OPERAND (e, 0)))
573 pp_c_left_paren (ppi);
574 pp_type_id (ppi, TREE_OPERAND (e, 0));
575 pp_c_right_paren (ppi);
578 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
583 pp_c_identifier (ppi, code == REALPART_EXPR ? "__real__" : "__imag__");
584 pp_c_whitespace (ppi);
585 pp_unary_expression (ppi, TREE_OPERAND (e, 0));
589 pp_postfix_expression (ppi, e);
595 pp_c_cast_expression (ppi, e)
596 c_pretty_print_info *ppi;
599 if (TREE_CODE (e) == CONVERT_EXPR || TREE_CODE (e) == FLOAT_EXPR)
601 pp_c_left_paren (ppi);
602 pp_type_id (ppi, TREE_TYPE (e));
603 pp_c_right_paren (ppi);
604 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
607 pp_unary_expression (ppi, e);
611 pp_c_multiplicative_expression (ppi, e)
612 c_pretty_print_info *ppi;
615 enum tree_code code = TREE_CODE (e);
621 pp_c_multiplicative_expression (ppi, TREE_OPERAND (e, 0));
622 pp_c_whitespace (ppi);
623 if (code == MULT_EXPR)
625 else if (code == TRUNC_DIV_EXPR)
629 pp_c_whitespace (ppi);
630 pp_c_cast_expression (ppi, TREE_OPERAND (e, 1));
634 pp_c_cast_expression (ppi, e);
640 pp_c_additive_expression (ppi, e)
641 c_pretty_print_info *ppi;
644 enum tree_code code = TREE_CODE (e);
649 pp_c_additive_expression (ppi, TREE_OPERAND (e, 0));
650 pp_c_whitespace (ppi);
651 if (code == PLUS_EXPR)
655 pp_c_whitespace (ppi);
656 pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1));
660 pp_multiplicative_expression (ppi, e);
666 pp_c_shift_expression (ppi, e)
667 c_pretty_print_info *ppi;
670 enum tree_code code = TREE_CODE (e);
675 pp_c_shift_expression (ppi, TREE_OPERAND (e, 0));
676 pp_c_whitespace (ppi);
677 pp_identifier (ppi, code == LSHIFT_EXPR ? "<<" : ">>");
678 pp_c_whitespace (ppi);
679 pp_c_additive_expression (ppi, TREE_OPERAND (e, 1));
683 pp_c_additive_expression (ppi, e);
688 pp_c_relational_expression (ppi, e)
689 c_pretty_print_info *ppi;
692 enum tree_code code = TREE_CODE (e);
699 pp_c_relational_expression (ppi, TREE_OPERAND (e, 0));
700 pp_c_whitespace (ppi);
703 else if (code == GT_EXPR)
705 else if (code == LE_EXPR)
706 pp_identifier (ppi, "<=");
707 else if (code == GE_EXPR)
708 pp_identifier (ppi, ">=");
709 pp_c_whitespace (ppi);
710 pp_c_shift_expression (ppi, TREE_OPERAND (e, 1));
714 pp_c_shift_expression (ppi, e);
720 pp_c_equality_expression (ppi, e)
721 c_pretty_print_info *ppi;
724 enum tree_code code = TREE_CODE (e);
729 pp_c_equality_expression (ppi, TREE_OPERAND (e, 0));
730 pp_c_maybe_whitespace (ppi);
731 pp_identifier (ppi, code == EQ_EXPR ? "==" : "!=");
732 pp_c_whitespace (ppi);
733 pp_c_relational_expression (ppi, TREE_OPERAND (e, 1));
737 pp_c_relational_expression (ppi, e);
743 pp_c_and_expression (ppi, e)
744 c_pretty_print_info *ppi;
747 if (TREE_CODE (e) == BIT_AND_EXPR)
749 pp_c_and_expression (ppi, TREE_OPERAND (e, 0));
750 pp_c_maybe_whitespace (ppi);
752 pp_c_whitespace (ppi);
753 pp_c_equality_expression (ppi, TREE_OPERAND (e, 1));
756 pp_c_equality_expression (ppi, e);
760 pp_c_exclusive_or_expression (ppi, e)
761 c_pretty_print_info *ppi;
764 if (TREE_CODE (e) == BIT_XOR_EXPR)
766 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
767 pp_c_maybe_whitespace (ppi);
769 pp_c_whitespace (ppi);
770 pp_c_and_expression (ppi, TREE_OPERAND (e, 1));
773 pp_c_and_expression (ppi, e);
777 pp_c_inclusive_or_expression (ppi, e)
778 c_pretty_print_info *ppi;
781 if (TREE_CODE (e) == BIT_IOR_EXPR)
783 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
784 pp_c_maybe_whitespace (ppi);
786 pp_c_whitespace (ppi);
787 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1));
790 pp_c_exclusive_or_expression (ppi, e);
794 pp_c_logical_and_expression (ppi, e)
795 c_pretty_print_info *ppi;
798 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
800 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0));
801 pp_c_maybe_whitespace (ppi);
802 pp_identifier (ppi, "&&");
803 pp_c_whitespace (ppi);
804 pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1));
807 pp_c_inclusive_or_expression (ppi, e);
811 pp_c_logical_or_expression (ppi, e)
812 c_pretty_print_info *ppi;
815 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
817 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
818 pp_c_maybe_whitespace (ppi);
819 pp_identifier (ppi, "||");
820 pp_c_whitespace (ppi);
821 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1));
824 pp_c_logical_and_expression (ppi, e);
828 pp_c_conditional_expression (ppi, e)
829 c_pretty_print_info *ppi;
832 if (TREE_CODE (e) == COND_EXPR)
834 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
835 pp_c_maybe_whitespace (ppi);
837 pp_c_whitespace (ppi);
838 pp_c_expression (ppi, TREE_OPERAND (e, 1));
839 pp_c_maybe_whitespace (ppi);
841 pp_c_whitespace (ppi);
842 pp_c_conditional_expression (ppi, TREE_OPERAND (e, 2));
845 pp_c_logical_or_expression (ppi, e);
849 /* Pretty-print a C assignment-expression. */
851 pp_c_assignment_expression (ppi, e)
852 c_pretty_print_info *ppi;
855 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
857 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
858 pp_c_maybe_whitespace (ppi);
861 pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1));
864 pp_c_conditional_expression (ppi, e);
867 /* Pretty-print an expression. */
869 pp_c_expression (ppi, e)
870 c_pretty_print_info *ppi;
873 switch (TREE_CODE (e))
876 pp_c_integer_literal (ppi, e);
880 pp_c_real_literal (ppi, e);
884 pp_c_string_literal (ppi, e);
897 pp_c_primary_expression (ppi, e);
900 case POSTINCREMENT_EXPR:
901 case POSTDECREMENT_EXPR:
911 case COMPOUND_LITERAL_EXPR:
914 pp_c_postfix_expression (ppi, e);
923 case PREINCREMENT_EXPR:
924 case PREDECREMENT_EXPR:
929 pp_c_unary_expression (ppi, e);
934 pp_c_cast_expression (ppi, e);
940 pp_c_multiplicative_expression (ppi, e);
945 pp_c_shift_expression (ppi, e);
952 pp_c_relational_expression (ppi, e);
956 pp_c_and_expression (ppi, e);
960 pp_c_exclusive_or_expression (ppi, e);
964 pp_c_inclusive_or_expression (ppi, e);
967 case TRUTH_ANDIF_EXPR:
968 pp_c_logical_and_expression (ppi, e);
971 case TRUTH_ORIF_EXPR:
972 pp_c_logical_or_expression (ppi, e);
976 pp_c_conditional_expression (ppi, e);
981 pp_c_assignment_expression (ppi, e);
985 pp_c_expression (ppi, TREE_OPERAND (e, 0));
989 pp_c_left_paren (ppi);
990 pp_c_expression (ppi, TREE_OPERAND (e, 0));
991 pp_separate_with (ppi, ',');
992 pp_assignment_expression (ppi, TREE_OPERAND (e, 1));
993 pp_c_right_paren (ppi);
998 pp_unsupported_tree (ppi, e);