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 static void pp_c_unary_expression PARAMS ((c_pretty_print_info *, tree));
39 static void pp_c_multiplicative_expression PARAMS ((c_pretty_print_info *,
41 static void pp_c_additive_expression PARAMS ((c_pretty_print_info *, tree));
42 static void pp_c_shift_expression PARAMS ((c_pretty_print_info *, tree));
43 static void pp_c_relational_expression PARAMS ((c_pretty_print_info *, tree));
44 static void pp_c_equality_expression PARAMS ((c_pretty_print_info *, tree));
45 static void pp_c_and_expression PARAMS ((c_pretty_print_info *, tree));
46 static void pp_c_exclusive_or_expression PARAMS ((c_pretty_print_info *,
48 static void pp_c_inclusive_or_expression PARAMS ((c_pretty_print_info *,
50 static void pp_c_logical_and_expression PARAMS ((c_pretty_print_info *, tree));
51 static void pp_c_conditional_expression PARAMS ((c_pretty_print_info *, tree));
52 static void pp_c_assignment_expression PARAMS ((c_pretty_print_info *, tree));
56 /* Print out CV-qualifiers. Take care of possible extension. */
58 pp_c_cv_qualifier (ppi, cv)
59 c_pretty_print_info *ppi;
62 if (cv & TYPE_QUAL_CONST)
63 pp_c_identifier (ppi, "const");
64 if (cv & TYPE_QUAL_VOLATILE)
65 pp_c_identifier (ppi, "volatile");
66 if (cv & TYPE_QUAL_RESTRICT)
67 pp_c_identifier (ppi, flag_isoc99 ? "restrict" : "__restrict__");
76 /* Print out a c-char. */
79 c_pretty_print_info *ppi;
85 pp_identifier (ppi, "\\n");
88 pp_identifier (ppi, "\\t");
91 pp_identifier (ppi, "\\v");
94 pp_identifier (ppi, "\\b");
97 pp_identifier (ppi, "\\r");
100 pp_identifier (ppi, "\\f");
103 pp_identifier (ppi, "\\a");
106 pp_identifier (ppi, "\\\\");
109 pp_identifier (ppi, "\\'");
112 pp_identifier (ppi, "\\\"");
116 pp_character (ppi, c);
118 pp_format_integer (ppi, "\\%03o", (unsigned) c);
123 /* Print out a STRING literal. */
125 pp_c_string_literal (ppi, s)
126 c_pretty_print_info *ppi;
129 const char *p = TREE_STRING_POINTER (s);
130 int n = TREE_STRING_LENGTH (s) - 1;
132 pp_doublequote (ppi);
133 for (i = 0; i < n; ++i)
134 pp_c_char (ppi, p[i]);
135 pp_doublequote (ppi);
138 /* Print out a CHARACTER literal. */
140 pp_c_character_literal (ppi, c)
141 c_pretty_print_info *ppi;
145 pp_c_char (ppi, tree_low_cst (c, 0));
149 /* Print out a BOOLEAN literal. */
151 pp_c_bool_literal (ppi, b)
152 c_pretty_print_info *ppi;
155 if (b == boolean_false_node || integer_zerop (b))
157 if (c_language == clk_cplusplus)
158 pp_c_identifier (ppi, "false");
159 else if (c_language == clk_c && flag_isoc99)
160 pp_c_identifier (ppi, "_False");
162 pp_unsupported_tree (ppi, b);
164 else if (b == boolean_true_node)
166 if (c_language == clk_cplusplus)
167 pp_c_identifier (ppi, "true");
168 else if (c_language == clk_c && flag_isoc99)
169 pp_c_identifier (ppi, "_True");
171 pp_unsupported_tree (ppi, b);
174 pp_unsupported_tree (ppi, b);
177 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
178 false; that means the value was obtained by a cast, in which case
179 print out the type-id part of the cast-expression -- the casted value
180 is then printed by pp_c_integer_literal. */
182 pp_c_enumerator (ppi, e)
183 c_pretty_print_info *ppi;
186 tree type = TREE_TYPE (e);
189 /* Find the name of this constant. */
190 for (value = TYPE_VALUES (type);
191 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
192 value = TREE_CHAIN (value))
195 if (value != NULL_TREE)
196 pp_c_tree_identifier (ppi, TREE_PURPOSE (value));
199 /* Value must have been cast. */
200 pp_c_left_paren (ppi);
201 pp_type_id (ppi, type);
202 pp_c_right_paren (ppi);
209 /* Print out an INTEGER constant value. */
211 pp_c_integer_literal (ppi, i)
212 c_pretty_print_info *ppi;
215 tree type = TREE_TYPE (i);
217 if (type == boolean_type_node)
218 pp_c_bool_literal (ppi, i);
219 else if (type == char_type_node)
220 pp_c_character_literal (ppi, i);
221 else if (TREE_CODE (type) == ENUMERAL_TYPE
222 && pp_c_enumerator (ppi, i))
226 if (host_integerp (i, 0))
227 pp_wide_integer (ppi, TREE_INT_CST_LOW (i));
230 if (tree_int_cst_sgn (i) < 0)
232 static char format[10]; /* "%x%09999x\0" */
234 sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
236 pp_c_char (ppi, '-');
237 i = build_int_2 (-TREE_INT_CST_LOW (i),
238 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
239 sprintf (pp_buffer (ppi)->digit_buffer, format,
240 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
241 pp_identifier (ppi, pp_buffer (ppi)->digit_buffer);
248 /* Print out a REAL value. */
250 pp_c_real_literal (ppi, r)
251 c_pretty_print_info *ppi;
254 REAL_VALUE_TO_DECIMAL (TREE_REAL_CST (r), "%.16g",
255 pp_buffer (ppi)->digit_buffer);
256 pp_identifier (ppi, pp_buffer(ppi)->digit_buffer);
261 pp_c_literal (ppi, e)
262 c_pretty_print_info *ppi;
265 switch (TREE_CODE (e))
268 pp_c_integer_literal (ppi, e);
272 pp_c_real_literal (ppi, e);
276 pp_c_string_literal (ppi, e);
280 pp_unsupported_tree (ppi, e);
285 /* Pretty-print a C primary-expression. */
287 pp_c_primary_expression (ppi, e)
288 c_pretty_print_info *ppi;
291 switch (TREE_CODE (e))
301 case IDENTIFIER_NODE:
302 pp_c_tree_identifier (ppi, e);
306 pp_c_identifier (ppi, "<erroneous-expression>");
310 pp_c_identifier (ppi, "<return-value>");
316 pp_c_literal (ppi, e);
320 /* Make sure this call won't cause any infinite loop. */
321 pp_c_left_paren (ppi);
322 pp_c_expression (ppi, e);
323 pp_c_right_paren (ppi);
329 pp_c_postfix_expression (ppi, e)
330 c_pretty_print_info *ppi;
333 enum tree_code code = TREE_CODE (e);
336 case POSTINCREMENT_EXPR:
337 case POSTDECREMENT_EXPR:
338 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
339 pp_identifier (ppi, code == POSTINCREMENT_EXPR ? "++" : "--");
343 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
344 pp_c_left_bracket (ppi);
345 pp_c_expression (ppi, TREE_OPERAND (e, 1));
346 pp_c_right_bracket (ppi);
350 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
351 pp_c_left_paren (ppi);
352 pp_c_expression_list (ppi, TREE_OPERAND (e, 1));
353 pp_c_right_paren (ppi);
358 tree object = TREE_OPERAND (e, 0);
359 if (TREE_CODE (object) == INDIRECT_REF)
361 pp_postfix_expression (ppi, TREE_OPERAND (object, 0));
366 pp_postfix_expression (ppi, object);
369 pp_c_expression (ppi, TREE_OPERAND (e, 1));
376 pp_unsupported_tree (ppi, e);
380 pp_primary_expression (ppi, e);
385 /* Print out an expession-list; E is expected to be a TREE_LIST */
387 pp_c_expression_list (ppi, e)
388 c_pretty_print_info *ppi;
391 for (; e != NULL_TREE; e = TREE_CHAIN (e))
393 pp_c_assignment_expression (ppi, TREE_VALUE (e));
395 pp_separate_with (ppi, ',');
400 pp_c_unary_expression (ppi, e)
401 c_pretty_print_info *ppi;
404 enum tree_code code = TREE_CODE (e);
407 case PREINCREMENT_EXPR:
408 case PREDECREMENT_EXPR:
409 pp_identifier (ppi, code == PREINCREMENT_EXPR ? "++" : "--");
410 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
419 if (code == ADDR_EXPR)
421 else if (code == INDIRECT_REF)
423 else if (code == NEGATE_EXPR)
425 else if (code == BIT_NOT_EXPR)
427 else if (code == TRUTH_NOT_EXPR)
428 pp_exclamation (ppi);
429 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
434 pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
435 pp_c_whitespace (ppi);
436 if (TYPE_P (TREE_OPERAND (e, 0)))
438 pp_c_left_paren (ppi);
439 pp_type_id (ppi, TREE_OPERAND (e, 0));
440 pp_c_right_paren (ppi);
443 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
447 pp_postfix_expression (ppi, e);
453 pp_c_cast_expression (ppi, e)
454 c_pretty_print_info *ppi;
457 if (TREE_CODE (e) == CONVERT_EXPR)
459 pp_c_left_paren (ppi);
460 pp_type_id (ppi, TREE_TYPE (e));
461 pp_c_right_paren (ppi);
462 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
465 pp_unary_expression (ppi, e);
469 pp_c_multiplicative_expression (ppi, e)
470 c_pretty_print_info *ppi;
473 enum tree_code code = TREE_CODE (e);
479 pp_c_multiplicative_expression (ppi, TREE_OPERAND (e, 0));
480 pp_c_whitespace (ppi);
481 if (code == MULT_EXPR)
483 else if (code == TRUNC_DIV_EXPR)
487 pp_c_whitespace (ppi);
488 pp_c_cast_expression (ppi, TREE_OPERAND (e, 1));
492 pp_c_cast_expression (ppi, e);
498 pp_c_additive_expression (ppi, e)
499 c_pretty_print_info *ppi;
502 enum tree_code code = TREE_CODE (e);
507 pp_c_additive_expression (ppi, TREE_OPERAND (e, 0));
508 pp_c_whitespace (ppi);
509 if (code == PLUS_EXPR)
513 pp_c_whitespace (ppi);
514 pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1));
518 pp_multiplicative_expression (ppi, e);
524 pp_c_shift_expression (ppi, e)
525 c_pretty_print_info *ppi;
528 enum tree_code code = TREE_CODE (e);
533 pp_c_shift_expression (ppi, TREE_OPERAND (e, 0));
534 pp_c_whitespace (ppi);
535 pp_identifier (ppi, code == LSHIFT_EXPR ? "<<" : ">>");
536 pp_c_whitespace (ppi);
537 pp_c_additive_expression (ppi, TREE_OPERAND (e, 1));
541 pp_c_additive_expression (ppi, e);
546 pp_c_relational_expression (ppi, e)
547 c_pretty_print_info *ppi;
550 enum tree_code code = TREE_CODE (e);
557 pp_c_relational_expression (ppi, TREE_OPERAND (e, 0));
558 pp_c_whitespace (ppi);
561 else if (code == GT_EXPR)
563 else if (code == LE_EXPR)
564 pp_identifier (ppi, "<=");
565 else if (code == GE_EXPR)
566 pp_identifier (ppi, ">=");
567 pp_c_whitespace (ppi);
568 pp_c_shift_expression (ppi, TREE_OPERAND (e, 1));
572 pp_c_shift_expression (ppi, e);
578 pp_c_equality_expression (ppi, e)
579 c_pretty_print_info *ppi;
582 enum tree_code code = TREE_CODE (e);
587 pp_c_equality_expression (ppi, TREE_OPERAND (e, 0));
588 pp_c_maybe_whitespace (ppi);
589 pp_identifier (ppi, code == EQ_EXPR ? "==" : "!=");
590 pp_c_whitespace (ppi);
591 pp_c_relational_expression (ppi, TREE_OPERAND (e, 1));
595 pp_c_relational_expression (ppi, e);
601 pp_c_and_expression (ppi, e)
602 c_pretty_print_info *ppi;
605 if (TREE_CODE (e) == BIT_AND_EXPR)
607 pp_c_and_expression (ppi, TREE_OPERAND (e, 0));
608 pp_c_maybe_whitespace (ppi);
610 pp_c_whitespace (ppi);
611 pp_c_equality_expression (ppi, TREE_OPERAND (e, 1));
614 pp_c_equality_expression (ppi, e);
618 pp_c_exclusive_or_expression (ppi, e)
619 c_pretty_print_info *ppi;
622 if (TREE_CODE (e) == BIT_XOR_EXPR)
624 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
625 pp_c_maybe_whitespace (ppi);
627 pp_c_whitespace (ppi);
628 pp_c_and_expression (ppi, TREE_OPERAND (e, 1));
631 pp_c_and_expression (ppi, e);
635 pp_c_inclusive_or_expression (ppi, e)
636 c_pretty_print_info *ppi;
639 if (TREE_CODE (e) == BIT_IOR_EXPR)
641 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
642 pp_c_maybe_whitespace (ppi);
644 pp_c_whitespace (ppi);
645 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1));
648 pp_c_exclusive_or_expression (ppi, e);
652 pp_c_logical_and_expression (ppi, e)
653 c_pretty_print_info *ppi;
656 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
658 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0));
659 pp_c_maybe_whitespace (ppi);
660 pp_identifier (ppi, "&&");
661 pp_c_whitespace (ppi);
662 pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1));
665 pp_c_inclusive_or_expression (ppi, e);
669 pp_c_logical_or_expression (ppi, e)
670 c_pretty_print_info *ppi;
673 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
675 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
676 pp_c_maybe_whitespace (ppi);
677 pp_identifier (ppi, "||");
678 pp_c_whitespace (ppi);
679 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1));
682 pp_c_logical_and_expression (ppi, e);
686 pp_c_conditional_expression (ppi, e)
687 c_pretty_print_info *ppi;
690 if (TREE_CODE (e) == COND_EXPR)
692 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
693 pp_c_maybe_whitespace (ppi);
695 pp_c_whitespace (ppi);
696 pp_c_expression (ppi, TREE_OPERAND (e, 1));
697 pp_c_maybe_whitespace (ppi);
699 pp_c_whitespace (ppi);
700 pp_c_conditional_expression (ppi, TREE_OPERAND (e, 2));
703 pp_c_logical_or_expression (ppi, e);
707 /* Pretty-print a C assignment-expression. */
709 pp_c_assignment_expression (ppi, e)
710 c_pretty_print_info *ppi;
713 if (TREE_CODE (e) == MODIFY_EXPR)
715 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
716 pp_c_maybe_whitespace (ppi);
719 pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1));
722 pp_c_conditional_expression (ppi, e);
725 /* Pretty-print an expression. */
727 pp_c_expression (ppi, e)
728 c_pretty_print_info *ppi;
731 switch (TREE_CODE (e))
734 pp_c_integer_literal (ppi, e);
738 pp_c_real_literal (ppi, e);
742 pp_c_string_literal (ppi, e);
753 pp_c_primary_expression (ppi, e);
756 case POSTINCREMENT_EXPR:
757 case POSTDECREMENT_EXPR:
764 pp_c_postfix_expression (ppi, e);
768 pp_c_cast_expression (ppi, e);
774 pp_c_multiplicative_expression (ppi, e);
779 pp_c_shift_expression (ppi, e);
786 pp_c_relational_expression (ppi, e);
790 pp_c_and_expression (ppi, e);
794 pp_c_exclusive_or_expression (ppi, e);
798 pp_c_inclusive_or_expression (ppi, e);
801 case TRUTH_ANDIF_EXPR:
802 pp_c_logical_and_expression (ppi, e);
805 case TRUTH_ORIF_EXPR:
806 pp_c_logical_or_expression (ppi, e);
810 pp_c_conditional_expression (ppi, e);
814 pp_c_assignment_expression (ppi, e);
818 pp_c_expression (ppi, TREE_OPERAND (e, 0));
822 pp_c_left_paren (ppi);
823 pp_c_expression (ppi, TREE_OPERAND (e, 0));
824 pp_separate_with (ppi, ',');
825 pp_assignment_expression (ppi, TREE_OPERAND (e, 1));
826 pp_c_right_paren (ppi);
831 pp_unsupported_tree (ppi, e);