Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / c-pretty-print.c
blob2176e34850b08c8c8103f0627426f5883d807e4c
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005 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. This is called solely for characters which are
716 in the *target* execution character set. We ought to convert them
717 back to the *host* execution character set before printing, but we
718 have no way to do this at present. A decent compromise is to print
719 all characters as if they were in the host execution character set,
720 and not attempt to recover any named escape characters, but render
721 all unprintables as octal escapes. If the host and target character
722 sets are the same, this produces relatively readable output. If they
723 are not the same, strings may appear as gibberish, but that's okay
724 (in fact, it may well be what the reader wants, e.g. if they are looking
725 to see if conversion to the target character set happened correctly).
727 A special case: we need to prefix \, ", and ' with backslashes. It is
728 correct to do so for the *host*'s \, ", and ', because the rest of the
729 file appears in the host character set. */
731 static void
732 pp_c_char (c_pretty_printer *pp, int c)
734 if (ISPRINT (c))
736 switch (c)
738 case '\\': pp_string (pp, "\\\\"); break;
739 case '\'': pp_string (pp, "\\\'"); break;
740 case '\"': pp_string (pp, "\\\""); break;
741 default: pp_character (pp, c);
744 else
745 pp_scalar (pp, "\\%03o", (unsigned) c);
748 /* Print out a STRING literal. */
750 void
751 pp_c_string_literal (c_pretty_printer *pp, tree s)
753 const char *p = TREE_STRING_POINTER (s);
754 int n = TREE_STRING_LENGTH (s) - 1;
755 int i;
756 pp_doublequote (pp);
757 for (i = 0; i < n; ++i)
758 pp_c_char (pp, p[i]);
759 pp_doublequote (pp);
762 /* Pretty-print an INTEGER literal. */
764 static void
765 pp_c_integer_constant (c_pretty_printer *pp, tree i)
767 tree type = TREE_TYPE (i);
769 if (TREE_INT_CST_HIGH (i) == 0)
770 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
771 else
773 if (tree_int_cst_sgn (i) < 0)
775 pp_character (pp, '-');
776 i = build_int_cst_wide (NULL_TREE,
777 -TREE_INT_CST_LOW (i),
778 ~TREE_INT_CST_HIGH (i)
779 + !TREE_INT_CST_LOW (i));
781 sprintf (pp_buffer (pp)->digit_buffer,
782 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
783 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
784 pp_string (pp, pp_buffer (pp)->digit_buffer);
786 if (TYPE_UNSIGNED (type))
787 pp_character (pp, 'u');
788 if (type == long_integer_type_node || type == long_unsigned_type_node)
789 pp_character (pp, 'l');
790 else if (type == long_long_integer_type_node
791 || type == long_long_unsigned_type_node)
792 pp_string (pp, "ll");
795 /* Print out a CHARACTER literal. */
797 static void
798 pp_c_character_constant (c_pretty_printer *pp, tree c)
800 tree type = TREE_TYPE (c);
801 if (type == wchar_type_node)
802 pp_character (pp, 'L');
803 pp_quote (pp);
804 if (host_integerp (c, TYPE_UNSIGNED (type)))
805 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
806 else
807 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
808 pp_quote (pp);
811 /* Print out a BOOLEAN literal. */
813 static void
814 pp_c_bool_constant (c_pretty_printer *pp, tree b)
816 if (b == boolean_false_node)
818 if (c_dialect_cxx ())
819 pp_c_identifier (pp, "false");
820 else if (flag_isoc99)
821 pp_c_identifier (pp, "_False");
822 else
823 pp_unsupported_tree (pp, b);
825 else if (b == boolean_true_node)
827 if (c_dialect_cxx ())
828 pp_c_identifier (pp, "true");
829 else if (flag_isoc99)
830 pp_c_identifier (pp, "_True");
831 else
832 pp_unsupported_tree (pp, b);
834 else if (TREE_CODE (b) == INTEGER_CST)
835 pp_c_integer_constant (pp, b);
836 else
837 pp_unsupported_tree (pp, b);
840 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
841 false; that means the value was obtained by a cast, in which case
842 print out the type-id part of the cast-expression -- the casted value
843 is then printed by pp_c_integer_literal. */
845 static bool
846 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
848 bool value_is_named = true;
849 tree type = TREE_TYPE (e);
850 tree value;
852 /* Find the name of this constant. */
853 for (value = TYPE_VALUES (type);
854 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
855 value = TREE_CHAIN (value))
858 if (value != NULL_TREE)
859 pp_id_expression (pp, TREE_PURPOSE (value));
860 else
862 /* Value must have been cast. */
863 pp_c_type_cast (pp, type);
864 value_is_named = false;
867 return value_is_named;
870 /* Print out a REAL value as a decimal-floating-constant. */
872 static void
873 pp_c_floating_constant (c_pretty_printer *pp, tree r)
875 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
876 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
877 pp_string (pp, pp_buffer(pp)->digit_buffer);
878 if (TREE_TYPE (r) == float_type_node)
879 pp_character (pp, 'f');
880 else if (TREE_TYPE (r) == long_double_type_node)
881 pp_character (pp, 'l');
884 /* Pretty-print a compound literal expression. GNU extensions include
885 vector constants. */
887 static void
888 pp_c_compound_literal (c_pretty_printer *pp, tree e)
890 tree type = TREE_TYPE (e);
891 pp_c_type_cast (pp, type);
893 switch (TREE_CODE (type))
895 case RECORD_TYPE:
896 case UNION_TYPE:
897 case ARRAY_TYPE:
898 case VECTOR_TYPE:
899 case COMPLEX_TYPE:
900 pp_c_brace_enclosed_initializer_list (pp, e);
901 break;
903 default:
904 pp_unsupported_tree (pp, e);
905 break;
909 /* constant:
910 integer-constant
911 floating-constant
912 enumeration-constant
913 character-constant */
915 void
916 pp_c_constant (c_pretty_printer *pp, tree e)
918 const enum tree_code code = TREE_CODE (e);
920 switch (code)
922 case INTEGER_CST:
924 tree type = TREE_TYPE (e);
925 if (type == boolean_type_node)
926 pp_c_bool_constant (pp, e);
927 else if (type == char_type_node)
928 pp_c_character_constant (pp, e);
929 else if (TREE_CODE (type) == ENUMERAL_TYPE
930 && pp_c_enumeration_constant (pp, e))
932 else
933 pp_c_integer_constant (pp, e);
935 break;
937 case REAL_CST:
938 pp_c_floating_constant (pp, e);
939 break;
941 case STRING_CST:
942 pp_c_string_literal (pp, e);
943 break;
945 default:
946 pp_unsupported_tree (pp, e);
947 break;
951 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
953 void
954 pp_c_identifier (c_pretty_printer *pp, const char *id)
956 pp_c_maybe_whitespace (pp);
957 pp_identifier (pp, id);
958 pp_base (pp)->padding = pp_before;
961 /* Pretty-print a C primary-expression.
962 primary-expression:
963 identifier
964 constant
965 string-literal
966 ( expression ) */
968 void
969 pp_c_primary_expression (c_pretty_printer *pp, tree e)
971 switch (TREE_CODE (e))
973 case VAR_DECL:
974 case PARM_DECL:
975 case FIELD_DECL:
976 case CONST_DECL:
977 case FUNCTION_DECL:
978 case LABEL_DECL:
979 pp_c_tree_decl_identifier (pp, e);
980 break;
982 case IDENTIFIER_NODE:
983 pp_c_tree_identifier (pp, e);
984 break;
986 case ERROR_MARK:
987 pp_c_identifier (pp, "<erroneous-expression>");
988 break;
990 case RESULT_DECL:
991 pp_c_identifier (pp, "<return-value>");
992 break;
994 case INTEGER_CST:
995 case REAL_CST:
996 case STRING_CST:
997 pp_c_constant (pp, e);
998 break;
1000 case TARGET_EXPR:
1001 pp_c_identifier (pp, "__builtin_memcpy");
1002 pp_c_left_paren (pp);
1003 pp_ampersand (pp);
1004 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1005 pp_separate_with (pp, ',');
1006 pp_ampersand (pp);
1007 pp_initializer (pp, TREE_OPERAND (e, 1));
1008 if (TREE_OPERAND (e, 2))
1010 pp_separate_with (pp, ',');
1011 pp_c_expression (pp, TREE_OPERAND (e, 2));
1013 pp_c_right_paren (pp);
1014 break;
1016 case STMT_EXPR:
1017 pp_c_left_paren (pp);
1018 pp_statement (pp, STMT_EXPR_STMT (e));
1019 pp_c_right_paren (pp);
1020 break;
1022 default:
1023 /* FIXME: Make sure we won't get into an infinie loop. */
1024 pp_c_left_paren (pp);
1025 pp_expression (pp, e);
1026 pp_c_right_paren (pp);
1027 break;
1031 /* Print out a C initializer -- also support C compound-literals.
1032 initializer:
1033 assignment-expression:
1034 { initializer-list }
1035 { initializer-list , } */
1037 static void
1038 pp_c_initializer (c_pretty_printer *pp, tree e)
1040 if (TREE_CODE (e) == CONSTRUCTOR)
1041 pp_c_brace_enclosed_initializer_list (pp, e);
1042 else
1043 pp_expression (pp, e);
1046 /* init-declarator:
1047 declarator:
1048 declarator = initializer */
1050 void
1051 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1053 pp_declarator (pp, t);
1054 /* We don't want to output function definitions here. There are handled
1055 elsewhere (and the syntactic form is bogus anyway). */
1056 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1058 tree init = DECL_INITIAL (t);
1059 /* This C++ bit is handled here because it is easier to do so.
1060 In templates, the C++ parser builds a TREE_LIST for a
1061 direct-initialization; the TREE_PURPOSE is the variable to
1062 initialize and the TREE_VALUE is the initializer. */
1063 if (TREE_CODE (init) == TREE_LIST)
1065 pp_c_left_paren (pp);
1066 pp_expression (pp, TREE_VALUE (init));
1067 pp_right_paren (pp);
1069 else
1071 pp_space (pp);
1072 pp_equal (pp);
1073 pp_space (pp);
1074 pp_c_initializer (pp, init);
1079 /* initializer-list:
1080 designation(opt) initializer
1081 initializer-list , designation(opt) initializer
1083 designation:
1084 designator-list =
1086 designator-list:
1087 designator
1088 designator-list designator
1090 designator:
1091 [ constant-expression ]
1092 identifier */
1094 static void
1095 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1097 tree type = TREE_TYPE (e);
1098 const enum tree_code code = TREE_CODE (type);
1100 switch (code)
1102 case RECORD_TYPE:
1103 case UNION_TYPE:
1104 case ARRAY_TYPE:
1106 tree init = TREE_OPERAND (e, 0);
1107 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1109 if (code == RECORD_TYPE || code == UNION_TYPE)
1111 pp_c_dot (pp);
1112 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1114 else
1116 pp_c_left_bracket (pp);
1117 if (TREE_PURPOSE (init))
1118 pp_c_constant (pp, TREE_PURPOSE (init));
1119 pp_c_right_bracket (pp);
1121 pp_c_whitespace (pp);
1122 pp_equal (pp);
1123 pp_c_whitespace (pp);
1124 pp_initializer (pp, TREE_VALUE (init));
1125 if (TREE_CHAIN (init))
1126 pp_separate_with (pp, ',');
1129 return;
1131 case VECTOR_TYPE:
1132 if (TREE_CODE (e) == VECTOR_CST)
1133 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1134 else if (TREE_CODE (e) == CONSTRUCTOR)
1135 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1136 else
1137 break;
1138 return;
1140 case COMPLEX_TYPE:
1141 if (TREE_CODE (e) == CONSTRUCTOR)
1142 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1143 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1145 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1146 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1147 pp_separate_with (pp, ',');
1148 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1150 else
1151 break;
1152 return;
1154 default:
1155 break;
1158 pp_unsupported_tree (pp, type);
1161 /* Pretty-print a brace-enclosed initializer-list. */
1163 static void
1164 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1166 pp_c_left_brace (pp);
1167 pp_c_initializer_list (pp, l);
1168 pp_c_right_brace (pp);
1172 /* This is a convenient function, used to bridge gap between C and C++
1173 grammars.
1175 id-expression:
1176 identifier */
1178 void
1179 pp_c_id_expression (c_pretty_printer *pp, tree t)
1181 switch (TREE_CODE (t))
1183 case VAR_DECL:
1184 case PARM_DECL:
1185 case CONST_DECL:
1186 case TYPE_DECL:
1187 case FUNCTION_DECL:
1188 case FIELD_DECL:
1189 case LABEL_DECL:
1190 pp_c_tree_decl_identifier (pp, t);
1191 break;
1193 case IDENTIFIER_NODE:
1194 pp_c_tree_identifier (pp, t);
1195 break;
1197 default:
1198 pp_unsupported_tree (pp, t);
1199 break;
1203 /* postfix-expression:
1204 primary-expression
1205 postfix-expression [ expression ]
1206 postfix-expression ( argument-expression-list(opt) )
1207 postfix-expression . identifier
1208 postfix-expression -> identifier
1209 postfix-expression ++
1210 postfix-expression --
1211 ( type-name ) { initializer-list }
1212 ( type-name ) { initializer-list , } */
1214 void
1215 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1217 enum tree_code code = TREE_CODE (e);
1218 switch (code)
1220 case POSTINCREMENT_EXPR:
1221 case POSTDECREMENT_EXPR:
1222 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1223 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1224 break;
1226 case ARROW_EXPR:
1227 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1228 pp_c_arrow (pp);
1229 break;
1231 case ARRAY_REF:
1232 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1233 pp_c_left_bracket (pp);
1234 pp_expression (pp, TREE_OPERAND (e, 1));
1235 pp_c_right_bracket (pp);
1236 break;
1238 case CALL_EXPR:
1239 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1240 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1241 break;
1243 case UNORDERED_EXPR:
1244 pp_c_identifier (pp, flag_isoc99
1245 ? "isunordered"
1246 : "__builtin_isunordered");
1247 goto two_args_fun;
1249 case ORDERED_EXPR:
1250 pp_c_identifier (pp, flag_isoc99
1251 ? "!isunordered"
1252 : "!__builtin_isunordered");
1253 goto two_args_fun;
1255 case UNLT_EXPR:
1256 pp_c_identifier (pp, flag_isoc99
1257 ? "!isgreaterequal"
1258 : "!__builtin_isgreaterequal");
1259 goto two_args_fun;
1261 case UNLE_EXPR:
1262 pp_c_identifier (pp, flag_isoc99
1263 ? "!isgreater"
1264 : "!__builtin_isgreater");
1265 goto two_args_fun;
1267 case UNGT_EXPR:
1268 pp_c_identifier (pp, flag_isoc99
1269 ? "!islessequal"
1270 : "!__builtin_islessequal");
1271 goto two_args_fun;
1273 case UNGE_EXPR:
1274 pp_c_identifier (pp, flag_isoc99
1275 ? "!isless"
1276 : "!__builtin_isless");
1277 goto two_args_fun;
1279 case UNEQ_EXPR:
1280 pp_c_identifier (pp, flag_isoc99
1281 ? "!islessgreater"
1282 : "!__builtin_islessgreater");
1283 goto two_args_fun;
1285 case LTGT_EXPR:
1286 pp_c_identifier (pp, flag_isoc99
1287 ? "islessgreater"
1288 : "__builtin_islessgreater");
1289 goto two_args_fun;
1291 two_args_fun:
1292 pp_c_left_paren (pp);
1293 pp_expression (pp, TREE_OPERAND (e, 0));
1294 pp_separate_with (pp, ',');
1295 pp_expression (pp, TREE_OPERAND (e, 1));
1296 pp_c_right_paren (pp);
1297 break;
1299 case ABS_EXPR:
1300 pp_c_identifier (pp, "__builtin_abs");
1301 pp_c_left_paren (pp);
1302 pp_expression (pp, TREE_OPERAND (e, 0));
1303 pp_c_right_paren (pp);
1304 break;
1306 case COMPONENT_REF:
1308 tree object = TREE_OPERAND (e, 0);
1309 if (TREE_CODE (object) == INDIRECT_REF)
1311 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1312 pp_c_arrow (pp);
1314 else
1316 pp_postfix_expression (pp, object);
1317 pp_c_dot (pp);
1319 pp_expression (pp, TREE_OPERAND (e, 1));
1321 break;
1323 case COMPLEX_CST:
1324 case VECTOR_CST:
1325 case COMPLEX_EXPR:
1326 pp_c_compound_literal (pp, e);
1327 break;
1329 case COMPOUND_LITERAL_EXPR:
1330 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1331 /* Fall through. */
1332 case CONSTRUCTOR:
1333 pp_initializer (pp, e);
1334 break;
1336 case VA_ARG_EXPR:
1337 pp_c_identifier (pp, "__builtin_va_arg");
1338 pp_c_left_paren (pp);
1339 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1340 pp_separate_with (pp, ',');
1341 pp_type_id (pp, TREE_TYPE (e));
1342 pp_c_right_paren (pp);
1343 break;
1345 case ADDR_EXPR:
1346 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1348 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1349 break;
1351 /* else fall through. */
1353 default:
1354 pp_primary_expression (pp, e);
1355 break;
1359 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1361 void
1362 pp_c_expression_list (c_pretty_printer *pp, tree e)
1364 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1366 pp_expression (pp, TREE_VALUE (e));
1367 if (TREE_CHAIN (e))
1368 pp_separate_with (pp, ',');
1372 /* Print out an expression-list in parens, as in a function call. */
1374 void
1375 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1377 pp_c_left_paren (pp);
1378 if (t && TREE_CODE (t) == TREE_LIST)
1379 pp_c_expression_list (pp, t);
1380 pp_c_right_paren (pp);
1383 /* unary-expression:
1384 postfix-expression
1385 ++ cast-expression
1386 -- cast-expression
1387 unary-operator cast-expression
1388 sizeof unary-expression
1389 sizeof ( type-id )
1391 unary-operator: one of
1392 * & + - ! ~
1394 GNU extensions.
1395 unary-expression:
1396 __alignof__ unary-expression
1397 __alignof__ ( type-id )
1398 __real__ unary-expression
1399 __imag__ unary-expression */
1401 void
1402 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1404 enum tree_code code = TREE_CODE (e);
1405 switch (code)
1407 case PREINCREMENT_EXPR:
1408 case PREDECREMENT_EXPR:
1409 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1410 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1411 break;
1413 case ADDR_EXPR:
1414 case INDIRECT_REF:
1415 case NEGATE_EXPR:
1416 case BIT_NOT_EXPR:
1417 case TRUTH_NOT_EXPR:
1418 case CONJ_EXPR:
1419 /* String literal are used by address. */
1420 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1421 pp_ampersand (pp);
1422 else if (code == INDIRECT_REF)
1423 pp_c_star (pp);
1424 else if (code == NEGATE_EXPR)
1425 pp_minus (pp);
1426 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1427 pp_complement (pp);
1428 else if (code == TRUTH_NOT_EXPR)
1429 pp_exclamation (pp);
1430 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1431 break;
1433 case SIZEOF_EXPR:
1434 case ALIGNOF_EXPR:
1435 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1436 pp_c_whitespace (pp);
1437 if (TYPE_P (TREE_OPERAND (e, 0)))
1438 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1439 else
1440 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1441 break;
1443 case REALPART_EXPR:
1444 case IMAGPART_EXPR:
1445 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1446 pp_c_whitespace (pp);
1447 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1448 break;
1450 default:
1451 pp_postfix_expression (pp, e);
1452 break;
1456 /* cast-expression:
1457 unary-expression
1458 ( type-name ) cast-expression */
1460 void
1461 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1463 switch (TREE_CODE (e))
1465 case FLOAT_EXPR:
1466 case FIX_TRUNC_EXPR:
1467 case CONVERT_EXPR:
1468 pp_c_type_cast (pp, TREE_TYPE (e));
1469 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1470 break;
1472 default:
1473 pp_unary_expression (pp, e);
1477 /* multiplicative-expression:
1478 cast-expression
1479 multiplicative-expression * cast-expression
1480 multiplicative-expression / cast-expression
1481 multiplicative-expression % cast-expression */
1483 static void
1484 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1486 enum tree_code code = TREE_CODE (e);
1487 switch (code)
1489 case MULT_EXPR:
1490 case TRUNC_DIV_EXPR:
1491 case TRUNC_MOD_EXPR:
1492 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1493 pp_c_whitespace (pp);
1494 if (code == MULT_EXPR)
1495 pp_c_star (pp);
1496 else if (code == TRUNC_DIV_EXPR)
1497 pp_slash (pp);
1498 else
1499 pp_modulo (pp);
1500 pp_c_whitespace (pp);
1501 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1502 break;
1504 default:
1505 pp_c_cast_expression (pp, e);
1506 break;
1510 /* additive-expression:
1511 multiplicative-expression
1512 additive-expression + multiplicative-expression
1513 additive-expression - multiplicative-expression */
1515 static void
1516 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1518 enum tree_code code = TREE_CODE (e);
1519 switch (code)
1521 case PLUS_EXPR:
1522 case MINUS_EXPR:
1523 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1524 pp_c_whitespace (pp);
1525 if (code == PLUS_EXPR)
1526 pp_plus (pp);
1527 else
1528 pp_minus (pp);
1529 pp_c_whitespace (pp);
1530 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1531 break;
1533 default:
1534 pp_multiplicative_expression (pp, e);
1535 break;
1539 /* additive-expression:
1540 additive-expression
1541 shift-expression << additive-expression
1542 shift-expression >> additive-expression */
1544 static void
1545 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1547 enum tree_code code = TREE_CODE (e);
1548 switch (code)
1550 case LSHIFT_EXPR:
1551 case RSHIFT_EXPR:
1552 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1553 pp_c_whitespace (pp);
1554 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1555 pp_c_whitespace (pp);
1556 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1557 break;
1559 default:
1560 pp_c_additive_expression (pp, e);
1564 /* relational-expression:
1565 shift-expression
1566 relational-expression < shift-expression
1567 relational-expression > shift-expression
1568 relational-expression <= shift-expression
1569 relational-expression >= shift-expression */
1571 static void
1572 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1574 enum tree_code code = TREE_CODE (e);
1575 switch (code)
1577 case LT_EXPR:
1578 case GT_EXPR:
1579 case LE_EXPR:
1580 case GE_EXPR:
1581 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1582 pp_c_whitespace (pp);
1583 if (code == LT_EXPR)
1584 pp_less (pp);
1585 else if (code == GT_EXPR)
1586 pp_greater (pp);
1587 else if (code == LE_EXPR)
1588 pp_identifier (pp, "<=");
1589 else if (code == GE_EXPR)
1590 pp_identifier (pp, ">=");
1591 pp_c_whitespace (pp);
1592 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1593 break;
1595 default:
1596 pp_c_shift_expression (pp, e);
1597 break;
1601 /* equality-expression:
1602 relational-expression
1603 equality-expression == relational-expression
1604 equality-equality != relational-expression */
1606 static void
1607 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1609 enum tree_code code = TREE_CODE (e);
1610 switch (code)
1612 case EQ_EXPR:
1613 case NE_EXPR:
1614 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1615 pp_c_whitespace (pp);
1616 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1617 pp_c_whitespace (pp);
1618 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1619 break;
1621 default:
1622 pp_c_relational_expression (pp, e);
1623 break;
1627 /* AND-expression:
1628 equality-expression
1629 AND-expression & equality-equality */
1631 static void
1632 pp_c_and_expression (c_pretty_printer *pp, tree e)
1634 if (TREE_CODE (e) == BIT_AND_EXPR)
1636 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1637 pp_c_whitespace (pp);
1638 pp_ampersand (pp);
1639 pp_c_whitespace (pp);
1640 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1642 else
1643 pp_c_equality_expression (pp, e);
1646 /* exclusive-OR-expression:
1647 AND-expression
1648 exclusive-OR-expression ^ AND-expression */
1650 static void
1651 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1653 if (TREE_CODE (e) == BIT_XOR_EXPR)
1655 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1656 pp_c_maybe_whitespace (pp);
1657 pp_carret (pp);
1658 pp_c_whitespace (pp);
1659 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1661 else
1662 pp_c_and_expression (pp, e);
1665 /* inclusive-OR-expression:
1666 exclusive-OR-expression
1667 inclusive-OR-expression | exclusive-OR-expression */
1669 static void
1670 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1672 if (TREE_CODE (e) == BIT_IOR_EXPR)
1674 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1675 pp_c_whitespace (pp);
1676 pp_bar (pp);
1677 pp_c_whitespace (pp);
1678 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1680 else
1681 pp_c_exclusive_or_expression (pp, e);
1684 /* logical-AND-expression:
1685 inclusive-OR-expression
1686 logical-AND-expression && inclusive-OR-expression */
1688 static void
1689 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1691 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1693 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1694 pp_c_whitespace (pp);
1695 pp_identifier (pp, "&&");
1696 pp_c_whitespace (pp);
1697 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1699 else
1700 pp_c_inclusive_or_expression (pp, e);
1703 /* logical-OR-expression:
1704 logical-AND-expression
1705 logical-OR-expression || logical-AND-expression */
1707 void
1708 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1710 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1712 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1713 pp_c_whitespace (pp);
1714 pp_identifier (pp, "||");
1715 pp_c_whitespace (pp);
1716 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1718 else
1719 pp_c_logical_and_expression (pp, e);
1722 /* conditional-expression:
1723 logical-OR-expression
1724 logical-OR-expression ? expression : conditional-expression */
1726 static void
1727 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1729 if (TREE_CODE (e) == COND_EXPR)
1731 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1732 pp_c_whitespace (pp);
1733 pp_question (pp);
1734 pp_c_whitespace (pp);
1735 pp_expression (pp, TREE_OPERAND (e, 1));
1736 pp_c_whitespace (pp);
1737 pp_colon (pp);
1738 pp_c_whitespace (pp);
1739 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1741 else
1742 pp_c_logical_or_expression (pp, e);
1746 /* assignment-expression:
1747 conditional-expression
1748 unary-expression assignment-operator assignment-expression
1750 assignment-expression: one of
1751 = *= /= %= += -= >>= <<= &= ^= |= */
1753 static void
1754 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1756 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1758 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1759 pp_c_whitespace (pp);
1760 pp_equal (pp);
1761 pp_space (pp);
1762 pp_c_expression (pp, TREE_OPERAND (e, 1));
1764 else
1765 pp_c_conditional_expression (pp, e);
1768 /* expression:
1769 assignment-expression
1770 expression , assignment-expression
1772 Implementation note: instead of going through the usual recursion
1773 chain, I take the liberty of dispatching nodes to the appropriate
1774 functions. This makes some redundancy, but it worths it. That also
1775 prevents a possible infinite recursion between pp_c_primary_expression ()
1776 and pp_c_expression (). */
1778 void
1779 pp_c_expression (c_pretty_printer *pp, tree e)
1781 switch (TREE_CODE (e))
1783 case INTEGER_CST:
1784 pp_c_integer_constant (pp, e);
1785 break;
1787 case REAL_CST:
1788 pp_c_floating_constant (pp, e);
1789 break;
1791 case STRING_CST:
1792 pp_c_string_literal (pp, e);
1793 break;
1795 case IDENTIFIER_NODE:
1796 case FUNCTION_DECL:
1797 case VAR_DECL:
1798 case CONST_DECL:
1799 case PARM_DECL:
1800 case RESULT_DECL:
1801 case FIELD_DECL:
1802 case LABEL_DECL:
1803 case ERROR_MARK:
1804 case STMT_EXPR:
1805 pp_primary_expression (pp, e);
1806 break;
1808 case POSTINCREMENT_EXPR:
1809 case POSTDECREMENT_EXPR:
1810 case ARROW_EXPR:
1811 case ARRAY_REF:
1812 case CALL_EXPR:
1813 case COMPONENT_REF:
1814 case COMPLEX_CST:
1815 case COMPLEX_EXPR:
1816 case VECTOR_CST:
1817 case ORDERED_EXPR:
1818 case UNORDERED_EXPR:
1819 case LTGT_EXPR:
1820 case UNEQ_EXPR:
1821 case UNLE_EXPR:
1822 case UNLT_EXPR:
1823 case UNGE_EXPR:
1824 case UNGT_EXPR:
1825 case ABS_EXPR:
1826 case CONSTRUCTOR:
1827 case COMPOUND_LITERAL_EXPR:
1828 case VA_ARG_EXPR:
1829 pp_postfix_expression (pp, e);
1830 break;
1832 case CONJ_EXPR:
1833 case ADDR_EXPR:
1834 case INDIRECT_REF:
1835 case NEGATE_EXPR:
1836 case BIT_NOT_EXPR:
1837 case TRUTH_NOT_EXPR:
1838 case PREINCREMENT_EXPR:
1839 case PREDECREMENT_EXPR:
1840 case SIZEOF_EXPR:
1841 case ALIGNOF_EXPR:
1842 case REALPART_EXPR:
1843 case IMAGPART_EXPR:
1844 pp_c_unary_expression (pp, e);
1845 break;
1847 case FLOAT_EXPR:
1848 case FIX_TRUNC_EXPR:
1849 case CONVERT_EXPR:
1850 pp_c_cast_expression (pp, e);
1851 break;
1853 case MULT_EXPR:
1854 case TRUNC_MOD_EXPR:
1855 case TRUNC_DIV_EXPR:
1856 pp_multiplicative_expression (pp, e);
1857 break;
1859 case LSHIFT_EXPR:
1860 case RSHIFT_EXPR:
1861 pp_c_shift_expression (pp, e);
1862 break;
1864 case LT_EXPR:
1865 case GT_EXPR:
1866 case LE_EXPR:
1867 case GE_EXPR:
1868 pp_c_relational_expression (pp, e);
1869 break;
1871 case BIT_AND_EXPR:
1872 pp_c_and_expression (pp, e);
1873 break;
1875 case BIT_XOR_EXPR:
1876 pp_c_exclusive_or_expression (pp, e);
1877 break;
1879 case BIT_IOR_EXPR:
1880 pp_c_inclusive_or_expression (pp, e);
1881 break;
1883 case TRUTH_ANDIF_EXPR:
1884 pp_c_logical_and_expression (pp, e);
1885 break;
1887 case TRUTH_ORIF_EXPR:
1888 pp_c_logical_or_expression (pp, e);
1889 break;
1891 case EQ_EXPR:
1892 case NE_EXPR:
1893 pp_c_equality_expression (pp, e);
1894 break;
1896 case COND_EXPR:
1897 pp_conditional_expression (pp, e);
1898 break;
1900 case PLUS_EXPR:
1901 case MINUS_EXPR:
1902 pp_c_additive_expression (pp, e);
1903 break;
1905 case MODIFY_EXPR:
1906 case INIT_EXPR:
1907 pp_assignment_expression (pp, e);
1908 break;
1910 case COMPOUND_EXPR:
1911 pp_c_left_paren (pp);
1912 pp_expression (pp, TREE_OPERAND (e, 0));
1913 pp_separate_with (pp, ',');
1914 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1915 pp_c_right_paren (pp);
1916 break;
1918 case NOP_EXPR:
1919 case NON_LVALUE_EXPR:
1920 case SAVE_EXPR:
1921 pp_expression (pp, TREE_OPERAND (e, 0));
1922 break;
1924 case TARGET_EXPR:
1925 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1926 break;
1928 default:
1929 pp_unsupported_tree (pp, e);
1930 break;
1936 /* Statements. */
1938 /* statement:
1939 labeled-statement
1940 compound-statement
1941 expression-statement
1942 selection-statement
1943 iteration-statement
1944 jump-statement */
1946 void
1947 pp_c_statement (c_pretty_printer *pp, tree stmt)
1949 enum tree_code code;
1951 if (stmt == NULL)
1952 return;
1954 if (pp_needs_newline (pp))
1955 pp_newline_and_indent (pp, 0);
1957 code = TREE_CODE (stmt);
1958 switch (code)
1960 /* expression-statement:
1961 expression(opt) ; */
1962 case EXPR_STMT:
1963 pp_expression (pp, EXPR_STMT_EXPR (stmt));
1964 pp_c_semicolon (pp);
1965 pp_needs_newline (pp) = true;
1966 break;
1968 case SWITCH_STMT:
1969 pp_c_identifier (pp, "switch");
1970 pp_space (pp);
1971 pp_c_left_paren (pp);
1972 pp_expression (pp, SWITCH_COND (stmt));
1973 pp_c_right_paren (pp);
1974 pp_indentation (pp) += 3;
1975 pp_needs_newline (pp) = true;
1976 pp_statement (pp, SWITCH_BODY (stmt));
1977 pp_newline_and_indent (pp, -3);
1978 break;
1980 /* iteration-statement:
1981 while ( expression ) statement
1982 do statement while ( expression ) ;
1983 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1984 for ( declaration expression(opt) ; expression(opt) ) statement */
1985 case WHILE_STMT:
1986 pp_c_identifier (pp, "while");
1987 pp_space (pp);
1988 pp_c_left_paren (pp);
1989 pp_expression (pp, WHILE_COND (stmt));
1990 pp_c_right_paren (pp);
1991 pp_newline_and_indent (pp, 3);
1992 pp_statement (pp, WHILE_BODY (stmt));
1993 pp_indentation (pp) -= 3;
1994 pp_needs_newline (pp) = true;
1995 break;
1997 case DO_STMT:
1998 pp_c_identifier (pp, "do");
1999 pp_newline_and_indent (pp, 3);
2000 pp_statement (pp, DO_BODY (stmt));
2001 pp_newline_and_indent (pp, -3);
2002 pp_c_identifier (pp, "while");
2003 pp_space (pp);
2004 pp_c_left_paren (pp);
2005 pp_expression (pp, DO_COND (stmt));
2006 pp_c_right_paren (pp);
2007 pp_c_semicolon (pp);
2008 pp_needs_newline (pp) = true;
2009 break;
2011 case FOR_STMT:
2012 pp_c_identifier (pp, "for");
2013 pp_space (pp);
2014 pp_c_left_paren (pp);
2015 if (FOR_INIT_STMT (stmt))
2016 pp_statement (pp, FOR_INIT_STMT (stmt));
2017 else
2018 pp_c_semicolon (pp);
2019 pp_needs_newline (pp) = false;
2020 pp_c_whitespace (pp);
2021 if (FOR_COND (stmt))
2022 pp_expression (pp, FOR_COND (stmt));
2023 pp_c_semicolon (pp);
2024 pp_needs_newline (pp) = false;
2025 pp_c_whitespace (pp);
2026 if (FOR_EXPR (stmt))
2027 pp_expression (pp, FOR_EXPR (stmt));
2028 pp_c_right_paren (pp);
2029 pp_newline_and_indent (pp, 3);
2030 pp_statement (pp, FOR_BODY (stmt));
2031 pp_indentation (pp) -= 3;
2032 pp_needs_newline (pp) = true;
2033 break;
2035 /* jump-statement:
2036 goto identifier;
2037 continue ;
2038 return expression(opt) ; */
2039 case BREAK_STMT:
2040 case CONTINUE_STMT:
2041 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
2042 pp_c_semicolon (pp);
2043 pp_needs_newline (pp) = true;
2044 break;
2046 default:
2047 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2048 break;
2053 /* Initialize the PRETTY-PRINTER for handling C codes. */
2055 void
2056 pp_c_pretty_printer_init (c_pretty_printer *pp)
2058 pp->offset_list = 0;
2060 pp->declaration = pp_c_declaration;
2061 pp->declaration_specifiers = pp_c_declaration_specifiers;
2062 pp->declarator = pp_c_declarator;
2063 pp->direct_declarator = pp_c_direct_declarator;
2064 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2065 pp->abstract_declarator = pp_c_abstract_declarator;
2066 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2067 pp->ptr_operator = pp_c_pointer;
2068 pp->parameter_list = pp_c_parameter_type_list;
2069 pp->type_id = pp_c_type_id;
2070 pp->simple_type_specifier = pp_c_type_specifier;
2071 pp->function_specifier = pp_c_function_specifier;
2072 pp->storage_class_specifier = pp_c_storage_class_specifier;
2074 pp->statement = pp_c_statement;
2076 pp->id_expression = pp_c_id_expression;
2077 pp->primary_expression = pp_c_primary_expression;
2078 pp->postfix_expression = pp_c_postfix_expression;
2079 pp->unary_expression = pp_c_unary_expression;
2080 pp->initializer = pp_c_initializer;
2081 pp->multiplicative_expression = pp_c_multiplicative_expression;
2082 pp->conditional_expression = pp_c_conditional_expression;
2083 pp->assignment_expression = pp_c_assignment_expression;
2084 pp->expression = pp_c_expression;
2088 /* Print the tree T in full, on file FILE. */
2090 void
2091 print_c_tree (FILE *file, tree t)
2093 static c_pretty_printer pp_rec;
2094 static bool initialized = 0;
2095 c_pretty_printer *pp = &pp_rec;
2097 if (!initialized)
2099 initialized = 1;
2100 pp_construct (pp_base (pp), NULL, 0);
2101 pp_c_pretty_printer_init (pp);
2102 pp_needs_newline (pp) = true;
2104 pp_base (pp)->buffer->stream = file;
2106 pp_statement (pp, t);
2108 pp_newline (pp);
2109 pp_flush (pp);
2112 /* Print the tree T in full, on stderr. */
2114 void
2115 debug_c_tree (tree t)
2117 print_c_tree (stderr, t);
2118 fputc ('\n', stderr);
2121 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2122 up of T's memory address. */
2124 void
2125 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2127 const char *name;
2129 gcc_assert (DECL_P (t));
2131 if (DECL_NAME (t))
2132 name = IDENTIFIER_POINTER (DECL_NAME (t));
2133 else
2135 static char xname[8];
2136 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2137 name = xname;
2140 pp_c_identifier (pp, name);