* tree-ssa-operands.c (get_call_expr_operands): Add VUSE operands for
[official-gcc.git] / gcc / c-pretty-print.c
blobdbb56ed4503ce3107ada9412486cc0849d897865
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 else if (!c_dialect_cxx ())
388 pp_c_whitespace (pp);
389 pp_ptr_operator (pp, t);
391 break;
393 case FUNCTION_TYPE:
394 case ARRAY_TYPE:
395 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
396 break;
398 case VECTOR_TYPE:
399 case COMPLEX_TYPE:
400 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
401 if (code == COMPLEX_TYPE)
402 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
403 else if (code == VECTOR_TYPE)
404 pp_c_identifier (pp, "__vector__");
405 break;
407 default:
408 pp_simple_type_specifier (pp, t);
409 break;
413 /* parameter-type-list:
414 parameter-list
415 parameter-list , ...
417 parameter-list:
418 parameter-declaration
419 parameter-list , parameter-declaration
421 parameter-declaration:
422 declaration-specifiers declarator
423 declaration-specifiers abstract-declarator(opt) */
425 void
426 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
428 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
429 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
430 pp_c_left_paren (pp);
431 if (parms == void_list_node)
432 pp_c_identifier (pp, "void");
433 else
435 bool first = true;
436 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
438 if (!first)
439 pp_separate_with (pp, ',');
440 first = false;
441 pp_declaration_specifiers
442 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
443 if (want_parm_decl)
444 pp_declarator (pp, parms);
445 else
446 pp_abstract_declarator (pp, TREE_VALUE (parms));
449 pp_c_right_paren (pp);
452 /* abstract-declarator:
453 pointer
454 pointer(opt) direct-abstract-declarator */
456 static void
457 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
459 if (TREE_CODE (t) == POINTER_TYPE)
461 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
462 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
463 pp_c_right_paren (pp);
464 t = TREE_TYPE (t);
467 pp_direct_abstract_declarator (pp, t);
470 /* direct-abstract-declarator:
471 ( abstract-declarator )
472 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
473 direct-abstract-declarator(opt) [ * ]
474 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
476 void
477 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
479 switch (TREE_CODE (t))
481 case POINTER_TYPE:
482 pp_abstract_declarator (pp, t);
483 break;
485 case FUNCTION_TYPE:
486 pp_c_parameter_type_list (pp, t);
487 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
488 break;
490 case ARRAY_TYPE:
491 pp_c_left_bracket (pp);
492 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
493 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
494 pp_c_right_bracket (pp);
495 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
496 break;
498 case IDENTIFIER_NODE:
499 case VOID_TYPE:
500 case BOOLEAN_TYPE:
501 case INTEGER_TYPE:
502 case REAL_TYPE:
503 case ENUMERAL_TYPE:
504 case RECORD_TYPE:
505 case UNION_TYPE:
506 case VECTOR_TYPE:
507 case COMPLEX_TYPE:
508 case TYPE_DECL:
509 break;
511 default:
512 pp_unsupported_tree (pp, t);
513 break;
517 /* type-name:
518 specifier-qualifier-list abstract-declarator(opt) */
520 void
521 pp_c_type_id (c_pretty_printer *pp, tree t)
523 pp_c_specifier_qualifier_list (pp, t);
524 pp_abstract_declarator (pp, t);
527 /* storage-class-specifier:
528 typedef
529 extern
530 static
531 auto
532 register */
534 void
535 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
537 if (TREE_CODE (t) == TYPE_DECL)
538 pp_c_identifier (pp, "typedef");
539 else if (DECL_P (t))
541 if (DECL_REGISTER (t))
542 pp_c_identifier (pp, "register");
543 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
544 pp_c_identifier (pp, "static");
548 /* function-specifier:
549 inline */
551 void
552 pp_c_function_specifier (c_pretty_printer *pp, tree t)
554 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
555 pp_c_identifier (pp, "inline");
558 /* declaration-specifiers:
559 storage-class-specifier declaration-specifiers(opt)
560 type-specifier declaration-specifiers(opt)
561 type-qualifier declaration-specifiers(opt)
562 function-specifier declaration-specifiers(opt) */
564 void
565 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
567 pp_storage_class_specifier (pp, t);
568 pp_function_specifier (pp, t);
569 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
572 /* direct-declarator
573 identifier
574 ( declarator )
575 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
576 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
577 direct-declarator [ type-qualifier-list static assignment-expression ]
578 direct-declarator [ type-qualifier-list * ]
579 direct-declarator ( parameter-type-list )
580 direct-declarator ( identifier-list(opt) ) */
582 void
583 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
585 switch (TREE_CODE (t))
587 case VAR_DECL:
588 case PARM_DECL:
589 case TYPE_DECL:
590 case FIELD_DECL:
591 case LABEL_DECL:
592 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
593 pp_c_tree_decl_identifier (pp, t);
594 break;
596 case ARRAY_TYPE:
597 case POINTER_TYPE:
598 pp_abstract_declarator (pp, TREE_TYPE (t));
599 break;
601 case FUNCTION_TYPE:
602 pp_parameter_list (pp, t);
603 pp_abstract_declarator (pp, TREE_TYPE (t));
604 break;
606 case FUNCTION_DECL:
607 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
608 pp_c_tree_decl_identifier (pp, t);
609 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
610 pp_abstract_declarator (pp, TREE_TYPE (t));
611 else
613 pp_parameter_list (pp, t);
614 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
616 break;
618 case INTEGER_TYPE:
619 case REAL_TYPE:
620 case ENUMERAL_TYPE:
621 case UNION_TYPE:
622 case RECORD_TYPE:
623 break;
625 default:
626 pp_unsupported_tree (pp, t);
627 break;
632 /* declarator:
633 pointer(opt) direct-declarator */
635 void
636 pp_c_declarator (c_pretty_printer *pp, tree t)
638 switch (TREE_CODE (t))
640 case INTEGER_TYPE:
641 case REAL_TYPE:
642 case ENUMERAL_TYPE:
643 case UNION_TYPE:
644 case RECORD_TYPE:
645 break;
647 case VAR_DECL:
648 case PARM_DECL:
649 case FIELD_DECL:
650 case ARRAY_TYPE:
651 case FUNCTION_TYPE:
652 case FUNCTION_DECL:
653 case TYPE_DECL:
654 pp_direct_declarator (pp, t);
655 break;
658 default:
659 pp_unsupported_tree (pp, t);
660 break;
664 /* declaration:
665 declaration-specifiers init-declarator-list(opt) ; */
667 void
668 pp_c_declaration (c_pretty_printer *pp, tree t)
670 pp_declaration_specifiers (pp, t);
671 pp_c_init_declarator (pp, t);
674 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
676 void
677 pp_c_attributes (c_pretty_printer *pp, tree attributes)
679 if (attributes == NULL_TREE)
680 return;
682 pp_c_identifier (pp, "__attribute__");
683 pp_c_left_paren (pp);
684 pp_c_left_paren (pp);
685 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
687 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
688 if (TREE_VALUE (attributes))
689 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
691 if (TREE_CHAIN (attributes))
692 pp_separate_with (pp, ',');
694 pp_c_right_paren (pp);
695 pp_c_right_paren (pp);
698 /* function-definition:
699 declaration-specifiers declarator compound-statement */
701 void
702 pp_c_function_definition (c_pretty_printer *pp, tree t)
704 pp_declaration_specifiers (pp, t);
705 pp_declarator (pp, t);
706 pp_needs_newline (pp) = true;
707 pp_statement (pp, DECL_SAVED_TREE (t));
708 pp_newline (pp);
709 pp_flush (pp);
713 /* Expressions. */
715 /* Print out a c-char. */
717 static void
718 pp_c_char (c_pretty_printer *pp, int c)
720 switch (c)
722 case TARGET_NEWLINE:
723 pp_string (pp, "\\n");
724 break;
725 case TARGET_TAB:
726 pp_string (pp, "\\t");
727 break;
728 case TARGET_VT:
729 pp_string (pp, "\\v");
730 break;
731 case TARGET_BS:
732 pp_string (pp, "\\b");
733 break;
734 case TARGET_CR:
735 pp_string (pp, "\\r");
736 break;
737 case TARGET_FF:
738 pp_string (pp, "\\f");
739 break;
740 case TARGET_BELL:
741 pp_string (pp, "\\a");
742 break;
743 case '\\':
744 pp_string (pp, "\\\\");
745 break;
746 case '\'':
747 pp_string (pp, "\\'");
748 break;
749 case '\"':
750 pp_string (pp, "\\\"");
751 break;
752 default:
753 if (ISPRINT (c))
754 pp_character (pp, c);
755 else
756 pp_scalar (pp, "\\%03o", (unsigned) c);
757 break;
761 /* Print out a STRING literal. */
763 void
764 pp_c_string_literal (c_pretty_printer *pp, tree s)
766 const char *p = TREE_STRING_POINTER (s);
767 int n = TREE_STRING_LENGTH (s) - 1;
768 int i;
769 pp_doublequote (pp);
770 for (i = 0; i < n; ++i)
771 pp_c_char (pp, p[i]);
772 pp_doublequote (pp);
775 /* Pretty-print an INTEGER literal. */
777 static void
778 pp_c_integer_constant (c_pretty_printer *pp, tree i)
780 tree type = TREE_TYPE (i);
782 if (TREE_INT_CST_HIGH (i) == 0)
783 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
784 else
786 if (tree_int_cst_sgn (i) < 0)
788 pp_c_char (pp, '-');
789 i = build_int_cst (NULL_TREE,
790 -TREE_INT_CST_LOW (i),
791 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
793 sprintf (pp_buffer (pp)->digit_buffer,
794 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
795 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
796 pp_string (pp, pp_buffer (pp)->digit_buffer);
798 if (TYPE_UNSIGNED (type))
799 pp_character (pp, 'u');
800 if (type == long_integer_type_node || type == long_unsigned_type_node)
801 pp_character (pp, 'l');
802 else if (type == long_long_integer_type_node
803 || type == long_long_unsigned_type_node)
804 pp_string (pp, "ll");
807 /* Print out a CHARACTER literal. */
809 static void
810 pp_c_character_constant (c_pretty_printer *pp, tree c)
812 tree type = TREE_TYPE (c);
813 if (type == wchar_type_node)
814 pp_character (pp, 'L');
815 pp_quote (pp);
816 if (host_integerp (c, TYPE_UNSIGNED (type)))
817 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
818 else
819 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
820 pp_quote (pp);
823 /* Print out a BOOLEAN literal. */
825 static void
826 pp_c_bool_constant (c_pretty_printer *pp, tree b)
828 if (b == boolean_false_node)
830 if (c_dialect_cxx ())
831 pp_c_identifier (pp, "false");
832 else if (flag_isoc99)
833 pp_c_identifier (pp, "_False");
834 else
835 pp_unsupported_tree (pp, b);
837 else if (b == boolean_true_node)
839 if (c_dialect_cxx ())
840 pp_c_identifier (pp, "true");
841 else if (flag_isoc99)
842 pp_c_identifier (pp, "_True");
843 else
844 pp_unsupported_tree (pp, b);
846 else if (TREE_CODE (b) == INTEGER_CST)
847 pp_c_integer_constant (pp, b);
848 else
849 pp_unsupported_tree (pp, b);
852 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
853 false; that means the value was obtained by a cast, in which case
854 print out the type-id part of the cast-expression -- the casted value
855 is then printed by pp_c_integer_literal. */
857 static bool
858 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
860 bool value_is_named = true;
861 tree type = TREE_TYPE (e);
862 tree value;
864 /* Find the name of this constant. */
865 for (value = TYPE_VALUES (type);
866 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
867 value = TREE_CHAIN (value))
870 if (value != NULL_TREE)
871 pp_id_expression (pp, TREE_PURPOSE (value));
872 else
874 /* Value must have been cast. */
875 pp_c_type_cast (pp, type);
876 value_is_named = false;
879 return value_is_named;
882 /* Print out a REAL value as a decimal-floating-constant. */
884 static void
885 pp_c_floating_constant (c_pretty_printer *pp, tree r)
887 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
888 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
889 pp_string (pp, pp_buffer(pp)->digit_buffer);
890 if (TREE_TYPE (r) == float_type_node)
891 pp_character (pp, 'f');
892 else if (TREE_TYPE (r) == long_double_type_node)
893 pp_character (pp, 'l');
896 /* Pretty-print a compound literal expression. GNU extensions include
897 vector constants. */
899 static void
900 pp_c_compound_literal (c_pretty_printer *pp, tree e)
902 tree type = TREE_TYPE (e);
903 pp_c_type_cast (pp, type);
905 switch (TREE_CODE (type))
907 case RECORD_TYPE:
908 case UNION_TYPE:
909 case ARRAY_TYPE:
910 case VECTOR_TYPE:
911 case COMPLEX_TYPE:
912 pp_c_brace_enclosed_initializer_list (pp, e);
913 break;
915 default:
916 pp_unsupported_tree (pp, e);
917 break;
921 /* constant:
922 integer-constant
923 floating-constant
924 enumeration-constant
925 character-constant */
927 void
928 pp_c_constant (c_pretty_printer *pp, tree e)
930 const enum tree_code code = TREE_CODE (e);
932 switch (code)
934 case INTEGER_CST:
936 tree type = TREE_TYPE (e);
937 if (type == boolean_type_node)
938 pp_c_bool_constant (pp, e);
939 else if (type == char_type_node)
940 pp_c_character_constant (pp, e);
941 else if (TREE_CODE (type) == ENUMERAL_TYPE
942 && pp_c_enumeration_constant (pp, e))
944 else
945 pp_c_integer_constant (pp, e);
947 break;
949 case REAL_CST:
950 pp_c_floating_constant (pp, e);
951 break;
953 case STRING_CST:
954 pp_c_string_literal (pp, e);
955 break;
957 default:
958 pp_unsupported_tree (pp, e);
959 break;
963 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
965 void
966 pp_c_identifier (c_pretty_printer *pp, const char *id)
968 pp_c_maybe_whitespace (pp);
969 pp_identifier (pp, id);
970 pp_base (pp)->padding = pp_before;
973 /* Pretty-print a C primary-expression.
974 primary-expression:
975 identifier
976 constant
977 string-literal
978 ( expression ) */
980 void
981 pp_c_primary_expression (c_pretty_printer *pp, tree e)
983 switch (TREE_CODE (e))
985 case VAR_DECL:
986 case PARM_DECL:
987 case FIELD_DECL:
988 case CONST_DECL:
989 case FUNCTION_DECL:
990 case LABEL_DECL:
991 pp_c_tree_decl_identifier (pp, e);
992 break;
994 case IDENTIFIER_NODE:
995 pp_c_tree_identifier (pp, e);
996 break;
998 case ERROR_MARK:
999 pp_c_identifier (pp, "<erroneous-expression>");
1000 break;
1002 case RESULT_DECL:
1003 pp_c_identifier (pp, "<return-value>");
1004 break;
1006 case INTEGER_CST:
1007 case REAL_CST:
1008 case STRING_CST:
1009 pp_c_constant (pp, e);
1010 break;
1012 case TARGET_EXPR:
1013 pp_c_identifier (pp, "__builtin_memcpy");
1014 pp_c_left_paren (pp);
1015 pp_ampersand (pp);
1016 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1017 pp_separate_with (pp, ',');
1018 pp_ampersand (pp);
1019 pp_initializer (pp, TREE_OPERAND (e, 1));
1020 if (TREE_OPERAND (e, 2))
1022 pp_separate_with (pp, ',');
1023 pp_c_expression (pp, TREE_OPERAND (e, 2));
1025 pp_c_right_paren (pp);
1026 break;
1028 case STMT_EXPR:
1029 pp_c_left_paren (pp);
1030 pp_statement (pp, STMT_EXPR_STMT (e));
1031 pp_c_right_paren (pp);
1032 break;
1034 default:
1035 /* FIXME: Make sure we won't get into an infinie loop. */
1036 pp_c_left_paren (pp);
1037 pp_expression (pp, e);
1038 pp_c_right_paren (pp);
1039 break;
1043 /* Print out a C initializer -- also support C compound-literals.
1044 initializer:
1045 assignment-expression:
1046 { initializer-list }
1047 { initializer-list , } */
1049 static void
1050 pp_c_initializer (c_pretty_printer *pp, tree e)
1052 if (TREE_CODE (e) == CONSTRUCTOR)
1053 pp_c_brace_enclosed_initializer_list (pp, e);
1054 else
1055 pp_expression (pp, e);
1058 /* init-declarator:
1059 declarator:
1060 declarator = initializer */
1062 void
1063 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1065 pp_declarator (pp, t);
1066 /* We don't want to output function definitions here. There are handled
1067 elsewhere (and the syntactic form is bogus anyway). */
1068 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1070 tree init = DECL_INITIAL (t);
1071 /* This C++ bit is handled here because it is easier to do so.
1072 In templates, the C++ parser builds a TREE_LIST for a
1073 direct-initialization; the TREE_PURPOSE is the variable to
1074 initialize and the TREE_VALUE is the initializer. */
1075 if (TREE_CODE (init) == TREE_LIST)
1077 pp_c_left_paren (pp);
1078 pp_expression (pp, TREE_VALUE (init));
1079 pp_right_paren (pp);
1081 else
1083 pp_space (pp);
1084 pp_equal (pp);
1085 pp_space (pp);
1086 pp_c_initializer (pp, init);
1091 /* initializer-list:
1092 designation(opt) initializer
1093 initializer-list , designation(opt) initializer
1095 designation:
1096 designator-list =
1098 designator-list:
1099 designator
1100 designator-list designator
1102 designator:
1103 [ constant-expression ]
1104 identifier */
1106 static void
1107 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1109 tree type = TREE_TYPE (e);
1110 const enum tree_code code = TREE_CODE (type);
1112 switch (code)
1114 case RECORD_TYPE:
1115 case UNION_TYPE:
1116 case ARRAY_TYPE:
1118 tree init = TREE_OPERAND (e, 0);
1119 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1121 if (code == RECORD_TYPE || code == UNION_TYPE)
1123 pp_c_dot (pp);
1124 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1126 else
1128 pp_c_left_bracket (pp);
1129 if (TREE_PURPOSE (init))
1130 pp_c_constant (pp, TREE_PURPOSE (init));
1131 pp_c_right_bracket (pp);
1133 pp_c_whitespace (pp);
1134 pp_equal (pp);
1135 pp_c_whitespace (pp);
1136 pp_initializer (pp, TREE_VALUE (init));
1137 if (TREE_CHAIN (init))
1138 pp_separate_with (pp, ',');
1141 return;
1143 case VECTOR_TYPE:
1144 if (TREE_CODE (e) == VECTOR_CST)
1145 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1146 else if (TREE_CODE (e) == CONSTRUCTOR)
1147 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1148 else
1149 break;
1150 return;
1152 case COMPLEX_TYPE:
1153 if (TREE_CODE (e) == CONSTRUCTOR)
1154 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1155 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1157 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1158 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1159 pp_separate_with (pp, ',');
1160 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1162 else
1163 break;
1164 return;
1166 default:
1167 break;
1170 pp_unsupported_tree (pp, type);
1173 /* Pretty-print a brace-enclosed initializer-list. */
1175 static void
1176 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1178 pp_c_left_brace (pp);
1179 pp_c_initializer_list (pp, l);
1180 pp_c_right_brace (pp);
1184 /* This is a convenient function, used to bridge gap between C and C++
1185 grammars.
1187 id-expression:
1188 identifier */
1190 void
1191 pp_c_id_expression (c_pretty_printer *pp, tree t)
1193 switch (TREE_CODE (t))
1195 case VAR_DECL:
1196 case PARM_DECL:
1197 case CONST_DECL:
1198 case TYPE_DECL:
1199 case FUNCTION_DECL:
1200 case FIELD_DECL:
1201 case LABEL_DECL:
1202 pp_c_tree_decl_identifier (pp, t);
1203 break;
1205 case IDENTIFIER_NODE:
1206 pp_c_tree_identifier (pp, t);
1207 break;
1209 default:
1210 pp_unsupported_tree (pp, t);
1211 break;
1215 /* postfix-expression:
1216 primary-expression
1217 postfix-expression [ expression ]
1218 postfix-expression ( argument-expression-list(opt) )
1219 postfix-expression . identifier
1220 postfix-expression -> identifier
1221 postfix-expression ++
1222 postfix-expression --
1223 ( type-name ) { initializer-list }
1224 ( type-name ) { initializer-list , } */
1226 void
1227 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1229 enum tree_code code = TREE_CODE (e);
1230 switch (code)
1232 case POSTINCREMENT_EXPR:
1233 case POSTDECREMENT_EXPR:
1234 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1235 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1236 break;
1238 case ARROW_EXPR:
1239 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1240 pp_c_arrow (pp);
1241 break;
1243 case ARRAY_REF:
1244 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1245 pp_c_left_bracket (pp);
1246 pp_expression (pp, TREE_OPERAND (e, 1));
1247 pp_c_right_bracket (pp);
1248 break;
1250 case CALL_EXPR:
1251 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1252 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1253 break;
1255 case UNORDERED_EXPR:
1256 pp_c_identifier (pp, flag_isoc99
1257 ? "isunordered"
1258 : "__builtin_isunordered");
1259 goto two_args_fun;
1261 case ORDERED_EXPR:
1262 pp_c_identifier (pp, flag_isoc99
1263 ? "!isunordered"
1264 : "!__builtin_isunordered");
1265 goto two_args_fun;
1267 case UNLT_EXPR:
1268 pp_c_identifier (pp, flag_isoc99
1269 ? "!isgreaterequal"
1270 : "!__builtin_isgreaterequal");
1271 goto two_args_fun;
1273 case UNLE_EXPR:
1274 pp_c_identifier (pp, flag_isoc99
1275 ? "!isgreater"
1276 : "!__builtin_isgreater");
1277 goto two_args_fun;
1279 case UNGT_EXPR:
1280 pp_c_identifier (pp, flag_isoc99
1281 ? "!islessequal"
1282 : "!__builtin_islessequal");
1283 goto two_args_fun;
1285 case UNGE_EXPR:
1286 pp_c_identifier (pp, flag_isoc99
1287 ? "!isless"
1288 : "!__builtin_isless");
1289 goto two_args_fun;
1291 case UNEQ_EXPR:
1292 pp_c_identifier (pp, flag_isoc99
1293 ? "!islessgreater"
1294 : "!__builtin_islessgreater");
1295 goto two_args_fun;
1297 case LTGT_EXPR:
1298 pp_c_identifier (pp, flag_isoc99
1299 ? "islessgreater"
1300 : "__builtin_islessgreater");
1301 goto two_args_fun;
1303 two_args_fun:
1304 pp_c_left_paren (pp);
1305 pp_expression (pp, TREE_OPERAND (e, 0));
1306 pp_separate_with (pp, ',');
1307 pp_expression (pp, TREE_OPERAND (e, 1));
1308 pp_c_right_paren (pp);
1309 break;
1311 case ABS_EXPR:
1312 pp_c_identifier (pp, "__builtin_abs");
1313 pp_c_left_paren (pp);
1314 pp_expression (pp, TREE_OPERAND (e, 0));
1315 pp_c_right_paren (pp);
1316 break;
1318 case COMPONENT_REF:
1320 tree object = TREE_OPERAND (e, 0);
1321 if (TREE_CODE (object) == INDIRECT_REF)
1323 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1324 pp_c_arrow (pp);
1326 else
1328 pp_postfix_expression (pp, object);
1329 pp_c_dot (pp);
1331 pp_expression (pp, TREE_OPERAND (e, 1));
1333 break;
1335 case COMPLEX_CST:
1336 case VECTOR_CST:
1337 case COMPLEX_EXPR:
1338 pp_c_compound_literal (pp, e);
1339 break;
1341 case COMPOUND_LITERAL_EXPR:
1342 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1343 /* Fall through. */
1344 case CONSTRUCTOR:
1345 pp_initializer (pp, e);
1346 break;
1348 case VA_ARG_EXPR:
1349 pp_c_identifier (pp, "__builtin_va_arg");
1350 pp_c_left_paren (pp);
1351 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1352 pp_separate_with (pp, ',');
1353 pp_type_id (pp, TREE_TYPE (e));
1354 pp_c_right_paren (pp);
1355 break;
1357 case ADDR_EXPR:
1358 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1360 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1361 break;
1363 /* else fall through. */
1365 default:
1366 pp_primary_expression (pp, e);
1367 break;
1371 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1373 void
1374 pp_c_expression_list (c_pretty_printer *pp, tree e)
1376 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1378 pp_expression (pp, TREE_VALUE (e));
1379 if (TREE_CHAIN (e))
1380 pp_separate_with (pp, ',');
1384 /* Print out an expression-list in parens, as in a function call. */
1386 void
1387 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1389 pp_c_left_paren (pp);
1390 if (t && TREE_CODE (t) == TREE_LIST)
1391 pp_c_expression_list (pp, t);
1392 pp_c_right_paren (pp);
1395 /* unary-expression:
1396 postfix-expression
1397 ++ cast-expression
1398 -- cast-expression
1399 unary-operator cast-expression
1400 sizeof unary-expression
1401 sizeof ( type-id )
1403 unary-operator: one of
1404 * & + - ! ~
1406 GNU extensions.
1407 unary-expression:
1408 __alignof__ unary-expression
1409 __alignof__ ( type-id )
1410 __real__ unary-expression
1411 __imag__ unary-expression */
1413 void
1414 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1416 enum tree_code code = TREE_CODE (e);
1417 switch (code)
1419 case PREINCREMENT_EXPR:
1420 case PREDECREMENT_EXPR:
1421 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1422 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1423 break;
1425 case ADDR_EXPR:
1426 case INDIRECT_REF:
1427 case NEGATE_EXPR:
1428 case BIT_NOT_EXPR:
1429 case TRUTH_NOT_EXPR:
1430 case CONJ_EXPR:
1431 /* String literal are used by address. */
1432 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1433 pp_ampersand (pp);
1434 else if (code == INDIRECT_REF)
1435 pp_c_star (pp);
1436 else if (code == NEGATE_EXPR)
1437 pp_minus (pp);
1438 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1439 pp_complement (pp);
1440 else if (code == TRUTH_NOT_EXPR)
1441 pp_exclamation (pp);
1442 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1443 break;
1445 case SIZEOF_EXPR:
1446 case ALIGNOF_EXPR:
1447 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1448 pp_c_whitespace (pp);
1449 if (TYPE_P (TREE_OPERAND (e, 0)))
1450 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1451 else
1452 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1453 break;
1455 case REALPART_EXPR:
1456 case IMAGPART_EXPR:
1457 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1458 pp_c_whitespace (pp);
1459 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1460 break;
1462 default:
1463 pp_postfix_expression (pp, e);
1464 break;
1468 /* cast-expression:
1469 unary-expression
1470 ( type-name ) cast-expression */
1472 void
1473 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1475 switch (TREE_CODE (e))
1477 case FLOAT_EXPR:
1478 case FIX_TRUNC_EXPR:
1479 case CONVERT_EXPR:
1480 pp_c_type_cast (pp, TREE_TYPE (e));
1481 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1482 break;
1484 default:
1485 pp_unary_expression (pp, e);
1489 /* multiplicative-expression:
1490 cast-expression
1491 multiplicative-expression * cast-expression
1492 multiplicative-expression / cast-expression
1493 multiplicative-expression % cast-expression */
1495 static void
1496 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1498 enum tree_code code = TREE_CODE (e);
1499 switch (code)
1501 case MULT_EXPR:
1502 case TRUNC_DIV_EXPR:
1503 case TRUNC_MOD_EXPR:
1504 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1505 pp_c_whitespace (pp);
1506 if (code == MULT_EXPR)
1507 pp_c_star (pp);
1508 else if (code == TRUNC_DIV_EXPR)
1509 pp_slash (pp);
1510 else
1511 pp_modulo (pp);
1512 pp_c_whitespace (pp);
1513 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1514 break;
1516 default:
1517 pp_c_cast_expression (pp, e);
1518 break;
1522 /* additive-expression:
1523 multiplicative-expression
1524 additive-expression + multiplicative-expression
1525 additive-expression - multiplicative-expression */
1527 static void
1528 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1530 enum tree_code code = TREE_CODE (e);
1531 switch (code)
1533 case PLUS_EXPR:
1534 case MINUS_EXPR:
1535 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1536 pp_c_whitespace (pp);
1537 if (code == PLUS_EXPR)
1538 pp_plus (pp);
1539 else
1540 pp_minus (pp);
1541 pp_c_whitespace (pp);
1542 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1543 break;
1545 default:
1546 pp_multiplicative_expression (pp, e);
1547 break;
1551 /* additive-expression:
1552 additive-expression
1553 shift-expression << additive-expression
1554 shift-expression >> additive-expression */
1556 static void
1557 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1559 enum tree_code code = TREE_CODE (e);
1560 switch (code)
1562 case LSHIFT_EXPR:
1563 case RSHIFT_EXPR:
1564 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1565 pp_c_whitespace (pp);
1566 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1567 pp_c_whitespace (pp);
1568 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1569 break;
1571 default:
1572 pp_c_additive_expression (pp, e);
1576 /* relational-expression:
1577 shift-expression
1578 relational-expression < shift-expression
1579 relational-expression > shift-expression
1580 relational-expression <= shift-expression
1581 relational-expression >= shift-expression */
1583 static void
1584 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1586 enum tree_code code = TREE_CODE (e);
1587 switch (code)
1589 case LT_EXPR:
1590 case GT_EXPR:
1591 case LE_EXPR:
1592 case GE_EXPR:
1593 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1594 pp_c_whitespace (pp);
1595 if (code == LT_EXPR)
1596 pp_less (pp);
1597 else if (code == GT_EXPR)
1598 pp_greater (pp);
1599 else if (code == LE_EXPR)
1600 pp_identifier (pp, "<=");
1601 else if (code == GE_EXPR)
1602 pp_identifier (pp, ">=");
1603 pp_c_whitespace (pp);
1604 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1605 break;
1607 default:
1608 pp_c_shift_expression (pp, e);
1609 break;
1613 /* equality-expression:
1614 relational-expression
1615 equality-expression == relational-expression
1616 equality-equality != relational-expression */
1618 static void
1619 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1621 enum tree_code code = TREE_CODE (e);
1622 switch (code)
1624 case EQ_EXPR:
1625 case NE_EXPR:
1626 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1627 pp_c_whitespace (pp);
1628 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1629 pp_c_whitespace (pp);
1630 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1631 break;
1633 default:
1634 pp_c_relational_expression (pp, e);
1635 break;
1639 /* AND-expression:
1640 equality-expression
1641 AND-expression & equality-equality */
1643 static void
1644 pp_c_and_expression (c_pretty_printer *pp, tree e)
1646 if (TREE_CODE (e) == BIT_AND_EXPR)
1648 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1649 pp_c_whitespace (pp);
1650 pp_ampersand (pp);
1651 pp_c_whitespace (pp);
1652 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1654 else
1655 pp_c_equality_expression (pp, e);
1658 /* exclusive-OR-expression:
1659 AND-expression
1660 exclusive-OR-expression ^ AND-expression */
1662 static void
1663 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1665 if (TREE_CODE (e) == BIT_XOR_EXPR)
1667 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1668 pp_c_maybe_whitespace (pp);
1669 pp_carret (pp);
1670 pp_c_whitespace (pp);
1671 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1673 else
1674 pp_c_and_expression (pp, e);
1677 /* inclusive-OR-expression:
1678 exclusive-OR-expression
1679 inclusive-OR-expression | exclusive-OR-expression */
1681 static void
1682 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1684 if (TREE_CODE (e) == BIT_IOR_EXPR)
1686 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1687 pp_c_whitespace (pp);
1688 pp_bar (pp);
1689 pp_c_whitespace (pp);
1690 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1692 else
1693 pp_c_exclusive_or_expression (pp, e);
1696 /* logical-AND-expression:
1697 inclusive-OR-expression
1698 logical-AND-expression && inclusive-OR-expression */
1700 static void
1701 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1703 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1705 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1706 pp_c_whitespace (pp);
1707 pp_identifier (pp, "&&");
1708 pp_c_whitespace (pp);
1709 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1711 else
1712 pp_c_inclusive_or_expression (pp, e);
1715 /* logical-OR-expression:
1716 logical-AND-expression
1717 logical-OR-expression || logical-AND-expression */
1719 void
1720 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1722 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1724 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1725 pp_c_whitespace (pp);
1726 pp_identifier (pp, "||");
1727 pp_c_whitespace (pp);
1728 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1730 else
1731 pp_c_logical_and_expression (pp, e);
1734 /* conditional-expression:
1735 logical-OR-expression
1736 logical-OR-expression ? expression : conditional-expression */
1738 static void
1739 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1741 if (TREE_CODE (e) == COND_EXPR)
1743 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1744 pp_c_whitespace (pp);
1745 pp_question (pp);
1746 pp_c_whitespace (pp);
1747 pp_expression (pp, TREE_OPERAND (e, 1));
1748 pp_c_whitespace (pp);
1749 pp_colon (pp);
1750 pp_c_whitespace (pp);
1751 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1753 else
1754 pp_c_logical_or_expression (pp, e);
1758 /* assignment-expression:
1759 conditional-expression
1760 unary-expression assignment-operator assignment-expression
1762 assignment-expression: one of
1763 = *= /= %= += -= >>= <<= &= ^= |= */
1765 static void
1766 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1768 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1770 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1771 pp_c_whitespace (pp);
1772 pp_equal (pp);
1773 pp_space (pp);
1774 pp_c_expression (pp, TREE_OPERAND (e, 1));
1776 else
1777 pp_c_conditional_expression (pp, e);
1780 /* expression:
1781 assignment-expression
1782 expression , assignment-expression
1784 Implementation note: instead of going through the usual recursion
1785 chain, I take the liberty of dispatching nodes to the appropriate
1786 functions. This makes some redundancy, but it worths it. That also
1787 prevents a possible infinite recursion between pp_c_primary_expression ()
1788 and pp_c_expression (). */
1790 void
1791 pp_c_expression (c_pretty_printer *pp, tree e)
1793 switch (TREE_CODE (e))
1795 case INTEGER_CST:
1796 pp_c_integer_constant (pp, e);
1797 break;
1799 case REAL_CST:
1800 pp_c_floating_constant (pp, e);
1801 break;
1803 case STRING_CST:
1804 pp_c_string_literal (pp, e);
1805 break;
1807 case IDENTIFIER_NODE:
1808 case FUNCTION_DECL:
1809 case VAR_DECL:
1810 case CONST_DECL:
1811 case PARM_DECL:
1812 case RESULT_DECL:
1813 case FIELD_DECL:
1814 case LABEL_DECL:
1815 case ERROR_MARK:
1816 case STMT_EXPR:
1817 pp_primary_expression (pp, e);
1818 break;
1820 case POSTINCREMENT_EXPR:
1821 case POSTDECREMENT_EXPR:
1822 case ARROW_EXPR:
1823 case ARRAY_REF:
1824 case CALL_EXPR:
1825 case COMPONENT_REF:
1826 case COMPLEX_CST:
1827 case COMPLEX_EXPR:
1828 case VECTOR_CST:
1829 case ORDERED_EXPR:
1830 case UNORDERED_EXPR:
1831 case LTGT_EXPR:
1832 case UNEQ_EXPR:
1833 case UNLE_EXPR:
1834 case UNLT_EXPR:
1835 case UNGE_EXPR:
1836 case UNGT_EXPR:
1837 case ABS_EXPR:
1838 case CONSTRUCTOR:
1839 case COMPOUND_LITERAL_EXPR:
1840 case VA_ARG_EXPR:
1841 pp_postfix_expression (pp, e);
1842 break;
1844 case CONJ_EXPR:
1845 case ADDR_EXPR:
1846 case INDIRECT_REF:
1847 case NEGATE_EXPR:
1848 case BIT_NOT_EXPR:
1849 case TRUTH_NOT_EXPR:
1850 case PREINCREMENT_EXPR:
1851 case PREDECREMENT_EXPR:
1852 case SIZEOF_EXPR:
1853 case ALIGNOF_EXPR:
1854 case REALPART_EXPR:
1855 case IMAGPART_EXPR:
1856 pp_c_unary_expression (pp, e);
1857 break;
1859 case FLOAT_EXPR:
1860 case FIX_TRUNC_EXPR:
1861 case CONVERT_EXPR:
1862 pp_c_cast_expression (pp, e);
1863 break;
1865 case MULT_EXPR:
1866 case TRUNC_MOD_EXPR:
1867 case TRUNC_DIV_EXPR:
1868 pp_multiplicative_expression (pp, e);
1869 break;
1871 case LSHIFT_EXPR:
1872 case RSHIFT_EXPR:
1873 pp_c_shift_expression (pp, e);
1874 break;
1876 case LT_EXPR:
1877 case GT_EXPR:
1878 case LE_EXPR:
1879 case GE_EXPR:
1880 pp_c_relational_expression (pp, e);
1881 break;
1883 case BIT_AND_EXPR:
1884 pp_c_and_expression (pp, e);
1885 break;
1887 case BIT_XOR_EXPR:
1888 pp_c_exclusive_or_expression (pp, e);
1889 break;
1891 case BIT_IOR_EXPR:
1892 pp_c_inclusive_or_expression (pp, e);
1893 break;
1895 case TRUTH_ANDIF_EXPR:
1896 pp_c_logical_and_expression (pp, e);
1897 break;
1899 case TRUTH_ORIF_EXPR:
1900 pp_c_logical_or_expression (pp, e);
1901 break;
1903 case EQ_EXPR:
1904 case NE_EXPR:
1905 pp_c_equality_expression (pp, e);
1906 break;
1908 case COND_EXPR:
1909 pp_conditional_expression (pp, e);
1910 break;
1912 case PLUS_EXPR:
1913 case MINUS_EXPR:
1914 pp_c_additive_expression (pp, e);
1915 break;
1917 case MODIFY_EXPR:
1918 case INIT_EXPR:
1919 pp_assignment_expression (pp, e);
1920 break;
1922 case COMPOUND_EXPR:
1923 pp_c_left_paren (pp);
1924 pp_expression (pp, TREE_OPERAND (e, 0));
1925 pp_separate_with (pp, ',');
1926 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1927 pp_c_right_paren (pp);
1928 break;
1930 case NOP_EXPR:
1931 case NON_LVALUE_EXPR:
1932 case SAVE_EXPR:
1933 pp_expression (pp, TREE_OPERAND (e, 0));
1934 break;
1936 case TARGET_EXPR:
1937 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1938 break;
1940 default:
1941 pp_unsupported_tree (pp, e);
1942 break;
1948 /* Statements. */
1950 /* statement:
1951 labeled-statement
1952 compound-statement
1953 expression-statement
1954 selection-statement
1955 iteration-statement
1956 jump-statement */
1958 void
1959 pp_c_statement (c_pretty_printer *pp, tree stmt)
1961 enum tree_code code;
1963 if (stmt == NULL)
1964 return;
1966 if (pp_needs_newline (pp))
1967 pp_newline_and_indent (pp, 0);
1969 code = TREE_CODE (stmt);
1970 switch (code)
1972 /* expression-statement:
1973 expression(opt) ; */
1974 case EXPR_STMT:
1975 pp_expression (pp, EXPR_STMT_EXPR (stmt));
1976 pp_c_semicolon (pp);
1977 pp_needs_newline (pp) = true;
1978 break;
1980 case SWITCH_STMT:
1981 pp_c_identifier (pp, "switch");
1982 pp_space (pp);
1983 pp_c_left_paren (pp);
1984 pp_expression (pp, SWITCH_COND (stmt));
1985 pp_c_right_paren (pp);
1986 pp_indentation (pp) += 3;
1987 pp_needs_newline (pp) = true;
1988 pp_statement (pp, SWITCH_BODY (stmt));
1989 pp_newline_and_indent (pp, -3);
1990 break;
1992 /* iteration-statement:
1993 while ( expression ) statement
1994 do statement while ( expression ) ;
1995 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1996 for ( declaration expression(opt) ; expression(opt) ) statement */
1997 case WHILE_STMT:
1998 pp_c_identifier (pp, "while");
1999 pp_space (pp);
2000 pp_c_left_paren (pp);
2001 pp_expression (pp, WHILE_COND (stmt));
2002 pp_c_right_paren (pp);
2003 pp_newline_and_indent (pp, 3);
2004 pp_statement (pp, WHILE_BODY (stmt));
2005 pp_indentation (pp) -= 3;
2006 pp_needs_newline (pp) = true;
2007 break;
2009 case DO_STMT:
2010 pp_c_identifier (pp, "do");
2011 pp_newline_and_indent (pp, 3);
2012 pp_statement (pp, DO_BODY (stmt));
2013 pp_newline_and_indent (pp, -3);
2014 pp_c_identifier (pp, "while");
2015 pp_space (pp);
2016 pp_c_left_paren (pp);
2017 pp_expression (pp, DO_COND (stmt));
2018 pp_c_right_paren (pp);
2019 pp_c_semicolon (pp);
2020 pp_needs_newline (pp) = true;
2021 break;
2023 case FOR_STMT:
2024 pp_c_identifier (pp, "for");
2025 pp_space (pp);
2026 pp_c_left_paren (pp);
2027 if (FOR_INIT_STMT (stmt))
2028 pp_statement (pp, FOR_INIT_STMT (stmt));
2029 else
2030 pp_c_semicolon (pp);
2031 pp_needs_newline (pp) = false;
2032 pp_c_whitespace (pp);
2033 if (FOR_COND (stmt))
2034 pp_expression (pp, FOR_COND (stmt));
2035 pp_c_semicolon (pp);
2036 pp_needs_newline (pp) = false;
2037 pp_c_whitespace (pp);
2038 if (FOR_EXPR (stmt))
2039 pp_expression (pp, FOR_EXPR (stmt));
2040 pp_c_right_paren (pp);
2041 pp_newline_and_indent (pp, 3);
2042 pp_statement (pp, FOR_BODY (stmt));
2043 pp_indentation (pp) -= 3;
2044 pp_needs_newline (pp) = true;
2045 break;
2047 /* jump-statement:
2048 goto identifier;
2049 continue ;
2050 return expression(opt) ; */
2051 case BREAK_STMT:
2052 case CONTINUE_STMT:
2053 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
2054 pp_c_semicolon (pp);
2055 pp_needs_newline (pp) = true;
2056 break;
2058 default:
2059 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2060 break;
2065 /* Initialize the PRETTY-PRINTER for handling C codes. */
2067 void
2068 pp_c_pretty_printer_init (c_pretty_printer *pp)
2070 pp->offset_list = 0;
2072 pp->declaration = pp_c_declaration;
2073 pp->declaration_specifiers = pp_c_declaration_specifiers;
2074 pp->declarator = pp_c_declarator;
2075 pp->direct_declarator = pp_c_direct_declarator;
2076 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2077 pp->abstract_declarator = pp_c_abstract_declarator;
2078 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2079 pp->ptr_operator = pp_c_pointer;
2080 pp->parameter_list = pp_c_parameter_type_list;
2081 pp->type_id = pp_c_type_id;
2082 pp->simple_type_specifier = pp_c_type_specifier;
2083 pp->function_specifier = pp_c_function_specifier;
2084 pp->storage_class_specifier = pp_c_storage_class_specifier;
2086 pp->statement = pp_c_statement;
2088 pp->id_expression = pp_c_id_expression;
2089 pp->primary_expression = pp_c_primary_expression;
2090 pp->postfix_expression = pp_c_postfix_expression;
2091 pp->unary_expression = pp_c_unary_expression;
2092 pp->initializer = pp_c_initializer;
2093 pp->multiplicative_expression = pp_c_multiplicative_expression;
2094 pp->conditional_expression = pp_c_conditional_expression;
2095 pp->assignment_expression = pp_c_assignment_expression;
2096 pp->expression = pp_c_expression;
2100 /* Print the tree T in full, on file FILE. */
2102 void
2103 print_c_tree (FILE *file, tree t)
2105 static c_pretty_printer pp_rec;
2106 static bool initialized = 0;
2107 c_pretty_printer *pp = &pp_rec;
2109 if (!initialized)
2111 initialized = 1;
2112 pp_construct (pp_base (pp), NULL, 0);
2113 pp_c_pretty_printer_init (pp);
2114 pp_needs_newline (pp) = true;
2116 pp_base (pp)->buffer->stream = file;
2118 pp_statement (pp, t);
2120 pp_newline (pp);
2121 pp_flush (pp);
2124 /* Print the tree T in full, on stderr. */
2126 void
2127 debug_c_tree (tree t)
2129 print_c_tree (stderr, t);
2130 fputc ('\n', stderr);
2133 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2134 up of T's memory address. */
2136 void
2137 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2139 const char *name;
2141 if (!DECL_P (t))
2142 abort ();
2144 if (DECL_NAME (t))
2145 name = IDENTIFIER_POINTER (DECL_NAME (t));
2146 else
2148 static char xname[8];
2149 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2150 name = xname;
2153 pp_c_identifier (pp, name);