Fix typo in ChangeLog entry date.
[official-gcc.git] / gcc / c-pretty-print.c
blobf9694bfc080c81993ae0341f7ec22336ef541e80
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "real.h"
27 #include "fixed-value.h"
28 #include "intl.h"
29 #include "c-pretty-print.h"
30 #include "c-tree.h"
31 #include "tree-iterator.h"
32 #include "diagnostic.h"
34 /* Translate if being used for diagnostics, but not for dump files or
35 __PRETTY_FUNCTION. */
36 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
38 /* The pretty-printer code is primarily designed to closely follow
39 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
40 codes we used to have in the past. Following a structured
41 approach (preferably the official grammars) is believed to make it
42 much easier to add extensions and nifty pretty-printing effects that
43 takes expression or declaration contexts into account. */
46 #define pp_c_maybe_whitespace(PP) \
47 do { \
48 if (pp_base (PP)->padding == pp_before) \
49 pp_c_whitespace (PP); \
50 } while (0)
52 /* literal */
53 static void pp_c_char (c_pretty_printer *, int);
55 /* postfix-expression */
56 static void pp_c_initializer_list (c_pretty_printer *, tree);
57 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
59 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
60 static void pp_c_additive_expression (c_pretty_printer *, tree);
61 static void pp_c_shift_expression (c_pretty_printer *, tree);
62 static void pp_c_relational_expression (c_pretty_printer *, tree);
63 static void pp_c_equality_expression (c_pretty_printer *, tree);
64 static void pp_c_and_expression (c_pretty_printer *, tree);
65 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
66 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
67 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
68 static void pp_c_conditional_expression (c_pretty_printer *, tree);
69 static void pp_c_assignment_expression (c_pretty_printer *, tree);
71 /* declarations. */
74 /* Helper functions. */
76 void
77 pp_c_whitespace (c_pretty_printer *pp)
79 pp_space (pp);
80 pp_base (pp)->padding = pp_none;
83 void
84 pp_c_left_paren (c_pretty_printer *pp)
86 pp_left_paren (pp);
87 pp_base (pp)->padding = pp_none;
90 void
91 pp_c_right_paren (c_pretty_printer *pp)
93 pp_right_paren (pp);
94 pp_base (pp)->padding = pp_none;
97 void
98 pp_c_left_brace (c_pretty_printer *pp)
100 pp_left_brace (pp);
101 pp_base (pp)->padding = pp_none;
104 void
105 pp_c_right_brace (c_pretty_printer *pp)
107 pp_right_brace (pp);
108 pp_base (pp)->padding = pp_none;
111 void
112 pp_c_left_bracket (c_pretty_printer *pp)
114 pp_left_bracket (pp);
115 pp_base (pp)->padding = pp_none;
118 void
119 pp_c_right_bracket (c_pretty_printer *pp)
121 pp_right_bracket (pp);
122 pp_base (pp)->padding = pp_none;
125 void
126 pp_c_dot (c_pretty_printer *pp)
128 pp_dot (pp);
129 pp_base (pp)->padding = pp_none;
132 void
133 pp_c_ampersand (c_pretty_printer *pp)
135 pp_ampersand (pp);
136 pp_base (pp)->padding = pp_none;
139 void
140 pp_c_star (c_pretty_printer *pp)
142 pp_star (pp);
143 pp_base (pp)->padding = pp_none;
146 void
147 pp_c_arrow (c_pretty_printer *pp)
149 pp_arrow (pp);
150 pp_base (pp)->padding = pp_none;
153 void
154 pp_c_semicolon (c_pretty_printer *pp)
156 pp_semicolon (pp);
157 pp_base (pp)->padding = pp_none;
160 void
161 pp_c_complement (c_pretty_printer *pp)
163 pp_complement (pp);
164 pp_base (pp)->padding = pp_none;
167 void
168 pp_c_exclamation (c_pretty_printer *pp)
170 pp_exclamation (pp);
171 pp_base (pp)->padding = pp_none;
174 /* Print out the external representation of CV-QUALIFIER. */
176 static void
177 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
179 const char *p = pp_last_position_in_text (pp);
180 /* The C programming language does not have references, but it is much
181 simpler to handle those here rather than going through the same
182 logic in the C++ pretty-printer. */
183 if (p != NULL && (*p == '*' || *p == '&'))
184 pp_c_whitespace (pp);
185 pp_c_ws_string (pp, cv);
188 /* Pretty-print T using the type-cast notation '( type-name )'. */
190 static void
191 pp_c_type_cast (c_pretty_printer *pp, tree t)
193 pp_c_left_paren (pp);
194 pp_type_id (pp, t);
195 pp_c_right_paren (pp);
198 /* We're about to pretty-print a pointer type as indicated by T.
199 Output a whitespace, if needed, preparing for subsequent output. */
201 void
202 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
204 if (POINTER_TYPE_P (t))
206 tree pointee = strip_pointer_operator (TREE_TYPE (t));
207 if (TREE_CODE (pointee) != ARRAY_TYPE
208 && TREE_CODE (pointee) != FUNCTION_TYPE)
209 pp_c_whitespace (pp);
214 /* Declarations. */
216 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
217 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
218 of its type. Take care of possible extensions.
220 type-qualifier-list:
221 type-qualifier
222 type-qualifier-list type-qualifier
224 type-qualifier:
225 const
226 restrict -- C99
227 __restrict__ -- GNU C
228 volatile */
230 void
231 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
233 int qualifiers;
235 if (!t || t == error_mark_node)
236 return;
238 if (!TYPE_P (t))
239 t = TREE_TYPE (t);
241 qualifiers = TYPE_QUALS (t);
242 if (qualifiers & TYPE_QUAL_CONST)
243 pp_c_cv_qualifier (pp, "const");
244 if (qualifiers & TYPE_QUAL_VOLATILE)
245 pp_c_cv_qualifier (pp, "volatile");
246 if (qualifiers & TYPE_QUAL_RESTRICT)
247 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
250 /* pointer:
251 * type-qualifier-list(opt)
252 * type-qualifier-list(opt) pointer */
254 static void
255 pp_c_pointer (c_pretty_printer *pp, tree t)
257 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
258 t = TREE_TYPE (t);
259 switch (TREE_CODE (t))
261 case POINTER_TYPE:
262 /* It is easier to handle C++ reference types here. */
263 case REFERENCE_TYPE:
264 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
265 pp_c_pointer (pp, TREE_TYPE (t));
266 if (TREE_CODE (t) == POINTER_TYPE)
267 pp_c_star (pp);
268 else
269 pp_c_ampersand (pp);
270 pp_c_type_qualifier_list (pp, t);
271 break;
273 /* ??? This node is now in GENERIC and so shouldn't be here. But
274 we'll fix that later. */
275 case DECL_EXPR:
276 pp_declaration (pp, DECL_EXPR_DECL (t));
277 pp_needs_newline (pp) = true;
278 break;
280 default:
281 pp_unsupported_tree (pp, t);
285 /* type-specifier:
286 void
287 char
288 short
290 long
291 float
292 double
293 signed
294 unsigned
295 _Bool -- C99
296 _Complex -- C99
297 _Imaginary -- C99
298 struct-or-union-specifier
299 enum-specifier
300 typedef-name.
302 GNU extensions.
303 simple-type-specifier:
304 __complex__
305 __vector__ */
307 void
308 pp_c_type_specifier (c_pretty_printer *pp, tree t)
310 const enum tree_code code = TREE_CODE (t);
311 switch (code)
313 case ERROR_MARK:
314 pp_c_ws_string (pp, M_("<type-error>"));
315 break;
317 case IDENTIFIER_NODE:
318 pp_c_tree_decl_identifier (pp, t);
319 break;
321 case VOID_TYPE:
322 case BOOLEAN_TYPE:
323 case INTEGER_TYPE:
324 case REAL_TYPE:
325 case FIXED_POINT_TYPE:
326 if (TYPE_NAME (t))
328 t = TYPE_NAME (t);
329 pp_c_type_specifier (pp, t);
331 else
333 int prec = TYPE_PRECISION (t);
334 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
335 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
336 else
337 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
338 if (TYPE_NAME (t))
340 pp_c_type_specifier (pp, t);
341 if (TYPE_PRECISION (t) != prec)
343 pp_string (pp, ":");
344 pp_decimal_int (pp, prec);
347 else
349 switch (code)
351 case INTEGER_TYPE:
352 pp_string (pp, (TYPE_UNSIGNED (t)
353 ? M_("<unnamed-unsigned:")
354 : M_("<unnamed-signed:")));
355 break;
356 case REAL_TYPE:
357 pp_string (pp, M_("<unnamed-float:"));
358 break;
359 case FIXED_POINT_TYPE:
360 pp_string (pp, M_("<unnamed-fixed:"));
361 break;
362 default:
363 gcc_unreachable ();
365 pp_decimal_int (pp, prec);
366 pp_string (pp, ">");
369 break;
371 case TYPE_DECL:
372 if (DECL_NAME (t))
373 pp_id_expression (pp, t);
374 else
375 pp_c_ws_string (pp, M_("<typedef-error>"));
376 break;
378 case UNION_TYPE:
379 case RECORD_TYPE:
380 case ENUMERAL_TYPE:
381 if (code == UNION_TYPE)
382 pp_c_ws_string (pp, "union");
383 else if (code == RECORD_TYPE)
384 pp_c_ws_string (pp, "struct");
385 else if (code == ENUMERAL_TYPE)
386 pp_c_ws_string (pp, "enum");
387 else
388 pp_c_ws_string (pp, M_("<tag-error>"));
390 if (TYPE_NAME (t))
391 pp_id_expression (pp, TYPE_NAME (t));
392 else
393 pp_c_ws_string (pp, M_("<anonymous>"));
394 break;
396 default:
397 pp_unsupported_tree (pp, t);
398 break;
402 /* specifier-qualifier-list:
403 type-specifier specifier-qualifier-list-opt
404 type-qualifier specifier-qualifier-list-opt
407 Implementation note: Because of the non-linearities in array or
408 function declarations, this routine prints not just the
409 specifier-qualifier-list of such entities or types of such entities,
410 but also the 'pointer' production part of their declarators. The
411 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
413 void
414 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
416 const enum tree_code code = TREE_CODE (t);
418 if (TREE_CODE (t) != POINTER_TYPE)
419 pp_c_type_qualifier_list (pp, t);
420 switch (code)
422 case REFERENCE_TYPE:
423 case POINTER_TYPE:
425 /* Get the types-specifier of this type. */
426 tree pointee = strip_pointer_operator (TREE_TYPE (t));
427 pp_c_specifier_qualifier_list (pp, pointee);
428 if (TREE_CODE (pointee) == ARRAY_TYPE
429 || TREE_CODE (pointee) == FUNCTION_TYPE)
431 pp_c_whitespace (pp);
432 pp_c_left_paren (pp);
434 else if (!c_dialect_cxx ())
435 pp_c_whitespace (pp);
436 pp_ptr_operator (pp, t);
438 break;
440 case FUNCTION_TYPE:
441 case ARRAY_TYPE:
442 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
443 break;
445 case VECTOR_TYPE:
446 case COMPLEX_TYPE:
447 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
448 if (code == COMPLEX_TYPE)
449 pp_c_ws_string (pp, flag_isoc99 ? "_Complex" : "__complex__");
450 else if (code == VECTOR_TYPE)
451 pp_c_ws_string (pp, "__vector__");
452 break;
454 default:
455 pp_simple_type_specifier (pp, t);
456 break;
460 /* parameter-type-list:
461 parameter-list
462 parameter-list , ...
464 parameter-list:
465 parameter-declaration
466 parameter-list , parameter-declaration
468 parameter-declaration:
469 declaration-specifiers declarator
470 declaration-specifiers abstract-declarator(opt) */
472 void
473 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
475 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
476 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
477 pp_c_left_paren (pp);
478 if (parms == void_list_node)
479 pp_c_ws_string (pp, "void");
480 else
482 bool first = true;
483 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
485 if (!first)
486 pp_separate_with (pp, ',');
487 first = false;
488 pp_declaration_specifiers
489 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
490 if (want_parm_decl)
491 pp_declarator (pp, parms);
492 else
493 pp_abstract_declarator (pp, TREE_VALUE (parms));
496 pp_c_right_paren (pp);
499 /* abstract-declarator:
500 pointer
501 pointer(opt) direct-abstract-declarator */
503 static void
504 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
506 if (TREE_CODE (t) == POINTER_TYPE)
508 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
509 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
510 pp_c_right_paren (pp);
511 t = TREE_TYPE (t);
514 pp_direct_abstract_declarator (pp, t);
517 /* direct-abstract-declarator:
518 ( abstract-declarator )
519 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
520 direct-abstract-declarator(opt) [ * ]
521 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
523 void
524 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
526 switch (TREE_CODE (t))
528 case POINTER_TYPE:
529 pp_abstract_declarator (pp, t);
530 break;
532 case FUNCTION_TYPE:
533 pp_c_parameter_type_list (pp, t);
534 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
535 break;
537 case ARRAY_TYPE:
538 pp_c_left_bracket (pp);
539 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
541 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
542 tree type = TREE_TYPE (maxval);
544 if (host_integerp (maxval, 0))
545 pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
546 else
547 pp_expression (pp, fold_build2 (PLUS_EXPR, type, maxval,
548 build_int_cst (type, 1)));
550 pp_c_right_bracket (pp);
551 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
552 break;
554 case IDENTIFIER_NODE:
555 case VOID_TYPE:
556 case BOOLEAN_TYPE:
557 case INTEGER_TYPE:
558 case REAL_TYPE:
559 case FIXED_POINT_TYPE:
560 case ENUMERAL_TYPE:
561 case RECORD_TYPE:
562 case UNION_TYPE:
563 case VECTOR_TYPE:
564 case COMPLEX_TYPE:
565 case TYPE_DECL:
566 break;
568 default:
569 pp_unsupported_tree (pp, t);
570 break;
574 /* type-name:
575 specifier-qualifier-list abstract-declarator(opt) */
577 void
578 pp_c_type_id (c_pretty_printer *pp, tree t)
580 pp_c_specifier_qualifier_list (pp, t);
581 pp_abstract_declarator (pp, t);
584 /* storage-class-specifier:
585 typedef
586 extern
587 static
588 auto
589 register */
591 void
592 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
594 if (TREE_CODE (t) == TYPE_DECL)
595 pp_c_ws_string (pp, "typedef");
596 else if (DECL_P (t))
598 if (DECL_REGISTER (t))
599 pp_c_ws_string (pp, "register");
600 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
601 pp_c_ws_string (pp, "static");
605 /* function-specifier:
606 inline */
608 void
609 pp_c_function_specifier (c_pretty_printer *pp, tree t)
611 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
612 pp_c_ws_string (pp, "inline");
615 /* declaration-specifiers:
616 storage-class-specifier declaration-specifiers(opt)
617 type-specifier declaration-specifiers(opt)
618 type-qualifier declaration-specifiers(opt)
619 function-specifier declaration-specifiers(opt) */
621 void
622 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
624 pp_storage_class_specifier (pp, t);
625 pp_function_specifier (pp, t);
626 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
629 /* direct-declarator
630 identifier
631 ( declarator )
632 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
633 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
634 direct-declarator [ type-qualifier-list static assignment-expression ]
635 direct-declarator [ type-qualifier-list * ]
636 direct-declarator ( parameter-type-list )
637 direct-declarator ( identifier-list(opt) ) */
639 void
640 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
642 switch (TREE_CODE (t))
644 case VAR_DECL:
645 case PARM_DECL:
646 case TYPE_DECL:
647 case FIELD_DECL:
648 case LABEL_DECL:
649 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
650 pp_c_tree_decl_identifier (pp, t);
651 break;
653 case ARRAY_TYPE:
654 case POINTER_TYPE:
655 pp_abstract_declarator (pp, TREE_TYPE (t));
656 break;
658 case FUNCTION_TYPE:
659 pp_parameter_list (pp, t);
660 pp_abstract_declarator (pp, TREE_TYPE (t));
661 break;
663 case FUNCTION_DECL:
664 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
665 pp_c_tree_decl_identifier (pp, t);
666 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
667 pp_abstract_declarator (pp, TREE_TYPE (t));
668 else
670 pp_parameter_list (pp, t);
671 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
673 break;
675 case INTEGER_TYPE:
676 case REAL_TYPE:
677 case FIXED_POINT_TYPE:
678 case ENUMERAL_TYPE:
679 case UNION_TYPE:
680 case RECORD_TYPE:
681 break;
683 default:
684 pp_unsupported_tree (pp, t);
685 break;
690 /* declarator:
691 pointer(opt) direct-declarator */
693 void
694 pp_c_declarator (c_pretty_printer *pp, tree t)
696 switch (TREE_CODE (t))
698 case INTEGER_TYPE:
699 case REAL_TYPE:
700 case FIXED_POINT_TYPE:
701 case ENUMERAL_TYPE:
702 case UNION_TYPE:
703 case RECORD_TYPE:
704 break;
706 case VAR_DECL:
707 case PARM_DECL:
708 case FIELD_DECL:
709 case ARRAY_TYPE:
710 case FUNCTION_TYPE:
711 case FUNCTION_DECL:
712 case TYPE_DECL:
713 pp_direct_declarator (pp, t);
714 break;
717 default:
718 pp_unsupported_tree (pp, t);
719 break;
723 /* declaration:
724 declaration-specifiers init-declarator-list(opt) ; */
726 void
727 pp_c_declaration (c_pretty_printer *pp, tree t)
729 pp_declaration_specifiers (pp, t);
730 pp_c_init_declarator (pp, t);
733 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
735 void
736 pp_c_attributes (c_pretty_printer *pp, tree attributes)
738 if (attributes == NULL_TREE)
739 return;
741 pp_c_ws_string (pp, "__attribute__");
742 pp_c_left_paren (pp);
743 pp_c_left_paren (pp);
744 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
746 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
747 if (TREE_VALUE (attributes))
748 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
750 if (TREE_CHAIN (attributes))
751 pp_separate_with (pp, ',');
753 pp_c_right_paren (pp);
754 pp_c_right_paren (pp);
757 /* function-definition:
758 declaration-specifiers declarator compound-statement */
760 void
761 pp_c_function_definition (c_pretty_printer *pp, tree t)
763 pp_declaration_specifiers (pp, t);
764 pp_declarator (pp, t);
765 pp_needs_newline (pp) = true;
766 pp_statement (pp, DECL_SAVED_TREE (t));
767 pp_newline (pp);
768 pp_flush (pp);
772 /* Expressions. */
774 /* Print out a c-char. This is called solely for characters which are
775 in the *target* execution character set. We ought to convert them
776 back to the *host* execution character set before printing, but we
777 have no way to do this at present. A decent compromise is to print
778 all characters as if they were in the host execution character set,
779 and not attempt to recover any named escape characters, but render
780 all unprintables as octal escapes. If the host and target character
781 sets are the same, this produces relatively readable output. If they
782 are not the same, strings may appear as gibberish, but that's okay
783 (in fact, it may well be what the reader wants, e.g. if they are looking
784 to see if conversion to the target character set happened correctly).
786 A special case: we need to prefix \, ", and ' with backslashes. It is
787 correct to do so for the *host*'s \, ", and ', because the rest of the
788 file appears in the host character set. */
790 static void
791 pp_c_char (c_pretty_printer *pp, int c)
793 if (ISPRINT (c))
795 switch (c)
797 case '\\': pp_string (pp, "\\\\"); break;
798 case '\'': pp_string (pp, "\\\'"); break;
799 case '\"': pp_string (pp, "\\\""); break;
800 default: pp_character (pp, c);
803 else
804 pp_scalar (pp, "\\%03o", (unsigned) c);
807 /* Print out a STRING literal. */
809 void
810 pp_c_string_literal (c_pretty_printer *pp, tree s)
812 const char *p = TREE_STRING_POINTER (s);
813 int n = TREE_STRING_LENGTH (s) - 1;
814 int i;
815 pp_doublequote (pp);
816 for (i = 0; i < n; ++i)
817 pp_c_char (pp, p[i]);
818 pp_doublequote (pp);
821 /* Pretty-print an INTEGER literal. */
823 static void
824 pp_c_integer_constant (c_pretty_printer *pp, tree i)
826 tree type = TREE_TYPE (i);
828 if (TREE_INT_CST_HIGH (i) == 0)
829 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
830 else
832 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
833 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
834 if (tree_int_cst_sgn (i) < 0)
836 pp_character (pp, '-');
837 high = ~high + !low;
838 low = -low;
840 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
841 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
842 pp_string (pp, pp_buffer (pp)->digit_buffer);
844 if (TYPE_UNSIGNED (type))
845 pp_character (pp, 'u');
846 if (type == long_integer_type_node || type == long_unsigned_type_node)
847 pp_character (pp, 'l');
848 else if (type == long_long_integer_type_node
849 || type == long_long_unsigned_type_node)
850 pp_string (pp, "ll");
853 /* Print out a CHARACTER literal. */
855 static void
856 pp_c_character_constant (c_pretty_printer *pp, tree c)
858 tree type = TREE_TYPE (c);
859 if (type == wchar_type_node)
860 pp_character (pp, 'L');
861 pp_quote (pp);
862 if (host_integerp (c, TYPE_UNSIGNED (type)))
863 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
864 else
865 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
866 pp_quote (pp);
869 /* Print out a BOOLEAN literal. */
871 static void
872 pp_c_bool_constant (c_pretty_printer *pp, tree b)
874 if (b == boolean_false_node)
876 if (c_dialect_cxx ())
877 pp_c_ws_string (pp, "false");
878 else if (flag_isoc99)
879 pp_c_ws_string (pp, "_False");
880 else
881 pp_unsupported_tree (pp, b);
883 else if (b == boolean_true_node)
885 if (c_dialect_cxx ())
886 pp_c_ws_string (pp, "true");
887 else if (flag_isoc99)
888 pp_c_ws_string (pp, "_True");
889 else
890 pp_unsupported_tree (pp, b);
892 else if (TREE_CODE (b) == INTEGER_CST)
893 pp_c_integer_constant (pp, b);
894 else
895 pp_unsupported_tree (pp, b);
898 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
899 false; that means the value was obtained by a cast, in which case
900 print out the type-id part of the cast-expression -- the casted value
901 is then printed by pp_c_integer_literal. */
903 static bool
904 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
906 bool value_is_named = true;
907 tree type = TREE_TYPE (e);
908 tree value;
910 /* Find the name of this constant. */
911 for (value = TYPE_VALUES (type);
912 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
913 value = TREE_CHAIN (value))
916 if (value != NULL_TREE)
917 pp_id_expression (pp, TREE_PURPOSE (value));
918 else
920 /* Value must have been cast. */
921 pp_c_type_cast (pp, type);
922 value_is_named = false;
925 return value_is_named;
928 /* Print out a REAL value as a decimal-floating-constant. */
930 static void
931 pp_c_floating_constant (c_pretty_printer *pp, tree r)
933 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
934 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
935 pp_string (pp, pp_buffer(pp)->digit_buffer);
936 if (TREE_TYPE (r) == float_type_node)
937 pp_character (pp, 'f');
938 else if (TREE_TYPE (r) == long_double_type_node)
939 pp_character (pp, 'l');
940 else if (TREE_TYPE (r) == dfloat128_type_node)
941 pp_string (pp, "dl");
942 else if (TREE_TYPE (r) == dfloat64_type_node)
943 pp_string (pp, "dd");
944 else if (TREE_TYPE (r) == dfloat32_type_node)
945 pp_string (pp, "df");
948 /* Print out a FIXED value as a decimal-floating-constant. */
950 static void
951 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
953 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
954 sizeof (pp_buffer (pp)->digit_buffer));
955 pp_string (pp, pp_buffer(pp)->digit_buffer);
958 /* Pretty-print a compound literal expression. GNU extensions include
959 vector constants. */
961 static void
962 pp_c_compound_literal (c_pretty_printer *pp, tree e)
964 tree type = TREE_TYPE (e);
965 pp_c_type_cast (pp, type);
967 switch (TREE_CODE (type))
969 case RECORD_TYPE:
970 case UNION_TYPE:
971 case ARRAY_TYPE:
972 case VECTOR_TYPE:
973 case COMPLEX_TYPE:
974 pp_c_brace_enclosed_initializer_list (pp, e);
975 break;
977 default:
978 pp_unsupported_tree (pp, e);
979 break;
983 /* Pretty-print a COMPLEX_EXPR expression. */
985 static void
986 pp_c_complex_expr (c_pretty_printer *pp, tree e)
988 /* Handle a few common special cases, otherwise fallback
989 to printing it as compound literal. */
990 tree type = TREE_TYPE (e);
991 tree realexpr = TREE_OPERAND (e, 0);
992 tree imagexpr = TREE_OPERAND (e, 1);
994 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
995 if (TREE_CODE (realexpr) == NOP_EXPR
996 && TREE_CODE (imagexpr) == NOP_EXPR
997 && TREE_TYPE (realexpr) == TREE_TYPE (type)
998 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
999 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1000 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1001 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1002 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1004 pp_c_type_cast (pp, type);
1005 pp_expression (pp, TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1006 return;
1009 /* Cast of an scalar expression to COMPLEX_TYPE. */
1010 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1011 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1013 pp_c_type_cast (pp, type);
1014 if (TREE_CODE (realexpr) == NOP_EXPR)
1015 realexpr = TREE_OPERAND (realexpr, 0);
1016 pp_expression (pp, realexpr);
1017 return;
1020 pp_c_compound_literal (pp, e);
1023 /* constant:
1024 integer-constant
1025 floating-constant
1026 fixed-point-constant
1027 enumeration-constant
1028 character-constant */
1030 void
1031 pp_c_constant (c_pretty_printer *pp, tree e)
1033 const enum tree_code code = TREE_CODE (e);
1035 switch (code)
1037 case INTEGER_CST:
1039 tree type = TREE_TYPE (e);
1040 if (type == boolean_type_node)
1041 pp_c_bool_constant (pp, e);
1042 else if (type == char_type_node)
1043 pp_c_character_constant (pp, e);
1044 else if (TREE_CODE (type) == ENUMERAL_TYPE
1045 && pp_c_enumeration_constant (pp, e))
1047 else
1048 pp_c_integer_constant (pp, e);
1050 break;
1052 case REAL_CST:
1053 pp_c_floating_constant (pp, e);
1054 break;
1056 case FIXED_CST:
1057 pp_c_fixed_constant (pp, e);
1058 break;
1060 case STRING_CST:
1061 pp_c_string_literal (pp, e);
1062 break;
1064 case COMPLEX_CST:
1065 /* Sometimes, we are confused and we think a complex literal
1066 is a constant. Such thing is a compound literal which
1067 grammatically belongs to postfix-expr production. */
1068 pp_c_compound_literal (pp, e);
1069 break;
1071 default:
1072 pp_unsupported_tree (pp, e);
1073 break;
1077 /* Pretty-print a string such as an identifier, without changing its
1078 encoding, preceded by whitespace is necessary. */
1080 void
1081 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1083 pp_c_maybe_whitespace (pp);
1084 pp_string (pp, str);
1085 pp_base (pp)->padding = pp_before;
1088 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1089 that need converting to the locale encoding, preceded by whitespace
1090 is necessary. */
1092 void
1093 pp_c_identifier (c_pretty_printer *pp, const char *id)
1095 pp_c_maybe_whitespace (pp);
1096 pp_identifier (pp, id);
1097 pp_base (pp)->padding = pp_before;
1100 /* Pretty-print a C primary-expression.
1101 primary-expression:
1102 identifier
1103 constant
1104 string-literal
1105 ( expression ) */
1107 void
1108 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1110 switch (TREE_CODE (e))
1112 case VAR_DECL:
1113 case PARM_DECL:
1114 case FIELD_DECL:
1115 case CONST_DECL:
1116 case FUNCTION_DECL:
1117 case LABEL_DECL:
1118 pp_c_tree_decl_identifier (pp, e);
1119 break;
1121 case IDENTIFIER_NODE:
1122 pp_c_tree_identifier (pp, e);
1123 break;
1125 case ERROR_MARK:
1126 pp_c_ws_string (pp, M_("<erroneous-expression>"));
1127 break;
1129 case RESULT_DECL:
1130 pp_c_ws_string (pp, M_("<return-value>"));
1131 break;
1133 case INTEGER_CST:
1134 case REAL_CST:
1135 case FIXED_CST:
1136 case STRING_CST:
1137 pp_c_constant (pp, e);
1138 break;
1140 case TARGET_EXPR:
1141 pp_c_ws_string (pp, "__builtin_memcpy");
1142 pp_c_left_paren (pp);
1143 pp_ampersand (pp);
1144 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1145 pp_separate_with (pp, ',');
1146 pp_ampersand (pp);
1147 pp_initializer (pp, TREE_OPERAND (e, 1));
1148 if (TREE_OPERAND (e, 2))
1150 pp_separate_with (pp, ',');
1151 pp_c_expression (pp, TREE_OPERAND (e, 2));
1153 pp_c_right_paren (pp);
1154 break;
1156 default:
1157 /* FIXME: Make sure we won't get into an infinite loop. */
1158 pp_c_left_paren (pp);
1159 pp_expression (pp, e);
1160 pp_c_right_paren (pp);
1161 break;
1165 /* Print out a C initializer -- also support C compound-literals.
1166 initializer:
1167 assignment-expression:
1168 { initializer-list }
1169 { initializer-list , } */
1171 static void
1172 pp_c_initializer (c_pretty_printer *pp, tree e)
1174 if (TREE_CODE (e) == CONSTRUCTOR)
1175 pp_c_brace_enclosed_initializer_list (pp, e);
1176 else
1177 pp_expression (pp, e);
1180 /* init-declarator:
1181 declarator:
1182 declarator = initializer */
1184 void
1185 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1187 pp_declarator (pp, t);
1188 /* We don't want to output function definitions here. There are handled
1189 elsewhere (and the syntactic form is bogus anyway). */
1190 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1192 tree init = DECL_INITIAL (t);
1193 /* This C++ bit is handled here because it is easier to do so.
1194 In templates, the C++ parser builds a TREE_LIST for a
1195 direct-initialization; the TREE_PURPOSE is the variable to
1196 initialize and the TREE_VALUE is the initializer. */
1197 if (TREE_CODE (init) == TREE_LIST)
1199 pp_c_left_paren (pp);
1200 pp_expression (pp, TREE_VALUE (init));
1201 pp_right_paren (pp);
1203 else
1205 pp_space (pp);
1206 pp_equal (pp);
1207 pp_space (pp);
1208 pp_c_initializer (pp, init);
1213 /* initializer-list:
1214 designation(opt) initializer
1215 initializer-list , designation(opt) initializer
1217 designation:
1218 designator-list =
1220 designator-list:
1221 designator
1222 designator-list designator
1224 designator:
1225 [ constant-expression ]
1226 identifier */
1228 static void
1229 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1231 tree type = TREE_TYPE (e);
1232 const enum tree_code code = TREE_CODE (type);
1234 if (TREE_CODE (e) == CONSTRUCTOR)
1236 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1237 return;
1240 switch (code)
1242 case RECORD_TYPE:
1243 case UNION_TYPE:
1244 case ARRAY_TYPE:
1246 tree init = TREE_OPERAND (e, 0);
1247 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1249 if (code == RECORD_TYPE || code == UNION_TYPE)
1251 pp_c_dot (pp);
1252 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1254 else
1256 pp_c_left_bracket (pp);
1257 if (TREE_PURPOSE (init))
1258 pp_c_constant (pp, TREE_PURPOSE (init));
1259 pp_c_right_bracket (pp);
1261 pp_c_whitespace (pp);
1262 pp_equal (pp);
1263 pp_c_whitespace (pp);
1264 pp_initializer (pp, TREE_VALUE (init));
1265 if (TREE_CHAIN (init))
1266 pp_separate_with (pp, ',');
1269 return;
1271 case VECTOR_TYPE:
1272 if (TREE_CODE (e) == VECTOR_CST)
1273 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1274 else
1275 break;
1276 return;
1278 case COMPLEX_TYPE:
1279 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1281 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1282 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1283 pp_separate_with (pp, ',');
1284 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1286 else
1287 break;
1288 return;
1290 default:
1291 break;
1294 pp_unsupported_tree (pp, type);
1297 /* Pretty-print a brace-enclosed initializer-list. */
1299 static void
1300 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1302 pp_c_left_brace (pp);
1303 pp_c_initializer_list (pp, l);
1304 pp_c_right_brace (pp);
1308 /* This is a convenient function, used to bridge gap between C and C++
1309 grammars.
1311 id-expression:
1312 identifier */
1314 void
1315 pp_c_id_expression (c_pretty_printer *pp, tree t)
1317 switch (TREE_CODE (t))
1319 case VAR_DECL:
1320 case PARM_DECL:
1321 case CONST_DECL:
1322 case TYPE_DECL:
1323 case FUNCTION_DECL:
1324 case FIELD_DECL:
1325 case LABEL_DECL:
1326 pp_c_tree_decl_identifier (pp, t);
1327 break;
1329 case IDENTIFIER_NODE:
1330 pp_c_tree_identifier (pp, t);
1331 break;
1333 default:
1334 pp_unsupported_tree (pp, t);
1335 break;
1339 /* postfix-expression:
1340 primary-expression
1341 postfix-expression [ expression ]
1342 postfix-expression ( argument-expression-list(opt) )
1343 postfix-expression . identifier
1344 postfix-expression -> identifier
1345 postfix-expression ++
1346 postfix-expression --
1347 ( type-name ) { initializer-list }
1348 ( type-name ) { initializer-list , } */
1350 void
1351 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1353 enum tree_code code = TREE_CODE (e);
1354 switch (code)
1356 case POSTINCREMENT_EXPR:
1357 case POSTDECREMENT_EXPR:
1358 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1359 pp_string (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1360 break;
1362 case ARRAY_REF:
1363 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1364 pp_c_left_bracket (pp);
1365 pp_expression (pp, TREE_OPERAND (e, 1));
1366 pp_c_right_bracket (pp);
1367 break;
1369 case CALL_EXPR:
1371 call_expr_arg_iterator iter;
1372 tree arg;
1373 pp_postfix_expression (pp, CALL_EXPR_FN (e));
1374 pp_c_left_paren (pp);
1375 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1377 pp_expression (pp, arg);
1378 if (more_call_expr_args_p (&iter))
1379 pp_separate_with (pp, ',');
1381 pp_c_right_paren (pp);
1382 break;
1385 case UNORDERED_EXPR:
1386 pp_c_ws_string (pp, flag_isoc99
1387 ? "isunordered"
1388 : "__builtin_isunordered");
1389 goto two_args_fun;
1391 case ORDERED_EXPR:
1392 pp_c_ws_string (pp, flag_isoc99
1393 ? "!isunordered"
1394 : "!__builtin_isunordered");
1395 goto two_args_fun;
1397 case UNLT_EXPR:
1398 pp_c_ws_string (pp, flag_isoc99
1399 ? "!isgreaterequal"
1400 : "!__builtin_isgreaterequal");
1401 goto two_args_fun;
1403 case UNLE_EXPR:
1404 pp_c_ws_string (pp, flag_isoc99
1405 ? "!isgreater"
1406 : "!__builtin_isgreater");
1407 goto two_args_fun;
1409 case UNGT_EXPR:
1410 pp_c_ws_string (pp, flag_isoc99
1411 ? "!islessequal"
1412 : "!__builtin_islessequal");
1413 goto two_args_fun;
1415 case UNGE_EXPR:
1416 pp_c_ws_string (pp, flag_isoc99
1417 ? "!isless"
1418 : "!__builtin_isless");
1419 goto two_args_fun;
1421 case UNEQ_EXPR:
1422 pp_c_ws_string (pp, flag_isoc99
1423 ? "!islessgreater"
1424 : "!__builtin_islessgreater");
1425 goto two_args_fun;
1427 case LTGT_EXPR:
1428 pp_c_ws_string (pp, flag_isoc99
1429 ? "islessgreater"
1430 : "__builtin_islessgreater");
1431 goto two_args_fun;
1433 two_args_fun:
1434 pp_c_left_paren (pp);
1435 pp_expression (pp, TREE_OPERAND (e, 0));
1436 pp_separate_with (pp, ',');
1437 pp_expression (pp, TREE_OPERAND (e, 1));
1438 pp_c_right_paren (pp);
1439 break;
1441 case ABS_EXPR:
1442 pp_c_ws_string (pp, "__builtin_abs");
1443 pp_c_left_paren (pp);
1444 pp_expression (pp, TREE_OPERAND (e, 0));
1445 pp_c_right_paren (pp);
1446 break;
1448 case COMPONENT_REF:
1450 tree object = TREE_OPERAND (e, 0);
1451 if (TREE_CODE (object) == INDIRECT_REF)
1453 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1454 pp_c_arrow (pp);
1456 else
1458 pp_postfix_expression (pp, object);
1459 pp_c_dot (pp);
1461 pp_expression (pp, TREE_OPERAND (e, 1));
1463 break;
1465 case BIT_FIELD_REF:
1467 tree type = TREE_TYPE (e);
1469 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1470 if (type
1471 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1473 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1474 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1475 if ((bitpos % size) == 0)
1477 pp_c_left_paren (pp);
1478 pp_c_left_paren (pp);
1479 pp_type_id (pp, type);
1480 pp_c_star (pp);
1481 pp_c_right_paren (pp);
1482 pp_c_ampersand (pp);
1483 pp_expression (pp, TREE_OPERAND (e, 0));
1484 pp_c_right_paren (pp);
1485 pp_c_left_bracket (pp);
1486 pp_wide_integer (pp, bitpos / size);
1487 pp_c_right_bracket (pp);
1488 break;
1491 pp_unsupported_tree (pp, e);
1493 break;
1495 case COMPLEX_CST:
1496 case VECTOR_CST:
1497 pp_c_compound_literal (pp, e);
1498 break;
1500 case COMPLEX_EXPR:
1501 pp_c_complex_expr (pp, e);
1502 break;
1504 case COMPOUND_LITERAL_EXPR:
1505 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1506 /* Fall through. */
1507 case CONSTRUCTOR:
1508 pp_initializer (pp, e);
1509 break;
1511 case VA_ARG_EXPR:
1512 pp_c_ws_string (pp, "__builtin_va_arg");
1513 pp_c_left_paren (pp);
1514 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1515 pp_separate_with (pp, ',');
1516 pp_type_id (pp, TREE_TYPE (e));
1517 pp_c_right_paren (pp);
1518 break;
1520 case ADDR_EXPR:
1521 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1523 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1524 break;
1526 /* else fall through. */
1528 default:
1529 pp_primary_expression (pp, e);
1530 break;
1534 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1536 void
1537 pp_c_expression_list (c_pretty_printer *pp, tree e)
1539 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1541 pp_expression (pp, TREE_VALUE (e));
1542 if (TREE_CHAIN (e))
1543 pp_separate_with (pp, ',');
1547 /* Print out V, which contains the elements of a constructor. */
1549 void
1550 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1552 unsigned HOST_WIDE_INT ix;
1553 tree value;
1555 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1557 pp_expression (pp, value);
1558 if (ix != VEC_length (constructor_elt, v) - 1)
1559 pp_separate_with (pp, ',');
1563 /* Print out an expression-list in parens, as if it were the argument
1564 list to a function. */
1566 void
1567 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1569 pp_c_left_paren (pp);
1570 if (t && TREE_CODE (t) == TREE_LIST)
1571 pp_c_expression_list (pp, t);
1572 pp_c_right_paren (pp);
1575 /* unary-expression:
1576 postfix-expression
1577 ++ cast-expression
1578 -- cast-expression
1579 unary-operator cast-expression
1580 sizeof unary-expression
1581 sizeof ( type-id )
1583 unary-operator: one of
1584 * & + - ! ~
1586 GNU extensions.
1587 unary-expression:
1588 __alignof__ unary-expression
1589 __alignof__ ( type-id )
1590 __real__ unary-expression
1591 __imag__ unary-expression */
1593 void
1594 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1596 enum tree_code code = TREE_CODE (e);
1597 switch (code)
1599 case PREINCREMENT_EXPR:
1600 case PREDECREMENT_EXPR:
1601 pp_string (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1602 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1603 break;
1605 case ADDR_EXPR:
1606 case INDIRECT_REF:
1607 case NEGATE_EXPR:
1608 case BIT_NOT_EXPR:
1609 case TRUTH_NOT_EXPR:
1610 case CONJ_EXPR:
1611 /* String literal are used by address. */
1612 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1613 pp_ampersand (pp);
1614 else if (code == INDIRECT_REF)
1615 pp_c_star (pp);
1616 else if (code == NEGATE_EXPR)
1617 pp_minus (pp);
1618 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1619 pp_complement (pp);
1620 else if (code == TRUTH_NOT_EXPR)
1621 pp_exclamation (pp);
1622 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1623 break;
1625 case REALPART_EXPR:
1626 case IMAGPART_EXPR:
1627 pp_c_ws_string (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1628 pp_c_whitespace (pp);
1629 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1630 break;
1632 default:
1633 pp_postfix_expression (pp, e);
1634 break;
1638 /* cast-expression:
1639 unary-expression
1640 ( type-name ) cast-expression */
1642 void
1643 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1645 switch (TREE_CODE (e))
1647 case FLOAT_EXPR:
1648 case FIX_TRUNC_EXPR:
1649 CASE_CONVERT:
1650 case VIEW_CONVERT_EXPR:
1651 pp_c_type_cast (pp, TREE_TYPE (e));
1652 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1653 break;
1655 default:
1656 pp_unary_expression (pp, e);
1660 /* multiplicative-expression:
1661 cast-expression
1662 multiplicative-expression * cast-expression
1663 multiplicative-expression / cast-expression
1664 multiplicative-expression % cast-expression */
1666 static void
1667 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1669 enum tree_code code = TREE_CODE (e);
1670 switch (code)
1672 case MULT_EXPR:
1673 case TRUNC_DIV_EXPR:
1674 case TRUNC_MOD_EXPR:
1675 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1676 pp_c_whitespace (pp);
1677 if (code == MULT_EXPR)
1678 pp_c_star (pp);
1679 else if (code == TRUNC_DIV_EXPR)
1680 pp_slash (pp);
1681 else
1682 pp_modulo (pp);
1683 pp_c_whitespace (pp);
1684 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1685 break;
1687 default:
1688 pp_c_cast_expression (pp, e);
1689 break;
1693 /* additive-expression:
1694 multiplicative-expression
1695 additive-expression + multiplicative-expression
1696 additive-expression - multiplicative-expression */
1698 static void
1699 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1701 enum tree_code code = TREE_CODE (e);
1702 switch (code)
1704 case POINTER_PLUS_EXPR:
1705 case PLUS_EXPR:
1706 case MINUS_EXPR:
1707 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1708 pp_c_whitespace (pp);
1709 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1710 pp_plus (pp);
1711 else
1712 pp_minus (pp);
1713 pp_c_whitespace (pp);
1714 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1715 break;
1717 default:
1718 pp_multiplicative_expression (pp, e);
1719 break;
1723 /* additive-expression:
1724 additive-expression
1725 shift-expression << additive-expression
1726 shift-expression >> additive-expression */
1728 static void
1729 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1731 enum tree_code code = TREE_CODE (e);
1732 switch (code)
1734 case LSHIFT_EXPR:
1735 case RSHIFT_EXPR:
1736 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1737 pp_c_whitespace (pp);
1738 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1739 pp_c_whitespace (pp);
1740 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1741 break;
1743 default:
1744 pp_c_additive_expression (pp, e);
1748 /* relational-expression:
1749 shift-expression
1750 relational-expression < shift-expression
1751 relational-expression > shift-expression
1752 relational-expression <= shift-expression
1753 relational-expression >= shift-expression */
1755 static void
1756 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1758 enum tree_code code = TREE_CODE (e);
1759 switch (code)
1761 case LT_EXPR:
1762 case GT_EXPR:
1763 case LE_EXPR:
1764 case GE_EXPR:
1765 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1766 pp_c_whitespace (pp);
1767 if (code == LT_EXPR)
1768 pp_less (pp);
1769 else if (code == GT_EXPR)
1770 pp_greater (pp);
1771 else if (code == LE_EXPR)
1772 pp_string (pp, "<=");
1773 else if (code == GE_EXPR)
1774 pp_string (pp, ">=");
1775 pp_c_whitespace (pp);
1776 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1777 break;
1779 default:
1780 pp_c_shift_expression (pp, e);
1781 break;
1785 /* equality-expression:
1786 relational-expression
1787 equality-expression == relational-expression
1788 equality-equality != relational-expression */
1790 static void
1791 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1793 enum tree_code code = TREE_CODE (e);
1794 switch (code)
1796 case EQ_EXPR:
1797 case NE_EXPR:
1798 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1799 pp_c_whitespace (pp);
1800 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1801 pp_c_whitespace (pp);
1802 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1803 break;
1805 default:
1806 pp_c_relational_expression (pp, e);
1807 break;
1811 /* AND-expression:
1812 equality-expression
1813 AND-expression & equality-equality */
1815 static void
1816 pp_c_and_expression (c_pretty_printer *pp, tree e)
1818 if (TREE_CODE (e) == BIT_AND_EXPR)
1820 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1821 pp_c_whitespace (pp);
1822 pp_ampersand (pp);
1823 pp_c_whitespace (pp);
1824 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1826 else
1827 pp_c_equality_expression (pp, e);
1830 /* exclusive-OR-expression:
1831 AND-expression
1832 exclusive-OR-expression ^ AND-expression */
1834 static void
1835 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1837 if (TREE_CODE (e) == BIT_XOR_EXPR
1838 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1840 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1841 if (TREE_CODE (e) == BIT_XOR_EXPR)
1842 pp_c_maybe_whitespace (pp);
1843 else
1844 pp_c_whitespace (pp);
1845 pp_carret (pp);
1846 pp_c_whitespace (pp);
1847 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1849 else
1850 pp_c_and_expression (pp, e);
1853 /* inclusive-OR-expression:
1854 exclusive-OR-expression
1855 inclusive-OR-expression | exclusive-OR-expression */
1857 static void
1858 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1860 if (TREE_CODE (e) == BIT_IOR_EXPR)
1862 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1863 pp_c_whitespace (pp);
1864 pp_bar (pp);
1865 pp_c_whitespace (pp);
1866 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1868 else
1869 pp_c_exclusive_or_expression (pp, e);
1872 /* logical-AND-expression:
1873 inclusive-OR-expression
1874 logical-AND-expression && inclusive-OR-expression */
1876 static void
1877 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1879 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
1880 || TREE_CODE (e) == TRUTH_AND_EXPR)
1882 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1883 pp_c_whitespace (pp);
1884 pp_string (pp, "&&");
1885 pp_c_whitespace (pp);
1886 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1888 else
1889 pp_c_inclusive_or_expression (pp, e);
1892 /* logical-OR-expression:
1893 logical-AND-expression
1894 logical-OR-expression || logical-AND-expression */
1896 void
1897 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1899 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
1900 || TREE_CODE (e) == TRUTH_OR_EXPR)
1902 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1903 pp_c_whitespace (pp);
1904 pp_string (pp, "||");
1905 pp_c_whitespace (pp);
1906 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1908 else
1909 pp_c_logical_and_expression (pp, e);
1912 /* conditional-expression:
1913 logical-OR-expression
1914 logical-OR-expression ? expression : conditional-expression */
1916 static void
1917 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1919 if (TREE_CODE (e) == COND_EXPR)
1921 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1922 pp_c_whitespace (pp);
1923 pp_question (pp);
1924 pp_c_whitespace (pp);
1925 pp_expression (pp, TREE_OPERAND (e, 1));
1926 pp_c_whitespace (pp);
1927 pp_colon (pp);
1928 pp_c_whitespace (pp);
1929 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1931 else
1932 pp_c_logical_or_expression (pp, e);
1936 /* assignment-expression:
1937 conditional-expression
1938 unary-expression assignment-operator assignment-expression
1940 assignment-expression: one of
1941 = *= /= %= += -= >>= <<= &= ^= |= */
1943 static void
1944 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1946 if (TREE_CODE (e) == MODIFY_EXPR
1947 || TREE_CODE (e) == INIT_EXPR)
1949 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1950 pp_c_whitespace (pp);
1951 pp_equal (pp);
1952 pp_space (pp);
1953 pp_c_expression (pp, TREE_OPERAND (e, 1));
1955 else
1956 pp_c_conditional_expression (pp, e);
1959 /* expression:
1960 assignment-expression
1961 expression , assignment-expression
1963 Implementation note: instead of going through the usual recursion
1964 chain, I take the liberty of dispatching nodes to the appropriate
1965 functions. This makes some redundancy, but it worths it. That also
1966 prevents a possible infinite recursion between pp_c_primary_expression ()
1967 and pp_c_expression (). */
1969 void
1970 pp_c_expression (c_pretty_printer *pp, tree e)
1972 switch (TREE_CODE (e))
1974 case INTEGER_CST:
1975 pp_c_integer_constant (pp, e);
1976 break;
1978 case REAL_CST:
1979 pp_c_floating_constant (pp, e);
1980 break;
1982 case FIXED_CST:
1983 pp_c_fixed_constant (pp, e);
1984 break;
1986 case STRING_CST:
1987 pp_c_string_literal (pp, e);
1988 break;
1990 case IDENTIFIER_NODE:
1991 case FUNCTION_DECL:
1992 case VAR_DECL:
1993 case CONST_DECL:
1994 case PARM_DECL:
1995 case RESULT_DECL:
1996 case FIELD_DECL:
1997 case LABEL_DECL:
1998 case ERROR_MARK:
1999 pp_primary_expression (pp, e);
2000 break;
2002 case POSTINCREMENT_EXPR:
2003 case POSTDECREMENT_EXPR:
2004 case ARRAY_REF:
2005 case CALL_EXPR:
2006 case COMPONENT_REF:
2007 case BIT_FIELD_REF:
2008 case COMPLEX_CST:
2009 case COMPLEX_EXPR:
2010 case VECTOR_CST:
2011 case ORDERED_EXPR:
2012 case UNORDERED_EXPR:
2013 case LTGT_EXPR:
2014 case UNEQ_EXPR:
2015 case UNLE_EXPR:
2016 case UNLT_EXPR:
2017 case UNGE_EXPR:
2018 case UNGT_EXPR:
2019 case ABS_EXPR:
2020 case CONSTRUCTOR:
2021 case COMPOUND_LITERAL_EXPR:
2022 case VA_ARG_EXPR:
2023 pp_postfix_expression (pp, e);
2024 break;
2026 case CONJ_EXPR:
2027 case ADDR_EXPR:
2028 case INDIRECT_REF:
2029 case NEGATE_EXPR:
2030 case BIT_NOT_EXPR:
2031 case TRUTH_NOT_EXPR:
2032 case PREINCREMENT_EXPR:
2033 case PREDECREMENT_EXPR:
2034 case REALPART_EXPR:
2035 case IMAGPART_EXPR:
2036 pp_c_unary_expression (pp, e);
2037 break;
2039 case FLOAT_EXPR:
2040 case FIX_TRUNC_EXPR:
2041 CASE_CONVERT:
2042 case VIEW_CONVERT_EXPR:
2043 pp_c_cast_expression (pp, e);
2044 break;
2046 case MULT_EXPR:
2047 case TRUNC_MOD_EXPR:
2048 case TRUNC_DIV_EXPR:
2049 pp_multiplicative_expression (pp, e);
2050 break;
2052 case LSHIFT_EXPR:
2053 case RSHIFT_EXPR:
2054 pp_c_shift_expression (pp, e);
2055 break;
2057 case LT_EXPR:
2058 case GT_EXPR:
2059 case LE_EXPR:
2060 case GE_EXPR:
2061 pp_c_relational_expression (pp, e);
2062 break;
2064 case BIT_AND_EXPR:
2065 pp_c_and_expression (pp, e);
2066 break;
2068 case BIT_XOR_EXPR:
2069 case TRUTH_XOR_EXPR:
2070 pp_c_exclusive_or_expression (pp, e);
2071 break;
2073 case BIT_IOR_EXPR:
2074 pp_c_inclusive_or_expression (pp, e);
2075 break;
2077 case TRUTH_ANDIF_EXPR:
2078 case TRUTH_AND_EXPR:
2079 pp_c_logical_and_expression (pp, e);
2080 break;
2082 case TRUTH_ORIF_EXPR:
2083 case TRUTH_OR_EXPR:
2084 pp_c_logical_or_expression (pp, e);
2085 break;
2087 case EQ_EXPR:
2088 case NE_EXPR:
2089 pp_c_equality_expression (pp, e);
2090 break;
2092 case COND_EXPR:
2093 pp_conditional_expression (pp, e);
2094 break;
2096 case POINTER_PLUS_EXPR:
2097 case PLUS_EXPR:
2098 case MINUS_EXPR:
2099 pp_c_additive_expression (pp, e);
2100 break;
2102 case MODIFY_EXPR:
2103 case INIT_EXPR:
2104 pp_assignment_expression (pp, e);
2105 break;
2107 case COMPOUND_EXPR:
2108 pp_c_left_paren (pp);
2109 pp_expression (pp, TREE_OPERAND (e, 0));
2110 pp_separate_with (pp, ',');
2111 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
2112 pp_c_right_paren (pp);
2113 break;
2115 case NON_LVALUE_EXPR:
2116 case SAVE_EXPR:
2117 pp_expression (pp, TREE_OPERAND (e, 0));
2118 break;
2120 case TARGET_EXPR:
2121 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
2122 break;
2124 case BIND_EXPR:
2125 case GOTO_EXPR:
2126 /* We don't yet have a way of dumping statements in a
2127 human-readable format. */
2128 pp_string (pp, "({...})");
2129 break;
2131 default:
2132 pp_unsupported_tree (pp, e);
2133 break;
2139 /* Statements. */
2141 void
2142 pp_c_statement (c_pretty_printer *pp, tree stmt)
2144 if (stmt == NULL)
2145 return;
2147 if (pp_needs_newline (pp))
2148 pp_newline_and_indent (pp, 0);
2150 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2154 /* Initialize the PRETTY-PRINTER for handling C codes. */
2156 void
2157 pp_c_pretty_printer_init (c_pretty_printer *pp)
2159 pp->offset_list = 0;
2161 pp->declaration = pp_c_declaration;
2162 pp->declaration_specifiers = pp_c_declaration_specifiers;
2163 pp->declarator = pp_c_declarator;
2164 pp->direct_declarator = pp_c_direct_declarator;
2165 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2166 pp->abstract_declarator = pp_c_abstract_declarator;
2167 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2168 pp->ptr_operator = pp_c_pointer;
2169 pp->parameter_list = pp_c_parameter_type_list;
2170 pp->type_id = pp_c_type_id;
2171 pp->simple_type_specifier = pp_c_type_specifier;
2172 pp->function_specifier = pp_c_function_specifier;
2173 pp->storage_class_specifier = pp_c_storage_class_specifier;
2175 pp->statement = pp_c_statement;
2177 pp->constant = pp_c_constant;
2178 pp->id_expression = pp_c_id_expression;
2179 pp->primary_expression = pp_c_primary_expression;
2180 pp->postfix_expression = pp_c_postfix_expression;
2181 pp->unary_expression = pp_c_unary_expression;
2182 pp->initializer = pp_c_initializer;
2183 pp->multiplicative_expression = pp_c_multiplicative_expression;
2184 pp->conditional_expression = pp_c_conditional_expression;
2185 pp->assignment_expression = pp_c_assignment_expression;
2186 pp->expression = pp_c_expression;
2190 /* Print the tree T in full, on file FILE. */
2192 void
2193 print_c_tree (FILE *file, tree t)
2195 static c_pretty_printer pp_rec;
2196 static bool initialized = 0;
2197 c_pretty_printer *pp = &pp_rec;
2199 if (!initialized)
2201 initialized = 1;
2202 pp_construct (pp_base (pp), NULL, 0);
2203 pp_c_pretty_printer_init (pp);
2204 pp_needs_newline (pp) = true;
2206 pp_base (pp)->buffer->stream = file;
2208 pp_statement (pp, t);
2210 pp_newline (pp);
2211 pp_flush (pp);
2214 /* Print the tree T in full, on stderr. */
2216 void
2217 debug_c_tree (tree t)
2219 print_c_tree (stderr, t);
2220 fputc ('\n', stderr);
2223 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2224 up of T's memory address. */
2226 void
2227 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2229 const char *name;
2231 gcc_assert (DECL_P (t));
2233 if (DECL_NAME (t))
2234 name = IDENTIFIER_POINTER (DECL_NAME (t));
2235 else
2237 static char xname[8];
2238 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2239 name = xname;
2242 pp_c_identifier (pp, name);