PR other/16240
[official-gcc.git] / gcc / c-pretty-print.c
blobfd3ef82ac0270c6d1d55a2463bf2a7800713cbfe
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004 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"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
41 do { \
42 if (pp_base (PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
44 } while (0)
46 /* literal */
47 static void pp_c_char (c_pretty_printer *, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer *, tree);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
53 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
54 static void pp_c_additive_expression (c_pretty_printer *, tree);
55 static void pp_c_shift_expression (c_pretty_printer *, tree);
56 static void pp_c_relational_expression (c_pretty_printer *, tree);
57 static void pp_c_equality_expression (c_pretty_printer *, tree);
58 static void pp_c_and_expression (c_pretty_printer *, tree);
59 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
61 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62 static void pp_c_conditional_expression (c_pretty_printer *, tree);
63 static void pp_c_assignment_expression (c_pretty_printer *, tree);
65 /* declarations. */
68 /* Helper functions. */
70 void
71 pp_c_whitespace (c_pretty_printer *pp)
73 pp_space (pp);
74 pp_base (pp)->padding = pp_none;
77 void
78 pp_c_left_paren (c_pretty_printer *pp)
80 pp_left_paren (pp);
81 pp_base (pp)->padding = pp_none;
84 void
85 pp_c_right_paren (c_pretty_printer *pp)
87 pp_right_paren (pp);
88 pp_base (pp)->padding = pp_none;
91 void
92 pp_c_left_brace (c_pretty_printer *pp)
94 pp_left_brace (pp);
95 pp_base (pp)->padding = pp_none;
98 void
99 pp_c_right_brace (c_pretty_printer *pp)
101 pp_right_brace (pp);
102 pp_base (pp)->padding = pp_none;
105 void
106 pp_c_left_bracket (c_pretty_printer *pp)
108 pp_left_bracket (pp);
109 pp_base (pp)->padding = pp_none;
112 void
113 pp_c_right_bracket (c_pretty_printer *pp)
115 pp_right_bracket (pp);
116 pp_base (pp)->padding = pp_none;
119 void
120 pp_c_dot (c_pretty_printer *pp)
122 pp_dot (pp);
123 pp_base (pp)->padding = pp_none;
126 void
127 pp_c_ampersand (c_pretty_printer *pp)
129 pp_ampersand (pp);
130 pp_base (pp)->padding = pp_none;
133 void
134 pp_c_star (c_pretty_printer *pp)
136 pp_star (pp);
137 pp_base (pp)->padding = pp_none;
140 void
141 pp_c_arrow (c_pretty_printer *pp)
143 pp_arrow (pp);
144 pp_base (pp)->padding = pp_none;
147 void
148 pp_c_semicolon (c_pretty_printer *pp)
150 pp_semicolon (pp);
151 pp_base (pp)->padding = pp_none;
154 void
155 pp_c_complement (c_pretty_printer *pp)
157 pp_complement (pp);
158 pp_base (pp)->padding = pp_none;
161 void
162 pp_c_exclamation (c_pretty_printer *pp)
164 pp_exclamation (pp);
165 pp_base (pp)->padding = pp_none;
168 /* Print out the external representation of CV-QUALIFIER. */
170 static void
171 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
173 const char *p = pp_last_position_in_text (pp);
174 /* The C programming language does not have references, but it is much
175 simpler to handle those here rather than going through the same
176 logic in the C++ pretty-printer. */
177 if (p != NULL && (*p == '*' || *p == '&'))
178 pp_c_whitespace (pp);
179 pp_c_identifier (pp, cv);
182 /* Pretty-print T using the type-cast notation '( type-name )'. */
184 static void
185 pp_c_type_cast (c_pretty_printer *pp, tree t)
187 pp_c_left_paren (pp);
188 pp_type_id (pp, t);
189 pp_c_right_paren (pp);
192 /* We're about to pretty-print a pointer type as indicated by T.
193 Output a whitespace, if needed, preparing for subsequent output. */
195 void
196 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
198 if (POINTER_TYPE_P (t))
200 tree pointee = strip_pointer_operator (TREE_TYPE (t));
201 if (TREE_CODE (pointee) != ARRAY_TYPE
202 && TREE_CODE (pointee) != FUNCTION_TYPE)
203 pp_c_whitespace (pp);
208 /* Declarations. */
210 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
211 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
212 of its type. Take care of possible extensions.
214 type-qualifier-list:
215 type-qualifier
216 type-qualifier-list type-qualifier
218 type-qualifier:
219 const
220 restrict -- C99
221 __restrict__ -- GNU C
222 volatile */
224 void
225 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
227 int qualifiers;
229 if (!TYPE_P (t))
230 t = TREE_TYPE (t);
232 qualifiers = TYPE_QUALS (t);
233 if (qualifiers & TYPE_QUAL_CONST)
234 pp_c_cv_qualifier (pp, "const");
235 if (qualifiers & TYPE_QUAL_VOLATILE)
236 pp_c_cv_qualifier (pp, "volatile");
237 if (qualifiers & TYPE_QUAL_RESTRICT)
238 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
241 /* pointer:
242 * type-qualifier-list(opt)
243 * type-qualifier-list(opt) pointer */
245 static void
246 pp_c_pointer (c_pretty_printer *pp, tree t)
248 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
249 t = TREE_TYPE (t);
250 switch (TREE_CODE (t))
252 case POINTER_TYPE:
253 /* It is easier to handle C++ reference types here. */
254 case REFERENCE_TYPE:
255 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
256 pp_c_pointer (pp, TREE_TYPE (t));
257 if (TREE_CODE (t) == POINTER_TYPE)
258 pp_c_star (pp);
259 else
260 pp_c_ampersand (pp);
261 pp_c_type_qualifier_list (pp, t);
262 break;
264 /* ??? This node is now in GENERIC and so shouldn't be here. But
265 we'll fix that later. */
266 case DECL_EXPR:
267 pp_declaration (pp, DECL_EXPR_DECL (t));
268 pp_needs_newline (pp) = true;
269 break;
271 default:
272 pp_unsupported_tree (pp, t);
276 /* type-specifier:
277 void
278 char
279 short
281 long
282 float
283 double
284 signed
285 unsigned
286 _Bool -- C99
287 _Complex -- C99
288 _Imaginary -- C99
289 struct-or-union-specifier
290 enum-specifier
291 typedef-name.
293 GNU extensions.
294 simple-type-specifier:
295 __complex__
296 __vector__ */
298 void
299 pp_c_type_specifier (c_pretty_printer *pp, tree t)
301 const enum tree_code code = TREE_CODE (t);
302 switch (code)
304 case ERROR_MARK:
305 pp_c_identifier (pp, "<type-error>");
306 break;
308 case IDENTIFIER_NODE:
309 pp_c_tree_decl_identifier (pp, t);
310 break;
312 case VOID_TYPE:
313 case BOOLEAN_TYPE:
314 case CHAR_TYPE:
315 case INTEGER_TYPE:
316 case REAL_TYPE:
317 if (TYPE_NAME (t))
318 t = TYPE_NAME (t);
319 else
320 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
321 pp_c_type_specifier (pp, t);
322 break;
324 case TYPE_DECL:
325 if (DECL_NAME (t))
326 pp_id_expression (pp, t);
327 else
328 pp_c_identifier (pp, "<typedef-error>");
329 break;
331 case UNION_TYPE:
332 case RECORD_TYPE:
333 case ENUMERAL_TYPE:
334 if (code == UNION_TYPE)
335 pp_c_identifier (pp, "union");
336 else if (code == RECORD_TYPE)
337 pp_c_identifier (pp, "struct");
338 else if (code == ENUMERAL_TYPE)
339 pp_c_identifier (pp, "enum");
340 else
341 pp_c_identifier (pp, "<tag-error>");
343 if (TYPE_NAME (t))
344 pp_id_expression (pp, TYPE_NAME (t));
345 else
346 pp_c_identifier (pp, "<anonymous>");
347 break;
349 default:
350 pp_unsupported_tree (pp, t);
351 break;
355 /* specifier-qualifier-list:
356 type-specifier specifier-qualifier-list-opt
357 type-qualifier specifier-qualifier-list-opt
360 Implementation note: Because of the non-linearities in array or
361 function declarations, this routine prints not just the
362 specifier-qualifier-list of such entities or types of such entities,
363 but also the 'pointer' production part of their declarators. The
364 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
366 void
367 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
369 const enum tree_code code = TREE_CODE (t);
371 if (TREE_CODE (t) != POINTER_TYPE)
372 pp_c_type_qualifier_list (pp, t);
373 switch (code)
375 case REFERENCE_TYPE:
376 case POINTER_TYPE:
378 /* Get the types-specifier of this type. */
379 tree pointee = strip_pointer_operator (TREE_TYPE (t));
380 pp_c_specifier_qualifier_list (pp, pointee);
381 if (TREE_CODE (pointee) == ARRAY_TYPE
382 || TREE_CODE (pointee) == FUNCTION_TYPE)
384 pp_c_whitespace (pp);
385 pp_c_left_paren (pp);
387 pp_ptr_operator (pp, t);
389 break;
391 case FUNCTION_TYPE:
392 case ARRAY_TYPE:
393 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
394 break;
396 case VECTOR_TYPE:
397 case COMPLEX_TYPE:
398 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
399 if (code == COMPLEX_TYPE)
400 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
401 else if (code == VECTOR_TYPE)
402 pp_c_identifier (pp, "__vector__");
403 break;
405 default:
406 pp_simple_type_specifier (pp, t);
407 break;
411 /* parameter-type-list:
412 parameter-list
413 parameter-list , ...
415 parameter-list:
416 parameter-declaration
417 parameter-list , parameter-declaration
419 parameter-declaration:
420 declaration-specifiers declarator
421 declaration-specifiers abstract-declarator(opt) */
423 void
424 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
426 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
427 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
428 pp_c_left_paren (pp);
429 if (parms == void_list_node)
430 pp_c_identifier (pp, "void");
431 else
433 bool first = true;
434 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
436 if (!first)
437 pp_separate_with (pp, ',');
438 first = false;
439 pp_declaration_specifiers
440 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
441 if (want_parm_decl)
442 pp_declarator (pp, parms);
443 else
444 pp_abstract_declarator (pp, TREE_VALUE (parms));
447 pp_c_right_paren (pp);
450 /* abstract-declarator:
451 pointer
452 pointer(opt) direct-abstract-declarator */
454 static void
455 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
457 if (TREE_CODE (t) == POINTER_TYPE)
459 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
460 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
461 pp_c_right_paren (pp);
462 t = TREE_TYPE (t);
465 pp_direct_abstract_declarator (pp, t);
468 /* direct-abstract-declarator:
469 ( abstract-declarator )
470 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
471 direct-abstract-declarator(opt) [ * ]
472 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
474 void
475 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
477 switch (TREE_CODE (t))
479 case POINTER_TYPE:
480 pp_abstract_declarator (pp, t);
481 break;
483 case FUNCTION_TYPE:
484 pp_c_parameter_type_list (pp, t);
485 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
486 break;
488 case ARRAY_TYPE:
489 pp_c_left_bracket (pp);
490 if (TYPE_DOMAIN (t))
491 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
492 pp_c_right_bracket (pp);
493 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
494 break;
496 case IDENTIFIER_NODE:
497 case VOID_TYPE:
498 case BOOLEAN_TYPE:
499 case INTEGER_TYPE:
500 case REAL_TYPE:
501 case ENUMERAL_TYPE:
502 case RECORD_TYPE:
503 case UNION_TYPE:
504 case VECTOR_TYPE:
505 case COMPLEX_TYPE:
506 case TYPE_DECL:
507 break;
509 default:
510 pp_unsupported_tree (pp, t);
511 break;
515 /* type-name:
516 specifier-qualifier-list abstract-declarator(opt) */
518 void
519 pp_c_type_id (c_pretty_printer *pp, tree t)
521 pp_c_specifier_qualifier_list (pp, t);
522 pp_abstract_declarator (pp, t);
525 /* storage-class-specifier:
526 typedef
527 extern
528 static
529 auto
530 register */
532 void
533 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
535 if (TREE_CODE (t) == TYPE_DECL)
536 pp_c_identifier (pp, "typedef");
537 else if (DECL_P (t))
539 if (DECL_REGISTER (t))
540 pp_c_identifier (pp, "register");
541 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
542 pp_c_identifier (pp, "static");
546 /* function-specifier:
547 inline */
549 void
550 pp_c_function_specifier (c_pretty_printer *pp, tree t)
552 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
553 pp_c_identifier (pp, "inline");
556 /* declaration-specifiers:
557 storage-class-specifier declaration-specifiers(opt)
558 type-specifier declaration-specifiers(opt)
559 type-qualifier declaration-specifiers(opt)
560 function-specifier declaration-specifiers(opt) */
562 void
563 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
565 pp_storage_class_specifier (pp, t);
566 pp_function_specifier (pp, t);
567 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
570 /* direct-declarator
571 identifier
572 ( declarator )
573 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
574 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
575 direct-declarator [ type-qualifier-list static assignment-expression ]
576 direct-declarator [ type-qualifier-list * ]
577 direct-declarator ( parameter-type-list )
578 direct-declarator ( identifier-list(opt) ) */
580 void
581 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
583 switch (TREE_CODE (t))
585 case VAR_DECL:
586 case PARM_DECL:
587 case TYPE_DECL:
588 case FIELD_DECL:
589 case LABEL_DECL:
590 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
591 pp_c_tree_decl_identifier (pp, t);
592 break;
594 case ARRAY_TYPE:
595 case POINTER_TYPE:
596 pp_abstract_declarator (pp, TREE_TYPE (t));
597 break;
599 case FUNCTION_TYPE:
600 pp_parameter_list (pp, t);
601 pp_abstract_declarator (pp, TREE_TYPE (t));
602 break;
604 case FUNCTION_DECL:
605 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
606 pp_c_tree_decl_identifier (pp, t);
607 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
608 pp_abstract_declarator (pp, TREE_TYPE (t));
609 else
611 pp_parameter_list (pp, t);
612 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
614 break;
616 case INTEGER_TYPE:
617 case REAL_TYPE:
618 case ENUMERAL_TYPE:
619 case UNION_TYPE:
620 case RECORD_TYPE:
621 break;
623 default:
624 pp_unsupported_tree (pp, t);
625 break;
630 /* declarator:
631 pointer(opt) direct-declarator */
633 void
634 pp_c_declarator (c_pretty_printer *pp, tree t)
636 switch (TREE_CODE (t))
638 case INTEGER_TYPE:
639 case REAL_TYPE:
640 case ENUMERAL_TYPE:
641 case UNION_TYPE:
642 case RECORD_TYPE:
643 break;
645 case VAR_DECL:
646 case PARM_DECL:
647 case FIELD_DECL:
648 case ARRAY_TYPE:
649 case FUNCTION_TYPE:
650 case FUNCTION_DECL:
651 case TYPE_DECL:
652 pp_direct_declarator (pp, t);
653 break;
656 default:
657 pp_unsupported_tree (pp, t);
658 break;
662 /* declaration:
663 declaration-specifiers init-declarator-list(opt) ; */
665 void
666 pp_c_declaration (c_pretty_printer *pp, tree t)
668 pp_declaration_specifiers (pp, t);
669 pp_c_init_declarator (pp, t);
672 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
674 void
675 pp_c_attributes (c_pretty_printer *pp, tree attributes)
677 if (attributes == NULL_TREE)
678 return;
680 pp_c_identifier (pp, "__attribute__");
681 pp_c_left_paren (pp);
682 pp_c_left_paren (pp);
683 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
685 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
686 if (TREE_VALUE (attributes))
687 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
689 if (TREE_CHAIN (attributes))
690 pp_separate_with (pp, ',');
692 pp_c_right_paren (pp);
693 pp_c_right_paren (pp);
696 /* function-definition:
697 declaration-specifiers declarator compound-statement */
699 void
700 pp_c_function_definition (c_pretty_printer *pp, tree t)
702 pp_declaration_specifiers (pp, t);
703 pp_declarator (pp, t);
704 pp_needs_newline (pp) = true;
705 pp_statement (pp, DECL_SAVED_TREE (t));
706 pp_newline (pp);
707 pp_flush (pp);
711 /* Expressions. */
713 /* Print out a c-char. */
715 static void
716 pp_c_char (c_pretty_printer *pp, int c)
718 switch (c)
720 case TARGET_NEWLINE:
721 pp_string (pp, "\\n");
722 break;
723 case TARGET_TAB:
724 pp_string (pp, "\\t");
725 break;
726 case TARGET_VT:
727 pp_string (pp, "\\v");
728 break;
729 case TARGET_BS:
730 pp_string (pp, "\\b");
731 break;
732 case TARGET_CR:
733 pp_string (pp, "\\r");
734 break;
735 case TARGET_FF:
736 pp_string (pp, "\\f");
737 break;
738 case TARGET_BELL:
739 pp_string (pp, "\\a");
740 break;
741 case '\\':
742 pp_string (pp, "\\\\");
743 break;
744 case '\'':
745 pp_string (pp, "\\'");
746 break;
747 case '\"':
748 pp_string (pp, "\\\"");
749 break;
750 default:
751 if (ISPRINT (c))
752 pp_character (pp, c);
753 else
754 pp_scalar (pp, "\\%03o", (unsigned) c);
755 break;
759 /* Print out a STRING literal. */
761 void
762 pp_c_string_literal (c_pretty_printer *pp, tree s)
764 const char *p = TREE_STRING_POINTER (s);
765 int n = TREE_STRING_LENGTH (s) - 1;
766 int i;
767 pp_doublequote (pp);
768 for (i = 0; i < n; ++i)
769 pp_c_char (pp, p[i]);
770 pp_doublequote (pp);
773 /* Pretty-print an INTEGER literal. */
775 static void
776 pp_c_integer_constant (c_pretty_printer *pp, tree i)
778 tree type = TREE_TYPE (i);
780 if (TREE_INT_CST_HIGH (i) == 0)
781 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
782 else
784 if (tree_int_cst_sgn (i) < 0)
786 pp_c_char (pp, '-');
787 i = build_int_2 (-TREE_INT_CST_LOW (i),
788 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
790 sprintf (pp_buffer (pp)->digit_buffer,
791 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
792 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
793 pp_string (pp, pp_buffer (pp)->digit_buffer);
795 if (TYPE_UNSIGNED (type))
796 pp_character (pp, 'u');
797 if (type == long_integer_type_node || type == long_unsigned_type_node)
798 pp_character (pp, 'l');
799 else if (type == long_long_integer_type_node
800 || type == long_long_unsigned_type_node)
801 pp_string (pp, "ll");
804 /* Print out a CHARACTER literal. */
806 static void
807 pp_c_character_constant (c_pretty_printer *pp, tree c)
809 tree type = TREE_TYPE (c);
810 if (type == wchar_type_node)
811 pp_character (pp, 'L');
812 pp_quote (pp);
813 if (host_integerp (c, TYPE_UNSIGNED (type)))
814 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
815 else
816 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
817 pp_quote (pp);
820 /* Print out a BOOLEAN literal. */
822 static void
823 pp_c_bool_constant (c_pretty_printer *pp, tree b)
825 if (b == boolean_false_node)
827 if (c_dialect_cxx ())
828 pp_c_identifier (pp, "false");
829 else if (flag_isoc99)
830 pp_c_identifier (pp, "_False");
831 else
832 pp_unsupported_tree (pp, b);
834 else if (b == boolean_true_node)
836 if (c_dialect_cxx ())
837 pp_c_identifier (pp, "true");
838 else if (flag_isoc99)
839 pp_c_identifier (pp, "_True");
840 else
841 pp_unsupported_tree (pp, b);
843 else if (TREE_CODE (b) == INTEGER_CST)
844 pp_c_integer_constant (pp, b);
845 else
846 pp_unsupported_tree (pp, b);
849 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
850 false; that means the value was obtained by a cast, in which case
851 print out the type-id part of the cast-expression -- the casted value
852 is then printed by pp_c_integer_literal. */
854 static bool
855 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
857 bool value_is_named = true;
858 tree type = TREE_TYPE (e);
859 tree value;
861 /* Find the name of this constant. */
862 for (value = TYPE_VALUES (type);
863 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
864 value = TREE_CHAIN (value))
867 if (value != NULL_TREE)
868 pp_id_expression (pp, TREE_PURPOSE (value));
869 else
871 /* Value must have been cast. */
872 pp_c_type_cast (pp, type);
873 value_is_named = false;
876 return value_is_named;
879 /* Print out a REAL value as a decimal-floating-constant. */
881 static void
882 pp_c_floating_constant (c_pretty_printer *pp, tree r)
884 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
885 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
886 pp_string (pp, pp_buffer(pp)->digit_buffer);
887 if (TREE_TYPE (r) == float_type_node)
888 pp_character (pp, 'f');
889 else if (TREE_TYPE (r) == long_double_type_node)
890 pp_character (pp, 'l');
893 /* Pretty-print a compound literal expression. GNU extensions include
894 vector constants. */
896 static void
897 pp_c_compound_literal (c_pretty_printer *pp, tree e)
899 tree type = TREE_TYPE (e);
900 pp_c_type_cast (pp, type);
902 switch (TREE_CODE (type))
904 case RECORD_TYPE:
905 case UNION_TYPE:
906 case ARRAY_TYPE:
907 case VECTOR_TYPE:
908 case COMPLEX_TYPE:
909 pp_c_brace_enclosed_initializer_list (pp, e);
910 break;
912 default:
913 pp_unsupported_tree (pp, e);
914 break;
918 /* constant:
919 integer-constant
920 floating-constant
921 enumeration-constant
922 character-constant */
924 void
925 pp_c_constant (c_pretty_printer *pp, tree e)
927 const enum tree_code code = TREE_CODE (e);
929 switch (code)
931 case INTEGER_CST:
933 tree type = TREE_TYPE (e);
934 if (type == boolean_type_node)
935 pp_c_bool_constant (pp, e);
936 else if (type == char_type_node)
937 pp_c_character_constant (pp, e);
938 else if (TREE_CODE (type) == ENUMERAL_TYPE
939 && pp_c_enumeration_constant (pp, e))
941 else
942 pp_c_integer_constant (pp, e);
944 break;
946 case REAL_CST:
947 pp_c_floating_constant (pp, e);
948 break;
950 case STRING_CST:
951 pp_c_string_literal (pp, e);
952 break;
954 default:
955 pp_unsupported_tree (pp, e);
956 break;
960 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
962 void
963 pp_c_identifier (c_pretty_printer *pp, const char *id)
965 pp_c_maybe_whitespace (pp);
966 pp_identifier (pp, id);
967 pp_base (pp)->padding = pp_before;
970 /* Pretty-print a C primary-expression.
971 primary-expression:
972 identifier
973 constant
974 string-literal
975 ( expression ) */
977 void
978 pp_c_primary_expression (c_pretty_printer *pp, tree e)
980 switch (TREE_CODE (e))
982 case VAR_DECL:
983 case PARM_DECL:
984 case FIELD_DECL:
985 case CONST_DECL:
986 case FUNCTION_DECL:
987 case LABEL_DECL:
988 pp_c_tree_decl_identifier (pp, e);
989 break;
991 case IDENTIFIER_NODE:
992 pp_c_tree_identifier (pp, e);
993 break;
995 case ERROR_MARK:
996 pp_c_identifier (pp, "<erroneous-expression>");
997 break;
999 case RESULT_DECL:
1000 pp_c_identifier (pp, "<return-value>");
1001 break;
1003 case INTEGER_CST:
1004 case REAL_CST:
1005 case STRING_CST:
1006 pp_c_constant (pp, e);
1007 break;
1009 case TARGET_EXPR:
1010 pp_c_identifier (pp, "__builtin_memcpy");
1011 pp_c_left_paren (pp);
1012 pp_ampersand (pp);
1013 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1014 pp_separate_with (pp, ',');
1015 pp_ampersand (pp);
1016 pp_initializer (pp, TREE_OPERAND (e, 1));
1017 if (TREE_OPERAND (e, 2))
1019 pp_separate_with (pp, ',');
1020 pp_c_expression (pp, TREE_OPERAND (e, 2));
1022 pp_c_right_paren (pp);
1023 break;
1025 case STMT_EXPR:
1026 pp_c_left_paren (pp);
1027 pp_statement (pp, STMT_EXPR_STMT (e));
1028 pp_c_right_paren (pp);
1029 break;
1031 default:
1032 /* FIXME: Make sure we won't get into an infinie loop. */
1033 pp_c_left_paren (pp);
1034 pp_expression (pp, e);
1035 pp_c_right_paren (pp);
1036 break;
1040 /* Print out a C initializer -- also support C compound-literals.
1041 initializer:
1042 assignment-expression:
1043 { initializer-list }
1044 { initializer-list , } */
1046 static void
1047 pp_c_initializer (c_pretty_printer *pp, tree e)
1049 if (TREE_CODE (e) == CONSTRUCTOR)
1050 pp_c_brace_enclosed_initializer_list (pp, e);
1051 else
1052 pp_expression (pp, e);
1055 /* init-declarator:
1056 declarator:
1057 declarator = initializer */
1059 void
1060 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1062 pp_declarator (pp, t);
1063 /* We don't want to output function definitions here. There are handled
1064 elsewhere (and the syntactic form is bogus anyway). */
1065 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1067 tree init = DECL_INITIAL (t);
1068 /* This C++ bit is handled here because it is easier to do so.
1069 In templates, the C++ parser builds a TREE_LIST for a
1070 direct-initialization; the TREE_PURPOSE is the variable to
1071 initialize and the TREE_VALUE is the initializer. */
1072 if (TREE_CODE (init) == TREE_LIST)
1074 pp_c_left_paren (pp);
1075 pp_expression (pp, TREE_VALUE (init));
1076 pp_right_paren (pp);
1078 else
1080 pp_space (pp);
1081 pp_equal (pp);
1082 pp_space (pp);
1083 pp_c_initializer (pp, init);
1088 /* initializer-list:
1089 designation(opt) initializer
1090 initializer-list , designation(opt) initializer
1092 designation:
1093 designator-list =
1095 designator-list:
1096 designator
1097 designator-list designator
1099 designator:
1100 [ constant-expression ]
1101 identifier */
1103 static void
1104 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1106 tree type = TREE_TYPE (e);
1107 const enum tree_code code = TREE_CODE (type);
1109 switch (code)
1111 case RECORD_TYPE:
1112 case UNION_TYPE:
1113 case ARRAY_TYPE:
1115 tree init = TREE_OPERAND (e, 0);
1116 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1118 if (code == RECORD_TYPE || code == UNION_TYPE)
1120 pp_c_dot (pp);
1121 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1123 else
1125 pp_c_left_bracket (pp);
1126 if (TREE_PURPOSE (init))
1127 pp_c_constant (pp, TREE_PURPOSE (init));
1128 pp_c_right_bracket (pp);
1130 pp_c_whitespace (pp);
1131 pp_equal (pp);
1132 pp_c_whitespace (pp);
1133 pp_initializer (pp, TREE_VALUE (init));
1134 if (TREE_CHAIN (init))
1135 pp_separate_with (pp, ',');
1138 return;
1140 case VECTOR_TYPE:
1141 if (TREE_CODE (e) == VECTOR_CST)
1142 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1143 else if (TREE_CODE (e) == CONSTRUCTOR)
1144 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1145 else
1146 break;
1147 return;
1149 case COMPLEX_TYPE:
1150 if (TREE_CODE (e) == CONSTRUCTOR)
1151 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1152 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1154 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1155 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1156 pp_separate_with (pp, ',');
1157 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1159 else
1160 break;
1161 return;
1163 default:
1164 break;
1167 pp_unsupported_tree (pp, type);
1170 /* Pretty-print a brace-enclosed initializer-list. */
1172 static void
1173 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1175 pp_c_left_brace (pp);
1176 pp_c_initializer_list (pp, l);
1177 pp_c_right_brace (pp);
1181 /* This is a convenient function, used to bridge gap between C and C++
1182 grammars.
1184 id-expression:
1185 identifier */
1187 void
1188 pp_c_id_expression (c_pretty_printer *pp, tree t)
1190 switch (TREE_CODE (t))
1192 case VAR_DECL:
1193 case PARM_DECL:
1194 case CONST_DECL:
1195 case TYPE_DECL:
1196 case FUNCTION_DECL:
1197 case FIELD_DECL:
1198 case LABEL_DECL:
1199 pp_c_tree_decl_identifier (pp, t);
1200 break;
1202 case IDENTIFIER_NODE:
1203 pp_c_tree_identifier (pp, t);
1204 break;
1206 default:
1207 pp_unsupported_tree (pp, t);
1208 break;
1212 /* postfix-expression:
1213 primary-expression
1214 postfix-expression [ expression ]
1215 postfix-expression ( argument-expression-list(opt) )
1216 postfix-expression . identifier
1217 postfix-expression -> identifier
1218 postfix-expression ++
1219 postfix-expression --
1220 ( type-name ) { initializer-list }
1221 ( type-name ) { initializer-list , } */
1223 void
1224 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1226 enum tree_code code = TREE_CODE (e);
1227 switch (code)
1229 case POSTINCREMENT_EXPR:
1230 case POSTDECREMENT_EXPR:
1231 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1232 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1233 break;
1235 case ARROW_EXPR:
1236 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1237 pp_c_arrow (pp);
1238 break;
1240 case ARRAY_REF:
1241 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1242 pp_c_left_bracket (pp);
1243 pp_expression (pp, TREE_OPERAND (e, 1));
1244 pp_c_right_bracket (pp);
1245 break;
1247 case CALL_EXPR:
1248 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1249 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1250 break;
1252 case ABS_EXPR:
1253 pp_c_identifier (pp, "__builtin_abs");
1254 pp_c_left_paren (pp);
1255 pp_expression (pp, TREE_OPERAND (e, 0));
1256 pp_c_right_paren (pp);
1257 break;
1259 case COMPONENT_REF:
1261 tree object = TREE_OPERAND (e, 0);
1262 if (TREE_CODE (object) == INDIRECT_REF)
1264 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1265 pp_c_arrow (pp);
1267 else
1269 pp_postfix_expression (pp, object);
1270 pp_c_dot (pp);
1272 pp_expression (pp, TREE_OPERAND (e, 1));
1274 break;
1276 case COMPLEX_CST:
1277 case VECTOR_CST:
1278 case COMPLEX_EXPR:
1279 pp_c_compound_literal (pp, e);
1280 break;
1282 case COMPOUND_LITERAL_EXPR:
1283 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1284 /* Fall through. */
1285 case CONSTRUCTOR:
1286 pp_initializer (pp, e);
1287 break;
1289 case VA_ARG_EXPR:
1290 pp_c_identifier (pp, "__builtin_va_arg");
1291 pp_c_left_paren (pp);
1292 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1293 pp_separate_with (pp, ',');
1294 pp_type_id (pp, TREE_TYPE (e));
1295 pp_c_right_paren (pp);
1296 break;
1298 case ADDR_EXPR:
1299 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1301 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1302 break;
1304 /* else fall through. */
1306 default:
1307 pp_primary_expression (pp, e);
1308 break;
1312 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1314 void
1315 pp_c_expression_list (c_pretty_printer *pp, tree e)
1317 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1319 pp_expression (pp, TREE_VALUE (e));
1320 if (TREE_CHAIN (e))
1321 pp_separate_with (pp, ',');
1325 /* Print out an expression-list in parens, as in a function call. */
1327 void
1328 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1330 pp_c_left_paren (pp);
1331 if (t && TREE_CODE (t) == TREE_LIST)
1332 pp_c_expression_list (pp, t);
1333 pp_c_right_paren (pp);
1336 /* unary-expression:
1337 postfix-expression
1338 ++ cast-expression
1339 -- cast-expression
1340 unary-operator cast-expression
1341 sizeof unary-expression
1342 sizeof ( type-id )
1344 unary-operator: one of
1345 * & + - ! ~
1347 GNU extensions.
1348 unary-expression:
1349 __alignof__ unary-expression
1350 __alignof__ ( type-id )
1351 __real__ unary-expression
1352 __imag__ unary-expression */
1354 void
1355 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1357 enum tree_code code = TREE_CODE (e);
1358 switch (code)
1360 case PREINCREMENT_EXPR:
1361 case PREDECREMENT_EXPR:
1362 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1363 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1364 break;
1366 case ADDR_EXPR:
1367 case INDIRECT_REF:
1368 case NEGATE_EXPR:
1369 case BIT_NOT_EXPR:
1370 case TRUTH_NOT_EXPR:
1371 case CONJ_EXPR:
1372 /* String literal are used by address. */
1373 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1374 pp_ampersand (pp);
1375 else if (code == INDIRECT_REF)
1376 pp_c_star (pp);
1377 else if (code == NEGATE_EXPR)
1378 pp_minus (pp);
1379 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1380 pp_complement (pp);
1381 else if (code == TRUTH_NOT_EXPR)
1382 pp_exclamation (pp);
1383 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1384 break;
1386 case SIZEOF_EXPR:
1387 case ALIGNOF_EXPR:
1388 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1389 pp_c_whitespace (pp);
1390 if (TYPE_P (TREE_OPERAND (e, 0)))
1391 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1392 else
1393 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1394 break;
1396 case REALPART_EXPR:
1397 case IMAGPART_EXPR:
1398 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1399 pp_c_whitespace (pp);
1400 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1401 break;
1403 default:
1404 pp_postfix_expression (pp, e);
1405 break;
1409 /* cast-expression:
1410 unary-expression
1411 ( type-name ) cast-expression */
1413 void
1414 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1416 switch (TREE_CODE (e))
1418 case FLOAT_EXPR:
1419 case FIX_TRUNC_EXPR:
1420 case CONVERT_EXPR:
1421 pp_c_type_cast (pp, TREE_TYPE (e));
1422 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1423 break;
1425 default:
1426 pp_unary_expression (pp, e);
1430 /* multiplicative-expression:
1431 cast-expression
1432 multiplicative-expression * cast-expression
1433 multiplicative-expression / cast-expression
1434 multiplicative-expression % cast-expression */
1436 static void
1437 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1439 enum tree_code code = TREE_CODE (e);
1440 switch (code)
1442 case MULT_EXPR:
1443 case TRUNC_DIV_EXPR:
1444 case TRUNC_MOD_EXPR:
1445 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1446 pp_c_whitespace (pp);
1447 if (code == MULT_EXPR)
1448 pp_c_star (pp);
1449 else if (code == TRUNC_DIV_EXPR)
1450 pp_slash (pp);
1451 else
1452 pp_modulo (pp);
1453 pp_c_whitespace (pp);
1454 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1455 break;
1457 default:
1458 pp_c_cast_expression (pp, e);
1459 break;
1463 /* additive-expression:
1464 multiplicative-expression
1465 additive-expression + multiplicative-expression
1466 additive-expression - multiplicative-expression */
1468 static void
1469 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1471 enum tree_code code = TREE_CODE (e);
1472 switch (code)
1474 case PLUS_EXPR:
1475 case MINUS_EXPR:
1476 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1477 pp_c_whitespace (pp);
1478 if (code == PLUS_EXPR)
1479 pp_plus (pp);
1480 else
1481 pp_minus (pp);
1482 pp_c_whitespace (pp);
1483 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1484 break;
1486 default:
1487 pp_multiplicative_expression (pp, e);
1488 break;
1492 /* additive-expression:
1493 additive-expression
1494 shift-expression << additive-expression
1495 shift-expression >> additive-expression */
1497 static void
1498 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1500 enum tree_code code = TREE_CODE (e);
1501 switch (code)
1503 case LSHIFT_EXPR:
1504 case RSHIFT_EXPR:
1505 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1506 pp_c_whitespace (pp);
1507 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1508 pp_c_whitespace (pp);
1509 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1510 break;
1512 default:
1513 pp_c_additive_expression (pp, e);
1517 /* relational-expression:
1518 shift-expression
1519 relational-expression < shift-expression
1520 relational-expression > shift-expression
1521 relational-expression <= shift-expression
1522 relational-expression >= shift-expression */
1524 static void
1525 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1527 enum tree_code code = TREE_CODE (e);
1528 switch (code)
1530 case LT_EXPR:
1531 case GT_EXPR:
1532 case LE_EXPR:
1533 case GE_EXPR:
1534 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1535 pp_c_whitespace (pp);
1536 if (code == LT_EXPR)
1537 pp_less (pp);
1538 else if (code == GT_EXPR)
1539 pp_greater (pp);
1540 else if (code == LE_EXPR)
1541 pp_identifier (pp, "<=");
1542 else if (code == GE_EXPR)
1543 pp_identifier (pp, ">=");
1544 pp_c_whitespace (pp);
1545 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1546 break;
1548 default:
1549 pp_c_shift_expression (pp, e);
1550 break;
1554 /* equality-expression:
1555 relational-expression
1556 equality-expression == relational-expression
1557 equality-equality != relational-expression */
1559 static void
1560 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1562 enum tree_code code = TREE_CODE (e);
1563 switch (code)
1565 case EQ_EXPR:
1566 case NE_EXPR:
1567 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1568 pp_c_whitespace (pp);
1569 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1570 pp_c_whitespace (pp);
1571 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1572 break;
1574 default:
1575 pp_c_relational_expression (pp, e);
1576 break;
1580 /* AND-expression:
1581 equality-expression
1582 AND-expression & equality-equality */
1584 static void
1585 pp_c_and_expression (c_pretty_printer *pp, tree e)
1587 if (TREE_CODE (e) == BIT_AND_EXPR)
1589 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1590 pp_c_whitespace (pp);
1591 pp_ampersand (pp);
1592 pp_c_whitespace (pp);
1593 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1595 else
1596 pp_c_equality_expression (pp, e);
1599 /* exclusive-OR-expression:
1600 AND-expression
1601 exclusive-OR-expression ^ AND-expression */
1603 static void
1604 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1606 if (TREE_CODE (e) == BIT_XOR_EXPR)
1608 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1609 pp_c_maybe_whitespace (pp);
1610 pp_carret (pp);
1611 pp_c_whitespace (pp);
1612 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1614 else
1615 pp_c_and_expression (pp, e);
1618 /* inclusive-OR-expression:
1619 exclusive-OR-expression
1620 inclusive-OR-expression | exclusive-OR-expression */
1622 static void
1623 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1625 if (TREE_CODE (e) == BIT_IOR_EXPR)
1627 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1628 pp_c_whitespace (pp);
1629 pp_bar (pp);
1630 pp_c_whitespace (pp);
1631 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1633 else
1634 pp_c_exclusive_or_expression (pp, e);
1637 /* logical-AND-expression:
1638 inclusive-OR-expression
1639 logical-AND-expression && inclusive-OR-expression */
1641 static void
1642 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1644 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1646 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1647 pp_c_whitespace (pp);
1648 pp_identifier (pp, "&&");
1649 pp_c_whitespace (pp);
1650 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1652 else
1653 pp_c_inclusive_or_expression (pp, e);
1656 /* logical-OR-expression:
1657 logical-AND-expression
1658 logical-OR-expression || logical-AND-expression */
1660 void
1661 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1663 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1665 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1666 pp_c_whitespace (pp);
1667 pp_identifier (pp, "||");
1668 pp_c_whitespace (pp);
1669 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1671 else
1672 pp_c_logical_and_expression (pp, e);
1675 /* conditional-expression:
1676 logical-OR-expression
1677 logical-OR-expression ? expression : conditional-expression */
1679 static void
1680 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1682 if (TREE_CODE (e) == COND_EXPR)
1684 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1685 pp_c_whitespace (pp);
1686 pp_question (pp);
1687 pp_c_whitespace (pp);
1688 pp_expression (pp, TREE_OPERAND (e, 1));
1689 pp_c_whitespace (pp);
1690 pp_colon (pp);
1691 pp_c_whitespace (pp);
1692 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1694 else
1695 pp_c_logical_or_expression (pp, e);
1699 /* assignment-expression:
1700 conditional-expression
1701 unary-expression assignment-operator assignment-expression
1703 assignment-expression: one of
1704 = *= /= %= += -= >>= <<= &= ^= |= */
1706 static void
1707 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1709 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1711 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1712 pp_c_whitespace (pp);
1713 pp_equal (pp);
1714 pp_space (pp);
1715 pp_c_expression (pp, TREE_OPERAND (e, 1));
1717 else
1718 pp_c_conditional_expression (pp, e);
1721 /* expression:
1722 assignment-expression
1723 expression , assignment-expression
1725 Implementation note: instead of going through the usual recursion
1726 chain, I take the liberty of dispatching nodes to the appropriate
1727 functions. This makes some redundancy, but it worths it. That also
1728 prevents a possible infinite recursion between pp_c_primary_expression ()
1729 and pp_c_expression (). */
1731 void
1732 pp_c_expression (c_pretty_printer *pp, tree e)
1734 switch (TREE_CODE (e))
1736 case INTEGER_CST:
1737 pp_c_integer_constant (pp, e);
1738 break;
1740 case REAL_CST:
1741 pp_c_floating_constant (pp, e);
1742 break;
1744 case STRING_CST:
1745 pp_c_string_literal (pp, e);
1746 break;
1748 case IDENTIFIER_NODE:
1749 case FUNCTION_DECL:
1750 case VAR_DECL:
1751 case CONST_DECL:
1752 case PARM_DECL:
1753 case RESULT_DECL:
1754 case FIELD_DECL:
1755 case LABEL_DECL:
1756 case ERROR_MARK:
1757 case STMT_EXPR:
1758 pp_primary_expression (pp, e);
1759 break;
1761 case POSTINCREMENT_EXPR:
1762 case POSTDECREMENT_EXPR:
1763 case ARROW_EXPR:
1764 case ARRAY_REF:
1765 case CALL_EXPR:
1766 case COMPONENT_REF:
1767 case COMPLEX_CST:
1768 case COMPLEX_EXPR:
1769 case VECTOR_CST:
1770 case ABS_EXPR:
1771 case CONSTRUCTOR:
1772 case COMPOUND_LITERAL_EXPR:
1773 case VA_ARG_EXPR:
1774 pp_postfix_expression (pp, e);
1775 break;
1777 case CONJ_EXPR:
1778 case ADDR_EXPR:
1779 case INDIRECT_REF:
1780 case NEGATE_EXPR:
1781 case BIT_NOT_EXPR:
1782 case TRUTH_NOT_EXPR:
1783 case PREINCREMENT_EXPR:
1784 case PREDECREMENT_EXPR:
1785 case SIZEOF_EXPR:
1786 case ALIGNOF_EXPR:
1787 case REALPART_EXPR:
1788 case IMAGPART_EXPR:
1789 pp_c_unary_expression (pp, e);
1790 break;
1792 case FLOAT_EXPR:
1793 case FIX_TRUNC_EXPR:
1794 case CONVERT_EXPR:
1795 pp_c_cast_expression (pp, e);
1796 break;
1798 case MULT_EXPR:
1799 case TRUNC_MOD_EXPR:
1800 case TRUNC_DIV_EXPR:
1801 pp_multiplicative_expression (pp, e);
1802 break;
1804 case LSHIFT_EXPR:
1805 case RSHIFT_EXPR:
1806 pp_c_shift_expression (pp, e);
1807 break;
1809 case LT_EXPR:
1810 case GT_EXPR:
1811 case LE_EXPR:
1812 case GE_EXPR:
1813 pp_c_relational_expression (pp, e);
1814 break;
1816 case BIT_AND_EXPR:
1817 pp_c_and_expression (pp, e);
1818 break;
1820 case BIT_XOR_EXPR:
1821 pp_c_exclusive_or_expression (pp, e);
1822 break;
1824 case BIT_IOR_EXPR:
1825 pp_c_inclusive_or_expression (pp, e);
1826 break;
1828 case TRUTH_ANDIF_EXPR:
1829 pp_c_logical_and_expression (pp, e);
1830 break;
1832 case TRUTH_ORIF_EXPR:
1833 pp_c_logical_or_expression (pp, e);
1834 break;
1836 case EQ_EXPR:
1837 case NE_EXPR:
1838 pp_c_equality_expression (pp, e);
1839 break;
1841 case COND_EXPR:
1842 pp_conditional_expression (pp, e);
1843 break;
1845 case PLUS_EXPR:
1846 case MINUS_EXPR:
1847 pp_c_additive_expression (pp, e);
1848 break;
1850 case MODIFY_EXPR:
1851 case INIT_EXPR:
1852 pp_assignment_expression (pp, e);
1853 break;
1855 case COMPOUND_EXPR:
1856 pp_c_left_paren (pp);
1857 pp_expression (pp, TREE_OPERAND (e, 0));
1858 pp_separate_with (pp, ',');
1859 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1860 pp_c_right_paren (pp);
1861 break;
1863 case NOP_EXPR:
1864 case NON_LVALUE_EXPR:
1865 case SAVE_EXPR:
1866 case UNSAVE_EXPR:
1867 pp_expression (pp, TREE_OPERAND (e, 0));
1868 break;
1870 case TARGET_EXPR:
1871 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1872 break;
1874 default:
1875 pp_unsupported_tree (pp, e);
1876 break;
1882 /* Statements. */
1884 /* statement:
1885 labeled-statement
1886 compound-statement
1887 expression-statement
1888 selection-statement
1889 iteration-statement
1890 jump-statement */
1892 void
1893 pp_c_statement (c_pretty_printer *pp, tree stmt)
1895 enum tree_code code;
1897 if (stmt == NULL)
1898 return;
1900 if (pp_needs_newline (pp))
1901 pp_newline_and_indent (pp, 0);
1903 code = TREE_CODE (stmt);
1904 switch (code)
1906 /* expression-statement:
1907 expression(opt) ; */
1908 case EXPR_STMT:
1909 pp_expression (pp, EXPR_STMT_EXPR (stmt));
1910 pp_c_semicolon (pp);
1911 pp_needs_newline (pp) = true;
1912 break;
1914 case SWITCH_STMT:
1915 pp_c_identifier (pp, "switch");
1916 pp_space (pp);
1917 pp_c_left_paren (pp);
1918 pp_expression (pp, SWITCH_COND (stmt));
1919 pp_c_right_paren (pp);
1920 pp_indentation (pp) += 3;
1921 pp_needs_newline (pp) = true;
1922 pp_statement (pp, SWITCH_BODY (stmt));
1923 pp_newline_and_indent (pp, -3);
1924 break;
1926 /* iteration-statement:
1927 while ( expression ) statement
1928 do statement while ( expression ) ;
1929 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1930 for ( declaration expression(opt) ; expression(opt) ) statement */
1931 case WHILE_STMT:
1932 pp_c_identifier (pp, "while");
1933 pp_space (pp);
1934 pp_c_left_paren (pp);
1935 pp_expression (pp, WHILE_COND (stmt));
1936 pp_c_right_paren (pp);
1937 pp_newline_and_indent (pp, 3);
1938 pp_statement (pp, WHILE_BODY (stmt));
1939 pp_indentation (pp) -= 3;
1940 pp_needs_newline (pp) = true;
1941 break;
1943 case DO_STMT:
1944 pp_c_identifier (pp, "do");
1945 pp_newline_and_indent (pp, 3);
1946 pp_statement (pp, DO_BODY (stmt));
1947 pp_newline_and_indent (pp, -3);
1948 pp_c_identifier (pp, "while");
1949 pp_space (pp);
1950 pp_c_left_paren (pp);
1951 pp_expression (pp, DO_COND (stmt));
1952 pp_c_right_paren (pp);
1953 pp_c_semicolon (pp);
1954 pp_needs_newline (pp) = true;
1955 break;
1957 case FOR_STMT:
1958 pp_c_identifier (pp, "for");
1959 pp_space (pp);
1960 pp_c_left_paren (pp);
1961 if (FOR_INIT_STMT (stmt))
1962 pp_statement (pp, FOR_INIT_STMT (stmt));
1963 else
1964 pp_c_semicolon (pp);
1965 pp_needs_newline (pp) = false;
1966 pp_c_whitespace (pp);
1967 if (FOR_COND (stmt))
1968 pp_expression (pp, FOR_COND (stmt));
1969 pp_c_semicolon (pp);
1970 pp_needs_newline (pp) = false;
1971 pp_c_whitespace (pp);
1972 if (FOR_EXPR (stmt))
1973 pp_expression (pp, FOR_EXPR (stmt));
1974 pp_c_right_paren (pp);
1975 pp_newline_and_indent (pp, 3);
1976 pp_statement (pp, FOR_BODY (stmt));
1977 pp_indentation (pp) -= 3;
1978 pp_needs_newline (pp) = true;
1979 break;
1981 /* jump-statement:
1982 goto identifier;
1983 continue ;
1984 return expression(opt) ; */
1985 case BREAK_STMT:
1986 case CONTINUE_STMT:
1987 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
1988 pp_c_semicolon (pp);
1989 pp_needs_newline (pp) = true;
1990 break;
1992 default:
1993 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1994 break;
1999 /* Initialize the PRETTY-PRINTER for handling C codes. */
2001 void
2002 pp_c_pretty_printer_init (c_pretty_printer *pp)
2004 pp->offset_list = 0;
2006 pp->declaration = pp_c_declaration;
2007 pp->declaration_specifiers = pp_c_declaration_specifiers;
2008 pp->declarator = pp_c_declarator;
2009 pp->direct_declarator = pp_c_direct_declarator;
2010 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2011 pp->abstract_declarator = pp_c_abstract_declarator;
2012 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2013 pp->ptr_operator = pp_c_pointer;
2014 pp->parameter_list = pp_c_parameter_type_list;
2015 pp->type_id = pp_c_type_id;
2016 pp->simple_type_specifier = pp_c_type_specifier;
2017 pp->function_specifier = pp_c_function_specifier;
2018 pp->storage_class_specifier = pp_c_storage_class_specifier;
2020 pp->statement = pp_c_statement;
2022 pp->id_expression = pp_c_id_expression;
2023 pp->primary_expression = pp_c_primary_expression;
2024 pp->postfix_expression = pp_c_postfix_expression;
2025 pp->unary_expression = pp_c_unary_expression;
2026 pp->initializer = pp_c_initializer;
2027 pp->multiplicative_expression = pp_c_multiplicative_expression;
2028 pp->conditional_expression = pp_c_conditional_expression;
2029 pp->assignment_expression = pp_c_assignment_expression;
2030 pp->expression = pp_c_expression;
2034 /* Print the tree T in full, on file FILE. */
2036 void
2037 print_c_tree (FILE *file, tree t)
2039 static c_pretty_printer pp_rec;
2040 static bool initialized = 0;
2041 c_pretty_printer *pp = &pp_rec;
2043 if (!initialized)
2045 initialized = 1;
2046 pp_construct (pp_base (pp), NULL, 0);
2047 pp_c_pretty_printer_init (pp);
2048 pp_needs_newline (pp) = true;
2050 pp_base (pp)->buffer->stream = file;
2052 pp_statement (pp, t);
2054 pp_newline (pp);
2055 pp_flush (pp);
2058 /* Print the tree T in full, on stderr. */
2060 void
2061 debug_c_tree (tree t)
2063 print_c_tree (stderr, t);
2064 fputc ('\n', stderr);
2067 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2068 up of T's memory address. */
2070 void
2071 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2073 const char *name;
2075 if (!DECL_P (t))
2076 abort ();
2078 if (DECL_NAME (t))
2079 name = IDENTIFIER_POINTER (DECL_NAME (t));
2080 else
2082 static char xname[8];
2083 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2084 name = xname;
2087 pp_c_identifier (pp, name);