* config.gcc: Add an extra_header for ARM targets.
[official-gcc.git] / gcc / c-pretty-print.c
blob39d72588281c576eb984d16a243675adbb88117b
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 pp_c_char (ppi, '-');
453 i = build_int_2 (-TREE_INT_CST_LOW (i),
454 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
455 sprintf (pp_buffer (ppi)->digit_buffer,
456 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
457 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
458 pp_identifier (ppi, pp_buffer (ppi)->digit_buffer);
465 /* Print out a REAL value. */
466 static inline void
467 pp_c_real_literal (ppi, r)
468 c_pretty_printer ppi;
469 tree r;
471 real_to_decimal (pp_buffer (ppi)->digit_buffer, &TREE_REAL_CST (r),
472 sizeof (pp_buffer (ppi)->digit_buffer), 0, 1);
473 pp_identifier (ppi, pp_buffer(ppi)->digit_buffer);
477 void
478 pp_c_literal (ppi, e)
479 c_pretty_printer ppi;
480 tree e;
482 switch (TREE_CODE (e))
484 case INTEGER_CST:
485 pp_c_integer_literal (ppi, e);
486 break;
488 case REAL_CST:
489 pp_c_real_literal (ppi, e);
490 break;
492 case STRING_CST:
493 pp_c_string_literal (ppi, e);
494 break;
496 default:
497 pp_unsupported_tree (ppi, e);
498 break;
502 /* Pretty-print a C primary-expression. */
503 static void
504 pp_c_primary_expression (ppi, e)
505 c_pretty_printer ppi;
506 tree e;
508 switch (TREE_CODE (e))
510 case VAR_DECL:
511 case PARM_DECL:
512 case FIELD_DECL:
513 case CONST_DECL:
514 case FUNCTION_DECL:
515 case LABEL_DECL:
516 e = DECL_NAME (e);
517 /* Fall through. */
518 case IDENTIFIER_NODE:
519 pp_c_tree_identifier (ppi, e);
520 break;
522 case ERROR_MARK:
523 pp_c_identifier (ppi, "<erroneous-expression>");
524 break;
526 case RESULT_DECL:
527 pp_c_identifier (ppi, "<return-value>");
528 break;
530 case INTEGER_CST:
531 case REAL_CST:
532 case STRING_CST:
533 pp_c_literal (ppi, e);
534 break;
536 case TARGET_EXPR:
537 pp_c_left_paren (ppi);
538 pp_c_identifier (ppi, "__builtin_memcpy");
539 pp_c_left_paren (ppi);
540 pp_ampersand (ppi);
541 pp_c_primary_expression (ppi, TREE_OPERAND (e, 0));
542 pp_separate_with (ppi, ',');
543 pp_ampersand (ppi);
544 pp_initializer (ppi, TREE_OPERAND (e, 1));
545 if (TREE_OPERAND (e, 2))
547 pp_separate_with (ppi, ',');
548 pp_c_expression (ppi, TREE_OPERAND (e, 2));
550 pp_c_right_paren (ppi);
552 case STMT_EXPR:
553 pp_c_left_paren (ppi);
554 pp_statement (ppi, STMT_EXPR_STMT (e));
555 pp_c_right_paren (ppi);
556 break;
558 default:
559 /* Make sure this call won't cause any infinite loop. */
560 pp_c_left_paren (ppi);
561 pp_c_expression (ppi, e);
562 pp_c_right_paren (ppi);
563 break;
567 /* Print out a C initializer -- also support C compound-literals. */
568 void
569 pp_c_initializer (ppi, e)
570 c_pretty_printer ppi;
571 tree e;
573 if (TREE_CODE (e) == CONSTRUCTOR)
575 enum tree_code code = TREE_CODE (TREE_TYPE (e));
576 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
578 pp_left_brace (ppi);
579 pp_c_initializer_list (ppi, e);
580 pp_right_brace (ppi);
582 else
583 pp_unsupported_tree (ppi, TREE_OPERAND (e, 1));
585 else
586 pp_assignment_expression (ppi, e);
589 static void
590 pp_c_initializer_list (ppi, e)
591 c_pretty_printer ppi;
592 tree e;
594 tree type = TREE_TYPE (e);
595 const enum tree_code code = TREE_CODE (type);
597 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
599 tree init = TREE_OPERAND (e, 1);
600 for (; init != NULL_TREE; init = TREE_CHAIN (init))
602 if (code == RECORD_TYPE || code == UNION_TYPE)
604 pp_dot (ppi);
605 pp_c_primary_expression (ppi, TREE_PURPOSE (init));
607 else
609 pp_c_left_bracket (ppi);
610 if (TREE_PURPOSE (init))
611 pp_c_literal (ppi, TREE_PURPOSE (init));
612 pp_c_right_bracket (ppi);
614 pp_c_whitespace (ppi);
615 pp_equal (ppi);
616 pp_c_whitespace (ppi);
617 pp_initializer (ppi, TREE_VALUE (init));
618 if (TREE_CHAIN (init))
619 pp_separate_with (ppi, ',');
622 else
623 pp_unsupported_tree (ppi, type);
626 void
627 pp_c_postfix_expression (ppi, e)
628 c_pretty_printer ppi;
629 tree e;
631 enum tree_code code = TREE_CODE (e);
632 switch (code)
634 case POSTINCREMENT_EXPR:
635 case POSTDECREMENT_EXPR:
636 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
637 pp_identifier (ppi, code == POSTINCREMENT_EXPR ? "++" : "--");
638 break;
640 case ARROW_EXPR:
641 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
642 pp_arrow (ppi);
643 break;
645 case ARRAY_REF:
646 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
647 pp_c_left_bracket (ppi);
648 pp_c_expression (ppi, TREE_OPERAND (e, 1));
649 pp_c_right_bracket (ppi);
650 break;
652 case CALL_EXPR:
653 pp_postfix_expression (ppi, TREE_OPERAND (e, 0));
654 pp_c_left_paren (ppi);
655 pp_c_expression_list (ppi, TREE_OPERAND (e, 1));
656 pp_c_right_paren (ppi);
657 break;
659 case ABS_EXPR:
660 case FFS_EXPR:
661 pp_c_identifier (ppi,
662 code == ABS_EXPR ? "__builtin_abs" : "__builtin_ffs");
663 pp_c_left_paren (ppi);
664 pp_c_expression (ppi, TREE_OPERAND (e, 0));
665 pp_c_right_paren (ppi);
666 break;
668 case COMPONENT_REF:
670 tree object = TREE_OPERAND (e, 0);
671 if (TREE_CODE (object) == INDIRECT_REF)
673 pp_postfix_expression (ppi, TREE_OPERAND (object, 0));
674 pp_arrow (ppi);
676 else
678 pp_postfix_expression (ppi, object);
679 pp_dot (ppi);
681 pp_c_expression (ppi, TREE_OPERAND (e, 1));
683 break;
685 case COMPLEX_CST:
686 case VECTOR_CST:
687 case COMPLEX_EXPR:
688 pp_c_left_paren (ppi);
689 pp_type_id (ppi, TREE_TYPE (e));
690 pp_c_right_paren (ppi);
691 pp_left_brace (ppi);
693 if (code == COMPLEX_CST)
695 pp_c_expression (ppi, TREE_REALPART (e));
696 pp_separate_with (ppi, ',');
697 pp_c_expression (ppi, TREE_IMAGPART (e));
699 else if (code == VECTOR_CST)
700 pp_c_expression_list (ppi, TREE_VECTOR_CST_ELTS (e));
701 else if (code == COMPLEX_EXPR)
703 pp_c_expression (ppi, TREE_OPERAND (e, 0));
704 pp_separate_with (ppi, ',');
705 pp_c_expression (ppi, TREE_OPERAND (e, 1));
708 pp_right_brace (ppi);
709 break;
711 case COMPOUND_LITERAL_EXPR:
712 e = DECL_INITIAL (e);
713 /* Fall through. */
714 case CONSTRUCTOR:
715 pp_initializer (ppi, e);
716 break;
718 case VA_ARG_EXPR:
719 pp_c_identifier (ppi, "__builtin_va_arg");
720 pp_c_left_paren (ppi);
721 pp_assignment_expression (ppi, TREE_OPERAND (e, 0));
722 pp_separate_with (ppi, ',');
723 pp_type_id (ppi, TREE_TYPE (e));
724 pp_c_right_paren (ppi);
725 break;
727 default:
728 pp_primary_expression (ppi, e);
729 break;
733 /* Print out an expression-list; E is expected to be a TREE_LIST */
734 void
735 pp_c_expression_list (ppi, e)
736 c_pretty_printer ppi;
737 tree e;
739 for (; e != NULL_TREE; e = TREE_CHAIN (e))
741 pp_c_assignment_expression (ppi, TREE_VALUE (e));
742 if (TREE_CHAIN (e))
743 pp_separate_with (ppi, ',');
747 static void
748 pp_c_unary_expression (ppi, e)
749 c_pretty_printer ppi;
750 tree e;
752 enum tree_code code = TREE_CODE (e);
753 switch (code)
755 case PREINCREMENT_EXPR:
756 case PREDECREMENT_EXPR:
757 pp_identifier (ppi, code == PREINCREMENT_EXPR ? "++" : "--");
758 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
759 break;
761 case ADDR_EXPR:
762 case INDIRECT_REF:
763 case CONVERT_EXPR:
764 case NEGATE_EXPR:
765 case BIT_NOT_EXPR:
766 case TRUTH_NOT_EXPR:
767 case CONJ_EXPR:
768 if (code == ADDR_EXPR)
769 pp_ampersand (ppi);
770 else if (code == INDIRECT_REF)
771 pp_star (ppi);
772 else if (code == NEGATE_EXPR)
773 pp_minus (ppi);
774 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
775 pp_complement (ppi);
776 else if (code == TRUTH_NOT_EXPR)
777 pp_exclamation (ppi);
778 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
779 break;
781 case SIZEOF_EXPR:
782 case ALIGNOF_EXPR:
783 pp_c_identifier (ppi, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
784 pp_c_whitespace (ppi);
785 if (TYPE_P (TREE_OPERAND (e, 0)))
787 pp_c_left_paren (ppi);
788 pp_type_id (ppi, TREE_OPERAND (e, 0));
789 pp_c_right_paren (ppi);
791 else
792 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
793 break;
795 case REALPART_EXPR:
796 case IMAGPART_EXPR:
797 pp_c_identifier (ppi, code == REALPART_EXPR ? "__real__" : "__imag__");
798 pp_c_whitespace (ppi);
799 pp_unary_expression (ppi, TREE_OPERAND (e, 0));
800 break;
802 default:
803 pp_postfix_expression (ppi, e);
804 break;
808 void
809 pp_c_cast_expression (ppi, e)
810 c_pretty_printer ppi;
811 tree e;
813 if (TREE_CODE (e) == CONVERT_EXPR || TREE_CODE (e) == FLOAT_EXPR)
815 pp_c_left_paren (ppi);
816 pp_type_id (ppi, TREE_TYPE (e));
817 pp_c_right_paren (ppi);
818 pp_c_cast_expression (ppi, TREE_OPERAND (e, 0));
820 else
821 pp_unary_expression (ppi, e);
824 static void
825 pp_c_multiplicative_expression (ppi, e)
826 c_pretty_printer ppi;
827 tree e;
829 enum tree_code code = TREE_CODE (e);
830 switch (code)
832 case MULT_EXPR:
833 case TRUNC_DIV_EXPR:
834 case TRUNC_MOD_EXPR:
835 pp_c_multiplicative_expression (ppi, TREE_OPERAND (e, 0));
836 pp_c_whitespace (ppi);
837 if (code == MULT_EXPR)
838 pp_star (ppi);
839 else if (code == TRUNC_DIV_EXPR)
840 pp_slash (ppi);
841 else
842 pp_modulo (ppi);
843 pp_c_whitespace (ppi);
844 pp_c_cast_expression (ppi, TREE_OPERAND (e, 1));
845 break;
847 default:
848 pp_c_cast_expression (ppi, e);
849 break;
853 static inline void
854 pp_c_additive_expression (ppi, e)
855 c_pretty_printer ppi;
856 tree e;
858 enum tree_code code = TREE_CODE (e);
859 switch (code)
861 case PLUS_EXPR:
862 case MINUS_EXPR:
863 pp_c_additive_expression (ppi, TREE_OPERAND (e, 0));
864 pp_c_whitespace (ppi);
865 if (code == PLUS_EXPR)
866 pp_plus (ppi);
867 else
868 pp_minus (ppi);
869 pp_c_whitespace (ppi);
870 pp_multiplicative_expression (ppi, TREE_OPERAND (e, 1));
871 break;
873 default:
874 pp_multiplicative_expression (ppi, e);
875 break;
879 static inline void
880 pp_c_shift_expression (ppi, e)
881 c_pretty_printer ppi;
882 tree e;
884 enum tree_code code = TREE_CODE (e);
885 switch (code)
887 case LSHIFT_EXPR:
888 case RSHIFT_EXPR:
889 pp_c_shift_expression (ppi, TREE_OPERAND (e, 0));
890 pp_c_whitespace (ppi);
891 pp_identifier (ppi, code == LSHIFT_EXPR ? "<<" : ">>");
892 pp_c_whitespace (ppi);
893 pp_c_additive_expression (ppi, TREE_OPERAND (e, 1));
894 break;
896 default:
897 pp_c_additive_expression (ppi, e);
901 static void
902 pp_c_relational_expression (ppi, e)
903 c_pretty_printer ppi;
904 tree e;
906 enum tree_code code = TREE_CODE (e);
907 switch (code)
909 case LT_EXPR:
910 case GT_EXPR:
911 case LE_EXPR:
912 case GE_EXPR:
913 pp_c_relational_expression (ppi, TREE_OPERAND (e, 0));
914 pp_c_whitespace (ppi);
915 if (code == LT_EXPR)
916 pp_less (ppi);
917 else if (code == GT_EXPR)
918 pp_greater (ppi);
919 else if (code == LE_EXPR)
920 pp_identifier (ppi, "<=");
921 else if (code == GE_EXPR)
922 pp_identifier (ppi, ">=");
923 pp_c_whitespace (ppi);
924 pp_c_shift_expression (ppi, TREE_OPERAND (e, 1));
925 break;
927 default:
928 pp_c_shift_expression (ppi, e);
929 break;
933 static inline void
934 pp_c_equality_expression (ppi, e)
935 c_pretty_printer ppi;
936 tree e;
938 enum tree_code code = TREE_CODE (e);
939 switch (code)
941 case EQ_EXPR:
942 case NE_EXPR:
943 pp_c_equality_expression (ppi, TREE_OPERAND (e, 0));
944 pp_c_maybe_whitespace (ppi);
945 pp_identifier (ppi, code == EQ_EXPR ? "==" : "!=");
946 pp_c_whitespace (ppi);
947 pp_c_relational_expression (ppi, TREE_OPERAND (e, 1));
948 break;
950 default:
951 pp_c_relational_expression (ppi, e);
952 break;
956 static inline void
957 pp_c_and_expression (ppi, e)
958 c_pretty_printer ppi;
959 tree e;
961 if (TREE_CODE (e) == BIT_AND_EXPR)
963 pp_c_and_expression (ppi, TREE_OPERAND (e, 0));
964 pp_c_maybe_whitespace (ppi);
965 pp_ampersand (ppi);
966 pp_c_whitespace (ppi);
967 pp_c_equality_expression (ppi, TREE_OPERAND (e, 1));
969 else
970 pp_c_equality_expression (ppi, e);
973 static inline void
974 pp_c_exclusive_or_expression (ppi, e)
975 c_pretty_printer ppi;
976 tree e;
978 if (TREE_CODE (e) == BIT_XOR_EXPR)
980 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
981 pp_c_maybe_whitespace (ppi);
982 pp_carret (ppi);
983 pp_c_whitespace (ppi);
984 pp_c_and_expression (ppi, TREE_OPERAND (e, 1));
986 else
987 pp_c_and_expression (ppi, e);
990 static inline void
991 pp_c_inclusive_or_expression (ppi, e)
992 c_pretty_printer ppi;
993 tree e;
995 if (TREE_CODE (e) == BIT_IOR_EXPR)
997 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 0));
998 pp_c_maybe_whitespace (ppi);
999 pp_bar (ppi);
1000 pp_c_whitespace (ppi);
1001 pp_c_exclusive_or_expression (ppi, TREE_OPERAND (e, 1));
1003 else
1004 pp_c_exclusive_or_expression (ppi, e);
1007 static inline void
1008 pp_c_logical_and_expression (ppi, e)
1009 c_pretty_printer ppi;
1010 tree e;
1012 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1014 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 0));
1015 pp_c_maybe_whitespace (ppi);
1016 pp_identifier (ppi, "&&");
1017 pp_c_whitespace (ppi);
1018 pp_c_inclusive_or_expression (ppi, TREE_OPERAND (e, 1));
1020 else
1021 pp_c_inclusive_or_expression (ppi, e);
1024 void
1025 pp_c_logical_or_expression (ppi, e)
1026 c_pretty_printer ppi;
1027 tree e;
1029 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1031 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
1032 pp_c_maybe_whitespace (ppi);
1033 pp_identifier (ppi, "||");
1034 pp_c_whitespace (ppi);
1035 pp_c_logical_and_expression (ppi, TREE_OPERAND (e, 1));
1037 else
1038 pp_c_logical_and_expression (ppi, e);
1041 static void
1042 pp_c_conditional_expression (ppi, e)
1043 c_pretty_printer ppi;
1044 tree e;
1046 if (TREE_CODE (e) == COND_EXPR)
1048 pp_c_logical_or_expression (ppi, TREE_OPERAND (e, 0));
1049 pp_c_maybe_whitespace (ppi);
1050 pp_question (ppi);
1051 pp_c_whitespace (ppi);
1052 pp_c_expression (ppi, TREE_OPERAND (e, 1));
1053 pp_c_maybe_whitespace (ppi);
1054 pp_colon (ppi);
1055 pp_c_whitespace (ppi);
1056 pp_c_conditional_expression (ppi, TREE_OPERAND (e, 2));
1058 else
1059 pp_c_logical_or_expression (ppi, e);
1063 /* Pretty-print a C assignment-expression. */
1064 static void
1065 pp_c_assignment_expression (ppi, e)
1066 c_pretty_printer ppi;
1067 tree e;
1069 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1071 pp_c_unary_expression (ppi, TREE_OPERAND (e, 0));
1072 pp_c_maybe_whitespace (ppi);
1073 pp_equal (ppi);
1074 pp_whitespace (ppi);
1075 pp_c_assignment_expression (ppi, TREE_OPERAND (e, 1));
1077 else
1078 pp_c_conditional_expression (ppi, e);
1081 /* Pretty-print an expression. */
1082 void
1083 pp_c_expression (ppi, e)
1084 c_pretty_printer ppi;
1085 tree e;
1087 switch (TREE_CODE (e))
1089 case INTEGER_CST:
1090 pp_c_integer_literal (ppi, e);
1091 break;
1093 case REAL_CST:
1094 pp_c_real_literal (ppi, e);
1095 break;
1097 case STRING_CST:
1098 pp_c_string_literal (ppi, e);
1099 break;
1101 case FUNCTION_DECL:
1102 case VAR_DECL:
1103 case CONST_DECL:
1104 case PARM_DECL:
1105 case RESULT_DECL:
1106 case FIELD_DECL:
1107 case LABEL_DECL:
1108 case ERROR_MARK:
1109 case TARGET_EXPR:
1110 case STMT_EXPR:
1111 pp_c_primary_expression (ppi, e);
1112 break;
1114 case POSTINCREMENT_EXPR:
1115 case POSTDECREMENT_EXPR:
1116 case ARROW_EXPR:
1117 case ARRAY_REF:
1118 case CALL_EXPR:
1119 case COMPONENT_REF:
1120 case COMPLEX_CST:
1121 case VECTOR_CST:
1122 case ABS_EXPR:
1123 case FFS_EXPR:
1124 case CONSTRUCTOR:
1125 case COMPOUND_LITERAL_EXPR:
1126 case COMPLEX_EXPR:
1127 case VA_ARG_EXPR:
1128 pp_c_postfix_expression (ppi, e);
1129 break;
1131 case CONJ_EXPR:
1132 case ADDR_EXPR:
1133 case INDIRECT_REF:
1134 case NEGATE_EXPR:
1135 case BIT_NOT_EXPR:
1136 case TRUTH_NOT_EXPR:
1137 case PREINCREMENT_EXPR:
1138 case PREDECREMENT_EXPR:
1139 case SIZEOF_EXPR:
1140 case ALIGNOF_EXPR:
1141 case REALPART_EXPR:
1142 case IMAGPART_EXPR:
1143 pp_c_unary_expression (ppi, e);
1144 break;
1146 case CONVERT_EXPR:
1147 case FLOAT_EXPR:
1148 pp_c_cast_expression (ppi, e);
1149 break;
1151 case MULT_EXPR:
1152 case TRUNC_MOD_EXPR:
1153 case TRUNC_DIV_EXPR:
1154 pp_c_multiplicative_expression (ppi, e);
1155 break;
1157 case LSHIFT_EXPR:
1158 case RSHIFT_EXPR:
1159 pp_c_shift_expression (ppi, e);
1160 break;
1162 case LT_EXPR:
1163 case GT_EXPR:
1164 case LE_EXPR:
1165 case GE_EXPR:
1166 pp_c_relational_expression (ppi, e);
1167 break;
1169 case BIT_AND_EXPR:
1170 pp_c_and_expression (ppi, e);
1171 break;
1173 case BIT_XOR_EXPR:
1174 pp_c_exclusive_or_expression (ppi, e);
1175 break;
1177 case BIT_IOR_EXPR:
1178 pp_c_inclusive_or_expression (ppi, e);
1179 break;
1181 case TRUTH_ANDIF_EXPR:
1182 pp_c_logical_and_expression (ppi, e);
1183 break;
1185 case TRUTH_ORIF_EXPR:
1186 pp_c_logical_or_expression (ppi, e);
1187 break;
1189 case COND_EXPR:
1190 pp_c_conditional_expression (ppi, e);
1191 break;
1193 case MODIFY_EXPR:
1194 case INIT_EXPR:
1195 pp_c_assignment_expression (ppi, e);
1196 break;
1198 case NOP_EXPR:
1199 pp_c_expression (ppi, TREE_OPERAND (e, 0));
1200 break;
1202 case COMPOUND_EXPR:
1203 pp_c_left_paren (ppi);
1204 pp_c_expression (ppi, TREE_OPERAND (e, 0));
1205 pp_separate_with (ppi, ',');
1206 pp_assignment_expression (ppi, TREE_OPERAND (e, 1));
1207 pp_c_right_paren (ppi);
1208 break;
1211 default:
1212 pp_unsupported_tree (ppi, e);
1213 break;
1218 /* Statements. */
1219 void
1220 pp_c_statement (ppi, stmt)
1221 c_pretty_printer ppi;
1222 tree stmt;
1224 const enum tree_code code = TREE_CODE (stmt);
1225 switch (code)
1227 case LABEL_STMT:
1228 case CASE_LABEL:
1229 pp_newline (ppi);
1230 if (code == LABEL_STMT)
1231 pp_tree_identifier (ppi, DECL_NAME (LABEL_STMT_LABEL (stmt)));
1232 else if (code == LABEL_STMT)
1234 if (CASE_LOW (stmt) == NULL_TREE)
1235 pp_identifier (ppi, "default");
1236 else
1238 pp_c_identifier (ppi, "case");
1239 pp_c_whitespace (ppi);
1240 pp_conditional_expression (ppi, CASE_LOW (stmt));
1241 if (CASE_HIGH (stmt))
1243 pp_identifier (ppi, "...");
1244 pp_conditional_expression (ppi, CASE_HIGH (stmt));
1248 pp_colon (ppi);
1249 pp_newline_and_indent (ppi, 3);
1250 break;
1252 case COMPOUND_STMT:
1253 pp_left_brace (ppi);
1254 pp_newline_and_indent (ppi, 3);
1255 for (stmt = COMPOUND_BODY (stmt); stmt; stmt = TREE_CHAIN (stmt))
1256 pp_c_statement (ppi, stmt);
1257 pp_newline_and_indent (ppi, -3);
1258 pp_right_brace (ppi);
1259 pp_newline (ppi);
1260 break;
1262 case EXPR_STMT:
1263 case CLEANUP_STMT:
1264 pp_newline (ppi);
1265 pp_c_expression (ppi, code == EXPR_STMT
1266 ? EXPR_STMT_EXPR (stmt)
1267 : CLEANUP_EXPR (stmt));
1268 pp_semicolon (ppi);
1269 pp_newline (ppi);
1270 break;
1272 case IF_STMT:
1273 pp_c_identifier (ppi, "if");
1274 pp_whitespace (ppi);
1275 pp_c_left_paren (ppi);
1276 pp_c_expression (ppi, IF_COND (stmt));
1277 pp_right_paren (ppi);
1278 pp_newline_and_indent (ppi, 3);
1279 pp_statement (ppi, THEN_CLAUSE (stmt));
1280 pp_newline_and_indent (ppi, -3);
1281 if (ELSE_CLAUSE (stmt))
1283 tree else_clause = ELSE_CLAUSE (stmt);
1284 pp_c_identifier (ppi, "else");
1285 if (TREE_CODE (else_clause) == IF_STMT)
1286 pp_c_whitespace (ppi);
1287 else
1288 pp_newline_and_indent (ppi, 3);
1289 pp_statement (ppi, else_clause);
1290 if (TREE_CODE (else_clause) != IF_STMT)
1291 pp_newline_and_indent (ppi, -3);
1293 break;
1295 case SWITCH_STMT:
1296 pp_newline (ppi);
1297 pp_c_identifier (ppi, "switch");
1298 pp_whitespace (ppi);
1299 pp_c_left_paren (ppi);
1300 pp_c_expression (ppi, SWITCH_COND (stmt));
1301 pp_right_paren (ppi);
1302 pp_newline_and_indent (ppi, 3);
1303 pp_statement (ppi, SWITCH_BODY (stmt));
1304 pp_newline_and_indent (ppi, -3);
1305 break;
1307 case WHILE_STMT:
1308 pp_c_identifier (ppi, "while");
1309 pp_whitespace (ppi);
1310 pp_c_left_paren (ppi);
1311 pp_c_expression (ppi, WHILE_COND (stmt));
1312 pp_right_paren (ppi);
1313 pp_newline_and_indent (ppi, 3);
1314 pp_statement (ppi, WHILE_BODY (stmt));
1315 pp_newline_and_indent (ppi, -3);
1316 break;
1318 case DO_STMT:
1319 pp_c_identifier (ppi, "do");
1320 pp_newline_and_indent (ppi, 3);
1321 pp_statement (ppi, DO_BODY (stmt));
1322 pp_newline_and_indent (ppi, -3);
1323 pp_c_identifier (ppi, "while");
1324 pp_whitespace (ppi);
1325 pp_c_left_paren (ppi);
1326 pp_c_expression (ppi, DO_COND (stmt));
1327 pp_c_right_paren (ppi);
1328 pp_semicolon (ppi);
1329 pp_newline (ppi);
1330 break;
1332 case FOR_STMT:
1333 pp_c_identifier (ppi, "for");
1334 pp_whitespace (ppi);
1335 pp_c_left_paren (ppi);
1336 pp_statement (ppi, FOR_INIT_STMT (stmt));
1337 pp_c_whitespace (ppi);
1338 if (FOR_COND (stmt))
1339 pp_c_expression (ppi, FOR_COND (stmt));
1340 pp_semicolon (ppi);
1341 pp_c_whitespace (ppi);
1342 if (FOR_EXPR (stmt))
1343 pp_c_expression (ppi, FOR_EXPR (stmt));
1344 pp_right_paren (ppi);
1345 pp_newline_and_indent (ppi, 3);
1346 pp_statement (ppi, FOR_BODY (stmt));
1347 pp_newline_and_indent (ppi, -3);
1348 break;
1350 case BREAK_STMT:
1351 case CONTINUE_STMT:
1352 pp_newline (ppi);
1353 pp_identifier (ppi, code == BREAK_STMT ? "break" : "continue");
1354 pp_semicolon (ppi);
1355 pp_newline (ppi);
1356 break;
1358 case RETURN_STMT:
1359 case GOTO_STMT:
1361 tree e = code == RETURN_STMT
1362 ? RETURN_STMT_EXPR (stmt)
1363 : GOTO_DESTINATION (stmt);
1365 pp_newline (ppi);
1366 pp_c_identifier (ppi, code == RETURN_STMT ? "return" : "goto");
1367 if (e)
1368 pp_c_expression (ppi, e);
1369 pp_semicolon (ppi);
1370 pp_newline (ppi);
1372 break;
1374 case SCOPE_STMT:
1375 if (!SCOPE_NULLIFIED_P (stmt) && SCOPE_NO_CLEANUPS_P (stmt))
1377 if (SCOPE_BEGIN_P (stmt))
1379 pp_left_brace (ppi);
1380 pp_newline_and_indent (ppi, 3);
1382 else if (SCOPE_END_P (stmt))
1384 pp_right_brace (ppi);
1385 pp_newline_and_indent (ppi, -3);
1388 break;
1390 case DECL_STMT:
1391 pp_declaration (ppi, DECL_STMT_DECL (stmt));
1392 pp_semicolon (ppi);
1393 pp_newline (ppi);
1394 break;
1396 case ASM_STMT:
1398 bool has_volatile_p = ASM_VOLATILE_P (stmt);
1399 bool is_extended = has_volatile_p || ASM_INPUTS (stmt)
1400 || ASM_OUTPUTS (stmt) || ASM_CLOBBERS (stmt);
1401 pp_c_identifier (ppi, is_extended ? "__asm__" : "asm");
1402 if (has_volatile_p)
1403 pp_c_identifier (ppi, "__volatile__");
1404 pp_whitespace (ppi);
1405 pp_c_left_paren (ppi);
1406 pp_c_string_literal (ppi, ASM_STRING (stmt));
1407 if (is_extended)
1409 pp_whitespace (ppi);
1410 pp_separate_with (ppi, ':');
1411 if (ASM_OUTPUTS (stmt))
1412 pp_c_expression (ppi, ASM_OUTPUTS (stmt));
1413 pp_whitespace (ppi);
1414 pp_separate_with (ppi, ':');
1415 if (ASM_INPUTS (stmt))
1416 pp_c_expression (ppi, ASM_INPUTS (stmt));
1417 pp_whitespace (ppi);
1418 pp_separate_with (ppi, ':');
1419 if (ASM_CLOBBERS (stmt))
1420 pp_c_expression (ppi, ASM_CLOBBERS (stmt));
1422 pp_right_paren (ppi);
1423 pp_newline (ppi);
1425 break;
1427 case FILE_STMT:
1428 pp_c_identifier (ppi, "__FILE__");
1429 pp_whitespace (ppi);
1430 pp_equal (ppi);
1431 pp_c_whitespace (ppi);
1432 pp_c_identifier (ppi, FILE_STMT_FILENAME (stmt));
1433 pp_semicolon (ppi);
1434 pp_newline (ppi);
1435 break;
1437 default:
1438 pp_unsupported_tree (ppi, stmt);
1444 /* Initialize the PRETTY-PRINTER for handling C codes. */
1445 void
1446 pp_c_pretty_printer_init (pp)
1447 c_pretty_printer pp;
1449 pp->offset_list = 0;
1451 pp->declaration = pp_c_declaration;
1452 pp->declaration_specifiers = pp_c_declaration_specifiers;
1453 pp->type_specifier = pp_c_simple_type_specifier;
1454 pp->declarator = pp_c_declarator;
1455 pp->direct_declarator = pp_c_direct_declarator;
1456 pp->parameter_declaration = pp_c_parameter_declaration;
1457 pp->type_id = pp_c_type_id;
1459 pp->statement = pp_c_statement;
1461 pp->primary_expression = pp_c_primary_expression;
1462 pp->postfix_expression = pp_c_postfix_expression;
1463 pp->unary_expression = pp_c_unary_expression;
1464 pp->initializer = pp_c_initializer;
1465 pp->multiplicative_expression = pp_c_multiplicative_expression;
1466 pp->conditional_expression = pp_c_conditional_expression;
1467 pp->assignment_expression = pp_c_assignment_expression;