* java/io/FileDescriptor.java (position): New private field.
[official-gcc.git] / gcc / c-pretty-print.c
blob685b46d434f5e1999626ac855c80abaa0449afcc
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "real.h"
27 #include "c-pretty-print.h"
28 #include "c-tree.h"
30 /* literal */
31 static void pp_c_char PARAMS ((c_pretty_printer, int));
32 static void pp_c_character_literal PARAMS ((c_pretty_printer, tree));
33 static void pp_c_bool_literal PARAMS ((c_pretty_printer, tree));
34 static bool pp_c_enumerator PARAMS ((c_pretty_printer, tree));
35 static void pp_c_integer_literal PARAMS ((c_pretty_printer, tree));
36 static void pp_c_real_literal PARAMS ((c_pretty_printer, tree));
37 static void pp_c_string_literal PARAMS ((c_pretty_printer, tree));
39 static void pp_c_primary_expression PARAMS ((c_pretty_printer, tree));
41 /* postfix-expression */
42 static void pp_c_initializer_list PARAMS ((c_pretty_printer, tree));
44 static void pp_c_unary_expression PARAMS ((c_pretty_printer, tree));
45 static void pp_c_multiplicative_expression PARAMS ((c_pretty_printer, tree));
46 static void pp_c_additive_expression PARAMS ((c_pretty_printer, tree));
47 static void pp_c_shift_expression PARAMS ((c_pretty_printer, tree));
48 static void pp_c_relational_expression PARAMS ((c_pretty_printer, tree));
49 static void pp_c_equality_expression PARAMS ((c_pretty_printer, tree));
50 static void pp_c_and_expression PARAMS ((c_pretty_printer, tree));
51 static void pp_c_exclusive_or_expression PARAMS ((c_pretty_printer,
52 tree));
53 static void pp_c_inclusive_or_expression PARAMS ((c_pretty_printer,
54 tree));
55 static void pp_c_logical_and_expression PARAMS ((c_pretty_printer, tree));
56 static void pp_c_conditional_expression PARAMS ((c_pretty_printer, tree));
57 static void pp_c_assignment_expression PARAMS ((c_pretty_printer, tree));
59 /* declarations. */
60 static void pp_c_declaration_specifiers PARAMS ((c_pretty_printer, tree));
61 static void pp_c_init_declarator PARAMS ((c_pretty_printer, tree));
62 static void pp_c_declarator PARAMS ((c_pretty_printer, tree));
63 static void pp_c_direct_declarator PARAMS ((c_pretty_printer, tree));
64 static void pp_c_abstract_declarator PARAMS ((c_pretty_printer, tree));
65 static void pp_c_specifier_qualifier_list PARAMS ((c_pretty_printer, tree));
66 static void pp_c_simple_type_specifier PARAMS ((c_pretty_printer, tree));
67 static void pp_c_parameter_declaration PARAMS ((c_pretty_printer, tree));
68 static void pp_c_type_id PARAMS ((c_pretty_printer, tree));
69 static void pp_c_storage_class_specifier PARAMS ((c_pretty_printer, tree));
70 static void pp_c_function_specifier PARAMS ((c_pretty_printer, tree));
73 /* Declarations. */
75 /* Print out CV-qualifiers. Take care of possible extensions. */
76 void
77 pp_c_cv_qualifier (ppi, cv)
78 c_pretty_printer ppi;
79 int cv;
81 if (cv & TYPE_QUAL_CONST)
82 pp_c_identifier (ppi, "const");
83 if (cv & TYPE_QUAL_VOLATILE)
84 pp_c_identifier (ppi, "volatile");
85 if (cv & TYPE_QUAL_RESTRICT)
86 pp_c_identifier (ppi, flag_isoc99 ? "restrict" : "__restrict__");
89 static void
90 pp_c_simple_type_specifier (ppi, t)
91 c_pretty_printer ppi;
92 tree t;
94 const enum tree_code code = TREE_CODE (t);
95 switch (code)
97 case ERROR_MARK:
98 pp_c_identifier (ppi, "<type-error>");
99 break;
101 #if 0
102 case UNKNOWN_TYPE:
103 pp_c_identifier (ppi, "<unkown-type>");
104 break;
105 #endif
107 case IDENTIFIER_NODE:
108 pp_c_tree_identifier (ppi, t);
109 break;
111 case VOID_TYPE:
112 case BOOLEAN_TYPE:
113 case CHAR_TYPE:
114 case INTEGER_TYPE:
115 case REAL_TYPE:
116 pp_c_tree_identifier (ppi, DECL_NAME (t));
117 break;
119 case COMPLEX_TYPE:
120 case VECTOR_TYPE:
121 pp_c_simple_type_specifier (ppi, TYPE_MAIN_VARIANT (TREE_TYPE (t)));
122 if (code == COMPLEX_TYPE)
123 pp_c_identifier (ppi, flag_isoc99 ? "_Complex" : "__complex__");
124 else if (code == VECTOR_TYPE)
125 pp_c_identifier (ppi, "__vector__");
126 break;
128 case TYPE_DECL:
129 if (DECL_NAME (t))
130 pp_c_tree_identifier (ppi, DECL_NAME (t));
131 else
132 pp_c_identifier (ppi, "<typedef-error>");
133 break;
135 case UNION_TYPE:
136 case RECORD_TYPE:
137 case ENUMERAL_TYPE:
138 if (code == UNION_TYPE)
139 pp_c_identifier (ppi, "union");
140 else if (code == RECORD_TYPE)
141 pp_c_identifier (ppi, "struct");
142 else if (code == ENUMERAL_TYPE)
143 pp_c_identifier (ppi, "enum");
144 else
145 pp_c_identifier (ppi, "<tag-error>");
147 if (TYPE_NAME (t))
148 pp_c_tree_identifier (ppi, TYPE_NAME (t));
149 else
150 pp_c_identifier (ppi, "<anonymous>");
151 break;
153 default:
154 pp_unsupported_tree (ppi, t);
158 static inline void
159 pp_c_specifier_qualifier_list (ppi, t)
160 c_pretty_printer ppi;
161 tree t;
163 pp_c_simple_type_specifier (ppi, TYPE_MAIN_VARIANT (TREE_TYPE (t)));
164 pp_c_cv_qualifier (ppi, TYPE_QUALS (t));
167 static void
168 pp_c_abstract_declarator (ppi, t)
169 c_pretty_printer ppi;
170 tree t;
172 pp_unsupported_tree (ppi, t);
176 static inline void
177 pp_c_type_id (ppi, t)
178 c_pretty_printer ppi;
179 tree t;
181 pp_c_specifier_qualifier_list (ppi, t);
182 pp_c_abstract_declarator (ppi, t);
185 static inline void
186 pp_c_storage_class_specifier (pp, t)
187 c_pretty_printer pp;
188 tree t;
190 if (TREE_CODE (t) == TYPE_DECL)
191 pp_c_identifier (pp, "typedef");
192 else if (DECL_REGISTER (t))
193 pp_c_identifier (pp, "register");
196 static inline void
197 pp_c_function_specifier (pp, t)
198 c_pretty_printer pp;
199 tree t;
201 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
202 pp_c_identifier (pp, "inline");
205 static inline void
206 pp_c_declaration_specifiers (pp, t)
207 c_pretty_printer pp;
208 tree t;
210 pp_c_storage_class_specifier (pp, t);
211 pp_c_function_specifier (pp, t);
212 pp_type_specifier (pp, TYPE_MAIN_VARIANT (TREE_TYPE (t)));
213 pp_c_cv_qualifier (pp, TYPE_QUALS (TREE_TYPE (t)));
216 static inline void
217 pp_c_direct_declarator (pp, t)
218 c_pretty_printer pp;
219 tree t;
221 pp_unsupported_tree (pp, t);
224 static inline void
225 pp_c_declarator (pp, t)
226 c_pretty_printer pp;
227 tree t;
229 pp_unsupported_tree (pp, t);
232 static inline void
233 pp_c_init_declarator (pp, t)
234 c_pretty_printer pp;
235 tree t;
237 pp_declarator (pp, t);
238 if (DECL_INITIAL (t))
240 pp_whitespace (pp);
241 pp_equal (pp);
242 pp_whitespace (pp);
243 pp_c_initializer (pp, DECL_INITIAL (t));
247 void
248 pp_c_declaration (pp, t)
249 c_pretty_printer pp;
250 tree t;
252 pp_declaration_specifiers (pp, t);
253 pp_c_init_declarator (pp, t);
256 static void
257 pp_c_parameter_declaration (pp, t)
258 c_pretty_printer pp;
259 tree t;
261 pp_unsupported_tree (pp, t);
264 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
265 void
266 pp_c_attributes (pp, attributes)
267 c_pretty_printer pp;
268 tree attributes;
270 if (attributes == NULL_TREE)
271 return;
273 pp_c_identifier (pp, "__attribute__");
274 pp_c_left_paren (pp);
275 pp_c_left_paren (pp);
276 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
278 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
279 if (TREE_VALUE (attributes))
281 pp_c_left_paren (pp);
282 pp_c_expression_list (pp, TREE_VALUE (attributes));
283 pp_c_right_paren (pp);
286 if (TREE_CHAIN (attributes))
287 pp_separate_with (pp, ',');
289 pp_c_right_paren (pp);
290 pp_c_right_paren (pp);
294 /* Expressions. */
296 /* Print out a c-char. */
297 static void
298 pp_c_char (ppi, c)
299 c_pretty_printer ppi;
300 int c;
302 switch (c)
304 case TARGET_NEWLINE:
305 pp_identifier (ppi, "\\n");
306 break;
307 case TARGET_TAB:
308 pp_identifier (ppi, "\\t");
309 break;
310 case TARGET_VT:
311 pp_identifier (ppi, "\\v");
312 break;
313 case TARGET_BS:
314 pp_identifier (ppi, "\\b");
315 break;
316 case TARGET_CR:
317 pp_identifier (ppi, "\\r");
318 break;
319 case TARGET_FF:
320 pp_identifier (ppi, "\\f");
321 break;
322 case TARGET_BELL:
323 pp_identifier (ppi, "\\a");
324 break;
325 case '\\':
326 pp_identifier (ppi, "\\\\");
327 break;
328 case '\'':
329 pp_identifier (ppi, "\\'");
330 break;
331 case '\"':
332 pp_identifier (ppi, "\\\"");
333 break;
334 default:
335 if (ISPRINT (c))
336 pp_character (ppi, c);
337 else
338 pp_format_scalar (ppi, "\\%03o", (unsigned) c);
339 break;
343 /* Print out a STRING literal. */
344 static inline void
345 pp_c_string_literal (ppi, s)
346 c_pretty_printer ppi;
347 tree s;
349 const char *p = TREE_STRING_POINTER (s);
350 int n = TREE_STRING_LENGTH (s) - 1;
351 int i;
352 pp_doublequote (ppi);
353 for (i = 0; i < n; ++i)
354 pp_c_char (ppi, p[i]);
355 pp_doublequote (ppi);
358 /* Print out a CHARACTER literal. */
359 static inline void
360 pp_c_character_literal (ppi, c)
361 c_pretty_printer ppi;
362 tree c;
364 pp_quote (ppi);
365 pp_c_char (ppi, tree_low_cst (c, 0));
366 pp_quote (ppi);
369 /* Print out a BOOLEAN literal. */
370 static inline void
371 pp_c_bool_literal (ppi, b)
372 c_pretty_printer ppi;
373 tree b;
375 if (b == boolean_false_node || integer_zerop (b))
377 if (c_language == clk_cplusplus)
378 pp_c_identifier (ppi, "false");
379 else if (c_language == clk_c && flag_isoc99)
380 pp_c_identifier (ppi, "_False");
381 else
382 pp_unsupported_tree (ppi, b);
384 else if (b == boolean_true_node)
386 if (c_language == clk_cplusplus)
387 pp_c_identifier (ppi, "true");
388 else if (c_language == clk_c && flag_isoc99)
389 pp_c_identifier (ppi, "_True");
390 else
391 pp_unsupported_tree (ppi, b);
393 else
394 pp_unsupported_tree (ppi, b);
397 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
398 false; that means the value was obtained by a cast, in which case
399 print out the type-id part of the cast-expression -- the casted value
400 is then printed by pp_c_integer_literal. */
401 static bool
402 pp_c_enumerator (ppi, e)
403 c_pretty_printer ppi;
404 tree e;
406 tree type = TREE_TYPE (e);
407 tree value;
409 /* Find the name of this constant. */
410 for (value = TYPE_VALUES (type);
411 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
412 value = TREE_CHAIN (value))
415 if (value != NULL_TREE)
416 pp_c_tree_identifier (ppi, TREE_PURPOSE (value));
417 else
419 /* Value must have been cast. */
420 pp_c_left_paren (ppi);
421 pp_type_id (ppi, type);
422 pp_c_right_paren (ppi);
423 return false;
426 return true;
429 /* Print out an INTEGER constant value. */
430 static void
431 pp_c_integer_literal (ppi, i)
432 c_pretty_printer ppi;
433 tree i;
435 tree type = TREE_TYPE (i);
437 if (type == boolean_type_node)
438 pp_c_bool_literal (ppi, i);
439 else if (type == char_type_node)
440 pp_c_character_literal (ppi, i);
441 else if (TREE_CODE (type) == ENUMERAL_TYPE
442 && pp_c_enumerator (ppi, i))
444 else
446 if (host_integerp (i, 0))
447 pp_wide_integer (ppi, TREE_INT_CST_LOW (i));
448 else
450 if (tree_int_cst_sgn (i) < 0)
452 static char format[10]; /* "%x%09999x\0" */
453 if (!format[0])
454 sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
456 pp_c_char (ppi, '-');
457 i = build_int_2 (-TREE_INT_CST_LOW (i),
458 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
459 sprintf (pp_buffer (ppi)->digit_buffer, format,
460 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
461 pp_identifier (ppi, pp_buffer (ppi)->digit_buffer);
468 /* Print out a REAL value. */
469 static inline void
470 pp_c_real_literal (ppi, r)
471 c_pretty_printer ppi;
472 tree r;
474 real_to_decimal (pp_buffer (ppi)->digit_buffer, &TREE_REAL_CST (r),
475 sizeof (pp_buffer (ppi)->digit_buffer), 0, 1);
476 pp_identifier (ppi, pp_buffer(ppi)->digit_buffer);
480 void
481 pp_c_literal (ppi, e)
482 c_pretty_printer ppi;
483 tree e;
485 switch (TREE_CODE (e))
487 case INTEGER_CST:
488 pp_c_integer_literal (ppi, e);
489 break;
491 case REAL_CST:
492 pp_c_real_literal (ppi, e);
493 break;
495 case STRING_CST:
496 pp_c_string_literal (ppi, e);
497 break;
499 default:
500 pp_unsupported_tree (ppi, e);
501 break;
505 /* Pretty-print a C primary-expression. */
506 static void
507 pp_c_primary_expression (ppi, e)
508 c_pretty_printer ppi;
509 tree e;
511 switch (TREE_CODE (e))
513 case VAR_DECL:
514 case PARM_DECL:
515 case FIELD_DECL:
516 case CONST_DECL:
517 case FUNCTION_DECL:
518 case LABEL_DECL:
519 e = DECL_NAME (e);
520 /* Fall through. */
521 case IDENTIFIER_NODE:
522 pp_c_tree_identifier (ppi, e);
523 break;
525 case ERROR_MARK:
526 pp_c_identifier (ppi, "<erroneous-expression>");
527 break;
529 case RESULT_DECL:
530 pp_c_identifier (ppi, "<return-value>");
531 break;
533 case INTEGER_CST:
534 case REAL_CST:
535 case STRING_CST:
536 pp_c_literal (ppi, e);
537 break;
539 case TARGET_EXPR:
540 pp_c_left_paren (ppi);
541 pp_c_identifier (ppi, "__builtin_memcpy");
542 pp_c_left_paren (ppi);
543 pp_ampersand (ppi);
544 pp_c_primary_expression (ppi, TREE_OPERAND (e, 0));
545 pp_separate_with (ppi, ',');
546 pp_ampersand (ppi);
547 pp_initializer (ppi, TREE_OPERAND (e, 1));
548 if (TREE_OPERAND (e, 2))
550 pp_separate_with (ppi, ',');
551 pp_c_expression (ppi, TREE_OPERAND (e, 2));
553 pp_c_right_paren (ppi);
555 case STMT_EXPR:
556 pp_c_left_paren (ppi);
557 pp_statement (ppi, STMT_EXPR_STMT (e));
558 pp_c_right_paren (ppi);
559 break;
561 default:
562 /* Make sure this call won't cause any infinite loop. */
563 pp_c_left_paren (ppi);
564 pp_c_expression (ppi, e);
565 pp_c_right_paren (ppi);
566 break;
570 /* Print out a C initializer -- also support C compound-literals. */
571 void
572 pp_c_initializer (ppi, e)
573 c_pretty_printer ppi;
574 tree e;
576 if (TREE_CODE (e) == CONSTRUCTOR)
578 enum tree_code code = TREE_CODE (TREE_TYPE (e));
579 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
581 pp_left_brace (ppi);
582 pp_c_initializer_list (ppi, e);
583 pp_right_brace (ppi);
585 else
586 pp_unsupported_tree (ppi, TREE_OPERAND (e, 1));
588 else
589 pp_assignment_expression (ppi, e);
592 static void
593 pp_c_initializer_list (ppi, e)
594 c_pretty_printer ppi;
595 tree e;
597 tree type = TREE_TYPE (e);
598 const enum tree_code code = TREE_CODE (type);
600 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
602 tree init = TREE_OPERAND (e, 1);
603 for (; init != NULL_TREE; init = TREE_CHAIN (init))
605 if (code == RECORD_TYPE || code == UNION_TYPE)
607 pp_dot (ppi);
608 pp_c_primary_expression (ppi, TREE_PURPOSE (init));
610 else
612 pp_c_left_bracket (ppi);
613 if (TREE_PURPOSE (init))
614 pp_c_literal (ppi, TREE_PURPOSE (init));
615 pp_c_right_bracket (ppi);
617 pp_c_whitespace (ppi);
618 pp_equal (ppi);
619 pp_c_whitespace (ppi);
620 pp_initializer (ppi, TREE_VALUE (init));
621 if (TREE_CHAIN (init))
622 pp_separate_with (ppi, ',');
625 else
626 pp_unsupported_tree (ppi, type);
629 void
630 pp_c_postfix_expression (ppi, e)
631 c_pretty_printer ppi;
632 tree e;
634 enum tree_code code = TREE_CODE (e);
635 switch (code)
637 case POSTINCREMENT_EXPR:
638 case POSTDECREMENT_EXPR:
639 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
640 pp_identifier (ppi, code == POSTINCREMENT_EXPR ? "++" : "--");
641 break;
643 case ARROW_EXPR:
644 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
645 pp_arrow (ppi);
646 break;
648 case ARRAY_REF:
649 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
650 pp_c_left_bracket (ppi);
651 pp_c_expression (ppi, TREE_OPERAND (e, 1));
652 pp_c_right_bracket (ppi);
653 break;
655 case CALL_EXPR:
656 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
657 pp_c_left_paren (ppi);
658 pp_c_expression_list (ppi, TREE_OPERAND (e, 1));
659 pp_c_right_paren (ppi);
660 break;
662 case ABS_EXPR:
663 case FFS_EXPR:
664 pp_c_identifier (ppi,
665 code == ABS_EXPR ? "__builtin_abs" : "__builtin_ffs");
666 pp_c_left_paren (ppi);
667 pp_c_expression (ppi, TREE_OPERAND (e, 0));
668 pp_c_right_paren (ppi);
669 break;
671 case COMPONENT_REF:
673 tree object = TREE_OPERAND (e, 0);
674 if (TREE_CODE (object) == INDIRECT_REF)
676 pp_postfix_expression (ppi, TREE_OPERAND (object, 0));
677 pp_arrow (ppi);
679 else
681 pp_postfix_expression (ppi, object);
682 pp_dot (ppi);
684 pp_c_expression (ppi, TREE_OPERAND (e, 1));
686 break;
688 case COMPLEX_CST:
689 case VECTOR_CST:
690 case COMPLEX_EXPR:
691 pp_c_left_paren (ppi);
692 pp_type_id (ppi, TREE_TYPE (e));
693 pp_c_right_paren (ppi);
694 pp_left_brace (ppi);
696 if (code == COMPLEX_CST)
698 pp_c_expression (ppi, TREE_REALPART (e));
699 pp_separate_with (ppi, ',');
700 pp_c_expression (ppi, TREE_IMAGPART (e));
702 else if (code == VECTOR_CST)
703 pp_c_expression_list (ppi, TREE_VECTOR_CST_ELTS (e));
704 else if (code == COMPLEX_EXPR)
706 pp_c_expression (ppi, TREE_OPERAND (e, 0));
707 pp_separate_with (ppi, ',');
708 pp_c_expression (ppi, TREE_OPERAND (e, 1));
711 pp_right_brace (ppi);
712 break;
714 case COMPOUND_LITERAL_EXPR:
715 e = DECL_INITIAL (e);
716 /* Fall through. */
717 case CONSTRUCTOR:
718 pp_initializer (ppi, e);
719 break;
721 #if 0
722 case SRCLOC:
723 pp_left_paren (ppi);
724 pp_identifier (ppi, "__location__");
725 pp_right_paren (ppi);
726 pp_whitespace (ppi);
727 pp_left_brace (ppi);
728 pp_dot (ppi);
729 pp_identifier (ppi, "file");
730 pp_whitespace (ppi);
731 pp_equal (ppi);
732 pp_c_whitespace (ppi);
733 pp_c_expression (ppi, SRCLOC_FILE (e));
734 pp_separate_with (ppi, ',');
735 pp_dot (ppi);
736 pp_identifier (ppi, "line");
737 pp_whitespace (ppi);
738 pp_equal (ppi);
739 pp_c_whitespace (ppi);
740 pp_c_expression (ppi, SRCLOC_LINE (e));
741 pp_right_brace (ppi);
742 break;
743 #endif
745 case VA_ARG_EXPR:
746 pp_c_identifier (ppi, "__builtin_va_arg");
747 pp_c_left_paren (ppi);
748 pp_assignment_expression (ppi, TREE_OPERAND (e, 0));
749 pp_separate_with (ppi, ',');
750 pp_type_id (ppi, TREE_TYPE (e));
751 pp_c_right_paren (ppi);
752 break;
754 default:
755 pp_primary_expression (ppi, e);
756 break;
760 /* Print out an expression-list; E is expected to be a TREE_LIST */
761 void
762 pp_c_expression_list (ppi, e)
763 c_pretty_printer ppi;
764 tree e;
766 for (; e != NULL_TREE; e = TREE_CHAIN (e))
768 pp_c_assignment_expression (ppi, TREE_VALUE (e));
769 if (TREE_CHAIN (e))
770 pp_separate_with (ppi, ',');
774 static void
775 pp_c_unary_expression (ppi, e)
776 c_pretty_printer ppi;
777 tree e;
779 enum tree_code code = TREE_CODE (e);
780 switch (code)
782 case PREINCREMENT_EXPR:
783 case PREDECREMENT_EXPR:
784 pp_identifier (ppi, code == PREINCREMENT_EXPR ? "++" : "--");
785 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
786 break;
788 case ADDR_EXPR:
789 case INDIRECT_REF:
790 case CONVERT_EXPR:
791 case NEGATE_EXPR:
792 case BIT_NOT_EXPR:
793 case TRUTH_NOT_EXPR:
794 case CONJ_EXPR:
795 if (code == ADDR_EXPR)
796 pp_ampersand (ppi);
797 else if (code == INDIRECT_REF)
798 pp_star (ppi);
799 else if (code == NEGATE_EXPR)
800 pp_minus (ppi);
801 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
802 pp_complement (ppi);
803 else if (code == TRUTH_NOT_EXPR)
804 pp_exclamation (ppi);
805 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
806 break;
808 case SIZEOF_EXPR:
809 case ALIGNOF_EXPR:
810 pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
811 pp_c_whitespace (ppi);
812 if (TYPE_P (TREE_OPERAND (e, 0)))
814 pp_c_left_paren (ppi);
815 pp_type_id (ppi, TREE_OPERAND (e, 0));
816 pp_c_right_paren (ppi);
818 else
819 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
820 break;
822 case REALPART_EXPR:
823 case IMAGPART_EXPR:
824 pp_c_identifier (ppi, code == REALPART_EXPR ? "__real__" : "__imag__");
825 pp_c_whitespace (ppi);
826 pp_unary_expression (ppi, TREE_OPERAND (e, 0));
827 break;
829 default:
830 pp_postfix_expression (ppi, e);
831 break;
835 void
836 pp_c_cast_expression (ppi, e)
837 c_pretty_printer ppi;
838 tree e;
840 if (TREE_CODE (e) == CONVERT_EXPR || TREE_CODE (e) == FLOAT_EXPR)
842 pp_c_left_paren (ppi);
843 pp_type_id (ppi, TREE_TYPE (e));
844 pp_c_right_paren (ppi);
845 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
847 else
848 pp_unary_expression (ppi, e);
851 static void
852 pp_c_multiplicative_expression (ppi, e)
853 c_pretty_printer ppi;
854 tree e;
856 enum tree_code code = TREE_CODE (e);
857 switch (code)
859 case MULT_EXPR:
860 case TRUNC_DIV_EXPR:
861 case TRUNC_MOD_EXPR:
862 pp_c_multiplicative_expression (ppi, TREE_OPERAND (e, 0));
863 pp_c_whitespace (ppi);
864 if (code == MULT_EXPR)
865 pp_star (ppi);
866 else if (code == TRUNC_DIV_EXPR)
867 pp_slash (ppi);
868 else
869 pp_modulo (ppi);
870 pp_c_whitespace (ppi);
871 pp_c_cast_expression (ppi, TREE_OPERAND (e, 1));
872 break;
874 default:
875 pp_c_cast_expression (ppi, e);
876 break;
880 static inline void
881 pp_c_additive_expression (ppi, e)
882 c_pretty_printer ppi;
883 tree e;
885 enum tree_code code = TREE_CODE (e);
886 switch (code)
888 case PLUS_EXPR:
889 case MINUS_EXPR:
890 pp_c_additive_expression (ppi, TREE_OPERAND (e, 0));
891 pp_c_whitespace (ppi);
892 if (code == PLUS_EXPR)
893 pp_plus (ppi);
894 else
895 pp_minus (ppi);
896 pp_c_whitespace (ppi);
897 pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1));
898 break;
900 default:
901 pp_multiplicative_expression (ppi, e);
902 break;
906 static inline void
907 pp_c_shift_expression (ppi, e)
908 c_pretty_printer ppi;
909 tree e;
911 enum tree_code code = TREE_CODE (e);
912 switch (code)
914 case LSHIFT_EXPR:
915 case RSHIFT_EXPR:
916 pp_c_shift_expression (ppi, TREE_OPERAND (e, 0));
917 pp_c_whitespace (ppi);
918 pp_identifier (ppi, code == LSHIFT_EXPR ? "<<" : ">>");
919 pp_c_whitespace (ppi);
920 pp_c_additive_expression (ppi, TREE_OPERAND (e, 1));
921 break;
923 default:
924 pp_c_additive_expression (ppi, e);
928 static void
929 pp_c_relational_expression (ppi, e)
930 c_pretty_printer ppi;
931 tree e;
933 enum tree_code code = TREE_CODE (e);
934 switch (code)
936 case LT_EXPR:
937 case GT_EXPR:
938 case LE_EXPR:
939 case GE_EXPR:
940 pp_c_relational_expression (ppi, TREE_OPERAND (e, 0));
941 pp_c_whitespace (ppi);
942 if (code == LT_EXPR)
943 pp_less (ppi);
944 else if (code == GT_EXPR)
945 pp_greater (ppi);
946 else if (code == LE_EXPR)
947 pp_identifier (ppi, "<=");
948 else if (code == GE_EXPR)
949 pp_identifier (ppi, ">=");
950 pp_c_whitespace (ppi);
951 pp_c_shift_expression (ppi, TREE_OPERAND (e, 1));
952 break;
954 default:
955 pp_c_shift_expression (ppi, e);
956 break;
960 static inline void
961 pp_c_equality_expression (ppi, e)
962 c_pretty_printer ppi;
963 tree e;
965 enum tree_code code = TREE_CODE (e);
966 switch (code)
968 case EQ_EXPR:
969 case NE_EXPR:
970 pp_c_equality_expression (ppi, TREE_OPERAND (e, 0));
971 pp_c_maybe_whitespace (ppi);
972 pp_identifier (ppi, code == EQ_EXPR ? "==" : "!=");
973 pp_c_whitespace (ppi);
974 pp_c_relational_expression (ppi, TREE_OPERAND (e, 1));
975 break;
977 default:
978 pp_c_relational_expression (ppi, e);
979 break;
983 static inline void
984 pp_c_and_expression (ppi, e)
985 c_pretty_printer ppi;
986 tree e;
988 if (TREE_CODE (e) == BIT_AND_EXPR)
990 pp_c_and_expression (ppi, TREE_OPERAND (e, 0));
991 pp_c_maybe_whitespace (ppi);
992 pp_ampersand (ppi);
993 pp_c_whitespace (ppi);
994 pp_c_equality_expression (ppi, TREE_OPERAND (e, 1));
996 else
997 pp_c_equality_expression (ppi, e);
1000 static inline void
1001 pp_c_exclusive_or_expression (ppi, e)
1002 c_pretty_printer ppi;
1003 tree e;
1005 if (TREE_CODE (e) == BIT_XOR_EXPR)
1007 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
1008 pp_c_maybe_whitespace (ppi);
1009 pp_carret (ppi);
1010 pp_c_whitespace (ppi);
1011 pp_c_and_expression (ppi, TREE_OPERAND (e, 1));
1013 else
1014 pp_c_and_expression (ppi, e);
1017 static inline void
1018 pp_c_inclusive_or_expression (ppi, e)
1019 c_pretty_printer ppi;
1020 tree e;
1022 if (TREE_CODE (e) == BIT_IOR_EXPR)
1024 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
1025 pp_c_maybe_whitespace (ppi);
1026 pp_bar (ppi);
1027 pp_c_whitespace (ppi);
1028 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1));
1030 else
1031 pp_c_exclusive_or_expression (ppi, e);
1034 static inline void
1035 pp_c_logical_and_expression (ppi, e)
1036 c_pretty_printer ppi;
1037 tree e;
1039 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1041 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0));
1042 pp_c_maybe_whitespace (ppi);
1043 pp_identifier (ppi, "&&");
1044 pp_c_whitespace (ppi);
1045 pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1));
1047 else
1048 pp_c_inclusive_or_expression (ppi, e);
1051 void
1052 pp_c_logical_or_expression (ppi, e)
1053 c_pretty_printer ppi;
1054 tree e;
1056 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1058 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
1059 pp_c_maybe_whitespace (ppi);
1060 pp_identifier (ppi, "||");
1061 pp_c_whitespace (ppi);
1062 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1));
1064 else
1065 pp_c_logical_and_expression (ppi, e);
1068 static void
1069 pp_c_conditional_expression (ppi, e)
1070 c_pretty_printer ppi;
1071 tree e;
1073 if (TREE_CODE (e) == COND_EXPR)
1075 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
1076 pp_c_maybe_whitespace (ppi);
1077 pp_question (ppi);
1078 pp_c_whitespace (ppi);
1079 pp_c_expression (ppi, TREE_OPERAND (e, 1));
1080 pp_c_maybe_whitespace (ppi);
1081 pp_colon (ppi);
1082 pp_c_whitespace (ppi);
1083 pp_c_conditional_expression (ppi, TREE_OPERAND (e, 2));
1085 else
1086 pp_c_logical_or_expression (ppi, e);
1090 /* Pretty-print a C assignment-expression. */
1091 static void
1092 pp_c_assignment_expression (ppi, e)
1093 c_pretty_printer ppi;
1094 tree e;
1096 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1098 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
1099 pp_c_maybe_whitespace (ppi);
1100 pp_equal (ppi);
1101 pp_whitespace (ppi);
1102 pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1));
1104 else
1105 pp_c_conditional_expression (ppi, e);
1108 /* Pretty-print an expression. */
1109 void
1110 pp_c_expression (ppi, e)
1111 c_pretty_printer ppi;
1112 tree e;
1114 switch (TREE_CODE (e))
1116 case INTEGER_CST:
1117 pp_c_integer_literal (ppi, e);
1118 break;
1120 case REAL_CST:
1121 pp_c_real_literal (ppi, e);
1122 break;
1124 case STRING_CST:
1125 pp_c_string_literal (ppi, e);
1126 break;
1128 case FUNCTION_DECL:
1129 case VAR_DECL:
1130 case CONST_DECL:
1131 case PARM_DECL:
1132 case RESULT_DECL:
1133 case FIELD_DECL:
1134 case LABEL_DECL:
1135 case ERROR_MARK:
1136 case TARGET_EXPR:
1137 case STMT_EXPR:
1138 pp_c_primary_expression (ppi, e);
1139 break;
1141 case POSTINCREMENT_EXPR:
1142 case POSTDECREMENT_EXPR:
1143 case ARROW_EXPR:
1144 case ARRAY_REF:
1145 case CALL_EXPR:
1146 case COMPONENT_REF:
1147 case COMPLEX_CST:
1148 case VECTOR_CST:
1149 case ABS_EXPR:
1150 case FFS_EXPR:
1151 case CONSTRUCTOR:
1152 case COMPOUND_LITERAL_EXPR:
1153 case COMPLEX_EXPR:
1154 case VA_ARG_EXPR:
1155 pp_c_postfix_expression (ppi, e);
1156 break;
1158 case CONJ_EXPR:
1159 case ADDR_EXPR:
1160 case INDIRECT_REF:
1161 case NEGATE_EXPR:
1162 case BIT_NOT_EXPR:
1163 case TRUTH_NOT_EXPR:
1164 case PREINCREMENT_EXPR:
1165 case PREDECREMENT_EXPR:
1166 case SIZEOF_EXPR:
1167 case ALIGNOF_EXPR:
1168 case REALPART_EXPR:
1169 case IMAGPART_EXPR:
1170 pp_c_unary_expression (ppi, e);
1171 break;
1173 case CONVERT_EXPR:
1174 case FLOAT_EXPR:
1175 pp_c_cast_expression (ppi, e);
1176 break;
1178 case MULT_EXPR:
1179 case TRUNC_MOD_EXPR:
1180 case TRUNC_DIV_EXPR:
1181 pp_c_multiplicative_expression (ppi, e);
1182 break;
1184 case LSHIFT_EXPR:
1185 case RSHIFT_EXPR:
1186 pp_c_shift_expression (ppi, e);
1187 break;
1189 case LT_EXPR:
1190 case GT_EXPR:
1191 case LE_EXPR:
1192 case GE_EXPR:
1193 pp_c_relational_expression (ppi, e);
1194 break;
1196 case BIT_AND_EXPR:
1197 pp_c_and_expression (ppi, e);
1198 break;
1200 case BIT_XOR_EXPR:
1201 pp_c_exclusive_or_expression (ppi, e);
1202 break;
1204 case BIT_IOR_EXPR:
1205 pp_c_inclusive_or_expression (ppi, e);
1206 break;
1208 case TRUTH_ANDIF_EXPR:
1209 pp_c_logical_and_expression (ppi, e);
1210 break;
1212 case TRUTH_ORIF_EXPR:
1213 pp_c_logical_or_expression (ppi, e);
1214 break;
1216 case COND_EXPR:
1217 pp_c_conditional_expression (ppi, e);
1218 break;
1220 case MODIFY_EXPR:
1221 case INIT_EXPR:
1222 pp_c_assignment_expression (ppi, e);
1223 break;
1225 case NOP_EXPR:
1226 pp_c_expression (ppi, TREE_OPERAND (e, 0));
1227 break;
1229 case COMPOUND_EXPR:
1230 pp_c_left_paren (ppi);
1231 pp_c_expression (ppi, TREE_OPERAND (e, 0));
1232 pp_separate_with (ppi, ',');
1233 pp_assignment_expression (ppi, TREE_OPERAND (e, 1));
1234 pp_c_right_paren (ppi);
1235 break;
1238 default:
1239 pp_unsupported_tree (ppi, e);
1240 break;
1245 /* Statements. */
1246 void
1247 pp_c_statement (ppi, stmt)
1248 c_pretty_printer ppi;
1249 tree stmt;
1251 const enum tree_code code = TREE_CODE (stmt);
1252 switch (code)
1254 case LABEL_STMT:
1255 case CASE_LABEL:
1256 pp_newline (ppi);
1257 if (code == LABEL_STMT)
1258 pp_tree_identifier (ppi, DECL_NAME (LABEL_STMT_LABEL (stmt)));
1259 else if (code == LABEL_STMT)
1261 if (CASE_LOW (stmt) == NULL_TREE)
1262 pp_identifier (ppi, "default");
1263 else
1265 pp_c_identifier (ppi, "case");
1266 pp_c_whitespace (ppi);
1267 pp_conditional_expression (ppi, CASE_LOW (stmt));
1268 if (CASE_HIGH (stmt))
1270 pp_identifier (ppi, "...");
1271 pp_conditional_expression (ppi, CASE_HIGH (stmt));
1275 pp_colon (ppi);
1276 pp_newline_and_indent (ppi, 3);
1277 break;
1279 case COMPOUND_STMT:
1280 pp_left_brace (ppi);
1281 pp_newline_and_indent (ppi, 3);
1282 for (stmt = COMPOUND_BODY (stmt); stmt; stmt = TREE_CHAIN (stmt))
1283 pp_c_statement (ppi, stmt);
1284 pp_newline_and_indent (ppi, -3);
1285 pp_right_brace (ppi);
1286 pp_newline (ppi);
1287 break;
1289 case EXPR_STMT:
1290 case CLEANUP_STMT:
1291 pp_newline (ppi);
1292 pp_c_expression (ppi, code == EXPR_STMT
1293 ? EXPR_STMT_EXPR (stmt)
1294 : CLEANUP_EXPR (stmt));
1295 pp_semicolon (ppi);
1296 pp_newline (ppi);
1297 break;
1299 case IF_STMT:
1300 pp_c_identifier (ppi, "if");
1301 pp_whitespace (ppi);
1302 pp_c_left_paren (ppi);
1303 pp_c_expression (ppi, IF_COND (stmt));
1304 pp_right_paren (ppi);
1305 pp_newline_and_indent (ppi, 3);
1306 pp_statement (ppi, THEN_CLAUSE (stmt));
1307 pp_newline_and_indent (ppi, -3);
1308 if (ELSE_CLAUSE (stmt))
1310 tree else_clause = ELSE_CLAUSE (stmt);
1311 pp_c_identifier (ppi, "else");
1312 if (TREE_CODE (else_clause) == IF_STMT)
1313 pp_c_whitespace (ppi);
1314 else
1315 pp_newline_and_indent (ppi, 3);
1316 pp_statement (ppi, else_clause);
1317 if (TREE_CODE (else_clause) != IF_STMT)
1318 pp_newline_and_indent (ppi, -3);
1320 break;
1322 case SWITCH_STMT:
1323 pp_newline (ppi);
1324 pp_c_identifier (ppi, "switch");
1325 pp_whitespace (ppi);
1326 pp_c_left_paren (ppi);
1327 pp_c_expression (ppi, SWITCH_COND (stmt));
1328 pp_right_paren (ppi);
1329 pp_newline_and_indent (ppi, 3);
1330 pp_statement (ppi, SWITCH_BODY (stmt));
1331 pp_newline_and_indent (ppi, -3);
1332 break;
1334 case WHILE_STMT:
1335 pp_c_identifier (ppi, "while");
1336 pp_whitespace (ppi);
1337 pp_c_left_paren (ppi);
1338 pp_c_expression (ppi, WHILE_COND (stmt));
1339 pp_right_paren (ppi);
1340 pp_newline_and_indent (ppi, 3);
1341 pp_statement (ppi, WHILE_BODY (stmt));
1342 pp_newline_and_indent (ppi, -3);
1343 break;
1345 case DO_STMT:
1346 pp_c_identifier (ppi, "do");
1347 pp_newline_and_indent (ppi, 3);
1348 pp_statement (ppi, DO_BODY (stmt));
1349 pp_newline_and_indent (ppi, -3);
1350 pp_c_identifier (ppi, "while");
1351 pp_whitespace (ppi);
1352 pp_c_left_paren (ppi);
1353 pp_c_expression (ppi, DO_COND (stmt));
1354 pp_c_right_paren (ppi);
1355 pp_semicolon (ppi);
1356 pp_newline (ppi);
1357 break;
1359 case FOR_STMT:
1360 pp_c_identifier (ppi, "for");
1361 pp_whitespace (ppi);
1362 pp_c_left_paren (ppi);
1363 pp_statement (ppi, FOR_INIT_STMT (stmt));
1364 pp_c_whitespace (ppi);
1365 if (FOR_COND (stmt))
1366 pp_c_expression (ppi, FOR_COND (stmt));
1367 pp_semicolon (ppi);
1368 pp_c_whitespace (ppi);
1369 if (FOR_EXPR (stmt))
1370 pp_c_expression (ppi, FOR_EXPR (stmt));
1371 pp_right_paren (ppi);
1372 pp_newline_and_indent (ppi, 3);
1373 pp_statement (ppi, FOR_BODY (stmt));
1374 pp_newline_and_indent (ppi, -3);
1375 break;
1377 case BREAK_STMT:
1378 case CONTINUE_STMT:
1379 pp_newline (ppi);
1380 pp_identifier (ppi, code == BREAK_STMT ? "break" : "continue");
1381 pp_semicolon (ppi);
1382 pp_newline (ppi);
1383 break;
1385 case RETURN_STMT:
1386 case GOTO_STMT:
1388 tree e = code == RETURN_STMT
1389 ? RETURN_STMT_EXPR (stmt)
1390 : GOTO_DESTINATION (stmt);
1392 pp_newline (ppi);
1393 pp_c_identifier (ppi, code == RETURN_STMT ? "return" : "goto");
1394 if (e)
1395 pp_c_expression (ppi, e);
1396 pp_semicolon (ppi);
1397 pp_newline (ppi);
1399 break;
1401 case SCOPE_STMT:
1402 if (!SCOPE_NULLIFIED_P (stmt) && SCOPE_NO_CLEANUPS_P (stmt))
1404 if (SCOPE_BEGIN_P (stmt))
1406 pp_left_brace (ppi);
1407 pp_newline_and_indent (ppi, 3);
1409 else if (SCOPE_END_P (stmt))
1411 pp_right_brace (ppi);
1412 pp_newline_and_indent (ppi, -3);
1415 break;
1417 case DECL_STMT:
1418 pp_declaration (ppi, DECL_STMT_DECL (stmt));
1419 pp_semicolon (ppi);
1420 pp_newline (ppi);
1421 break;
1423 case ASM_STMT:
1425 bool has_volatile_p = ASM_VOLATILE_P (stmt);
1426 bool is_extended = has_volatile_p || ASM_INPUTS (stmt)
1427 || ASM_OUTPUTS (stmt) || ASM_CLOBBERS (stmt);
1428 pp_c_identifier (ppi, is_extended ? "__asm__" : "asm");
1429 if (has_volatile_p)
1430 pp_c_identifier (ppi, "__volatile__");
1431 pp_whitespace (ppi);
1432 pp_c_left_paren (ppi);
1433 pp_c_string_literal (ppi, ASM_STRING (stmt));
1434 if (is_extended)
1436 pp_whitespace (ppi);
1437 pp_separate_with (ppi, ':');
1438 if (ASM_OUTPUTS (stmt))
1439 pp_c_expression (ppi, ASM_OUTPUTS (stmt));
1440 pp_whitespace (ppi);
1441 pp_separate_with (ppi, ':');
1442 if (ASM_INPUTS (stmt))
1443 pp_c_expression (ppi, ASM_INPUTS (stmt));
1444 pp_whitespace (ppi);
1445 pp_separate_with (ppi, ':');
1446 if (ASM_CLOBBERS (stmt))
1447 pp_c_expression (ppi, ASM_CLOBBERS (stmt));
1449 pp_right_paren (ppi);
1450 pp_newline (ppi);
1452 break;
1454 case FILE_STMT:
1455 pp_c_identifier (ppi, "__FILE__");
1456 pp_whitespace (ppi);
1457 pp_equal (ppi);
1458 pp_c_whitespace (ppi);
1459 pp_c_identifier (ppi, FILE_STMT_FILENAME (stmt));
1460 pp_semicolon (ppi);
1461 pp_newline (ppi);
1462 break;
1464 default:
1465 pp_unsupported_tree (ppi, stmt);
1471 /* Initialize the PRETTY-PRINTER for handling C codes. */
1472 void
1473 pp_c_pretty_printer_init (pp)
1474 c_pretty_printer pp;
1476 pp->offset_list = 0;
1478 pp->declaration = pp_c_declaration;
1479 pp->declaration_specifiers = pp_c_declaration_specifiers;
1480 pp->type_specifier = pp_c_simple_type_specifier;
1481 pp->declarator = pp_c_declarator;
1482 pp->direct_declarator = pp_c_direct_declarator;
1483 pp->parameter_declaration = pp_c_parameter_declaration;
1484 pp->type_id = pp_c_type_id;
1486 pp->statement = pp_c_statement;
1488 pp->primary_expression = pp_c_primary_expression;
1489 pp->postfix_expression = pp_c_postfix_expression;
1490 pp->unary_expression = pp_c_unary_expression;
1491 pp->initializer = pp_c_initializer;
1492 pp->multiplicative_expression = pp_c_multiplicative_expression;
1493 pp->conditional_expression = pp_c_conditional_expression;
1494 pp->assignment_expression = pp_c_assignment_expression;